Chromium Code Reviews| Index: media/base/android/media_codec_player.cc |
| diff --git a/media/base/android/media_codec_player.cc b/media/base/android/media_codec_player.cc |
| index d7f847336606bee296b72c60d07083259f8c49d8..dfc13d28d3177411e3dbb2a0701a1dd9e633fa33 100644 |
| --- a/media/base/android/media_codec_player.cc |
| +++ b/media/base/android/media_codec_player.cc |
| @@ -4,9 +4,19 @@ |
| #include "media/base/android/media_codec_player.h" |
| +#include "base/barrier_closure.h" |
| #include "base/bind.h" |
| #include "base/lazy_instance.h" |
| #include "base/logging.h" |
| +#include "base/threading/thread.h" |
| + |
| +#include "media/base/android/media_codec_audio_decoder.h" |
| +#include "media/base/android/media_codec_player_state.h" |
| +#include "media/base/android/media_codec_video_decoder.h" |
| +#include "media/base/android/media_player_manager.h" |
| +#include "media/base/buffers.h" |
| + |
| +//#include "base/tvlog.h" |
| #define RUN_ON_MEDIA_THREAD(METHOD, ...) \ |
| do { \ |
| @@ -37,26 +47,39 @@ scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner() { |
| return g_media_thread.Pointer()->task_runner(); |
| } |
| + |
| // MediaCodecPlayer implementation. |
| MediaCodecPlayer::MediaCodecPlayer( |
| int player_id, |
| - MediaPlayerManager* manager, |
| + base::WeakPtr<MediaPlayerManager> manager, |
| const RequestMediaResourcesCB& request_media_resources_cb, |
| scoped_ptr<DemuxerAndroid> demuxer, |
| const GURL& frame_url) |
| : MediaPlayerAndroid(player_id, |
| - manager, |
| + manager.get(), |
| request_media_resources_cb, |
| frame_url), |
| ui_task_runner_(base::MessageLoopProxy::current()), |
| demuxer_(demuxer.Pass()), |
| + state_(StatePaused::Instance()), |
| + interpolator_(&default_tick_clock_), |
| + pending_start_(false), |
| weak_factory_(this) { |
| // UI thread |
| DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| DVLOG(1) << "MediaCodecPlayer::MediaCodecPlayer: player_id:" << player_id; |
| + request_resources_cb_ = base::Bind(request_media_resources_cb_, player_id); |
| + |
| + metadata_changed_cb_ = base::Bind( |
| + &MediaPlayerManager::OnMediaMetadataChanged, manager, player_id); |
| + time_update_cb_ = base::Bind( |
| + &MediaPlayerManager::OnTimeUpdate, manager, player_id); |
| + completion_cb_ = base::Bind( |
| + &MediaPlayerManager::OnPlaybackComplete, manager, player_id); |
| + |
| weak_this_ = weak_factory_.GetWeakPtr(); |
| // Finish initializaton on Media thread |
| @@ -69,6 +92,9 @@ MediaCodecPlayer::~MediaCodecPlayer() |
| // Media thread |
| DVLOG(1) << "MediaCodecPlayer::~MediaCodecPlayer"; |
| DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + |
| + // Stop decoder threads before destroying decoders |
| + ReleaseDecoderResources(); |
| } |
| void MediaCodecPlayer::Initialize() { |
| @@ -77,17 +103,23 @@ void MediaCodecPlayer::Initialize() { |
| DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| demuxer_->Initialize(this); |
| + |
| + interpolator_.SetUpperBound(base::TimeDelta()); |
| + |
| + CreateDecoders(); |
| } |
| -// MediaPlayerAndroid implementation. |
| +// The implementation of MediaPlayerAndroid interface. |
| void MediaCodecPlayer::DeleteOnCorrectThread() { |
| // UI thread |
| DVLOG(1) << __FUNCTION__; |
| DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| - // The listener-related portion of the base class has to be |
| - // destroyed on UI thread. |
| + DetachListener(); |
| + |
| + // The base class part that deals with MediaPlayerListener |
| + // has to be destroyed on UI thread. |
| DestroyListenerOnUIThread(); |
| // Post deletion onto Media thread |
| @@ -98,9 +130,18 @@ void MediaCodecPlayer::SetVideoSurface(gfx::ScopedJavaSurface surface) { |
| RUN_ON_MEDIA_THREAD(SetVideoSurface, base::Passed(&surface)); |
| // Media thread |
| - DVLOG(1) << __FUNCTION__; |
| - |
| - NOTIMPLEMENTED(); |
| + DVLOG(1) << __FUNCTION__ << (surface.IsEmpty() ? " empty" : " non-empty"); |
| + |
| + // I assume that if video decoder already has the surface, |
| + // there will be two calls: |
| + // (1) SetVideoSurface(0) |
| + // (2) SetVideoSurface(new_surface) |
| + video_decoder_->SetPendingSurface(surface.Pass()); |
| + |
| + if (!video_decoder_->HasPendingSurface()) |
| + state_->EventRemoveVideoSurface(this); |
| + else |
| + state_->EventSetVideoSurface(this); |
| } |
| void MediaCodecPlayer::Start() { |
| @@ -108,8 +149,7 @@ void MediaCodecPlayer::Start() { |
| // Media thread |
| DVLOG(1) << __FUNCTION__; |
| - |
| - NOTIMPLEMENTED(); |
| + state_->EventStart(this); |
| } |
| void MediaCodecPlayer::Pause(bool is_media_related_action) { |
| @@ -117,8 +157,7 @@ void MediaCodecPlayer::Pause(bool is_media_related_action) { |
| // Media thread |
| DVLOG(1) << __FUNCTION__; |
| - |
| - NOTIMPLEMENTED(); |
| + state_->EventPause(this); |
| } |
| void MediaCodecPlayer::SeekTo(base::TimeDelta timestamp) { |
| @@ -126,8 +165,7 @@ void MediaCodecPlayer::SeekTo(base::TimeDelta timestamp) { |
| // Media thread |
| DVLOG(1) << __FUNCTION__ << " " << timestamp; |
| - |
| - NOTIMPLEMENTED(); |
| + state_->EventSeekTo(this, timestamp); |
| } |
| void MediaCodecPlayer::Release() { |
| @@ -135,8 +173,7 @@ void MediaCodecPlayer::Release() { |
| // Media thread |
| DVLOG(1) << __FUNCTION__; |
| - |
| - NOTIMPLEMENTED(); |
| + state_->EventRelease(this); |
| } |
| void MediaCodecPlayer::SetVolume(double volume) { |
| @@ -144,45 +181,39 @@ void MediaCodecPlayer::SetVolume(double volume) { |
| // Media thread |
| DVLOG(1) << __FUNCTION__ << " " << volume; |
| - |
| - NOTIMPLEMENTED(); |
| + audio_decoder_->SetVolume(volume); |
| } |
| int MediaCodecPlayer::GetVideoWidth() { |
| - // UI thread |
| - DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| - |
| - NOTIMPLEMENTED(); |
| - return 320; |
| + // UI thread, Media thread |
| + return video_decoder_->GetVideoWidth(); |
| } |
| int MediaCodecPlayer::GetVideoHeight() { |
| - // UI thread |
| - DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| - |
| - NOTIMPLEMENTED(); |
| - return 240; |
| + // UI thread, Media thread |
| + return video_decoder_->GetVideoHeight(); |
| } |
| base::TimeDelta MediaCodecPlayer::GetCurrentTime() { |
| // UI thread, Media thread |
| - NOTIMPLEMENTED(); |
| - return base::TimeDelta(); |
| + base::TimeDelta interpolator_result; |
| + { |
| + base::AutoLock lock(interpolator_lock_); |
| + interpolator_result = interpolator_.GetInterpolatedTime(); |
| + } |
| + return std::min(interpolator_result, duration_); |
| } |
| base::TimeDelta MediaCodecPlayer::GetDuration() { |
| // UI thread |
| DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| - |
| - NOTIMPLEMENTED(); |
| - return base::TimeDelta(); |
| + return duration_; |
| } |
| bool MediaCodecPlayer::IsPlaying() { |
| // UI thread |
| DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| - NOTIMPLEMENTED(); |
| - return false; |
| + return state_ == StatePlaying::Instance(); |
| } |
| bool MediaCodecPlayer::CanPause() { |
| @@ -209,7 +240,9 @@ bool MediaCodecPlayer::CanSeekBackward() { |
| bool MediaCodecPlayer::IsPlayerReady() { |
| // UI thread |
| DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| - NOTIMPLEMENTED(); |
| + // This method is called to check whether it' save to release |
| + // this player if the OS needs more resources. |
| + // For this player Release() can be called at any time. |
| return true; |
| } |
| @@ -226,19 +259,40 @@ void MediaCodecPlayer::OnDemuxerConfigsAvailable( |
| // Media thread |
| DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| - NOTIMPLEMENTED(); |
| + duration_ = configs.duration; |
| + |
| + state_->EventDemuxerConfigsAvailable(this, configs); |
| + |
| + // Post on UI thread |
| + ui_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(metadata_changed_cb_, duration_, |
| + GetVideoWidth(), GetVideoHeight(), true)); |
| } |
| void MediaCodecPlayer::OnDemuxerDataAvailable(const DemuxerData& data) { |
| // Media thread |
| DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| - NOTIMPLEMENTED(); |
| + |
| + DCHECK_LT(0u, data.access_units.size()); |
| + CHECK_GE(1u, data.demuxer_configs.size()); |
| + |
| + DVLOG(2) << "Player::" << __FUNCTION__; |
| + |
| + if (data.type == DemuxerStream::AUDIO) |
| + audio_decoder_->OnDemuxerDataAvailable(data); |
| + |
| + if (data.type == DemuxerStream::VIDEO) |
| + video_decoder_->OnDemuxerDataAvailable(data); |
| } |
| void MediaCodecPlayer::OnDemuxerSeekDone( |
| base::TimeDelta actual_browser_seek_time) { |
| // Media thread |
| DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + |
| + DVLOG(1) << __FUNCTION__ << " actual_time:" << actual_browser_seek_time; |
| + |
| NOTIMPLEMENTED(); |
| } |
| @@ -246,7 +300,309 @@ void MediaCodecPlayer::OnDemuxerDurationChanged( |
| base::TimeDelta duration) { |
| // Media thread |
| DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| - NOTIMPLEMENTED(); |
| + DVLOG(1) << __FUNCTION__ << " duration:" << duration; |
| + duration_ = duration; |
| +} |
| + |
| + |
| +// Events from Decoders, called on Media thread |
| + |
| +void MediaCodecPlayer::RequestDemuxerData(DemuxerStream::Type stream_type) { |
| + DVLOG(1) << __FUNCTION__ << " streamType:" << stream_type; |
| + |
| + // Use this method instead of directly binding with |
| + // DemuxerAndroid::RequestDemuxerData() to avoid the race condition |
| + // on deletion: |
| + // 1. DestroySelf is posted from UI to Media thread. |
| + // 2. RequestDemuxerData callback is posted from Decoder to Media thread. |
| + // 3. DestroySelf arrives, we delete the player and detach from |
| + // BrowserDemuxerAndroid. |
| + // 4. RequestDemuxerData is sent to the player, but weak_ptr blocks it. |
| + // If we used DemuxerAndroid::RequestDemuxerData() it would arrive, |
| + // but the client (i.e. this player) would not exist. |
| + demuxer_->RequestDemuxerData(stream_type); |
| +} |
| + |
| +void MediaCodecPlayer::OnPrefetchDone() { |
| + // Media thread |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + DVLOG(1) << __FUNCTION__; |
| + state_->EventPrefetchDone(this); |
| +} |
| + |
| +void MediaCodecPlayer::OnStarvation() { |
| + // Media thread |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + DVLOG(1) << __FUNCTION__; |
| + state_->EventStarvation(this); |
| +} |
| + |
| +void MediaCodecPlayer::OnStopDone() { |
| + // Media thread |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + DVLOG(1) << __FUNCTION__; |
| + |
| + if (audio_decoder_->IsStopped() && video_decoder_->IsStopped()) { |
| + // At this point decoder threads should not be running |
| + if (interpolator_.interpolating()) |
| + interpolator_.StopInterpolating(); |
| + |
| + state_->EventStopDone(this); |
| + |
| + // DetachListener to UI thread |
| + ui_task_runner_->PostTask(FROM_HERE, base::Bind( |
| + &MediaPlayerAndroid::DetachListener, weak_ptr_for_ui_thread())); |
|
qinmin
2015/05/13 23:17:32
binding the ui thread weakptr on media thread?
Tima Vaisburd
2015/05/14 00:14:59
Yes. What I wanted to do here is to have the weak
qinmin
2015/05/14 17:54:07
Normally it is better to bind the weak ptr on the
Tima Vaisburd
2015/05/15 00:12:38
Done.
|
| + |
| + if (AudioFinished() && VideoFinished()) |
| + ui_task_runner_->PostTask(FROM_HERE, completion_cb_); |
| + } |
| +} |
| + |
| +void MediaCodecPlayer::OnError() { |
| + // Media thread |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + DVLOG(1) << __FUNCTION__; |
| + state_->EventError(this); |
| +} |
| + |
| +// Event from Decoders, called on Decoder thread |
| + |
| +void MediaCodecPlayer::OnTimeIntervalUpdate(base::TimeDelta now_playing, |
| + base::TimeDelta last_buffered) { |
| + // Decoder thread |
| + base::TimeDelta current_time; |
| + { |
| + base::AutoLock lock(interpolator_lock_); |
| + interpolator_.SetBounds(now_playing, last_buffered); |
| + |
| + current_time = std::min(interpolator_.GetInterpolatedTime(), duration_); |
| + } |
| + |
| + // Post on UI thread |
| + ui_task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(time_update_cb_, current_time, base::TimeTicks::Now())); |
| +} |
| + |
| +// State machine operations, called on Media thread |
| + |
| +void MediaCodecPlayer::SetState(MediaCodecPlayerState* new_state) { |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + DCHECK(new_state != nullptr); |
|
qinmin
2015/05/13 23:17:32
nit: simply DCHECK(new_state);
Tima Vaisburd
2015/05/14 00:14:59
Done.
|
| + DVLOG(1) << "SetState:" << state_->name() << " -> " << new_state->name(); |
| + |
| + state_ = new_state; |
| +} |
| + |
| +void MediaCodecPlayer::SetPendingSurface(gfx::ScopedJavaSurface surface) { |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + DVLOG(1) << __FUNCTION__; |
| + |
| + video_decoder_->SetPendingSurface(surface.Pass()); |
| +} |
| + |
| +bool MediaCodecPlayer::HasPendingSurface() { |
| + return video_decoder_->HasPendingSurface(); |
| +} |
| + |
| +void MediaCodecPlayer::SetPendingStart(bool need_to_start) { |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + DVLOG(1) << __FUNCTION__ << ": " << need_to_start; |
| + pending_start_ = need_to_start; |
| +} |
| + |
| +bool MediaCodecPlayer::HasPendingStart() { |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + // Return and reset. |
| + bool ret = pending_start_; |
| + pending_start_ = false; |
| + return ret; |
| +} |
| + |
| +bool MediaCodecPlayer::HasAudio() { |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + return audio_decoder_->HasStream(); |
| +} |
| + |
| +bool MediaCodecPlayer::HasVideo() { |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + return video_decoder_->HasStream(); |
| +} |
| + |
| +void MediaCodecPlayer::CreateDecoders() { |
| + // Media thread |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + DVLOG(1) << __FUNCTION__; |
| + |
| + audio_decoder_.reset(new MediaCodecAudioDecoder( |
| + GetMediaTaskRunner(), |
| + ui_task_runner_, |
| + base::Bind(&MediaCodecPlayer::RequestDemuxerData, weak_this_, |
| + DemuxerStream::AUDIO), |
| + base::Bind(&MediaCodecPlayer::OnStarvation, weak_this_), |
| + base::Bind(&MediaCodecPlayer::OnStopDone, weak_this_), |
| + base::Bind(&MediaCodecPlayer::OnError, weak_this_), |
| + base::Bind(&MediaCodecPlayer::OnTimeIntervalUpdate, |
| + base::Unretained(this)))); |
| + |
| + video_decoder_.reset(new MediaCodecVideoDecoder( |
| + GetMediaTaskRunner(), |
| + ui_task_runner_, |
| + base::Bind(&MediaCodecPlayer::RequestDemuxerData, weak_this_, |
| + DemuxerStream::VIDEO), |
| + base::Bind(&MediaCodecPlayer::OnStarvation, weak_this_), |
| + base::Bind(&MediaCodecPlayer::OnStopDone, weak_this_), |
| + base::Bind(&MediaCodecPlayer::OnError, weak_this_), |
| + request_resources_cb_)); |
| +} |
| + |
| +bool MediaCodecPlayer::AudioFinished() { |
| + return audio_decoder_->IsCompleted() || !audio_decoder_->HasStream(); |
| +} |
| + |
| +bool MediaCodecPlayer::VideoFinished() { |
| + return video_decoder_->IsCompleted() || !video_decoder_->HasStream(); |
| +} |
| + |
| +void MediaCodecPlayer::SetDemuxerConfigs(const DemuxerConfigs& configs) { |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + DVLOG(1) << __FUNCTION__; |
| + |
| + if (configs.audio_codec != kUnknownAudioCodec) |
| + audio_decoder_->SetDemuxerConfigs(configs); |
| + |
| + if (configs.video_codec != kUnknownVideoCodec) |
| + video_decoder_->SetDemuxerConfigs(configs); |
| +} |
| + |
| +void MediaCodecPlayer::StartPrefetchDecoders() { |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + DVLOG(1) << __FUNCTION__; |
| + |
| + bool do_audio = false; |
| + bool do_video = false; |
| + int count = 0; |
| + if (!AudioFinished()) { |
| + do_audio = true; |
| + ++count; |
| + } |
| + if (!VideoFinished()) { |
| + do_video = true; |
| + ++count; |
| + } |
| + |
| + DCHECK_LT(0, count); // at least one decoder should be active |
| + |
| + base::Closure prefetch_cb = base::BarrierClosure( |
| + count, base::Bind(&MediaCodecPlayer::OnPrefetchDone, weak_this_)); |
| + |
| + if (do_audio) |
| + audio_decoder_->Prefetch(prefetch_cb); |
| + |
| + if (do_video) |
| + video_decoder_->Prefetch(prefetch_cb); |
| +} |
| + |
| +void MediaCodecPlayer::StartPlaybackDecoders() { |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + DVLOG(1) << __FUNCTION__; |
| + |
| + // Configure all streams before the start since |
| + // we may discover that browser seek is required. |
| + |
| + bool do_audio = !AudioFinished(); |
| + bool do_video = !VideoFinished(); |
| + |
| + // If there is nothing to play, the state machine should determine |
| + // this at the prefetch state and never call this method. |
| + DCHECK(do_audio || do_video); |
| + |
| + if (do_audio) { |
| + MediaCodecDecoder::ConfigStatus status = audio_decoder_->Configure(); |
| + if (status != MediaCodecDecoder::CONFIG_OK) |
| + goto error; |
| + } |
| + |
| + if (do_video) { |
| + MediaCodecDecoder::ConfigStatus status = video_decoder_->Configure(); |
| + if (status != MediaCodecDecoder::CONFIG_OK) |
| + goto error; |
| + } |
| + |
| + // At this point decoder threads should not be running |
| + if (!interpolator_.interpolating()) |
| + interpolator_.StartInterpolating(); |
| + |
| + if (do_audio) { |
| + if (!audio_decoder_->Start(GetCurrentTime())) |
| + goto error; |
| + |
| + // Attach listener on UI thread |
| + ui_task_runner_->PostTask(FROM_HERE, base::Bind( |
| + &MediaPlayerAndroid::AttachListener, weak_ptr_for_ui_thread(), |
|
qinmin
2015/05/13 23:17:31
ditto
Tima Vaisburd
2015/05/14 00:14:59
Same consideration about weak_ptr here.
|
| + nullptr)); |
| + } |
| + |
| + if (do_video) { |
| + if (!video_decoder_->Start(GetCurrentTime())) |
| + goto error; |
| + } |
| + |
| + return; |
| + |
| +error: |
| + GetMediaTaskRunner()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&MediaCodecPlayer::OnError, weak_this_)); |
| +} |
| + |
| +void MediaCodecPlayer::StopDecoders() { |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + DVLOG(1) << __FUNCTION__; |
| + |
| + audio_decoder_->SyncStop(); |
| + video_decoder_->SyncStop(); |
| +} |
| + |
| +void MediaCodecPlayer::RequestToStopDecoders() { |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + DVLOG(1) << __FUNCTION__; |
| + |
| + bool do_audio = false; |
| + bool do_video = false; |
| + int count = 0; |
| + if (audio_decoder_->IsPrefetchingOrPlaying()) { |
| + do_audio = true; |
| + ++count; |
| + } |
| + if (video_decoder_->IsPrefetchingOrPlaying()) { |
| + do_video = true; |
| + ++count; |
| + } |
| + |
| + if (count == 0) { |
| + GetMediaTaskRunner()->PostTask( |
| + FROM_HERE, base::Bind(&MediaCodecPlayer::OnStopDone, weak_this_)); |
| + return; |
| + } |
| + |
| + if (do_audio) |
| + audio_decoder_->RequestToStop(); |
| + |
| + if (do_video) |
| + video_decoder_->RequestToStop(); |
| +} |
| + |
| +void MediaCodecPlayer::ReleaseDecoderResources() { |
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread()); |
| + DVLOG(1) << __FUNCTION__; |
| + |
| + audio_decoder_->ReleaseDecoderResources(); |
| + video_decoder_->ReleaseDecoderResources(); |
| + |
| + // At this point decoder threads should not be running |
| + if (interpolator_.interpolating()) |
| + interpolator_.StopInterpolating(); |
| } |
| } // namespace media |