Skip to content

Commit

Permalink
chore: revert previous changes
Browse files Browse the repository at this point in the history
  • Loading branch information
phantumcode committed Jan 8, 2024
1 parent f731a7e commit 78eb6e0
Showing 1 changed file with 46 additions and 50 deletions.
96 changes: 46 additions & 50 deletions Sources/FaceLiveness/AV/LivenessCaptureSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 78eb6e0

Please sign in to comment.