Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / media / audio / audio_manager.cc
blob9e2ac35f9ceab9b7c06971a11bcb8d85d28270cb
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/debug/alias.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/power_monitor/power_monitor.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "build/build_config.h"
16 #include "media/audio/audio_manager_factory.h"
17 #include "media/audio/fake_audio_log_factory.h"
19 namespace media {
20 namespace {
22 // The singleton instance of AudioManager. This is set when Create() is called.
23 AudioManager* g_last_created = NULL;
25 // The singleton instance of AudioManagerFactory. This is only set if
26 // SetFactory() is called. If it is set when Create() is called, its
27 // CreateInstance() function is used to set |g_last_created|. Otherwise, the
28 // linked implementation of media::CreateAudioManager is used to set
29 // |g_last_created|.
30 AudioManagerFactory* g_audio_manager_factory = NULL;
32 // Maximum number of failed pings to the audio thread allowed. A crash will be
33 // issued once this count is reached. We require at least two pings before
34 // crashing to ensure unobservable power events aren't mistakenly caught (e.g.,
35 // the system suspends before a OnSuspend() event can be fired.).
36 const int kMaxHangFailureCount = 2;
38 // Helper class for managing global AudioManager data and hang timers. If the
39 // audio thread is unresponsive for more than two minutes we want to crash the
40 // process so we can catch offenders quickly in the field.
41 class AudioManagerHelper : public base::PowerObserver {
42 public:
43 AudioManagerHelper()
44 : max_hung_task_time_(base::TimeDelta::FromMinutes(1)),
45 hang_detection_enabled_(true) {}
46 ~AudioManagerHelper() override {}
48 void StartHangTimer(
49 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) {
50 CHECK(!monitor_task_runner_);
51 monitor_task_runner_ = monitor_task_runner;
52 base::PowerMonitor::Get()->AddObserver(this);
53 hang_failures_ = 0;
54 UpdateLastAudioThreadTimeTick();
55 CrashOnAudioThreadHang();
58 // Disable hang detection when the system goes into the suspend state.
59 void OnSuspend() override {
60 base::AutoLock lock(hang_lock_);
61 hang_detection_enabled_ = false;
62 hang_failures_ = 0;
65 // Reenable hang detection once the system comes out of the suspend state.
66 void OnResume() override {
67 base::AutoLock lock(hang_lock_);
68 hang_detection_enabled_ = true;
69 last_audio_thread_timer_tick_ = base::TimeTicks::Now();
70 hang_failures_ = 0;
73 // Runs on |monitor_task_runner| typically, but may be started on any thread.
74 void CrashOnAudioThreadHang() {
76 base::AutoLock lock(hang_lock_);
78 // Don't attempt to verify the tick time if the system is in the process
79 // of suspending or resuming.
80 if (hang_detection_enabled_) {
81 const base::TimeTicks now = base::TimeTicks::Now();
82 const base::TimeDelta tick_delta = now - last_audio_thread_timer_tick_;
83 if (tick_delta > max_hung_task_time_) {
84 if (++hang_failures_ >= kMaxHangFailureCount) {
85 base::debug::Alias(&now);
86 base::debug::Alias(&tick_delta);
87 base::debug::Alias(&last_audio_thread_timer_tick_);
88 CHECK(false);
90 } else {
91 hang_failures_ = 0;
96 // Don't hold the lock while posting the next task.
97 monitor_task_runner_->PostDelayedTask(
98 FROM_HERE, base::Bind(&AudioManagerHelper::CrashOnAudioThreadHang,
99 base::Unretained(this)),
100 max_hung_task_time_);
103 // Runs on the audio thread typically, but may be started on any thread.
104 void UpdateLastAudioThreadTimeTick() {
106 base::AutoLock lock(hang_lock_);
107 last_audio_thread_timer_tick_ = base::TimeTicks::Now();
108 hang_failures_ = 0;
111 // Don't hold the lock while posting the next task.
112 g_last_created->GetTaskRunner()->PostDelayedTask(
113 FROM_HERE,
114 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick,
115 base::Unretained(this)),
116 max_hung_task_time_ / 10);
119 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; }
121 private:
122 FakeAudioLogFactory fake_log_factory_;
124 const base::TimeDelta max_hung_task_time_;
125 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_;
127 base::Lock hang_lock_;
128 bool hang_detection_enabled_;
129 base::TimeTicks last_audio_thread_timer_tick_;
130 int hang_failures_;
132 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper);
135 static base::LazyInstance<AudioManagerHelper>::Leaky g_helper =
136 LAZY_INSTANCE_INITIALIZER;
137 } // namespace
139 // Forward declaration of the platform specific AudioManager factory function.
140 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory);
142 AudioManager::AudioManager() {}
144 AudioManager::~AudioManager() {
145 CHECK(!g_last_created || g_last_created == this);
146 g_last_created = NULL;
149 // static
150 void AudioManager::SetFactory(AudioManagerFactory* factory) {
151 CHECK(factory);
152 CHECK(!g_last_created);
153 CHECK(!g_audio_manager_factory);
154 g_audio_manager_factory = factory;
157 // static
158 void AudioManager::ResetFactoryForTesting() {
159 if (g_audio_manager_factory) {
160 delete g_audio_manager_factory;
161 g_audio_manager_factory = nullptr;
165 // static
166 AudioManager* AudioManager::Create(AudioLogFactory* audio_log_factory) {
167 CHECK(!g_last_created);
168 if (g_audio_manager_factory)
169 g_last_created = g_audio_manager_factory->CreateInstance(audio_log_factory);
170 else
171 g_last_created = CreateAudioManager(audio_log_factory);
173 return g_last_created;
176 // static
177 AudioManager* AudioManager::CreateWithHangTimer(
178 AudioLogFactory* audio_log_factory,
179 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) {
180 AudioManager* manager = Create(audio_log_factory);
181 // On OSX the audio thread is the UI thread, for which a hang monitor is not
182 // necessary.
183 #if !defined(OS_MACOSX)
184 g_helper.Pointer()->StartHangTimer(monitor_task_runner);
185 #endif
186 return manager;
189 // static
190 AudioManager* AudioManager::CreateForTesting() {
191 AudioManager* manager = Create(g_helper.Pointer()->fake_log_factory());
193 // When created for testing, always ensure all methods are ready to run (even
194 // if they end up called from other threads.
195 if (!manager->GetTaskRunner()->BelongsToCurrentThread()) {
196 base::WaitableEvent event(false, false);
197 manager->GetTaskRunner()->PostTask(
198 FROM_HERE,
199 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)));
200 event.Wait();
203 return manager;
206 // static
207 AudioManager* AudioManager::Get() {
208 return g_last_created;
211 } // namespace media