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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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), |
79 output_listeners_( | 82 output_listeners_( |
80 ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), | 83 ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), |
81 audio_thread_(new base::Thread("AudioThread")) { | 84 audio_thread_(new base::Thread("AudioThread")) { |
82 #if defined(OS_WIN) | 85 #if defined(OS_WIN) |
83 audio_thread_->init_com_with_mta(true); | 86 audio_thread_->init_com_with_mta(true); |
87 #elif defined(OS_MACOSX) | |
88 // CoreAudio calls must occur on the main thread of the process, which in our | |
89 // case is sadly the browser UI thread. Failure to execute calls on the right | |
90 // thread leads to crashes and odd behavior. See http://crbug.com/158170. | |
91 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
92 if (!cmd_line->HasSwitch(switches::kDisableMainThreadAudio) && | |
93 base::MessageLoopProxy::current()) { | |
94 message_loop_ = base::MessageLoopProxy::current(); | |
95 return; | |
96 } | |
84 #endif | 97 #endif |
85 #if defined(OS_MACOSX) | 98 |
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()); | 99 CHECK(audio_thread_->Start()); |
92 #endif | |
93 message_loop_ = audio_thread_->message_loop_proxy(); | 100 message_loop_ = audio_thread_->message_loop_proxy(); |
94 } | 101 } |
95 | 102 |
96 AudioManagerBase::~AudioManagerBase() { | 103 AudioManagerBase::~AudioManagerBase() { |
97 // The platform specific AudioManager implementation must have already | 104 // The platform specific AudioManager implementation must have already |
98 // stopped the audio thread. Otherwise, we may destroy audio streams before | 105 // stopped the audio thread. Otherwise, we may destroy audio streams before |
99 // stopping the thread, resulting an unexpected behavior. | 106 // stopping the thread, resulting an unexpected behavior. |
100 // This way we make sure activities of the audio streams are all stopped | 107 // This way we make sure activities of the audio streams are all stopped |
101 // before we destroy them. | 108 // before we destroy them. |
102 CHECK(!audio_thread_.get()); | 109 CHECK(!audio_thread_.get()); |
103 // All the output streams should have been deleted. | 110 // All the output streams should have been deleted. |
104 DCHECK_EQ(0, num_output_streams_); | 111 DCHECK_EQ(0, num_output_streams_); |
105 // All the input streams should have been deleted. | 112 // All the input streams should have been deleted. |
106 DCHECK_EQ(0, num_input_streams_); | 113 DCHECK_EQ(0, num_input_streams_); |
107 } | 114 } |
108 | 115 |
109 string16 AudioManagerBase::GetAudioInputDeviceModel() { | 116 string16 AudioManagerBase::GetAudioInputDeviceModel() { |
110 return string16(); | 117 return string16(); |
111 } | 118 } |
112 | 119 |
113 scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetMessageLoop() { | 120 scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetMessageLoop() { |
114 return message_loop_; | 121 return message_loop_; |
115 } | 122 } |
116 | 123 |
124 scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetWorkerLoop() { | |
125 // Lazily start the worker thread. | |
126 if (!audio_thread_->IsRunning()) | |
127 CHECK(audio_thread_->Start()); | |
128 | |
129 return audio_thread_->message_loop_proxy(); | |
130 } | |
131 | |
117 AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( | 132 AudioOutputStream* AudioManagerBase::MakeAudioOutputStream( |
118 const AudioParameters& params, | 133 const AudioParameters& params, |
119 const std::string& input_device_id) { | 134 const std::string& input_device_id) { |
120 // TODO(miu): Fix ~50 call points across several unit test modules to call | 135 // TODO(miu): Fix ~50 call points across several unit test modules to call |
121 // this method on the audio thread, then uncomment the following: | 136 // this method on the audio thread, then uncomment the following: |
122 // DCHECK(message_loop_->BelongsToCurrentThread()); | 137 // DCHECK(message_loop_->BelongsToCurrentThread()); |
123 | 138 |
124 if (!params.IsValid()) { | 139 if (!params.IsValid()) { |
125 DLOG(ERROR) << "Audio parameters are invalid"; | 140 DLOG(ERROR) << "Audio parameters are invalid"; |
126 return NULL; | 141 return NULL; |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
307 | 322 |
308 void AudioManagerBase::Shutdown() { | 323 void AudioManagerBase::Shutdown() { |
309 // To avoid running into deadlocks while we stop the thread, shut it down | 324 // To avoid running into deadlocks while we stop the thread, shut it down |
310 // via a local variable while not holding the audio thread lock. | 325 // via a local variable while not holding the audio thread lock. |
311 scoped_ptr<base::Thread> audio_thread; | 326 scoped_ptr<base::Thread> audio_thread; |
312 { | 327 { |
313 base::AutoLock lock(audio_thread_lock_); | 328 base::AutoLock lock(audio_thread_lock_); |
314 audio_thread_.swap(audio_thread); | 329 audio_thread_.swap(audio_thread); |
315 } | 330 } |
316 | 331 |
317 if (!audio_thread) | 332 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
| |
318 return; | 333 return; |
319 | 334 |
320 CHECK_NE(base::MessageLoop::current(), audio_thread->message_loop()); | 335 #if defined(OS_MACOSX) |
336 // Only true when we're sharing the UI message loop with the browser. The UI | |
337 // loop is no longer running at this time and browser destruction is imminent. | |
338 if (message_loop_->BelongsToCurrentThread()) { | |
339 // After ShutdownOnAudioThread() there should be no tasks running on the | |
340 // |worker_loop_|. | |
341 ShutdownOnAudioThread(); | |
342 audio_thread->Stop(); | |
343 return; | |
344 } | |
345 #endif | |
321 | 346 |
322 // We must use base::Unretained since Shutdown might have been called from | 347 message_loop_->PostTask(FROM_HERE, base::Bind( |
323 // the destructor and we can't alter the refcount of the object at that point. | 348 &AudioManagerBase::ShutdownOnAudioThread, base::Unretained(this))); |
324 audio_thread->message_loop()->PostTask(FROM_HERE, base::Bind( | |
325 &AudioManagerBase::ShutdownOnAudioThread, | |
326 base::Unretained(this))); | |
327 | 349 |
328 // Stop() will wait for any posted messages to be processed first. | 350 // Stop() will wait for any posted messages to be processed first. |
329 audio_thread->Stop(); | 351 audio_thread->Stop(); |
330 } | 352 } |
331 | 353 |
332 void AudioManagerBase::ShutdownOnAudioThread() { | 354 void AudioManagerBase::ShutdownOnAudioThread() { |
333 // IOS implements audio input only. | 355 // IOS implements audio input only. |
334 #if defined(OS_IOS) | 356 #if defined(OS_IOS) |
335 return; | 357 return; |
336 #else | 358 #else |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
379 return GetPreferredOutputStreamParameters(AudioParameters()); | 401 return GetPreferredOutputStreamParameters(AudioParameters()); |
380 } | 402 } |
381 | 403 |
382 AudioParameters AudioManagerBase::GetInputStreamParameters( | 404 AudioParameters AudioManagerBase::GetInputStreamParameters( |
383 const std::string& device_id) { | 405 const std::string& device_id) { |
384 NOTREACHED(); | 406 NOTREACHED(); |
385 return AudioParameters(); | 407 return AudioParameters(); |
386 } | 408 } |
387 | 409 |
388 } // namespace media | 410 } // namespace media |
OLD | NEW |