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"
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"
21 #include "base/win/scoped_com_initializer.h"
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
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
{
49 : max_hung_task_time_(base::TimeDelta::FromMinutes(1)),
50 hang_detection_enabled_(true) {}
51 ~AudioManagerHelper() override
{}
54 const scoped_refptr
<base::SingleThreadTaskRunner
>& monitor_task_runner
) {
55 CHECK(!monitor_task_runner_
);
56 monitor_task_runner_
= monitor_task_runner
;
57 base::PowerMonitor::Get()->AddObserver(this);
59 UpdateLastAudioThreadTimeTick();
60 CrashOnAudioThreadHang();
63 // Disable hang detection when the system goes into the suspend state.
64 void OnSuspend() override
{
65 base::AutoLock
lock(hang_lock_
);
66 hang_detection_enabled_
= false;
70 // Reenable hang detection once the system comes out of the suspend state.
71 void OnResume() override
{
72 base::AutoLock
lock(hang_lock_
);
73 hang_detection_enabled_
= true;
74 last_audio_thread_timer_tick_
= base::TimeTicks::Now();
78 // Runs on |monitor_task_runner| typically, but may be started on any thread.
79 void CrashOnAudioThreadHang() {
81 base::AutoLock
lock(hang_lock_
);
83 // Don't attempt to verify the tick time if the system is in the process
84 // of suspending or resuming.
85 if (hang_detection_enabled_
) {
86 const base::TimeTicks now
= base::TimeTicks::Now();
87 const base::TimeDelta tick_delta
= now
- last_audio_thread_timer_tick_
;
88 if (tick_delta
> max_hung_task_time_
) {
89 if (++hang_failures_
>= kMaxHangFailureCount
) {
90 base::debug::Alias(&now
);
91 base::debug::Alias(&tick_delta
);
92 base::debug::Alias(&last_audio_thread_timer_tick_
);
101 // Don't hold the lock while posting the next task.
102 monitor_task_runner_
->PostDelayedTask(
103 FROM_HERE
, base::Bind(&AudioManagerHelper::CrashOnAudioThreadHang
,
104 base::Unretained(this)),
105 max_hung_task_time_
);
108 // Runs on the audio thread typically, but may be started on any thread.
109 void UpdateLastAudioThreadTimeTick() {
111 base::AutoLock
lock(hang_lock_
);
112 last_audio_thread_timer_tick_
= base::TimeTicks::Now();
116 // Don't hold the lock while posting the next task.
117 g_last_created
->GetTaskRunner()->PostDelayedTask(
119 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick
,
120 base::Unretained(this)),
121 max_hung_task_time_
/ 10);
124 AudioLogFactory
* fake_log_factory() { return &fake_log_factory_
; }
127 // This should be called before creating an AudioManager in tests to ensure
128 // that the creating thread is COM initialized.
129 void InitializeCOMForTesting() {
130 com_initializer_for_testing_
.reset(new base::win::ScopedCOMInitializer());
135 FakeAudioLogFactory fake_log_factory_
;
137 const base::TimeDelta max_hung_task_time_
;
138 scoped_refptr
<base::SingleThreadTaskRunner
> monitor_task_runner_
;
140 base::Lock hang_lock_
;
141 bool hang_detection_enabled_
;
142 base::TimeTicks last_audio_thread_timer_tick_
;
146 scoped_ptr
<base::win::ScopedCOMInitializer
> com_initializer_for_testing_
;
149 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper
);
152 static bool g_hang_monitor_enabled
= false;
154 static base::LazyInstance
<AudioManagerHelper
>::Leaky g_helper
=
155 LAZY_INSTANCE_INITIALIZER
;
158 // Forward declaration of the platform specific AudioManager factory function.
159 AudioManager
* CreateAudioManager(AudioLogFactory
* audio_log_factory
);
161 AudioManager::AudioManager() {}
163 AudioManager::~AudioManager() {
164 CHECK(!g_last_created
|| g_last_created
== this);
165 g_last_created
= NULL
;
169 void AudioManager::SetFactory(AudioManagerFactory
* factory
) {
171 CHECK(!g_last_created
);
172 CHECK(!g_audio_manager_factory
);
173 g_audio_manager_factory
= factory
;
177 void AudioManager::ResetFactoryForTesting() {
178 if (g_audio_manager_factory
) {
179 delete g_audio_manager_factory
;
180 g_audio_manager_factory
= nullptr;
185 AudioManager
* AudioManager::Create(AudioLogFactory
* audio_log_factory
) {
186 CHECK(!g_last_created
);
187 if (g_audio_manager_factory
)
188 g_last_created
= g_audio_manager_factory
->CreateInstance(audio_log_factory
);
190 g_last_created
= CreateAudioManager(audio_log_factory
);
192 return g_last_created
;
196 AudioManager
* AudioManager::CreateWithHangTimer(
197 AudioLogFactory
* audio_log_factory
,
198 const scoped_refptr
<base::SingleThreadTaskRunner
>& monitor_task_runner
) {
199 AudioManager
* manager
= Create(audio_log_factory
);
200 if (g_hang_monitor_enabled
||
201 base::CommandLine::ForCurrentProcess()->HasSwitch(
202 switches::kEnableAudioHangMonitor
)) {
203 g_helper
.Pointer()->StartHangTimer(monitor_task_runner
);
209 AudioManager
* AudioManager::CreateForTesting() {
211 g_helper
.Pointer()->InitializeCOMForTesting();
213 return Create(g_helper
.Pointer()->fake_log_factory());
217 void AudioManager::EnableHangMonitor() {
218 CHECK(!g_last_created
);
219 // On OSX the audio thread is the UI thread, for which a hang monitor is not
220 // necessary or recommended. If it's manually requested, we should allow it
222 #if !defined(OS_MACOSX)
223 g_hang_monitor_enabled
= true;
228 AudioManager
* AudioManager::Get() {
229 return g_last_created
;