| OLD | NEW |
| (Empty) |
| 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/single_thread_task_runner.h" | |
| 10 #include "base/threading/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" | |
| 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::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; | |
| 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_ABORT, false, 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 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; | |
| 128 } | |
| 129 | |
| 130 decode_cb_ = callback; | |
| 131 | |
| 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; | |
| 138 } | |
| 139 | |
| 140 DecodeCurrentAccessUnit(start_time_ticks, start_presentation_timestamp); | |
| 141 return STATUS_SUCCESS; | |
| 142 } | |
| 143 | |
| 144 void MediaDecoderJob::StopDecode() { | |
| 145 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 146 DCHECK(is_decoding()); | |
| 147 stop_decode_pending_ = true; | |
| 148 } | |
| 149 | |
| 150 bool MediaDecoderJob::OutputEOSReached() const { | |
| 151 return !drain_decoder_ && output_eos_encountered_; | |
| 152 } | |
| 153 | |
| 154 void MediaDecoderJob::SetDrmBridge(MediaDrmBridge* drm_bridge) { | |
| 155 drm_bridge_ = drm_bridge; | |
| 156 need_to_reconfig_decoder_job_ = true; | |
| 157 } | |
| 158 | |
| 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()); | |
| 164 | |
| 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; | |
| 173 | |
| 174 // Do nothing, flush when the next Decode() happens. | |
| 175 needs_flush_ = true; | |
| 176 } | |
| 177 | |
| 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()); | |
| 182 | |
| 183 preroll_timestamp_ = preroll_timestamp; | |
| 184 prerolling_ = true; | |
| 185 } | |
| 186 | |
| 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; | |
| 196 } | |
| 197 | |
| 198 // Release |media_codec_bridge_| once decoding is completed. | |
| 199 release_resources_pending_ = true; | |
| 200 } | |
| 201 | |
| 202 jobject MediaDecoderJob::GetMediaCrypto() { | |
| 203 return drm_bridge_ ? drm_bridge_->GetMediaCrypto() : nullptr; | |
| 204 } | |
| 205 | |
| 206 bool MediaDecoderJob::SetCurrentFrameToPreviouslyCachedKeyFrame() { | |
| 207 const std::vector<AccessUnit>& access_units = | |
| 208 received_data_[current_demuxer_data_index_].access_units; | |
| 209 // If the current data chunk is empty, the player must be in an initial or | |
| 210 // seek state. The next access unit will always be a key frame. | |
| 211 if (access_units.size() == 0) | |
| 212 return true; | |
| 213 | |
| 214 // Find key frame in all the access units the decoder have decoded, | |
| 215 // or is about to decode. | |
| 216 int i = std::min(access_unit_index_[current_demuxer_data_index_], | |
| 217 access_units.size() - 1); | |
| 218 for (; i >= 0; --i) { | |
| 219 // Config change is always the last access unit, and it always come with | |
| 220 // a key frame afterwards. | |
| 221 if (access_units[i].status == DemuxerStream::kConfigChanged) | |
| 222 return true; | |
| 223 if (access_units[i].is_key_frame) { | |
| 224 access_unit_index_[current_demuxer_data_index_] = i; | |
| 225 return true; | |
| 226 } | |
| 227 } | |
| 228 return false; | |
| 229 } | |
| 230 | |
| 231 | |
| 232 void MediaDecoderJob::Release() { | |
| 233 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 234 DVLOG(1) << __FUNCTION__; | |
| 235 | |
| 236 // If the decoder job is still decoding, we cannot delete the job immediately. | |
| 237 destroy_pending_ = is_decoding(); | |
| 238 | |
| 239 request_data_cb_.Reset(); | |
| 240 data_received_cb_.Reset(); | |
| 241 decode_cb_.Reset(); | |
| 242 | |
| 243 if (destroy_pending_) { | |
| 244 DVLOG(1) << __FUNCTION__ << " : delete is pending decode completion"; | |
| 245 return; | |
| 246 } | |
| 247 | |
| 248 delete this; | |
| 249 } | |
| 250 | |
| 251 MediaCodecStatus MediaDecoderJob::QueueInputBuffer(const AccessUnit& unit) { | |
| 252 DVLOG(1) << __FUNCTION__; | |
| 253 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
| 254 TRACE_EVENT0("media", __FUNCTION__); | |
| 255 | |
| 256 int input_buf_index = input_buf_index_; | |
| 257 input_buf_index_ = -1; | |
| 258 | |
| 259 // TODO(xhwang): Hide DequeueInputBuffer() and the index in MediaCodecBridge. | |
| 260 if (input_buf_index == -1) { | |
| 261 base::TimeDelta timeout = base::TimeDelta::FromMilliseconds( | |
| 262 kMediaCodecTimeoutInMilliseconds); | |
| 263 MediaCodecStatus status = | |
| 264 media_codec_bridge_->DequeueInputBuffer(timeout, &input_buf_index); | |
| 265 if (status != MEDIA_CODEC_OK) { | |
| 266 DVLOG(1) << "DequeueInputBuffer fails: " << status; | |
| 267 return status; | |
| 268 } | |
| 269 } | |
| 270 | |
| 271 // TODO(qinmin): skip frames if video is falling far behind. | |
| 272 DCHECK_GE(input_buf_index, 0); | |
| 273 if (unit.is_end_of_stream || unit.data.empty()) { | |
| 274 media_codec_bridge_->QueueEOS(input_buf_index); | |
| 275 return MEDIA_CODEC_INPUT_END_OF_STREAM; | |
| 276 } | |
| 277 | |
| 278 if (unit.key_id.empty() || unit.iv.empty()) { | |
| 279 DCHECK(unit.iv.empty() || !unit.key_id.empty()); | |
| 280 return media_codec_bridge_->QueueInputBuffer( | |
| 281 input_buf_index, &unit.data[0], unit.data.size(), unit.timestamp); | |
| 282 } | |
| 283 | |
| 284 MediaCodecStatus status = media_codec_bridge_->QueueSecureInputBuffer( | |
| 285 input_buf_index, &unit.data[0], unit.data.size(), unit.key_id, unit.iv, | |
| 286 unit.subsamples.empty() ? NULL : &unit.subsamples[0], | |
| 287 unit.subsamples.size(), unit.timestamp); | |
| 288 | |
| 289 // In case of MEDIA_CODEC_NO_KEY, we must reuse the |input_buf_index_|. | |
| 290 // Otherwise MediaDrm will report errors. | |
| 291 if (status == MEDIA_CODEC_NO_KEY) | |
| 292 input_buf_index_ = input_buf_index; | |
| 293 | |
| 294 return status; | |
| 295 } | |
| 296 | |
| 297 bool MediaDecoderJob::HasData() const { | |
| 298 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 299 // When |input_eos_encountered_| is set, |access_unit_index_| and | |
| 300 // |current_demuxer_data_index_| must be pointing to an EOS unit, | |
| 301 // or a |kConfigChanged| unit if |drain_decoder_| is true. In both cases, | |
| 302 // we'll feed an EOS input unit to drain the decoder until we hit output EOS. | |
| 303 DCHECK(!input_eos_encountered_ || !NoAccessUnitsRemainingInChunk(true)); | |
| 304 return !NoAccessUnitsRemainingInChunk(true) || | |
| 305 !NoAccessUnitsRemainingInChunk(false); | |
| 306 } | |
| 307 | |
| 308 void MediaDecoderJob::RequestData(const base::Closure& done_cb) { | |
| 309 DVLOG(1) << __FUNCTION__; | |
| 310 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 311 DCHECK(data_received_cb_.is_null()); | |
| 312 DCHECK(!input_eos_encountered_); | |
| 313 DCHECK(NoAccessUnitsRemainingInChunk(false)); | |
| 314 | |
| 315 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaDecoderJob::RequestData", this); | |
| 316 | |
| 317 data_received_cb_ = done_cb; | |
| 318 | |
| 319 // If we are already expecting new data, just set the callback and do | |
| 320 // nothing. | |
| 321 if (is_requesting_demuxer_data_) | |
| 322 return; | |
| 323 | |
| 324 // The new incoming data will be stored as the next demuxer data chunk, since | |
| 325 // the decoder might still be decoding the current one. | |
| 326 size_t next_demuxer_data_index = inactive_demuxer_data_index(); | |
| 327 received_data_[next_demuxer_data_index] = DemuxerData(); | |
| 328 access_unit_index_[next_demuxer_data_index] = 0; | |
| 329 is_requesting_demuxer_data_ = true; | |
| 330 | |
| 331 request_data_cb_.Run(); | |
| 332 } | |
| 333 | |
| 334 void MediaDecoderJob::DecodeCurrentAccessUnit( | |
| 335 base::TimeTicks start_time_ticks, | |
| 336 base::TimeDelta start_presentation_timestamp) { | |
| 337 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 338 DCHECK(!decode_cb_.is_null()); | |
| 339 | |
| 340 RequestCurrentChunkIfEmpty(); | |
| 341 const AccessUnit& access_unit = CurrentAccessUnit(); | |
| 342 if (CurrentAccessUnit().status == DemuxerStream::kConfigChanged) { | |
| 343 int index = CurrentReceivedDataChunkIndex(); | |
| 344 const DemuxerConfigs& configs = received_data_[index].demuxer_configs[0]; | |
| 345 bool reconfigure_needed = IsCodecReconfigureNeeded(configs); | |
| 346 SetDemuxerConfigs(configs); | |
| 347 if (!drain_decoder_) { | |
| 348 // If we haven't decoded any data yet, just skip the current access unit | |
| 349 // and request the MediaCodec to be recreated on next Decode(). | |
| 350 if (skip_eos_enqueue_ || !reconfigure_needed) { | |
| 351 need_to_reconfig_decoder_job_ = | |
| 352 need_to_reconfig_decoder_job_ || reconfigure_needed; | |
| 353 // Report MEDIA_CODEC_OK status so decoder will continue decoding and | |
| 354 // MEDIA_CODEC_OUTPUT_FORMAT_CHANGED status will come later. | |
| 355 ui_task_runner_->PostTask( | |
| 356 FROM_HERE, base::Bind(&MediaDecoderJob::OnDecodeCompleted, | |
| 357 base::Unretained(this), MEDIA_CODEC_OK, false, | |
| 358 kNoTimestamp, kNoTimestamp)); | |
| 359 return; | |
| 360 } | |
| 361 // Start draining the decoder so that all the remaining frames are | |
| 362 // rendered. | |
| 363 drain_decoder_ = true; | |
| 364 } | |
| 365 } | |
| 366 | |
| 367 DCHECK(!(needs_flush_ && drain_decoder_)); | |
| 368 decoder_task_runner_->PostTask(FROM_HERE, base::Bind( | |
| 369 &MediaDecoderJob::DecodeInternal, base::Unretained(this), | |
| 370 drain_decoder_ ? eos_unit_ : access_unit, | |
| 371 start_time_ticks, start_presentation_timestamp, needs_flush_, | |
| 372 media::BindToCurrentLoop(base::Bind( | |
| 373 &MediaDecoderJob::OnDecodeCompleted, base::Unretained(this))))); | |
| 374 needs_flush_ = false; | |
| 375 } | |
| 376 | |
| 377 void MediaDecoderJob::DecodeInternal( | |
| 378 const AccessUnit& unit, | |
| 379 base::TimeTicks start_time_ticks, | |
| 380 base::TimeDelta start_presentation_timestamp, | |
| 381 bool needs_flush, | |
| 382 const MediaDecoderJob::DecoderCallback& callback) { | |
| 383 DVLOG(1) << __FUNCTION__; | |
| 384 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
| 385 TRACE_EVENT0("media", __FUNCTION__); | |
| 386 | |
| 387 if (needs_flush) { | |
| 388 DVLOG(1) << "DecodeInternal needs flush."; | |
| 389 input_eos_encountered_ = false; | |
| 390 output_eos_encountered_ = false; | |
| 391 input_buf_index_ = -1; | |
| 392 MediaCodecStatus flush_status = media_codec_bridge_->Flush(); | |
| 393 if (flush_status != MEDIA_CODEC_OK) { | |
| 394 callback.Run(flush_status, false, kNoTimestamp, kNoTimestamp); | |
| 395 return; | |
| 396 } | |
| 397 } | |
| 398 | |
| 399 // Once output EOS has occurred, we should not be asked to decode again. | |
| 400 // MediaCodec has undefined behavior if similarly asked to decode after output | |
| 401 // EOS. | |
| 402 DCHECK(!output_eos_encountered_); | |
| 403 | |
| 404 // For aborted access unit, just skip it and inform the player. | |
| 405 if (unit.status == DemuxerStream::kAborted) { | |
| 406 callback.Run(MEDIA_CODEC_ABORT, false, kNoTimestamp, kNoTimestamp); | |
| 407 return; | |
| 408 } | |
| 409 | |
| 410 if (skip_eos_enqueue_) { | |
| 411 if (unit.is_end_of_stream || unit.data.empty()) { | |
| 412 input_eos_encountered_ = true; | |
| 413 output_eos_encountered_ = true; | |
| 414 callback.Run(MEDIA_CODEC_OUTPUT_END_OF_STREAM, false, kNoTimestamp, | |
| 415 kNoTimestamp); | |
| 416 return; | |
| 417 } | |
| 418 | |
| 419 skip_eos_enqueue_ = false; | |
| 420 } | |
| 421 | |
| 422 MediaCodecStatus input_status = MEDIA_CODEC_INPUT_END_OF_STREAM; | |
| 423 if (!input_eos_encountered_) { | |
| 424 input_status = QueueInputBuffer(unit); | |
| 425 if (input_status == MEDIA_CODEC_INPUT_END_OF_STREAM) { | |
| 426 input_eos_encountered_ = true; | |
| 427 } else if (input_status == MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER) { | |
| 428 // In some cases, all buffers must be released to codec before format | |
| 429 // change can be resolved. Context: b/21786703 | |
| 430 DVLOG(1) << "dequeueInputBuffer gave AGAIN_LATER, dequeue output buffers"; | |
| 431 } else if (input_status != MEDIA_CODEC_OK) { | |
| 432 callback.Run(input_status, false, kNoTimestamp, kNoTimestamp); | |
| 433 return; | |
| 434 } | |
| 435 } | |
| 436 | |
| 437 int buffer_index = 0; | |
| 438 size_t offset = 0; | |
| 439 size_t size = 0; | |
| 440 base::TimeDelta presentation_timestamp; | |
| 441 | |
| 442 base::TimeDelta timeout = base::TimeDelta::FromMilliseconds( | |
| 443 kMediaCodecTimeoutInMilliseconds); | |
| 444 | |
| 445 MediaCodecStatus status = MEDIA_CODEC_OK; | |
| 446 bool has_format_change = false; | |
| 447 // Dequeue the output buffer until a MEDIA_CODEC_OK, MEDIA_CODEC_ERROR or | |
| 448 // MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER is received. | |
| 449 do { | |
| 450 status = media_codec_bridge_->DequeueOutputBuffer( | |
| 451 timeout, | |
| 452 &buffer_index, | |
| 453 &offset, | |
| 454 &size, | |
| 455 &presentation_timestamp, | |
| 456 &output_eos_encountered_, | |
| 457 NULL); | |
| 458 if (status == MEDIA_CODEC_OUTPUT_FORMAT_CHANGED) { | |
| 459 // TODO(qinmin): instead of waiting for the next output buffer to be | |
| 460 // dequeued, post a task on the UI thread to signal the format change. | |
| 461 if (OnOutputFormatChanged()) | |
| 462 has_format_change = true; | |
| 463 else | |
| 464 status = MEDIA_CODEC_ERROR; | |
| 465 } | |
| 466 } while (status != MEDIA_CODEC_OK && status != MEDIA_CODEC_ERROR && | |
| 467 status != MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER); | |
| 468 | |
| 469 if (status != MEDIA_CODEC_OK) { | |
| 470 callback.Run(status, false, kNoTimestamp, kNoTimestamp); | |
| 471 return; | |
| 472 } | |
| 473 | |
| 474 // TODO(xhwang/qinmin): This logic is correct but strange. Clean it up. | |
| 475 if (output_eos_encountered_) | |
| 476 status = MEDIA_CODEC_OUTPUT_END_OF_STREAM; | |
| 477 else if (has_format_change) | |
| 478 status = MEDIA_CODEC_OUTPUT_FORMAT_CHANGED; | |
| 479 | |
| 480 bool render_output = presentation_timestamp >= preroll_timestamp_ && | |
| 481 (status != MEDIA_CODEC_OUTPUT_END_OF_STREAM || size != 0u); | |
| 482 base::TimeDelta time_to_render; | |
| 483 DCHECK(!start_time_ticks.is_null()); | |
| 484 if (render_output && ComputeTimeToRender()) { | |
| 485 time_to_render = presentation_timestamp - (base::TimeTicks::Now() - | |
| 486 start_time_ticks + start_presentation_timestamp); | |
| 487 } | |
| 488 | |
| 489 if (time_to_render > base::TimeDelta()) { | |
| 490 decoder_task_runner_->PostDelayedTask( | |
| 491 FROM_HERE, base::Bind(&MediaDecoderJob::ReleaseOutputBuffer, | |
| 492 base::Unretained(this), buffer_index, offset, | |
| 493 size, render_output, | |
| 494 false, // this is not a late frame | |
| 495 presentation_timestamp, status, callback), | |
| 496 time_to_render); | |
| 497 return; | |
| 498 } | |
| 499 | |
| 500 // TODO(qinmin): The codec is lagging behind, need to recalculate the | |
| 501 // |start_presentation_timestamp_| and |start_time_ticks_| in | |
| 502 // media_source_player.cc. | |
| 503 DVLOG(1) << "codec is lagging behind :" << time_to_render.InMicroseconds(); | |
| 504 if (render_output) { | |
| 505 // The player won't expect a timestamp smaller than the | |
| 506 // |start_presentation_timestamp|. However, this could happen due to decoder | |
| 507 // errors. | |
| 508 presentation_timestamp = std::max( | |
| 509 presentation_timestamp, start_presentation_timestamp); | |
| 510 } else { | |
| 511 presentation_timestamp = kNoTimestamp; | |
| 512 } | |
| 513 | |
| 514 const bool is_late_frame = (time_to_render < base::TimeDelta()); | |
| 515 ReleaseOutputBuffer(buffer_index, offset, size, render_output, is_late_frame, | |
| 516 presentation_timestamp, status, callback); | |
| 517 } | |
| 518 | |
| 519 void MediaDecoderJob::OnDecodeCompleted( | |
| 520 MediaCodecStatus status, | |
| 521 bool is_late_frame, | |
| 522 base::TimeDelta current_presentation_timestamp, | |
| 523 base::TimeDelta max_presentation_timestamp) { | |
| 524 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 525 | |
| 526 if (destroy_pending_) { | |
| 527 DVLOG(1) << __FUNCTION__ << " : completing pending deletion"; | |
| 528 delete this; | |
| 529 return; | |
| 530 } | |
| 531 | |
| 532 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM) | |
| 533 output_eos_encountered_ = true; | |
| 534 | |
| 535 DCHECK(!decode_cb_.is_null()); | |
| 536 | |
| 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; | |
| 541 } | |
| 542 | |
| 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; | |
| 551 | |
| 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; | |
| 559 | |
| 560 case MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED: | |
| 561 DCHECK(false) << "Invalid output status"; | |
| 562 break; | |
| 563 }; | |
| 564 | |
| 565 if (status == MEDIA_CODEC_OUTPUT_END_OF_STREAM && drain_decoder_) { | |
| 566 OnDecoderDrained(); | |
| 567 status = MEDIA_CODEC_OK; | |
| 568 } | |
| 569 | |
| 570 if (status == MEDIA_CODEC_OUTPUT_FORMAT_CHANGED) { | |
| 571 if (UpdateOutputFormat()) | |
| 572 config_changed_cb_.Run(); | |
| 573 status = MEDIA_CODEC_OK; | |
| 574 } | |
| 575 | |
| 576 if (release_resources_pending_) { | |
| 577 ReleaseMediaCodecBridge(); | |
| 578 release_resources_pending_ = false; | |
| 579 if (drain_decoder_) | |
| 580 OnDecoderDrained(); | |
| 581 } | |
| 582 | |
| 583 stop_decode_pending_ = false; | |
| 584 base::ResetAndReturn(&decode_cb_) | |
| 585 .Run(status, is_late_frame, current_presentation_timestamp, | |
| 586 max_presentation_timestamp); | |
| 587 } | |
| 588 | |
| 589 const AccessUnit& MediaDecoderJob::CurrentAccessUnit() const { | |
| 590 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 591 DCHECK(HasData()); | |
| 592 size_t index = CurrentReceivedDataChunkIndex(); | |
| 593 return received_data_[index].access_units[access_unit_index_[index]]; | |
| 594 } | |
| 595 | |
| 596 size_t MediaDecoderJob::CurrentReceivedDataChunkIndex() const { | |
| 597 return NoAccessUnitsRemainingInChunk(true) ? | |
| 598 inactive_demuxer_data_index() : current_demuxer_data_index_; | |
| 599 } | |
| 600 | |
| 601 bool MediaDecoderJob::NoAccessUnitsRemainingInChunk( | |
| 602 bool is_active_chunk) const { | |
| 603 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 604 size_t index = is_active_chunk ? current_demuxer_data_index_ : | |
| 605 inactive_demuxer_data_index(); | |
| 606 return received_data_[index].access_units.size() <= access_unit_index_[index]; | |
| 607 } | |
| 608 | |
| 609 void MediaDecoderJob::RequestCurrentChunkIfEmpty() { | |
| 610 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 611 DCHECK(HasData()); | |
| 612 if (!NoAccessUnitsRemainingInChunk(true)) | |
| 613 return; | |
| 614 | |
| 615 // Requests new data if the the last access unit of the next chunk is not EOS. | |
| 616 current_demuxer_data_index_ = inactive_demuxer_data_index(); | |
| 617 const AccessUnit& last_access_unit = | |
| 618 received_data_[current_demuxer_data_index_].access_units.back(); | |
| 619 if (!last_access_unit.is_end_of_stream && | |
| 620 last_access_unit.status != DemuxerStream::kAborted) { | |
| 621 RequestData(base::Closure()); | |
| 622 } | |
| 623 } | |
| 624 | |
| 625 void MediaDecoderJob::InitializeReceivedData() { | |
| 626 for (size_t i = 0; i < 2; ++i) { | |
| 627 received_data_[i] = DemuxerData(); | |
| 628 access_unit_index_[i] = 0; | |
| 629 } | |
| 630 } | |
| 631 | |
| 632 void MediaDecoderJob::OnDecoderDrained() { | |
| 633 DVLOG(1) << __FUNCTION__; | |
| 634 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 635 DCHECK(drain_decoder_); | |
| 636 | |
| 637 input_eos_encountered_ = false; | |
| 638 output_eos_encountered_ = false; | |
| 639 drain_decoder_ = false; | |
| 640 ReleaseMediaCodecBridge(); | |
| 641 // Increase the access unit index so that the new decoder will not handle | |
| 642 // the config change again. | |
| 643 access_unit_index_[current_demuxer_data_index_]++; | |
| 644 } | |
| 645 | |
| 646 MediaDecoderJob::MediaDecoderJobStatus | |
| 647 MediaDecoderJob::CreateMediaCodecBridge() { | |
| 648 DVLOG(1) << __FUNCTION__; | |
| 649 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 650 DCHECK(decode_cb_.is_null()); | |
| 651 | |
| 652 if (!HasStream()) { | |
| 653 ReleaseMediaCodecBridge(); | |
| 654 return STATUS_FAILURE; | |
| 655 } | |
| 656 | |
| 657 // Create |media_codec_bridge_| only if config changes. | |
| 658 if (media_codec_bridge_ && !need_to_reconfig_decoder_job_) | |
| 659 return STATUS_SUCCESS; | |
| 660 | |
| 661 if (is_content_encrypted_ && !GetMediaCrypto()) | |
| 662 return STATUS_FAILURE; | |
| 663 | |
| 664 ReleaseMediaCodecBridge(); | |
| 665 DVLOG(1) << __FUNCTION__ << " : creating new media codec bridge"; | |
| 666 | |
| 667 return CreateMediaCodecBridgeInternal(); | |
| 668 } | |
| 669 | |
| 670 bool MediaDecoderJob::IsCodecReconfigureNeeded( | |
| 671 const DemuxerConfigs& configs) const { | |
| 672 if (!AreDemuxerConfigsChanged(configs)) | |
| 673 return false; | |
| 674 return true; | |
| 675 } | |
| 676 | |
| 677 bool MediaDecoderJob::OnOutputFormatChanged() { | |
| 678 return true; | |
| 679 } | |
| 680 | |
| 681 bool MediaDecoderJob::UpdateOutputFormat() { | |
| 682 return false; | |
| 683 } | |
| 684 | |
| 685 void MediaDecoderJob::ReleaseMediaCodecBridge() { | |
| 686 if (!media_codec_bridge_) | |
| 687 return; | |
| 688 | |
| 689 media_codec_bridge_.reset(); | |
| 690 input_buf_index_ = -1; | |
| 691 } | |
| 692 | |
| 693 } // namespace media | |
| OLD | NEW |