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_( |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 stream = FakeAudioInputStream::MakeFakeStream(this, params); | 154 stream = FakeAudioInputStream::MakeFakeStream(this, params); |
163 break; | 155 break; |
164 default: | 156 default: |
165 stream = NULL; | 157 stream = NULL; |
166 break; | 158 break; |
167 } | 159 } |
168 | 160 |
169 if (stream) | 161 if (stream) |
170 ++num_input_streams_; | 162 ++num_input_streams_; |
171 | 163 |
172 #if defined(OS_ANDROID) | |
173 if (num_input_streams_ == 1) | |
174 SetAudioMode(kAudioModeInCommunication); | |
175 #endif | |
176 | |
177 return stream; | 164 return stream; |
178 } | 165 } |
179 | 166 |
180 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( | 167 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( |
181 const AudioParameters& params) { | 168 const AudioParameters& params) { |
182 #if defined(OS_IOS) | 169 #if defined(OS_IOS) |
183 // IOS implements audio input only. | 170 // IOS implements audio input only. |
184 NOTIMPLEMENTED(); | 171 NOTIMPLEMENTED(); |
185 return NULL; | 172 return NULL; |
186 #else | 173 #else |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 // streams. | 243 // streams. |
257 --num_output_streams_; | 244 --num_output_streams_; |
258 delete stream; | 245 delete stream; |
259 } | 246 } |
260 | 247 |
261 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { | 248 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { |
262 DCHECK(stream); | 249 DCHECK(stream); |
263 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. | 250 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. |
264 --num_input_streams_; | 251 --num_input_streams_; |
265 delete stream; | 252 delete stream; |
266 #if defined(OS_ANDROID) | |
267 if (!num_input_streams_) | |
268 SetAudioMode(kAudioModeNormal); | |
269 #endif | |
270 } | 253 } |
271 | 254 |
272 void AudioManagerBase::IncreaseActiveInputStreamCount() { | 255 void AudioManagerBase::IncreaseActiveInputStreamCount() { |
273 base::AtomicRefCountInc(&num_active_input_streams_); | 256 base::AtomicRefCountInc(&num_active_input_streams_); |
274 } | 257 } |
275 | 258 |
276 void AudioManagerBase::DecreaseActiveInputStreamCount() { | 259 void AudioManagerBase::DecreaseActiveInputStreamCount() { |
277 DCHECK(IsRecordingInProcess()); | 260 DCHECK(IsRecordingInProcess()); |
278 base::AtomicRefCountDec(&num_active_input_streams_); | 261 base::AtomicRefCountDec(&num_active_input_streams_); |
279 } | 262 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 // So, better crash now than later. | 310 // So, better crash now than later. |
328 DCHECK(dispatcher->HasOneRef()) << "AudioOutputProxies are still alive"; | 311 DCHECK(dispatcher->HasOneRef()) << "AudioOutputProxies are still alive"; |
329 dispatcher = NULL; | 312 dispatcher = NULL; |
330 } | 313 } |
331 } | 314 } |
332 | 315 |
333 output_dispatchers_.clear(); | 316 output_dispatchers_.clear(); |
334 #endif // defined(OS_IOS) | 317 #endif // defined(OS_IOS) |
335 } | 318 } |
336 | 319 |
337 #if defined(OS_ANDROID) | |
338 // static | |
339 bool AudioManagerBase::RegisterAudioManager(JNIEnv* env) { | |
340 return RegisterNativesImpl(env); | |
341 } | |
342 #endif | |
343 | |
344 void AudioManagerBase::AddOutputDeviceChangeListener( | 320 void AudioManagerBase::AddOutputDeviceChangeListener( |
345 AudioDeviceListener* listener) { | 321 AudioDeviceListener* listener) { |
346 DCHECK(message_loop_->BelongsToCurrentThread()); | 322 DCHECK(message_loop_->BelongsToCurrentThread()); |
347 output_listeners_.AddObserver(listener); | 323 output_listeners_.AddObserver(listener); |
348 } | 324 } |
349 | 325 |
350 void AudioManagerBase::RemoveOutputDeviceChangeListener( | 326 void AudioManagerBase::RemoveOutputDeviceChangeListener( |
351 AudioDeviceListener* listener) { | 327 AudioDeviceListener* listener) { |
352 DCHECK(message_loop_->BelongsToCurrentThread()); | 328 DCHECK(message_loop_->BelongsToCurrentThread()); |
353 output_listeners_.RemoveObserver(listener); | 329 output_listeners_.RemoveObserver(listener); |
354 } | 330 } |
355 | 331 |
356 void AudioManagerBase::NotifyAllOutputDeviceChangeListeners() { | 332 void AudioManagerBase::NotifyAllOutputDeviceChangeListeners() { |
357 DCHECK(message_loop_->BelongsToCurrentThread()); | 333 DCHECK(message_loop_->BelongsToCurrentThread()); |
358 DVLOG(1) << "Firing OnDeviceChange() notifications."; | 334 DVLOG(1) << "Firing OnDeviceChange() notifications."; |
359 FOR_EACH_OBSERVER(AudioDeviceListener, output_listeners_, OnDeviceChange()); | 335 FOR_EACH_OBSERVER(AudioDeviceListener, output_listeners_, OnDeviceChange()); |
360 } | 336 } |
361 | 337 |
362 AudioParameters AudioManagerBase::GetDefaultOutputStreamParameters() { | 338 AudioParameters AudioManagerBase::GetDefaultOutputStreamParameters() { |
363 return GetPreferredOutputStreamParameters(AudioParameters()); | 339 return GetPreferredOutputStreamParameters(AudioParameters()); |
364 } | 340 } |
365 | 341 |
366 AudioParameters AudioManagerBase::GetInputStreamParameters( | 342 AudioParameters AudioManagerBase::GetInputStreamParameters( |
367 const std::string& device_id) { | 343 const std::string& device_id) { |
368 NOTREACHED(); | 344 NOTREACHED(); |
369 return AudioParameters(); | 345 return AudioParameters(); |
370 } | 346 } |
371 | 347 |
372 #if defined(OS_ANDROID) | |
373 void AudioManagerBase::SetAudioMode(int mode) { | |
374 JNIEnv* env = base::android::AttachCurrentThread(); | |
375 jobject context = base::android::GetApplicationContext(); | |
376 DCHECK(context); | |
377 | |
378 Java_AudioManagerAndroid_setMode(env, context, mode); | |
379 } | |
380 #endif | |
381 | |
382 } // namespace media | 348 } // namespace media |
OLD | NEW |