| 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 "base/threading/thread_checker.h" |
| 12 #include "media/audio/audio_buffers_state.h" | 13 #include "media/audio/audio_buffers_state.h" |
| 13 #include "media/audio/audio_io.h" | 14 #include "media/audio/audio_io.h" |
| 14 #include "media/audio/audio_manager.h" | 15 #include "media/audio/audio_manager.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 |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 AudioBuffersState buffers_state) OVERRIDE; | 148 AudioBuffersState buffers_state) OVERRIDE; |
| 148 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; | 149 virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE; |
| 149 virtual void WaitTillDataReady() OVERRIDE; | 150 virtual void WaitTillDataReady() OVERRIDE; |
| 150 | 151 |
| 151 // AudioDeviceListener implementation. When called AudioOutputController will | 152 // AudioDeviceListener implementation. When called AudioOutputController will |
| 152 // shutdown the existing |stream_|, transition to the kRecreating state, | 153 // shutdown the existing |stream_|, transition to the kRecreating state, |
| 153 // create a new stream, and then transition back to an equivalent state prior | 154 // create a new stream, and then transition back to an equivalent state prior |
| 154 // to being called. | 155 // to being called. |
| 155 virtual void OnDeviceChange() OVERRIDE; | 156 virtual void OnDeviceChange() OVERRIDE; |
| 156 | 157 |
| 158 // Accessor to audio output parameters. |
| 159 const AudioParameters& params() const { return params_; } |
| 160 |
| 161 // Stops the normal audio output stream and begins providing audio data to |
| 162 // another destination. |to_stream| remains under complete control of |
| 163 // AudioOutputController. |
| 164 void StartDiverting(AudioOutputStream* to_stream); |
| 165 |
| 166 // Restores normal audio output behavior. The stream that was provided in the |
| 167 // previous call to StartDiverting() is closed at some point by |
| 168 // AudioOutputController. |
| 169 void StopDiverting(); |
| 170 |
| 157 protected: | 171 protected: |
| 158 // Internal state of the source. | 172 // Internal state of the source. |
| 159 enum State { | 173 enum State { |
| 160 kEmpty, | 174 kEmpty, |
| 161 kCreated, | 175 kCreated, |
| 162 kPlaying, | 176 kPlaying, |
| 163 kStarting, | 177 kStarting, |
| 164 kPausedWhenStarting, | 178 kPausedWhenStarting, |
| 165 kPaused, | 179 kPaused, |
| 166 kClosed, | 180 kClosed, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 189 void DoSetVolume(double volume); | 203 void DoSetVolume(double volume); |
| 190 void DoReportError(int code); | 204 void DoReportError(int code); |
| 191 | 205 |
| 192 // Helper method that starts physical stream. | 206 // Helper method that starts physical stream. |
| 193 void StartStream(); | 207 void StartStream(); |
| 194 | 208 |
| 195 // Helper method that stops, closes, and NULLs |*stream_|. | 209 // Helper method that stops, closes, and NULLs |*stream_|. |
| 196 // Signals event when done if it is not NULL. | 210 // Signals event when done if it is not NULL. |
| 197 void DoStopCloseAndClearStream(base::WaitableEvent *done); | 211 void DoStopCloseAndClearStream(base::WaitableEvent *done); |
| 198 | 212 |
| 199 AudioManager* audio_manager_; | 213 AudioManager* const audio_manager_; |
| 214 const AudioParameters params_; |
| 200 | 215 |
| 201 // |handler_| may be called only if |state_| is not kClosed. | 216 // |handler_| may be called only if |state_| is not kClosed. |
| 202 EventHandler* handler_; | 217 EventHandler* handler_; |
| 218 |
| 219 // Currently open stream. |
| 203 AudioOutputStream* stream_; | 220 AudioOutputStream* stream_; |
| 204 | 221 |
| 222 // When non-NULL, audio is being diverted to this stream. |
| 223 AudioOutputStream* diverting_to_stream_; |
| 224 |
| 225 // Checks that the same single outside thread is calling StartDiverting() and |
| 226 // StopDiverting(). |
| 227 base::ThreadChecker divert_thread_checker_; |
| 228 |
| 205 // The current volume of the audio stream. | 229 // The current volume of the audio stream. |
| 206 double volume_; | 230 double volume_; |
| 207 | 231 |
| 208 // |state_| is written on the audio manager thread and is read on the | 232 // |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 | 233 // hardware audio thread. These operations need to be locked. But lock |
| 210 // is not required for reading on the audio manager thread. | 234 // is not required for reading on the audio manager thread. |
| 211 State state_; | 235 State state_; |
| 212 | 236 |
| 213 // The |lock_| must be acquired whenever we access |state_| from a thread | 237 // The |lock_| must be acquired whenever we access |state_| from a thread |
| 214 // other than the audio manager thread. | 238 // other than the audio manager thread. |
| 215 base::Lock lock_; | 239 base::Lock lock_; |
| 216 | 240 |
| 217 // SyncReader is used only in low latency mode for synchronous reading. | 241 // SyncReader is used only in low latency mode for synchronous reading. |
| 218 SyncReader* sync_reader_; | 242 SyncReader* sync_reader_; |
| 219 | 243 |
| 220 // The message loop of audio manager thread that this object runs on. | 244 // The message loop of audio manager thread that this object runs on. |
| 221 scoped_refptr<base::MessageLoopProxy> message_loop_; | 245 scoped_refptr<base::MessageLoopProxy> message_loop_; |
| 222 | 246 |
| 223 // When starting stream we wait for data to become available. | 247 // When starting stream we wait for data to become available. |
| 224 // Number of times left. | 248 // Number of times left. |
| 225 int number_polling_attempts_left_; | 249 int number_polling_attempts_left_; |
| 226 | 250 |
| 227 AudioParameters params_; | |
| 228 | |
| 229 // Used to post delayed tasks to ourselves that we can cancel. | 251 // 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 | 252 // 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. | 253 // 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. | 254 // Also, if we're shutting down, we do not want to poll for more data. |
| 233 base::WeakPtrFactory<AudioOutputController> weak_this_; | 255 base::WeakPtrFactory<AudioOutputController> weak_this_; |
| 234 | 256 |
| 235 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); | 257 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); |
| 236 }; | 258 }; |
| 237 | 259 |
| 238 } // namespace media | 260 } // namespace media |
| 239 | 261 |
| 240 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 262 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
| OLD | NEW |