fix: Fix torch not working on iOS (#2031)

* fix: Fix `torch` not working on iOS

* Format

* fix: Use `withSessionLock` and `withDeviceLock`

* Update CameraSession.swift

* Update RecordingSession.swift
This commit is contained in:
Marc Rousavy
2023-10-18 18:04:58 +02:00
committed by GitHub
parent 6956fded2d
commit 89dfd351e0
4 changed files with 90 additions and 55 deletions

View File

@@ -114,56 +114,50 @@ class CameraSession: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate, AVC
do {
// If needed, configure the AVCaptureSession (inputs, outputs)
if difference.isSessionConfigurationDirty {
// Lock Capture Session for configuration
ReactLogger.log(level: .info, message: "Beginning CameraSession configuration...")
self.captureSession.beginConfiguration()
// 1. Update input device
if difference.inputChanged {
try self.configureDevice(configuration: config)
try self.withSessionLock {
// 1. Update input device
if difference.inputChanged {
try self.configureDevice(configuration: config)
}
// 2. Update outputs
if difference.outputsChanged {
try self.configureOutputs(configuration: config)
}
// 3. Update output orientation
if difference.orientationChanged {
self.configureOrientation(configuration: config)
}
}
// 2. Update outputs
if difference.outputsChanged {
try self.configureOutputs(configuration: config)
}
// 3. Update output orientation
if difference.orientationChanged {
self.configureOrientation(configuration: config)
}
// Unlock Capture Session again and submit configuration to Hardware
self.captureSession.commitConfiguration()
ReactLogger.log(level: .info, message: "Committed CameraSession configuration!")
}
// If needed, configure the AVCaptureDevice (format, zoom, low-light-boost, ..)
if difference.isDeviceConfigurationDirty {
guard let device = self.videoDeviceInput?.device else {
throw CameraError.session(.cameraNotReady)
try self.withDeviceLock { device in
// 4. Configure format
if difference.formatChanged {
try self.configureFormat(configuration: config, device: device)
}
// 5. Configure side-props (fps, lowLightBoost)
if difference.sidePropsChanged {
try self.configureSideProps(configuration: config, device: device)
}
// 6. Configure zoom
if difference.zoomChanged {
self.configureZoom(configuration: config, device: device)
}
}
ReactLogger.log(level: .info, message: "Beginning CaptureDevice configuration...")
try device.lockForConfiguration()
// 4. Configure format
if difference.formatChanged {
try self.configureFormat(configuration: config)
}
// 5. Configure side-props (fps, lowLightBoost)
if difference.sidePropsChanged {
try self.configureSideProps(configuration: config)
}
// 6. Configure zoom
if difference.zoomChanged {
try self.configureZoom(configuration: config)
}
device.unlockForConfiguration()
ReactLogger.log(level: .info, message: "Committed CaptureDevice configuration!")
}
// 6. Start or stop the session if needed
// 7. Start or stop the session if needed
self.checkIsActive(configuration: config)
// 8. Enable or disable the Torch if needed (requires session to be running)
if difference.torchChanged {
try self.withDeviceLock { device in
try self.configureTorch(configuration: config, device: device)
}
}
// Update successful, set the new configuration!
self.configuration = config
} catch {
@@ -191,6 +185,41 @@ class CameraSession: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate, AVC
}
}
/**
Runs the given [lambda] under an AVCaptureSession configuration lock (`beginConfiguration()`)
*/
private func withSessionLock(_ lambda: () throws -> Void) throws {
// Lock Capture Session for configuration
ReactLogger.log(level: .info, message: "Beginning CameraSession configuration...")
captureSession.beginConfiguration()
defer {
// Unlock Capture Session again and submit configuration to Hardware
self.captureSession.commitConfiguration()
ReactLogger.log(level: .info, message: "Committed CameraSession configuration!")
}
// Call lambda
try lambda()
}
/**
Runs the given [lambda] under an AVCaptureDevice configuration lock (`lockForConfiguration()`)
*/
private func withDeviceLock(_ lambda: (_ device: AVCaptureDevice) throws -> Void) throws {
guard let device = videoDeviceInput?.device else {
throw CameraError.session(.cameraNotReady)
}
ReactLogger.log(level: .info, message: "Beginning CaptureDevice configuration...")
try device.lockForConfiguration()
defer {
device.unlockForConfiguration()
ReactLogger.log(level: .info, message: "Committed CaptureDevice configuration!")
}
// Call lambda with Device
try lambda(device)
}
/**
Starts or stops the CaptureSession if needed (`isActive`)
*/