1 // Copyright 2014 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 #ifndef MEDIA_RENDERERS_RENDERER_IMPL_H_
6 #define MEDIA_RENDERERS_RENDERER_IMPL_H_
10 #include "base/cancelable_callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/synchronization/lock.h"
15 #include "base/time/clock.h"
16 #include "base/time/default_tick_clock.h"
17 #include "base/time/time.h"
18 #include "media/base/buffering_state.h"
19 #include "media/base/decryptor.h"
20 #include "media/base/media_export.h"
21 #include "media/base/pipeline_status.h"
22 #include "media/base/renderer.h"
25 class SingleThreadTaskRunner
;
31 class DemuxerStreamProvider
;
34 class WallClockTimeSource
;
36 class MEDIA_EXPORT RendererImpl
: public Renderer
{
38 // Renders audio/video streams using |audio_renderer| and |video_renderer|
39 // provided. All methods except for GetMediaTime() run on the |task_runner|.
40 // GetMediaTime() runs on the render main thread because it's part of JS sync
42 RendererImpl(const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
43 scoped_ptr
<AudioRenderer
> audio_renderer
,
44 scoped_ptr
<VideoRenderer
> video_renderer
);
46 ~RendererImpl() final
;
48 // Renderer implementation.
49 void Initialize(DemuxerStreamProvider
* demuxer_stream_provider
,
50 const PipelineStatusCB
& init_cb
,
51 const StatisticsCB
& statistics_cb
,
52 const BufferingStateCB
& buffering_state_cb
,
53 const base::Closure
& ended_cb
,
54 const PipelineStatusCB
& error_cb
,
55 const base::Closure
& waiting_for_decryption_key_cb
) final
;
56 void SetCdm(CdmContext
* cdm_context
,
57 const CdmAttachedCB
& cdm_attached_cb
) final
;
58 void Flush(const base::Closure
& flush_cb
) final
;
59 void StartPlayingFrom(base::TimeDelta time
) final
;
60 void SetPlaybackRate(double playback_rate
) final
;
61 void SetVolume(float volume
) final
;
62 base::TimeDelta
GetMediaTime() final
;
63 bool HasAudio() final
;
64 bool HasVideo() final
;
66 // Helper functions for testing purposes. Must be called before Initialize().
67 void DisableUnderflowForTesting();
68 void EnableClocklessVideoPlaybackForTesting();
69 void set_time_source_for_testing(TimeSource
* time_source
) {
70 time_source_
= time_source
;
72 void set_video_underflow_threshold_for_testing(base::TimeDelta threshold
) {
73 video_underflow_threshold_
= threshold
;
85 bool GetWallClockTimes(const std::vector
<base::TimeDelta
>& media_timestamps
,
86 std::vector
<base::TimeTicks
>* wall_clock_times
);
88 // Requests that this object notifies when a decryptor is ready through the
89 // |decryptor_ready_cb| provided.
90 // If |decryptor_ready_cb| is null, the existing callback will be fired with
91 // nullptr immediately and reset.
92 void SetDecryptorReadyCallback(const DecryptorReadyCB
& decryptor_ready_cb
);
94 // Helper functions and callbacks for Initialize().
95 void InitializeAudioRenderer();
96 void OnAudioRendererInitializeDone(PipelineStatus status
);
97 void InitializeVideoRenderer();
98 void OnVideoRendererInitializeDone(PipelineStatus status
);
100 // Helper functions and callbacks for Flush().
101 void FlushAudioRenderer();
102 void OnAudioRendererFlushDone();
103 void FlushVideoRenderer();
104 void OnVideoRendererFlushDone();
106 // Callback executed by filters to update statistics.
107 void OnUpdateStatistics(const PipelineStatistics
& stats
);
109 // Collection of callback methods and helpers for tracking changes in
110 // buffering state and transition from paused/underflow states and playing
113 // While in the kPlaying state:
114 // - A waiting to non-waiting transition indicates preroll has completed
115 // and StartPlayback() should be called
116 // - A non-waiting to waiting transition indicates underflow has occurred
117 // and PausePlayback() should be called
118 void OnBufferingStateChanged(BufferingState
* buffering_state
,
119 BufferingState new_buffering_state
);
120 bool WaitingForEnoughData() const;
121 void PausePlayback();
122 void StartPlayback();
124 // Callbacks executed when a renderer has ended.
125 void OnAudioRendererEnded();
126 void OnVideoRendererEnded();
127 bool PlaybackHasEnded() const;
128 void RunEndedCallbackIfNeeded();
130 // Callback executed when a runtime error happens.
131 void OnError(PipelineStatus error
);
135 // Task runner used to execute pipeline tasks.
136 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
138 DemuxerStreamProvider
* demuxer_stream_provider_
;
140 // Permanent callbacks to notify various renderer states/stats.
141 StatisticsCB statistics_cb_
;
142 base::Closure ended_cb_
;
143 PipelineStatusCB error_cb_
;
144 BufferingStateCB buffering_state_cb_
;
145 base::Closure waiting_for_decryption_key_cb_
;
147 // Temporary callback used for Initialize() and Flush().
148 PipelineStatusCB init_cb_
;
149 base::Closure flush_cb_
;
151 scoped_ptr
<AudioRenderer
> audio_renderer_
;
152 scoped_ptr
<VideoRenderer
> video_renderer_
;
154 // Renderer-provided time source used to control playback.
155 TimeSource
* time_source_
;
156 scoped_ptr
<WallClockTimeSource
> wall_clock_time_source_
;
158 double playback_rate_
;
160 // The time to start playback from after starting/seeking has completed.
161 base::TimeDelta start_time_
;
163 BufferingState audio_buffering_state_
;
164 BufferingState video_buffering_state_
;
166 // Whether we've received the audio/video ended events.
170 CdmContext
* cdm_context_
;
172 // Callback registered by filters (decoder or demuxer) to be informed of a
174 // Note: We could have multiple filters registering this callback. One
175 // callback is okay because:
176 // 1, We always initialize filters in sequence.
177 // 2, Filter initialization will not finish until this callback is satisfied.
178 DecryptorReadyCB decryptor_ready_cb_
;
180 bool underflow_disabled_for_testing_
;
181 bool clockless_video_playback_enabled_for_testing_
;
183 // Used to defer underflow for video when audio is present.
184 base::CancelableClosure deferred_underflow_cb_
;
186 // The amount of time to wait before declaring underflow if the video renderer
187 // runs out of data but the audio renderer still has enough.
188 base::TimeDelta video_underflow_threshold_
;
190 base::WeakPtr
<RendererImpl
> weak_this_
;
191 base::WeakPtrFactory
<RendererImpl
> weak_factory_
;
193 DISALLOW_COPY_AND_ASSIGN(RendererImpl
);
198 #endif // MEDIA_RENDERERS_RENDERER_IMPL_H_