From 43d540854fe75e01292891a299f852407ff54d65 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Sun, 23 Apr 2017 21:23:21 +0100 Subject: [PATCH] Discard audio received before the time of the last accurate seek. --- src/lib/player.cc | 42 ++++++++++++++++++++++++++++++------------ src/lib/player.h | 1 + 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/lib/player.cc b/src/lib/player.cc index 5fb349b0e..e59451abe 100644 --- a/src/lib/player.cc +++ b/src/lib/player.cc @@ -585,12 +585,20 @@ Player::pass () list, DCPTime> > audio = _audio_merger.pull (pull_from); for (list, DCPTime> >::iterator i = audio.begin(); i != audio.end(); ++i) { if (_last_audio_time && i->second < _last_audio_time.get()) { - cout << "FAIL " << to_string(i->second) << " " << to_string(_last_audio_time.get()) << "\n"; + /* There has been an accurate seek and we have received some audio before the seek time; + discard it. + */ + pair, DCPTime> cut = discard_audio (i->first, i->second, *_last_audio_time); + if (!cut.first) { + continue; + } + *i = cut; } - DCPOMATIC_ASSERT (!_last_audio_time || i->second >= _last_audio_time.get()); + if (_last_audio_time) { fill_audio (DCPTimePeriod (_last_audio_time.get(), i->second)); } + Audio (i->first, i->second); _last_audio_time = i->second + DCPTime::from_frames(i->first->frames(), _film->audio_frame_rate()); } @@ -795,17 +803,13 @@ Player::audio (weak_ptr wp, AudioStreamPtr stream, ContentAudio content_a /* Remove anything that comes before the start or after the end of the content */ if (time < piece->content->position()) { - DCPTime const discard_time = piece->content->position() - time; - Frame discard_frames = discard_time.frames_round(_film->audio_frame_rate()); - Frame remaining_frames = content_audio.audio->frames() - discard_frames; - if (remaining_frames <= 0) { + pair, DCPTime> cut = discard_audio (content_audio.audio, time, piece->content->position()); + if (!cut.first) { /* This audio is entirely discarded */ return; } - shared_ptr cut (new AudioBuffers (content_audio.audio->channels(), remaining_frames)); - cut->copy_from (content_audio.audio.get(), remaining_frames, discard_frames, 0); - content_audio.audio = cut; - time += discard_time; + content_audio.audio = cut.first; + time = cut.second; } else if (time > piece->content->end()) { /* Discard it all */ return; @@ -907,6 +911,8 @@ Player::seek (DCPTime time, bool accurate) i->second->reset (); } + _audio_merger.clear (); + BOOST_FOREACH (shared_ptr i, _pieces) { i->done = false; if (i->content->position() <= time && time < i->content->end()) { @@ -917,11 +923,9 @@ Player::seek (DCPTime time, bool accurate) if (accurate) { _last_video_time = time - one_video_frame (); _last_audio_time = time; - cout << "_last_audio_time -> " << to_string(time) << "\n"; } else { _last_video_time = optional (); _last_audio_time = optional (); - cout << "_last_audio_time -> []\n"; } } @@ -990,3 +994,17 @@ Player::one_video_frame () const { return DCPTime::from_frames (1, _film->video_frame_rate ()); } + +pair, DCPTime> +Player::discard_audio (shared_ptr audio, DCPTime time, DCPTime discard_to) const +{ + DCPTime const discard_time = discard_to - time; + Frame const discard_frames = discard_time.frames_round(_film->audio_frame_rate()); + Frame remaining_frames = audio->frames() - discard_frames; + if (remaining_frames <= 0) { + return make_pair(shared_ptr(), DCPTime()); + } + shared_ptr cut (new AudioBuffers (audio->channels(), remaining_frames)); + cut->copy_from (audio.get(), remaining_frames, discard_frames, 0); + return make_pair(cut, time + discard_time); +} diff --git a/src/lib/player.h b/src/lib/player.h index ea07e1fc8..dec4529ba 100644 --- a/src/lib/player.h +++ b/src/lib/player.h @@ -112,6 +112,7 @@ private: void fill_audio (DCPTimePeriod period); void audio_flush (boost::shared_ptr, AudioStreamPtr stream); void audio_transform (boost::shared_ptr content, AudioStreamPtr stream, ContentAudio content_audio, DCPTime time); + std::pair, DCPTime> discard_audio (boost::shared_ptr audio, DCPTime time, DCPTime discard_to) const; boost::shared_ptr _film; boost::shared_ptr _playlist; -- 2.30.2