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/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 "build/build_config.h"
15 #include "media/audio/fake_audio_log_factory.h"
19 AudioManager
* g_last_created
= NULL
;
21 // Maximum number of failed pings to the audio thread allowed. A crash will be
22 // issued once this count is reached. We require at least two pings before
23 // crashing to ensure unobservable power events aren't mistakenly caught (e.g.,
24 // the system suspends before a OnSuspend() event can be fired.).
25 const int kMaxHangFailureCount
= 2;
27 // Helper class for managing global AudioManager data and hang timers. If the
28 // audio thread is unresponsive for more than two minutes we want to crash the
29 // process so we can catch offenders quickly in the field.
30 class AudioManagerHelper
: public base::PowerObserver
{
33 : max_hung_task_time_(base::TimeDelta::FromMinutes(1)),
34 hang_detection_enabled_(true) {}
35 ~AudioManagerHelper() override
{}
38 const scoped_refptr
<base::SingleThreadTaskRunner
>& monitor_task_runner
) {
39 CHECK(!monitor_task_runner_
);
40 monitor_task_runner_
= monitor_task_runner
;
41 base::PowerMonitor::Get()->AddObserver(this);
43 UpdateLastAudioThreadTimeTick();
44 CrashOnAudioThreadHang();
47 // Disable hang detection when the system goes into the suspend state.
48 void OnSuspend() override
{
49 base::AutoLock
lock(hang_lock_
);
50 hang_detection_enabled_
= false;
54 // Reenable hang detection once the system comes out of the suspend state.
55 void OnResume() override
{
56 base::AutoLock
lock(hang_lock_
);
57 hang_detection_enabled_
= true;
58 last_audio_thread_timer_tick_
= base::TimeTicks::Now();
62 // Runs on |monitor_task_runner| typically, but may be started on any thread.
63 void CrashOnAudioThreadHang() {
65 base::AutoLock
lock(hang_lock_
);
67 // Don't attempt to verify the tick time if the system is in the process
68 // of suspending or resuming.
69 if (hang_detection_enabled_
) {
70 const base::TimeTicks now
= base::TimeTicks::Now();
71 const base::TimeDelta tick_delta
= now
- last_audio_thread_timer_tick_
;
72 if (tick_delta
> max_hung_task_time_
) {
73 if (++hang_failures_
>= kMaxHangFailureCount
) {
74 base::debug::Alias(&now
);
75 base::debug::Alias(&tick_delta
);
76 base::debug::Alias(&last_audio_thread_timer_tick_
);
85 // Don't hold the lock while posting the next task.
86 monitor_task_runner_
->PostDelayedTask(
87 FROM_HERE
, base::Bind(&AudioManagerHelper::CrashOnAudioThreadHang
,
88 base::Unretained(this)),
92 // Runs on the audio thread typically, but may be started on any thread.
93 void UpdateLastAudioThreadTimeTick() {
95 base::AutoLock
lock(hang_lock_
);
96 last_audio_thread_timer_tick_
= base::TimeTicks::Now();
100 // Don't hold the lock while posting the next task.
101 g_last_created
->GetTaskRunner()->PostDelayedTask(
103 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick
,
104 base::Unretained(this)),
105 max_hung_task_time_
/ 10);
108 AudioLogFactory
* fake_log_factory() { return &fake_log_factory_
; }
111 FakeAudioLogFactory fake_log_factory_
;
113 const base::TimeDelta max_hung_task_time_
;
114 scoped_refptr
<base::SingleThreadTaskRunner
> monitor_task_runner_
;
116 base::Lock hang_lock_
;
117 bool hang_detection_enabled_
;
118 base::TimeTicks last_audio_thread_timer_tick_
;
121 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper
);
124 static base::LazyInstance
<AudioManagerHelper
>::Leaky g_helper
=
125 LAZY_INSTANCE_INITIALIZER
;
128 // Forward declaration of the platform specific AudioManager factory function.
129 AudioManager
* CreateAudioManager(AudioLogFactory
* audio_log_factory
);
131 AudioManager::AudioManager() {}
133 AudioManager::~AudioManager() {
134 CHECK(!g_last_created
|| g_last_created
== this);
135 g_last_created
= NULL
;
139 AudioManager
* AudioManager::Create(AudioLogFactory
* audio_log_factory
) {
140 CHECK(!g_last_created
);
141 g_last_created
= CreateAudioManager(audio_log_factory
);
142 return g_last_created
;
146 AudioManager
* AudioManager::CreateWithHangTimer(
147 AudioLogFactory
* audio_log_factory
,
148 const scoped_refptr
<base::SingleThreadTaskRunner
>& monitor_task_runner
) {
149 AudioManager
* manager
= Create(audio_log_factory
);
150 // On OSX the audio thread is the UI thread, for which a hang monitor is not
152 #if !defined(OS_MACOSX)
153 g_helper
.Pointer()->StartHangTimer(monitor_task_runner
);
159 AudioManager
* AudioManager::CreateForTesting() {
160 return Create(g_helper
.Pointer()->fake_log_factory());
164 AudioManager
* AudioManager::Get() {
165 return g_last_created
;