Revert of Sandbox html_viewer on Linux. (patchset #6 id:100001 of https://codereview...
[chromium-blink-merge.git] / media / audio / audio_manager.cc
blob0c2aeb878929f199c91472a7e3b9175baddd7633
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 = NULL;
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 = NULL;
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 private:
160 FakeAudioLogFactory fake_log_factory_;
162 const base::TimeDelta max_hung_task_time_;
163 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_;
165 base::Lock hang_lock_;
166 bool hang_detection_enabled_;
167 base::TimeTicks last_audio_thread_timer_tick_;
168 int hang_failures_;
169 bool io_task_running_;
170 bool audio_task_running_;
172 #if defined(OS_WIN)
173 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_for_testing_;
174 #endif
176 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper);
179 static bool g_hang_monitor_enabled = false;
181 static base::LazyInstance<AudioManagerHelper>::Leaky g_helper =
182 LAZY_INSTANCE_INITIALIZER;
183 } // namespace
185 // Forward declaration of the platform specific AudioManager factory function.
186 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory);
188 AudioManager::AudioManager() {}
190 AudioManager::~AudioManager() {
191 CHECK(!g_last_created || g_last_created == this);
192 g_last_created = NULL;
195 // static
196 void AudioManager::SetFactory(AudioManagerFactory* factory) {
197 CHECK(factory);
198 CHECK(!g_last_created);
199 CHECK(!g_audio_manager_factory);
200 g_audio_manager_factory = factory;
203 // static
204 void AudioManager::ResetFactoryForTesting() {
205 if (g_audio_manager_factory) {
206 delete g_audio_manager_factory;
207 g_audio_manager_factory = nullptr;
211 // static
212 AudioManager* AudioManager::Create(AudioLogFactory* audio_log_factory) {
213 CHECK(!g_last_created);
214 if (g_audio_manager_factory)
215 g_last_created = g_audio_manager_factory->CreateInstance(audio_log_factory);
216 else
217 g_last_created = CreateAudioManager(audio_log_factory);
219 return g_last_created;
222 // static
223 AudioManager* AudioManager::CreateWithHangTimer(
224 AudioLogFactory* audio_log_factory,
225 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) {
226 AudioManager* manager = Create(audio_log_factory);
227 if (g_hang_monitor_enabled ||
228 base::CommandLine::ForCurrentProcess()->HasSwitch(
229 switches::kEnableAudioHangMonitor)) {
230 g_helper.Pointer()->StartHangTimer(monitor_task_runner);
232 return manager;
235 // static
236 AudioManager* AudioManager::CreateForTesting() {
237 #if defined(OS_WIN)
238 g_helper.Pointer()->InitializeCOMForTesting();
239 #endif
240 return Create(g_helper.Pointer()->fake_log_factory());
243 // static
244 void AudioManager::EnableHangMonitor() {
245 CHECK(!g_last_created);
246 // On OSX the audio thread is the UI thread, for which a hang monitor is not
247 // necessary or recommended. If it's manually requested, we should allow it
248 // to start though.
249 #if !defined(OS_MACOSX)
250 g_hang_monitor_enabled = true;
251 #endif
254 // static
255 AudioManager* AudioManager::Get() {
256 return g_last_created;
259 } // namespace media