离屏时短视频的录制优化

hdrplus
Matthew 1 year ago
parent 555e220672
commit fe377d5ff4

@ -70,6 +70,16 @@ public final class AspectRatioFrameLayout extends FrameLayout {
private float videoAspectRatio; private float videoAspectRatio;
private int resizeMode; 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) { public AspectRatioFrameLayout(Context context) {
this(context, null); this(context, null);
} }
@ -144,6 +154,17 @@ public final class AspectRatioFrameLayout extends FrameLayout {
} }
break; 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), super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
} }

@ -8,7 +8,8 @@ import android.view.View;
public class AutoFitGLView extends GLSurfaceView implements View.OnTouchListener { public class AutoFitGLView extends GLSurfaceView implements View.OnTouchListener {
private float mAspectRatio; private int mRatioWidth = 0;
private int mRatioHeight = 0;
public AutoFitGLView(Context context) { public AutoFitGLView(Context context) {
this(context, null); this(context, null);
@ -21,11 +22,23 @@ public class AutoFitGLView extends GLSurfaceView implements View.OnTouchListener
private TouchListener touchListener; private TouchListener touchListener;
public void setAspectRatio(int width, int height){ /**
mAspectRatio = (float)width / height; * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
getHolder().setFixedSize(width, height); * calculated from the parameters. Note that the actual sizes of parameters don't matter, that
requestLayout(); * 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 @Override
public boolean onTouch(View v, MotionEvent event) { public boolean onTouch(View v, MotionEvent event) {
@ -53,26 +66,14 @@ public class AutoFitGLView extends GLSurfaceView implements View.OnTouchListener
super.onMeasure(widthMeasureSpec, heightMeasureSpec); super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec);
if (mAspectRatio == 0) { if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height); setMeasuredDimension(width, height);
}else { } else {
int newW,newH; if (width < height * mRatioWidth / mRatioHeight) {
float actualRatio; setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
if (width > height) { } else {
actualRatio = mAspectRatio; setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}else {
actualRatio = 1 / mAspectRatio;
} }
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.provider.MediaStore;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.Window; import android.view.Window;
import android.view.WindowInsets; import android.view.WindowInsets;
import android.view.WindowManager; import android.view.WindowManager;
import android.view.WindowMetrics; import android.view.WindowMetrics;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -241,6 +244,14 @@ public class Camera2VideoActivity extends AppCompatActivity {
mCameraHeight = mVideoHeight; mCameraHeight = mVideoHeight;
AspectRatioFrameLayout frameLayout = (AspectRatioFrameLayout)findViewById(R.id.wrap_view); 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); frameLayout.setAspectRatio((float)mVideoWidth / (float)mVideoHeight);
mTimeMask = 0; mTimeMask = 0;
@ -298,6 +309,7 @@ public class Camera2VideoActivity extends AppCompatActivity {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
Log.i("OSD", "onResume");
mHandler.postDelayed(new Runnable() { mHandler.postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -318,7 +330,16 @@ public class Camera2VideoActivity extends AppCompatActivity {
@Override @Override
protected void onStop() { protected void onStop() {
super.onStop(); super.onStop();
Log.i("OSD", "onStop");
// releaseCamera();
}
@Override
protected void onDestroy() {
Log.i("OSD", "onDestroy");
releaseCamera(); releaseCamera();
super.onDestroy();
} }
private void initOSD(Bitmap bm, long ts) { private void initOSD(Bitmap bm, long ts) {
@ -602,7 +623,7 @@ public class Camera2VideoActivity extends AppCompatActivity {
runOnUiThread(() -> { runOnUiThread(() -> {
AspectRatioFrameLayout frameLayout = (AspectRatioFrameLayout)findViewById(R.id.wrap_view); final AspectRatioFrameLayout frameLayout = (AspectRatioFrameLayout)findViewById(R.id.wrap_view);
frameLayout.removeAllViews(); frameLayout.removeAllViews();
mPreviewView = null; mPreviewView = null;
@ -612,8 +633,41 @@ public class Camera2VideoActivity extends AppCompatActivity {
mGPUCameraRecorder.changeManualFocusPoint(event.getX(), event.getY(), width, height); 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); frameLayout.addView(mPreviewView);
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) {
int width = right - left;
int height = bottom - top;
if (width <= 0 || height <= 0) {
return;
}
frameLayout.removeOnLayoutChangeListener(this);
if (!TextUtils.isEmpty(mOSDLeftTop) || !TextUtils.isEmpty(mOSDLeftTop) || !TextUtils.isEmpty(mOSDLeftTop) || !TextUtils.isEmpty(mOSDLeftTop)) { if (!TextUtils.isEmpty(mOSDLeftTop) || !TextUtils.isEmpty(mOSDLeftTop) || !TextUtils.isEmpty(mOSDLeftTop) || !TextUtils.isEmpty(mOSDLeftTop)) {
mPreviewWidth = frameLayout.getMeasuredWidth(); mPreviewWidth = frameLayout.getMeasuredWidth();
mPreviewHeight = frameLayout.getMeasuredHeight(); mPreviewHeight = frameLayout.getMeasuredHeight();
@ -646,6 +700,12 @@ public class Camera2VideoActivity extends AppCompatActivity {
mOsdThread.start(); mOsdThread.start();
} }
}
});
}); });
} }

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

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

Loading…
Cancel
Save