Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / base / android / media_decoder_job.cc
blob90094439605c4380a35021bf59ab8daa763c06ce
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/message_loop/message_loop_proxy.h"
10 #include "base/trace_event/trace_event.h"
11 #include "media/base/android/media_codec_bridge.h"
12 #include "media/base/android/media_drm_bridge.h"
13 #include "media/base/bind_to_current_loop.h"
14 #include "media/base/buffers.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::MessageLoopProxy::current()),
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 MediaCodecStatus reset_status = media_codec_bridge_->Reset();
398 if (MEDIA_CODEC_OK != reset_status) {
399 callback.Run(reset_status, kNoTimestamp(), kNoTimestamp());
400 return;
404 // Once output EOS has occurred, we should not be asked to decode again.
405 // MediaCodec has undefined behavior if similarly asked to decode after output
406 // EOS.
407 DCHECK(!output_eos_encountered_);
409 // For aborted access unit, just skip it and inform the player.
410 if (unit.status == DemuxerStream::kAborted) {
411 callback.Run(MEDIA_CODEC_ABORT, kNoTimestamp(), kNoTimestamp());
412 return;
415 if (skip_eos_enqueue_) {
416 if (unit.is_end_of_stream || unit.data.empty()) {
417 input_eos_encountered_ = true;
418 output_eos_encountered_ = true;
419 callback.Run(MEDIA_CODEC_OUTPUT_END_OF_STREAM, kNoTimestamp(),
420 kNoTimestamp());
421 return;
424 skip_eos_enqueue_ = false;
427 MediaCodecStatus input_status = MEDIA_CODEC_INPUT_END_OF_STREAM;
428 if (!input_eos_encountered_) {
429 input_status = QueueInputBuffer(unit);
430 if (input_status == MEDIA_CODEC_INPUT_END_OF_STREAM) {
431 input_eos_encountered_ = true;
432 } else if (input_status != MEDIA_CODEC_OK) {
433 callback.Run(input_status, kNoTimestamp(), kNoTimestamp());
434 return;
438 int buffer_index = 0;
439 size_t offset = 0;
440 size_t size = 0;
441 base::TimeDelta presentation_timestamp;
443 base::TimeDelta timeout = base::TimeDelta::FromMilliseconds(
444 kMediaCodecTimeoutInMilliseconds);
446 MediaCodecStatus status = MEDIA_CODEC_OK;
447 bool has_format_change = false;
448 // Dequeue the output buffer until a MEDIA_CODEC_OK, MEDIA_CODEC_ERROR or
449 // MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER is received.
450 do {
451 status = media_codec_bridge_->DequeueOutputBuffer(
452 timeout,
453 &buffer_index,
454 &offset,
455 &size,
456 &presentation_timestamp,
457 &output_eos_encountered_,
458 NULL);
459 if (status == MEDIA_CODEC_OUTPUT_FORMAT_CHANGED) {
460 // TODO(qinmin): instead of waiting for the next output buffer to be
461 // dequeued, post a task on the UI thread to signal the format change.
462 OnOutputFormatChanged();
463 has_format_change = true;
465 } while (status != MEDIA_CODEC_OK && status != MEDIA_CODEC_ERROR &&
466 status != MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER);
468 if (status != MEDIA_CODEC_OK) {
469 callback.Run(status, kNoTimestamp(), kNoTimestamp());
470 return;
473 // TODO(xhwang/qinmin): This logic is correct but strange. Clean it up.
474 if (output_eos_encountered_)
475 status = MEDIA_CODEC_OUTPUT_END_OF_STREAM;
476 else if (has_format_change)
477 status = MEDIA_CODEC_OUTPUT_FORMAT_CHANGED;
479 bool render_output = presentation_timestamp >= preroll_timestamp_ &&
480 (status != MEDIA_CODEC_OUTPUT_END_OF_STREAM || size != 0u);
481 base::TimeDelta time_to_render;
482 DCHECK(!start_time_ticks.is_null());
483 if (render_output && ComputeTimeToRender()) {
484 time_to_render = presentation_timestamp - (base::TimeTicks::Now() -
485 start_time_ticks + start_presentation_timestamp);
488 if (time_to_render > base::TimeDelta()) {
489 decoder_task_runner_->PostDelayedTask(
490 FROM_HERE,
491 base::Bind(&MediaDecoderJob::ReleaseOutputBuffer,
492 base::Unretained(this),
493 buffer_index,
494 size,
495 render_output,
496 presentation_timestamp,
497 base::Bind(callback, status)),
498 time_to_render);
499 return;
502 // TODO(qinmin): The codec is lagging behind, need to recalculate the
503 // |start_presentation_timestamp_| and |start_time_ticks_| in
504 // media_source_player.cc.
505 DVLOG(1) << "codec is lagging behind :" << time_to_render.InMicroseconds();
506 if (render_output) {
507 // The player won't expect a timestamp smaller than the
508 // |start_presentation_timestamp|. However, this could happen due to decoder
509 // errors.
510 presentation_timestamp = std::max(
511 presentation_timestamp, start_presentation_timestamp);
512 } else {
513 presentation_timestamp = kNoTimestamp();
515 ReleaseOutputCompletionCallback completion_callback = base::Bind(
516 callback, status);
517 ReleaseOutputBuffer(buffer_index, size, render_output, presentation_timestamp,
518 completion_callback);
521 void MediaDecoderJob::OnDecodeCompleted(
522 MediaCodecStatus status, base::TimeDelta current_presentation_timestamp,
523 base::TimeDelta max_presentation_timestamp) {
524 DCHECK(ui_task_runner_->BelongsToCurrentThread());
526 if (destroy_pending_) {
527 DVLOG(1) << __FUNCTION__ << " : completing pending deletion";
528 delete this;
529 return;
532 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM)
533 output_eos_encountered_ = true;
535 DCHECK(!decode_cb_.is_null());
537 // If output was queued for rendering, then we have completed prerolling.
538 if (current_presentation_timestamp != kNoTimestamp() ||
539 status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) {
540 prerolling_ = false;
543 switch (status) {
544 case MEDIA_CODEC_OK:
545 case MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER:
546 case MEDIA_CODEC_OUTPUT_FORMAT_CHANGED:
547 case MEDIA_CODEC_OUTPUT_END_OF_STREAM:
548 if (!input_eos_encountered_)
549 access_unit_index_[current_demuxer_data_index_]++;
550 break;
552 case MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER:
553 case MEDIA_CODEC_INPUT_END_OF_STREAM:
554 case MEDIA_CODEC_NO_KEY:
555 case MEDIA_CODEC_ABORT:
556 case MEDIA_CODEC_ERROR:
557 // Do nothing.
558 break;
560 case MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED:
561 DCHECK(false) << "Invalid output status";
562 break;
565 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM && drain_decoder_) {
566 OnDecoderDrained();
567 status = MEDIA_CODEC_OK;
570 if (status == MEDIA_CODEC_OUTPUT_FORMAT_CHANGED) {
571 if (UpdateOutputFormat())
572 config_changed_cb_.Run();
573 status = MEDIA_CODEC_OK;
576 if (release_resources_pending_) {
577 ReleaseMediaCodecBridge();
578 release_resources_pending_ = false;
579 if (drain_decoder_)
580 OnDecoderDrained();
583 stop_decode_pending_ = false;
584 base::ResetAndReturn(&decode_cb_).Run(
585 status, current_presentation_timestamp, max_presentation_timestamp);
588 const AccessUnit& MediaDecoderJob::CurrentAccessUnit() const {
589 DCHECK(ui_task_runner_->BelongsToCurrentThread());
590 DCHECK(HasData());
591 size_t index = CurrentReceivedDataChunkIndex();
592 return received_data_[index].access_units[access_unit_index_[index]];
595 size_t MediaDecoderJob::CurrentReceivedDataChunkIndex() const {
596 return NoAccessUnitsRemainingInChunk(true) ?
597 inactive_demuxer_data_index() : current_demuxer_data_index_;
600 bool MediaDecoderJob::NoAccessUnitsRemainingInChunk(
601 bool is_active_chunk) const {
602 DCHECK(ui_task_runner_->BelongsToCurrentThread());
603 size_t index = is_active_chunk ? current_demuxer_data_index_ :
604 inactive_demuxer_data_index();
605 return received_data_[index].access_units.size() <= access_unit_index_[index];
608 void MediaDecoderJob::RequestCurrentChunkIfEmpty() {
609 DCHECK(ui_task_runner_->BelongsToCurrentThread());
610 DCHECK(HasData());
611 if (!NoAccessUnitsRemainingInChunk(true))
612 return;
614 // Requests new data if the the last access unit of the next chunk is not EOS.
615 current_demuxer_data_index_ = inactive_demuxer_data_index();
616 const AccessUnit& last_access_unit =
617 received_data_[current_demuxer_data_index_].access_units.back();
618 if (!last_access_unit.is_end_of_stream &&
619 last_access_unit.status != DemuxerStream::kAborted) {
620 RequestData(base::Closure());
624 void MediaDecoderJob::InitializeReceivedData() {
625 for (size_t i = 0; i < 2; ++i) {
626 received_data_[i] = DemuxerData();
627 access_unit_index_[i] = 0;
631 void MediaDecoderJob::OnDecoderDrained() {
632 DVLOG(1) << __FUNCTION__;
633 DCHECK(ui_task_runner_->BelongsToCurrentThread());
634 DCHECK(drain_decoder_);
636 input_eos_encountered_ = false;
637 output_eos_encountered_ = false;
638 drain_decoder_ = false;
639 ReleaseMediaCodecBridge();
640 // Increase the access unit index so that the new decoder will not handle
641 // the config change again.
642 access_unit_index_[current_demuxer_data_index_]++;
645 MediaDecoderJob::MediaDecoderJobStatus
646 MediaDecoderJob::CreateMediaCodecBridge() {
647 DVLOG(1) << __FUNCTION__;
648 DCHECK(ui_task_runner_->BelongsToCurrentThread());
649 DCHECK(decode_cb_.is_null());
651 if (!HasStream()) {
652 ReleaseMediaCodecBridge();
653 return STATUS_FAILURE;
656 // Create |media_codec_bridge_| only if config changes.
657 if (media_codec_bridge_ && !need_to_reconfig_decoder_job_)
658 return STATUS_SUCCESS;
660 base::android::ScopedJavaLocalRef<jobject> media_crypto = GetMediaCrypto();
661 if (is_content_encrypted_ && media_crypto.is_null())
662 return STATUS_FAILURE;
664 ReleaseMediaCodecBridge();
665 DVLOG(1) << __FUNCTION__ << " : creating new media codec bridge";
667 return CreateMediaCodecBridgeInternal();
670 bool MediaDecoderJob::IsCodecReconfigureNeeded(
671 const DemuxerConfigs& configs) const {
672 if (!AreDemuxerConfigsChanged(configs))
673 return false;
674 return true;
677 void MediaDecoderJob::OnOutputFormatChanged() {}
679 bool MediaDecoderJob::UpdateOutputFormat() {
680 return false;
683 void MediaDecoderJob::ReleaseMediaCodecBridge() {
684 if (!media_codec_bridge_)
685 return;
687 media_codec_bridge_.reset();
688 input_buf_index_ = -1;
691 } // namespace media