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
This commit is contained in:
henrike@webrtc.org
2013-06-14 05:37:13 +00:00
parent d4ed1a3e2c
commit f27389ca9f
2 changed files with 39 additions and 39 deletions

View File

@ -6,7 +6,6 @@
android:debuggable="true">
<activity android:name=".WebRTCDemo"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation"
android:screenOrientation="landscape"
>
<intent-filter>

View File

@ -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();
}
}
};
@ -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: