fix(audio): only count audio that actually reached the encoder

Second measurement of the same recording family (ty-20261112-1133-701-0000-2.mp4):

  wall clock (audio start -> audio stop)  6.477 s
  video: 386 frames                       6.455 s   correct
  audio: 294 packets                      6.252 s   226 ms SHORT

Same magnitude as the original bug, opposite sign — the clock fixed the tick-rate error and
exposed the next one underneath. Audio started 49 ms before the pump, ffmpeg connected ~80 ms
after, and every chunk written in that window was dropped (dropped writes 27). The loop still
counted those frames as produced, so the track lost exactly that much time.

Now: the clock does not start until the pipe is connected, and AudioClock.Advance counts a
frame only when its write reached the pipe. A dropped write stays owed and is paid off on the
next tick, so a start-up gap costs content (silence, which there was anyway) instead of
length.

Two measurements, two different signs, one lesson: the audio track's LENGTH has to be the
clock's, and anything that removes samples without removing time — a fast tick, a dropped
write — shows up as drift no static offset can fix.

674 tests (one notification-timing flake under full-suite load; passes alone), 0 warnings.
This commit is contained in:
2026-11-12 11:39:20 -08:00
parent 47f409f0e4
commit 102cb96b28
5 changed files with 42 additions and 9 deletions
+14 -5
View File
@@ -402,7 +402,7 @@ public sealed class AudioMixer : IDisposable
// any scheduler error into a permanent A/V rate error (measured 2026-11-12: 2.1% fast, 224 ms
// of drift in ten seconds); asking the clock how much is owed and producing exactly that keeps
// the track the same length as the show.
var clock = Stopwatch.StartNew();
Stopwatch? clock = null;
long framesProduced = 0;
var maxChunkFrames = micChunk.Length;
@@ -411,6 +411,15 @@ public sealed class AudioMixer : IDisposable
var nextTick = DateTime.UtcNow + _mixInterval;
try
{
// The clock starts when ffmpeg is actually reading. Before that there is nowhere for
// audio to go, and time spent waiting must not become a shorter track.
if (!pipe.IsConnected)
{
await Task.Delay(1, cancellationToken).ConfigureAwait(false);
continue;
}
clock ??= Stopwatch.StartNew();
var frames = AudioClock.FramesDue(
clock.Elapsed.TotalSeconds, framesProduced, OutputSampleRate, maxChunkFrames);
if (frames <= 0)
@@ -421,7 +430,6 @@ public sealed class AudioMixer : IDisposable
}
var (_, micDrained, loopDrained) = FillAndMix(micChunk, loopbackChunk, mixBuffer, frames);
framesProduced += frames;
var samples = frames * 2;
// Negative offset: skip the armed |N| ms of stream head. The pipe
@@ -436,9 +444,10 @@ public sealed class AudioMixer : IDisposable
writeFrom = drop;
}
if (writeFrom < samples)
await pipe.WriteAsync(mixBuffer.AsMemory(writeFrom, samples - writeFrom),
cancellationToken).ConfigureAwait(false);
var wrote = writeFrom >= samples
|| await pipe.WriteAsync(mixBuffer.AsMemory(writeFrom, samples - writeFrom),
cancellationToken).ConfigureAwait(false);
framesProduced = AudioClock.Advance(framesProduced, frames, wrote);
_micDrainedTotal += micDrained;
_loopDrainedTotal += loopDrained;