From 09bf1a169b5347bae6fe9fe85ba290b029d0cf6b Mon Sep 17 00:00:00 2001 From: henrika Date: Fri, 10 Apr 2015 11:46:55 +0200 Subject: [PATCH] Delays changing to COMMUNICATION mode until streaming starts. Restores stored audio mode when all streaming stops. TBR=glaznev BUG=NONE TEST=AppRTCDemo Review URL: https://webrtc-codereview.appspot.com/46869005 Cr-Commit-Position: refs/heads/master@{#8970} --- .../android/audio_device_template.h | 18 +++++++++-- .../audio_device/android/audio_manager.cc | 12 +++++++ .../audio_device/android/audio_manager.h | 9 +++--- .../voiceengine/WebRtcAudioManager.java | 32 ++++++++++++++++--- 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/webrtc/modules/audio_device/android/audio_device_template.h b/webrtc/modules/audio_device/android/audio_device_template.h index c1a7e1917..fa37eb283 100644 --- a/webrtc/modules/audio_device/android/audio_device_template.h +++ b/webrtc/modules/audio_device/android/audio_device_template.h @@ -118,6 +118,7 @@ class AudioDeviceTemplate : public AudioDeviceGeneric { } int32_t InitPlayout() override { + audio_manager_.SetCommunicationMode(true); return output_.InitPlayout(); } @@ -131,6 +132,7 @@ class AudioDeviceTemplate : public AudioDeviceGeneric { } int32_t InitRecording() override { + audio_manager_.SetCommunicationMode(true); return input_.InitRecording(); } @@ -143,7 +145,13 @@ class AudioDeviceTemplate : public AudioDeviceGeneric { } int32_t StopPlayout() override { - return output_.StopPlayout(); + int32_t err = output_.StopPlayout(); + if (!Recording()) { + // Restore initial audio mode since all audio streaming is disabled. + // The default mode was stored in Init(). + audio_manager_.SetCommunicationMode(false); + } + return err; } bool Playing() const override { @@ -155,7 +163,13 @@ class AudioDeviceTemplate : public AudioDeviceGeneric { } int32_t StopRecording() override { - return input_.StopRecording(); + int32_t err = input_.StopRecording(); + if (!Playing()) { + // Restore initial audio mode since all audio streaming is disabled. + // The default mode was is stored in Init(). + audio_manager_.SetCommunicationMode(false); + } + return err; } bool Recording() const override { diff --git a/webrtc/modules/audio_device/android/audio_manager.cc b/webrtc/modules/audio_device/android/audio_manager.cc index a4f54bcca..717d164a0 100644 --- a/webrtc/modules/audio_device/android/audio_manager.cc +++ b/webrtc/modules/audio_device/android/audio_manager.cc @@ -121,6 +121,18 @@ bool AudioManager::Close() { return true; } +void AudioManager::SetCommunicationMode(bool enable) { + ALOGD("SetCommunicationMode(%d)%s", enable, GetThreadInfo().c_str()); + DCHECK(thread_checker_.CalledOnValidThread()); + DCHECK(initialized_); + AttachThreadScoped ats(g_jvm); + JNIEnv* jni = ats.env(); + jmethodID setcommID = GetMethodID( + jni, g_audio_manager_class, "setCommunicationMode", "(Z)V"); + jni->CallVoidMethod(j_audio_manager_, setcommID, enable); + CHECK_EXCEPTION(jni); +} + void JNICALL AudioManager::CacheAudioParameters(JNIEnv* env, jobject obj, jint sample_rate, jint channels, jlong nativeAudioManager) { webrtc::AudioManager* this_object = diff --git a/webrtc/modules/audio_device/android/audio_manager.h b/webrtc/modules/audio_device/android/audio_manager.h index 5d2c0563f..3ab29b7c8 100644 --- a/webrtc/modules/audio_device/android/audio_manager.h +++ b/webrtc/modules/audio_device/android/audio_manager.h @@ -81,14 +81,15 @@ class AudioManager { AudioManager(); ~AudioManager(); - // Initializes the audio manager (changes mode to MODE_IN_COMMUNICATION, - // request audio focus etc.). - // It is possible to use this class without calling Init() if the calling - // application prefers to set up the audio environment on its own instead. + // Initializes the audio manager and stores the current audio mode. bool Init(); // Revert any setting done by Init(). bool Close(); + // Sets audio mode to AudioManager.MODE_IN_COMMUNICATION if |enable| is true. + // Restores audio mode that was stored in Init() if |enable| is false. + void SetCommunicationMode(bool enable); + // Native audio parameters stored during construction. AudioParameters GetPlayoutAudioParameters() const; AudioParameters GetRecordAudioParameters() const; diff --git a/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioManager.java b/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioManager.java index 4472a61ec..3a0d3a142 100644 --- a/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioManager.java +++ b/webrtc/modules/audio_device/android/java/src/org/webrtc/voiceengine/WebRtcAudioManager.java @@ -37,6 +37,14 @@ class WebRtcAudioManager { // TODO(henrika): add stereo support for playout. private static final int CHANNELS = 1; + // List of possible audio modes. + private static final String[] AUDIO_MODES = new String[] { + "MODE_NORMAL", + "MODE_RINGTONE", + "MODE_IN_CALL", + "MODE_IN_COMMUNICATION", + }; + private final long nativeAudioManager; private final Context context; private final AudioManager audioManager; @@ -68,13 +76,11 @@ class WebRtcAudioManager { return true; } - // Store current audio state so we can restore it when close() is called. + // Store current audio state so we can restore it when close() or + // setCommunicationMode(false) is called. savedAudioMode = audioManager.getMode(); savedIsSpeakerPhoneOn = audioManager.isSpeakerphoneOn(); - // Switch to COMMUNICATION mode for best possible VoIP performance. - audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); - if (DEBUG) { Logd("savedAudioMode: " + savedAudioMode); Logd("savedIsSpeakerPhoneOn: " + savedIsSpeakerPhoneOn); @@ -95,6 +101,24 @@ class WebRtcAudioManager { audioManager.setMode(savedAudioMode); } + private void setCommunicationMode(boolean enable) { + Logd("setCommunicationMode(" + enable + ")" + + WebRtcAudioUtils.getThreadInfo()); + assertTrue(initialized); + if (enable) { + // Avoid switching mode if MODE_IN_COMMUNICATION is already in use. + if (audioManager.getMode() == AudioManager.MODE_IN_COMMUNICATION) { + return; + } + // Switch to COMMUNICATION mode for best possible VoIP performance. + audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); + } else { + // Restore audio mode that was stored in init(). + audioManager.setMode(savedAudioMode); + } + Logd("changing audio mode to: " + AUDIO_MODES[audioManager.getMode()]); + } + private void storeAudioParameters() { // Only mono is supported currently (in both directions). // TODO(henrika): add support for stereo playout.