diff --git a/Sources/FaceLiveness/Views/GetReadyPage/CameraPreviewOutputSampleBufferDelegate.swift b/Sources/FaceLiveness/Views/GetReadyPage/CameraPreviewOutputSampleBufferDelegate.swift new file mode 100644 index 00000000..5b98ca19 --- /dev/null +++ b/Sources/FaceLiveness/Views/GetReadyPage/CameraPreviewOutputSampleBufferDelegate.swift @@ -0,0 +1,28 @@ +// +// Copyright Amazon.com Inc. or its affiliates. +// All Rights Reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// + +import AVFoundation +import CoreImage + +class CameraPreviewOutputSampleBufferDelegate: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate { + + let updateBufferHandler: ((CVImageBuffer) -> Void) + + init(_ updateBufferHandler: @escaping (CVImageBuffer) -> Void) { + self.updateBufferHandler = updateBufferHandler + } + + func captureOutput( + _ output: AVCaptureOutput, + didOutput sampleBuffer: CMSampleBuffer, + from connection: AVCaptureConnection + ) { + if let buffer = sampleBuffer.imageBuffer { + updateBufferHandler(buffer) + } + } +} diff --git a/Sources/FaceLiveness/Views/GetReadyPage/CameraPreviewViewModel.swift b/Sources/FaceLiveness/Views/GetReadyPage/CameraPreviewViewModel.swift index db408664..b50173b0 100644 --- a/Sources/FaceLiveness/Views/GetReadyPage/CameraPreviewViewModel.swift +++ b/Sources/FaceLiveness/Views/GetReadyPage/CameraPreviewViewModel.swift @@ -27,9 +27,13 @@ class CameraPreviewViewModel: NSObject, ObservableObject { position: .front ).devices.first + let outputDelegate = CameraPreviewOutputSampleBufferDelegate { [weak self] buffer in + self?.updateBuffer(buffer) + } + self.previewCaptureSession = LivenessCaptureSession( captureDevice: .init(avCaptureDevice: avCaptureDevice), - outputDelegate: self + outputDelegate: outputDelegate ) do { @@ -52,18 +56,10 @@ class CameraPreviewViewModel: NSObject, ObservableObject { func stopSession() { previewCaptureSession?.stopRunning() } -} -extension CameraPreviewViewModel: AVCaptureVideoDataOutputSampleBufferDelegate { - func captureOutput( - _ output: AVCaptureOutput, - didOutput sampleBuffer: CMSampleBuffer, - from connection: AVCaptureConnection - ) { - if let buffer = sampleBuffer.imageBuffer { - DispatchQueue.main.async { - self.buffer = buffer - } + func updateBuffer(_ buffer: CVImageBuffer) { + DispatchQueue.main.async { + self.buffer = buffer } } }