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/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/power_monitor/power_monitor.h" | 14 #include "base/power_monitor/power_monitor.h" |
15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
16 #include "media/audio/audio_manager_factory.h" | 16 #include "media/audio/audio_manager_factory.h" |
17 #include "media/audio/fake_audio_log_factory.h" | 17 #include "media/audio/fake_audio_log_factory.h" |
18 #include "media/base/media_switches.h" | 18 #include "media/base/media_switches.h" |
19 | 19 |
20 #if defined(OS_WIN) | |
21 #include "base/win/scoped_com_initializer.h" | |
22 #endif | |
23 | |
20 namespace media { | 24 namespace media { |
21 namespace { | 25 namespace { |
22 | 26 |
23 // The singleton instance of AudioManager. This is set when Create() is called. | 27 // The singleton instance of AudioManager. This is set when Create() is called. |
24 AudioManager* g_last_created = NULL; | 28 AudioManager* g_last_created = NULL; |
25 | 29 |
26 // The singleton instance of AudioManagerFactory. This is only set if | 30 // The singleton instance of AudioManagerFactory. This is only set if |
27 // SetFactory() is called. If it is set when Create() is called, its | 31 // SetFactory() is called. If it is set when Create() is called, its |
28 // CreateInstance() function is used to set |g_last_created|. Otherwise, the | 32 // CreateInstance() function is used to set |g_last_created|. Otherwise, the |
29 // linked implementation of media::CreateAudioManager is used to set | 33 // linked implementation of media::CreateAudioManager is used to set |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 // Don't hold the lock while posting the next task. | 116 // Don't hold the lock while posting the next task. |
113 g_last_created->GetTaskRunner()->PostDelayedTask( | 117 g_last_created->GetTaskRunner()->PostDelayedTask( |
114 FROM_HERE, | 118 FROM_HERE, |
115 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick, | 119 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick, |
116 base::Unretained(this)), | 120 base::Unretained(this)), |
117 max_hung_task_time_ / 10); | 121 max_hung_task_time_ / 10); |
118 } | 122 } |
119 | 123 |
120 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; } | 124 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; } |
121 | 125 |
126 #if defined(OS_WIN) | |
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( | |
131 base::win::ScopedCOMInitializer::kMTA)); | |
DaleCurtis
2015/05/07 02:11:12
Did using a regular initializer (i.e. w/ MTA) fail
| |
132 } | |
133 #endif | |
134 | |
122 private: | 135 private: |
123 FakeAudioLogFactory fake_log_factory_; | 136 FakeAudioLogFactory fake_log_factory_; |
124 | 137 |
125 const base::TimeDelta max_hung_task_time_; | 138 const base::TimeDelta max_hung_task_time_; |
126 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_; | 139 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_; |
127 | 140 |
128 base::Lock hang_lock_; | 141 base::Lock hang_lock_; |
129 bool hang_detection_enabled_; | 142 bool hang_detection_enabled_; |
130 base::TimeTicks last_audio_thread_timer_tick_; | 143 base::TimeTicks last_audio_thread_timer_tick_; |
131 int hang_failures_; | 144 int hang_failures_; |
132 | 145 |
146 #if defined(OS_WIN) | |
147 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_for_testing_; | |
148 #endif | |
149 | |
133 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); | 150 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); |
134 }; | 151 }; |
135 | 152 |
136 static bool g_hang_monitor_enabled = false; | 153 static bool g_hang_monitor_enabled = false; |
137 | 154 |
138 static base::LazyInstance<AudioManagerHelper>::Leaky g_helper = | 155 static base::LazyInstance<AudioManagerHelper>::Leaky g_helper = |
139 LAZY_INSTANCE_INITIALIZER; | 156 LAZY_INSTANCE_INITIALIZER; |
140 } // namespace | 157 } // namespace |
141 | 158 |
142 // Forward declaration of the platform specific AudioManager factory function. | 159 // Forward declaration of the platform specific AudioManager factory function. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 if (g_hang_monitor_enabled || | 201 if (g_hang_monitor_enabled || |
185 base::CommandLine::ForCurrentProcess()->HasSwitch( | 202 base::CommandLine::ForCurrentProcess()->HasSwitch( |
186 switches::kEnableAudioHangMonitor)) { | 203 switches::kEnableAudioHangMonitor)) { |
187 g_helper.Pointer()->StartHangTimer(monitor_task_runner); | 204 g_helper.Pointer()->StartHangTimer(monitor_task_runner); |
188 } | 205 } |
189 return manager; | 206 return manager; |
190 } | 207 } |
191 | 208 |
192 // static | 209 // static |
193 AudioManager* AudioManager::CreateForTesting() { | 210 AudioManager* AudioManager::CreateForTesting() { |
211 #if defined(OS_WIN) | |
212 g_helper.Pointer()->InitializeCOMForTesting(); | |
213 #endif | |
194 return Create(g_helper.Pointer()->fake_log_factory()); | 214 return Create(g_helper.Pointer()->fake_log_factory()); |
195 } | 215 } |
196 | 216 |
197 // static | 217 // static |
198 void AudioManager::EnableHangMonitor() { | 218 void AudioManager::EnableHangMonitor() { |
199 CHECK(!g_last_created); | 219 CHECK(!g_last_created); |
200 // On OSX the audio thread is the UI thread, for which a hang monitor is not | 220 // 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 | 221 // necessary or recommended. If it's manually requested, we should allow it |
202 // to start though. | 222 // to start though. |
203 #if !defined(OS_MACOSX) | 223 #if !defined(OS_MACOSX) |
204 g_hang_monitor_enabled = true; | 224 g_hang_monitor_enabled = true; |
205 #endif | 225 #endif |
206 } | 226 } |
207 | 227 |
208 // static | 228 // static |
209 AudioManager* AudioManager::Get() { | 229 AudioManager* AudioManager::Get() { |
210 return g_last_created; | 230 return g_last_created; |
211 } | 231 } |
212 | 232 |
213 } // namespace media | 233 } // namespace media |
OLD | NEW |