| 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 <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); | 234 DISALLOW_COPY_AND_ASSIGN(AudioManagerHelper); |
| 235 }; | 235 }; |
| 236 | 236 |
| 237 AudioManagerHelper* GetHelper() { | 237 AudioManagerHelper* GetHelper() { |
| 238 static AudioManagerHelper* helper = new AudioManagerHelper(); | 238 static AudioManagerHelper* helper = new AudioManagerHelper(); |
| 239 return helper; | 239 return helper; |
| 240 } | 240 } |
| 241 | 241 |
| 242 } // namespace | 242 } // namespace |
| 243 | 243 |
| 244 void AudioManagerDeleter::operator()(const AudioManager* instance) const { | |
| 245 CHECK(instance); | |
| 246 // We reset g_last_created here instead of in the destructor of AudioManager | |
| 247 // because the destructor runs on the audio thread. We want to always change | |
| 248 // g_last_created from the main thread. | |
| 249 if (g_last_created == instance) { | |
| 250 g_last_created = nullptr; | |
| 251 } else { | |
| 252 // We create multiple instances of AudioManager only when testing. | |
| 253 // We should not encounter this case in production. | |
| 254 LOG(WARNING) << "Multiple instances of AudioManager detected"; | |
| 255 } | |
| 256 | |
| 257 // The deleter runs on the main thread, and AudioManager must be destroyed on | |
| 258 // the audio thread. If the audio thread is the same as the main one, tasks | |
| 259 // after this point are not executed, hence this is the only chance to delete | |
| 260 // AudioManager. See http://crbug.com/623703 for more details. | |
| 261 if (instance->GetTaskRunner()->BelongsToCurrentThread()) { | |
| 262 delete instance; | |
| 263 return; | |
| 264 } | |
| 265 | |
| 266 // AudioManager must be destroyed on the audio thread. See | |
| 267 // http://crbug.com/705455 for an existing AudioManager lifetime issue. | |
| 268 if (!instance->GetTaskRunner()->DeleteSoon(FROM_HERE, instance)) | |
| 269 LOG(WARNING) << "Failed to delete AudioManager instance."; | |
| 270 } | |
| 271 | |
| 272 // Forward declaration of the platform specific AudioManager factory function. | 244 // Forward declaration of the platform specific AudioManager factory function. |
| 273 ScopedAudioManagerPtr CreateAudioManager( | 245 std::unique_ptr<AudioManager> CreateAudioManager( |
| 274 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 246 std::unique_ptr<AudioThread> audio_thread, |
| 275 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, | |
| 276 AudioLogFactory* audio_log_factory); | 247 AudioLogFactory* audio_log_factory); |
| 277 | 248 |
| 278 void AudioManager::SetMaxStreamCountForTesting(int max_input, int max_output) { | 249 void AudioManager::SetMaxStreamCountForTesting(int max_input, int max_output) { |
| 279 NOTREACHED(); | 250 NOTREACHED(); |
| 280 } | 251 } |
| 281 | 252 |
| 282 AudioManager::AudioManager( | 253 AudioManager::AudioManager(std::unique_ptr<AudioThread> audio_thread) |
| 283 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 254 : audio_thread_(std::move(audio_thread)) { |
| 284 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner) | 255 DCHECK(audio_thread_); |
| 285 : task_runner_(std::move(task_runner)), | |
| 286 worker_task_runner_(std::move(worker_task_runner)) { | |
| 287 DCHECK(task_runner_); | |
| 288 DCHECK(worker_task_runner_); | |
| 289 | 256 |
| 290 if (g_last_created) { | 257 if (g_last_created) { |
| 291 // We create multiple instances of AudioManager only when testing. | 258 // We create multiple instances of AudioManager only when testing. |
| 292 // We should not encounter this case in production. | 259 // We should not encounter this case in production. |
| 293 LOG(WARNING) << "Multiple instances of AudioManager detected"; | 260 LOG(WARNING) << "Multiple instances of AudioManager detected"; |
| 294 } | 261 } |
| 295 // We always override |g_last_created| irrespective of whether it is already | 262 // We always override |g_last_created| irrespective of whether it is already |
| 296 // set or not becuase it represents the last created instance. | 263 // set or not becuase it represents the last created instance. |
| 297 g_last_created = this; | 264 g_last_created = this; |
| 298 } | 265 } |
| 299 | 266 |
| 300 AudioManager::~AudioManager() { | 267 AudioManager::~AudioManager() { |
| 301 DCHECK(task_runner_->BelongsToCurrentThread()); | 268 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 269 DCHECK(shutdown_); |
| 270 |
| 271 if (g_last_created == this) { |
| 272 g_last_created = nullptr; |
| 273 } else { |
| 274 // We create multiple instances of AudioManager only when testing. |
| 275 // We should not encounter this case in production. |
| 276 LOG(WARNING) << "Multiple instances of AudioManager detected"; |
| 277 } |
| 302 } | 278 } |
| 303 | 279 |
| 304 // static | 280 // static |
| 305 ScopedAudioManagerPtr AudioManager::Create( | 281 std::unique_ptr<AudioManager> AudioManager::Create( |
| 306 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 282 std::unique_ptr<AudioThread> audio_thread, |
| 307 scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, | |
| 308 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 283 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 309 AudioLogFactory* audio_log_factory) { | 284 AudioLogFactory* audio_log_factory) { |
| 310 DCHECK(task_runner); | 285 std::unique_ptr<AudioManager> manager = |
| 311 DCHECK(worker_task_runner); | 286 CreateAudioManager(std::move(audio_thread), audio_log_factory); |
| 312 ScopedAudioManagerPtr manager = CreateAudioManager( | |
| 313 std::move(task_runner), std::move(worker_task_runner), audio_log_factory); | |
| 314 #if BUILDFLAG(ENABLE_WEBRTC) | 287 #if BUILDFLAG(ENABLE_WEBRTC) |
| 315 manager->InitializeOutputDebugRecording(std::move(file_task_runner)); | 288 manager->InitializeOutputDebugRecording(std::move(file_task_runner)); |
| 316 #endif | 289 #endif |
| 317 return manager; | 290 return manager; |
| 318 } | 291 } |
| 319 | 292 |
| 320 // static | 293 // static |
| 321 ScopedAudioManagerPtr AudioManager::CreateForTesting( | 294 std::unique_ptr<AudioManager> AudioManager::CreateForTesting( |
| 322 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | 295 std::unique_ptr<AudioThread> audio_thread) { |
| 323 #if defined(OS_WIN) | 296 #if defined(OS_WIN) |
| 324 GetHelper()->InitializeCOMForTesting(); | 297 GetHelper()->InitializeCOMForTesting(); |
| 325 #endif | 298 #endif |
| 326 return Create(task_runner, task_runner, task_runner, | 299 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner = |
| 300 audio_thread->GetWorkerTaskRunner(); |
| 301 return Create(std::move(audio_thread), std::move(file_task_runner), |
| 327 GetHelper()->fake_log_factory()); | 302 GetHelper()->fake_log_factory()); |
| 328 } | 303 } |
| 329 | 304 |
| 330 // static | 305 // static |
| 331 void AudioManager::StartHangMonitorIfNeeded( | 306 void AudioManager::StartHangMonitorIfNeeded( |
| 332 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | 307 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 333 if (GetHelper()->monitor_task_runner()) | 308 if (GetHelper()->monitor_task_runner()) |
| 334 return; | 309 return; |
| 335 | 310 |
| 336 DCHECK(AudioManager::Get()); | 311 DCHECK(AudioManager::Get()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 350 const std::string& AudioManager::GetGlobalAppName() { | 325 const std::string& AudioManager::GetGlobalAppName() { |
| 351 return GetHelper()->app_name(); | 326 return GetHelper()->app_name(); |
| 352 } | 327 } |
| 353 #endif | 328 #endif |
| 354 | 329 |
| 355 // static | 330 // static |
| 356 AudioManager* AudioManager::Get() { | 331 AudioManager* AudioManager::Get() { |
| 357 return g_last_created; | 332 return g_last_created; |
| 358 } | 333 } |
| 359 | 334 |
| 335 void AudioManager::Shutdown() { |
| 336 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 337 |
| 338 // TODO(alokp): Suspend hang monitor. |
| 339 |
| 340 if (audio_thread_->GetTaskRunner()->BelongsToCurrentThread()) { |
| 341 ShutdownOnAudioThread(); |
| 342 } else { |
| 343 audio_thread_->GetTaskRunner()->PostTask( |
| 344 FROM_HERE, base::Bind(&AudioManager::ShutdownOnAudioThread, |
| 345 base::Unretained(this))); |
| 346 } |
| 347 audio_thread_->Stop(); |
| 348 shutdown_ = true; |
| 349 } |
| 350 |
| 360 } // namespace media | 351 } // namespace media |
| OLD | NEW |