| 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/atomic_ref_count.h" | 8 #include "base/atomic_ref_count.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "base/timer.h" | 12 #include "base/timer.h" |
| 14 #include "media/audio/audio_buffers_state.h" | |
| 15 #include "media/audio/audio_io.h" | 13 #include "media/audio/audio_io.h" |
| 16 #include "media/audio/audio_manager.h" | 14 #include "media/audio/audio_manager.h" |
| 17 #include "media/audio/audio_source_diverter.h" | 15 #include "media/audio/audio_source_diverter.h" |
| 18 #include "media/audio/simple_sources.h" | 16 #include "media/audio/simple_sources.h" |
| 19 #include "media/base/media_export.h" | 17 #include "media/base/media_export.h" |
| 20 | 18 |
| 21 // An AudioOutputController controls an AudioOutputStream and provides data | 19 // An AudioOutputController controls an AudioOutputStream and provides data |
| 22 // to this output stream. It has an important function that it executes | 20 // to this output stream. It has an important function that it executes |
| 23 // audio operations like play, pause, stop, etc. on a separate thread, | 21 // audio operations like play, pause, stop, etc. on a separate thread, |
| 24 // namely the audio manager thread. | 22 // namely the audio manager thread. |
| 25 // | 23 // |
| 26 // All the public methods of AudioOutputController are non-blocking. | 24 // All the public methods of AudioOutputController are non-blocking. |
| 27 // The actual operations are performed on the audio manager thread. | 25 // The actual operations are performed on the audio manager thread. |
| 28 // | 26 // |
| 29 // Here is a state transition diagram for the AudioOutputController: | 27 // Here is a state transition diagram for the AudioOutputController: |
| 30 // | 28 // |
| 31 // *[ Empty ] --> [ Created ] --> [ Starting ] --> [ Playing ] --. | 29 // *[ Empty ] --> [ Created ] --> [ Playing ] -------. |
| 32 // | | | ^ | | | 30 // | | | ^ | |
| 33 // | | | | | | | 31 // | | | | | |
| 34 // | | | | v | | 32 // | | | | v |
| 35 // | | | `--------- [ Paused ] | | 33 // | | | `----- [ Paused ] |
| 36 // | | | | | | 34 // | | | | |
| 37 // | v v | | | 35 // | v v | |
| 38 // `-----------> [ Closed ] <-------------------------' | 36 // `-----------> [ Closed ] <-----------' |
| 39 // | 37 // |
| 40 // * Initial state | 38 // * Initial state |
| 41 // | 39 // |
| 42 // At any time after reaching the Created state but before Closed, the | 40 // At any time after reaching the Created state but before Closed, the |
| 43 // AudioOutputController may be notified of a device change via | 41 // AudioOutputController may be notified of a device change via |
| 44 // OnDeviceChange(). As the OnDeviceChange() is processed, state transitions | 42 // OnDeviceChange(). As the OnDeviceChange() is processed, state transitions |
| 45 // will occur, ultimately ending up in an equivalent pre-call state. E.g., if | 43 // will occur, ultimately ending up in an equivalent pre-call state. E.g., if |
| 46 // the state was Paused, the new state will be Created, since these states are | 44 // the state was Paused, the new state will be Created, since these states are |
| 47 // all functionally equivalent and require a Play() call to continue to the next | 45 // all functionally equivalent and require a Play() call to continue to the next |
| 48 // state. | 46 // state. |
| 49 // | 47 // |
| 50 // The AudioOutputStream can request data from the AudioOutputController via the | 48 // The AudioOutputStream can request data from the AudioOutputController via the |
| 51 // AudioSourceCallback interface. AudioOutputController uses the SyncReader | 49 // AudioSourceCallback interface. AudioOutputController uses the SyncReader |
| 52 // passed to it via construction to synchronously fulfill this read request. | 50 // passed to it via construction to synchronously fulfill this read request. |
| 53 // | 51 // |
| 54 // Since AudioOutputController uses AudioManager's message loop the controller | |
| 55 // uses WeakPtr to allow safe cancellation of pending tasks. | |
| 56 // | |
| 57 | 52 |
| 58 namespace media { | 53 namespace media { |
| 59 | 54 |
| 60 class AudioSilenceDetector; | 55 class AudioSilenceDetector; |
| 61 | 56 |
| 62 class MEDIA_EXPORT AudioOutputController | 57 class MEDIA_EXPORT AudioOutputController |
| 63 : public base::RefCountedThreadSafe<AudioOutputController>, | 58 : public base::RefCountedThreadSafe<AudioOutputController>, |
| 64 public AudioOutputStream::AudioSourceCallback, | 59 public AudioOutputStream::AudioSourceCallback, |
| 65 public AudioSourceDiverter, | 60 public AudioSourceDiverter, |
| 66 NON_EXPORTED_BASE(public AudioManager::AudioDeviceListener) { | 61 NON_EXPORTED_BASE(public AudioManager::AudioDeviceListener) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 86 // now that it can handle synchronized I/O. | 81 // now that it can handle synchronized I/O. |
| 87 class SyncReader { | 82 class SyncReader { |
| 88 public: | 83 public: |
| 89 virtual ~SyncReader() {} | 84 virtual ~SyncReader() {} |
| 90 | 85 |
| 91 // Notify the synchronous reader the number of bytes in the | 86 // Notify the synchronous reader the number of bytes in the |
| 92 // AudioOutputController not yet played. This is used by SyncReader to | 87 // AudioOutputController not yet played. This is used by SyncReader to |
| 93 // prepare more data and perform synchronization. | 88 // prepare more data and perform synchronization. |
| 94 virtual void UpdatePendingBytes(uint32 bytes) = 0; | 89 virtual void UpdatePendingBytes(uint32 bytes) = 0; |
| 95 | 90 |
| 96 // Attempt to completely fill |dest|, return the actual number of | 91 // Attempt to completely fill |dest|, return the actual number of frames |
| 97 // frames that could be read. | 92 // that could be read. |source| may optionally be provided for input data. |
| 98 // |source| may optionally be provided for input data. | 93 // If |block| is specified, the Read() will block until data is available |
| 99 virtual int Read(AudioBus* source, AudioBus* dest) = 0; | 94 // or a timeout is reached. |
| 95 virtual int Read(bool block, const AudioBus* source, AudioBus* dest) = 0; |
| 100 | 96 |
| 101 // Close this synchronous reader. | 97 // Close this synchronous reader. |
| 102 virtual void Close() = 0; | 98 virtual void Close() = 0; |
| 103 | |
| 104 // Check if data is ready. | |
| 105 virtual bool DataReady() = 0; | |
| 106 }; | 99 }; |
| 107 | 100 |
| 108 // Factory method for creating an AudioOutputController. | 101 // Factory method for creating an AudioOutputController. |
| 109 // This also creates and opens an AudioOutputStream on the audio manager | 102 // This also creates and opens an AudioOutputStream on the audio manager |
| 110 // thread, and if this is successful, the |event_handler| will receive an | 103 // thread, and if this is successful, the |event_handler| will receive an |
| 111 // OnCreated() call from the same audio manager thread. |audio_manager| must | 104 // OnCreated() call from the same audio manager thread. |audio_manager| must |
| 112 // outlive AudioOutputController. | 105 // outlive AudioOutputController. |
| 113 static scoped_refptr<AudioOutputController> Create( | 106 static scoped_refptr<AudioOutputController> Create( |
| 114 AudioManager* audio_manager, EventHandler* event_handler, | 107 AudioManager* audio_manager, EventHandler* event_handler, |
| 115 const AudioParameters& params, const std::string& input_device_id, | 108 const AudioParameters& params, const std::string& input_device_id, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 135 // Sets the volume of the audio output stream. | 128 // Sets the volume of the audio output stream. |
| 136 void SetVolume(double volume); | 129 void SetVolume(double volume); |
| 137 | 130 |
| 138 // AudioSourceCallback implementation. | 131 // AudioSourceCallback implementation. |
| 139 virtual int OnMoreData(AudioBus* dest, | 132 virtual int OnMoreData(AudioBus* dest, |
| 140 AudioBuffersState buffers_state) OVERRIDE; | 133 AudioBuffersState buffers_state) OVERRIDE; |
| 141 virtual int OnMoreIOData(AudioBus* source, | 134 virtual int OnMoreIOData(AudioBus* source, |
| 142 AudioBus* dest, | 135 AudioBus* dest, |
| 143 AudioBuffersState buffers_state) OVERRIDE; | 136 AudioBuffersState buffers_state) OVERRIDE; |
| 144 virtual void OnError(AudioOutputStream* stream) OVERRIDE; | 137 virtual void OnError(AudioOutputStream* stream) OVERRIDE; |
| 145 // Deprecated: Currently only used for starting audio playback and for audio | |
| 146 // mirroring. | |
| 147 virtual void WaitTillDataReady() OVERRIDE; | |
| 148 | 138 |
| 149 // AudioDeviceListener implementation. When called AudioOutputController will | 139 // AudioDeviceListener implementation. When called AudioOutputController will |
| 150 // shutdown the existing |stream_|, transition to the kRecreating state, | 140 // shutdown the existing |stream_|, transition to the kRecreating state, |
| 151 // create a new stream, and then transition back to an equivalent state prior | 141 // create a new stream, and then transition back to an equivalent state prior |
| 152 // to being called. | 142 // to being called. |
| 153 virtual void OnDeviceChange() OVERRIDE; | 143 virtual void OnDeviceChange() OVERRIDE; |
| 154 | 144 |
| 155 // AudioSourceDiverter implementation. | 145 // AudioSourceDiverter implementation. |
| 156 virtual const AudioParameters& GetAudioParameters() OVERRIDE; | 146 virtual const AudioParameters& GetAudioParameters() OVERRIDE; |
| 157 virtual void StartDiverting(AudioOutputStream* to_stream) OVERRIDE; | 147 virtual void StartDiverting(AudioOutputStream* to_stream) OVERRIDE; |
| 158 virtual void StopDiverting() OVERRIDE; | 148 virtual void StopDiverting() OVERRIDE; |
| 159 | 149 |
| 160 protected: | 150 protected: |
| 161 // Internal state of the source. | 151 // Internal state of the source. |
| 162 enum State { | 152 enum State { |
| 163 kEmpty, | 153 kEmpty, |
| 164 kCreated, | 154 kCreated, |
| 165 kStarting, | |
| 166 kPlaying, | 155 kPlaying, |
| 167 kPaused, | 156 kPaused, |
| 168 kClosed, | 157 kClosed, |
| 169 kError, | 158 kError, |
| 170 }; | 159 }; |
| 171 | 160 |
| 172 friend class base::RefCountedThreadSafe<AudioOutputController>; | 161 friend class base::RefCountedThreadSafe<AudioOutputController>; |
| 173 virtual ~AudioOutputController(); | 162 virtual ~AudioOutputController(); |
| 174 | 163 |
| 175 private: | 164 private: |
| 176 // We are polling sync reader if data became available. | 165 // We are polling sync reader if data became available. |
| 177 static const int kPollNumAttempts; | 166 static const int kPollNumAttempts; |
| 178 static const int kPollPauseInMilliseconds; | 167 static const int kPollPauseInMilliseconds; |
| 179 | 168 |
| 180 AudioOutputController(AudioManager* audio_manager, EventHandler* handler, | 169 AudioOutputController(AudioManager* audio_manager, EventHandler* handler, |
| 181 const AudioParameters& params, | 170 const AudioParameters& params, |
| 182 const std::string& input_device_id, | 171 const std::string& input_device_id, |
| 183 SyncReader* sync_reader); | 172 SyncReader* sync_reader); |
| 184 | 173 |
| 185 // The following methods are executed on the audio manager thread. | 174 // The following methods are executed on the audio manager thread. |
| 186 void DoCreate(bool is_for_device_change); | 175 void DoCreate(bool is_for_device_change); |
| 187 void DoPlay(); | 176 void DoPlay(); |
| 188 void PollAndStartIfDataReady(); | |
| 189 void DoPause(); | 177 void DoPause(); |
| 190 void DoClose(); | 178 void DoClose(); |
| 191 void DoSetVolume(double volume); | 179 void DoSetVolume(double volume); |
| 192 void DoReportError(); | 180 void DoReportError(); |
| 193 void DoStartDiverting(AudioOutputStream* to_stream); | 181 void DoStartDiverting(AudioOutputStream* to_stream); |
| 194 void DoStopDiverting(); | 182 void DoStopDiverting(); |
| 195 | 183 |
| 196 // Called at regular intervals during playback to check for a change in | 184 // Called at regular intervals during playback to check for a change in |
| 197 // silence and call EventHandler::OnAudible() when state changes occur. | 185 // silence and call EventHandler::OnAudible() when state changes occur. |
| 198 void MaybeInvokeAudibleCallback(); | 186 void MaybeInvokeAudibleCallback(); |
| 199 | 187 |
| 200 // Helper methods that start/stop physical stream. | 188 // Helper method that stops the physical stream. |
| 201 void StartStream(); | |
| 202 void StopStream(); | 189 void StopStream(); |
| 203 | 190 |
| 204 // Helper method that stops, closes, and NULLs |*stream_|. | 191 // Helper method that stops, closes, and NULLs |*stream_|. |
| 205 void DoStopCloseAndClearStream(); | 192 void DoStopCloseAndClearStream(); |
| 206 | 193 |
| 207 // Sanity-check that entry/exit to OnMoreIOData() by the hardware audio thread | 194 // Sanity-check that entry/exit to OnMoreIOData() by the hardware audio thread |
| 208 // happens only between AudioOutputStream::Start() and Stop(). | 195 // happens only between AudioOutputStream::Start() and Stop(). |
| 209 void AllowEntryToOnMoreIOData(); | 196 void AllowEntryToOnMoreIOData(); |
| 210 void DisallowEntryToOnMoreIOData(); | 197 void DisallowEntryToOnMoreIOData(); |
| 211 | 198 |
| 212 AudioManager* const audio_manager_; | 199 AudioManager* const audio_manager_; |
| 213 const AudioParameters params_; | 200 const AudioParameters params_; |
| 214 EventHandler* const handler_; | 201 EventHandler* const handler_; |
| 215 | 202 |
| 216 // Used by the unified IO to open the correct input device. | 203 // Used by the unified IO to open the correct input device. |
| 217 std::string input_device_id_; | 204 std::string input_device_id_; |
| 218 | 205 |
| 219 // Note: It's important to invalidate the weak pointers whenever stream_ is | |
| 220 // changed. See comment for weak_this_. | |
| 221 AudioOutputStream* stream_; | 206 AudioOutputStream* stream_; |
| 222 | 207 |
| 223 // When non-NULL, audio is being diverted to this stream. | 208 // When non-NULL, audio is being diverted to this stream. |
| 224 AudioOutputStream* diverting_to_stream_; | 209 AudioOutputStream* diverting_to_stream_; |
| 225 | 210 |
| 226 // The current volume of the audio stream. | 211 // The current volume of the audio stream. |
| 227 double volume_; | 212 double volume_; |
| 228 | 213 |
| 229 // |state_| is written on the audio manager thread and is read on the | 214 // |state_| is written on the audio manager thread and is read on the |
| 230 // hardware audio thread. These operations need to be locked. But lock | 215 // hardware audio thread. These operations need to be locked. But lock |
| (...skipping 10 matching lines...) Expand all Loading... |
| 241 // SyncReader is used only in low latency mode for synchronous reading. | 226 // SyncReader is used only in low latency mode for synchronous reading. |
| 242 SyncReader* const sync_reader_; | 227 SyncReader* const sync_reader_; |
| 243 | 228 |
| 244 // The message loop of audio manager thread that this object runs on. | 229 // The message loop of audio manager thread that this object runs on. |
| 245 const scoped_refptr<base::MessageLoopProxy> message_loop_; | 230 const scoped_refptr<base::MessageLoopProxy> message_loop_; |
| 246 | 231 |
| 247 // When starting stream we wait for data to become available. | 232 // When starting stream we wait for data to become available. |
| 248 // Number of times left. | 233 // Number of times left. |
| 249 int number_polling_attempts_left_; | 234 int number_polling_attempts_left_; |
| 250 | 235 |
| 251 // Used to auto-cancel the delayed tasks that are created to poll for data | |
| 252 // (when starting-up a stream). | |
| 253 base::WeakPtrFactory<AudioOutputController> weak_this_; | |
| 254 | |
| 255 // Scans audio samples from OnMoreIOData() as input and causes | 236 // Scans audio samples from OnMoreIOData() as input and causes |
| 256 // EventHandler::OnAudbile() to be called whenever a transition to a period of | 237 // EventHandler::OnAudbile() to be called whenever a transition to a period of |
| 257 // silence or non-silence is detected. | 238 // silence or non-silence is detected. |
| 258 scoped_ptr<AudioSilenceDetector> silence_detector_; | 239 scoped_ptr<AudioSilenceDetector> silence_detector_; |
| 259 | 240 |
| 260 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); | 241 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); |
| 261 }; | 242 }; |
| 262 | 243 |
| 263 } // namespace media | 244 } // namespace media |
| 264 | 245 |
| 265 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 246 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
| OLD | NEW |