Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / media / base / pipeline.cc
blob385db3a7e1ce5f96c0af89caf48717e8b699b488
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/base/pipeline.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/callback_helpers.h"
12 #include "base/compiler_specific.h"
13 #include "base/location.h"
14 #include "base/metrics/histogram.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/stl_util.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_util.h"
19 #include "base/synchronization/condition_variable.h"
20 #include "media/base/audio_decoder.h"
21 #include "media/base/audio_renderer.h"
22 #include "media/base/clock.h"
23 #include "media/base/filter_collection.h"
24 #include "media/base/media_log.h"
25 #include "media/base/text_renderer.h"
26 #include "media/base/text_track_config.h"
27 #include "media/base/video_decoder.h"
28 #include "media/base/video_decoder_config.h"
29 #include "media/base/video_renderer.h"
31 using base::TimeDelta;
33 namespace media {
35 Pipeline::Pipeline(
36 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
37 MediaLog* media_log)
38 : task_runner_(task_runner),
39 media_log_(media_log),
40 running_(false),
41 did_loading_progress_(false),
42 volume_(1.0f),
43 playback_rate_(0.0f),
44 clock_(new Clock(&default_tick_clock_)),
45 waiting_for_clock_update_(false),
46 status_(PIPELINE_OK),
47 state_(kCreated),
48 audio_ended_(false),
49 video_ended_(false),
50 text_ended_(false),
51 demuxer_(NULL),
52 creation_time_(default_tick_clock_.NowTicks()) {
53 media_log_->AddEvent(media_log_->CreatePipelineStateChangedEvent(kCreated));
54 media_log_->AddEvent(
55 media_log_->CreateEvent(MediaLogEvent::PIPELINE_CREATED));
58 Pipeline::~Pipeline() {
59 DCHECK(thread_checker_.CalledOnValidThread())
60 << "Pipeline must be destroyed on same thread that created it";
61 DCHECK(!running_) << "Stop() must complete before destroying object";
62 DCHECK(stop_cb_.is_null());
63 DCHECK(seek_cb_.is_null());
65 media_log_->AddEvent(
66 media_log_->CreateEvent(MediaLogEvent::PIPELINE_DESTROYED));
69 void Pipeline::Start(scoped_ptr<FilterCollection> collection,
70 const base::Closure& ended_cb,
71 const PipelineStatusCB& error_cb,
72 const PipelineStatusCB& seek_cb,
73 const PipelineMetadataCB& metadata_cb,
74 const base::Closure& preroll_completed_cb,
75 const base::Closure& duration_change_cb) {
76 DCHECK(!ended_cb.is_null());
77 DCHECK(!error_cb.is_null());
78 DCHECK(!seek_cb.is_null());
79 DCHECK(!metadata_cb.is_null());
80 DCHECK(!preroll_completed_cb.is_null());
82 base::AutoLock auto_lock(lock_);
83 CHECK(!running_) << "Media pipeline is already running";
84 running_ = true;
86 filter_collection_ = collection.Pass();
87 ended_cb_ = ended_cb;
88 error_cb_ = error_cb;
89 seek_cb_ = seek_cb;
90 metadata_cb_ = metadata_cb;
91 preroll_completed_cb_ = preroll_completed_cb;
92 duration_change_cb_ = duration_change_cb;
94 task_runner_->PostTask(
95 FROM_HERE, base::Bind(&Pipeline::StartTask, base::Unretained(this)));
98 void Pipeline::Stop(const base::Closure& stop_cb) {
99 base::AutoLock auto_lock(lock_);
100 task_runner_->PostTask(FROM_HERE, base::Bind(
101 &Pipeline::StopTask, base::Unretained(this), stop_cb));
104 void Pipeline::Seek(TimeDelta time, const PipelineStatusCB& seek_cb) {
105 base::AutoLock auto_lock(lock_);
106 if (!running_) {
107 NOTREACHED() << "Media pipeline isn't running";
108 return;
111 task_runner_->PostTask(FROM_HERE, base::Bind(
112 &Pipeline::SeekTask, base::Unretained(this), time, seek_cb));
115 bool Pipeline::IsRunning() const {
116 base::AutoLock auto_lock(lock_);
117 return running_;
120 float Pipeline::GetPlaybackRate() const {
121 base::AutoLock auto_lock(lock_);
122 return playback_rate_;
125 void Pipeline::SetPlaybackRate(float playback_rate) {
126 if (playback_rate < 0.0f)
127 return;
129 base::AutoLock auto_lock(lock_);
130 playback_rate_ = playback_rate;
131 if (running_) {
132 task_runner_->PostTask(FROM_HERE, base::Bind(
133 &Pipeline::PlaybackRateChangedTask, base::Unretained(this),
134 playback_rate));
138 float Pipeline::GetVolume() const {
139 base::AutoLock auto_lock(lock_);
140 return volume_;
143 void Pipeline::SetVolume(float volume) {
144 if (volume < 0.0f || volume > 1.0f)
145 return;
147 base::AutoLock auto_lock(lock_);
148 volume_ = volume;
149 if (running_) {
150 task_runner_->PostTask(FROM_HERE, base::Bind(
151 &Pipeline::VolumeChangedTask, base::Unretained(this), volume));
155 TimeDelta Pipeline::GetMediaTime() const {
156 base::AutoLock auto_lock(lock_);
157 return clock_->Elapsed();
160 Ranges<TimeDelta> Pipeline::GetBufferedTimeRanges() {
161 base::AutoLock auto_lock(lock_);
162 return buffered_time_ranges_;
165 TimeDelta Pipeline::GetMediaDuration() const {
166 base::AutoLock auto_lock(lock_);
167 return clock_->Duration();
170 bool Pipeline::DidLoadingProgress() const {
171 base::AutoLock auto_lock(lock_);
172 bool ret = did_loading_progress_;
173 did_loading_progress_ = false;
174 return ret;
177 PipelineStatistics Pipeline::GetStatistics() const {
178 base::AutoLock auto_lock(lock_);
179 return statistics_;
182 void Pipeline::SetClockForTesting(Clock* clock) {
183 clock_.reset(clock);
186 void Pipeline::SetErrorForTesting(PipelineStatus status) {
187 SetError(status);
190 void Pipeline::SetState(State next_state) {
191 if (state_ != kStarted && next_state == kStarted &&
192 !creation_time_.is_null()) {
193 UMA_HISTOGRAM_TIMES("Media.TimeToPipelineStarted",
194 default_tick_clock_.NowTicks() - creation_time_);
195 creation_time_ = base::TimeTicks();
198 DVLOG(2) << GetStateString(state_) << " -> " << GetStateString(next_state);
200 state_ = next_state;
201 media_log_->AddEvent(media_log_->CreatePipelineStateChangedEvent(next_state));
204 #define RETURN_STRING(state) case state: return #state;
206 const char* Pipeline::GetStateString(State state) {
207 switch (state) {
208 RETURN_STRING(kCreated);
209 RETURN_STRING(kInitDemuxer);
210 RETURN_STRING(kInitAudioRenderer);
211 RETURN_STRING(kInitVideoRenderer);
212 RETURN_STRING(kInitPrerolling);
213 RETURN_STRING(kSeeking);
214 RETURN_STRING(kStarting);
215 RETURN_STRING(kStarted);
216 RETURN_STRING(kStopping);
217 RETURN_STRING(kStopped);
219 NOTREACHED();
220 return "INVALID";
223 #undef RETURN_STRING
225 Pipeline::State Pipeline::GetNextState() const {
226 DCHECK(task_runner_->BelongsToCurrentThread());
227 DCHECK(stop_cb_.is_null())
228 << "State transitions don't happen when stopping";
229 DCHECK_EQ(status_, PIPELINE_OK)
230 << "State transitions don't happen when there's an error: " << status_;
232 switch (state_) {
233 case kCreated:
234 return kInitDemuxer;
236 case kInitDemuxer:
237 if (demuxer_->GetStream(DemuxerStream::AUDIO))
238 return kInitAudioRenderer;
239 if (demuxer_->GetStream(DemuxerStream::VIDEO))
240 return kInitVideoRenderer;
241 return kInitPrerolling;
243 case kInitAudioRenderer:
244 if (demuxer_->GetStream(DemuxerStream::VIDEO))
245 return kInitVideoRenderer;
246 return kInitPrerolling;
248 case kInitVideoRenderer:
249 return kInitPrerolling;
251 case kInitPrerolling:
252 return kStarting;
254 case kSeeking:
255 return kStarting;
257 case kStarting:
258 return kStarted;
260 case kStarted:
261 case kStopping:
262 case kStopped:
263 break;
265 NOTREACHED() << "State has no transition: " << state_;
266 return state_;
269 void Pipeline::OnDemuxerError(PipelineStatus error) {
270 SetError(error);
273 void Pipeline::AddTextStream(DemuxerStream* text_stream,
274 const TextTrackConfig& config) {
275 task_runner_->PostTask(FROM_HERE, base::Bind(
276 &Pipeline::AddTextStreamTask, base::Unretained(this),
277 text_stream, config));
280 void Pipeline::RemoveTextStream(DemuxerStream* text_stream) {
281 task_runner_->PostTask(FROM_HERE, base::Bind(
282 &Pipeline::RemoveTextStreamTask, base::Unretained(this),
283 text_stream));
286 void Pipeline::SetError(PipelineStatus error) {
287 DCHECK(IsRunning());
288 DCHECK_NE(PIPELINE_OK, error);
289 VLOG(1) << "Media pipeline error: " << error;
291 task_runner_->PostTask(FROM_HERE, base::Bind(
292 &Pipeline::ErrorChangedTask, base::Unretained(this), error));
294 media_log_->AddEvent(media_log_->CreatePipelineErrorEvent(error));
297 void Pipeline::OnAudioTimeUpdate(TimeDelta time, TimeDelta max_time) {
298 DCHECK_LE(time.InMicroseconds(), max_time.InMicroseconds());
299 DCHECK(IsRunning());
300 base::AutoLock auto_lock(lock_);
302 if (waiting_for_clock_update_ && time < clock_->Elapsed())
303 return;
305 // TODO(scherkus): |state_| should only be accessed on pipeline thread, see
306 // http://crbug.com/137973
307 if (state_ == kSeeking)
308 return;
310 clock_->SetTime(time, max_time);
311 StartClockIfWaitingForTimeUpdate_Locked();
314 void Pipeline::OnVideoTimeUpdate(TimeDelta max_time) {
315 DCHECK(IsRunning());
316 base::AutoLock auto_lock(lock_);
318 if (audio_renderer_)
319 return;
321 // TODO(scherkus): |state_| should only be accessed on pipeline thread, see
322 // http://crbug.com/137973
323 if (state_ == kSeeking)
324 return;
326 DCHECK(!waiting_for_clock_update_);
327 clock_->SetMaxTime(max_time);
330 void Pipeline::SetDuration(TimeDelta duration) {
331 DCHECK(IsRunning());
332 media_log_->AddEvent(
333 media_log_->CreateTimeEvent(
334 MediaLogEvent::DURATION_SET, "duration", duration));
335 UMA_HISTOGRAM_LONG_TIMES("Media.Duration", duration);
337 base::AutoLock auto_lock(lock_);
338 clock_->SetDuration(duration);
339 if (!duration_change_cb_.is_null())
340 duration_change_cb_.Run();
343 void Pipeline::OnStateTransition(PipelineStatus status) {
344 // Force post to process state transitions after current execution frame.
345 task_runner_->PostTask(FROM_HERE, base::Bind(
346 &Pipeline::StateTransitionTask, base::Unretained(this), status));
349 void Pipeline::StateTransitionTask(PipelineStatus status) {
350 DCHECK(task_runner_->BelongsToCurrentThread());
352 // No-op any state transitions if we're stopping.
353 if (state_ == kStopping || state_ == kStopped)
354 return;
356 // Preserve existing abnormal status, otherwise update based on the result of
357 // the previous operation.
358 status_ = (status_ != PIPELINE_OK ? status_ : status);
360 if (status_ != PIPELINE_OK) {
361 ErrorChangedTask(status_);
362 return;
365 // Guard against accidentally clearing |pending_callbacks_| for states that
366 // use it as well as states that should not be using it.
368 // TODO(scherkus): Make every state transition use |pending_callbacks_|.
369 DCHECK_EQ(pending_callbacks_.get() != NULL,
370 (state_ == kInitPrerolling || state_ == kStarting ||
371 state_ == kSeeking));
372 pending_callbacks_.reset();
374 PipelineStatusCB done_cb = base::Bind(
375 &Pipeline::OnStateTransition, base::Unretained(this));
377 // Switch states, performing any entrance actions for the new state as well.
378 SetState(GetNextState());
379 switch (state_) {
380 case kInitDemuxer:
381 return InitializeDemuxer(done_cb);
383 case kInitAudioRenderer:
384 return InitializeAudioRenderer(done_cb);
386 case kInitVideoRenderer:
387 return InitializeVideoRenderer(done_cb);
389 case kInitPrerolling:
390 filter_collection_.reset();
392 base::AutoLock l(lock_);
393 // We do not want to start the clock running. We only want to set the
394 // base media time so our timestamp calculations will be correct.
395 clock_->SetTime(demuxer_->GetStartTime(), demuxer_->GetStartTime());
397 if (!audio_renderer_ && !video_renderer_) {
398 done_cb.Run(PIPELINE_ERROR_COULD_NOT_RENDER);
399 return;
403 PipelineMetadata metadata;
404 metadata.has_audio = audio_renderer_;
405 metadata.has_video = video_renderer_;
406 metadata.timeline_offset = demuxer_->GetTimelineOffset();
407 DemuxerStream* stream = demuxer_->GetStream(DemuxerStream::VIDEO);
408 if (stream)
409 metadata.natural_size = stream->video_decoder_config().natural_size();
410 metadata_cb_.Run(metadata);
413 return DoInitialPreroll(done_cb);
415 case kStarting:
416 return DoPlay(done_cb);
418 case kStarted:
420 base::AutoLock l(lock_);
421 // We use audio stream to update the clock. So if there is such a
422 // stream, we pause the clock until we receive a valid timestamp.
423 waiting_for_clock_update_ = true;
424 if (!audio_renderer_) {
425 clock_->SetMaxTime(clock_->Duration());
426 StartClockIfWaitingForTimeUpdate_Locked();
430 DCHECK(!seek_cb_.is_null());
431 DCHECK_EQ(status_, PIPELINE_OK);
433 // Fire canplaythrough immediately after playback begins because of
434 // crbug.com/106480.
435 // TODO(vrk): set ready state to HaveFutureData when bug above is fixed.
436 preroll_completed_cb_.Run();
437 return base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
439 case kStopping:
440 case kStopped:
441 case kCreated:
442 case kSeeking:
443 NOTREACHED() << "State has no transition: " << state_;
444 return;
448 // Note that the usage of base::Unretained() with the audio/video renderers
449 // in the following DoXXX() functions is considered safe as they are owned by
450 // |pending_callbacks_| and share the same lifetime.
452 // That being said, deleting the renderers while keeping |pending_callbacks_|
453 // running on the media thread would result in crashes.
454 void Pipeline::DoInitialPreroll(const PipelineStatusCB& done_cb) {
455 DCHECK(task_runner_->BelongsToCurrentThread());
456 DCHECK(!pending_callbacks_.get());
457 SerialRunner::Queue bound_fns;
459 base::TimeDelta seek_timestamp = demuxer_->GetStartTime();
461 // Preroll renderers.
462 if (audio_renderer_) {
463 bound_fns.Push(base::Bind(
464 &AudioRenderer::Preroll, base::Unretained(audio_renderer_.get()),
465 seek_timestamp));
468 if (video_renderer_) {
469 bound_fns.Push(base::Bind(
470 &VideoRenderer::Preroll, base::Unretained(video_renderer_.get()),
471 seek_timestamp));
474 pending_callbacks_ = SerialRunner::Run(bound_fns, done_cb);
477 void Pipeline::DoSeek(
478 base::TimeDelta seek_timestamp,
479 const PipelineStatusCB& done_cb) {
480 DCHECK(task_runner_->BelongsToCurrentThread());
481 DCHECK(!pending_callbacks_.get());
482 SerialRunner::Queue bound_fns;
484 // Pause.
485 if (audio_renderer_) {
486 bound_fns.Push(base::Bind(
487 &AudioRenderer::Pause, base::Unretained(audio_renderer_.get())));
489 if (video_renderer_) {
490 bound_fns.Push(base::Bind(
491 &VideoRenderer::Pause, base::Unretained(video_renderer_.get())));
493 if (text_renderer_) {
494 bound_fns.Push(base::Bind(
495 &TextRenderer::Pause, base::Unretained(text_renderer_.get())));
498 // Flush.
499 if (audio_renderer_) {
500 bound_fns.Push(base::Bind(
501 &AudioRenderer::Flush, base::Unretained(audio_renderer_.get())));
503 if (video_renderer_) {
504 bound_fns.Push(base::Bind(
505 &VideoRenderer::Flush, base::Unretained(video_renderer_.get())));
507 if (text_renderer_) {
508 bound_fns.Push(base::Bind(
509 &TextRenderer::Flush, base::Unretained(text_renderer_.get())));
512 // Seek demuxer.
513 bound_fns.Push(base::Bind(
514 &Demuxer::Seek, base::Unretained(demuxer_), seek_timestamp));
516 // Preroll renderers.
517 if (audio_renderer_) {
518 bound_fns.Push(base::Bind(
519 &AudioRenderer::Preroll, base::Unretained(audio_renderer_.get()),
520 seek_timestamp));
523 if (video_renderer_) {
524 bound_fns.Push(base::Bind(
525 &VideoRenderer::Preroll, base::Unretained(video_renderer_.get()),
526 seek_timestamp));
529 pending_callbacks_ = SerialRunner::Run(bound_fns, done_cb);
532 void Pipeline::DoPlay(const PipelineStatusCB& done_cb) {
533 DCHECK(task_runner_->BelongsToCurrentThread());
534 DCHECK(!pending_callbacks_.get());
535 SerialRunner::Queue bound_fns;
537 PlaybackRateChangedTask(GetPlaybackRate());
538 VolumeChangedTask(GetVolume());
540 if (audio_renderer_) {
541 bound_fns.Push(base::Bind(
542 &AudioRenderer::Play, base::Unretained(audio_renderer_.get())));
545 if (video_renderer_) {
546 bound_fns.Push(base::Bind(
547 &VideoRenderer::Play, base::Unretained(video_renderer_.get())));
550 if (text_renderer_) {
551 bound_fns.Push(base::Bind(
552 &TextRenderer::Play, base::Unretained(text_renderer_.get())));
555 pending_callbacks_ = SerialRunner::Run(bound_fns, done_cb);
558 void Pipeline::DoStop(const PipelineStatusCB& done_cb) {
559 DCHECK(task_runner_->BelongsToCurrentThread());
560 DCHECK(!pending_callbacks_.get());
561 SerialRunner::Queue bound_fns;
563 if (demuxer_) {
564 bound_fns.Push(base::Bind(
565 &Demuxer::Stop, base::Unretained(demuxer_)));
568 if (audio_renderer_) {
569 bound_fns.Push(base::Bind(
570 &AudioRenderer::Stop, base::Unretained(audio_renderer_.get())));
573 if (video_renderer_) {
574 bound_fns.Push(base::Bind(
575 &VideoRenderer::Stop, base::Unretained(video_renderer_.get())));
578 if (text_renderer_) {
579 bound_fns.Push(base::Bind(
580 &TextRenderer::Stop, base::Unretained(text_renderer_.get())));
583 pending_callbacks_ = SerialRunner::Run(bound_fns, done_cb);
586 void Pipeline::OnStopCompleted(PipelineStatus status) {
587 DCHECK(task_runner_->BelongsToCurrentThread());
588 DCHECK_EQ(state_, kStopping);
590 base::AutoLock l(lock_);
591 running_ = false;
594 SetState(kStopped);
595 pending_callbacks_.reset();
596 filter_collection_.reset();
597 audio_renderer_.reset();
598 video_renderer_.reset();
599 text_renderer_.reset();
600 demuxer_ = NULL;
602 // If we stop during initialization/seeking we want to run |seek_cb_|
603 // followed by |stop_cb_| so we don't leave outstanding callbacks around.
604 if (!seek_cb_.is_null()) {
605 base::ResetAndReturn(&seek_cb_).Run(status_);
606 error_cb_.Reset();
608 if (!stop_cb_.is_null()) {
609 error_cb_.Reset();
610 base::ResetAndReturn(&stop_cb_).Run();
612 // NOTE: pipeline may be deleted at this point in time as a result of
613 // executing |stop_cb_|.
614 return;
616 if (!error_cb_.is_null()) {
617 DCHECK_NE(status_, PIPELINE_OK);
618 base::ResetAndReturn(&error_cb_).Run(status_);
622 void Pipeline::AddBufferedTimeRange(base::TimeDelta start,
623 base::TimeDelta end) {
624 DCHECK(IsRunning());
625 base::AutoLock auto_lock(lock_);
626 buffered_time_ranges_.Add(start, end);
627 did_loading_progress_ = true;
630 void Pipeline::OnAudioRendererEnded() {
631 // Force post to process ended tasks after current execution frame.
632 task_runner_->PostTask(FROM_HERE, base::Bind(
633 &Pipeline::DoAudioRendererEnded, base::Unretained(this)));
634 media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::AUDIO_ENDED));
637 void Pipeline::OnVideoRendererEnded() {
638 // Force post to process ended tasks after current execution frame.
639 task_runner_->PostTask(FROM_HERE, base::Bind(
640 &Pipeline::DoVideoRendererEnded, base::Unretained(this)));
641 media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::VIDEO_ENDED));
644 void Pipeline::OnTextRendererEnded() {
645 // Force post to process ended messages after current execution frame.
646 task_runner_->PostTask(FROM_HERE, base::Bind(
647 &Pipeline::DoTextRendererEnded, base::Unretained(this)));
648 media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::TEXT_ENDED));
651 // Called from any thread.
652 void Pipeline::OnUpdateStatistics(const PipelineStatistics& stats) {
653 base::AutoLock auto_lock(lock_);
654 statistics_.audio_bytes_decoded += stats.audio_bytes_decoded;
655 statistics_.video_bytes_decoded += stats.video_bytes_decoded;
656 statistics_.video_frames_decoded += stats.video_frames_decoded;
657 statistics_.video_frames_dropped += stats.video_frames_dropped;
660 void Pipeline::StartTask() {
661 DCHECK(task_runner_->BelongsToCurrentThread());
662 CHECK_EQ(kCreated, state_)
663 << "Media pipeline cannot be started more than once";
665 text_renderer_ = filter_collection_->GetTextRenderer();
667 if (text_renderer_) {
668 text_renderer_->Initialize(
669 base::Bind(&Pipeline::OnTextRendererEnded, base::Unretained(this)));
672 StateTransitionTask(PIPELINE_OK);
675 void Pipeline::StopTask(const base::Closure& stop_cb) {
676 DCHECK(task_runner_->BelongsToCurrentThread());
677 DCHECK(stop_cb_.is_null());
679 if (state_ == kStopped) {
680 stop_cb.Run();
681 return;
684 stop_cb_ = stop_cb;
686 // We may already be stopping due to a runtime error.
687 if (state_ == kStopping)
688 return;
690 SetState(kStopping);
691 pending_callbacks_.reset();
692 DoStop(base::Bind(&Pipeline::OnStopCompleted, base::Unretained(this)));
695 void Pipeline::ErrorChangedTask(PipelineStatus error) {
696 DCHECK(task_runner_->BelongsToCurrentThread());
697 DCHECK_NE(PIPELINE_OK, error) << "PIPELINE_OK isn't an error!";
699 if (state_ == kStopping || state_ == kStopped)
700 return;
702 SetState(kStopping);
703 pending_callbacks_.reset();
704 status_ = error;
706 DoStop(base::Bind(&Pipeline::OnStopCompleted, base::Unretained(this)));
709 void Pipeline::PlaybackRateChangedTask(float playback_rate) {
710 DCHECK(task_runner_->BelongsToCurrentThread());
712 // Playback rate changes are only carried out while playing.
713 if (state_ != kStarting && state_ != kStarted)
714 return;
717 base::AutoLock auto_lock(lock_);
718 clock_->SetPlaybackRate(playback_rate);
721 if (audio_renderer_)
722 audio_renderer_->SetPlaybackRate(playback_rate_);
723 if (video_renderer_)
724 video_renderer_->SetPlaybackRate(playback_rate_);
727 void Pipeline::VolumeChangedTask(float volume) {
728 DCHECK(task_runner_->BelongsToCurrentThread());
730 // Volume changes are only carried out while playing.
731 if (state_ != kStarting && state_ != kStarted)
732 return;
734 if (audio_renderer_)
735 audio_renderer_->SetVolume(volume);
738 void Pipeline::SeekTask(TimeDelta time, const PipelineStatusCB& seek_cb) {
739 DCHECK(task_runner_->BelongsToCurrentThread());
740 DCHECK(stop_cb_.is_null());
742 // Suppress seeking if we're not fully started.
743 if (state_ != kStarted) {
744 DCHECK(state_ == kStopping || state_ == kStopped)
745 << "Receive extra seek in unexpected state: " << state_;
747 // TODO(scherkus): should we run the callback? I'm tempted to say the API
748 // will only execute the first Seek() request.
749 DVLOG(1) << "Media pipeline has not started, ignoring seek to "
750 << time.InMicroseconds() << " (current state: " << state_ << ")";
751 return;
754 DCHECK(seek_cb_.is_null());
756 SetState(kSeeking);
757 base::TimeDelta seek_timestamp = std::max(time, demuxer_->GetStartTime());
758 seek_cb_ = seek_cb;
759 audio_ended_ = false;
760 video_ended_ = false;
761 text_ended_ = false;
763 // Kick off seeking!
765 base::AutoLock auto_lock(lock_);
766 if (clock_->IsPlaying())
767 clock_->Pause();
768 clock_->SetTime(seek_timestamp, seek_timestamp);
770 DoSeek(seek_timestamp, base::Bind(
771 &Pipeline::OnStateTransition, base::Unretained(this)));
774 void Pipeline::DoAudioRendererEnded() {
775 DCHECK(task_runner_->BelongsToCurrentThread());
777 if (state_ != kStarted)
778 return;
780 DCHECK(!audio_ended_);
781 audio_ended_ = true;
783 // Start clock since there is no more audio to trigger clock updates.
785 base::AutoLock auto_lock(lock_);
786 clock_->SetMaxTime(clock_->Duration());
787 StartClockIfWaitingForTimeUpdate_Locked();
790 RunEndedCallbackIfNeeded();
793 void Pipeline::DoVideoRendererEnded() {
794 DCHECK(task_runner_->BelongsToCurrentThread());
796 if (state_ != kStarted)
797 return;
799 DCHECK(!video_ended_);
800 video_ended_ = true;
802 RunEndedCallbackIfNeeded();
805 void Pipeline::DoTextRendererEnded() {
806 DCHECK(task_runner_->BelongsToCurrentThread());
808 if (state_ != kStarted)
809 return;
811 DCHECK(!text_ended_);
812 text_ended_ = true;
814 RunEndedCallbackIfNeeded();
817 void Pipeline::RunEndedCallbackIfNeeded() {
818 DCHECK(task_runner_->BelongsToCurrentThread());
820 if (audio_renderer_ && !audio_ended_)
821 return;
823 if (video_renderer_ && !video_ended_)
824 return;
826 if (text_renderer_ && text_renderer_->HasTracks() && !text_ended_)
827 return;
830 base::AutoLock auto_lock(lock_);
831 clock_->EndOfStream();
834 DCHECK_EQ(status_, PIPELINE_OK);
835 ended_cb_.Run();
838 void Pipeline::AddTextStreamTask(DemuxerStream* text_stream,
839 const TextTrackConfig& config) {
840 DCHECK(task_runner_->BelongsToCurrentThread());
841 // TODO(matthewjheaney): fix up text_ended_ when text stream
842 // is added (http://crbug.com/321446).
843 text_renderer_->AddTextStream(text_stream, config);
846 void Pipeline::RemoveTextStreamTask(DemuxerStream* text_stream) {
847 DCHECK(task_runner_->BelongsToCurrentThread());
848 text_renderer_->RemoveTextStream(text_stream);
851 void Pipeline::InitializeDemuxer(const PipelineStatusCB& done_cb) {
852 DCHECK(task_runner_->BelongsToCurrentThread());
854 demuxer_ = filter_collection_->GetDemuxer();
855 demuxer_->Initialize(this, done_cb, text_renderer_);
858 void Pipeline::InitializeAudioRenderer(const PipelineStatusCB& done_cb) {
859 DCHECK(task_runner_->BelongsToCurrentThread());
861 audio_renderer_ = filter_collection_->GetAudioRenderer();
862 audio_renderer_->Initialize(
863 demuxer_->GetStream(DemuxerStream::AUDIO),
864 done_cb,
865 base::Bind(&Pipeline::OnUpdateStatistics, base::Unretained(this)),
866 base::Bind(&Pipeline::OnAudioUnderflow, base::Unretained(this)),
867 base::Bind(&Pipeline::OnAudioTimeUpdate, base::Unretained(this)),
868 base::Bind(&Pipeline::OnAudioRendererEnded, base::Unretained(this)),
869 base::Bind(&Pipeline::SetError, base::Unretained(this)));
872 void Pipeline::InitializeVideoRenderer(const PipelineStatusCB& done_cb) {
873 DCHECK(task_runner_->BelongsToCurrentThread());
875 video_renderer_ = filter_collection_->GetVideoRenderer();
876 video_renderer_->Initialize(
877 demuxer_->GetStream(DemuxerStream::VIDEO),
878 demuxer_->GetLiveness() == Demuxer::LIVENESS_LIVE,
879 done_cb,
880 base::Bind(&Pipeline::OnUpdateStatistics, base::Unretained(this)),
881 base::Bind(&Pipeline::OnVideoTimeUpdate, base::Unretained(this)),
882 base::Bind(&Pipeline::OnVideoRendererEnded, base::Unretained(this)),
883 base::Bind(&Pipeline::SetError, base::Unretained(this)),
884 base::Bind(&Pipeline::GetMediaTime, base::Unretained(this)),
885 base::Bind(&Pipeline::GetMediaDuration, base::Unretained(this)));
888 void Pipeline::OnAudioUnderflow() {
889 if (!task_runner_->BelongsToCurrentThread()) {
890 task_runner_->PostTask(FROM_HERE, base::Bind(
891 &Pipeline::OnAudioUnderflow, base::Unretained(this)));
892 return;
895 if (state_ != kStarted)
896 return;
898 if (audio_renderer_)
899 audio_renderer_->ResumeAfterUnderflow();
902 void Pipeline::StartClockIfWaitingForTimeUpdate_Locked() {
903 lock_.AssertAcquired();
904 if (!waiting_for_clock_update_)
905 return;
907 waiting_for_clock_update_ = false;
908 clock_->Play();
911 } // namespace media