| 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..c0fc1939886ed830405f535f3fbf6a3b257e8417 100644
|
| --- a/media/base/android/media_codec_player.cc
|
| +++ b/media/base/android/media_codec_player.cc
|
| @@ -4,9 +4,15 @@
|
|
|
| #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_video_decoder.h"
|
| +#include "media/base/android/media_player_manager.h"
|
| +#include "media/base/buffers.h"
|
|
|
| #define RUN_ON_MEDIA_THREAD(METHOD, ...) \
|
| do { \
|
| @@ -41,22 +47,38 @@ scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner() {
|
|
|
| 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_(STATE_PAUSED),
|
| + 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);
|
| +
|
| + completion_cb_ = base::Bind(
|
| + &MediaPlayerManager::OnPlaybackComplete, manager, player_id);
|
| + attach_listener_cb_ = base::Bind(
|
| + &MediaPlayerAndroid::AttachListener, WeakPtrForUIThread(), nullptr);
|
| + detach_listener_cb_ = base::Bind(
|
| + &MediaPlayerAndroid::DetachListener, WeakPtrForUIThread());
|
| + metadata_changed_cb_ = base::Bind(
|
| + &MediaPlayerAndroid::OnMediaMetadataChanged, WeakPtrForUIThread());
|
| + time_update_cb_ = base::Bind(
|
| + &MediaPlayerAndroid::OnTimeUpdate, WeakPtrForUIThread());
|
| +
|
| weak_this_ = weak_factory_.GetWeakPtr();
|
|
|
| // Finish initializaton on Media thread
|
| @@ -69,6 +91,9 @@ MediaCodecPlayer::~MediaCodecPlayer()
|
| // Media thread
|
| DVLOG(1) << "MediaCodecPlayer::~MediaCodecPlayer";
|
| DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
|
| +
|
| + // Stop decoder threads before destroying decoders
|
| + ReleaseDecoderResources();
|
| }
|
|
|
| void MediaCodecPlayer::Initialize() {
|
| @@ -77,17 +102,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 +129,19 @@ 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_ == STATE_WAITING_FOR_SURFACE) {
|
| + SetState(STATE_PLAYING);
|
| + StartPlaybackDecoders();
|
| + }
|
| }
|
|
|
| void MediaCodecPlayer::Start() {
|
| @@ -109,7 +150,22 @@ void MediaCodecPlayer::Start() {
|
| // Media thread
|
| DVLOG(1) << __FUNCTION__;
|
|
|
| - NOTIMPLEMENTED();
|
| + switch (state_) {
|
| + case STATE_PAUSED:
|
| + if (HasAudio() || HasVideo()) {
|
| + SetState(STATE_PREFETCHING);
|
| + StartPrefetchDecoders();
|
| + } else {
|
| + SetState(STATE_WAITING_FOR_CONFIG);
|
| + }
|
| + break;
|
| + case STATE_STOPPING:
|
| + SetPendingStart(true);
|
| + break;
|
| + default:
|
| + // Ignore
|
| + break;
|
| + }
|
| }
|
|
|
| void MediaCodecPlayer::Pause(bool is_media_related_action) {
|
| @@ -118,7 +174,23 @@ void MediaCodecPlayer::Pause(bool is_media_related_action) {
|
| // Media thread
|
| DVLOG(1) << __FUNCTION__;
|
|
|
| - NOTIMPLEMENTED();
|
| + switch (state_) {
|
| + case STATE_PREFETCHING:
|
| + SetState(STATE_PAUSED);
|
| + StopDecoders();
|
| + break;
|
| + case STATE_WAITING_FOR_SURFACE:
|
| + SetState(STATE_PAUSED);
|
| + StopDecoders();
|
| + break;
|
| + case STATE_PLAYING:
|
| + SetState(STATE_STOPPING);
|
| + RequestToStopDecoders();
|
| + break;
|
| + default:
|
| + // Ignore
|
| + break;
|
| + }
|
| }
|
|
|
| void MediaCodecPlayer::SeekTo(base::TimeDelta timestamp) {
|
| @@ -126,7 +198,6 @@ void MediaCodecPlayer::SeekTo(base::TimeDelta timestamp) {
|
|
|
| // Media thread
|
| DVLOG(1) << __FUNCTION__ << " " << timestamp;
|
| -
|
| NOTIMPLEMENTED();
|
| }
|
|
|
| @@ -136,7 +207,8 @@ void MediaCodecPlayer::Release() {
|
| // Media thread
|
| DVLOG(1) << __FUNCTION__;
|
|
|
| - NOTIMPLEMENTED();
|
| + SetState(STATE_PAUSED);
|
| + ReleaseDecoderResources();
|
| }
|
|
|
| void MediaCodecPlayer::SetVolume(double volume) {
|
| @@ -144,45 +216,37 @@ 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;
|
| + return metadata_cache_.video_size.width();
|
| }
|
|
|
| int MediaCodecPlayer::GetVideoHeight() {
|
| // UI thread
|
| DCHECK(ui_task_runner_->BelongsToCurrentThread());
|
| -
|
| - NOTIMPLEMENTED();
|
| - return 240;
|
| + return metadata_cache_.video_size.height();
|
| }
|
|
|
| base::TimeDelta MediaCodecPlayer::GetCurrentTime() {
|
| - // UI thread, Media thread
|
| - NOTIMPLEMENTED();
|
| - return base::TimeDelta();
|
| + // UI thread
|
| + DCHECK(ui_task_runner_->BelongsToCurrentThread());
|
| + return current_time_cache_;
|
| }
|
|
|
| base::TimeDelta MediaCodecPlayer::GetDuration() {
|
| // UI thread
|
| DCHECK(ui_task_runner_->BelongsToCurrentThread());
|
| -
|
| - NOTIMPLEMENTED();
|
| - return base::TimeDelta();
|
| + return metadata_cache_.duration;
|
| }
|
|
|
| bool MediaCodecPlayer::IsPlaying() {
|
| // UI thread
|
| DCHECK(ui_task_runner_->BelongsToCurrentThread());
|
| - NOTIMPLEMENTED();
|
| - return false;
|
| + return state_ == STATE_PLAYING;
|
| }
|
|
|
| bool MediaCodecPlayer::CanPause() {
|
| @@ -209,7 +273,8 @@ bool MediaCodecPlayer::CanSeekBackward() {
|
| bool MediaCodecPlayer::IsPlayerReady() {
|
| // UI thread
|
| DCHECK(ui_task_runner_->BelongsToCurrentThread());
|
| - NOTIMPLEMENTED();
|
| + // This method is called to check whether it's save to release the player if
|
| + // the OS needs more resources. This class can be released at any time.
|
| return true;
|
| }
|
|
|
| @@ -226,19 +291,40 @@ void MediaCodecPlayer::OnDemuxerConfigsAvailable(
|
| // Media thread
|
| DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
|
|
|
| - NOTIMPLEMENTED();
|
| + DVLOG(1) << __FUNCTION__;
|
| +
|
| + duration_ = configs.duration;
|
| +
|
| + SetDemuxerConfigs(configs);
|
| +
|
| + // Update cache and notify manager on UI thread
|
| + ui_task_runner_->PostTask(
|
| + FROM_HERE, base::Bind(metadata_changed_cb_, duration_, gfx::Size()));
|
| }
|
|
|
| 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 +332,425 @@ void MediaCodecPlayer::OnDemuxerDurationChanged(
|
| base::TimeDelta duration) {
|
| // Media thread
|
| DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
|
| - NOTIMPLEMENTED();
|
| + DVLOG(1) << __FUNCTION__ << " duration:" << duration;
|
| +
|
| + duration_ = duration;
|
| +
|
| + // Update cache and notify manager on UI thread
|
| + ui_task_runner_->PostTask(
|
| + FROM_HERE, base::Bind(metadata_changed_cb_, duration_, gfx::Size()));
|
| +}
|
| +
|
| +// Events from Player, called on UI thread
|
| +
|
| +void MediaCodecPlayer::OnMediaMetadataChanged(base::TimeDelta duration,
|
| + const gfx::Size& video_size) {
|
| + // UI thread
|
| + DCHECK(ui_task_runner_->BelongsToCurrentThread());
|
| +
|
| + if (duration != kNoTimestamp())
|
| + metadata_cache_.duration = duration;
|
| +
|
| + if (!video_size.IsEmpty())
|
| + metadata_cache_.video_size = video_size;
|
| +
|
| + manager()->OnMediaMetadataChanged(
|
| + player_id(), metadata_cache_.duration, metadata_cache_.video_size.width(),
|
| + metadata_cache_.video_size.height(), true);
|
| +}
|
| +
|
| +void MediaCodecPlayer::OnTimeUpdate(base::TimeDelta current_timestamp,
|
| + base::TimeTicks current_time_ticks) {
|
| + // UI thread
|
| + DCHECK(ui_task_runner_->BelongsToCurrentThread());
|
| +
|
| + current_time_cache_ = current_timestamp;
|
| + manager()->OnTimeUpdate(player_id(), current_timestamp, current_time_ticks);
|
| +}
|
| +
|
| +// Events from Decoders, called on Media thread
|
| +
|
| +void MediaCodecPlayer::RequestDemuxerData(DemuxerStream::Type stream_type) {
|
| + DVLOG(2) << __FUNCTION__ << " streamType:" << stream_type;
|
| +
|
| + // Use this method instead of directly binding with
|
| + // DemuxerAndroid::RequestDemuxerData() to avoid the race condition on
|
| + // deletion:
|
| + // 1. DeleteSoon is posted from UI to Media thread.
|
| + // 2. RequestDemuxerData callback is posted from Decoder to Media thread.
|
| + // 3. DeleteSoon arrives, we delete the player and detach from
|
| + // BrowserDemuxerAndroid.
|
| + // 4. RequestDemuxerData is processed by the media thread queue. Since the
|
| + // weak_ptr was invalidated in (3), this is a no-op. If we used
|
| + // DemuxerAndroid::RequestDemuxerData() it would arrive and will try to
|
| + // call the client, 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__;
|
| +
|
| + if (state_ != STATE_PREFETCHING)
|
| + return; // Ignore
|
| +
|
| + if (!HasAudio() && !HasVideo()) {
|
| + // No configuration at all after prefetching.
|
| + // This is an error, initial configuration is expected
|
| + // before the first data chunk.
|
| + GetMediaTaskRunner()->PostTask(FROM_HERE, error_cb_);
|
| + return;
|
| + }
|
| +
|
| + if (HasVideo() && !HasPendingSurface()) {
|
| + SetState(STATE_WAITING_FOR_SURFACE);
|
| + return;
|
| + }
|
| +
|
| + SetState(STATE_PLAYING);
|
| + StartPlaybackDecoders();
|
| +}
|
| +
|
| +void MediaCodecPlayer::OnStarvation() {
|
| + // Media thread
|
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
|
| + DVLOG(1) << __FUNCTION__;
|
| +
|
| + if (state_ != STATE_PLAYING)
|
| + return; // Ignore
|
| +
|
| + SetState(STATE_STOPPING);
|
| + RequestToStopDecoders();
|
| + SetPendingStart(true);
|
| +}
|
| +
|
| +void MediaCodecPlayer::OnStopDone() {
|
| + // Media thread
|
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
|
| + DVLOG(1) << __FUNCTION__;
|
| +
|
| + if (!(audio_decoder_->IsStopped() && video_decoder_->IsStopped()))
|
| + return; // Wait until other stream is stopped
|
| +
|
| + // At this point decoder threads should not be running
|
| + if (interpolator_.interpolating())
|
| + interpolator_.StopInterpolating();
|
| +
|
| + switch (state_) {
|
| + case STATE_STOPPING:
|
| + if (HasPendingStart()) {
|
| + SetPendingStart(false);
|
| + SetState(STATE_PREFETCHING);
|
| + StartPrefetchDecoders();
|
| + } else {
|
| + SetState(STATE_PAUSED);
|
| + }
|
| + break;
|
| + case STATE_PLAYING:
|
| + // Unexpected stop means completion
|
| + SetState(STATE_PAUSED);
|
| + break;
|
| + default:
|
| + DVLOG(0) << __FUNCTION__ << " illegal state: " << AsString(state_);
|
| + NOTREACHED();
|
| + break;
|
| + }
|
| +
|
| + // DetachListener to UI thread
|
| + ui_task_runner_->PostTask(FROM_HERE, detach_listener_cb_);
|
| +
|
| + if (AudioFinished() && VideoFinished())
|
| + ui_task_runner_->PostTask(FROM_HERE, completion_cb_);
|
| +}
|
| +
|
| +void MediaCodecPlayer::OnError() {
|
| + // Media thread
|
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
|
| + DVLOG(1) << __FUNCTION__;
|
| +
|
| + // STATE_ERROR blocks all events
|
| + SetState(STATE_ERROR);
|
| +
|
| + ReleaseDecoderResources();
|
| +}
|
| +
|
| +void MediaCodecPlayer::OnVideoCodecCreated() {
|
| + // Media thread
|
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
|
| +
|
| + // This callback requests resources by releasing other players.
|
| + ui_task_runner_->PostTask(FROM_HERE, request_resources_cb_);
|
| +}
|
| +
|
| +void MediaCodecPlayer::OnVideoSizeChanged(const gfx::Size& size) {
|
| + // Media thread
|
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
|
| +
|
| + DVLOG(1) << __FUNCTION__ << " " << size.width() << "x" << size.height();
|
| +
|
| + // Update cache and notify manager on UI thread
|
| + ui_task_runner_->PostTask(
|
| + FROM_HERE, base::Bind(metadata_changed_cb_, kNoTimestamp(), size));
|
| +}
|
| +
|
| +void MediaCodecPlayer::OnTimeIntervalUpdate(base::TimeDelta now_playing,
|
| + base::TimeDelta last_buffered) {
|
| + // Media thread
|
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
|
| +
|
| + interpolator_.SetBounds(now_playing, last_buffered);
|
| +
|
| + // Post to UI thread
|
| + ui_task_runner_->PostTask(
|
| + FROM_HERE, base::Bind(time_update_cb_, GetInterpolatedTime(),
|
| + base::TimeTicks::Now()));
|
| +}
|
| +
|
| +// State machine operations, called on Media thread
|
| +
|
| +void MediaCodecPlayer::SetState(PlayerState new_state) {
|
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
|
| +
|
| + DVLOG(1) << "SetState:" << AsString(state_) << " -> " << AsString(new_state);
|
| + state_ = new_state;
|
| +}
|
| +
|
| +#undef RETURN_STRING
|
| +#define RETURN_STRING(x) case x: return #x;
|
| +
|
| +const char* MediaCodecPlayer::AsString(PlayerState state) {
|
| + switch (state) {
|
| + RETURN_STRING(STATE_PAUSED);
|
| + RETURN_STRING(STATE_WAITING_FOR_CONFIG);
|
| + RETURN_STRING(STATE_PREFETCHING);
|
| + RETURN_STRING(STATE_PLAYING);
|
| + RETURN_STRING(STATE_STOPPING);
|
| + RETURN_STRING(STATE_WAITING_FOR_SURFACE);
|
| + RETURN_STRING(STATE_WAITING_FOR_SEEK);
|
| + RETURN_STRING(STATE_ERROR);
|
| + default: return "Unknown PlayerState";
|
| + }
|
| +}
|
| +#undef RETURN_STRING
|
| +
|
| +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 pending_start_;
|
| +}
|
| +
|
| +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__;
|
| +
|
| + error_cb_ = base::Bind(&MediaCodecPlayer::OnError, weak_this_);
|
| +
|
| + audio_decoder_.reset(new MediaCodecAudioDecoder(
|
| + GetMediaTaskRunner(),
|
| + base::Bind(&MediaCodecPlayer::RequestDemuxerData, weak_this_,
|
| + DemuxerStream::AUDIO),
|
| + base::Bind(&MediaCodecPlayer::OnStarvation, weak_this_),
|
| + base::Bind(&MediaCodecPlayer::OnStopDone, weak_this_),
|
| + error_cb_,
|
| + base::Bind(&MediaCodecPlayer::OnTimeIntervalUpdate, weak_this_)));
|
| +
|
| + video_decoder_.reset(new MediaCodecVideoDecoder(
|
| + GetMediaTaskRunner(),
|
| + base::Bind(&MediaCodecPlayer::RequestDemuxerData, weak_this_,
|
| + DemuxerStream::VIDEO),
|
| + base::Bind(&MediaCodecPlayer::OnStarvation, weak_this_),
|
| + base::Bind(&MediaCodecPlayer::OnStopDone, weak_this_),
|
| + error_cb_,
|
| + base::Bind(&MediaCodecPlayer::OnVideoSizeChanged, weak_this_),
|
| + base::Bind(&MediaCodecPlayer::OnVideoCodecCreated, weak_this_)));
|
| +}
|
| +
|
| +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__
|
| + << (configs.audio_codec != kUnknownAudioCodec ? " AUDIO" : "")
|
| + << (configs.video_codec != kUnknownVideoCodec ? " VIDEO" : "");
|
| +
|
| + if (configs.audio_codec != kUnknownAudioCodec)
|
| + audio_decoder_->SetDemuxerConfigs(configs);
|
| +
|
| + if (configs.video_codec != kUnknownVideoCodec)
|
| + video_decoder_->SetDemuxerConfigs(configs);
|
| +
|
| + if (state_ == STATE_WAITING_FOR_CONFIG) {
|
| + SetState(STATE_PREFETCHING);
|
| + StartPrefetchDecoders();
|
| + }
|
| +}
|
| +
|
| +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) {
|
| + GetMediaTaskRunner()->PostTask(FROM_HERE, error_cb_);
|
| + return;
|
| + }
|
| + }
|
| +
|
| + if (do_video) {
|
| + MediaCodecDecoder::ConfigStatus status = video_decoder_->Configure();
|
| + if (status != MediaCodecDecoder::CONFIG_OK) {
|
| + GetMediaTaskRunner()->PostTask(FROM_HERE, error_cb_);
|
| + return;
|
| + }
|
| + }
|
| +
|
| + // At this point decoder threads should not be running.
|
| + if (!interpolator_.interpolating())
|
| + interpolator_.StartInterpolating();
|
| +
|
| + base::TimeDelta current_time = GetInterpolatedTime();
|
| +
|
| + if (do_audio) {
|
| + if (!audio_decoder_->Start(current_time)) {
|
| + GetMediaTaskRunner()->PostTask(FROM_HERE, error_cb_);
|
| + return;
|
| + }
|
| +
|
| + // Attach listener on UI thread
|
| + ui_task_runner_->PostTask(FROM_HERE, attach_listener_cb_);
|
| + }
|
| +
|
| + if (do_video) {
|
| + if (!video_decoder_->Start(current_time)) {
|
| + GetMediaTaskRunner()->PostTask(FROM_HERE, error_cb_);
|
| + return;
|
| + }
|
| + }
|
| +}
|
| +
|
| +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;
|
| +
|
| + if (audio_decoder_->IsPrefetchingOrPlaying())
|
| + do_audio = true;
|
| + if (video_decoder_->IsPrefetchingOrPlaying())
|
| + do_video = true;
|
| +
|
| + if (!do_audio && !do_video) {
|
| + 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();
|
| +}
|
| +
|
| +base::TimeDelta MediaCodecPlayer::GetInterpolatedTime() {
|
| + // Media thread
|
| + DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
|
| +
|
| + base::TimeDelta interpolated_time = interpolator_.GetInterpolatedTime();
|
| + return std::min(interpolated_time, duration_);
|
| }
|
|
|
| } // namespace media
|
|
|