BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / media / base / android / media_codec_video_decoder.cc
blob48b278ad137ca961fdcb415cdf948ee5e7dda29b
1 // Copyright 2015 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/media_codec_video_decoder.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "media/base/android/media_codec_bridge.h"
10 #include "media/base/demuxer_stream.h"
11 #include "media/base/timestamp_constants.h"
13 namespace media {
15 namespace {
16 const int kDelayForStandAloneEOS = 2; // milliseconds
19 MediaCodecVideoDecoder::MediaCodecVideoDecoder(
20 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner,
21 const base::Closure& request_data_cb,
22 const base::Closure& starvation_cb,
23 const base::Closure& decoder_drained_cb,
24 const base::Closure& stop_done_cb,
25 const base::Closure& error_cb,
26 const SetTimeCallback& update_current_time_cb,
27 const VideoSizeChangedCallback& video_size_changed_cb,
28 const base::Closure& codec_created_cb)
29 : MediaCodecDecoder(media_task_runner,
30 request_data_cb,
31 starvation_cb,
32 decoder_drained_cb,
33 stop_done_cb,
34 error_cb,
35 "VideoDecoder"),
36 update_current_time_cb_(update_current_time_cb),
37 video_size_changed_cb_(video_size_changed_cb),
38 codec_created_cb_(codec_created_cb) {
41 MediaCodecVideoDecoder::~MediaCodecVideoDecoder() {
42 DCHECK(media_task_runner_->BelongsToCurrentThread());
43 DVLOG(1) << "VideoDecoder::~VideoDecoder()";
44 ReleaseDecoderResources();
47 const char* MediaCodecVideoDecoder::class_name() const {
48 return "VideoDecoder";
51 bool MediaCodecVideoDecoder::HasStream() const {
52 DCHECK(media_task_runner_->BelongsToCurrentThread());
54 return configs_.video_codec != kUnknownVideoCodec;
57 void MediaCodecVideoDecoder::SetDemuxerConfigs(const DemuxerConfigs& configs) {
58 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << configs;
60 configs_ = configs;
62 if (video_size_.IsEmpty()) {
63 video_size_ = configs_.video_size;
64 media_task_runner_->PostTask(
65 FROM_HERE, base::Bind(video_size_changed_cb_, video_size_));
69 void MediaCodecVideoDecoder::ReleaseDecoderResources() {
70 DCHECK(media_task_runner_->BelongsToCurrentThread());
71 DVLOG(1) << class_name() << "::" << __FUNCTION__;
73 DoEmergencyStop();
75 ReleaseMediaCodec();
77 surface_ = gfx::ScopedJavaSurface();
80 void MediaCodecVideoDecoder::ReleaseMediaCodec() {
81 DCHECK(media_task_runner_->BelongsToCurrentThread());
83 MediaCodecDecoder::ReleaseMediaCodec();
84 delayed_buffers_.clear();
87 void MediaCodecVideoDecoder::SetVideoSurface(gfx::ScopedJavaSurface surface) {
88 DCHECK(media_task_runner_->BelongsToCurrentThread());
90 DVLOG(1) << class_name() << "::" << __FUNCTION__
91 << (surface.IsEmpty() ? " empty" : " non-empty");
93 surface_ = surface.Pass();
95 needs_reconfigure_ = true;
98 bool MediaCodecVideoDecoder::HasVideoSurface() const {
99 DCHECK(media_task_runner_->BelongsToCurrentThread());
101 return !surface_.IsEmpty();
104 bool MediaCodecVideoDecoder::IsCodecReconfigureNeeded(
105 const DemuxerConfigs& next) const {
106 if (always_reconfigure_for_tests_)
107 return true;
109 if (configs_.video_codec != next.video_codec ||
110 configs_.is_video_encrypted != next.is_video_encrypted) {
111 return true;
114 // Only size changes below this point
116 if (configs_.video_size.width() == next.video_size.width() &&
117 configs_.video_size.height() == next.video_size.height()) {
118 return false; // i.e. configs_ == next
121 return !static_cast<VideoCodecBridge*>(media_codec_bridge_.get())
122 ->IsAdaptivePlaybackSupported(next.video_size.width(),
123 next.video_size.height());
126 MediaCodecDecoder::ConfigStatus MediaCodecVideoDecoder::ConfigureInternal() {
127 DCHECK(media_task_runner_->BelongsToCurrentThread());
129 DVLOG(1) << class_name() << "::" << __FUNCTION__;
131 // If we cannot find a key frame in cache, the browser seek is needed.
132 if (!au_queue_.RewindToLastKeyFrame()) {
133 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " key frame required";
134 return kConfigKeyFrameRequired;
137 if (configs_.video_codec == kUnknownVideoCodec) {
138 DVLOG(0) << class_name() << "::" << __FUNCTION__
139 << " configuration parameters are required";
140 return kConfigFailure;
143 // TODO(timav): implement DRM.
144 // bool is_secure = is_content_encrypted() && drm_bridge() &&
145 // drm_bridge()->IsProtectedSurfaceRequired();
147 bool is_secure = false; // DRM is not implemented
149 if (surface_.IsEmpty()) {
150 DVLOG(0) << class_name() << "::" << __FUNCTION__ << " surface required";
151 return kConfigFailure;
154 media_codec_bridge_.reset(VideoCodecBridge::CreateDecoder(
155 configs_.video_codec,
156 is_secure,
157 configs_.video_size,
158 surface_.j_surface().obj(),
159 GetMediaCrypto().obj()));
161 if (!media_codec_bridge_) {
162 DVLOG(0) << class_name() << "::" << __FUNCTION__
163 << " failed: cannot create video codec";
164 return kConfigFailure;
167 DVLOG(0) << class_name() << "::" << __FUNCTION__ << " succeeded";
169 media_task_runner_->PostTask(FROM_HERE, codec_created_cb_);
171 if (!codec_created_for_tests_cb_.is_null())
172 media_task_runner_->PostTask(FROM_HERE, codec_created_for_tests_cb_);
174 return kConfigOk;
177 void MediaCodecVideoDecoder::AssociateCurrentTimeWithPTS(base::TimeDelta pts) {
178 DCHECK(media_task_runner_->BelongsToCurrentThread());
180 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " pts:" << pts;
182 start_time_ticks_ = base::TimeTicks::Now();
183 start_pts_ = pts;
184 last_seen_pts_ = pts;
187 void MediaCodecVideoDecoder::DissociatePTSFromTime() {
188 DCHECK(media_task_runner_->BelongsToCurrentThread());
190 start_pts_ = last_seen_pts_ = kNoTimestamp();
193 void MediaCodecVideoDecoder::OnOutputFormatChanged() {
194 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
196 gfx::Size prev_size = video_size_;
198 // See b/18224769. The values reported from MediaCodecBridge::GetOutputFormat
199 // correspond to the actual video frame size, but this is not necessarily the
200 // size that should be output.
201 video_size_ = configs_.video_size;
202 if (video_size_ != prev_size) {
203 media_task_runner_->PostTask(
204 FROM_HERE, base::Bind(video_size_changed_cb_, video_size_));
208 void MediaCodecVideoDecoder::Render(int buffer_index,
209 size_t offset,
210 size_t size,
211 RenderMode render_mode,
212 base::TimeDelta pts,
213 bool eos_encountered) {
214 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
216 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts
217 << (eos_encountered ? " EOS " : " ") << AsString(render_mode);
219 // Normally EOS comes as a separate access unit that does not have data,
220 // the corresponding |size| will be 0.
221 if (!size && eos_encountered) {
222 // Stand-alone EOS
223 // Discard the PTS that comes with it and ensure it is released last.
224 pts = last_seen_pts_ +
225 base::TimeDelta::FromMilliseconds(kDelayForStandAloneEOS);
226 } else {
227 // Keep track of last seen PTS
228 last_seen_pts_ = pts;
231 // Do not update time for stand-alone EOS.
232 const bool update_time = !(eos_encountered && size == 0u);
234 // For video we simplify the preroll operation and render the first frame
235 // after preroll during the preroll phase, i.e. without waiting for audio
236 // stream to finish prerolling.
237 switch (render_mode) {
238 case kRenderSkip:
239 ReleaseOutputBuffer(buffer_index, pts, false, false, eos_encountered);
240 return;
241 case kRenderAfterPreroll:
242 // We get here in the preroll phase. Render now as explained above.
243 // |start_pts_| is not set yet, thus we cannot calculate |time_to_render|.
244 ReleaseOutputBuffer(buffer_index, pts, (size > 0), update_time,
245 eos_encountered);
246 return;
247 case kRenderNow:
248 break;
251 DCHECK_EQ(kRenderNow, render_mode);
252 DCHECK_NE(kNoTimestamp(), start_pts_); // start_pts_ must be set
254 base::TimeDelta time_to_render =
255 pts - (base::TimeTicks::Now() - start_time_ticks_ + start_pts_);
257 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts
258 << " ticks delta:" << (base::TimeTicks::Now() - start_time_ticks_)
259 << " time_to_render:" << time_to_render;
261 if (time_to_render < base::TimeDelta()) {
262 // Skip late frames
263 ReleaseOutputBuffer(buffer_index, pts, false, update_time, eos_encountered);
264 return;
267 delayed_buffers_.insert(buffer_index);
269 const bool render = (size > 0);
270 decoder_thread_.task_runner()->PostDelayedTask(
271 FROM_HERE, base::Bind(&MediaCodecVideoDecoder::ReleaseOutputBuffer,
272 base::Unretained(this), buffer_index, pts, render,
273 update_time, eos_encountered),
274 time_to_render);
277 int MediaCodecVideoDecoder::NumDelayedRenderTasks() const {
278 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
280 return delayed_buffers_.size();
283 void MediaCodecVideoDecoder::ReleaseDelayedBuffers() {
284 // Media thread
285 // Called when there is no decoder thread
286 for (int index : delayed_buffers_)
287 media_codec_bridge_->ReleaseOutputBuffer(index, false);
289 delayed_buffers_.clear();
292 #ifndef NDEBUG
293 void MediaCodecVideoDecoder::VerifyUnitIsKeyFrame(
294 const AccessUnit* unit) const {
295 // The first video frame in a sequence must be a key frame or stand-alone EOS.
296 DCHECK(unit);
297 bool stand_alone_eos = unit->is_end_of_stream && unit->data.empty();
298 DCHECK(stand_alone_eos || unit->is_key_frame);
300 #endif
302 void MediaCodecVideoDecoder::ReleaseOutputBuffer(int buffer_index,
303 base::TimeDelta pts,
304 bool render,
305 bool update_time,
306 bool eos_encountered) {
307 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
309 DVLOG(2) << class_name() << "::" << __FUNCTION__ << " pts:" << pts;
311 // Do not render if we are in emergency stop, there might be no surface.
312 if (InEmergencyStop())
313 render = false;
315 media_codec_bridge_->ReleaseOutputBuffer(buffer_index, render);
317 delayed_buffers_.erase(buffer_index);
319 CheckLastFrame(eos_encountered, !delayed_buffers_.empty());
321 // |update_current_time_cb_| might be null if there is audio stream.
322 // Do not update current time for stand-alone EOS frames.
323 if (!update_current_time_cb_.is_null() && update_time) {
324 media_task_runner_->PostTask(
325 FROM_HERE, base::Bind(update_current_time_cb_, pts, pts, false));
329 } // namespace media