Skip to content

Commit a468602

Browse files
authored
fix: Avoid "CameraNotReady" errors when updating props in background (#2382)
1 parent 9ecc09c commit a468602

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

package/android/src/main/java/com/mrousavy/camera/core/CameraSession.kt

+28-13
Original file line numberDiff line numberDiff line change
@@ -163,23 +163,38 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
163163
Log.i(TAG, "configure { ... }: Updating CameraSession Configuration... $diff")
164164

165165
try {
166-
val needsRebuild = config.isActive && (cameraDevice == null || captureSession == null)
166+
val needsRebuild = cameraDevice == null || captureSession == null
167167
if (needsRebuild) {
168168
Log.i(TAG, "Need to rebuild CameraDevice and CameraCaptureSession...")
169169
}
170170

171-
// Build up session or update any props
172-
if (diff.deviceChanged || needsRebuild) {
173-
// 1. cameraId changed, open device
174-
configureCameraDevice(config)
175-
}
176-
if (diff.outputsChanged || needsRebuild) {
177-
// 2. outputs changed, build new session
178-
configureOutputs(config)
179-
}
180-
if (diff.sidePropsChanged || needsRebuild) {
181-
// 3. zoom etc changed, update repeating request
182-
configureCaptureRequest(config)
171+
// Since cameraDevice and captureSession are OS resources, we have three possible paths here:
172+
if (needsRebuild) {
173+
if (config.isActive) {
174+
// A: The Camera has been torn down by the OS and we want it to be active - rebuild everything
175+
Log.i(TAG, "Need to rebuild CameraDevice and CameraCaptureSession...")
176+
configureCameraDevice(config)
177+
configureOutputs(config)
178+
configureCaptureRequest(config)
179+
} else {
180+
// B: The Camera has been torn down by the OS but it's currently in the background - ignore this
181+
Log.i(TAG, "CameraDevice and CameraCaptureSession is torn down but Camera is not active, skipping update...")
182+
}
183+
} else {
184+
// C: The Camera has not been torn down and we just want to update some props - update incrementally
185+
// Build up session or update any props
186+
if (diff.deviceChanged) {
187+
// 1. cameraId changed, open device
188+
configureCameraDevice(config)
189+
}
190+
if (diff.outputsChanged) {
191+
// 2. outputs changed, build new session
192+
configureOutputs(config)
193+
}
194+
if (diff.sidePropsChanged) {
195+
// 3. zoom etc changed, update repeating request
196+
configureCaptureRequest(config)
197+
}
183198
}
184199

185200
Log.i(TAG, "Successfully updated CameraSession Configuration! isActive: ${config.isActive}")

0 commit comments

Comments
 (0)