4 class CapturaCaptureSession: AVCaptureSession, AVCaptureFileOutputRecordingDelegate,
5 AVCaptureVideoDataOutputSampleBufferDelegate
8 let videoOutput = AVCaptureVideoDataOutput()
9 let movieFileOutput = AVCaptureMovieFileOutput()
10 var receivedFrames = false
12 init(_ screen: NSScreen, box: NSRect) {
16 screen.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as! CGDirectDisplayID
17 let screenInput = AVCaptureScreenInput(displayID: displayId)
18 var croppingBox = NSOffsetRect(box, -screen.frame.origin.x, -screen.frame.origin.y)
19 if croppingBox.width.truncatingRemainder(dividingBy: 2) != 0 {
20 croppingBox.size.width -= 1
22 screenInput?.cropRect = croppingBox.insetBy(dx: 1, dy: 1)
24 if self.canAddInput(screenInput!) {
25 self.addInput(screenInput!)
28 videoOutput.setSampleBufferDelegate(
29 self, queue: Dispatch.DispatchQueue(label: "sample buffer delegate", attributes: []))
31 if self.canAddOutput(videoOutput) {
32 self.addOutput(videoOutput)
35 if self.canAddOutput(movieFileOutput) {
36 self.addOutput(movieFileOutput)
40 func startRecording() {
41 receivedFrames = false
44 DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
45 if !self.receivedFrames {
46 NotificationCenter.default.post(name: .failedToStart, object: nil, userInfo: nil)
51 func startRecording(to url: URL) {
53 movieFileOutput.startRecording(to: url, recordingDelegate: self)
56 // MARK: - AVCaptureVideoDataOutputSampleBufferDelegate Implementation
59 _ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer,
60 from connection: AVCaptureConnection
64 guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
65 NotificationCenter.default.post(
66 name: .receivedFrame, object: nil, userInfo: ["frame": imageBuffer])
69 // MARK: - AVCaptureFileOutputRecordingDelegate Implementation
72 _ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL,
73 from connections: [AVCaptureConnection], error: Error?