I've written the code below based on several examples I found online, in order to capture video data from the built-in camera on macOS. No fatal errors are thrown while setting up the AVCaptureSession
, yet the delegate is not being called, and some apparent errors are logged as soon as I call captureSession.startRunning()
.
So far, I've tried making small adjustments to my code (e.g. removing videoSettings
on the output or the sessionPreset
from the session), as well as checking in the systems preferences that my test app has camera access permission. However, the logs remain, and the delegate is not getting called.
CameraInput class (includes delegate):
final class CameraInput: NSObject {
private lazy var sampleBufferDelegateQueue = DispatchQueue(label: "CameraInput")
private lazy var captureSession: AVCaptureSession = {
let session = AVCaptureSession()
session.sessionPreset = .hd1280x720
let device = AVCaptureDevice.default(for: .video)!
let input = try! AVCaptureDeviceInput(device: device)
session.addInput(input)
let output = AVCaptureVideoDataOutput()
output.videoSettings = [
kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
kCVPixelBufferMetalCompatibilityKey as String: true
]
output.alwaysDiscardsLateVideoFrames = true
output.setSampleBufferDelegate(self, queue: self.sampleBufferDelegateQueue)
session.addOutput(output)
return session
}()
}
extension CameraInput: CVInput {
func start() {
guard !self.captureSession.isRunning else {
return
}
self.captureSession.startRunning()
}
func stop() {
guard self.captureSession.isRunning else {
return
}
self.captureSession.stopRunning()
}
}
extension CameraInput: AVCaptureVideoDataOutputSampleBufferDelegate {
private func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
// Handle captured frame
}
}
ViewController (default viewcontroller in new cocoa app):
class ViewController: NSViewController {
lazy var cameraInput = CameraInput()
override func viewDidLoad() {
super.viewDidLoad()
self.cameraInput.start()
}
}
The logged information:
2019-01-02 16:27:15.821858+0100 TestApp[14182:1492243] [] CMIO_Unit_ScopeElement.h:200:SafeGetElement Throwing err: -67454
2019-01-02 16:27:15.823248+0100 TestApp[14182:1492243] [] CMIOUnitFigBaseObjectImpl.c:246:CMIOUnitCreateFromDescription Invalid paramater
2019-01-02 16:27:15.840190+0100 TestApp[14182:1492243] [] CMIO_Unit_Input_Device.cpp:244:GetPropertyInfo CMIOUInputFromProcs::GetPropertyInfo() failed for id 102, Error: -67456
Any help in getting this to work is greatly appreciated! Also, please inform me if more information is required.
EDIT After running into this question, I've ruled out that it is a problem regarding sandboxing.