summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhenrika@webrtc.org <henrika@webrtc.org>2014-11-06 12:19:19 +0000
committerhenrika@webrtc.org <henrika@webrtc.org>2014-11-06 12:19:19 +0000
commitd0a8777044d2271f163e56cb4eb199eb55246873 (patch)
treec47ff4149c9aec2e51b2e364152b542d57ec9578
parent99766a1e81973265d444f71c492960819c12e6e2 (diff)
downloadtalk-d0a8777044d2271f163e56cb4eb199eb55246873.tar.gz
Volume buttons in AppRTCDemo should affect output audio volume.
BUG=3279 R=glaznev@webrtc.org Review URL: https://webrtc-codereview.appspot.com/32399004 git-svn-id: http://webrtc.googlecode.com/svn/trunk/talk@7651 4adac7df-926f-26a2-2b94-8c16560cd09d
-rw-r--r--examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java108
-rw-r--r--examples/android/src/org/appspot/apprtc/AppRTCDemoActivity.java24
-rwxr-xr-xlibjingle_examples.gyp1
3 files changed, 123 insertions, 10 deletions
diff --git a/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java b/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java
new file mode 100644
index 0000000..b2a1a44
--- /dev/null
+++ b/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java
@@ -0,0 +1,108 @@
+/*
+ * libjingle
+ * Copyright 2013, Google Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.appspot.apprtc;
+
+import android.content.Context;
+import android.media.AudioManager;
+import android.util.Log;
+
+/**
+ * AppRTCAudioManager manages all audio related parts of the AppRTC demo.
+ * TODO(henrika): add support for device enumeration, device selection etc.
+ */
+public class AppRTCAudioManager {
+ private static final String TAG = "AppRTCAudioManager";
+
+ private boolean initialized = false;
+ private AudioManager audioManager;
+ private int savedAudioMode = AudioManager.MODE_INVALID;
+ private boolean savedIsSpeakerPhoneOn = false;
+ private boolean savedIsMicrophoneMute = false;
+
+ /** Construction */
+ static AppRTCAudioManager create(Context context) {
+ return new AppRTCAudioManager(context);
+ }
+
+ private AppRTCAudioManager(Context context) {
+ Log.d(TAG, "AppRTCAudioManager");
+ audioManager = ((AudioManager) context.getSystemService(
+ Context.AUDIO_SERVICE));
+ }
+
+ public void init() {
+ Log.d(TAG, "init");
+ if (initialized) {
+ return;
+ }
+
+ // Store current audio state so we can restore it when close() is called.
+ savedAudioMode = audioManager.getMode();
+ savedIsSpeakerPhoneOn = audioManager.isSpeakerphoneOn();
+ savedIsMicrophoneMute = audioManager.isMicrophoneMute();
+
+ // 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);
+
+ initialized = true;
+ }
+
+ public void close() {
+ Log.d(TAG, "close");
+ if (!initialized) {
+ return;
+ }
+
+ // Restore previously stored audio states.
+ setSpeakerphoneOn(savedIsSpeakerPhoneOn);
+ setMicrophoneMute(savedIsMicrophoneMute);
+ audioManager.setMode(savedAudioMode);
+
+ initialized = false;
+ }
+
+ /** Sets the speaker phone mode. */
+ private void setSpeakerphoneOn(boolean on) {
+ boolean wasOn = audioManager.isSpeakerphoneOn();
+ if (wasOn == on) {
+ return;
+ }
+ audioManager.setSpeakerphoneOn(on);
+ }
+
+ /** Sets the microphone mute state. */
+ private void setMicrophoneMute(boolean on) {
+ boolean wasMuted = audioManager.isMicrophoneMute();
+ if (wasMuted == on) {
+ return;
+ }
+ audioManager.setMicrophoneMute(on);
+ }
+}
diff --git a/examples/android/src/org/appspot/apprtc/AppRTCDemoActivity.java b/examples/android/src/org/appspot/apprtc/AppRTCDemoActivity.java
index ad0e2d5..3ad26af 100644
--- a/examples/android/src/org/appspot/apprtc/AppRTCDemoActivity.java
+++ b/examples/android/src/org/appspot/apprtc/AppRTCDemoActivity.java
@@ -34,7 +34,6 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
-import android.media.AudioManager;
import android.net.Uri;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
@@ -72,6 +71,7 @@ public class AppRTCDemoActivity extends Activity
private PeerConnectionClient pc;
private AppRTCClient appRtcClient = new GAERTCClient(this, this);
private AppRTCSignalingParameters appRtcParameters;
+ private AppRTCAudioManager audioManager = null;
private View rootView;
private View menuBar;
private GLSurfaceView videoView;
@@ -187,15 +187,9 @@ public class AppRTCDemoActivity extends Activity
hudView.setVisibility(View.INVISIBLE);
addContentView(hudView, hudLayout);
- AudioManager audioManager =
- ((AudioManager) getSystemService(AUDIO_SERVICE));
- // TODO(fischman): figure out how to do this Right(tm) and remove the
- // suppression.
- @SuppressWarnings("deprecation")
- boolean isWiredHeadsetOn = audioManager.isWiredHeadsetOn();
- audioManager.setMode(isWiredHeadsetOn ?
- AudioManager.MODE_IN_CALL : AudioManager.MODE_IN_COMMUNICATION);
- audioManager.setSpeakerphoneOn(!isWiredHeadsetOn);
+ // Create and audio manager that will take care of audio routing,
+ // audio modes, audio device enumeration etc.
+ audioManager = AppRTCAudioManager.create(this);
final Intent intent = getIntent();
Uri url = intent.getData();
@@ -253,6 +247,10 @@ public class AppRTCDemoActivity extends Activity
@Override
protected void onDestroy() {
disconnect();
+ if (audioManager != null) {
+ audioManager.close();
+ audioManager = null;
+ }
super.onDestroy();
}
@@ -360,6 +358,12 @@ public class AppRTCDemoActivity extends Activity
// All events are called from UI thread.
@Override
public void onConnectedToRoom(final AppRTCSignalingParameters params) {
+ if (audioManager != null) {
+ // Store existing audio settings and change audio mode to
+ // MODE_IN_COMMUNICATION for best possible VoIP performance.
+ logAndToast("Initializing the audio manager...");
+ audioManager.init();
+ }
appRtcParameters = params;
abortUnless(PeerConnectionFactory.initializeAndroidGlobals(
this, true, true, VideoRendererGui.getEGLContext()),
diff --git a/libjingle_examples.gyp b/libjingle_examples.gyp
index d0f1747..f7ce53b 100755
--- a/libjingle_examples.gyp
+++ b/libjingle_examples.gyp
@@ -344,6 +344,7 @@
'examples/android/res/values/arrays.xml',
'examples/android/res/values/strings.xml',
'examples/android/res/xml/preferences.xml',
+ 'examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java',
'examples/android/src/org/appspot/apprtc/AppRTCClient.java',
'examples/android/src/org/appspot/apprtc/AppRTCDemoActivity.java',
'examples/android/src/org/appspot/apprtc/ConnectActivity.java',