diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2024-09-16 11:07:46 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2024-09-16 11:07:46 +0200 |
| commit | 505c1e620497828ffb914e05dd76d9ab124f144a (patch) | |
| tree | 0d93162c4228dbee9e29c35e9f97f58daa5e3eef /Captura/Domain | |
| parent | 5802c153cae64142d84e3cd5f762939501ee7e53 (diff) | |
Format the code
Diffstat (limited to 'Captura/Domain')
| -rw-r--r-- | Captura/Domain/CapturaCaptureSession.swift | 53 | ||||
| -rw-r--r-- | Captura/Domain/CaptureSessionConfiguration.swift | 14 |
2 files changed, 39 insertions, 28 deletions
diff --git a/Captura/Domain/CapturaCaptureSession.swift b/Captura/Domain/CapturaCaptureSession.swift index 8943282..dbc72b9 100644 --- a/Captura/Domain/CapturaCaptureSession.swift +++ b/Captura/Domain/CapturaCaptureSession.swift @@ -1,64 +1,75 @@ -import AppKit import AVFoundation +import AppKit -class CapturaCaptureSession: AVCaptureSession, AVCaptureFileOutputRecordingDelegate, AVCaptureVideoDataOutputSampleBufferDelegate { +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 displayId = + screen.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as! CGDirectDisplayID let screenInput = AVCaptureScreenInput(displayID: displayId) var croppingBox = NSOffsetRect(box, -screen.frame.origin.x, -screen.frame.origin.y) if croppingBox.width.truncatingRemainder(dividingBy: 2) != 0 { croppingBox.size.width -= 1 } screenInput?.cropRect = croppingBox.insetBy(dx: 1, dy: 1) - + if self.canAddInput(screenInput!) { self.addInput(screenInput!) } - - videoOutput.setSampleBufferDelegate(self, queue: Dispatch.DispatchQueue(label: "sample buffer delegate", attributes: [])) - + + 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) { + + 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]) + 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?) {} + + func fileOutput( + _ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, + from connections: [AVCaptureConnection], error: Error? + ) {} } diff --git a/Captura/Domain/CaptureSessionConfiguration.swift b/Captura/Domain/CaptureSessionConfiguration.swift index c2ebe75..673db5f 100644 --- a/Captura/Domain/CaptureSessionConfiguration.swift +++ b/Captura/Domain/CaptureSessionConfiguration.swift @@ -14,7 +14,7 @@ struct CaptureSessionConfiguration { let preventResize: Bool let autoStart: Bool let maxLength: Int - + init( frameRate: Int? = nil, outputFormats: OutputFormatSetting? = nil, @@ -36,7 +36,7 @@ struct CaptureSessionConfiguration { autoStart = false maxLength = 300 } - + init(from action: RecordAction) { self.frameRate = action.fps ?? CapturaSettings.frameRate self.outputFormats = action.outputs ?? CapturaSettings.outputFormats @@ -56,23 +56,23 @@ struct CaptureSessionConfiguration { autoStart = action.autoStart ?? false maxLength = action.maxLength ?? 300 } - + var shouldSaveMp4: Bool { outputFormats.shouldSaveMp4() || (shouldUseBackend && shouldUploadMp4) } - + var shouldSaveGif: Bool { outputFormats.shouldSaveGif() || (shouldUseBackend && shouldUploadGif) } - + var shouldUploadGif: Bool { backendFormat.shouldSaveGif() } - + var shouldUploadMp4: Bool { backendFormat.shouldSaveMp4() } - + var shouldUseBackend: Bool { backend != nil } |