OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/base/android/media_codec_decoder.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback_helpers.h" |
| 10 #include "base/logging.h" |
| 11 #include "media/base/android/media_codec_bridge.h" |
| 12 |
| 13 namespace media { |
| 14 |
| 15 namespace { |
| 16 |
| 17 // Stop requesting new data in the PREFETCH state when the queue size reached |
| 18 // this limit. |
| 19 const int PREFETCH_LIMIT = 8; |
| 20 |
| 21 // Request new data in the RUNNING state if the queue size is less than this. |
| 22 const int PLAYBACK_LOW_LIMIT = 4; |
| 23 |
| 24 // Posting delay of the next frame processing, in milliseconds |
| 25 const int NEXT_FRAME_DELAY_MS = 2; |
| 26 } |
| 27 |
| 28 MediaCodecDecoder::MediaCodecDecoder( |
| 29 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
| 30 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, |
| 31 const base::Closure& request_data_cb, |
| 32 const base::Closure& starvation_cb, |
| 33 const base::Closure& stop_done_cb, |
| 34 const base::Closure& error_cb, |
| 35 const char* decoder_thread_name) |
| 36 : media_task_runner_(media_task_runner), |
| 37 ui_task_runner_(ui_task_runner), |
| 38 decoder_thread_(decoder_thread_name), |
| 39 request_data_cb_(request_data_cb), |
| 40 starvation_cb_(starvation_cb), |
| 41 stop_done_cb_(stop_done_cb), |
| 42 error_cb_(error_cb), |
| 43 state_(STOPPED), |
| 44 eos_enqueued_(false), |
| 45 completed_(false), |
| 46 last_frame_posted_(false), |
| 47 weak_factory_(this) { |
| 48 // Media thread |
| 49 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 50 |
| 51 DVLOG(1) << "Decoder::Decoder() " << decoder_thread_name; |
| 52 |
| 53 weak_this_ = weak_factory_.GetWeakPtr(); |
| 54 } |
| 55 |
| 56 MediaCodecDecoder::~MediaCodecDecoder() { |
| 57 // Media thread |
| 58 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 59 |
| 60 DVLOG(1) << "Decoder::~Decoder()"; |
| 61 |
| 62 // NB: ReleaseDecoderResources() is virtual |
| 63 ReleaseDecoderResources(); |
| 64 } |
| 65 |
| 66 base::android::ScopedJavaLocalRef<jobject> |
| 67 MediaCodecDecoder::GetMediaCrypto() { |
| 68 base::android::ScopedJavaLocalRef<jobject> media_crypto; |
| 69 |
| 70 // drm_bridge_ is not implemented |
| 71 // if (drm_bridge_) |
| 72 // media_crypto = drm_bridge_->GetMediaCrypto(); |
| 73 return media_crypto; |
| 74 } |
| 75 |
| 76 void MediaCodecDecoder::Prefetch(const base::Closure& prefetch_done_cb) { |
| 77 // Media thread |
| 78 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 79 |
| 80 DVLOG(1) << class_name() << "::" << __FUNCTION__; |
| 81 |
| 82 DCHECK(GetState() == STOPPED); |
| 83 |
| 84 prefetch_done_cb_ = prefetch_done_cb; |
| 85 |
| 86 SetState(PREFETCHING); |
| 87 PrefetchNextChunk(); |
| 88 } |
| 89 |
| 90 MediaCodecDecoder::ConfigStatus MediaCodecDecoder::Configure() { |
| 91 // Media thread |
| 92 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 93 |
| 94 DVLOG(1) << class_name() << "::" << __FUNCTION__; |
| 95 |
| 96 // Here I assume that OnDemuxerConfigsAvailable won't come |
| 97 // in the middle of demuxer data. |
| 98 |
| 99 if (media_codec_bridge_) { |
| 100 DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| 101 << ": reconfiguration is not required, ignoring"; |
| 102 return CONFIG_OK; |
| 103 } |
| 104 |
| 105 return ConfigureInternal(); |
| 106 } |
| 107 |
| 108 bool MediaCodecDecoder::Start(base::TimeDelta current_time) { |
| 109 // Media thread |
| 110 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 111 |
| 112 DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| 113 << " current_time:" << current_time; |
| 114 |
| 115 if (state_ == RUNNING) { |
| 116 DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": already started"; |
| 117 return true; // already started |
| 118 } |
| 119 |
| 120 if (state_ == STOPPING) { |
| 121 DVLOG(0) << class_name() << "::" << __FUNCTION__ |
| 122 << ": wrong state STOPPING, ignoring"; |
| 123 return false; |
| 124 } |
| 125 |
| 126 DCHECK(!decoder_thread_.IsRunning()); |
| 127 |
| 128 // We only synchronize video stream. |
| 129 // When audio is present, the |current_time| is audio time. |
| 130 SynchronizePTSWithTime(current_time); |
| 131 |
| 132 last_frame_posted_ = false; |
| 133 |
| 134 // Start the decoder thread |
| 135 if (!decoder_thread_.Start()) { |
| 136 DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| 137 << ": cannot start decoder thread"; |
| 138 return false; |
| 139 } |
| 140 |
| 141 SetState(RUNNING); |
| 142 |
| 143 decoder_thread_.task_runner()->PostTask( |
| 144 FROM_HERE, |
| 145 base::Bind(&MediaCodecDecoder::ProcessNextFrame, base::Unretained(this))); |
| 146 |
| 147 return true; |
| 148 } |
| 149 |
| 150 void MediaCodecDecoder::SyncStop() { |
| 151 // Media thread |
| 152 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 153 |
| 154 DVLOG(1) << class_name() << "::" << __FUNCTION__; |
| 155 |
| 156 // After this method returns, decoder thread will not be running. |
| 157 |
| 158 decoder_thread_.Stop(); // synchronous |
| 159 state_ = STOPPED; |
| 160 |
| 161 // Shall we move |delayed_buffers_| from VideoDecoder to Decoder class? |
| 162 ReleaseDelayedBuffers(); |
| 163 } |
| 164 |
| 165 void MediaCodecDecoder::RequestToStop() { |
| 166 // Media thread |
| 167 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 168 |
| 169 DVLOG(1) << class_name() << "::" << __FUNCTION__; |
| 170 |
| 171 DCHECK(GetState() == RUNNING); |
| 172 SetState(STOPPING); |
| 173 } |
| 174 |
| 175 void MediaCodecDecoder::OnLastFrameRendered(bool completed) { |
| 176 // Media thread |
| 177 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 178 |
| 179 DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| 180 << " completed:" << completed; |
| 181 |
| 182 decoder_thread_.Stop(); // synchronous |
| 183 state_ = STOPPED; |
| 184 completed_ = completed; |
| 185 |
| 186 media_task_runner_->PostTask(FROM_HERE, stop_done_cb_); |
| 187 } |
| 188 |
| 189 void MediaCodecDecoder::Flush() { |
| 190 // Media thread |
| 191 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 192 |
| 193 DVLOG(1) << class_name() << "::" << __FUNCTION__; |
| 194 |
| 195 DCHECK_EQ(GetState(), STOPPED); |
| 196 |
| 197 eos_enqueued_ = false; |
| 198 completed_ = false; |
| 199 au_queue_.Flush(); |
| 200 |
| 201 if (media_codec_bridge_) { |
| 202 // MediaCodecBridge::Reset() performs MediaCodecBridge.flush() |
| 203 MediaCodecStatus flush_status = media_codec_bridge_->Reset(); |
| 204 if (flush_status != MEDIA_CODEC_OK) |
| 205 media_task_runner_->PostTask(FROM_HERE, error_cb_); |
| 206 } |
| 207 } |
| 208 |
| 209 void MediaCodecDecoder::OnDemuxerDataAvailable(const DemuxerData& data) { |
| 210 // Media thread |
| 211 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 212 |
| 213 DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| 214 << " #AUs:" << data.access_units.size() |
| 215 << " #Configs:" << data.demuxer_configs.size(); |
| 216 |
| 217 if (!data.access_units.empty()) |
| 218 DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| 219 << " AU[0]: " << data.access_units[0]; |
| 220 |
| 221 au_queue_.PushBack(data); |
| 222 |
| 223 if (state_ == PREFETCHING) |
| 224 PrefetchNextChunk(); |
| 225 } |
| 226 |
| 227 void MediaCodecDecoder::ReleaseDecoderResources() { |
| 228 // Media thread |
| 229 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 230 |
| 231 DVLOG(1) << class_name() << "::" << __FUNCTION__; |
| 232 |
| 233 decoder_thread_.Stop(); // synchronous |
| 234 state_ = STOPPED; |
| 235 media_codec_bridge_.reset(); |
| 236 } |
| 237 |
| 238 void MediaCodecDecoder::ReleaseMediaCodec() { |
| 239 // Media thread |
| 240 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 241 |
| 242 DVLOG(1) << class_name() << "::" << __FUNCTION__; |
| 243 |
| 244 media_codec_bridge_.reset(); |
| 245 } |
| 246 |
| 247 bool MediaCodecDecoder::IsPrefetchingOrPlaying() const { |
| 248 // Media thread |
| 249 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 250 |
| 251 base::AutoLock lock(state_lock_); |
| 252 return state_ == PREFETCHING || state_ == RUNNING; |
| 253 } |
| 254 |
| 255 bool MediaCodecDecoder::IsStopped() const { |
| 256 // Media thread |
| 257 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 258 |
| 259 return GetState() == STOPPED; |
| 260 } |
| 261 |
| 262 bool MediaCodecDecoder::IsCompleted() const { |
| 263 // Media thread |
| 264 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 265 |
| 266 return completed_; |
| 267 } |
| 268 |
| 269 MediaCodecDecoder::DecoderState MediaCodecDecoder::GetState() const { |
| 270 base::AutoLock lock(state_lock_); |
| 271 return state_; |
| 272 } |
| 273 |
| 274 void MediaCodecDecoder::SetState(DecoderState state) { |
| 275 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " " << state; |
| 276 |
| 277 base::AutoLock lock(state_lock_); |
| 278 state_ = state; |
| 279 } |
| 280 |
| 281 void MediaCodecDecoder::PrefetchNextChunk() { |
| 282 // Media thread |
| 283 DCHECK(media_task_runner_->BelongsToCurrentThread()); |
| 284 |
| 285 DVLOG(1) << class_name() << "::" << __FUNCTION__; |
| 286 |
| 287 AccessUnitQueue::Info au_info; |
| 288 au_queue_.GetInfo(&au_info); |
| 289 |
| 290 if (eos_enqueued_ || au_info.length >= PREFETCH_LIMIT || au_info.has_eos) { |
| 291 // We are done prefetching |
| 292 DVLOG(1) << class_name() << "::" << __FUNCTION__ << " posting PrefetchDone"; |
| 293 media_task_runner_->PostTask( |
| 294 FROM_HERE, base::ResetAndReturn(&prefetch_done_cb_)); |
| 295 return; |
| 296 } |
| 297 |
| 298 request_data_cb_.Run(); |
| 299 } |
| 300 |
| 301 void MediaCodecDecoder::ProcessNextFrame() { |
| 302 // Decoder thread |
| 303 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); |
| 304 |
| 305 DVLOG(1) << class_name() << "::" << __FUNCTION__; |
| 306 |
| 307 DecoderState state = GetState(); |
| 308 |
| 309 if (state != RUNNING && state != STOPPING) { |
| 310 DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": not running"; |
| 311 return; |
| 312 } |
| 313 |
| 314 if (state == STOPPING) { |
| 315 if (NumDelayedRenderTasks() == 0 && !last_frame_posted_) { |
| 316 DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| 317 << ": STOPPING, posting OnLastFrameRendered"; |
| 318 media_task_runner_->PostTask( |
| 319 FROM_HERE, |
| 320 base::Bind(&MediaCodecDecoder::OnLastFrameRendered, |
| 321 weak_this_, false)); |
| 322 last_frame_posted_ = true; |
| 323 } |
| 324 |
| 325 // We can stop processing, the |au_queue_| and MediaCodec queues can freeze. |
| 326 // We only need to let finish the delayed rendering tasks. |
| 327 return; |
| 328 } |
| 329 |
| 330 DCHECK(state == RUNNING); |
| 331 |
| 332 // Keep the number pending video frames low, ideally maintaining |
| 333 // the same audio and video duration after stop request |
| 334 |
| 335 if (NumDelayedRenderTasks() <= 1) { |
| 336 if (!EnqueueInputBuffer()) |
| 337 return; |
| 338 } |
| 339 |
| 340 bool eos_encountered = false; |
| 341 if (!DepleteOutputBufferQueue(&eos_encountered)) |
| 342 return; |
| 343 |
| 344 if (eos_encountered) { |
| 345 DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| 346 << " EOS dequeued, stopping frame processing"; |
| 347 return; |
| 348 } |
| 349 |
| 350 // We need a small delay if we want to stop this thread by |
| 351 // decoder_thread_.Stop() reliably. |
| 352 // The decoder thread message loop processes all pending |
| 353 // (but not delayed) tasks before it can quit; without a delay |
| 354 // the message loop might be forever processing the pendng tasks. |
| 355 decoder_thread_.task_runner()->PostDelayedTask( |
| 356 FROM_HERE, |
| 357 base::Bind(&MediaCodecDecoder::ProcessNextFrame, |
| 358 base::Unretained(this)), |
| 359 base::TimeDelta::FromMilliseconds(NEXT_FRAME_DELAY_MS)); |
| 360 } |
| 361 |
| 362 // Returns false if there was MediaCodec error. |
| 363 bool MediaCodecDecoder::EnqueueInputBuffer() { |
| 364 // Decoder thread |
| 365 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); |
| 366 |
| 367 DVLOG(2) << class_name() << "::" << __FUNCTION__; |
| 368 |
| 369 if (eos_enqueued_) { |
| 370 DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| 371 << ": eos_enqueued, returning"; |
| 372 return true; // Nothing to do |
| 373 } |
| 374 |
| 375 // Get the next frame from the queue and the queue info |
| 376 |
| 377 AccessUnitQueue::Info au_info; |
| 378 au_queue_.GetInfo(&au_info); |
| 379 |
| 380 // Request the data from Demuxer |
| 381 if (au_info.length <= PLAYBACK_LOW_LIMIT && !au_info.has_eos) |
| 382 media_task_runner_->PostTask(FROM_HERE, request_data_cb_); |
| 383 |
| 384 // Get the next frame from the queue |
| 385 |
| 386 if (!au_info.length) { |
| 387 // Report starvation and return, Start() will be called again later. |
| 388 DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| 389 << ": starvation detected"; |
| 390 media_task_runner_->PostTask(FROM_HERE, starvation_cb_); |
| 391 return true; |
| 392 } |
| 393 |
| 394 if (au_info.configs) { |
| 395 DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| 396 << ": received new configs, not implemented"; |
| 397 // post an error for now? |
| 398 media_task_runner_->PostTask(FROM_HERE, error_cb_); |
| 399 return true; |
| 400 } |
| 401 |
| 402 // Dequeue input buffer |
| 403 |
| 404 base::TimeDelta timeout = base::TimeDelta::FromMilliseconds(20); |
| 405 int index = -1; |
| 406 MediaCodecStatus status = |
| 407 media_codec_bridge_->DequeueInputBuffer(timeout, &index); |
| 408 |
| 409 DVLOG(2) << class_name() << ":: DequeueInputBuffer index:" << index; |
| 410 |
| 411 switch (status) { |
| 412 case MEDIA_CODEC_ERROR: |
| 413 DVLOG(0) << class_name() << "::" << __FUNCTION__ |
| 414 << ": MEDIA_CODEC_ERROR DequeueInputBuffer failed"; |
| 415 media_task_runner_->PostTask(FROM_HERE, error_cb_); |
| 416 return false; |
| 417 break; |
| 418 |
| 419 case MEDIA_CODEC_DEQUEUE_INPUT_AGAIN_LATER: |
| 420 return true; |
| 421 break; |
| 422 |
| 423 default: |
| 424 break; |
| 425 } |
| 426 |
| 427 // We got the buffer |
| 428 DCHECK_EQ(status, MEDIA_CODEC_OK); |
| 429 DCHECK_GE(index, 0); |
| 430 |
| 431 const AccessUnit* unit = au_info.front_unit; |
| 432 DCHECK(unit); |
| 433 |
| 434 if (unit->is_end_of_stream) { |
| 435 DVLOG(1) << class_name() << "::" << __FUNCTION__ << ": QueueEOS"; |
| 436 media_codec_bridge_->QueueEOS(index); |
| 437 eos_enqueued_ = true; |
| 438 return true; |
| 439 } |
| 440 |
| 441 status = media_codec_bridge_->QueueInputBuffer( |
| 442 index, &unit->data[0], unit->data.size(), unit->timestamp); |
| 443 |
| 444 if (status == MEDIA_CODEC_ERROR) { |
| 445 DVLOG(1) << class_name() << "::" << __FUNCTION__ |
| 446 << ": MEDIA_CODEC_ERROR: QueueInputBuffer failed"; |
| 447 media_task_runner_->PostTask(FROM_HERE, error_cb_); |
| 448 return false; |
| 449 } |
| 450 |
| 451 // Have successfully queued input buffer |
| 452 au_queue_.PopFront(); |
| 453 return true; |
| 454 } |
| 455 |
| 456 // Returns false if there was MediaCodec error. |
| 457 bool MediaCodecDecoder::DepleteOutputBufferQueue(bool* eos_encountered) { |
| 458 // Decoder thread |
| 459 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); |
| 460 |
| 461 DVLOG(2) << class_name() << "::" << __FUNCTION__; |
| 462 |
| 463 int buffer_index = 0; |
| 464 size_t offset = 0; |
| 465 size_t size = 0; |
| 466 base::TimeDelta pts; |
| 467 MediaCodecStatus status; |
| 468 |
| 469 base::TimeDelta timeout[2]; |
| 470 timeout[0] = base::TimeDelta::FromMilliseconds(20); |
| 471 timeout[1] = base::TimeDelta::FromMilliseconds(0); |
| 472 |
| 473 int timeout_index = 0; |
| 474 |
| 475 do { |
| 476 status = media_codec_bridge_->DequeueOutputBuffer( |
| 477 timeout[timeout_index], |
| 478 &buffer_index, |
| 479 &offset, |
| 480 &size, |
| 481 &pts, |
| 482 eos_encountered, |
| 483 nullptr); |
| 484 |
| 485 timeout_index = 1; |
| 486 |
| 487 switch (status) { |
| 488 case MEDIA_CODEC_OUTPUT_FORMAT_CHANGED: |
| 489 DVLOG(2) << class_name() << "::" << __FUNCTION__ |
| 490 << " MEDIA_CODEC_OUTPUT_FORMAT_CHANGED"; |
| 491 OnOutputFormatChanged(); |
| 492 break; |
| 493 |
| 494 case MEDIA_CODEC_OK: |
| 495 // We got the decoded frame |
| 496 Render(buffer_index, size, true, pts, *eos_encountered); |
| 497 break; |
| 498 |
| 499 case MEDIA_CODEC_ERROR: |
| 500 DVLOG(0) << class_name() << "::" << __FUNCTION__ |
| 501 << ": MEDIA_CODEC_ERROR from DequeueOutputBuffer"; |
| 502 media_task_runner_->PostTask(FROM_HERE, error_cb_); |
| 503 break; |
| 504 |
| 505 default: |
| 506 break; |
| 507 } |
| 508 |
| 509 } while (status != MEDIA_CODEC_DEQUEUE_OUTPUT_AGAIN_LATER && |
| 510 status != MEDIA_CODEC_ERROR && |
| 511 !*eos_encountered); |
| 512 |
| 513 return status != MEDIA_CODEC_ERROR; |
| 514 } |
| 515 |
| 516 void MediaCodecDecoder::CheckLastFrame(bool eos_encountered, |
| 517 bool has_delayed_tasks) { |
| 518 // Decoder thread |
| 519 DCHECK(decoder_thread_.task_runner()->BelongsToCurrentThread()); |
| 520 |
| 521 bool last_frame_when_stopping = |
| 522 GetState() == STOPPING && !has_delayed_tasks; |
| 523 |
| 524 if (last_frame_when_stopping || eos_encountered) { |
| 525 media_task_runner_->PostTask( |
| 526 FROM_HERE, |
| 527 base::Bind(&MediaCodecDecoder::OnLastFrameRendered, |
| 528 weak_this_, eos_encountered)); |
| 529 last_frame_posted_ = true; |
| 530 } |
| 531 } |
| 532 |
| 533 } // namespace media |
OLD | NEW |