From a125d7d7adc5fe6147b07be06f2380a772b30bdd Mon Sep 17 00:00:00 2001 From: henrika Date: Fri, 10 Apr 2015 15:19:24 +0200 Subject: [PATCH] Changes default audio mode in AppRTCDemo to MODE_RINGTONE. Also prevents that we try to restore audio mode when it has not been changed. TBR=glaznev BUG=NONE TEST=AppRTCDemo and verify that volume control switches from "Ringtone to Phone" mode when call starts and switches back to Ringtone mode when call ends. Review URL: https://webrtc-codereview.appspot.com/46879004 Cr-Commit-Position: refs/heads/master@{#8975} --- .../src/org/appspot/apprtc/AppRTCAudioManager.java | 8 ++++---- .../audio_device/android/audio_device_template.h | 6 ++++++ .../org/webrtc/voiceengine/WebRtcAudioManager.java | 12 +++++++++--- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/talk/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java b/talk/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java index 916de657b..90c6610a8 100644 --- a/talk/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java +++ b/talk/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java @@ -151,10 +151,10 @@ public class AppRTCAudioManager { audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); - // The AppRTC demo shall always run in COMMUNICATION mode since it will - // result in best possible "VoIP settings", like audio routing, volume - // control etc. - audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); + // Start by setting RINGTONE as default audio mode. The native WebRTC + // audio layer will switch to COMMUNICATION mode when the first streaming + // session starts and return to RINGTONE mode when all streaming stops. + audioManager.setMode(AudioManager.MODE_RINGTONE); // Always disable microphone mute during a WebRTC call. setMicrophoneMute(false); diff --git a/webrtc/modules/audio_device/android/audio_device_template.h b/webrtc/modules/audio_device/android/audio_device_template.h index fa37eb283..d8f3ada3b 100644 --- a/webrtc/modules/audio_device/android/audio_device_template.h +++ b/webrtc/modules/audio_device/android/audio_device_template.h @@ -145,6 +145,9 @@ class AudioDeviceTemplate : public AudioDeviceGeneric { } int32_t StopPlayout() override { + // Avoid using audio manger (JNI/Java cost) if playout was inactive. + if (!Playing()) + return 0; int32_t err = output_.StopPlayout(); if (!Recording()) { // Restore initial audio mode since all audio streaming is disabled. @@ -163,6 +166,9 @@ class AudioDeviceTemplate : public AudioDeviceGeneric { } int32_t StopRecording() override { + // Avoid using audio manger (JNI/Java cost) if recording was inactive. + if (!Recording()) + return 0; int32_t err = input_.StopRecording(); if (!Playing()) { // Restore initial audio mode since all audio streaming is disabled. 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 3a0d3a142..f116900da 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 @@ -50,6 +50,7 @@ class WebRtcAudioManager { private final AudioManager audioManager; private boolean initialized = false; + private boolean audioModeNeedsRestore = false; private int nativeSampleRate; private int nativeChannels; private int savedAudioMode = AudioManager.MODE_INVALID; @@ -97,7 +98,9 @@ class WebRtcAudioManager { return; } // Restore previously stored audio states. - setSpeakerphoneOn(savedIsSpeakerPhoneOn); + if (audioModeNeedsRestore) { + setSpeakerphoneOn(savedIsSpeakerPhoneOn); + } audioManager.setMode(savedAudioMode); } @@ -112,11 +115,14 @@ class WebRtcAudioManager { } // Switch to COMMUNICATION mode for best possible VoIP performance. audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); - } else { + audioModeNeedsRestore = true; + Logd("changing audio mode to: " + AUDIO_MODES[audioManager.getMode()]); + } else if (audioModeNeedsRestore) { // Restore audio mode that was stored in init(). audioManager.setMode(savedAudioMode); + audioModeNeedsRestore = false; + Logd("restoring audio mode to: " + AUDIO_MODES[audioManager.getMode()]); } - Logd("changing audio mode to: " + AUDIO_MODES[audioManager.getMode()]); } private void storeAudioParameters() {