| 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/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "media/audio/audio_buffers_state.h" | 13 #include "media/audio/audio_buffers_state.h" |
| 14 #include "media/audio/audio_io.h" | 14 #include "media/audio/audio_io.h" |
| 15 #include "media/audio/audio_manager.h" | 15 #include "media/audio/audio_manager.h" |
| 16 #include "media/audio/simple_sources.h" | 16 #include "media/audio/simple_sources.h" |
| 17 | 17 |
| 18 class MessageLoop; | 18 class MessageLoop; |
| 19 | 19 |
| 20 // An AudioOutputController controls an AudioOutputStream and provides data | 20 // An AudioOutputController controls an AudioOutputStream and provides data |
| 21 // to this output stream. It has an important function that it executes | 21 // to this output stream. It has an important function that it executes |
| 22 // audio operations like play, pause, stop, etc. on a separate thread, | 22 // audio operations like play, pause, stop, etc. on a separate thread, |
| 23 // namely the audio controller thread. | 23 // namely the audio controller thread. |
| 24 // | 24 // |
| 25 // All the public methods of AudioOutputController are non-blocking except | 25 // All the public methods of AudioOutputController are non-blocking except |
| 26 // close, the actual operations are performed on the audio controller thread. | 26 // close, the actual operations are performed on the audio controller thread. |
| 27 // | 27 // |
| 28 // Here is a state diagram for the AudioOutputController: | 28 // 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 // starting" states, "created" immediately switches to "playing": |
| 29 // | 31 // |
| 30 // .----> [ Closed / Error ] <------. | 32 // .-----------------------> [ Closed / Error ] <------. |
| 31 // | ^ | | 33 // | ^ | |
| 32 // | | | | 34 // | | | |
| 33 // [ Created ] --> [ Playing ] --> [ Paused ] | 35 // [ Created ] --> [ Starting ] --> [ Playing ] --> [ Paused ] |
| 34 // ^ ^ | | 36 // ^ | ^ | ^ |
| 35 // | | | | 37 // | | | | | |
| 36 // *[ Empty ] `-----------------' | 38 // | | `----------------' | |
| 39 // | V | |
| 40 // | [ PausedWhenStarting ] ------------------------' |
| 41 // | |
| 42 // *[ Empty ] |
| 37 // | 43 // |
| 38 // * Initial state | 44 // * Initial state |
| 39 // | 45 // |
| 40 // There are two modes of buffering operations supported by this class. | 46 // There are two modes of buffering operations supported by this class. |
| 41 // | 47 // |
| 42 // Regular latency mode: | 48 // Regular latency mode: |
| 43 // In this mode we receive signals from AudioOutputController and then we | 49 // In this mode we receive signals from AudioOutputController and then we |
| 44 // enqueue data into it. | 50 // enqueue data into it. |
| 45 // | 51 // |
| 46 // Low latency mode: | 52 // Low latency mode: |
| 47 // In this mode a DataSource object is given to the AudioOutputController | 53 // In this mode a DataSource object is given to the AudioOutputController |
| 48 // and AudioOutputController reads from it synchronously. | 54 // and AudioOutputController reads from it synchronously. |
| 49 // | 55 // |
| 50 #include "media/base/media_export.h" | 56 #include "media/base/media_export.h" |
| 51 | 57 |
| 52 namespace media { | 58 namespace media { |
| 53 | 59 |
| 54 class MEDIA_EXPORT AudioOutputController | 60 class MEDIA_EXPORT AudioOutputController |
| 55 : public base::RefCountedThreadSafe<AudioOutputController>, | 61 : public base::RefCountedThreadSafe<AudioOutputController>, |
| 56 public AudioOutputStream::AudioSourceCallback { | 62 public AudioOutputStream::AudioSourceCallback { |
| 57 public: | 63 public: |
| 58 // Internal state of the source. | |
| 59 enum State { | |
| 60 kEmpty, | |
| 61 kCreated, | |
| 62 kPlaying, | |
| 63 kPaused, | |
| 64 kClosed, | |
| 65 kError, | |
| 66 }; | |
| 67 | |
| 68 // Value sent by the controller to the renderer in low-latency mode | 64 // Value sent by the controller to the renderer in low-latency mode |
| 69 // indicating that the stream is paused. | 65 // indicating that the stream is paused. |
| 70 static const int kPauseMark; | 66 static const int kPauseMark; |
| 71 | 67 |
| 72 // An event handler that receives events from the AudioOutputController. The | 68 // An event handler that receives events from the AudioOutputController. The |
| 73 // following methods are called on the audio controller thread. | 69 // following methods are called on the audio controller thread. |
| 74 class MEDIA_EXPORT EventHandler { | 70 class MEDIA_EXPORT EventHandler { |
| 75 public: | 71 public: |
| 76 virtual ~EventHandler() {} | 72 virtual ~EventHandler() {} |
| 77 virtual void OnCreated(AudioOutputController* controller) = 0; | 73 virtual void OnCreated(AudioOutputController* controller) = 0; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 void EnqueueData(const uint8* data, uint32 size); | 158 void EnqueueData(const uint8* data, uint32 size); |
| 163 | 159 |
| 164 bool LowLatencyMode() const { return sync_reader_ != NULL; } | 160 bool LowLatencyMode() const { return sync_reader_ != NULL; } |
| 165 | 161 |
| 166 /////////////////////////////////////////////////////////////////////////// | 162 /////////////////////////////////////////////////////////////////////////// |
| 167 // AudioSourceCallback methods. | 163 // AudioSourceCallback methods. |
| 168 virtual uint32 OnMoreData(AudioOutputStream* stream, uint8* dest, | 164 virtual uint32 OnMoreData(AudioOutputStream* stream, uint8* dest, |
| 169 uint32 max_size, AudioBuffersState buffers_state); | 165 uint32 max_size, AudioBuffersState buffers_state); |
| 170 virtual void OnError(AudioOutputStream* stream, int code); | 166 virtual void OnError(AudioOutputStream* stream, int code); |
| 171 | 167 |
| 168 protected: |
| 169 // Internal state of the source. |
| 170 enum State { |
| 171 kEmpty, |
| 172 kCreated, |
| 173 kPlaying, |
| 174 kStarting, |
| 175 kPausedWhenStarting, |
| 176 kPaused, |
| 177 kClosed, |
| 178 kError, |
| 179 }; |
| 180 |
| 172 private: | 181 private: |
| 173 // We are polling sync reader if data became available. | 182 // We are polling sync reader if data became available. |
| 174 static const int kPollNumAttempts; | 183 static const int kPollNumAttempts; |
| 175 static const int kPollPauseInMilliseconds; | 184 static const int kPollPauseInMilliseconds; |
| 176 | 185 |
| 177 AudioOutputController(EventHandler* handler, | 186 AudioOutputController(EventHandler* handler, |
| 178 uint32 capacity, SyncReader* sync_reader); | 187 uint32 capacity, SyncReader* sync_reader); |
| 179 | 188 |
| 180 // The following methods are executed on the audio controller thread. | 189 // The following methods are executed on the audio controller thread. |
| 181 void DoCreate(const AudioParameters& params); | 190 void DoCreate(const AudioParameters& params); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 // When starting stream we wait for data to become available. | 232 // When starting stream we wait for data to become available. |
| 224 // Number of times left. | 233 // Number of times left. |
| 225 int number_polling_attempts_left_; | 234 int number_polling_attempts_left_; |
| 226 | 235 |
| 227 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); | 236 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); |
| 228 }; | 237 }; |
| 229 | 238 |
| 230 } // namespace media | 239 } // namespace media |
| 231 | 240 |
| 232 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 241 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
| OLD | NEW |