Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: media/filters/audio_renderer_base.h

Issue 9826023: Merge AudioRendererImpl and AudioRendererBase; add NullAudioSink (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // AudioRendererBase takes care of the tricky queuing work and provides simple 5 // Audio rendering unit utilizing an AudioRendererSink to output data.
6 // methods for subclasses to peek and poke at audio data. In addition to
7 // AudioRenderer interface methods this classes doesn't implement, subclasses
8 // must also implement the following methods:
9 // OnInitialized
10 // OnStop
11 // OnRenderEndOfStream
12 // 6 //
13 // The general assumption is that subclasses start a callback-based audio thread 7 // This class lives inside three threads during it's lifetime, namely:
14 // which needs to be filled with decoded audio data. AudioDecoderBase provides 8 // 1. Render thread.
15 // FillBuffer which handles filling the provided buffer, dequeuing items, 9 // This object is created on the render thread.
16 // scheduling additional reads and updating the clock. In a sense, 10 // 2. Pipeline thread
17 // AudioRendererBase is the producer and the subclass is the consumer. 11 // Initialize() is called here with the audio format.
12 // Play/Pause/Seek also happens here.
13 // 3. Audio thread created by the AudioRendererSink.
14 // Render() is called here where audio data is decoded into raw PCM data.
15 //
16 // AudioRendererBase talks to an AudioRendererAlgorithmBase that takes care of
17 // queueing audio data and stretching/shrinking audio data when playback rate !=
18 // 1.0 or 0.0.
18 19
19 #ifndef MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ 20 #ifndef MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_
20 #define MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ 21 #define MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_
21 22
22 #include <deque> 23 #include <deque>
23 24
24 #include "base/synchronization/lock.h" 25 #include "base/synchronization/lock.h"
25 #include "media/base/audio_decoder.h" 26 #include "media/base/audio_decoder.h"
27 #include "media/base/audio_renderer_sink.h"
26 #include "media/base/buffers.h" 28 #include "media/base/buffers.h"
27 #include "media/base/filters.h" 29 #include "media/base/filters.h"
28 #include "media/filters/audio_renderer_algorithm_base.h" 30 #include "media/filters/audio_renderer_algorithm_base.h"
29 31
30 namespace media { 32 namespace media {
31 33
32 class MEDIA_EXPORT AudioRendererBase : public AudioRenderer { 34 class MEDIA_EXPORT AudioRendererBase
35 : public AudioRenderer,
36 NON_EXPORTED_BASE(public media::AudioRendererSink::RenderCallback) {
37
scherkus (not reviewing) 2012/03/23 15:24:52 nit: remove blank line
vrk (LEFT CHROMIUM) 2012/04/02 21:17:54 Done.
33 public: 38 public:
34 AudioRendererBase(); 39 // Methods called on Render thread ------------------------------------------
40 // An AudioRendererSink is used as the destination for the rendered audio.
41 explicit AudioRendererBase(media::AudioRendererSink* sink);
35 virtual ~AudioRendererBase(); 42 virtual ~AudioRendererBase();
36 43
44 // Methods called on pipeline thread ----------------------------------------
37 // Filter implementation. 45 // Filter implementation.
38 virtual void Play(const base::Closure& callback) OVERRIDE; 46 virtual void Play(const base::Closure& callback) OVERRIDE;
39 virtual void Pause(const base::Closure& callback) OVERRIDE; 47 virtual void Pause(const base::Closure& callback) OVERRIDE;
40 virtual void Flush(const base::Closure& callback) OVERRIDE; 48 virtual void Flush(const base::Closure& callback) OVERRIDE;
41 virtual void Stop(const base::Closure& callback) OVERRIDE; 49 virtual void Stop(const base::Closure& callback) OVERRIDE;
50 virtual void SetPlaybackRate(float rate) OVERRIDE;
42 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE; 51 virtual void Seek(base::TimeDelta time, const PipelineStatusCB& cb) OVERRIDE;
43 52
44 // AudioRenderer implementation. 53 // AudioRenderer implementation.
45 virtual void Initialize(const scoped_refptr<AudioDecoder>& decoder, 54 virtual void Initialize(const scoped_refptr<AudioDecoder>& decoder,
46 const PipelineStatusCB& init_cb, 55 const PipelineStatusCB& init_cb,
47 const base::Closure& underflow_cb, 56 const base::Closure& underflow_cb,
48 const TimeCB& time_cb) OVERRIDE; 57 const TimeCB& time_cb) OVERRIDE;
49 virtual bool HasEnded() OVERRIDE; 58 virtual bool HasEnded() OVERRIDE;
50 virtual void ResumeAfterUnderflow(bool buffer_more_audio) OVERRIDE; 59 virtual void ResumeAfterUnderflow(bool buffer_more_audio) OVERRIDE;
60 virtual void SetVolume(float volume) OVERRIDE;
51 61
52 protected: 62 private:
63 friend class AudioRendererBaseTest;
53 FRIEND_TEST_ALL_PREFIXES(AudioRendererBaseTest, EndOfStream); 64 FRIEND_TEST_ALL_PREFIXES(AudioRendererBaseTest, EndOfStream);
54 FRIEND_TEST_ALL_PREFIXES(AudioRendererBaseTest, Underflow_EndOfStream); 65 FRIEND_TEST_ALL_PREFIXES(AudioRendererBaseTest, Underflow_EndOfStream);
55 66
56 // Subclasses should return true if they were able to initialize, false
57 // otherwise.
58 virtual bool OnInitialize(int bits_per_channel,
59 ChannelLayout channel_layout,
60 int sample_rate) = 0;
61
62 // Called by Stop(). Subclasses should perform any necessary cleanup during
63 // this time, such as stopping any running threads.
64 virtual void OnStop() = 0;
65
66 // Method called by FillBuffer() when it finds that it reached end of stream.
67 // FillBuffer() cannot immediately signal end of stream event because browser
68 // may have buffered data.
69 virtual void OnRenderEndOfStream() = 0;
70
71 // Callback from the audio decoder delivering decoded audio samples. 67 // Callback from the audio decoder delivering decoded audio samples.
72 void DecodedAudioReady(scoped_refptr<Buffer> buffer); 68 void DecodedAudioReady(scoped_refptr<Buffer> buffer);
73 69
74 // Fills the given buffer with audio data by delegating to its |algorithm_|. 70 // Fills the given buffer with audio data by delegating to its |algorithm_|.
75 // FillBuffer() also takes care of updating the clock. Returns the number of 71 // FillBuffer() also takes care of updating the clock. Returns the number of
76 // frames copied into |dest|, which may be less than or equal to 72 // frames copied into |dest|, which may be less than or equal to
77 // |requested_frames|. 73 // |requested_frames|.
78 // 74 //
79 // If this method returns fewer frames than |requested_frames|, it could 75 // If this method returns fewer frames than |requested_frames|, it could
80 // be a sign that the pipeline is stalled or unable to stream the data fast 76 // be a sign that the pipeline is stalled or unable to stream the data fast
81 // enough. In such scenarios, the callee should zero out unused portions 77 // enough. In such scenarios, the callee should zero out unused portions
82 // of their buffer to playback silence. 78 // of their buffer to playback silence.
83 // 79 //
84 // FillBuffer() updates the pipeline's playback timestamp. If FillBuffer() is 80 // FillBuffer() updates the pipeline's playback timestamp. If FillBuffer() is
85 // not called at the same rate as audio samples are played, then the reported 81 // not called at the same rate as audio samples are played, then the reported
86 // timestamp in the pipeline will be ahead of the actual audio playback. In 82 // timestamp in the pipeline will be ahead of the actual audio playback. In
87 // this case |playback_delay| should be used to indicate when in the future 83 // this case |playback_delay| should be used to indicate when in the future
88 // should the filled buffer be played. If FillBuffer() is called as the audio 84 // should the filled buffer be played. If FillBuffer() is called as the audio
89 // hardware plays the buffer, then |playback_delay| should be zero. 85 // hardware plays the buffer, then |playback_delay| should be zero.
90 // 86 //
91 // FillBuffer() calls OnRenderEndOfStream() when it reaches end of stream. 87 // FillBuffer() calls SignalEndOfStream() when it reaches end of stream.
92 // It is responsibility of derived class to provide implementation of
93 // OnRenderEndOfStream() that calls SignalEndOfStream() when all the hardware
94 // buffers become empty (i.e. when all the data written to the device has
95 // been played).
96 // 88 //
97 // Safe to call on any thread. 89 // Safe to call on any thread.
98 uint32 FillBuffer(uint8* dest, 90 uint32 FillBuffer(uint8* dest,
99 uint32 requested_frames, 91 uint32 requested_frames,
100 const base::TimeDelta& playback_delay); 92 const base::TimeDelta& playback_delay);
101 93
102 // Called by OnRenderEndOfStream() or some callback scheduled by derived class 94 // Called at the end of stream when all the hardware buffers become empty
103 // to signal end of stream. 95 // (i.e. when all the data written to the device has been played).
104 void SignalEndOfStream(); 96 void SignalEndOfStream();
105 97
106 // Get/Set the playback rate of |algorithm_|. 98 // Get the playback rate of |algorithm_|.
107 virtual void SetPlaybackRate(float playback_rate) OVERRIDE; 99 float GetPlaybackRate();
108 virtual float GetPlaybackRate();
109 100
110 private: 101 // Convert number of bytes to duration of time using information about the
111 friend class AudioRendererBaseTest; 102 // number of channels, sample rate and sample bits.
103 base::TimeDelta ConvertToDuration(int bytes);
104
105 // Estimate earliest time when current buffer can stop playing.
106 void UpdateEarliestEndTime(int bytes_filled,
107 base::TimeDelta request_delay,
108 base::Time time_now);
109
110 // Methods called on pipeline thread ----------------------------------------
111 void DoPlay();
112 void DoPause();
113 void DoSeek();
114
115 // media::AudioRendererSink::RenderCallback implementation.
116 int Render(const std::vector<float*>& audio_data,
117 int number_of_frames,
118 int audio_delay_milliseconds) OVERRIDE;
119 virtual void OnRenderError() OVERRIDE;
112 120
113 // Helper method that schedules an asynchronous read from the decoder and 121 // Helper method that schedules an asynchronous read from the decoder and
114 // increments |pending_reads_|. 122 // increments |pending_reads_|.
115 // 123 //
116 // Safe to call from any thread. 124 // Safe to call from any thread.
117 void ScheduleRead_Locked(); 125 void ScheduleRead_Locked();
118 126
119 // Returns true if the data in the buffer is all before 127 // Returns true if the data in the buffer is all before
120 // |seek_timestamp_|. This can only return true while 128 // |seek_timestamp_|. This can only return true while
121 // in the kSeeking state. 129 // in the kSeeking state.
(...skipping 19 matching lines...) Expand all
141 }; 149 };
142 State state_; 150 State state_;
143 151
144 // Keep track of our outstanding read to |decoder_|. 152 // Keep track of our outstanding read to |decoder_|.
145 bool pending_read_; 153 bool pending_read_;
146 154
147 // Keeps track of whether we received and rendered the end of stream buffer. 155 // Keeps track of whether we received and rendered the end of stream buffer.
148 bool received_end_of_stream_; 156 bool received_end_of_stream_;
149 bool rendered_end_of_stream_; 157 bool rendered_end_of_stream_;
150 158
151 // Audio time at end of last call to FillBuffer(). 159 // The timestamp of the last frame (i.e. furthest in the future) buffered.
152 // TODO(ralphl): Update this value after seeking. 160 // TODO(ralphl): Update this value after seeking.
153 base::TimeDelta last_fill_buffer_time_; 161 base::TimeDelta audio_time_buffered_;
154 162
155 // Filter callbacks. 163 // Filter callbacks.
156 base::Closure pause_cb_; 164 base::Closure pause_cb_;
157 PipelineStatusCB seek_cb_; 165 PipelineStatusCB seek_cb_;
158 166
159 base::Closure underflow_cb_; 167 base::Closure underflow_cb_;
160 168
161 TimeCB time_cb_; 169 TimeCB time_cb_;
162 170
163 base::TimeDelta seek_timestamp_; 171 base::TimeDelta seek_timestamp_;
164 172
165 uint32 bytes_per_frame_; 173 uint32 bytes_per_frame_;
166 174
175 // Used to calculate audio delay given bytes.
176 uint32 bytes_per_second_;
177
178 // A flag that indicates this filter is called to stop.
179 bool stopped_;
180
181 // The sink (destination) for rendered audio.
182 scoped_refptr<media::AudioRendererSink> sink_;
183
184 // Set to true when OnInitialize() is called.
185 bool is_initialized_;
186
187 // We're supposed to know amount of audio data OS or hardware buffered, but
188 // that is not always so -- on my Linux box
189 // AudioBuffersState::hardware_delay_bytes never reaches 0.
190 //
191 // As a result we cannot use it to find when stream ends. If we just ignore
192 // buffered data we will notify host that stream ended before it is actually
193 // did so, I've seen it done ~140ms too early when playing ~150ms file.
194 //
195 // Instead of trying to invent OS-specific solution for each and every OS we
196 // are supporting, use simple workaround: every time we fill the buffer we
197 // remember when it should stop playing, and do not assume that buffer is
198 // empty till that time. Workaround is not bulletproof, as we don't exactly
199 // know when that particular data would start playing, but it is much better
200 // than nothing.
201 base::Time earliest_end_time_;
202
203 AudioParameters audio_parameters_;
204
167 AudioDecoder::ReadCB read_cb_; 205 AudioDecoder::ReadCB read_cb_;
168 206
169 DISALLOW_COPY_AND_ASSIGN(AudioRendererBase); 207 DISALLOW_COPY_AND_ASSIGN(AudioRendererBase);
170 }; 208 };
171 209
172 } // namespace media 210 } // namespace media
173 211
174 #endif // MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_ 212 #endif // MEDIA_FILTERS_AUDIO_RENDERER_BASE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698