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}
diff --git a/talk/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java b/talk/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java
index 916de65..90c6610 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 @@
     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 fa37eb2..d8f3ada 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 @@
   }
 
   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 @@
   }
 
   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 3a0d3a1..f116900 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 @@
   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 @@
       return;
     }
     // Restore previously stored audio states.
-    setSpeakerphoneOn(savedIsSpeakerPhoneOn);
+    if (audioModeNeedsRestore) {
+      setSpeakerphoneOn(savedIsSpeakerPhoneOn);
+    }
     audioManager.setMode(savedAudioMode);
   }
 
@@ -112,11 +115,14 @@
       }
       // 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() {