Chromium Code Reviews| Index: media/audio/audio_manager_base.cc |
| diff --git a/media/audio/audio_manager_base.cc b/media/audio/audio_manager_base.cc |
| index 43dc31e20e742cbe56554c307ad1cf3efdd5b4c0..01fb19461bf8b909b9dd792a8407e4522fd3a8fd 100644 |
| --- a/media/audio/audio_manager_base.cc |
| +++ b/media/audio/audio_manager_base.cc |
| @@ -6,14 +6,17 @@ |
| #include "base/bind.h" |
| #include "base/bind_helpers.h" |
| +#include "base/command_line.h" |
| #include "base/message_loop_proxy.h" |
| #include "base/threading/thread.h" |
| +#include "build/build_config.h" |
| #include "media/audio/audio_output_dispatcher_impl.h" |
| #include "media/audio/audio_output_proxy.h" |
| #include "media/audio/audio_output_resampler.h" |
| #include "media/audio/audio_util.h" |
| #include "media/audio/fake_audio_input_stream.h" |
| #include "media/audio/fake_audio_output_stream.h" |
| +#include "media/base/media_switches.h" |
| namespace media { |
| @@ -81,15 +84,19 @@ AudioManagerBase::AudioManagerBase() |
| audio_thread_(new base::Thread("AudioThread")) { |
| #if defined(OS_WIN) |
| audio_thread_->init_com_with_mta(true); |
| +#elif defined(OS_MACOSX) |
| + // CoreAudio calls must occur on the main thread of the process, which in our |
| + // case is sadly the browser UI thread. Failure to execute calls on the right |
| + // thread leads to crashes and odd behavior. See http://crbug.com/158170. |
| + const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| + if (!cmd_line->HasSwitch(switches::kDisableMainThreadAudio) && |
| + base::MessageLoopProxy::current()) { |
| + message_loop_ = base::MessageLoopProxy::current(); |
| + return; |
| + } |
| #endif |
| -#if defined(OS_MACOSX) |
| - // On Mac, use a UI loop to get native message pump so that CoreAudio property |
| - // listener callbacks fire. |
| - CHECK(audio_thread_->StartWithOptions( |
| - base::Thread::Options(base::MessageLoop::TYPE_UI, 0))); |
| -#else |
| + |
| CHECK(audio_thread_->Start()); |
| -#endif |
| message_loop_ = audio_thread_->message_loop_proxy(); |
| } |
| @@ -114,6 +121,14 @@ scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetMessageLoop() { |
| return message_loop_; |
| } |
| +scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetWorkerLoop() { |
| + // Lazily start the worker thread. |
| + if (!audio_thread_->IsRunning()) |
| + CHECK(audio_thread_->Start()); |
| + |
| + return audio_thread_->message_loop_proxy(); |
| +} |
| + |
| AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( |
| const AudioParameters& params, |
| const std::string& input_device_id) { |
| @@ -317,13 +332,20 @@ void AudioManagerBase::Shutdown() { |
| if (!audio_thread) |
|
scherkus (not reviewing)
2013/06/03 22:04:28
OOC does Shutdown() get called multiple times, hen
DaleCurtis
2013/06/03 22:59:28
It shouldn't be. Likely this is dead code. I'll re
DaleCurtis
2013/06/04 00:15:17
Actually going to save this for a follow on cleanu
|
| return; |
| - CHECK_NE(base::MessageLoop::current(), audio_thread->message_loop()); |
| +#if defined(OS_MACOSX) |
| + // Only true when we're sharing the UI message loop with the browser. The UI |
| + // loop is no longer running at this time and browser destruction is imminent. |
| + if (message_loop_->BelongsToCurrentThread()) { |
| + // After ShutdownOnAudioThread() there should be no tasks running on the |
| + // |worker_loop_|. |
| + ShutdownOnAudioThread(); |
| + audio_thread->Stop(); |
| + return; |
| + } |
| +#endif |
| - // We must use base::Unretained since Shutdown might have been called from |
| - // the destructor and we can't alter the refcount of the object at that point. |
| - audio_thread->message_loop()->PostTask(FROM_HERE, base::Bind( |
| - &AudioManagerBase::ShutdownOnAudioThread, |
| - base::Unretained(this))); |
| + message_loop_->PostTask(FROM_HERE, base::Bind( |
| + &AudioManagerBase::ShutdownOnAudioThread, base::Unretained(this))); |
| // Stop() will wait for any posted messages to be processed first. |
| audio_thread->Stop(); |