Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / media / base / android / media_codec_decoder.cc
blobdc51904ee953b070b33b21733a1091beaaea8d28
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_decoder.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback_helpers.h"
10 #include "base/logging.h"
11 #include "media/base/android/media_codec_bridge.h"
13 namespace media {
15 namespace {
17 // Stop requesting new data in the kPrefetching state when the queue size
18 // reaches this limit.
19 const int kPrefetchLimit = 8;
21 // Request new data in the kRunning state if the queue size is less than this.
22 const int kPlaybackLowLimit = 4;
24 // Posting delay of the next frame processing, in milliseconds
25 const int kNextFrameDelay = 1;
27 // Timeout for dequeuing an input buffer from MediaCodec in milliseconds.
28 const int kInputBufferTimeout = 20;
30 // Timeout for dequeuing an output buffer from MediaCodec in milliseconds.
31 const int kOutputBufferTimeout = 20;
34 MediaCodecDecoder::MediaCodecDecoder(
35 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner,
36 const base::Closure& external_request_data_cb,
37 const base::Closure& starvation_cb,
38 const base::Closure& stop_done_cb,
39 const base::Closure& error_cb,
40 const char* decoder_thread_name)
41 : media_task_runner_(media_task_runner),
42 decoder_thread_(decoder_thread_name),
43 external_request_data_cb_(external_request_data_cb),
44 starvation_cb_(starvation_cb),
45 stop_done_cb_(stop_done_cb),
46 error_cb_(error_cb),
47 state_(kStopped),
48 eos_enqueued_(false),
49 completed_(false),
50 last_frame_posted_(false),
51 is_data_request_in_progress_(false),
52 is_incoming_data_invalid_(false),
53 #ifndef NDEBUG
54 verify_next_frame_is_key_(false),
55 #endif
56 weak_factory_(this) {
57 DCHECK(media_task_runner_->BelongsToCurrentThread());
59 DVLOG(1) << "Decoder::Decoder() " << decoder_thread_name;
61 internal_error_cb_ =
62 base::Bind(&MediaCodecDecoder::OnCodecError, weak_factory_.GetWeakPtr());
63 request_data_cb_ =
64 base::Bind(&MediaCodecDecoder::RequestData, weak_factory_.GetWeakPtr());
67 MediaCodecDecoder::~MediaCodecDecoder() {
68 DCHECK(media_task_runner_->BelongsToCurrentThread());
70 DVLOG(1) << "Decoder::~Decoder()";
72 // NB: ReleaseDecoderResources() is virtual
73 ReleaseDecoderResources();
76 const char* MediaCodecDecoder::class_name() const {
77 return "Decoder";
80 void MediaCodecDecoder::ReleaseDecoderResources() {
81 DCHECK(media_task_runner_->BelongsToCurrentThread());
83 DVLOG(1) << class_name() << "::" << __FUNCTION__;
85 decoder_thread_.Stop(); // synchronous
86 state_ = kStopped;
87 media_codec_bridge_.reset();
90 void MediaCodecDecoder::Flush() {
91 DCHECK(media_task_runner_->BelongsToCurrentThread());
93 DVLOG(1) << class_name() << "::" << __FUNCTION__;
95 DCHECK_EQ(GetState(), kStopped);
97 // Flush() is a part of the Seek request. Whenever we request a seek we need
98 // to invalidate the current data request.
99 if (is_data_request_in_progress_)
100 is_incoming_data_invalid_ = true;
102 eos_enqueued_ = false;
103 completed_ = false;
104 au_queue_.Flush();
106 #ifndef NDEBUG
107 // We check and reset |verify_next_frame_is_key_| on Decoder thread.
108 // This DCHECK ensures we won't need to lock this variable.
109 DCHECK(!decoder_thread_.IsRunning());
111 // For video the first frame after flush must be key frame.
112 verify_next_frame_is_key_ = true;
113 #endif
115 if (media_codec_bridge_) {
116 // MediaCodecBridge::Reset() performs MediaCodecBridge.flush()
117 MediaCodecStatus flush_status = media_codec_bridge_->Reset();
118 if (flush_status != MEDIA_CODEC_OK) {
119 DVLOG(0) << class_name() << "::" << __FUNCTION__
120 << "MediaCodecBridge::Reset() failed";
121 media_task_runner_->PostTask(FROM_HERE, internal_error_cb_);
126 void MediaCodecDecoder::ReleaseMediaCodec() {
127 DCHECK(media_task_runner_->BelongsToCurrentThread());
129 DVLOG(1) << class_name() << "::" << __FUNCTION__;
131 media_codec_bridge_.reset();
134 bool MediaCodecDecoder::IsPrefetchingOrPlaying() const {
135 DCHECK(media_task_runner_->BelongsToCurrentThread());
137 base::AutoLock lock(state_lock_);
138 return state_ == kPrefetching || state_ == kRunning;
141 bool MediaCodecDecoder::IsStopped() const {
142 DCHECK(media_task_runner_->BelongsToCurrentThread());
144 return GetState() == kStopped;
147 bool MediaCodecDecoder::IsCompleted() const {
148 DCHECK(media_task_runner_->BelongsToCurrentThread());
150 return completed_;
153 base::android::ScopedJavaLocalRef<jobject> MediaCodecDecoder::GetMediaCrypto() {
154 base::android::ScopedJavaLocalRef<jobject> media_crypto;
156 // TODO(timav): implement DRM.
157 // drm_bridge_ is not implemented
158 // if (drm_bridge_)
159 // media_crypto = drm_bridge_->GetMediaCrypto();
160 return media_crypto;
163 void MediaCodecDecoder::Prefetch(const base::Closure& prefetch_done_cb) {
164 DCHECK(media_task_runner_->BelongsToCurrentThread());
166 DVLOG(1) << class_name() << "::" << __FUNCTION__;
168 DCHECK(GetState() == kStopped);
170 prefetch_done_cb_ = prefetch_done_cb;
172 SetState(kPrefetching);
173 PrefetchNextChunk();
176 MediaCodecDecoder::ConfigStatus MediaCodecDecoder::Configure() {
177 DCHECK(media_task_runner_->BelongsToCurrentThread());
179 DVLOG(1) << class_name() << "::" << __FUNCTION__;
181 if (GetState() == kError) {
182 DVLOG(0) << class_name() << "::" << __FUNCTION__ << ": wrong state kError";
183 return CONFIG_FAILURE;
186 MediaCodecDecoder::ConfigStatus result;
187 if (media_codec_bridge_) {
188 DVLOG(1) << class_name() << "::" << __FUNCTION__
189 << ": reconfiguration is not required, ignoring";
190 result = CONFIG_OK;
191 } else {
192 result = ConfigureInternal();
194 #ifndef NDEBUG
195 // We check and reset |verify_next_frame_is_key_| on Decoder thread.
196 // This DCHECK ensures we won't need to lock this variable.
197 DCHECK(!decoder_thread_.IsRunning());
199 // For video the first frame after reconfiguration must be key frame.
200 if (result == CONFIG_OK)
201 verify_next_frame_is_key_ = true;
202 #endif
205 return result;
208 bool MediaCodecDecoder::Start(base::TimeDelta current_time) {
209 DCHECK(media_task_runner_->BelongsToCurrentThread());
211 DVLOG(1) << class_name() << "::" << __FUNCTION__
212 << " current_time:" << current_time;
214 DecoderState state = GetState();
215 if (state == kRunning) {
216 DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": already started";
217 return true; // already started
220 if (state != kPrefetched) {
221 DVLOG(0) << class_name() << "::" << __FUNCTION__ << ": wrong state "
222 << AsString(state) << " ignoring";
223 return false;
226 if (!media_codec_bridge_) {
227 DVLOG(0) << class_name() << "::" << __FUNCTION__
228 << ": not configured, ignoring";
229 return false;
232 DCHECK(!decoder_thread_.IsRunning());
234 // We only synchronize video stream.
235 // When audio is present, the |current_time| is audio time.
236 SynchronizePTSWithTime(current_time);
238 last_frame_posted_ = false;
240 // Start the decoder thread
241 if (!decoder_thread_.Start()) {
242 DVLOG(1) << class_name() << "::" << __FUNCTION__
243 << ": cannot start decoder thread";
244 return false;
247 SetState(kRunning);
249 decoder_thread_.task_runner()->PostTask(
250 FROM_HERE,
251 base::Bind(&MediaCodecDecoder::ProcessNextFrame, base::Unretained(this)));
253 return true;
256 void MediaCodecDecoder::SyncStop() {
257 DCHECK(media_task_runner_->BelongsToCurrentThread());
259 DVLOG(1) << class_name() << "::" << __FUNCTION__;
261 if (GetState() == kError) {
262 DVLOG(0) << class_name() << "::" << __FUNCTION__
263 << ": wrong state kError, ignoring";
264 return;
267 // After this method returns, decoder thread will not be running.
269 decoder_thread_.Stop(); // synchronous
270 state_ = kStopped;
272 // Shall we move |delayed_buffers_| from VideoDecoder to Decoder class?
273 ReleaseDelayedBuffers();
276 void MediaCodecDecoder::RequestToStop() {
277 DCHECK(media_task_runner_->BelongsToCurrentThread());
279 DVLOG(1) << class_name() << "::" << __FUNCTION__;
281 DecoderState state = GetState();
282 switch (state) {
283 case kError:
284 DVLOG(0) << class_name() << "::" << __FUNCTION__
285 << ": wrong state kError, ignoring";
286 break;
287 case kRunning:
288 SetState(kStopping);
289 break;
290 case kStopping:
291 break; // ignore
292 case kStopped:
293 case kPrefetching:
294 case kPrefetched:
295 // There is nothing to wait for, we can sent nofigication right away.
296 DCHECK(!decoder_thread_.IsRunning());
297 SetState(kStopped);
298 media_task_runner_->PostTask(FROM_HERE, stop_done_cb_);
299 break;
300 default:
301 NOTREACHED();
302 break;
306 void MediaCodecDecoder::OnLastFrameRendered(bool completed) {
307 DCHECK(media_task_runner_->BelongsToCurrentThread());
309 DVLOG(1) << class_name() << "::" << __FUNCTION__
310 << " completed:" << completed;
312 decoder_thread_.Stop(); // synchronous
313 state_ = kStopped;
314 completed_ = completed;
316 media_task_runner_->PostTask(FROM_HERE, stop_done_cb_);
319 void MediaCodecDecoder::OnDemuxerDataAvailable(const DemuxerData& data) {
320 DCHECK(media_task_runner_->BelongsToCurrentThread());
322 // If |data| contains an aborted data, the last AU will have kAborted status.
323 bool aborted_data =
324 !data.access_units.empty() &&
325 data.access_units.back().status == DemuxerStream::kAborted;
327 #ifndef NDEBUG
328 const char* explain_if_skipped =
329 is_incoming_data_invalid_ ? " skipped as invalid"
330 : (aborted_data ? " skipped as aborted" : "");
332 for (const auto& unit : data.access_units)
333 DVLOG(1) << class_name() << "::" << __FUNCTION__ << explain_if_skipped
334 << " au: " << unit;
335 for (const auto& configs : data.demuxer_configs)
336 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " configs: " << configs;
337 #endif
339 if (!is_incoming_data_invalid_ && !aborted_data)
340 au_queue_.PushBack(data);
342 is_incoming_data_invalid_ = false;
343 is_data_request_in_progress_ = false;
345 // Do not request data if we got kAborted. There is no point to request the
346 // data after kAborted and before the OnDemuxerSeekDone.
347 if (state_ == kPrefetching && !aborted_data)
348 PrefetchNextChunk();
351 int MediaCodecDecoder::NumDelayedRenderTasks() const {
352 return 0;
355 void MediaCodecDecoder::CheckLastFrame(bool eos_encountered,
356 bool has_delayed_tasks) {
357 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
359 bool last_frame_when_stopping = GetState() == kStopping && !has_delayed_tasks;
361 if (last_frame_when_stopping || eos_encountered) {
362 media_task_runner_->PostTask(
363 FROM_HERE, base::Bind(&MediaCodecDecoder::OnLastFrameRendered,
364 weak_factory_.GetWeakPtr(), eos_encountered));
365 last_frame_posted_ = true;
369 void MediaCodecDecoder::OnCodecError() {
370 DCHECK(media_task_runner_->BelongsToCurrentThread());
372 SetState(kError);
373 error_cb_.Run();
376 void MediaCodecDecoder::RequestData() {
377 DCHECK(media_task_runner_->BelongsToCurrentThread());
379 // Ensure one data request at a time.
380 if (!is_data_request_in_progress_) {
381 is_data_request_in_progress_ = true;
382 external_request_data_cb_.Run();
386 void MediaCodecDecoder::PrefetchNextChunk() {
387 DCHECK(media_task_runner_->BelongsToCurrentThread());
389 DVLOG(1) << class_name() << "::" << __FUNCTION__;
391 AccessUnitQueue::Info au_info = au_queue_.GetInfo();
393 if (eos_enqueued_ || au_info.length >= kPrefetchLimit || au_info.has_eos) {
394 // We are done prefetching
395 SetState(kPrefetched);
396 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " posting PrefetchDone";
397 media_task_runner_->PostTask(FROM_HERE,
398 base::ResetAndReturn(&prefetch_done_cb_));
399 return;
402 request_data_cb_.Run();
405 void MediaCodecDecoder::ProcessNextFrame() {
406 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
408 DVLOG(2) << class_name() << "::" << __FUNCTION__;
410 DecoderState state = GetState();
412 if (state != kRunning && state != kStopping) {
413 DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": not running";
414 return;
417 if (state == kStopping) {
418 if (NumDelayedRenderTasks() == 0 && !last_frame_posted_) {
419 DVLOG(1) << class_name() << "::" << __FUNCTION__
420 << ": kStopping, posting OnLastFrameRendered";
421 media_task_runner_->PostTask(
422 FROM_HERE, base::Bind(&MediaCodecDecoder::OnLastFrameRendered,
423 weak_factory_.GetWeakPtr(), false));
424 last_frame_posted_ = true;
427 // We can stop processing, the |au_queue_| and MediaCodec queues can freeze.
428 // We only need to let finish the delayed rendering tasks.
429 return;
432 DCHECK(state == kRunning);
434 if (!EnqueueInputBuffer())
435 return;
437 bool eos_encountered = false;
438 if (!DepleteOutputBufferQueue(&eos_encountered))
439 return;
441 if (eos_encountered) {
442 DVLOG(1) << class_name() << "::" << __FUNCTION__
443 << " EOS dequeued, stopping frame processing";
444 return;
447 // We need a small delay if we want to stop this thread by
448 // decoder_thread_.Stop() reliably.
449 // The decoder thread message loop processes all pending
450 // (but not delayed) tasks before it can quit; without a delay
451 // the message loop might be forever processing the pendng tasks.
452 decoder_thread_.task_runner()->PostDelayedTask(
453 FROM_HERE,
454 base::Bind(&MediaCodecDecoder::ProcessNextFrame, base::Unretained(this)),
455 base::TimeDelta::FromMilliseconds(kNextFrameDelay));
458 // Returns false if we should stop decoding process. Right now
459 // it happens if we got MediaCodec error or detected starvation.
460 bool MediaCodecDecoder::EnqueueInputBuffer() {
461 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
463 DVLOG(2) << class_name() << "::" << __FUNCTION__;
465 if (eos_enqueued_) {
466 DVLOG(1) << class_name() << "::" << __FUNCTION__
467 << ": eos_enqueued, returning";
468 return true; // Nothing to do
471 // Keep the number pending video frames low, ideally maintaining
472 // the same audio and video duration after stop request
473 if (NumDelayedRenderTasks() > 1) {
474 DVLOG(2) << class_name() << "::" << __FUNCTION__ << ": # delayed buffers ("
475 << NumDelayedRenderTasks() << ") exceeds 1, returning";
476 return true; // Nothing to do
479 // Get the next frame from the queue and the queue info
481 AccessUnitQueue::Info au_info = au_queue_.GetInfo();
483 // Request the data from Demuxer
484 if (au_info.length <= kPlaybackLowLimit && !au_info.has_eos)
485 media_task_runner_->PostTask(FROM_HERE, request_data_cb_);
487 // Get the next frame from the queue
489 if (!au_info.length) {
490 // Report starvation and return, Start() will be called again later.
491 DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": starvation detected";
492 media_task_runner_->PostTask(FROM_HERE, starvation_cb_);
493 return true;
496 if (au_info.configs) {
497 DVLOG(1) << class_name() << "::" << __FUNCTION__
498 << ": received new configs, not implemented";
499 // post an error for now?
500 media_task_runner_->PostTask(FROM_HERE, internal_error_cb_);
501 return false;
504 // We are ready to enqueue the front unit.
506 #ifndef NDEBUG
507 if (verify_next_frame_is_key_) {
508 verify_next_frame_is_key_ = false;
509 VerifyUnitIsKeyFrame(au_info.front_unit);
511 #endif
513 // Dequeue input buffer
515 base::TimeDelta timeout =
516 base::TimeDelta::FromMilliseconds(kInputBufferTimeout);
517 int index = -1;
518 MediaCodecStatus status =
519 media_codec_bridge_->DequeueInputBuffer(timeout, &index);
521 DVLOG(2) << class_name() << ":: DequeueInputBuffer index:" << index;
523 switch (status) {
524 case MEDIA_CODEC_ERROR:
525 DVLOG(0) << class_name() << "::" << __FUNCTION__
526 << ": MEDIA_CODEC_ERROR DequeueInputBuffer failed";
527 media_task_runner_->PostTask(FROM_HERE, internal_error_cb_);
528 return false;
530 case MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER:
531 return true;
533 default:
534 break;
537 // We got the buffer
538 DCHECK_EQ(status, MEDIA_CODEC_OK);
539 DCHECK_GE(index, 0);
541 const AccessUnit* unit = au_info.front_unit;
542 DCHECK(unit);
544 if (unit->is_end_of_stream) {
545 DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": QueueEOS";
546 media_codec_bridge_->QueueEOS(index);
547 eos_enqueued_ = true;
548 return true;
551 DVLOG(2) << class_name() << ":: QueueInputBuffer pts:" << unit->timestamp;
553 status = media_codec_bridge_->QueueInputBuffer(
554 index, &unit->data[0], unit->data.size(), unit->timestamp);
556 if (status == MEDIA_CODEC_ERROR) {
557 DVLOG(0) << class_name() << "::" << __FUNCTION__
558 << ": MEDIA_CODEC_ERROR: QueueInputBuffer failed";
559 media_task_runner_->PostTask(FROM_HERE, internal_error_cb_);
560 return false;
563 // Have successfully queued input buffer, go to next access unit.
564 au_queue_.Advance();
565 return true;
568 // Returns false if there was MediaCodec error.
569 bool MediaCodecDecoder::DepleteOutputBufferQueue(bool* eos_encountered) {
570 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread());
572 DVLOG(2) << class_name() << "::" << __FUNCTION__;
574 int buffer_index = 0;
575 size_t offset = 0;
576 size_t size = 0;
577 base::TimeDelta pts;
578 MediaCodecStatus status;
580 base::TimeDelta timeout =
581 base::TimeDelta::FromMilliseconds(kOutputBufferTimeout);
583 // Extract all output buffers that are available.
584 // Usually there will be only one, but sometimes it is preceeded by
585 // MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED or MEDIA_CODEC_OUTPUT_FORMAT_CHANGED.
586 do {
587 status = media_codec_bridge_->DequeueOutputBuffer(
588 timeout, &buffer_index, &offset, &size, &pts, eos_encountered, nullptr);
590 // Reset the timeout to 0 for the subsequent DequeueOutputBuffer() calls
591 // to quickly break the loop after we got all currently available buffers.
592 timeout = base::TimeDelta::FromMilliseconds(0);
594 switch (status) {
595 case MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED:
596 // Output buffers are replaced in MediaCodecBridge, nothing to do.
597 break;
599 case MEDIA_CODEC_OUTPUT_FORMAT_CHANGED:
600 DVLOG(2) << class_name() << "::" << __FUNCTION__
601 << " MEDIA_CODEC_OUTPUT_FORMAT_CHANGED";
602 OnOutputFormatChanged();
603 break;
605 case MEDIA_CODEC_OK:
606 // We got the decoded frame
607 Render(buffer_index, size, true, pts, *eos_encountered);
608 break;
610 case MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER:
611 // Nothing to do.
612 break;
614 case MEDIA_CODEC_ERROR:
615 DVLOG(0) << class_name() << "::" << __FUNCTION__
616 << ": MEDIA_CODEC_ERROR from DequeueOutputBuffer";
617 media_task_runner_->PostTask(FROM_HERE, internal_error_cb_);
618 break;
620 default:
621 NOTREACHED();
622 break;
625 } while (status != MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER &&
626 status != MEDIA_CODEC_ERROR && !*eos_encountered);
628 return status != MEDIA_CODEC_ERROR;
631 MediaCodecDecoder::DecoderState MediaCodecDecoder::GetState() const {
632 base::AutoLock lock(state_lock_);
633 return state_;
636 void MediaCodecDecoder::SetState(DecoderState state) {
637 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << state;
639 base::AutoLock lock(state_lock_);
640 state_ = state;
643 #undef RETURN_STRING
644 #define RETURN_STRING(x) \
645 case x: \
646 return #x;
648 const char* MediaCodecDecoder::AsString(DecoderState state) {
649 switch (state) {
650 RETURN_STRING(kStopped);
651 RETURN_STRING(kPrefetching);
652 RETURN_STRING(kPrefetched);
653 RETURN_STRING(kRunning);
654 RETURN_STRING(kStopping);
655 RETURN_STRING(kError);
656 default:
657 return "Unknown DecoderState";
661 #undef RETURN_STRING
663 } // namespace media