| 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" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 THREAD_NONE = 0, | 62 THREAD_NONE = 0, |
| 63 THREAD_STARTED, | 63 THREAD_STARTED, |
| 64 THREAD_HUNG, | 64 THREAD_HUNG, |
| 65 THREAD_RECOVERED, | 65 THREAD_RECOVERED, |
| 66 THREAD_MAX = THREAD_RECOVERED | 66 THREAD_MAX = THREAD_RECOVERED |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 AudioManagerHelper() {} | 69 AudioManagerHelper() {} |
| 70 ~AudioManagerHelper() override {} | 70 ~AudioManagerHelper() override {} |
| 71 | 71 |
| 72 void HistogramThreadStatus(ThreadStatus status) { | 72 void StartHangTimer( |
| 73 audio_thread_status_ = status; | 73 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner) { |
| 74 UMA_HISTOGRAM_ENUMERATION("Media.AudioThreadStatus", audio_thread_status_, | 74 CHECK(!monitor_task_runner_); |
| 75 THREAD_MAX + 1); | 75 CHECK(!audio_task_runner_); |
| 76 CHECK(g_last_created); |
| 77 monitor_task_runner_ = std::move(monitor_task_runner); |
| 78 audio_task_runner_ = g_last_created->GetTaskRunner(); |
| 79 base::PowerMonitor::Get()->AddObserver(this); |
| 80 |
| 81 io_task_running_ = audio_task_running_ = true; |
| 82 audio_task_runner_->PostTask( |
| 83 FROM_HERE, |
| 84 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick, |
| 85 base::Unretained(this))); |
| 86 monitor_task_runner_->PostTask( |
| 87 FROM_HERE, base::Bind(&AudioManagerHelper::RecordAudioThreadStatus, |
| 88 base::Unretained(this))); |
| 76 } | 89 } |
| 77 | 90 |
| 78 void StartHangTimer( | 91 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; } |
| 79 const scoped_refptr<base::SingleThreadTaskRunner>& monitor_task_runner) { | 92 |
| 80 CHECK(!monitor_task_runner_); | 93 #if defined(OS_WIN) |
| 81 monitor_task_runner_ = monitor_task_runner; | 94 // This should be called before creating an AudioManager in tests to ensure |
| 82 base::PowerMonitor::Get()->AddObserver(this); | 95 // that the creating thread is COM initialized. |
| 83 failed_pings_ = 0; | 96 void InitializeCOMForTesting() { |
| 84 io_task_running_ = audio_task_running_ = true; | 97 com_initializer_for_testing_.reset(new base::win::ScopedCOMInitializer()); |
| 85 UpdateLastAudioThreadTimeTick(); | |
| 86 RecordAudioThreadStatus(); | |
| 87 } | 98 } |
| 99 #endif |
| 88 | 100 |
| 101 #if defined(OS_LINUX) |
| 102 void set_app_name(const std::string& app_name) { app_name_ = app_name; } |
| 103 const std::string& app_name() const { return app_name_; } |
| 104 #endif |
| 105 |
| 106 void enable_crash_key_logging() { enable_crash_key_logging_ = true; } |
| 107 |
| 108 private: |
| 109 // base::PowerObserver overrides. |
| 89 // Disable hang detection when the system goes into the suspend state. | 110 // Disable hang detection when the system goes into the suspend state. |
| 90 void OnSuspend() override { | 111 void OnSuspend() override { |
| 91 base::AutoLock lock(hang_lock_); | 112 base::AutoLock lock(hang_lock_); |
| 92 hang_detection_enabled_ = false; | 113 hang_detection_enabled_ = false; |
| 93 failed_pings_ = successful_pings_ = 0; | 114 failed_pings_ = successful_pings_ = 0; |
| 94 } | 115 } |
| 95 | |
| 96 // Reenable hang detection once the system comes out of the suspend state. | 116 // Reenable hang detection once the system comes out of the suspend state. |
| 97 void OnResume() override { | 117 void OnResume() override { |
| 98 base::AutoLock lock(hang_lock_); | 118 base::AutoLock lock(hang_lock_); |
| 99 hang_detection_enabled_ = true; | 119 hang_detection_enabled_ = true; |
| 100 last_audio_thread_timer_tick_ = base::TimeTicks::Now(); | 120 last_audio_thread_timer_tick_ = base::TimeTicks::Now(); |
| 101 failed_pings_ = successful_pings_ = 0; | 121 failed_pings_ = successful_pings_ = 0; |
| 102 | 122 |
| 103 // If either of the tasks were stopped during suspend, start them now. | 123 // If either of the tasks were stopped during suspend, start them now. |
| 104 if (!audio_task_running_) { | 124 if (!audio_task_running_) { |
| 105 audio_task_running_ = true; | 125 audio_task_running_ = true; |
| 106 | 126 |
| 107 base::AutoUnlock unlock(hang_lock_); | 127 base::AutoUnlock unlock(hang_lock_); |
| 108 UpdateLastAudioThreadTimeTick(); | 128 audio_task_runner_->PostTask( |
| 129 FROM_HERE, |
| 130 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick, |
| 131 base::Unretained(this))); |
| 109 } | 132 } |
| 110 | 133 |
| 111 if (!io_task_running_) { | 134 if (!io_task_running_) { |
| 112 io_task_running_ = true; | 135 io_task_running_ = true; |
| 113 | 136 |
| 114 base::AutoUnlock unlock(hang_lock_); | 137 base::AutoUnlock unlock(hang_lock_); |
| 115 RecordAudioThreadStatus(); | 138 monitor_task_runner_->PostTask( |
| 139 FROM_HERE, base::Bind(&AudioManagerHelper::RecordAudioThreadStatus, |
| 140 base::Unretained(this))); |
| 116 } | 141 } |
| 117 } | 142 } |
| 118 | 143 |
| 119 // Runs on |monitor_task_runner| typically, but may be started on any thread. | 144 // Runs on |monitor_task_runner|. |
| 120 void RecordAudioThreadStatus() { | 145 void RecordAudioThreadStatus() { |
| 146 DCHECK(monitor_task_runner_->BelongsToCurrentThread()); |
| 121 { | 147 { |
| 122 base::AutoLock lock(hang_lock_); | 148 base::AutoLock lock(hang_lock_); |
| 123 | 149 |
| 124 // Don't attempt to verify the tick time or post our task if the system is | 150 // Don't attempt to verify the tick time or post our task if the system is |
| 125 // in the process of suspending or resuming. | 151 // in the process of suspending or resuming. |
| 126 if (!hang_detection_enabled_) { | 152 if (!hang_detection_enabled_) { |
| 127 io_task_running_ = false; | 153 io_task_running_ = false; |
| 128 return; | 154 return; |
| 129 } | 155 } |
| 130 | 156 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 152 } | 178 } |
| 153 } | 179 } |
| 154 | 180 |
| 155 // Don't hold the lock while posting the next task. | 181 // Don't hold the lock while posting the next task. |
| 156 monitor_task_runner_->PostDelayedTask( | 182 monitor_task_runner_->PostDelayedTask( |
| 157 FROM_HERE, base::Bind(&AudioManagerHelper::RecordAudioThreadStatus, | 183 FROM_HERE, base::Bind(&AudioManagerHelper::RecordAudioThreadStatus, |
| 158 base::Unretained(this)), | 184 base::Unretained(this)), |
| 159 max_hung_task_time_); | 185 max_hung_task_time_); |
| 160 } | 186 } |
| 161 | 187 |
| 162 // Runs on the audio thread typically, but may be started on any thread. | 188 // Runs on the audio thread. |
| 163 void UpdateLastAudioThreadTimeTick() { | 189 void UpdateLastAudioThreadTimeTick() { |
| 190 DCHECK(audio_task_runner_->BelongsToCurrentThread()); |
| 164 { | 191 { |
| 165 base::AutoLock lock(hang_lock_); | 192 base::AutoLock lock(hang_lock_); |
| 166 last_audio_thread_timer_tick_ = base::TimeTicks::Now(); | 193 last_audio_thread_timer_tick_ = base::TimeTicks::Now(); |
| 167 failed_pings_ = 0; | 194 failed_pings_ = 0; |
| 168 | 195 |
| 169 // Don't post our task if the system is or will be suspended. | 196 // Don't post our task if the system is or will be suspended. |
| 170 if (!hang_detection_enabled_) { | 197 if (!hang_detection_enabled_) { |
| 171 audio_task_running_ = false; | 198 audio_task_running_ = false; |
| 172 return; | 199 return; |
| 173 } | 200 } |
| 174 | 201 |
| 175 DCHECK(audio_task_running_); | 202 DCHECK(audio_task_running_); |
| 176 } | 203 } |
| 177 | 204 |
| 178 // Don't hold the lock while posting the next task. | 205 // Don't hold the lock while posting the next task. |
| 179 g_last_created->GetTaskRunner()->PostDelayedTask( | 206 audio_task_runner_->PostDelayedTask( |
| 180 FROM_HERE, | 207 FROM_HERE, |
| 181 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick, | 208 base::Bind(&AudioManagerHelper::UpdateLastAudioThreadTimeTick, |
| 182 base::Unretained(this)), | 209 base::Unretained(this)), |
| 183 max_hung_task_time_ / 5); | 210 max_hung_task_time_ / 5); |
| 184 } | 211 } |
| 185 | 212 |
| 186 AudioLogFactory* fake_log_factory() { return &fake_log_factory_; } | 213 void HistogramThreadStatus(ThreadStatus status) { |
| 187 | 214 DCHECK(monitor_task_runner_->BelongsToCurrentThread()); |
| 188 #if defined(OS_WIN) | 215 audio_thread_status_ = status; |
| 189 // This should be called before creating an AudioManager in tests to ensure | 216 UMA_HISTOGRAM_ENUMERATION("Media.AudioThreadStatus", audio_thread_status_, |
| 190 // that the creating thread is COM initialized. | 217 THREAD_MAX + 1); |
| 191 void InitializeCOMForTesting() { | |
| 192 com_initializer_for_testing_.reset(new base::win::ScopedCOMInitializer()); | |
| 193 } | |
| 194 #endif | |
| 195 | |
| 196 #if defined(OS_LINUX) | |
| 197 void set_app_name(const std::string& app_name) { | |
| 198 app_name_ = app_name; | |
| 199 } | 218 } |
| 200 | 219 |
| 201 const std::string& app_name() const { | |
| 202 return app_name_; | |
| 203 } | |
| 204 #endif | |
| 205 | |
| 206 void enable_crash_key_logging() { enable_crash_key_logging_ = true; } | |
| 207 | |
| 208 private: | |
| 209 void LogAudioDriverCrashKeys() { | 220 void LogAudioDriverCrashKeys() { |
| 221 DCHECK(monitor_task_runner_->BelongsToCurrentThread()); |
| 210 DCHECK(enable_crash_key_logging_); | 222 DCHECK(enable_crash_key_logging_); |
| 211 | 223 |
| 212 #if defined(OS_WIN) | 224 #if defined(OS_WIN) |
| 213 std::string driver_name, driver_version; | 225 std::string driver_name, driver_version; |
| 214 if (!CoreAudioUtil::GetDxDiagDetails(&driver_name, &driver_version)) | 226 if (!CoreAudioUtil::GetDxDiagDetails(&driver_name, &driver_version)) |
| 215 return; | 227 return; |
| 216 | 228 |
| 217 base::debug::ScopedCrashKey crash_key( | 229 base::debug::ScopedCrashKey crash_key( |
| 218 "hung-audio-thread-details", | 230 "hung-audio-thread-details", |
| 219 base::StringPrintf("%s:%s", driver_name.c_str(), | 231 base::StringPrintf("%s:%s", driver_name.c_str(), |
| 220 driver_version.c_str())); | 232 driver_version.c_str())); |
| 221 | 233 |
| 222 // Please forward crash reports to http://crbug.com/422522 | 234 // Please forward crash reports to http://crbug.com/422522 |
| 223 base::debug::DumpWithoutCrashing(); | 235 base::debug::DumpWithoutCrashing(); |
| 224 #endif | 236 #endif |
| 225 } | 237 } |
| 226 | 238 |
| 227 FakeAudioLogFactory fake_log_factory_; | 239 FakeAudioLogFactory fake_log_factory_; |
| 228 | 240 |
| 229 const base::TimeDelta max_hung_task_time_ = base::TimeDelta::FromMinutes(1); | 241 const base::TimeDelta max_hung_task_time_ = base::TimeDelta::FromMinutes(1); |
| 230 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_; | 242 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner_; |
| 243 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner_; |
| 231 | 244 |
| 232 base::Lock hang_lock_; | 245 base::Lock hang_lock_; |
| 233 bool hang_detection_enabled_ = true; | 246 bool hang_detection_enabled_ = true; |
| 234 base::TimeTicks last_audio_thread_timer_tick_; | 247 base::TimeTicks last_audio_thread_timer_tick_; |
| 235 uint32_t failed_pings_ = 0; | 248 uint32_t failed_pings_ = 0; |
| 236 bool io_task_running_ = false; | 249 bool io_task_running_ = false; |
| 237 bool audio_task_running_ = false; | 250 bool audio_task_running_ = false; |
| 238 ThreadStatus audio_thread_status_ = THREAD_NONE; | 251 ThreadStatus audio_thread_status_ = THREAD_NONE; |
| 239 bool enable_crash_key_logging_ = false; | 252 bool enable_crash_key_logging_ = false; |
| 240 uint32_t successful_pings_ = 0; | 253 uint32_t successful_pings_ = 0; |
| 241 | 254 |
| 242 #if defined(OS_WIN) | 255 #if defined(OS_WIN) |
| 243 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_for_testing_; | 256 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_for_testing_; |
| 244 #endif | 257 #endif |
| 245 | 258 |
| 246 #if defined(OS_LINUX) | 259 #if defined(OS_LINUX) |
| 247 std::string app_name_; | 260 std::string app_name_; |
| 248 #endif | 261 #endif |
| 249 | 262 |
| 250 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); | 263 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); |
| 251 }; | 264 }; |
| 252 | 265 |
| 253 base::LazyInstance<AudioManagerHelper>::Leaky g_helper = | 266 base::LazyInstance<AudioManagerHelper>::Leaky g_helper = |
| 254 LAZY_INSTANCE_INITIALIZER; | 267 LAZY_INSTANCE_INITIALIZER; |
| 255 | 268 |
| 256 } // namespace | 269 } // namespace |
| 257 | 270 |
| 271 void AudioManagerDeleter::operator()(const AudioManager* instance) const { |
| 272 CHECK(instance); |
| 273 // We reset g_last_created here instead of in the destructor of AudioManager |
| 274 // because the destructor runs on the audio thread. We want to always change |
| 275 // g_last_created from the main thread. |
| 276 if (g_last_created == instance) { |
| 277 g_last_created = nullptr; |
| 278 } else { |
| 279 // We create multiple instances of AudioManager only when testing. |
| 280 // We should not encounter this case in production. |
| 281 LOG(WARNING) << "Multiple instances of AudioManager detected"; |
| 282 } |
| 283 |
| 284 // AudioManager must be destroyed on the audio thread. |
| 285 if (!instance->GetTaskRunner()->DeleteSoon(FROM_HERE, instance)) { |
| 286 LOG(WARNING) << "Failed to delete AudioManager instance."; |
| 287 } |
| 288 } |
| 289 |
| 258 // Forward declaration of the platform specific AudioManager factory function. | 290 // Forward declaration of the platform specific AudioManager factory function. |
| 259 AudioManager* CreateAudioManager(AudioLogFactory* audio_log_factory); | 291 ScopedAudioManagerPtr CreateAudioManager( |
| 292 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 293 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, |
| 294 AudioLogFactory* audio_log_factory); |
| 260 | 295 |
| 261 AudioManager::AudioManager() {} | 296 AudioManager::AudioManager( |
| 297 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 298 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner) |
| 299 : task_runner_(std::move(task_runner)), |
| 300 worker_task_runner_(std::move(worker_task_runner)) { |
| 301 DCHECK(task_runner_); |
| 302 DCHECK(worker_task_runner_); |
| 303 |
| 304 if (g_last_created) { |
| 305 // We create multiple instances of AudioManager only when testing. |
| 306 // We should not encounter this case in production. |
| 307 LOG(WARNING) << "Multiple instances of AudioManager detected"; |
| 308 } |
| 309 // We always override |g_last_created| irrespective of whether it is already |
| 310 // set or not becuase it represents the last created instance. |
| 311 g_last_created = this; |
| 312 } |
| 262 | 313 |
| 263 AudioManager::~AudioManager() { | 314 AudioManager::~AudioManager() { |
| 264 CHECK(!g_last_created || g_last_created == this); | 315 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 265 g_last_created = nullptr; | |
| 266 } | 316 } |
| 267 | 317 |
| 268 // static | 318 // static |
| 269 void AudioManager::SetFactory(AudioManagerFactory* factory) { | 319 void AudioManager::SetFactory(AudioManagerFactory* factory) { |
| 270 CHECK(factory); | 320 CHECK(factory); |
| 271 CHECK(!g_last_created); | 321 CHECK(!g_last_created); |
| 272 CHECK(!g_audio_manager_factory); | 322 CHECK(!g_audio_manager_factory); |
| 273 g_audio_manager_factory = factory; | 323 g_audio_manager_factory = factory; |
| 274 } | 324 } |
| 275 | 325 |
| 276 // static | 326 // static |
| 277 void AudioManager::ResetFactoryForTesting() { | 327 void AudioManager::ResetFactoryForTesting() { |
| 278 if (g_audio_manager_factory) { | 328 if (g_audio_manager_factory) { |
| 279 delete g_audio_manager_factory; | 329 delete g_audio_manager_factory; |
| 280 g_audio_manager_factory = nullptr; | 330 g_audio_manager_factory = nullptr; |
| 281 } | 331 } |
| 282 } | 332 } |
| 283 | 333 |
| 284 // static | 334 // static |
| 285 AudioManager* AudioManager::Create(AudioLogFactory* audio_log_factory) { | 335 ScopedAudioManagerPtr AudioManager::Create( |
| 286 CHECK(!g_last_created); | 336 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 287 if (g_audio_manager_factory) | 337 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, |
| 288 g_last_created = g_audio_manager_factory->CreateInstance(audio_log_factory); | 338 scoped_refptr<base::SingleThreadTaskRunner> monitor_task_runner, |
| 289 else | 339 AudioLogFactory* audio_log_factory) { |
| 290 g_last_created = CreateAudioManager(audio_log_factory); | 340 DCHECK(task_runner); |
| 341 DCHECK(worker_task_runner); |
| 342 ScopedAudioManagerPtr manager; |
| 343 if (g_audio_manager_factory) { |
| 344 manager = g_audio_manager_factory->CreateInstance( |
| 345 std::move(task_runner), std::move(worker_task_runner), |
| 346 audio_log_factory); |
| 347 } else { |
| 348 manager = |
| 349 CreateAudioManager(std::move(task_runner), |
| 350 std::move(worker_task_runner), audio_log_factory); |
| 351 } |
| 291 | 352 |
| 292 return g_last_created; | 353 if (monitor_task_runner) |
| 293 } | 354 g_helper.Pointer()->StartHangTimer(std::move(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 | 355 |
| 307 return manager; | 356 return manager; |
| 308 } | 357 } |
| 309 | 358 |
| 310 // static | 359 // static |
| 311 AudioManager* AudioManager::CreateForTesting() { | 360 ScopedAudioManagerPtr AudioManager::CreateForTesting( |
| 361 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 312 #if defined(OS_WIN) | 362 #if defined(OS_WIN) |
| 313 g_helper.Pointer()->InitializeCOMForTesting(); | 363 g_helper.Pointer()->InitializeCOMForTesting(); |
| 314 #endif | 364 #endif |
| 315 return Create(g_helper.Pointer()->fake_log_factory()); | 365 return Create(task_runner, task_runner, nullptr, |
| 366 g_helper.Pointer()->fake_log_factory()); |
| 316 } | 367 } |
| 317 | 368 |
| 318 // static | 369 // static |
| 319 void AudioManager::EnableCrashKeyLoggingForAudioThreadHangs() { | 370 void AudioManager::EnableCrashKeyLoggingForAudioThreadHangs() { |
| 320 CHECK(!g_last_created); | 371 CHECK(!g_last_created); |
| 321 g_helper.Pointer()->enable_crash_key_logging(); | 372 g_helper.Pointer()->enable_crash_key_logging(); |
| 322 } | 373 } |
| 323 | 374 |
| 324 #if defined(OS_LINUX) | 375 #if defined(OS_LINUX) |
| 325 // static | 376 // static |
| (...skipping 26 matching lines...) Expand all Loading... |
| 352 std::string AudioManager::GetCommunicationsDeviceName() { | 403 std::string AudioManager::GetCommunicationsDeviceName() { |
| 353 #if defined(OS_WIN) | 404 #if defined(OS_WIN) |
| 354 return GetLocalizedStringUTF8(COMMUNICATIONS_AUDIO_DEVICE_NAME); | 405 return GetLocalizedStringUTF8(COMMUNICATIONS_AUDIO_DEVICE_NAME); |
| 355 #else | 406 #else |
| 356 NOTREACHED(); | 407 NOTREACHED(); |
| 357 return ""; | 408 return ""; |
| 358 #endif | 409 #endif |
| 359 } | 410 } |
| 360 | 411 |
| 361 } // namespace media | 412 } // namespace media |
| OLD | NEW |