Skip to content

Commit 549f13d

Browse files
committed
fix(audio): keep a sluggish queue from holding a dead source inside the grace
Each pass still counts the 100 ms threshold as running time, so passes that are always a little over it apart can no longer keep a silent source alive. Also cover the ingest-first order of the blocked-queue test.
1 parent 243ac27 commit 549f13d

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

‎electron/native/screencapturekit/Sources/OpenScreenCaptureCore/AudioTrackMixer.swift‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,19 @@ public final class AudioTrackMixer {
216216
/// whole time; it is the mixer that was not listening. So a pass that comes much later than
217217
/// the ticker allows moves each source's last delivery forward by the gap, which measures
218218
/// the grace against the time the queue was actually running.
219+
///
220+
/// Every pass still counts `queueStallFrames` as running time, so a queue that is merely
221+
/// sluggish — passes always a little over the threshold apart — cannot keep a dead source
222+
/// inside the grace forever: it stalls after a few passes and the track keeps moving.
219223
private func discountQueueStall(until now: CMTime) {
220224
guard let anchor, now.isValid, now.isNumeric else {
221225
return
222226
}
223227
let nowFrame = frameIndex(of: now, from: anchor)
224228
if let lastPassFrame, nowFrame - lastPassFrame > Int64(MixFormat.queueStallFrames) {
225-
let gap = nowFrame - lastPassFrame
229+
let stall = nowFrame - lastPassFrame - Int64(MixFormat.queueStallFrames)
226230
for index in sources.indices {
227-
sources[index].lastDeliveryFrame += gap
231+
sources[index].lastDeliveryFrame += stall
228232
}
229233
}
230234
lastPassFrame = nowFrame

‎electron/native/screencapturekit/Tests/OpenScreenCaptureCoreTests/AudioTrackMixerTests.swift‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,40 @@ final class AudioTrackMixerTests: XCTestCase {
399399
/// clock alone, both sources would look a second quiet, the cursor would run to now in
400400
/// silence, and the queued second would then arrive behind it and be dropped.
401401
func testABlockedQueueDoesNotTurnQueuedAudioIntoSilence() {
402+
assertBlockedQueueKeepsQueuedAudio(tickFirst: true)
403+
}
404+
405+
/// The same stall with a queued buffer first in line instead of the ticker.
406+
func testABlockedQueueDoesNotTurnQueuedAudioIntoSilenceWhenABufferIsFirst() {
407+
assertBlockedQueueKeepsQueuedAudio(tickFirst: false)
408+
}
409+
410+
/// A sluggish queue must not keep a dead source alive: passes 150 ms apart, just over the
411+
/// stall threshold, still let a microphone that stopped be written off mid-take.
412+
func testASluggishQueueStillWritesOffADeadSource() {
413+
let sink = RecordingSink()
414+
let clock = TestClock(.zero)
415+
let mixer = makeMixer(sink: sink, clock: clock, includesMicrophone: true, microphoneGain: 1)
416+
mixer.beginTimeline(at: clock.now)
417+
418+
mixer.ingest(
419+
makeSourceBuffer(burst(seconds: 0.15, left: 0, right: 0.5), at: clock.now, nonInterleaved: false),
420+
from: .microphone
421+
)
422+
for _ in 0..<20 {
423+
mixer.ingest(
424+
makeSourceBuffer(burst(seconds: 0.15, left: 0.5, right: 0), at: clock.now, nonInterleaved: true),
425+
from: .system
426+
)
427+
clock.advance(seconds: 0.15)
428+
mixer.tick()
429+
}
430+
431+
// Before `finish`: the track has to have kept moving without the microphone.
432+
XCTAssertGreaterThan(seconds(ofFrames: mixedFrames(sink)), 2.0)
433+
}
434+
435+
private func assertBlockedQueueKeepsQueuedAudio(tickFirst: Bool) {
402436
let sink = RecordingSink()
403437
let clock = TestClock(.zero)
404438
let mixer = makeMixer(sink: sink, clock: clock, includesMicrophone: true, microphoneGain: 1)
@@ -424,7 +458,9 @@ final class AudioTrackMixerTests: XCTestCase {
424458

425459
// The queue is blocked for a second: no tick, no buffer, while the clock runs on.
426460
clock.advance(seconds: 1)
427-
mixer.tick()
461+
if tickFirst {
462+
mixer.tick()
463+
}
428464
for _ in 0..<100 {
429465
deliverBoth(seconds: 0.01)
430466
}

0 commit comments

Comments
 (0)