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