Move sandboxed_unpacker.{h,cc} from chrome/ to extensions/
[chromium-blink-merge.git] / media / filters / audio_renderer_impl.h
blobbaf1d80e9e03ce4542c56124a5a71c97be24426f
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_FILTERS_AUDIO_RENDERER_IMPL_H_
20 #define MEDIA_FILTERS_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;
41 namespace media {
43 class AudioBufferConverter;
44 class AudioBus;
45 class AudioClock;
46 class AudioHardwareConfig;
47 class AudioSplicer;
48 class DecryptingDemuxerStream;
50 class MEDIA_EXPORT AudioRendererImpl
51 : public AudioRenderer,
52 public TimeSource,
53 NON_EXPORTED_BASE(public AudioRendererSink::RenderCallback) {
54 public:
55 // |task_runner| is the thread on which AudioRendererImpl will execute.
57 // |sink| is used as the destination for the rendered audio.
59 // |decoders| contains the AudioDecoders to use when initializing.
60 AudioRendererImpl(
61 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
62 AudioRendererSink* sink,
63 ScopedVector<AudioDecoder> decoders,
64 const AudioHardwareConfig& hardware_config,
65 const scoped_refptr<MediaLog>& media_log);
66 ~AudioRendererImpl() override;
68 // TimeSource implementation.
69 void StartTicking() override;
70 void StopTicking() override;
71 void SetPlaybackRate(float rate) override;
72 void SetMediaTime(base::TimeDelta time) override;
73 base::TimeDelta CurrentMediaTime() override;
74 base::TimeDelta CurrentMediaTimeForSyncingVideo() override;
76 // AudioRenderer implementation.
77 void Initialize(DemuxerStream* stream,
78 const PipelineStatusCB& init_cb,
79 const SetDecryptorReadyCB& set_decryptor_ready_cb,
80 const StatisticsCB& statistics_cb,
81 const BufferingStateCB& buffering_state_cb,
82 const base::Closure& ended_cb,
83 const PipelineStatusCB& error_cb) override;
84 TimeSource* GetTimeSource() override;
85 void Flush(const base::Closure& callback) override;
86 void StartPlaying() override;
87 void SetVolume(float volume) override;
89 private:
90 friend class AudioRendererImplTest;
92 // Important detail: being in kPlaying doesn't imply that audio is being
93 // rendered. Rather, it means that the renderer is ready to go. The actual
94 // rendering of audio is controlled via Start/StopRendering().
96 // kUninitialized
97 // | Initialize()
98 // |
99 // V
100 // kInitializing
101 // | Decoders initialized
102 // |
103 // V Decoders reset
104 // kFlushed <------------------ kFlushing
105 // | StartPlaying() ^
106 // | |
107 // | | Flush()
108 // `---------> kPlaying --------'
109 enum State {
110 kUninitialized,
111 kInitializing,
112 kFlushing,
113 kFlushed,
114 kPlaying
117 // Callback from the audio decoder delivering decoded audio samples.
118 void DecodedAudioReady(AudioBufferStream::Status status,
119 const scoped_refptr<AudioBuffer>& buffer);
121 // Handles buffers that come out of |splicer_|.
122 // Returns true if more buffers are needed.
123 bool HandleSplicerBuffer_Locked(const scoped_refptr<AudioBuffer>& buffer);
125 // Helper functions for AudioDecoder::Status values passed to
126 // DecodedAudioReady().
127 void HandleAbortedReadOrDecodeError(bool is_decode_error);
129 void StartRendering_Locked();
130 void StopRendering_Locked();
132 // AudioRendererSink::RenderCallback implementation.
134 // NOTE: These are called on the audio callback thread!
136 // Render() fills the given buffer with audio data by delegating to its
137 // |algorithm_|. Render() also takes care of updating the clock.
138 // Returns the number of frames copied into |audio_bus|, which may be less
139 // than or equal to the initial number of frames in |audio_bus|
141 // If this method returns fewer frames than the initial number of frames in
142 // |audio_bus|, it could be a sign that the pipeline is stalled or unable to
143 // stream the data fast enough. In such scenarios, the callee should zero out
144 // unused portions of their buffer to play back silence.
146 // Render() updates the pipeline's playback timestamp. If Render() is
147 // not called at the same rate as audio samples are played, then the reported
148 // timestamp in the pipeline will be ahead of the actual audio playback. In
149 // this case |audio_delay_milliseconds| should be used to indicate when in the
150 // future should the filled buffer be played.
151 int Render(AudioBus* audio_bus, int audio_delay_milliseconds) override;
152 void OnRenderError() override;
154 // Helper methods that schedule an asynchronous read from the decoder as long
155 // as there isn't a pending read.
157 // Must be called on |task_runner_|.
158 void AttemptRead();
159 void AttemptRead_Locked();
160 bool CanRead_Locked();
161 void ChangeState_Locked(State new_state);
163 // Returns true if the data in the buffer is all before |start_timestamp_|.
164 // This can only return true while in the kPlaying state.
165 bool IsBeforeStartTime(const scoped_refptr<AudioBuffer>& buffer);
167 // Called upon AudioBufferStream initialization, or failure thereof (indicated
168 // by the value of |success|).
169 void OnAudioBufferStreamInitialized(bool succes);
171 // Used to initiate the flush operation once all pending reads have
172 // completed.
173 void DoFlush_Locked();
175 // Calls |decoder_|.Reset() and arranges for ResetDecoderDone() to get
176 // called when the reset completes.
177 void ResetDecoder();
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 // Cached copy of hardware params from |hardware_config_|.
210 AudioParameters audio_parameters_;
212 // Callbacks provided during Initialize().
213 PipelineStatusCB init_cb_;
214 BufferingStateCB buffering_state_cb_;
215 base::Closure ended_cb_;
216 PipelineStatusCB error_cb_;
218 // Callback provided to Flush().
219 base::Closure flush_cb_;
221 // After Initialize() has completed, all variables below must be accessed
222 // under |lock_|. ------------------------------------------------------------
223 base::Lock lock_;
225 // Algorithm for scaling audio.
226 float playback_rate_;
227 scoped_ptr<AudioRendererAlgorithm> algorithm_;
229 // Simple state tracking variable.
230 State state_;
232 BufferingState buffering_state_;
234 // Keep track of whether or not the sink is playing and whether we should be
235 // rendering.
236 bool rendering_;
237 bool sink_playing_;
239 // Keep track of our outstanding read to |decoder_|.
240 bool pending_read_;
242 // Keeps track of whether we received and rendered the end of stream buffer.
243 bool received_end_of_stream_;
244 bool rendered_end_of_stream_;
246 scoped_ptr<AudioClock> audio_clock_;
248 // The media timestamp to begin playback at after seeking. Set via
249 // SetMediaTime().
250 base::TimeDelta start_timestamp_;
252 // The media timestamp to signal end of audio playback. Determined during
253 // Render() when writing the final frames of decoded audio data.
254 base::TimeDelta ended_timestamp_;
256 // Set every Render() and used to provide an interpolated time value to
257 // CurrentMediaTimeForSyncingVideo().
258 base::TimeTicks last_render_ticks_;
260 // Set upon receipt of the first decoded buffer after a StartPlayingFrom().
261 // Used to determine how long to delay playback.
262 base::TimeDelta first_packet_timestamp_;
264 // End variables which must be accessed under |lock_|. ----------------------
266 // NOTE: Weak pointers must be invalidated before all other member variables.
267 base::WeakPtrFactory<AudioRendererImpl> weak_factory_;
269 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl);
272 } // namespace media
274 #endif // MEDIA_FILTERS_AUDIO_RENDERER_IMPL_H_