From b28c192d1b6505b57f0d290f6349cf296cc24f6c Mon Sep 17 00:00:00 2001 From: Tuan Pham <103537251+phantumcode@users.noreply.github.com> Date: Wed, 10 Jan 2024 16:14:43 +0000 Subject: [PATCH] fix: resolve race condition when starting AVCaptureSession (#93) --- Sources/FaceLiveness/AV/LivenessCaptureSession.swift | 7 +++---- .../Views/Liveness/FaceLivenessDetectionView.swift | 3 +++ .../Views/Liveness/LivenessViewController.swift | 7 +++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Sources/FaceLiveness/AV/LivenessCaptureSession.swift b/Sources/FaceLiveness/AV/LivenessCaptureSession.swift index 9cd8eccf..f8400262 100644 --- a/Sources/FaceLiveness/AV/LivenessCaptureSession.swift +++ b/Sources/FaceLiveness/AV/LivenessCaptureSession.swift @@ -11,6 +11,7 @@ import AVFoundation class LivenessCaptureSession { let captureDevice: LivenessCaptureDevice private let captureQueue = DispatchQueue(label: "com.amazonaws.faceliveness.cameracapturequeue") + private let configurationQueue = DispatchQueue(label: "com.amazonaws.faceliveness.sessionconfiguration", qos: .userInitiated) let outputDelegate: AVCaptureVideoDataOutputSampleBufferDelegate var captureSession: AVCaptureSession? @@ -43,6 +44,7 @@ class LivenessCaptureSession { else { throw LivenessCaptureSessionError.cameraUnavailable } let cameraInput = try AVCaptureDeviceInput(device: camera) + let videoOutput = AVCaptureVideoDataOutput() teardownExistingSession(input: cameraInput) captureSession = AVCaptureSession() @@ -53,13 +55,10 @@ class LivenessCaptureSession { try setupInput(cameraInput, for: captureSession) captureSession.sessionPreset = captureDevice.preset - - let videoOutput = AVCaptureVideoDataOutput() try setupOutput(videoOutput, for: captureSession) - try captureDevice.configure() - DispatchQueue.global().async { + configurationQueue.async { captureSession.startRunning() } diff --git a/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionView.swift b/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionView.swift index 64098e5f..320da58e 100644 --- a/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionView.swift +++ b/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionView.swift @@ -172,6 +172,9 @@ public struct FaceLivenessDetectorView: View { UIScreen.main.brightness = 1.0 } } + .onDisappear() { + viewModel.stopRecording() + } .onReceive(viewModel.$livenessState) { output in switch output.state { case .completed: diff --git a/Sources/FaceLiveness/Views/Liveness/LivenessViewController.swift b/Sources/FaceLiveness/Views/Liveness/LivenessViewController.swift index 0189c3c8..5e5111a7 100644 --- a/Sources/FaceLiveness/Views/Liveness/LivenessViewController.swift +++ b/Sources/FaceLiveness/Views/Liveness/LivenessViewController.swift @@ -37,6 +37,12 @@ final class _LivenessViewController: UIViewController { } } } + + deinit { + self.previewLayer.removeFromSuperlayer() + (self.previewLayer as? AVCaptureVideoPreviewLayer)?.session = nil + self.previewLayer = nil + } override func viewDidLoad() { super.viewDidLoad() @@ -110,6 +116,7 @@ extension _LivenessViewController: FaceLivenessViewControllerPresenter { imageView.frame = self.previewLayer.frame self.view.addSubview(imageView) self.previewLayer.removeFromSuperlayer() + (self.previewLayer as? AVCaptureVideoPreviewLayer)?.session = nil self.viewModel.stopRecording() } }