Chromium Code Reviews| 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 class SourceSharingCallback; | |
|
DaleCurtis
2012/12/12 20:38:38
Don't nest, allows you to avoid huge method prefix
miu
2012/12/13 01:22:51
I removed this.
| |
| 181 | |
| 175 // We are polling sync reader if data became available. | 182 // We are polling sync reader if data became available. |
| 176 static const int kPollNumAttempts; | 183 static const int kPollNumAttempts; |
| 177 static const int kPollPauseInMilliseconds; | 184 static const int kPollPauseInMilliseconds; |
| 178 | 185 |
| 179 AudioOutputController(AudioManager* audio_manager, EventHandler* handler, | 186 AudioOutputController(AudioManager* audio_manager, EventHandler* handler, |
| 180 const AudioParameters& params, SyncReader* sync_reader); | 187 const AudioParameters& params, SyncReader* sync_reader); |
| 181 | 188 |
| 182 // The following methods are executed on the audio manager thread. | 189 // The following methods are executed on the audio manager thread. |
| 183 void DoCreate(); | 190 void DoCreate(); |
| 184 void DoPlay(); | 191 void DoPlay(); |
| 185 void PollAndStartIfDataReady(); | 192 void PollAndStartIfDataReady(); |
| 186 void DoPause(); | 193 void DoPause(); |
| 187 void DoFlush(); | 194 void DoFlush(); |
| 188 void DoClose(); | 195 void DoClose(); |
| 189 void DoSetVolume(double volume); | 196 void DoSetVolume(double volume); |
| 190 void DoReportError(int code); | 197 void DoReportError(int code); |
| 191 | 198 |
| 192 // Helper method that starts physical stream. | 199 // Helper method that starts physical stream. |
| 193 void StartStream(); | 200 void StartStream(); |
| 194 | 201 |
| 195 // Helper method that stops, closes, and NULLs |*stream_|. | 202 // Helper method that stops, closes, and NULLs |*stream_|. |
| 196 // Signals event when done if it is not NULL. | 203 // Signals event when done if it is not NULL. |
| 197 void DoStopCloseAndClearStream(base::WaitableEvent *done); | 204 void DoStopCloseAndClearStream(base::WaitableEvent *done); |
| 198 | 205 |
| 199 AudioManager* audio_manager_; | 206 AudioManager* const audio_manager_; |
| 207 const AudioParameters params_; | |
| 200 | 208 |
| 201 // |handler_| may be called only if |state_| is not kClosed. | 209 // |handler_| may be called only if |state_| is not kClosed. |
| 202 EventHandler* handler_; | 210 EventHandler* handler_; |
| 211 | |
| 212 // AudioManager-owned stream. | |
| 203 AudioOutputStream* stream_; | 213 AudioOutputStream* stream_; |
| 204 | 214 |
| 215 // Consumers of audio data which share sync_reader_. | |
| 216 scoped_refptr<SourceSharingCallback> callback_for_stream_; | |
| 217 scoped_refptr<SourceSharingCallback> callback_for_divert_; | |
| 218 | |
| 205 // The current volume of the audio stream. | 219 // The current volume of the audio stream. |
| 206 double volume_; | 220 double volume_; |
| 207 | 221 |
| 208 // |state_| is written on the audio manager thread and is read on the | 222 // |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 | 223 // hardware audio thread. These operations need to be locked. But lock |
| 210 // is not required for reading on the audio manager thread. | 224 // is not required for reading on the audio manager thread. |
| 211 State state_; | 225 State state_; |
| 212 | 226 |
| 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. | 227 // SyncReader is used only in low latency mode for synchronous reading. |
| 218 SyncReader* sync_reader_; | 228 SyncReader* sync_reader_; |
| 219 | 229 |
| 220 // The message loop of audio manager thread that this object runs on. | 230 // The message loop of audio manager thread that this object runs on. |
| 221 scoped_refptr<base::MessageLoopProxy> message_loop_; | 231 scoped_refptr<base::MessageLoopProxy> message_loop_; |
| 222 | 232 |
| 223 // When starting stream we wait for data to become available. | 233 // When starting stream we wait for data to become available. |
| 224 // Number of times left. | 234 // Number of times left. |
| 225 int number_polling_attempts_left_; | 235 int number_polling_attempts_left_; |
| 226 | 236 |
| 227 AudioParameters params_; | |
| 228 | |
| 229 // Used to post delayed tasks to ourselves that we can cancel. | 237 // 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 | 238 // 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. | 239 // 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. | 240 // Also, if we're shutting down, we do not want to poll for more data. |
| 233 base::WeakPtrFactory<AudioOutputController> weak_this_; | 241 base::WeakPtrFactory<AudioOutputController> weak_this_; |
| 234 | 242 |
| 235 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); | 243 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); |
| 236 }; | 244 }; |
| 237 | 245 |
| 238 } // namespace media | 246 } // namespace media |
| 239 | 247 |
| 240 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 248 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
| OLD | NEW |