| 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" |
| 11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
| 12 #include "media/audio/audio_buffers_state.h" | 12 #include "media/audio/audio_buffers_state.h" |
| 13 #include "media/audio/audio_io.h" | 13 #include "media/audio/audio_io.h" |
| 14 #include "media/audio/audio_manager.h" | 14 #include "media/audio/audio_manager.h" |
| 15 #include "media/audio/audio_source_diverter.h" |
| 15 #include "media/audio/simple_sources.h" | 16 #include "media/audio/simple_sources.h" |
| 16 #include "media/base/media_export.h" | 17 #include "media/base/media_export.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 class WaitableEvent; | 20 class WaitableEvent; |
| 20 } // namespace base | 21 } // namespace base |
| 21 | 22 |
| 22 class MessageLoop; | 23 class MessageLoop; |
| 23 | 24 |
| 24 // An AudioOutputController controls an AudioOutputStream and provides data | 25 // An AudioOutputController controls an AudioOutputStream and provides data |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 // | 60 // |
| 60 // Since AudioOutputController uses AudioManager's message loop the controller | 61 // Since AudioOutputController uses AudioManager's message loop the controller |
| 61 // uses WeakPtr to allow safe cancellation of pending tasks. | 62 // uses WeakPtr to allow safe cancellation of pending tasks. |
| 62 // | 63 // |
| 63 | 64 |
| 64 namespace media { | 65 namespace media { |
| 65 | 66 |
| 66 class MEDIA_EXPORT AudioOutputController | 67 class MEDIA_EXPORT AudioOutputController |
| 67 : public base::RefCountedThreadSafe<AudioOutputController>, | 68 : public base::RefCountedThreadSafe<AudioOutputController>, |
| 68 public AudioOutputStream::AudioSourceCallback, | 69 public AudioOutputStream::AudioSourceCallback, |
| 70 public AudioSourceDiverter, |
| 69 NON_EXPORTED_BASE(public AudioManager::AudioDeviceListener) { | 71 NON_EXPORTED_BASE(public AudioManager::AudioDeviceListener) { |
| 70 public: | 72 public: |
| 71 // An event handler that receives events from the AudioOutputController. The | 73 // An event handler that receives events from the AudioOutputController. The |
| 72 // following methods are called on the audio manager thread. | 74 // following methods are called on the audio manager thread. |
| 73 class MEDIA_EXPORT EventHandler { | 75 class MEDIA_EXPORT EventHandler { |
| 74 public: | 76 public: |
| 75 virtual void OnCreated(AudioOutputController* controller) = 0; | 77 virtual void OnCreated(AudioOutputController* controller) = 0; |
| 76 virtual void OnPlaying(AudioOutputController* controller) = 0; | 78 virtual void OnPlaying(AudioOutputController* controller) = 0; |
| 77 virtual void OnPaused(AudioOutputController* controller) = 0; | 79 virtual void OnPaused(AudioOutputController* controller) = 0; |
| 78 virtual void OnError(AudioOutputController* controller, int error_code) = 0; | 80 virtual void OnError(AudioOutputController* controller, int error_code) = 0; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 AudioBuffersState buffers_state) OVERRIDE; | 149 AudioBuffersState buffers_state) OVERRIDE; |
| 148 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; | 150 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; |
| 149 virtual void WaitTillDataReady() OVERRIDE; | 151 virtual void WaitTillDataReady() OVERRIDE; |
| 150 | 152 |
| 151 // AudioDeviceListener implementation. When called AudioOutputController will | 153 // AudioDeviceListener implementation. When called AudioOutputController will |
| 152 // shutdown the existing |stream_|, transition to the kRecreating state, | 154 // shutdown the existing |stream_|, transition to the kRecreating state, |
| 153 // create a new stream, and then transition back to an equivalent state prior | 155 // create a new stream, and then transition back to an equivalent state prior |
| 154 // to being called. | 156 // to being called. |
| 155 virtual void OnDeviceChange() OVERRIDE; | 157 virtual void OnDeviceChange() OVERRIDE; |
| 156 | 158 |
| 159 // AudioSourceDiverter implementation. |
| 160 virtual const AudioParameters& GetAudioParameters() OVERRIDE; |
| 161 virtual void StartDiverting(AudioOutputStream* to_stream) OVERRIDE; |
| 162 virtual void StopDiverting() OVERRIDE; |
| 163 |
| 157 protected: | 164 protected: |
| 158 // Internal state of the source. | 165 // Internal state of the source. |
| 159 enum State { | 166 enum State { |
| 160 kEmpty, | 167 kEmpty, |
| 161 kCreated, | 168 kCreated, |
| 162 kPlaying, | 169 kPlaying, |
| 163 kStarting, | 170 kStarting, |
| 164 kPausedWhenStarting, | 171 kPausedWhenStarting, |
| 165 kPaused, | 172 kPaused, |
| 166 kClosed, | 173 kClosed, |
| 167 kError, | 174 kError, |
| 168 kRecreating, | 175 kRecreating, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 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); |
| 198 void DoStartDiverting(AudioOutputStream* to_stream); |
| 199 void DoStopDiverting(); |
| 191 | 200 |
| 192 // Helper method that starts physical stream. | 201 // Helper method that starts physical stream. |
| 193 void StartStream(); | 202 void StartStream(); |
| 194 | 203 |
| 195 // Helper method that stops, closes, and NULLs |*stream_|. | 204 // Helper method that stops, closes, and NULLs |*stream_|. |
| 196 // Signals event when done if it is not NULL. | 205 // Signals event when done if it is not NULL. |
| 197 void DoStopCloseAndClearStream(base::WaitableEvent *done); | 206 void DoStopCloseAndClearStream(base::WaitableEvent *done); |
| 198 | 207 |
| 199 AudioManager* const audio_manager_; | 208 AudioManager* const audio_manager_; |
| 209 const AudioParameters params_; |
| 200 | 210 |
| 201 // |handler_| may be called only if |state_| is not kClosed. | 211 // |handler_| may be called only if |state_| is not kClosed. |
| 202 EventHandler* handler_; | 212 EventHandler* handler_; |
| 203 | 213 |
| 204 // Note: It's important to invalidate the weak pointers whenever stream_ is | 214 // Note: It's important to invalidate the weak pointers whenever stream_ is |
| 205 // changed. See comment for weak_this_. | 215 // changed. See comment for weak_this_. |
| 206 AudioOutputStream* stream_; | 216 AudioOutputStream* stream_; |
| 207 | 217 |
| 218 // When non-NULL, audio is being diverted to this stream. |
| 219 AudioOutputStream* diverting_to_stream_; |
| 220 |
| 208 // The current volume of the audio stream. | 221 // The current volume of the audio stream. |
| 209 double volume_; | 222 double volume_; |
| 210 | 223 |
| 211 // |state_| is written on the audio manager thread and is read on the | 224 // |state_| is written on the audio manager thread and is read on the |
| 212 // hardware audio thread. These operations need to be locked. But lock | 225 // hardware audio thread. These operations need to be locked. But lock |
| 213 // is not required for reading on the audio manager thread. | 226 // is not required for reading on the audio manager thread. |
| 214 State state_; | 227 State state_; |
| 215 | 228 |
| 216 // The |lock_| must be acquired whenever we access |state_| from a thread | 229 // The |lock_| must be acquired whenever we access |state_| from a thread |
| 217 // other than the audio manager thread. | 230 // other than the audio manager thread. |
| 218 base::Lock lock_; | 231 base::Lock lock_; |
| 219 | 232 |
| 220 // SyncReader is used only in low latency mode for synchronous reading. | 233 // SyncReader is used only in low latency mode for synchronous reading. |
| 221 SyncReader* sync_reader_; | 234 SyncReader* sync_reader_; |
| 222 | 235 |
| 223 // The message loop of audio manager thread that this object runs on. | 236 // The message loop of audio manager thread that this object runs on. |
| 224 scoped_refptr<base::MessageLoopProxy> message_loop_; | 237 scoped_refptr<base::MessageLoopProxy> message_loop_; |
| 225 | 238 |
| 226 // When starting stream we wait for data to become available. | 239 // When starting stream we wait for data to become available. |
| 227 // Number of times left. | 240 // Number of times left. |
| 228 int number_polling_attempts_left_; | 241 int number_polling_attempts_left_; |
| 229 | 242 |
| 230 AudioParameters params_; | |
| 231 | |
| 232 // Used to auto-cancel the delayed tasks that are created to poll for data | 243 // Used to auto-cancel the delayed tasks that are created to poll for data |
| 233 // (when starting-up a stream). | 244 // (when starting-up a stream). |
| 234 base::WeakPtrFactory<AudioOutputController> weak_this_; | 245 base::WeakPtrFactory<AudioOutputController> weak_this_; |
| 235 | 246 |
| 236 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); | 247 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); |
| 237 }; | 248 }; |
| 238 | 249 |
| 239 } // namespace media | 250 } // namespace media |
| 240 | 251 |
| 241 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 252 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
| OLD | NEW |