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 310e4be16498d5501b74f59f9a1b4d17f46ee577..84da2366ea2f191462673c8db32466a92f02ac59 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 { |
| @@ -43,16 +46,20 @@ 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()) { |
|
scherkus (not reviewing)
2013/05/31 19:30:35
when would this be NULL?
DaleCurtis
2013/06/03 19:51:10
Many unittests create an AudioManager without owni
scherkus (not reviewing)
2013/06/03 22:04:28
Hmm... it's unfortunate that production code is al
DaleCurtis
2013/06/03 22:26:35
"Fixing" means giving those tests a MessageLoop an
DaleCurtis
2013/06/03 22:59:28
Actually just changing BrowserMainLoop would means
scherkus (not reviewing)
2013/06/03 23:22:18
I agree that implicit dependencies kinda suck, hen
DaleCurtis
2013/06/04 00:15:17
Lets leave it as is until it's sticky then I'll su
scherkus (not reviewing)
2013/06/04 00:21:30
SGTM
|
| + 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()); |
|
scherkus (not reviewing)
2013/05/31 19:30:35
does it make sense to lazily start this thread?
DaleCurtis
2013/06/03 19:51:10
It is lazily started? See if (!worker_loop_) on li
scherkus (not reviewing)
2013/06/03 22:04:28
I was referring to the Linux/Win/CrOS/disable-main
DaleCurtis
2013/06/03 22:26:35
Probably? I don't see any reason why not. We would
scherkus (not reviewing)
2013/06/03 23:22:18
Yeah I'm wonder if there would be any races as AFA
DaleCurtis
2013/06/04 00:15:17
Shouldn't be too hard, we'd need to make generic I
|
| -#endif |
| - message_loop_ = audio_thread_->message_loop_proxy(); |
| + worker_loop_ = message_loop_ = audio_thread_->message_loop_proxy(); |
|
scherkus (not reviewing)
2013/05/31 19:30:35
nit: can you split the assignments?
multiple assi
DaleCurtis
2013/06/03 19:51:10
Done. It shouldn't be possible to call GetWorkerL
|
| } |
| AudioManagerBase::~AudioManagerBase() { |
| @@ -76,6 +83,16 @@ scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetMessageLoop() { |
| return message_loop_; |
| } |
| +scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetWorkerLoop() { |
| + // Start the worker loop on demand. |
| + if (!worker_loop_) { |
| + CHECK(audio_thread_->Start()); |
| + worker_loop_ = audio_thread_->message_loop_proxy(); |
| + } |
| + |
| + return worker_loop_; |
| +} |
| + |
| AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( |
| const AudioParameters& params) { |
| // TODO(miu): Fix ~50 call points across several unit test modules to call |
| @@ -271,13 +288,18 @@ void AudioManagerBase::Shutdown() { |
| if (!audio_thread) |
| 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. |
| + if (message_loop_->BelongsToCurrentThread()) { |
| + // After ShutdownOnAudioThread() there should be no tasks running on the |
| + // |worker_loop_|. |
| + ShutdownOnAudioThread(); |
| + return audio_thread->Stop(); |
|
scherkus (not reviewing)
2013/05/31 19:30:35
split return to next line -- it's too subtle and m
DaleCurtis
2013/06/03 19:51:10
Done.
|
| + } |
| +#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(); |