Revert "Omit calls to set composing region when pasting image."
[chromium-blink-merge.git] / media / audio / audio_manager.cc
blobf39b82b522793e6a29a2df6f8e3383b0fff066bc
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
3 // found in the LICENSE file.
5 #include "media/audio/audio_manager.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/debug/alias.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/power_monitor/power_monitor.h"
15 #include "build/build_config.h"
16 #include "media/audio/audio_manager_factory.h"
17 #include "media/audio/fake_audio_log_factory.h"
18 #include "media/base/media_switches.h"
20 #if defined(OS_WIN)
21 #include "base/win/scoped_com_initializer.h"
22 #endif
24 namespace media {
25 namespace {
27 // The singleton instance of AudioManager. This is set when Create() is called.
28 AudioManager* g_last_created = nullptr;
30 // The singleton instance of AudioManagerFactory. This is only set if
31 // SetFactory() is called. If it is set when Create() is called, its
32 // CreateInstance() function is used to set |g_last_created|. Otherwise, the
33 // linked implementation of media::CreateAudioManager is used to set
34 // |g_last_created|.
35 AudioManagerFactory* g_audio_manager_factory = nullptr;
37 // Maximum number of failed pings to the audio thread allowed. A crash will be
38 // issued once this count is reached. We require at least two pings before
39 // crashing to ensure unobservable power events aren't mistakenly caught (e.g.,
40 // the system suspends before a OnSuspend() event can be fired.).
41 const int kMaxHangFailureCount = 2;
43 // Helper class for managing global AudioManager data and hang timers. If the
44 // audio thread is unresponsive for more than two minutes we want to crash the
45 // process so we can catch offenders quickly in the field.
46 class AudioManagerHelper : public base::PowerObserver {
47 public:
48 AudioManagerHelper()
49 : max_hung_task_time_(base::TimeDelta::FromMinutes(1)),
50 hang_detection_enabled_(true),
51 io_task_running_(false),
52 audio_task_running_(false) {}
53 ~AudioManagerHelper() override {}
55 void StartHangTimer(
56 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) {
57 CHECK(!monitor_task_runner_);
58 monitor_task_runner_ = monitor_task_runner;
59 base::PowerMonitor::Get()->AddObserver(this);
60 hang_failures_ = 0;
61 io_task_running_ = audio_task_running_ = true;
62 UpdateLastAudioThreadTimeTick();
63 CrashOnAudioThreadHang();
66 // Disable hang detection when the system goes into the suspend state.
67 void OnSuspend() override {
68 base::AutoLock lock(hang_lock_);
69 hang_detection_enabled_ = false;
70 hang_failures_ = 0;
73 // Reenable hang detection once the system comes out of the suspend state.
74 void OnResume() override {
75 base::AutoLock lock(hang_lock_);
76 hang_detection_enabled_ = true;
77 last_audio_thread_timer_tick_ = base::TimeTicks::Now();
78 hang_failures_ = 0;
80 // If either of the tasks were stopped during suspend, start them now.
81 if (!audio_task_running_) {
82 audio_task_running_ = true;
84 base::AutoUnlock unlock(hang_lock_);
85 UpdateLastAudioThreadTimeTick();
88 if (!io_task_running_) {
89 io_task_running_ = true;
91 base::AutoUnlock unlock(hang_lock_);
92 CrashOnAudioThreadHang();
96 // Runs on |monitor_task_runner| typically, but may be started on any thread.
97 void CrashOnAudioThreadHang() {
99 base::AutoLock lock(hang_lock_);
101 // Don't attempt to verify the tick time or post our task if the system is
102 // in the process of suspending or resuming.
103 if (!hang_detection_enabled_) {
104 io_task_running_ = false;
105 return;
108 DCHECK(io_task_running_);
109 const base::TimeTicks now = base::TimeTicks::Now();
110 const base::TimeDelta tick_delta = now - last_audio_thread_timer_tick_;
111 if (tick_delta > max_hung_task_time_) {
112 CHECK_LT(++hang_failures_, kMaxHangFailureCount);
113 } else {
114 hang_failures_ = 0;
118 // Don't hold the lock while posting the next task.
119 monitor_task_runner_->PostDelayedTask(
120 FROM_HERE, base::Bind(&AudioManagerHelper::CrashOnAudioThreadHang,
121 base::Unretained(this)),
122 max_hung_task_time_);
125 // Runs on the audio thread typically, but may be started on any thread.
126 void UpdateLastAudioThreadTimeTick() {
128 base::AutoLock lock(hang_lock_);
129 last_audio_thread_timer_tick_ = base::TimeTicks::Now();
130 hang_failures_ = 0;
132 // Don't post our task if the system is or will be suspended.
133 if (!hang_detection_enabled_) {
134 audio_task_running_ = false;
135 return;
138 DCHECK(audio_task_running_);
141 // Don't hold the lock while posting the next task.
142 g_last_created->GetTaskRunner()->PostDelayedTask(
143 FROM_HERE,
144 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick,
145 base::Unretained(this)),
146 max_hung_task_time_ / 5);
149 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; }
151 #if defined(OS_WIN)
152 // This should be called before creating an AudioManager in tests to ensure
153 // that the creating thread is COM initialized.
154 void InitializeCOMForTesting() {
155 com_initializer_for_testing_.reset(new base::win::ScopedCOMInitializer());
157 #endif
159 #if defined(OS_LINUX)
160 void set_app_name(const std::string& app_name) {
161 app_name_ = app_name;
164 const std::string& app_name() const {
165 return app_name_;
167 #endif
169 private:
170 FakeAudioLogFactory fake_log_factory_;
172 const base::TimeDelta max_hung_task_time_;
173 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_;
175 base::Lock hang_lock_;
176 bool hang_detection_enabled_;
177 base::TimeTicks last_audio_thread_timer_tick_;
178 int hang_failures_;
179 bool io_task_running_;
180 bool audio_task_running_;
182 #if defined(OS_WIN)
183 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_for_testing_;
184 #endif
186 #if defined(OS_LINUX)
187 std::string app_name_;
188 #endif
190 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper);
193 bool g_hang_monitor_enabled = false;
195 base::LazyInstance<AudioManagerHelper>::Leaky g_helper =
196 LAZY_INSTANCE_INITIALIZER;
198 } // namespace
200 // Forward declaration of the platform specific AudioManager factory function.
201 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory);
203 AudioManager::AudioManager() {}
205 AudioManager::~AudioManager() {
206 CHECK(!g_last_created || g_last_created == this);
207 g_last_created = nullptr;
210 // static
211 void AudioManager::SetFactory(AudioManagerFactory* factory) {
212 CHECK(factory);
213 CHECK(!g_last_created);
214 CHECK(!g_audio_manager_factory);
215 g_audio_manager_factory = factory;
218 // static
219 void AudioManager::ResetFactoryForTesting() {
220 if (g_audio_manager_factory) {
221 delete g_audio_manager_factory;
222 g_audio_manager_factory = nullptr;
226 // static
227 AudioManager* AudioManager::Create(AudioLogFactory* audio_log_factory) {
228 CHECK(!g_last_created);
229 if (g_audio_manager_factory)
230 g_last_created = g_audio_manager_factory->CreateInstance(audio_log_factory);
231 else
232 g_last_created = CreateAudioManager(audio_log_factory);
234 return g_last_created;
237 // static
238 AudioManager* AudioManager::CreateWithHangTimer(
239 AudioLogFactory* audio_log_factory,
240 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) {
241 AudioManager* manager = Create(audio_log_factory);
242 if (g_hang_monitor_enabled ||
243 base::CommandLine::ForCurrentProcess()->HasSwitch(
244 switches::kEnableAudioHangMonitor)) {
245 g_helper.Pointer()->StartHangTimer(monitor_task_runner);
247 return manager;
250 // static
251 AudioManager* AudioManager::CreateForTesting() {
252 #if defined(OS_WIN)
253 g_helper.Pointer()->InitializeCOMForTesting();
254 #endif
255 return Create(g_helper.Pointer()->fake_log_factory());
258 // static
259 void AudioManager::EnableHangMonitor() {
260 CHECK(!g_last_created);
261 // On OSX the audio thread is the UI thread, for which a hang monitor is not
262 // necessary or recommended. If it's manually requested, we should allow it
263 // to start though.
264 #if !defined(OS_MACOSX)
265 g_hang_monitor_enabled = true;
266 #endif
269 #if defined(OS_LINUX)
270 // static
271 void AudioManager::SetGlobalAppName(const std::string& app_name) {
272 g_helper.Pointer()->set_app_name(app_name);
275 // static
276 const std::string& AudioManager::GetGlobalAppName() {
277 return g_helper.Pointer()->app_name();
279 #endif
281 // static
282 AudioManager* AudioManager::Get() {
283 return g_last_created;
286 } // namespace media