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"
10 #include "base/callback.h"
11 #include "base/callback_helpers.h"
12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/location.h"
15 #include "base/metrics/histogram.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/stl_util.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_util.h"
20 #include "base/synchronization/condition_variable.h"
21 #include "media/base/media_log.h"
22 #include "media/base/media_switches.h"
23 #include "media/base/renderer.h"
24 #include "media/base/text_renderer.h"
25 #include "media/base/text_track_config.h"
26 #include "media/base/video_decoder_config.h"
28 using base::TimeDelta
;
33 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
35 : task_runner_(task_runner
),
36 media_log_(media_log
),
38 did_loading_progress_(false),
42 is_initialized_(false),
44 renderer_ended_(false),
45 text_renderer_ended_(false),
48 media_log_
->AddEvent(media_log_
->CreatePipelineStateChangedEvent(kCreated
));
50 media_log_
->CreateEvent(MediaLogEvent::PIPELINE_CREATED
));
53 Pipeline::~Pipeline() {
54 DCHECK(thread_checker_
.CalledOnValidThread())
55 << "Pipeline must be destroyed on same thread that created it";
56 DCHECK(!running_
) << "Stop() must complete before destroying object";
57 DCHECK(stop_cb_
.is_null());
58 DCHECK(seek_cb_
.is_null());
61 media_log_
->CreateEvent(MediaLogEvent::PIPELINE_DESTROYED
));
64 void Pipeline::Start(Demuxer
* demuxer
,
65 scoped_ptr
<Renderer
> renderer
,
66 const base::Closure
& ended_cb
,
67 const PipelineStatusCB
& error_cb
,
68 const PipelineStatusCB
& seek_cb
,
69 const PipelineMetadataCB
& metadata_cb
,
70 const BufferingStateCB
& buffering_state_cb
,
71 const base::Closure
& duration_change_cb
,
72 const AddTextTrackCB
& add_text_track_cb
) {
73 DCHECK(!ended_cb
.is_null());
74 DCHECK(!error_cb
.is_null());
75 DCHECK(!seek_cb
.is_null());
76 DCHECK(!metadata_cb
.is_null());
77 DCHECK(!buffering_state_cb
.is_null());
79 base::AutoLock
auto_lock(lock_
);
80 CHECK(!running_
) << "Media pipeline is already running";
84 renderer_
= renderer
.Pass();
88 metadata_cb_
= metadata_cb
;
89 buffering_state_cb_
= buffering_state_cb
;
90 duration_change_cb_
= duration_change_cb
;
91 add_text_track_cb_
= add_text_track_cb
;
93 task_runner_
->PostTask(
94 FROM_HERE
, base::Bind(&Pipeline::StartTask
, weak_factory_
.GetWeakPtr()));
97 void Pipeline::Stop(const base::Closure
& stop_cb
) {
98 DVLOG(2) << __FUNCTION__
;
99 task_runner_
->PostTask(
101 base::Bind(&Pipeline::StopTask
, weak_factory_
.GetWeakPtr(), stop_cb
));
104 void Pipeline::Seek(TimeDelta time
, const PipelineStatusCB
& seek_cb
) {
105 base::AutoLock
auto_lock(lock_
);
107 DLOG(ERROR
) << "Media pipeline isn't running. Ignoring Seek().";
111 task_runner_
->PostTask(
114 &Pipeline::SeekTask
, weak_factory_
.GetWeakPtr(), time
, seek_cb
));
117 bool Pipeline::IsRunning() const {
118 base::AutoLock
auto_lock(lock_
);
122 float Pipeline::GetPlaybackRate() const {
123 base::AutoLock
auto_lock(lock_
);
124 return playback_rate_
;
127 void Pipeline::SetPlaybackRate(float playback_rate
) {
128 if (playback_rate
< 0.0f
)
131 base::AutoLock
auto_lock(lock_
);
132 playback_rate_
= playback_rate
;
134 task_runner_
->PostTask(FROM_HERE
,
135 base::Bind(&Pipeline::PlaybackRateChangedTask
,
136 weak_factory_
.GetWeakPtr(),
141 float Pipeline::GetVolume() const {
142 base::AutoLock
auto_lock(lock_
);
146 void Pipeline::SetVolume(float volume
) {
147 if (volume
< 0.0f
|| volume
> 1.0f
)
150 base::AutoLock
auto_lock(lock_
);
153 task_runner_
->PostTask(
156 &Pipeline::VolumeChangedTask
, weak_factory_
.GetWeakPtr(), volume
));
160 TimeDelta
Pipeline::GetMediaTime() const {
161 base::AutoLock
auto_lock(lock_
);
165 TimeDelta media_time
= renderer_
->GetMediaTime();
166 return std::min(media_time
, duration_
);
169 Ranges
<TimeDelta
> Pipeline::GetBufferedTimeRanges() const {
170 base::AutoLock
auto_lock(lock_
);
171 return buffered_time_ranges_
;
174 TimeDelta
Pipeline::GetMediaDuration() const {
175 base::AutoLock
auto_lock(lock_
);
179 bool Pipeline::DidLoadingProgress() {
180 base::AutoLock
auto_lock(lock_
);
181 bool ret
= did_loading_progress_
;
182 did_loading_progress_
= false;
186 PipelineStatistics
Pipeline::GetStatistics() const {
187 base::AutoLock
auto_lock(lock_
);
191 void Pipeline::SetErrorForTesting(PipelineStatus status
) {
195 bool Pipeline::HasWeakPtrsForTesting() const {
196 DCHECK(task_runner_
->BelongsToCurrentThread());
197 return weak_factory_
.HasWeakPtrs();
200 void Pipeline::SetState(State next_state
) {
201 DVLOG(1) << GetStateString(state_
) << " -> " << GetStateString(next_state
);
204 media_log_
->AddEvent(media_log_
->CreatePipelineStateChangedEvent(next_state
));
207 #define RETURN_STRING(state) case state: return #state;
209 const char* Pipeline::GetStateString(State state
) {
211 RETURN_STRING(kCreated
);
212 RETURN_STRING(kInitDemuxer
);
213 RETURN_STRING(kInitRenderer
);
214 RETURN_STRING(kSeeking
);
215 RETURN_STRING(kPlaying
);
216 RETURN_STRING(kStopping
);
217 RETURN_STRING(kStopped
);
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_
;
237 return kInitRenderer
;
248 NOTREACHED() << "State has no transition: " << state_
;
252 void Pipeline::OnDemuxerError(PipelineStatus error
) {
253 task_runner_
->PostTask(FROM_HERE
,
254 base::Bind(&Pipeline::ErrorChangedTask
,
255 weak_factory_
.GetWeakPtr(),
259 void Pipeline::AddTextStream(DemuxerStream
* text_stream
,
260 const TextTrackConfig
& config
) {
261 task_runner_
->PostTask(FROM_HERE
,
262 base::Bind(&Pipeline::AddTextStreamTask
,
263 weak_factory_
.GetWeakPtr(),
268 void Pipeline::RemoveTextStream(DemuxerStream
* text_stream
) {
269 task_runner_
->PostTask(FROM_HERE
,
270 base::Bind(&Pipeline::RemoveTextStreamTask
,
271 weak_factory_
.GetWeakPtr(),
275 void Pipeline::OnError(PipelineStatus error
) {
276 DCHECK(task_runner_
->BelongsToCurrentThread());
278 DCHECK_NE(PIPELINE_OK
, error
);
279 VLOG(1) << "Media pipeline error: " << error
;
281 task_runner_
->PostTask(FROM_HERE
, base::Bind(
282 &Pipeline::ErrorChangedTask
, weak_factory_
.GetWeakPtr(), error
));
285 void Pipeline::SetDuration(TimeDelta duration
) {
287 media_log_
->AddEvent(
288 media_log_
->CreateTimeEvent(
289 MediaLogEvent::DURATION_SET
, "duration", duration
));
290 UMA_HISTOGRAM_LONG_TIMES("Media.Duration", duration
);
292 base::AutoLock
auto_lock(lock_
);
293 duration_
= duration
;
294 if (!duration_change_cb_
.is_null())
295 duration_change_cb_
.Run();
298 void Pipeline::OnStateTransition(PipelineStatus status
) {
299 DCHECK(task_runner_
->BelongsToCurrentThread());
300 // Force post to process state transitions after current execution frame.
301 task_runner_
->PostTask(
304 &Pipeline::StateTransitionTask
, weak_factory_
.GetWeakPtr(), status
));
307 void Pipeline::StateTransitionTask(PipelineStatus status
) {
308 DCHECK(task_runner_
->BelongsToCurrentThread());
310 // No-op any state transitions if we're stopping.
311 if (state_
== kStopping
|| state_
== kStopped
)
314 // Preserve existing abnormal status, otherwise update based on the result of
315 // the previous operation.
316 status_
= (status_
!= PIPELINE_OK
? status_
: status
);
318 if (status_
!= PIPELINE_OK
) {
319 ErrorChangedTask(status_
);
323 // Guard against accidentally clearing |pending_callbacks_| for states that
324 // use it as well as states that should not be using it.
325 DCHECK_EQ(pending_callbacks_
.get() != NULL
, state_
== kSeeking
);
327 pending_callbacks_
.reset();
329 PipelineStatusCB done_cb
=
330 base::Bind(&Pipeline::OnStateTransition
, weak_factory_
.GetWeakPtr());
332 // Switch states, performing any entrance actions for the new state as well.
333 SetState(GetNextState());
336 return InitializeDemuxer(done_cb
);
339 return InitializeRenderer(base::Bind(done_cb
, PIPELINE_OK
));
342 // Report metadata the first time we enter the playing state.
343 if (!is_initialized_
) {
344 is_initialized_
= true;
346 start_timestamp_
= demuxer_
->GetStartTime();
349 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
351 DCHECK(start_timestamp_
>= base::TimeDelta());
352 renderer_
->StartPlayingFrom(start_timestamp_
);
355 text_renderer_
->StartPlaying();
357 PlaybackRateChangedTask(GetPlaybackRate());
358 VolumeChangedTask(GetVolume());
365 NOTREACHED() << "State has no transition: " << state_
;
370 // Note that the usage of base::Unretained() with the renderers is considered
371 // safe as they are owned by |pending_callbacks_| and share the same lifetime.
373 // That being said, deleting the renderers while keeping |pending_callbacks_|
374 // running on the media thread would result in crashes.
375 void Pipeline::DoSeek(TimeDelta seek_timestamp
,
376 const PipelineStatusCB
& done_cb
) {
377 DCHECK(task_runner_
->BelongsToCurrentThread());
378 DCHECK(!pending_callbacks_
.get());
379 DCHECK_EQ(state_
, kSeeking
);
380 SerialRunner::Queue bound_fns
;
383 if (text_renderer_
) {
384 bound_fns
.Push(base::Bind(
385 &TextRenderer::Pause
, base::Unretained(text_renderer_
.get())));
391 base::Bind(&Renderer::Flush
, base::Unretained(renderer_
.get())));
393 if (text_renderer_
) {
394 bound_fns
.Push(base::Bind(
395 &TextRenderer::Flush
, base::Unretained(text_renderer_
.get())));
399 bound_fns
.Push(base::Bind(
400 &Demuxer::Seek
, base::Unretained(demuxer_
), seek_timestamp
));
402 pending_callbacks_
= SerialRunner::Run(bound_fns
, done_cb
);
405 void Pipeline::DoStop(const PipelineStatusCB
& done_cb
) {
406 DVLOG(2) << __FUNCTION__
;
407 DCHECK(task_runner_
->BelongsToCurrentThread());
408 DCHECK(!pending_callbacks_
.get());
410 // TODO(scherkus): Enforce that Renderer is only called on a single thread,
411 // even for accessing media time http://crbug.com/370634
412 scoped_ptr
<Renderer
> renderer
;
414 base::AutoLock
auto_lock(lock_
);
415 renderer
.swap(renderer_
);
418 text_renderer_
.reset();
425 task_runner_
->PostTask(FROM_HERE
, base::Bind(done_cb
, PIPELINE_OK
));
428 void Pipeline::OnStopCompleted(PipelineStatus status
) {
429 DVLOG(2) << __FUNCTION__
;
430 DCHECK(task_runner_
->BelongsToCurrentThread());
431 DCHECK_EQ(state_
, kStopping
);
433 DCHECK(!text_renderer_
);
436 base::AutoLock
auto_lock(lock_
);
443 // If we stop during initialization/seeking we want to run |seek_cb_|
444 // followed by |stop_cb_| so we don't leave outstanding callbacks around.
445 if (!seek_cb_
.is_null()) {
446 base::ResetAndReturn(&seek_cb_
).Run(status_
);
449 if (!stop_cb_
.is_null()) {
452 // Invalid all weak pointers so it's safe to destroy |this| on the render
454 weak_factory_
.InvalidateWeakPtrs();
456 base::ResetAndReturn(&stop_cb_
).Run();
458 // NOTE: pipeline may be deleted at this point in time as a result of
459 // executing |stop_cb_|.
462 if (!error_cb_
.is_null()) {
463 DCHECK_NE(status_
, PIPELINE_OK
);
464 base::ResetAndReturn(&error_cb_
).Run(status_
);
468 void Pipeline::AddBufferedTimeRange(TimeDelta start
, TimeDelta end
) {
470 base::AutoLock
auto_lock(lock_
);
471 buffered_time_ranges_
.Add(start
, end
);
472 did_loading_progress_
= true;
475 // Called from any thread.
476 void Pipeline::OnUpdateStatistics(const PipelineStatistics
& stats
) {
477 base::AutoLock
auto_lock(lock_
);
478 statistics_
.audio_bytes_decoded
+= stats
.audio_bytes_decoded
;
479 statistics_
.video_bytes_decoded
+= stats
.video_bytes_decoded
;
480 statistics_
.video_frames_decoded
+= stats
.video_frames_decoded
;
481 statistics_
.video_frames_dropped
+= stats
.video_frames_dropped
;
484 void Pipeline::StartTask() {
485 DCHECK(task_runner_
->BelongsToCurrentThread());
487 CHECK_EQ(kCreated
, state_
)
488 << "Media pipeline cannot be started more than once";
490 text_renderer_
= CreateTextRenderer();
491 if (text_renderer_
) {
492 text_renderer_
->Initialize(
493 base::Bind(&Pipeline::OnTextRendererEnded
, weak_factory_
.GetWeakPtr()));
496 StateTransitionTask(PIPELINE_OK
);
499 void Pipeline::StopTask(const base::Closure
& stop_cb
) {
500 DCHECK(task_runner_
->BelongsToCurrentThread());
501 DCHECK(stop_cb_
.is_null());
503 if (state_
== kStopped
) {
504 // Invalid all weak pointers so it's safe to destroy |this| on the render
506 weak_factory_
.InvalidateWeakPtrs();
508 // NOTE: pipeline may be deleted at this point in time as a result of
509 // executing |stop_cb|.
517 // We may already be stopping due to a runtime error.
518 if (state_
== kStopping
)
521 // Do not report statistics if the pipeline is not fully initialized.
522 if (state_
== kSeeking
|| state_
== kPlaying
) {
523 PipelineStatistics stats
= GetStatistics();
524 if (renderer_
->HasVideo() && stats
.video_frames_decoded
> 0) {
525 UMA_HISTOGRAM_COUNTS("Media.DroppedFrameCount",
526 stats
.video_frames_dropped
);
531 pending_callbacks_
.reset();
532 DoStop(base::Bind(&Pipeline::OnStopCompleted
, weak_factory_
.GetWeakPtr()));
535 void Pipeline::ErrorChangedTask(PipelineStatus error
) {
536 DCHECK(task_runner_
->BelongsToCurrentThread());
537 DCHECK_NE(PIPELINE_OK
, error
) << "PIPELINE_OK isn't an error!";
539 media_log_
->AddEvent(media_log_
->CreatePipelineErrorEvent(error
));
541 if (state_
== kStopping
|| state_
== kStopped
)
545 pending_callbacks_
.reset();
548 DoStop(base::Bind(&Pipeline::OnStopCompleted
, weak_factory_
.GetWeakPtr()));
551 void Pipeline::PlaybackRateChangedTask(float playback_rate
) {
552 DCHECK(task_runner_
->BelongsToCurrentThread());
554 // Playback rate changes are only carried out while playing.
555 if (state_
!= kPlaying
)
558 renderer_
->SetPlaybackRate(playback_rate
);
561 void Pipeline::VolumeChangedTask(float volume
) {
562 DCHECK(task_runner_
->BelongsToCurrentThread());
564 // Volume changes are only carried out while playing.
565 if (state_
!= kPlaying
)
568 renderer_
->SetVolume(volume
);
571 void Pipeline::SeekTask(TimeDelta time
, const PipelineStatusCB
& seek_cb
) {
572 DCHECK(task_runner_
->BelongsToCurrentThread());
573 DCHECK(stop_cb_
.is_null());
575 // Suppress seeking if we're not fully started.
576 if (state_
!= kPlaying
) {
577 DCHECK(state_
== kStopping
|| state_
== kStopped
)
578 << "Receive extra seek in unexpected state: " << state_
;
580 // TODO(scherkus): should we run the callback? I'm tempted to say the API
581 // will only execute the first Seek() request.
582 DVLOG(1) << "Media pipeline has not started, ignoring seek to "
583 << time
.InMicroseconds() << " (current state: " << state_
<< ")";
587 DCHECK(seek_cb_
.is_null());
589 const base::TimeDelta seek_timestamp
=
590 std::max(time
, demuxer_
->GetStartTime());
594 renderer_ended_
= false;
595 text_renderer_ended_
= false;
596 start_timestamp_
= seek_timestamp
;
598 DoSeek(seek_timestamp
,
599 base::Bind(&Pipeline::OnStateTransition
, weak_factory_
.GetWeakPtr()));
602 void Pipeline::OnRendererEnded() {
603 DCHECK(task_runner_
->BelongsToCurrentThread());
604 media_log_
->AddEvent(media_log_
->CreateEvent(MediaLogEvent::ENDED
));
606 if (state_
!= kPlaying
)
609 DCHECK(!renderer_ended_
);
610 renderer_ended_
= true;
612 RunEndedCallbackIfNeeded();
615 void Pipeline::OnTextRendererEnded() {
616 DCHECK(task_runner_
->BelongsToCurrentThread());
617 media_log_
->AddEvent(media_log_
->CreateEvent(MediaLogEvent::TEXT_ENDED
));
619 if (state_
!= kPlaying
)
622 DCHECK(!text_renderer_ended_
);
623 text_renderer_ended_
= true;
625 RunEndedCallbackIfNeeded();
628 void Pipeline::RunEndedCallbackIfNeeded() {
629 DCHECK(task_runner_
->BelongsToCurrentThread());
631 if (renderer_
&& !renderer_ended_
)
634 if (text_renderer_
&& text_renderer_
->HasTracks() && !text_renderer_ended_
)
637 DCHECK_EQ(status_
, PIPELINE_OK
);
641 scoped_ptr
<TextRenderer
> Pipeline::CreateTextRenderer() {
642 DCHECK(task_runner_
->BelongsToCurrentThread());
644 const CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
645 if (!cmd_line
->HasSwitch(switches::kEnableInbandTextTracks
))
646 return scoped_ptr
<media::TextRenderer
>();
648 return scoped_ptr
<media::TextRenderer
>(new media::TextRenderer(
650 base::Bind(&Pipeline::OnAddTextTrack
, weak_factory_
.GetWeakPtr())));
653 void Pipeline::AddTextStreamTask(DemuxerStream
* text_stream
,
654 const TextTrackConfig
& config
) {
655 DCHECK(task_runner_
->BelongsToCurrentThread());
656 // TODO(matthewjheaney): fix up text_ended_ when text stream
657 // is added (http://crbug.com/321446).
659 text_renderer_
->AddTextStream(text_stream
, config
);
662 void Pipeline::RemoveTextStreamTask(DemuxerStream
* text_stream
) {
663 DCHECK(task_runner_
->BelongsToCurrentThread());
665 text_renderer_
->RemoveTextStream(text_stream
);
668 void Pipeline::OnAddTextTrack(const TextTrackConfig
& config
,
669 const AddTextTrackDoneCB
& done_cb
) {
670 DCHECK(task_runner_
->BelongsToCurrentThread());
671 add_text_track_cb_
.Run(config
, done_cb
);
674 void Pipeline::InitializeDemuxer(const PipelineStatusCB
& done_cb
) {
675 DCHECK(task_runner_
->BelongsToCurrentThread());
676 demuxer_
->Initialize(this, done_cb
, text_renderer_
);
679 void Pipeline::InitializeRenderer(const base::Closure
& done_cb
) {
680 DCHECK(task_runner_
->BelongsToCurrentThread());
682 if (!demuxer_
->GetStream(DemuxerStream::AUDIO
) &&
683 !demuxer_
->GetStream(DemuxerStream::VIDEO
)) {
685 base::AutoLock
auto_lock(lock_
);
688 OnError(PIPELINE_ERROR_COULD_NOT_RENDER
);
692 base::WeakPtr
<Pipeline
> weak_this
= weak_factory_
.GetWeakPtr();
693 renderer_
->Initialize(
695 base::Bind(&Pipeline::OnUpdateStatistics
, weak_this
),
696 base::Bind(&Pipeline::OnRendererEnded
, weak_this
),
697 base::Bind(&Pipeline::OnError
, weak_this
),
698 base::Bind(&Pipeline::BufferingStateChanged
, weak_this
));
701 void Pipeline::ReportMetadata() {
702 DCHECK(task_runner_
->BelongsToCurrentThread());
703 PipelineMetadata metadata
;
704 metadata
.has_audio
= renderer_
->HasAudio();
705 metadata
.has_video
= renderer_
->HasVideo();
706 metadata
.timeline_offset
= demuxer_
->GetTimelineOffset();
707 DemuxerStream
* stream
= demuxer_
->GetStream(DemuxerStream::VIDEO
);
709 metadata
.natural_size
= stream
->video_decoder_config().natural_size();
710 metadata
.video_rotation
= stream
->video_rotation();
712 metadata_cb_
.Run(metadata
);
715 void Pipeline::BufferingStateChanged(BufferingState new_buffering_state
) {
716 DVLOG(1) << __FUNCTION__
<< "(" << new_buffering_state
<< ") ";
717 DCHECK(task_runner_
->BelongsToCurrentThread());
718 buffering_state_cb_
.Run(new_buffering_state
);