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 |
12 #include "media/audio/audio_output_dispatcher_impl.h" | 15 #include "media/audio/audio_output_dispatcher_impl.h" |
13 #include "media/audio/audio_output_proxy.h" | 16 #include "media/audio/audio_output_proxy.h" |
14 #include "media/audio/audio_output_resampler.h" | 17 #include "media/audio/audio_output_resampler.h" |
15 #include "media/audio/audio_util.h" | 18 #include "media/audio/audio_util.h" |
16 #include "media/audio/fake_audio_input_stream.h" | 19 #include "media/audio/fake_audio_input_stream.h" |
17 #include "media/audio/fake_audio_output_stream.h" | 20 #include "media/audio/fake_audio_output_stream.h" |
18 #include "media/base/media_switches.h" | 21 #include "media/base/media_switches.h" |
19 | 22 |
20 namespace media { | 23 namespace media { |
21 | 24 |
22 static const int kStreamCloseDelaySeconds = 5; | 25 static const int kStreamCloseDelaySeconds = 5; |
23 | 26 |
24 // Default maximum number of output streams that can be open simultaneously | 27 // Default maximum number of output streams that can be open simultaneously |
25 // for all platforms. | 28 // for all platforms. |
26 static const int kDefaultMaxOutputStreams = 16; | 29 static const int kDefaultMaxOutputStreams = 16; |
27 | 30 |
28 // Default maximum number of input streams that can be open simultaneously | 31 // Default maximum number of input streams that can be open simultaneously |
29 // for all platforms. | 32 // for all platforms. |
30 static const int kDefaultMaxInputStreams = 16; | 33 static const int kDefaultMaxInputStreams = 16; |
31 | 34 |
32 static const int kMaxInputChannels = 2; | 35 static const int kMaxInputChannels = 2; |
33 | 36 |
| 37 #if defined(OS_ANDROID) |
| 38 static const int kAudioModeNormal = 0x00000000; |
| 39 static const int kAudioModeInCommunication = 0x00000003; |
| 40 #endif |
| 41 |
34 const char AudioManagerBase::kDefaultDeviceName[] = "Default"; | 42 const char AudioManagerBase::kDefaultDeviceName[] = "Default"; |
35 const char AudioManagerBase::kDefaultDeviceId[] = "default"; | 43 const char AudioManagerBase::kDefaultDeviceId[] = "default"; |
36 | 44 |
37 AudioManagerBase::AudioManagerBase() | 45 AudioManagerBase::AudioManagerBase() |
38 : num_active_input_streams_(0), | 46 : num_active_input_streams_(0), |
39 max_num_output_streams_(kDefaultMaxOutputStreams), | 47 max_num_output_streams_(kDefaultMaxOutputStreams), |
40 max_num_input_streams_(kDefaultMaxInputStreams), | 48 max_num_input_streams_(kDefaultMaxInputStreams), |
41 num_output_streams_(0), | 49 num_output_streams_(0), |
42 num_input_streams_(0), | 50 num_input_streams_(0), |
43 output_listeners_( | 51 output_listeners_( |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 stream = FakeAudioInputStream::MakeFakeStream(this, params); | 162 stream = FakeAudioInputStream::MakeFakeStream(this, params); |
155 break; | 163 break; |
156 default: | 164 default: |
157 stream = NULL; | 165 stream = NULL; |
158 break; | 166 break; |
159 } | 167 } |
160 | 168 |
161 if (stream) | 169 if (stream) |
162 ++num_input_streams_; | 170 ++num_input_streams_; |
163 | 171 |
| 172 #if defined(OS_ANDROID) |
| 173 if (num_input_streams_ == 1) |
| 174 SetAudioMode(kAudioModeInCommunication); |
| 175 #endif |
| 176 |
164 return stream; | 177 return stream; |
165 } | 178 } |
166 | 179 |
167 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( | 180 AudioOutputStream* AudioManagerBase::MakeAudioOutputStreamProxy( |
168 const AudioParameters& params) { | 181 const AudioParameters& params) { |
169 #if defined(OS_IOS) | 182 #if defined(OS_IOS) |
170 // IOS implements audio input only. | 183 // IOS implements audio input only. |
171 NOTIMPLEMENTED(); | 184 NOTIMPLEMENTED(); |
172 return NULL; | 185 return NULL; |
173 #else | 186 #else |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 // streams. | 256 // streams. |
244 --num_output_streams_; | 257 --num_output_streams_; |
245 delete stream; | 258 delete stream; |
246 } | 259 } |
247 | 260 |
248 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { | 261 void AudioManagerBase::ReleaseInputStream(AudioInputStream* stream) { |
249 DCHECK(stream); | 262 DCHECK(stream); |
250 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. | 263 // TODO(xians) : Have a clearer destruction path for the AudioInputStream. |
251 --num_input_streams_; | 264 --num_input_streams_; |
252 delete stream; | 265 delete stream; |
| 266 #if defined(OS_ANDROID) |
| 267 if (!num_input_streams_) |
| 268 SetAudioMode(kAudioModeNormal); |
| 269 #endif |
253 } | 270 } |
254 | 271 |
255 void AudioManagerBase::IncreaseActiveInputStreamCount() { | 272 void AudioManagerBase::IncreaseActiveInputStreamCount() { |
256 base::AtomicRefCountInc(&num_active_input_streams_); | 273 base::AtomicRefCountInc(&num_active_input_streams_); |
257 } | 274 } |
258 | 275 |
259 void AudioManagerBase::DecreaseActiveInputStreamCount() { | 276 void AudioManagerBase::DecreaseActiveInputStreamCount() { |
260 DCHECK(IsRecordingInProcess()); | 277 DCHECK(IsRecordingInProcess()); |
261 base::AtomicRefCountDec(&num_active_input_streams_); | 278 base::AtomicRefCountDec(&num_active_input_streams_); |
262 } | 279 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 // So, better crash now than later. | 327 // So, better crash now than later. |
311 DCHECK(dispatcher->HasOneRef()) << "AudioOutputProxies are still alive"; | 328 DCHECK(dispatcher->HasOneRef()) << "AudioOutputProxies are still alive"; |
312 dispatcher = NULL; | 329 dispatcher = NULL; |
313 } | 330 } |
314 } | 331 } |
315 | 332 |
316 output_dispatchers_.clear(); | 333 output_dispatchers_.clear(); |
317 #endif // defined(OS_IOS) | 334 #endif // defined(OS_IOS) |
318 } | 335 } |
319 | 336 |
| 337 #if defined(OS_ANDROID) |
| 338 // static |
| 339 bool AudioManagerBase::RegisterAudioManager(JNIEnv* env) { |
| 340 return RegisterNativesImpl(env); |
| 341 } |
| 342 #endif |
| 343 |
320 void AudioManagerBase::AddOutputDeviceChangeListener( | 344 void AudioManagerBase::AddOutputDeviceChangeListener( |
321 AudioDeviceListener* listener) { | 345 AudioDeviceListener* listener) { |
322 DCHECK(message_loop_->BelongsToCurrentThread()); | 346 DCHECK(message_loop_->BelongsToCurrentThread()); |
323 output_listeners_.AddObserver(listener); | 347 output_listeners_.AddObserver(listener); |
324 } | 348 } |
325 | 349 |
326 void AudioManagerBase::RemoveOutputDeviceChangeListener( | 350 void AudioManagerBase::RemoveOutputDeviceChangeListener( |
327 AudioDeviceListener* listener) { | 351 AudioDeviceListener* listener) { |
328 DCHECK(message_loop_->BelongsToCurrentThread()); | 352 DCHECK(message_loop_->BelongsToCurrentThread()); |
329 output_listeners_.RemoveObserver(listener); | 353 output_listeners_.RemoveObserver(listener); |
330 } | 354 } |
331 | 355 |
332 void AudioManagerBase::NotifyAllOutputDeviceChangeListeners() { | 356 void AudioManagerBase::NotifyAllOutputDeviceChangeListeners() { |
333 DCHECK(message_loop_->BelongsToCurrentThread()); | 357 DCHECK(message_loop_->BelongsToCurrentThread()); |
334 DVLOG(1) << "Firing OnDeviceChange() notifications."; | 358 DVLOG(1) << "Firing OnDeviceChange() notifications."; |
335 FOR_EACH_OBSERVER(AudioDeviceListener, output_listeners_, OnDeviceChange()); | 359 FOR_EACH_OBSERVER(AudioDeviceListener, output_listeners_, OnDeviceChange()); |
336 } | 360 } |
337 | 361 |
338 AudioParameters AudioManagerBase::GetDefaultOutputStreamParameters() { | 362 AudioParameters AudioManagerBase::GetDefaultOutputStreamParameters() { |
339 return GetPreferredOutputStreamParameters(AudioParameters()); | 363 return GetPreferredOutputStreamParameters(AudioParameters()); |
340 } | 364 } |
341 | 365 |
342 AudioParameters AudioManagerBase::GetInputStreamParameters( | 366 AudioParameters AudioManagerBase::GetInputStreamParameters( |
343 const std::string& device_id) { | 367 const std::string& device_id) { |
344 NOTREACHED(); | 368 NOTREACHED(); |
345 return AudioParameters(); | 369 return AudioParameters(); |
346 } | 370 } |
347 | 371 |
| 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 |
348 } // namespace media | 382 } // namespace media |
OLD | NEW |