| 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, SyncReader* sync_reader); | 108 const AudioParameters& params, SyncReader* sync_reader); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 134 // Sets the volume of the audio output stream. | 127 // Sets the volume of the audio output stream. |
| 135 void SetVolume(double volume); | 128 void SetVolume(double volume); |
| 136 | 129 |
| 137 // AudioSourceCallback implementation. | 130 // AudioSourceCallback implementation. |
| 138 virtual int OnMoreData(AudioBus* dest, | 131 virtual int OnMoreData(AudioBus* dest, |
| 139 AudioBuffersState buffers_state) OVERRIDE; | 132 AudioBuffersState buffers_state) OVERRIDE; |
| 140 virtual int OnMoreIOData(AudioBus* source, | 133 virtual int OnMoreIOData(AudioBus* source, |
| 141 AudioBus* dest, | 134 AudioBus* dest, |
| 142 AudioBuffersState buffers_state) OVERRIDE; | 135 AudioBuffersState buffers_state) OVERRIDE; |
| 143 virtual void OnError(AudioOutputStream* stream) OVERRIDE; | 136 virtual void OnError(AudioOutputStream* stream) OVERRIDE; |
| 144 // Deprecated: Currently only used for starting audio playback and for audio | |
| 145 // mirroring. | |
| 146 virtual void WaitTillDataReady() OVERRIDE; | |
| 147 | 137 |
| 148 // AudioDeviceListener implementation. When called AudioOutputController will | 138 // AudioDeviceListener implementation. When called AudioOutputController will |
| 149 // shutdown the existing |stream_|, transition to the kRecreating state, | 139 // shutdown the existing |stream_|, transition to the kRecreating state, |
| 150 // create a new stream, and then transition back to an equivalent state prior | 140 // create a new stream, and then transition back to an equivalent state prior |
| 151 // to being called. | 141 // to being called. |
| 152 virtual void OnDeviceChange() OVERRIDE; | 142 virtual void OnDeviceChange() OVERRIDE; |
| 153 | 143 |
| 154 // AudioSourceDiverter implementation. | 144 // AudioSourceDiverter implementation. |
| 155 virtual const AudioParameters& GetAudioParameters() OVERRIDE; | 145 virtual const AudioParameters& GetAudioParameters() OVERRIDE; |
| 156 virtual void StartDiverting(AudioOutputStream* to_stream) OVERRIDE; | 146 virtual void StartDiverting(AudioOutputStream* to_stream) OVERRIDE; |
| 157 virtual void StopDiverting() OVERRIDE; | 147 virtual void StopDiverting() OVERRIDE; |
| 158 | 148 |
| 159 protected: | 149 protected: |
| 160 // Internal state of the source. | 150 // Internal state of the source. |
| 161 enum State { | 151 enum State { |
| 162 kEmpty, | 152 kEmpty, |
| 163 kCreated, | 153 kCreated, |
| 164 kStarting, | |
| 165 kPlaying, | 154 kPlaying, |
| 166 kPaused, | 155 kPaused, |
| 167 kClosed, | 156 kClosed, |
| 168 kError, | 157 kError, |
| 169 }; | 158 }; |
| 170 | 159 |
| 171 friend class base::RefCountedThreadSafe<AudioOutputController>; | 160 friend class base::RefCountedThreadSafe<AudioOutputController>; |
| 172 virtual ~AudioOutputController(); | 161 virtual ~AudioOutputController(); |
| 173 | 162 |
| 174 private: | 163 private: |
| 175 // We are polling sync reader if data became available. | 164 // We are polling sync reader if data became available. |
| 176 static const int kPollNumAttempts; | 165 static const int kPollNumAttempts; |
| 177 static const int kPollPauseInMilliseconds; | 166 static const int kPollPauseInMilliseconds; |
| 178 | 167 |
| 179 AudioOutputController(AudioManager* audio_manager, EventHandler* handler, | 168 AudioOutputController(AudioManager* audio_manager, EventHandler* handler, |
| 180 const AudioParameters& params, SyncReader* sync_reader); | 169 const AudioParameters& params, SyncReader* sync_reader); |
| 181 | 170 |
| 182 // The following methods are executed on the audio manager thread. | 171 // The following methods are executed on the audio manager thread. |
| 183 void DoCreate(bool is_for_device_change); | 172 void DoCreate(bool is_for_device_change); |
| 184 void DoPlay(); | 173 void DoPlay(); |
| 185 void PollAndStartIfDataReady(); | |
| 186 void DoPause(); | 174 void DoPause(); |
| 187 void DoClose(); | 175 void DoClose(); |
| 188 void DoSetVolume(double volume); | 176 void DoSetVolume(double volume); |
| 189 void DoReportError(); | 177 void DoReportError(); |
| 190 void DoStartDiverting(AudioOutputStream* to_stream); | 178 void DoStartDiverting(AudioOutputStream* to_stream); |
| 191 void DoStopDiverting(); | 179 void DoStopDiverting(); |
| 192 | 180 |
| 193 // Called at regular intervals during playback to check for a change in | 181 // Called at regular intervals during playback to check for a change in |
| 194 // silence and call EventHandler::OnAudible() when state changes occur. | 182 // silence and call EventHandler::OnAudible() when state changes occur. |
| 195 void MaybeInvokeAudibleCallback(); | 183 void MaybeInvokeAudibleCallback(); |
| 196 | 184 |
| 197 // Helper methods that start/stop physical stream. | 185 // Helper method that stops the physical stream. |
| 198 void StartStream(); | |
| 199 void StopStream(); | 186 void StopStream(); |
| 200 | 187 |
| 201 // Helper method that stops, closes, and NULLs |*stream_|. | 188 // Helper method that stops, closes, and NULLs |*stream_|. |
| 202 void DoStopCloseAndClearStream(); | 189 void DoStopCloseAndClearStream(); |
| 203 | 190 |
| 204 // Sanity-check that entry/exit to OnMoreIOData() by the hardware audio thread | 191 // Sanity-check that entry/exit to OnMoreIOData() by the hardware audio thread |
| 205 // happens only between AudioOutputStream::Start() and Stop(). | 192 // happens only between AudioOutputStream::Start() and Stop(). |
| 206 void AllowEntryToOnMoreIOData(); | 193 void AllowEntryToOnMoreIOData(); |
| 207 void DisallowEntryToOnMoreIOData(); | 194 void DisallowEntryToOnMoreIOData(); |
| 208 | 195 |
| 209 AudioManager* const audio_manager_; | 196 AudioManager* const audio_manager_; |
| 210 const AudioParameters params_; | 197 const AudioParameters params_; |
| 211 EventHandler* const handler_; | 198 EventHandler* const handler_; |
| 212 | |
| 213 // Note: It's important to invalidate the weak pointers whenever stream_ is | |
| 214 // changed. See comment for weak_this_. | |
| 215 AudioOutputStream* stream_; | 199 AudioOutputStream* stream_; |
| 216 | 200 |
| 217 // When non-NULL, audio is being diverted to this stream. | 201 // When non-NULL, audio is being diverted to this stream. |
| 218 AudioOutputStream* diverting_to_stream_; | 202 AudioOutputStream* diverting_to_stream_; |
| 219 | 203 |
| 220 // The current volume of the audio stream. | 204 // The current volume of the audio stream. |
| 221 double volume_; | 205 double volume_; |
| 222 | 206 |
| 223 // |state_| is written on the audio manager thread and is read on the | 207 // |state_| is written on the audio manager thread and is read on the |
| 224 // hardware audio thread. These operations need to be locked. But lock | 208 // hardware audio thread. These operations need to be locked. But lock |
| (...skipping 10 matching lines...) Expand all Loading... |
| 235 // SyncReader is used only in low latency mode for synchronous reading. | 219 // SyncReader is used only in low latency mode for synchronous reading. |
| 236 SyncReader* const sync_reader_; | 220 SyncReader* const sync_reader_; |
| 237 | 221 |
| 238 // The message loop of audio manager thread that this object runs on. | 222 // The message loop of audio manager thread that this object runs on. |
| 239 const scoped_refptr<base::MessageLoopProxy> message_loop_; | 223 const scoped_refptr<base::MessageLoopProxy> message_loop_; |
| 240 | 224 |
| 241 // When starting stream we wait for data to become available. | 225 // When starting stream we wait for data to become available. |
| 242 // Number of times left. | 226 // Number of times left. |
| 243 int number_polling_attempts_left_; | 227 int number_polling_attempts_left_; |
| 244 | 228 |
| 245 // Used to auto-cancel the delayed tasks that are created to poll for data | |
| 246 // (when starting-up a stream). | |
| 247 base::WeakPtrFactory<AudioOutputController> weak_this_; | |
| 248 | |
| 249 // Scans audio samples from OnMoreIOData() as input and causes | 229 // Scans audio samples from OnMoreIOData() as input and causes |
| 250 // EventHandler::OnAudbile() to be called whenever a transition to a period of | 230 // EventHandler::OnAudbile() to be called whenever a transition to a period of |
| 251 // silence or non-silence is detected. | 231 // silence or non-silence is detected. |
| 252 scoped_ptr<AudioSilenceDetector> silence_detector_; | 232 scoped_ptr<AudioSilenceDetector> silence_detector_; |
| 253 | 233 |
| 254 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); | 234 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); |
| 255 }; | 235 }; |
| 256 | 236 |
| 257 } // namespace media | 237 } // namespace media |
| 258 | 238 |
| 259 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 239 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
| OLD | NEW |