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