X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fplayer.cc;h=09f1f55a32cdc3a909448d37642278d59b5961ea;hb=f861018389acd9d277fe34d7621182b9b54f977f;hp=c77059b0ab546ce5fdb503fa12b6becce155e3e1;hpb=e234776046e3c735c85063e1c5a7691093a4a722;p=dcpomatic.git diff --git a/src/lib/player.cc b/src/lib/player.cc index c77059b0a..09f1f55a3 100644 --- a/src/lib/player.cc +++ b/src/lib/player.cc @@ -30,6 +30,7 @@ using std::list; using std::cout; +using std::vector; using boost::shared_ptr; using boost::weak_ptr; using boost::dynamic_pointer_cast; @@ -41,7 +42,6 @@ Player::Player (shared_ptr f, shared_ptr p) , _audio (true) , _subtitles (true) , _have_valid_decoders (false) - , _video_sync (true) { _playlist->Changed.connect (bind (&Player::playlist_changed, this)); _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2)); @@ -75,25 +75,47 @@ Player::pass () bool done = true; - if (_video_decoder != _video_decoders.end ()) { - if ((*_video_decoder)->pass ()) { + 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.end ()) { + if (_video_decoder < _video_decoders.size ()) { done = false; } + } - if (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) { - for (list >::iterator i = _sndfile_decoders.begin(); i != _sndfile_decoders.end(); ++i) { + 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 (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) { + + /* We're getting audio from SndfileContent */ + + for (vector >::iterator i = _audio_decoders.begin(); i != _audio_decoders.end(); ++i) { if (!(*i)->pass ()) { done = false; } } - Audio (_audio_buffers); + Audio (_audio_buffers, _audio_time.get()); _audio_buffers.reset (); + _audio_time = boost::none; } return done; @@ -104,32 +126,30 @@ Player::set_progress (shared_ptr job) { /* Assume progress can be divined from how far through the video we are */ - if (_video_decoder == _video_decoders.end() || !_playlist->video_length()) { + if (_video_decoder >= _video_decoders.size() || !_playlist->video_length()) { return; } - - ContentVideoFrame p = 0; - list >::iterator i = _video_decoders.begin (); - while (i != _video_decoders.end() && i != _video_decoder) { - p += (*i)->video_length (); - } - job->set_progress (float ((*_video_decoder)->video_frame ()) / _playlist->video_length ()); + job->set_progress ((_video_start[_video_decoder] + _video_decoders[_video_decoder]->video_frame()) / _playlist->video_length ()); } void -Player::process_video (shared_ptr i, bool same, shared_ptr s) +Player::process_video (shared_ptr i, bool same, shared_ptr s, double t) { - Video (i, same, s); + Video (i, same, s, _video_start[_video_decoder] + t); } void -Player::process_audio (weak_ptr c, shared_ptr b) +Player::process_audio (weak_ptr c, shared_ptr b, double t) { 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]; + } } for (int i = 0; i < b->channels(); ++i) { @@ -141,8 +161,9 @@ Player::process_audio (weak_ptr c, shared_ptr if (_playlist->audio_from() == Playlist::AUDIO_FFMPEG) { /* We can just emit this audio now as it will all be here */ - Audio (_audio_buffers); + Audio (_audio_buffers, t); _audio_buffers.reset (); + _audio_time = boost::none; } } @@ -155,19 +176,23 @@ Player::seek (double t) _have_valid_decoders = true; } + if (_video_decoders.empty ()) { + return true; + } + /* Find the decoder that contains this position */ - _video_decoder = _video_decoders.begin (); - while (_video_decoder != _video_decoders.end ()) { - double const this_length = double ((*_video_decoder)->video_length()) / _film->video_frame_rate (); - if (t < this_length) { + _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; } - t -= this_length; - ++_video_decoder; } - - if (_video_decoder != _video_decoders.end()) { - (*_video_decoder)->seek (t); + + if (_video_decoder < _video_decoders.size()) { + _video_decoders[_video_decoder]->seek (t); } else { return true; } @@ -177,76 +202,102 @@ Player::seek (double t) return false; } + +void +Player::seek_back () +{ + /* XXX */ +} + +void +Player::seek_forward () +{ + /* XXX */ +} + + void Player::setup_decoders () { _video_decoders.clear (); - _video_decoder = _video_decoders.end (); - _sndfile_decoders.clear (); - - if (_video) { - list > vc = _playlist->video (); - for (list >::iterator i = vc.begin(); i != vc.end(); ++i) { - - shared_ptr d; - - /* XXX: into content? */ - - shared_ptr fc = dynamic_pointer_cast (*i); - if (fc) { - shared_ptr fd ( - new FFmpegDecoder ( - _film, fc, _video, - _audio && _playlist->audio_from() == Playlist::AUDIO_FFMPEG, - _subtitles, - _video_sync - ) - ); - - if (_playlist->audio_from() == Playlist::AUDIO_FFMPEG) { - fd->Audio.connect (bind (&Player::process_audio, this, fc, _1)); - } - - d = fd; - } + _video_decoder = 0; + _audio_decoders.clear (); + _sequential_audio_decoder = 0; - shared_ptr ic = dynamic_pointer_cast (*i); - if (ic) { - d.reset (new ImageMagickDecoder (_film, ic)); - } + _video_start.clear(); + _audio_start.clear(); - d->connect_video (shared_from_this ()); - _video_decoders.push_back (d); + double video_so_far = 0; + double audio_so_far = 0; + + list > vc = _playlist->video (); + for (list >::iterator i = vc.begin(); i != vc.end(); ++i) { + + shared_ptr video_content; + shared_ptr audio_content; + shared_ptr video_decoder; + shared_ptr audio_decoder; + + /* XXX: into content? */ + + shared_ptr fc = dynamic_pointer_cast (*i); + if (fc) { + shared_ptr 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 ic = dynamic_pointer_cast (*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 = _video_decoders.begin (); } + + _video_decoder = 0; + _sequential_audio_decoder = 0; - if (_audio && _playlist->audio_from() == Playlist::AUDIO_SNDFILE) { - list > sc = _playlist->sndfile (); - for (list >::iterator i = sc.begin(); i != sc.end(); ++i) { - shared_ptr d (new SndfileDecoder (_film, *i)); - _sndfile_decoders.push_back (d); - d->Audio.connect (bind (&Player::process_audio, this, *i, _1)); + if (_playlist->audio_from() == Playlist::AUDIO_SNDFILE) { + + list > ac = _playlist->audio (); + for (list >::iterator i = ac.begin(); i != ac.end(); ++i) { + + shared_ptr sc = dynamic_pointer_cast (*i); + assert (sc); + + shared_ptr 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); } } } -void -Player::disable_video_sync () -{ - _video_sync = false; -} - double Player::last_video_time () const { - double t = 0; - for (list >::const_iterator i = _video_decoders.begin(); i != _video_decoder; ++i) { - t += (*i)->video_length() / (*i)->video_frame_rate (); - } - - return t + (*_video_decoder)->last_content_time (); + return _video_start[_video_decoder] + _video_decoders[_video_decoder]->last_content_time (); } void