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_MANAGER_H_ | 5 #ifndef MEDIA_AUDIO_AUDIO_MANAGER_H_ |
6 #define MEDIA_AUDIO_AUDIO_MANAGER_H_ | 6 #define MEDIA_AUDIO_AUDIO_MANAGER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "media/audio/audio_device_name.h" | 14 #include "media/audio/audio_device_name.h" |
15 #include "media/audio/audio_logging.h" | 15 #include "media/audio/audio_logging.h" |
16 #include "media/audio/audio_parameters.h" | 16 #include "media/audio/audio_parameters.h" |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 class SingleThreadTaskRunner; | 19 class SingleThreadTaskRunner; |
20 } | 20 } |
21 | 21 |
22 namespace media { | 22 namespace media { |
23 | 23 |
24 class AudioInputStream; | 24 class AudioInputStream; |
| 25 class AudioManager; |
25 class AudioManagerFactory; | 26 class AudioManagerFactory; |
26 class AudioOutputStream; | 27 class AudioOutputStream; |
27 | 28 |
| 29 class MEDIA_EXPORT AudioManagerDeleter { |
| 30 public: |
| 31 void operator()(const AudioManager* instance) const; |
| 32 }; |
| 33 using ScopedAudioManagerPtr = scoped_ptr<AudioManager, AudioManagerDeleter>; |
| 34 |
28 // Manages all audio resources. Provides some convenience functions that avoid | 35 // Manages all audio resources. Provides some convenience functions that avoid |
29 // the need to provide iterators over the existing streams. | 36 // the need to provide iterators over the existing streams. |
30 // | 37 // |
31 // Except on OSX, a hang monitor for the audio thread is always created. When a | 38 // Except on OSX, a hang monitor for the audio thread is always created. When a |
32 // thread hang is detected, it is reported to UMA. Optionally, if called prior, | 39 // thread hang is detected, it is reported to UMA. Optionally, if called prior, |
33 // EnableCrashKeyLoggingForAudioThreadHangs() will cause a non-crash dump to be | 40 // EnableCrashKeyLoggingForAudioThreadHangs() will cause a non-crash dump to be |
34 // logged on Windows (this allows us to report driver hangs to Microsoft). | 41 // logged on Windows (this allows us to report driver hangs to Microsoft). |
35 class MEDIA_EXPORT AudioManager { | 42 class MEDIA_EXPORT AudioManager { |
36 public: | 43 public: |
37 virtual ~AudioManager(); | 44 virtual ~AudioManager(); |
38 | |
39 // This provides an alternative to the statically linked factory method used | 45 // This provides an alternative to the statically linked factory method used |
40 // to create AudioManager. This is useful for dynamically-linked third | 46 // to create AudioManager. This is useful for dynamically-linked third |
41 // party clients seeking to provide a platform-specific implementation of | 47 // party clients seeking to provide a platform-specific implementation of |
42 // AudioManager. After this is called, all static AudioManager::Create* | 48 // AudioManager. After this is called, all static AudioManager::Create* |
43 // methods will return an instance of AudioManager provided by |factory|. This | 49 // methods will return an instance of AudioManager provided by |factory|. This |
44 // call may be made at most once per process, and before any calls to static | 50 // call may be made at most once per process, and before any calls to static |
45 // AudioManager::Create* methods. This method takes ownership of |factory|, | 51 // AudioManager::Create* methods. This method takes ownership of |factory|, |
46 // which must not be NULL. | 52 // which must not be NULL. |
47 static void SetFactory(AudioManagerFactory* factory); | 53 static void SetFactory(AudioManagerFactory* factory); |
48 | 54 |
49 // Construct the audio manager; only one instance is allowed. The manager | 55 // Construct the audio manager; only one instance is allowed. |
50 // will forward CreateAudioLog() calls to the provided AudioLogFactory; as | 56 // The returned instance must be deleted on AudioManager::GetTaskRunnner(). |
51 // such |audio_log_factory| must outlive the AudioManager. | 57 // |
52 static AudioManager* Create(AudioLogFactory* audio_log_factory); | 58 // The manager will forward CreateAudioLog() calls to the provided |
| 59 // AudioLogFactory; as such |audio_log_factory| must outlive the AudioManager. |
| 60 // |
| 61 // The manager will use |task_runner| for audio IO. This same task runner |
| 62 // is returned by GetTaskRunner(). |
| 63 // On OS_MACOSX, CoreAudio requires that |task_runner| must belong to the |
| 64 // main thread of the process, which in our case is sadly the browser UI |
| 65 // thread. Failure to execute calls on the right thread leads to crashes and |
| 66 // odd behavior. See http://crbug.com/158170. |
| 67 // |
| 68 // The manager will use |worker_task_runner| for heavyweight tasks. |
| 69 // The |worker_task_runner| may be the same as |task_runner|. This same |
| 70 // task runner is returned by GetWorkerTaskRunner. |
| 71 // |
| 72 // If |monitor_task_runner| is not NULL, a monitor will be scheduled on |
| 73 // |monitor_task_runner| to monitor |task_runner|. See EnableHangMonitor(). |
| 74 static ScopedAudioManagerPtr Create( |
| 75 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 76 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, |
| 77 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner, |
| 78 AudioLogFactory* audio_log_factory); |
53 | 79 |
54 // Similar to Create() except also schedules a monitor on the given task | 80 // A convenience wrapper of AudioManager::Create for testing. |
55 // runner to ensure the audio thread is not stuck for more than 60 seconds; if | 81 // The given |task_runner| is shared for both audio io and heavyweight tasks. |
56 // a hang is detected, the process will be crashed. See EnableHangMonitor(). | 82 static ScopedAudioManagerPtr CreateForTesting( |
57 static AudioManager* CreateWithHangTimer( | 83 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
58 AudioLogFactory* audio_log_factory, | |
59 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner); | |
60 | |
61 // Similar to Create() except uses a FakeAudioLogFactory for testing. | |
62 static AudioManager* CreateForTesting(); | |
63 | 84 |
64 // Enables non-crash dumps when audio thread hangs are detected. | 85 // Enables non-crash dumps when audio thread hangs are detected. |
65 // TODO(dalecurtis): There are no callers to this function at present. A list | 86 // TODO(dalecurtis): There are no callers to this function at present. A list |
66 // of bad drivers has been given to Microsoft. This should be re-enabled in | 87 // of bad drivers has been given to Microsoft. This should be re-enabled in |
67 // the future if Microsoft is able to triage third party drivers. | 88 // the future if Microsoft is able to triage third party drivers. |
68 // See http://crbug.com/422522 | 89 // See http://crbug.com/422522 |
69 static void EnableCrashKeyLoggingForAudioThreadHangs(); | 90 static void EnableCrashKeyLoggingForAudioThreadHangs(); |
70 | 91 |
71 #if defined(OS_LINUX) | 92 #if defined(OS_LINUX) |
72 // Sets the name of the audio source as seen by external apps. Only actually | 93 // Sets the name of the audio source as seen by external apps. Only actually |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 // Returns NULL if the combination of the parameters is not supported, or if | 192 // Returns NULL if the combination of the parameters is not supported, or if |
172 // we have reached some other platform specific limit. | 193 // we have reached some other platform specific limit. |
173 // | 194 // |
174 // Do not free the returned AudioInputStream. It is owned by AudioManager. | 195 // Do not free the returned AudioInputStream. It is owned by AudioManager. |
175 // When you are done with it, call |Stop()| and |Close()| to release it. | 196 // When you are done with it, call |Stop()| and |Close()| to release it. |
176 virtual AudioInputStream* MakeAudioInputStream( | 197 virtual AudioInputStream* MakeAudioInputStream( |
177 const AudioParameters& params, | 198 const AudioParameters& params, |
178 const std::string& device_id) = 0; | 199 const std::string& device_id) = 0; |
179 | 200 |
180 // Returns the task runner used for audio IO. | 201 // Returns the task runner used for audio IO. |
181 virtual scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() = 0; | 202 // TODO(alokp): Rename to task_runner(). |
| 203 base::SingleThreadTaskRunner* GetTaskRunner() const { |
| 204 return task_runner_.get(); |
| 205 } |
182 | 206 |
183 // Heavyweight tasks should use GetWorkerTaskRunner() instead of | 207 // Heavyweight tasks should use GetWorkerTaskRunner() instead of |
184 // GetTaskRunner(). On most platforms they are the same, but some share the | 208 // GetTaskRunner(). On most platforms they are the same, but some share the |
185 // UI loop with the audio IO loop. | 209 // UI loop with the audio IO loop. |
186 virtual scoped_refptr<base::SingleThreadTaskRunner> GetWorkerTaskRunner() = 0; | 210 // TODO(alokp): Rename to worker_task_runner(). |
| 211 base::SingleThreadTaskRunner* GetWorkerTaskRunner() const { |
| 212 return worker_task_runner_.get(); |
| 213 } |
187 | 214 |
188 // Allows clients to listen for device state changes; e.g. preferred sample | 215 // Allows clients to listen for device state changes; e.g. preferred sample |
189 // rate or channel layout changes. The typical response to receiving this | 216 // rate or channel layout changes. The typical response to receiving this |
190 // callback is to recreate the stream. | 217 // callback is to recreate the stream. |
191 class AudioDeviceListener { | 218 class AudioDeviceListener { |
192 public: | 219 public: |
193 virtual void OnDeviceChange() = 0; | 220 virtual void OnDeviceChange() = 0; |
194 }; | 221 }; |
195 | 222 |
196 virtual void AddOutputDeviceChangeListener(AudioDeviceListener* listener) = 0; | 223 virtual void AddOutputDeviceChangeListener(AudioDeviceListener* listener) = 0; |
(...skipping 26 matching lines...) Expand all Loading... |
223 // GetWorkerTaskRunner()). | 250 // GetWorkerTaskRunner()). |
224 virtual std::string GetAssociatedOutputDeviceID( | 251 virtual std::string GetAssociatedOutputDeviceID( |
225 const std::string& input_device_id) = 0; | 252 const std::string& input_device_id) = 0; |
226 | 253 |
227 // Create a new AudioLog object for tracking the behavior for one or more | 254 // Create a new AudioLog object for tracking the behavior for one or more |
228 // instances of the given component. See AudioLogFactory for more details. | 255 // instances of the given component. See AudioLogFactory for more details. |
229 virtual scoped_ptr<AudioLog> CreateAudioLog( | 256 virtual scoped_ptr<AudioLog> CreateAudioLog( |
230 AudioLogFactory::AudioComponent component) = 0; | 257 AudioLogFactory::AudioComponent component) = 0; |
231 | 258 |
232 protected: | 259 protected: |
233 AudioManager(); | 260 AudioManager(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 261 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner); |
234 | 262 |
235 private: | 263 private: |
| 264 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 265 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; |
236 DISALLOW_COPY_AND_ASSIGN(AudioManager); | 266 DISALLOW_COPY_AND_ASSIGN(AudioManager); |
237 }; | 267 }; |
238 | 268 |
239 } // namespace media | 269 } // namespace media |
240 | 270 |
241 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_ | 271 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_ |
OLD | NEW |