Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / base / android / video_decoder_job.cc
blobd34ea28aadacf529b8dfe10abd46af31c9e392ba
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/base/android/video_decoder_job.h"
7 #include "base/bind.h"
8 #include "base/lazy_instance.h"
9 #include "base/threading/thread.h"
10 #include "media/base/android/media_codec_bridge.h"
11 #include "media/base/android/media_drm_bridge.h"
13 namespace media {
15 class VideoDecoderThread : public base::Thread {
16 public:
17 VideoDecoderThread() : base::Thread("MediaSource_VideoDecoderThread") {
18 Start();
22 // TODO(qinmin): Check if it is tolerable to use worker pool to handle all the
23 // decoding tasks so that we don't need a global thread here.
24 // http://crbug.com/245750
25 base::LazyInstance<VideoDecoderThread>::Leaky
26 g_video_decoder_thread = LAZY_INSTANCE_INITIALIZER;
28 VideoDecoderJob::VideoDecoderJob(
29 const base::Closure& request_data_cb,
30 const base::Closure& request_resources_cb,
31 const base::Closure& on_demuxer_config_changed_cb)
32 : MediaDecoderJob(g_video_decoder_thread.Pointer()->message_loop_proxy(),
33 request_data_cb,
34 on_demuxer_config_changed_cb),
35 video_codec_(kUnknownVideoCodec),
36 config_width_(0),
37 config_height_(0),
38 output_width_(0),
39 output_height_(0),
40 request_resources_cb_(request_resources_cb) {
43 VideoDecoderJob::~VideoDecoderJob() {}
45 bool VideoDecoderJob::SetVideoSurface(gfx::ScopedJavaSurface surface) {
46 // For an empty surface, always pass it to the |media_codec_bridge_| job so
47 // that it can detach from the current one. Otherwise, don't pass an
48 // unprotected surface if the video content requires a protected one.
49 if (!surface.IsEmpty() && IsProtectedSurfaceRequired() &&
50 !surface.is_protected()) {
51 return false;
54 surface_ = surface.Pass();
55 need_to_reconfig_decoder_job_ = true;
56 return true;
59 bool VideoDecoderJob::HasStream() const {
60 return video_codec_ != kUnknownVideoCodec;
63 void VideoDecoderJob::ReleaseDecoderResources() {
64 MediaDecoderJob::ReleaseDecoderResources();
65 surface_ = gfx::ScopedJavaSurface();
68 void VideoDecoderJob::SetDemuxerConfigs(const DemuxerConfigs& configs) {
69 video_codec_ = configs.video_codec;
70 config_width_ = configs.video_size.width();
71 config_height_ = configs.video_size.height();
72 set_is_content_encrypted(configs.is_video_encrypted);
73 if (!media_codec_bridge_) {
74 output_width_ = config_width_;
75 output_height_ = config_height_;
79 void VideoDecoderJob::ReleaseOutputBuffer(
80 int output_buffer_index,
81 size_t size,
82 bool render_output,
83 base::TimeDelta current_presentation_timestamp,
84 const ReleaseOutputCompletionCallback& callback) {
85 media_codec_bridge_->ReleaseOutputBuffer(output_buffer_index, render_output);
86 callback.Run(current_presentation_timestamp, current_presentation_timestamp);
89 bool VideoDecoderJob::ComputeTimeToRender() const {
90 return true;
93 bool VideoDecoderJob::IsCodecReconfigureNeeded(
94 const DemuxerConfigs& configs) const {
95 if (!media_codec_bridge_)
96 return true;
98 if (!AreDemuxerConfigsChanged(configs))
99 return false;
101 bool only_size_changed = false;
102 if (video_codec_ == configs.video_codec &&
103 is_content_encrypted() == configs.is_video_encrypted) {
104 only_size_changed = true;
107 return !only_size_changed ||
108 !static_cast<VideoCodecBridge*>(media_codec_bridge_.get())->
109 IsAdaptivePlaybackSupported(configs.video_size.width(),
110 configs.video_size.height());
113 bool VideoDecoderJob::AreDemuxerConfigsChanged(
114 const DemuxerConfigs& configs) const {
115 return video_codec_ != configs.video_codec ||
116 is_content_encrypted() != configs.is_video_encrypted ||
117 config_width_ != configs.video_size.width() ||
118 config_height_ != configs.video_size.height();
121 MediaDecoderJob::MediaDecoderJobStatus
122 VideoDecoderJob::CreateMediaCodecBridgeInternal() {
123 if (surface_.IsEmpty()) {
124 ReleaseMediaCodecBridge();
125 return STATUS_FAILURE;
128 // If we cannot find a key frame in cache, browser seek is needed.
129 bool next_video_data_is_iframe = SetCurrentFrameToPreviouslyCachedKeyFrame();
130 if (!next_video_data_is_iframe)
131 return STATUS_KEY_FRAME_REQUIRED;
133 bool is_secure = is_content_encrypted() && drm_bridge() &&
134 drm_bridge()->IsProtectedSurfaceRequired();
136 media_codec_bridge_.reset(VideoCodecBridge::CreateDecoder(
137 video_codec_, is_secure, gfx::Size(config_width_, config_height_),
138 surface_.j_surface().obj(), GetMediaCrypto().obj()));
140 if (!media_codec_bridge_)
141 return STATUS_FAILURE;
143 request_resources_cb_.Run();
144 return STATUS_SUCCESS;
147 bool VideoDecoderJob::UpdateOutputFormat() {
148 if (!media_codec_bridge_)
149 return false;
150 int prev_output_width = output_width_;
151 int prev_output_height = output_height_;
152 // See b/18224769. The values reported from MediaCodecBridge::GetOutputFormat
153 // correspond to the actual video frame size, but this is not necessarily the
154 // size that should be output.
155 output_width_ = config_width_;
156 output_height_ = config_height_;
157 return (output_width_ != prev_output_width) ||
158 (output_height_ != prev_output_height);
161 bool VideoDecoderJob::IsProtectedSurfaceRequired() {
162 return is_content_encrypted() && drm_bridge() &&
163 drm_bridge()->IsProtectedSurfaceRequired();
166 } // namespace media