Created
May 9, 2022 20:09
-
-
Save sturdysturge/9272796f8da13db632ba0dd7d097b864 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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