Skip to content

voice_mixer: a late underrun grows the target by how late the audio was - #1416

Merged
transfix merged 2 commits into
masterfrom
fix/voice-mixer-late-growth
Nov 21, 2026
Merged

transfix merged 2 commits into
masterfrom
fix/voice-mixer-late-growth

Conversation

@transfix

Copy link
Copy Markdown
Owner

What this changes

cvc::voice_mixer (inc/cvc/media/voice_mixer.h, src/cvc/media/voice_mixer.cpp): a late underrun used to grow a peer's jitter-buffer target by a fixed 20 ms. A sender whose main thread stalls 150 ms therefore got one audible gap per step while the target caught up: in #1412's headless-Chrome busy case that was 9 late underruns, from 80 to 260 ms.

Now a late underrun grows the target by how late the audio was, as the device saw it. The proposal in the workstream was max(step_s, now - underrun_at) rounded up to a step. This PR differs from it in three ways, each for a reason I measured:

  1. Growth is by the audio the device went short of, not by the time since running dry. A browser's own main-thread stalls make the clock wrong in both directions.
  2. A dry spell on quiet audio is not counted as late. Silence suppression made pauses in speech look like late audio, and growing by the pause would have inflated every target.
  3. After a late underrun, the voice restarts at its old target, not the grown one. Waiting for the grown target was a long gap of its own.

In detail:

  • The growth is max(short_by, step_s), rounded up to whole step_s steps and capped at max_delay_s. short_by counts the samples the device asked for and didn't get, from the dry pull until the audio came back. step_s = 0 still turns growth off.

  • Why not now - underrun_at. A device that pulls at its own pace (SDL's audio thread, natively) asks for audio continuously, so there the shortfall equals the time since running dry. A browser is different: its main thread pulls for the AudioWorklet ring (Conference audio in a browser: the mic and the speaker on AudioWorklets, off the main thread #1412), on each tick target - ahead. When that main thread stalls, it pulls a stall's worth at once afterwards, or nothing during the stall, and the clock misreads both. I added a temporary log line in headless Chrome under SwiftShader, and the clock and the shortfall disagreed both ways:

    • 3 ms by the clock but 56 ms short: the receiver pulled the stall's worth at once.
    • 90 ms by the clock but 1 ms short: the receiver stalled just after running dry, and the audio was there before it pulled again.

    In a first browser run, using max(time, shortfall), the clock's overestimates grew targets to 300-400 ms under SwiftShader.

  • After a late underrun the voice plays again once its old target is buffered, not the grown one (new peer::restart_s). The audio that comes back brings the delay the stall needed with it, so waiting for the rest is a gap of its own: with the shortfall alone, a second browser run had single gaps of 580 and 235 ms under SwiftShader busy. The grown target still keeps that audio from being trimmed away, and the next talk spurt starts at it. Master's 9-step ramp came from exactly that trimming: a stall's burst larger than twice the target was cut back to the target.

  • A voice that runs dry on quiet audio is not late (new params::quiet_rms, 0.008, against a 20 ms average of the peer's played level). Growth by lateness needs this. The uplink (forward_frame, the same in Conference audio: a native client's audio is off the frame loop #1408 and Conference audio in a browser: the mic and the speaker on AudioWorklets, off the main thread #1412) sends a mic chunk while its RMS is above 0.004 or within 0.8 s of one that was, then sends nothing. So a quiet stretch of 0.8 + x s reaches the receiver as a gap of x, and any x up to 300 ms (late_window_s) used to count as a late underrun. Master grew the target 20 ms for each such pause. Growing by lateness would have grown it by the whole pause.

Relaxing: kept at one step per relax_after_s

I compared three ways back down: one step per 10 s (as now), undoing the last jump after 10 s, and shrinking to the buffer's low-water mark. Simulated with 20 ms blocks and a 20 ms device and the final growth rule; dry spells, silence and mean buffered audio, mean of 5 seeds:

case master this PR (relax a step) undo the jump low-water
both main threads stall 150 ms in every 400, 60 s 9.4, 1636 ms, 130 ms 2.8, 392 ms, 130 ms 6.6, 1048 ms, 131 ms 2.8, 392 ms, 130 ms
a 200 ms sender stall every 15 s, 120 s 8.0, 1104 ms, 107 ms 2.8, 280 ms, 181 ms 8.0, 1132 ms, 150 ms 7.2, 1104 ms, 148 ms
a 200 ms sender stall every 40 s, 240 s 6.0, 908 ms, 49 ms 3.8, 424 ms, 157 ms 6.0, 976 ms, 69 ms 6.0, 988 ms, 78 ms
one 250 ms stall, then clean, 125 s 1, 160 ms, 89 ms 1, 160 ms, 157 ms 1, 160 ms, 83 ms 1, 160 ms, 47 ms

The latency trade-off. A jump sized to the stall means the buffer holds that much more audio until it relaxes. After a one-off 250 ms stall, the target goes from 80 to 240 ms and comes back at 20 ms per 10 s: 220, 180, 140, 80 and 40 ms at 20, 40, 60, 90 and 120 s. Relaxing faster gives that latency back within 10 s, but every stall that comes back after the probe is another full gap: in the recurring cases above, undoing the jump ran dry 1.6-3.0 times as often as relaxing a step at a time. Stalls that size do come back in practice (slow frames, GC, a busy tab), so I kept the slow relax. The cap stays at max_delay_s (400 ms).

Pauses in speech

A simulated speaker (talk U(0.5, 3) s, pauses lognormal with a 0.6 s median), sent with the uplink's silence rule over a clean link; 5 minutes, mean of 8:

rule late underruns target mean at 5 min
master (a step) 15.4 109 ms 122 ms
by lateness, no quiet check 10.9 320 ms 367 ms
this PR (by lateness, quiet check) 0.4 45 ms 40 ms

With 150 ms sender stalls during speech that has quiet dips between words: master had 26.6 late underruns and a 279 ms mean target; this PR has 9.0 and 296 ms.

Tests

src/cvc/tests/voice_mixer_test.cpp: 20 tests, all passing natively.

  • OnlyLateUnderrunsGrowTheTarget: the audio comes back 50 ms late, with the device 60 ms short, and the target now grows three steps (0.04 to 0.10 s); it was one step (0.06).
  • New tests:
    • OneLargeLateArrival: a 200 ms sender stall gives one late underrun and a 0.22 s target, and the next stall like it plays through.
    • ADeviceThatPullsAStallAtOnce.
    • TimeTheDeviceDidNotPullIsNotLate.
    • ALateVoicePlaysOnceItsOldTargetIsBack.
    • LateGrowthIsWholeStepsUpToTheMost.
    • APauseInSpeechIsNotLate.
    • PausesInSpeechDoNotGrowTheTarget: 2 minutes of speech under the uplink's silence rule.
    • StallingMainThreadsGrowTheTargetAtOnce. For this one, simulate() now models sender and receiver main-thread stalls.
  • The two existing simulations keep their bounds; their jitter is small next to the target, so the new rule barely changes them.
  • I ran the same test file against a scratch copy of the mixer with each rule (20 tests). Master's 20 ms step fails 8. The time-only rule fails 6. max(time, shortfall), without the quiet check or the restart, fails 4. This PR without the restart fails 1.

Browser measurement (src/arigl/web/test/arigl_web_audio.mjs)

Headless Chrome 152 on prettyhatemachine, worklet mode, 30 s a case. I built #1412's head (09e86e0) twice: "before" as it is, and "after" with this commit applied. The two builds alternated, with the order flipped on trial 2, over 3 trials (2026-11-20, 19:32-20:06, 1-minute load 3-15). I ran it with --gpu (ANGLE on the GTX 1650) and without (SwiftShader, where each arigl frame is a ~100 ms long task). Each cell lists the three trials:

Case Before: heard; gaps (total ms) After: heard; gaps (total ms) Late underruns, before → after
SwiftShader, busy 95.3, 97.0, 95.5%; 8, 9, 5 gaps (1420, 890, 1350 ms; longest 455) 99.7, 99.9, 98.2%; 5, 3, 6 gaps (80, 30, 550 ms; longest 435) 10, 13, 10 → 7, 4, 5
SwiftShader, idle 99.1, 99.7, 99.5%; 6, 4, 4 (260, 95, 155 ms) 99.4, 99.1, 99.7%; 4, 3, 4 (175, 265, 105 ms) 6, 6, 7 → 4, 3, 4
SwiftShader, sender hidden 98.7, 100, 99.9%; 7, 2, 2 (380, 15, 25 ms) 99.9, 99.9, 99.9%; 3, 3, 2 (20, 30, 20 ms) 6, 5, 5 → 2, 2, 4
SwiftShader, receiver hidden 99.7, 99.3, 99.5%; 3, 4, 3 (95, 205, 140 ms) 99.6, 100, 99.6%; 4, 1, 5 (135, 10, 120 ms) 5, 6, 5 → 3, 1, 3
GPU, busy 99.7, 99.4, 99.3%; 6, 6, 6 (85, 180, 220 ms) 99.4, 99.8, 99.7%; 6, 5, 5 (195, 50, 105 ms) 5, 7, 5 → 2, 3, 4
GPU, idle 99.8, 99.9, 99.8% 99.8, 99.8, 100% 1, 1, 2 → 2, 1, 0
GPU, sender hidden 99.9, 99.9, 99.7% 99.9, 99.9, 99.9% 0, 1, 1 → 2, 2, 1
GPU, receiver hidden 99.8, 99.9, 100% 99.9, 99.9, 100% 1, 1, 0 → 1, 1, 0

Latency. The test measures no mouth-to-ear delay. What I report is the receiver's own log: the jitter buffer's target at its last 10 s line, and what the playback ring plays ahead.

  • GPU, busy: targets 160, 200, 140 ms before against 120, 160, 240 ms after.
  • SwiftShader, busy: 280, 320, 260 ms against 320, 280, 300 ms.
  • SwiftShader, receiver hidden: 140, 160, 140 ms against 200, 180, 180 ms. This is the largest rise, 40-60 ms.
  • Idle and the other hidden cases: about the same.
  • The ring plays 160-400 ms ahead in both builds.

Caveats.

  • Run-to-run spread is large. Over today's seven "before" runs of busy SwiftShader, heard ranged from 95.3% to 99.8%. Three trials cannot resolve differences under about half a percent; on the GPU, where everything is at 99.3-100%, the change is within that noise.

  • Two earlier versions of this change ran the same matrix with 2 trials each; their logs are not in the repo:

    • max(time, shortfall) without the restart: SwiftShader targets reached 300-400 ms, and single gaps reached 300 ms.
    • The shortfall alone without the restart: SwiftShader busy went to 97.3% and 98.0%, with a 580 ms gap.

    Those two runs led to the shortfall-only rule and the restart at the old target.

  • Not verified: real mics and ears, other browsers, and the native client's end to end with this change. Its unit tests and simulations cover it, with SDL's pull pace, where the shortfall equals the time.

Docs

docs/roadmap/ARIADNE_CONFERENCE_ROADMAP.md §5j: the "Jitter buffers" bullet, rewritten for the new rule.

The "Measured" bullet under "Browser audio off the main thread" comes with #1412, so it is not on master yet. It ends by saying that growing the target by how late the audio was "would take one or two" late underruns. Once #1412 lands, I'll rebase this branch, replace that sentence with the measurement above, and resolve any §5j conflict.

Merge order

This should merge after #1412. The code doesn't depend on it, since it touches only voice_mixer.{h,cpp}, its test and the roadmap. But the measurement is against #1412's AudioWorklet path, and the "Measured" edit above needs #1412's text. #1408 doesn't touch these files either.

A late underrun grew a peer's jitter-buffer target a fixed 20 ms, so a sender
whose main thread stalls 150 ms took a gap per step to catch up (9 late
underruns, 80 to 260 ms, in headless Chrome). Now it grows by how late the
audio was to the device: the audio the device asked for from running dry to the
audio's return and did not get, at least a step, rounded up to whole steps,
capped at max_delay_s. For a device pulling at its own pace that is the time
since; a receiver whose own main thread stalled pulls a stall's worth at once
after it, or nothing during it, and the time alone reads those wrong (measured
in headless Chrome under SwiftShader: 90 ms by the clock against 1 ms short,
and 3 ms against 56).

After a late underrun the voice plays again once what it waited for before is
buffered, not the grown target. The audio that comes back brings the delay the
stall needed; waiting for the rest was a gap of its own (up to 580 ms in one,
in headless Chrome). The grown target keeps that audio from being trimmed, and
the next talk spurt starts at it.

A voice that runs dry on quiet audio (below quiet_rms, 0.008) is not late: a
conference sender sends nothing through a silence after 0.8 s of its mic below
0.004, and the next talk spurt can come within late_window_s. Counted late, a
pause grew the target 20 ms, and by its length would take it to the most.

Relaxing stays one step per relax_after_s, so a stall that recurs still finds
the buffer big enough; the delay a one-off stall added lingers longer.

The tests: OnlyLateUnderrunsGrowTheTarget expects three steps for a device that
went 60 ms short; new tests for one large late arrival, a device that pulls a
stall's worth at once, one that pulled nothing while dry, a late voice playing
again at its old target, whole steps up to max_delay_s, a pause in speech, and
two minutes of speech sent with the uplink's silence rule; the simulation gains
sender and receiver main-thread stalls, and a test where both stall.
@transfix
transfix enabled auto-merge (squash) November 21, 2026 05:16
@transfix
transfix merged commit 92a2b0e into master Nov 21, 2026
29 checks passed
@transfix
transfix deleted the fix/voice-mixer-late-growth branch November 21, 2026 05:58
transfix added a commit that referenced this pull request Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant