X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fplayer.cc;h=85b4cbd4f77a1c016605bee7274545c7a9d520af;hb=a183c1776cfd020a37d028ebb0f641352f49697b;hp=5d4635e5a865b0a7334e5381f50f59f5aa873ddd;hpb=237a0052c60af768f4d62b00321932918b7ba4d9;p=dcpomatic.git diff --git a/src/lib/player.cc b/src/lib/player.cc index 5d4635e5a..85b4cbd4f 100644 --- a/src/lib/player.cc +++ b/src/lib/player.cc @@ -1,5 +1,3 @@ -/* -*- c-basic-offset: 8; default-tab-width: 8; -*- */ - /* Copyright (C) 2013 Carl Hetherington @@ -19,6 +17,7 @@ */ +#include #include "player.h" #include "film.h" #include "ffmpeg_decoder.h" @@ -30,27 +29,40 @@ #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 (MAX_AUDIO_CHANNELS, 0) - , _last_video (0) - , _last_was_black (false) - , _last_audio (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)); @@ -77,82 +89,101 @@ 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; } /* Here we are just finding the active decoder with the earliest last emission time, then - calling pass on it. If there is no decoder, we skip our position on until there is. - Hence this method will cause video and audio to be emitted, and it is up to the - process_{video,audio} methods to tidy it up. + calling pass on it. */ - Time earliest_pos = TIME_MAX; - shared_ptr earliest; - Time next_wait = TIME_MAX; - - for (list >::iterator i = _decoders.begin(); i != _decoders.end(); ++i) { - Time const ts = (*i)->content->time(); - Time const te = (*i)->content->time() + (*i)->content->length (_film); - if (ts <= _position && te > _position) { - Time const pos = ts + (*i)->last; - if (pos < earliest_pos) { - earliest_pos = pos; - earliest = *i; - } - } + Time earliest_t = TIME_MAX; + shared_ptr earliest; - if (ts > _position) { - next_wait = min (next_wait, ts - _position); - } - } + 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 ((*i)->decoder->done ()) { + continue; + } - if (earliest) { - earliest->decoder->pass (); - _position = earliest->last; - } else if (next_wait < TIME_MAX) { - _position += next_wait; - } else { - return true; - } + 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 (!earliest) { + flush (); + return true; + } + + 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 false; } void -Player::process_video (shared_ptr dr, shared_ptr image, bool same, shared_ptr sub, Time time) +Player::process_video (weak_ptr weak_content, shared_ptr image, bool same, Time time) { - shared_ptr vd = dynamic_pointer_cast (dr->decoder); - - Time const global_time = dr->content->time() + time; - while ((global_time - _last_video) > 1) { - /* Fill in with black */ - emit_black_frame (); - } - - Video (image, same, sub, global_time); - dr->last = time; - _last_video = global_time; - _last_was_black = false; + shared_ptr content = weak_content.lock (); + if (!content) { + return; + } + + time += content->start (); + + Video (image, same, time); } void -Player::process_audio (shared_ptr dr, shared_ptr audio, Time time) +Player::process_audio (weak_ptr weak_content, shared_ptr audio, Time time) { - /* XXX: mapping */ - + 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. */ - if (time > _last_audio) { + 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 - _last_audio), static_cast (_audio_buffers.frames())); + 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, _last_audio); - _last_audio += _film->audio_frames_to_time (N); + Audio (emit, _next_audio); + _next_audio += _film->audio_frames_to_time (N); /* And remove it from our buffers */ if (_audio_buffers.frames() > N) { @@ -162,65 +193,99 @@ Player::process_audio (shared_ptr dr, shared_ptrframes()); + _audio_buffers.accumulate_frames (audio.get(), 0, 0, audio->frames ()); + _audio_buffers.set_frames (_audio_buffers.frames() + audio->frames()); +} - if (_audio_buffers.frames() == 0) { - /* We have no remaining data. Emit silence up to the start of this new data */ - if ((time - _last_audio) > 0) { - emit_silence (time - _last_audio); - } - } - - _audio_buffers.ensure_size (time - _last_audio + audio->frames()); - _audio_buffers.accumulate (audio.get(), 0, _film->time_to_audio_frames (time - _last_audio)); - dr->last = time + _film->audio_frames_to_time (audio->frames ()); +void +Player::flush () +{ + 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 +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; } - if (_decoders.empty ()) { - return true; + if (_pieces.empty ()) { + return; } - /* XXX */ +// cout << "seek to " << t << " " << (t / TIME_HZ) << "\n"; - /* XXX: don't seek audio because we don't need to... */ + for (list >::iterator i = _pieces.begin(); i != _pieces.end(); ++i) { + Time s = t - (*i)->content->start (); + s = max (static_cast