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> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/sequenced_task_runner_helpers.h" | 15 #include "base/sequenced_task_runner_helpers.h" |
16 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
| 17 #include "base/threading/thread_checker.h" |
17 #include "build/build_config.h" | 18 #include "build/build_config.h" |
18 #include "media/audio/audio_device_description.h" | 19 #include "media/audio/audio_device_description.h" |
19 #include "media/audio/audio_logging.h" | 20 #include "media/audio/audio_logging.h" |
| 21 #include "media/audio/audio_thread.h" |
20 #include "media/base/audio_parameters.h" | 22 #include "media/base/audio_parameters.h" |
21 | 23 |
22 namespace base { | 24 namespace base { |
23 class FilePath; | 25 class FilePath; |
24 class SingleThreadTaskRunner; | 26 class SingleThreadTaskRunner; |
25 } | 27 } |
26 | 28 |
27 namespace media { | 29 namespace media { |
28 | 30 |
29 class AudioInputStream; | 31 class AudioInputStream; |
30 class AudioManager; | 32 class AudioManager; |
31 class AudioOutputStream; | 33 class AudioOutputStream; |
32 | 34 |
33 class MEDIA_EXPORT AudioManagerDeleter { | |
34 public: | |
35 void operator()(const AudioManager* instance) const; | |
36 }; | |
37 using ScopedAudioManagerPtr = | |
38 std::unique_ptr<AudioManager, AudioManagerDeleter>; | |
39 | |
40 // Manages all audio resources. Provides some convenience functions that avoid | 35 // Manages all audio resources. Provides some convenience functions that avoid |
41 // the need to provide iterators over the existing streams. | 36 // the need to provide iterators over the existing streams. |
42 // | 37 // |
43 // 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 |
44 // thread hang is detected, it is reported to UMA. | 39 // thread hang is detected, it is reported to UMA. |
45 class MEDIA_EXPORT AudioManager { | 40 class MEDIA_EXPORT AudioManager { |
46 public: | 41 public: |
| 42 virtual ~AudioManager(); |
| 43 |
47 // Construct the audio manager; only one instance is allowed. | 44 // Construct the audio manager; only one instance is allowed. |
48 // The returned instance must be deleted on AudioManager::GetTaskRunnner(). | 45 // The returned instance must be deleted on AudioManager::GetTaskRunner(). |
49 // | 46 // |
50 // The manager will forward CreateAudioLog() calls to the provided | 47 // The manager will forward CreateAudioLog() calls to the provided |
51 // AudioLogFactory; as such |audio_log_factory| must outlive the AudioManager. | 48 // AudioLogFactory; as such |audio_log_factory| must outlive the AudioManager. |
52 // | 49 // |
53 // The manager will use |task_runner| for audio IO. This same task runner | 50 // The manager will use |task_runner| for audio IO. This same task runner |
54 // is returned by GetTaskRunner(). | 51 // is returned by GetTaskRunner(). |
55 // On OS_MACOSX, CoreAudio requires that |task_runner| must belong to the | 52 // On OS_MACOSX, CoreAudio requires that |task_runner| must belong to the |
56 // main thread of the process, which in our case is sadly the browser UI | 53 // main thread of the process, which in our case is sadly the browser UI |
57 // thread. Failure to execute calls on the right thread leads to crashes and | 54 // thread. Failure to execute calls on the right thread leads to crashes and |
58 // odd behavior. See http://crbug.com/158170. | 55 // odd behavior. See http://crbug.com/158170. |
59 // | 56 // |
60 // The manager will use |worker_task_runner| for heavyweight tasks. | 57 // The manager will use |worker_task_runner| for heavyweight tasks. |
61 // The |worker_task_runner| may be the same as |task_runner|. This same | 58 // The |worker_task_runner| may be the same as |task_runner|. This same |
62 // task runner is returned by GetWorkerTaskRunner. | 59 // task runner is returned by GetWorkerTaskRunner. |
63 // | 60 // |
64 // |file_task_runner| is used for audio debug recordings and is the task | 61 // |file_task_runner| is used for audio debug recordings and is the task |
65 // runner to do file output operations on. | 62 // runner to do file output operations on. |
66 static ScopedAudioManagerPtr Create( | 63 static std::unique_ptr<AudioManager> Create( |
67 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 64 std::unique_ptr<AudioThread> audio_thread, |
68 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, | |
69 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 65 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
70 AudioLogFactory* audio_log_factory); | 66 AudioLogFactory* audio_log_factory); |
71 | 67 |
72 // A convenience wrapper of AudioManager::Create for testing. | 68 // A convenience wrapper of AudioManager::Create for testing. |
73 // The given |task_runner| is shared for both audio io and heavyweight tasks. | 69 // The given |task_runner| is shared for both audio io and heavyweight tasks. |
74 static ScopedAudioManagerPtr CreateForTesting( | 70 static std::unique_ptr<AudioManager> CreateForTesting( |
75 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | 71 std::unique_ptr<AudioThread> audio_thread); |
76 | 72 |
77 // Starts monitoring AudioManager task runner for hangs. | 73 // Starts monitoring AudioManager task runner for hangs. |
78 // Runs the monitor on the given |task_runner|, which must be different from | 74 // Runs the monitor on the given |task_runner|, which must be different from |
79 // AudioManager::GetTaskRunner to be meaningful. | 75 // AudioManager::GetTaskRunner to be meaningful. |
80 // This must be called only after an AudioManager instance is created. | 76 // This must be called only after an AudioManager instance is created. |
81 static void StartHangMonitorIfNeeded( | 77 static void StartHangMonitorIfNeeded( |
82 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | 78 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
83 | 79 |
84 #if defined(OS_LINUX) | 80 #if defined(OS_LINUX) |
85 // Sets the name of the audio source as seen by external apps. Only actually | 81 // Sets the name of the audio source as seen by external apps. Only actually |
86 // used with PulseAudio as of this writing. | 82 // used with PulseAudio as of this writing. |
87 static void SetGlobalAppName(const std::string& app_name); | 83 static void SetGlobalAppName(const std::string& app_name); |
88 | 84 |
89 // Returns the app name or an empty string if it is not set. | 85 // Returns the app name or an empty string if it is not set. |
90 static const std::string& GetGlobalAppName(); | 86 static const std::string& GetGlobalAppName(); |
91 #endif | 87 #endif |
92 | 88 |
93 // Returns the pointer to the last created instance, or NULL if not yet | 89 // Returns the pointer to the last created instance, or NULL if not yet |
94 // created. This is a utility method for the code outside of media directory, | 90 // created. This is a utility method for the code outside of media directory, |
95 // like src/chrome. | 91 // like src/chrome. |
96 static AudioManager* Get(); | 92 static AudioManager* Get(); |
97 | 93 |
| 94 // Releases all audio resources. |
| 95 // Must be called on the same thread as AudioManager was created. |
| 96 void Shutdown(); |
| 97 |
98 // Returns true if the OS reports existence of audio devices. This does not | 98 // Returns true if the OS reports existence of audio devices. This does not |
99 // guarantee that the existing devices support all formats and sample rates. | 99 // guarantee that the existing devices support all formats and sample rates. |
100 virtual bool HasAudioOutputDevices() = 0; | 100 virtual bool HasAudioOutputDevices() = 0; |
101 | 101 |
102 // Returns true if the OS reports existence of audio recording devices. This | 102 // Returns true if the OS reports existence of audio recording devices. This |
103 // does not guarantee that the existing devices support all formats and | 103 // does not guarantee that the existing devices support all formats and |
104 // sample rates. | 104 // sample rates. |
105 virtual bool HasAudioInputDevices() = 0; | 105 virtual bool HasAudioInputDevices() = 0; |
106 | 106 |
107 // Returns a human readable string for the model/make of the active audio | 107 // Returns a human readable string for the model/make of the active audio |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 // we have reached some other platform specific limit. | 181 // we have reached some other platform specific limit. |
182 // | 182 // |
183 // Do not free the returned AudioInputStream. It is owned by AudioManager. | 183 // Do not free the returned AudioInputStream. It is owned by AudioManager. |
184 // When you are done with it, call |Stop()| and |Close()| to release it. | 184 // When you are done with it, call |Stop()| and |Close()| to release it. |
185 virtual AudioInputStream* MakeAudioInputStream( | 185 virtual AudioInputStream* MakeAudioInputStream( |
186 const AudioParameters& params, | 186 const AudioParameters& params, |
187 const std::string& device_id, | 187 const std::string& device_id, |
188 const LogCallback& log_callback) = 0; | 188 const LogCallback& log_callback) = 0; |
189 | 189 |
190 // Returns the task runner used for audio IO. | 190 // Returns the task runner used for audio IO. |
191 // TODO(alokp): Rename to task_runner(). | |
192 base::SingleThreadTaskRunner* GetTaskRunner() const { | 191 base::SingleThreadTaskRunner* GetTaskRunner() const { |
193 return task_runner_.get(); | 192 DCHECK(audio_thread_); |
| 193 return audio_thread_->GetTaskRunner(); |
194 } | 194 } |
195 | 195 |
196 // Heavyweight tasks should use GetWorkerTaskRunner() instead of | 196 // Heavyweight tasks should use GetWorkerTaskRunner() instead of |
197 // GetTaskRunner(). On most platforms they are the same, but some share the | 197 // GetTaskRunner(). On most platforms they are the same, but some share the |
198 // UI loop with the audio IO loop. | 198 // UI loop with the audio IO loop. |
199 // TODO(alokp): Rename to worker_task_runner(). | |
200 base::SingleThreadTaskRunner* GetWorkerTaskRunner() const { | 199 base::SingleThreadTaskRunner* GetWorkerTaskRunner() const { |
201 return worker_task_runner_.get(); | 200 DCHECK(audio_thread_); |
| 201 return audio_thread_->GetWorkerTaskRunner(); |
202 } | 202 } |
203 | 203 |
204 // Allows clients to listen for device state changes; e.g. preferred sample | 204 // Allows clients to listen for device state changes; e.g. preferred sample |
205 // rate or channel layout changes. The typical response to receiving this | 205 // rate or channel layout changes. The typical response to receiving this |
206 // callback is to recreate the stream. | 206 // callback is to recreate the stream. |
207 class AudioDeviceListener { | 207 class AudioDeviceListener { |
208 public: | 208 public: |
209 virtual void OnDeviceChange() = 0; | 209 virtual void OnDeviceChange() = 0; |
210 }; | 210 }; |
211 | 211 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 | 256 |
257 // Gets the name of the audio manager (e.g., Windows, Mac, PulseAudio). | 257 // Gets the name of the audio manager (e.g., Windows, Mac, PulseAudio). |
258 virtual const char* GetName() = 0; | 258 virtual const char* GetName() = 0; |
259 | 259 |
260 // Limits the number of streams that can be created for testing purposes. | 260 // Limits the number of streams that can be created for testing purposes. |
261 virtual void SetMaxStreamCountForTesting(int max_input, int max_output); | 261 virtual void SetMaxStreamCountForTesting(int max_input, int max_output); |
262 | 262 |
263 protected: | 263 protected: |
264 FRIEND_TEST_ALL_PREFIXES(AudioManagerTest, AudioDebugRecording); | 264 FRIEND_TEST_ALL_PREFIXES(AudioManagerTest, AudioDebugRecording); |
265 | 265 |
266 AudioManager(scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 266 explicit AudioManager(std::unique_ptr<AudioThread> audio_thread); |
267 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner); | |
268 virtual ~AudioManager(); | |
269 | 267 |
270 // Initializes output debug recording. Can be called on any thread; will post | 268 // Initializes output debug recording. Can be called on any thread; will post |
271 // to the audio thread if not called on it. | 269 // to the audio thread if not called on it. |
272 virtual void InitializeOutputDebugRecording( | 270 virtual void InitializeOutputDebugRecording( |
273 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) = 0; | 271 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) = 0; |
274 | 272 |
| 273 virtual void ShutdownOnAudioThread() = 0; |
| 274 |
275 private: | 275 private: |
276 friend class base::DeleteHelper<AudioManager>; | 276 base::ThreadChecker thread_checker_; |
277 friend class AudioManagerDeleter; | 277 std::unique_ptr<AudioThread> audio_thread_; |
278 | 278 |
279 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
280 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; | |
281 DISALLOW_COPY_AND_ASSIGN(AudioManager); | 279 DISALLOW_COPY_AND_ASSIGN(AudioManager); |
282 }; | 280 }; |
283 | 281 |
284 } // namespace media | 282 } // namespace media |
285 | 283 |
286 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_ | 284 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_ |
OLD | NEW |