Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / media / renderers / audio_renderer_impl.h
blob164cdbc560490d162256eb326135ba5a8117ed7e
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 // Audio rendering unit utilizing an AudioRendererSink to output data.
6 //
7 // This class lives inside three threads during it's lifetime, namely:
8 // 1. Render thread
9 // Where the object is created.
10 // 2. Media thread (provided via constructor)
11 // All AudioDecoder methods are called on this thread.
12 // 3. Audio thread created by the AudioRendererSink.
13 // Render() is called here where audio data is decoded into raw PCM data.
15 // AudioRendererImpl talks to an AudioRendererAlgorithm that takes care of
16 // queueing audio data and stretching/shrinking audio data when playback rate !=
17 // 1.0 or 0.0.
19 #ifndef MEDIA_RENDERERS_AUDIO_RENDERER_IMPL_H_
20 #define MEDIA_RENDERERS_AUDIO_RENDERER_IMPL_H_
22 #include <deque>
24 #include "base/gtest_prod_util.h"
25 #include "base/memory/scoped_ptr.h"
26 #include "base/memory/weak_ptr.h"
27 #include "base/synchronization/lock.h"
28 #include "media/base/audio_decoder.h"
29 #include "media/base/audio_renderer.h"
30 #include "media/base/audio_renderer_sink.h"
31 #include "media/base/decryptor.h"
32 #include "media/base/media_log.h"
33 #include "media/base/time_source.h"
34 #include "media/filters/audio_renderer_algorithm.h"
35 #include "media/filters/decoder_stream.h"
37 namespace base {
38 class SingleThreadTaskRunner;
39 class TickClock;
42 namespace media {
44 class AudioBufferConverter;
45 class AudioBus;
46 class AudioClock;
47 class AudioHardwareConfig;
48 class AudioSplicer;
49 class DecryptingDemuxerStream;
51 class MEDIA_EXPORT AudioRendererImpl
52 : public AudioRenderer,
53 public TimeSource,
54 NON_EXPORTED_BASE(public AudioRendererSink::RenderCallback) {
55 public:
56 // |task_runner| is the thread on which AudioRendererImpl will execute.
58 // |sink| is used as the destination for the rendered audio.
60 // |decoders| contains the AudioDecoders to use when initializing.
61 AudioRendererImpl(
62 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
63 AudioRendererSink* sink,
64 ScopedVector<AudioDecoder> decoders,
65 const AudioHardwareConfig& hardware_config,
66 const scoped_refptr<MediaLog>& media_log);
67 ~AudioRendererImpl() override;
69 // TimeSource implementation.
70 void StartTicking() override;
71 void StopTicking() override;
72 void SetPlaybackRate(double rate) override;
73 void SetMediaTime(base::TimeDelta time) override;
74 base::TimeDelta CurrentMediaTime() override;
75 bool GetWallClockTimes(
76 const std::vector<base::TimeDelta>& media_timestamps,
77 std::vector<base::TimeTicks>* wall_clock_times) override;
79 // AudioRenderer implementation.
80 void Initialize(DemuxerStream* stream,
81 const PipelineStatusCB& init_cb,
82 const SetDecryptorReadyCB& set_decryptor_ready_cb,
83 const StatisticsCB& statistics_cb,
84 const BufferingStateCB& buffering_state_cb,
85 const base::Closure& ended_cb,
86 const PipelineStatusCB& error_cb,
87 const base::Closure& waiting_for_decryption_key_cb) override;
88 TimeSource* GetTimeSource() override;
89 void Flush(const base::Closure& callback) override;
90 void StartPlaying() override;
91 void SetVolume(float volume) override;
93 private:
94 friend class AudioRendererImplTest;
96 // Important detail: being in kPlaying doesn't imply that audio is being
97 // rendered. Rather, it means that the renderer is ready to go. The actual
98 // rendering of audio is controlled via Start/StopRendering().
100 // kUninitialized
101 // | Initialize()
102 // |
103 // V
104 // kInitializing
105 // | Decoders initialized
106 // |
107 // V Decoders reset
108 // kFlushed <------------------ kFlushing
109 // | StartPlaying() ^
110 // | |
111 // | | Flush()
112 // `---------> kPlaying --------'
113 enum State {
114 kUninitialized,
115 kInitializing,
116 kFlushing,
117 kFlushed,
118 kPlaying
121 // Callback from the audio decoder delivering decoded audio samples.
122 void DecodedAudioReady(AudioBufferStream::Status status,
123 const scoped_refptr<AudioBuffer>& buffer);
125 // Handles buffers that come out of |splicer_|.
126 // Returns true if more buffers are needed.
127 bool HandleSplicerBuffer_Locked(const scoped_refptr<AudioBuffer>& buffer);
129 // Helper functions for AudioDecoder::Status values passed to
130 // DecodedAudioReady().
131 void HandleAbortedReadOrDecodeError(bool is_decode_error);
133 void StartRendering_Locked();
134 void StopRendering_Locked();
136 // AudioRendererSink::RenderCallback implementation.
138 // NOTE: These are called on the audio callback thread!
140 // Render() fills the given buffer with audio data by delegating to its
141 // |algorithm_|. Render() also takes care of updating the clock.
142 // Returns the number of frames copied into |audio_bus|, which may be less
143 // than or equal to the initial number of frames in |audio_bus|
145 // If this method returns fewer frames than the initial number of frames in
146 // |audio_bus|, it could be a sign that the pipeline is stalled or unable to
147 // stream the data fast enough. In such scenarios, the callee should zero out
148 // unused portions of their buffer to play back silence.
150 // Render() updates the pipeline's playback timestamp. If Render() is
151 // not called at the same rate as audio samples are played, then the reported
152 // timestamp in the pipeline will be ahead of the actual audio playback. In
153 // this case |audio_delay_milliseconds| should be used to indicate when in the
154 // future should the filled buffer be played.
155 int Render(AudioBus* audio_bus, int audio_delay_milliseconds) override;
156 void OnRenderError() override;
158 // Helper methods that schedule an asynchronous read from the decoder as long
159 // as there isn't a pending read.
161 // Must be called on |task_runner_|.
162 void AttemptRead();
163 void AttemptRead_Locked();
164 bool CanRead_Locked();
165 void ChangeState_Locked(State new_state);
167 // Returns true if the data in the buffer is all before |start_timestamp_|.
168 // This can only return true while in the kPlaying state.
169 bool IsBeforeStartTime(const scoped_refptr<AudioBuffer>& buffer);
171 // Called upon AudioBufferStream initialization, or failure thereof (indicated
172 // by the value of |success|).
173 void OnAudioBufferStreamInitialized(bool succes);
175 // Used to initiate the flush operation once all pending reads have
176 // completed.
177 void DoFlush_Locked();
179 // Called when the |decoder_|.Reset() has completed.
180 void ResetDecoderDone();
182 // Called by the AudioBufferStream when a splice buffer is demuxed.
183 void OnNewSpliceBuffer(base::TimeDelta);
185 // Called by the AudioBufferStream when a config change occurs.
186 void OnConfigChange();
188 // Updates |buffering_state_| and fires |buffering_state_cb_|.
189 void SetBufferingState_Locked(BufferingState buffering_state);
191 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
193 scoped_ptr<AudioSplicer> splicer_;
194 scoped_ptr<AudioBufferConverter> buffer_converter_;
196 // Whether or not we expect to handle config changes.
197 bool expecting_config_changes_;
199 // The sink (destination) for rendered audio. |sink_| must only be accessed
200 // on |task_runner_|. |sink_| must never be called under |lock_| or else we
201 // may deadlock between |task_runner_| and the audio callback thread.
202 scoped_refptr<media::AudioRendererSink> sink_;
204 scoped_ptr<AudioBufferStream> audio_buffer_stream_;
206 // Interface to the hardware audio params.
207 const AudioHardwareConfig& hardware_config_;
209 scoped_refptr<MediaLog> media_log_;
211 // Cached copy of hardware params from |hardware_config_|.
212 AudioParameters audio_parameters_;
214 // Callbacks provided during Initialize().
215 PipelineStatusCB init_cb_;
216 BufferingStateCB buffering_state_cb_;
217 base::Closure ended_cb_;
218 PipelineStatusCB error_cb_;
220 // Callback provided to Flush().
221 base::Closure flush_cb_;
223 // Overridable tick clock for testing.
224 scoped_ptr<base::TickClock> tick_clock_;
226 // After Initialize() has completed, all variables below must be accessed
227 // under |lock_|. ------------------------------------------------------------
228 base::Lock lock_;
230 // Algorithm for scaling audio.
231 double playback_rate_;
232 scoped_ptr<AudioRendererAlgorithm> algorithm_;
234 // Simple state tracking variable.
235 State state_;
237 BufferingState buffering_state_;
239 // Keep track of whether or not the sink is playing and whether we should be
240 // rendering.
241 bool rendering_;
242 bool sink_playing_;
244 // Keep track of our outstanding read to |decoder_|.
245 bool pending_read_;
247 // Keeps track of whether we received and rendered the end of stream buffer.
248 bool received_end_of_stream_;
249 bool rendered_end_of_stream_;
251 scoped_ptr<AudioClock> audio_clock_;
253 // The media timestamp to begin playback at after seeking. Set via
254 // SetMediaTime().
255 base::TimeDelta start_timestamp_;
257 // The media timestamp to signal end of audio playback. Determined during
258 // Render() when writing the final frames of decoded audio data.
259 base::TimeDelta ended_timestamp_;
261 // Set every Render() and used to provide an interpolated time value to
262 // CurrentMediaTimeForSyncingVideo().
263 base::TimeTicks last_render_time_;
265 // Set to the value of |last_render_time_| when StopRendering_Locked() is
266 // called for any reason. Cleared by the next successful Render() call after
267 // being used to adjust for lost time between the last call.
268 base::TimeTicks stop_rendering_time_;
270 // Set upon receipt of the first decoded buffer after a StartPlayingFrom().
271 // Used to determine how long to delay playback.
272 base::TimeDelta first_packet_timestamp_;
274 // End variables which must be accessed under |lock_|. ----------------------
276 // NOTE: Weak pointers must be invalidated before all other member variables.
277 base::WeakPtrFactory<AudioRendererImpl> weak_factory_;
279 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl);
282 } // namespace media
284 #endif // MEDIA_RENDERERS_AUDIO_RENDERER_IMPL_H_