Repository navigation
Let stream/clear drop only audio from a timestamp on - #336
Conversation
Servers could only append new audio after everything already buffered, or clear the whole buffer and leave a silent gap, so a changed next track or an announcement had to wait out up to the full send-ahead.
|
The cut lead assumes a player can drop any audio more than On ESPHome, sendspin-cpp can add a way to clear the speaker's buffers (Sendspin/sendspin-cpp#55), but some of the audio stack can never be cleared. Should the player report that amount, either as its own field or as a stated requirement on |
|
Hmm, yeah. I was assuming that |
# What does this implement/fix? <!-- Quick description and explanation of changes. --> `PushStream.clear_from(timestamp_us)` replaces the audio committed from a timestamp on, for example when the next track changes after it was already sent ahead. Clients keep what they buffered before the cut and the next commit continues at it, without a gap or a new `stream/start`. Each player is cleared from the start of its last chunk at or before the cut, since `from_timestamp` must be the timestamp of a sent chunk, and the audio up to the cut is sent again. The visualizer is cleared at the cut. It waits for a running commit and returns False without changing anything when the cut can't be made, for example when it is closer than a player's lead time, so the caller can fall back to `clear()`. Clients that don't know `from_timestamp` treat the message as a full clear, so the caller decides when to use it. **Related issue or spec PR (if applicable):** - Sendspin/spec#336 ## 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` - [x] New feature (non-breaking change which adds functionality): `new-feature` - [ ] Enhancement to an existing feature: `enhancement` - [ ] 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`
# What does this implement/fix? <!-- Quick description and explanation of changes. --> A `stream/clear` with `from_timestamp` now drops only the audio from that time on, and the player keeps playing what it buffered before it. Until now the player cleared everything, so when a server replaced a buffered next track, the rest of the current track went silent. Apps get the timestamp as a new argument of `onStreamClear`, on application roles and on `SendspinCore`. Chunks the clear keeps keep their `generation`, so a `SendspinCore` app can receive them after chunks of a newer one. Tested by hand in the Music Assistant web player against a server that replaces the next track. With 6.0.0, changing the next track near the end of a track left the rest of the track silent. With this change, the track played to its end and the new next track followed without a gap. **Related issue or spec PR (if applicable):** - Sendspin/spec#336 ## 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` - [x] New feature (non-breaking change which adds functionality): `new-feature` - [ ] Enhancement to an existing feature: `enhancement` - [ ] 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`
Adds an optional
from_timestamptostream/clear, so a server can replace a player's buffered audio from a point on without waiting for the buffer to play out or leaving a silent gap.Why
Servers can currently only append audio after everything a player has buffered, or clear the whole buffer and start over after a silent gap. With a large buffer, a changed next track or an announcement either waits for the buffer to play out or cuts to silence first.
What changes
from_timestamp, players and visualizers drop only buffered data at or after it and keep playing everything before it. The server sends the replacement from that point.min_buffer_msandrequired_lead_time_ms, plusoutput_delay_ms, ahead of the message. While a stream runs,required_lead_time_msalso has to cover the audio a player has buffered in its audio backend and can't discard. If a cut still lands inside that audio, it plays and the player drops the new audio up to where it ends.from_timestamp,stream/clearworks as before.Breaking changes
from_timestamp. A client that ignores it does a full clear instead, so it goes silent until the cut and then continues.from_timestampkeeps working unchanged.Announcements
This is also especially useful for announcements. The separate announcement role in #126 puts most of the work on the client: a second stream, mixing and ducking. With this change most of it moves to the server, which mixes the announcement into the music itself, so it's likely simpler for clients. It covers most of what #126 does, and grouped players stay in sync.
To play the announcement louder than the music, the server turns the music down further by the amount it raises the volume, and leaves a short gap of turned-down music before and after the clip. It raises the volume during the gap before the clip and restores it during the gap after. The gap is needed because volume commands aren't timed to the audio. This only works on players that accept volume commands. It also adds a short delay before the announcement, and other controllers see the volume go up and back down.
A separate announcement role would still be needed for:
How a server could play an announcement
stream/clearwithfrom_timestampset to that chunk start.stream/startfirst.Trade-offs
stream/startbefore a replacement in the old format. I went with this since rolling back would need every client to keep a format history.required_clear_lead_time_msfield, so a player with a slow cold start doesn't delay every announcement by its startup time. The clients below report fixed values between 150 and 500 ms, so I don't think it's worth one more field.required_lead_time_mswhile playing. In a group, it pushes the cut later for every player. I made this a requirement onrequired_lead_time_msinstead of a new field, since a lead measured from astream/clearon a running pipeline already includes waiting for that audio.Client effort
I had Claude check three clients, so take this with a grain of salt. It looks moderate in all of them. Each already keeps a server timestamp on every buffered chunk, so dropping the tail itself is easy. The main work is dropping only the chunks at or after the cut that are still being decoded or queued when the clear arrives, since today a clear drops all of them.
required_lead_time_mstodayretainSizes are rough estimates and exclude tests.
Related:
Add announcement role (announcement@v1) #126
I accept the Contributor License Agreement for this contribution.