离屏时短视频的录制优化

hdrplus
Matthew 1 year ago
parent 555e220672
commit fe377d5ff4

@ -70,6 +70,16 @@ public final class AspectRatioFrameLayout extends FrameLayout {
private float videoAspectRatio;
private int resizeMode;
public static interface OnSizeChangeListener {
void onSizeChanged(int width, int height);
}
private OnSizeChangeListener mOnSizeChangedListener = null;
public void setOnSizeChangedListener(OnSizeChangeListener sizeChangedListener) {
mOnSizeChangedListener = sizeChangedListener;
}
public AspectRatioFrameLayout(Context context) {
this(context, null);
}
@ -144,6 +154,17 @@ public final class AspectRatioFrameLayout extends FrameLayout {
}
break;
}
if (mOnSizeChangedListener != null) {
final int fw = width;
final int fh = height;
this.postDelayed(new Runnable() {
@Override
public void run() {
mOnSizeChangedListener.onSizeChanged(fw, fh);
}
}, 0);
}
super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
}

@ -8,7 +8,8 @@ import android.view.View;
public class AutoFitGLView extends GLSurfaceView implements View.OnTouchListener {
private float mAspectRatio;
private int mRatioWidth = 0;
private int mRatioHeight = 0;
public AutoFitGLView(Context context) {
this(context, null);
@ -21,12 +22,24 @@ public class AutoFitGLView extends GLSurfaceView implements View.OnTouchListener
private TouchListener touchListener;
public void setAspectRatio(int width, int height){
mAspectRatio = (float)width / height;
getHolder().setFixedSize(width, height);
requestLayout();
/**
* Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
* calculated from the parameters. Note that the actual sizes of parameters don't matter, that
* is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
*
* @param width Relative horizontal size
* @param height Relative vertical size
*/
public void setAspectRatio(int width, int height) {
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Size cannot be negative.");
}
mRatioWidth = width;
mRatioHeight = height;
post(() -> requestLayout());
}
@Override
public boolean onTouch(View v, MotionEvent event) {
final int actionMasked = event.getActionMasked();
@ -53,26 +66,14 @@ public class AutoFitGLView extends GLSurfaceView implements View.OnTouchListener
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (mAspectRatio == 0) {
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
}else {
int newW,newH;
float actualRatio;
if (width > height) {
actualRatio = mAspectRatio;
}else {
actualRatio = 1 / mAspectRatio;
} else {
if (width < height * mRatioWidth / mRatioHeight) {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
if (width < height * actualRatio){
newH = height;
newW = (int) (height * actualRatio);
}else {
newW = width;
newH = (int) (width / actualRatio);
}
setMeasuredDimension(newW, newH);
}
}
}

@ -22,12 +22,15 @@ import android.os.Message;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.view.WindowMetrics;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -241,6 +244,14 @@ public class Camera2VideoActivity extends AppCompatActivity {
mCameraHeight = mVideoHeight;
AspectRatioFrameLayout frameLayout = (AspectRatioFrameLayout)findViewById(R.id.wrap_view);
frameLayout.setOnSizeChangedListener(new AspectRatioFrameLayout.OnSizeChangeListener() {
@Override
public void onSizeChanged(int width, int height) {
Log.i("OSD", "width=" + Integer.toString(width));
}
});
frameLayout.setAspectRatio((float)mVideoWidth / (float)mVideoHeight);
mTimeMask = 0;
@ -298,6 +309,7 @@ public class Camera2VideoActivity extends AppCompatActivity {
@Override
protected void onResume() {
super.onResume();
Log.i("OSD", "onResume");
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
@ -318,7 +330,16 @@ public class Camera2VideoActivity extends AppCompatActivity {
@Override
protected void onStop() {
super.onStop();
Log.i("OSD", "onStop");
// releaseCamera();
}
@Override
protected void onDestroy() {
Log.i("OSD", "onDestroy");
releaseCamera();
super.onDestroy();
}
private void initOSD(Bitmap bm, long ts) {
@ -602,7 +623,7 @@ public class Camera2VideoActivity extends AppCompatActivity {
runOnUiThread(() -> {
AspectRatioFrameLayout frameLayout = (AspectRatioFrameLayout)findViewById(R.id.wrap_view);
final AspectRatioFrameLayout frameLayout = (AspectRatioFrameLayout)findViewById(R.id.wrap_view);
frameLayout.removeAllViews();
mPreviewView = null;
@ -612,40 +633,79 @@ public class Camera2VideoActivity extends AppCompatActivity {
mGPUCameraRecorder.changeManualFocusPoint(event.getX(), event.getY(), width, height);
});
mPreviewView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(@NonNull SurfaceHolder holder) {
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
if (width > 0 && height > 0) {
}
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
}
});
frameLayout.addView(mPreviewView);
if (!TextUtils.isEmpty(mOSDLeftTop) || !TextUtils.isEmpty(mOSDLeftTop) || !TextUtils.isEmpty(mOSDLeftTop) || !TextUtils.isEmpty(mOSDLeftTop)) {
mPreviewWidth = frameLayout.getMeasuredWidth();
mPreviewHeight = frameLayout.getMeasuredHeight();
frameLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.WHITE);
int fontSize = DEFAULT_FONT_SIZE * mPreviewHeight / 1024;
float strokeWidth = DEFAULT_STROKE_WIDTH * mPreviewHeight / 1024;
mPaint.setTextSize(fontSize);
mPaintStroker = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintStroker.setStyle(Paint.Style.STROKE);
mPaintStroker.setColor(Color.BLACK);
mPaintStroker.setTextSize(fontSize);
mPaintStroker.setStrokeWidth(strokeWidth);
Bitmap bm = Bitmap.createBitmap(mPreviewWidth, mPreviewHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
int width = right - left;
int height = bottom - top;
long ts = System.currentTimeMillis();
long zeroTs = ts - (ts % 1000);
initOSD(bm, zeroTs);
mOSDFilter = new GlWatermarkFilter(bm);
if (width <= 0 || height <= 0) {
return;
}
frameLayout.removeOnLayoutChangeListener(this);
if (!TextUtils.isEmpty(mOSDLeftTop) || !TextUtils.isEmpty(mOSDLeftTop) || !TextUtils.isEmpty(mOSDLeftTop) || !TextUtils.isEmpty(mOSDLeftTop)) {
mPreviewWidth = frameLayout.getMeasuredWidth();
mPreviewHeight = frameLayout.getMeasuredHeight();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.WHITE);
int fontSize = DEFAULT_FONT_SIZE * mPreviewHeight / 1024;
float strokeWidth = DEFAULT_STROKE_WIDTH * mPreviewHeight / 1024;
mPaint.setTextSize(fontSize);
mPaintStroker = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintStroker.setStyle(Paint.Style.STROKE);
mPaintStroker.setColor(Color.BLACK);
mPaintStroker.setTextSize(fontSize);
mPaintStroker.setStrokeWidth(strokeWidth);
Bitmap bm = Bitmap.createBitmap(mPreviewWidth, mPreviewHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
long ts = System.currentTimeMillis();
long zeroTs = ts - (ts % 1000);
initOSD(bm, zeroTs);
mOSDFilter = new GlWatermarkFilter(bm);
if (mGPUCameraRecorder != null) {
mGPUCameraRecorder.setFilter(mOSDFilter);
}
mOsdThread.start();
}
if (mGPUCameraRecorder != null) {
mGPUCameraRecorder.setFilter(mOSDFilter);
}
});
mOsdThread.start();
}
});
}

@ -1,9 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
tools:context="com.xypower.mpapp.v2.Camera2VideoActivity"
>
<com.xypower.mpapp.v2.AspectRatioFrameLayout

@ -110,8 +110,11 @@ public class MediaMuxerCaptureWrapper {
Log.v(TAG, "stop:startedCount=" + startedCount);
startedCount--;
if ((encoderCount > 0) && (startedCount <= 0)) {
mediaMuxer.stop();
mediaMuxer.release();
try {
mediaMuxer.stop();
mediaMuxer.release();
} catch (Exception ex) {
}
isStarted = false;
Log.v(TAG, "MediaMuxer stopped:");
}

Loading…
Cancel
Save