Roll src/third_party/WebKit aa8346d:dbb8a38 (svn 202629:202630)
[chromium-blink-merge.git] / media / base / android / media_decoder_job.cc
blob6a96ccbc59b64cad79d8bb83601e013993499bc9
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/media_decoder_job.h"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/trace_event/trace_event.h"
12 #include "media/base/android/media_drm_bridge.h"
13 #include "media/base/bind_to_current_loop.h"
14 #include "media/base/timestamp_constants.h"
16 namespace media {
18 // Timeout value for media codec operations. Because the first
19 // DequeInputBuffer() can take about 150 milliseconds, use 250 milliseconds
20 // here. See http://b/9357571.
21 static const int kMediaCodecTimeoutInMilliseconds = 250;
23 MediaDecoderJob::MediaDecoderJob(
24 const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner,
25 const base::Closure& request_data_cb,
26 const base::Closure& config_changed_cb)
27 : need_to_reconfig_decoder_job_(false),
28 ui_task_runner_(base::ThreadTaskRunnerHandle::Get()),
29 decoder_task_runner_(decoder_task_runner),
30 needs_flush_(false),
31 input_eos_encountered_(false),
32 output_eos_encountered_(false),
33 skip_eos_enqueue_(true),
34 prerolling_(true),
35 request_data_cb_(request_data_cb),
36 config_changed_cb_(config_changed_cb),
37 current_demuxer_data_index_(0),
38 input_buf_index_(-1),
39 is_content_encrypted_(false),
40 stop_decode_pending_(false),
41 destroy_pending_(false),
42 is_requesting_demuxer_data_(false),
43 is_incoming_data_invalid_(false),
44 release_resources_pending_(false),
45 drm_bridge_(NULL),
46 drain_decoder_(false) {
47 InitializeReceivedData();
48 eos_unit_.is_end_of_stream = true;
51 MediaDecoderJob::~MediaDecoderJob() {
52 ReleaseMediaCodecBridge();
55 void MediaDecoderJob::OnDataReceived(const DemuxerData& data) {
56 DVLOG(1) << __FUNCTION__ << ": " << data.access_units.size() << " units";
57 DCHECK(ui_task_runner_->BelongsToCurrentThread());
58 DCHECK(NoAccessUnitsRemainingInChunk(false));
60 TRACE_EVENT_ASYNC_END2(
61 "media", "MediaDecoderJob::RequestData", this,
62 "Data type", data.type == media::DemuxerStream::AUDIO ? "AUDIO" : "VIDEO",
63 "Units read", data.access_units.size());
65 if (is_incoming_data_invalid_) {
66 is_incoming_data_invalid_ = false;
68 // If there is a pending callback, need to request the data again to get
69 // valid data.
70 if (!data_received_cb_.is_null())
71 request_data_cb_.Run();
72 else
73 is_requesting_demuxer_data_ = false;
74 return;
77 size_t next_demuxer_data_index = inactive_demuxer_data_index();
78 received_data_[next_demuxer_data_index] = data;
79 access_unit_index_[next_demuxer_data_index] = 0;
80 is_requesting_demuxer_data_ = false;
82 base::Closure done_cb = base::ResetAndReturn(&data_received_cb_);
84 // If this data request is for the inactive chunk, or |data_received_cb_|
85 // was set to null by Flush() or Release(), do nothing.
86 if (done_cb.is_null())
87 return;
89 if (stop_decode_pending_) {
90 DCHECK(is_decoding());
91 OnDecodeCompleted(MEDIA_CODEC_ABORT, kNoTimestamp(), kNoTimestamp());
92 return;
95 done_cb.Run();
98 void MediaDecoderJob::Prefetch(const base::Closure& prefetch_cb) {
99 DCHECK(ui_task_runner_->BelongsToCurrentThread());
100 DCHECK(data_received_cb_.is_null());
101 DCHECK(decode_cb_.is_null());
103 if (HasData()) {
104 DVLOG(1) << __FUNCTION__ << " : using previously received data";
105 ui_task_runner_->PostTask(FROM_HERE, prefetch_cb);
106 return;
109 DVLOG(1) << __FUNCTION__ << " : requesting data";
110 RequestData(prefetch_cb);
113 MediaDecoderJob::MediaDecoderJobStatus MediaDecoderJob::Decode(
114 base::TimeTicks start_time_ticks,
115 base::TimeDelta start_presentation_timestamp,
116 const DecoderCallback& callback) {
117 DCHECK(decode_cb_.is_null());
118 DCHECK(data_received_cb_.is_null());
119 DCHECK(ui_task_runner_->BelongsToCurrentThread());
120 if (!media_codec_bridge_ || need_to_reconfig_decoder_job_) {
121 if (drain_decoder_)
122 OnDecoderDrained();
123 MediaDecoderJobStatus status = CreateMediaCodecBridge();
124 need_to_reconfig_decoder_job_ = (status != STATUS_SUCCESS);
125 skip_eos_enqueue_ = true;
126 if (need_to_reconfig_decoder_job_)
127 return status;
130 decode_cb_ = callback;
132 if (!HasData()) {
133 RequestData(base::Bind(&MediaDecoderJob::DecodeCurrentAccessUnit,
134 base::Unretained(this),
135 start_time_ticks,
136 start_presentation_timestamp));
137 return STATUS_SUCCESS;
140 DecodeCurrentAccessUnit(start_time_ticks, start_presentation_timestamp);
141 return STATUS_SUCCESS;
144 void MediaDecoderJob::StopDecode() {
145 DCHECK(ui_task_runner_->BelongsToCurrentThread());
146 DCHECK(is_decoding());
147 stop_decode_pending_ = true;
150 bool MediaDecoderJob::OutputEOSReached() const {
151 return !drain_decoder_ && output_eos_encountered_;
154 void MediaDecoderJob::SetDrmBridge(MediaDrmBridge* drm_bridge) {
155 drm_bridge_ = drm_bridge;
156 need_to_reconfig_decoder_job_ = true;
159 void MediaDecoderJob::Flush() {
160 DVLOG(1) << __FUNCTION__;
161 DCHECK(ui_task_runner_->BelongsToCurrentThread());
162 DCHECK(data_received_cb_.is_null());
163 DCHECK(decode_cb_.is_null());
165 // Clean up the received data.
166 current_demuxer_data_index_ = 0;
167 InitializeReceivedData();
168 if (is_requesting_demuxer_data_)
169 is_incoming_data_invalid_ = true;
170 input_eos_encountered_ = false;
171 output_eos_encountered_ = false;
172 drain_decoder_ = false;
174 // Do nothing, flush when the next Decode() happens.
175 needs_flush_ = true;
178 void MediaDecoderJob::BeginPrerolling(base::TimeDelta preroll_timestamp) {
179 DVLOG(1) << __FUNCTION__ << "(" << preroll_timestamp.InSecondsF() << ")";
180 DCHECK(ui_task_runner_->BelongsToCurrentThread());
181 DCHECK(!is_decoding());
183 preroll_timestamp_ = preroll_timestamp;
184 prerolling_ = true;
187 void MediaDecoderJob::ReleaseDecoderResources() {
188 DVLOG(1) << __FUNCTION__;
189 DCHECK(ui_task_runner_->BelongsToCurrentThread());
190 if (decode_cb_.is_null()) {
191 DCHECK(!drain_decoder_);
192 // Since the decoder job is not decoding data, we can safely destroy
193 // |media_codec_bridge_|.
194 ReleaseMediaCodecBridge();
195 return;
198 // Release |media_codec_bridge_| once decoding is completed.
199 release_resources_pending_ = true;
202 base::android::ScopedJavaLocalRef<jobject> MediaDecoderJob::GetMediaCrypto() {
203 base::android::ScopedJavaLocalRef<jobject> media_crypto;
204 if (drm_bridge_)
205 media_crypto = drm_bridge_->GetMediaCrypto();
206 return media_crypto;
209 bool MediaDecoderJob::SetCurrentFrameToPreviouslyCachedKeyFrame() {
210 const std::vector<AccessUnit>& access_units =
211 received_data_[current_demuxer_data_index_].access_units;
212 // If the current data chunk is empty, the player must be in an initial or
213 // seek state. The next access unit will always be a key frame.
214 if (access_units.size() == 0)
215 return true;
217 // Find key frame in all the access units the decoder have decoded,
218 // or is about to decode.
219 int i = std::min(access_unit_index_[current_demuxer_data_index_],
220 access_units.size() - 1);
221 for (; i >= 0; --i) {
222 // Config change is always the last access unit, and it always come with
223 // a key frame afterwards.
224 if (access_units[i].status == DemuxerStream::kConfigChanged)
225 return true;
226 if (access_units[i].is_key_frame) {
227 access_unit_index_[current_demuxer_data_index_] = i;
228 return true;
231 return false;
235 void MediaDecoderJob::Release() {
236 DCHECK(ui_task_runner_->BelongsToCurrentThread());
237 DVLOG(1) << __FUNCTION__;
239 // If the decoder job is still decoding, we cannot delete the job immediately.
240 destroy_pending_ = is_decoding();
242 request_data_cb_.Reset();
243 data_received_cb_.Reset();
244 decode_cb_.Reset();
246 if (destroy_pending_) {
247 DVLOG(1) << __FUNCTION__ << " : delete is pending decode completion";
248 return;
251 delete this;
254 MediaCodecStatus MediaDecoderJob::QueueInputBuffer(const AccessUnit& unit) {
255 DVLOG(1) << __FUNCTION__;
256 DCHECK(decoder_task_runner_->BelongsToCurrentThread());
257 TRACE_EVENT0("media", __FUNCTION__);
259 int input_buf_index = input_buf_index_;
260 input_buf_index_ = -1;
262 // TODO(xhwang): Hide DequeueInputBuffer() and the index in MediaCodecBridge.
263 if (input_buf_index == -1) {
264 base::TimeDelta timeout = base::TimeDelta::FromMilliseconds(
265 kMediaCodecTimeoutInMilliseconds);
266 MediaCodecStatus status =
267 media_codec_bridge_->DequeueInputBuffer(timeout, &input_buf_index);
268 if (status != MEDIA_CODEC_OK) {
269 DVLOG(1) << "DequeueInputBuffer fails: " << status;
270 return status;
274 // TODO(qinmin): skip frames if video is falling far behind.
275 DCHECK_GE(input_buf_index, 0);
276 if (unit.is_end_of_stream || unit.data.empty()) {
277 media_codec_bridge_->QueueEOS(input_buf_index);
278 return MEDIA_CODEC_INPUT_END_OF_STREAM;
281 if (unit.key_id.empty() || unit.iv.empty()) {
282 DCHECK(unit.iv.empty() || !unit.key_id.empty());
283 return media_codec_bridge_->QueueInputBuffer(
284 input_buf_index, &unit.data[0], unit.data.size(), unit.timestamp);
287 MediaCodecStatus status = media_codec_bridge_->QueueSecureInputBuffer(
288 input_buf_index,
289 &unit.data[0], unit.data.size(),
290 reinterpret_cast<const uint8*>(&unit.key_id[0]), unit.key_id.size(),
291 reinterpret_cast<const uint8*>(&unit.iv[0]), unit.iv.size(),
292 unit.subsamples.empty() ? NULL : &unit.subsamples[0],
293 unit.subsamples.size(),
294 unit.timestamp);
296 // In case of MEDIA_CODEC_NO_KEY, we must reuse the |input_buf_index_|.
297 // Otherwise MediaDrm will report errors.
298 if (status == MEDIA_CODEC_NO_KEY)
299 input_buf_index_ = input_buf_index;
301 return status;
304 bool MediaDecoderJob::HasData() const {
305 DCHECK(ui_task_runner_->BelongsToCurrentThread());
306 // When |input_eos_encountered_| is set, |access_unit_index_| and
307 // |current_demuxer_data_index_| must be pointing to an EOS unit,
308 // or a |kConfigChanged| unit if |drain_decoder_| is true. In both cases,
309 // we'll feed an EOS input unit to drain the decoder until we hit output EOS.
310 DCHECK(!input_eos_encountered_ || !NoAccessUnitsRemainingInChunk(true));
311 return !NoAccessUnitsRemainingInChunk(true) ||
312 !NoAccessUnitsRemainingInChunk(false);
315 void MediaDecoderJob::RequestData(const base::Closure& done_cb) {
316 DVLOG(1) << __FUNCTION__;
317 DCHECK(ui_task_runner_->BelongsToCurrentThread());
318 DCHECK(data_received_cb_.is_null());
319 DCHECK(!input_eos_encountered_);
320 DCHECK(NoAccessUnitsRemainingInChunk(false));
322 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaDecoderJob::RequestData", this);
324 data_received_cb_ = done_cb;
326 // If we are already expecting new data, just set the callback and do
327 // nothing.
328 if (is_requesting_demuxer_data_)
329 return;
331 // The new incoming data will be stored as the next demuxer data chunk, since
332 // the decoder might still be decoding the current one.
333 size_t next_demuxer_data_index = inactive_demuxer_data_index();
334 received_data_[next_demuxer_data_index] = DemuxerData();
335 access_unit_index_[next_demuxer_data_index] = 0;
336 is_requesting_demuxer_data_ = true;
338 request_data_cb_.Run();
341 void MediaDecoderJob::DecodeCurrentAccessUnit(
342 base::TimeTicks start_time_ticks,
343 base::TimeDelta start_presentation_timestamp) {
344 DCHECK(ui_task_runner_->BelongsToCurrentThread());
345 DCHECK(!decode_cb_.is_null());
347 RequestCurrentChunkIfEmpty();
348 const AccessUnit& access_unit = CurrentAccessUnit();
349 if (CurrentAccessUnit().status == DemuxerStream::kConfigChanged) {
350 int index = CurrentReceivedDataChunkIndex();
351 const DemuxerConfigs& configs = received_data_[index].demuxer_configs[0];
352 bool reconfigure_needed = IsCodecReconfigureNeeded(configs);
353 SetDemuxerConfigs(configs);
354 if (!drain_decoder_) {
355 // If we haven't decoded any data yet, just skip the current access unit
356 // and request the MediaCodec to be recreated on next Decode().
357 if (skip_eos_enqueue_ || !reconfigure_needed) {
358 need_to_reconfig_decoder_job_ =
359 need_to_reconfig_decoder_job_ || reconfigure_needed;
360 // Report MEDIA_CODEC_OK status so decoder will continue decoding and
361 // MEDIA_CODEC_OUTPUT_FORMAT_CHANGED status will come later.
362 ui_task_runner_->PostTask(FROM_HERE, base::Bind(
363 &MediaDecoderJob::OnDecodeCompleted, base::Unretained(this),
364 MEDIA_CODEC_OK, kNoTimestamp(), kNoTimestamp()));
365 return;
367 // Start draining the decoder so that all the remaining frames are
368 // rendered.
369 drain_decoder_ = true;
373 DCHECK(!(needs_flush_ && drain_decoder_));
374 decoder_task_runner_->PostTask(FROM_HERE, base::Bind(
375 &MediaDecoderJob::DecodeInternal, base::Unretained(this),
376 drain_decoder_ ? eos_unit_ : access_unit,
377 start_time_ticks, start_presentation_timestamp, needs_flush_,
378 media::BindToCurrentLoop(base::Bind(
379 &MediaDecoderJob::OnDecodeCompleted, base::Unretained(this)))));
380 needs_flush_ = false;
383 void MediaDecoderJob::DecodeInternal(
384 const AccessUnit& unit,
385 base::TimeTicks start_time_ticks,
386 base::TimeDelta start_presentation_timestamp,
387 bool needs_flush,
388 const MediaDecoderJob::DecoderCallback& callback) {
389 DVLOG(1) << __FUNCTION__;
390 DCHECK(decoder_task_runner_->BelongsToCurrentThread());
391 TRACE_EVENT0("media", __FUNCTION__);
393 if (needs_flush) {
394 DVLOG(1) << "DecodeInternal needs flush.";
395 input_eos_encountered_ = false;
396 output_eos_encountered_ = false;
397 input_buf_index_ = -1;
398 MediaCodecStatus reset_status = media_codec_bridge_->Reset();
399 if (MEDIA_CODEC_OK != reset_status) {
400 callback.Run(reset_status, kNoTimestamp(), kNoTimestamp());
401 return;
405 // Once output EOS has occurred, we should not be asked to decode again.
406 // MediaCodec has undefined behavior if similarly asked to decode after output
407 // EOS.
408 DCHECK(!output_eos_encountered_);
410 // For aborted access unit, just skip it and inform the player.
411 if (unit.status == DemuxerStream::kAborted) {
412 callback.Run(MEDIA_CODEC_ABORT, kNoTimestamp(), kNoTimestamp());
413 return;
416 if (skip_eos_enqueue_) {
417 if (unit.is_end_of_stream || unit.data.empty()) {
418 input_eos_encountered_ = true;
419 output_eos_encountered_ = true;
420 callback.Run(MEDIA_CODEC_OUTPUT_END_OF_STREAM, kNoTimestamp(),
421 kNoTimestamp());
422 return;
425 skip_eos_enqueue_ = false;
428 MediaCodecStatus input_status = MEDIA_CODEC_INPUT_END_OF_STREAM;
429 if (!input_eos_encountered_) {
430 input_status = QueueInputBuffer(unit);
431 if (input_status == MEDIA_CODEC_INPUT_END_OF_STREAM) {
432 input_eos_encountered_ = true;
433 } else if (input_status == MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER) {
434 // In some cases, all buffers must be released to codec before format
435 // change can be resolved. Context: b/21786703
436 DVLOG(1) << "dequeueInputBuffer gave AGAIN_LATER, dequeue output buffers";
437 } else if (input_status != MEDIA_CODEC_OK) {
438 callback.Run(input_status, kNoTimestamp(), kNoTimestamp());
439 return;
443 int buffer_index = 0;
444 size_t offset = 0;
445 size_t size = 0;
446 base::TimeDelta presentation_timestamp;
448 base::TimeDelta timeout = base::TimeDelta::FromMilliseconds(
449 kMediaCodecTimeoutInMilliseconds);
451 MediaCodecStatus status = MEDIA_CODEC_OK;
452 bool has_format_change = false;
453 // Dequeue the output buffer until a MEDIA_CODEC_OK, MEDIA_CODEC_ERROR or
454 // MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER is received.
455 do {
456 status = media_codec_bridge_->DequeueOutputBuffer(
457 timeout,
458 &buffer_index,
459 &offset,
460 &size,
461 &presentation_timestamp,
462 &output_eos_encountered_,
463 NULL);
464 if (status == MEDIA_CODEC_OUTPUT_FORMAT_CHANGED) {
465 // TODO(qinmin): instead of waiting for the next output buffer to be
466 // dequeued, post a task on the UI thread to signal the format change.
467 OnOutputFormatChanged();
468 has_format_change = true;
470 } while (status != MEDIA_CODEC_OK && status != MEDIA_CODEC_ERROR &&
471 status != MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER);
473 if (status != MEDIA_CODEC_OK) {
474 callback.Run(status, kNoTimestamp(), kNoTimestamp());
475 return;
478 // TODO(xhwang/qinmin): This logic is correct but strange. Clean it up.
479 if (output_eos_encountered_)
480 status = MEDIA_CODEC_OUTPUT_END_OF_STREAM;
481 else if (has_format_change)
482 status = MEDIA_CODEC_OUTPUT_FORMAT_CHANGED;
484 bool render_output = presentation_timestamp >= preroll_timestamp_ &&
485 (status != MEDIA_CODEC_OUTPUT_END_OF_STREAM || size != 0u);
486 base::TimeDelta time_to_render;
487 DCHECK(!start_time_ticks.is_null());
488 if (render_output && ComputeTimeToRender()) {
489 time_to_render = presentation_timestamp - (base::TimeTicks::Now() -
490 start_time_ticks + start_presentation_timestamp);
493 if (time_to_render > base::TimeDelta()) {
494 decoder_task_runner_->PostDelayedTask(
495 FROM_HERE,
496 base::Bind(&MediaDecoderJob::ReleaseOutputBuffer,
497 base::Unretained(this),
498 buffer_index,
499 offset,
500 size,
501 render_output,
502 presentation_timestamp,
503 base::Bind(callback, status)),
504 time_to_render);
505 return;
508 // TODO(qinmin): The codec is lagging behind, need to recalculate the
509 // |start_presentation_timestamp_| and |start_time_ticks_| in
510 // media_source_player.cc.
511 DVLOG(1) << "codec is lagging behind :" << time_to_render.InMicroseconds();
512 if (render_output) {
513 // The player won't expect a timestamp smaller than the
514 // |start_presentation_timestamp|. However, this could happen due to decoder
515 // errors.
516 presentation_timestamp = std::max(
517 presentation_timestamp, start_presentation_timestamp);
518 } else {
519 presentation_timestamp = kNoTimestamp();
521 ReleaseOutputCompletionCallback completion_callback = base::Bind(
522 callback, status);
523 ReleaseOutputBuffer(buffer_index, offset, size, render_output,
524 presentation_timestamp, completion_callback);
527 void MediaDecoderJob::OnDecodeCompleted(
528 MediaCodecStatus status, base::TimeDelta current_presentation_timestamp,
529 base::TimeDelta max_presentation_timestamp) {
530 DCHECK(ui_task_runner_->BelongsToCurrentThread());
532 if (destroy_pending_) {
533 DVLOG(1) << __FUNCTION__ << " : completing pending deletion";
534 delete this;
535 return;
538 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM)
539 output_eos_encountered_ = true;
541 DCHECK(!decode_cb_.is_null());
543 // If output was queued for rendering, then we have completed prerolling.
544 if (current_presentation_timestamp != kNoTimestamp() ||
545 status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) {
546 prerolling_ = false;
549 switch (status) {
550 case MEDIA_CODEC_OK:
551 case MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER:
552 case MEDIA_CODEC_OUTPUT_FORMAT_CHANGED:
553 case MEDIA_CODEC_OUTPUT_END_OF_STREAM:
554 if (!input_eos_encountered_)
555 access_unit_index_[current_demuxer_data_index_]++;
556 break;
558 case MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER:
559 case MEDIA_CODEC_INPUT_END_OF_STREAM:
560 case MEDIA_CODEC_NO_KEY:
561 case MEDIA_CODEC_ABORT:
562 case MEDIA_CODEC_ERROR:
563 // Do nothing.
564 break;
566 case MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED:
567 DCHECK(false) << "Invalid output status";
568 break;
571 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM && drain_decoder_) {
572 OnDecoderDrained();
573 status = MEDIA_CODEC_OK;
576 if (status == MEDIA_CODEC_OUTPUT_FORMAT_CHANGED) {
577 if (UpdateOutputFormat())
578 config_changed_cb_.Run();
579 status = MEDIA_CODEC_OK;
582 if (release_resources_pending_) {
583 ReleaseMediaCodecBridge();
584 release_resources_pending_ = false;
585 if (drain_decoder_)
586 OnDecoderDrained();
589 stop_decode_pending_ = false;
590 base::ResetAndReturn(&decode_cb_).Run(
591 status, current_presentation_timestamp, max_presentation_timestamp);
594 const AccessUnit& MediaDecoderJob::CurrentAccessUnit() const {
595 DCHECK(ui_task_runner_->BelongsToCurrentThread());
596 DCHECK(HasData());
597 size_t index = CurrentReceivedDataChunkIndex();
598 return received_data_[index].access_units[access_unit_index_[index]];
601 size_t MediaDecoderJob::CurrentReceivedDataChunkIndex() const {
602 return NoAccessUnitsRemainingInChunk(true) ?
603 inactive_demuxer_data_index() : current_demuxer_data_index_;
606 bool MediaDecoderJob::NoAccessUnitsRemainingInChunk(
607 bool is_active_chunk) const {
608 DCHECK(ui_task_runner_->BelongsToCurrentThread());
609 size_t index = is_active_chunk ? current_demuxer_data_index_ :
610 inactive_demuxer_data_index();
611 return received_data_[index].access_units.size() <= access_unit_index_[index];
614 void MediaDecoderJob::RequestCurrentChunkIfEmpty() {
615 DCHECK(ui_task_runner_->BelongsToCurrentThread());
616 DCHECK(HasData());
617 if (!NoAccessUnitsRemainingInChunk(true))
618 return;
620 // Requests new data if the the last access unit of the next chunk is not EOS.
621 current_demuxer_data_index_ = inactive_demuxer_data_index();
622 const AccessUnit& last_access_unit =
623 received_data_[current_demuxer_data_index_].access_units.back();
624 if (!last_access_unit.is_end_of_stream &&
625 last_access_unit.status != DemuxerStream::kAborted) {
626 RequestData(base::Closure());
630 void MediaDecoderJob::InitializeReceivedData() {
631 for (size_t i = 0; i < 2; ++i) {
632 received_data_[i] = DemuxerData();
633 access_unit_index_[i] = 0;
637 void MediaDecoderJob::OnDecoderDrained() {
638 DVLOG(1) << __FUNCTION__;
639 DCHECK(ui_task_runner_->BelongsToCurrentThread());
640 DCHECK(drain_decoder_);
642 input_eos_encountered_ = false;
643 output_eos_encountered_ = false;
644 drain_decoder_ = false;
645 ReleaseMediaCodecBridge();
646 // Increase the access unit index so that the new decoder will not handle
647 // the config change again.
648 access_unit_index_[current_demuxer_data_index_]++;
651 MediaDecoderJob::MediaDecoderJobStatus
652 MediaDecoderJob::CreateMediaCodecBridge() {
653 DVLOG(1) << __FUNCTION__;
654 DCHECK(ui_task_runner_->BelongsToCurrentThread());
655 DCHECK(decode_cb_.is_null());
657 if (!HasStream()) {
658 ReleaseMediaCodecBridge();
659 return STATUS_FAILURE;
662 // Create |media_codec_bridge_| only if config changes.
663 if (media_codec_bridge_ && !need_to_reconfig_decoder_job_)
664 return STATUS_SUCCESS;
666 base::android::ScopedJavaLocalRef<jobject> media_crypto = GetMediaCrypto();
667 if (is_content_encrypted_ && media_crypto.is_null())
668 return STATUS_FAILURE;
670 ReleaseMediaCodecBridge();
671 DVLOG(1) << __FUNCTION__ << " : creating new media codec bridge";
673 return CreateMediaCodecBridgeInternal();
676 bool MediaDecoderJob::IsCodecReconfigureNeeded(
677 const DemuxerConfigs& configs) const {
678 if (!AreDemuxerConfigsChanged(configs))
679 return false;
680 return true;
683 void MediaDecoderJob::OnOutputFormatChanged() {}
685 bool MediaDecoderJob::UpdateOutputFormat() {
686 return false;
689 void MediaDecoderJob::ReleaseMediaCodecBridge() {
690 if (!media_codec_bridge_)
691 return;
693 media_codec_bridge_.reset();
694 input_buf_index_ = -1;
697 } // namespace media