From f27389ca9fc43c47ad631b925fefcf2fd14523b3 Mon Sep 17 00:00:00 2001 From: "henrike@webrtc.org" Date: Fri, 14 Jun 2013 05:37:13 +0000 Subject: [PATCH] WebRTCDemo: ensures that using front and back camera work as expected. I.e. egress: Real world up is stream up. Ingress: stream up is app up. Local (preview): Real world up is app up. BUG=1763 R=fischman@webrtc.org Review URL: https://webrtc-codereview.appspot.com/1642004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@4227 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../test/android/AndroidManifest.xml | 1 - .../org/webrtc/videoengineapp/WebRTCDemo.java | 77 ++++++++++--------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/webrtc/video_engine/test/android/AndroidManifest.xml b/webrtc/video_engine/test/android/AndroidManifest.xml index 2e99fdaf1a..39a3c515e4 100644 --- a/webrtc/video_engine/test/android/AndroidManifest.xml +++ b/webrtc/video_engine/test/android/AndroidManifest.xml @@ -6,7 +6,6 @@ android:debuggable="true"> diff --git a/webrtc/video_engine/test/android/src/org/webrtc/videoengineapp/WebRTCDemo.java b/webrtc/video_engine/test/android/src/org/webrtc/videoengineapp/WebRTCDemo.java index ffe4e60c0a..921311fee8 100644 --- a/webrtc/video_engine/test/android/src/org/webrtc/videoengineapp/WebRTCDemo.java +++ b/webrtc/video_engine/test/android/src/org/webrtc/videoengineapp/WebRTCDemo.java @@ -22,6 +22,7 @@ import android.content.res.Configuration; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PixelFormat; +import android.hardware.Camera; import android.hardware.SensorManager; import android.media.AudioManager; import android.media.MediaPlayer; @@ -195,38 +196,43 @@ public class WebRTCDemo extends TabActivity implements IViEAndroidCallback, private String[] mVoiceCodecsStrings = null; private OrientationEventListener orientationListener; - int currentOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; - int currentCameraOrientation = 0; + int currentDeviceOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; private StatsView statsView = null; private BroadcastReceiver receiver; - public int getCameraOrientation(int cameraOrientation) { - Display display = this.getWindowManager().getDefaultDisplay(); - int displatyRotation = display.getRotation(); - int degrees = 0; - switch (displatyRotation) { - case Surface.ROTATION_0: degrees = 0; break; - case Surface.ROTATION_90: degrees = 90; break; - case Surface.ROTATION_180: degrees = 180; break; - case Surface.ROTATION_270: degrees = 270; break; - } - int result = 0; - if (cameraOrientation > 180) { - result = (cameraOrientation + degrees) % 360; - } else { - result = (cameraOrientation - degrees + 360) % 360; - } - return result; + // Rounds rotation to the nearest 90 degree rotation. + private static int roundRotation(int rotation) { + return (int)(Math.round((double)rotation / 90) * 90) % 360; } - public void onConfigurationChanged(Configuration newConfig) { - super.onConfigurationChanged(newConfig); - int newRotation = getCameraOrientation(currentCameraOrientation); - if (viERunning) { - vieAndroidAPI.SetRotation(cameraId, newRotation); + // This function ensures that egress streams always send real world up + // streams. + // Note: There are two components of the camera rotation. The rotation of + // the capturer relative to the device. I.e. up for the camera might not be + // device up. When rotating the device the camera is also rotated. + // The former is called orientation and the second is called rotation here. + public void compensateCameraRotation() { + Camera.CameraInfo info = new Camera.CameraInfo(); + Camera.getCameraInfo(usingFrontCamera ? 1 : 0, info); + int cameraOrientation = info.orientation; + // The device orientation is the device's rotation relative to its + // natural position. + int cameraRotation = roundRotation(currentDeviceOrientation); + + int totalCameraRotation = 0; + if (usingFrontCamera) { + // The front camera rotates in the opposite direction of the + // device. + int inverseCameraRotation = (360 - cameraRotation) % 360; + totalCameraRotation = + (inverseCameraRotation + cameraOrientation) % 360; + } else { + totalCameraRotation = + (cameraRotation + cameraOrientation) % 360; } + vieAndroidAPI.SetRotation(cameraId, totalCameraRotation); } // Called when the activity is first created. @@ -300,7 +306,8 @@ public class WebRTCDemo extends TabActivity implements IViEAndroidCallback, new OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) { public void onOrientationChanged (int orientation) { if (orientation != ORIENTATION_UNKNOWN) { - currentOrientation = orientation; + currentDeviceOrientation = orientation; + compensateCameraRotation(); } } }; @@ -355,7 +362,7 @@ public class WebRTCDemo extends TabActivity implements IViEAndroidCallback, loadText = "< " + frameRateO + " fps/ " + bitRateO/1024 + " kbps"; canvas.drawText(loadText, 4, 262, loadPaint); - loadText = "Incoming resolution " + widthI + "x" + heightI; + loadText = "Incoming resolution " + widthI + "x" + heightI; canvas.drawText(loadText, 4, 282, loadPaint); } updateDisplay(); @@ -663,16 +670,13 @@ public class WebRTCDemo extends TabActivity implements IViEAndroidCallback, } if (enableVideoSend) { - currentCameraOrientation = - vieAndroidAPI.GetCameraOrientation(usingFrontCamera ? 1 : 0); ret = vieAndroidAPI.SetSendCodec(channel, codecType, INIT_BITRATE, codecSizeWidth, codecSizeHeight, SEND_CODEC_FRAMERATE); int camId = vieAndroidAPI.StartCamera(channel, usingFrontCamera ? 1 : 0); - if (camId > 0) { + if (camId >= 0) { cameraId = camId; - int neededRotation = getCameraOrientation(currentCameraOrientation); - vieAndroidAPI.SetRotation(cameraId, neededRotation); + compensateCameraRotation(); } else { ret = camId; } @@ -841,15 +845,12 @@ public class WebRTCDemo extends TabActivity implements IViEAndroidCallback, usingFrontCamera = !usingFrontCamera; if (viERunning) { - currentCameraOrientation = - vieAndroidAPI.GetCameraOrientation(usingFrontCamera ? 1 : 0); vieAndroidAPI.StopCamera(cameraId); mLlLocalSurface.removeView(svLocal); vieAndroidAPI.StartCamera(channel, usingFrontCamera ? 1 : 0); mLlLocalSurface.addView(svLocal); - int neededRotation = getCameraOrientation(currentCameraOrientation); - vieAndroidAPI.SetRotation(cameraId, neededRotation); + compensateCameraRotation(); } break; case R.id.btStartStopCall: @@ -1029,9 +1030,9 @@ public class WebRTCDemo extends TabActivity implements IViEAndroidCallback, } public int newIncomingResolution(int width, int height) { - widthI = width; - heightI = height; - return 0; + widthI = width; + heightI = height; + return 0; } private void addStatusView() {