| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/base/android/media_source_player.h" | 5 #include "media/base/android/media_source_player.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING); | 293 SetPendingEvent(PREFETCH_REQUEST_EVENT_PENDING); |
| 294 ProcessPendingEvents(); | 294 ProcessPendingEvents(); |
| 295 } | 295 } |
| 296 | 296 |
| 297 void MediaSourcePlayer::OnDemuxerConfigsAvailable( | 297 void MediaSourcePlayer::OnDemuxerConfigsAvailable( |
| 298 const DemuxerConfigs& configs) { | 298 const DemuxerConfigs& configs) { |
| 299 DVLOG(1) << __FUNCTION__; | 299 DVLOG(1) << __FUNCTION__; |
| 300 duration_ = configs.duration; | 300 duration_ = configs.duration; |
| 301 clock_.SetDuration(duration_); | 301 clock_.SetDuration(duration_); |
| 302 | 302 |
| 303 audio_codec_ = configs.audio_codec; | 303 SetDemuxerConfigs(configs, true); |
| 304 num_channels_ = configs.audio_channels; | 304 SetDemuxerConfigs(configs, false); |
| 305 sampling_rate_ = configs.audio_sampling_rate; | |
| 306 is_audio_encrypted_ = configs.is_audio_encrypted; | |
| 307 audio_extra_data_ = configs.audio_extra_data; | |
| 308 video_codec_ = configs.video_codec; | |
| 309 width_ = configs.video_size.width(); | |
| 310 height_ = configs.video_size.height(); | |
| 311 is_video_encrypted_ = configs.is_video_encrypted; | |
| 312 | 305 |
| 313 manager()->OnMediaMetadataChanged( | 306 manager()->OnMediaMetadataChanged( |
| 314 player_id(), duration_, width_, height_, true); | 307 player_id(), duration_, width_, height_, true); |
| 315 | |
| 316 if (IsEventPending(CONFIG_CHANGE_EVENT_PENDING)) { | |
| 317 if (reconfig_audio_decoder_) | |
| 318 ConfigureAudioDecoderJob(); | |
| 319 | |
| 320 if (reconfig_video_decoder_) | |
| 321 ConfigureVideoDecoderJob(); | |
| 322 | |
| 323 ClearPendingEvent(CONFIG_CHANGE_EVENT_PENDING); | |
| 324 | |
| 325 // Resume decoding after the config change if we are still playing. | |
| 326 if (playing_) | |
| 327 StartInternal(); | |
| 328 } | |
| 329 } | 308 } |
| 330 | 309 |
| 331 void MediaSourcePlayer::OnDemuxerDataAvailable(const DemuxerData& data) { | 310 void MediaSourcePlayer::OnDemuxerDataAvailable(const DemuxerData& data) { |
| 332 DVLOG(1) << __FUNCTION__ << "(" << data.type << ")"; | 311 DVLOG(1) << __FUNCTION__ << "(" << data.type << ")"; |
| 333 DCHECK_LT(0u, data.access_units.size()); | 312 DCHECK_LT(0u, data.access_units.size()); |
| 334 | 313 |
| 335 if (has_pending_audio_data_request_ && data.type == DemuxerStream::AUDIO) { | 314 if (has_pending_audio_data_request_ && data.type == DemuxerStream::AUDIO) { |
| 336 has_pending_audio_data_request_ = false; | 315 has_pending_audio_data_request_ = false; |
| 337 ProcessPendingEvents(); | 316 ProcessPendingEvents(); |
| 338 return; | 317 return; |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 489 if (audio_decoder_job_) | 468 if (audio_decoder_job_) |
| 490 audio_decoder_job_->SetBaseTimestamp(GetCurrentTime()); | 469 audio_decoder_job_->SetBaseTimestamp(GetCurrentTime()); |
| 491 demuxer_->RequestDemuxerSeek(GetCurrentTime(), doing_browser_seek_); | 470 demuxer_->RequestDemuxerSeek(GetCurrentTime(), doing_browser_seek_); |
| 492 return; | 471 return; |
| 493 } | 472 } |
| 494 | 473 |
| 495 start_time_ticks_ = base::TimeTicks(); | 474 start_time_ticks_ = base::TimeTicks(); |
| 496 if (IsEventPending(CONFIG_CHANGE_EVENT_PENDING)) { | 475 if (IsEventPending(CONFIG_CHANGE_EVENT_PENDING)) { |
| 497 DVLOG(1) << __FUNCTION__ << " : Handling CONFIG_CHANGE_EVENT."; | 476 DVLOG(1) << __FUNCTION__ << " : Handling CONFIG_CHANGE_EVENT."; |
| 498 DCHECK(reconfig_audio_decoder_ || reconfig_video_decoder_); | 477 DCHECK(reconfig_audio_decoder_ || reconfig_video_decoder_); |
| 499 demuxer_->RequestDemuxerConfigs(); | 478 manager()->OnMediaMetadataChanged( |
| 500 return; | 479 player_id(), duration_, width_, height_, true); |
| 480 |
| 481 if (reconfig_audio_decoder_) |
| 482 ConfigureAudioDecoderJob(); |
| 483 |
| 484 if (reconfig_video_decoder_) |
| 485 ConfigureVideoDecoderJob(); |
| 486 |
| 487 ClearPendingEvent(CONFIG_CHANGE_EVENT_PENDING); |
| 501 } | 488 } |
| 502 | 489 |
| 503 if (IsEventPending(SURFACE_CHANGE_EVENT_PENDING)) { | 490 if (IsEventPending(SURFACE_CHANGE_EVENT_PENDING)) { |
| 504 DVLOG(1) << __FUNCTION__ << " : Handling SURFACE_CHANGE_EVENT."; | 491 DVLOG(1) << __FUNCTION__ << " : Handling SURFACE_CHANGE_EVENT."; |
| 505 // Setting a new surface will require a new MediaCodec to be created. | 492 // Setting a new surface will require a new MediaCodec to be created. |
| 506 ResetVideoDecoderJob(); | 493 ResetVideoDecoderJob(); |
| 507 ConfigureVideoDecoderJob(); | 494 ConfigureVideoDecoderJob(); |
| 508 | 495 |
| 509 // Return early if we can't successfully configure a new video decoder job | 496 // Return early if we can't successfully configure a new video decoder job |
| 510 // yet. | 497 // yet. |
| 511 if (HasVideo() && !video_decoder_job_) | 498 if (HasVideo() && !video_decoder_job_) |
| 512 return; | 499 return; |
| 513 } | 500 } |
| 514 | 501 |
| 515 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING)) { | 502 if (IsEventPending(PREFETCH_REQUEST_EVENT_PENDING)) { |
| 516 DVLOG(1) << __FUNCTION__ << " : Handling PREFETCH_REQUEST_EVENT."; | 503 DVLOG(1) << __FUNCTION__ << " : Handling PREFETCH_REQUEST_EVENT."; |
| 517 // If one of the decoder is not initialized, cancel this event as it will be | 504 // If one of the decoder is not initialized, cancel this event as it will be |
| 518 // called later when Start() is called again. | 505 // called later when Start() is called again. |
| 519 if ((HasVideo() && !video_decoder_job_) || | 506 if ((HasVideo() && !video_decoder_job_) || |
| 520 (HasAudio() && !audio_decoder_job_)) { | 507 (HasAudio() && !audio_decoder_job_)) { |
| 521 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING); | 508 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING); |
| 522 return; | 509 return; |
| 523 } | 510 } |
| 524 | 511 |
| 525 DCHECK(audio_decoder_job_ || AudioFinished()); | 512 DCHECK(audio_decoder_job_ || AudioFinished()); |
| 526 DCHECK(video_decoder_job_ || VideoFinished()); | 513 DCHECK(video_decoder_job_ || VideoFinished()); |
| 527 | |
| 528 int count = (AudioFinished() ? 0 : 1) + (VideoFinished() ? 0 : 1); | 514 int count = (AudioFinished() ? 0 : 1) + (VideoFinished() ? 0 : 1); |
| 529 | 515 |
| 530 // It is possible that all streams have finished decode, yet starvation | 516 // It is possible that all streams have finished decode, yet starvation |
| 531 // occurred during the last stream's EOS decode. In this case, prefetch is a | 517 // occurred during the last stream's EOS decode. In this case, prefetch is a |
| 532 // no-op. | 518 // no-op. |
| 533 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING); | 519 ClearPendingEvent(PREFETCH_REQUEST_EVENT_PENDING); |
| 534 if (count == 0) | 520 if (count == 0) |
| 535 return; | 521 return; |
| 536 | 522 |
| 537 SetPendingEvent(PREFETCH_DONE_EVENT_PENDING); | 523 SetPendingEvent(PREFETCH_DONE_EVENT_PENDING); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 642 // If we have a valid timestamp, start the starvation callback. Otherwise, | 628 // If we have a valid timestamp, start the starvation callback. Otherwise, |
| 643 // reset the |start_time_ticks_| so that the next frame will not suffer | 629 // reset the |start_time_ticks_| so that the next frame will not suffer |
| 644 // from the decoding delay caused by the current frame. | 630 // from the decoding delay caused by the current frame. |
| 645 if (current_presentation_timestamp != kNoTimestamp()) | 631 if (current_presentation_timestamp != kNoTimestamp()) |
| 646 StartStarvationCallback(current_presentation_timestamp, | 632 StartStarvationCallback(current_presentation_timestamp, |
| 647 max_presentation_timestamp); | 633 max_presentation_timestamp); |
| 648 else | 634 else |
| 649 start_time_ticks_ = base::TimeTicks::Now(); | 635 start_time_ticks_ = base::TimeTicks::Now(); |
| 650 } | 636 } |
| 651 | 637 |
| 652 if (is_audio) { | 638 if (is_audio) |
| 653 DecodeMoreAudio(); | 639 DecodeMoreAudio(); |
| 654 return; | 640 else |
| 655 } | 641 DecodeMoreVideo(); |
| 656 | 642 |
| 657 DecodeMoreVideo(); | 643 if (IsEventPending(CONFIG_CHANGE_EVENT_PENDING)) |
| 644 ProcessPendingEvents(); |
| 658 } | 645 } |
| 659 | 646 |
| 660 void MediaSourcePlayer::DecodeMoreAudio() { | 647 void MediaSourcePlayer::DecodeMoreAudio() { |
| 661 DVLOG(1) << __FUNCTION__; | 648 DVLOG(1) << __FUNCTION__; |
| 662 DCHECK(!audio_decoder_job_->is_decoding()); | 649 DCHECK(!audio_decoder_job_->is_decoding()); |
| 663 DCHECK(!AudioFinished()); | 650 DCHECK(!AudioFinished()); |
| 664 | 651 |
| 665 if (audio_decoder_job_->Decode( | 652 if (audio_decoder_job_->Decode( |
| 666 start_time_ticks_, | 653 start_time_ticks_, |
| 667 start_presentation_timestamp_, | 654 start_presentation_timestamp_, |
| 668 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, | 655 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, |
| 669 weak_factory_.GetWeakPtr(), | 656 weak_factory_.GetWeakPtr(), |
| 670 true))) { | 657 true))) { |
| 671 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreAudio", | 658 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreAudio", |
| 672 audio_decoder_job_.get()); | 659 audio_decoder_job_.get()); |
| 673 return; | 660 return; |
| 674 } | 661 } |
| 675 | 662 |
| 676 // Failed to start the next decode. | 663 // Failed to start the next decode. |
| 677 // Wait for demuxer ready message. | 664 // Wait for demuxer ready message. |
| 678 DCHECK(!reconfig_audio_decoder_); | 665 DCHECK(!reconfig_audio_decoder_); |
| 679 reconfig_audio_decoder_ = true; | 666 reconfig_audio_decoder_ = true; |
| 667 DemuxerConfigs* configs = audio_decoder_job_->GetDemuxerConfigs(); |
| 668 DCHECK(configs); |
| 669 SetDemuxerConfigs(*configs, true); |
| 680 | 670 |
| 681 // Config change may have just been detected on the other stream. If so, | 671 // Config change may have just been detected on the other stream. If so, |
| 682 // don't send a duplicate demuxer config request. | 672 // don't send a duplicate demuxer config request. |
| 683 if (IsEventPending(CONFIG_CHANGE_EVENT_PENDING)) { | 673 if (IsEventPending(CONFIG_CHANGE_EVENT_PENDING)) { |
| 684 DCHECK(reconfig_video_decoder_); | 674 DCHECK(reconfig_video_decoder_); |
| 685 return; | 675 return; |
| 686 } | 676 } |
| 687 | 677 |
| 688 SetPendingEvent(CONFIG_CHANGE_EVENT_PENDING); | 678 SetPendingEvent(CONFIG_CHANGE_EVENT_PENDING); |
| 689 ProcessPendingEvents(); | |
| 690 } | 679 } |
| 691 | 680 |
| 692 void MediaSourcePlayer::DecodeMoreVideo() { | 681 void MediaSourcePlayer::DecodeMoreVideo() { |
| 693 DVLOG(1) << __FUNCTION__; | 682 DVLOG(1) << __FUNCTION__; |
| 694 DCHECK(!video_decoder_job_->is_decoding()); | 683 DCHECK(!video_decoder_job_->is_decoding()); |
| 695 DCHECK(!VideoFinished()); | 684 DCHECK(!VideoFinished()); |
| 696 | 685 |
| 697 if (video_decoder_job_->Decode( | 686 if (video_decoder_job_->Decode( |
| 698 start_time_ticks_, | 687 start_time_ticks_, |
| 699 start_presentation_timestamp_, | 688 start_presentation_timestamp_, |
| 700 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, | 689 base::Bind(&MediaSourcePlayer::MediaDecoderCallback, |
| 701 weak_factory_.GetWeakPtr(), | 690 weak_factory_.GetWeakPtr(), |
| 702 false))) { | 691 false))) { |
| 703 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreVideo", | 692 TRACE_EVENT_ASYNC_BEGIN0("media", "MediaSourcePlayer::DecodeMoreVideo", |
| 704 video_decoder_job_.get()); | 693 video_decoder_job_.get()); |
| 705 return; | 694 return; |
| 706 } | 695 } |
| 707 | 696 |
| 708 // Failed to start the next decode. | 697 // Failed to start the next decode. |
| 709 // Wait for demuxer ready message. | 698 // Wait for demuxer ready message. |
| 710 | 699 |
| 711 // After this detection of video config change, next video data received | 700 // After this detection of video config change, next video data received |
| 712 // will begin with I-frame. | 701 // will begin with I-frame. |
| 713 next_video_data_is_iframe_ = true; | 702 next_video_data_is_iframe_ = true; |
| 714 | 703 |
| 715 DCHECK(!reconfig_video_decoder_); | 704 DCHECK(!reconfig_video_decoder_); |
| 716 reconfig_video_decoder_ = true; | 705 reconfig_video_decoder_ = true; |
| 706 DemuxerConfigs* configs = video_decoder_job_->GetDemuxerConfigs(); |
| 707 DCHECK(configs); |
| 708 SetDemuxerConfigs(*configs, false); |
| 717 | 709 |
| 718 // Config change may have just been detected on the other stream. If so, | 710 // Config change may have just been detected on the other stream. If so, |
| 719 // don't send a duplicate demuxer config request. | 711 // don't send a duplicate demuxer config request. |
| 720 if (IsEventPending(CONFIG_CHANGE_EVENT_PENDING)) { | 712 if (IsEventPending(CONFIG_CHANGE_EVENT_PENDING)) { |
| 721 DCHECK(reconfig_audio_decoder_); | 713 DCHECK(reconfig_audio_decoder_); |
| 722 return; | 714 return; |
| 723 } | 715 } |
| 724 | 716 |
| 725 SetPendingEvent(CONFIG_CHANGE_EVENT_PENDING); | 717 SetPendingEvent(CONFIG_CHANGE_EVENT_PENDING); |
| 726 ProcessPendingEvents(); | |
| 727 } | 718 } |
| 728 | 719 |
| 729 void MediaSourcePlayer::PlaybackCompleted(bool is_audio) { | 720 void MediaSourcePlayer::PlaybackCompleted(bool is_audio) { |
| 730 DVLOG(1) << __FUNCTION__ << "(" << is_audio << ")"; | 721 DVLOG(1) << __FUNCTION__ << "(" << is_audio << ")"; |
| 731 if (is_audio) | 722 if (is_audio) |
| 732 reached_audio_eos_ = true; | 723 reached_audio_eos_ = true; |
| 733 else | 724 else |
| 734 reached_video_eos_ = true; | 725 reached_video_eos_ = true; |
| 735 | 726 |
| 736 if (AudioFinished() && VideoFinished()) { | 727 if (AudioFinished() && VideoFinished()) { |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 967 start_time_ticks_ = base::TimeTicks::Now(); | 958 start_time_ticks_ = base::TimeTicks::Now(); |
| 968 start_presentation_timestamp_ = GetCurrentTime(); | 959 start_presentation_timestamp_ = GetCurrentTime(); |
| 969 if (!clock_.IsPlaying()) | 960 if (!clock_.IsPlaying()) |
| 970 clock_.Play(); | 961 clock_.Play(); |
| 971 | 962 |
| 972 if (!AudioFinished()) | 963 if (!AudioFinished()) |
| 973 DecodeMoreAudio(); | 964 DecodeMoreAudio(); |
| 974 | 965 |
| 975 if (!VideoFinished()) | 966 if (!VideoFinished()) |
| 976 DecodeMoreVideo(); | 967 DecodeMoreVideo(); |
| 968 |
| 969 if (IsEventPending(CONFIG_CHANGE_EVENT_PENDING)) |
| 970 ProcessPendingEvents(); |
| 977 } | 971 } |
| 978 | 972 |
| 979 const char* MediaSourcePlayer::GetEventName(PendingEventFlags event) { | 973 const char* MediaSourcePlayer::GetEventName(PendingEventFlags event) { |
| 980 static const char* kPendingEventNames[] = { | 974 static const char* kPendingEventNames[] = { |
| 981 "SEEK", | 975 "SEEK", |
| 982 "SURFACE_CHANGE", | 976 "SURFACE_CHANGE", |
| 983 "CONFIG_CHANGE", | 977 "CONFIG_CHANGE", |
| 984 "PREFETCH_REQUEST", | 978 "PREFETCH_REQUEST", |
| 985 "PREFETCH_DONE", | 979 "PREFETCH_DONE", |
| 986 }; | 980 }; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1007 } | 1001 } |
| 1008 | 1002 |
| 1009 void MediaSourcePlayer::ClearPendingEvent(PendingEventFlags event) { | 1003 void MediaSourcePlayer::ClearPendingEvent(PendingEventFlags event) { |
| 1010 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")"; | 1004 DVLOG(1) << __FUNCTION__ << "(" << GetEventName(event) << ")"; |
| 1011 DCHECK_NE(event, NO_EVENT_PENDING); | 1005 DCHECK_NE(event, NO_EVENT_PENDING); |
| 1012 DCHECK(IsEventPending(event)) << GetEventName(event); | 1006 DCHECK(IsEventPending(event)) << GetEventName(event); |
| 1013 | 1007 |
| 1014 pending_event_ &= ~event; | 1008 pending_event_ &= ~event; |
| 1015 } | 1009 } |
| 1016 | 1010 |
| 1011 void MediaSourcePlayer::SetDemuxerConfigs(const DemuxerConfigs& configs, |
| 1012 bool is_audio) { |
| 1013 if (is_audio) { |
| 1014 audio_codec_ = configs.audio_codec; |
| 1015 num_channels_ = configs.audio_channels; |
| 1016 sampling_rate_ = configs.audio_sampling_rate; |
| 1017 is_audio_encrypted_ = configs.is_audio_encrypted; |
| 1018 audio_extra_data_ = configs.audio_extra_data; |
| 1019 } else { |
| 1020 video_codec_ = configs.video_codec; |
| 1021 width_ = configs.video_size.width(); |
| 1022 height_ = configs.video_size.height(); |
| 1023 is_video_encrypted_ = configs.is_video_encrypted; |
| 1024 } |
| 1025 } |
| 1026 |
| 1017 } // namespace media | 1027 } // namespace media |
| OLD | NEW |