# Browser audio buffering

The browser receives stereo 48 kHz Opus in 5 ms packets. WebCodecs decodes the
packets, and `browser/web/audio-worklet.js` queues the planar PCM for Web Audio.

## Capture source

Browser sessions disable Host playback and use the same virtual **Sunshine
Stereo** capture path as the native client's usual configuration. Earlier browser
builds enabled Host playback, which could select an HDMI sink instead. The browser
ready message reports `audioSource: sunshine-stereo` (or `disabled` when Host
audio streaming is off), and the settings summary and decoder details expose that
choice. This changes capture selection, not the Opus format or the browser's output
buffering. Native session behavior and existing sink restoration remain separate.

This capture-source correction ships in **Browser Preview 10**. Install the matching
browser-enabled Host package and reload the browser page.

A designated-server candidate build verified the actual Host sink and monitor:
`sink-sunshine-stereo` and `sink-sunshine-stereo.monitor` were selected, then the
previous `source_audio` default was restored after disconnect. A 4K60 H.264 session
completed 1245 frames at 60 FPS plus 526 reconnect frames with zero presentation
drops. Host, decoded, output and reconnect PCM had no silent 5 ms windows or
clipping, with more than 53 dB stereo separation. These are controlled synthetic
loopback results, not physical-speaker or every-device qualification.

### Silent first connection or reconnect with a saved output

Preview 9 can race PipeWire/WirePlumber's saved default-output restoration. When
Sunshine recreates its virtual sinks, the desktop can restore Sunshine Stereo and
move an already-playing application there while the browser session has selected
the HDMI monitor for recording. Video and audio packets continue, but the recorded
monitor is silent. Switching HDMI → Sunshine Stereo in the Gaming PC's Sound
settings moves the recording route and temporarily restores sound; the race can
return on the next connection.

Disabling browser Host playback explicitly selects the virtual stereo sink before
opening capture. The Linux backend then uses that requested sink rather than
sampling a changing default. The existing disconnect path restores the previous
default; no output-device toggle is needed to start the next stream.

A matched private PipeWire 1.0.5/WirePlumber 0.4.17 regression keeps one application
playing from before the first connection through six sessions. With a remembered
Sunshine Stereo default, the exact published Preview 9 package captured HDMI and
was silent in five of six sessions, including the first. The HDMI → Sunshine Stereo
toggle restored each failure. The corrected candidate passed all six without
manual routing: both playback and recording used Sunshine Stereo, and disconnect
restored the prior sink. Normal/default and previously selected application-route
controls also passed. These checks use ordinary default-device playback, unlike
the earlier direct-to-test-sink tone checks, which did not exercise this race.

## Device callbacks are larger than a render quantum

An AudioWorklet render quantum does not describe the entire audio-device callback.
A backend can consume many 128-frame quanta before processing new port messages.
For example, a 4096-frame PipeWire quantum at 48 kHz represents about 85 ms of
audio demand. `AudioContext.baseLatency` can still report only about 10.7 ms.

The original browser queue started after 15 ms and discarded its entire backlog
above 60 ms. Large device callbacks or batched PCM delivery could therefore
produce repeated starvation and discontinuities even when decoding and network
delivery were healthy. This defect was reproduced with and without Chrome's
`--disable-frame-rate-limit` option.

## Adaptive playback

The worklet observes both source-frame demand between input messages and PCM
delivery accumulated between render calls. It retains 15 ms minimum priming,
increases the reserve for observed bursts, and bounds adaptive priming to 125 ms
and queued PCM to 250 ms. The ring stores 16384 samples per channel.

Overload trims only the excess oldest samples instead of emptying the queue.
Short output-sample fades smooth deliberate discontinuities and starvation edges.
Reset discards old audio and interpolation state while retaining the device's
learned burst budget. A long producer pause is not learned as a huge device
callback. Resampling checks both interpolation lookahead and the number of source
samples advanced, so it does not consume unwritten input.

This adapts to backend behavior without changing the system's audio settings or
adding a fixed large delay on every device. A device that actually consumes large
bursts needs a larger reserve. Hardware/output latency still exists independently
of this queue; the worklet does not promise zero audio latency or repair arbitrary
network loss, corrupt capture, clipping, or microphone problems.

## Verification

The dedicated `browser/tests/audio-worklet.test.js` suite exercises continuous
stereo playback, regular/batched/jittered delivery, 1024/4096-frame device callbacks,
8/44.1/48/96 kHz output, resets, pauses, bounded overload and invalid input. Run it
with the existing browser JavaScript suite:

```sh
node --test tests/*.test.js
```

The original source is an optional negative control through the test-only
`MXS_AUDIO_WORKLET_SOURCE` environment variable. Original behavior fails six
regressions, including the independently generated steady-tone gap checks.

Designated-server Google Chrome 156 tests used private PipeWire output graphs,
known stereo tones and actual sink-monitor PCM. The 4096-frame callback reproduced
approximately 59% silent 5 ms windows with gaps up to 55 ms in the original
worklet. The adaptive worklet removes those gaps in the matched reproduction;
normal callbacks and 80 ms delivery bursts are also checked. Full Host/WebTransport/
Opus/4K60 hardware-video tests verify decoded PCM, output PCM, ordinary input,
reconnect and silence after disconnect. These are controlled Linux acceptance
results, not certification of every physical output device or browser.

Included in Browser Preview 4 (`1.0.268+browser.4`). Reload the browser page after
upgrading: the corrected web assets are embedded in the updated browser helper.
