X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fplayer.cc;h=85b4cbd4f77a1c016605bee7274545c7a9d520af;hb=a183c1776cfd020a37d028ebb0f641352f49697b;hp=4caddb7e61032ca4eae0a8591b8fa16eb382e6c5;hpb=18593159bef6174dc65520df1c4b871e13baf1f7;p=dcpomatic.git diff --git a/src/lib/player.cc b/src/lib/player.cc index 4caddb7e6..85b4cbd4f 100644 --- a/src/lib/player.cc +++ b/src/lib/player.cc @@ -17,6 +17,7 @@ */ +#include #include "player.h" #include "film.h" #include "ffmpeg_decoder.h" @@ -27,21 +28,41 @@ #include "sndfile_content.h" #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; using boost::dynamic_pointer_cast; +struct Piece +{ + Piece (shared_ptr c, shared_ptr d) + : content (c) + , decoder (d) + {} + + shared_ptr content; + shared_ptr decoder; +}; + Player::Player (shared_ptr f, shared_ptr p) : _film (f) , _playlist (p) , _video (true) , _audio (true) , _subtitles (true) - , _have_valid_decoders (false) + , _have_valid_pieces (false) + , _position (0) + , _audio_buffers (f->dcp_audio_channels(), 0) + , _next_audio (0) { _playlist->Changed.connect (bind (&Player::playlist_changed, this)); _playlist->ContentChanged.connect (bind (&Player::content_changed, this, _1, _2)); @@ -68,232 +89,279 @@ Player::disable_subtitles () bool Player::pass () { - if (!_have_valid_decoders) { - setup_decoders (); - _have_valid_decoders = true; + if (!_have_valid_pieces) { + setup_pieces (); + _have_valid_pieces = true; } - - bool done = true; - - 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; - } - - } + /* Here we are just finding the active decoder with the earliest last emission time, then + calling pass on it. + */ - if (!_video && _playlist->audio_from() == Playlist::AUDIO_FFMPEG && _sequential_audio_decoder < _audio_decoders.size ()) { + Time earliest_t = TIME_MAX; + shared_ptr earliest; - /* We're not producing video, so we may need to run FFmpeg content to get the audio */ + for (list >::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 (_audio_decoders[_sequential_audio_decoder]->pass ()) { - _sequential_audio_decoder++; + if ((*i)->decoder->done ()) { + continue; } - - if (_sequential_audio_decoder < _audio_decoders.size ()) { - done = false; + + if (!_audio && dynamic_pointer_cast ((*i)->decoder) && !dynamic_pointer_cast ((*i)->decoder)) { + 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; + } } - if (_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; - } - } + if (!earliest) { + flush (); + return true; + } - Audio (_audio_buffers, _audio_time.get()); - _audio_buffers.reset (); - _audio_time = boost::none; + cout << "PASS:\n"; + cout << "\tpass " << earliest->content->file() << " "; + if (dynamic_pointer_cast (earliest->content)) { + cout << " FFmpeg.\n"; + } else if (dynamic_pointer_cast (earliest->content)) { + cout << " ImageMagickContent.\n"; + } else if (dynamic_pointer_cast (earliest->content)) { + cout << " SndfileContent.\n"; + } else if (dynamic_pointer_cast (earliest->decoder)) { + cout << " Black.\n"; + } else if (dynamic_pointer_cast (earliest->decoder)) { + cout << " Silence.\n"; } + + earliest->decoder->pass (); + _position = earliest->content->start() + earliest->decoder->next (); + cout << "\tpassed to " << _position << " " << (_position / TIME_HZ) << "\n"; - return done; + return false; } void -Player::set_progress (shared_ptr job) +Player::process_video (weak_ptr weak_content, shared_ptr image, bool same, Time time) { - /* Assume progress can be divined from how far through the video we are */ - - if (_video_decoder >= _video_decoders.size() || !_playlist->video_length()) { + shared_ptr content = weak_content.lock (); + if (!content) { return; } - - job->set_progress ((_video_start[_video_decoder] + _video_decoders[_video_decoder]->video_frame()) / _playlist->video_length ()); + + time += content->start (); + + Video (image, same, time); } void -Player::process_video (shared_ptr i, bool same, shared_ptr s, double t) +Player::process_audio (weak_ptr weak_content, shared_ptr audio, Time time) { - Video (i, same, s, _video_start[_video_decoder] + t); + shared_ptr 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 += content->start (); + + if (time > _next_audio) { + /* We can emit some audio from our buffers */ + OutputAudioFrame const N = _film->time_to_audio_frames (time - _next_audio); + assert (N <= _audio_buffers.frames()); + shared_ptr emit (new AudioBuffers (_audio_buffers.channels(), N)); + emit->copy_from (&_audio_buffers, N, 0, 0); + Audio (emit, _next_audio); + _next_audio += _film->audio_frames_to_time (N); + + /* And remove it from our buffers */ + if (_audio_buffers.frames() > N) { + _audio_buffers.move (N, 0, _audio_buffers.frames() - N); + } + _audio_buffers.set_frames (_audio_buffers.frames() - N); + } + + /* Now accumulate the new audio into our buffers */ + _audio_buffers.ensure_size (_audio_buffers.frames() + audio->frames()); + _audio_buffers.accumulate_frames (audio.get(), 0, 0, audio->frames ()); + _audio_buffers.set_frames (_audio_buffers.frames() + audio->frames()); } void -Player::process_audio (weak_ptr c, shared_ptr b, double t) +Player::flush () { - 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) { - list dcp = mapping.content_to_dcp (AudioMapping::Channel (c, i)); - for (list::iterator j = dcp.begin(); j != dcp.end(); ++j) { - _audio_buffers->accumulate (b, i, static_cast (*j)); - } - } - - 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; + if (_audio_buffers.frames() > 0) { + shared_ptr 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 */ -bool -Player::seek (double t) +void +Player::seek (Time t) { - if (!_have_valid_decoders) { - setup_decoders (); - _have_valid_decoders = true; + if (!_have_valid_pieces) { + setup_pieces (); + _have_valid_pieces = 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; - } + if (_pieces.empty ()) { + return; } - if (_video_decoder < _video_decoders.size()) { - _video_decoders[_video_decoder]->seek (t); - } else { - return true; +// cout << "seek to " << t << " " << (t / TIME_HZ) << "\n"; + + for (list >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) { + Time s = t - (*i)->content->start (); + s = max (static_cast