Chromium Code Reviews| 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 <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "media/audio/audio_device_name.h" | 14 #include "media/audio/audio_device_name.h" |
| 15 #include "media/audio/audio_logging.h" | 15 #include "media/audio/audio_logging.h" |
| 16 #include "media/audio/audio_parameters.h" | 16 #include "media/audio/audio_parameters.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 class SingleThreadTaskRunner; | 19 class SingleThreadTaskRunner; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace media { | 22 namespace media { |
| 23 | 23 |
| 24 class AudioInputStream; | 24 class AudioInputStream; |
| 25 class AudioManager; | |
| 25 class AudioManagerFactory; | 26 class AudioManagerFactory; |
| 26 class AudioOutputStream; | 27 class AudioOutputStream; |
| 27 | 28 |
| 29 class AudioManagerDeleter { | |
| 30 public: | |
| 31 void operator()(const AudioManager* instance) const; | |
| 32 }; | |
| 33 using ScopedAudioManagerPtr = scoped_ptr<AudioManager, AudioManagerDeleter>; | |
| 34 | |
| 28 // Manages all audio resources. Provides some convenience functions that avoid | 35 // Manages all audio resources. Provides some convenience functions that avoid |
| 29 // the need to provide iterators over the existing streams. | 36 // the need to provide iterators over the existing streams. |
| 30 // | 37 // |
| 31 // 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 |
| 32 // thread hang is detected, it is reported to UMA. Optionally, if called prior, | 39 // thread hang is detected, it is reported to UMA. Optionally, if called prior, |
| 33 // EnableCrashKeyLoggingForAudioThreadHangs() will cause a non-crash dump to be | 40 // EnableCrashKeyLoggingForAudioThreadHangs() will cause a non-crash dump to be |
| 34 // logged on Windows (this allows us to report driver hangs to Microsoft). | 41 // logged on Windows (this allows us to report driver hangs to Microsoft). |
| 35 class MEDIA_EXPORT AudioManager { | 42 class MEDIA_EXPORT AudioManager { |
| 36 public: | 43 public: |
| 37 virtual ~AudioManager(); | 44 virtual ~AudioManager(); |
| 38 | |
| 39 // This provides an alternative to the statically linked factory method used | 45 // This provides an alternative to the statically linked factory method used |
| 40 // to create AudioManager. This is useful for dynamically-linked third | 46 // to create AudioManager. This is useful for dynamically-linked third |
| 41 // party clients seeking to provide a platform-specific implementation of | 47 // party clients seeking to provide a platform-specific implementation of |
| 42 // AudioManager. After this is called, all static AudioManager::Create* | 48 // AudioManager. After this is called, all static AudioManager::Create* |
| 43 // methods will return an instance of AudioManager provided by |factory|. This | 49 // 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 | 50 // 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|, | 51 // AudioManager::Create* methods. This method takes ownership of |factory|, |
| 46 // which must not be NULL. | 52 // which must not be NULL. |
| 47 static void SetFactory(AudioManagerFactory* factory); | 53 static void SetFactory(AudioManagerFactory* factory); |
| 48 | 54 |
| 49 // Construct the audio manager; only one instance is allowed. The manager | 55 // Construct the audio manager; only one instance is allowed. |
|
tommi (sloooow) - chröme
2016/03/19 02:32:41
since it has come up a few times in the past and b
alokp
2016/03/22 21:47:07
Done.
| |
| 50 // will forward CreateAudioLog() calls to the provided AudioLogFactory; as | 56 // The returned instance must be deleted on AudioManager::GetTaskRunnner(). |
| 51 // such |audio_log_factory| must outlive the AudioManager. | 57 // |
| 52 static AudioManager* Create(AudioLogFactory* audio_log_factory); | 58 // The manager will forward CreateAudioLog() calls to the provided |
| 53 | 59 // AudioLogFactory; as such |audio_log_factory| must outlive the AudioManager. |
| 54 // Similar to Create() except also schedules a monitor on the given task | 60 // |
| 55 // runner to ensure the audio thread is not stuck for more than 60 seconds; if | 61 // The manager will use |task_runner| for audio IO. If the given |task_runner| |
| 56 // a hang is detected, the process will be crashed. See EnableHangMonitor(). | 62 // is NULL, the implementation will choose a default task runner, which can |
| 57 static AudioManager* CreateWithHangTimer( | 63 // be obtained by calling GetTaskRunner(). |
| 58 AudioLogFactory* audio_log_factory, | 64 // |
| 59 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner); | 65 // The manager will use |worker_task_runner| for heavyweight tasks. |
| 60 | 66 // The |worker_task_runner| may be the same as |task_runner| or NULL, in |
| 61 // Similar to Create() except uses a FakeAudioLogFactory for testing. | 67 // which case the implementation will choose a default one. |
| 62 static AudioManager* CreateForTesting(); | 68 // |
| 69 // If |monitor_task_runner| is not NULL, a monitor will be scheduled on | |
| 70 // |monitor_task_runner| to monitor |task_runner|. See EnableHangMonitor(). | |
| 71 static ScopedAudioManagerPtr Create( | |
| 72 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
|
tommi (sloooow) - chröme
2016/03/19 02:32:41
nit: passing by value implies that the caller migh
alokp
2016/03/22 21:47:07
I agree. I plan to look into it in a later patch.
| |
| 73 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, | |
| 74 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner, | |
| 75 AudioLogFactory* audio_log_factory); | |
| 63 | 76 |
| 64 // Enables non-crash dumps when audio thread hangs are detected. | 77 // Enables non-crash dumps when audio thread hangs are detected. |
| 65 // TODO(dalecurtis): There are no callers to this function at present. A list | 78 // 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 | 79 // 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. | 80 // the future if Microsoft is able to triage third party drivers. |
| 68 // See http://crbug.com/422522 | 81 // See http://crbug.com/422522 |
| 69 static void EnableCrashKeyLoggingForAudioThreadHangs(); | 82 static void EnableCrashKeyLoggingForAudioThreadHangs(); |
| 70 | 83 |
| 71 #if defined(OS_LINUX) | 84 #if defined(OS_LINUX) |
| 72 // Sets the name of the audio source as seen by external apps. Only actually | 85 // 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 | 184 // Returns NULL if the combination of the parameters is not supported, or if |
| 172 // we have reached some other platform specific limit. | 185 // we have reached some other platform specific limit. |
| 173 // | 186 // |
| 174 // Do not free the returned AudioInputStream. It is owned by AudioManager. | 187 // 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. | 188 // When you are done with it, call |Stop()| and |Close()| to release it. |
| 176 virtual AudioInputStream* MakeAudioInputStream( | 189 virtual AudioInputStream* MakeAudioInputStream( |
| 177 const AudioParameters& params, | 190 const AudioParameters& params, |
| 178 const std::string& device_id) = 0; | 191 const std::string& device_id) = 0; |
| 179 | 192 |
| 180 // Returns the task runner used for audio IO. | 193 // Returns the task runner used for audio IO. |
| 181 virtual scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() = 0; | 194 // TODO(alokp): Rename to task_runner(). |
| 195 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() const { | |
| 196 return task_runner_; | |
| 197 } | |
| 182 | 198 |
| 183 // Heavyweight tasks should use GetWorkerTaskRunner() instead of | 199 // Heavyweight tasks should use GetWorkerTaskRunner() instead of |
| 184 // GetTaskRunner(). On most platforms they are the same, but some share the | 200 // GetTaskRunner(). On most platforms they are the same, but some share the |
| 185 // UI loop with the audio IO loop. | 201 // UI loop with the audio IO loop. |
| 186 virtual scoped_refptr<base::SingleThreadTaskRunner> GetWorkerTaskRunner() = 0; | 202 // TODO(alokp): Rename to worker_task_runner(). |
| 203 scoped_refptr<base::SingleThreadTaskRunner> GetWorkerTaskRunner() const { | |
| 204 return worker_task_runner_; | |
| 205 } | |
| 187 | 206 |
| 188 // Allows clients to listen for device state changes; e.g. preferred sample | 207 // Allows clients to listen for device state changes; e.g. preferred sample |
| 189 // rate or channel layout changes. The typical response to receiving this | 208 // rate or channel layout changes. The typical response to receiving this |
| 190 // callback is to recreate the stream. | 209 // callback is to recreate the stream. |
| 191 class AudioDeviceListener { | 210 class AudioDeviceListener { |
| 192 public: | 211 public: |
| 193 virtual void OnDeviceChange() = 0; | 212 virtual void OnDeviceChange() = 0; |
| 194 }; | 213 }; |
| 195 | 214 |
| 196 virtual void AddOutputDeviceChangeListener(AudioDeviceListener* listener) = 0; | 215 virtual void AddOutputDeviceChangeListener(AudioDeviceListener* listener) = 0; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 223 // GetWorkerTaskRunner()). | 242 // GetWorkerTaskRunner()). |
| 224 virtual std::string GetAssociatedOutputDeviceID( | 243 virtual std::string GetAssociatedOutputDeviceID( |
| 225 const std::string& input_device_id) = 0; | 244 const std::string& input_device_id) = 0; |
| 226 | 245 |
| 227 // Create a new AudioLog object for tracking the behavior for one or more | 246 // Create a new AudioLog object for tracking the behavior for one or more |
| 228 // instances of the given component. See AudioLogFactory for more details. | 247 // instances of the given component. See AudioLogFactory for more details. |
| 229 virtual scoped_ptr<AudioLog> CreateAudioLog( | 248 virtual scoped_ptr<AudioLog> CreateAudioLog( |
| 230 AudioLogFactory::AudioComponent component) = 0; | 249 AudioLogFactory::AudioComponent component) = 0; |
| 231 | 250 |
| 232 protected: | 251 protected: |
| 233 AudioManager(); | 252 AudioManager(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 253 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner); | |
| 234 | 254 |
| 235 private: | 255 private: |
| 256 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 257 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; | |
| 236 DISALLOW_COPY_AND_ASSIGN(AudioManager); | 258 DISALLOW_COPY_AND_ASSIGN(AudioManager); |
| 237 }; | 259 }; |
| 238 | 260 |
| 239 } // namespace media | 261 } // namespace media |
| 240 | 262 |
| 241 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_ | 263 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_ |
| OLD | NEW |