Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

docs(macos): state the timestamp gate's measured bounds, not assumed ones

Etienne Lescot2134b6bb3ffe
diff --git a/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/VideoTimestampGate.swift b/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/VideoTimestampGate.swift
index cdbf57b86..d02445668 100644
--- a/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/VideoTimestampGate.swift
+++ b/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/VideoTimestampGate.swift
@@ -20,15 +20,17 @@ import CoreMedia
/// Pause/resume. The helper measures a pause on the host clock and shifts every later sample
/// back by it, but a ScreenCaptureKit frame's presentation time is not the instant it was
/// delivered: it runs a few milliseconds ahead of the host clock, by an amount that varies from
-/// frame to frame (median 4.8 ms, spread about 20 ms). When the last frame before a pause led
-/// the clock by more than the first frame after it does, subtracting the exact pause length
-/// lands the new frame just behind the old one. Over 200 real pause/resume cycles at 1080p60 on
-/// an M1 (macOS 26.5), 6 did, by 0.2 to 1.9 ms. Every one was captured after resume, so gating
-/// frames on when they were captured would not have caught them.
+/// frame to frame (median 4.8 ms, spread about 20 ms). Subtracting the exact pause length lands
+/// the first frame after a resume behind the last one before it when that lead drops, between
+/// the two frames, by more than the delivery gaps on either side of the pause add up to. Over
+/// 200 real pause/resume cycles at 1080p60 on an M1 (macOS 26.5), 6 resumes did, by 0.2 to
+/// 1.9 ms; 7 did at 4K60. Every one was captured after resume, so gating frames on when they
+/// were captured would not have caught them.
///
-/// Dropping the frame is the whole fix for that case, because the overlap is always less than
-/// one frame interval: the next frame is already past it. Audio is not involved — its track is
-/// clocked by `AudioTrackMixer`, and the writer accepts audio that steps backwards.
+/// The gate refuses frames until time moves past the last one the writer received, however many
+/// that takes. Every overlap measured was under 2 ms, so in practice that has been a single frame
+/// at 60 fps. Audio is not involved — its track is clocked by `AudioTrackMixer`, and the writer
+/// accepts audio that steps backwards.
///
/// The gate compares in the samples' own time base. Two frames closer together than the track's
/// 1/600 s media timescale are accepted and re-spaced by the writer, so there is no rounding to
diff --git a/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift b/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift
index 4b586bb77..9d549c724 100644
--- a/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift
+++ b/electron/native/screencapturekit/Sources/OpenScreenScreenCaptureKitHelper/ScreenCaptureRecorder.swift
@@ -359,9 +359,10 @@ final class ScreenCaptureRecorder: NSObject, SCStreamOutput, SCStreamDelegate {
)
}
- /// Once per take, with the numbers that tell the cause apart: a refusal right after a resume
- /// with a sub-frame overlap is the pause shift; anything else is a source handing over time
- /// that goes backwards, which nothing here has observed yet and is worth a report.
+ /// Once per take, with the numbers that tell the cause apart. A refusal with a non-zero pause
+ /// offset, a few milliseconds behind the previous frame, is the pause shift measured in
+ /// `VideoTimestampGate`. One with no pause offset at all is a source handing over time that
+ /// goes backwards, which nothing here has observed yet and is worth a report.
private func reportRefusedVideoFrame(_ presentationTime: CMTime, previous: CMTime?, pauseOffset: CMTime) {
guard videoTimestampGate.rejectedCount == 1 else {
return
diff --git a/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/VideoTimestampGateTests.swift b/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/VideoTimestampGateTests.swift
index 8aab16921..e5d480160 100644
--- a/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/VideoTimestampGateTests.swift
+++ b/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/VideoTimestampGateTests.swift
@@ -37,12 +37,29 @@ final class VideoTimestampGateTests: XCTestCase {
let firstAfterResume = CMTimeSubtract(seconds(2420.5588043749999), seconds(0.93589941600000004))
XCTAssertEqual(gate.check(firstAfterResume), .notAfterPrevious(previous: lastBeforePause))
- // The overlap is less than one frame, so the frame after it is already clear.
+ // This overlap was under one frame, so the frame after it is already clear.
let next = CMTimeSubtract(seconds(2420.5588043749999 + 1.0 / 60), seconds(0.93589941600000004))
XCTAssertEqual(gate.check(next), .admit)
XCTAssertEqual(gate.rejectedCount, 1)
}
+ /// Nothing bounds the overlap to one frame, so the gate has to keep refusing until time moves
+ /// past the last frame the writer received, and then let the stream through again.
+ func testOverlapLongerThanOneFrameIsRefusedUntilTimeAdvances() {
+ var gate = VideoTimestampGate()
+ let lastBeforePause = seconds(100)
+ gate.record(lastBeforePause)
+
+ let frame = 1.0 / 60
+ let first = seconds(100 - 1.5 * frame)
+ let second = seconds(100 - 0.5 * frame)
+ let third = seconds(100 + 0.5 * frame)
+ XCTAssertEqual(gate.check(first), .notAfterPrevious(previous: lastBeforePause))
+ XCTAssertEqual(gate.check(second), .notAfterPrevious(previous: lastBeforePause))
+ XCTAssertEqual(gate.check(third), .admit)
+ XCTAssertEqual(gate.rejectedCount, 2)
+ }
+
func testDuplicateTimestampIsRefused() {
var gate = VideoTimestampGate()
gate.record(seconds(5))