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(). | |
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 // Log callback used for sending log messages from a stream to the object | 96 // Log callback used for sending log messages from a stream to the object |
99 // that manages the stream. | 97 // that manages the stream. |
100 using LogCallback = base::Callback<void(const std::string&)>; | 98 using LogCallback = base::Callback<void(const std::string&)>; |
101 | 99 |
102 // Factory for all the supported stream formats. |params| defines parameters | 100 // Factory for all the supported stream formats. |params| defines parameters |
103 // of the audio stream to be created. | 101 // of the audio stream to be created. |
104 // | 102 // |
105 // |params.sample_per_packet| is the requested buffer allocation which the | 103 // |params.sample_per_packet| is the requested buffer allocation which the |
106 // audio source thinks it can usually fill without blocking. Internally two | 104 // audio source thinks it can usually fill without blocking. Internally two |
107 // or three buffers are created, one will be locked for playback and one will | 105 // or three buffers are created, one will be locked for playback and one will |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 // we have reached some other platform specific limit. | 142 // we have reached some other platform specific limit. |
145 // | 143 // |
146 // Do not free the returned AudioInputStream. It is owned by AudioManager. | 144 // Do not free the returned AudioInputStream. It is owned by AudioManager. |
147 // When you are done with it, call |Stop()| and |Close()| to release it. | 145 // When you are done with it, call |Stop()| and |Close()| to release it. |
148 virtual AudioInputStream* MakeAudioInputStream( | 146 virtual AudioInputStream* MakeAudioInputStream( |
149 const AudioParameters& params, | 147 const AudioParameters& params, |
150 const std::string& device_id, | 148 const std::string& device_id, |
151 const LogCallback& log_callback) = 0; | 149 const LogCallback& log_callback) = 0; |
152 | 150 |
153 // Returns the task runner used for audio IO. | 151 // Returns the task runner used for audio IO. |
154 // TODO(alokp): Rename to task_runner(). | |
155 base::SingleThreadTaskRunner* GetTaskRunner() const { | 152 base::SingleThreadTaskRunner* GetTaskRunner() const { |
156 return task_runner_.get(); | 153 return audio_thread_->GetTaskRunner(); |
157 } | 154 } |
158 | 155 |
159 // Heavyweight tasks should use GetWorkerTaskRunner() instead of | 156 // Heavyweight tasks should use GetWorkerTaskRunner() instead of |
160 // GetTaskRunner(). On most platforms they are the same, but some share the | 157 // GetTaskRunner(). On most platforms they are the same, but some share the |
161 // UI loop with the audio IO loop. | 158 // UI loop with the audio IO loop. |
162 // TODO(alokp): Rename to worker_task_runner(). | |
163 base::SingleThreadTaskRunner* GetWorkerTaskRunner() const { | 159 base::SingleThreadTaskRunner* GetWorkerTaskRunner() const { |
164 return worker_task_runner_.get(); | 160 return audio_thread_->GetWorkerTaskRunner(); |
165 } | 161 } |
166 | 162 |
167 // Allows clients to listen for device state changes; e.g. preferred sample | 163 // Allows clients to listen for device state changes; e.g. preferred sample |
168 // rate or channel layout changes. The typical response to receiving this | 164 // rate or channel layout changes. The typical response to receiving this |
169 // callback is to recreate the stream. | 165 // callback is to recreate the stream. |
170 class AudioDeviceListener { | 166 class AudioDeviceListener { |
171 public: | 167 public: |
172 virtual void OnDeviceChange() = 0; | 168 virtual void OnDeviceChange() = 0; |
173 }; | 169 }; |
174 | 170 |
(...skipping 18 matching lines...) Expand all Loading... | |
193 // Gets the name of the audio manager (e.g., Windows, Mac, PulseAudio). | 189 // Gets the name of the audio manager (e.g., Windows, Mac, PulseAudio). |
194 virtual const char* GetName() = 0; | 190 virtual const char* GetName() = 0; |
195 | 191 |
196 // Limits the number of streams that can be created for testing purposes. | 192 // Limits the number of streams that can be created for testing purposes. |
197 virtual void SetMaxStreamCountForTesting(int max_input, int max_output); | 193 virtual void SetMaxStreamCountForTesting(int max_input, int max_output); |
198 | 194 |
199 protected: | 195 protected: |
200 FRIEND_TEST_ALL_PREFIXES(AudioManagerTest, AudioDebugRecording); | 196 FRIEND_TEST_ALL_PREFIXES(AudioManagerTest, AudioDebugRecording); |
201 friend class AudioDeviceInfoAccessorForTests; | 197 friend class AudioDeviceInfoAccessorForTests; |
202 | 198 |
203 AudioManager(scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 199 explicit AudioManager(std::unique_ptr<AudioThread> audio_thread); |
204 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner); | 200 |
205 virtual ~AudioManager(); | 201 virtual void ShutdownOnAudioThread() = 0; |
206 | 202 |
207 // Initializes output debug recording. Can be called on any thread; will post | 203 // Initializes output debug recording. Can be called on any thread; will post |
208 // to the audio thread if not called on it. | 204 // to the audio thread if not called on it. |
209 virtual void InitializeOutputDebugRecording( | 205 virtual void InitializeOutputDebugRecording( |
210 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) = 0; | 206 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) = 0; |
211 | 207 |
212 // Returns true if the OS reports existence of audio devices. This does not | 208 // Returns true if the OS reports existence of audio devices. This does not |
213 // guarantee that the existing devices support all formats and sample rates. | 209 // guarantee that the existing devices support all formats and sample rates. |
214 virtual bool HasAudioOutputDevices() = 0; | 210 virtual bool HasAudioOutputDevices() = 0; |
215 | 211 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
271 // an empty string. Must be called on the audio worker thread (see | 267 // an empty string. Must be called on the audio worker thread (see |
272 // GetTaskRunner()). | 268 // GetTaskRunner()). |
273 virtual std::string GetAssociatedOutputDeviceID( | 269 virtual std::string GetAssociatedOutputDeviceID( |
274 const std::string& input_device_id) = 0; | 270 const std::string& input_device_id) = 0; |
275 | 271 |
276 private: | 272 private: |
277 friend class base::DeleteHelper<AudioManager>; | 273 friend class base::DeleteHelper<AudioManager>; |
278 friend class AudioManagerDeleter; | 274 friend class AudioManagerDeleter; |
279 friend class AudioSystemImpl; | 275 friend class AudioSystemImpl; |
280 | 276 |
281 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 277 base::ThreadChecker thread_checker_; |
282 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; | 278 std::unique_ptr<AudioThread> audio_thread_; |
279 bool shutdown_; // True after |this| has been shutdown. | |
o1ka
2017/05/10 15:57:57
= false?
alokp
2017/05/10 18:04:03
Done.
| |
280 | |
283 DISALLOW_COPY_AND_ASSIGN(AudioManager); | 281 DISALLOW_COPY_AND_ASSIGN(AudioManager); |
284 }; | 282 }; |
285 | 283 |
286 } // namespace media | 284 } // namespace media |
287 | 285 |
288 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_ | 286 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_ |
OLD | NEW |