Fix the Android Mandoline ui build.
[chromium-blink-merge.git] / media / audio / audio_manager.cc
blob97076affb68de799697226aa81b1ef84d60c1a86
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 namespace media {
21 namespace {
23 // The singleton instance of AudioManager. This is set when Create() is called.
24 AudioManager* g_last_created = NULL;
26 // The singleton instance of AudioManagerFactory. This is only set if
27 // SetFactory() is called. If it is set when Create() is called, its
28 // CreateInstance() function is used to set |g_last_created|. Otherwise, the
29 // linked implementation of media::CreateAudioManager is used to set
30 // |g_last_created|.
31 AudioManagerFactory* g_audio_manager_factory = NULL;
33 // Maximum number of failed pings to the audio thread allowed. A crash will be
34 // issued once this count is reached. We require at least two pings before
35 // crashing to ensure unobservable power events aren't mistakenly caught (e.g.,
36 // the system suspends before a OnSuspend() event can be fired.).
37 const int kMaxHangFailureCount = 2;
39 // Helper class for managing global AudioManager data and hang timers. If the
40 // audio thread is unresponsive for more than two minutes we want to crash the
41 // process so we can catch offenders quickly in the field.
42 class AudioManagerHelper : public base::PowerObserver {
43 public:
44 AudioManagerHelper()
45 : max_hung_task_time_(base::TimeDelta::FromMinutes(1)),
46 hang_detection_enabled_(true) {}
47 ~AudioManagerHelper() override {}
49 void StartHangTimer(
50 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) {
51 CHECK(!monitor_task_runner_);
52 monitor_task_runner_ = monitor_task_runner;
53 base::PowerMonitor::Get()->AddObserver(this);
54 hang_failures_ = 0;
55 UpdateLastAudioThreadTimeTick();
56 CrashOnAudioThreadHang();
59 // Disable hang detection when the system goes into the suspend state.
60 void OnSuspend() override {
61 base::AutoLock lock(hang_lock_);
62 hang_detection_enabled_ = false;
63 hang_failures_ = 0;
66 // Reenable hang detection once the system comes out of the suspend state.
67 void OnResume() override {
68 base::AutoLock lock(hang_lock_);
69 hang_detection_enabled_ = true;
70 last_audio_thread_timer_tick_ = base::TimeTicks::Now();
71 hang_failures_ = 0;
74 // Runs on |monitor_task_runner| typically, but may be started on any thread.
75 void CrashOnAudioThreadHang() {
77 base::AutoLock lock(hang_lock_);
79 // Don't attempt to verify the tick time if the system is in the process
80 // of suspending or resuming.
81 if (hang_detection_enabled_) {
82 const base::TimeTicks now = base::TimeTicks::Now();
83 const base::TimeDelta tick_delta = now - last_audio_thread_timer_tick_;
84 if (tick_delta > max_hung_task_time_) {
85 if (++hang_failures_ >= kMaxHangFailureCount) {
86 base::debug::Alias(&now);
87 base::debug::Alias(&tick_delta);
88 base::debug::Alias(&last_audio_thread_timer_tick_);
89 CHECK(false);
91 } else {
92 hang_failures_ = 0;
97 // Don't hold the lock while posting the next task.
98 monitor_task_runner_->PostDelayedTask(
99 FROM_HERE, base::Bind(&AudioManagerHelper::CrashOnAudioThreadHang,
100 base::Unretained(this)),
101 max_hung_task_time_);
104 // Runs on the audio thread typically, but may be started on any thread.
105 void UpdateLastAudioThreadTimeTick() {
107 base::AutoLock lock(hang_lock_);
108 last_audio_thread_timer_tick_ = base::TimeTicks::Now();
109 hang_failures_ = 0;
112 // Don't hold the lock while posting the next task.
113 g_last_created->GetTaskRunner()->PostDelayedTask(
114 FROM_HERE,
115 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick,
116 base::Unretained(this)),
117 max_hung_task_time_ / 10);
120 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; }
122 private:
123 FakeAudioLogFactory fake_log_factory_;
125 const base::TimeDelta max_hung_task_time_;
126 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_;
128 base::Lock hang_lock_;
129 bool hang_detection_enabled_;
130 base::TimeTicks last_audio_thread_timer_tick_;
131 int hang_failures_;
133 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper);
136 static bool g_hang_monitor_enabled = false;
138 static base::LazyInstance<AudioManagerHelper>::Leaky g_helper =
139 LAZY_INSTANCE_INITIALIZER;
140 } // namespace
142 // Forward declaration of the platform specific AudioManager factory function.
143 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory);
145 AudioManager::AudioManager() {}
147 AudioManager::~AudioManager() {
148 CHECK(!g_last_created || g_last_created == this);
149 g_last_created = NULL;
152 // static
153 void AudioManager::SetFactory(AudioManagerFactory* factory) {
154 CHECK(factory);
155 CHECK(!g_last_created);
156 CHECK(!g_audio_manager_factory);
157 g_audio_manager_factory = factory;
160 // static
161 void AudioManager::ResetFactoryForTesting() {
162 if (g_audio_manager_factory) {
163 delete g_audio_manager_factory;
164 g_audio_manager_factory = nullptr;
168 // static
169 AudioManager* AudioManager::Create(AudioLogFactory* audio_log_factory) {
170 CHECK(!g_last_created);
171 if (g_audio_manager_factory)
172 g_last_created = g_audio_manager_factory->CreateInstance(audio_log_factory);
173 else
174 g_last_created = CreateAudioManager(audio_log_factory);
176 return g_last_created;
179 // static
180 AudioManager* AudioManager::CreateWithHangTimer(
181 AudioLogFactory* audio_log_factory,
182 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) {
183 AudioManager* manager = Create(audio_log_factory);
184 if (g_hang_monitor_enabled ||
185 base::CommandLine::ForCurrentProcess()->HasSwitch(
186 switches::kEnableAudioHangMonitor)) {
187 g_helper.Pointer()->StartHangTimer(monitor_task_runner);
189 return manager;
192 // static
193 AudioManager* AudioManager::CreateForTesting() {
194 return Create(g_helper.Pointer()->fake_log_factory());
197 // static
198 void AudioManager::EnableHangMonitor() {
199 CHECK(!g_last_created);
200 // On OSX the audio thread is the UI thread, for which a hang monitor is not
201 // necessary or recommended. If it's manually requested, we should allow it
202 // to start though.
203 #if !defined(OS_MACOSX)
204 g_hang_monitor_enabled = true;
205 #endif
208 // static
209 AudioManager* AudioManager::Get() {
210 return g_last_created;
213 } // namespace media