Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / media / base / android / media_decoder_job.cc
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.
4
5 #include "media/base/android/media_decoder_job.h"
6
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/debug/trace_event.h"
10 #include "base/message_loop/message_loop_proxy.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"
15
16 namespace media {
17
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;
22
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_.end_of_stream = true;
49 }
50
51 MediaDecoderJob::~MediaDecoderJob() {
52   ReleaseMediaCodecBridge();
53 }
54
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));
59
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());
64
65   if (is_incoming_data_invalid_) {
66     is_incoming_data_invalid_ = false;
67
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;
75   }
76
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;
81
82   base::Closure done_cb = base::ResetAndReturn(&data_received_cb_);
83
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;
88
89   if (stop_decode_pending_) {
90     DCHECK(is_decoding());
91     OnDecodeCompleted(MEDIA_CODEC_STOPPED, kNoTimestamp(), kNoTimestamp());
92     return;
93   }
94
95   done_cb.Run();
96 }
97
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());
102
103   if (HasData()) {
104     DVLOG(1) << __FUNCTION__ << " : using previously received data";
105     ui_task_runner_->PostTask(FROM_HERE, prefetch_cb);
106     return;
107   }
108
109   DVLOG(1) << __FUNCTION__ << " : requesting data";
110   RequestData(prefetch_cb);
111 }
112
113 bool 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     need_to_reconfig_decoder_job_ = !CreateMediaCodecBridge();
124     skip_eos_enqueue_ = true;
125     if (need_to_reconfig_decoder_job_)
126       return false;
127   }
128
129   decode_cb_ = callback;
130
131   if (!HasData()) {
132     RequestData(base::Bind(&MediaDecoderJob::DecodeCurrentAccessUnit,
133                            base::Unretained(this),
134                            start_time_ticks,
135                            start_presentation_timestamp));
136     return true;
137   }
138
139   DecodeCurrentAccessUnit(start_time_ticks, start_presentation_timestamp);
140   return true;
141 }
142
143 void MediaDecoderJob::StopDecode() {
144   DCHECK(ui_task_runner_->BelongsToCurrentThread());
145   DCHECK(is_decoding());
146   stop_decode_pending_ = true;
147 }
148
149 bool MediaDecoderJob::OutputEOSReached() const {
150   return !drain_decoder_ && output_eos_encountered_;
151 }
152
153 void MediaDecoderJob::SetDrmBridge(MediaDrmBridge* drm_bridge) {
154   drm_bridge_ = drm_bridge;
155   need_to_reconfig_decoder_job_ = true;
156 }
157
158 void MediaDecoderJob::Flush() {
159   DVLOG(1) << __FUNCTION__;
160   DCHECK(ui_task_runner_->BelongsToCurrentThread());
161   DCHECK(data_received_cb_.is_null());
162   DCHECK(decode_cb_.is_null());
163
164   // Clean up the received data.
165   current_demuxer_data_index_ = 0;
166   InitializeReceivedData();
167   if (is_requesting_demuxer_data_)
168     is_incoming_data_invalid_ = true;
169   input_eos_encountered_ = false;
170   output_eos_encountered_ = false;
171   drain_decoder_ = false;
172
173   // Do nothing, flush when the next Decode() happens.
174   needs_flush_ = true;
175 }
176
177 void MediaDecoderJob::BeginPrerolling(base::TimeDelta preroll_timestamp) {
178   DVLOG(1) << __FUNCTION__ << "(" << preroll_timestamp.InSecondsF() << ")";
179   DCHECK(ui_task_runner_->BelongsToCurrentThread());
180   DCHECK(!is_decoding());
181
182   preroll_timestamp_ = preroll_timestamp;
183   prerolling_ = true;
184 }
185
186 void MediaDecoderJob::ReleaseDecoderResources() {
187   DVLOG(1) << __FUNCTION__;
188   DCHECK(ui_task_runner_->BelongsToCurrentThread());
189   if (decode_cb_.is_null()) {
190     DCHECK(!drain_decoder_);
191     // Since the decoder job is not decoding data, we can safely destroy
192     // |media_codec_bridge_|.
193     ReleaseMediaCodecBridge();
194     return;
195   }
196
197   // Release |media_codec_bridge_| once decoding is completed.
198   release_resources_pending_ = true;
199 }
200
201 base::android::ScopedJavaLocalRef<jobject> MediaDecoderJob::GetMediaCrypto() {
202   base::android::ScopedJavaLocalRef<jobject> media_crypto;
203   if (drm_bridge_)
204     media_crypto = drm_bridge_->GetMediaCrypto();
205   return media_crypto;
206 }
207
208 void MediaDecoderJob::Release() {
209   DCHECK(ui_task_runner_->BelongsToCurrentThread());
210   DVLOG(1) << __FUNCTION__;
211
212   // If the decoder job is still decoding, we cannot delete the job immediately.
213   destroy_pending_ = is_decoding();
214
215   request_data_cb_.Reset();
216   data_received_cb_.Reset();
217   decode_cb_.Reset();
218
219   if (destroy_pending_) {
220     DVLOG(1) << __FUNCTION__ << " : delete is pending decode completion";
221     return;
222   }
223
224   delete this;
225 }
226
227 MediaCodecStatus MediaDecoderJob::QueueInputBuffer(const AccessUnit& unit) {
228   DVLOG(1) << __FUNCTION__;
229   DCHECK(decoder_task_runner_->BelongsToCurrentThread());
230   TRACE_EVENT0("media", __FUNCTION__);
231
232   int input_buf_index = input_buf_index_;
233   input_buf_index_ = -1;
234
235   // TODO(xhwang): Hide DequeueInputBuffer() and the index in MediaCodecBridge.
236   if (input_buf_index == -1) {
237     base::TimeDelta timeout = base::TimeDelta::FromMilliseconds(
238         kMediaCodecTimeoutInMilliseconds);
239     MediaCodecStatus status =
240         media_codec_bridge_->DequeueInputBuffer(timeout, &input_buf_index);
241     if (status != MEDIA_CODEC_OK) {
242       DVLOG(1) << "DequeueInputBuffer fails: " << status;
243       return status;
244     }
245   }
246
247   // TODO(qinmin): skip frames if video is falling far behind.
248   DCHECK_GE(input_buf_index, 0);
249   if (unit.end_of_stream || unit.data.empty()) {
250     media_codec_bridge_->QueueEOS(input_buf_index);
251     return MEDIA_CODEC_INPUT_END_OF_STREAM;
252   }
253
254   if (unit.key_id.empty() || unit.iv.empty()) {
255     DCHECK(unit.iv.empty() || !unit.key_id.empty());
256     return media_codec_bridge_->QueueInputBuffer(
257         input_buf_index, &unit.data[0], unit.data.size(), unit.timestamp);
258   }
259
260   MediaCodecStatus status = media_codec_bridge_->QueueSecureInputBuffer(
261       input_buf_index,
262       &unit.data[0], unit.data.size(),
263       reinterpret_cast<const uint8*>(&unit.key_id[0]), unit.key_id.size(),
264       reinterpret_cast<const uint8*>(&unit.iv[0]), unit.iv.size(),
265       unit.subsamples.empty() ? NULL : &unit.subsamples[0],
266       unit.subsamples.size(),
267       unit.timestamp);
268
269   // In case of MEDIA_CODEC_NO_KEY, we must reuse the |input_buf_index_|.
270   // Otherwise MediaDrm will report errors.
271   if (status == MEDIA_CODEC_NO_KEY)
272     input_buf_index_ = input_buf_index;
273
274   return status;
275 }
276
277 bool MediaDecoderJob::HasData() const {
278   DCHECK(ui_task_runner_->BelongsToCurrentThread());
279   // When |input_eos_encountered_| is set, |access_unit_index_| and
280   // |current_demuxer_data_index_| must be pointing to an EOS unit,
281   // or a |kConfigChanged| unit if |drain_decoder_| is true. In both cases,
282   // we'll feed an EOS input unit to drain the decoder until we hit output EOS.
283   DCHECK(!input_eos_encountered_ || !NoAccessUnitsRemainingInChunk(true));
284   return !NoAccessUnitsRemainingInChunk(true) ||
285       !NoAccessUnitsRemainingInChunk(false);
286 }
287
288 void MediaDecoderJob::RequestData(const base::Closure& done_cb) {
289   DVLOG(1) << __FUNCTION__;
290   DCHECK(ui_task_runner_->BelongsToCurrentThread());
291   DCHECK(data_received_cb_.is_null());
292   DCHECK(!input_eos_encountered_);
293   DCHECK(NoAccessUnitsRemainingInChunk(false));
294
295   TRACE_EVENT_ASYNC_BEGIN0("media", "MediaDecoderJob::RequestData", this);
296
297   data_received_cb_ = done_cb;
298
299   // If we are already expecting new data, just set the callback and do
300   // nothing.
301   if (is_requesting_demuxer_data_)
302     return;
303
304   // The new incoming data will be stored as the next demuxer data chunk, since
305   // the decoder might still be decoding the current one.
306   size_t next_demuxer_data_index = inactive_demuxer_data_index();
307   received_data_[next_demuxer_data_index] = DemuxerData();
308   access_unit_index_[next_demuxer_data_index] = 0;
309   is_requesting_demuxer_data_ = true;
310
311   request_data_cb_.Run();
312 }
313
314 void MediaDecoderJob::DecodeCurrentAccessUnit(
315     base::TimeTicks start_time_ticks,
316     base::TimeDelta start_presentation_timestamp) {
317   DCHECK(ui_task_runner_->BelongsToCurrentThread());
318   DCHECK(!decode_cb_.is_null());
319
320   RequestCurrentChunkIfEmpty();
321   const AccessUnit& access_unit = CurrentAccessUnit();
322   if (CurrentAccessUnit().status == DemuxerStream::kConfigChanged) {
323     int index = CurrentReceivedDataChunkIndex();
324     const DemuxerConfigs& configs = received_data_[index].demuxer_configs[0];
325     bool reconfigure_needed = IsCodecReconfigureNeeded(configs);
326     SetDemuxerConfigs(configs);
327     if (!drain_decoder_) {
328       // If we haven't decoded any data yet, just skip the current access unit
329       // and request the MediaCodec to be recreated on next Decode().
330       if (skip_eos_enqueue_ || !reconfigure_needed) {
331         need_to_reconfig_decoder_job_ =
332             need_to_reconfig_decoder_job_ || reconfigure_needed;
333         // Report MEDIA_CODEC_OK status so decoder will continue decoding and
334         // MEDIA_CODEC_OUTPUT_FORMAT_CHANGED status will come later.
335         ui_task_runner_->PostTask(FROM_HERE, base::Bind(
336             &MediaDecoderJob::OnDecodeCompleted, base::Unretained(this),
337             MEDIA_CODEC_OK, kNoTimestamp(), kNoTimestamp()));
338         return;
339       }
340       // Start draining the decoder so that all the remaining frames are
341       // rendered.
342       drain_decoder_ = true;
343     }
344   }
345
346   DCHECK(!(needs_flush_ && drain_decoder_));
347   decoder_task_runner_->PostTask(FROM_HERE, base::Bind(
348       &MediaDecoderJob::DecodeInternal, base::Unretained(this),
349       drain_decoder_ ? eos_unit_ : access_unit,
350       start_time_ticks, start_presentation_timestamp, needs_flush_,
351       media::BindToCurrentLoop(base::Bind(
352           &MediaDecoderJob::OnDecodeCompleted, base::Unretained(this)))));
353   needs_flush_ = false;
354 }
355
356 void MediaDecoderJob::DecodeInternal(
357     const AccessUnit& unit,
358     base::TimeTicks start_time_ticks,
359     base::TimeDelta start_presentation_timestamp,
360     bool needs_flush,
361     const MediaDecoderJob::DecoderCallback& callback) {
362   DVLOG(1) << __FUNCTION__;
363   DCHECK(decoder_task_runner_->BelongsToCurrentThread());
364   TRACE_EVENT0("media", __FUNCTION__);
365
366   if (needs_flush) {
367     DVLOG(1) << "DecodeInternal needs flush.";
368     input_eos_encountered_ = false;
369     output_eos_encountered_ = false;
370     MediaCodecStatus reset_status = media_codec_bridge_->Reset();
371     if (MEDIA_CODEC_OK != reset_status) {
372       callback.Run(reset_status, kNoTimestamp(), kNoTimestamp());
373       return;
374     }
375   }
376
377   // Once output EOS has occurred, we should not be asked to decode again.
378   // MediaCodec has undefined behavior if similarly asked to decode after output
379   // EOS.
380   DCHECK(!output_eos_encountered_);
381
382   // For aborted access unit, just skip it and inform the player.
383   if (unit.status == DemuxerStream::kAborted) {
384     // TODO(qinmin): use a new enum instead of MEDIA_CODEC_STOPPED.
385     callback.Run(MEDIA_CODEC_STOPPED, kNoTimestamp(), kNoTimestamp());
386     return;
387   }
388
389   if (skip_eos_enqueue_) {
390     if (unit.end_of_stream || unit.data.empty()) {
391       input_eos_encountered_ = true;
392       output_eos_encountered_ = true;
393       callback.Run(MEDIA_CODEC_OUTPUT_END_OF_STREAM, kNoTimestamp(),
394                    kNoTimestamp());
395       return;
396     }
397
398     skip_eos_enqueue_ = false;
399   }
400
401   MediaCodecStatus input_status = MEDIA_CODEC_INPUT_END_OF_STREAM;
402   if (!input_eos_encountered_) {
403     input_status = QueueInputBuffer(unit);
404     if (input_status == MEDIA_CODEC_INPUT_END_OF_STREAM) {
405       input_eos_encountered_ = true;
406     } else if (input_status != MEDIA_CODEC_OK) {
407       callback.Run(input_status, kNoTimestamp(), kNoTimestamp());
408       return;
409     }
410   }
411
412   int buffer_index = 0;
413   size_t offset = 0;
414   size_t size = 0;
415   base::TimeDelta presentation_timestamp;
416
417   base::TimeDelta timeout = base::TimeDelta::FromMilliseconds(
418       kMediaCodecTimeoutInMilliseconds);
419
420   MediaCodecStatus status = MEDIA_CODEC_OK;
421   bool has_format_change = false;
422   // Dequeue the output buffer until a MEDIA_CODEC_OK, MEDIA_CODEC_ERROR or
423   // MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER is received.
424   do {
425     status = media_codec_bridge_->DequeueOutputBuffer(
426         timeout,
427         &buffer_index,
428         &offset,
429         &size,
430         &presentation_timestamp,
431         &output_eos_encountered_,
432         NULL);
433     if (status == MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED &&
434         !media_codec_bridge_->GetOutputBuffers()) {
435       status = MEDIA_CODEC_ERROR;
436     } else if (status == MEDIA_CODEC_OUTPUT_FORMAT_CHANGED) {
437       // TODO(qinmin): instead of waiting for the next output buffer to be
438       // dequeued, post a task on the UI thread to signal the format change.
439       has_format_change = true;
440     }
441   } while (status != MEDIA_CODEC_OK && status != MEDIA_CODEC_ERROR &&
442            status != MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER);
443
444   if (status != MEDIA_CODEC_OK) {
445     callback.Run(status, kNoTimestamp(), kNoTimestamp());
446     return;
447   }
448
449   // TODO(xhwang/qinmin): This logic is correct but strange. Clean it up.
450   if (output_eos_encountered_)
451     status = MEDIA_CODEC_OUTPUT_END_OF_STREAM;
452   else if (has_format_change)
453     status = MEDIA_CODEC_OUTPUT_FORMAT_CHANGED;
454
455   bool render_output  = presentation_timestamp >= preroll_timestamp_ &&
456       (status != MEDIA_CODEC_OUTPUT_END_OF_STREAM || size != 0u);
457   base::TimeDelta time_to_render;
458   DCHECK(!start_time_ticks.is_null());
459   if (render_output && ComputeTimeToRender()) {
460     time_to_render = presentation_timestamp - (base::TimeTicks::Now() -
461         start_time_ticks + start_presentation_timestamp);
462   }
463
464   if (time_to_render > base::TimeDelta()) {
465     decoder_task_runner_->PostDelayedTask(
466         FROM_HERE,
467         base::Bind(&MediaDecoderJob::ReleaseOutputBuffer,
468                    base::Unretained(this),
469                    buffer_index,
470                    size,
471                    render_output,
472                    presentation_timestamp,
473                    base::Bind(callback, status)),
474         time_to_render);
475     return;
476   }
477
478   // TODO(qinmin): The codec is lagging behind, need to recalculate the
479   // |start_presentation_timestamp_| and |start_time_ticks_| in
480   // media_source_player.cc.
481   DVLOG(1) << "codec is lagging behind :" << time_to_render.InMicroseconds();
482   if (render_output) {
483     // The player won't expect a timestamp smaller than the
484     // |start_presentation_timestamp|. However, this could happen due to decoder
485     // errors.
486     presentation_timestamp = std::max(
487         presentation_timestamp, start_presentation_timestamp);
488   } else {
489     presentation_timestamp = kNoTimestamp();
490   }
491   ReleaseOutputCompletionCallback completion_callback = base::Bind(
492       callback, status);
493   ReleaseOutputBuffer(buffer_index, size, render_output, presentation_timestamp,
494                       completion_callback);
495 }
496
497 void MediaDecoderJob::OnDecodeCompleted(
498     MediaCodecStatus status, base::TimeDelta current_presentation_timestamp,
499     base::TimeDelta max_presentation_timestamp) {
500   DCHECK(ui_task_runner_->BelongsToCurrentThread());
501
502   if (destroy_pending_) {
503     DVLOG(1) << __FUNCTION__ << " : completing pending deletion";
504     delete this;
505     return;
506   }
507
508   if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM)
509     output_eos_encountered_ = true;
510
511   DCHECK(!decode_cb_.is_null());
512
513   // If output was queued for rendering, then we have completed prerolling.
514   if (current_presentation_timestamp != kNoTimestamp() ||
515       status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) {
516     prerolling_ = false;
517   }
518
519   switch (status) {
520     case MEDIA_CODEC_OK:
521     case MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER:
522     case MEDIA_CODEC_OUTPUT_FORMAT_CHANGED:
523     case MEDIA_CODEC_OUTPUT_END_OF_STREAM:
524       if (!input_eos_encountered_) {
525         CurrentDataConsumed(
526             CurrentAccessUnit().status == DemuxerStream::kConfigChanged);
527         access_unit_index_[current_demuxer_data_index_]++;
528       }
529       break;
530
531     case MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER:
532     case MEDIA_CODEC_INPUT_END_OF_STREAM:
533     case MEDIA_CODEC_NO_KEY:
534     case MEDIA_CODEC_STOPPED:
535     case MEDIA_CODEC_ERROR:
536       // Do nothing.
537       break;
538
539     case MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED:
540       DCHECK(false) << "Invalid output status";
541       break;
542   };
543
544   if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM && drain_decoder_) {
545     OnDecoderDrained();
546     status = MEDIA_CODEC_OK;
547   }
548
549   if (status == MEDIA_CODEC_OUTPUT_FORMAT_CHANGED) {
550     if (UpdateOutputFormat())
551       config_changed_cb_.Run();
552     status = MEDIA_CODEC_OK;
553   }
554
555   if (release_resources_pending_) {
556     ReleaseMediaCodecBridge();
557     release_resources_pending_ = false;
558     if (drain_decoder_)
559       OnDecoderDrained();
560   }
561
562   stop_decode_pending_ = false;
563   base::ResetAndReturn(&decode_cb_).Run(
564       status, current_presentation_timestamp, max_presentation_timestamp);
565 }
566
567 const AccessUnit& MediaDecoderJob::CurrentAccessUnit() const {
568   DCHECK(ui_task_runner_->BelongsToCurrentThread());
569   DCHECK(HasData());
570   size_t index = CurrentReceivedDataChunkIndex();
571   return received_data_[index].access_units[access_unit_index_[index]];
572 }
573
574 size_t MediaDecoderJob::CurrentReceivedDataChunkIndex() const {
575   return NoAccessUnitsRemainingInChunk(true) ?
576       inactive_demuxer_data_index() : current_demuxer_data_index_;
577 }
578
579 bool MediaDecoderJob::NoAccessUnitsRemainingInChunk(
580     bool is_active_chunk) const {
581   DCHECK(ui_task_runner_->BelongsToCurrentThread());
582   size_t index = is_active_chunk ? current_demuxer_data_index_ :
583       inactive_demuxer_data_index();
584   return received_data_[index].access_units.size() <= access_unit_index_[index];
585 }
586
587 void MediaDecoderJob::RequestCurrentChunkIfEmpty() {
588   DCHECK(ui_task_runner_->BelongsToCurrentThread());
589   DCHECK(HasData());
590   if (!NoAccessUnitsRemainingInChunk(true))
591     return;
592
593   // Requests new data if the the last access unit of the next chunk is not EOS.
594   current_demuxer_data_index_ = inactive_demuxer_data_index();
595   const AccessUnit last_access_unit =
596       received_data_[current_demuxer_data_index_].access_units.back();
597   if (!last_access_unit.end_of_stream &&
598       last_access_unit.status != DemuxerStream::kAborted) {
599     RequestData(base::Closure());
600   }
601 }
602
603 void MediaDecoderJob::InitializeReceivedData() {
604   for (size_t i = 0; i < 2; ++i) {
605     received_data_[i] = DemuxerData();
606     access_unit_index_[i] = 0;
607   }
608 }
609
610 void MediaDecoderJob::OnDecoderDrained() {
611   DVLOG(1) << __FUNCTION__;
612   DCHECK(ui_task_runner_->BelongsToCurrentThread());
613   DCHECK(drain_decoder_);
614
615   input_eos_encountered_ = false;
616   output_eos_encountered_ = false;
617   drain_decoder_ = false;
618   ReleaseMediaCodecBridge();
619   // Increase the access unit index so that the new decoder will not handle
620   // the config change again.
621   access_unit_index_[current_demuxer_data_index_]++;
622   CurrentDataConsumed(true);
623 }
624
625 bool MediaDecoderJob::CreateMediaCodecBridge() {
626   DVLOG(1) << __FUNCTION__;
627   DCHECK(ui_task_runner_->BelongsToCurrentThread());
628   DCHECK(decode_cb_.is_null());
629
630   if (!HasStream()) {
631     ReleaseMediaCodecBridge();
632     return false;
633   }
634
635   // Create |media_codec_bridge_| only if config changes.
636   if (media_codec_bridge_ && !need_to_reconfig_decoder_job_)
637     return true;
638
639   base::android::ScopedJavaLocalRef<jobject> media_crypto = GetMediaCrypto();
640   if (is_content_encrypted_ && media_crypto.is_null())
641     return false;
642
643   ReleaseMediaCodecBridge();
644   DVLOG(1) << __FUNCTION__ << " : creating new media codec bridge";
645
646   return CreateMediaCodecBridgeInternal();
647 }
648
649 bool MediaDecoderJob::IsCodecReconfigureNeeded(
650     const DemuxerConfigs& configs) const {
651   if (!AreDemuxerConfigsChanged(configs))
652     return false;
653   return true;
654 }
655
656 bool MediaDecoderJob::UpdateOutputFormat() {
657   return false;
658 }
659
660 void MediaDecoderJob::ReleaseMediaCodecBridge() {
661   if (!media_codec_bridge_)
662     return;
663
664   media_codec_bridge_.reset();
665   input_buf_index_ = -1;
666 }
667
668 }  // namespace media