Skip to content

Commit

Permalink
address a couple data races
Browse files Browse the repository at this point in the history
  • Loading branch information
jcm committed Dec 12, 2023
1 parent af761a1 commit a331ad5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CaptureSample/CaptureEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ class CaptureEngineStreamOutput: NSObject, SCStreamOutput, SCStreamDelegate {
let contentScale = attachments[.contentScale] as? CGFloat,
let scaleFactor = attachments[.scaleFactor] as? CGFloat else { return nil }

let encoderFrame = self.encoder?.videoSink?.mostRecentSampleBuffer
//let encoderFrame = self.encoder?.videoSink?.mostRecentSampleBuffer
let encoderFrame: CMSampleBuffer? = nil

// Create a new frame with the relevant data.
let frame = CapturedFrame(surface: surface,
Expand Down
11 changes: 9 additions & 2 deletions CaptureSample/Encoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class VTEncoder {
var stoppingEncoding = false
var pixelTransferBuffer: CVPixelBuffer!

var hasStartedLock = NSLock()

var destWidth: Int
var destHeight: Int

Expand Down Expand Up @@ -180,10 +182,13 @@ class VTEncoder {
}

func startSession(buffer: CVImageBuffer, timeStamp: CMTime, duration: CMTime, properties: CFDictionary?, infoFlags: UnsafeMutablePointer<VTEncodeInfoFlags>?) throws {
self.hasStartedLock.lock()
guard !self.isStarting else {
self.hasStartedLock.unlock()
return
}
self.isStarting = true
self.hasStartedLock.unlock()
guard !self.criticalErrorEncountered else {
throw self.currentError!
}
Expand All @@ -205,7 +210,9 @@ class VTEncoder {
if sbuf != nil {
do {
try self.videoSink.startSession(sbuf!)
self.hasStartedLock.lock()
self.hasStarted = true
self.hasStartedLock.unlock()
self.isStarting = false
} catch {
self.currentError = error
Expand Down Expand Up @@ -249,10 +256,10 @@ class VTEncoder {
(status: OSStatus, infoFlags: VTEncodeInfoFlags, sbuf: CMSampleBuffer?) -> Void in
if sbuf != nil {
self.videoSink.sendSampleBuffer(sbuf!)
if self.decodes {
/*if self.decodes {
self.videoSink.mostRecentSampleBuffer = sbuf!
VTDecompressionSessionDecodeFrame(self.decodeSession, sampleBuffer: sbuf!, flags: [._1xRealTimePlayback], infoFlagsOut: nil, outputHandler: self.outputHandler)
}
}*/
}
}
} else {
Expand Down
5 changes: 4 additions & 1 deletion CaptureSample/VideoSink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class VideoSink {
var hasStarted = false

var mostRecentImageBuffer: CVImageBuffer?
var mostRecentSampleBuffer: CMSampleBuffer?
//var mostRecentSampleBuffer: CMSampleBuffer?

private var bookmarkedURL: URL?

Expand All @@ -33,6 +33,7 @@ public class VideoSink {
private let usesReplayBuffer: Bool
private let replayBufferDuration: Int
private var isActive: Bool
private var replayBufferLock = NSLock()

var accessingBookmarkURL = false

Expand Down Expand Up @@ -265,8 +266,10 @@ public class VideoSink {
}

func stopReplayBuffer() throws {
self.replayBufferLock.lock()
self.videoReplayBuffer = nil
self.audioReplayBuffer = nil
self.replayBufferLock.unlock()
}

/// Closes the destination movie file.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Record
Record aims to be a low-overhead macOS screen recorder that emphasizes not missing frames, color correctness and color-matching between output color spaces, and a variety of encoder options for size and quality flexibility.
Record aims to be a low-overhead macOS screen recorder that emphasizes not missing frames, color correctness, and a variety of encoder options for size and quality flexibility.

<img width="1840" alt="Screenshot 2023-08-25 at 7 58 44 PM" src="https://github.com/jcm93/Record/assets/6864788/7e683799-7ff9-4d31-8393-86c4ffa53f07">

Expand Down

0 comments on commit a331ad5

Please sign in to comment.