Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Unified Diff: media/base/android/media_codec_decoder.cc

Issue 1128383003: Implementation of MediaCodecPlayer stage 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed unneeded changed to MediaPlayerAndroid, style guide compliance Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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
new file mode 100644
index 0000000000000000000000000000000000000000..3c23ccd13a5f4b0dacd8bfe574b4370f75d7cb1f
--- /dev/null
+++ b/media/base/android/media_codec_decoder.cc
@@ -0,0 +1,558 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "media/base/android/media_codec_decoder.h"
+
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/callback_helpers.h"
+#include "base/logging.h"
+#include "media/base/android/media_codec_bridge.h"
+
+//#include "base/tvlog.h"
qinmin 2015/05/14 17:54:07 remove unused include
Tima Vaisburd 2015/05/15 00:12:39 Removed everywhere.
+
+namespace media {
+
+namespace {
+
+// Stop requesting new data in the PREFETCH state when
+// the queue size rached this limit.
+const int PREFETCH_LIMIT = 8;
+
+// Request new data in the RUNNING state if the size of the queue
+// is less than this.
+const int PLAYBACK_LOW_LIMIT = 4;
+
+// Posting delay of the next frame processing, in milliseconds
+const int NEXT_FRAME_DELAY_MS = 2;
+}
+
+MediaCodecDecoder::MediaCodecDecoder(
+ const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner,
+ const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner,
+ const base::Closure& request_data_cb,
+ const base::Closure& starvation_cb,
+ const base::Closure& stop_done_cb,
+ const base::Closure& error_cb,
+ const char* decoder_thread_name)
+ : media_task_runner_(media_task_runner),
+ ui_task_runner_(ui_task_runner),
+ decoder_thread_(decoder_thread_name),
+ request_data_cb_(request_data_cb),
+ starvation_cb_(starvation_cb),
+ stop_done_cb_(stop_done_cb),
+ error_cb_(error_cb),
+ state_(STOPPED),
+ eos_enqueued_(false),
+ completed_(false),
+ last_frame_posted_(false),
+ weak_factory_(this) {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ DVLOG(1) << "Decoder::Decoder() " << decoder_thread_name;
+
+ weak_this_ = weak_factory_.GetWeakPtr();
+}
+
+MediaCodecDecoder::~MediaCodecDecoder() {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ DVLOG(1) << "Decoder::~Decoder()";
+
+ // NB: ReleaseDecoderResources() is virtual
+ ReleaseDecoderResources();
+}
+
+void MediaCodecDecoder::SetDemuxerConfigs(const DemuxerConfigs& configs) {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ DVLOG(1) << class_name() << "::" << __FUNCTION__;
+
+ base::AutoLock lock(configs_lock_);
+ configs_ = configs;
+}
+
+base::android::ScopedJavaLocalRef<jobject>
+MediaCodecDecoder::GetMediaCrypto() {
+ base::android::ScopedJavaLocalRef<jobject> media_crypto;
+
+ // drm_bridge_ is not implemented
+ // if (drm_bridge_)
+ // media_crypto = drm_bridge_->GetMediaCrypto();
+ return media_crypto;
+}
+
+void MediaCodecDecoder::Prefetch(const base::Closure& prefetch_done_cb) {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ DVLOG(1) << class_name() << "::" << __FUNCTION__;
+
+ DCHECK(GetState() == STOPPED);
+
+ prefetch_done_cb_ = prefetch_done_cb;
+
+ SetState(PREFETCHING);
+ PrefetchNextChunk();
+}
+
+MediaCodecDecoder::ConfigStatus MediaCodecDecoder::Configure() {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ DVLOG(1) << class_name() << "::" << __FUNCTION__;
+
+ // Here I assume that OnDemuxerConfigsAvailable won't come
+ // in the middle of demuxer data.
+
+ bool is_reconfigure_needed = !media_codec_bridge_;
+ // || IsCodecReconfigureNeeded(configs_, configs_);
+
+ if (!is_reconfigure_needed) {
+ DVLOG(1) << class_name() << "::" << __FUNCTION__
+ << ": reconfiguration is not required, ignoring";
+ return CONFIG_OK;
+ }
+
+ return ConfigureInternal();
+}
+
+bool MediaCodecDecoder::Start(base::TimeDelta current_time) {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ DVLOG(1) << class_name() << "::" << __FUNCTION__
+ << " current_time:" << current_time;
+
+ if (state_ == RUNNING) {
+ DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": already started";
+ return true; // already started
+ }
+
+ if (state_ == STOPPING) {
+ DVLOG(0) << class_name() << "::" << __FUNCTION__
+ << ": wrong state STOPPING, ignoring";
+ return false;
+ }
+
+ DCHECK(!decoder_thread_.IsRunning());
+
+ // We only synchronize video stream.
+ // When audio is present, the |current_time| is audio time.
+ SynchronizePTSWithTime(current_time);
+
+ last_frame_posted_ = false;
+
+ // Start the decoder thread
+ if (!decoder_thread_.Start()) {
+ DVLOG(1) << class_name() << "::" << __FUNCTION__
+ << ": cannot start decoder thread";
+ return false;
+ }
+
+ SetState(RUNNING);
+
+ decoder_thread_.task_runner()->PostTask(
+ FROM_HERE,
+ base::Bind(&MediaCodecDecoder::ProcessNextFrame, base::Unretained(this)));
+
+ return true;
+}
+
+void MediaCodecDecoder::SyncStop() {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ DVLOG(1) << class_name() << "::" << __FUNCTION__;
+
+ // After this method returns, decoder thread will not be running.
+
+ decoder_thread_.Stop(); // synchronous
+ state_ = STOPPED;
+
+ // Shall we move |delayed_buffers_| from VideoDecoder to Decoder class?
+ ReleaseDelayedBuffers();
+}
+
+void MediaCodecDecoder::RequestToStop() {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ DVLOG(1) << class_name() << "::" << __FUNCTION__;
+
+ DCHECK(GetState() == RUNNING);
+ SetState(STOPPING);
+}
+
+void MediaCodecDecoder::OnLastFrameRendered(bool completed) {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ DVLOG(1) << class_name() << "::" << __FUNCTION__
+ << " completed:" << completed;
+
+ decoder_thread_.Stop(); // synchronous
+ state_ = STOPPED;
+ completed_ = completed;
+
+ media_task_runner_->PostTask(FROM_HERE, stop_done_cb_);
+}
+
+void MediaCodecDecoder::Flush() {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ DVLOG(1) << class_name() << "::" << __FUNCTION__;
+
+ DCHECK_EQ(GetState(), STOPPED);
+
+ eos_enqueued_ = false;
+ completed_ = false;
+ au_queue_.Flush();
+
+ if (media_codec_bridge_) {
+ // MediaCodecBridge::Reset() performs MediaCodecBridge.flush()
+ MediaCodecStatus flush_status = media_codec_bridge_->Reset();
+ if (flush_status != MEDIA_CODEC_OK)
+ media_task_runner_->PostTask(FROM_HERE, error_cb_);
+ }
+}
+
+void MediaCodecDecoder::OnDemuxerDataAvailable(const DemuxerData& data) {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ DVLOG(1) << class_name() << "::" << __FUNCTION__
+ << " #AUs:" << data.access_units.size()
+ << " #Configs:" << data.demuxer_configs.size();
+
+ if (!data.access_units.empty())
+ DVLOG(1) << class_name() << "::" << __FUNCTION__
+ << " AU[0]: " << data.access_units[0];
+
+ au_queue_.PushBack(data);
+
+ if (state_ == PREFETCHING)
+ PrefetchNextChunk();
+}
+
+void MediaCodecDecoder::ReleaseDecoderResources() {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ DVLOG(1) << class_name() << "::" << __FUNCTION__;
+
+ decoder_thread_.Stop(); // synchronous
+ state_ = STOPPED;
+ media_codec_bridge_.reset();
+}
+
+void MediaCodecDecoder::ReleaseMediaCodec() {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ DVLOG(1) << class_name() << "::" << __FUNCTION__;
+
+ media_codec_bridge_.reset();
+}
+
+bool MediaCodecDecoder::IsPrefetchingOrPlaying() const {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ base::AutoLock lock(state_lock_);
+ return state_ == PREFETCHING || state_ == RUNNING;
+}
+
+bool MediaCodecDecoder::IsStopped() const {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ return GetState() == STOPPED;
+}
+
+bool MediaCodecDecoder::IsCompleted() const {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ return completed_;
+}
+
+DecoderState MediaCodecDecoder::GetState() const {
+ base::AutoLock lock(state_lock_);
+ return state_;
+}
+
+void MediaCodecDecoder::SetState(DecoderState state) {
+ DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << state;
+
+ base::AutoLock lock(state_lock_);
+ state_ = state;
+}
+
+void MediaCodecDecoder::PrefetchNextChunk() {
+ // Media thread
+ DCHECK(media_task_runner_->BelongsToCurrentThread());
+
+ DVLOG(1) << class_name() << "::" << __FUNCTION__;
+
+ if (eos_enqueued_ ||
+ au_queue_.Length() >= PREFETCH_LIMIT ||
+ au_queue_.HasEOS()) {
+ // We are done prefetching
+ DVLOG(1) << class_name() << "::" << __FUNCTION__ << " posting PrefetchDone";
+ media_task_runner_->PostTask(
+ FROM_HERE, base::ResetAndReturn(&prefetch_done_cb_));
+ return;
+ }
+
+ request_data_cb_.Run();
+}
+
+void MediaCodecDecoder::ProcessNextFrame() {
+ // Decoder thread
+ DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
+
+ DVLOG(1) << class_name() << "::" << __FUNCTION__;
+
+ DecoderState state = GetState();
+
+ if (state != RUNNING && state != STOPPING) {
+ DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": not running";
+ return;
+ }
+
+ if (state == STOPPING) {
+ if (NumDelayedRenderTasks() == 0 && !last_frame_posted_) {
+ DVLOG(1) << class_name() << "::" << __FUNCTION__
+ << ": STOPPING, posting OnLastFrameRendered";
+ media_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&MediaCodecDecoder::OnLastFrameRendered,
+ weak_this_, false));
+ last_frame_posted_ = true;
+ }
+
+ // We can stop processing, the |au_queue_| and MediaCodec queues can freeze
+ return;
+ }
+
+ DCHECK(state == RUNNING);
+
+ // Keep the number pending video frames low, ideally maintaining
+ // the same audio and video duration after stop request
+
+ if (NumDelayedRenderTasks() <= 1) {
+ bool success= EnqueueInputBuffer(state);
+ if (!success)
+ return;
+ }
+
+ bool eos_encountered = false;
+ bool success = DepleteOutputBufferQueue(state, &eos_encountered);
+ if (!success)
+ return;
+
+ if (eos_encountered) {
+ DVLOG(1) << class_name() << "::" << __FUNCTION__
+ << " EOS dequeued, stopping frame processing";
+ return;
+ }
+
+ // We need a small delay if we want to stop this thread by
+ // decoder_thread_.Stop() reliably.
+ // The decoder thread message loop processes all pending
+ // (but not delayed) tasks before it can quit; without a delay
+ // the message loop might be forever processing the pendng tasks.
+ decoder_thread_.task_runner()->PostDelayedTask(
+ FROM_HERE,
+ base::Bind(&MediaCodecDecoder::ProcessNextFrame,
+ base::Unretained(this)),
+ base::TimeDelta::FromMilliseconds(NEXT_FRAME_DELAY_MS));
+}
+
+// Returns false if there was MediaCodec error.
qinmin 2015/05/14 19:52:57 this should be moved to .h file
+bool MediaCodecDecoder::EnqueueInputBuffer(DecoderState state) {
+ // Decoder thread
+ DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
+
+ //DVLOG(2) << class_name() << "::" << __FUNCTION__;
+
+ if (eos_enqueued_) {
+ DVLOG(1) << class_name() << "::" << __FUNCTION__
+ << ": eos_enqueued, returning";
+ return true; // Nothing to do
+ }
+
+ // Get the next frame from the queue and the queue info
+
+ AccessUnitQueue::Info au_info;
+ au_queue_.GetInfo(&au_info);
+
+ // Request the data from Demuxer
+ if (au_info.length <= PLAYBACK_LOW_LIMIT && !au_info.has_eos)
+ media_task_runner_->PostTask(FROM_HERE, request_data_cb_);
+
+ // Get the next frame from the queue
+
+ if (!au_info.front_unit) {
+ // Report starvation and return, Start() will be called again later.
+ DVLOG(1) << class_name() << "::" << __FUNCTION__
+ << ": starvation detected state:" << state;
+ if (state != STOPPING)
+ media_task_runner_->PostTask(FROM_HERE, starvation_cb_);
+ return true;
+ }
+
+ 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, error_cb_);
+ return true;
+ }
+
+ // Dequeue input buffer
+
+ base::TimeDelta timeout = base::TimeDelta::FromMilliseconds(20);
+ int index = -1;
+ MediaCodecStatus status =
+ media_codec_bridge_->DequeueInputBuffer(timeout, &index);
+
+ DVLOG(2) << class_name() << ":: DequeueInputBuffer index:" << index;
+
+ switch (status) {
+ case MEDIA_CODEC_ERROR:
+ DVLOG(0) << class_name() << "::" << __FUNCTION__
+ << ": MEDIA_CODEC_ERROR DequeueInputBuffer failed";
+ media_task_runner_->PostTask(FROM_HERE, error_cb_);
+ return false;
+ break;
+
+ case MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER:
+ //DVLOG(2) << class_name() << "::" << __FUNCTION__
+ // << " DequeueInputBuffer AGAIN";
+ return true;
+ break;
+
+ default:
+ break;
+ }
+
+ // We got the buffer
+ DCHECK_EQ(status, MEDIA_CODEC_OK);
+ DCHECK_GE(index, 0);
+
+ const AccessUnit* unit = au_info.front_unit;
+
+ if (unit->is_end_of_stream || unit->data.empty()) {
+ DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": QueueEOS";
+ media_codec_bridge_->QueueEOS(index);
+ eos_enqueued_ = true;
+ return true;
+ }
+
+ status = media_codec_bridge_->QueueInputBuffer(
+ index, &unit->data[0], unit->data.size(), unit->timestamp);
+
+ if (status == MEDIA_CODEC_ERROR) {
+ DVLOG(1) << class_name() << "::" << __FUNCTION__
+ << ": MEDIA_CODEC_ERROR: QueueInputBuffer failed";
+ media_task_runner_->PostTask(FROM_HERE, error_cb_);
+ return false;
+ }
+
+ // Have successfully queued input buffer
+ au_queue_.PopFront();
+ return true;
+}
+
+// Returns false if there was MediaCodec error.
+bool MediaCodecDecoder::DepleteOutputBufferQueue(DecoderState state,
+ bool* eos_encountered) {
+ // Decoder thread
+ DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
+
+ //DVLOG(2) << class_name() << "::" << __FUNCTION__;
+
+ int buffer_index = 0;
+ size_t offset = 0;
+ size_t size = 0;
+ base::TimeDelta pts;
+ MediaCodecStatus status;
+
+ base::TimeDelta timeout[2];
+ timeout[0] = base::TimeDelta::FromMilliseconds(20);
+ timeout[1] = base::TimeDelta::FromMilliseconds(0);
+
+ int timeout_index = 0;
+
+ do {
+ status = media_codec_bridge_->DequeueOutputBuffer(
+ timeout[timeout_index],
+ &buffer_index,
+ &offset,
+ &size,
+ &pts,
+ eos_encountered,
+ nullptr);
+
+ timeout_index = 1;
+
+ //DVLOG(2) << class_name()
+ // << ":: DequeueOutputBuffer index:" << buffer_index;
+
+ switch (status) {
+ case MEDIA_CODEC_OUTPUT_FORMAT_CHANGED:
+ // TODO:
+ // Audio: set new rate. If it changed,
+ // the MediaSourcePlayer code calls ResetTimestampHelper().
+ // Video: report new frame size to manager()
+ DVLOG(2) << class_name() << "::" << __FUNCTION__
+ << " MEDIA_CODEC_OUTPUT_FORMAT_CHANGED";
+ break;
+
+ case MEDIA_CODEC_OK:
+ // We got the decoded frame
+ Render(buffer_index, size, true, pts, *eos_encountered);
+ break;
+
+ case MEDIA_CODEC_ERROR:
+ DVLOG(0) << class_name() << "::" << __FUNCTION__
+ << ": MEDIA_CODEC_ERROR from DequeueOutputBuffer";
+ media_task_runner_->PostTask(FROM_HERE, error_cb_);
+ break;
+
+ default:
+ break;
+ }
+
+ } while (status != MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER &&
+ status != MEDIA_CODEC_ERROR &&
+ !*eos_encountered);
+
+ return status != MEDIA_CODEC_ERROR;
+}
+
+void MediaCodecDecoder::ProcessLastFrame(bool eos_encountered,
+ bool has_delayed_tasks) {
+ // Decoder thread
+ DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
+
+ bool last_frame_when_stopping =
+ GetState() == STOPPING && !has_delayed_tasks;
+
+ if (last_frame_when_stopping || eos_encountered) {
+ media_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&MediaCodecDecoder::OnLastFrameRendered,
+ weak_this_, eos_encountered));
+ last_frame_posted_ = true;
+ }
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698