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