Skip to content

Create polished demo videos without editing skills. Mac/Windows/Linux

Keep audio finalization alive while encoding progresses

webadderall9d7285a118cf
diff --git a/electron/native/AudioSampleQueue.test.ts b/electron/native/AudioSampleQueue.test.ts
index f43989a1..420608af 100644
--- a/electron/native/AudioSampleQueue.test.ts
+++ b/electron/native/AudioSampleQueue.test.ts
@@ -115,11 +115,18 @@ Task {
let terminalFailure = await terminal.finish(at: at(0.01), timeout: 0) != nil
var writes = 0
let longGap = AudioSampleQueue(isReady: { true }, canWrite: { true }, append: { _ in writes += 1; return true })
- longGap.enqueue(tone(index: 0), at: at(1800))
+ longGap.enqueue(tone(index: 0), at: at(10))
precondition(writes <= 16)
- let longGapError = await longGap.finish(at: at(1800), timeout: 0)
- precondition(longGapError != nil)
- precondition(writes <= 32)
+ let longGapError = await longGap.finish(at: at(10), timeout: 0)
+ precondition(longGapError == nil, "Progressing gap restoration must not time out")
+ precondition(longGap.endTime == at(10))
+ var clipReady = false
+ var clippedFrames = 0
+ let clipped = AudioSampleQueue(isReady: { clipReady }, canWrite: { true }, append: { clippedFrames += $0.numSamples; return true })
+ for index in 0..<10 { clipped.enqueue(tone(index: index), at: at(Double(index) / 100)) }
+ clipReady = true
+ let clipError = await clipped.finish(at: at(0.055), timeout: 0)
+ precondition(clipError == nil && clippedFrames == 2640, "Queued audio must stop at the final media time")
print("{\"tracks\":{" + entries.joined(separator: ",") + "},\"pressure\":{\"dropped\":\(pressure.droppedBuffers),\"peakBytes\":\(pressure.peakQueuedBytes),\"peakSeconds\":\(pressure.peakQueuedDuration),\"failedFinish\":\(failedFinish),\"terminalFailure\":\(terminalFailure)}}")
done.signal()
}
diff --git a/electron/native/ScreenCaptureKitRecorder.swift b/electron/native/ScreenCaptureKitRecorder.swift
index 9bc551d3..52f9b20a 100644
--- a/electron/native/ScreenCaptureKitRecorder.swift
+++ b/electron/native/ScreenCaptureKitRecorder.swift
@@ -90,9 +90,11 @@ final class AudioSampleQueue {
/// Pads the end too: a final dropped buffer has no later timestamp to reveal its gap.
func finish(at targetTime: CMTime, timeout: TimeInterval = 5.0) async -> Error? {
- let deadline = ProcessInfo.processInfo.systemUptime + timeout
+ var deadline = ProcessInfo.processInfo.systemUptime + timeout
while failure == nil {
- drain()
+ if Task.isCancelled { failure = CancellationError(); break }
+ let previousEnd = endTime
+ drain(until: targetTime)
if pending.isEmpty {
if let format = lastFormat {
fillSilence(to: targetTime, format: format)
@@ -101,6 +103,12 @@ final class AudioSampleQueue {
break
}
}
+ if endTime > previousEnd {
+ // Timeout means stalled, not successfully restoring a long quiet interval.
+ deadline = ProcessInfo.processInfo.systemUptime + timeout
+ await Task.yield()
+ continue
+ }
if ProcessInfo.processInfo.systemUptime >= deadline {
failure = audioError("Audio encoder did not drain before finalization")
break
@@ -114,7 +122,7 @@ final class AudioSampleQueue {
return failure
}
- private func drain() {
+ private func drain(until limit: CMTime? = nil) {
guard failure == nil else { return }
guard canWrite() else {
failure = audioError("Audio writer stopped accepting data")
@@ -123,7 +131,12 @@ final class AudioSampleQueue {
queuedBytes = 0
return
}
- while let next = pending.first, isReady(), failure == nil {
+ while let next = pending.first, failure == nil {
+ if let limit, next.time >= limit || endTime >= limit {
+ removeFirst()
+ continue
+ }
+ guard isReady() else { return }
guard let format = next.buffer.formatDescription,
let asbd = format.audioStreamBasicDescription else {
failure = audioError("Missing audio format")
@@ -135,12 +148,13 @@ final class AudioSampleQueue {
// Trim overlapping or repeated samples instead of writing time twice.
let overlap = Int(min(Double(next.buffer.numSamples), max(0, ((endTime - next.time).seconds * asbd.mSampleRate).rounded())))
let skip = overlap <= 1 ? 0 : overlap // CMTime(seconds:) can round adjacent timestamps by one frame.
- if skip < next.buffer.numSamples {
+ let remainingFrames = next.buffer.numSamples - skip
+ let count = limit.map { Int(min(Double(remainingFrames), max(0, (($0 - endTime).seconds * asbd.mSampleRate).rounded()))) } ?? remainingFrames
+ if count > 0 {
do {
let buffer: CMSampleBuffer
- if skip > 0 {
+ if skip > 0 || count < next.buffer.numSamples {
let pcmFormat = AVAudioFormat(cmAudioFormatDescription: format)
- let count = next.buffer.numSamples - skip
guard let pcm = AVAudioPCMBuffer(pcmFormat: pcmFormat, frameCapacity: AVAudioFrameCount(count)) else {
throw audioError("Cannot allocate trimmed audio")
}
@@ -155,12 +169,16 @@ final class AudioSampleQueue {
endTime = endTime + CMTime(value: Int64(buffer.numSamples), timescale: rate)
} catch { failure = error; return }
}
- pending.removeFirst()
- queuedDuration = max(0, queuedDuration - next.duration)
- queuedBytes -= next.bytes
+ removeFirst()
}
}
+ private func removeFirst() {
+ let next = pending.removeFirst()
+ queuedDuration = max(0, queuedDuration - next.duration)
+ queuedBytes -= next.bytes
+ }
+
@discardableResult
private func fillSilence(to target: CMTime, format: CMFormatDescription) -> Bool {
guard let asbd = format.audioStreamBasicDescription else { return false }