Fix some rotation issue

This commit is contained in:
Schoumi 2022-10-04 17:31:53 +02:00
parent 3abf6c3554
commit cfffa2e418
1 changed files with 57 additions and 48 deletions

View File

@ -38,6 +38,8 @@ import fr.mobdev.peertubelive.R
import fr.mobdev.peertubelive.databinding.StreamBinding import fr.mobdev.peertubelive.databinding.StreamBinding
import fr.mobdev.peertubelive.manager.InstanceManager.EXTRA_DATA import fr.mobdev.peertubelive.manager.InstanceManager.EXTRA_DATA
import fr.mobdev.peertubelive.objects.StreamData import fr.mobdev.peertubelive.objects.StreamData
import java.util.*
import kotlin.collections.ArrayList
class StreamActivity : AppCompatActivity() { class StreamActivity : AppCompatActivity() {
@ -50,11 +52,12 @@ class StreamActivity : AppCompatActivity() {
private var surfaceInit: Boolean = false private var surfaceInit: Boolean = false
private var permissionGiven: Boolean = false private var permissionGiven: Boolean = false
private var streamIsActive: Boolean = false private var streamIsActive: Boolean = false
private var screenOrientation: Int = 0 private var screenOrientation: Int = -1
private var lastScreenOrientation: Int = 0 private var lastScreenOrientation: Int = 0
private var orientationCounter: Int = 0
private var rotationIsEnabled: Boolean = true private var rotationIsEnabled: Boolean = true
private var orientationTimer: Timer = Timer()
companion object { companion object {
const val BACKGROUND :Int = 1 const val BACKGROUND :Int = 1
const val LOCK :Int = 2 const val LOCK :Int = 2
@ -69,48 +72,11 @@ class StreamActivity : AppCompatActivity() {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
binding = DataBindingUtil.setContentView(this, R.layout.stream) binding = DataBindingUtil.setContentView(this, R.layout.stream)
orientationEventListener = object: OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){ orientationEventListener = object: OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL){
override fun onOrientationChanged(orientation: Int) { override fun onOrientationChanged(orientation: Int) {
if(orientation < 0 || !rotationIsEnabled) { handlerOrientation(orientation)
return
}
var localOrientation: Int
localOrientation = when (orientation) {
in 45..135 -> {
90
}
in 135..225 -> {
180
}
in 225..315 -> {
270
}
else -> {
0
}
}
if(localOrientation != lastScreenOrientation) {
lastScreenOrientation = localOrientation
orientationCounter = 0
} else {
orientationCounter++
}
if (lastScreenOrientation != screenOrientation && orientationCounter > 30) {
screenOrientation = lastScreenOrientation
rtmpCamera2.glInterface.setStreamRotation(screenOrientation)
if (screenOrientation == 90) {
localOrientation = 270
} else if(screenOrientation == 270) {
localOrientation = 90
}
binding.flash.rotation = localOrientation.toFloat()
binding.muteMicro.rotation = localOrientation.toFloat()
binding.switchCamera.rotation = localOrientation.toFloat()
binding.rotation.rotation = localOrientation.toFloat()
}
} }
} }
orientationEventListener.enable() orientationEventListener.enable()
@ -146,14 +112,11 @@ class StreamActivity : AppCompatActivity() {
binding.flash.visibility = View.GONE binding.flash.visibility = View.GONE
binding.rotation.setOnClickListener { binding.rotation.setOnClickListener {
if (rotationIsEnabled) { if (rotationIsEnabled)
rotationIsEnabled = !rotationIsEnabled
binding.rotation.setImageResource(R.drawable.baseline_screen_lock_rotation_24) binding.rotation.setImageResource(R.drawable.baseline_screen_lock_rotation_24)
} else
else {
rotationIsEnabled = !rotationIsEnabled
binding.rotation.setImageResource(R.drawable.baseline_screen_rotation_24) binding.rotation.setImageResource(R.drawable.baseline_screen_rotation_24)
} rotationIsEnabled = !rotationIsEnabled
} }
binding.rotation.visibility = View.GONE binding.rotation.visibility = View.GONE
binding.surfaceView.holder.addCallback(object: SurfaceHolder.Callback { binding.surfaceView.holder.addCallback(object: SurfaceHolder.Callback {
@ -262,6 +225,52 @@ class StreamActivity : AppCompatActivity() {
} }
} }
private fun handlerOrientation(orientation: Int) {
if(orientation < 0 || !rotationIsEnabled) {
return
}
var localOrientation: Int
localOrientation = when (orientation) {
in 45..135 -> {
90
}
in 135..225 -> {
180
}
in 225..315 -> {
270
}
else -> {
0
}
}
if(localOrientation != lastScreenOrientation) {
lastScreenOrientation = localOrientation
orientationTimer.cancel()
orientationTimer.purge()
orientationTimer = Timer()
orientationTimer.schedule(object : TimerTask() {
override fun run() {
if (lastScreenOrientation != screenOrientation) {
screenOrientation = lastScreenOrientation
rtmpCamera2.glInterface.setStreamRotation(screenOrientation)
if (screenOrientation == 90) {
localOrientation = 270
} else if(screenOrientation == 270) {
localOrientation = 90
}
binding.flash.rotation = localOrientation.toFloat()
binding.muteMicro.rotation = localOrientation.toFloat()
binding.switchCamera.rotation = localOrientation.toFloat()
binding.rotation.rotation = localOrientation.toFloat()
}
}
},3000)
}
}
private fun startStream() { private fun startStream() {
streamIsActive = true streamIsActive = true
binding.permissionInfo.visibility = View.GONE binding.permissionInfo.visibility = View.GONE