diff --git a/talk/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java b/talk/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java index 90c6610a8..9660cc59a 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 @@ public class AppRTCAudioManager { 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 69ede541c..adc66fa6d 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 + #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 @@ class AudioDeviceTemplate : public AudioDeviceGeneric { } 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 @@ class AudioDeviceTemplate : public AudioDeviceGeneric { } 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 @@ class AudioDeviceTemplate : public AudioDeviceGeneric { } 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 @@ class AudioDeviceTemplate : public AudioDeviceGeneric { 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 @@ class AudioDeviceTemplate : public AudioDeviceGeneric { } 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 @@ class AudioDeviceTemplate : public AudioDeviceGeneric { 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 5caeebb15..25735eb44 100644 --- a/webrtc/modules/audio_device/android/audio_manager.cc +++ b/webrtc/modules/audio_device/android/audio_manager.cc @@ -33,8 +33,8 @@ AudioManager::JavaAudioManager::JavaAudioManager( : 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 @@ void AudioManager::JavaAudioManager::Close() { audio_manager_->CallVoidMethod(dispose_); } -void AudioManager::JavaAudioManager::SetCommunicationMode(bool enable) { - audio_manager_->CallVoidMethod(set_communication_mode_, - static_cast(enable)); +bool AudioManager::JavaAudioManager::IsCommunicationModeEnabled() { + return audio_manager_->CallBooleanMethod(is_communication_mode_enabled_); } // AudioManager implementation @@ -126,11 +125,10 @@ bool AudioManager::Close() { 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 5ec071625..a2f192ca0 100644 --- a/webrtc/modules/audio_device/android/audio_manager.h +++ b/webrtc/modules/audio_device/android/audio_manager.h @@ -97,13 +97,13 @@ class AudioManager { bool Init(); void Close(); - void SetCommunicationMode(bool enable); + bool IsCommunicationModeEnabled(); private: rtc::scoped_ptr audio_manager_; jmethodID init_; jmethodID dispose_; - jmethodID set_communication_mode_; + jmethodID is_communication_mode_enabled_; }; AudioManager(); @@ -118,9 +118,8 @@ class AudioManager { // 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 a1b0bb4d3..95c2ca0e4 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 @@ 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; private boolean hardwareAEC; private boolean lowLatencyOutput; @@ -94,16 +92,7 @@ class WebRtcAudioManager { 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 @@ class WebRtcAudioManager { 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() {