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/timer/timer.h" | 11 #include "base/timer/timer.h" |
| 12 #include "build/build_config.h" |
12 #include "media/audio/audio_io.h" | 13 #include "media/audio/audio_io.h" |
13 #include "media/audio/audio_manager.h" | 14 #include "media/audio/audio_manager.h" |
14 #include "media/audio/audio_power_monitor.h" | 15 #include "media/audio/audio_power_monitor.h" |
15 #include "media/audio/audio_source_diverter.h" | 16 #include "media/audio/audio_source_diverter.h" |
16 #include "media/audio/simple_sources.h" | 17 #include "media/audio/simple_sources.h" |
17 #include "media/base/media_export.h" | 18 #include "media/base/media_export.h" |
18 | 19 |
19 // An AudioOutputController controls an AudioOutputStream and provides data | 20 // An AudioOutputController controls an AudioOutputStream and provides data |
20 // 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 |
21 // audio operations like play, pause, stop, etc. on a separate thread, | 22 // audio operations like play, pause, stop, etc. on a separate thread, |
(...skipping 23 matching lines...) Expand all Loading... |
45 // all functionally equivalent and require a Play() call to continue to the next | 46 // all functionally equivalent and require a Play() call to continue to the next |
46 // state. | 47 // state. |
47 // | 48 // |
48 // The AudioOutputStream can request data from the AudioOutputController via the | 49 // The AudioOutputStream can request data from the AudioOutputController via the |
49 // AudioSourceCallback interface. AudioOutputController uses the SyncReader | 50 // AudioSourceCallback interface. AudioOutputController uses the SyncReader |
50 // passed to it via construction to synchronously fulfill this read request. | 51 // passed to it via construction to synchronously fulfill this read request. |
51 // | 52 // |
52 | 53 |
53 namespace media { | 54 namespace media { |
54 | 55 |
55 // Only do power monitoring for non-mobile platforms that need it for the UI. | |
56 #if !defined(OS_ANDROID) && !defined(OS_IOS) | |
57 #define AUDIO_POWER_MONITORING | |
58 #endif | |
59 | |
60 class MEDIA_EXPORT AudioOutputController | 56 class MEDIA_EXPORT AudioOutputController |
61 : public base::RefCountedThreadSafe<AudioOutputController>, | 57 : public base::RefCountedThreadSafe<AudioOutputController>, |
62 public AudioOutputStream::AudioSourceCallback, | 58 public AudioOutputStream::AudioSourceCallback, |
63 public AudioSourceDiverter, | 59 public AudioSourceDiverter, |
64 NON_EXPORTED_BASE(public AudioManager::AudioDeviceListener) { | 60 NON_EXPORTED_BASE(public AudioManager::AudioDeviceListener) { |
65 public: | 61 public: |
66 // An event handler that receives events from the AudioOutputController. The | 62 // An event handler that receives events from the AudioOutputController. The |
67 // following methods are called on the audio manager thread. | 63 // following methods are called on the audio manager thread. |
68 class MEDIA_EXPORT EventHandler { | 64 class MEDIA_EXPORT EventHandler { |
69 public: | 65 public: |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 // thread, and if this is successful, the |event_handler| will receive an | 99 // thread, and if this is successful, the |event_handler| will receive an |
104 // OnCreated() call from the same audio manager thread. |audio_manager| must | 100 // OnCreated() call from the same audio manager thread. |audio_manager| must |
105 // outlive AudioOutputController. | 101 // outlive AudioOutputController. |
106 // The |output_device_id| can be either empty (default device) or specify a | 102 // The |output_device_id| can be either empty (default device) or specify a |
107 // specific hardware device for audio output. | 103 // specific hardware device for audio output. |
108 static scoped_refptr<AudioOutputController> Create( | 104 static scoped_refptr<AudioOutputController> Create( |
109 AudioManager* audio_manager, EventHandler* event_handler, | 105 AudioManager* audio_manager, EventHandler* event_handler, |
110 const AudioParameters& params, const std::string& output_device_id, | 106 const AudioParameters& params, const std::string& output_device_id, |
111 SyncReader* sync_reader); | 107 SyncReader* sync_reader); |
112 | 108 |
| 109 // Indicates whether audio power level analysis will be performed. If false, |
| 110 // ReadCurrentPowerAndClip() can not be called. |
| 111 static bool will_monitor_audio_levels() { |
| 112 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 113 return false; |
| 114 #else |
| 115 return true; |
| 116 #endif |
| 117 } |
| 118 |
113 // Methods to control playback of the stream. | 119 // Methods to control playback of the stream. |
114 | 120 |
115 // Starts the playback of this audio output stream. | 121 // Starts the playback of this audio output stream. |
116 void Play(); | 122 void Play(); |
117 | 123 |
118 // Pause this audio output stream. | 124 // Pause this audio output stream. |
119 void Pause(); | 125 void Pause(); |
120 | 126 |
121 // Closes the audio output stream. The state is changed and the resources | 127 // Closes the audio output stream. The state is changed and the resources |
122 // are freed on the audio manager thread. closed_task is executed after that. | 128 // are freed on the audio manager thread. closed_task is executed after that. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 // Internal state of the source. | 177 // Internal state of the source. |
172 enum State { | 178 enum State { |
173 kEmpty, | 179 kEmpty, |
174 kCreated, | 180 kCreated, |
175 kPlaying, | 181 kPlaying, |
176 kPaused, | 182 kPaused, |
177 kClosed, | 183 kClosed, |
178 kError, | 184 kError, |
179 }; | 185 }; |
180 | 186 |
| 187 // Time constant for AudioPowerMonitor. See AudioPowerMonitor ctor comments |
| 188 // for semantics. This value was arbitrarily chosen, but seems to work well. |
| 189 enum { kPowerMeasurementTimeConstantMillis = 10 }; |
| 190 |
181 friend class base::RefCountedThreadSafe<AudioOutputController>; | 191 friend class base::RefCountedThreadSafe<AudioOutputController>; |
182 virtual ~AudioOutputController(); | 192 virtual ~AudioOutputController(); |
183 | 193 |
184 private: | 194 private: |
185 AudioOutputController(AudioManager* audio_manager, EventHandler* handler, | 195 AudioOutputController(AudioManager* audio_manager, EventHandler* handler, |
186 const AudioParameters& params, | 196 const AudioParameters& params, |
187 const std::string& output_device_id, | 197 const std::string& output_device_id, |
188 SyncReader* sync_reader); | 198 SyncReader* sync_reader); |
189 | 199 |
190 // The following methods are executed on the audio manager thread. | 200 // The following methods are executed on the audio manager thread. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 // hardware audio thread. These operations need to be locked. But lock | 238 // hardware audio thread. These operations need to be locked. But lock |
229 // is not required for reading on the audio manager thread. | 239 // is not required for reading on the audio manager thread. |
230 State state_; | 240 State state_; |
231 | 241 |
232 // SyncReader is used only in low latency mode for synchronous reading. | 242 // SyncReader is used only in low latency mode for synchronous reading. |
233 SyncReader* const sync_reader_; | 243 SyncReader* const sync_reader_; |
234 | 244 |
235 // The message loop of audio manager thread that this object runs on. | 245 // The message loop of audio manager thread that this object runs on. |
236 const scoped_refptr<base::SingleThreadTaskRunner> message_loop_; | 246 const scoped_refptr<base::SingleThreadTaskRunner> message_loop_; |
237 | 247 |
238 #if defined(AUDIO_POWER_MONITORING) | |
239 // Scans audio samples from OnMoreData() as input to compute power levels. | 248 // Scans audio samples from OnMoreData() as input to compute power levels. |
240 AudioPowerMonitor power_monitor_; | 249 AudioPowerMonitor power_monitor_; |
241 #endif | |
242 | 250 |
243 // Flags when we've asked for a stream to start but it never did. | 251 // Flags when we've asked for a stream to start but it never did. |
244 base::AtomicRefCount on_more_io_data_called_; | 252 base::AtomicRefCount on_more_io_data_called_; |
245 scoped_ptr<base::OneShotTimer<AudioOutputController> > wedge_timer_; | 253 scoped_ptr<base::OneShotTimer<AudioOutputController> > wedge_timer_; |
246 | 254 |
247 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); | 255 DISALLOW_COPY_AND_ASSIGN(AudioOutputController); |
248 }; | 256 }; |
249 | 257 |
250 } // namespace media | 258 } // namespace media |
251 | 259 |
252 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ | 260 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_CONTROLLER_H_ |
OLD | NEW |