| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/debug/alias.h" | 12 #include "base/debug/alias.h" |
| 13 #include "base/debug/crash_logging.h" | 13 #include "base/debug/crash_logging.h" |
| 14 #include "base/debug/dump_without_crashing.h" | 14 #include "base/debug/dump_without_crashing.h" |
| 15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
| 19 #include "base/metrics/histogram_macros.h" | 19 #include "base/metrics/histogram_macros.h" |
| 20 #include "base/power_monitor/power_monitor.h" | 20 #include "base/power_monitor/power_monitor.h" |
| 21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 22 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 23 #include "media/audio/audio_manager_factory.h" | 23 #include "media/audio/audio_manager_factory.h" |
| 24 #include "media/audio/fake_audio_log_factory.h" | |
| 25 #include "media/base/media_resources.h" | 24 #include "media/base/media_resources.h" |
| 26 #include "media/base/media_switches.h" | 25 #include "media/base/media_switches.h" |
| 27 | 26 |
| 28 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
| 29 #include "base/win/scoped_com_initializer.h" | 28 #include "base/win/scoped_com_initializer.h" |
| 30 #include "media/audio/win/core_audio_util_win.h" | 29 #include "media/audio/win/core_audio_util_win.h" |
| 31 #endif | 30 #endif |
| 32 | 31 |
| 33 namespace media { | 32 namespace media { |
| 34 namespace { | 33 namespace { |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 } | 175 } |
| 177 | 176 |
| 178 // Don't hold the lock while posting the next task. | 177 // Don't hold the lock while posting the next task. |
| 179 g_last_created->GetTaskRunner()->PostDelayedTask( | 178 g_last_created->GetTaskRunner()->PostDelayedTask( |
| 180 FROM_HERE, | 179 FROM_HERE, |
| 181 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick, | 180 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick, |
| 182 base::Unretained(this)), | 181 base::Unretained(this)), |
| 183 max_hung_task_time_ / 5); | 182 max_hung_task_time_ / 5); |
| 184 } | 183 } |
| 185 | 184 |
| 186 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; } | |
| 187 | |
| 188 #if defined(OS_WIN) | 185 #if defined(OS_WIN) |
| 189 // This should be called before creating an AudioManager in tests to ensure | 186 // This should be called before creating an AudioManager in tests to ensure |
| 190 // that the creating thread is COM initialized. | 187 // that the creating thread is COM initialized. |
| 191 void InitializeCOMForTesting() { | 188 void InitializeCOMForTesting() { |
| 192 com_initializer_for_testing_.reset(new base::win::ScopedCOMInitializer()); | 189 com_initializer_for_testing_.reset(new base::win::ScopedCOMInitializer()); |
| 193 } | 190 } |
| 194 #endif | 191 #endif |
| 195 | 192 |
| 196 #if defined(OS_LINUX) | 193 #if defined(OS_LINUX) |
| 197 void set_app_name(const std::string& app_name) { | 194 void set_app_name(const std::string& app_name) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 217 base::debug::ScopedCrashKey crash_key( | 214 base::debug::ScopedCrashKey crash_key( |
| 218 "hung-audio-thread-details", | 215 "hung-audio-thread-details", |
| 219 base::StringPrintf("%s:%s", driver_name.c_str(), | 216 base::StringPrintf("%s:%s", driver_name.c_str(), |
| 220 driver_version.c_str())); | 217 driver_version.c_str())); |
| 221 | 218 |
| 222 // Please forward crash reports to http://crbug.com/422522 | 219 // Please forward crash reports to http://crbug.com/422522 |
| 223 base::debug::DumpWithoutCrashing(); | 220 base::debug::DumpWithoutCrashing(); |
| 224 #endif | 221 #endif |
| 225 } | 222 } |
| 226 | 223 |
| 227 FakeAudioLogFactory fake_log_factory_; | |
| 228 | |
| 229 const base::TimeDelta max_hung_task_time_ = base::TimeDelta::FromMinutes(1); | 224 const base::TimeDelta max_hung_task_time_ = base::TimeDelta::FromMinutes(1); |
| 230 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_; | 225 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_; |
| 231 | 226 |
| 232 base::Lock hang_lock_; | 227 base::Lock hang_lock_; |
| 233 bool hang_detection_enabled_ = true; | 228 bool hang_detection_enabled_ = true; |
| 234 base::TimeTicks last_audio_thread_timer_tick_; | 229 base::TimeTicks last_audio_thread_timer_tick_; |
| 235 uint32_t failed_pings_ = 0; | 230 uint32_t failed_pings_ = 0; |
| 236 bool io_task_running_ = false; | 231 bool io_task_running_ = false; |
| 237 bool audio_task_running_ = false; | 232 bool audio_task_running_ = false; |
| 238 ThreadStatus audio_thread_status_ = THREAD_NONE; | 233 ThreadStatus audio_thread_status_ = THREAD_NONE; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 249 | 244 |
| 250 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); | 245 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); |
| 251 }; | 246 }; |
| 252 | 247 |
| 253 base::LazyInstance<AudioManagerHelper>::Leaky g_helper = | 248 base::LazyInstance<AudioManagerHelper>::Leaky g_helper = |
| 254 LAZY_INSTANCE_INITIALIZER; | 249 LAZY_INSTANCE_INITIALIZER; |
| 255 | 250 |
| 256 } // namespace | 251 } // namespace |
| 257 | 252 |
| 258 // Forward declaration of the platform specific AudioManager factory function. | 253 // Forward declaration of the platform specific AudioManager factory function. |
| 259 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory); | 254 AudioManager* CreateAudioManager( |
| 255 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 256 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner, |
| 257 AudioLogFactory* audio_log_factory); |
| 260 | 258 |
| 261 AudioManager::AudioManager() {} | 259 AudioManager::AudioManager( |
| 260 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 261 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner) |
| 262 : task_runner_(task_runner), worker_task_runner_(worker_task_runner) { |
| 263 DCHECK(task_runner_); |
| 264 DCHECK(worker_task_runner_); |
| 265 g_last_created = this; |
| 266 } |
| 262 | 267 |
| 263 AudioManager::~AudioManager() { | 268 AudioManager::~AudioManager() { |
| 264 CHECK(!g_last_created || g_last_created == this); | 269 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 265 g_last_created = nullptr; | |
| 266 } | 270 } |
| 267 | 271 |
| 268 // static | 272 // static |
| 269 void AudioManager::SetFactory(AudioManagerFactory* factory) { | 273 void AudioManager::SetFactory(AudioManagerFactory* factory) { |
| 270 CHECK(factory); | 274 CHECK(factory); |
| 271 CHECK(!g_last_created); | 275 CHECK(!g_last_created); |
| 272 CHECK(!g_audio_manager_factory); | 276 CHECK(!g_audio_manager_factory); |
| 273 g_audio_manager_factory = factory; | 277 g_audio_manager_factory = factory; |
| 274 } | 278 } |
| 275 | 279 |
| 276 // static | 280 // static |
| 277 void AudioManager::ResetFactoryForTesting() { | 281 void AudioManager::ResetFactoryForTesting() { |
| 278 if (g_audio_manager_factory) { | 282 if (g_audio_manager_factory) { |
| 279 delete g_audio_manager_factory; | 283 delete g_audio_manager_factory; |
| 280 g_audio_manager_factory = nullptr; | 284 g_audio_manager_factory = nullptr; |
| 281 } | 285 } |
| 282 } | 286 } |
| 283 | 287 |
| 284 // static | 288 // static |
| 285 AudioManager* AudioManager::Create(AudioLogFactory* audio_log_factory) { | 289 AudioManager* AudioManager::Create( |
| 286 CHECK(!g_last_created); | 290 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 287 if (g_audio_manager_factory) | 291 const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner, |
| 288 g_last_created = g_audio_manager_factory->CreateInstance(audio_log_factory); | 292 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner, |
| 289 else | 293 AudioLogFactory* audio_log_factory) { |
| 290 g_last_created = CreateAudioManager(audio_log_factory); | 294 AudioManager* manager = nullptr; |
| 295 if (g_audio_manager_factory) { |
| 296 manager = g_audio_manager_factory->CreateInstance( |
| 297 task_runner, worker_task_runner, audio_log_factory); |
| 298 } else { |
| 299 manager = |
| 300 CreateAudioManager(task_runner, worker_task_runner, audio_log_factory); |
| 301 } |
| 291 | 302 |
| 292 return g_last_created; | 303 if (monitor_task_runner) |
| 293 } | 304 g_helper.Pointer()->StartHangTimer(monitor_task_runner); |
| 294 | |
| 295 // static | |
| 296 AudioManager* AudioManager::CreateWithHangTimer( | |
| 297 AudioLogFactory* audio_log_factory, | |
| 298 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) { | |
| 299 AudioManager* manager = Create(audio_log_factory); | |
| 300 | |
| 301 // On OSX the audio thread is the UI thread, for which a hang monitor is not | |
| 302 // necessary or recommended. | |
| 303 #if !defined(OS_MACOSX) | |
| 304 g_helper.Pointer()->StartHangTimer(monitor_task_runner); | |
| 305 #endif | |
| 306 | 305 |
| 307 return manager; | 306 return manager; |
| 308 } | 307 } |
| 309 | 308 |
| 310 // static | 309 // static |
| 311 AudioManager* AudioManager::CreateForTesting() { | 310 void AudioManager::Destroy(AudioManager* instance) { |
| 312 #if defined(OS_WIN) | 311 CHECK(instance); |
| 313 g_helper.Pointer()->InitializeCOMForTesting(); | 312 CHECK_EQ(g_last_created, instance); |
| 314 #endif | 313 |
| 315 return Create(g_helper.Pointer()->fake_log_factory()); | 314 // AudioManager must be destroyed on the audio thread. |
| 315 scoped_refptr<base::SingleThreadTaskRunner> task_runner = |
| 316 instance->GetTaskRunner(); |
| 317 if (task_runner->BelongsToCurrentThread()) { |
| 318 delete instance; |
| 319 } else { |
| 320 task_runner->DeleteSoon(FROM_HERE, instance); |
| 321 } |
| 322 |
| 323 // We reset g_last_created here instead of in the destructor of AudioManager |
| 324 // to allow construction of another AudioManager while the current instance |
| 325 // is being destroyed. This overlap does not happen in production - only in |
| 326 // unittests. |
| 327 g_last_created = nullptr; |
| 316 } | 328 } |
| 317 | 329 |
| 318 // static | 330 // static |
| 319 void AudioManager::EnableCrashKeyLoggingForAudioThreadHangs() { | 331 void AudioManager::EnableCrashKeyLoggingForAudioThreadHangs() { |
| 320 CHECK(!g_last_created); | 332 CHECK(!g_last_created); |
| 321 g_helper.Pointer()->enable_crash_key_logging(); | 333 g_helper.Pointer()->enable_crash_key_logging(); |
| 322 } | 334 } |
| 323 | 335 |
| 324 #if defined(OS_LINUX) | 336 #if defined(OS_LINUX) |
| 325 // static | 337 // static |
| (...skipping 26 matching lines...) Expand all Loading... |
| 352 std::string AudioManager::GetCommunicationsDeviceName() { | 364 std::string AudioManager::GetCommunicationsDeviceName() { |
| 353 #if defined(OS_WIN) | 365 #if defined(OS_WIN) |
| 354 return GetLocalizedStringUTF8(COMMUNICATIONS_AUDIO_DEVICE_NAME); | 366 return GetLocalizedStringUTF8(COMMUNICATIONS_AUDIO_DEVICE_NAME); |
| 355 #else | 367 #else |
| 356 NOTREACHED(); | 368 NOTREACHED(); |
| 357 return ""; | 369 return ""; |
| 358 #endif | 370 #endif |
| 359 } | 371 } |
| 360 | 372 |
| 361 } // namespace media | 373 } // namespace media |
| OLD | NEW |