Skip to content

Decode the FLAC codec_header at parse into a fixed 42-byte array - #250

Merged
kahrendt merged 3 commits into
mainfrom
codec-header-inline
Nov 5, 2026
Merged

kahrendt merged 3 commits into
mainfrom
codec-header-inline

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A FLAC stream/start copied the player's codec_header into a std::string on the protocol task, the last per-stream heap allocation there that does not wait on a spec change (Sendspin/backlog#92). roles/player/v1.md "Codec framing" fixes the header as the fLaC marker followed by the STREAMINFO block, so it is always 42 bytes and can be held inline.

Breaking: ServerPlayerStreamObject::codec_header is removed from the public API. Only the sync task's decoder used it; neither the examples nor the ESPHome component read it.

Breaking Change Migration

  • Delete any read of codec_header from PlayerRole::get_current_stream_params() or a ServerPlayerStreamObject. The library already hands the header to its own decoder, so a player needs nothing in its place.
  • The stream's format is still in codec, sample_rate, channels and bit_depth, the same values STREAMINFO carries for decoding.

Change

  • The parser reads the header as a view and decodes it into a private 42-byte array (PlayerStreamPayload in protocol_messages.h). It accepts exactly the 56-character encoding of fLaC, a STREAMINFO block header with the last-block flag set (RFC 9639 section 8.1) and length 34, and the 34-byte body. Anything else rejects the stream/start, as a missing header already did.
  • The player copies those bytes into the sync task's item with hand_local(), still exempt from the quota. hand_flac_header() is gone, and InboundConsumer::hand() is private now that nothing outside calls it.
  • Integration guide, internals.md "Memory" and the CLAUDE.md alloc-window list are updated.

Tests

  • sendspin_alloc_tests has a new window around a FLAC stream/start the player takes, expecting 0. It closes inside the listener's on_stream_start(), before the sync task starts decoding, after one warm-up stream that absorbs the main loop's first held-event slot. Restoring the std::string copy fails it with 1 allocation; it passed 20 repeats under ASan and TSan.
  • A behavior table in test_protocol.cpp covers the header rules, with one row per guard (each mutant removing a check fails a row).
  • test_player_role.cpp checks that the sync task's item holds exactly the 42 bytes, typed as a FLAC header.
  • The FLAC header used across the tests had its last-block flag clear, which micro-flac never decoded; it is replaced with a valid one.

roles/player/v1.md "Codec framing" fixes a FLAC codec_header as the fLaC
marker followed by the STREAMINFO block, so the parser reads it as a view,
decodes it into a private 42-byte array and rejects anything else. The
player hands those bytes to the sync task, and a FLAC stream/start no
longer copies the header into a std::string on the protocol task.

Breaking: ServerPlayerStreamObject::codec_header is removed from the public
API; only the sync task's decoder used it.
@kahrendt
kahrendt marked this pull request as ready for review November 5, 2026 01:17
@kahrendt
kahrendt requested a max review from Copilot November 5, 2026 01:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The FLAC allocation test uses a PCM-only player configuration, violating fixture invariants and making the measurement depend on a stream outside the advertised formats.

2 open issues
What changed in this PR

This PR removes the protocol task’s per-stream FLAC header heap allocation by decoding the header into a private, fixed 42-byte array before handing it to the sync task.

Changes:

  • Validate FLAC header framing and Base64 encoding during parsing, then pass decoded bytes to the sync task.
  • Remove the public ServerPlayerStreamObject::codec_header field and update consumer and internal documentation.
  • Add header-validation, handoff, and allocation-count coverage, with valid FLAC fixtures.
File Description
tests/​test_protocol.cpp Adds FLAC header validation and decoding cases.
tests/​test_player_role.cpp Checks decoded-header handoff and adapts payload callers.
tests/​test_json_arena.cpp Updates the FLAC fixture and parsed-field access.
tests/​test_client_lifecycle.cpp Adapts stream payloads and audio-message handoffs.
tests/​test_allocations.cpp Adds a zero-allocation FLAC stream-start window.
src/​protocol.cpp Validates and decodes FLAC headers during parsing.
src/​protocol_messages.h Defines the fixed header array and private player payload.
src/​player_role.cpp Hands decoded header bytes to the sync task.
src/​player_role_impl.h Updates the stream-start interface and removes the decoding helper.
src/​inbound_ring.h Makes the low-level hand() method private.
src/​client_dispatch.cpp Passes the parsed player payload to the role.
include/​sendspin/​player_role.h Removes the public codec_header field.
docs/​internals.md Documents inline header storage and revised allocation sites.
docs/​integration-guide.md Updates the public stream-parameter documentation.
CLAUDE.md Documents the new allocation-test window.

Review effort: Max

Comment thread tests/test_allocations.cpp Resolved
Comment thread tests/test_player_role.cpp Resolved
@kahrendt
kahrendt enabled auto-merge (squash) November 5, 2026 01:37
@kahrendt
kahrendt merged commit fc6e090 into main Nov 5, 2026
16 checks passed
@kahrendt
kahrendt deleted the codec-header-inline branch November 5, 2026 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants