Chromium Code Reviews| 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_base.h" | 5 #include "media/audio/audio_manager_base.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" | |
| 9 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
| 10 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
| 12 #include "build/build_config.h" | |
| 11 #include "media/audio/audio_output_dispatcher_impl.h" | 13 #include "media/audio/audio_output_dispatcher_impl.h" |
| 12 #include "media/audio/audio_output_proxy.h" | 14 #include "media/audio/audio_output_proxy.h" |
| 13 #include "media/audio/audio_output_resampler.h" | 15 #include "media/audio/audio_output_resampler.h" |
| 14 #include "media/audio/audio_util.h" | 16 #include "media/audio/audio_util.h" |
| 15 #include "media/audio/fake_audio_input_stream.h" | 17 #include "media/audio/fake_audio_input_stream.h" |
| 16 #include "media/audio/fake_audio_output_stream.h" | 18 #include "media/audio/fake_audio_output_stream.h" |
| 19 #include "media/base/media_switches.h" | |
| 17 | 20 |
| 18 namespace media { | 21 namespace media { |
| 19 | 22 |
| 20 static const int kStreamCloseDelaySeconds = 5; | 23 static const int kStreamCloseDelaySeconds = 5; |
| 21 | 24 |
| 22 // Default maximum number of output streams that can be open simultaneously | 25 // Default maximum number of output streams that can be open simultaneously |
| 23 // for all platforms. | 26 // for all platforms. |
| 24 static const int kDefaultMaxOutputStreams = 16; | 27 static const int kDefaultMaxOutputStreams = 16; |
| 25 | 28 |
| 26 // Default maximum number of input streams that can be open simultaneously | 29 // Default maximum number of input streams that can be open simultaneously |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 private: | 72 private: |
| 70 const DispatcherParams* dispatcher_; | 73 const DispatcherParams* dispatcher_; |
| 71 }; | 74 }; |
| 72 | 75 |
| 73 AudioManagerBase::AudioManagerBase() | 76 AudioManagerBase::AudioManagerBase() |
| 74 : num_active_input_streams_(0), | 77 : num_active_input_streams_(0), |
| 75 max_num_output_streams_(kDefaultMaxOutputStreams), | 78 max_num_output_streams_(kDefaultMaxOutputStreams), |
| 76 max_num_input_streams_(kDefaultMaxInputStreams), | 79 max_num_input_streams_(kDefaultMaxInputStreams), |
| 77 num_output_streams_(0), | 80 num_output_streams_(0), |
| 78 num_input_streams_(0), | 81 num_input_streams_(0), |
| 82 // TODO(dalecurtis): Switch this to an ObserverListThreadSafe, so we don't | |
| 83 // block the UI thread when swapping devices. | |
| 79 output_listeners_( | 84 output_listeners_( |
| 80 ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), | 85 ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), |
| 81 audio_thread_(new base::Thread("AudioThread")) { | 86 audio_thread_(new base::Thread("AudioThread")) { |
| 82 #if defined(OS_WIN) | 87 #if defined(OS_WIN) |
| 83 audio_thread_->init_com_with_mta(true); | 88 audio_thread_->init_com_with_mta(true); |
| 89 #elif defined(OS_MACOSX) | |
|
Chris Rogers
2013/06/04 19:16:52
It'd be nice to have the ability to run on the mai
DaleCurtis
2013/06/04 20:03:14
I've changed the code so the only OS_MACOSX necess
| |
| 90 // CoreAudio calls must occur on the main thread of the process, which in our | |
| 91 // case is sadly the browser UI thread. Failure to execute calls on the right | |
| 92 // thread leads to crashes and odd behavior. See http://crbug.com/158170. | |
| 93 // TODO(dalecurtis): We should require the message loop to be passed in. | |
| 94 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
| 95 if (!cmd_line->HasSwitch(switches::kDisableMainThreadAudio) && | |
| 96 base::MessageLoopProxy::current() && | |
| 97 base::MessageLoop::current()->IsType(base::MessageLoop::TYPE_UI)) { | |
| 98 message_loop_ = base::MessageLoopProxy::current(); | |
| 99 return; | |
| 100 } | |
| 84 #endif | 101 #endif |
| 85 #if defined(OS_MACOSX) | 102 |
| 86 // On Mac, use a UI loop to get native message pump so that CoreAudio property | |
| 87 // listener callbacks fire. | |
| 88 CHECK(audio_thread_->StartWithOptions( | |
| 89 base::Thread::Options(base::MessageLoop::TYPE_UI, 0))); | |
| 90 #else | |
| 91 CHECK(audio_thread_->Start()); | 103 CHECK(audio_thread_->Start()); |
| 92 #endif | |
| 93 message_loop_ = audio_thread_->message_loop_proxy(); | 104 message_loop_ = audio_thread_->message_loop_proxy(); |
| 94 } | 105 } |
| 95 | 106 |
| 96 AudioManagerBase::~AudioManagerBase() { | 107 AudioManagerBase::~AudioManagerBase() { |
| 97 // The platform specific AudioManager implementation must have already | 108 // The platform specific AudioManager implementation must have already |
| 98 // stopped the audio thread. Otherwise, we may destroy audio streams before | 109 // stopped the audio thread. Otherwise, we may destroy audio streams before |
| 99 // stopping the thread, resulting an unexpected behavior. | 110 // stopping the thread, resulting an unexpected behavior. |
| 100 // This way we make sure activities of the audio streams are all stopped | 111 // This way we make sure activities of the audio streams are all stopped |
| 101 // before we destroy them. | 112 // before we destroy them. |
| 102 CHECK(!audio_thread_.get()); | 113 CHECK(!audio_thread_.get()); |
| 103 // All the output streams should have been deleted. | 114 // All the output streams should have been deleted. |
| 104 DCHECK_EQ(0, num_output_streams_); | 115 DCHECK_EQ(0, num_output_streams_); |
| 105 // All the input streams should have been deleted. | 116 // All the input streams should have been deleted. |
| 106 DCHECK_EQ(0, num_input_streams_); | 117 DCHECK_EQ(0, num_input_streams_); |
| 107 } | 118 } |
| 108 | 119 |
| 109 string16 AudioManagerBase::GetAudioInputDeviceModel() { | 120 string16 AudioManagerBase::GetAudioInputDeviceModel() { |
| 110 return string16(); | 121 return string16(); |
| 111 } | 122 } |
| 112 | 123 |
| 113 scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetMessageLoop() { | 124 scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetMessageLoop() { |
| 114 return message_loop_; | 125 return message_loop_; |
| 115 } | 126 } |
| 116 | 127 |
| 128 scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetWorkerLoop() { | |
| 129 // Lazily start the worker thread. | |
| 130 if (!audio_thread_->IsRunning()) | |
| 131 CHECK(audio_thread_->Start()); | |
| 132 | |
| 133 return audio_thread_->message_loop_proxy(); | |
| 134 } | |
| 135 | |
| 117 AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( | 136 AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( |
| 118 const AudioParameters& params, | 137 const AudioParameters& params, |
| 119 const std::string& input_device_id) { | 138 const std::string& input_device_id) { |
| 120 // TODO(miu): Fix ~50 call points across several unit test modules to call | 139 // TODO(miu): Fix ~50 call points across several unit test modules to call |
| 121 // this method on the audio thread, then uncomment the following: | 140 // this method on the audio thread, then uncomment the following: |
| 122 // DCHECK(message_loop_->BelongsToCurrentThread()); | 141 // DCHECK(message_loop_->BelongsToCurrentThread()); |
| 123 | 142 |
| 124 if (!params.IsValid()) { | 143 if (!params.IsValid()) { |
| 125 DLOG(ERROR) << "Audio parameters are invalid"; | 144 DLOG(ERROR) << "Audio parameters are invalid"; |
| 126 return NULL; | 145 return NULL; |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 // via a local variable while not holding the audio thread lock. | 329 // via a local variable while not holding the audio thread lock. |
| 311 scoped_ptr<base::Thread> audio_thread; | 330 scoped_ptr<base::Thread> audio_thread; |
| 312 { | 331 { |
| 313 base::AutoLock lock(audio_thread_lock_); | 332 base::AutoLock lock(audio_thread_lock_); |
| 314 audio_thread_.swap(audio_thread); | 333 audio_thread_.swap(audio_thread); |
| 315 } | 334 } |
| 316 | 335 |
| 317 if (!audio_thread) | 336 if (!audio_thread) |
| 318 return; | 337 return; |
| 319 | 338 |
| 320 CHECK_NE(base::MessageLoop::current(), audio_thread->message_loop()); | 339 #if defined(OS_MACOSX) |
| 340 // Only true when we're sharing the UI message loop with the browser. The UI | |
| 341 // loop is no longer running at this time and browser destruction is imminent. | |
| 342 if (message_loop_->BelongsToCurrentThread()) { | |
| 343 // After ShutdownOnAudioThread() there should be no tasks running on the | |
| 344 // |worker_loop_|. | |
|
Chris Rogers
2013/06/04 19:16:52
I see no member variable called |worker_loop_| - c
DaleCurtis
2013/06/04 20:03:14
Comment removed. It's not really helpful given the
| |
| 345 ShutdownOnAudioThread(); | |
| 346 audio_thread->Stop(); | |
| 347 return; | |
| 348 } | |
| 349 #endif | |
| 321 | 350 |
| 322 // We must use base::Unretained since Shutdown might have been called from | 351 message_loop_->PostTask(FROM_HERE, base::Bind( |
| 323 // the destructor and we can't alter the refcount of the object at that point. | 352 &AudioManagerBase::ShutdownOnAudioThread, base::Unretained(this))); |
| 324 audio_thread->message_loop()->PostTask(FROM_HERE, base::Bind( | |
| 325 &AudioManagerBase::ShutdownOnAudioThread, | |
| 326 base::Unretained(this))); | |
| 327 | 353 |
| 328 // Stop() will wait for any posted messages to be processed first. | 354 // Stop() will wait for any posted messages to be processed first. |
| 329 audio_thread->Stop(); | 355 audio_thread->Stop(); |
| 330 } | 356 } |
| 331 | 357 |
| 332 void AudioManagerBase::ShutdownOnAudioThread() { | 358 void AudioManagerBase::ShutdownOnAudioThread() { |
| 333 // IOS implements audio input only. | 359 // IOS implements audio input only. |
| 334 #if defined(OS_IOS) | 360 #if defined(OS_IOS) |
| 335 return; | 361 return; |
| 336 #else | 362 #else |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 379 return GetPreferredOutputStreamParameters(AudioParameters()); | 405 return GetPreferredOutputStreamParameters(AudioParameters()); |
| 380 } | 406 } |
| 381 | 407 |
| 382 AudioParameters AudioManagerBase::GetInputStreamParameters( | 408 AudioParameters AudioManagerBase::GetInputStreamParameters( |
| 383 const std::string& device_id) { | 409 const std::string& device_id) { |
| 384 NOTREACHED(); | 410 NOTREACHED(); |
| 385 return AudioParameters(); | 411 return AudioParameters(); |
| 386 } | 412 } |
| 387 | 413 |
| 388 } // namespace media | 414 } // namespace media |
| OLD | NEW |