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