Feature: listen orientation event and change video stream

pull/209/head
xufuji456 3 years ago
parent 7fd34de8e1
commit 1804315e3a
  1. 4
      Live/src/main/java/com/frank/live/LivePusherNew.java
  2. 4
      Live/src/main/java/com/frank/live/camera/Camera2Helper.java
  3. 21
      Live/src/main/java/com/frank/live/stream/VideoStream.java
  4. 2
      Live/src/main/java/com/frank/live/stream/VideoStreamBase.java
  5. 32
      Live/src/main/java/com/frank/live/stream/VideoStreamNew.java
  6. 11
      app/src/main/java/com/frank/ffmpeg/activity/LiveActivity.kt
  7. 82
      app/src/main/java/com/frank/ffmpeg/handler/OrientationHandler.kt

@ -65,6 +65,10 @@ public class LivePusherNew implements OnFrameDataCallback {
videoStream.switchCamera();
}
public void setPreviewDegree(int degree) {
videoStream.onPreviewDegreeChanged(degree);
}
/**
* setting mute
*

@ -277,6 +277,10 @@ public class Camera2Helper {
}
}
public void updatePreviewDegree(int degree) {
rotateDegree = degree;
}
public synchronized void stop() {
if (mCameraDevice == null) {
return;

@ -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);
}
}

@ -19,4 +19,6 @@ public abstract class VideoStreamBase {
public abstract void release();
public abstract void onPreviewDegreeChanged(int degree);
}

@ -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());
}
}
}

@ -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

@ -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()
}
}
}
Loading…
Cancel
Save