Rename next -> position in decoders.
[dcpomatic.git] / src / lib / player.cc
index 032b3d49bcc897129d99d5ddd7b6c53448908880..60686e78178e00276513e248a51a97623c861764 100644 (file)
@@ -1,5 +1,3 @@
-/* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
-
 /*
     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
 
@@ -19,6 +17,7 @@
 
 */
 
+#include <stdint.h>
 #include "player.h"
 #include "film.h"
 #include "ffmpeg_decoder.h"
@@ -59,7 +58,6 @@ Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
        , _playlist (p)
        , _video (true)
        , _audio (true)
-       , _subtitles (true)
        , _have_valid_pieces (false)
        , _position (0)
        , _audio_buffers (f->dcp_audio_channels(), 0)
@@ -81,12 +79,6 @@ Player::disable_audio ()
        _audio = false;
 }
 
-void
-Player::disable_subtitles ()
-{
-       _subtitles = false;
-}
-
 bool
 Player::pass ()
 {
@@ -103,12 +95,20 @@ Player::pass ()
         shared_ptr<Piece> earliest;
 
        for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
-               cout << "check " << (*i)->content->file() << " start=" << (*i)->content->start() << ", next=" << (*i)->decoder->next() << ", end=" << (*i)->content->end() << "\n";
-               if (((*i)->decoder->next() + (*i)->content->start()) >= (*i)->content->end()) {
+               cout << "check " << (*i)->content->file()
+                    << " start=" << (*i)->content->start()
+                    << ", position=" << (*i)->decoder->position()
+                    << ", end=" << (*i)->content->end() << "\n";
+               
+               if ((*i)->decoder->done ()) {
+                       continue;
+               }
+
+               if (!_audio && dynamic_pointer_cast<AudioDecoder> ((*i)->decoder) && !dynamic_pointer_cast<VideoDecoder> ((*i)->decoder)) {
                        continue;
                }
                
-               Time const t = (*i)->content->start() + (*i)->decoder->next();
+               Time const t = (*i)->content->start() + (*i)->decoder->position();
                if (t < earliest_t) {
                        cout << "\t candidate; " << t << " " << (t / TIME_HZ) << ".\n";
                        earliest_t = t;
@@ -117,6 +117,7 @@ Player::pass ()
        }
 
        if (!earliest) {
+               flush ();
                return true;
        }
 
@@ -135,17 +136,15 @@ Player::pass ()
        }
        
        earliest->decoder->pass ();
-       _position = earliest->content->start() + earliest->decoder->next ();
+       _position = earliest->content->start() + earliest->decoder->position ();
        cout << "\tpassed to " << _position << " " << (_position / TIME_HZ) << "\n";
 
         return false;
 }
 
 void
-Player::process_video (weak_ptr<Content> weak_content, shared_ptr<const Image> image, bool same, shared_ptr<Subtitle> sub, Time time)
+Player::process_video (weak_ptr<Content> weak_content, shared_ptr<const Image> image, bool same, Time time)
 {
-       cout << "[V]\n";
-       
        shared_ptr<Content> content = weak_content.lock ();
        if (!content) {
                return;
@@ -153,7 +152,7 @@ Player::process_video (weak_ptr<Content> weak_content, shared_ptr<const Image> i
        
        time += content->start ();
        
-        Video (image, same, sub, time);
+        Video (image, same, time);
 }
 
 void
@@ -172,8 +171,8 @@ Player::process_audio (weak_ptr<Content> weak_content, shared_ptr<const AudioBuf
 
         if (time > _next_audio) {
                 /* We can emit some audio from our buffers */
-               assert (_film->time_to_audio_frames (time - _next_audio) <= _audio_buffers.frames());
                 OutputAudioFrame const N = _film->time_to_audio_frames (time - _next_audio);
+               assert (N <= _audio_buffers.frames());
                 shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), N));
                 emit->copy_from (&_audio_buffers, N, 0, 0);
                 Audio (emit, _next_audio);
@@ -188,7 +187,20 @@ Player::process_audio (weak_ptr<Content> weak_content, shared_ptr<const AudioBuf
 
         /* Now accumulate the new audio into our buffers */
         _audio_buffers.ensure_size (_audio_buffers.frames() + audio->frames());
-        _audio_buffers.accumulate_frames (audio, 0, 0, audio->frames ());
+        _audio_buffers.accumulate_frames (audio.get(), 0, 0, audio->frames ());
+       _audio_buffers.set_frames (_audio_buffers.frames() + audio->frames());
+}
+
+void
+Player::flush ()
+{
+       if (_audio_buffers.frames() > 0) {
+               shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), _audio_buffers.frames()));
+               emit->copy_from (&_audio_buffers, _audio_buffers.frames(), 0, 0);
+               Audio (emit, _next_audio);
+               _next_audio += _film->audio_frames_to_time (_audio_buffers.frames ());
+               _audio_buffers.set_frames (0);
+       }
 }
 
 /** @return true on error */
@@ -204,13 +216,13 @@ Player::seek (Time t)
                return;
        }
 
-       cout << "seek to " << t << " " << (t / TIME_HZ) << "\n";
+//     cout << "seek to " << t << " " << (t / TIME_HZ) << "\n";
 
        for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
                Time s = t - (*i)->content->start ();
                s = max (static_cast<Time> (0), s);
                s = min ((*i)->content->length(), s);
-               cout << "seek [" << (*i)->content->file() << "," << (*i)->content->start() << "," << (*i)->content->end() << "] to " << s << "\n";
+//             cout << "seek [" << (*i)->content->file() << "," << (*i)->content->start() << "," << (*i)->content->end() << "] to " << s << "\n";
                (*i)->decoder->seek (s);
        }
 
@@ -230,18 +242,32 @@ Player::seek_forward ()
 
 }
 
-struct ContentSorter
+void
+Player::add_black_piece (Time s, Time len)
 {
-       bool operator() (shared_ptr<Content> a, shared_ptr<Content> b)
-       {
-               return a->start() < b->start();
-       }
-};
+       shared_ptr<NullContent> nc (new NullContent (_film, s, len));
+       nc->set_ratio (_film->container ());
+       shared_ptr<BlackDecoder> bd (new BlackDecoder (_film, nc));
+       bd->Video.connect (bind (&Player::process_video, this, nc, _1, _2, _3));
+       _pieces.push_back (shared_ptr<Piece> (new Piece (nc, bd)));
+       cout << "\tblack @ " << s << " -- " << (s + len) << "\n";
+}
+
+void
+Player::add_silent_piece (Time s, Time len)
+{
+       shared_ptr<NullContent> nc (new NullContent (_film, s, len));
+       shared_ptr<SilenceDecoder> sd (new SilenceDecoder (_film, nc));
+       sd->Audio.connect (bind (&Player::process_audio, this, nc, _1, _2));
+       _pieces.push_back (shared_ptr<Piece> (new Piece (nc, sd)));
+       cout << "\tsilence @ " << s << " -- " << (s + len) << "\n";
+}
+
 
 void
 Player::setup_pieces ()
 {
-//     cout << "----- Player SETUP PIECES.\n";
+       cout << "----- Player SETUP PIECES.\n";
 
        list<shared_ptr<Piece> > old_pieces = _pieces;
 
@@ -258,13 +284,16 @@ Player::setup_pieces ()
 
                shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
                if (fc) {
-                       shared_ptr<FFmpegDecoder> fd (new FFmpegDecoder (_film, fc, _video, _audio, _subtitles));
+                       shared_ptr<FFmpegDecoder> fd (new FFmpegDecoder (_film, fc, _video, _audio));
                        
-                       fd->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3, _4));
+                       fd->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3));
                        fd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
+                       if (_video_container_size) {
+                               fd->set_video_container_size (_video_container_size.get ());
+                       }
 
                        decoder = fd;
-//                     cout << "\tFFmpeg @ " << fc->start() << " -- " << fc->end() << "\n";
+                       cout << "\tFFmpeg @ " << fc->start() << " -- " << fc->end() << "\n";
                }
                
                shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
@@ -281,11 +310,14 @@ Player::setup_pieces ()
 
                        if (!id) {
                                id.reset (new ImageMagickDecoder (_film, ic));
-                               id->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3, _4));
+                               id->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3));
+                               if (_video_container_size) {
+                                       id->set_video_container_size (_video_container_size.get ());
+                               }
                        }
 
                        decoder = id;
-//                     cout << "\tImageMagick @ " << ic->start() << " -- " << ic->end() << "\n";
+                       cout << "\tImageMagick @ " << ic->start() << " -- " << ic->end() << "\n";
                }
 
                shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
@@ -294,7 +326,7 @@ Player::setup_pieces ()
                        sd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
 
                        decoder = sd;
-//                     cout << "\tSndfile @ " << sc->start() << " -- " << sc->end() << "\n";
+                       cout << "\tSndfile @ " << sc->start() << " -- " << sc->end() << "\n";
                }
 
                _pieces.push_back (shared_ptr<Piece> (new Piece (*i, decoder)));
@@ -309,26 +341,26 @@ Player::setup_pieces ()
                if (dynamic_pointer_cast<VideoContent> ((*i)->content)) {
                        Time const diff = (*i)->content->start() - video_pos;
                        if (diff > 0) {
-                               shared_ptr<NullContent> nc (new NullContent (_film, video_pos, diff));
-                               shared_ptr<BlackDecoder> bd (new BlackDecoder (_film, nc));
-                               bd->Video.connect (bind (&Player::process_video, this, nc, _1, _2, _3, _4));
-                               _pieces.push_back (shared_ptr<Piece> (new Piece (nc, bd)));
-//                             cout << "\tblack @ " << video_pos << " -- " << (video_pos + diff) << "\n";
+                               add_black_piece (video_pos, diff);
                        }
-                                               
                        video_pos = (*i)->content->end();
-               } else {
+               }
+
+               shared_ptr<AudioContent> ac = dynamic_pointer_cast<AudioContent> ((*i)->content);
+               if (ac && ac->audio_channels()) {
                        Time const diff = (*i)->content->start() - audio_pos;
                        if (diff > 0) {
-                               shared_ptr<NullContent> nc (new NullContent (_film, audio_pos, diff));
-                               shared_ptr<SilenceDecoder> sd (new SilenceDecoder (_film, nc));
-                               sd->Audio.connect (bind (&Player::process_audio, this, nc, _1, _2));
-                               _pieces.push_back (shared_ptr<Piece> (new Piece (nc, sd)));
-//                             cout << "\tsilence @ " << audio_pos << " -- " << (audio_pos + diff) << "\n";
+                               add_silent_piece (video_pos, diff);
                        }
                        audio_pos = (*i)->content->end();
                }
        }
+
+       if (video_pos < audio_pos) {
+               add_black_piece (video_pos, audio_pos - video_pos);
+       } else if (audio_pos < video_pos) {
+               add_silent_piece (audio_pos, video_pos - audio_pos);
+       }
 }
 
 void
@@ -339,7 +371,7 @@ Player::content_changed (weak_ptr<Content> w, int p)
                return;
        }
 
-       if (p == ContentProperty::START || p == VideoContentProperty::VIDEO_LENGTH) {
+       if (p == ContentProperty::START || p == ContentProperty::LENGTH) {
                _have_valid_pieces = false;
        }
 }
@@ -349,3 +381,15 @@ Player::playlist_changed ()
 {
        _have_valid_pieces = false;
 }
+
+void
+Player::set_video_container_size (libdcp::Size s)
+{
+       _video_container_size = s;
+       for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
+               shared_ptr<VideoDecoder> vd = dynamic_pointer_cast<VideoDecoder> ((*i)->decoder);
+               if (vd) {
+                       vd->set_video_container_size (s);
+               }
+       }
+}