Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Created May 9, 2022 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturdysturge/9272796f8da13db632ba0dd7d097b864 to your computer and use it in GitHub Desktop.
Save sturdysturge/9272796f8da13db632ba0dd7d097b864 to your computer and use it in GitHub Desktop.
import Foundation
import AVFoundation
extension AVCaptureSession {
func setup(deviceInput: AVCaptureDeviceInput = .defaultDevice,
delegate: AVCaptureVideoDataOutputSampleBufferDelegate) {
self.beginConfiguration()
self.sessionPreset = .vga640x480
guard self.canAddInput(deviceInput) else {
print("Could not add video device input")
self.commitConfiguration()
return
}
self.addInput(deviceInput)
self.addOutput(delegate)
self.commitConfiguration()
}
func addOutput(_ delegate: AVCaptureVideoDataOutputSampleBufferDelegate) {
let videoDataOutput = AVCaptureVideoDataOutput()
let videoDataOutputQueue = DispatchQueue(label: "VideoDataOutput",
qos: .userInitiated, attributes: [],
autoreleaseFrequency: .workItem)
guard self.canAddOutput(videoDataOutput) else {
fatalError("Could not add video data output to the self")
}
self.addOutput(videoDataOutput)
videoDataOutput.alwaysDiscardsLateVideoFrames = true
videoDataOutput.videoSettings = [
kCVPixelBufferPixelFormatTypeKey as String:
Int(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)]
videoDataOutput.setSampleBufferDelegate(delegate, queue: videoDataOutputQueue)
let captureConnection = videoDataOutput.connection(with: .video)
captureConnection?.isEnabled = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment