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/command_line.h" |
10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
11 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
12 #if defined(OS_ANDROID) | |
13 #include "jni/AudioManagerAndroid_jni.h" | |
14 #endif | |
15 #include "media/audio/audio_output_dispatcher_impl.h" | 12 #include "media/audio/audio_output_dispatcher_impl.h" |
16 #include "media/audio/audio_output_proxy.h" | 13 #include "media/audio/audio_output_proxy.h" |
17 #include "media/audio/audio_output_resampler.h" | 14 #include "media/audio/audio_output_resampler.h" |
18 #include "media/audio/audio_util.h" | 15 #include "media/audio/audio_util.h" |
19 #include "media/audio/fake_audio_input_stream.h" | 16 #include "media/audio/fake_audio_input_stream.h" |
20 #include "media/audio/fake_audio_output_stream.h" | 17 #include "media/audio/fake_audio_output_stream.h" |
21 #include "media/base/media_switches.h" | 18 #include "media/base/media_switches.h" |
22 | 19 |
23 namespace media { | 20 namespace media { |
24 | 21 |
25 static const int kStreamCloseDelaySeconds = 5; | 22 static const int kStreamCloseDelaySeconds = 5; |
26 | 23 |
27 // Default maximum number of output streams that can be open simultaneously | 24 // Default maximum number of output streams that can be open simultaneously |
28 // for all platforms. | 25 // for all platforms. |
29 static const int kDefaultMaxOutputStreams = 16; | 26 static const int kDefaultMaxOutputStreams = 16; |
30 | 27 |
31 // Default maximum number of input streams that can be open simultaneously | 28 // Default maximum number of input streams that can be open simultaneously |
32 // for all platforms. | 29 // for all platforms. |
33 static const int kDefaultMaxInputStreams = 16; | 30 static const int kDefaultMaxInputStreams = 16; |
34 | 31 |
35 static const int kMaxInputChannels = 2; | 32 static const int kMaxInputChannels = 2; |
36 | 33 |
37 #if defined(OS_ANDROID) | |
38 static const int kAudioModeNormal = 0x00000000; | |
39 static const int kAudioModeInCommunication = 0x00000003; | |
40 #endif | |
41 | |
42 const char AudioManagerBase::kDefaultDeviceName[] = "Default"; | 34 const char AudioManagerBase::kDefaultDeviceName[] = "Default"; |
43 const char AudioManagerBase::kDefaultDeviceId[] = "default"; | 35 const char AudioManagerBase::kDefaultDeviceId[] = "default"; |
44 | 36 |
45 AudioManagerBase::AudioManagerBase() | 37 AudioManagerBase::AudioManagerBase() |
46 : num_active_input_streams_(0), | 38 : num_active_input_streams_(0), |
47 max_num_output_streams_(kDefaultMaxOutputStreams), | 39 max_num_output_streams_(kDefaultMaxOutputStreams), |
48 max_num_input_streams_(kDefaultMaxInputStreams), | 40 max_num_input_streams_(kDefaultMaxInputStreams), |
49 num_output_streams_(0), | 41 num_output_streams_(0), |
50 num_input_streams_(0), | 42 num_input_streams_(0), |
51 output_listeners_( | 43 output_listeners_( |
52 ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), | 44 ObserverList<AudioDeviceListener>::NOTIFY_EXISTING_ONLY), |
53 audio_thread_(new base::Thread("AudioThread")) { | 45 audio_thread_(new base::Thread("AudioThread")) { |
54 #if defined(OS_WIN) | 46 #if defined(OS_WIN) |
55 audio_thread_->init_com_with_mta(true); | 47 audio_thread_->init_com_with_mta(true); |
56 #endif | 48 #endif |
57 #if defined(OS_MACOSX) | 49 #if defined(OS_MACOSX) |
58 // On Mac, use a UI loop to get native message pump so that CoreAudio property | 50 // On Mac, use a UI loop to get native message pump so that CoreAudio property |
59 // listener callbacks fire. | 51 // listener callbacks fire. |
60 CHECK(audio_thread_->StartWithOptions( | 52 CHECK(audio_thread_->StartWithOptions( |
61 base::Thread::Options(MessageLoop::TYPE_UI, 0))); | 53 base::Thread::Options(MessageLoop::TYPE_UI, 0))); |
62 #else | 54 #else |
63 CHECK(audio_thread_->Start()); | 55 CHECK(audio_thread_->Start()); |
64 #endif | 56 #endif |
65 message_loop_ = audio_thread_->message_loop_proxy(); | 57 message_loop_ = audio_thread_->message_loop_proxy(); |
66 | |
67 #if defined(OS_ANDROID) | |
68 JNIEnv* env = base::android::AttachCurrentThread(); | |
69 jobject context = base::android::GetApplicationContext(); | |
70 j_audio_manager_.Reset( | |
71 Java_AudioManagerAndroid_createAudioManagerAndroid(env, context)); | |
72 #endif | |
73 } | 58 } |
74 | 59 |
75 AudioManagerBase::~AudioManagerBase() { | 60 AudioManagerBase::~AudioManagerBase() { |
76 // The platform specific AudioManager implementation must have already | 61 // The platform specific AudioManager implementation must have already |
77 // stopped the audio thread. Otherwise, we may destroy audio streams before | 62 // stopped the audio thread. Otherwise, we may destroy audio streams before |
78 // stopping the thread, resulting an unexpected behavior. | 63 // stopping the thread, resulting an unexpected behavior. |
79 // This way we make sure activities of the audio streams are all stopped | 64 // This way we make sure activities of the audio streams are all stopped |
80 // before we destroy them. | 65 // before we destroy them. |
81 CHECK(!audio_thread_.get()); | 66 CHECK(!audio_thread_.get()); |
82 // All the output streams should have been deleted. | 67 // All the output streams should have been deleted. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 case AudioParameters::AUDIO_FAKE: | 112 case AudioParameters::AUDIO_FAKE: |
128 stream = FakeAudioOutputStream::MakeFakeStream(this, params); | 113 stream = FakeAudioOutputStream::MakeFakeStream(this, params); |
129 break; | 114 break; |
130 default: | 115 default: |
131 stream = NULL; | 116 stream = NULL; |
132 break; | 117 break; |
133 } | 118 } |
134 | 119 |
135 if (stream) { | 120 if (stream) { |
136 ++num_output_streams_; | 121 ++num_output_streams_; |
137 #if defined(OS_ANDROID) | |
138 if (num_output_streams_ == 1) | |
139 RegisterHeadsetReceiver(); | |
140 #endif | |
141 } | 122 } |
142 | 123 |
143 return stream; | 124 return stream; |
144 } | 125 } |
145 | 126 |
146 AudioInputStream* AudioManagerBase::MakeAudioInputStream( | 127 AudioInputStream* AudioManagerBase::MakeAudioInputStream( |
147 const AudioParameters& params, const std::string& device_id) { | 128 const AudioParameters& params, const std::string& device_id) { |
148 // TODO(miu): Fix ~20 call points across several unit test modules to call | 129 // TODO(miu): Fix ~20 call points across several unit test modules to call |
149 // this method on the audio thread, then uncomment the following: | 130 // this method on the audio thread, then uncomment the following: |
150 // DCHECK(message_loop_->BelongsToCurrentThread()); | 131 // DCHECK(message_loop_->BelongsToCurrentThread()); |
(...skipping 22 matching lines...) Expand all Loading... |
173 case AudioParameters::AUDIO_FAKE: | 154 case AudioParameters::AUDIO_FAKE: |
174 stream = FakeAudioInputStream::MakeFakeStream(this, params); | 155 stream = FakeAudioInputStream::MakeFakeStream(this, params); |
175 break; | 156 break; |
176 default: | 157 default: |
177 stream = NULL; | 158 stream = NULL; |
178 break; | 159 break; |
179 } | 160 } |
180 | 161 |
181 if (stream) { | 162 if (stream) { |
182 ++num_input_streams_; | 163 ++num_input_streams_; |
183 #if defined(OS_ANDROID) | |
184 if (num_input_streams_ == 1) | |
185 SetAudioMode(kAudioModeInCommunication); | |
186 #endif | |
187 } | 164 } |
188 | 165 |
189 return stream; | 166 return stream; |
190 } | 167 } |
191 | 168 |
192 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( | 169 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( |
193 const AudioParameters& params) { | 170 const AudioParameters& params) { |
194 #if defined(OS_IOS) | 171 #if defined(OS_IOS) |
195 // IOS implements audio input only. | 172 // IOS implements audio input only. |
196 NOTIMPLEMENTED(); | 173 NOTIMPLEMENTED(); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 media::AudioDeviceNames* device_names) { | 238 media::AudioDeviceNames* device_names) { |
262 } | 239 } |
263 | 240 |
264 void AudioManagerBase::ReleaseOutputStream(AudioOutputStream* stream) { | 241 void AudioManagerBase::ReleaseOutputStream(AudioOutputStream* stream) { |
265 DCHECK(stream); | 242 DCHECK(stream); |
266 // TODO(xians) : Have a clearer destruction path for the AudioOutputStream. | 243 // TODO(xians) : Have a clearer destruction path for the AudioOutputStream. |
267 // For example, pass the ownership to AudioManager so it can delete the | 244 // For example, pass the ownership to AudioManager so it can delete the |
268 // streams. | 245 // streams. |
269 --num_output_streams_; | 246 --num_output_streams_; |
270 delete stream; | 247 delete stream; |
271 #if defined(OS_ANDROID) | |
272 if (!num_output_streams_) | |
273 UnregisterHeadsetReceiver(); | |
274 #endif | |
275 } | 248 } |
276 | 249 |
277 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { | 250 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { |
278 DCHECK(stream); | 251 DCHECK(stream); |
279 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. | 252 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. |
280 --num_input_streams_; | 253 --num_input_streams_; |
281 delete stream; | 254 delete stream; |
282 #if defined(OS_ANDROID) | |
283 if (!num_input_streams_) | |
284 SetAudioMode(kAudioModeNormal); | |
285 #endif | |
286 } | 255 } |
287 | 256 |
288 void AudioManagerBase::IncreaseActiveInputStreamCount() { | 257 void AudioManagerBase::IncreaseActiveInputStreamCount() { |
289 base::AtomicRefCountInc(&num_active_input_streams_); | 258 base::AtomicRefCountInc(&num_active_input_streams_); |
290 } | 259 } |
291 | 260 |
292 void AudioManagerBase::DecreaseActiveInputStreamCount() { | 261 void AudioManagerBase::DecreaseActiveInputStreamCount() { |
293 DCHECK(IsRecordingInProcess()); | 262 DCHECK(IsRecordingInProcess()); |
294 base::AtomicRefCountDec(&num_active_input_streams_); | 263 base::AtomicRefCountDec(&num_active_input_streams_); |
295 } | 264 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 // So, better crash now than later. | 312 // So, better crash now than later. |
344 DCHECK(dispatcher->HasOneRef()) << "AudioOutputProxies are still alive"; | 313 DCHECK(dispatcher->HasOneRef()) << "AudioOutputProxies are still alive"; |
345 dispatcher = NULL; | 314 dispatcher = NULL; |
346 } | 315 } |
347 } | 316 } |
348 | 317 |
349 output_dispatchers_.clear(); | 318 output_dispatchers_.clear(); |
350 #endif // defined(OS_IOS) | 319 #endif // defined(OS_IOS) |
351 } | 320 } |
352 | 321 |
353 #if defined(OS_ANDROID) | |
354 // static | |
355 bool AudioManagerBase::RegisterAudioManager(JNIEnv* env) { | |
356 return RegisterNativesImpl(env); | |
357 } | |
358 #endif | |
359 | |
360 void AudioManagerBase::AddOutputDeviceChangeListener( | 322 void AudioManagerBase::AddOutputDeviceChangeListener( |
361 AudioDeviceListener* listener) { | 323 AudioDeviceListener* listener) { |
362 DCHECK(message_loop_->BelongsToCurrentThread()); | 324 DCHECK(message_loop_->BelongsToCurrentThread()); |
363 output_listeners_.AddObserver(listener); | 325 output_listeners_.AddObserver(listener); |
364 } | 326 } |
365 | 327 |
366 void AudioManagerBase::RemoveOutputDeviceChangeListener( | 328 void AudioManagerBase::RemoveOutputDeviceChangeListener( |
367 AudioDeviceListener* listener) { | 329 AudioDeviceListener* listener) { |
368 DCHECK(message_loop_->BelongsToCurrentThread()); | 330 DCHECK(message_loop_->BelongsToCurrentThread()); |
369 output_listeners_.RemoveObserver(listener); | 331 output_listeners_.RemoveObserver(listener); |
370 } | 332 } |
371 | 333 |
372 void AudioManagerBase::NotifyAllOutputDeviceChangeListeners() { | 334 void AudioManagerBase::NotifyAllOutputDeviceChangeListeners() { |
373 DCHECK(message_loop_->BelongsToCurrentThread()); | 335 DCHECK(message_loop_->BelongsToCurrentThread()); |
374 DVLOG(1) << "Firing OnDeviceChange() notifications."; | 336 DVLOG(1) << "Firing OnDeviceChange() notifications."; |
375 FOR_EACH_OBSERVER(AudioDeviceListener, output_listeners_, OnDeviceChange()); | 337 FOR_EACH_OBSERVER(AudioDeviceListener, output_listeners_, OnDeviceChange()); |
376 } | 338 } |
377 | 339 |
378 AudioParameters AudioManagerBase::GetDefaultOutputStreamParameters() { | 340 AudioParameters AudioManagerBase::GetDefaultOutputStreamParameters() { |
379 return GetPreferredOutputStreamParameters(AudioParameters()); | 341 return GetPreferredOutputStreamParameters(AudioParameters()); |
380 } | 342 } |
381 | 343 |
382 AudioParameters AudioManagerBase::GetInputStreamParameters( | 344 AudioParameters AudioManagerBase::GetInputStreamParameters( |
383 const std::string& device_id) { | 345 const std::string& device_id) { |
384 NOTREACHED(); | 346 NOTREACHED(); |
385 return AudioParameters(); | 347 return AudioParameters(); |
386 } | 348 } |
387 | 349 |
388 #if defined(OS_ANDROID) | |
389 void AudioManagerBase::SetAudioMode(int mode) { | |
390 Java_AudioManagerAndroid_setMode( | |
391 base::android::AttachCurrentThread(), | |
392 j_audio_manager_.obj(), mode); | |
393 } | |
394 | |
395 void AudioManagerBase::RegisterHeadsetReceiver() { | |
396 Java_AudioManagerAndroid_registerHeadsetReceiver( | |
397 base::android::AttachCurrentThread(), | |
398 j_audio_manager_.obj()); | |
399 } | |
400 | |
401 void AudioManagerBase::UnregisterHeadsetReceiver() { | |
402 Java_AudioManagerAndroid_unregisterHeadsetReceiver( | |
403 base::android::AttachCurrentThread(), | |
404 j_audio_manager_.obj()); | |
405 } | |
406 #endif // defined(OS_ANDROID) | |
407 | |
408 } // namespace media | 350 } // namespace media |
OLD | NEW |