Add total-playlist loop option; move state of content list into the Playlist.
[dcpomatic.git] / src / lib / player.cc
index d1f04785115c05a15f6ba87bff5913e6dfb67bf1..d62d8f8b6408bf66b95ddb7eb08f1c51bea3aae0 100644 (file)
 #include "player.h"
 #include "film.h"
 #include "ffmpeg_decoder.h"
+#include "ffmpeg_content.h"
 #include "imagemagick_decoder.h"
+#include "imagemagick_content.h"
 #include "sndfile_decoder.h"
+#include "sndfile_content.h"
 #include "playlist.h"
 #include "job.h"
 
 using std::list;
 using std::cout;
+using std::vector;
 using boost::shared_ptr;
 using boost::weak_ptr;
 using boost::dynamic_pointer_cast;
@@ -38,8 +42,6 @@ Player::Player (shared_ptr<const Film> f, shared_ptr<const Playlist> p)
        , _audio (true)
        , _subtitles (true)
        , _have_valid_decoders (false)
-       , _ffmpeg_decoder_done (false)
-       , _video_sync (true)
 {
        _playlist->Changed.connect (bind (&Player::playlist_changed, this));
        _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2));
@@ -73,29 +75,48 @@ Player::pass ()
        
        bool done = true;
        
-       if (_playlist->video_from() == Playlist::VIDEO_FFMPEG || _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
-               if (!_ffmpeg_decoder_done) {
-                       if (_ffmpeg_decoder->pass ()) {
-                               _ffmpeg_decoder_done = true;
-                       } else {
-                               done = false;
-                       }
+       if (_video && _video_decoder < _video_decoders.size ()) {
+
+               /* Run video decoder; this may also produce audio */
+               
+               if (_video_decoders[_video_decoder]->pass ()) {
+                       _video_decoder++;
                }
+               
+               if (_video_decoder < _video_decoders.size ()) {
+                       done = false;
+               }
+               
        }
 
-       if (_playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
-               if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
-                       if ((*_imagemagick_decoder)->pass ()) {
-                               _imagemagick_decoder++;
-                       }
+       if (!_video && _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG && _sequential_audio_decoder < _audio_decoders.size ()) {
+
+               /* We're not producing video, so we may need to run FFmpeg content to get the audio */
+               
+               if (_audio_decoders[_sequential_audio_decoder]->pass ()) {
+                       _sequential_audio_decoder++;
+               }
+               
+               if (_sequential_audio_decoder < _audio_decoders.size ()) {
+                       done = false;
+               }
+               
+       }
 
-                       if (_imagemagick_decoder != _imagemagick_decoders.end ()) {
+       if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
+               
+               /* We're getting audio from SndfileContent */
+               
+               for (vector<shared_ptr<AudioDecoder> >::iterator i = _audio_decoders.begin(); i != _audio_decoders.end(); ++i) {
+                       if (!(*i)->pass ()) {
                                done = false;
                        }
                }
-       }
 
-       /* XXX: sndfile */
+               Audio (_audio_buffers, _audio_time.get());
+               _audio_buffers.reset ();
+               _audio_time = boost::none;
+       }
 
        return done;
 }
@@ -104,183 +125,181 @@ void
 Player::set_progress (shared_ptr<Job> job)
 {
        /* Assume progress can be divined from how far through the video we are */
-       switch (_playlist->video_from ()) {
-       case Playlist::VIDEO_NONE:
-               break;
-       case Playlist::VIDEO_FFMPEG:
-               if (_playlist->video_length ()) {
-                       job->set_progress (float(_ffmpeg_decoder->video_frame()) / _playlist->video_length ());
-               }
-               break;
-       case Playlist::VIDEO_IMAGEMAGICK:
-       {
-               int n = 0;
-               for (list<shared_ptr<ImageMagickDecoder> >::iterator i = _imagemagick_decoders.begin(); i != _imagemagick_decoders.end(); ++i) {
-                       if (_imagemagick_decoder == i) {
-                               job->set_progress (float (n) / _imagemagick_decoders.size ());
-                       }
-                       ++n;
-               }
-               break;
-       }
+
+       if (_video_decoder >= _video_decoders.size() || !_playlist->video_length()) {
+               return;
        }
-}
 
-void
-Player::process_video (shared_ptr<Image> i, bool same, shared_ptr<Subtitle> s)
-{
-       Video (i, same, s);
+       job->set_progress ((_video_start[_video_decoder] + _video_decoders[_video_decoder]->video_frame()) / _playlist->video_length ());
 }
 
 void
-Player::process_audio (shared_ptr<AudioBuffers> b)
+Player::process_video (shared_ptr<const Image> i, bool same, shared_ptr<Subtitle> s, double t)
 {
-       Audio (b);
+       Video (i, same, s, _video_start[_video_decoder] + t);
 }
 
-/** @return true on error */
-bool
-Player::seek (double t)
+void
+Player::process_audio (weak_ptr<const AudioContent> c, shared_ptr<const AudioBuffers> b, double t)
 {
-       if (!_have_valid_decoders) {
-               setup_decoders ();
-               _have_valid_decoders = true;
-       }
-       
-       bool r = false;
-
-       switch (_playlist->video_from()) {
-       case Playlist::VIDEO_NONE:
-               break;
-       case Playlist::VIDEO_FFMPEG:
-               if (!_ffmpeg_decoder || _ffmpeg_decoder->seek (t)) {
-                       r = true;
-               }
-               /* We're seeking, so all `all done' bets are off */
-               _ffmpeg_decoder_done = false;
-               break;
-       case Playlist::VIDEO_IMAGEMAGICK:
-               /* Find the decoder that contains this position */
-               _imagemagick_decoder = _imagemagick_decoders.begin ();
-               while (_imagemagick_decoder != _imagemagick_decoders.end ()) {
-                       double const this_length = (*_imagemagick_decoder)->video_length() / _film->video_frame_rate ();
-                       if (t < this_length) {
-                               break;
-                       }
-                       t -= this_length;
-                       ++_imagemagick_decoder;
+       AudioMapping mapping = _film->audio_mapping ();
+       if (!_audio_buffers) {
+               _audio_buffers.reset (new AudioBuffers (mapping.dcp_channels(), b->frames ()));
+               _audio_buffers->make_silent ();
+               _audio_time = t;
+               if (_playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
+                       _audio_time = _audio_time.get() + _audio_start[_sequential_audio_decoder];
                }
+       }
 
-               if (_imagemagick_decoder != _imagemagick_decoders.end()) {
-                       (*_imagemagick_decoder)->seek (t);
-               } else {
-                       r = true;
+       for (int i = 0; i < b->channels(); ++i) {
+               list<libdcp::Channel> dcp = mapping.content_to_dcp (AudioMapping::Channel (c, i));
+               for (list<libdcp::Channel>::iterator j = dcp.begin(); j != dcp.end(); ++j) {
+                       _audio_buffers->accumulate (b, i, static_cast<int> (*j));
                }
-               break;
        }
 
-       /* XXX: don't seek audio because we don't need to... */
-
-       return r;
+       if (_playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
+               /* We can just emit this audio now as it will all be here */
+               Audio (_audio_buffers, t);
+               _audio_buffers.reset ();
+               _audio_time = boost::none;
+       }
 }
 
+/** @return true on error */
 bool
-Player::seek_to_last ()
+Player::seek (double t)
 {
        if (!_have_valid_decoders) {
                setup_decoders ();
                _have_valid_decoders = true;
        }
 
-       bool r = false;
-       
-       switch (_playlist->video_from ()) {
-       case Playlist::VIDEO_NONE:
-               break;
-       case Playlist::VIDEO_FFMPEG:
-               if (!_ffmpeg_decoder || _ffmpeg_decoder->seek_to_last ()) {
-                       r = true;
-               }
+       if (_video_decoders.empty ()) {
+               return true;
+       }
 
-               /* We're seeking, so all `all done' bets are off */
-               _ffmpeg_decoder_done = false;
-               break;
-       case Playlist::VIDEO_IMAGEMAGICK:
-               if ((*_imagemagick_decoder)->seek_to_last ()) {
-                       r = true;
+       /* Find the decoder that contains this position */
+       _video_decoder = 0;
+       while (1) {
+               ++_video_decoder;
+               if (_video_decoder >= _video_decoders.size () || t < _video_start[_video_decoder]) {
+                       --_video_decoder;
+                       t -= _video_start[_video_decoder];
+                       break;
                }
-               break;
+       }
+
+       if (_video_decoder < _video_decoders.size()) {
+               _video_decoders[_video_decoder]->seek (t);
+       } else {
+               return true;
        }
 
        /* XXX: don't seek audio because we don't need to... */
 
-       return r;
+       return false;
 }
 
+
 void
-Player::setup_decoders ()
+Player::seek_back ()
 {
-       if ((_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) || (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG)) {
-               _ffmpeg_decoder.reset (
-                       new FFmpegDecoder (
-                               _film,
-                               _playlist->ffmpeg(),
-                               _video && _playlist->video_from() == Playlist::VIDEO_FFMPEG,
-                               _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
-                               _subtitles && _film->with_subtitles(),
-                               _video_sync
-                               )
-                       );
-       }
-       
-       if (_video && _playlist->video_from() == Playlist::VIDEO_FFMPEG) {
-               _ffmpeg_decoder->connect_video (shared_from_this ());
-       }
-
-       if (_audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
-               _ffmpeg_decoder->connect_audio (shared_from_this ());
-       }
-
-       if (_video && _playlist->video_from() == Playlist::VIDEO_IMAGEMAGICK) {
-               list<shared_ptr<const ImageMagickContent> > ic = _playlist->imagemagick ();
-               for (list<shared_ptr<const ImageMagickContent> >::iterator i = ic.begin(); i != ic.end(); ++i) {
-                       shared_ptr<ImageMagickDecoder> d (new ImageMagickDecoder (_film, *i));
-                       _imagemagick_decoders.push_back (d);
-                       d->connect_video (shared_from_this ());
-               }
-
-               _imagemagick_decoder = _imagemagick_decoders.begin ();
-       }
+       /* XXX */
+}
 
-       if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
-               list<shared_ptr<const SndfileContent> > sc = _playlist->sndfile ();
-               for (list<shared_ptr<const SndfileContent> >::iterator i = sc.begin(); i != sc.end(); ++i) {
-                       shared_ptr<SndfileDecoder> d (new SndfileDecoder (_film, *i));
-                       _sndfile_decoders.push_back (d);
-                       d->connect_audio (shared_from_this ());
-               }
-       }
+void
+Player::seek_forward ()
+{
+       /* XXX */
 }
 
+
 void
-Player::disable_video_sync ()
+Player::setup_decoders ()
 {
-       _video_sync = false;
+       _video_decoders.clear ();
+       _video_decoder = 0;
+       _audio_decoders.clear ();
+       _sequential_audio_decoder = 0;
+
+       _video_start.clear();
+       _audio_start.clear();
+
+       double video_so_far = 0;
+       double audio_so_far = 0;
+
+       for (int l = 0; l < _playlist->loop(); ++l) {
+               list<shared_ptr<const VideoContent> > vc = _playlist->video ();
+               for (list<shared_ptr<const VideoContent> >::iterator i = vc.begin(); i != vc.end(); ++i) {
+                       
+                       shared_ptr<const VideoContent> video_content;
+                       shared_ptr<const AudioContent> audio_content;
+                       shared_ptr<VideoDecoder> video_decoder;
+                       shared_ptr<AudioDecoder> audio_decoder;
+                       
+                       /* XXX: into content? */
+                       
+                       shared_ptr<const FFmpegContent> fc = dynamic_pointer_cast<const FFmpegContent> (*i);
+                       if (fc) {
+                               shared_ptr<FFmpegDecoder> fd (
+                                       new FFmpegDecoder (
+                                               _film, fc, _video,
+                                               _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG,
+                                               _subtitles
+                                               )
+                                       );
+                               
+                               video_content = fc;
+                               audio_content = fc;
+                               video_decoder = fd;
+                               audio_decoder = fd;
+                       }
+                       
+                       shared_ptr<const ImageMagickContent> ic = dynamic_pointer_cast<const ImageMagickContent> (*i);
+                       if (ic) {
+                               video_content = ic;
+                               video_decoder.reset (new ImageMagickDecoder (_film, ic));
+                       }
+                       
+                       video_decoder->connect_video (shared_from_this ());
+                       _video_decoders.push_back (video_decoder);
+                       _video_start.push_back (video_so_far);
+                       video_so_far += video_content->video_length() / video_content->video_frame_rate();
+                       
+                       if (audio_decoder && _playlist->audio_from() == Playlist::AUDIO_FFMPEG) {
+                               audio_decoder->Audio.connect (bind (&Player::process_audio, this, audio_content, _1, _2));
+                               _audio_decoders.push_back (audio_decoder);
+                               _audio_start.push_back (audio_so_far);
+                               audio_so_far += double(audio_content->audio_length()) / audio_content->audio_frame_rate();
+                       }
+               }
+               
+               _video_decoder = 0;
+               _sequential_audio_decoder = 0;
+               
+               if (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) {
+                       
+                       list<shared_ptr<const AudioContent> > ac = _playlist->audio ();
+                       for (list<shared_ptr<const AudioContent> >::iterator i = ac.begin(); i != ac.end(); ++i) {
+                               
+                               shared_ptr<const SndfileContent> sc = dynamic_pointer_cast<const SndfileContent> (*i);
+                               assert (sc);
+                               
+                               shared_ptr<AudioDecoder> d (new SndfileDecoder (_film, sc));
+                               d->Audio.connect (bind (&Player::process_audio, this, sc, _1, _2));
+                               _audio_decoders.push_back (d);
+                               _audio_start.push_back (audio_so_far);
+                       }
+               }
+       }
 }
 
 double
 Player::last_video_time () const
 {
-       switch (_playlist->video_from ()) {
-       case Playlist::VIDEO_NONE:
-               return 0;
-       case Playlist::VIDEO_FFMPEG:
-               return _ffmpeg_decoder->last_source_time ();
-       case Playlist::VIDEO_IMAGEMAGICK:
-               return (*_imagemagick_decoder)->last_source_time ();
-       }
-
-       return 0;
+       return _video_start[_video_decoder] + _video_decoders[_video_decoder]->last_content_time ();
 }
 
 void
@@ -292,9 +311,7 @@ Player::content_changed (weak_ptr<Content> w, int p)
        }
 
        if (p == VideoContentProperty::VIDEO_LENGTH) {
-               if (dynamic_pointer_cast<FFmpegContent> (c)) {
-                       _have_valid_decoders = false;
-               }
+               _have_valid_decoders = false;
        }
 }