fix: Always call CaptureSession
fully synchronously under Mutex (#1972)
* fix: Always call `CaptureSession` fully synchronously under Mutex * Update CameraView.kt * chore: Format
This commit is contained in:
parent
915ef331d3
commit
18b30cd073
@ -28,8 +28,8 @@ import com.mrousavy.camera.parsers.PixelFormat
|
|||||||
import com.mrousavy.camera.parsers.ResizeMode
|
import com.mrousavy.camera.parsers.ResizeMode
|
||||||
import com.mrousavy.camera.parsers.Torch
|
import com.mrousavy.camera.parsers.Torch
|
||||||
import com.mrousavy.camera.parsers.VideoStabilizationMode
|
import com.mrousavy.camera.parsers.VideoStabilizationMode
|
||||||
|
import kotlin.coroutines.CoroutineContext
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -42,7 +42,9 @@ import kotlinx.coroutines.launch
|
|||||||
// TODO: takePhoto() return with jsi::Value Image reference for faster capture
|
// TODO: takePhoto() return with jsi::Value Image reference for faster capture
|
||||||
|
|
||||||
@SuppressLint("ClickableViewAccessibility", "ViewConstructor", "MissingPermission")
|
@SuppressLint("ClickableViewAccessibility", "ViewConstructor", "MissingPermission")
|
||||||
class CameraView(context: Context) : FrameLayout(context) {
|
class CameraView(context: Context) :
|
||||||
|
FrameLayout(context),
|
||||||
|
CoroutineScope {
|
||||||
companion object {
|
companion object {
|
||||||
const val TAG = "CameraView"
|
const val TAG = "CameraView"
|
||||||
|
|
||||||
@ -104,6 +106,8 @@ class CameraView(context: Context) : FrameLayout(context) {
|
|||||||
internal val outputOrientation: Orientation
|
internal val outputOrientation: Orientation
|
||||||
get() = orientation ?: inputOrientation
|
get() = orientation ?: inputOrientation
|
||||||
|
|
||||||
|
override val coroutineContext: CoroutineContext = CameraQueues.cameraQueue.coroutineDispatcher
|
||||||
|
|
||||||
init {
|
init {
|
||||||
this.installHierarchyFitter()
|
this.installHierarchyFitter()
|
||||||
setupPreviewView()
|
setupPreviewView()
|
||||||
@ -116,12 +120,12 @@ class CameraView(context: Context) : FrameLayout(context) {
|
|||||||
isMounted = true
|
isMounted = true
|
||||||
invokeOnViewReady()
|
invokeOnViewReady()
|
||||||
}
|
}
|
||||||
updateLifecycle()
|
launch { updateLifecycle() }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDetachedFromWindow() {
|
override fun onDetachedFromWindow() {
|
||||||
super.onDetachedFromWindow()
|
super.onDetachedFromWindow()
|
||||||
updateLifecycle()
|
launch { updateLifecycle() }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getPreviewTargetSize(): Size {
|
private fun getPreviewTargetSize(): Size {
|
||||||
@ -142,7 +146,7 @@ class CameraView(context: Context) : FrameLayout(context) {
|
|||||||
|
|
||||||
val previewView = PreviewView(context, this.getPreviewTargetSize(), resizeMode) { surface ->
|
val previewView = PreviewView(context, this.getPreviewTargetSize(), resizeMode) { surface ->
|
||||||
previewSurface = surface
|
previewSurface = surface
|
||||||
configureSession()
|
launch { configureSession() }
|
||||||
}
|
}
|
||||||
previewView.layoutParams = LayoutParams(
|
previewView.layoutParams = LayoutParams(
|
||||||
LayoutParams.MATCH_PARENT,
|
LayoutParams.MATCH_PARENT,
|
||||||
@ -164,6 +168,8 @@ class CameraView(context: Context) : FrameLayout(context) {
|
|||||||
val shouldCheckActive = shouldReconfigureFormat || changedProps.contains("isActive")
|
val shouldCheckActive = shouldReconfigureFormat || changedProps.contains("isActive")
|
||||||
val shouldReconfigureZoomGesture = changedProps.contains("enableZoomGesture")
|
val shouldReconfigureZoomGesture = changedProps.contains("enableZoomGesture")
|
||||||
|
|
||||||
|
launch {
|
||||||
|
// Expensive Calls
|
||||||
if (shouldReconfigurePreview) {
|
if (shouldReconfigurePreview) {
|
||||||
setupPreviewView()
|
setupPreviewView()
|
||||||
}
|
}
|
||||||
@ -176,7 +182,7 @@ class CameraView(context: Context) : FrameLayout(context) {
|
|||||||
if (shouldCheckActive) {
|
if (shouldCheckActive) {
|
||||||
updateLifecycle()
|
updateLifecycle()
|
||||||
}
|
}
|
||||||
|
// Fast Calls
|
||||||
if (shouldReconfigureZoom) {
|
if (shouldReconfigureZoom) {
|
||||||
updateZoom()
|
updateZoom()
|
||||||
}
|
}
|
||||||
@ -186,13 +192,14 @@ class CameraView(context: Context) : FrameLayout(context) {
|
|||||||
if (shouldReconfigureZoomGesture) {
|
if (shouldReconfigureZoomGesture) {
|
||||||
updateZoomGesture()
|
updateZoomGesture()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
Log.e(TAG, "update() threw: ${e.message}")
|
Log.e(TAG, "update() threw: ${e.message}")
|
||||||
invokeOnError(e)
|
invokeOnError(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun configureSession() {
|
private suspend fun configureSession() {
|
||||||
try {
|
try {
|
||||||
Log.i(TAG, "Configuring Camera Device...")
|
Log.i(TAG, "Configuring Camera Device...")
|
||||||
|
|
||||||
@ -236,23 +243,21 @@ class CameraView(context: Context) : FrameLayout(context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun configureFormat() {
|
private suspend fun configureFormat() {
|
||||||
cameraSession.configureFormat(fps, videoStabilizationMode, hdr, lowLightBoost)
|
cameraSession.configureFormat(fps, videoStabilizationMode, hdr, lowLightBoost)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateLifecycle() {
|
private suspend fun updateLifecycle() {
|
||||||
cameraSession.setIsActive(isActive && isAttachedToWindow)
|
cameraSession.setIsActive(isActive && isAttachedToWindow)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateZoom() {
|
private suspend fun updateZoom() {
|
||||||
cameraSession.setZoom(zoom)
|
cameraSession.setZoom(zoom)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateTorch() {
|
private suspend fun updateTorch() {
|
||||||
CoroutineScope(Dispatchers.Default).launch {
|
|
||||||
cameraSession.setTorchMode(torch == Torch.ON)
|
cameraSession.setTorchMode(torch == Torch.ON)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("ClickableViewAccessibility")
|
@SuppressLint("ClickableViewAccessibility")
|
||||||
private fun updateZoomGesture() {
|
private fun updateZoomGesture() {
|
||||||
@ -262,7 +267,7 @@ class CameraView(context: Context) : FrameLayout(context) {
|
|||||||
object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
|
object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
|
||||||
override fun onScale(detector: ScaleGestureDetector): Boolean {
|
override fun onScale(detector: ScaleGestureDetector): Boolean {
|
||||||
zoom *= detector.scaleFactor
|
zoom *= detector.scaleFactor
|
||||||
cameraSession.setZoom(zoom)
|
launch { updateZoom() }
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,9 +40,6 @@ import com.mrousavy.camera.parsers.VideoFileType
|
|||||||
import com.mrousavy.camera.parsers.VideoStabilizationMode
|
import com.mrousavy.camera.parsers.VideoStabilizationMode
|
||||||
import java.io.Closeable
|
import java.io.Closeable
|
||||||
import java.util.concurrent.CancellationException
|
import java.util.concurrent.CancellationException
|
||||||
import kotlin.coroutines.CoroutineContext
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.sync.Mutex
|
||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.sync.withLock
|
||||||
|
|
||||||
@ -52,7 +49,6 @@ class CameraSession(
|
|||||||
private val onInitialized: () -> Unit,
|
private val onInitialized: () -> Unit,
|
||||||
private val onError: (e: Throwable) -> Unit
|
private val onError: (e: Throwable) -> Unit
|
||||||
) : CameraManager.AvailabilityCallback(),
|
) : CameraManager.AvailabilityCallback(),
|
||||||
CoroutineScope,
|
|
||||||
Closeable,
|
Closeable,
|
||||||
CameraOutputs.Callback {
|
CameraOutputs.Callback {
|
||||||
companion object {
|
companion object {
|
||||||
@ -112,8 +108,6 @@ class CameraSession(
|
|||||||
updateVideoOutputs()
|
updateVideoOutputs()
|
||||||
}
|
}
|
||||||
|
|
||||||
override val coroutineContext: CoroutineContext = CameraQueues.cameraQueue.coroutineDispatcher
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
cameraManager.registerAvailabilityCallback(this, CameraQueues.cameraQueue.handler)
|
cameraManager.registerAvailabilityCallback(this, CameraQueues.cameraQueue.handler)
|
||||||
}
|
}
|
||||||
@ -135,7 +129,7 @@ class CameraSession(
|
|||||||
return Orientation.fromRotationDegrees(sensorRotation)
|
return Orientation.fromRotationDegrees(sensorRotation)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun configureSession(
|
suspend fun configureSession(
|
||||||
cameraId: String,
|
cameraId: String,
|
||||||
preview: CameraOutputs.PreviewOutput? = null,
|
preview: CameraOutputs.PreviewOutput? = null,
|
||||||
photo: CameraOutputs.PhotoOutput? = null,
|
photo: CameraOutputs.PhotoOutput? = null,
|
||||||
@ -165,12 +159,10 @@ class CameraSession(
|
|||||||
updateVideoOutputs()
|
updateVideoOutputs()
|
||||||
|
|
||||||
this.cameraId = cameraId
|
this.cameraId = cameraId
|
||||||
launch {
|
|
||||||
startRunning()
|
startRunning()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fun configureFormat(
|
suspend fun configureFormat(
|
||||||
fps: Int? = null,
|
fps: Int? = null,
|
||||||
videoStabilizationMode: VideoStabilizationMode? = null,
|
videoStabilizationMode: VideoStabilizationMode? = null,
|
||||||
hdr: Boolean? = null,
|
hdr: Boolean? = null,
|
||||||
@ -198,31 +190,27 @@ class CameraSession(
|
|||||||
)
|
)
|
||||||
needsReconfiguration = true
|
needsReconfiguration = true
|
||||||
}
|
}
|
||||||
launch {
|
|
||||||
if (needsReconfiguration) {
|
if (needsReconfiguration) {
|
||||||
startRunning()
|
startRunning()
|
||||||
} else {
|
} else {
|
||||||
updateRepeatingRequest()
|
updateRepeatingRequest()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts or stops the Camera.
|
* Starts or stops the Camera.
|
||||||
*/
|
*/
|
||||||
fun setIsActive(isActive: Boolean) {
|
suspend fun setIsActive(isActive: Boolean) {
|
||||||
Log.i(TAG, "Setting isActive: $isActive (isRunning: $isRunning)")
|
Log.i(TAG, "Setting isActive: $isActive (isRunning: $isRunning)")
|
||||||
this.isActive = isActive
|
this.isActive = isActive
|
||||||
if (isActive == isRunning) return
|
if (isActive == isRunning) return
|
||||||
|
|
||||||
launch {
|
|
||||||
if (isActive) {
|
if (isActive) {
|
||||||
startRunning()
|
startRunning()
|
||||||
} else {
|
} else {
|
||||||
stopRunning()
|
stopRunning()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private fun updateVideoOutputs() {
|
private fun updateVideoOutputs() {
|
||||||
val videoPipeline = outputs?.videoOutput?.videoPipeline ?: return
|
val videoPipeline = outputs?.videoOutput?.videoPipeline ?: return
|
||||||
@ -328,14 +316,12 @@ class CameraSession(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setZoom(zoom: Float) {
|
suspend fun setZoom(zoom: Float) {
|
||||||
if (this.zoom != zoom) {
|
if (this.zoom != zoom) {
|
||||||
this.zoom = zoom
|
this.zoom = zoom
|
||||||
launch {
|
|
||||||
updateRepeatingRequest()
|
updateRepeatingRequest()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun focus(x: Int, y: Int) {
|
suspend fun focus(x: Int, y: Int) {
|
||||||
val captureSession = captureSession ?: throw CameraNotReadyError()
|
val captureSession = captureSession ?: throw CameraNotReadyError()
|
||||||
|
Loading…
Reference in New Issue
Block a user