From 78eb6e064e813cfe259686e95a9f86fa6adbbc58 Mon Sep 17 00:00:00 2001 From: Tuan Pham Date: Mon, 8 Jan 2024 14:12:33 -0600 Subject: [PATCH] chore: revert previous changes --- .../AV/LivenessCaptureSession.swift | 96 +++++++++---------- 1 file changed, 46 insertions(+), 50 deletions(-) diff --git a/Sources/FaceLiveness/AV/LivenessCaptureSession.swift b/Sources/FaceLiveness/AV/LivenessCaptureSession.swift index 988e0c97..f8400262 100644 --- a/Sources/FaceLiveness/AV/LivenessCaptureSession.swift +++ b/Sources/FaceLiveness/AV/LivenessCaptureSession.swift @@ -14,9 +14,6 @@ class LivenessCaptureSession { private let configurationQueue = DispatchQueue(label: "com.amazonaws.faceliveness.sessionconfiguration", qos: .userInitiated) let outputDelegate: AVCaptureVideoDataOutputSampleBufferDelegate var captureSession: AVCaptureSession? - private var deviceInput: AVCaptureDeviceInput? - private var videoOutput: AVCaptureVideoDataOutput? - private var previewLayer: AVCaptureVideoPreviewLayer? var outputSampleBufferCapturer: OutputSampleBufferCapturer? { return outputDelegate as? OutputSampleBufferCapturer @@ -38,80 +35,79 @@ class LivenessCaptureSession { frame: frame, for: captureSession ) - self.previewLayer = previewLayer + return previewLayer } func startSession() throws { - teardownCurrentSession() guard let camera = captureDevice.avCaptureDevice else { throw LivenessCaptureSessionError.cameraUnavailable } + + let cameraInput = try AVCaptureDeviceInput(device: camera) + let videoOutput = AVCaptureVideoDataOutput() + + teardownExistingSession(input: cameraInput) captureSession = AVCaptureSession() - deviceInput = try AVCaptureDeviceInput(device: camera) - videoOutput = AVCaptureVideoDataOutput() guard let captureSession = captureSession else { throw LivenessCaptureSessionError.captureSessionUnavailable } - guard let input = deviceInput, captureSession.canAddInput(input) else { - throw LivenessCaptureSessionError.captureSessionInputUnavailable - } - guard let output = videoOutput, captureSession.canAddOutput(output) else { - throw LivenessCaptureSessionError.captureSessionOutputUnavailable - } + + try setupInput(cameraInput, for: captureSession) + captureSession.sessionPreset = captureDevice.preset + try setupOutput(videoOutput, for: captureSession) try captureDevice.configure() - - configureOutput(output) - + configurationQueue.async { - captureSession.beginConfiguration() - captureSession.sessionPreset = self.captureDevice.preset - captureSession.addInput(input) - captureSession.addOutput(output) - captureSession.commitConfiguration() captureSession.startRunning() } + + videoOutput.setSampleBufferDelegate( + outputDelegate, + queue: captureQueue + ) } func stopRunning() { - teardownCurrentSession() + if captureSession?.isRunning == true { + captureSession?.stopRunning() + } } - - private func configureOutput(_ output: AVCaptureVideoDataOutput) { + + private func teardownExistingSession(input: AVCaptureDeviceInput) { + stopRunning() + captureSession?.removeInput(input) + } + + private func setupInput( + _ input: AVCaptureDeviceInput, + for captureSession: AVCaptureSession + ) throws { + if captureSession.canAddInput(input) { + captureSession.addInput(input) + } else { + throw LivenessCaptureSessionError.captureSessionInputUnavailable + } + } + + private func setupOutput( + _ output: AVCaptureVideoDataOutput, + for captureSession: AVCaptureSession + ) throws { + if captureSession.canAddOutput(output) { + captureSession.addOutput(output) + } else { + throw LivenessCaptureSessionError.captureSessionOutputUnavailable + } output.videoSettings = [ kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA ] - + output.connections .filter(\.isVideoOrientationSupported) .forEach { $0.videoOrientation = .portrait - } - - output.setSampleBufferDelegate( - outputDelegate, - queue: captureQueue - ) - } - - private func teardownCurrentSession() { - if captureSession?.isRunning == true { - captureSession?.stopRunning() - } - - if let output = videoOutput { - captureSession?.removeOutput(output) - videoOutput = nil - } - if let input = deviceInput { - captureSession?.removeInput(input) - deviceInput = nil } - - previewLayer?.removeFromSuperlayer() - previewLayer?.session = nil - previewLayer = nil - captureSession = nil } private func previewLayer(