Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(789)

Unified Diff: media/base/android/media_codec_player.cc

Issue 1128383003: Implementation of MediaCodecPlayer stage 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed unused media_codec_player_state.h and .cc Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..ba10199bb6df134b42c1dc784e22fe5b8aa2c92e 100644
--- a/media/base/android/media_codec_player.cc
+++ b/media/base/android/media_codec_player.cc
@@ -4,9 +4,16 @@
#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"
+
wolenetz 2015/05/19 23:05:23 nit: drop extra line
Tima Vaisburd 2015/05/22 22:48:55 Done.
+#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 { \
@@ -37,26 +44,82 @@ scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner() {
return g_media_thread.Pointer()->task_runner();
}
+
qinmin 2015/05/19 19:26:25 nit: remove the extra line.
Tima Vaisburd 2015/05/20 05:19:58 Done.
// MediaCodecPlayer implementation.
+/*
wolenetz 2015/05/19 23:05:23 nit: please move the state machine diagram to the
+ State machine diagram.
+
+
+ --------------------------> Paused
+ | StopDone:
+ | ^ |
+ | | |
+ | Pause: | | Start: dec.Prefetch
+ | | |
+ | | |
+ | | |
+ | | v PrefetchDone,
+ | no surface:
+ | ------------------> Prefetching --------------> Waiting
+ | | for surface
+ | | | /
+ | | | /
+ | | StopDone w/ | /
+ | | pending start: | PrefetchDone: /
+ | | dec.Prefetch | dec.Start /
+ | | | / SetSurface:
+ | | | / dec.Start
+ | | | /
+ | | v /
+ | | /
+ | | Playing <----------/
+ | |
+ | | |
+ | | |
+ | | | Pause: dec.RequestToStop
+ | | |
+ | | |
+ | | v
+ | |
+ -------------------------- Stopping
+
+ */
+
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);
+
+ 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);
+ attach_listener_cb_ = base::Bind(
+ &MediaPlayerAndroid::AttachListener, WeakPtrForUIThread(), nullptr);
+ detach_listener_cb_ = base::Bind(
+ &MediaPlayerAndroid::DetachListener, WeakPtrForUIThread());
+
weak_this_ = weak_factory_.GetWeakPtr();
// Finish initializaton on Media thread
@@ -69,6 +132,9 @@ MediaCodecPlayer::~MediaCodecPlayer()
// Media thread
DVLOG(1) << "MediaCodecPlayer::~MediaCodecPlayer";
DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
+
+ // Stop decoder threads before destroying decoders
+ ReleaseDecoderResources();
}
void MediaCodecPlayer::Initialize() {
@@ -77,17 +143,23 @@ void MediaCodecPlayer::Initialize() {
DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
demuxer_->Initialize(this);
+
+ interpolator_.SetUpperBound(base::TimeDelta());
qinmin 2015/05/19 19:26:25 Can you call this without calling startInterpolati
+
+ 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 +170,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() &&
qinmin 2015/05/19 19:26:25 nit: extra space after "if"
Tima Vaisburd 2015/05/20 05:19:59 Done.
+ state_ == STATE_WAITING_FOR_SURFACE) {
+ SetState(STATE_PLAYING);
+ StartPlaybackDecoders();
+ }
}
void MediaCodecPlayer::Start() {
@@ -109,7 +191,18 @@ void MediaCodecPlayer::Start() {
// Media thread
DVLOG(1) << __FUNCTION__;
- NOTIMPLEMENTED();
+ switch (state_) {
+ case STATE_PAUSED:
+ SetState(STATE_PREFETCHING);
+ StartPrefetchDecoders();
+ break;
+ case STATE_STOPPING:
+ SetPendingStart(true);
+ break;
+ default:
+ // Ignore
+ break;
+ }
}
void MediaCodecPlayer::Pause(bool is_media_related_action) {
@@ -118,7 +211,26 @@ 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_CONFIG:
+ SetState(STATE_PAUSED);
+ 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 +238,6 @@ void MediaCodecPlayer::SeekTo(base::TimeDelta timestamp) {
// Media thread
DVLOG(1) << __FUNCTION__ << " " << timestamp;
-
NOTIMPLEMENTED();
}
@@ -136,7 +247,8 @@ void MediaCodecPlayer::Release() {
// Media thread
DVLOG(1) << __FUNCTION__;
- NOTIMPLEMENTED();
+ SetState(STATE_PAUSED);
+ ReleaseDecoderResources();
}
void MediaCodecPlayer::SetVolume(double volume) {
@@ -144,45 +256,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
qinmin 2015/05/19 19:26:25 It is bad that these calls can span multiple threa
Tima Vaisburd 2015/05/20 05:19:58 I did the caching but I'm not sure I followed the
+ return video_decoder_->GetVideoWidth();
}
int MediaCodecPlayer::GetVideoHeight() {
- // UI thread
- DCHECK(ui_task_runner_->BelongsToCurrentThread());
-
- NOTIMPLEMENTED();
- return 240;
+ // UI thread, Media thread
qinmin 2015/05/19 19:26:26 ditto
Tima Vaisburd 2015/05/20 05:19:59 Same as above.
+ 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_ == STATE_PLAYING;
}
bool MediaCodecPlayer::CanPause() {
@@ -209,7 +315,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 +334,54 @@ void MediaCodecPlayer::OnDemuxerConfigsAvailable(
// Media thread
DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
- NOTIMPLEMENTED();
+ duration_ = configs.duration;
+
+ SetDemuxerConfigs(configs);
+
+ switch (state_) {
+ case STATE_WAITING_FOR_CONFIG:
+ if (HasVideo() && !HasPendingSurface()) {
+ SetState(STATE_WAITING_FOR_SURFACE);
+ } else {
+ SetState(STATE_PLAYING);
qinmin 2015/05/19 19:26:25 No idea why the player has to start playing here.
Tima Vaisburd 2015/05/20 05:19:58 But below you confirmed that this state is impossi
qinmin 2015/05/20 15:39:25 Prefetching and OnDemuxerConfigsAvailable are 2 di
Tima Vaisburd 2015/05/20 17:34:56 Please take a look at PatchSet 9 where I changed t
+ StartPlaybackDecoders();
+ }
+ break;
+ default:
+ // Do nothing
+ break;
+ }
+
+ // 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 +389,371 @@ void MediaCodecPlayer::OnDemuxerDurationChanged(
base::TimeDelta duration) {
// Media thread
DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
- NOTIMPLEMENTED();
+ DVLOG(1) << __FUNCTION__ << " duration:" << duration;
+ duration_ = duration;
+}
+
qinmin 2015/05/19 19:26:25 nit: extra line
Tima Vaisburd 2015/05/20 05:19:58 Done.
+
+// 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
wolenetz 2015/05/19 23:05:23 nit: remove extra space after 'the'
Tima Vaisburd 2015/05/22 22:48:55 Done.
+ // on deletion:
+ // 1. DestroySelf is posted from UI to Media thread.
wolenetz 2015/05/19 23:05:23 What is DestroySelf? It's not in existence in this
Tima Vaisburd 2015/05/22 22:48:54 Yes, exactly. Fixed and reformulated slightly.
+ // 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
wolenetz 2015/05/19 23:05:23 Here, and elsewhere, a DCHECK ( I am on the right
Tima Vaisburd 2015/05/22 22:48:55 I found it easier to grasp a short comment written
+ DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
+ DVLOG(1) << __FUNCTION__;
+
+ if (state_ != STATE_PREFETCHING)
wolenetz 2015/05/19 23:05:23 When might this happen with this new player/decode
Tima Vaisburd 2015/05/22 22:48:54 Release() may be called on Media thread before we
+ return; // Ignore
+
+ if (!HasAudio() && !HasVideo()) {
+ // No configuration at all, let's wait for it.
qinmin 2015/05/19 19:26:25 This shouldn't happen. I would just put NOT_REACHE
Tima Vaisburd 2015/05/20 05:19:59 I raised an error
+ // Although it probably should never happen here,
+ // after the data has come from demuxer.
+ SetState(STATE_WAITING_FOR_CONFIG);
+ return;
+ }
+
+ if (HasVideo() && !HasPendingSurface()) {
+ SetState(STATE_WAITING_FOR_SURFACE);
wolenetz 2015/05/19 23:05:23 I'm not sure how this happens given the current co
Tima Vaisburd 2015/05/22 22:48:54 Yes, the surface can be removed at any time, but c
+ 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();
wolenetz 2015/05/19 23:05:23 10,000' view: why does starvation stop decoders?
Tima Vaisburd 2015/05/22 22:48:54 I think we need to stop the other channel... Upon
+ 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()) {
+ SetState(STATE_PREFETCHING);
+ StartPrefetchDecoders();
+ } else {
+ SetState(STATE_PAUSED);
+ }
+ break;
+ case STATE_PLAYING:
+ // Unexpected stop means completion
+ SetState(STATE_PAUSED);
+ break;
+ default:
+ 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();
+}
+
+// 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(PlayerState new_state) {
+ DCHECK(GetMediaTaskRunner()->BelongsToCurrentThread());
+
+ DVLOG(1) << "SetState:" << AsString(state_) << " -> " << AsString(new_state);
+ state_ = new_state;
+}
+
+#undef ENTRY
+#define ENTRY(x) case x: return #x;
+
+const char* MediaCodecPlayer::AsString(PlayerState state) {
+ switch (state) {
+ ENTRY(STATE_PAUSED);
+ ENTRY(STATE_PREFETCHING);
+ ENTRY(STATE_PLAYING);
+ ENTRY(STATE_STOPPING);
+ ENTRY(STATE_WAITING_FOR_CONFIG);
+ ENTRY(STATE_WAITING_FOR_SURFACE);
+ ENTRY(STATE_WAITING_FOR_SEEK);
+ ENTRY(STATE_ERROR);
+ default: return "Unknown PlayerState";
+ }
+}
+#undef ENTRY
+
+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() {
qinmin 2015/05/19 19:26:25 The function name just don't match what it does. H
Tima Vaisburd 2015/05/20 05:19:58 Right now called two methods since so far HasPendi
+ 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__;
+
+ error_cb_ = base::Bind(&MediaCodecPlayer::OnError, weak_this_);
+
+ 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_),
+ error_cb_,
+ 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_),
+ error_cb_,
+ request_resources_cb_));
qinmin 2015/05/19 19:26:25 request_resources_cb_ is binded to the UI thread,
Tima Vaisburd 2015/05/20 05:19:58 ui_task_runner_ is accessible from VideoDecoder vi
qinmin 2015/05/20 15:48:16 I don't feel it proper for Decoder to know ui_task
Tima Vaisburd 2015/05/22 23:37:52 We have two callbacks now, both from the video dec
qinmin 2015/05/26 00:49:19 No, i don't that's logically sound. And I don't th
+}
+
+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) {
+ 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();
+
+ if (do_audio) {
+ if (!audio_decoder_->Start(GetCurrentTime())) {
+ 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(GetCurrentTime())) {
+ 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;
+ int count = 0;
+ if (audio_decoder_->IsPrefetchingOrPlaying()) {
+ do_audio = true;
+ ++count;
+ }
+ if (video_decoder_->IsPrefetchingOrPlaying()) {
+ do_video = true;
+ ++count;
+ }
+
+ if (count == 0) {
qinmin 2015/05/19 19:26:26 No barrier closure is used here, why not simply re
Tima Vaisburd 2015/05/20 05:19:59 Yes, it was a silly copy-pasting.
+ 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

Powered by Google App Engine
This is Rietveld 408576698