Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent AVCaptureSession from starting during session configuration #124

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions Sources/FaceLiveness/AV/LivenessCaptureSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class LivenessCaptureSession {
self.outputDelegate = outputDelegate
}

func startSession(frame: CGRect) throws -> CALayer {
try startSession()
func configureCamera(frame: CGRect) throws -> CALayer {
try configureCamera()

guard let captureSession = captureSession else {
throw LivenessCaptureSessionError.captureSessionUnavailable
Expand All @@ -39,7 +39,7 @@ class LivenessCaptureSession {
return previewLayer
}

func startSession() throws {
func configureCamera() throws {
guard let camera = captureDevice.avCaptureDevice
else { throw LivenessCaptureSessionError.cameraUnavailable }

Expand All @@ -58,19 +58,21 @@ class LivenessCaptureSession {
try setupOutput(videoOutput, for: captureSession)
try captureDevice.configure()

configurationQueue.async {
captureSession.startRunning()
}

videoOutput.setSampleBufferDelegate(
outputDelegate,
queue: captureQueue
)
}

func stopRunning() {
func startSession() {
guard let session = captureSession else { return }
configurationQueue.async {
session.startRunning()
}
}

func stopRunning() {
guard let session = captureSession else { return }
defer {
captureSession = nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class CameraPreviewViewModel: NSObject, ObservableObject {
)

do {
try previewCaptureSession?.startSession()
try previewCaptureSession?.configureCamera()
previewCaptureSession?.startSession()
} catch {
Amplify.Logging.default.error("Error starting preview capture session with error: \(error)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,17 @@ class FaceLivenessDetectionViewModel: ObservableObject {
}
}

func startSession() {
captureSession.startSession()
}

func stopRecording() {
captureSession.stopRunning()
}

func startCamera(withinFrame frame: CGRect) -> CALayer? {
func configureCamera(withinFrame frame: CGRect) -> CALayer? {
do {
let avLayer = try captureSession.startSession(frame: frame)
let avLayer = try captureSession.configureCamera(frame: frame)
DispatchQueue.main.async {
self.livenessState.checkIsFacePrepared()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ final class _LivenessViewController: UIViewController {
super.viewDidLoad()
view.backgroundColor = .black
layoutSubviews()
}

override func viewDidAppear(_ animated: Bool) {
setupAVLayer()
}

Expand All @@ -69,13 +66,14 @@ final class _LivenessViewController: UIViewController {
}

private func setupAVLayer() {
guard previewLayer == nil else { return }
let x = view.frame.minX
let y = view.frame.minY
let width = view.frame.width
let height = width / 3 * 4
let cameraFrame = CGRect(x: x, y: y, width: width, height: height)

guard let avLayer = viewModel.startCamera(withinFrame: cameraFrame) else {
guard let avLayer = viewModel.configureCamera(withinFrame: cameraFrame) else {
DispatchQueue.main.async {
self.viewModel.livenessState
.unrecoverableStateEncountered(.missingVideoPermission)
Expand All @@ -92,6 +90,8 @@ final class _LivenessViewController: UIViewController {
DispatchQueue.main.async {
self.view.layer.insertSublayer(avLayer, at: 0)
self.view.layoutIfNeeded()

self.viewModel.startSession()
}
}

Expand Down
Loading