Merge master and multifarious hackery.
[dcpomatic.git] / src / lib / player.cc
index eb704f7332fe5ce90b515038ed0c177466d495fa..ff13f95dbd2197be7be681ff520b29ab1ef4422b 100644 (file)
 #include "playlist.h"
 #include "job.h"
 #include "image.h"
+#include "null_content.h"
+#include "black_decoder.h"
+#include "silence_decoder.h"
 
 using std::list;
 using std::cout;
 using std::min;
+using std::max;
 using std::vector;
 using boost::shared_ptr;
 using boost::weak_ptr;
@@ -56,11 +60,9 @@ Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
        , _video (true)
        , _audio (true)
        , _subtitles (true)
-       , _have_valid_decoders (false)
+       , _have_valid_pieces (false)
        , _position (0)
-       , _audio_buffers (MAX_AUDIO_CHANNELS, 0)
-       , _last_video (0)
-       , _last_was_black (false)
+       , _audio_buffers (f->dcp_audio_channels(), 0)
        , _next_audio (0)
 {
        _playlist->Changed.connect (bind (&Player::playlist_changed, this));
@@ -101,12 +103,18 @@ Player::pass ()
         shared_ptr<Piece> earliest;
 
        for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
-               if ((*i)->content->end(_film) < _position) {
+               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()) {
+                       continue;
+               }
+
+               if (!_audio && dynamic_pointer_cast<SndfileContent> ((*i)->content)) {
                        continue;
                }
                
                Time const t = (*i)->content->start() + (*i)->decoder->next();
                if (t < earliest_t) {
+                       cout << "\t candidate; " << t << " " << (t / TIME_HZ) << ".\n";
                        earliest_t = t;
                        earliest = *i;
                }
@@ -115,48 +123,61 @@ Player::pass ()
        if (!earliest) {
                return true;
        }
-       
-       earliest->decoder->pass ();
-
-       /* Move position to earliest active next emission */
-
-       for (list<shared_ptr<Piece> >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) {
-               if ((*i)->content->end(_film) < _position) {
-                       continue;
-               }
-
-               Time const t = (*i)->content->start() + (*i)->decoder->next();
 
-               if (t < _position) {
-                       _position = t;
-               }
+       cout << "PASS:\n";
+       cout << "\tpass " << earliest->content->file() << " ";
+       if (dynamic_pointer_cast<FFmpegContent> (earliest->content)) {
+               cout << " FFmpeg.\n";
+       } else if (dynamic_pointer_cast<ImageMagickContent> (earliest->content)) {
+               cout << " ImageMagickContent.\n";
+       } else if (dynamic_pointer_cast<SndfileContent> (earliest->content)) {
+               cout << " SndfileContent.\n";
+       } else if (dynamic_pointer_cast<BlackDecoder> (earliest->decoder)) {
+               cout << " Black.\n";
+       } else if (dynamic_pointer_cast<SilenceDecoder> (earliest->decoder)) {
+               cout << " Silence.\n";
        }
+       
+       earliest->decoder->pass ();
+       _position = earliest->content->start() + earliest->decoder->next ();
+       cout << "\tpassed to " << _position << " " << (_position / TIME_HZ) << "\n";
 
         return false;
 }
 
 void
-Player::process_video (shared_ptr<Piece> piece, 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, shared_ptr<Subtitle> sub, Time time)
 {
-       time += piece->start ();
+       cout << "[V]\n";
+       
+       shared_ptr<Content> content = weak_content.lock ();
+       if (!content) {
+               return;
+       }
+       
+       time += content->start ();
        
         Video (image, same, sub, time);
 }
 
 void
-Player::process_audio (shared_ptr<Piece> piece, shared_ptr<const AudioBuffers> audio, Time time)
+Player::process_audio (weak_ptr<Content> weak_content, shared_ptr<const AudioBuffers> audio, Time time)
 {
-        /* XXX: mapping */
-
+       shared_ptr<Content> content = weak_content.lock ();
+       if (!content) {
+               return;
+       }
+       
         /* The time of this audio may indicate that some of our buffered audio is not going to
            be added to any more, so it can be emitted.
         */
 
-       time += piece->start ();
+       time += content->start ();
 
         if (time > _next_audio) {
                 /* We can emit some audio from our buffers */
-                OutputAudioFrame const N = min (_film->time_to_audio_frames (time - _next_audio), static_cast<OutputAudioFrame> (_audio_buffers.frames()));
+               assert (_film->time_to_audio_frames (time - _next_audio) <= _audio_buffers.frames());
+                OutputAudioFrame const N = _film->time_to_audio_frames (time - _next_audio);
                 shared_ptr<AudioBuffers> emit (new AudioBuffers (_audio_buffers.channels(), N));
                 emit->copy_from (&_audio_buffers, N, 0, 0);
                 Audio (emit, _next_audio);
@@ -170,12 +191,12 @@ Player::process_audio (shared_ptr<Piece> piece, shared_ptr<const AudioBuffers> a
         }
 
         /* Now accumulate the new audio into our buffers */
-        _audio_buffers.ensure_size (time - _next_audio + audio->frames());
-        _audio_buffers.accumulate (audio.get(), 0, _film->time_to_audio_frames (time - _next_audio));
+        _audio_buffers.ensure_size (_audio_buffers.frames() + audio->frames());
+        _audio_buffers.accumulate_frames (audio.get(), 0, 0, audio->frames ());
 }
 
 /** @return true on error */
-bool
+void
 Player::seek (Time t)
 {
        if (!_have_valid_pieces) {
@@ -184,44 +205,54 @@ Player::seek (Time t)
        }
 
        if (_pieces.empty ()) {
-               return true;
+               return;
        }
 
-       /* XXX: don't seek audio because we don't need to... */
+       cout << "seek to " << t << " " << (t / TIME_HZ) << "\n";
 
-       return false;
+       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";
+               (*i)->decoder->seek (s);
+       }
+
+       /* XXX: don't seek audio because we don't need to... */
 }
 
 
 void
 Player::seek_back ()
 {
-       /* XXX */
+
 }
 
 void
 Player::seek_forward ()
 {
-       /* XXX */
+
 }
 
 struct ContentSorter
 {
        bool operator() (shared_ptr<Content> a, shared_ptr<Content> b)
        {
-               return a->time() < b->time();
+               return a->start() < b->start();
        }
 };
 
 void
-Player::setup_decoders ()
+Player::setup_pieces ()
 {
+//     cout << "----- Player SETUP PIECES.\n";
+
        list<shared_ptr<Piece> > old_pieces = _pieces;
 
        _pieces.clear ();
 
        Playlist::ContentList content = _playlist->content ();
-       content.sort (ContentSorter ());
+       sort (content.begin(), content.end(), ContentSorter ());
        
        for (Playlist::ContentList::iterator i = content.begin(); i != content.end(); ++i) {
 
@@ -233,10 +264,11 @@ Player::setup_decoders ()
                if (fc) {
                        shared_ptr<FFmpegDecoder> fd (new FFmpegDecoder (_film, fc, _video, _audio, _subtitles));
                        
-                       fd->Video.connect (bind (&Player::process_video, this, dr, _1, _2, _3, _4));
-                       fd->Audio.connect (bind (&Player::process_audio, this, dr, _1, _2));
+                       fd->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3, _4));
+                       fd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
 
                        decoder = fd;
+//                     cout << "\tFFmpeg @ " << fc->start() << " -- " << fc->end() << "\n";
                }
                
                shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
@@ -244,33 +276,32 @@ Player::setup_decoders ()
                        shared_ptr<ImageMagickDecoder> id;
                        
                        /* See if we can re-use an old ImageMagickDecoder */
-                       for (list<shared_ptr<Piece> >::const_iterator i = old_pieces.begin(); i != old_pieces.end(); ++i) {
-                               shared_ptr<ContentPiece> cp = dynamic_pointer_cast<ContentPiece> (*i);
-                               if (cp) {
-                                       shared_ptr<ImageMagickDecoder> imd = dynamic_pointer_cast<ImageMagickDecoder> (cp->decoder ());
-                                       if (imd && imd->content() == ic) {
-                                               id = imd;
-                                       }
+                       for (list<shared_ptr<Piece> >::const_iterator j = old_pieces.begin(); j != old_pieces.end(); ++j) {
+                               shared_ptr<ImageMagickDecoder> imd = dynamic_pointer_cast<ImageMagickDecoder> ((*j)->decoder);
+                               if (imd && imd->content() == ic) {
+                                       id = imd;
                                }
                        }
 
                        if (!id) {
                                id.reset (new ImageMagickDecoder (_film, ic));
-                               id->Video.connect (bind (&Player::process_video, this, dr, _1, _2, _3, _4));
+                               id->Video.connect (bind (&Player::process_video, this, *i, _1, _2, _3, _4));
                        }
 
                        decoder = id;
+//                     cout << "\tImageMagick @ " << ic->start() << " -- " << ic->end() << "\n";
                }
 
                shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
                if (sc) {
                        shared_ptr<AudioDecoder> sd (new SndfileDecoder (_film, sc));
-                       sd->Audio.connect (bind (&Player::process_audio, this, dr, _1, _2));
+                       sd->Audio.connect (bind (&Player::process_audio, this, *i, _1, _2));
 
                        decoder = sd;
+//                     cout << "\tSndfile @ " << sc->start() << " -- " << sc->end() << "\n";
                }
 
-               _pieces.push_back (shared_ptr<new ContentPiece> (*i, decoder));
+               _pieces.push_back (shared_ptr<Piece> (new Piece (*i, decoder)));
        }
 
        /* Fill in visual gaps with black and audio gaps with silence */
@@ -280,32 +311,28 @@ Player::setup_decoders ()
        list<shared_ptr<Piece> > pieces_copy = _pieces;
        for (list<shared_ptr<Piece> >::iterator i = pieces_copy.begin(); i != pieces_copy.end(); ++i) {
                if (dynamic_pointer_cast<VideoContent> ((*i)->content)) {
-                       Time const diff = video_pos - (*i)->content->time();
+                       Time const diff = (*i)->content->start() - video_pos;
                        if (diff > 0) {
-                               _pieces.push_back (
-                                       shared_ptr<Piece> (
-                                               shared_ptr<Content> (new NullContent (video_pos, diff)),
-                                               shared_ptr<Decoder> (new BlackDecoder (video_pos, diff))
-                                               )
-                                       );
+                               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";
                        }
                                                
-                       video_pos = (*i)->content->time() + (*i)->content->length();
+                       video_pos = (*i)->content->end();
                } else {
-                       Time const diff = audio_pos - (*i)->content->time();
+                       Time const diff = (*i)->content->start() - audio_pos;
                        if (diff > 0) {
-                               _pieces.push_back (
-                                       shared_ptr<Piece> (
-                                               shared_ptr<Content> (new NullContent (audio_pos, diff)),
-                                               shared_ptr<Decoder> (new SilenceDecoder (audio_pos, diff))
-                                               )
-                                       );
+                               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";
                        }
-                       audio_pos = (*i)->content->time() + (*i)->content->length();
+                       audio_pos = (*i)->content->end();
                }
        }
-
-       _position = 0;
 }
 
 void
@@ -316,7 +343,7 @@ Player::content_changed (weak_ptr<Content> w, int p)
                return;
        }
 
-       if (p == VideoContentProperty::VIDEO_LENGTH) {
+       if (p == ContentProperty::START || p == VideoContentProperty::VIDEO_LENGTH) {
                _have_valid_pieces = false;
        }
 }
@@ -326,26 +353,3 @@ Player::playlist_changed ()
 {
        _have_valid_pieces = false;
 }
-
-void
-Player::emit_black_frame ()
-{
-       shared_ptr<SimpleImage> image (new SimpleImage (AV_PIX_FMT_RGB24, libdcp::Size (128, 128), true));
-       Video (image, _last_was_black, shared_ptr<Subtitle> (), _last_video);
-       _last_video += _film->video_frames_to_time (1);
-}
-
-void
-Player::emit_silence (Time t)
-{
-       OutputAudioFrame frames = _film->time_to_audio_frames (t);
-       while (frames) {
-               /* Do this in half-second chunks so we don't overwhelm anybody */
-               OutputAudioFrame this_time = min (_film->dcp_audio_frame_rate() / 2, frames);
-               shared_ptr<AudioBuffers> silence (new AudioBuffers (MAX_AUDIO_CHANNELS, this_time));
-               silence->make_silent ();
-               Audio (silence, _last_audio);
-               _last_audio += _film->audio_frames_to_time (this_time);
-               frames -= this_time;
-       }
-}