Skip to content

Conference audio: a jitter buffer per peer, pulled by the device; the mic drains its backlog - #1398

Merged
transfix merged 1 commit into
masterfrom
fix/conference-audio
Nov 20, 2026
Merged

transfix merged 1 commit into
masterfrom
fix/conference-audio

Conversation

@transfix

Copy link
Copy Markdown
Owner

Peers' voices broke up in a conference while the scene, the avatars and the video stayed smooth. This fixes the two causes on the clients. The relay is not involved: its log for the Nov 18 session shows no media dropped, no backlog and no stalls. But it also shows the round trip on the same connection swinging between 40 and 90 ms, and per-viewer video queues of 460 to 750 ms.

Causes

  1. No jitter buffer. mix_audio played a peer's audio as soon as it landed, into a 100 ms output it filled once a frame. Over a WebSocket, audio lands in bursts: the sender captures in blocks, sends once a frame, and shares one TCP connection with video. A late burst left the speaker with nothing to play.
  2. The mic producer fell behind for good. It read one 21 ms chunk per tick (46.9 Hz). SDL's Web Audio backend delivers the mic in 2048-frame blocks (43 ms) on the main thread, and browser timers skip the beats a busy frame misses. Each skipped tick left a chunk behind permanently. At 80 ms of backlog the capture discarded it down to one chunk, losing about 64 ms of audio each time.

Changes

  • cvc::voice_mixer (inc/cvc/media/voice_mixer.h) holds one jitter buffer per peer and mixes them. The playback device pulls the mix at its own pace through the new open_audio_output_pull (SDL's get-callback), not at the frame loop's. It is thread-safe: pushes come from the app thread, pulls from SDL's audio thread or the page's audio callback.
    • A peer starts once its target is buffered: 80 ms, and at least the device's block plus 20 ms (audio_output::device_period_s). In a browser that is 63 ms and 85 ms to start.
    • When a peer runs dry and its audio comes back within 300 ms, the audio was late, and its target grows 20 ms (400 ms at most). After 10 s without running dry it shrinks a step.
    • When a talk spurt ends, the peer runs dry too, but nothing comes back, so its target stands.
    • A burst past twice the target (and at least 120 ms past it) is cut back to the target, with a cross-fade across the cut.
    • Every 10 s the client logs each heard peer's target, its depth, how often it ran dry, and what was trimmed. The next live test will have numbers.
  • frame_source::more. A FIFO source can say it has another chunk ready, and the producer reads up to 16 chunks a tick. The SDL mic says so while its backlog holds a whole chunk. muted_source and gain_source forward it. The drop-at-source ceiling goes from 80 ms to 250 ms: a tick now drains everything, so only a real stall reaches it.
  • Opus reordering guard. A packet at or behind the last one is dropped. If it is far behind, the sender reloaded, and its decoder starts over.
  • A/V sync now uses the jitter buffer's depth plus the device's block.
  • Docs: §5j of the conference roadmap, and the current-state table of CONFERENCE_SHARING.md.

Tests

  • voice_mixer_test (12). The headline case simulates a browser link: 43 ms blocks, 30 ms + U(0, 40 ms) delay, 10% of blocks 60 ms later still, delivered in order and seen at 60 Hz frames, with 3 s talk spurts. Over 20 seeds, playing as it lands runs dry 61 to 105 times in 30 s. The jitter buffer runs dry once, settles at 105 ms, and drops nothing. Also tested: start at target, late vs. end-of-spurt underruns, short spurts, trimming and the cross-fade, overflow, gains and clamping, stereo, relaxing, the delay floor.
  • stream_test: DrainsAFifoSourcesBacklogEachTick and MutedSourceForwardsMore. All 41 pass.
  • cvcmedia_audio_output: pull mode on SDL's dummy driver (the device pulls by itself, in whole frames), and device_period_s.
  • Still green: media_rungs_test, room_intrinsics_test, cvcgl_audio_* (live, on the dummy driver), cvcgl_media_*, cvcmedia_audio_output.

End to end (native)

The setup:

  • a local arisrv;
  • a TCP proxy in front of its WebSocket port, adding 20 ms + U(0, 40 ms), plus 60 ms for 10% of chunks, in order;
  • two arigl clients running cyprty's main.ari (synthetic cameras), on Xvfb with llvmpipe;
  • SDL's disk driver, which records exactly what each client plays.

Each peer sends a steady 440 Hz tone, so any gap or discontinuity in the recording is a glitch. The box was at load average 40 to 49 the whole time. Old and new builds ran alternately under the same conditions. Figures are per client, over about 31 s:

run build gaps silent discontinuities
tone mic, trial 1 old 146 / 139 17.3% / 14.5% 318 / 301
new 3 / 4 0.9% / 1.2% 0 / 1
tone mic, trial 2 old 93 / 51 8.4% / 3.9% 202 / 135
new 2 / 5 0.6% / 2.0% 4 / 13
SDL capture mic (disk input) old 105 / 103 10.6% / 8.6% 229 / 206
new 2 / 4 0.9% / 0.9% 0 / 6

What remains comes from this box's frame stalls under that load. Targets settled at 240 to 260 ms there; on an idle machine they should sit near the floor.

Not verified

  • The browser build is only compiled, not run. All changed translation units, arigl/main.cpp included, compile for single-threaded wasm with the WebSocket transport and Opus. The page has not been linked or run in a browser with this change.
  • Nothing here takes audio off a browser's main thread. SDL 3.4.12's Web Audio backend uses a ScriptProcessorNode for both the mic and the speaker, and a long frame still delays both.

… mic drains its backlog

Peers' voices broke up while video and avatars stayed smooth. Two causes:

- The receiver played a peer's audio the moment it landed, into a 100 ms output it filled once a
  frame. Over a WebSocket, audio lands in bursts (the sender captures in blocks and sends once a
  frame, and it shares the connection with video), so a late burst left the speaker with nothing.
- The mic producer read one 21 ms chunk per tick. A browser delivers its mic in 43 ms blocks and
  its timers skip the beats a busy frame misses, so every skipped tick left a chunk behind for
  good, and at 80 ms of backlog the capture dropped it (about 64 ms of audio each time).

cvc::voice_mixer (voice_mixer.h) holds a jitter buffer per peer and mixes them. The playback
device pulls the mix at its own pace through open_audio_output_pull (SDL's get-callback), not at
the frame loop's. A peer starts once its target is buffered: 80 ms, at least the device's block
plus 20 ms. The target grows 20 ms each time the peer runs dry and its audio comes back within
300 ms, and shrinks after 10 s without that. A burst past twice the target is cut back to it with
a cross-fade. The client logs each peer's buffer every 10 s.

frame_source::more lets a FIFO source say it has another chunk ready, and the producer reads up to
16 chunks a tick. The SDL mic says so while its backlog holds a chunk, and keeps up to 250 ms
before it drops. A decoder also drops an Opus packet at or behind the last one, and starts over
when the sender has.
@transfix
transfix merged commit 825a7ba into master Nov 20, 2026
29 checks passed
@transfix
transfix deleted the fix/conference-audio branch November 20, 2026 21:04
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