You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Hand the player's PCM with its format on every write - #268
Next step of the public API move to standard view types (after #260, #261, #266). The player handed PCM through on_audio_write(uint8_t*, size_t, uint32_t) and left the consumer to read its format from get_current_stream_params(), a value the main loop applies when it drains a stream/start. ESPHome reads that getter on the sync thread for every write: a data race, and wrong across an updating stream/start, since the main loop switches it while audio buffered in the old format is still being written (roles/player/v1.md "stream/start"). Each write now carries the format of exactly its own bytes. Part of Sendspin/backlog#92.
PlayerRole::stream_info(), main loop only, for what the stream announced (codec included); each write's AudioFormat for the PCM itself
ServerPlayerStreamObject
PlayerStreamInfo
AudioFormat is the sample rate, channels and container depth (8, 16, 24 or 32; 24-bit packed in 3 bytes, odd depths reported at the next multiple of 8 with their samples in the top bits). The return value is unchanged: bytes written, whole frames. Overriding on_audio_write() draws no warning, so search for the name and mark overrides override.
Change
Dispatch: the sync task calls only on_audio_output() (its return value counts bytes, so both forms cannot run); its default hands the bytes to on_audio_write(), now non-pure. A listener overriding neither plays nothing, and the sync task retries after its usual back-off rather than spinning. on_audio_output() becomes pure in v0.11.0.
Format per write: silence and decoded audio both go through SyncTask::write_to_sink(), which labels each write from the sync task's own decoded stream info. TransferBuffer no longer holds a listener. Pending underflow silence is dropped when a codec header changes the format, since its bytes need not be whole frames of the new one.
stream_info(): a main-loop PlayerStreamInfo (codec, sample rate, channels, announced depth), or nullopt while no stream is active. The stream parse moves to an internal PlayerStreamParams.
The examples, the integration guide (player section, threading table, Deprecated API) and internals use the new API; the PortAudio sink reopens on a format change after its old audio plays out.
Tests
PlayerAudioOutput.EachWriteCarriesTheFormatOfItsOwnBytes: across an updating PCM stream/start (44.1 kHz stereo 16-bit to 48 kHz mono 24-bit), each write, silence included, carries the format of its own bytes, though the main loop has already applied the update, and no partial frame of leftover silence goes out.
PlayerAudioOutput.TheDeprecatedWriteStillReceivesTheAudio: a listener still on on_audio_write(), as the ESPHome hub is, gets the audio through the forward.
The FLAC stream/start and first-codec-header allocation windows stay at 0 with a listener overriding on_audio_output().
Builds checked: GCC 16 -DENABLE_WERROR=ON with tests and examples, clang-tidy, and an ESP-IDF esp32s3 build with no deprecation warnings. The sync task's static stack bound is unchanged (5,088 B). Not run on hardware; the ESPHome hub needs a follow-up to move to on_audio_output().
Adds on_audio_output(), which hands the PCM as a std::span<const uint8_t>
beside the AudioFormat of exactly those bytes, taken from the sync task's
decoder rather than from the stream/start the main loop applied, so audio
buffered before an updating stream/start keeps its own format (roles/
player/v1.md "stream/start"). bits_per_sample is the container depth, 24-bit
packed in 3 bytes. The library calls only on_audio_output(); its default
hands the bytes to on_audio_write(), which is deprecated and no longer pure.
Adds PlayerRole::stream_info(), a main-loop PlayerStreamInfo of what the
latest stream/start announced, codec included.
get_current_stream_params() and ServerPlayerStreamObject are deprecated for
removal in v0.11.0.
Pending underflow silence is dropped when a codec header changes the
format, since its bytes need not be whole frames of the new one.
The per-write format test covers the change's purpose and the deprecated
write's test covers the path the ESPHome hub uses until it moves. The
listener-form table, the same-format silence control and the container
depth test guarded nothing a realistic change could break.
The PCM layout, container depth and whole-frame rule live in the
integration guide's PlayerRoleListener section; the header keeps each
item's purpose and points there, internals keeps one sentence on where
the format comes from, and PlayerStreamInfo no longer repeats
AudioFormat's field docs.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Queued old-format audio is accounted at the new sample rate, potentially triggering false synchronization corrections that drop valid audio or insert silence.
… out of stream_info()
The PortAudio ring's clear() drops only what was written before it, so a same-format restart's
first writes survive the deferred clear; the sink reopens whenever it has no stream; stream_info()
is nullopt for a STREAM_START whose params a teardown replaced. Lists stream_info() in the state
snippet and fixes a stale test comment.
Initialize both stream representations in the fixture
tests/test_player_role.cpp:1322
This only updates the deprecated cache; the new main-loop stream_info remains empty and stream_active is false. Thus the fixture does not establish the advertised case where the main loop has applied the new stream before the old-format writes. Set up both main-loop representations consistently (or drain the starts) before exercising the writes, as required for fixtures by docs/conventions.md:290.
Drain queued old-format audio before reconfiguring output
docs/integration-guide.md:244
The sample reconfigures the output as soon as the first new-format bytes arrive, but old-format bytes can still be queued in the output device. A backend that reopens or immediately changes format will discard or misinterpret those bytes, even though an in-place stream/start must retain the old audio. Please show or explicitly require draining the old-format queue before reconfiguration (returning zero and retrying if it cannot drain within the timeout), as the PortAudio sink does.
Document callback argument lifetimes and require copies
include/sendspin/player_role.h:113
Please state the lifetime of both arguments here. pcm points into a reused decode buffer or silence scratch, and format is a temporary created for this call; a sink that retains either for asynchronous playback will otherwise read overwritten data or a dangling reference. Consumers need to copy anything they keep after the callback returns.
Reverts the stream_info() resets: with START, END, START queued, the first START takes the second's
params, and the resets left the final stream reporting nullopt. A START left without its own params
keeps the previous ones, as the deprecated getter always has. Drops fixture lines nothing reads and
tells sinks to play out old-format audio before reopening in a new one.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Next step of the public API move to standard view types (after #260, #261, #266). The player handed PCM through
on_audio_write(uint8_t*, size_t, uint32_t)and left the consumer to read its format fromget_current_stream_params(), a value the main loop applies when it drains astream/start. ESPHome reads that getter on the sync thread for every write: a data race, and wrong across an updatingstream/start, since the main loop switches it while audio buffered in the old format is still being written (roles/player/v1.md "stream/start"). Each write now carries the format of exactly its own bytes. Part of Sendspin/backlog#92.Migration:
PlayerRoleListener::on_audio_write(uint8_t*, size_t, uint32_t)on_audio_output(std::span<const uint8_t>, const AudioFormat&, uint32_t)PlayerRole::get_current_stream_params()PlayerRole::stream_info(), main loop only, for what the stream announced (codec included); each write'sAudioFormatfor the PCM itselfServerPlayerStreamObjectPlayerStreamInfoAudioFormatis the sample rate, channels and container depth (8, 16, 24 or 32; 24-bit packed in 3 bytes, odd depths reported at the next multiple of 8 with their samples in the top bits). The return value is unchanged: bytes written, whole frames. Overridingon_audio_write()draws no warning, so search for the name and mark overridesoverride.Change
on_audio_output()(its return value counts bytes, so both forms cannot run); its default hands the bytes toon_audio_write(), now non-pure. A listener overriding neither plays nothing, and the sync task retries after its usual back-off rather than spinning.on_audio_output()becomes pure in v0.11.0.SyncTask::write_to_sink(), which labels each write from the sync task's own decoded stream info.TransferBufferno longer holds a listener. Pending underflow silence is dropped when a codec header changes the format, since its bytes need not be whole frames of the new one.stream_info(): a main-loopPlayerStreamInfo(codec, sample rate, channels, announced depth), ornulloptwhile no stream is active. The stream parse moves to an internalPlayerStreamParams.Tests
PlayerAudioOutput.EachWriteCarriesTheFormatOfItsOwnBytes: across an updating PCMstream/start(44.1 kHz stereo 16-bit to 48 kHz mono 24-bit), each write, silence included, carries the format of its own bytes, though the main loop has already applied the update, and no partial frame of leftover silence goes out.PlayerAudioOutput.TheDeprecatedWriteStillReceivesTheAudio: a listener still onon_audio_write(), as the ESPHome hub is, gets the audio through the forward.stream/startand first-codec-header allocation windows stay at 0 with a listener overridingon_audio_output().-DENABLE_WERROR=ONwith tests and examples, clang-tidy, and an ESP-IDF esp32s3 build with no deprecation warnings. The sync task's static stack bound is unchanged (5,088 B). Not run on hardware; the ESPHome hub needs a follow-up to move toon_audio_output().