diff --git a/Live/src/main/java/com/frank/live/LivePusherNew.java b/Live/src/main/java/com/frank/live/LivePusherNew.java index ad4bbf7..9c86a68 100644 --- a/Live/src/main/java/com/frank/live/LivePusherNew.java +++ b/Live/src/main/java/com/frank/live/LivePusherNew.java @@ -65,6 +65,10 @@ public class LivePusherNew implements OnFrameDataCallback { videoStream.switchCamera(); } + public void setPreviewDegree(int degree) { + videoStream.onPreviewDegreeChanged(degree); + } + /** * setting mute * diff --git a/Live/src/main/java/com/frank/live/camera/Camera2Helper.java b/Live/src/main/java/com/frank/live/camera/Camera2Helper.java index 65b9a1f..feb8eb2 100644 --- a/Live/src/main/java/com/frank/live/camera/Camera2Helper.java +++ b/Live/src/main/java/com/frank/live/camera/Camera2Helper.java @@ -277,6 +277,10 @@ public class Camera2Helper { } } + public void updatePreviewDegree(int degree) { + rotateDegree = degree; + } + public synchronized void stop() { if (mCameraDevice == null) { return; diff --git a/Live/src/main/java/com/frank/live/stream/VideoStream.java b/Live/src/main/java/com/frank/live/stream/VideoStream.java index 5977bb8..3a67fc8 100644 --- a/Live/src/main/java/com/frank/live/stream/VideoStream.java +++ b/Live/src/main/java/com/frank/live/stream/VideoStream.java @@ -18,6 +18,9 @@ public class VideoStream extends VideoStreamBase implements Camera.PreviewCallba private final int mBitrate; private final int mFrameRate; private boolean isLiving; + private int previewWidth; + private int previewHeight; + private int rotateDegree = 90; public VideoStream(OnFrameDataCallback callback, View view, @@ -68,8 +71,24 @@ public class VideoStream extends VideoStreamBase implements Camera.PreviewCallba @Override public void onChanged(int w, int h) { + previewWidth = w; + previewHeight = h; + updateVideoCodecInfo(w, h, rotateDegree); + } + + @Override + public void onPreviewDegreeChanged(int degree) { + updateVideoCodecInfo(previewWidth, previewHeight, degree); + } + + private void updateVideoCodecInfo(int width, int height, int degree) { + if (degree == 90 || degree == 270) { + int temp = width; + width = height; + height = temp; + } if (mCallback != null) { - mCallback.onVideoCodecInfo(w, h, mFrameRate, mBitrate); + mCallback.onVideoCodecInfo(width, height, mFrameRate, mBitrate); } } diff --git a/Live/src/main/java/com/frank/live/stream/VideoStreamBase.java b/Live/src/main/java/com/frank/live/stream/VideoStreamBase.java index 2874947..1b5152c 100644 --- a/Live/src/main/java/com/frank/live/stream/VideoStreamBase.java +++ b/Live/src/main/java/com/frank/live/stream/VideoStreamBase.java @@ -19,4 +19,6 @@ public abstract class VideoStreamBase { public abstract void release(); + public abstract void onPreviewDegreeChanged(int degree); + } diff --git a/Live/src/main/java/com/frank/live/stream/VideoStreamNew.java b/Live/src/main/java/com/frank/live/stream/VideoStreamNew.java index 3ad6ba1..caf8dc2 100644 --- a/Live/src/main/java/com/frank/live/stream/VideoStreamNew.java +++ b/Live/src/main/java/com/frank/live/stream/VideoStreamNew.java @@ -27,6 +27,7 @@ public class VideoStreamNew extends VideoStreamBase private int rotation = 0; private boolean isLiving; + private Size previewSize; private final Context mContext; private Camera2Helper camera2Helper; private final VideoParam mVideoParam; @@ -157,16 +158,8 @@ public class VideoStreamNew extends VideoStreamBase @Override public void onCameraOpened(Size previewSize, int displayOrientation) { Log.i(TAG, "onCameraOpened previewSize=" + previewSize.toString()); - if (mCallback != null && mVideoParam != null) { - int width = previewSize.getWidth(); - int height = previewSize.getHeight(); - if (getPreviewDegree(rotation) == 90 || getPreviewDegree(rotation) == 270) { - int temp = width; - width = height; - height = temp; - } - mCallback.onVideoCodecInfo(width, height, mVideoParam.getFrameRate(), mVideoParam.getBitRate()); - } + this.previewSize = previewSize; + updateVideoCodecInfo(getPreviewDegree(rotation)); } @Override @@ -179,4 +172,23 @@ public class VideoStreamNew extends VideoStreamBase Log.e(TAG, "onCameraError=" + e.toString()); } + @Override + public void onPreviewDegreeChanged(int degree) { + updateVideoCodecInfo(degree); + } + + private void updateVideoCodecInfo(int degree) { + camera2Helper.updatePreviewDegree(degree); + if (mCallback != null && mVideoParam != null) { + int width = previewSize.getWidth(); + int height = previewSize.getHeight(); + if (degree == 90 || degree == 270) { + int temp = width; + width = height; + height = temp; + } + mCallback.onVideoCodecInfo(width, height, mVideoParam.getFrameRate(), mVideoParam.getBitRate()); + } + } + } diff --git a/app/src/main/java/com/frank/ffmpeg/activity/LiveActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/LiveActivity.kt index 5b235bc..91f4e73 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/LiveActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/LiveActivity.kt @@ -19,6 +19,7 @@ import android.widget.ToggleButton import com.frank.ffmpeg.R import com.frank.ffmpeg.handler.ConnectionReceiver +import com.frank.ffmpeg.handler.OrientationHandler import com.frank.ffmpeg.listener.OnNetworkChangeListener import com.frank.live.camera.Camera2Helper import com.frank.live.listener.LiveStateChangeListener @@ -37,6 +38,7 @@ open class LiveActivity : BaseActivity(), CompoundButton.OnCheckedChangeListener private var livePusher: LivePusherNew? = null private var isPushing = false private var connectionReceiver: ConnectionReceiver? = null + private var orientationHandler: OrientationHandler? = null @SuppressLint("HandlerLeak") private val mHandler = object : Handler() { @@ -61,6 +63,14 @@ open class LiveActivity : BaseActivity(), CompoundButton.OnCheckedChangeListener initView() initPusher() registerBroadcast(this) + orientationHandler = OrientationHandler(this) + orientationHandler?.enable() + orientationHandler?.setOnOrientationListener(object :OrientationHandler.OnOrientationListener { + override fun onOrientation(orientation: Int) { + val previewDegree = (orientation + 90) % 360 + livePusher?.setPreviewDegree(previewDegree) + } + }) } private fun initView() { @@ -124,6 +134,7 @@ open class LiveActivity : BaseActivity(), CompoundButton.OnCheckedChangeListener override fun onDestroy() { super.onDestroy() + orientationHandler?.disable() if (livePusher != null) { if (isPushing) { isPushing = false diff --git a/app/src/main/java/com/frank/ffmpeg/handler/OrientationHandler.kt b/app/src/main/java/com/frank/ffmpeg/handler/OrientationHandler.kt new file mode 100644 index 0000000..c7edd65 --- /dev/null +++ b/app/src/main/java/com/frank/ffmpeg/handler/OrientationHandler.kt @@ -0,0 +1,82 @@ +package com.frank.ffmpeg.handler + +import android.content.Context +import android.util.Log +import android.view.OrientationEventListener + +/** + * Handler of orientation rotate event + * Created by frank on 2022/4/13. + */ + +class OrientationHandler(context: Context) { + + companion object { + private const val TAG = "OrientationHandler" + private const val OFFSET_ANGLE = 5 + } + + private var lastOrientationDegree = 0 + private var onOrientationListener: OnOrientationListener? = null + private var orientationEventListener: OrientationEventListener? = null + + interface OnOrientationListener { + fun onOrientation(orientation: Int) + } + + init { + initOrientation(context) + } + + fun setOnOrientationListener(onOrientationListener: OnOrientationListener) { + this.onOrientationListener = onOrientationListener + } + + private fun initOrientation(context: Context) { + orientationEventListener = object : OrientationEventListener(context.applicationContext) { + override fun onOrientationChanged(orientation: Int) { + if (orientation == ORIENTATION_UNKNOWN) + return + + if (orientation >= 0 - OFFSET_ANGLE && orientation <= OFFSET_ANGLE) { + if (lastOrientationDegree != 0) { + Log.i(TAG, "0, portrait down") + lastOrientationDegree = 0 + onOrientationListener?.onOrientation(lastOrientationDegree) + } + } else if (orientation >= 90 - OFFSET_ANGLE && orientation <= 90 + OFFSET_ANGLE) { + if (lastOrientationDegree != 90) { + Log.i(TAG, "90, landscape right") + lastOrientationDegree = 90 + onOrientationListener?.onOrientation(lastOrientationDegree) + } + } else if (orientation >= 180 - OFFSET_ANGLE && orientation <= 180 + OFFSET_ANGLE) { + if (lastOrientationDegree != 180) { + Log.i(TAG, "180, portrait up") + lastOrientationDegree = 180 + onOrientationListener?.onOrientation(lastOrientationDegree) + } + } else if (orientation >= 270 - OFFSET_ANGLE && orientation <= 270 + OFFSET_ANGLE) { + if (lastOrientationDegree !=270) { + Log.i(TAG, "270, landscape left") + lastOrientationDegree = 270 + onOrientationListener?.onOrientation(lastOrientationDegree) + } + } + } + } + } + + fun enable() { + if (orientationEventListener?.canDetectOrientation()!!) { + orientationEventListener?.enable() + } + } + + fun disable() { + if (orientationEventListener?.canDetectOrientation()!!) { + orientationEventListener?.disable() + } + } + +} \ No newline at end of file