From f6cfa7a0f52725371b74e5c8ff229ad8ee32efb9 Mon Sep 17 00:00:00 2001 From: Ryan Gaus Date: Mon, 23 Nov 2026 08:52:41 +1300 Subject: [PATCH 1/2] Add data stream conformance test harness Shared helpers for porting the Rust livekit-data-stream crate's tests to end-to-end C++ tests: - FfiPeer drives a room's participant through the raw FFI, so it sends exactly what another FFI-based SDK sends: one-shot sends and stream writers go through the Rust outgoing manager (data streams v2), and raw header/chunk/trailer requests send hand-built v1 packets. - RawPacketSniffer records the raw stream packets a room receives. - Text/ByteStreamCollector receive through the public C++ API and record content, info and errors. The harness only uses API that predates the FFI reader/writer port, so tests built on it compile before and after that port. Also mint an optional LIVEKIT_TOKEN_C (cpp-test-c) for tests that need a third participant, and add RoomTestAccess::roomHandleId. Co-Authored-By: Claude Opus 5.5 --- docs/testing.md | 13 +- scripts/set-test-tokens.sh | 7 +- src/tests/common/data_stream_test_utils.h | 1055 +++++++++++++++++++++ src/tests/common/room_test_access.h | 8 + 4 files changed, 1080 insertions(+), 3 deletions(-) create mode 100644 src/tests/common/data_stream_test_utils.h diff --git a/docs/testing.md b/docs/testing.md index 86202f5b..7e0a4d0e 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -76,6 +76,10 @@ export LIVEKIT_URL="ws://localhost:7880" # or wss://your-server.livek export LIVEKIT_TOKEN_A="" export LIVEKIT_TOKEN_B="" +# Optional: a third participant (same room) for data stream tests that need a +# bystander, e.g. targeted streams. Tests that need it skip when it is unset. +export LIVEKIT_TOKEN_C="" + # Optional (for stress tests) export RPC_STRESS_DURATION_SECONDS=3600 # Test duration (default: 1 hour) export RPC_STRESS_CALLER_THREADS=4 # Concurrent caller threads (default: 4) @@ -83,9 +87,10 @@ export RPC_STRESS_CALLER_THREADS=4 # Concurrent caller threads (default: ### Generating tokens for the test suites -The easiest path is to source the helper script, which mints both +The easiest path is to source the helper script, which mints the participant tokens against a local `livekit-server --dev` and exports -`LIVEKIT_TOKEN_A`, `LIVEKIT_TOKEN_B`, and `LIVEKIT_URL` for the current shell: +`LIVEKIT_TOKEN_A`, `LIVEKIT_TOKEN_B`, `LIVEKIT_TOKEN_C`, and `LIVEKIT_URL` for +the current shell: ```bash source scripts/set-test-tokens.sh @@ -103,6 +108,10 @@ export LIVEKIT_TOKEN_B="$(lk token create --api-key devkey --api-secret secret - --join --valid-for 99999h --room cpp_data_track_test \ --grant '{"canPublish":true,"canSubscribe":true,"canPublishData":true}' \ --token-only)" +export LIVEKIT_TOKEN_C="$(lk token create --api-key devkey --api-secret secret -i cpp-test-c \ + --join --valid-for 99999h --room cpp_data_track_test \ + --grant '{"canPublish":true,"canSubscribe":true,"canPublishData":true}' \ + --token-only)" ``` ## Installed SDK packaging tests diff --git a/scripts/set-test-tokens.sh b/scripts/set-test-tokens.sh index 08370f16..6b068f40 100755 --- a/scripts/set-test-tokens.sh +++ b/scripts/set-test-tokens.sh @@ -48,6 +48,8 @@ fi LIVEKIT_ROOM="cpp_data_track_test" LIVEKIT_IDENTITY_A="cpp-test-a" LIVEKIT_IDENTITY_B="cpp-test-b" +# Third participant for tests that need a bystander (e.g. targeted data streams). +LIVEKIT_IDENTITY_C="cpp-test-c" if [[ $# -ne 0 ]]; then _fail "this script is hard-coded and does not accept arguments" 2 @@ -107,22 +109,25 @@ _create_token() { LIVEKIT_TOKEN_A="$(_create_token "$LIVEKIT_IDENTITY_A")" LIVEKIT_TOKEN_B="$(_create_token "$LIVEKIT_IDENTITY_B")" +LIVEKIT_TOKEN_C="$(_create_token "$LIVEKIT_IDENTITY_C")" _apply() { export LIVEKIT_TOKEN_A export LIVEKIT_TOKEN_B + export LIVEKIT_TOKEN_C export LIVEKIT_URL } _emit_eval() { printf 'export LIVEKIT_TOKEN_A=%q\n' "$LIVEKIT_TOKEN_A" printf 'export LIVEKIT_TOKEN_B=%q\n' "$LIVEKIT_TOKEN_B" + printf 'export LIVEKIT_TOKEN_C=%q\n' "$LIVEKIT_TOKEN_C" printf 'export LIVEKIT_URL=%q\n' "$LIVEKIT_URL" } if [[ "$_sourced" -eq 1 ]]; then _apply - echo "LIVEKIT_TOKEN_A, LIVEKIT_TOKEN_B, and LIVEKIT_URL set for this shell." >&2 + echo "LIVEKIT_TOKEN_A, LIVEKIT_TOKEN_B, LIVEKIT_TOKEN_C, and LIVEKIT_URL set for this shell." >&2 else _emit_eval echo "set_test_tokens: for this shell run: source $0 or: eval \"\$(bash $0)\"" >&2 diff --git a/src/tests/common/data_stream_test_utils.h b/src/tests/common/data_stream_test_utils.h new file mode 100644 index 00000000..688aa7a6 --- /dev/null +++ b/src/tests/common/data_stream_test_utils.h @@ -0,0 +1,1055 @@ +/* + * Copyright 2026 LiveKit + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Shared harness for the data stream conformance tests, which port the Rust +// `livekit-data-stream` crate's unit tests to end-to-end tests against a real +// SFU. Three pieces: +// +// * FfiPeer - drives a connected Room's local participant through the +// raw FFI, bypassing the C++ data stream API. It sends +// exactly what another FFI-based SDK (Python, Unity, ...) +// would: one-shot sends and stream writers go through the +// Rust outgoing manager (so they use data streams v2: +// inline / compressed packets), and raw header / chunk / +// trailer requests send hand-built v1 packets. +// * RawPacketSniffer - records the raw stream packets a room receives +// (`stream_{header,chunk,trailer}_received` room events). +// The FFI forwards neither `inline_content` nor +// `compression` on the header, so an inline stream shows +// up as a header with no chunk or trailer, and compression +// shows up as chunk bytes that differ from the plaintext. +// * Text/ByteStreamCollector - receives streams through the public C++ API +// and records content, info, and any error. +// +// Everything here uses only API that predates the FFI reader/writer port, so +// receive-side tests built on it compile both before and after that port. + +#pragma once + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "data_stream.pb.h" +#include "ffi.pb.h" +#include "ffi_client.h" +#include "room.pb.h" +#include "room_test_access.h" +#include "test_common.h" + +namespace livekit::test::ds { + +using namespace std::chrono_literals; + +/// How long to wait for something that should happen. +constexpr auto kStreamTimeout = 10s; +/// How long to wait to be confident something did NOT happen (e.g. no chunk +/// packets for an inline stream, or no stream for a non-targeted participant). +constexpr auto kQuietPeriod = 1500ms; + +/// Chunk size the Rust core splits outgoing streams at (`STREAM_CHUNK_SIZE`). +constexpr std::size_t kMtu = 15'000; + +inline std::string uniqueTopic(const std::string& name) { + static std::atomic counter{0}; + return "ds_conf_" + name + "_" + std::to_string(getTimestampUs()) + "_" + std::to_string(counter++); +} + +inline std::string uniqueStreamId(const std::string& name) { + static std::atomic counter{0}; + return "sid_" + name + "_" + std::to_string(getTimestampUs()) + "_" + std::to_string(counter++); +} + +// --------------------------------------------------------------------------- +// Payload generators +// --------------------------------------------------------------------------- + +/// `n` bytes of a short repeated phrase: deflate shrinks this enormously. +inline std::string compressibleText(std::size_t n) { + static const std::string phrase = "the quick brown fox jumps over the lazy dog. "; + std::string out; + out.reserve(n); + while (out.size() < n) { + out += phrase; + } + out.resize(n); + return out; +} + +/// Deterministic pseudo-random bytes (xorshift64*): deflate cannot shrink it. +inline std::string randomBytes(std::size_t n, std::uint64_t seed = 0x9E3779B97F4A7C15ULL) { + std::string out(n, '\0'); + std::uint64_t x = seed; + for (std::size_t i = 0; i < n; ++i) { + x ^= x >> 12; + x ^= x << 25; + x ^= x >> 27; + out[i] = static_cast((x * 0x2545F4914F6CDD1DULL) >> 56); + } + return out; +} + +/// Deterministic pseudo-random text over 64 ASCII symbols: valid UTF-8 that +/// deflate shrinks only modestly (6 bits of entropy per 8-bit byte). +inline std::string randomText(std::size_t n, std::uint64_t seed = 0xD1B54A32D192ED03ULL) { + static const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + const std::string bytes = randomBytes(n, seed); + std::string out(n, '\0'); + for (std::size_t i = 0; i < n; ++i) { + out[i] = alphabet[static_cast(bytes[i]) % 64]; + } + return out; +} + +inline std::vector toBytes(const std::string& s) { return {s.begin(), s.end()}; } +inline std::string fromBytes(const std::vector& v) { return {v.begin(), v.end()}; } + +/// True if `s` is well-formed UTF-8 (no truncated or stray sequences). +inline bool isValidUtf8(const std::string& s) { + std::size_t i = 0; + while (i < s.size()) { + const auto c = static_cast(s[i]); + std::size_t len = 0; + if (c < 0x80) { + len = 1; + } else if ((c >> 5) == 0x6) { + len = 2; + } else if ((c >> 4) == 0xE) { + len = 3; + } else if ((c >> 3) == 0x1E) { + len = 4; + } else { + return false; + } + if (i + len > s.size()) { + return false; + } + for (std::size_t k = 1; k < len; ++k) { + if ((static_cast(s[i + k]) >> 6) != 0x2) { + return false; + } + } + i += len; + } + return true; +} + +// --------------------------------------------------------------------------- +// Rooms +// --------------------------------------------------------------------------- + +/// Connects three rooms (LIVEKIT_TOKEN_A/B/C). Returns an empty vector when +/// LIVEKIT_TOKEN_C is not set so callers can skip. +inline std::vector> threeTestRooms() { + const char* token_c = std::getenv("LIVEKIT_TOKEN_C"); + if (token_c == nullptr || token_c[0] == '\0') { + return {}; + } + const auto tokens = getDataTrackTestTokens(); + const std::vector all_tokens{tokens[0], tokens[1], token_c}; + std::vector> rooms; + for (std::size_t i = 0; i < all_tokens.size(); ++i) { + auto room = std::make_unique(); + if (!room->connect(kLocalTestLiveKitUrl, all_tokens[i], RoomOptions{})) { + throw std::runtime_error("Failed to connect test room " + std::to_string(i)); + } + rooms.push_back(std::move(room)); + } + waitForParticipantVisibility(rooms); + return rooms; +} + +inline std::string identityOf(const Room& room) { return lockLocalParticipant(room)->identity(); } + +// --------------------------------------------------------------------------- +// Raw packet sniffer +// --------------------------------------------------------------------------- + +struct RawHeader { + std::string sender; + std::string stream_id; + std::string topic; + std::string mime_type; + std::int64_t timestamp = 0; + std::optional total_length; + std::map attributes; + bool is_text = false; + // Text header + int operation_type = 0; + int version = 0; + std::string reply_to_stream_id; + std::vector attached_stream_ids; + bool generated = false; + // Byte header + std::string name; +}; + +struct RawChunk { + std::string sender; + std::string stream_id; + std::uint64_t chunk_index = 0; + std::string content; +}; + +struct RawTrailer { + std::string sender; + std::string stream_id; + std::string reason; + std::map attributes; +}; + +/// Records the raw data stream packets one room receives. Packets are copied +/// field by field into plain structs (never as protobuf messages: the test +/// binary links its own copy of the generated protobuf code). +class RawPacketSniffer { +public: + explicit RawPacketSniffer(const Room& room) : state_(std::make_shared()) { + state_->room_handle = RoomTestAccess::roomHandleId(room); + // The listener owns a reference to the state, so a callback that races + // with destruction never touches freed memory. + listener_id_ = FfiClient::instance().addListener( + [state = state_](const proto::FfiEvent& event) { State::onEvent(*state, event); }); + } + + ~RawPacketSniffer() { FfiClient::instance().removeListener(listener_id_); } + + RawPacketSniffer(const RawPacketSniffer&) = delete; + RawPacketSniffer& operator=(const RawPacketSniffer&) = delete; + RawPacketSniffer(RawPacketSniffer&&) = delete; + RawPacketSniffer& operator=(RawPacketSniffer&&) = delete; + + /// Waits for the first header on `topic`. + std::optional waitForHeader(const std::string& topic, + std::chrono::milliseconds timeout = kStreamTimeout) const { + std::unique_lock lock(state_->mutex); + std::optional found; + state_->cv.wait_for(lock, timeout, [&] { + for (const auto& h : state_->headers) { + if (h.topic == topic) { + found = h; + return true; + } + } + return false; + }); + return found; + } + + /// Waits for the trailer of `stream_id`. + std::optional waitForTrailer(const std::string& stream_id, + std::chrono::milliseconds timeout = kStreamTimeout) const { + std::unique_lock lock(state_->mutex); + std::optional found; + state_->cv.wait_for(lock, timeout, [&] { + for (const auto& t : state_->trailers) { + if (t.stream_id == stream_id) { + found = t; + return true; + } + } + return false; + }); + return found; + } + + /// Waits until at least `count` chunks of `stream_id` have arrived. + bool waitForChunks(const std::string& stream_id, std::size_t count, + std::chrono::milliseconds timeout = kStreamTimeout) const { + std::unique_lock lock(state_->mutex); + return state_->cv.wait_for(lock, timeout, [&] { return countChunksLocked(stream_id) >= count; }); + } + + std::vector headers(const std::string& topic) const { + const std::scoped_lock lock(state_->mutex); + std::vector out; + for (const auto& h : state_->headers) { + if (h.topic == topic) { + out.push_back(h); + } + } + return out; + } + + std::vector chunks(const std::string& stream_id) const { + const std::scoped_lock lock(state_->mutex); + std::vector out; + for (const auto& c : state_->chunks) { + if (c.stream_id == stream_id) { + out.push_back(c); + } + } + return out; + } + + std::vector trailers(const std::string& stream_id) const { + const std::scoped_lock lock(state_->mutex); + std::vector out; + for (const auto& t : state_->trailers) { + if (t.stream_id == stream_id) { + out.push_back(t); + } + } + return out; + } + + /// Concatenated raw chunk content of `stream_id`, in arrival order. + std::string rawContent(const std::string& stream_id) const { + std::string out; + for (const auto& c : chunks(stream_id)) { + out += c.content; + } + return out; + } + +private: + struct State { + std::uint64_t room_handle = 0; + std::mutex mutex; + std::condition_variable cv; + std::vector headers; + std::vector chunks; + std::vector trailers; + + static void onEvent(State& state, const proto::FfiEvent& event) { + if (event.message_case() != proto::FfiEvent::kRoomEvent) { + return; + } + const auto& re = event.room_event(); + if (re.room_handle() != state.room_handle) { + return; + } + switch (re.message_case()) { + case proto::RoomEvent::kStreamHeaderReceived: { + const auto& ev = re.stream_header_received(); + const auto& h = ev.header(); + RawHeader out; + out.sender = ev.participant_identity(); + out.stream_id = h.stream_id(); + out.topic = h.topic(); + out.mime_type = h.mime_type(); + out.timestamp = h.timestamp(); + if (h.has_total_length()) { + out.total_length = h.total_length(); + } + for (const auto& kv : h.attributes()) { + out.attributes.emplace(kv.first, kv.second); + } + if (h.has_text_header()) { + const auto& th = h.text_header(); + out.is_text = true; + out.operation_type = static_cast(th.operation_type()); + out.version = th.version(); + out.reply_to_stream_id = th.reply_to_stream_id(); + out.attached_stream_ids.assign(th.attached_stream_ids().begin(), th.attached_stream_ids().end()); + out.generated = th.generated(); + } else if (h.has_byte_header()) { + out.name = h.byte_header().name(); + } + push(state, [&] { state.headers.push_back(std::move(out)); }); + break; + } + case proto::RoomEvent::kStreamChunkReceived: { + const auto& ev = re.stream_chunk_received(); + RawChunk out; + out.sender = ev.participant_identity(); + out.stream_id = ev.chunk().stream_id(); + out.chunk_index = ev.chunk().chunk_index(); + out.content = ev.chunk().content(); + push(state, [&] { state.chunks.push_back(std::move(out)); }); + break; + } + case proto::RoomEvent::kStreamTrailerReceived: { + const auto& ev = re.stream_trailer_received(); + RawTrailer out; + out.sender = ev.participant_identity(); + out.stream_id = ev.trailer().stream_id(); + out.reason = ev.trailer().reason(); + for (const auto& kv : ev.trailer().attributes()) { + out.attributes.emplace(kv.first, kv.second); + } + push(state, [&] { state.trailers.push_back(std::move(out)); }); + break; + } + default: + break; + } + } + + template + static void push(State& state, F&& f) { + { + const std::scoped_lock lock(state.mutex); + f(); + } + state.cv.notify_all(); + } + }; + + std::size_t countChunksLocked(const std::string& stream_id) const { + std::size_t n = 0; + for (const auto& c : state_->chunks) { + if (c.stream_id == stream_id) { + ++n; + } + } + return n; + } + + std::shared_ptr state_; + FfiClient::ListenerId listener_id_ = 0; +}; + +// --------------------------------------------------------------------------- +// FfiPeer: a remote participant driven through the raw FFI +// --------------------------------------------------------------------------- + +struct PeerTextOptions { + std::string topic; + std::map attributes; + std::vector destination_identities; + std::optional stream_id; + std::optional reply_to_stream_id; + std::vector attached_stream_ids; + std::optional operation_type; + std::optional version; + std::optional generated; + std::optional compress; + std::optional sender_identity; +}; + +struct PeerByteOptions { + std::string topic; + std::map attributes; + std::vector destination_identities; + std::optional stream_id; + std::optional name; + std::optional mime_type; + std::optional total_length; + std::optional compress; + std::optional sender_identity; +}; + +/// A raw v1 header. Field meaning matches `DataStream.Header` on the wire. +struct PeerRawHeader { + std::string stream_id; + std::string topic; + std::string mime_type = "text/plain"; + std::optional total_length; + std::map attributes; + bool is_text = true; + std::string name; // byte streams only +}; + +/// Outcome of an FFI request that completes with an async callback. +struct PeerResult { + bool ok = false; + std::string error; + std::string stream_id; + std::uint64_t writer_handle = 0; +}; + +class FfiPeer { +public: + explicit FfiPeer(const Room& room) + : participant_handle_(lockLocalParticipant(room)->ffiHandleId()), identity_(identityOf(room)) {} + + const std::string& identity() const { return identity_; } + + // ---- One-shot sends (Rust outgoing manager: v2 inline / compressed) ---- + + PeerResult sendText(const std::string& text, const PeerTextOptions& opts) const { + proto::FfiRequest req; + auto* r = req.mutable_send_text(); + const auto id = nextAsyncId(); + r->set_local_participant_handle(participant_handle_); + r->set_text(text); + r->set_request_async_id(id); + fillTextOptions(*r->mutable_options(), opts); + return call(req, id, [](const proto::FfiEvent& e, std::uint64_t async_id) -> std::optional { + if (e.message_case() != proto::FfiEvent::kSendText || e.send_text().async_id() != async_id) { + return std::nullopt; + } + PeerResult res; + if (e.send_text().has_info()) { + res.ok = true; + res.stream_id = e.send_text().info().stream_id(); + } else { + res.error = e.send_text().error().description(); + } + return res; + }); + } + + PeerResult sendBytes(const std::string& bytes, const PeerByteOptions& opts) const { + proto::FfiRequest req; + auto* r = req.mutable_send_bytes(); + const auto id = nextAsyncId(); + r->set_local_participant_handle(participant_handle_); + r->set_bytes(bytes); + r->set_request_async_id(id); + fillByteOptions(*r->mutable_options(), opts); + return call(req, id, [](const proto::FfiEvent& e, std::uint64_t async_id) -> std::optional { + if (e.message_case() != proto::FfiEvent::kSendBytes || e.send_bytes().async_id() != async_id) { + return std::nullopt; + } + PeerResult res; + if (e.send_bytes().has_info()) { + res.ok = true; + res.stream_id = e.send_bytes().info().stream_id(); + } else { + res.error = e.send_bytes().error().description(); + } + return res; + }); + } + + // ---- Stream writers (Rust outgoing manager: chunked, never inline) ---- + + PeerResult openText(const PeerTextOptions& opts) const { + proto::FfiRequest req; + auto* r = req.mutable_text_stream_open(); + const auto id = nextAsyncId(); + r->set_local_participant_handle(participant_handle_); + r->set_request_async_id(id); + fillTextOptions(*r->mutable_options(), opts); + return call(req, id, [](const proto::FfiEvent& e, std::uint64_t async_id) -> std::optional { + if (e.message_case() != proto::FfiEvent::kTextStreamOpen || e.text_stream_open().async_id() != async_id) { + return std::nullopt; + } + PeerResult res; + if (e.text_stream_open().has_writer()) { + res.ok = true; + res.writer_handle = e.text_stream_open().writer().handle().id(); + res.stream_id = e.text_stream_open().writer().info().stream_id(); + } else { + res.error = e.text_stream_open().error().description(); + } + return res; + }); + } + + PeerResult writeText(std::uint64_t writer_handle, const std::string& text) const { + proto::FfiRequest req; + auto* r = req.mutable_text_stream_write(); + const auto id = nextAsyncId(); + r->set_writer_handle(writer_handle); + r->set_text(text); + r->set_request_async_id(id); + return call(req, id, [](const proto::FfiEvent& e, std::uint64_t async_id) -> std::optional { + if (e.message_case() != proto::FfiEvent::kTextStreamWriterWrite || + e.text_stream_writer_write().async_id() != async_id) { + return std::nullopt; + } + return errorResult(e.text_stream_writer_write().has_error(), e.text_stream_writer_write().error().description()); + }); + } + + PeerResult closeText(std::uint64_t writer_handle, const std::string& reason = "", + const std::map& attributes = {}) const { + proto::FfiRequest req; + auto* r = req.mutable_text_stream_close(); + const auto id = nextAsyncId(); + r->set_writer_handle(writer_handle); + if (!reason.empty()) { + r->set_reason(reason); + } + for (const auto& kv : attributes) { + (*r->mutable_attributes())[kv.first] = kv.second; + } + r->set_request_async_id(id); + return call(req, id, [](const proto::FfiEvent& e, std::uint64_t async_id) -> std::optional { + if (e.message_case() != proto::FfiEvent::kTextStreamWriterClose || + e.text_stream_writer_close().async_id() != async_id) { + return std::nullopt; + } + return errorResult(e.text_stream_writer_close().has_error(), e.text_stream_writer_close().error().description()); + }); + } + + PeerResult openBytes(const PeerByteOptions& opts) const { + proto::FfiRequest req; + auto* r = req.mutable_byte_stream_open(); + const auto id = nextAsyncId(); + r->set_local_participant_handle(participant_handle_); + r->set_request_async_id(id); + fillByteOptions(*r->mutable_options(), opts); + return call(req, id, [](const proto::FfiEvent& e, std::uint64_t async_id) -> std::optional { + if (e.message_case() != proto::FfiEvent::kByteStreamOpen || e.byte_stream_open().async_id() != async_id) { + return std::nullopt; + } + PeerResult res; + if (e.byte_stream_open().has_writer()) { + res.ok = true; + res.writer_handle = e.byte_stream_open().writer().handle().id(); + res.stream_id = e.byte_stream_open().writer().info().stream_id(); + } else { + res.error = e.byte_stream_open().error().description(); + } + return res; + }); + } + + PeerResult writeBytes(std::uint64_t writer_handle, const std::string& bytes) const { + proto::FfiRequest req; + auto* r = req.mutable_byte_stream_write(); + const auto id = nextAsyncId(); + r->set_writer_handle(writer_handle); + r->set_bytes(bytes); + r->set_request_async_id(id); + return call(req, id, [](const proto::FfiEvent& e, std::uint64_t async_id) -> std::optional { + if (e.message_case() != proto::FfiEvent::kByteStreamWriterWrite || + e.byte_stream_writer_write().async_id() != async_id) { + return std::nullopt; + } + return errorResult(e.byte_stream_writer_write().has_error(), e.byte_stream_writer_write().error().description()); + }); + } + + PeerResult closeBytes(std::uint64_t writer_handle, const std::string& reason = "", + const std::map& attributes = {}) const { + proto::FfiRequest req; + auto* r = req.mutable_byte_stream_close(); + const auto id = nextAsyncId(); + r->set_writer_handle(writer_handle); + if (!reason.empty()) { + r->set_reason(reason); + } + for (const auto& kv : attributes) { + (*r->mutable_attributes())[kv.first] = kv.second; + } + r->set_request_async_id(id); + return call(req, id, [](const proto::FfiEvent& e, std::uint64_t async_id) -> std::optional { + if (e.message_case() != proto::FfiEvent::kByteStreamWriterClose || + e.byte_stream_writer_close().async_id() != async_id) { + return std::nullopt; + } + return errorResult(e.byte_stream_writer_close().has_error(), e.byte_stream_writer_close().error().description()); + }); + } + + // ---- Hand-built v1 packets ---- + + PeerResult sendRawHeader(const PeerRawHeader& h, const std::vector& destinations = {}) const { + proto::FfiRequest req; + auto* r = req.mutable_send_stream_header(); + const auto id = nextAsyncId(); + r->set_local_participant_handle(participant_handle_); + r->set_sender_identity(identity_); + r->set_request_async_id(id); + for (const auto& d : destinations) { + r->add_destination_identities(d); + } + auto* header = r->mutable_header(); + header->set_stream_id(h.stream_id); + header->set_timestamp(static_cast(getTimestampUs() / 1000)); + header->set_mime_type(h.mime_type); + header->set_topic(h.topic); + if (h.total_length) { + header->set_total_length(*h.total_length); + } + for (const auto& kv : h.attributes) { + (*header->mutable_attributes())[kv.first] = kv.second; + } + if (h.is_text) { + header->mutable_text_header()->set_operation_type(proto::DataStream::CREATE); + } else { + header->mutable_byte_header()->set_name(h.name); + } + return call(req, id, [](const proto::FfiEvent& e, std::uint64_t async_id) -> std::optional { + if (e.message_case() != proto::FfiEvent::kSendStreamHeader || e.send_stream_header().async_id() != async_id) { + return std::nullopt; + } + return errorResult(e.send_stream_header().has_error(), e.send_stream_header().error()); + }); + } + + PeerResult sendRawChunk(const std::string& stream_id, std::uint64_t chunk_index, const std::string& content, + const std::vector& destinations = {}) const { + proto::FfiRequest req; + auto* r = req.mutable_send_stream_chunk(); + const auto id = nextAsyncId(); + r->set_local_participant_handle(participant_handle_); + r->set_sender_identity(identity_); + r->set_request_async_id(id); + for (const auto& d : destinations) { + r->add_destination_identities(d); + } + auto* chunk = r->mutable_chunk(); + chunk->set_stream_id(stream_id); + chunk->set_chunk_index(chunk_index); + chunk->set_content(content); + return call(req, id, [](const proto::FfiEvent& e, std::uint64_t async_id) -> std::optional { + if (e.message_case() != proto::FfiEvent::kSendStreamChunk || e.send_stream_chunk().async_id() != async_id) { + return std::nullopt; + } + return errorResult(e.send_stream_chunk().has_error(), e.send_stream_chunk().error()); + }); + } + + PeerResult sendRawTrailer(const std::string& stream_id, const std::string& reason = "", + const std::map& attributes = {}, + const std::vector& destinations = {}) const { + proto::FfiRequest req; + auto* r = req.mutable_send_stream_trailer(); + const auto id = nextAsyncId(); + r->set_local_participant_handle(participant_handle_); + r->set_sender_identity(identity_); + r->set_request_async_id(id); + for (const auto& d : destinations) { + r->add_destination_identities(d); + } + auto* trailer = r->mutable_trailer(); + trailer->set_stream_id(stream_id); + trailer->set_reason(reason); + for (const auto& kv : attributes) { + (*trailer->mutable_attributes())[kv.first] = kv.second; + } + return call(req, id, [](const proto::FfiEvent& e, std::uint64_t async_id) -> std::optional { + if (e.message_case() != proto::FfiEvent::kSendStreamTrailer || e.send_stream_trailer().async_id() != async_id) { + return std::nullopt; + } + return errorResult(e.send_stream_trailer().has_error(), e.send_stream_trailer().error()); + }); + } + +private: + using Extract = std::function(const proto::FfiEvent&, std::uint64_t)>; + + static std::uint64_t nextAsyncId() { + // Far above FfiClient's own counter (which starts at 1) so ids never collide. + static std::atomic next{std::uint64_t{1} << 62}; + return next++; + } + + static PeerResult errorResult(bool has_error, const std::string& error) { + PeerResult res; + res.ok = !has_error; + if (has_error) { + res.error = error; + } + return res; + } + + static void fillTextOptions(proto::StreamTextOptions& o, const PeerTextOptions& opts) { + o.set_topic(opts.topic); + for (const auto& kv : opts.attributes) { + (*o.mutable_attributes())[kv.first] = kv.second; + } + for (const auto& d : opts.destination_identities) { + o.add_destination_identities(d); + } + if (opts.stream_id) { + o.set_id(*opts.stream_id); + } + if (opts.reply_to_stream_id) { + o.set_reply_to_stream_id(*opts.reply_to_stream_id); + } + for (const auto& a : opts.attached_stream_ids) { + o.add_attached_stream_ids(a); + } + if (opts.operation_type) { + o.set_operation_type(static_cast(*opts.operation_type)); + } + if (opts.version) { + o.set_version(*opts.version); + } + if (opts.generated) { + o.set_generated(*opts.generated); + } + if (opts.compress) { + o.set_compress(*opts.compress); + } + if (opts.sender_identity) { + o.set_sender_identity(*opts.sender_identity); + } + } + + static void fillByteOptions(proto::StreamByteOptions& o, const PeerByteOptions& opts) { + o.set_topic(opts.topic); + for (const auto& kv : opts.attributes) { + (*o.mutable_attributes())[kv.first] = kv.second; + } + for (const auto& d : opts.destination_identities) { + o.add_destination_identities(d); + } + if (opts.stream_id) { + o.set_id(*opts.stream_id); + } + if (opts.name) { + o.set_name(*opts.name); + } + if (opts.mime_type) { + o.set_mime_type(*opts.mime_type); + } + if (opts.total_length) { + o.set_total_length(*opts.total_length); + } + if (opts.compress) { + o.set_compress(*opts.compress); + } + if (opts.sender_identity) { + o.set_sender_identity(*opts.sender_identity); + } + } + + /// Sends `req` and waits for the callback `extract` recognizes. + static PeerResult call(const proto::FfiRequest& req, std::uint64_t async_id, const Extract& extract) { + struct Waiter { + std::mutex mutex; + std::condition_variable cv; + std::optional result; + }; + auto waiter = std::make_shared(); + const auto listener = FfiClient::instance().addListener([waiter, extract, async_id](const proto::FfiEvent& e) { + auto res = extract(e, async_id); + if (!res) { + return; + } + { + const std::scoped_lock lock(waiter->mutex); + waiter->result = std::move(res); + } + waiter->cv.notify_all(); + }); + + PeerResult out; + try { + (void)FfiClient::instance().sendRequest(req); + std::unique_lock lock(waiter->mutex); + if (waiter->cv.wait_for(lock, 30s, [&] { return waiter->result.has_value(); })) { + out = *waiter->result; + } else { + out.error = "timed out waiting for the FFI callback"; + } + } catch (const std::exception& e) { + out.error = e.what(); + } + FfiClient::instance().removeListener(listener); + return out; + } + + std::uint64_t participant_handle_; + std::string identity_; +}; + +// --------------------------------------------------------------------------- +// Receive side: public C++ API +// --------------------------------------------------------------------------- + +/// One stream as received through the public C++ API. +template +struct ReceivedStream { + std::string sender; + Info info; + std::string content; + std::size_t chunk_count = 0; + bool errored = false; + std::string error; +}; + +/// Registers a stream handler on `topic` and reads every stream that arrives +/// on its own thread. Readers that never end (e.g. a stream the SDK failed to +/// decode) leave their thread blocked; such threads are detached on +/// destruction, since the reader cannot be unblocked without API that only +/// exists after the FFI reader port. +template +class StreamCollector { +public: + StreamCollector() : state_(std::make_shared()) {} + + ~StreamCollector() { + if (room_ != nullptr) { + unregister(*room_, topic_); + } + std::vector threads; + { + const std::scoped_lock lock(state_->mutex); + threads.swap(state_->threads); + } + for (auto& t : threads) { + // Give readers a moment to finish, then detach the stragglers. + if (waitDone(t.get_id(), 2s)) { + t.join(); + } else { + t.detach(); + } + } + } + + StreamCollector(const StreamCollector&) = delete; + StreamCollector& operator=(const StreamCollector&) = delete; + StreamCollector(StreamCollector&&) = delete; + StreamCollector& operator=(StreamCollector&&) = delete; + + void registerOn(Room& room, const std::string& topic) { + room_ = &room; + topic_ = topic; + registerHandler(room, topic, [state = state_](std::shared_ptr reader, const std::string& identity) { + const std::scoped_lock lock(state->mutex); + state->threads.emplace_back([state, reader, identity] { read(*state, reader, identity); }); + }); + } + + /// Waits until `count` streams have finished (successfully or with an error). + bool waitFor(std::size_t count, std::chrono::milliseconds timeout = kStreamTimeout) const { + std::unique_lock lock(state_->mutex); + return state_->cv.wait_for(lock, timeout, [&] { return state_->finished.size() >= count; }); + } + + /// Waits until `count` streams have been opened (handler invoked). + bool waitForOpened(std::size_t count, std::chrono::milliseconds timeout = kStreamTimeout) const { + std::unique_lock lock(state_->mutex); + return state_->cv.wait_for(lock, timeout, [&] { return state_->opened >= count; }); + } + + std::size_t openedCount() const { + const std::scoped_lock lock(state_->mutex); + return state_->opened; + } + + std::vector> streams() const { + const std::scoped_lock lock(state_->mutex); + return state_->finished; + } + + /// The single finished stream; fails the test if there is not exactly one. + ReceivedStream only() const { + auto all = streams(); + EXPECT_EQ(all.size(), 1U); + return all.empty() ? ReceivedStream{} : all.front(); + } + +private: + struct State { + std::mutex mutex; + std::condition_variable cv; + std::vector threads; + std::vector done_threads; + std::vector> finished; + std::size_t opened = 0; + }; + + static void read(State& state, const std::shared_ptr& reader, const std::string& identity) { + { + const std::scoped_lock lock(state.mutex); + ++state.opened; + } + state.cv.notify_all(); + + ReceivedStream out; + out.sender = identity; + try { + Chunk chunk; + while (reader->readNext(chunk)) { + out.content.append(chunk.begin(), chunk.end()); + ++out.chunk_count; + } + } catch (const std::exception& e) { + out.errored = true; + out.error = e.what(); + } + // Read info after the stream ends so trailer attributes are included. + out.info = reader->info(); + { + const std::scoped_lock lock(state.mutex); + state.finished.push_back(std::move(out)); + state.done_threads.push_back(std::this_thread::get_id()); + } + state.cv.notify_all(); + } + + bool waitDone(std::thread::id id, std::chrono::milliseconds timeout) const { + std::unique_lock lock(state_->mutex); + return state_->cv.wait_for(lock, timeout, [&] { + for (const auto& d : state_->done_threads) { + if (d == id) { + return true; + } + } + return false; + }); + } + + using Chunk = std::conditional_t, std::string, std::vector>; + + template + static void registerHandler(Room& room, const std::string& topic, H handler) { + if constexpr (std::is_same_v) { + room.registerTextStreamHandler(topic, std::move(handler)); + } else { + room.registerByteStreamHandler(topic, std::move(handler)); + } + } + + static void unregister(Room& room, const std::string& topic) { + if constexpr (std::is_same_v) { + room.unregisterTextStreamHandler(topic); + } else { + room.unregisterByteStreamHandler(topic); + } + } + + std::shared_ptr state_; + Room* room_ = nullptr; + std::string topic_; +}; + +using TextStreamCollector = StreamCollector; +using ByteStreamCollector = StreamCollector; + +/// Base fixture: two connected rooms (A sends, B receives) per test. +class DataStreamConformanceTest : public LiveKitTestBase { +protected: + void SetUp() override { + LiveKitTestBase::SetUp(); + skipIfNotConfigured(); + if (IsSkipped()) { + return; + } + rooms_ = testRooms(2); + } + + void TearDown() override { + rooms_.clear(); + LiveKitTestBase::TearDown(); + } + + Room& senderRoom() { return *rooms_[0]; } + Room& receiverRoom() { return *rooms_[1]; } + std::string senderIdentity() { return identityOf(senderRoom()); } + std::string receiverIdentity() { return identityOf(receiverRoom()); } + + std::vector> rooms_; +}; + +} // namespace livekit::test::ds diff --git a/src/tests/common/room_test_access.h b/src/tests/common/room_test_access.h index 9509b00c..bb1c4baa 100644 --- a/src/tests/common/room_test_access.h +++ b/src/tests/common/room_test_access.h @@ -18,6 +18,7 @@ #include +#include #include #include #include @@ -33,6 +34,13 @@ struct RoomTestAccess { return room.listener_id_; } + /// FFI handle id of the connected room, used to match `room_event`s to this + /// room when several rooms share the process. Returns 0 when disconnected. + static std::uint64_t roomHandleId(const Room& room) { + const std::scoped_lock guard(room.lock_); + return room.room_handle_ ? room.room_handle_->get() : 0; + } + static void simulateScenario(Room& room, proto::SimulateScenarioKind scenario) { std::shared_ptr handle; { From e440a87d5bb86aa96b4df2801b4bf65267bd03f6 Mon Sep 17 00:00:00 2001 From: Ryan Gaus Date: Mon, 23 Nov 2026 08:52:42 +1300 Subject: [PATCH 2/2] Add v1 receive conformance tests that pass today Ports the incoming-manager tests of the Rust livekit-data-stream crate that the current C++ receive path already handles: v1 text and byte round trips, trailer attribute merging, empty chunks, and a remote text writer's trailer attributes. The other v1 tests (duplicate chunks, length and gap errors, close reasons, orphan packets, disconnects) fail on main and land with the receive-path fix that makes them pass. Co-Authored-By: Claude Opus 5.5 --- ...est_data_stream_conformance_receive_v1.cpp | 166 ++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 src/tests/integration/test_data_stream_conformance_receive_v1.cpp diff --git a/src/tests/integration/test_data_stream_conformance_receive_v1.cpp b/src/tests/integration/test_data_stream_conformance_receive_v1.cpp new file mode 100644 index 00000000..7c5137b2 --- /dev/null +++ b/src/tests/integration/test_data_stream_conformance_receive_v1.cpp @@ -0,0 +1,166 @@ +/* + * Copyright 2026 LiveKit + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Receive-side data stream conformance, v1 (multi-packet, uncompressed) +// streams: a remote participant sends streams exactly as another SDK would, +// and the C++ SDK must receive them correctly. Covers hand-built v1 packets, +// remote stream writers, and participant disconnects mid-stream. +// +// Ported from the incoming-manager tests of the Rust `livekit-data-stream` +// crate (client-sdk-rust/livekit-data-stream/src/incoming/manager.rs). The +// remote side is an FfiPeer: it drives the sender room's participant through +// the raw FFI, so its one-shot sends go through the Rust outgoing manager and +// use data streams v2 (inline and compressed packets) just like the Python or +// Rust SDKs, and its raw requests send hand-built v1 packets. The C++ side +// receives through the public API (stream handlers + readers) only. +// +// Run with: +// ./build-debug/bin/livekit_integration_tests --gtest_filter=DataStreamReceiveV1* + +#include + +#include +#include + +#include "../common/data_stream_test_utils.h" + +namespace livekit::test::ds { + +class DataStreamReceiveV1Test : public DataStreamConformanceTest {}; + +// --------------------------------------------------------------------------- +// v1 multi-packet streams (hand-built header / chunk / trailer packets) +// --------------------------------------------------------------------------- + +// Rust: v1_text_stream_round_trips +TEST_F(DataStreamReceiveV1Test, V1TextStreamRoundTrips) { + const auto topic = uniqueTopic("v1_text"); + const auto sid = uniqueStreamId("v1_text"); + TextStreamCollector collector; + collector.registerOn(receiverRoom(), topic); + FfiPeer peer(senderRoom()); + + ASSERT_TRUE(peer.sendRawHeader({sid, topic, "text/plain", 11, {{"foo", "bar"}}}).ok); + ASSERT_TRUE(peer.sendRawChunk(sid, 0, "hello world").ok); + ASSERT_TRUE(peer.sendRawTrailer(sid).ok); + + ASSERT_TRUE(collector.waitFor(1)); + const auto s = collector.only(); + EXPECT_FALSE(s.errored) << s.error; + EXPECT_EQ(s.content, "hello world"); + EXPECT_EQ(s.sender, peer.identity()); + EXPECT_EQ(s.info.stream_id, sid); + EXPECT_EQ(s.info.topic, topic); + EXPECT_EQ(s.info.mime_type, "text/plain"); + ASSERT_TRUE(s.info.size.has_value()); + EXPECT_EQ(*s.info.size, 11U); + EXPECT_EQ(s.info.attributes.at("foo"), "bar"); +} + +// Rust: v1_byte_stream_round_trips +TEST_F(DataStreamReceiveV1Test, V1ByteStreamRoundTrips) { + const auto topic = uniqueTopic("v1_bytes"); + const auto sid = uniqueStreamId("v1_bytes"); + ByteStreamCollector collector; + collector.registerOn(receiverRoom(), topic); + FfiPeer peer(senderRoom()); + + PeerRawHeader header{sid, topic, "application/octet-stream", 4}; + header.is_text = false; + header.name = "file"; + ASSERT_TRUE(peer.sendRawHeader(header).ok); + ASSERT_TRUE(peer.sendRawChunk(sid, 0, std::string("\x01\x02\x03\x04", 4)).ok); + ASSERT_TRUE(peer.sendRawTrailer(sid).ok); + + ASSERT_TRUE(collector.waitFor(1)); + const auto s = collector.only(); + EXPECT_FALSE(s.errored) << s.error; + EXPECT_EQ(s.content, std::string("\x01\x02\x03\x04", 4)); + EXPECT_EQ(s.info.name, "file"); + EXPECT_EQ(s.info.mime_type, "application/octet-stream"); +} + +// Rust: v1_merges_trailer_attributes, v1_trailer_attributes_merged_after_close +TEST_F(DataStreamReceiveV1Test, V1TrailerAttributesMergedAfterClose) { + const auto topic = uniqueTopic("v1_trailer_attrs"); + const auto sid = uniqueStreamId("v1_trailer_attrs"); + TextStreamCollector collector; + collector.registerOn(receiverRoom(), topic); + FfiPeer peer(senderRoom()); + + ASSERT_TRUE(peer.sendRawHeader({sid, topic, "text/plain", 11, {{"foo", "bar"}, {"baz", "quux"}}}).ok); + ASSERT_TRUE(peer.sendRawChunk(sid, 0, "hello world").ok); + ASSERT_TRUE(peer.sendRawTrailer(sid, "", {{"hello", "world"}, {"foo", "updated"}}).ok); + + ASSERT_TRUE(collector.waitFor(1)); + const auto s = collector.only(); + EXPECT_FALSE(s.errored) << s.error; + EXPECT_EQ(s.content, "hello world"); + const std::map expected{{"baz", "quux"}, {"hello", "world"}, {"foo", "updated"}}; + EXPECT_EQ(s.info.attributes, expected) << "trailer attributes are merged over the header's"; +} + +// Rust: empty_chunks_are_ignored +TEST_F(DataStreamReceiveV1Test, EmptyChunksAreIgnored) { + const auto topic = uniqueTopic("v1_empty_chunk"); + const auto sid = uniqueStreamId("v1_empty_chunk"); + TextStreamCollector collector; + collector.registerOn(receiverRoom(), topic); + FfiPeer peer(senderRoom()); + + ASSERT_TRUE(peer.sendRawHeader({sid, topic, "text/plain", 11}).ok); + ASSERT_TRUE(peer.sendRawChunk(sid, 0, "").ok); + ASSERT_TRUE(peer.sendRawChunk(sid, 1, "hello world").ok); + ASSERT_TRUE(peer.sendRawTrailer(sid).ok); + + ASSERT_TRUE(collector.waitFor(1)); + const auto s = collector.only(); + EXPECT_FALSE(s.errored) << s.error; + EXPECT_EQ(s.content, "hello world"); +} + +// --------------------------------------------------------------------------- +// Remote stream writers (chunked, uncompressed, incremental) +// --------------------------------------------------------------------------- + +// Rust: v1_merges_trailer_attributes via a writer (close_with_options), +// progress_reports_completion_uncompressed_bytes (three writes) +TEST_F(DataStreamReceiveV1Test, RemoteTextWriterRoundTripsWithTrailerAttributes) { + const auto topic = uniqueTopic("remote_writer"); + TextStreamCollector collector; + collector.registerOn(receiverRoom(), topic); + FfiPeer peer(senderRoom()); + + PeerTextOptions opts; + opts.topic = topic; + opts.attributes = {{"foo", "bar"}}; + const auto writer = peer.openText(opts); + ASSERT_TRUE(writer.ok) << writer.error; + ASSERT_TRUE(peer.writeText(writer.writer_handle, "hello ").ok); + ASSERT_TRUE(peer.writeText(writer.writer_handle, "streaming ").ok); + ASSERT_TRUE(peer.writeText(writer.writer_handle, "world").ok); + ASSERT_TRUE(peer.closeText(writer.writer_handle, "", {{"result", "ok"}, {"foo", "updated"}}).ok); + + ASSERT_TRUE(collector.waitFor(1)); + const auto s = collector.only(); + EXPECT_FALSE(s.errored) << s.error; + EXPECT_EQ(s.content, "hello streaming world"); + EXPECT_EQ(s.info.stream_id, writer.stream_id); + const std::map expected{{"foo", "updated"}, {"result", "ok"}}; + EXPECT_EQ(s.info.attributes, expected); +} + +} // namespace livekit::test::ds