Index: trunk/src/media/audio/android/audio_manager_android.cc |
=================================================================== |
--- trunk/src/media/audio/android/audio_manager_android.cc (revision 240886) |
+++ trunk/src/media/audio/android/audio_manager_android.cc (working copy) |
@@ -37,9 +37,6 @@ |
// 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; |
@@ -126,13 +123,18 @@ |
const AudioParameters& params, |
const std::string& device_id, |
const std::string& input_device_id) { |
+ bool had_no_streams = HadNoAudioStreams(); |
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 && had_no_streams) |
+ SetCommunicationAudioModeOn(true); |
+ |
{ |
base::AutoLock lock(streams_lock_); |
streams_.insert(static_cast<OpenSLESOutputStream*>(stream)); |
@@ -143,22 +145,37 @@ |
AudioInputStream* AudioManagerAndroid::MakeAudioInputStream( |
const AudioParameters& params, const std::string& device_id) { |
+ bool had_no_streams = HadNoAudioStreams(); |
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 && had_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 (HadNoAudioStreams()) |
+ SetCommunicationAudioModeOn(false); |
base::AutoLock lock(streams_lock_); |
streams_.erase(static_cast<OpenSLESOutputStream*>(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 (HadNoAudioStreams()) |
+ SetCommunicationAudioModeOn(false); |
} |
AudioOutputStream* AudioManagerAndroid::MakeLinearOutputStream( |
@@ -189,11 +206,14 @@ |
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; |
+ } |
if (params.effects() != AudioParameters::NO_EFFECTS) { |
// Platform effects can only be enabled through the AudioRecord path. |
@@ -252,6 +272,10 @@ |
sample_rate, bits_per_sample, buffer_size, AudioParameters::NO_EFFECTS); |
} |
+bool AudioManagerAndroid::HadNoAudioStreams() { |
+ return output_stream_count() == 0 && input_stream_count() == 0; |
+} |
+ |
// static |
bool AudioManagerAndroid::RegisterAudioManager(JNIEnv* env) { |
return RegisterNativesImpl(env); |
@@ -286,13 +310,13 @@ |
} |
} |
-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 |
@@ -302,7 +326,7 @@ |
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()); |
} |