OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/audio/audio_manager.h" | 5 #include "media/audio/audio_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/debug/alias.h" | 9 #include "base/debug/alias.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/power_monitor/power_monitor.h" | 13 #include "base/power_monitor/power_monitor.h" |
14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "media/audio/audio_manager_factory.h" |
15 #include "media/audio/fake_audio_log_factory.h" | 16 #include "media/audio/fake_audio_log_factory.h" |
16 | 17 |
17 namespace media { | 18 namespace media { |
18 namespace { | 19 namespace { |
| 20 |
| 21 // The singleton instance of AudioManager. This is set when Create() is called. |
19 AudioManager* g_last_created = NULL; | 22 AudioManager* g_last_created = NULL; |
20 | 23 |
| 24 // The singleton instance of AudioManagerFactory. This is only set if |
| 25 // SetFactory() is called. If it is set when Create() is called, its |
| 26 // CreateInstance() function is used to set |g_last_created|. Otherwise, the |
| 27 // linked implementation of media::CreateAudioManager is used to set |
| 28 // |g_last_created|. |
| 29 AudioManagerFactory* g_audio_manager_factory = NULL; |
| 30 |
21 // Maximum number of failed pings to the audio thread allowed. A crash will be | 31 // 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 | 32 // 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., | 33 // crashing to ensure unobservable power events aren't mistakenly caught (e.g., |
24 // the system suspends before a OnSuspend() event can be fired.). | 34 // the system suspends before a OnSuspend() event can be fired.). |
25 const int kMaxHangFailureCount = 2; | 35 const int kMaxHangFailureCount = 2; |
26 | 36 |
27 // Helper class for managing global AudioManager data and hang timers. If the | 37 // 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 | 38 // 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. | 39 // process so we can catch offenders quickly in the field. |
30 class AudioManagerHelper : public base::PowerObserver { | 40 class AudioManagerHelper : public base::PowerObserver { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 base::Lock hang_lock_; | 126 base::Lock hang_lock_; |
117 bool hang_detection_enabled_; | 127 bool hang_detection_enabled_; |
118 base::TimeTicks last_audio_thread_timer_tick_; | 128 base::TimeTicks last_audio_thread_timer_tick_; |
119 int hang_failures_; | 129 int hang_failures_; |
120 | 130 |
121 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); | 131 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); |
122 }; | 132 }; |
123 | 133 |
124 static base::LazyInstance<AudioManagerHelper>::Leaky g_helper = | 134 static base::LazyInstance<AudioManagerHelper>::Leaky g_helper = |
125 LAZY_INSTANCE_INITIALIZER; | 135 LAZY_INSTANCE_INITIALIZER; |
126 } | 136 } // namespace |
127 | 137 |
128 // Forward declaration of the platform specific AudioManager factory function. | 138 // Forward declaration of the platform specific AudioManager factory function. |
129 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory); | 139 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory); |
130 | 140 |
131 AudioManager::AudioManager() {} | 141 AudioManager::AudioManager() {} |
132 | 142 |
133 AudioManager::~AudioManager() { | 143 AudioManager::~AudioManager() { |
134 CHECK(!g_last_created || g_last_created == this); | 144 CHECK(!g_last_created || g_last_created == this); |
135 g_last_created = NULL; | 145 g_last_created = NULL; |
136 } | 146 } |
137 | 147 |
138 // static | 148 // static |
| 149 void AudioManager::SetFactory(AudioManagerFactory* factory) { |
| 150 CHECK(factory); |
| 151 CHECK(!g_last_created); |
| 152 CHECK(!g_audio_manager_factory); |
| 153 g_audio_manager_factory = factory; |
| 154 } |
| 155 |
| 156 // static |
| 157 void AudioManager::ResetFactoryForTesting() { |
| 158 if (g_audio_manager_factory) { |
| 159 delete g_audio_manager_factory; |
| 160 g_audio_manager_factory = nullptr; |
| 161 } |
| 162 } |
| 163 |
| 164 // static |
139 AudioManager* AudioManager::Create(AudioLogFactory* audio_log_factory) { | 165 AudioManager* AudioManager::Create(AudioLogFactory* audio_log_factory) { |
140 CHECK(!g_last_created); | 166 CHECK(!g_last_created); |
141 g_last_created = CreateAudioManager(audio_log_factory); | 167 if (g_audio_manager_factory) |
| 168 g_last_created = g_audio_manager_factory->CreateInstance(audio_log_factory); |
| 169 else |
| 170 g_last_created = CreateAudioManager(audio_log_factory); |
| 171 |
142 return g_last_created; | 172 return g_last_created; |
143 } | 173 } |
144 | 174 |
145 // static | 175 // static |
146 AudioManager* AudioManager::CreateWithHangTimer( | 176 AudioManager* AudioManager::CreateWithHangTimer( |
147 AudioLogFactory* audio_log_factory, | 177 AudioLogFactory* audio_log_factory, |
148 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) { | 178 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) { |
149 AudioManager* manager = Create(audio_log_factory); | 179 AudioManager* manager = Create(audio_log_factory); |
150 // On OSX the audio thread is the UI thread, for which a hang monitor is not | 180 // On OSX the audio thread is the UI thread, for which a hang monitor is not |
151 // necessary. | 181 // necessary. |
152 #if !defined(OS_MACOSX) | 182 #if !defined(OS_MACOSX) |
153 g_helper.Pointer()->StartHangTimer(monitor_task_runner); | 183 g_helper.Pointer()->StartHangTimer(monitor_task_runner); |
154 #endif | 184 #endif |
155 return manager; | 185 return manager; |
156 } | 186 } |
157 | 187 |
158 // static | 188 // static |
159 AudioManager* AudioManager::CreateForTesting() { | 189 AudioManager* AudioManager::CreateForTesting() { |
160 return Create(g_helper.Pointer()->fake_log_factory()); | 190 return Create(g_helper.Pointer()->fake_log_factory()); |
161 } | 191 } |
162 | 192 |
163 // static | 193 // static |
164 AudioManager* AudioManager::Get() { | 194 AudioManager* AudioManager::Get() { |
165 return g_last_created; | 195 return g_last_created; |
166 } | 196 } |
167 | 197 |
168 } // namespace media | 198 } // namespace media |
OLD | NEW |