| 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..57052a9ef98db4d00fe60a2d92991e4d616c1b67
|
| --- /dev/null
|
| +++ b/media/base/android/media_codec_video_decoder.cc
|
| @@ -0,0 +1,199 @@
|
| +// 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 SetTimeCallback& update_current_time_cb,
|
| + const base::Closure& request_resources_cb)
|
| + : MediaCodecDecoder(media_task_runner, ui_task_runner,
|
| + request_data_cb, starvation_cb,
|
| + stop_done_cb, error_cb, update_current_time_cb,
|
| + "VideoDecoder"),
|
| + request_resources_cb_(request_resources_cb)
|
| +{}
|
| +
|
| +MediaCodecVideoDecoder::~MediaCodecVideoDecoder()
|
| +{
|
| + DVLOG(1) << "VideoDecoder::~VideoDecoder()";
|
| +}
|
| +
|
| +void MediaCodecVideoDecoder::SetPendingSurface(gfx::ScopedJavaSurface surface) {
|
| + // Media thread
|
| + surface_ = surface.Pass();
|
| +
|
| + if (surface_.IsEmpty()) {
|
| + // Synchronously stop decoder thread and release MediaCodec
|
| + ReleaseDecoderResources();
|
| + }
|
| +}
|
| +
|
| +bool MediaCodecVideoDecoder::HasPendingSurface() const {
|
| + return !surface_.IsEmpty();
|
| +}
|
| +
|
| +bool MediaCodecVideoDecoder::HasStream() const {
|
| + return configs_.video_codec != kUnknownVideoCodec;
|
| +}
|
| +
|
| +int MediaCodecVideoDecoder::GetVideoWidth() const {
|
| + base::AutoLock lock(configs_lock_);
|
| + return configs_.video_size.width();
|
| +}
|
| +
|
| +int MediaCodecVideoDecoder::GetVideoHeight() const {
|
| + 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() {
|
| + DVLOG(1) << class_name() << "::" << __FUNCTION__;
|
| +
|
| + // If we cannot find a key frame in cache, the browser seek is needed.
|
| + if (! au_queue_.SkipToKeyFrame()) {
|
| + 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() {
|
| + DVLOG(1) << class_name() << "::" << __FUNCTION__;
|
| +
|
| + MediaCodecDecoder::ReleaseDecoderResources();
|
| + surface_ = gfx::ScopedJavaSurface();
|
| + delayed_buffers_.clear();
|
| +}
|
| +
|
| +void MediaCodecVideoDecoder::Render(int buffer_index,
|
| + size_t size,
|
| + bool render_output,
|
| + base::TimeDelta pts,
|
| + bool eos_encountered) {
|
| + // Decoder thread
|
| + 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) {
|
| + DVLOG(2) << class_name() << "::" << __FUNCTION__;
|
| +
|
| + media_codec_bridge_->ReleaseOutputBuffer(buffer_index, render);
|
| +
|
| + delayed_buffers_.erase(buffer_index);
|
| +
|
| + bool last_frame_when_stopping =
|
| + GetState() == STOPPING && delayed_buffers_.empty();
|
| +
|
| + if (last_frame_when_stopping || eos_encountered) {
|
| + SetState(STOPPED);
|
| + media_task_runner_->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&MediaCodecDecoder::OnLastFrameRendered,
|
| + weak_this_, eos_encountered));
|
| + }
|
| +}
|
| +
|
| +int MediaCodecVideoDecoder::NumDelayedRenderTasks() const {
|
| + // Decoder thread
|
| + 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
|
|
|