Chromium Code Reviews| Index: media/base/android/media_codec_video_decoder.cc |
| diff --git a/media/base/android/media_codec_video_decoder.cc b/media/base/android/media_codec_video_decoder.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cc46c187a4360de141556611241f6d2a4db9ae6e |
| --- /dev/null |
| +++ b/media/base/android/media_codec_video_decoder.cc |
| @@ -0,0 +1,224 @@ |
| +// 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_video_decoder.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| + |
| +#include "media/base/android/media_codec_bridge.h" |
| +#include "media/base/demuxer_stream.h" |
| + |
| +//#include "base/tvlog.h" |
| + |
| +namespace media { |
| + |
| +MediaCodecVideoDecoder::MediaCodecVideoDecoder( |
| + 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 base::Closure& request_resources_cb) |
| + : MediaCodecDecoder(media_task_runner, |
| + ui_task_runner, |
| + request_data_cb, |
| + starvation_cb, |
| + stop_done_cb, |
| + error_cb, |
| + "VideoDecoder"), |
| + request_resources_cb_(request_resources_cb) |
| +{} |
| + |
| +MediaCodecVideoDecoder::~MediaCodecVideoDecoder() |
| +{ |
| + DVLOG(1) << "VideoDecoder::~VideoDecoder()"; |
| +} |
| + |
| +void MediaCodecVideoDecoder::SetPendingSurface(gfx::ScopedJavaSurface surface) { |
| + // Media thread |
| + DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| + |
| + surface_ = surface.Pass(); |
| + |
| + if (surface_.IsEmpty()) { |
| + // Synchronously stop decoder thread and release MediaCodec |
| + ReleaseDecoderResources(); |
| + } |
| +} |
| + |
| +bool MediaCodecVideoDecoder::HasPendingSurface() const { |
| + // Media thread |
| + DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| + |
| + return !surface_.IsEmpty(); |
| +} |
| + |
| +bool MediaCodecVideoDecoder::HasStream() const { |
| + // Media thread |
| + DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| + |
| + return configs_.video_codec != kUnknownVideoCodec; |
| +} |
| + |
| +int MediaCodecVideoDecoder::GetVideoWidth() const { |
| + // UI thread, Media thread |
| + base::AutoLock lock(configs_lock_); |
| + return configs_.video_size.width(); |
| +} |
| + |
| +int MediaCodecVideoDecoder::GetVideoHeight() const { |
| + // UI thread, Media thread |
| + base::AutoLock lock(configs_lock_); |
| + return configs_.video_size.height(); |
| +} |
| + |
| +bool MediaCodecVideoDecoder::IsCodecReconfigureNeeded( |
| + const DemuxerConfigs& curr, const DemuxerConfigs& next) const { |
| + |
| + if (curr.video_codec != next.video_codec || |
| + curr.is_video_encrypted != next.is_video_encrypted) |
| + return true; |
| + |
| + // Only size changes below this point |
| + |
| + if (curr.video_size.width() == next.video_size.width() && |
| + curr.video_size.height() == next.video_size.height()) |
| + return false; // i.e. curr == next |
| + |
| + return !static_cast<VideoCodecBridge*>(media_codec_bridge_.get())-> |
| + IsAdaptivePlaybackSupported(next.video_size.width(), |
| + next.video_size.height()); |
| +} |
| + |
| +MediaCodecDecoder::ConfigStatus |
| +MediaCodecVideoDecoder::ConfigureInternal() { |
| + // Media thread |
| + DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| + |
| + DVLOG(1) << class_name() << "::" << __FUNCTION__; |
| + |
| + // If we cannot find a key frame in cache, the browser seek is needed. |
| + if (! au_queue_.SkipToKeyFrame()) { |
|
qinmin
2015/05/13 23:17:32
extra space after !
Tima Vaisburd
2015/05/14 00:14:59
Oops! Removed.
|
| + DVLOG(1) << class_name() << "::" << __FUNCTION__ << " key frame required"; |
| + |
| + // The processing of CONFIG_KEY_FRAME_REQUIRED is not implemented yet, |
| + // return error for now |
| + //return CONFIG_KEY_FRAME_REQUIRED; |
| + return CONFIG_FAILURE; |
| + } |
| + |
| + //bool is_secure = is_content_encrypted() && drm_bridge() && |
| + // drm_bridge()->IsProtectedSurfaceRequired(); |
| + |
| + bool is_secure = false; // DRM is not implemented |
| + |
| + DCHECK(!surface_.IsEmpty()); |
| + |
| + media_codec_bridge_.reset(VideoCodecBridge::CreateDecoder( |
| + configs_.video_codec, |
| + is_secure, |
| + configs_.video_size, |
| + surface_.j_surface().obj(), |
| + GetMediaCrypto().obj())); |
| + |
| + if (!media_codec_bridge_) { |
| + DVLOG(1) << class_name() << "::" << __FUNCTION__ << " failed"; |
| + return CONFIG_FAILURE; |
| + } |
| + |
| + DVLOG(1) << class_name() << "::" << __FUNCTION__ << " succeeded"; |
| + |
| + // This callback requests resources by releasing other players, |
| + // post it to the UI thread. |
| + ui_task_runner_->PostTask(FROM_HERE, request_resources_cb_); |
| + |
| + return CONFIG_OK; |
| +} |
| + |
| +void MediaCodecVideoDecoder::ReleaseDecoderResources() { |
| + // Media thread |
| + DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| + |
| + DVLOG(1) << class_name() << "::" << __FUNCTION__; |
| + |
| + MediaCodecDecoder::ReleaseDecoderResources(); |
| + surface_ = gfx::ScopedJavaSurface(); |
| + delayed_buffers_.clear(); |
| +} |
| + |
| +void MediaCodecVideoDecoder::SynchronizePTSWithTime( |
| + base::TimeDelta current_time) { |
| + // Media thread |
| + DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| + |
| + start_time_ticks_ = base::TimeTicks::Now(); |
| + start_pts_ = current_time; |
| +} |
| + |
| +void MediaCodecVideoDecoder::Render(int buffer_index, |
| + size_t size, |
| + bool render_output, |
| + base::TimeDelta pts, |
| + bool eos_encountered) { |
| + // Decoder thread |
| + DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); |
| + |
| + DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts; |
| + |
| + if (!render_output || size == 0u) { |
| + ReleaseOutputBuffer(buffer_index, false, eos_encountered); |
| + return; |
| + } |
| + |
| + base::TimeDelta time_to_render = |
| + pts - (base::TimeTicks::Now() - start_time_ticks_ + start_pts_); |
| + |
| + if (time_to_render < base::TimeDelta()) { |
| + // Skip late frames |
| + ReleaseOutputBuffer(buffer_index, false, eos_encountered); |
| + return; |
| + } |
| + |
| + delayed_buffers_.insert(buffer_index); |
| + |
| + decoder_thread_.task_runner()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&MediaCodecVideoDecoder::ReleaseOutputBuffer, |
| + base::Unretained(this), |
| + buffer_index, true, eos_encountered), |
| + time_to_render); |
| +} |
| + |
| +void MediaCodecVideoDecoder::ReleaseOutputBuffer( |
| + int buffer_index, bool render, bool eos_encountered) { |
| + // Decoder thread |
| + DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); |
| + |
| + DVLOG(2) << class_name() << "::" << __FUNCTION__; |
| + |
| + media_codec_bridge_->ReleaseOutputBuffer(buffer_index, render); |
| + |
| + delayed_buffers_.erase(buffer_index); |
| + |
| + ProcessLastFrame(eos_encountered, !delayed_buffers_.empty()); |
| +} |
| + |
| +int MediaCodecVideoDecoder::NumDelayedRenderTasks() const { |
| + // Decoder thread |
| + DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); |
| + |
| + return delayed_buffers_.size(); |
| +} |
| + |
| +void MediaCodecVideoDecoder::ReleaseDelayedBuffers() { |
| + // Media thread |
| + // Called when there is no decoder thread |
| + for (int index : delayed_buffers_) |
| + media_codec_bridge_->ReleaseOutputBuffer(index, false); |
| + delayed_buffers_.clear(); |
| +} |
| + |
| +} // namespace media |