Skip to content

Commit

Permalink
Select proper resolution for 16:9 screen ratio
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Ma <[email protected]>
  • Loading branch information
begeekmyfriend committed Jun 5, 2016
1 parent 9607b4f commit c9c5ee8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/net/ossrs/yasea/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback, Ca

private int mPreviewRotation = 90;
private int mCamId = Camera.getNumberOfCameras() - 1; // default camera
private byte[] mYuvFrameBuffer = new byte[SrsEncoder.VWIDTH * SrsEncoder.VHEIGHT * 3 / 2];
private byte[] mYuvFrameBuffer = new byte[SrsEncoder.VPREV_WIDTH * SrsEncoder.VPREV_HEIGHT * 3 / 2];

private String mNotifyMsg;
private SharedPreferences sp;
Expand Down Expand Up @@ -301,7 +301,7 @@ private void startCamera() {
mCamera = Camera.open(mCamId);
Camera.Parameters params = mCamera.getParameters();
/* preview size */
Size size = mCamera.new Size(SrsEncoder.VWIDTH, SrsEncoder.VHEIGHT);
Size size = mCamera.new Size(SrsEncoder.VPREV_WIDTH, SrsEncoder.VPREV_HEIGHT);
if (!params.getSupportedPreviewSizes().contains(size)) {
Thread.getDefaultUncaughtExceptionHandler().uncaughtException(Thread.currentThread(),
new IllegalArgumentException(String.format("Unsupported preview size %dx%d", size.width, size.height)));
Expand All @@ -317,8 +317,8 @@ private void startCamera() {
//params.set("orientation", "portrait");
//params.set("orientation", "landscape");
//params.setRotation(90);
params.setPictureSize(SrsEncoder.VWIDTH, SrsEncoder.VHEIGHT);
params.setPreviewSize(SrsEncoder.VWIDTH, SrsEncoder.VHEIGHT);
params.setPictureSize(SrsEncoder.VPREV_WIDTH, SrsEncoder.VPREV_HEIGHT);
params.setPreviewSize(SrsEncoder.VPREV_WIDTH, SrsEncoder.VPREV_HEIGHT);
int[] range = findClosestFpsRange(SrsEncoder.VFPS, params.getSupportedPreviewFpsRange());
params.setPreviewFpsRange(range[0], range[1]);
params.setPreviewFormat(SrsEncoder.VFORMAT);
Expand Down
28 changes: 14 additions & 14 deletions app/src/main/java/net/ossrs/yasea/SrsEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public class SrsEncoder {

public static final String VCODEC = "video/avc";
public static final String ACODEC = "audio/mp4a-latm";
public static final int VWIDTH = 640;
public static final int VHEIGHT = 480;
public static final int VCROP_WIDTH = 384;
public static final int VCROP_HEIGHT = 640;
public static final int VPREV_WIDTH = 1280;
public static final int VPREV_HEIGHT = 720;
public static final int VCROP_WIDTH = 32 * 9 * 2;
public static final int VCROP_HEIGHT = 32 * 16 * 2;
public static int vCropWidth = VCROP_WIDTH; // Note: the stride of resolution must be set as 16x for hard encoding with some chip like MTK
public static int vCropHeight = VCROP_HEIGHT; // Since Y component is quadruple size as U and V component, the stride must be set as 32x
public static final int VBITRATE = 500 * 1000; // 500kbps
Expand Down Expand Up @@ -307,12 +307,12 @@ private void portraitPreprocessYuvFrame(byte[] data) {
if (mCameraFaceFront) {
switch (mVideoColorFormat) {
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar:
cropYUV420PlannerFrame(data, VWIDTH, VHEIGHT, mCroppedFrameBuffer, vCropHeight, vCropWidth);
cropYUV420PlannerFrame(data, VPREV_WIDTH, VPREV_HEIGHT, mCroppedFrameBuffer, vCropHeight, vCropWidth);
flipYUV420PlannerFrame(mCroppedFrameBuffer, mFlippedFrameBuffer, vCropHeight, vCropWidth);
rotateYUV420PlannerFrame(mFlippedFrameBuffer, mRotatedFrameBuffer, vCropHeight, vCropWidth);
break;
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar:
cropYUV420SemiPlannerFrame(data, VWIDTH, VHEIGHT, mCroppedFrameBuffer, vCropHeight, vCropWidth);
cropYUV420SemiPlannerFrame(data, VPREV_WIDTH, VPREV_HEIGHT, mCroppedFrameBuffer, vCropHeight, vCropWidth);
flipYUV420SemiPlannerFrame(mCroppedFrameBuffer, mFlippedFrameBuffer, vCropHeight, vCropWidth);
rotateYUV420SemiPlannerFrame(mFlippedFrameBuffer, mRotatedFrameBuffer, vCropHeight, vCropWidth);
break;
Expand All @@ -322,11 +322,11 @@ private void portraitPreprocessYuvFrame(byte[] data) {
} else {
switch (mVideoColorFormat) {
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar:
cropYUV420PlannerFrame(data, VWIDTH, VHEIGHT, mCroppedFrameBuffer, vCropHeight, vCropWidth);
cropYUV420PlannerFrame(data, VPREV_WIDTH, VPREV_HEIGHT, mCroppedFrameBuffer, vCropHeight, vCropWidth);
rotateYUV420PlannerFrame(mCroppedFrameBuffer, mRotatedFrameBuffer, vCropHeight, vCropWidth);
break;
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar:
cropYUV420SemiPlannerFrame(data, VWIDTH, VHEIGHT, mCroppedFrameBuffer, vCropHeight, vCropWidth);
cropYUV420SemiPlannerFrame(data, VPREV_WIDTH, VPREV_HEIGHT, mCroppedFrameBuffer, vCropHeight, vCropWidth);
rotateYUV420SemiPlannerFrame(mCroppedFrameBuffer, mRotatedFrameBuffer, vCropHeight, vCropWidth);
break;
default:
Expand All @@ -339,12 +339,12 @@ private void landscapePreprocessYuvFrame(byte[] data) {
if (mCameraFaceFront) {
switch (mVideoColorFormat) {
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar:
cropYUV420PlannerFrame(data, VWIDTH, VHEIGHT, mCroppedFrameBuffer, vCropWidth, vCropHeight);
cropYUV420PlannerFrame(data, VPREV_WIDTH, VPREV_HEIGHT, mCroppedFrameBuffer, vCropWidth, vCropHeight);
flipYUV420PlannerFrame(mCroppedFrameBuffer, mFlippedFrameBuffer, vCropHeight, vCropWidth);
unrotateYUV420PlannerFrame(mFlippedFrameBuffer, mRotatedFrameBuffer, vCropWidth, vCropHeight);
break;
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar:
cropYUV420SemiPlannerFrame(data, VWIDTH, VHEIGHT, mCroppedFrameBuffer, vCropWidth, vCropHeight);
cropYUV420SemiPlannerFrame(data, VPREV_WIDTH, VPREV_HEIGHT, mCroppedFrameBuffer, vCropWidth, vCropHeight);
flipYUV420SemiPlannerFrame(mCroppedFrameBuffer, mFlippedFrameBuffer, vCropWidth, vCropHeight);
unrotateYUV420SemiPlannerFrame(mFlippedFrameBuffer, mRotatedFrameBuffer, vCropWidth, vCropHeight);
break;
Expand All @@ -354,11 +354,11 @@ private void landscapePreprocessYuvFrame(byte[] data) {
} else {
switch (mVideoColorFormat) {
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar:
cropYUV420PlannerFrame(data, VWIDTH, VHEIGHT, mCroppedFrameBuffer, vCropWidth, vCropHeight);
cropYUV420PlannerFrame(data, VPREV_WIDTH, VPREV_HEIGHT, mCroppedFrameBuffer, vCropWidth, vCropHeight);
unrotateYUV420PlannerFrame(mCroppedFrameBuffer, mRotatedFrameBuffer, vCropWidth, vCropHeight);
break;
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar:
cropYUV420SemiPlannerFrame(data, VWIDTH, VHEIGHT, mCroppedFrameBuffer, vCropWidth, vCropHeight);
cropYUV420SemiPlannerFrame(data, VPREV_WIDTH, VPREV_HEIGHT, mCroppedFrameBuffer, vCropWidth, vCropHeight);
unrotateYUV420SemiPlannerFrame(mCroppedFrameBuffer, mRotatedFrameBuffer, vCropWidth, vCropHeight);
break;
default:
Expand All @@ -379,7 +379,7 @@ private void landscapePreprocessYuvFrame(byte[] data) {
// YUY2 -> YUV422SP yuyv yuyv
private byte[] cropYUV420SemiPlannerFrame(byte[] input, int iw, int ih, byte[] output, int ow, int oh) {
if (iw < ow || ih < oh) {
throw new AssertionError("Crop revolution size must be less than original one");
throw new AssertionError("Crop resolution size must be less than original one");
}
if (ow % 32 != 0 || oh % 32 != 0) {
// Note: the stride of resolution must be set as 16x for hard encoding with some chip like MTK
Expand Down Expand Up @@ -413,7 +413,7 @@ private byte[] cropYUV420SemiPlannerFrame(byte[] input, int iw, int ih, byte[] o

private byte[] cropYUV420PlannerFrame(byte[] input, int iw, int ih, byte[] output, int ow, int oh) {
if (iw < ow || ih < oh) {
throw new AssertionError("Crop revolution size must be less than original one");
throw new AssertionError("Crop resolution size must be less than original one");
}
if (ow % 32 != 0 || oh % 32 != 0) {
// Note: the stride of resolution must be set as 16x for hard encoding with some chip like MTK
Expand Down

0 comments on commit c9c5ee8

Please sign in to comment.