| 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/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
| 10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
| 11 #include "base/win/scoped_com_initializer.h" |
| 11 #include "media/audio/audio_output_dispatcher_impl.h" | 12 #include "media/audio/audio_output_dispatcher_impl.h" |
| 12 #include "media/audio/audio_output_proxy.h" | 13 #include "media/audio/audio_output_proxy.h" |
| 13 #include "media/audio/audio_output_resampler.h" | 14 #include "media/audio/audio_output_resampler.h" |
| 14 #include "media/audio/audio_util.h" | 15 #include "media/audio/audio_util.h" |
| 15 #include "media/audio/fake_audio_input_stream.h" | 16 #include "media/audio/fake_audio_input_stream.h" |
| 16 #include "media/audio/fake_audio_output_stream.h" | 17 #include "media/audio/fake_audio_output_stream.h" |
| 17 #include "media/base/media_switches.h" | 18 #include "media/base/media_switches.h" |
| 18 | 19 |
| 19 // TODO(dalecurtis): Temporarily disabled while switching pipeline to use float, | 20 // TODO(dalecurtis): Temporarily disabled while switching pipeline to use float, |
| 20 // http://crbug.com/114700 | 21 // http://crbug.com/114700 |
| 21 #if defined(ENABLE_AUDIO_MIXER) | 22 #if defined(ENABLE_AUDIO_MIXER) |
| 22 #include "media/audio/audio_output_mixer.h" | 23 #include "media/audio/audio_output_mixer.h" |
| 23 #endif | 24 #endif |
| 24 | 25 |
| 26 using base::win::ScopedCOMInitializer; |
| 27 |
| 25 namespace media { | 28 namespace media { |
| 26 | 29 |
| 27 static const int kStreamCloseDelaySeconds = 5; | 30 static const int kStreamCloseDelaySeconds = 5; |
| 28 | 31 |
| 29 // Default maximum number of output streams that can be open simultaneously | 32 // Default maximum number of output streams that can be open simultaneously |
| 30 // for all platforms. | 33 // for all platforms. |
| 31 static const int kDefaultMaxOutputStreams = 16; | 34 static const int kDefaultMaxOutputStreams = 16; |
| 32 | 35 |
| 33 // Default maximum number of input streams that can be open simultaneously | 36 // Default maximum number of input streams that can be open simultaneously |
| 34 // for all platforms. | 37 // for all platforms. |
| 35 static const int kDefaultMaxInputStreams = 16; | 38 static const int kDefaultMaxInputStreams = 16; |
| 36 | 39 |
| 37 static const int kMaxInputChannels = 2; | 40 static const int kMaxInputChannels = 2; |
| 38 | 41 |
| 39 const char AudioManagerBase::kDefaultDeviceName[] = "Default"; | 42 const char AudioManagerBase::kDefaultDeviceName[] = "Default"; |
| 40 const char AudioManagerBase::kDefaultDeviceId[] = "default"; | 43 const char AudioManagerBase::kDefaultDeviceId[] = "default"; |
| 41 | 44 |
| 45 // Initializes the COM library for use by this thread and sets the thread's |
| 46 // COM threading model on Windows to MTA. |
| 47 class AudioThread : public base::Thread { |
| 48 public: |
| 49 AudioThread(); |
| 50 virtual ~AudioThread(); |
| 51 |
| 52 protected: |
| 53 virtual void Init() OVERRIDE; |
| 54 virtual void CleanUp() OVERRIDE; |
| 55 |
| 56 private: |
| 57 scoped_ptr<ScopedCOMInitializer> com_initializer_; |
| 58 DISALLOW_COPY_AND_ASSIGN(AudioThread); |
| 59 }; |
| 60 |
| 61 AudioThread::AudioThread() : base::Thread("AudioThread") { |
| 62 } |
| 63 |
| 64 AudioThread::~AudioThread() {} |
| 65 |
| 66 void AudioThread::Init() { |
| 67 com_initializer_.reset(new ScopedCOMInitializer(ScopedCOMInitializer::kMTA)); |
| 68 CHECK(com_initializer_->succeeded()); |
| 69 } |
| 70 |
| 71 void AudioThread::CleanUp() { |
| 72 com_initializer_.reset(); |
| 73 } |
| 74 |
| 42 AudioManagerBase::AudioManagerBase() | 75 AudioManagerBase::AudioManagerBase() |
| 43 : num_active_input_streams_(0), | 76 : num_active_input_streams_(0), |
| 44 max_num_output_streams_(kDefaultMaxOutputStreams), | 77 max_num_output_streams_(kDefaultMaxOutputStreams), |
| 45 max_num_input_streams_(kDefaultMaxInputStreams), | 78 max_num_input_streams_(kDefaultMaxInputStreams), |
| 46 num_output_streams_(0), | 79 num_output_streams_(0), |
| 47 num_input_streams_(0) { | 80 num_input_streams_(0) { |
| 48 } | 81 } |
| 49 | 82 |
| 50 AudioManagerBase::~AudioManagerBase() { | 83 AudioManagerBase::~AudioManagerBase() { |
| 51 // The platform specific AudioManager implementation must have already | 84 // The platform specific AudioManager implementation must have already |
| 52 // stopped the audio thread. Otherwise, we may destroy audio streams before | 85 // stopped the audio thread. Otherwise, we may destroy audio streams before |
| 53 // stopping the thread, resulting an unexpected behavior. | 86 // stopping the thread, resulting an unexpected behavior. |
| 54 // This way we make sure activities of the audio streams are all stopped | 87 // This way we make sure activities of the audio streams are all stopped |
| 55 // before we destroy them. | 88 // before we destroy them. |
| 56 CHECK(!audio_thread_.get()); | 89 CHECK(!audio_thread_.get()); |
| 57 // All the output streams should have been deleted. | 90 // All the output streams should have been deleted. |
| 58 DCHECK_EQ(0, num_output_streams_); | 91 DCHECK_EQ(0, num_output_streams_); |
| 59 // All the input streams should have been deleted. | 92 // All the input streams should have been deleted. |
| 60 DCHECK_EQ(0, num_input_streams_); | 93 DCHECK_EQ(0, num_input_streams_); |
| 61 } | 94 } |
| 62 | 95 |
| 63 void AudioManagerBase::Init() { | 96 void AudioManagerBase::Init() { |
| 64 base::AutoLock lock(audio_thread_lock_); | 97 base::AutoLock lock(audio_thread_lock_); |
| 65 DCHECK(!audio_thread_.get()); | 98 DCHECK(!audio_thread_.get()); |
| 66 audio_thread_.reset(new base::Thread("AudioThread")); | 99 audio_thread_.reset(new media::AudioThread()); |
| 67 CHECK(audio_thread_->Start()); | 100 CHECK(audio_thread_->Start()); |
| 68 } | 101 } |
| 69 | 102 |
| 70 string16 AudioManagerBase::GetAudioInputDeviceModel() { | 103 string16 AudioManagerBase::GetAudioInputDeviceModel() { |
| 71 return string16(); | 104 return string16(); |
| 72 } | 105 } |
| 73 | 106 |
| 74 scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetMessageLoop() { | 107 scoped_refptr<base::MessageLoopProxy> AudioManagerBase::GetMessageLoop() { |
| 75 base::AutoLock lock(audio_thread_lock_); | 108 base::AutoLock lock(audio_thread_lock_); |
| 76 return audio_thread_.get() ? audio_thread_->message_loop_proxy() : NULL; | 109 return audio_thread_.get() ? audio_thread_->message_loop_proxy() : NULL; |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 base::AtomicRefCountDec(&num_active_input_streams_); | 254 base::AtomicRefCountDec(&num_active_input_streams_); |
| 222 } | 255 } |
| 223 | 256 |
| 224 bool AudioManagerBase::IsRecordingInProcess() { | 257 bool AudioManagerBase::IsRecordingInProcess() { |
| 225 return !base::AtomicRefCountIsZero(&num_active_input_streams_); | 258 return !base::AtomicRefCountIsZero(&num_active_input_streams_); |
| 226 } | 259 } |
| 227 | 260 |
| 228 void AudioManagerBase::Shutdown() { | 261 void AudioManagerBase::Shutdown() { |
| 229 // To avoid running into deadlocks while we stop the thread, shut it down | 262 // To avoid running into deadlocks while we stop the thread, shut it down |
| 230 // via a local variable while not holding the audio thread lock. | 263 // via a local variable while not holding the audio thread lock. |
| 231 scoped_ptr<base::Thread> audio_thread; | 264 scoped_ptr<media::AudioThread> audio_thread; |
| 232 { | 265 { |
| 233 base::AutoLock lock(audio_thread_lock_); | 266 base::AutoLock lock(audio_thread_lock_); |
| 234 audio_thread_.swap(audio_thread); | 267 audio_thread_.swap(audio_thread); |
| 235 } | 268 } |
| 236 | 269 |
| 237 if (!audio_thread.get()) | 270 if (!audio_thread.get()) |
| 238 return; | 271 return; |
| 239 | 272 |
| 240 CHECK_NE(MessageLoop::current(), audio_thread->message_loop()); | 273 CHECK_NE(MessageLoop::current(), audio_thread->message_loop()); |
| 241 | 274 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 #else | 319 #else |
| 287 // TODO(dalecurtis): This should include bits per channel and channel layout | 320 // TODO(dalecurtis): This should include bits per channel and channel layout |
| 288 // eventually. | 321 // eventually. |
| 289 return AudioParameters( | 322 return AudioParameters( |
| 290 AudioParameters::AUDIO_PCM_LOW_LATENCY, input_params.channel_layout(), | 323 AudioParameters::AUDIO_PCM_LOW_LATENCY, input_params.channel_layout(), |
| 291 GetAudioHardwareSampleRate(), 16, GetAudioHardwareBufferSize()); | 324 GetAudioHardwareSampleRate(), 16, GetAudioHardwareBufferSize()); |
| 292 #endif // defined(OS_IOS) | 325 #endif // defined(OS_IOS) |
| 293 } | 326 } |
| 294 | 327 |
| 295 } // namespace media | 328 } // namespace media |
| OLD | NEW |