| 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 <stdint.h> | 8 #include <stdint.h> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 class MEDIA_EXPORT AudioOutputController | 64 class MEDIA_EXPORT AudioOutputController |
| 65 : public base::RefCountedThreadSafe<AudioOutputController>, | 65 : public base::RefCountedThreadSafe<AudioOutputController>, |
| 66 public AudioOutputStream::AudioSourceCallback, | 66 public AudioOutputStream::AudioSourceCallback, |
| 67 public AudioSourceDiverter, | 67 public AudioSourceDiverter, |
| 68 NON_EXPORTED_BASE(public AudioManager::AudioDeviceListener) { | 68 NON_EXPORTED_BASE(public AudioManager::AudioDeviceListener) { |
| 69 public: | 69 public: |
| 70 // An event handler that receives events from the AudioOutputController. The | 70 // An event handler that receives events from the AudioOutputController. The |
| 71 // following methods are called on the audio manager thread. | 71 // following methods are called on the audio manager thread. |
| 72 class MEDIA_EXPORT EventHandler { | 72 class MEDIA_EXPORT EventHandler { |
| 73 public: | 73 public: |
| 74 virtual void OnCreated() = 0; | 74 virtual void OnControllerCreated() = 0; |
| 75 virtual void OnPlaying() = 0; | 75 virtual void OnControllerPlaying() = 0; |
| 76 virtual void OnPaused() = 0; | 76 virtual void OnControllerPaused() = 0; |
| 77 virtual void OnError() = 0; | 77 virtual void OnControllerError() = 0; |
| 78 | 78 |
| 79 protected: | 79 protected: |
| 80 virtual ~EventHandler() {} | 80 virtual ~EventHandler() {} |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 // A synchronous reader interface used by AudioOutputController for | 83 // A synchronous reader interface used by AudioOutputController for |
| 84 // synchronous reading. | 84 // synchronous reading. |
| 85 // TODO(crogers): find a better name for this class and the Read() method | 85 // TODO(crogers): find a better name for this class and the Read() method |
| 86 // now that it can handle synchronized I/O. | 86 // now that it can handle synchronized I/O. |
| 87 class SyncReader { | 87 class SyncReader { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 101 // be fulfilled (due to timeout). | 101 // be fulfilled (due to timeout). |
| 102 virtual void Read(AudioBus* dest) = 0; | 102 virtual void Read(AudioBus* dest) = 0; |
| 103 | 103 |
| 104 // Close this synchronous reader. | 104 // Close this synchronous reader. |
| 105 virtual void Close() = 0; | 105 virtual void Close() = 0; |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 // Factory method for creating an AudioOutputController. | 108 // Factory method for creating an AudioOutputController. |
| 109 // This also creates and opens an AudioOutputStream on the audio manager | 109 // This also creates and opens an AudioOutputStream on the audio manager |
| 110 // thread, and if this is successful, the |event_handler| will receive an | 110 // thread, and if this is successful, the |event_handler| will receive an |
| 111 // OnCreated() call from the same audio manager thread. |audio_manager| must | 111 // OnControllerCreated() call from the same audio manager thread. |
| 112 // outlive AudioOutputController. | 112 // |audio_manager| must outlive AudioOutputController. |
| 113 // The |output_device_id| can be either empty (default device) or specify a | 113 // The |output_device_id| can be either empty (default device) or specify a |
| 114 // specific hardware device for audio output. | 114 // specific hardware device for audio output. |
| 115 static scoped_refptr<AudioOutputController> Create( | 115 static scoped_refptr<AudioOutputController> Create( |
| 116 AudioManager* audio_manager, EventHandler* event_handler, | 116 AudioManager* audio_manager, |
| 117 const AudioParameters& params, const std::string& output_device_id, | 117 EventHandler* event_handler, |
| 118 SyncReader* sync_reader); | 118 const AudioParameters& params, |
| 119 const std::string& output_device_id, |
| 120 std::unique_ptr<SyncReader> sync_reader); |
| 119 | 121 |
| 120 // Indicates whether audio power level analysis will be performed. If false, | 122 // Indicates whether audio power level analysis will be performed. If false, |
| 121 // ReadCurrentPowerAndClip() can not be called. | 123 // ReadCurrentPowerAndClip() can not be called. |
| 122 static bool will_monitor_audio_levels() { | 124 static bool will_monitor_audio_levels() { |
| 123 #if defined(OS_ANDROID) || defined(OS_IOS) | 125 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 124 return false; | 126 return false; |
| 125 #else | 127 #else |
| 126 return true; | 128 return true; |
| 127 #endif | 129 #endif |
| 128 } | 130 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 const AudioParameters& GetAudioParameters() override; | 183 const AudioParameters& GetAudioParameters() override; |
| 182 void StartDiverting(AudioOutputStream* to_stream) override; | 184 void StartDiverting(AudioOutputStream* to_stream) override; |
| 183 void StopDiverting() override; | 185 void StopDiverting() override; |
| 184 void StartDuplicating(AudioPushSink* sink) override; | 186 void StartDuplicating(AudioPushSink* sink) override; |
| 185 void StopDuplicating(AudioPushSink* sink) override; | 187 void StopDuplicating(AudioPushSink* sink) override; |
| 186 | 188 |
| 187 // Accessor for AudioPowerMonitor::ReadCurrentPowerAndClip(). See comments in | 189 // Accessor for AudioPowerMonitor::ReadCurrentPowerAndClip(). See comments in |
| 188 // audio_power_monitor.h for usage. This may be called on any thread. | 190 // audio_power_monitor.h for usage. This may be called on any thread. |
| 189 std::pair<float, bool> ReadCurrentPowerAndClip(); | 191 std::pair<float, bool> ReadCurrentPowerAndClip(); |
| 190 | 192 |
| 193 // Accessor for enabling communication to the reader. |
| 194 SyncReader* reader() { return sync_reader_.get(); } |
| 195 |
| 191 protected: | 196 protected: |
| 192 // Internal state of the source. | 197 // Internal state of the source. |
| 193 enum State { | 198 enum State { |
| 194 kEmpty, | 199 kEmpty, |
| 195 kCreated, | 200 kCreated, |
| 196 kPlaying, | 201 kPlaying, |
| 197 kPaused, | 202 kPaused, |
| 198 kClosed, | 203 kClosed, |
| 199 kError, | 204 kError, |
| 200 }; | 205 }; |
| 201 | 206 |
| 202 // Time constant for AudioPowerMonitor. See AudioPowerMonitor ctor comments | 207 // Time constant for AudioPowerMonitor. See AudioPowerMonitor ctor comments |
| 203 // for semantics. This value was arbitrarily chosen, but seems to work well. | 208 // for semantics. This value was arbitrarily chosen, but seems to work well. |
| 204 enum { kPowerMeasurementTimeConstantMillis = 10 }; | 209 enum { kPowerMeasurementTimeConstantMillis = 10 }; |
| 205 | 210 |
| 206 friend class base::RefCountedThreadSafe<AudioOutputController>; | 211 friend class base::RefCountedThreadSafe<AudioOutputController>; |
| 207 ~AudioOutputController() override; | 212 ~AudioOutputController() override; |
| 208 | 213 |
| 209 private: | 214 private: |
| 210 AudioOutputController(AudioManager* audio_manager, EventHandler* handler, | 215 AudioOutputController(AudioManager* audio_manager, |
| 216 EventHandler* handler, |
| 211 const AudioParameters& params, | 217 const AudioParameters& params, |
| 212 const std::string& output_device_id, | 218 const std::string& output_device_id, |
| 213 SyncReader* sync_reader); | 219 std::unique_ptr<SyncReader> sync_reader); |
| 214 | 220 |
| 215 // The following methods are executed on the audio manager thread. | 221 // The following methods are executed on the audio manager thread. |
| 216 void DoCreate(bool is_for_device_change); | 222 void DoCreate(bool is_for_device_change); |
| 217 void DoPlay(); | 223 void DoPlay(); |
| 218 void DoPause(); | 224 void DoPause(); |
| 219 void DoClose(); | 225 void DoClose(); |
| 220 void DoSetVolume(double volume); | 226 void DoSetVolume(double volume); |
| 221 std::string DoGetOutputDeviceId() const; | 227 std::string DoGetOutputDeviceId() const; |
| 222 void DoSwitchOutputDevice(const std::string& output_device_id); | 228 void DoSwitchOutputDevice(const std::string& output_device_id); |
| 223 void DoReportError(); | 229 void DoReportError(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 std::set<AudioPushSink*> duplication_targets_; | 262 std::set<AudioPushSink*> duplication_targets_; |
| 257 base::Lock duplication_targets_lock_; | 263 base::Lock duplication_targets_lock_; |
| 258 | 264 |
| 259 // The current volume of the audio stream. | 265 // The current volume of the audio stream. |
| 260 double volume_; | 266 double volume_; |
| 261 | 267 |
| 262 // |state_| may only be used on the audio manager thread. | 268 // |state_| may only be used on the audio manager thread. |
| 263 State state_; | 269 State state_; |
| 264 | 270 |
| 265 // SyncReader is used only in low latency mode for synchronous reading. | 271 // SyncReader is used only in low latency mode for synchronous reading. |
| 266 SyncReader* const sync_reader_; | 272 const std::unique_ptr<SyncReader> sync_reader_; |
| 267 | 273 |
| 268 // The message loop of audio manager thread that this object runs on. | 274 // The message loop of audio manager thread that this object runs on. |
| 269 const scoped_refptr<base::SingleThreadTaskRunner> message_loop_; | 275 const scoped_refptr<base::SingleThreadTaskRunner> message_loop_; |
| 270 | 276 |
| 271 // Scans audio samples from OnMoreData() as input to compute power levels. | 277 // Scans audio samples from OnMoreData() as input to compute power levels. |
| 272 AudioPowerMonitor power_monitor_; | 278 AudioPowerMonitor power_monitor_; |
| 273 | 279 |
| 274 // Flags when we've asked for a stream to start but it never did. | 280 // Flags when we've asked for a stream to start but it never did. |
| 275 base::AtomicRefCount on_more_io_data_called_; | 281 base::AtomicRefCount on_more_io_data_called_; |
| 276 std::unique_ptr<base::OneShotTimer> wedge_timer_; | 282 std::unique_ptr<base::OneShotTimer> wedge_timer_; |
| 277 | 283 |
| 278 // Flag which indicates errors received during Stop/Close should be ignored. | 284 // Flag which indicates errors received during Stop/Close should be ignored. |
| 279 // These errors are generally harmless since a fresh stream is about to be | 285 // These errors are generally harmless since a fresh stream is about to be |
| 280 // recreated, but if forwarded, renderer side clients may consider them | 286 // recreated, but if forwarded, renderer side clients may consider them |
| 281 // catastrophic and abort their operations. | 287 // catastrophic and abort their operations. |
| 282 // | 288 // |
| 283 // If |stream_| is started then |ignore_errors_during_stop_close_| must only | 289 // If |stream_| is started then |ignore_errors_during_stop_close_| must only |
| 284 // be accessed while |error_lock_| is held. | 290 // be accessed while |error_lock_| is held. |
| 285 bool ignore_errors_during_stop_close_; | 291 bool ignore_errors_during_stop_close_; |
| 286 base::Lock error_lock_; | 292 base::Lock error_lock_; |
| 287 | 293 |
| 288 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); | 294 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); |
| 289 }; | 295 }; |
| 290 | 296 |
| 291 } // namespace media | 297 } // namespace media |
| 292 | 298 |
| 293 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 299 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
| OLD | NEW |