Fix video upside down

For some Android SDK versions (maybe Android M+) the camera preview rotation
will show upside down constract to the older ones. So we need to adjust
the preview rotation on dynamic orientation change as well as camera
face switch.

Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
camera2
Leo Ma 9 years ago
parent 74bfa1049d
commit d55ad9602b

@ -238,11 +238,6 @@ public class MainActivity extends AppCompatActivity implements RtmpHandler.RtmpL
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
mPublisher.setPreviewRotation(90);
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
mPublisher.setPreviewRotation(0);
}
mPublisher.stopEncode();
mPublisher.stopRecord();
btnRecord.setText("record");

@ -1,12 +1,14 @@
package net.ossrs.yasea;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import android.os.Build;
import android.util.AttributeSet;
import com.seu.magicfilter.base.gpuimage.GPUImageFilter;
@ -45,6 +47,7 @@ public class SrsCameraView extends GLSurfaceView implements GLSurfaceView.Render
private ByteBuffer mGLPreviewBuffer;
private int mCamId = -1;
private int mPreviewRotation = 90;
private int mPreviewOrientation = Configuration.ORIENTATION_PORTRAIT;
private Thread worker;
private final Object writeLock = new Object();
@ -172,7 +175,7 @@ public class SrsCameraView extends GLSurfaceView implements GLSurfaceView.Render
}
private void deleteTextures() {
if(mOESTextureId != OpenGLUtils.NO_TEXTURE){
if (mOESTextureId != OpenGLUtils.NO_TEXTURE) {
queueEvent(new Runnable() {
@Override
public void run() {
@ -183,12 +186,28 @@ public class SrsCameraView extends GLSurfaceView implements GLSurfaceView.Render
}
}
public void setPreviewRotation(int rotation) {
mPreviewRotation = rotation;
}
public void setCameraId(int id) {
mCamId = id;
setPreviewOrientation(mPreviewOrientation);
}
public void setPreviewOrientation(int orientation) {
mPreviewOrientation = orientation;
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(mCamId, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
mPreviewRotation = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? 270 : 90;
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
mPreviewRotation = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? 180 : 0;
}
} else if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
mPreviewRotation = 90;
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
mPreviewRotation = 0;
}
}
}
public int getCameraId() {
@ -293,8 +312,7 @@ public class SrsCameraView extends GLSurfaceView implements GLSurfaceView.Render
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
backCamId = i;
}
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
} else if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
frontCamId = i;
break;
}

@ -31,7 +31,7 @@ public class SrsEncoder {
public static int vLandscapeHeight = 720;
public static int vOutWidth = 720; // Note: the stride of resolution must be set as 16x for hard encoding with some chip like MTK
public static int vOutHeight = 1280; // Since Y component is quadruple size as U and V component, the stride must be set as 32x
public static int vBitrate = 500 * 1000; // 500kbps
public static int vBitrate = 1200 * 1000; // 1200kbps
public static final int VFPS = 24;
public static final int VGOP = 48;
public static final int ASAMPLERATE = 44100;
@ -98,7 +98,7 @@ public class SrsEncoder {
// Since Y component is quadruple size as U and V component, the stride must be set as 32x
if (!useSoftEncoder && vOutWidth % 32 != 0 || vOutHeight % 32 != 0) {
if (vmci.getName().contains("MTK")) {
throw new AssertionError("MTK encoding revolution stride must be 32x");
//throw new AssertionError("MTK encoding revolution stride must be 32x");
}
}

@ -187,13 +187,10 @@ public class SrsPublisher {
}
public void setScreenOrientation(int orientation) {
mCameraView.setPreviewOrientation(orientation);
mEncoder.setScreenOrientation(orientation);
}
public void setPreviewRotation(int rotation) {
mCameraView.setPreviewRotation(rotation);
}
public void setVideoHDMode() {
mEncoder.setVideoHDMode();
}

Loading…
Cancel
Save