Skip to content

Commit

Permalink
remove setConfig and add autoHandleOrientation
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Mar 6, 2024
1 parent 0459e74 commit 0c042f5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 47 deletions.
25 changes: 5 additions & 20 deletions app/src/main/java/com/pedro/streamer/rotation/CameraFragment.kt
Expand Up @@ -17,11 +17,9 @@
package com.pedro.streamer.rotation

import android.annotation.SuppressLint
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.OrientationEventListener
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
Expand Down Expand Up @@ -73,7 +71,11 @@ class CameraFragment: Fragment(), ConnectChecker {
fun getInstance(): CameraFragment = CameraFragment()
}

val genericStream: GenericStream by lazy { GenericStream(requireContext(), this) }
val genericStream: GenericStream by lazy {
GenericStream(requireContext(), this).apply {
getGlInterface().autoHandleOrientation = true
}
}
private lateinit var surfaceView: SurfaceView
private lateinit var bStartStop: ImageView
private val width = 640
Expand All @@ -85,13 +87,6 @@ class CameraFragment: Fragment(), ConnectChecker {
private val aBitrate = 128 * 1000
private var recordPath = ""

private val orientationEventListener by lazy { object: OrientationEventListener(requireContext()) {
override fun onOrientationChanged(orientation: Int) {
genericStream.setConfig(resources.configuration, requireContext())
}
}
}

@SuppressLint("ClickableViewAccessibility")
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
Expand Down Expand Up @@ -190,16 +185,6 @@ class CameraFragment: Fragment(), ConnectChecker {
genericStream.release()
}

override fun onResume() {
super.onResume()
orientationEventListener.enable()
}

override fun onPause() {
super.onPause()
orientationEventListener.disable()
}

override fun onConnectionStarted(url: String) {
}

Expand Down
26 changes: 1 addition & 25 deletions library/src/main/java/com/pedro/library/base/StreamBase.kt
Expand Up @@ -17,7 +17,6 @@
package com.pedro.library.base

import android.content.Context
import android.content.res.Configuration
import android.graphics.SurfaceTexture
import android.media.MediaCodec
import android.media.MediaFormat
Expand All @@ -34,7 +33,6 @@ import com.pedro.encoder.Frame
import com.pedro.encoder.audio.AudioEncoder
import com.pedro.encoder.audio.GetAacData
import com.pedro.encoder.input.audio.GetMicrophoneData
import com.pedro.encoder.input.video.CameraHelper
import com.pedro.encoder.utils.CodecUtil
import com.pedro.encoder.video.FormatVideoEncoder
import com.pedro.encoder.video.GetVideoData
Expand Down Expand Up @@ -336,35 +334,13 @@ abstract class StreamBase(

/**
* Change stream orientation depend of activity orientation.
* This method affect ro preview and stream.
* This method affect to preview and stream.
* Must be called after prepareVideo.
*/
fun setOrientation(orientation: Int) {
glInterface.setCameraOrientation(orientation)
}

fun setConfig(config: Configuration, context: Context) {
val orientation = CameraHelper.getCameraOrientation(context)
val isReversed = orientation == 180 || orientation == 270
when (config.orientation) {
Configuration.ORIENTATION_LANDSCAPE -> {
if (isReversed) {
setOrientation(90)
} else {
setOrientation(270)
}
}
Configuration.ORIENTATION_PORTRAIT -> {
if (isReversed) {
setOrientation(180)
} else {
setOrientation(0)
}
}
else -> {}
}
}

/**
* Get glInterface used to render video.
* This is useful to send filters to stream.
Expand Down
Expand Up @@ -23,27 +23,36 @@
public class SensorRotationManager {

private final OrientationEventListener listener;
private int currentOrientation = -1;

public interface RotationChangedListener {
void onRotationChanged(int rotation);
}

public SensorRotationManager(Context context, final RotationChangedListener rotationListener) {
public SensorRotationManager(Context context, boolean avoidDuplicated, final RotationChangedListener rotationListener) {
this.listener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int sensorOrientation) {
final int rotation = ((sensorOrientation + 45) / 90) % 4;
final int rotationDegrees = rotation * 90;
if (avoidDuplicated) {
if (currentOrientation == rotationDegrees) return;
currentOrientation = rotationDegrees;
}
rotationListener.onRotationChanged(rotationDegrees);
}
};
}

public void start() {
if (listener.canDetectOrientation()) listener.enable();
if (listener.canDetectOrientation()) {
currentOrientation = -1;
listener.enable();
}
}

public void stop() {
listener.disable();
currentOrientation = -1;
}
}
Expand Up @@ -34,11 +34,13 @@ import com.pedro.encoder.input.video.FpsLimiter
import com.pedro.encoder.utils.gl.AspectRatioMode
import com.pedro.encoder.utils.gl.GlUtil
import com.pedro.library.util.Filter
import com.pedro.library.util.SensorRotationManager
import java.util.concurrent.BlockingQueue
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.LinkedBlockingQueue


/**
* Created by pedro on 14/3/22.
*/
Expand Down Expand Up @@ -72,6 +74,10 @@ class GlStreamInterface(private val context: Context): OnFrameAvailableListener,
private var executor: ExecutorService? = null
private val fpsLimiter = FpsLimiter()
private val forceRender = ForceRenderer()
var autoHandleOrientation = false
private val sensorRotationManager = SensorRotationManager(context, true) {
if (autoHandleOrientation) setCameraOrientation(it)
}

override fun setEncoderSize(width: Int, height: Int) {
encoderWidth = width
Expand Down Expand Up @@ -139,12 +145,14 @@ class GlStreamInterface(private val context: Context): OnFrameAvailableListener,
running = true
mainRender.getSurfaceTexture().setOnFrameAvailableListener(this)
forceRender.start { onFrameAvailable(mainRender.getSurfaceTexture()) }
if (autoHandleOrientation) sensorRotationManager.start()
}
}

override fun stop() {
running = false
forceRender.stop()
sensorRotationManager.stop()
executor?.shutdownNow()
executor = null
surfaceManagerPhoto.release()
Expand Down

0 comments on commit 0c042f5

Please sign in to comment.