Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2740)

Unified Diff: media/audio/android/audio_manager_android.cc

Issue 117073002: Reland "Refactor audio manager for Android to avoid heavy tasks at startup" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 3464d89a30f4ccd9f8dd4a0c167162d69dbb631f..e67700c1b4d3aed10c5159d83341d6a988041da1 100644
--- a/media/audio/android/audio_manager_android.cc
+++ b/media/audio/android/audio_manager_android.cc
@@ -37,9 +37,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;
@@ -126,12 +123,17 @@ AudioOutputStream* AudioManagerAndroid::MakeAudioOutputStream(
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_);
@@ -143,22 +145,37 @@ AudioOutputStream* AudioManagerAndroid::MakeAudioOutputStream(
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 @@ 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;
+ }
if (params.effects() != AudioParameters::NO_EFFECTS) {
// Platform effects can only be enabled through the AudioRecord path.
@@ -252,6 +272,10 @@ AudioParameters AudioManagerAndroid::GetPreferredOutputStreamParameters(
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::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
@@ -302,7 +326,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());
}
« no previous file with comments | « media/audio/android/audio_manager_android.h ('k') | media/base/android/java/src/org/chromium/media/AudioManagerAndroid.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698