Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: media/audio/audio_manager.h

Issue 2784433002: Ensures that audio tasks cannot run after AudioManager is deleted. (Closed)
Patch Set: fixes content_browsertests and content_unittests Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
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(). 44 // 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 |task_runner| for audio IO. This same task runner
54 // is returned by GetTaskRunner(). 50 // is returned by GetTaskRunner().
55 // On OS_MACOSX, CoreAudio requires that |task_runner| must belong to the 51 // On OS_MACOSX, CoreAudio requires that |task_runner| must belong to the
56 // main thread of the process, which in our case is sadly the browser UI 52 // main thread of the process, which in our case is sadly the browser UI
57 // thread. Failure to execute calls on the right thread leads to crashes and 53 // thread. Failure to execute calls on the right thread leads to crashes and
58 // odd behavior. See http://crbug.com/158170. 54 // odd behavior. See http://crbug.com/158170.
59 // 55 //
60 // The manager will use |worker_task_runner| for heavyweight tasks. 56 // The manager will use |worker_task_runner| for heavyweight tasks.
61 // The |worker_task_runner| may be the same as |task_runner|. This same 57 // The |worker_task_runner| may be the same as |task_runner|. This same
62 // task runner is returned by GetWorkerTaskRunner. 58 // task runner is returned by GetWorkerTaskRunner.
63 // 59 //
64 // |file_task_runner| is used for audio debug recordings and is the task 60 // |file_task_runner| is used for audio debug recordings and is the task
65 // runner to do file output operations on. 61 // runner to do file output operations on.
66 static ScopedAudioManagerPtr Create( 62 static std::unique_ptr<AudioManager> Create(
67 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 63 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
68 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, 64 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner,
69 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, 65 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
70 AudioLogFactory* audio_log_factory); 66 AudioLogFactory* audio_log_factory);
71 67
72 // A convenience wrapper of AudioManager::Create for testing. 68 // A convenience wrapper of AudioManager::Create for testing.
73 // The given |task_runner| is shared for both audio io and heavyweight tasks. 69 // The given |task_runner| is shared for both audio io and heavyweight tasks.
74 static ScopedAudioManagerPtr CreateForTesting( 70 static std::unique_ptr<AudioManager> CreateForTesting(
75 scoped_refptr<base::SingleThreadTaskRunner> task_runner); 71 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
76 72
77 // Starts monitoring AudioManager task runner for hangs. 73 // Starts monitoring AudioManager task runner for hangs.
78 // Runs the monitor on the given |task_runner|, which must be different from 74 // Runs the monitor on the given |task_runner|, which must be different from
79 // AudioManager::GetTaskRunner to be meaningful. 75 // AudioManager::GetTaskRunner to be meaningful.
80 // This must be called only after an AudioManager instance is created. 76 // This must be called only after an AudioManager instance is created.
81 static void StartHangMonitorIfNeeded( 77 static void StartHangMonitorIfNeeded(
82 scoped_refptr<base::SingleThreadTaskRunner> task_runner); 78 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
83 79
84 #if defined(OS_LINUX) 80 #if defined(OS_LINUX)
85 // Sets the name of the audio source as seen by external apps. Only actually 81 // Sets the name of the audio source as seen by external apps. Only actually
86 // used with PulseAudio as of this writing. 82 // used with PulseAudio as of this writing.
87 static void SetGlobalAppName(const std::string& app_name); 83 static void SetGlobalAppName(const std::string& app_name);
88 84
89 // Returns the app name or an empty string if it is not set. 85 // Returns the app name or an empty string if it is not set.
90 static const std::string& GetGlobalAppName(); 86 static const std::string& GetGlobalAppName();
91 #endif 87 #endif
92 88
93 // Returns the pointer to the last created instance, or NULL if not yet 89 // 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, 90 // created. This is a utility method for the code outside of media directory,
95 // like src/chrome. 91 // like src/chrome.
96 static AudioManager* Get(); 92 static AudioManager* Get();
97 93
94 // Releases all audio resources.
95 // Must be called on the audio thread.
96 virtual void Shutdown() = 0;
97
98 // Returns true if the OS reports existence of audio devices. This does not 98 // 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. 99 // guarantee that the existing devices support all formats and sample rates.
100 virtual bool HasAudioOutputDevices() = 0; 100 virtual bool HasAudioOutputDevices() = 0;
101 101
102 // Returns true if the OS reports existence of audio recording devices. This 102 // Returns true if the OS reports existence of audio recording devices. This
103 // does not guarantee that the existing devices support all formats and 103 // does not guarantee that the existing devices support all formats and
104 // sample rates. 104 // sample rates.
105 virtual bool HasAudioInputDevices() = 0; 105 virtual bool HasAudioInputDevices() = 0;
106 106
107 // Returns a human readable string for the model/make of the active audio 107 // Returns a human readable string for the model/make of the active audio
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 virtual const char* GetName() = 0; 258 virtual const char* GetName() = 0;
259 259
260 // Limits the number of streams that can be created for testing purposes. 260 // Limits the number of streams that can be created for testing purposes.
261 virtual void SetMaxStreamCountForTesting(int max_input, int max_output); 261 virtual void SetMaxStreamCountForTesting(int max_input, int max_output);
262 262
263 protected: 263 protected:
264 FRIEND_TEST_ALL_PREFIXES(AudioManagerTest, AudioDebugRecording); 264 FRIEND_TEST_ALL_PREFIXES(AudioManagerTest, AudioDebugRecording);
265 265
266 AudioManager(scoped_refptr<base::SingleThreadTaskRunner> task_runner, 266 AudioManager(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
267 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner); 267 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner);
268 virtual ~AudioManager();
269 268
270 // Initializes output debug recording. Can be called on any thread; will post 269 // Initializes output debug recording. Can be called on any thread; will post
271 // to the audio thread if not called on it. 270 // to the audio thread if not called on it.
272 virtual void InitializeOutputDebugRecording( 271 virtual void InitializeOutputDebugRecording(
273 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) = 0; 272 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) = 0;
274 273
275 private: 274 private:
276 friend class base::DeleteHelper<AudioManager>;
277 friend class AudioManagerDeleter;
278
279 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 275 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
280 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_; 276 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner_;
277 base::ThreadChecker thread_checker_;
281 DISALLOW_COPY_AND_ASSIGN(AudioManager); 278 DISALLOW_COPY_AND_ASSIGN(AudioManager);
282 }; 279 };
283 280
284 } // namespace media 281 } // namespace media
285 282
286 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_ 283 #endif // MEDIA_AUDIO_AUDIO_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698