fix: Improve performance of zoom, fps, hdr and format selection

This commit is contained in:
Marc Rousavy 2023-08-23 15:50:34 +02:00
parent efe6556fc2
commit 4b009a0053

View File

@ -139,6 +139,7 @@ class CameraSession(private val context: Context,
this.hdr = hdr this.hdr = hdr
this.lowLightBoost = lowLightBoost this.lowLightBoost = lowLightBoost
var needsReconfiguration = false
val currentOutputs = outputs val currentOutputs = outputs
if (currentOutputs != null && currentOutputs.enableHdr != hdr) { if (currentOutputs != null && currentOutputs.enableHdr != hdr) {
// Update existing HDR for Outputs // Update existing HDR for Outputs
@ -149,9 +150,11 @@ class CameraSession(private val context: Context,
currentOutputs.video, currentOutputs.video,
hdr, hdr,
this) this)
needsReconfiguration = true
} }
launch { launch {
startRunning() if (needsReconfiguration) startRunning()
else updateRepeatingRequest()
} }
} }
@ -274,7 +277,7 @@ class CameraSession(private val context: Context,
suspend fun setTorchMode(enableTorch: Boolean) { suspend fun setTorchMode(enableTorch: Boolean) {
if (this.enableTorch != enableTorch) { if (this.enableTorch != enableTorch) {
this.enableTorch = enableTorch this.enableTorch = enableTorch
startRunning() updateRepeatingRequest()
} }
} }
@ -282,7 +285,7 @@ class CameraSession(private val context: Context,
if (this.zoom != zoom) { if (this.zoom != zoom) {
this.zoom = zoom this.zoom = zoom
launch { launch {
startRunning() updateRepeatingRequest()
} }
} }
} }
@ -354,6 +357,8 @@ class CameraSession(private val context: Context,
// Cache session in memory // Cache session in memory
captureSession = session captureSession = session
lastOutputsHashCode = outputs.hashCode() lastOutputsHashCode = outputs.hashCode()
// New session initialized
onInitialized()
return session return session
} }
@ -429,12 +434,7 @@ class CameraSession(private val context: Context,
try { try {
mutex.withLock { mutex.withLock {
val fps = fps
val videoStabilizationMode = videoStabilizationMode
val lowLightBoost = lowLightBoost
val hdr = hdr
val outputs = outputs val outputs = outputs
if (outputs == null || outputs.size == 0) { if (outputs == null || outputs.size == 0) {
Log.i(TAG, "CameraSession doesn't have any Outputs, canceling..") Log.i(TAG, "CameraSession doesn't have any Outputs, canceling..")
destroy() destroy()
@ -452,25 +452,42 @@ class CameraSession(private val context: Context,
isRunning = false isRunning = false
} }
// 4. Create repeating request (configures FPS, HDR, etc.) Log.i(TAG, "Camera Session initialized! Starting repeating request..")
val repeatingRequest = getPreviewCaptureRequest(session, outputs, fps, videoStabilizationMode, lowLightBoost, hdr)
// 5. Start repeating request
session.setRepeatingRequest(repeatingRequest, null, null)
Log.i(TAG, "Camera Session started!")
isRunning = true isRunning = true
this.captureSession = session this.captureSession = session
this.outputs = outputs
this.cameraDevice = camera this.cameraDevice = camera
onInitialized()
} }
updateRepeatingRequest()
} catch (e: IllegalStateException) { } catch (e: IllegalStateException) {
Log.e(TAG, "Failed to start Camera Session, this session is already closed.", e) Log.e(TAG, "Failed to start Camera Session, this session is already closed.", e)
} }
} }
private suspend fun updateRepeatingRequest() {
mutex.withLock {
val session = captureSession
if (session == null) {
// Not yet ready. Start session first, then it will update repeating request.
startRunning()
return
}
val fps = fps
val videoStabilizationMode = videoStabilizationMode
val lowLightBoost = lowLightBoost
val hdr = hdr
val outputs = outputs
if (outputs == null || outputs.size == 0) {
Log.i(TAG, "CameraSession doesn't have any Outputs, canceling..")
return
}
val repeatingRequest = getPreviewCaptureRequest(session, outputs, fps, videoStabilizationMode, lowLightBoost, hdr)
session.setRepeatingRequest(repeatingRequest, null, null)
}
}
private suspend fun stopRunning() { private suspend fun stopRunning() {
Log.i(TAG, "Stopping Camera Session...") Log.i(TAG, "Stopping Camera Session...")
try { try {