Removes automatic setting of COMM mode in WebRTC.
It is now up to the application to ensure that it is in COMM mode before any audio streaming is started.

BUG=b/21571563
R=glaznev@webrtc.org

Review URL: https://codereview.webrtc.org/1165923002

Cr-Commit-Position: refs/heads/master@{#9383}
diff --git a/talk/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java b/talk/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java
index 90c6610..9660cc5 100644
--- a/talk/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java
+++ b/talk/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java
@@ -151,10 +151,11 @@
     audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL,
         AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
 
-    // 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);
+    // Start by setting MODE_IN_COMMUNICATION as default audio mode. It is
+    // required to be in this mode when playout and/or recording starts for
+    // best possible VoIP performance.
+    // TODO(henrika): we migh want to start with RINGTONE mode here instead.
+    audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
 
     // 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 69ede54..adc66fa 100644
--- a/webrtc/modules/audio_device/android/audio_device_template.h
+++ b/webrtc/modules/audio_device/android/audio_device_template.h
@@ -11,12 +11,17 @@
 #ifndef WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_TEMPLATE_H_
 #define WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_AUDIO_DEVICE_TEMPLATE_H_
 
+#include <android/log.h>
+
 #include "webrtc/base/checks.h"
 #include "webrtc/base/thread_checker.h"
 #include "webrtc/modules/audio_device/android/audio_manager.h"
 #include "webrtc/modules/audio_device/audio_device_generic.h"
 #include "webrtc/system_wrappers/interface/trace.h"
 
+#define TAG "AudioDeviceTemplate"
+#define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__)
+
 namespace webrtc {
 
 // InputType/OutputType can be any class that implements the capturing/rendering
@@ -125,12 +130,6 @@
   }
 
   int32_t InitPlayout() override {
-    // Switches the Android audio mode to MODE_IN_COMMUNICATION to ensure that
-    // audio routing, volume control and echo performance are the best possible
-    // for VoIP. InitRecording() does the same type of call but only the first
-    // call has any effect.
-    // This call does nothing if MODE_IN_COMMUNICATION was already set.
-    audio_manager_->SetCommunicationMode(true);
     return output_.InitPlayout();
   }
 
@@ -144,12 +143,6 @@
   }
 
   int32_t InitRecording() override {
-    // Switches the Android audio mode to MODE_IN_COMMUNICATION to ensure that
-    // audio routing, volume control and echo performance are the best possible
-    // for VoIP. InitRecording() does the same type of call but only the first
-    // call has any effect.
-    // This call does nothing if MODE_IN_COMMUNICATION was already set.
-    audio_manager_->SetCommunicationMode(true);
     return input_.InitRecording();
   }
 
@@ -158,6 +151,9 @@
   }
 
   int32_t StartPlayout() override {
+    if (!audio_manager_->IsCommunicationModeEnabled()) {
+      ALOGW("The application should use MODE_IN_COMMUNICATION audio mode!");
+    }
     return output_.StartPlayout();
   }
 
@@ -166,11 +162,6 @@
     if (!Playing())
       return 0;
     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;
   }
 
@@ -179,6 +170,9 @@
   }
 
   int32_t StartRecording() override {
+    if (!audio_manager_->IsCommunicationModeEnabled()) {
+      ALOGW("The application should use MODE_IN_COMMUNICATION audio mode!");
+    }
     return input_.StartRecording();
   }
 
@@ -187,11 +181,6 @@
     if (!Recording())
       return 0;
     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;
   }
 
diff --git a/webrtc/modules/audio_device/android/audio_manager.cc b/webrtc/modules/audio_device/android/audio_manager.cc
index 5caeebb..25735eb 100644
--- a/webrtc/modules/audio_device/android/audio_manager.cc
+++ b/webrtc/modules/audio_device/android/audio_manager.cc
@@ -33,8 +33,8 @@
     : audio_manager_(audio_manager.Pass()),
       init_(native_reg->GetMethodId("init", "()Z")),
       dispose_(native_reg->GetMethodId("dispose", "()V")),
-      set_communication_mode_(
-          native_reg->GetMethodId("setCommunicationMode", "(Z)V")) {
+      is_communication_mode_enabled_(
+          native_reg->GetMethodId("isCommunicationModeEnabled", "()Z")) {
   ALOGD("JavaAudioManager::ctor%s", GetThreadInfo().c_str());
 }
 
@@ -50,9 +50,8 @@
   audio_manager_->CallVoidMethod(dispose_);
 }
 
-void AudioManager::JavaAudioManager::SetCommunicationMode(bool enable) {
-  audio_manager_->CallVoidMethod(set_communication_mode_,
-                                 static_cast<jboolean>(enable));
+bool AudioManager::JavaAudioManager::IsCommunicationModeEnabled() {
+  return audio_manager_->CallBooleanMethod(is_communication_mode_enabled_);
 }
 
 // AudioManager implementation
@@ -126,11 +125,10 @@
   return true;
 }
 
-void AudioManager::SetCommunicationMode(bool enable) {
-  ALOGD("SetCommunicationMode(%d)%s", enable, GetThreadInfo().c_str());
+bool AudioManager::IsCommunicationModeEnabled() const {
+  ALOGD("IsCommunicationModeEnabled()");
   DCHECK(thread_checker_.CalledOnValidThread());
-  DCHECK(initialized_);
-  j_audio_manager_->SetCommunicationMode(enable);
+  return j_audio_manager_->IsCommunicationModeEnabled();
 }
 
 bool AudioManager::IsAcousticEchoCancelerSupported() const {
diff --git a/webrtc/modules/audio_device/android/audio_manager.h b/webrtc/modules/audio_device/android/audio_manager.h
index 5ec0716..a2f192c 100644
--- a/webrtc/modules/audio_device/android/audio_manager.h
+++ b/webrtc/modules/audio_device/android/audio_manager.h
@@ -97,13 +97,13 @@
 
     bool Init();
     void Close();
-    void SetCommunicationMode(bool enable);
+    bool IsCommunicationModeEnabled();
 
    private:
     rtc::scoped_ptr<GlobalRef> audio_manager_;
     jmethodID init_;
     jmethodID dispose_;
-    jmethodID set_communication_mode_;
+    jmethodID is_communication_mode_enabled_;
   };
 
   AudioManager();
@@ -118,9 +118,8 @@
   // 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);
+  // Returns true if current audio mode is AudioManager.MODE_IN_COMMUNICATION.
+  bool IsCommunicationModeEnabled() const;
 
   // Native audio parameters stored during construction.
   const AudioParameters& GetPlayoutAudioParameters();
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 a1b0bb4..95c2ca0 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
@@ -62,10 +62,8 @@
   private final AudioManager audioManager;
 
   private boolean initialized = false;
-  private boolean audioModeNeedsRestore = false;
   private int nativeSampleRate;
   private int nativeChannels;
-  private int savedAudioMode = AudioManager.MODE_INVALID;
 
   private boolean hardwareAEC;
   private boolean lowLatencyOutput;
@@ -94,16 +92,7 @@
     if (initialized) {
       return true;
     }
-
-    // Store current audio state so we can restore it when close() or
-    // setCommunicationMode(false) is called.
-    savedAudioMode = audioManager.getMode();
-
-    if (DEBUG) {
-      Logd("savedAudioMode: " + savedAudioMode);
-      Logd("hasEarpiece: " + hasEarpiece());
-    }
-
+    Logd("audio mode is: " + AUDIO_MODES[audioManager.getMode()]);
     initialized = true;
     return true;
   }
@@ -113,31 +102,10 @@
     if (!initialized) {
       return;
     }
-    // Restore previously stored audio states.
-    if (audioModeNeedsRestore) {
-      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);
-      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()]);
-    }
+  private boolean isCommunicationModeEnabled() {
+    return (audioManager.getMode() == AudioManager.MODE_IN_COMMUNICATION);
   }
 
   private void storeAudioParameters() {