Chromium Code Reviews| Index: media/base/android/media_codec_decoder.cc |
| diff --git a/media/base/android/media_codec_decoder.cc b/media/base/android/media_codec_decoder.cc |
| index e6300b2f6a6927b396c118d542d52f69e4543c0a..a6731dd8c81497a1c93961d6424180ee7a8a47f4 100644 |
| --- a/media/base/android/media_codec_decoder.cc |
| +++ b/media/base/android/media_codec_decoder.cc |
| @@ -35,18 +35,22 @@ MediaCodecDecoder::MediaCodecDecoder( |
| const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
| const base::Closure& external_request_data_cb, |
| const base::Closure& starvation_cb, |
| + const base::Closure& decoder_drained_cb, |
| const base::Closure& stop_done_cb, |
| const base::Closure& error_cb, |
| const char* decoder_thread_name) |
| : media_task_runner_(media_task_runner), |
| decoder_thread_(decoder_thread_name), |
| needs_reconfigure_(false), |
| + drain_decoder_(false), |
| + always_reconfigure_for_tests_(false), |
| external_request_data_cb_(external_request_data_cb), |
| starvation_cb_(starvation_cb), |
| + decoder_drained_cb_(decoder_drained_cb), |
| stop_done_cb_(stop_done_cb), |
| error_cb_(error_cb), |
| state_(kStopped), |
| - needs_preroll_(true), |
| + preroll_mode_(kPrerollTillPTS), |
| eos_enqueued_(false), |
| completed_(false), |
| last_frame_posted_(false), |
| @@ -88,6 +92,7 @@ void MediaCodecDecoder::Flush() { |
| eos_enqueued_ = false; |
| completed_ = false; |
| + drain_decoder_ = false; |
| au_queue_.Flush(); |
| #ifndef NDEBUG |
| @@ -99,7 +104,7 @@ void MediaCodecDecoder::Flush() { |
| verify_next_frame_is_key_ = true; |
| #endif |
| - needs_preroll_ = true; |
| + preroll_mode_ = kPrerollTillPTS; |
| if (media_codec_bridge_) { |
| // MediaCodecBridge::Reset() performs MediaCodecBridge.flush() |
| @@ -118,7 +123,7 @@ void MediaCodecDecoder::ReleaseMediaCodec() { |
| DVLOG(1) << class_name() << "::" << __FUNCTION__; |
| media_codec_bridge_.reset(); |
| - needs_preroll_ = true; |
| + preroll_mode_ = kPrerollTillPTS; |
| } |
| bool MediaCodecDecoder::IsPrefetchingOrPlaying() const { |
| @@ -158,7 +163,14 @@ bool MediaCodecDecoder::IsCompleted() const { |
| bool MediaCodecDecoder::NotCompletedAndNeedsPreroll() const { |
| DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| - return HasStream() && needs_preroll_ && !completed_; |
| + return HasStream() && preroll_mode_ != kNoPreroll && !completed_; |
| +} |
| + |
| +void MediaCodecDecoder::SetDecodingUntilOutputIsPresent() { |
| + DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| + DVLOG(1) << class_name() << "::" << __FUNCTION__; |
| + |
| + preroll_mode_ = kPrerollTillOutputIsPresent; |
| } |
| base::android::ScopedJavaLocalRef<jobject> MediaCodecDecoder::GetMediaCrypto() { |
| @@ -201,24 +213,31 @@ MediaCodecDecoder::ConfigStatus MediaCodecDecoder::Configure() { |
| ReleaseMediaCodec(); |
| } |
| - MediaCodecDecoder::ConfigStatus result; |
| if (media_codec_bridge_) { |
| DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| << ": reconfiguration is not required, ignoring"; |
| - result = kConfigOk; |
| - } else { |
| - result = ConfigureInternal(); |
| + return kConfigOk; |
| + } |
| + |
| + // Read all |kConfigChanged| units preceding the data one. |
| + AccessUnitQueue::Info au_info = au_queue_.GetInfo(); |
| + while (au_info.configs) { |
| + SetDemuxerConfigs(*au_info.configs); |
| + au_queue_.Advance(); |
| + au_info = au_queue_.GetInfo(); |
| + } |
| + |
| + MediaCodecDecoder::ConfigStatus result = ConfigureInternal(); |
| #ifndef NDEBUG |
| - // We check and reset |verify_next_frame_is_key_| on Decoder thread. |
| - // This DCHECK ensures we won't need to lock this variable. |
| - DCHECK(!decoder_thread_.IsRunning()); |
| + // We check and reset |verify_next_frame_is_key_| on Decoder thread. |
| + // This DCHECK ensures we won't need to lock this variable. |
| + DCHECK(!decoder_thread_.IsRunning()); |
| - // For video the first frame after reconfiguration must be key frame. |
| - if (result == kConfigOk) |
| - verify_next_frame_is_key_ = true; |
| + // For video the first frame after reconfiguration must be key frame. |
| + if (result == kConfigOk) |
| + verify_next_frame_is_key_ = true; |
| #endif |
| - } |
| return result; |
| } |
| @@ -244,13 +263,15 @@ bool MediaCodecDecoder::Preroll(base::TimeDelta preroll_timestamp, |
| } |
| DCHECK(!decoder_thread_.IsRunning()); |
| - DCHECK(needs_preroll_); |
| + DCHECK(preroll_mode_ != kNoPreroll); |
| preroll_done_cb_ = preroll_done_cb; |
| // We only synchronize video stream. |
| DissociatePTSFromTime(); // associaton will happen after preroll is done. |
| - preroll_timestamp_ = preroll_timestamp; |
| + |
| + preroll_timestamp_ = (preroll_mode_ == kPrerollTillPTS) ? preroll_timestamp |
| + : base::TimeDelta(); |
| last_frame_posted_ = false; |
| @@ -356,7 +377,7 @@ void MediaCodecDecoder::RequestToStop() { |
| case kStopped: |
| case kPrefetching: |
| case kPrefetched: |
| - // There is nothing to wait for, we can sent nofigication right away. |
| + // There is nothing to wait for, we can sent notification right away. |
| DCHECK(!decoder_thread_.IsRunning()); |
| SetState(kStopped); |
| media_task_runner_->PostTask(FROM_HERE, stop_done_cb_); |
| @@ -367,20 +388,29 @@ void MediaCodecDecoder::RequestToStop() { |
| } |
| } |
| -void MediaCodecDecoder::OnLastFrameRendered(bool completed) { |
| +void MediaCodecDecoder::OnLastFrameRendered(bool eos_encountered) { |
| DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| - DVLOG(0) << class_name() << "::" << __FUNCTION__ |
| - << " completed:" << completed; |
| + DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| + << " eos_encountered:" << eos_encountered; |
| decoder_thread_.Stop(); // synchronous |
| SetState(kStopped); |
| - completed_ = completed; |
| + completed_ = (eos_encountered && !drain_decoder_); |
| - if (completed_ && !preroll_done_cb_.is_null()) |
| + if (completed_ && !preroll_done_cb_.is_null()) { |
| media_task_runner_->PostTask(FROM_HERE, |
| base::ResetAndReturn(&preroll_done_cb_)); |
| + } |
| + |
| + if (eos_encountered && drain_decoder_) { |
| + ReleaseMediaCodec(); |
| + drain_decoder_ = false; |
| + eos_enqueued_ = false; |
| + preroll_mode_ = kPrerollTillOutputIsPresent; |
| + media_task_runner_->PostTask(FROM_HERE, decoder_drained_cb_); |
| + } |
| media_task_runner_->PostTask(FROM_HERE, stop_done_cb_); |
| } |
| @@ -390,7 +420,7 @@ void MediaCodecDecoder::OnPrerollDone() { |
| DVLOG(1) << class_name() << "::" << __FUNCTION__; |
| - needs_preroll_ = false; |
| + preroll_mode_ = kNoPreroll; |
| // The state might be kStopping. |
| if (GetState() == kPrerolling) { |
| @@ -437,6 +467,16 @@ bool MediaCodecDecoder::IsPrerollingForTests() const { |
| return GetState() == kPrerolling; |
| } |
| +void MediaCodecDecoder::SetAlwaysReconfigureForTests() { |
| + // UI task runner. |
| + always_reconfigure_for_tests_ = true; |
| +} |
| + |
| +void MediaCodecDecoder::SetCodecCreatedCallbackForTests(base::Closure cb) { |
| + // UI task runner. |
| + codec_created_for_tests_cb_ = cb; |
| +} |
| + |
| int MediaCodecDecoder::NumDelayedRenderTasks() const { |
| return 0; |
| } |
| @@ -501,7 +541,8 @@ void MediaCodecDecoder::PrefetchNextChunk() { |
| AccessUnitQueue::Info au_info = au_queue_.GetInfo(); |
| - if (eos_enqueued_ || au_info.length >= kPrefetchLimit || au_info.has_eos) { |
| + if (eos_enqueued_ || au_info.data_length >= kPrefetchLimit || |
| + au_info.has_eos) { |
| // We are done prefetching |
| SetState(kPrefetched); |
| DVLOG(1) << class_name() << "::" << __FUNCTION__ << " posting PrefetchDone"; |
| @@ -580,39 +621,32 @@ bool MediaCodecDecoder::EnqueueInputBuffer() { |
| return true; // Nothing to do |
| } |
| - // Get the next frame from the queue and the queue info |
| - |
| - AccessUnitQueue::Info au_info = au_queue_.GetInfo(); |
| + // Get the next frame from the queue. As we go, request more data and |
| + // consume |kConfigChanged| units. |
| - // Request the data from Demuxer |
| - if (au_info.length <= kPlaybackLowLimit && !au_info.has_eos) |
| - media_task_runner_->PostTask(FROM_HERE, request_data_cb_); |
| - |
| - // Get the next frame from the queue |
| - |
| - if (!au_info.length) { |
| - // Report starvation and return, Start() will be called again later. |
| - DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": starvation detected"; |
| - media_task_runner_->PostTask(FROM_HERE, starvation_cb_); |
| - return true; |
| - } |
| + // |drain_decoder_| can be already set here if we could not dequeue the input |
| + // buffer for it right away. |
| - if (au_info.configs) { |
| - DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| - << ": received new configs, not implemented"; |
| - // post an error for now? |
| - media_task_runner_->PostTask(FROM_HERE, internal_error_cb_); |
| - return false; |
| - } |
| + AccessUnitQueue::Info au_info; |
| + if (!drain_decoder_) { |
| + au_info = AdvanceAccessUnitQueue(&drain_decoder_); |
| + if (!au_info.length) { |
| + // Report starvation and return, Start() will be called again later. |
| + DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| + << ": starvation detected"; |
|
liberato (no reviews please)
2015/08/28 21:42:47
might want to mention |drain_decoder_| in the log.
Tima Vaisburd
2015/08/28 22:28:55
Added the words about the drain inside AdvanceAcce
|
| + media_task_runner_->PostTask(FROM_HERE, starvation_cb_); |
| + return true; |
| + } |
| - // We are ready to enqueue the front unit. |
| + DCHECK(au_info.front_unit); |
| #ifndef NDEBUG |
| - if (verify_next_frame_is_key_) { |
| - verify_next_frame_is_key_ = false; |
| - VerifyUnitIsKeyFrame(au_info.front_unit); |
| - } |
| + if (verify_next_frame_is_key_) { |
| + verify_next_frame_is_key_ = false; |
| + VerifyUnitIsKeyFrame(au_info.front_unit); |
| + } |
| #endif |
| + } |
| // Dequeue input buffer |
| @@ -646,16 +680,18 @@ bool MediaCodecDecoder::EnqueueInputBuffer() { |
| DCHECK_GE(index, 0); |
| const AccessUnit* unit = au_info.front_unit; |
| - DCHECK(unit); |
| - if (unit->is_end_of_stream) { |
| + if (drain_decoder_ || unit->is_end_of_stream) { |
| DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": QueueEOS"; |
| media_codec_bridge_->QueueEOS(index); |
| eos_enqueued_ = true; |
| return true; |
| } |
| - DVLOG(2) << class_name() << ":: QueueInputBuffer pts:" << unit->timestamp; |
| + DCHECK(unit); |
| + |
| + DVLOG(2) << class_name() << "::" << __FUNCTION__ |
| + << ": QueueInputBuffer pts:" << unit->timestamp; |
| status = media_codec_bridge_->QueueInputBuffer( |
| index, &unit->data[0], unit->data.size(), unit->timestamp); |
| @@ -672,6 +708,51 @@ bool MediaCodecDecoder::EnqueueInputBuffer() { |
| return true; |
| } |
| +AccessUnitQueue::Info MediaCodecDecoder::AdvanceAccessUnitQueue( |
| + bool* drain_decoder) { |
| + DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); |
| + DVLOG(2) << class_name() << "::" << __FUNCTION__; |
| + |
| + // Retrieve access units from the |au_queue_| in a loop until we either get |
| + // a non-config front unit or until the queue is empty. |
| + |
| + DCHECK(drain_decoder != nullptr); |
| + |
| + AccessUnitQueue::Info au_info; |
| + |
| + do { |
| + // Get current frame |
|
liberato (no reviews please)
2015/08/28 21:42:47
without the drain_decoder check here, the behavior
Tima Vaisburd
2015/08/28 22:28:55
Yes, before we would break on first, and consume t
|
| + au_info = au_queue_.GetInfo(); |
| + |
| + // Request the data from Demuxer |
| + if (au_info.data_length <= kPlaybackLowLimit && !au_info.has_eos) |
| + media_task_runner_->PostTask(FROM_HERE, request_data_cb_); |
| + |
| + if (!au_info.length) |
| + break; // Starvation |
| + |
| + if (au_info.configs) { |
| + DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": received configs " |
| + << (*au_info.configs); |
| + |
| + // Compare the new and current configs. |
| + if (IsCodecReconfigureNeeded(*au_info.configs)) { |
| + DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| + << ": reconfiguration required"; |
| + drain_decoder_ = true; |
|
liberato (no reviews please)
2015/08/28 21:42:47
(*drain_decoder)?
Tima Vaisburd
2015/08/28 22:28:55
Yes, indeed. Thank you.
|
| + } |
| + |
| + // Replace the current configs. |
| + SetDemuxerConfigs(*au_info.configs); |
| + |
| + // Move to the next frame |
| + au_queue_.Advance(); |
| + } |
| + } while (au_info.configs); |
| + |
| + return au_info; |
| +} |
| + |
| // Returns false if there was MediaCodec error. |
| bool MediaCodecDecoder::DepleteOutputBufferQueue() { |
| DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); |
| @@ -726,7 +807,8 @@ bool MediaCodecDecoder::DepleteOutputBufferQueue() { |
| Render(buffer_index, offset, size, render_mode, pts, eos_encountered); |
| if (render_mode == kRenderAfterPreroll) { |
| - DVLOG(1) << class_name() << "::" << __FUNCTION__ << " pts:" << pts |
| + DVLOG(1) << class_name() << "::" << __FUNCTION__ << " pts " << pts |
| + << " >= preroll timestamp " << preroll_timestamp_ |
| << " preroll done, stopping frame processing"; |
| media_task_runner_->PostTask(FROM_HERE, internal_preroll_done_cb_); |
| return false; |