diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-07-28 13:48:53 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-07-28 13:48:53 +0200 |
| commit | c9b9e1d654ea697afad9f6427d94623bfdf55cce (patch) | |
| tree | a0095a75f2ba625f92923e72a9831740da989de7 /Captura/Domain | |
| parent | f5d16c1c8c259966338b23afd407d142f72784aa (diff) | |
Add clipboard
Diffstat (limited to 'Captura/Domain')
| -rw-r--r-- | Captura/Domain/CapturaCaptureSession.swift | 60 | ||||
| -rw-r--r-- | Captura/Domain/CaptureState.swift | 8 |
2 files changed, 68 insertions, 0 deletions
diff --git a/Captura/Domain/CapturaCaptureSession.swift b/Captura/Domain/CapturaCaptureSession.swift new file mode 100644 index 0000000..80240e6 --- /dev/null +++ b/Captura/Domain/CapturaCaptureSession.swift @@ -0,0 +1,60 @@ +import AppKit +import AVFoundation + +class CapturaCaptureSession: AVCaptureSession, AVCaptureFileOutputRecordingDelegate, AVCaptureVideoDataOutputSampleBufferDelegate { + + let videoOutput = AVCaptureVideoDataOutput() + let movieFileOutput = AVCaptureMovieFileOutput() + var receivedFrames = false + + init(_ screen: NSScreen, box: NSRect) { + super.init() + + let displayId = screen.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as! CGDirectDisplayID + let screenInput = AVCaptureScreenInput(displayID: displayId) + screenInput?.cropRect = box.insetBy(dx: 1, dy: 1) + + if self.canAddInput(screenInput!) { + self.addInput(screenInput!) + } + + videoOutput.setSampleBufferDelegate(self, queue: Dispatch.DispatchQueue(label: "sample buffer delegate", attributes: [])) + + if self.canAddOutput(videoOutput) { + self.addOutput(videoOutput) + } + + if self.canAddOutput(movieFileOutput) { + self.addOutput(movieFileOutput) + } + } + + func startRecording() { + receivedFrames = false + self.startRunning() + + DispatchQueue.main.asyncAfter(deadline: .now() + 1) { + if !self.receivedFrames { + NotificationCenter.default.post(name: .failedToStart, object: nil, userInfo: nil) + } + } + } + + func startRecording(to url: URL) { + self.startRecording() + movieFileOutput.startRecording(to: url, recordingDelegate: self) + } + + // MARK: - AVCaptureVideoDataOutputSampleBufferDelegate Implementation + + func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { + receivedFrames = true + + guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return } + NotificationCenter.default.post(name: .receivedFrame, object: nil, userInfo: ["frame": imageBuffer]) + } + + // MARK: - AVCaptureFileOutputRecordingDelegate Implementation + + func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {} +} diff --git a/Captura/Domain/CaptureState.swift b/Captura/Domain/CaptureState.swift new file mode 100644 index 0000000..0761b88 --- /dev/null +++ b/Captura/Domain/CaptureState.swift @@ -0,0 +1,8 @@ +enum CaptureState { + case idle + case selectingArea + case recording + case uploading + case uploaded + case error +} |