Disable draw_properties benchmark on Android.
[chromium-blink-merge.git] / media / audio / audio_manager.cc
blob6ee61d16cd7bd6ad3beb3a2cf912b7f4ac1aeffe
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 "build/build_config.h"
15 #include "media/audio/fake_audio_log_factory.h"
17 namespace media {
18 namespace {
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 {
31 public:
32 AudioManagerHelper()
33 : max_hung_task_time_(base::TimeDelta::FromMinutes(1)),
34 hang_detection_enabled_(true) {}
35 ~AudioManagerHelper() override {}
37 void StartHangTimer(
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);
42 hang_failures_ = 0;
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;
51 hang_failures_ = 0;
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();
59 hang_failures_ = 0;
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_);
77 CHECK(false);
79 } else {
80 hang_failures_ = 0;
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)),
89 max_hung_task_time_);
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();
97 hang_failures_ = 0;
100 // Don't hold the lock while posting the next task.
101 g_last_created->GetTaskRunner()->PostDelayedTask(
102 FROM_HERE,
103 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick,
104 base::Unretained(this)),
105 max_hung_task_time_ / 10);
108 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; }
110 private:
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_;
119 int hang_failures_;
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;
138 // static
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;
145 // static
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
151 // necessary.
152 #if !defined(OS_MACOSX)
153 g_helper.Pointer()->StartHangTimer(monitor_task_runner);
154 #endif
155 return manager;
158 // static
159 AudioManager* AudioManager::CreateForTesting() {
160 return Create(g_helper.Pointer()->fake_log_factory());
163 // static
164 AudioManager* AudioManager::Get() {
165 return g_last_created;
168 } // namespace media