Skip to content

Data streams v2 (5/5): move writers to FFI writer handles; add streamText / streamBytes - #333

Draft
1egoman wants to merge 2 commits into
ds/4-receive-additionsfrom
ds/5-writer-cutover
Draft

1egoman wants to merge 2 commits into
ds/4-receive-additionsfrom
ds/5-writer-cutover

Conversation

@1egoman

@1egoman 1egoman commented Nov 30, 2026 •

Copy link
Copy Markdown

Part 5 of 5 of the data streams v2 stack. Merge in order; each part is mergeable on its own.

  1. Data streams v2 (1/5): conformance test harness #329: Conformance test harness
  2. Data streams v2 (2/5): read incoming data streams through FFI readers #330: Read incoming data streams through FFI readers (fixes receiving from other SDKs)
  3. Data streams v2 (3/5): add sendText / sendBytes / sendFile #331: Add sendText / sendBytes / sendFile
  4. Data streams v2 (4/5): add ByteStreamReader::writeToFile and a receive size limit #332: Add ByteStreamReader::writeToFile and a receive size limit
  5. Data streams v2 (5/5): move writers to FFI writer handles; add streamText / streamBytes #333: Move writers to FFI writer handles; add streamText / streamBytes 👈 this PR

This stack replaces #321, #322 and #325 (same code, reordered so each step stands on its own).

Moves TextStreamWriter / ByteStreamWriter onto the FFI's writer handles, and adds LocalParticipant::streamText / streamBytes. The old way of making writers (public constructors) keeps working but is deprecated.

auto writer = local_participant->streamText(options);  // was: TextStreamWriter writer(*local_participant, topic);
writer->write("hello ");
writer->close();

Changes

  • New path: writers open an FFI writer (text/byte_stream_open) and send through *_stream_write / *_stream_close, instead of hand-building header / chunk / trailer packets. The Rust core does chunking and UTF-8 splitting.
  • streamText / streamBytes return an already-open writer and throw DataStreamError if the open fails. They take the same options as the one-shot sends, including the text header fields from part 3.
  • Deprecations:
    • The writer constructors are deprecated, with a [[deprecated]] attribute and @deprecated doc. They now open lazily on the first write or close.
    • So are kStreamChunkSize and the protected ensureHeaderSent / sendChunk / sendTrailer, which are reimplemented on the new path.
  • write() / close() ordering: they share one mutex and each waits for its FFI callback, so close() never overtakes an in-flight write (the fix(rtc): synchronize stream writer aclose with in-flight writes python-sdks#879 fix). It also fixes close() not taking the write lock.
  • Writers and disconnect: a LocalParticipant tracks its writers in a shared registry. On disconnect, or when the participant is destroyed, every writer is closed, and later calls throw DataStreamError instead of reaching an FFI session that would panic.
  • Writer destroyed without close(): it sends a non-waiting close. The Rust writer only sends its drop-time trailer from a Tokio thread, so dropping the handle from C++ left receivers waiting forever.
  • Removed: FfiClient::sendStream*Async, splitUtf8 and the now-unused header / chunk / trailer converters, which were all internal.

Nothing gets more broken: writers now send what other SDKs' writers send (never inline, never compressed), and the C++ receive path from part 2 reads it.

Tests

File Added here
test_data_stream_conformance_send.cpp 11 writer tests on raw packets: each write sent as it happens, never compressed or inlined, MTU and UTF-8 splitting, close attributes and reason, oversized attributes, destroyed writer still sends its trailer, packets attributed to the sender, plus targeted destinations and sender_identity across all send APIs
test_data_stream_conformance_text_header.cpp header fields through streamText
test_data_streams.cpp Unicode text and large payloads through writers, close() waiting for a write, write after close/disconnect throws, destroyed writer ends the stream, deprecated constructors still work; existing tests switched to streamText / streamBytes
test_data_stream.cpp, test_ffi_client.cpp (unit) writers without a live room, deprecated constructor metadata
Rust outgoing/manager.rs tests covered here
Rust test C++ test (DataStreamSendConformanceTest.*)
stream_text_never_compresses_or_inlines StreamTextSendsEachPacketAsItIsWritten
incremental_writer_sends_per_write StreamTextSendsEachPacketAsItIsWritten
stream_bytes_never_compresses_or_inlines StreamBytesNeverCompressesOrInlines
stream_bytes_multi_write_splits_at_mtu StreamBytesMultiWriteSplitsAtMtu
stream_text_splits_on_utf8_boundaries_at_mtu StreamTextSplitsOnUtf8BoundariesAtMtu
close_with_options_sends_trailer_attributes CloseWithAttributesSendsTrailerAttributes
close_with_options_sends_reason_and_attributes CloseWithReasonSendsReasonAndAttributes
stream_text_oversized_attributes_errors StreamTextOversizedAttributesThrows
drop_raw_stream_on_non_tokio_thread_does_not_panic DestroyedWriterSendsTrailer
packets_carry_no_identity_when_sender_identity_unset PacketsAreAttributedToTheSender
one_shot_send_text_targets_every_packet TargetedStreamsReachOnlyTheirDestinations (needs LIVEKIT_TOKEN_C)
stream_text_targets_every_packet TargetedStreamsReachOnlyTheirDestinations
send_file_targets_every_packet TargetedStreamsReachOnlyTheirDestinations
mixed_targeted_v2_subset_inlines_compressed TargetedStreamsReachOnlyTheirDestinations (partial)
stream_text_with_sender_identity_stamps_every_packet SenderIdentityOptionIsAcceptedAndDelivers (partial)
send_text_inline_with_sender_identity_stamps_packet SenderIdentityOptionIsAcceptedAndDelivers (partial)
one_shot_send_with_sender_identity_stamps_every_packet SenderIdentityOptionIsAcceptedAndDelivers (partial)

The sender_identity tests are partial because the SFU attributes every packet to the authenticated sender unless its token allows impersonation, which the test tokens don't. So the test checks that the option is accepted and the stream delivered.

The 4 Rust utf8_chunk.rs tests cover an internal helper. The C++ SDK no longer chunks text itself; StreamTextSplitsOnUtf8BoundariesAtMtu and V2CompressedTextReframesMultibyteUtf8 (part 2) check that behaviour end to end.

After this stack

All 124 Rust livekit-data-stream tests are either ported (fully or partly) or not testable from C++. The ones that aren't testable fall in these groups, and the crate's own tests cover them:

  • progress;
  • pre-v2 recipients and recipients without compression;
  • malformed v2 packets;
  • encryption mismatches;
  • manager internals.

Verification (local, macOS arm64, livekit-server --dev)

  • Unit tests: 384/384.
  • Full integration suite: 174 pass, 3 fail (see below), 1 skip. All 107 data stream tests pass.
  • Python livekit 1.3.0, both directions, 3/3 runs intact:
    • Python → C++;
    • C++ sendText, streamText (40 KB in two writes) and streamBytes (100 KB) → Python.
  • clang-tidy: one fewer warning than part 4.
  • One flaky test: CloseWaitsForInFlightWrite (a 2 MB write racing close()) timed out once at 30 s in a filtered run. It passed 3/3 on its own and in the full suite. Worth watching in CI.
  • Every commit builds and passes the unit tests.
  • Formatting: clang-format is clean on all changed files. Docs build OK. The release symbol leak check is OK.
  • The 3 failing integration tests are the DataTrackE2ETest schema tests (DefineAndGetSchema, DefineAndGetCustomSchemaEncoding, DefineDuplicateSchemaFails). They fail on main on this machine too, and the 1 skip is the token-server test.
  • Windows and Linux are covered only by CI.

🤖 Generated with Claude Code

1egoman and others added 2 commits November 30, 2026 20:49
…Bytes

TextStreamWriter / ByteStreamWriter now open an FFI writer
(text/byte_stream_open) and send through text/byte_stream_write and
*_stream_close instead of hand-built header/chunk/trailer packets. Rust does
the chunking and UTF-8 splitting.

- LocalParticipant::streamText / streamBytes return an already-open writer
  (blocking until the open callback, throwing DataStreamError on failure),
  taking the same StreamTextOptions / StreamByteOptions as the one-shot
  sends, including the text operation type, version and generated flag.
  The public writer constructors keep working but are deprecated; they open
  lazily on the first write or close, as the header did before.
- write() and close() share one mutex and each waits for its FFI callback,
  so a close never overtakes an in-flight write (python-sdks#879); this also
  fixes close() not taking the write lock.
- A LocalParticipant tracks its writers in a registry shared with them. On
  disconnect (or participant destruction) every writer is closed and its
  handle dropped, so later write/close throw DataStreamError instead of
  reaching an FFI session that would panic.
- A writer destroyed without close() sends a non-waiting FFI close. Rust's
  writer only sends its drop-time trailer from a Tokio thread, so a plain
  handle drop from C++ left receivers waiting forever.
- kStreamChunkSize and the protected ensureHeaderSent / sendChunk /
  sendTrailer helpers are deprecated and reimplemented on the new path.
- Removed FfiClient::sendStream*Async, splitUtf8 and the now-unused
  header/chunk/trailer converters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ports the Rust outgoing-manager tests that streamText / streamBytes can
reproduce, checking the raw packets the receiving room gets: each write
is sent as it happens, writers never compress or inline, multi-write
streams split at the 15000-byte MTU and text on UTF-8 boundaries, close
attributes and reasons reach the trailer, oversized attributes throw, a
destroyed writer still sends its trailer, and packets are attributed to
the sender. Also covers targeted destinations and the sender_identity
option across one-shot sends and writers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@1egoman

1egoman commented Nov 30, 2026

Copy link
Copy Markdown
Author

CI for this branch (manually dispatched, since ci.yml only runs on PRs into main): https://github.com/livekit/client-sdk-cpp/actions/runs/46380684325

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.

1 participant