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/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 // passed to it via construction to synchronously fulfill this read request. | 58 // passed to it via construction to synchronously fulfill this read request. |
59 // | 59 // |
60 // Since AudioOutputController uses AudioManager's message loop the controller | 60 // Since AudioOutputController uses AudioManager's message loop the controller |
61 // uses WeakPtr to allow safe cancellation of pending tasks. | 61 // uses WeakPtr to allow safe cancellation of pending tasks. |
62 // | 62 // |
63 | 63 |
64 namespace media { | 64 namespace media { |
65 | 65 |
66 class MEDIA_EXPORT AudioOutputController | 66 class MEDIA_EXPORT AudioOutputController |
67 : public base::RefCountedThreadSafe<AudioOutputController>, | 67 : public base::RefCountedThreadSafe<AudioOutputController>, |
68 public AudioOutputStream::AudioSourceCallback, | |
69 NON_EXPORTED_BASE(public AudioManager::AudioDeviceListener) { | 68 NON_EXPORTED_BASE(public AudioManager::AudioDeviceListener) { |
70 public: | 69 public: |
71 // An event handler that receives events from the AudioOutputController. The | 70 // An event handler that receives events from the AudioOutputController. The |
72 // following methods are called on the audio manager thread. | 71 // following methods are called on the audio manager thread. |
73 class MEDIA_EXPORT EventHandler { | 72 class MEDIA_EXPORT EventHandler { |
74 public: | 73 public: |
75 virtual void OnCreated(AudioOutputController* controller) = 0; | 74 virtual void OnCreated(AudioOutputController* controller) = 0; |
76 virtual void OnPlaying(AudioOutputController* controller) = 0; | 75 virtual void OnPlaying(AudioOutputController* controller) = 0; |
77 virtual void OnPaused(AudioOutputController* controller) = 0; | 76 virtual void OnPaused(AudioOutputController* controller) = 0; |
78 virtual void OnError(AudioOutputController* controller, int error_code) = 0; | 77 virtual void OnError(AudioOutputController* controller, int error_code) = 0; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 // Callbacks (EventHandler and SyncReader) must exist until closed_task is | 131 // Callbacks (EventHandler and SyncReader) must exist until closed_task is |
133 // called. | 132 // called. |
134 // | 133 // |
135 // It is safe to call this method more than once. Calls after the first one | 134 // It is safe to call this method more than once. Calls after the first one |
136 // will have no effect. | 135 // will have no effect. |
137 void Close(const base::Closure& closed_task); | 136 void Close(const base::Closure& closed_task); |
138 | 137 |
139 // Sets the volume of the audio output stream. | 138 // Sets the volume of the audio output stream. |
140 void SetVolume(double volume); | 139 void SetVolume(double volume); |
141 | 140 |
142 // AudioSourceCallback implementation. | |
143 virtual int OnMoreData(AudioBus* dest, | |
144 AudioBuffersState buffers_state) OVERRIDE; | |
145 virtual int OnMoreIOData(AudioBus* source, | |
146 AudioBus* dest, | |
147 AudioBuffersState buffers_state) OVERRIDE; | |
148 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; | |
149 virtual void WaitTillDataReady() OVERRIDE; | |
150 | |
151 // AudioDeviceListener implementation. When called AudioOutputController will | 141 // AudioDeviceListener implementation. When called AudioOutputController will |
152 // shutdown the existing |stream_|, transition to the kRecreating state, | 142 // shutdown the existing |stream_|, transition to the kRecreating state, |
153 // create a new stream, and then transition back to an equivalent state prior | 143 // create a new stream, and then transition back to an equivalent state prior |
154 // to being called. | 144 // to being called. |
155 virtual void OnDeviceChange() OVERRIDE; | 145 virtual void OnDeviceChange() OVERRIDE; |
156 | 146 |
147 // Accessor to audio output parameters. | |
148 const AudioParameters& params() const { return params_; } | |
149 | |
150 // Stops the normal audio output stream and creates an AudioSourceCallback to | |
151 // provide audio data to some other destination. The given object will always | |
152 // return data when OnMoreData() is invoked, even if the underlying | |
153 // implementation is paused/stopped. AudioOutputController retains ownership | |
154 // of the returned object. | |
155 AudioOutputStream::AudioSourceCallback* Divert(); | |
156 | |
157 // Restores normal audio output behavior for the current playback state. | |
158 // |asc| is the pointer returned by the previous call to Divert() and becomes | |
159 // invalid once this method is called. | |
160 void Revert(AudioOutputStream::AudioSourceCallback* asc); | |
161 | |
157 protected: | 162 protected: |
158 // Internal state of the source. | 163 // Internal state of the source. |
159 enum State { | 164 enum State { |
160 kEmpty, | 165 kEmpty, |
161 kCreated, | 166 kCreated, |
162 kPlaying, | 167 kPlaying, |
163 kStarting, | 168 kStarting, |
164 kPausedWhenStarting, | 169 kPausedWhenStarting, |
165 kPaused, | 170 kPaused, |
166 kClosed, | 171 kClosed, |
167 kError, | 172 kError, |
168 kRecreating, | 173 kRecreating, |
169 }; | 174 }; |
170 | 175 |
171 friend class base::RefCountedThreadSafe<AudioOutputController>; | 176 friend class base::RefCountedThreadSafe<AudioOutputController>; |
172 virtual ~AudioOutputController(); | 177 virtual ~AudioOutputController(); |
173 | 178 |
174 private: | 179 private: |
180 // An AudioSourceCallback that allows passing around the underlying SyncReader | |
DaleCurtis
2012/12/12 01:29:15
Move to CC file?
miu
2012/12/12 03:13:39
Done.
| |
181 // data source between consumers of the data. It does this in a "pass or | |
182 // delayed pass" scheme that ensures synchronization between timing-sensitive | |
183 // threads is effectively non-blocking. In addition, Glue ensures that only | |
184 // one consumer is pulling data from the SyncReader at a time. | |
185 class Glue | |
DaleCurtis
2012/12/12 01:29:15
A better name would be nice.
miu
2012/12/12 03:13:39
Done. s/Glue/SourceSharingCallback/
| |
186 : public AudioOutputStream::AudioSourceCallback, | |
187 public base::RefCountedThreadSafe<Glue> { | |
DaleCurtis
2012/12/12 01:29:15
Why does this need refcounting?
miu
2012/12/12 03:13:39
An instance has to live until the "pass" completes
| |
188 public: | |
189 Glue(AudioOutputController* controller, SyncReader* source); | |
190 | |
191 // Pass the SyncReader to another instance as soon as possible. | |
192 void PassSoon(const scoped_refptr<Glue>& receiver); | |
193 | |
194 // AudioSourceCallback implementation. | |
195 virtual int OnMoreData(AudioBus* dest_bus, | |
196 AudioBuffersState buffers_state) OVERRIDE; | |
197 virtual int OnMoreIOData(AudioBus* source_bus, | |
198 AudioBus* dest_bus, | |
199 AudioBuffersState buffers_state) OVERRIDE; | |
200 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; | |
201 virtual void WaitTillDataReady() OVERRIDE; | |
202 | |
203 private: | |
204 friend class base::RefCountedThreadSafe<Glue>; | |
205 virtual ~Glue(); | |
206 | |
207 // Take over reading from |source|. | |
208 void ReceiveSource(SyncReader* source); | |
209 | |
210 // Executes a delayed pass (i.e., a pass that was delayed because source_ | |
211 // was in-use at the time PassSoon() was called). | |
212 void DoDelayedPassWithLockHeld(const scoped_refptr<Glue>& to); | |
213 | |
214 // Access to parent/owner instance. | |
215 AudioOutputController* const controller_; | |
216 | |
217 // Guards passing of SyncReader to another Glue instance. The | |
218 // implementation should acquire lock_ for only very short operations since | |
219 // audio threads are timing-sensitive. | |
220 base::Lock lock_; | |
221 | |
222 // Audio data source, or NULL if none yet. | |
223 SyncReader* source_; | |
224 | |
225 // True while source_ is in-use without lock_ held. | |
226 bool is_reading_from_source_; | |
227 | |
228 // When !delayed_pass_.is_null(), source_ is scheduled to be passed as soon | |
229 // as possible. | |
230 base::Closure delayed_pass_; | |
231 | |
232 DISALLOW_COPY_AND_ASSIGN(Glue); | |
233 }; | |
234 | |
175 // We are polling sync reader if data became available. | 235 // We are polling sync reader if data became available. |
176 static const int kPollNumAttempts; | 236 static const int kPollNumAttempts; |
177 static const int kPollPauseInMilliseconds; | 237 static const int kPollPauseInMilliseconds; |
178 | 238 |
179 AudioOutputController(AudioManager* audio_manager, EventHandler* handler, | 239 AudioOutputController(AudioManager* audio_manager, EventHandler* handler, |
180 const AudioParameters& params, SyncReader* sync_reader); | 240 const AudioParameters& params, SyncReader* sync_reader); |
181 | 241 |
182 // The following methods are executed on the audio manager thread. | 242 // The following methods are executed on the audio manager thread. |
183 void DoCreate(); | 243 void DoCreate(); |
184 void DoPlay(); | 244 void DoPlay(); |
185 void PollAndStartIfDataReady(); | 245 void PollAndStartIfDataReady(); |
186 void DoPause(); | 246 void DoPause(); |
187 void DoFlush(); | 247 void DoFlush(); |
188 void DoClose(); | 248 void DoClose(); |
189 void DoSetVolume(double volume); | 249 void DoSetVolume(double volume); |
190 void DoReportError(int code); | 250 void DoReportError(int code); |
191 | 251 |
192 // Helper method that starts physical stream. | 252 // Helper method that starts physical stream. |
193 void StartStream(); | 253 void StartStream(); |
194 | 254 |
195 // Helper method that stops, closes, and NULLs |*stream_|. | 255 // Helper method that stops, closes, and NULLs |*stream_|. |
196 // Signals event when done if it is not NULL. | 256 // Signals event when done if it is not NULL. |
197 void DoStopCloseAndClearStream(base::WaitableEvent *done); | 257 void DoStopCloseAndClearStream(base::WaitableEvent *done); |
198 | 258 |
199 AudioManager* audio_manager_; | 259 AudioManager* const audio_manager_; |
260 const AudioParameters params_; | |
200 | 261 |
201 // |handler_| may be called only if |state_| is not kClosed. | 262 // |handler_| may be called only if |state_| is not kClosed. |
202 EventHandler* handler_; | 263 EventHandler* handler_; |
264 | |
265 // AudioManager-owned stream. | |
203 AudioOutputStream* stream_; | 266 AudioOutputStream* stream_; |
204 | 267 |
268 // Consumers of audio data which share sync_reader_. | |
269 scoped_refptr<Glue> stream_glue_; | |
270 scoped_refptr<Glue> divert_glue_; | |
271 | |
205 // The current volume of the audio stream. | 272 // The current volume of the audio stream. |
206 double volume_; | 273 double volume_; |
207 | 274 |
208 // |state_| is written on the audio manager thread and is read on the | 275 // |state_| is written on the audio manager thread and is read on the |
209 // hardware audio thread. These operations need to be locked. But lock | 276 // hardware audio thread. These operations need to be locked. But lock |
210 // is not required for reading on the audio manager thread. | 277 // is not required for reading on the audio manager thread. |
211 State state_; | 278 State state_; |
212 | 279 |
213 // The |lock_| must be acquired whenever we access |state_| from a thread | |
214 // other than the audio manager thread. | |
215 base::Lock lock_; | |
216 | |
217 // SyncReader is used only in low latency mode for synchronous reading. | 280 // SyncReader is used only in low latency mode for synchronous reading. |
218 SyncReader* sync_reader_; | 281 SyncReader* sync_reader_; |
219 | 282 |
220 // The message loop of audio manager thread that this object runs on. | 283 // The message loop of audio manager thread that this object runs on. |
221 scoped_refptr<base::MessageLoopProxy> message_loop_; | 284 scoped_refptr<base::MessageLoopProxy> message_loop_; |
222 | 285 |
223 // When starting stream we wait for data to become available. | 286 // When starting stream we wait for data to become available. |
224 // Number of times left. | 287 // Number of times left. |
225 int number_polling_attempts_left_; | 288 int number_polling_attempts_left_; |
226 | 289 |
227 AudioParameters params_; | |
228 | |
229 // Used to post delayed tasks to ourselves that we can cancel. | 290 // Used to post delayed tasks to ourselves that we can cancel. |
230 // We don't want the tasks to hold onto a reference as it will slow down | 291 // We don't want the tasks to hold onto a reference as it will slow down |
231 // shutdown and force it to wait for the most delayed task. | 292 // shutdown and force it to wait for the most delayed task. |
232 // Also, if we're shutting down, we do not want to poll for more data. | 293 // Also, if we're shutting down, we do not want to poll for more data. |
233 base::WeakPtrFactory<AudioOutputController> weak_this_; | 294 base::WeakPtrFactory<AudioOutputController> weak_this_; |
234 | 295 |
235 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); | 296 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); |
236 }; | 297 }; |
237 | 298 |
238 } // namespace media | 299 } // namespace media |
239 | 300 |
240 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 301 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
OLD | NEW |