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),
47 pending_cdm_context_(nullptr),
49 media_log_
->AddEvent(media_log_
->CreatePipelineStateChangedEvent(kCreated
));
51 media_log_
->CreateEvent(MediaLogEvent::PIPELINE_CREATED
));
54 Pipeline::~Pipeline() {
55 DCHECK(thread_checker_
.CalledOnValidThread())
56 << "Pipeline must be destroyed on same thread that created it";
57 DCHECK(!running_
) << "Stop() must complete before destroying object";
58 DCHECK(stop_cb_
.is_null());
59 DCHECK(seek_cb_
.is_null());
62 media_log_
->CreateEvent(MediaLogEvent::PIPELINE_DESTROYED
));
65 void Pipeline::Start(Demuxer
* demuxer
,
66 scoped_ptr
<Renderer
> renderer
,
67 const base::Closure
& ended_cb
,
68 const PipelineStatusCB
& error_cb
,
69 const PipelineStatusCB
& seek_cb
,
70 const PipelineMetadataCB
& metadata_cb
,
71 const BufferingStateCB
& buffering_state_cb
,
72 const PaintCB
& paint_cb
,
73 const base::Closure
& duration_change_cb
,
74 const AddTextTrackCB
& add_text_track_cb
) {
75 DCHECK(!ended_cb
.is_null());
76 DCHECK(!error_cb
.is_null());
77 DCHECK(!seek_cb
.is_null());
78 DCHECK(!metadata_cb
.is_null());
79 DCHECK(!buffering_state_cb
.is_null());
80 DCHECK(!paint_cb
.is_null());
82 base::AutoLock
auto_lock(lock_
);
83 CHECK(!running_
) << "Media pipeline is already running";
87 renderer_
= renderer
.Pass();
91 metadata_cb_
= metadata_cb
;
92 buffering_state_cb_
= buffering_state_cb
;
94 duration_change_cb_
= duration_change_cb
;
95 add_text_track_cb_
= add_text_track_cb
;
97 task_runner_
->PostTask(
98 FROM_HERE
, base::Bind(&Pipeline::StartTask
, weak_factory_
.GetWeakPtr()));
101 void Pipeline::Stop(const base::Closure
& stop_cb
) {
102 DVLOG(2) << __FUNCTION__
;
103 task_runner_
->PostTask(
105 base::Bind(&Pipeline::StopTask
, weak_factory_
.GetWeakPtr(), stop_cb
));
108 void Pipeline::Seek(TimeDelta time
, const PipelineStatusCB
& seek_cb
) {
109 base::AutoLock
auto_lock(lock_
);
111 DLOG(ERROR
) << "Media pipeline isn't running. Ignoring Seek().";
115 task_runner_
->PostTask(
118 &Pipeline::SeekTask
, weak_factory_
.GetWeakPtr(), time
, seek_cb
));
121 bool Pipeline::IsRunning() const {
122 base::AutoLock
auto_lock(lock_
);
126 float Pipeline::GetPlaybackRate() const {
127 base::AutoLock
auto_lock(lock_
);
128 return playback_rate_
;
131 void Pipeline::SetPlaybackRate(float playback_rate
) {
132 if (playback_rate
< 0.0f
)
135 base::AutoLock
auto_lock(lock_
);
136 playback_rate_
= playback_rate
;
138 task_runner_
->PostTask(FROM_HERE
,
139 base::Bind(&Pipeline::PlaybackRateChangedTask
,
140 weak_factory_
.GetWeakPtr(),
145 float Pipeline::GetVolume() const {
146 base::AutoLock
auto_lock(lock_
);
150 void Pipeline::SetVolume(float volume
) {
151 if (volume
< 0.0f
|| volume
> 1.0f
)
154 base::AutoLock
auto_lock(lock_
);
157 task_runner_
->PostTask(
160 &Pipeline::VolumeChangedTask
, weak_factory_
.GetWeakPtr(), volume
));
164 TimeDelta
Pipeline::GetMediaTime() const {
165 base::AutoLock
auto_lock(lock_
);
166 return renderer_
? std::min(renderer_
->GetMediaTime(), duration_
)
170 Ranges
<TimeDelta
> Pipeline::GetBufferedTimeRanges() const {
171 base::AutoLock
auto_lock(lock_
);
172 return buffered_time_ranges_
;
175 TimeDelta
Pipeline::GetMediaDuration() const {
176 base::AutoLock
auto_lock(lock_
);
180 bool Pipeline::DidLoadingProgress() {
181 base::AutoLock
auto_lock(lock_
);
182 bool ret
= did_loading_progress_
;
183 did_loading_progress_
= false;
187 PipelineStatistics
Pipeline::GetStatistics() const {
188 base::AutoLock
auto_lock(lock_
);
192 void Pipeline::SetCdm(CdmContext
* cdm_context
,
193 const CdmAttachedCB
& cdm_attached_cb
) {
194 task_runner_
->PostTask(
195 FROM_HERE
, base::Bind(&Pipeline::SetCdmTask
, weak_factory_
.GetWeakPtr(),
196 cdm_context
, cdm_attached_cb
));
199 void Pipeline::SetErrorForTesting(PipelineStatus status
) {
203 bool Pipeline::HasWeakPtrsForTesting() const {
204 DCHECK(task_runner_
->BelongsToCurrentThread());
205 return weak_factory_
.HasWeakPtrs();
208 void Pipeline::SetState(State next_state
) {
209 DVLOG(1) << GetStateString(state_
) << " -> " << GetStateString(next_state
);
212 media_log_
->AddEvent(media_log_
->CreatePipelineStateChangedEvent(next_state
));
215 #define RETURN_STRING(state) case state: return #state;
217 const char* Pipeline::GetStateString(State state
) {
219 RETURN_STRING(kCreated
);
220 RETURN_STRING(kInitDemuxer
);
221 RETURN_STRING(kInitRenderer
);
222 RETURN_STRING(kSeeking
);
223 RETURN_STRING(kPlaying
);
224 RETURN_STRING(kStopping
);
225 RETURN_STRING(kStopped
);
233 Pipeline::State
Pipeline::GetNextState() const {
234 DCHECK(task_runner_
->BelongsToCurrentThread());
235 DCHECK(stop_cb_
.is_null())
236 << "State transitions don't happen when stopping";
237 DCHECK_EQ(status_
, PIPELINE_OK
)
238 << "State transitions don't happen when there's an error: " << status_
;
245 return kInitRenderer
;
256 NOTREACHED() << "State has no transition: " << state_
;
260 void Pipeline::OnDemuxerError(PipelineStatus error
) {
261 task_runner_
->PostTask(FROM_HERE
,
262 base::Bind(&Pipeline::ErrorChangedTask
,
263 weak_factory_
.GetWeakPtr(),
267 void Pipeline::AddTextStream(DemuxerStream
* text_stream
,
268 const TextTrackConfig
& config
) {
269 task_runner_
->PostTask(FROM_HERE
,
270 base::Bind(&Pipeline::AddTextStreamTask
,
271 weak_factory_
.GetWeakPtr(),
276 void Pipeline::RemoveTextStream(DemuxerStream
* text_stream
) {
277 task_runner_
->PostTask(FROM_HERE
,
278 base::Bind(&Pipeline::RemoveTextStreamTask
,
279 weak_factory_
.GetWeakPtr(),
283 void Pipeline::OnError(PipelineStatus error
) {
284 DCHECK(task_runner_
->BelongsToCurrentThread());
286 DCHECK_NE(PIPELINE_OK
, error
);
287 VLOG(1) << "Media pipeline error: " << error
;
289 task_runner_
->PostTask(FROM_HERE
, base::Bind(
290 &Pipeline::ErrorChangedTask
, weak_factory_
.GetWeakPtr(), error
));
293 void Pipeline::SetDuration(TimeDelta duration
) {
295 media_log_
->AddEvent(
296 media_log_
->CreateTimeEvent(
297 MediaLogEvent::DURATION_SET
, "duration", duration
));
298 UMA_HISTOGRAM_LONG_TIMES("Media.Duration", duration
);
300 base::AutoLock
auto_lock(lock_
);
301 duration_
= duration
;
302 if (!duration_change_cb_
.is_null())
303 duration_change_cb_
.Run();
306 void Pipeline::StateTransitionTask(PipelineStatus status
) {
307 DCHECK(task_runner_
->BelongsToCurrentThread());
309 // No-op any state transitions if we're stopping.
310 if (state_
== kStopping
|| state_
== kStopped
)
313 // Preserve existing abnormal status, otherwise update based on the result of
314 // the previous operation.
315 status_
= (status_
!= PIPELINE_OK
? status_
: status
);
317 if (status_
!= PIPELINE_OK
) {
318 ErrorChangedTask(status_
);
322 // Guard against accidentally clearing |pending_callbacks_| for states that
323 // use it as well as states that should not be using it.
324 DCHECK_EQ(pending_callbacks_
.get() != NULL
, state_
== kSeeking
);
326 pending_callbacks_
.reset();
328 PipelineStatusCB done_cb
=
329 base::Bind(&Pipeline::StateTransitionTask
, weak_factory_
.GetWeakPtr());
331 // Switch states, performing any entrance actions for the new state as well.
332 SetState(GetNextState());
335 return InitializeDemuxer(done_cb
);
338 return InitializeRenderer(done_cb
);
341 // Report metadata the first time we enter the playing state.
342 if (!is_initialized_
) {
343 is_initialized_
= true;
345 start_timestamp_
= demuxer_
->GetStartTime();
348 DCHECK(start_timestamp_
>= base::TimeDelta());
349 renderer_
->StartPlayingFrom(start_timestamp_
);
352 text_renderer_
->StartPlaying();
354 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
356 PlaybackRateChangedTask(GetPlaybackRate());
357 VolumeChangedTask(GetVolume());
364 NOTREACHED() << "State has no transition: " << state_
;
369 // Note that the usage of base::Unretained() with the renderers is considered
370 // safe as they are owned by |pending_callbacks_| and share the same lifetime.
372 // That being said, deleting the renderers while keeping |pending_callbacks_|
373 // running on the media thread would result in crashes.
374 void Pipeline::DoSeek(TimeDelta seek_timestamp
,
375 const PipelineStatusCB
& done_cb
) {
376 DCHECK(task_runner_
->BelongsToCurrentThread());
377 DCHECK(!pending_callbacks_
.get());
378 DCHECK_EQ(state_
, kSeeking
);
379 SerialRunner::Queue bound_fns
;
382 if (text_renderer_
) {
383 bound_fns
.Push(base::Bind(
384 &TextRenderer::Pause
, base::Unretained(text_renderer_
.get())));
390 base::Bind(&Renderer::Flush
, base::Unretained(renderer_
.get())));
392 if (text_renderer_
) {
393 bound_fns
.Push(base::Bind(
394 &TextRenderer::Flush
, base::Unretained(text_renderer_
.get())));
398 bound_fns
.Push(base::Bind(
399 &Demuxer::Seek
, base::Unretained(demuxer_
), seek_timestamp
));
401 pending_callbacks_
= SerialRunner::Run(bound_fns
, done_cb
);
404 void Pipeline::DoStop(const PipelineStatusCB
& done_cb
) {
405 DVLOG(2) << __FUNCTION__
;
406 DCHECK(task_runner_
->BelongsToCurrentThread());
407 DCHECK(!pending_callbacks_
.get());
409 // TODO(scherkus): Enforce that Renderer is only called on a single thread,
410 // even for accessing media time http://crbug.com/370634
411 scoped_ptr
<Renderer
> renderer
;
413 base::AutoLock
auto_lock(lock_
);
414 renderer
.swap(renderer_
);
417 text_renderer_
.reset();
424 task_runner_
->PostTask(FROM_HERE
, base::Bind(done_cb
, PIPELINE_OK
));
427 void Pipeline::OnStopCompleted(PipelineStatus status
) {
428 DVLOG(2) << __FUNCTION__
;
429 DCHECK(task_runner_
->BelongsToCurrentThread());
430 DCHECK_EQ(state_
, kStopping
);
432 DCHECK(!text_renderer_
);
435 base::AutoLock
auto_lock(lock_
);
442 // If we stop during initialization/seeking we want to run |seek_cb_|
443 // followed by |stop_cb_| so we don't leave outstanding callbacks around.
444 if (!seek_cb_
.is_null()) {
445 base::ResetAndReturn(&seek_cb_
).Run(status_
);
448 if (!stop_cb_
.is_null()) {
451 // Invalid all weak pointers so it's safe to destroy |this| on the render
453 weak_factory_
.InvalidateWeakPtrs();
455 base::ResetAndReturn(&stop_cb_
).Run();
457 // NOTE: pipeline may be deleted at this point in time as a result of
458 // executing |stop_cb_|.
461 if (!error_cb_
.is_null()) {
462 DCHECK_NE(status_
, PIPELINE_OK
);
463 base::ResetAndReturn(&error_cb_
).Run(status_
);
467 void Pipeline::AddBufferedTimeRange(TimeDelta start
, TimeDelta end
) {
469 base::AutoLock
auto_lock(lock_
);
470 buffered_time_ranges_
.Add(start
, end
);
471 did_loading_progress_
= true;
474 // Called from any thread.
475 void Pipeline::OnUpdateStatistics(const PipelineStatistics
& stats
) {
476 base::AutoLock
auto_lock(lock_
);
477 statistics_
.audio_bytes_decoded
+= stats
.audio_bytes_decoded
;
478 statistics_
.video_bytes_decoded
+= stats
.video_bytes_decoded
;
479 statistics_
.video_frames_decoded
+= stats
.video_frames_decoded
;
480 statistics_
.video_frames_dropped
+= stats
.video_frames_dropped
;
483 void Pipeline::StartTask() {
484 DCHECK(task_runner_
->BelongsToCurrentThread());
486 CHECK_EQ(kCreated
, state_
)
487 << "Media pipeline cannot be started more than once";
489 text_renderer_
= CreateTextRenderer();
490 if (text_renderer_
) {
491 text_renderer_
->Initialize(
492 base::Bind(&Pipeline::OnTextRendererEnded
, weak_factory_
.GetWeakPtr()));
495 // Set CDM early to avoid unnecessary delay in Renderer::Initialize().
496 if (pending_cdm_context_
) {
497 renderer_
->SetCdm(pending_cdm_context_
, base::Bind(&IgnoreCdmAttached
));
498 pending_cdm_context_
= nullptr;
501 StateTransitionTask(PIPELINE_OK
);
504 void Pipeline::StopTask(const base::Closure
& stop_cb
) {
505 DCHECK(task_runner_
->BelongsToCurrentThread());
506 DCHECK(stop_cb_
.is_null());
508 if (state_
== kStopped
) {
509 // Invalid all weak pointers so it's safe to destroy |this| on the render
511 weak_factory_
.InvalidateWeakPtrs();
513 // NOTE: pipeline may be deleted at this point in time as a result of
514 // executing |stop_cb|.
522 // We may already be stopping due to a runtime error.
523 if (state_
== kStopping
)
526 // Do not report statistics if the pipeline is not fully initialized.
527 if (state_
== kSeeking
|| state_
== kPlaying
) {
528 PipelineStatistics stats
= GetStatistics();
529 if (renderer_
->HasVideo() && stats
.video_frames_decoded
> 0) {
530 UMA_HISTOGRAM_COUNTS("Media.DroppedFrameCount",
531 stats
.video_frames_dropped
);
536 pending_callbacks_
.reset();
537 DoStop(base::Bind(&Pipeline::OnStopCompleted
, weak_factory_
.GetWeakPtr()));
540 void Pipeline::ErrorChangedTask(PipelineStatus error
) {
541 DCHECK(task_runner_
->BelongsToCurrentThread());
542 DCHECK_NE(PIPELINE_OK
, error
) << "PIPELINE_OK isn't an error!";
544 media_log_
->AddEvent(media_log_
->CreatePipelineErrorEvent(error
));
546 if (state_
== kStopping
|| state_
== kStopped
)
550 pending_callbacks_
.reset();
553 DoStop(base::Bind(&Pipeline::OnStopCompleted
, weak_factory_
.GetWeakPtr()));
556 void Pipeline::PlaybackRateChangedTask(float playback_rate
) {
557 DCHECK(task_runner_
->BelongsToCurrentThread());
559 // Playback rate changes are only carried out while playing.
560 if (state_
!= kPlaying
)
563 renderer_
->SetPlaybackRate(playback_rate
);
566 void Pipeline::VolumeChangedTask(float volume
) {
567 DCHECK(task_runner_
->BelongsToCurrentThread());
569 // Volume changes are only carried out while playing.
570 if (state_
!= kPlaying
)
573 renderer_
->SetVolume(volume
);
576 void Pipeline::SeekTask(TimeDelta time
, const PipelineStatusCB
& seek_cb
) {
577 DCHECK(task_runner_
->BelongsToCurrentThread());
578 DCHECK(stop_cb_
.is_null());
580 // Suppress seeking if we're not fully started.
581 if (state_
!= kPlaying
) {
582 DCHECK(state_
== kStopping
|| state_
== kStopped
)
583 << "Receive extra seek in unexpected state: " << state_
;
585 // TODO(scherkus): should we run the callback? I'm tempted to say the API
586 // will only execute the first Seek() request.
587 DVLOG(1) << "Media pipeline has not started, ignoring seek to "
588 << time
.InMicroseconds() << " (current state: " << state_
<< ")";
592 DCHECK(seek_cb_
.is_null());
594 const base::TimeDelta seek_timestamp
=
595 std::max(time
, demuxer_
->GetStartTime());
599 renderer_ended_
= false;
600 text_renderer_ended_
= false;
601 start_timestamp_
= seek_timestamp
;
603 DoSeek(seek_timestamp
, base::Bind(&Pipeline::StateTransitionTask
,
604 weak_factory_
.GetWeakPtr()));
607 void Pipeline::SetCdmTask(CdmContext
* cdm_context
,
608 const CdmAttachedCB
& cdm_attached_cb
) {
609 base::AutoLock
auto_lock(lock_
);
611 pending_cdm_context_
= cdm_context
;
612 cdm_attached_cb
.Run(true);
616 renderer_
->SetCdm(cdm_context
, cdm_attached_cb
);
619 void Pipeline::OnRendererEnded() {
620 DCHECK(task_runner_
->BelongsToCurrentThread());
621 media_log_
->AddEvent(media_log_
->CreateEvent(MediaLogEvent::ENDED
));
623 if (state_
!= kPlaying
)
626 DCHECK(!renderer_ended_
);
627 renderer_ended_
= true;
629 RunEndedCallbackIfNeeded();
632 void Pipeline::OnTextRendererEnded() {
633 DCHECK(task_runner_
->BelongsToCurrentThread());
634 media_log_
->AddEvent(media_log_
->CreateEvent(MediaLogEvent::TEXT_ENDED
));
636 if (state_
!= kPlaying
)
639 DCHECK(!text_renderer_ended_
);
640 text_renderer_ended_
= true;
642 RunEndedCallbackIfNeeded();
645 void Pipeline::RunEndedCallbackIfNeeded() {
646 DCHECK(task_runner_
->BelongsToCurrentThread());
648 if (renderer_
&& !renderer_ended_
)
651 if (text_renderer_
&& text_renderer_
->HasTracks() && !text_renderer_ended_
)
654 // Correct the duration against current time if it turns out that
655 // the initially reported duration is wrong
656 // TODO(sriram): There are cases where duration is correct and current time
657 // falls short of duration by a few milliseconds. This is a workaround
658 // till we find the actual fix and 250ms is chosen here as it is
659 // the max time between timeupdate events (http://crbug.com/438581).
660 TimeDelta media_time
= renderer_
->GetMediaTime();
661 if ((duration_
- media_time
).InMilliseconds() > 250)
662 SetDuration(media_time
);
664 DCHECK_EQ(status_
, PIPELINE_OK
);
668 scoped_ptr
<TextRenderer
> Pipeline::CreateTextRenderer() {
669 DCHECK(task_runner_
->BelongsToCurrentThread());
671 const base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
672 if (!cmd_line
->HasSwitch(switches::kEnableInbandTextTracks
))
673 return scoped_ptr
<media::TextRenderer
>();
675 return scoped_ptr
<media::TextRenderer
>(new media::TextRenderer(
677 base::Bind(&Pipeline::OnAddTextTrack
, weak_factory_
.GetWeakPtr())));
680 void Pipeline::AddTextStreamTask(DemuxerStream
* text_stream
,
681 const TextTrackConfig
& config
) {
682 DCHECK(task_runner_
->BelongsToCurrentThread());
683 // TODO(matthewjheaney): fix up text_ended_ when text stream
684 // is added (http://crbug.com/321446).
686 text_renderer_
->AddTextStream(text_stream
, config
);
689 void Pipeline::RemoveTextStreamTask(DemuxerStream
* text_stream
) {
690 DCHECK(task_runner_
->BelongsToCurrentThread());
692 text_renderer_
->RemoveTextStream(text_stream
);
695 void Pipeline::OnAddTextTrack(const TextTrackConfig
& config
,
696 const AddTextTrackDoneCB
& done_cb
) {
697 DCHECK(task_runner_
->BelongsToCurrentThread());
698 add_text_track_cb_
.Run(config
, done_cb
);
701 void Pipeline::InitializeDemuxer(const PipelineStatusCB
& done_cb
) {
702 DCHECK(task_runner_
->BelongsToCurrentThread());
703 demuxer_
->Initialize(this, done_cb
, text_renderer_
);
706 void Pipeline::InitializeRenderer(const PipelineStatusCB
& done_cb
) {
707 DCHECK(task_runner_
->BelongsToCurrentThread());
709 if (!demuxer_
->GetStream(DemuxerStream::AUDIO
) &&
710 !demuxer_
->GetStream(DemuxerStream::VIDEO
)) {
712 base::AutoLock
auto_lock(lock_
);
715 OnError(PIPELINE_ERROR_COULD_NOT_RENDER
);
719 base::WeakPtr
<Pipeline
> weak_this
= weak_factory_
.GetWeakPtr();
720 renderer_
->Initialize(
723 base::Bind(&Pipeline::OnUpdateStatistics
, weak_this
),
724 base::Bind(&Pipeline::BufferingStateChanged
, weak_this
),
725 base::ResetAndReturn(&paint_cb_
),
726 base::Bind(&Pipeline::OnRendererEnded
, weak_this
),
727 base::Bind(&Pipeline::OnError
, weak_this
));
730 void Pipeline::ReportMetadata() {
731 DCHECK(task_runner_
->BelongsToCurrentThread());
732 PipelineMetadata metadata
;
733 metadata
.has_audio
= renderer_
->HasAudio();
734 metadata
.has_video
= renderer_
->HasVideo();
735 metadata
.timeline_offset
= demuxer_
->GetTimelineOffset();
736 DemuxerStream
* stream
= demuxer_
->GetStream(DemuxerStream::VIDEO
);
738 metadata
.natural_size
= stream
->video_decoder_config().natural_size();
739 metadata
.video_rotation
= stream
->video_rotation();
741 metadata_cb_
.Run(metadata
);
744 void Pipeline::BufferingStateChanged(BufferingState new_buffering_state
) {
745 DVLOG(1) << __FUNCTION__
<< "(" << new_buffering_state
<< ") ";
746 DCHECK(task_runner_
->BelongsToCurrentThread());
747 buffering_state_cb_
.Run(new_buffering_state
);