Skip to content

Data streams v2 (4/5): add ByteStreamReader::writeToFile and a receive size limit - #332

Draft
1egoman wants to merge 3 commits into
ds/3-oneshot-sendsfrom
ds/4-receive-additions
Draft

1egoman wants to merge 3 commits into
ds/3-oneshot-sendsfrom
ds/4-receive-additions

Conversation

@1egoman

@1egoman 1egoman commented Nov 30, 2026 •

Copy link
Copy Markdown

Part 4 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 👈 this PR
  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 stack replaces #321, #322 and #325 (same code, reordered so each step stands on its own).

Two receive-side additions.

ByteStreamReader::writeToFile()

Writes a received byte stream straight to disk through the SDK core.

  • The core rejects sender-chosen names that aren't plain file names: path separators, .., absolute paths, or empty names.
  • An optional directory (default: the system temp directory) and a name override can be passed.

Write-to-file and incremental reading both consume the FFI reader handle, so byte readers now start lazily:

  • the first readNext() / readAll() starts reading, or writeToFile() hands the stream to the core instead;
  • a reader that is closed or destroyed first drops its handle;
  • Room holds byte readers by weak_ptr, so a reader the app ignores releases its stream instead of being kept alive by the room.

Text readers are unchanged.

Receive size limit

RoomOptions::data_stream.max_payload_byte_length caps the decompressed size of each incoming stream. A stream that goes over it ends with a DataStreamError. When unset, the core's default of 5 GB applies.

Tests

File Added here
test_data_stream_conformance_write_to_file.cpp 12 tests: the 6 Rust file name tests, plus name override, temp directory default, a compressed multi-packet file, read-then-write rejected, no writeToFile() from the stream handler, and ignored readers not blocking later streams
test_data_stream_conformance_receive_api.cpp 6 size-limit tests: unknown total, declared total over the limit (rejected before any data), exactly at the limit, inline compressed and uncompressed, compressed multi-packet
test_data_streams.cpp, test_data_stream.cpp (unit) size limit end to end; RoomOptions conversion
Rust tests covered here
Rust test C++ test
write_to_file_accepts_plain_name DataStreamWriteToFileTest.AcceptsPlainName
write_to_file_rejects_traversal_in_stream_name DataStreamWriteToFileTest.RejectsTraversalInStreamName
write_to_file_rejects_traversal_in_name_override DataStreamWriteToFileTest.RejectsTraversalInNameOverride
write_to_file_rejects_absolute_path_in_stream_name DataStreamWriteToFileTest.RejectsAbsolutePathInStreamName
write_to_file_rejects_nested_path_in_stream_name DataStreamWriteToFileTest.RejectsNestedPathInStreamName
write_to_file_rejects_empty_stream_name DataStreamWriteToFileTest.RejectsEmptyStreamName
v1_max_payload_size_breached_with_unknown_total DataStreamReceiveApiCapTest.BreachedWithUnknownTotal
v1_max_payload_size_fast_fails_on_declared_total DataStreamReceiveApiCapTest.FastFailsOnDeclaredTotal
v1_payload_exactly_at_max_payload_size_succeeds DataStreamReceiveApiCapTest.PayloadExactlyAtCapSucceeds
v2_inline_uncompressed_max_payload_size_breached DataStreamReceiveApiCapTest.InlineUncompressedBreached
v2_inline_compressed_max_payload_size_breached DataStreamReceiveApiCapTest.InlineCompressedBreached
v2_max_payload_size_breached DataStreamReceiveApiCapTest.CompressedMultipacketBreached

Follow-ups from the #325 review

  • writeToFile replaces an existing file and follows a symlink at the destination. Both come from Rust's File::create. Do not overwrite or follow a symlink in write_to_file rust-sdks#1605 switches to create_new (approved, not yet released). Once it ships, bumping the submodule fixes it here; the doc line "An existing file is overwritten" then needs updating.
  • A completed but unread byte stream is lost on disconnect. Because of the lazy start, its data is still in the core when the room shuts down, and the reader gets "Disconnected while receiving". Fixing this means telling completed transfers apart from interrupted ones at shutdown.
  • RoomOptions layout. Adding data_stream changes the struct's layout, so an app must be recompiled (not just relinked) against the new headers. That matches earlier RoomOptions additions (v1.2.0, v1.7.0). A layout-stable alternative would be a separate setter or connect() overload.

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

  • Unit tests: 382/382.
  • Full integration suite: 156 pass, 3 fail (see below), 1 skip. All 89 data stream tests pass.
  • clang-tidy: no warnings beyond part 2's.
  • 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 3 commits November 30, 2026 20:47
Wrap the FFI's write-to-file so a received byte stream can be written
straight to disk by the SDK core, which also rejects sender-chosen file
names that are not plain file names (path separators, "..", absolute
paths, empty).

Write-to-file and incremental reading both consume the FFI reader
handle, so byte readers now start reading lazily: the first readNext() /
readAll() starts incremental reading, writeToFile() hands the stream to
the core instead, and a reader closed or destroyed unconsumed drops its
handle. Room tracks byte readers weakly so an ignored reader releases
its stream rather than being kept alive by the room.

Ports the Rust write_to_file tests end to end, plus the one-consumer
rule, the event-thread guard and ignored readers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
RoomOptions::data_stream.max_payload_byte_length caps how many
(decompressed) bytes each incoming data stream may carry. A stream that
exceeds it ends with a DataStreamError on its reader. When unset, the SDK
core's default (5 GB) applies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ports the Rust incoming-manager max_payload_byte_length tests through
RoomOptions::data_stream: a stream that grows past the limit, one whose
declared total exceeds it (rejected before any data), one exactly at the
limit, and inline (compressed and uncompressed) and compressed
multi-packet streams that breach it.

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/46380678667

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