Last active
August 23, 2022 15:06
-
-
Save hietalajulius/7d073f07f43783e95e53ae45103a2da1 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
func setupCapture() { | |
var deviceInput: AVCaptureDeviceInput! | |
let videoDevice = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: .back).devices.first | |
do { | |
deviceInput = try AVCaptureDeviceInput(device: videoDevice!) | |
} catch { | |
print("Could not create video device input: \(error)") | |
return | |
} | |
session.beginConfiguration() | |
session.sessionPreset = .vga640x480 | |
guard session.canAddInput(deviceInput) else { | |
print("Could not add video device input to the session") | |
session.commitConfiguration() | |
return | |
} | |
session.addInput(deviceInput) | |
do { | |
try videoDevice!.lockForConfiguration() | |
let dimensions = CMVideoFormatDescriptionGetDimensions((videoDevice?.activeFormat.formatDescription)!) | |
bufferSize.width = CGFloat(dimensions.width) | |
bufferSize.height = CGFloat(dimensions.height) | |
videoDevice!.unlockForConfiguration() | |
} catch { | |
print(error) | |
} | |
session.commitConfiguration() | |
} | |
func setupOutput() { | |
let videoDataOutput = AVCaptureVideoDataOutput() | |
let videoDataOutputQueue = DispatchQueue(label: "VideoDataOutput", qos: .userInitiated, attributes: [], autoreleaseFrequency: .workItem) | |
if session.canAddOutput(videoDataOutput) { | |
session.addOutput(videoDataOutput) | |
videoDataOutput.alwaysDiscardsLateVideoFrames = true | |
videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)] | |
videoDataOutput.setSampleBufferDelegate(self, queue: videoDataOutputQueue) | |
} else { | |
print("Could not add video data output to the session") | |
session.commitConfiguration() | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment