Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
| 12 #include "base/time.h" | 13 #include "base/time.h" |
| 13 #include "media/audio/audio_buffers_state.h" | 14 #include "media/audio/audio_buffers_state.h" |
| 14 #include "media/audio/audio_io.h" | 15 #include "media/audio/audio_io.h" |
| 15 #include "media/audio/audio_manager.h" | 16 #include "media/audio/audio_manager.h" |
| 16 #include "media/audio/simple_sources.h" | 17 #include "media/audio/simple_sources.h" |
| 17 | 18 |
| 18 class MessageLoop; | 19 class MessageLoop; |
| 19 | 20 |
| 20 // An AudioOutputController controls an AudioOutputStream and provides data | 21 // An AudioOutputController controls an AudioOutputStream and provides data |
| 21 // to this output stream. It has an important function that it executes | 22 // to this output stream. It has an important function that it executes |
| 22 // audio operations like play, pause, stop, etc. on a separate thread, | 23 // audio operations like play, pause, stop, etc. on a separate thread, |
| 23 // namely the audio controller thread. | 24 // namely the audio controller thread. |
| 24 // | 25 // |
| 25 // All the public methods of AudioOutputController are non-blocking except | 26 // All the public methods of AudioOutputController are non-blocking. |
| 26 // close, the actual operations are performed on the audio controller thread. | 27 // The actual operations are performed on the audio thread. |
| 27 // | 28 // |
| 28 // Here is a state diagram for the AudioOutputController for default low | 29 // Here is a state diagram for the AudioOutputController for default low |
| 29 // latency mode; in normal latency mode there is no "starting" or "paused when | 30 // latency mode; in normal latency mode there is no "starting" or "paused when |
| 30 // starting" states, "created" immediately switches to "playing": | 31 // starting" states, "created" immediately switches to "playing": |
| 31 // | 32 // |
| 32 // .-----------------------> [ Closed / Error ] <------. | 33 // .-----------------------> [ Closed / Error ] <------. |
| 33 // | ^ | | 34 // | ^ | |
| 34 // | | | | 35 // | | | |
| 35 // [ Created ] --> [ Starting ] --> [ Playing ] --> [ Paused ] | 36 // [ Created ] --> [ Starting ] --> [ Playing ] --> [ Paused ] |
| 36 // ^ | ^ | ^ | 37 // ^ | ^ | ^ |
| 37 // | | | | | | 38 // | | | | | |
| 38 // | | `----------------' | | 39 // | | `----------------' | |
| 39 // | V | | 40 // | V | |
| 40 // | [ PausedWhenStarting ] ------------------------' | 41 // | [ PausedWhenStarting ] ------------------------' |
| 41 // | | 42 // | |
| 42 // *[ Empty ] | 43 // *[ Empty ] |
| 43 // | 44 // |
| 44 // * Initial state | 45 // * Initial state |
| 45 // | 46 // |
| 46 // There are two modes of buffering operations supported by this class. | 47 // There are two modes of buffering operations supported by this class. |
| 47 // | 48 // |
| 48 // Regular latency mode: | 49 // Regular latency mode: |
| 49 // In this mode we receive signals from AudioOutputController and then we | 50 // In this mode we receive signals from AudioOutputController and then we |
| 50 // enqueue data into it. | 51 // enqueue data into it. |
| 51 // | 52 // |
| 52 // Low latency mode: | 53 // Low latency mode: |
| 53 // In this mode a DataSource object is given to the AudioOutputController | 54 // In this mode a DataSource object is given to the AudioOutputController |
| 54 // and AudioOutputController reads from it synchronously. | 55 // and AudioOutputController reads from it synchronously. |
| 55 // | 56 // |
| 57 // The audio thread itself is owned by the AudioManager that the | |
| 58 // AudioOutputController holds a reference to. When performing tasks on the | |
| 59 // audio thread, the controller must not add or release references to the | |
| 60 // AudioManager or itself (since it in turn holds a reference to the manager), | |
| 61 // for delayed tasks as it can slow down or even prevent normal shut down. | |
| 62 // So, for tasks on the audio thread, the controller uses WeakPtr which enables | |
| 63 // us to safely cancel pending polling tasks. | |
| 64 // The owner of the audio thread, AudioManager, will take care of properly | |
| 65 // shutting it down. | |
| 66 // | |
| 56 #include "media/base/media_export.h" | 67 #include "media/base/media_export.h" |
| 57 | 68 |
| 58 namespace media { | 69 namespace media { |
| 59 | 70 |
| 60 class MEDIA_EXPORT AudioOutputController | 71 class MEDIA_EXPORT AudioOutputController |
| 61 : public base::RefCountedThreadSafe<AudioOutputController>, | 72 : public base::RefCountedThreadSafe<AudioOutputController>, |
| 62 public AudioOutputStream::AudioSourceCallback { | 73 public AudioOutputStream::AudioSourceCallback { |
| 63 public: | 74 public: |
| 64 // Value sent by the controller to the renderer in low-latency mode | 75 // Value sent by the controller to the renderer in low-latency mode |
| 65 // indicating that the stream is paused. | 76 // indicating that the stream is paused. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 virtual bool DataReady() = 0; | 119 virtual bool DataReady() = 0; |
| 109 }; | 120 }; |
| 110 | 121 |
| 111 virtual ~AudioOutputController(); | 122 virtual ~AudioOutputController(); |
| 112 | 123 |
| 113 // Factory method for creating an AudioOutputController. | 124 // Factory method for creating an AudioOutputController. |
| 114 // If successful, an audio controller thread is created. The audio device | 125 // If successful, an audio controller thread is created. The audio device |
| 115 // will be created on the audio controller thread and when that is done | 126 // will be created on the audio controller thread and when that is done |
| 116 // event handler will receive a OnCreated() call. | 127 // event handler will receive a OnCreated() call. |
| 117 static scoped_refptr<AudioOutputController> Create( | 128 static scoped_refptr<AudioOutputController> Create( |
| 129 AudioManager* audio_manager, | |
| 118 EventHandler* event_handler, | 130 EventHandler* event_handler, |
| 119 const AudioParameters& params, | 131 const AudioParameters& params, |
| 120 // Soft limit for buffer capacity in this controller. This parameter | 132 // Soft limit for buffer capacity in this controller. This parameter |
| 121 // is used only in regular latency mode. | 133 // is used only in regular latency mode. |
| 122 uint32 buffer_capacity); | 134 uint32 buffer_capacity); |
| 123 | 135 |
| 124 // Factory method for creating a low latency audio stream. | 136 // Factory method for creating a low latency audio stream. |
| 125 static scoped_refptr<AudioOutputController> CreateLowLatency( | 137 static scoped_refptr<AudioOutputController> CreateLowLatency( |
| 138 AudioManager* audio_manager, | |
| 126 EventHandler* event_handler, | 139 EventHandler* event_handler, |
| 127 const AudioParameters& params, | 140 const AudioParameters& params, |
| 128 // External synchronous reader for audio controller. | 141 // External synchronous reader for audio controller. |
| 129 SyncReader* sync_reader); | 142 SyncReader* sync_reader); |
| 130 | 143 |
| 131 // Methods to control playback of the stream. | 144 // Methods to control playback of the stream. |
| 132 | 145 |
| 133 // Starts the playback of this audio output stream. | 146 // Starts the playback of this audio output stream. |
| 134 void Play(); | 147 void Play(); |
| 135 | 148 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 kPaused, | 192 kPaused, |
| 180 kClosed, | 193 kClosed, |
| 181 kError, | 194 kError, |
| 182 }; | 195 }; |
| 183 | 196 |
| 184 private: | 197 private: |
| 185 // We are polling sync reader if data became available. | 198 // We are polling sync reader if data became available. |
| 186 static const int kPollNumAttempts; | 199 static const int kPollNumAttempts; |
| 187 static const int kPollPauseInMilliseconds; | 200 static const int kPollPauseInMilliseconds; |
| 188 | 201 |
| 189 AudioOutputController(EventHandler* handler, | 202 AudioOutputController(AudioManager* audio_manager, |
| 203 EventHandler* handler, | |
| 190 uint32 capacity, SyncReader* sync_reader); | 204 uint32 capacity, SyncReader* sync_reader); |
| 191 | 205 |
| 192 // The following methods are executed on the audio controller thread. | 206 // The following methods are executed on the audio controller thread. |
| 193 void DoCreate(const AudioParameters& params); | 207 void DoCreate(const AudioParameters& params); |
| 194 void DoPlay(); | 208 void DoPlay(); |
| 195 void PollAndStartIfDataReady(); | 209 void PollAndStartIfDataReady(); |
| 196 void DoPause(); | 210 void DoPause(); |
| 197 void DoFlush(); | 211 void DoFlush(); |
| 198 void DoClose(const base::Closure& closed_task); | 212 void DoClose(const base::Closure& closed_task); |
| 199 void DoSetVolume(double volume); | 213 void DoSetVolume(double volume); |
| 200 void DoReportError(int code); | 214 void DoReportError(int code); |
| 201 | 215 |
| 202 // Helper method to submit a OnMoreData() call to the event handler. | 216 // Helper method to submit a OnMoreData() call to the event handler. |
| 203 void SubmitOnMoreData_Locked(); | 217 void SubmitOnMoreData_Locked(); |
| 204 | 218 |
| 205 // Helper method that starts physical stream. | 219 // Helper method that starts physical stream. |
| 206 void StartStream(); | 220 void StartStream(); |
| 207 | 221 |
| 208 // Helper method that stops, closes, and NULLs |*stream_|. | 222 // Helper method that stops, closes, and NULLs |*stream_|. |
| 209 void StopCloseAndClearStream(); | 223 void StopCloseAndClearStream(); |
| 210 | 224 |
| 225 scoped_refptr<AudioManager> audio_manager_; | |
| 211 // |handler_| may be called only if |state_| is not kClosed. | 226 // |handler_| may be called only if |state_| is not kClosed. |
| 212 EventHandler* handler_; | 227 EventHandler* handler_; |
| 213 AudioOutputStream* stream_; | 228 AudioOutputStream* stream_; |
| 214 | 229 |
| 215 // The current volume of the audio stream. | 230 // The current volume of the audio stream. |
| 216 double volume_; | 231 double volume_; |
| 217 | 232 |
| 218 // |state_| is written on the audio controller thread and is read on the | 233 // |state_| is written on the audio controller thread and is read on the |
| 219 // hardware audio thread. These operations need to be locked. But lock | 234 // hardware audio thread. These operations need to be locked. But lock |
| 220 // is not required for reading on the audio controller thread. | 235 // is not required for reading on the audio controller thread. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 232 // SyncReader is used only in low latency mode for synchronous reading. | 247 // SyncReader is used only in low latency mode for synchronous reading. |
| 233 SyncReader* sync_reader_; | 248 SyncReader* sync_reader_; |
| 234 | 249 |
| 235 // The message loop of audio thread that this object runs on. | 250 // The message loop of audio thread that this object runs on. |
| 236 MessageLoop* message_loop_; | 251 MessageLoop* message_loop_; |
| 237 | 252 |
| 238 // When starting stream we wait for data to become available. | 253 // When starting stream we wait for data to become available. |
| 239 // Number of times left. | 254 // Number of times left. |
| 240 int number_polling_attempts_left_; | 255 int number_polling_attempts_left_; |
| 241 | 256 |
| 257 // Used to post delayed tasks to ourselves that we can cancel. | |
| 258 // We don't want the tasks to hold onto a reference as it will slow down | |
| 259 // shutdown and force it to wait for the most delayed task. | |
|
scherkus (not reviewing)
2011/12/09 22:47:30
remove extra space
tommi (sloooow) - chröme
2011/12/10 00:11:14
Done.
| |
| 260 // Also, if we're shutting down, we do not want to poll for more data. | |
| 261 base::WeakPtrFactory<AudioOutputController> weak_this_; | |
| 262 | |
| 242 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); | 263 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); |
| 243 }; | 264 }; |
| 244 | 265 |
| 245 } // namespace media | 266 } // namespace media |
| 246 | 267 |
| 247 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 268 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
| OLD | NEW |