@@ -163,23 +163,38 @@ class CameraSession(private val context: Context, private val cameraManager: Cam
163
163
Log .i(TAG , " configure { ... }: Updating CameraSession Configuration... $diff " )
164
164
165
165
try {
166
- val needsRebuild = config.isActive && ( cameraDevice == null || captureSession == null )
166
+ val needsRebuild = cameraDevice == null || captureSession == null
167
167
if (needsRebuild) {
168
168
Log .i(TAG , " Need to rebuild CameraDevice and CameraCaptureSession..." )
169
169
}
170
170
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
+ }
183
198
}
184
199
185
200
Log .i(TAG , " Successfully updated CameraSession Configuration! isActive: ${config.isActive} " )
0 commit comments