• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
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.
14 //
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.
18 
19 #ifndef MEDIA_FILTERS_AUDIO_RENDERER_IMPL_H_
20 #define MEDIA_FILTERS_AUDIO_RENDERER_IMPL_H_
21 
22 #include <deque>
23 
24 #include "base/gtest_prod_util.h"
25 #include "base/memory/weak_ptr.h"
26 #include "base/synchronization/lock.h"
27 #include "media/base/audio_decoder.h"
28 #include "media/base/audio_renderer.h"
29 #include "media/base/audio_renderer_sink.h"
30 #include "media/base/decryptor.h"
31 #include "media/filters/audio_renderer_algorithm.h"
32 #include "media/filters/decoder_stream.h"
33 
34 namespace base {
35 class SingleThreadTaskRunner;
36 }
37 
38 namespace media {
39 
40 class AudioBufferConverter;
41 class AudioBus;
42 class AudioClock;
43 class AudioHardwareConfig;
44 class AudioSplicer;
45 class DecryptingDemuxerStream;
46 
47 class MEDIA_EXPORT AudioRendererImpl
48     : public AudioRenderer,
49       NON_EXPORTED_BASE(public AudioRendererSink::RenderCallback) {
50  public:
51   // |task_runner| is the thread on which AudioRendererImpl will execute.
52   //
53   // |sink| is used as the destination for the rendered audio.
54   //
55   // |decoders| contains the AudioDecoders to use when initializing.
56   //
57   // |set_decryptor_ready_cb| is fired when the audio decryptor is available
58   // (only applicable if the stream is encrypted and we have a decryptor).
59   AudioRendererImpl(
60       const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
61       AudioRendererSink* sink,
62       ScopedVector<AudioDecoder> decoders,
63       const SetDecryptorReadyCB& set_decryptor_ready_cb,
64       AudioHardwareConfig* hardware_params);
65   virtual ~AudioRendererImpl();
66 
67   // AudioRenderer implementation.
68   virtual void Initialize(DemuxerStream* stream,
69                           const PipelineStatusCB& init_cb,
70                           const StatisticsCB& statistics_cb,
71                           const base::Closure& underflow_cb,
72                           const TimeCB& time_cb,
73                           const base::Closure& ended_cb,
74                           const PipelineStatusCB& error_cb) OVERRIDE;
75   virtual void StartRendering() OVERRIDE;
76   virtual void StopRendering() OVERRIDE;
77   virtual void Flush(const base::Closure& callback) OVERRIDE;
78   virtual void Stop(const base::Closure& callback) OVERRIDE;
79   virtual void SetPlaybackRate(float rate) OVERRIDE;
80   virtual void Preroll(base::TimeDelta time,
81                        const PipelineStatusCB& cb) OVERRIDE;
82   virtual void ResumeAfterUnderflow() OVERRIDE;
83   virtual void SetVolume(float volume) OVERRIDE;
84 
85   // Allows injection of a custom time callback for non-realtime testing.
86   typedef base::Callback<base::TimeTicks()> NowCB;
set_now_cb_for_testing(const NowCB & now_cb)87   void set_now_cb_for_testing(const NowCB& now_cb) {
88     now_cb_ = now_cb;
89   }
90 
91  private:
92   friend class AudioRendererImplTest;
93 
94   // Important detail: being in kPlaying doesn't imply that audio is being
95   // rendered. Rather, it means that the renderer is ready to go. The actual
96   // rendering of audio is controlled via Start/StopRendering().
97   //
98   //   kUninitialized
99   //         | Initialize()
100   //         |
101   //         V
102   //    kInitializing
103   //         | Decoders initialized
104   //         |
105   //         V            Decoders reset
106   //      kFlushed <------------------ kFlushing
107   //         | Preroll()                  ^
108   //         |                            |
109   //         V                            | Flush()
110   //     kPrerolling ----------------> kPlaying ---------.
111   //           Enough data buffered       ^              | Not enough data
112   //                                      |              | buffered
113   //                 Enough data buffered |              V
114   //                                 kRebuffering <--- kUnderflow
115   //                                      ResumeAfterUnderflow()
116   enum State {
117     kUninitialized,
118     kInitializing,
119     kFlushing,
120     kFlushed,
121     kPrerolling,
122     kPlaying,
123     kStopped,
124     kUnderflow,
125     kRebuffering,
126   };
127 
128   // Callback from the audio decoder delivering decoded audio samples.
129   void DecodedAudioReady(AudioBufferStream::Status status,
130                          const scoped_refptr<AudioBuffer>& buffer);
131 
132   // Handles buffers that come out of |splicer_|.
133   // Returns true if more buffers are needed.
134   bool HandleSplicerBuffer(const scoped_refptr<AudioBuffer>& buffer);
135 
136   // Helper functions for AudioDecoder::Status values passed to
137   // DecodedAudioReady().
138   void HandleAbortedReadOrDecodeError(bool is_decode_error);
139 
140   // Estimate earliest time when current buffer can stop playing.
141   void UpdateEarliestEndTime_Locked(int frames_filled,
142                                     const base::TimeDelta& playback_delay,
143                                     const base::TimeTicks& time_now);
144 
145   void StartRendering_Locked();
146   void StopRendering_Locked();
147 
148   // AudioRendererSink::RenderCallback implementation.
149   //
150   // NOTE: These are called on the audio callback thread!
151   //
152   // Render() fills the given buffer with audio data by delegating to its
153   // |algorithm_|. Render() also takes care of updating the clock.
154   // Returns the number of frames copied into |audio_bus|, which may be less
155   // than or equal to the initial number of frames in |audio_bus|
156   //
157   // If this method returns fewer frames than the initial number of frames in
158   // |audio_bus|, it could be a sign that the pipeline is stalled or unable to
159   // stream the data fast enough.  In such scenarios, the callee should zero out
160   // unused portions of their buffer to play back silence.
161   //
162   // Render() updates the pipeline's playback timestamp. If Render() is
163   // not called at the same rate as audio samples are played, then the reported
164   // timestamp in the pipeline will be ahead of the actual audio playback. In
165   // this case |audio_delay_milliseconds| should be used to indicate when in the
166   // future should the filled buffer be played.
167   virtual int Render(AudioBus* audio_bus,
168                      int audio_delay_milliseconds) OVERRIDE;
169   virtual void OnRenderError() OVERRIDE;
170 
171   // Helper methods that schedule an asynchronous read from the decoder as long
172   // as there isn't a pending read.
173   //
174   // Must be called on |task_runner_|.
175   void AttemptRead();
176   void AttemptRead_Locked();
177   bool CanRead_Locked();
178   void ChangeState_Locked(State new_state);
179 
180   // Returns true if the data in the buffer is all before
181   // |preroll_timestamp_|. This can only return true while
182   // in the kPrerolling state.
183   bool IsBeforePrerollTime(const scoped_refptr<AudioBuffer>& buffer);
184 
185   // Called upon AudioBufferStream initialization, or failure thereof (indicated
186   // by the value of |success|).
187   void OnAudioBufferStreamInitialized(bool succes);
188 
189   // Used to initiate the flush operation once all pending reads have
190   // completed.
191   void DoFlush_Locked();
192 
193   // Calls |decoder_|.Reset() and arranges for ResetDecoderDone() to get
194   // called when the reset completes.
195   void ResetDecoder();
196 
197   // Called when the |decoder_|.Reset() has completed.
198   void ResetDecoderDone();
199 
200   // Called by the AudioBufferStream when a splice buffer is demuxed.
201   void OnNewSpliceBuffer(base::TimeDelta);
202 
203   // Called by the AudioBufferStream when a config change occurs.
204   void OnConfigChange();
205 
206   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
207 
208   scoped_ptr<AudioSplicer> splicer_;
209   scoped_ptr<AudioBufferConverter> buffer_converter_;
210 
211   // Whether or not we expect to handle config changes.
212   bool expecting_config_changes_;
213 
214   // The sink (destination) for rendered audio. |sink_| must only be accessed
215   // on |task_runner_|. |sink_| must never be called under |lock_| or else we
216   // may deadlock between |task_runner_| and the audio callback thread.
217   scoped_refptr<media::AudioRendererSink> sink_;
218 
219   AudioBufferStream audio_buffer_stream_;
220 
221   // Interface to the hardware audio params.
222   const AudioHardwareConfig* const hardware_config_;
223 
224   // Cached copy of hardware params from |hardware_config_|.
225   AudioParameters audio_parameters_;
226 
227   // Callbacks provided during Initialize().
228   PipelineStatusCB init_cb_;
229   base::Closure underflow_cb_;
230   TimeCB time_cb_;
231   base::Closure ended_cb_;
232   PipelineStatusCB error_cb_;
233 
234   // Callback provided to Flush().
235   base::Closure flush_cb_;
236 
237   // Callback provided to Preroll().
238   PipelineStatusCB preroll_cb_;
239 
240   // Typically calls base::TimeTicks::Now() but can be overridden by a test.
241   NowCB now_cb_;
242 
243   // After Initialize() has completed, all variables below must be accessed
244   // under |lock_|. ------------------------------------------------------------
245   base::Lock lock_;
246 
247   // Algorithm for scaling audio.
248   scoped_ptr<AudioRendererAlgorithm> algorithm_;
249 
250   // Simple state tracking variable.
251   State state_;
252 
253   // Keep track of whether or not the sink is playing and whether we should be
254   // rendering.
255   bool rendering_;
256   bool sink_playing_;
257 
258   // Keep track of our outstanding read to |decoder_|.
259   bool pending_read_;
260 
261   // Keeps track of whether we received and rendered the end of stream buffer.
262   bool received_end_of_stream_;
263   bool rendered_end_of_stream_;
264 
265   scoped_ptr<AudioClock> audio_clock_;
266 
267   base::TimeDelta preroll_timestamp_;
268 
269   // We're supposed to know amount of audio data OS or hardware buffered, but
270   // that is not always so -- on my Linux box
271   // AudioBuffersState::hardware_delay_bytes never reaches 0.
272   //
273   // As a result we cannot use it to find when stream ends. If we just ignore
274   // buffered data we will notify host that stream ended before it is actually
275   // did so, I've seen it done ~140ms too early when playing ~150ms file.
276   //
277   // Instead of trying to invent OS-specific solution for each and every OS we
278   // are supporting, use simple workaround: every time we fill the buffer we
279   // remember when it should stop playing, and do not assume that buffer is
280   // empty till that time. Workaround is not bulletproof, as we don't exactly
281   // know when that particular data would start playing, but it is much better
282   // than nothing.
283   base::TimeTicks earliest_end_time_;
284   size_t total_frames_filled_;
285 
286   // True if the renderer receives a buffer with kAborted status during preroll,
287   // false otherwise. This flag is cleared on the next Preroll() call.
288   bool preroll_aborted_;
289 
290   // End variables which must be accessed under |lock_|. ----------------------
291 
292   // NOTE: Weak pointers must be invalidated before all other member variables.
293   base::WeakPtrFactory<AudioRendererImpl> weak_factory_;
294 
295   DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl);
296 };
297 
298 }  // namespace media
299 
300 #endif  // MEDIA_FILTERS_AUDIO_RENDERER_IMPL_H_
301