Chromium Code Reviews| Index: media/audio/android/audio_manager_android.cc |
| diff --git a/media/audio/android/audio_manager_android.cc b/media/audio/android/audio_manager_android.cc |
| index b588768bbd08c1fcfbb75df70248a9da752a28a4..6d5b1259175fa3740323ec837269251b1525902e 100644 |
| --- a/media/audio/android/audio_manager_android.cc |
| +++ b/media/audio/android/audio_manager_android.cc |
| @@ -36,9 +36,6 @@ static void AddDefaultDevice(AudioDeviceNames* device_names) { |
| // Maximum number of output streams that can be open simultaneously. |
| static const int kMaxOutputStreams = 10; |
| -static const int kAudioModeNormal = 0x00000000; |
| -static const int kAudioModeInCommunication = 0x00000003; |
| - |
| static const int kDefaultInputBufferSize = 1024; |
| static const int kDefaultOutputBufferSize = 2048; |
| @@ -121,11 +118,17 @@ AudioOutputStream* AudioManagerAndroid::MakeAudioOutputStream( |
| const AudioParameters& params, |
| const std::string& device_id, |
| const std::string& input_device_id) { |
| + bool has_no_streams = HasNoAudioStreams(); |
| AudioOutputStream* stream = |
| AudioManagerBase::MakeAudioOutputStream(params, std::string(), |
| std::string()); |
| - if (stream && output_stream_count() == 1) { |
| - SetAudioMode(kAudioModeInCommunication); |
| + |
| + // The audio manager for Android creates streams intended for real-time |
| + // VoIP sessions and therefore sets the audio mode to MODE_IN_COMMUNICATION. |
| + // If a Bluetooth headset is used, the audio stream will use the SCO |
| + // channel and therefore have a limited bandwidth (8-16kHz). |
| + if (stream && has_no_streams) { |
|
bulach
2013/12/13 13:49:49
nit: this condition looks funky :) maybe rename s/
henrika (OOO until Aug 14)
2013/12/13 14:58:42
Hmm, it is a result of this long review...
Will tr
|
| + SetCommunicationAudioModeOn(true); |
| } |
| { |
| @@ -138,15 +141,27 @@ AudioOutputStream* AudioManagerAndroid::MakeAudioOutputStream( |
| AudioInputStream* AudioManagerAndroid::MakeAudioInputStream( |
| const AudioParameters& params, const std::string& device_id) { |
| + bool has_no_streams = HasNoAudioStreams(); |
| AudioInputStream* stream = |
| AudioManagerBase::MakeAudioInputStream(params, device_id); |
| + |
| + // The audio manager for Android creates streams intended for real-time |
| + // VoIP sessions and therefore sets the audio mode to MODE_IN_COMMUNICATION. |
| + // If a Bluetooth headset is used, the audio stream will use the SCO |
| + // channel and therefore have a limited bandwidth (8kHz). |
| + if (stream && has_no_streams) { |
| + SetCommunicationAudioModeOn(true); |
| + } |
| return stream; |
| } |
| void AudioManagerAndroid::ReleaseOutputStream(AudioOutputStream* stream) { |
| AudioManagerBase::ReleaseOutputStream(stream); |
| - if (!output_stream_count()) { |
| - SetAudioMode(kAudioModeNormal); |
| + |
| + // Restore the audio mode which was used before the first communication- |
| + // mode stream was created. |
| + if (HasNoAudioStreams()) { |
| + SetCommunicationAudioModeOn(false); |
| } |
| base::AutoLock lock(streams_lock_); |
| streams_.erase(static_cast<OpenSLESOutputStream*>(stream)); |
| @@ -154,6 +169,12 @@ void AudioManagerAndroid::ReleaseOutputStream(AudioOutputStream* stream) { |
| void AudioManagerAndroid::ReleaseInputStream(AudioInputStream* stream) { |
| AudioManagerBase::ReleaseInputStream(stream); |
| + |
| + // Restore the audio mode which was used before the first communication- |
| + // mode stream was created. |
| + if (HasNoAudioStreams()) { |
| + SetCommunicationAudioModeOn(false); |
| + } |
| } |
| AudioOutputStream* AudioManagerAndroid::MakeLinearOutputStream( |
| @@ -184,11 +205,14 @@ AudioInputStream* AudioManagerAndroid::MakeLowLatencyInputStream( |
| const AudioParameters& params, const std::string& device_id) { |
| DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); |
| DLOG_IF(ERROR, device_id.empty()) << "Invalid device ID!"; |
| - // Utilize the device ID to select the correct input device. |
| + // Use the device ID to select the correct input device. |
| // Note that the input device is always associated with a certain output |
| // device, i.e., this selection does also switch the output device. |
| // All input and output streams will be affected by the device selection. |
| - SetAudioDevice(device_id); |
| + if (!SetAudioDevice(device_id)) { |
| + LOG(ERROR) << "Unable to select audio device!"; |
| + return NULL; |
| + } |
| return new OpenSLESInputStream(this, params); |
| } |
| @@ -233,6 +257,10 @@ AudioParameters AudioManagerAndroid::GetPreferredOutputStreamParameters( |
| sample_rate, bits_per_sample, buffer_size); |
| } |
| +bool AudioManagerAndroid::HasNoAudioStreams() { |
| + return output_stream_count() == 0 && input_stream_count() == 0; |
| +} |
| + |
| // static |
| bool AudioManagerAndroid::RegisterAudioManager(JNIEnv* env) { |
| return RegisterNativesImpl(env); |
| @@ -267,13 +295,13 @@ void AudioManagerAndroid::DoSetMuteOnAudioThread(bool muted) { |
| } |
| } |
| -void AudioManagerAndroid::SetAudioMode(int mode) { |
| - Java_AudioManagerAndroid_setMode( |
| +void AudioManagerAndroid::SetCommunicationAudioModeOn(bool on) { |
| + Java_AudioManagerAndroid_setCommunicationAudioModeOn( |
| base::android::AttachCurrentThread(), |
| - j_audio_manager_.obj(), mode); |
| + j_audio_manager_.obj(), on); |
| } |
| -void AudioManagerAndroid::SetAudioDevice(const std::string& device_id) { |
| +bool AudioManagerAndroid::SetAudioDevice(const std::string& device_id) { |
| JNIEnv* env = AttachCurrentThread(); |
| // Send the unique device ID to the Java audio manager and make the |
| @@ -283,7 +311,7 @@ void AudioManagerAndroid::SetAudioDevice(const std::string& device_id) { |
| env, |
| device_id == AudioManagerBase::kDefaultDeviceId ? |
| std::string() : device_id); |
| - Java_AudioManagerAndroid_setDevice( |
| + return Java_AudioManagerAndroid_setDevice( |
| env, j_audio_manager_.obj(), j_device_id.obj()); |
| } |