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): keep a sluggish queue from holding a dead source inside the grace

EtienneLescot549f13da021c
diff --git a/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/AudioTrackMixer.swift b/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/AudioTrackMixer.swift
index 85331e7c7..71bba9457 100644
--- a/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/AudioTrackMixer.swift
+++ b/electron/native/screencapturekit/Sources/OpenScreenCaptureCore/AudioTrackMixer.swift
@@ -216,15 +216,19 @@ public final class AudioTrackMixer {
/// whole time; it is the mixer that was not listening. So a pass that comes much later than
/// 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
+ /// 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.
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 gap = nowFrame - lastPassFrame
+ let stall = nowFrame - lastPassFrame - Int64(MixFormat.queueStallFrames)
for index in sources.indices {
- sources[index].lastDeliveryFrame += gap
+ sources[index].lastDeliveryFrame += stall
}
}
lastPassFrame = nowFrame
diff --git a/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/AudioTrackMixerTests.swift b/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/AudioTrackMixerTests.swift
index 9a2a686ad..1df1d8d88 100644
--- a/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/AudioTrackMixerTests.swift
+++ b/electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/AudioTrackMixerTests.swift
@@ -399,6 +399,40 @@ final class AudioTrackMixerTests: XCTestCase {
/// clock alone, both sources would look a second quiet, the cursor would run to now in
/// silence, and the queued second would then arrive behind it and be dropped.
func testABlockedQueueDoesNotTurnQueuedAudioIntoSilence() {
+ assertBlockedQueueKeepsQueuedAudio(tickFirst: true)
+ }
+
+ /// The same stall with a queued buffer first in line instead of the ticker.
+ func testABlockedQueueDoesNotTurnQueuedAudioIntoSilenceWhenABufferIsFirst() {
+ assertBlockedQueueKeepsQueuedAudio(tickFirst: false)
+ }
+
+ /// A sluggish queue must not keep a dead source alive: passes 150 ms apart, just over the
+ /// stall threshold, still let a microphone that stopped be written off mid-take.
+ func testASluggishQueueStillWritesOffADeadSource() {
+ let sink = RecordingSink()
+ let clock = TestClock(.zero)
+ let mixer = makeMixer(sink: sink, clock: clock, includesMicrophone: true, microphoneGain: 1)
+ mixer.beginTimeline(at: clock.now)
+
+ mixer.ingest(
+ makeSourceBuffer(burst(seconds: 0.15, left: 0, right: 0.5), at: clock.now, nonInterleaved: false),
+ from: .microphone
+ )
+ for _ in 0..<20 {
+ mixer.ingest(
+ makeSourceBuffer(burst(seconds: 0.15, left: 0.5, right: 0), at: clock.now, nonInterleaved: true),
+ from: .system
+ )
+ clock.advance(seconds: 0.15)
+ mixer.tick()
+ }
+
+ // Before `finish`: the track has to have kept moving without the microphone.
+ XCTAssertGreaterThan(seconds(ofFrames: mixedFrames(sink)), 2.0)
+ }
+
+ private func assertBlockedQueueKeepsQueuedAudio(tickFirst: Bool) {
let sink = RecordingSink()
let clock = TestClock(.zero)
let mixer = makeMixer(sink: sink, clock: clock, includesMicrophone: true, microphoneGain: 1)
@@ -424,7 +458,9 @@ final class AudioTrackMixerTests: XCTestCase {
// The queue is blocked for a second: no tick, no buffer, while the clock runs on.
clock.advance(seconds: 1)
- mixer.tick()
+ if tickFirst {
+ mixer.tick()
+ }
for _ in 0..<100 {
deliverBoth(seconds: 0.01)
}