Skip to content

Commit

Permalink
fix(android): workaround on orientation for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultBee committed Apr 22, 2024
1 parent efd904d commit 3ae0597
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import video.api.livestream.enums.CameraFacingDirection
import video.api.livestream.interfaces.IConnectionListener
import video.api.livestream.models.AudioConfig
import video.api.livestream.models.VideoConfig
import video.api.reactnative.livestream.utils.OrientationManager
import video.api.reactnative.livestream.utils.permissions.PermissionsManager
import video.api.reactnative.livestream.utils.permissions.SerialPermissionsManager
import video.api.reactnative.livestream.utils.showDialog
Expand All @@ -33,6 +34,8 @@ class LiveStreamView @JvmOverloads constructor(
PermissionsManager((context as ThemedReactContext).reactApplicationContext)
)

private val orientationManager = OrientationManager(context)

// Connection listeners
var onConnectionSuccess: (() -> Unit)? = null
var onConnectionFailed: ((reason: String?) -> Unit)? = null
Expand Down Expand Up @@ -204,6 +207,15 @@ class LiveStreamView @JvmOverloads constructor(
require(permissionsManager.hasPermission(Manifest.permission.CAMERA)) { "Missing permissions Manifest.permission.CAMERA" }
require(permissionsManager.hasPermission(Manifest.permission.RECORD_AUDIO)) { "Missing permissions Manifest.permission.RECORD_AUDIO" }

/**
* Workaround to reapply video config in case orientation has changed.
* This happens because `configChanges` may be disabled in the AndroidManifest.xml of a RN
* application.
*/
if (orientationManager.orientationHasChanged) {
liveStream.videoConfig = liveStream.videoConfig
}

url?.let { liveStream.startStreaming(streamKey, it) }
?: liveStream.startStreaming(streamKey)

Expand All @@ -219,6 +231,7 @@ class LiveStreamView @JvmOverloads constructor(
}

override fun close() {
orientationManager.close()
liveStream.release()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package video.api.reactnative.livestream.utils

import android.content.Context
import android.view.OrientationEventListener
import java.io.Closeable

class OrientationManager(context: Context) : Closeable {
private val orientationEventListener = object : OrientationEventListener(context) {
override fun onOrientationChanged(orientation: Int) {
currentOrientation = (orientation + 45) / 90 * 90 % 360
}
}.apply {
enable()
}

private var currentOrientation: Int = OrientationEventListener.ORIENTATION_UNKNOWN
set(value) {
if (field != value) {
if (field == OrientationEventListener.ORIENTATION_UNKNOWN) {
// First time we get an orientation value, don't consider it as a change
field = value
return
} else {
field = value
_orientationHasChanged = true
}
}
}


private var _orientationHasChanged: Boolean = false

val orientationHasChanged: Boolean
get() {
if (!orientationEventListener.canDetectOrientation()) {
// Assume orientation has changed if we can't detect it
return true
}

val orientationHasChanged = _orientationHasChanged
_orientationHasChanged = false
return orientationHasChanged
}

override fun close() {
orientationEventListener.disable()
}
}

0 comments on commit 3ae0597

Please sign in to comment.