Skip to content

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

fix(audio): credit all but one ticker period of a queue stall

EtienneLescotd12d0071c555
diff --git a/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/AudioTrackMixer.swift b/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/AudioTrackMixer.swift
index 71bba9457..e0f3b1785 100644
--- a/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/AudioTrackMixer.swift
+++ b/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/AudioTrackMixer.swift
@@ -217,16 +217,19 @@ public final class AudioTrackMixer {
/// the ticker allows moves each source's last delivery forward by the gap, which measures
/// the grace against the time the queue was actually running.
///
- /// Every pass still counts `queueStallFrames` as running time, so a queue that is merely
+ /// Every pass still counts one ticker period as running time, so a queue that is merely
/// sluggish — passes always a little over the threshold apart — cannot keep a dead source
- /// inside the grace forever: it stalls after a few passes and the track keeps moving.
+ /// inside the grace forever: it stalls after at most `emissionGraceFrames / chunkFrames`
+ /// passes and the track keeps moving. One period and not the whole threshold, because
+ /// whatever is not credited is grace taken from a live source on every block.
private func discountQueueStall(until now: CMTime) {
guard let anchor, now.isValid, now.isNumeric else {
return
}
let nowFrame = frameIndex(of: now, from: anchor)
if let lastPassFrame, nowFrame - lastPassFrame > Int64(MixFormat.queueStallFrames) {
- let stall = nowFrame - lastPassFrame - Int64(MixFormat.queueStallFrames)
+ // The ticker period is the chunk size: 10 ms.
+ let stall = nowFrame - lastPassFrame - Int64(MixFormat.chunkFrames)
for index in sources.indices {
sources[index].lastDeliveryFrame += stall
}
diff --git a/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/AudioTrackMixerTests.swift b/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/AudioTrackMixerTests.swift
index 1df1d8d88..69da3d841 100644
--- a/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/AudioTrackMixerTests.swift
+++ b/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/AudioTrackMixerTests.swift
@@ -419,7 +419,9 @@ final class AudioTrackMixerTests: XCTestCase {
makeSourceBuffer(burst(seconds: 0.15, left: 0, right: 0.5), at: clock.now, nonInterleaved: false),
from: .microphone
)
- for _ in 0..<20 {
+ // 40 passes, 6 s: each credits 10 ms of running time, so the 250 ms grace runs out by
+ // the 26th and the microphone is written off with seconds of take still to go.
+ for _ in 0..<40 {
mixer.ingest(
makeSourceBuffer(burst(seconds: 0.15, left: 0.5, right: 0), at: clock.now, nonInterleaved: true),
from: .system
@@ -429,7 +431,37 @@ final class AudioTrackMixerTests: XCTestCase {
}
// Before `finish`: the track has to have kept moving without the microphone.
- XCTAssertGreaterThan(seconds(ofFrames: mixedFrames(sink)), 2.0)
+ XCTAssertGreaterThan(seconds(ofFrames: mixedFrames(sink)), 5.0)
+ }
+
+ /// A block must not eat into the grace of a source that is late but alive: here 200 ms
+ /// behind, within the 250 ms grace, when the queue blocks for a second.
+ func testABlockedQueueKeepsTheGraceOfALateSource() {
+ let sink = RecordingSink()
+ let clock = TestClock(.zero)
+ let mixer = makeMixer(sink: sink, clock: clock, includesMicrophone: false)
+ mixer.beginTimeline(at: clock.now)
+
+ mixer.ingest(
+ makeSourceBuffer(burst(seconds: 0.2, left: 0.5, right: 0.5), at: .zero, nonInterleaved: true),
+ from: .system
+ )
+ runTicker(mixer, clock, seconds: 0.2)
+ clock.advance(seconds: 1)
+ mixer.tick()
+ mixer.ingest(
+ makeSourceBuffer(
+ burst(seconds: 1, left: 0.5, right: 0.5),
+ at: CMTime(seconds: 0.2, preferredTimescale: 48_000),
+ nonInterleaved: true
+ ),
+ from: .system
+ )
+ runTicker(mixer, clock, seconds: 0.1)
+ mixer.finish(atSourceTime: clock.now)
+
+ XCTAssertEqual(mixer.deliveryReport(for: .system).droppedSeconds, 0, accuracy: 0.0001)
+ XCTAssertGreaterThan(peak(mixedFrames(sink), from: 0.2, to: 1.2), audibleThreshold)
}
private func assertBlockedQueueKeepsQueuedAudio(tickFirst: Bool) {