OLD | NEW |
---|---|
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 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 5 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
6 #define MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 6 #define MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "media/audio/audio_buffers_state.h" | 14 #include "media/audio/audio_buffers_state.h" |
15 #include "media/audio/audio_io.h" | 15 #include "media/audio/audio_io.h" |
16 #include "media/audio/audio_manager.h" | 16 #include "media/audio/audio_manager.h" |
17 #include "media/audio/simple_sources.h" | 17 #include "media/audio/simple_sources.h" |
18 | 18 |
19 namespace base { | 19 namespace base { |
20 class WaitableEvent; | 20 class WaitableEvent; |
21 } // namespace base | 21 } // namespace base |
22 | 22 |
23 class MessageLoop; | 23 class MessageLoop; |
24 | 24 |
25 // An AudioOutputController controls an AudioOutputStream and provides data | 25 // An AudioOutputController controls an AudioOutputStream and provides data |
26 // to this output stream. It has an important function that it executes | 26 // to this output stream. It has an important function that it executes |
27 // audio operations like play, pause, stop, etc. on a separate thread, | 27 // audio operations like play, pause, stop, etc. on a separate thread, |
28 // namely the audio controller thread. | 28 // namely the audio controller thread. |
henrika (OOO until Aug 14)
2012/01/29 20:54:00
Can we please change this comment as well while we
vrk (LEFT CHROMIUM)
2012/01/31 23:53:08
Sure! Done, and combed through the comments in thi
| |
29 // | 29 // |
30 // All the public methods of AudioOutputController are non-blocking. | 30 // All the public methods of AudioOutputController are non-blocking. |
31 // The actual operations are performed on the audio thread. | 31 // The actual operations are performed on the audio thread. |
32 // | 32 // |
33 // Here is a state diagram for the AudioOutputController for default low | 33 // Here is a state diagram for the AudioOutputController: |
34 // latency mode; in normal latency mode there is no "starting" or "paused when | |
35 // starting" states, "created" immediately switches to "playing": | |
36 // | 34 // |
37 // .-----------------------> [ Closed / Error ] <------. | 35 // .-----------------------> [ Closed / Error ] <------. |
38 // | ^ | | 36 // | ^ | |
39 // | | | | 37 // | | | |
40 // [ Created ] --> [ Starting ] --> [ Playing ] --> [ Paused ] | 38 // [ Created ] --> [ Starting ] --> [ Playing ] --> [ Paused ] |
41 // ^ | ^ | ^ | 39 // ^ | ^ | ^ |
42 // | | | | | | 40 // | | | | | |
43 // | | `----------------' | | 41 // | | `----------------' | |
44 // | V | | 42 // | V | |
45 // | [ PausedWhenStarting ] ------------------------' | 43 // | [ PausedWhenStarting ] ------------------------' |
46 // | | 44 // | |
47 // *[ Empty ] | 45 // *[ Empty ] |
48 // | 46 // |
49 // * Initial state | 47 // * Initial state |
50 // | 48 // |
51 // There are two modes of buffering operations supported by this class. | 49 // The AudioOutputStream can request data from the AudioOutputController via the |
52 // | 50 // AudioSourceCallback interface. AudioOutputController uses the SyncReader |
henrika (OOO until Aug 14)
2012/01/29 20:54:00
I think we call the same interface on the render s
vrk (LEFT CHROMIUM)
2012/01/31 23:53:08
Yeah, I am planning to go through and rename some
henrika (OOO until Aug 14)
2012/02/01 13:11:29
Sounds fine.
| |
53 // Regular latency mode: | 51 // passed to it via construction to synchronously fulfill this read request. |
54 // In this mode we receive signals from AudioOutputController and then we | |
55 // enqueue data into it. | |
56 // | |
57 // Low latency mode: | |
58 // In this mode a DataSource object is given to the AudioOutputController | |
59 // and AudioOutputController reads from it synchronously. | |
60 // | 52 // |
61 // The audio thread itself is owned by the AudioManager that the | 53 // The audio thread itself is owned by the AudioManager that the |
62 // AudioOutputController holds a reference to. When performing tasks on the | 54 // AudioOutputController holds a reference to. When performing tasks on the |
63 // audio thread, the controller must not add or release references to the | 55 // audio thread, the controller must not add or release references to the |
64 // AudioManager or itself (since it in turn holds a reference to the manager), | 56 // AudioManager or itself (since it in turn holds a reference to the manager), |
65 // for delayed tasks as it can slow down or even prevent normal shut down. | 57 // for delayed tasks as it can slow down or even prevent normal shut down. |
66 // So, for tasks on the audio thread, the controller uses WeakPtr which enables | 58 // So, for tasks on the audio thread, the controller uses WeakPtr which enables |
67 // us to safely cancel pending polling tasks. | 59 // us to safely cancel pending polling tasks. |
68 // The owner of the audio thread, AudioManager, will take care of properly | 60 // The owner of the audio thread, AudioManager, will take care of properly |
69 // shutting it down. | 61 // shutting it down. |
(...skipping 12 matching lines...) Expand all Loading... | |
82 | 74 |
83 // An event handler that receives events from the AudioOutputController. The | 75 // An event handler that receives events from the AudioOutputController. The |
84 // following methods are called on the audio controller thread. | 76 // following methods are called on the audio controller thread. |
85 class MEDIA_EXPORT EventHandler { | 77 class MEDIA_EXPORT EventHandler { |
86 public: | 78 public: |
87 virtual ~EventHandler() {} | 79 virtual ~EventHandler() {} |
88 virtual void OnCreated(AudioOutputController* controller) = 0; | 80 virtual void OnCreated(AudioOutputController* controller) = 0; |
89 virtual void OnPlaying(AudioOutputController* controller) = 0; | 81 virtual void OnPlaying(AudioOutputController* controller) = 0; |
90 virtual void OnPaused(AudioOutputController* controller) = 0; | 82 virtual void OnPaused(AudioOutputController* controller) = 0; |
91 virtual void OnError(AudioOutputController* controller, int error_code) = 0; | 83 virtual void OnError(AudioOutputController* controller, int error_code) = 0; |
92 | |
93 // Audio controller asks for more data. | |
94 // |pending_bytes| is the number of bytes still on the controller. | |
95 // |timestamp| is then time when |pending_bytes| is recorded. | |
96 virtual void OnMoreData(AudioOutputController* controller, | |
97 AudioBuffersState buffers_state) = 0; | |
98 }; | 84 }; |
99 | 85 |
100 // A synchronous reader interface used by AudioOutputController for | 86 // A synchronous reader interface used by AudioOutputController for |
101 // synchronous reading. | 87 // synchronous reading. |
102 class SyncReader { | 88 class SyncReader { |
103 public: | 89 public: |
104 virtual ~SyncReader() {} | 90 virtual ~SyncReader() {} |
105 | 91 |
106 // Notify the synchronous reader the number of bytes in the | 92 // Notify the synchronous reader the number of bytes in the |
107 // AudioOutputController not yet played. This is used by SyncReader to | 93 // AudioOutputController not yet played. This is used by SyncReader to |
(...skipping 16 matching lines...) Expand all Loading... | |
124 }; | 110 }; |
125 | 111 |
126 // Factory method for creating an AudioOutputController. | 112 // Factory method for creating an AudioOutputController. |
127 // If successful, an audio controller thread is created. The audio device | 113 // If successful, an audio controller thread is created. The audio device |
128 // will be created on the audio controller thread and when that is done | 114 // will be created on the audio controller thread and when that is done |
129 // event handler will receive a OnCreated() call. | 115 // event handler will receive a OnCreated() call. |
130 static scoped_refptr<AudioOutputController> Create( | 116 static scoped_refptr<AudioOutputController> Create( |
131 AudioManager* audio_manager, | 117 AudioManager* audio_manager, |
132 EventHandler* event_handler, | 118 EventHandler* event_handler, |
133 const AudioParameters& params, | 119 const AudioParameters& params, |
134 // Soft limit for buffer capacity in this controller. This parameter | |
135 // is used only in regular latency mode. | |
136 uint32 buffer_capacity); | |
137 | |
138 // Factory method for creating a low latency audio stream. | |
139 static scoped_refptr<AudioOutputController> CreateLowLatency( | |
140 AudioManager* audio_manager, | |
141 EventHandler* event_handler, | |
142 const AudioParameters& params, | |
143 // External synchronous reader for audio controller. | 120 // External synchronous reader for audio controller. |
144 SyncReader* sync_reader); | 121 SyncReader* sync_reader); |
145 | 122 |
146 // Methods to control playback of the stream. | 123 // Methods to control playback of the stream. |
147 | 124 |
148 // Starts the playback of this audio output stream. | 125 // Starts the playback of this audio output stream. |
149 void Play(); | 126 void Play(); |
150 | 127 |
151 // Pause this audio output stream. | 128 // Pause this audio output stream. |
152 void Pause(); | 129 void Pause(); |
153 | 130 |
154 // Discard all audio data buffered in this output stream. This method only | 131 // Discard all audio data buffered in this output stream. This method only |
155 // has effect when the stream is paused. | 132 // has effect when the stream is paused. |
156 void Flush(); | 133 void Flush(); |
157 | 134 |
158 // Closes the audio output stream. The state is changed and the resources | 135 // Closes the audio output stream. The state is changed and the resources |
159 // are freed on the audio thread. closed_task is executed after that. | 136 // are freed on the audio thread. closed_task is executed after that. |
160 // Callbacks (EventHandler and SyncReader) must exist until closed_task is | 137 // Callbacks (EventHandler and SyncReader) must exist until closed_task is |
161 // called. | 138 // called. |
162 // | 139 // |
163 // It is safe to call this method more than once. Calls after the first one | 140 // It is safe to call this method more than once. Calls after the first one |
164 // will have no effect. | 141 // will have no effect. |
165 void Close(const base::Closure& closed_task); | 142 void Close(const base::Closure& closed_task); |
166 | 143 |
167 // Sets the volume of the audio output stream. | 144 // Sets the volume of the audio output stream. |
168 void SetVolume(double volume); | 145 void SetVolume(double volume); |
169 | 146 |
170 // Enqueue audio |data| into the controller. This method is used only in | |
171 // the regular latency mode and it is illegal to call this method when | |
172 // SyncReader is present. | |
173 void EnqueueData(const uint8* data, uint32 size); | |
174 | |
175 bool LowLatencyMode() const { return sync_reader_ != NULL; } | |
176 | |
177 /////////////////////////////////////////////////////////////////////////// | 147 /////////////////////////////////////////////////////////////////////////// |
178 // AudioSourceCallback methods. | 148 // AudioSourceCallback methods. |
179 virtual uint32 OnMoreData(AudioOutputStream* stream, | 149 virtual uint32 OnMoreData(AudioOutputStream* stream, |
180 uint8* dest, | 150 uint8* dest, |
181 uint32 max_size, | 151 uint32 max_size, |
182 AudioBuffersState buffers_state) OVERRIDE; | 152 AudioBuffersState buffers_state) OVERRIDE; |
183 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; | 153 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; |
184 virtual void WaitTillDataReady() OVERRIDE; | 154 virtual void WaitTillDataReady() OVERRIDE; |
185 | 155 |
186 protected: | 156 protected: |
(...skipping 12 matching lines...) Expand all Loading... | |
199 friend class base::RefCountedThreadSafe<AudioOutputController>; | 169 friend class base::RefCountedThreadSafe<AudioOutputController>; |
200 virtual ~AudioOutputController(); | 170 virtual ~AudioOutputController(); |
201 | 171 |
202 private: | 172 private: |
203 // We are polling sync reader if data became available. | 173 // We are polling sync reader if data became available. |
204 static const int kPollNumAttempts; | 174 static const int kPollNumAttempts; |
205 static const int kPollPauseInMilliseconds; | 175 static const int kPollPauseInMilliseconds; |
206 | 176 |
207 AudioOutputController(AudioManager* audio_manager, | 177 AudioOutputController(AudioManager* audio_manager, |
208 EventHandler* handler, | 178 EventHandler* handler, |
209 uint32 capacity, SyncReader* sync_reader); | 179 SyncReader* sync_reader); |
210 | 180 |
211 // The following methods are executed on the audio controller thread. | 181 // The following methods are executed on the audio controller thread. |
212 void DoCreate(const AudioParameters& params); | 182 void DoCreate(const AudioParameters& params); |
213 void DoPlay(); | 183 void DoPlay(); |
214 void PollAndStartIfDataReady(); | 184 void PollAndStartIfDataReady(); |
215 void DoPause(); | 185 void DoPause(); |
216 void DoFlush(); | 186 void DoFlush(); |
217 void DoClose(const base::Closure& closed_task); | 187 void DoClose(const base::Closure& closed_task); |
218 void DoSetVolume(double volume); | 188 void DoSetVolume(double volume); |
219 void DoReportError(int code); | 189 void DoReportError(int code); |
220 | 190 |
221 // Helper method to submit a OnMoreData() call to the event handler. | |
222 void SubmitOnMoreData_Locked(); | |
223 | |
224 // Helper method that starts physical stream. | 191 // Helper method that starts physical stream. |
225 void StartStream(); | 192 void StartStream(); |
226 | 193 |
227 // Helper method that stops, closes, and NULLs |*stream_|. | 194 // Helper method that stops, closes, and NULLs |*stream_|. |
228 // Signals event when done if it is not NULL. | 195 // Signals event when done if it is not NULL. |
229 void DoStopCloseAndClearStream(base::WaitableEvent *done); | 196 void DoStopCloseAndClearStream(base::WaitableEvent *done); |
230 | 197 |
231 scoped_refptr<AudioManager> audio_manager_; | 198 scoped_refptr<AudioManager> audio_manager_; |
232 // |handler_| may be called only if |state_| is not kClosed. | 199 // |handler_| may be called only if |state_| is not kClosed. |
233 EventHandler* handler_; | 200 EventHandler* handler_; |
234 AudioOutputStream* stream_; | 201 AudioOutputStream* stream_; |
235 | 202 |
236 // The current volume of the audio stream. | 203 // The current volume of the audio stream. |
237 double volume_; | 204 double volume_; |
238 | 205 |
239 // |state_| is written on the audio controller thread and is read on the | 206 // |state_| is written on the audio controller thread and is read on the |
240 // hardware audio thread. These operations need to be locked. But lock | 207 // hardware audio thread. These operations need to be locked. But lock |
241 // is not required for reading on the audio controller thread. | 208 // is not required for reading on the audio controller thread. |
242 State state_; | 209 State state_; |
243 | 210 |
244 AudioBuffersState buffers_state_; | 211 // The |lock_| must be acquired whenever we access |state_| from a thread |
245 | 212 // other than the audio controller thread. |
henrika (OOO until Aug 14)
2012/01/29 20:54:00
Rename to audio-manager thread?
vrk (LEFT CHROMIUM)
2012/01/31 23:53:08
Done.
| |
246 // The |lock_| must be acquired whenever we access |buffer_|. | |
247 base::Lock lock_; | 213 base::Lock lock_; |
248 | 214 |
249 media::SeekableBuffer buffer_; | |
250 | |
251 bool pending_request_; | |
252 | |
253 // SyncReader is used only in low latency mode for synchronous reading. | 215 // SyncReader is used only in low latency mode for synchronous reading. |
254 SyncReader* sync_reader_; | 216 SyncReader* sync_reader_; |
255 | 217 |
256 // The message loop of audio thread that this object runs on. | 218 // The message loop of audio thread that this object runs on. |
257 scoped_refptr<base::MessageLoopProxy> message_loop_; | 219 scoped_refptr<base::MessageLoopProxy> message_loop_; |
258 | 220 |
259 // When starting stream we wait for data to become available. | 221 // When starting stream we wait for data to become available. |
260 // Number of times left. | 222 // Number of times left. |
261 int number_polling_attempts_left_; | 223 int number_polling_attempts_left_; |
262 | 224 |
263 // Used to post delayed tasks to ourselves that we can cancel. | 225 // Used to post delayed tasks to ourselves that we can cancel. |
264 // We don't want the tasks to hold onto a reference as it will slow down | 226 // We don't want the tasks to hold onto a reference as it will slow down |
265 // shutdown and force it to wait for the most delayed task. | 227 // shutdown and force it to wait for the most delayed task. |
266 // Also, if we're shutting down, we do not want to poll for more data. | 228 // Also, if we're shutting down, we do not want to poll for more data. |
267 base::WeakPtrFactory<AudioOutputController> weak_this_; | 229 base::WeakPtrFactory<AudioOutputController> weak_this_; |
268 | 230 |
269 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); | 231 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); |
270 }; | 232 }; |
271 | 233 |
272 } // namespace media | 234 } // namespace media |
273 | 235 |
274 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 236 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
OLD | NEW |