Skip to content

Commit 48a4648

Browse files
committed
fix(ios): only initialize metadata output after session is running
Somehow this has causes issues on some devices, such as iPad 7 Gen. which somehow deadlocked the initialization of the capture session causing an exception to be thrown in the `displayPreview` method because the capture session was not yet started.
1 parent 863f196 commit 48a4648

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

ios/Sources/CameraViewPlugin/CameraViewManager.swift

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,20 @@ internal let SUPPORTED_CAMERA_DEVICE_TYPES: [AVCaptureDevice.DeviceType] = [
9090
completion: { error in
9191
if error != nil { completion(error) }
9292

93+
// Handle barcode detection after session is running
94+
if configuration.enableBarcodeDetection {
95+
do {
96+
try self.enableBarcodeDetection()
97+
} catch {
98+
completion(error)
99+
return
100+
}
101+
}
102+
93103
// Complete already because the camera is ready to be used
94-
// We might asynchronously upgrade to a triple camera in the background if available and configured
95104
completion(nil)
96105

106+
// We might asynchronously upgrade to a triple camera in the background if available and configured
97107
if configuration.useTripleCameraIfAvailable {
98108
Task {
99109
await self.upgradeToTripleCameraIfAvailable()
@@ -359,10 +369,7 @@ internal let SUPPORTED_CAMERA_DEVICE_TYPES: [AVCaptureDevice.DeviceType] = [
359369
// Set up the video data output for snapshots
360370
try setupVideoDataOutput()
361371

362-
// Setup metadata output for QR code scanning if enabled
363-
if configuration.enableBarcodeDetection {
364-
try setupMetadataOutput()
365-
} else {
372+
if !configuration.enableBarcodeDetection {
366373
// Remove the metadata output in case it already existed for the
367374
// capture session
368375
removeMetadataOutput()
@@ -405,6 +412,26 @@ internal let SUPPORTED_CAMERA_DEVICE_TYPES: [AVCaptureDevice.DeviceType] = [
405412
}
406413
}
407414

415+
416+
/// Enables barcode detection by adding metadata output to the running session.
417+
/// Somehow adding the metadata output with the session not being started yet
418+
/// caused issues on some devices (iPad 7th Gen) where the session would just
419+
/// not start at all without an error being thrown. I haven't been able to
420+
/// figure out the root cause of this but it might generally be a good idea
421+
/// to only add the metadata output when the session is already running.
422+
///
423+
/// - Throws: An error if the metadata output cannot be added.
424+
private func enableBarcodeDetection() throws {
425+
guard captureSession.isRunning else {
426+
throw CameraError.sessionNotRunning
427+
}
428+
429+
captureSession.beginConfiguration()
430+
defer { captureSession.commitConfiguration() }
431+
432+
try setupMetadataOutput()
433+
}
434+
408435
/// Retrieve a list of a available camera devices
409436
///
410437
/// - Returns: a list of all available camera devices.

0 commit comments

Comments
 (0)