So you've got the Red5 Pro Server up and running on your Android device and your application is ready to stream. Ready, set, publish! Uh oh, the camera is upside down! Not quite the dramatic effect you were looking for. What's up with that? Some manufacturers are now physically installing the camera upside down to save space and make wiring easier. Rather than walking on the ceiling, this fix will have you right-side up in no time.

By modifying the existing R5Camera constructor we can tell the device to automatically rotate the view to the proper orientation.

First we create a new instance of camera and import the existing android camera:

R5Camera camera = newR5Camera(openFrontFacingCameraGingerbread());

Then we create a camera orientation class by modifying cameraInfo:

openFrontFacingCameraGingerbread() {
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
        Camera.getCameraInfo(camIdx, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            try {
                cam = Camera.open(camIdx);
                camOrientation = cameraInfo.orientation;
                applyDeviceRotation();
                break;
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
    }

    return cam;
}

Now to make sure the device knows how to rotate the image we add rotation settings:

protected void applyDeviceRotation(){
    int rotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
        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;
    }

    camOrientation += degrees;

    camOrientation = camOrientation%360;
}  

So, how is a camera flip allowed to happen in the first place? Isn't Google supposed to specify how manufacturers should make their devices to make them compatible with the Android operating system? Yes! Google's solution to devices with upside down cameras is the new Android Camera2 API. In addition to some new features and extra level of control, the Camera2 API can deal with this type of installation behind the scenes.

So why didn't we fix it? In order to allow for compatibility with older operating systems we have not implemented a fix for Camera2 yet, but no worries, that will be coming out soon with the next release. It just means we need to implement some branching logic in our SDK. In the meantime, the above code should work for you.

Let us know if this works for you, and as always happy coding!

  • Share: