summaryrefslogtreecommitdiffstats
path: root/chromium/media/base/pipeline.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/media/base/pipeline.cc')
-rw-r--r--chromium/media/base/pipeline.cc527
1 files changed, 241 insertions, 286 deletions
diff --git a/chromium/media/base/pipeline.cc b/chromium/media/base/pipeline.cc
index 5799dc3f410..46be2b86e68 100644
--- a/chromium/media/base/pipeline.cc
+++ b/chromium/media/base/pipeline.cc
@@ -10,8 +10,9 @@
#include "base/callback.h"
#include "base/callback_helpers.h"
#include "base/compiler_specific.h"
-#include "base/message_loop/message_loop.h"
+#include "base/location.h"
#include "base/metrics/histogram.h"
+#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
@@ -31,26 +32,24 @@ using base::TimeDelta;
namespace media {
-Pipeline::Pipeline(const scoped_refptr<base::MessageLoopProxy>& message_loop,
- MediaLog* media_log)
- : message_loop_(message_loop),
+Pipeline::Pipeline(
+ const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
+ MediaLog* media_log)
+ : task_runner_(task_runner),
media_log_(media_log),
running_(false),
did_loading_progress_(false),
- total_bytes_(0),
- natural_size_(0, 0),
volume_(1.0f),
playback_rate_(0.0f),
clock_(new Clock(&default_tick_clock_)),
- waiting_for_clock_update_(false),
+ clock_state_(CLOCK_PAUSED),
status_(PIPELINE_OK),
- has_audio_(false),
- has_video_(false),
state_(kCreated),
audio_ended_(false),
video_ended_(false),
text_ended_(false),
- audio_disabled_(false),
+ audio_buffering_state_(BUFFERING_HAVE_NOTHING),
+ video_buffering_state_(BUFFERING_HAVE_NOTHING),
demuxer_(NULL),
creation_time_(default_tick_clock_.NowTicks()) {
media_log_->AddEvent(media_log_->CreatePipelineStateChangedEvent(kCreated));
@@ -73,21 +72,34 @@ void Pipeline::Start(scoped_ptr<FilterCollection> collection,
const base::Closure& ended_cb,
const PipelineStatusCB& error_cb,
const PipelineStatusCB& seek_cb,
- const BufferingStateCB& buffering_state_cb,
+ const PipelineMetadataCB& metadata_cb,
+ const base::Closure& preroll_completed_cb,
const base::Closure& duration_change_cb) {
+ DCHECK(!ended_cb.is_null());
+ DCHECK(!error_cb.is_null());
+ DCHECK(!seek_cb.is_null());
+ DCHECK(!metadata_cb.is_null());
+ DCHECK(!preroll_completed_cb.is_null());
+
base::AutoLock auto_lock(lock_);
CHECK(!running_) << "Media pipeline is already running";
- DCHECK(!buffering_state_cb.is_null());
-
running_ = true;
- message_loop_->PostTask(FROM_HERE, base::Bind(
- &Pipeline::StartTask, base::Unretained(this), base::Passed(&collection),
- ended_cb, error_cb, seek_cb, buffering_state_cb, duration_change_cb));
+
+ filter_collection_ = collection.Pass();
+ ended_cb_ = ended_cb;
+ error_cb_ = error_cb;
+ seek_cb_ = seek_cb;
+ metadata_cb_ = metadata_cb;
+ preroll_completed_cb_ = preroll_completed_cb;
+ duration_change_cb_ = duration_change_cb;
+
+ task_runner_->PostTask(
+ FROM_HERE, base::Bind(&Pipeline::StartTask, base::Unretained(this)));
}
void Pipeline::Stop(const base::Closure& stop_cb) {
base::AutoLock auto_lock(lock_);
- message_loop_->PostTask(FROM_HERE, base::Bind(
+ task_runner_->PostTask(FROM_HERE, base::Bind(
&Pipeline::StopTask, base::Unretained(this), stop_cb));
}
@@ -98,7 +110,7 @@ void Pipeline::Seek(TimeDelta time, const PipelineStatusCB& seek_cb) {
return;
}
- message_loop_->PostTask(FROM_HERE, base::Bind(
+ task_runner_->PostTask(FROM_HERE, base::Bind(
&Pipeline::SeekTask, base::Unretained(this), time, seek_cb));
}
@@ -107,16 +119,6 @@ bool Pipeline::IsRunning() const {
return running_;
}
-bool Pipeline::HasAudio() const {
- base::AutoLock auto_lock(lock_);
- return has_audio_;
-}
-
-bool Pipeline::HasVideo() const {
- base::AutoLock auto_lock(lock_);
- return has_video_;
-}
-
float Pipeline::GetPlaybackRate() const {
base::AutoLock auto_lock(lock_);
return playback_rate_;
@@ -129,7 +131,7 @@ void Pipeline::SetPlaybackRate(float playback_rate) {
base::AutoLock auto_lock(lock_);
playback_rate_ = playback_rate;
if (running_) {
- message_loop_->PostTask(FROM_HERE, base::Bind(
+ task_runner_->PostTask(FROM_HERE, base::Bind(
&Pipeline::PlaybackRateChangedTask, base::Unretained(this),
playback_rate));
}
@@ -147,7 +149,7 @@ void Pipeline::SetVolume(float volume) {
base::AutoLock auto_lock(lock_);
volume_ = volume;
if (running_) {
- message_loop_->PostTask(FROM_HERE, base::Bind(
+ task_runner_->PostTask(FROM_HERE, base::Bind(
&Pipeline::VolumeChangedTask, base::Unretained(this), volume));
}
}
@@ -157,24 +159,9 @@ TimeDelta Pipeline::GetMediaTime() const {
return clock_->Elapsed();
}
-Ranges<TimeDelta> Pipeline::GetBufferedTimeRanges() {
+Ranges<TimeDelta> Pipeline::GetBufferedTimeRanges() const {
base::AutoLock auto_lock(lock_);
- Ranges<TimeDelta> time_ranges;
- for (size_t i = 0; i < buffered_time_ranges_.size(); ++i) {
- time_ranges.Add(buffered_time_ranges_.start(i),
- buffered_time_ranges_.end(i));
- }
- if (clock_->Duration() == TimeDelta() || total_bytes_ == 0)
- return time_ranges;
- for (size_t i = 0; i < buffered_byte_ranges_.size(); ++i) {
- TimeDelta start = TimeForByteOffset_Locked(buffered_byte_ranges_.start(i));
- TimeDelta end = TimeForByteOffset_Locked(buffered_byte_ranges_.end(i));
- // Cap approximated buffered time at the length of the video.
- end = std::min(end, clock_->Duration());
- time_ranges.Add(start, end);
- }
-
- return time_ranges;
+ return buffered_time_ranges_;
}
TimeDelta Pipeline::GetMediaDuration() const {
@@ -182,18 +169,7 @@ TimeDelta Pipeline::GetMediaDuration() const {
return clock_->Duration();
}
-int64 Pipeline::GetTotalBytes() const {
- base::AutoLock auto_lock(lock_);
- return total_bytes_;
-}
-
-void Pipeline::GetNaturalVideoSize(gfx::Size* out_size) const {
- CHECK(out_size);
- base::AutoLock auto_lock(lock_);
- *out_size = natural_size_;
-}
-
-bool Pipeline::DidLoadingProgress() const {
+bool Pipeline::DidLoadingProgress() {
base::AutoLock auto_lock(lock_);
bool ret = did_loading_progress_;
did_loading_progress_ = false;
@@ -214,14 +190,14 @@ void Pipeline::SetErrorForTesting(PipelineStatus status) {
}
void Pipeline::SetState(State next_state) {
- if (state_ != kStarted && next_state == kStarted &&
+ if (state_ != kPlaying && next_state == kPlaying &&
!creation_time_.is_null()) {
UMA_HISTOGRAM_TIMES("Media.TimeToPipelineStarted",
default_tick_clock_.NowTicks() - creation_time_);
creation_time_ = base::TimeTicks();
}
- DVLOG(2) << GetStateString(state_) << " -> " << GetStateString(next_state);
+ DVLOG(1) << GetStateString(state_) << " -> " << GetStateString(next_state);
state_ = next_state;
media_log_->AddEvent(media_log_->CreatePipelineStateChangedEvent(next_state));
@@ -237,8 +213,7 @@ const char* Pipeline::GetStateString(State state) {
RETURN_STRING(kInitVideoRenderer);
RETURN_STRING(kInitPrerolling);
RETURN_STRING(kSeeking);
- RETURN_STRING(kStarting);
- RETURN_STRING(kStarted);
+ RETURN_STRING(kPlaying);
RETURN_STRING(kStopping);
RETURN_STRING(kStopped);
}
@@ -249,7 +224,7 @@ const char* Pipeline::GetStateString(State state) {
#undef RETURN_STRING
Pipeline::State Pipeline::GetNextState() const {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK(stop_cb_.is_null())
<< "State transitions don't happen when stopping";
DCHECK_EQ(status_, PIPELINE_OK)
@@ -275,15 +250,12 @@ Pipeline::State Pipeline::GetNextState() const {
return kInitPrerolling;
case kInitPrerolling:
- return kStarting;
+ return kPlaying;
case kSeeking:
- return kStarting;
-
- case kStarting:
- return kStarted;
+ return kPlaying;
- case kStarted:
+ case kPlaying:
case kStopping:
case kStopped:
break;
@@ -298,13 +270,13 @@ void Pipeline::OnDemuxerError(PipelineStatus error) {
void Pipeline::AddTextStream(DemuxerStream* text_stream,
const TextTrackConfig& config) {
- message_loop_->PostTask(FROM_HERE, base::Bind(
+ task_runner_->PostTask(FROM_HERE, base::Bind(
&Pipeline::AddTextStreamTask, base::Unretained(this),
text_stream, config));
}
void Pipeline::RemoveTextStream(DemuxerStream* text_stream) {
- message_loop_->PostTask(FROM_HERE, base::Bind(
+ task_runner_->PostTask(FROM_HERE, base::Bind(
&Pipeline::RemoveTextStreamTask, base::Unretained(this),
text_stream));
}
@@ -314,32 +286,22 @@ void Pipeline::SetError(PipelineStatus error) {
DCHECK_NE(PIPELINE_OK, error);
VLOG(1) << "Media pipeline error: " << error;
- message_loop_->PostTask(FROM_HERE, base::Bind(
+ task_runner_->PostTask(FROM_HERE, base::Bind(
&Pipeline::ErrorChangedTask, base::Unretained(this), error));
media_log_->AddEvent(media_log_->CreatePipelineErrorEvent(error));
}
-void Pipeline::OnAudioDisabled() {
- DCHECK(IsRunning());
- message_loop_->PostTask(FROM_HERE, base::Bind(
- &Pipeline::AudioDisabledTask, base::Unretained(this)));
- media_log_->AddEvent(
- media_log_->CreateEvent(MediaLogEvent::AUDIO_RENDERER_DISABLED));
-}
-
void Pipeline::OnAudioTimeUpdate(TimeDelta time, TimeDelta max_time) {
+ DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK_LE(time.InMicroseconds(), max_time.InMicroseconds());
- DCHECK(IsRunning());
base::AutoLock auto_lock(lock_);
- if (!has_audio_)
- return;
- if (waiting_for_clock_update_ && time < clock_->Elapsed())
+ if (clock_state_ == CLOCK_WAITING_FOR_AUDIO_TIME_UPDATE &&
+ time < clock_->Elapsed()) {
return;
+ }
- // TODO(scherkus): |state_| should only be accessed on pipeline thread, see
- // http://crbug.com/137973
if (state_ == kSeeking)
return;
@@ -348,18 +310,16 @@ void Pipeline::OnAudioTimeUpdate(TimeDelta time, TimeDelta max_time) {
}
void Pipeline::OnVideoTimeUpdate(TimeDelta max_time) {
- DCHECK(IsRunning());
- base::AutoLock auto_lock(lock_);
+ DCHECK(task_runner_->BelongsToCurrentThread());
- if (has_audio_)
+ if (audio_renderer_)
return;
- // TODO(scherkus): |state_| should only be accessed on pipeline thread, see
- // http://crbug.com/137973
if (state_ == kSeeking)
return;
- DCHECK(!waiting_for_clock_update_);
+ base::AutoLock auto_lock(lock_);
+ DCHECK_NE(clock_state_, CLOCK_WAITING_FOR_AUDIO_TIME_UPDATE);
clock_->SetMaxTime(max_time);
}
@@ -376,47 +336,14 @@ void Pipeline::SetDuration(TimeDelta duration) {
duration_change_cb_.Run();
}
-void Pipeline::SetTotalBytes(int64 total_bytes) {
- DCHECK(IsRunning());
- media_log_->AddEvent(
- media_log_->CreateStringEvent(
- MediaLogEvent::TOTAL_BYTES_SET, "total_bytes",
- base::Int64ToString(total_bytes)));
- int64 total_mbytes = total_bytes >> 20;
- if (total_mbytes > kint32max)
- total_mbytes = kint32max;
- UMA_HISTOGRAM_CUSTOM_COUNTS(
- "Media.TotalMBytes", static_cast<int32>(total_mbytes), 1, kint32max, 50);
-
- base::AutoLock auto_lock(lock_);
- total_bytes_ = total_bytes;
-}
-
-TimeDelta Pipeline::TimeForByteOffset_Locked(int64 byte_offset) const {
- lock_.AssertAcquired();
- // Use floating point to avoid potential overflow when using 64 bit integers.
- double time_offset_in_ms = clock_->Duration().InMilliseconds() *
- (static_cast<double>(byte_offset) / total_bytes_);
- TimeDelta time_offset(TimeDelta::FromMilliseconds(
- static_cast<int64>(time_offset_in_ms)));
- // Since the byte->time calculation is approximate, fudge the beginning &
- // ending areas to look better.
- TimeDelta epsilon = clock_->Duration() / 100;
- if (time_offset < epsilon)
- return TimeDelta();
- if (time_offset + epsilon > clock_->Duration())
- return clock_->Duration();
- return time_offset;
-}
-
void Pipeline::OnStateTransition(PipelineStatus status) {
// Force post to process state transitions after current execution frame.
- message_loop_->PostTask(FROM_HERE, base::Bind(
+ task_runner_->PostTask(FROM_HERE, base::Bind(
&Pipeline::StateTransitionTask, base::Unretained(this), status));
}
void Pipeline::StateTransitionTask(PipelineStatus status) {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
// No-op any state transitions if we're stopping.
if (state_ == kStopping || state_ == kStopped)
@@ -433,11 +360,9 @@ void Pipeline::StateTransitionTask(PipelineStatus status) {
// Guard against accidentally clearing |pending_callbacks_| for states that
// use it as well as states that should not be using it.
- //
- // TODO(scherkus): Make every state transition use |pending_callbacks_|.
DCHECK_EQ(pending_callbacks_.get() != NULL,
- (state_ == kInitPrerolling || state_ == kStarting ||
- state_ == kSeeking));
+ (state_ == kInitPrerolling || state_ == kSeeking));
+
pending_callbacks_.reset();
PipelineStatusCB done_cb = base::Bind(
@@ -462,46 +387,43 @@ void Pipeline::StateTransitionTask(PipelineStatus status) {
// We do not want to start the clock running. We only want to set the
// base media time so our timestamp calculations will be correct.
clock_->SetTime(demuxer_->GetStartTime(), demuxer_->GetStartTime());
-
- // TODO(scherkus): |has_audio_| should be true no matter what --
- // otherwise people with muted/disabled sound cards will make our
- // default controls look as if every video doesn't contain an audio
- // track.
- has_audio_ = audio_renderer_ != NULL && !audio_disabled_;
- has_video_ = video_renderer_ != NULL;
}
if (!audio_renderer_ && !video_renderer_) {
done_cb.Run(PIPELINE_ERROR_COULD_NOT_RENDER);
return;
}
- buffering_state_cb_.Run(kHaveMetadata);
-
- return DoInitialPreroll(done_cb);
-
- case kStarting:
- return DoPlay(done_cb);
-
- case kStarted:
{
- base::AutoLock l(lock_);
- // We use audio stream to update the clock. So if there is such a
- // stream, we pause the clock until we receive a valid timestamp.
- waiting_for_clock_update_ = true;
- if (!has_audio_) {
- clock_->SetMaxTime(clock_->Duration());
- StartClockIfWaitingForTimeUpdate_Locked();
- }
+ PipelineMetadata metadata;
+ metadata.has_audio = audio_renderer_;
+ metadata.has_video = video_renderer_;
+ metadata.timeline_offset = demuxer_->GetTimelineOffset();
+ DemuxerStream* stream = demuxer_->GetStream(DemuxerStream::VIDEO);
+ if (stream)
+ metadata.natural_size = stream->video_decoder_config().natural_size();
+ metadata_cb_.Run(metadata);
}
- DCHECK(!seek_cb_.is_null());
- DCHECK_EQ(status_, PIPELINE_OK);
+ return DoInitialPreroll(done_cb);
- // Fire canplaythrough immediately after playback begins because of
- // crbug.com/106480.
- // TODO(vrk): set ready state to HaveFutureData when bug above is fixed.
- buffering_state_cb_.Run(kPrerollCompleted);
- return base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
+ case kPlaying:
+ PlaybackRateChangedTask(GetPlaybackRate());
+ VolumeChangedTask(GetVolume());
+
+ // We enter this state from either kInitPrerolling or kSeeking. As of now
+ // both those states call Preroll(), which means by time we enter this
+ // state we've already buffered enough data. Forcefully update the
+ // buffering state, which start the clock and renderers and transition
+ // into kPlaying state.
+ //
+ // TODO(scherkus): Remove after renderers are taught to fire buffering
+ // state callbacks http://crbug.com/144683
+ DCHECK(WaitingForEnoughData());
+ if (audio_renderer_)
+ BufferingStateChanged(&audio_buffering_state_, BUFFERING_HAVE_ENOUGH);
+ if (video_renderer_)
+ BufferingStateChanged(&video_buffering_state_, BUFFERING_HAVE_ENOUGH);
+ return;
case kStopping:
case kStopped:
@@ -519,7 +441,7 @@ void Pipeline::StateTransitionTask(PipelineStatus status) {
// That being said, deleting the renderers while keeping |pending_callbacks_|
// running on the media thread would result in crashes.
void Pipeline::DoInitialPreroll(const PipelineStatusCB& done_cb) {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK(!pending_callbacks_.get());
SerialRunner::Queue bound_fns;
@@ -536,6 +458,16 @@ void Pipeline::DoInitialPreroll(const PipelineStatusCB& done_cb) {
bound_fns.Push(base::Bind(
&VideoRenderer::Preroll, base::Unretained(video_renderer_.get()),
seek_timestamp));
+
+ // TODO(scherkus): Remove after VideoRenderer is taught to fire buffering
+ // state callbacks http://crbug.com/144683
+ bound_fns.Push(base::Bind(&VideoRenderer::Play,
+ base::Unretained(video_renderer_.get())));
+ }
+
+ if (text_renderer_) {
+ bound_fns.Push(base::Bind(
+ &TextRenderer::Play, base::Unretained(text_renderer_.get())));
}
pending_callbacks_ = SerialRunner::Run(bound_fns, done_cb);
@@ -544,19 +476,11 @@ void Pipeline::DoInitialPreroll(const PipelineStatusCB& done_cb) {
void Pipeline::DoSeek(
base::TimeDelta seek_timestamp,
const PipelineStatusCB& done_cb) {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK(!pending_callbacks_.get());
SerialRunner::Queue bound_fns;
// Pause.
- if (audio_renderer_) {
- bound_fns.Push(base::Bind(
- &AudioRenderer::Pause, base::Unretained(audio_renderer_.get())));
- }
- if (video_renderer_) {
- bound_fns.Push(base::Bind(
- &VideoRenderer::Pause, base::Unretained(video_renderer_.get())));
- }
if (text_renderer_) {
bound_fns.Push(base::Bind(
&TextRenderer::Pause, base::Unretained(text_renderer_.get())));
@@ -566,10 +490,24 @@ void Pipeline::DoSeek(
if (audio_renderer_) {
bound_fns.Push(base::Bind(
&AudioRenderer::Flush, base::Unretained(audio_renderer_.get())));
+
+ // TODO(scherkus): Remove after AudioRenderer is taught to fire buffering
+ // state callbacks http://crbug.com/144683
+ bound_fns.Push(base::Bind(&Pipeline::BufferingStateChanged,
+ base::Unretained(this),
+ &audio_buffering_state_,
+ BUFFERING_HAVE_NOTHING));
}
if (video_renderer_) {
bound_fns.Push(base::Bind(
&VideoRenderer::Flush, base::Unretained(video_renderer_.get())));
+
+ // TODO(scherkus): Remove after VideoRenderer is taught to fire buffering
+ // state callbacks http://crbug.com/144683
+ bound_fns.Push(base::Bind(&Pipeline::BufferingStateChanged,
+ base::Unretained(this),
+ &video_buffering_state_,
+ BUFFERING_HAVE_NOTHING));
}
if (text_renderer_) {
bound_fns.Push(base::Bind(
@@ -591,27 +529,11 @@ void Pipeline::DoSeek(
bound_fns.Push(base::Bind(
&VideoRenderer::Preroll, base::Unretained(video_renderer_.get()),
seek_timestamp));
- }
-
- pending_callbacks_ = SerialRunner::Run(bound_fns, done_cb);
-}
-
-void Pipeline::DoPlay(const PipelineStatusCB& done_cb) {
- DCHECK(message_loop_->BelongsToCurrentThread());
- DCHECK(!pending_callbacks_.get());
- SerialRunner::Queue bound_fns;
- PlaybackRateChangedTask(GetPlaybackRate());
- VolumeChangedTask(GetVolume());
-
- if (audio_renderer_) {
- bound_fns.Push(base::Bind(
- &AudioRenderer::Play, base::Unretained(audio_renderer_.get())));
- }
-
- if (video_renderer_) {
- bound_fns.Push(base::Bind(
- &VideoRenderer::Play, base::Unretained(video_renderer_.get())));
+ // TODO(scherkus): Remove after renderers are taught to fire buffering
+ // state callbacks http://crbug.com/144683
+ bound_fns.Push(base::Bind(&VideoRenderer::Play,
+ base::Unretained(video_renderer_.get())));
}
if (text_renderer_) {
@@ -623,7 +545,7 @@ void Pipeline::DoPlay(const PipelineStatusCB& done_cb) {
}
void Pipeline::DoStop(const PipelineStatusCB& done_cb) {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK(!pending_callbacks_.get());
SerialRunner::Queue bound_fns;
@@ -651,7 +573,7 @@ void Pipeline::DoStop(const PipelineStatusCB& done_cb) {
}
void Pipeline::OnStopCompleted(PipelineStatus status) {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK_EQ(state_, kStopping);
{
base::AutoLock l(lock_);
@@ -686,13 +608,6 @@ void Pipeline::OnStopCompleted(PipelineStatus status) {
}
}
-void Pipeline::AddBufferedByteRange(int64 start, int64 end) {
- DCHECK(IsRunning());
- base::AutoLock auto_lock(lock_);
- buffered_byte_ranges_.Add(start, end);
- did_loading_progress_ = true;
-}
-
void Pipeline::AddBufferedTimeRange(base::TimeDelta start,
base::TimeDelta end) {
DCHECK(IsRunning());
@@ -701,32 +616,23 @@ void Pipeline::AddBufferedTimeRange(base::TimeDelta start,
did_loading_progress_ = true;
}
-void Pipeline::OnNaturalVideoSizeChanged(const gfx::Size& size) {
- DCHECK(IsRunning());
- media_log_->AddEvent(media_log_->CreateVideoSizeSetEvent(
- size.width(), size.height()));
-
- base::AutoLock auto_lock(lock_);
- natural_size_ = size;
-}
-
void Pipeline::OnAudioRendererEnded() {
- // Force post to process ended messages after current execution frame.
- message_loop_->PostTask(FROM_HERE, base::Bind(
+ // Force post to process ended tasks after current execution frame.
+ task_runner_->PostTask(FROM_HERE, base::Bind(
&Pipeline::DoAudioRendererEnded, base::Unretained(this)));
media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::AUDIO_ENDED));
}
void Pipeline::OnVideoRendererEnded() {
- // Force post to process ended messages after current execution frame.
- message_loop_->PostTask(FROM_HERE, base::Bind(
+ // Force post to process ended tasks after current execution frame.
+ task_runner_->PostTask(FROM_HERE, base::Bind(
&Pipeline::DoVideoRendererEnded, base::Unretained(this)));
media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::VIDEO_ENDED));
}
void Pipeline::OnTextRendererEnded() {
// Force post to process ended messages after current execution frame.
- message_loop_->PostTask(FROM_HERE, base::Bind(
+ task_runner_->PostTask(FROM_HERE, base::Bind(
&Pipeline::DoTextRendererEnded, base::Unretained(this)));
media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::TEXT_ENDED));
}
@@ -740,23 +646,11 @@ void Pipeline::OnUpdateStatistics(const PipelineStatistics& stats) {
statistics_.video_frames_dropped += stats.video_frames_dropped;
}
-void Pipeline::StartTask(scoped_ptr<FilterCollection> filter_collection,
- const base::Closure& ended_cb,
- const PipelineStatusCB& error_cb,
- const PipelineStatusCB& seek_cb,
- const BufferingStateCB& buffering_state_cb,
- const base::Closure& duration_change_cb) {
- DCHECK(message_loop_->BelongsToCurrentThread());
+void Pipeline::StartTask() {
+ DCHECK(task_runner_->BelongsToCurrentThread());
CHECK_EQ(kCreated, state_)
<< "Media pipeline cannot be started more than once";
- filter_collection_ = filter_collection.Pass();
- ended_cb_ = ended_cb;
- error_cb_ = error_cb;
- seek_cb_ = seek_cb;
- buffering_state_cb_ = buffering_state_cb;
- duration_change_cb_ = duration_change_cb;
-
text_renderer_ = filter_collection_->GetTextRenderer();
if (text_renderer_) {
@@ -768,7 +662,7 @@ void Pipeline::StartTask(scoped_ptr<FilterCollection> filter_collection,
}
void Pipeline::StopTask(const base::Closure& stop_cb) {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK(stop_cb_.is_null());
if (state_ == kStopped) {
@@ -788,7 +682,7 @@ void Pipeline::StopTask(const base::Closure& stop_cb) {
}
void Pipeline::ErrorChangedTask(PipelineStatus error) {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK_NE(PIPELINE_OK, error) << "PIPELINE_OK isn't an error!";
if (state_ == kStopping || state_ == kStopped)
@@ -802,10 +696,10 @@ void Pipeline::ErrorChangedTask(PipelineStatus error) {
}
void Pipeline::PlaybackRateChangedTask(float playback_rate) {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
// Playback rate changes are only carried out while playing.
- if (state_ != kStarting && state_ != kStarted)
+ if (state_ != kPlaying)
return;
{
@@ -820,10 +714,10 @@ void Pipeline::PlaybackRateChangedTask(float playback_rate) {
}
void Pipeline::VolumeChangedTask(float volume) {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
// Volume changes are only carried out while playing.
- if (state_ != kStarting && state_ != kStarted)
+ if (state_ != kPlaying)
return;
if (audio_renderer_)
@@ -831,11 +725,11 @@ void Pipeline::VolumeChangedTask(float volume) {
}
void Pipeline::SeekTask(TimeDelta time, const PipelineStatusCB& seek_cb) {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK(stop_cb_.is_null());
// Suppress seeking if we're not fully started.
- if (state_ != kStarted) {
+ if (state_ != kPlaying) {
DCHECK(state_ == kStopping || state_ == kStopped)
<< "Receive extra seek in unexpected state: " << state_;
@@ -858,8 +752,7 @@ void Pipeline::SeekTask(TimeDelta time, const PipelineStatusCB& seek_cb) {
// Kick off seeking!
{
base::AutoLock auto_lock(lock_);
- if (clock_->IsPlaying())
- clock_->Pause();
+ PauseClockAndStopRendering_Locked();
clock_->SetTime(seek_timestamp, seek_timestamp);
}
DoSeek(seek_timestamp, base::Bind(
@@ -867,16 +760,16 @@ void Pipeline::SeekTask(TimeDelta time, const PipelineStatusCB& seek_cb) {
}
void Pipeline::DoAudioRendererEnded() {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
- if (state_ != kStarted)
+ if (state_ != kPlaying)
return;
DCHECK(!audio_ended_);
audio_ended_ = true;
// Start clock since there is no more audio to trigger clock updates.
- if (!audio_disabled_) {
+ {
base::AutoLock auto_lock(lock_);
clock_->SetMaxTime(clock_->Duration());
StartClockIfWaitingForTimeUpdate_Locked();
@@ -886,9 +779,9 @@ void Pipeline::DoAudioRendererEnded() {
}
void Pipeline::DoVideoRendererEnded() {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
- if (state_ != kStarted)
+ if (state_ != kPlaying)
return;
DCHECK(!video_ended_);
@@ -898,9 +791,9 @@ void Pipeline::DoVideoRendererEnded() {
}
void Pipeline::DoTextRendererEnded() {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
- if (state_ != kStarted)
+ if (state_ != kPlaying)
return;
DCHECK(!text_ended_);
@@ -910,9 +803,9 @@ void Pipeline::DoTextRendererEnded() {
}
void Pipeline::RunEndedCallbackIfNeeded() {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
- if (audio_renderer_ && !audio_ended_ && !audio_disabled_)
+ if (audio_renderer_ && !audio_ended_)
return;
if (video_renderer_ && !video_ended_)
@@ -923,50 +816,36 @@ void Pipeline::RunEndedCallbackIfNeeded() {
{
base::AutoLock auto_lock(lock_);
- clock_->EndOfStream();
+ PauseClockAndStopRendering_Locked();
+ clock_->SetTime(clock_->Duration(), clock_->Duration());
}
DCHECK_EQ(status_, PIPELINE_OK);
ended_cb_.Run();
}
-void Pipeline::AudioDisabledTask() {
- DCHECK(message_loop_->BelongsToCurrentThread());
-
- base::AutoLock auto_lock(lock_);
- has_audio_ = false;
- audio_disabled_ = true;
-
- // Notify our demuxer that we're no longer rendering audio.
- demuxer_->OnAudioRendererDisabled();
-
- // Start clock since there is no more audio to trigger clock updates.
- clock_->SetMaxTime(clock_->Duration());
- StartClockIfWaitingForTimeUpdate_Locked();
-}
-
void Pipeline::AddTextStreamTask(DemuxerStream* text_stream,
const TextTrackConfig& config) {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
// TODO(matthewjheaney): fix up text_ended_ when text stream
// is added (http://crbug.com/321446).
text_renderer_->AddTextStream(text_stream, config);
}
void Pipeline::RemoveTextStreamTask(DemuxerStream* text_stream) {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
text_renderer_->RemoveTextStream(text_stream);
}
void Pipeline::InitializeDemuxer(const PipelineStatusCB& done_cb) {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
demuxer_ = filter_collection_->GetDemuxer();
demuxer_->Initialize(this, done_cb, text_renderer_);
}
void Pipeline::InitializeAudioRenderer(const PipelineStatusCB& done_cb) {
- DCHECK(message_loop_->BelongsToCurrentThread());
+ DCHECK(task_runner_->BelongsToCurrentThread());
audio_renderer_ = filter_collection_->GetAudioRenderer();
audio_renderer_->Initialize(
@@ -976,29 +855,19 @@ void Pipeline::InitializeAudioRenderer(const PipelineStatusCB& done_cb) {
base::Bind(&Pipeline::OnAudioUnderflow, base::Unretained(this)),
base::Bind(&Pipeline::OnAudioTimeUpdate, base::Unretained(this)),
base::Bind(&Pipeline::OnAudioRendererEnded, base::Unretained(this)),
- base::Bind(&Pipeline::OnAudioDisabled, base::Unretained(this)),
base::Bind(&Pipeline::SetError, base::Unretained(this)));
}
void Pipeline::InitializeVideoRenderer(const PipelineStatusCB& done_cb) {
- DCHECK(message_loop_->BelongsToCurrentThread());
-
- DemuxerStream* stream = demuxer_->GetStream(DemuxerStream::VIDEO);
-
- {
- // Get an initial natural size so we have something when we signal
- // the kHaveMetadata buffering state.
- base::AutoLock l(lock_);
- natural_size_ = stream->video_decoder_config().natural_size();
- }
+ DCHECK(task_runner_->BelongsToCurrentThread());
video_renderer_ = filter_collection_->GetVideoRenderer();
video_renderer_->Initialize(
- stream,
+ demuxer_->GetStream(DemuxerStream::VIDEO),
+ demuxer_->GetLiveness() == Demuxer::LIVENESS_LIVE,
done_cb,
base::Bind(&Pipeline::OnUpdateStatistics, base::Unretained(this)),
base::Bind(&Pipeline::OnVideoTimeUpdate, base::Unretained(this)),
- base::Bind(&Pipeline::OnNaturalVideoSizeChanged, base::Unretained(this)),
base::Bind(&Pipeline::OnVideoRendererEnded, base::Unretained(this)),
base::Bind(&Pipeline::SetError, base::Unretained(this)),
base::Bind(&Pipeline::GetMediaTime, base::Unretained(this)),
@@ -1006,25 +875,111 @@ void Pipeline::InitializeVideoRenderer(const PipelineStatusCB& done_cb) {
}
void Pipeline::OnAudioUnderflow() {
- if (!message_loop_->BelongsToCurrentThread()) {
- message_loop_->PostTask(FROM_HERE, base::Bind(
+ if (!task_runner_->BelongsToCurrentThread()) {
+ task_runner_->PostTask(FROM_HERE, base::Bind(
&Pipeline::OnAudioUnderflow, base::Unretained(this)));
return;
}
- if (state_ != kStarted)
+ if (state_ != kPlaying)
return;
if (audio_renderer_)
audio_renderer_->ResumeAfterUnderflow();
}
+void Pipeline::BufferingStateChanged(BufferingState* buffering_state,
+ BufferingState new_buffering_state) {
+ DVLOG(1) << __FUNCTION__ << "(" << *buffering_state << ", "
+ << " " << new_buffering_state << ") "
+ << (buffering_state == &audio_buffering_state_ ? "audio" : "video");
+ DCHECK(task_runner_->BelongsToCurrentThread());
+ bool was_waiting_for_enough_data = WaitingForEnoughData();
+ *buffering_state = new_buffering_state;
+
+ // Renderer underflowed.
+ if (!was_waiting_for_enough_data && WaitingForEnoughData()) {
+ StartWaitingForEnoughData();
+ return;
+ }
+
+ // Renderer prerolled.
+ if (was_waiting_for_enough_data && !WaitingForEnoughData()) {
+ StartPlayback();
+ return;
+ }
+}
+
+bool Pipeline::WaitingForEnoughData() const {
+ DCHECK(task_runner_->BelongsToCurrentThread());
+ if (state_ != kPlaying)
+ return false;
+ if (audio_renderer_ && audio_buffering_state_ != BUFFERING_HAVE_ENOUGH)
+ return true;
+ if (video_renderer_ && video_buffering_state_ != BUFFERING_HAVE_ENOUGH)
+ return true;
+ return false;
+}
+
+void Pipeline::StartWaitingForEnoughData() {
+ DVLOG(1) << __FUNCTION__;
+ DCHECK_EQ(state_, kPlaying);
+ DCHECK(WaitingForEnoughData());
+
+ base::AutoLock auto_lock(lock_);
+ PauseClockAndStopRendering_Locked();
+}
+
+void Pipeline::StartPlayback() {
+ DVLOG(1) << __FUNCTION__;
+ DCHECK_EQ(state_, kPlaying);
+ DCHECK_EQ(clock_state_, CLOCK_PAUSED);
+ DCHECK(!WaitingForEnoughData());
+
+ if (audio_renderer_) {
+ // We use audio stream to update the clock. So if there is such a
+ // stream, we pause the clock until we receive a valid timestamp.
+ base::AutoLock auto_lock(lock_);
+ clock_state_ = CLOCK_WAITING_FOR_AUDIO_TIME_UPDATE;
+ audio_renderer_->StartRendering();
+ } else {
+ base::AutoLock auto_lock(lock_);
+ clock_state_ = CLOCK_PLAYING;
+ clock_->SetMaxTime(clock_->Duration());
+ clock_->Play();
+ }
+
+ preroll_completed_cb_.Run();
+ if (!seek_cb_.is_null())
+ base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
+}
+
+void Pipeline::PauseClockAndStopRendering_Locked() {
+ lock_.AssertAcquired();
+ switch (clock_state_) {
+ case CLOCK_PAUSED:
+ return;
+
+ case CLOCK_WAITING_FOR_AUDIO_TIME_UPDATE:
+ audio_renderer_->StopRendering();
+ break;
+
+ case CLOCK_PLAYING:
+ if (audio_renderer_)
+ audio_renderer_->StopRendering();
+ clock_->Pause();
+ break;
+ }
+
+ clock_state_ = CLOCK_PAUSED;
+}
+
void Pipeline::StartClockIfWaitingForTimeUpdate_Locked() {
lock_.AssertAcquired();
- if (!waiting_for_clock_update_)
+ if (clock_state_ != CLOCK_WAITING_FOR_AUDIO_TIME_UPDATE)
return;
- waiting_for_clock_update_ = false;
+ clock_state_ = CLOCK_PLAYING;
clock_->Play();
}