| 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" | |
| 16 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
| 16 #include "base/threading/thread_checker.h" |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "media/audio/audio_device_description.h" | 18 #include "media/audio/audio_device_description.h" |
| 19 #include "media/audio/audio_logging.h" | 19 #include "media/audio/audio_logging.h" |
| 20 #include "media/audio/audio_thread.h" |
| 20 #include "media/base/audio_parameters.h" | 21 #include "media/base/audio_parameters.h" |
| 21 | 22 |
| 22 namespace base { | 23 namespace base { |
| 23 class FilePath; | 24 class FilePath; |
| 24 class SingleThreadTaskRunner; | 25 class SingleThreadTaskRunner; |
| 25 } | 26 } |
| 26 | 27 |
| 27 namespace media { | 28 namespace media { |
| 28 | 29 |
| 29 class AudioInputStream; | 30 class AudioInputStream; |
| 30 class AudioManager; | 31 class AudioManager; |
| 31 class AudioOutputStream; | 32 class AudioOutputStream; |
| 32 | 33 |
| 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 | 34 // Manages all audio resources. Provides some convenience functions that avoid |
| 41 // the need to provide iterators over the existing streams. | 35 // the need to provide iterators over the existing streams. |
| 42 // | 36 // |
| 43 // Except on OSX, a hang monitor for the audio thread is always created. When a | 37 // 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. | 38 // thread hang is detected, it is reported to UMA. |
| 45 class MEDIA_EXPORT AudioManager { | 39 class MEDIA_EXPORT AudioManager { |
| 46 public: | 40 public: |
| 41 virtual ~AudioManager(); |
| 42 |
| 47 // Construct the audio manager; only one instance is allowed. | 43 // Construct the audio manager; only one instance is allowed. |
| 48 // The returned instance must be deleted on AudioManager::GetTaskRunnner(). | |
| 49 // | 44 // |
| 50 // The manager will forward CreateAudioLog() calls to the provided | 45 // The manager will forward CreateAudioLog() calls to the provided |
| 51 // AudioLogFactory; as such |audio_log_factory| must outlive the AudioManager. | 46 // AudioLogFactory; as such |audio_log_factory| must outlive the AudioManager. |
| 52 // | 47 // |
| 53 // The manager will use |task_runner| for audio IO. This same task runner | 48 // The manager will use |audio_thread->GetTaskRunner()| for audio IO. |
| 54 // is returned by GetTaskRunner(). | 49 // On OS_MACOSX, CoreAudio requires that |audio_thread->GetTaskRunner()| |
| 55 // On OS_MACOSX, CoreAudio requires that |task_runner| must belong to the | 50 // 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 | 51 // 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 | 52 // to crashes and odd behavior. See http://crbug.com/158170. |
| 58 // odd behavior. See http://crbug.com/158170. | |
| 59 // | 53 // |
| 60 // The manager will use |worker_task_runner| for heavyweight tasks. | 54 // The manager will use |audio_thread->GetWorkerTaskRunner()| for heavyweight |
| 61 // The |worker_task_runner| may be the same as |task_runner|. This same | 55 // tasks. The |audio_thread->GetWorkerTaskRunner()| may be the same as |
| 62 // task runner is returned by GetWorkerTaskRunner. | 56 // |audio_thread->GetTaskRunner()|. |
| 63 // | 57 // |
| 64 // |file_task_runner| is used for audio debug recordings and is the task | 58 // |file_task_runner| is used for audio debug recordings and is the task |
| 65 // runner to do file output operations on. | 59 // runner to do file output operations on. |
| 66 static ScopedAudioManagerPtr Create( | 60 static std::unique_ptr<AudioManager> Create( |
| 67 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 61 std::unique_ptr<AudioThread> audio_thread, |
| 68 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, | |
| 69 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 62 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 70 AudioLogFactory* audio_log_factory); | 63 AudioLogFactory* audio_log_factory); |
| 71 | 64 |
| 72 // A convenience wrapper of AudioManager::Create for testing. | 65 // A convenience wrapper of AudioManager::Create for testing. |
| 73 // The given |task_runner| is shared for both audio io and heavyweight tasks. | 66 static std::unique_ptr<AudioManager> CreateForTesting( |
| 74 static ScopedAudioManagerPtr CreateForTesting( | 67 std::unique_ptr<AudioThread> audio_thread); |
| 75 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
| 76 | 68 |
| 77 // Starts monitoring AudioManager task runner for hangs. | 69 // Starts monitoring AudioManager task runner for hangs. |
| 78 // Runs the monitor on the given |task_runner|, which must be different from | 70 // Runs the monitor on the given |task_runner|, which must be different from |
| 79 // AudioManager::GetTaskRunner to be meaningful. | 71 // AudioManager::GetTaskRunner to be meaningful. |
| 80 // This must be called only after an AudioManager instance is created. | 72 // This must be called only after an AudioManager instance is created. |
| 81 static void StartHangMonitorIfNeeded( | 73 static void StartHangMonitorIfNeeded( |
| 82 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | 74 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 83 | 75 |
| 84 #if defined(OS_LINUX) | 76 #if defined(OS_LINUX) |
| 85 // Sets the name of the audio source as seen by external apps. Only actually | 77 // Sets the name of the audio source as seen by external apps. Only actually |
| 86 // used with PulseAudio as of this writing. | 78 // used with PulseAudio as of this writing. |
| 87 static void SetGlobalAppName(const std::string& app_name); | 79 static void SetGlobalAppName(const std::string& app_name); |
| 88 | 80 |
| 89 // Returns the app name or an empty string if it is not set. | 81 // Returns the app name or an empty string if it is not set. |
| 90 static const std::string& GetGlobalAppName(); | 82 static const std::string& GetGlobalAppName(); |
| 91 #endif | 83 #endif |
| 92 | 84 |
| 93 // Returns the pointer to the last created instance, or NULL if not yet | 85 // 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, | 86 // created. This is a utility method for the code outside of media directory, |
| 95 // like src/chrome. | 87 // like src/chrome. |
| 96 static AudioManager* Get(); | 88 static AudioManager* Get(); |
| 97 | 89 |
| 90 // Releases all audio resources. |
| 91 // Must be called before deletion and on the same thread as AudioManager |
| 92 // was created. |
| 93 void Shutdown(); |
| 94 |
| 98 // Log callback used for sending log messages from a stream to the object | 95 // Log callback used for sending log messages from a stream to the object |
| 99 // that manages the stream. | 96 // that manages the stream. |
| 100 using LogCallback = base::Callback<void(const std::string&)>; | 97 using LogCallback = base::Callback<void(const std::string&)>; |
| 101 | 98 |
| 102 // Factory for all the supported stream formats. |params| defines parameters | 99 // Factory for all the supported stream formats. |params| defines parameters |
| 103 // of the audio stream to be created. | 100 // of the audio stream to be created. |
| 104 // | 101 // |
| 105 // |params.sample_per_packet| is the requested buffer allocation which the | 102 // |params.sample_per_packet| is the requested buffer allocation which the |
| 106 // audio source thinks it can usually fill without blocking. Internally two | 103 // 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 | 104 // 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. | 141 // we have reached some other platform specific limit. |
| 145 // | 142 // |
| 146 // Do not free the returned AudioInputStream. It is owned by AudioManager. | 143 // 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. | 144 // When you are done with it, call |Stop()| and |Close()| to release it. |
| 148 virtual AudioInputStream* MakeAudioInputStream( | 145 virtual AudioInputStream* MakeAudioInputStream( |
| 149 const AudioParameters& params, | 146 const AudioParameters& params, |
| 150 const std::string& device_id, | 147 const std::string& device_id, |
| 151 const LogCallback& log_callback) = 0; | 148 const LogCallback& log_callback) = 0; |
| 152 | 149 |
| 153 // Returns the task runner used for audio IO. | 150 // Returns the task runner used for audio IO. |
| 154 // TODO(alokp): Rename to task_runner(). | |
| 155 base::SingleThreadTaskRunner* GetTaskRunner() const { | 151 base::SingleThreadTaskRunner* GetTaskRunner() const { |
| 156 return task_runner_.get(); | 152 return audio_thread_->GetTaskRunner(); |
| 157 } | 153 } |
| 158 | 154 |
| 159 // Heavyweight tasks should use GetWorkerTaskRunner() instead of | 155 // Heavyweight tasks should use GetWorkerTaskRunner() instead of |
| 160 // GetTaskRunner(). On most platforms they are the same, but some share the | 156 // GetTaskRunner(). On most platforms they are the same, but some share the |
| 161 // UI loop with the audio IO loop. | 157 // UI loop with the audio IO loop. |
| 162 // TODO(alokp): Rename to worker_task_runner(). | |
| 163 base::SingleThreadTaskRunner* GetWorkerTaskRunner() const { | 158 base::SingleThreadTaskRunner* GetWorkerTaskRunner() const { |
| 164 return worker_task_runner_.get(); | 159 return audio_thread_->GetWorkerTaskRunner(); |
| 165 } | 160 } |
| 166 | 161 |
| 167 // Allows clients to listen for device state changes; e.g. preferred sample | 162 // Allows clients to listen for device state changes; e.g. preferred sample |
| 168 // rate or channel layout changes. The typical response to receiving this | 163 // rate or channel layout changes. The typical response to receiving this |
| 169 // callback is to recreate the stream. | 164 // callback is to recreate the stream. |
| 170 class AudioDeviceListener { | 165 class AudioDeviceListener { |
| 171 public: | 166 public: |
| 172 virtual void OnDeviceChange() = 0; | 167 virtual void OnDeviceChange() = 0; |
| 173 }; | 168 }; |
| 174 | 169 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 193 // Gets the name of the audio manager (e.g., Windows, Mac, PulseAudio). | 188 // Gets the name of the audio manager (e.g., Windows, Mac, PulseAudio). |
| 194 virtual const char* GetName() = 0; | 189 virtual const char* GetName() = 0; |
| 195 | 190 |
| 196 // Limits the number of streams that can be created for testing purposes. | 191 // Limits the number of streams that can be created for testing purposes. |
| 197 virtual void SetMaxStreamCountForTesting(int max_input, int max_output); | 192 virtual void SetMaxStreamCountForTesting(int max_input, int max_output); |
| 198 | 193 |
| 199 protected: | 194 protected: |
| 200 FRIEND_TEST_ALL_PREFIXES(AudioManagerTest, AudioDebugRecording); | 195 FRIEND_TEST_ALL_PREFIXES(AudioManagerTest, AudioDebugRecording); |
| 201 friend class AudioDeviceInfoAccessorForTests; | 196 friend class AudioDeviceInfoAccessorForTests; |
| 202 | 197 |
| 203 AudioManager(scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 198 explicit AudioManager(std::unique_ptr<AudioThread> audio_thread); |
| 204 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner); | 199 |
| 205 virtual ~AudioManager(); | 200 virtual void ShutdownOnAudioThread() = 0; |
| 206 | 201 |
| 207 // Initializes output debug recording. Can be called on any thread; will post | 202 // Initializes output debug recording. Can be called on any thread; will post |
| 208 // to the audio thread if not called on it. | 203 // to the audio thread if not called on it. |
| 209 virtual void InitializeOutputDebugRecording( | 204 virtual void InitializeOutputDebugRecording( |
| 210 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) = 0; | 205 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) = 0; |
| 211 | 206 |
| 212 // Returns true if the OS reports existence of audio devices. This does not | 207 // 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. | 208 // guarantee that the existing devices support all formats and sample rates. |
| 214 virtual bool HasAudioOutputDevices() = 0; | 209 virtual bool HasAudioOutputDevices() = 0; |
| 215 | 210 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 // as the specified input device. | 262 // as the specified input device. |
| 268 // If the hardware has only an input device (e.g. a webcam), the return value | 263 // If the hardware has only an input device (e.g. a webcam), the return value |
| 269 // will be empty (which the caller can then interpret to be the default output | 264 // will be empty (which the caller can then interpret to be the default output |
| 270 // device). Implementations that don't yet support this feature, must return | 265 // device). Implementations that don't yet support this feature, must return |
| 271 // an empty string. Must be called on the audio worker thread (see | 266 // an empty string. Must be called on the audio worker thread (see |
| 272 // GetTaskRunner()). | 267 // GetTaskRunner()). |
| 273 virtual std::string GetAssociatedOutputDeviceID( | 268 virtual std::string GetAssociatedOutputDeviceID( |
| 274 const std::string& input_device_id) = 0; | 269 const std::string& input_device_id) = 0; |
| 275 | 270 |
| 276 private: | 271 private: |
| 277 friend class base::DeleteHelper<AudioManager>; | |
| 278 friend class AudioManagerDeleter; | |
| 279 friend class AudioSystemImpl; | 272 friend class AudioSystemImpl; |
| 280 | 273 |
| 281 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 274 std::unique_ptr<AudioThread> audio_thread_; |
| 282 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; | 275 bool shutdown_ = false; // True after |this| has been shutdown. |
| 276 |
| 277 THREAD_CHECKER(thread_checker_); |
| 283 DISALLOW_COPY_AND_ASSIGN(AudioManager); | 278 DISALLOW_COPY_AND_ASSIGN(AudioManager); |
| 284 }; | 279 }; |
| 285 | 280 |
| 286 } // namespace media | 281 } // namespace media |
| 287 | 282 |
| 288 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_ | 283 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_ |
| OLD | NEW |