Skip to content

Conference audio in a browser: the mic and the speaker on AudioWorklets, off the main thread - #1412

Merged
transfix merged 1 commit into
masterfrom
feat/conference-audio-worklet
Nov 21, 2026
Merged

transfix merged 1 commit into
masterfrom
feat/conference-audio-worklet

Conversation

@transfix

Copy link
Copy Markdown
Owner

Browser audio for the conference client, off the main thread. This is the follow-up #1398 named (roadmap §5j, "Browser audio off the main thread").

Why

SDL 3.4.12's Web Audio backend plays and captures through a ScriptProcessorNode, in 43 ms blocks, and its callback runs on the main thread between arigl's frames. A long frame plays a gap and loses capture. In a hidden tab it is worse: measured below, a hidden sender sends nothing, and a hidden receiver hears 13-19% of the audio.

What

  • Two AudioWorklet processors (src/cvcMedia/web/cvc-audio-worklet.js). cvc-capture posts the mic in 20 ms blocks. cvc-playback plays from a ring that the main thread keeps filled.
  • One thread is enough. The worklets hold no wasm and share no memory with it, and every block goes over a MessagePort. So this works in the single-threaded build, the one cyprty runs, and on pages without COOP/COEP. cyberpcangel.com/cyprty is cross-origin isolated, but a SharedArrayBuffer ring would buy nothing there: the main thread has to be woken to encode and send anyway.
  • An audio clock (web_audio::audio_clock). Each block's message is a tick. On each one the conference sends the mic and fills the jitter buffers (ConferenceSession::audio_tick), so neither waits for a frame. The browser delivers those messages between frames, and at full rate in a hidden tab.
  • Capture. A FIFO frame_source, behind open_audio_capture. Each block wakes the stream's producer at once (frame_source::set_ready_callback, producer_thread::run_now), where it used to wait for a timer that a hidden tab slows to 1 Hz.
  • Playback. Behind open_audio_output_pull. The ring stays 60 ms ahead of the speaker. When it runs dry it grows by the silence it played and a block, up to 400 ms. After 10 s without running dry it shrinks a block, cross-faded. The jitter buffers stay in voice_mixer, and A/V sync counts what the ring holds.
  • SDL is the fallback. It is used when the browser has no AudioWorklet or the page is not a secure context, and for a rate other than the context's 48 kHz, more than two channels, a non-default device, or push-mode outputs. CVC_WEB_AUDIO=sdl forces it for an A/B. A device opened while the worklets load gets its node once they have loaded.
  • Loading. The page loads cvc-audio-worklet.js from beside itself (arigl-web and ariglx-web now ship it), else the copy compiled in, from a blob: URL. A page with a strict CSP needs the file.
  • The design note is in docs/roadmap/ARIADNE_CONFERENCE_ROADMAP.md §5j.

Tests

  • src/cvcMedia/test/cvc_audio_worklet_test.js tests the processors in node: blocks, the ring, ticks, running dry, the cross-faded drop, and overflow. It runs from run-js-tests.sh in the "cvcGL wasm JS tests" job.
  • src/arigl/web/test/arigl_web_audio.mjs runs a local arisrv and two headless Chromes with fake media. The sender's mic is a steady 440 Hz tone (voice processing off for the test). The receiver's playback is recorded on its own audio thread and checked for how much of the time the tone is heard, for gaps, and for clicks. It runs four cases in both modes. It is not in CI yet: it needs a native arisrv. To run it:
    node src/arigl/web/test/arigl_web_audio.mjs --web <share/arigl-web> --doc examples/ariadne/documents/conference_roster.ari --arisrv <arisrv> --chrome <chrome> [--gpu] [--expect]

Measured locally (headless Chrome 152, 30 s a case, on this branch before its rebase onto master, which changed nothing in these files). On the GPU, where frames are short:

Case AudioWorklets: heard, gaps, clicks SDL (?audio=sdl)
idle 99.7%, 3 (85 ms), 2 99.6%, 4 (120 ms), 10
busy (150 ms in every 400, both tabs) 99.8%, 6 (60 ms), 4 98.8%, 8 (350 ms), 346
sender hidden 100%, 2 (15 ms), 2 0%
receiver hidden 99.9%, 3 (35 ms), 3 12.7%, 31 gaps (26 s), up to 900 ms

In SwiftShader each frame is a long task of about 100 ms. There the worklets kept the tone 97.9-99.3% of the time. SDL kept it 89.9% busy, 0% with the sender hidden, and 18.7% with the receiver hidden, with hundreds of clicks. What the worklets still lose comes in the first seconds, while the ring and the jitter buffers grow. voice_mixer grows 20 ms per late underrun, which took 9 against a stalling sender. That is a follow-up.

The existing arigl_web_smoke.mjs passes 14/14 against the new package. The worklets also load under its strict CSP, and from blob: when the page lacks the file.

Not verified

  • Real mics, speakers and ears; Firefox and Safari; a phone.
  • Nothing is deployed. cy-pca/cyprty's stage.sh copies a fixed file list without cvc-audio-worklet.js, so a cyprty built from this would load the compiled-in copy, which works there (no CSP).
  • The native-audio PR from fix/conference-audio-off-frame-loop also edits ConferenceSession. Whichever merges second needs a rebase.

@transfix
transfix force-pushed the feat/conference-audio-worklet branch from 09e86e0 to 900a53c Compare November 21, 2026 00:04
transfix added a commit to cy-pca/cyprty that referenced this pull request Nov 21, 2026
…n A/B (#4)

An arigl-web from transfix/libcvc#1412 on runs the browser's mic and speaker on AudioWorklets and
installs their processors as share/arigl-web/cvc-audio-worklet.js. stage.sh copies the file when
the package has it. An older package has neither the file nor the code that loads it, so the file
is optional; a package whose arigl.js loads it but which lacks it stages with a warning (the page
falls back to the copy in arigl.wasm, from a blob: URL, after a 404).

Both page configs gain url_env "audio" -> CVC_WEB_AUDIO, ^(sdl|worklet)$, so ?audio=sdl puts the
mic and speaker back on SDL's ScriptProcessorNode. verify.py already checks every staged file; it
now also checks that each script is served as JavaScript, since an AudioWorklet module loads only
as one. ci stages two fake packages, with and without the file.
SDL 3.4.12's Web Audio backend plays and captures through a ScriptProcessorNode
in 43 ms blocks, and its callback runs on the main thread between arigl's
frames. A long frame plays a gap and loses capture. In a hidden tab the mic is
not captured at all, and the speaker barely plays.

- cvcMedia's web_audio (cvc/media/web_audio.h) has two AudioWorklet processors
  (src/cvcMedia/web/cvc-audio-worklet.js). cvc-capture posts the mic in 20 ms
  blocks. cvc-playback plays a ring the main thread keeps filled. They hold no
  wasm and share no memory with it: every block goes over a MessagePort, so
  the single-threaded build and a page without COOP/COEP both work.
- Each block's message ticks an audio clock. The conference sends the mic and
  fills the jitter buffers there (ConferenceSession::audio_tick), not only
  once a frame. The browser delivers those messages between frames, and at
  full rate in a hidden tab.
- Capture: a FIFO frame_source. Each block wakes the stream's producer at once
  (frame_source::set_ready_callback, producer_thread::run_now), where it used
  to wait for a timer that a hidden tab slows to 1 Hz.
- Playback: the ring stays 60 ms ahead of the speaker. When it runs dry it grows
  by the silence played and a block, up to 400 ms. After 10 s without that it
  shrinks a block, cross-faded. The jitter buffers stay in voice_mixer. A/V
  sync counts what the ring holds.
- SDL is the fallback: no AudioWorklet, not a secure context, a rate other
  than the context's 48 kHz, more than two channels, a device other than the
  default, push-mode outputs, or CVC_WEB_AUDIO=sdl. A device opened while the
  worklets load gets its node once they have loaded.
- The page loads cvc-audio-worklet.js from beside itself (arigl-web and
  ariglx-web ship it), else the copy compiled in, from a blob: URL.
- Tests: cvc_audio_worklet_test.js (the processors in node, in
  run-js-tests.sh) and arigl_web_audio.mjs (a local arisrv and two headless
  Chromes with fake media: a 440 Hz tone sent, the receiver's playback recorded
  and checked for gaps and clicks; idle, busy main threads, sender hidden,
  receiver hidden; worklet against ?audio=sdl).
@transfix
transfix force-pushed the feat/conference-audio-worklet branch from 900a53c to 0f9bb25 Compare November 21, 2026 05:46
@transfix
transfix enabled auto-merge (squash) November 21, 2026 05:57
@transfix
transfix merged commit f19d895 into master Nov 21, 2026
29 checks passed
@transfix
transfix deleted the feat/conference-audio-worklet branch November 21, 2026 10:12
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