Skip to content

player role: carry the server's send-ahead on binary audio chunks - #167

Merged
maximmaxim345 merged 3 commits into
mainfrom
156-audio-chunk-send-timing
Aug 28, 2026
Merged

maximmaxim345 merged 3 commits into
mainfrom
156-audio-chunk-send-timing

Conversation

@kahrendt

@kahrendt kahrendt commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Players report required_lead_time_ms and min_buffer_ms but have no way to measure them. The only server transmit timestamps on the wire were the server_transmitted field on stream/start, stream/clear and stream/end, which sample the network once per stream event. Intermittent interference; e.g., a doorbell camera taking 300 ms of airtime every few minutes, almost never coincides with a stream event, so a player sizes its buffer from measurements that never saw the condition that underruns. This causes a too small min_buffer_ms causing frequent dropouts during live streams.

Audio chunks now carry send_ahead: the microseconds between the server transmitting the chunk and the chunk's playback timestamp. Recovering the transmit time from it gives a player a delay sample per chunk, enough to build a distribution rather than infer one from a handful of points.

send_ahead is a duration rather than a second absolute timestamp. It halves the added header (4 bytes, not 8) at a chunk rate where the overhead is worst in the low-latency case that motivates the change, and it cannot overflow in practice: uint32 microseconds covers about 71 minutes against a send-ahead bounded by buffer_capacity. The field saturates at both ends instead of wrapping, so a lead too large to represent degrades to a discarded sample rather than a plausible wrong one.

Defines where the transmit timestamp is taken, as late as the implementation permits, never at enqueue, and where the matching arrival time is taken. Without that, two conforming servers stamping at different points report different delays for the same network and players tune to whichever server they were developed against.

Drops server_transmitted from stream/end, which nothing consumes.

The artwork and visualizer roles are deliberately unchanged: artwork is never dropped for lateness, and a visualizer losing its first frames is cosmetic. The source role is unchanged too; its chunks already carry a capture timestamp, and splitting out the client's pipeline latency would give a server information it has no lever to act on.

Closes #156

Breaking changes

Binary audio chunk layout. send_ahead occupies bytes 9-12, so the encoded audio frame moves from byte 9 to byte 13. Every player must update its chunk parser; one reading the payload at the old offset gets four bytes of header prepended to each frame and will fail to decode.

server_transmitted stamping point is now normative. Transmit timestamps requires the value to be taken as late as the implementation permits and prohibits stamping at enqueue time. The field's previous definition said only "timestamp that the server transmitted this message", so a server stamping at enqueue was not clearly wrong before and is non-conforming now. This applies to server/time, stream/start and stream/clear; server/time is where it matters most, since the value feeds the client's time filter directly. aiosendspin already stamps immediately before sending and needs no change.

server_transmitted removed from stream/end. Nothing consumed it, but a client that treats the field as required will reject the message.

Players report required_lead_time_ms and min_buffer_ms but have no way to
measure them. The only server transmit timestamps on the wire were the
server_transmitted field on stream/start, stream/clear and stream/end, which
sample the network once per stream event. Intermittent interference - a Wi-Fi
neighbour taking 300 ms of airtime every few minutes - almost never coincides
with a stream event, so a player sizes its buffer from measurements that never
saw the condition that underruns it, and truncates the start of an
announcement when it hits.

Audio chunks now carry send_ahead: the microseconds between the server
transmitting the chunk and the chunk's playback timestamp. Recovering the
transmit time from it gives a player a delay sample per chunk, enough to build
a distribution rather than infer one from a handful of points.

send_ahead is a duration rather than a second absolute timestamp. It halves
the added header (4 bytes, not 8) at a chunk rate where the overhead is worst
in the low-latency case that motivates the change, and it cannot overflow in
practice: uint32 microseconds covers about 71 minutes against a send-ahead
bounded by buffer_capacity. The field saturates at both ends instead of
wrapping, so a lead too large to represent degrades to a discarded sample
rather than a plausible wrong one.

Define where the transmit timestamp is taken - as late as the implementation
permits, never at enqueue - and where the matching arrival time is taken.
Without that, two conforming servers stamping at different points report
different delays for the same network and players tune to whichever server
they were developed against.

Drop server_transmitted from stream/end, which nothing consumes.

The artwork and visualizer roles are deliberately unchanged: artwork is never
dropped for lateness, and a visualizer losing its first frames is cosmetic.
The source role is unchanged too - its chunks already carry a capture
timestamp, and splitting out the client's pipeline latency would give a server
information it has no lever to act on.
@kahrendt kahrendt changed the title feat: carry the server's send-ahead on binary audio chunks Player: carry the server's send-ahead on binary audio chunks Aug 24, 2026
@kahrendt kahrendt changed the title Player: carry the server's send-ahead on binary audio chunks player role: carry the server's send-ahead on binary audio chunks Aug 24, 2026

@maximmaxim345 maximmaxim345 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left two nits.

This is also technically a breaking change if a server took server_transmitted at enqueue time for server/time. (I mean aiosendspin already took it right before sending, but still good to explicitly mention in the breaking changes section of this PR)

Comment thread messaging.md Resolved
Comment thread roles/player/v1.md Resolved
maximmaxim345 added a commit to Sendspin/aiosendspin that referenced this pull request Aug 25, 2026
A client that advertises no timing preferences falls back to the server
default, so most ESPHome players run on it. At 500ms that is the entire
jitter budget for a device on WiFi, where hiccups of one to two seconds
are possible.

Measured against the late chunk timings in music-assistant/support#6006,
moving from the 250ms those reports ran on to 500ms accounts for about
40% of the drops, and 1s for about 80%. The cost is 500ms of extra
scheduling lead before the first chunk plays.

Follows the same reasoning as #285, which raised this default from 250ms
to 500ms.

This will of course not be needed anymore once proper measurement is
implemented (powered by Sendspin/spec#167)

@maximmaxim345 maximmaxim345 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@maximmaxim345
maximmaxim345 merged commit bda23f3 into main Aug 28, 2026
1 check passed
@maximmaxim345
maximmaxim345 deleted the 156-audio-chunk-send-timing branch August 28, 2026 13:35
maximmaxim345 added a commit to Sendspin/sendspin-js that referenced this pull request Oct 28, 2026
# What does this implement/fix?

<!-- Quick description and explanation of changes. -->

Audio from a server that sends the spec's 13-byte audio chunk header was
corrupted, since the client read the 4-byte `send_ahead` field as audio.
The client also sent `trust_level` and player `supported_commands` in
`client/hello`, which strict servers reject.

The client now sends the rc1 `client/hello` and skips the full 13-byte
header, and `send_ahead` doesn't affect playback. The operator's
`languages` for a spoken pairing code are now read from `server/hello`,
where the spec defines them.

Breaking changes:
- A server that still sends 9-byte audio chunk headers now plays
corrupted audio.
- `ClientHello` no longer has `trust_level` or player
`supported_commands`, and `ActivatePairing` no longer has `languages`.

Closing the connection on an audio message shorter than 13 bytes is left
for a later PR.

**Related issue or spec PR (if applicable):**

- Sendspin/spec#158
- Sendspin/spec#167
- Sendspin/spec#177
- Sendspin/spec#241

## Types of changes

<!--
Tick exactly one box. CI (.github/workflows/pr-labels.yaml) applies the
matching label, and the release notes use it to sort this change.
-->

- [ ] Bugfix (non-breaking change which fixes an issue): `bugfix`
- [ ] New feature (non-breaking change which adds functionality):
`new-feature`
- [ ] Enhancement to an existing feature: `enhancement`
- [x] Breaking change (changes the public API or the protocol in a way
that is not backwards compatible): `breaking-change`
- [ ] Refactor (no behavior change): `refactor`
- [ ] Documentation only: `documentation`
- [ ] Maintenance / chore: `maintenance`
- [ ] CI / workflow change: `ci`
- [ ] Dependencies bump: `dependencies`
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.

Intermittent Wifi noise not measurable

2 participants