From 623ba8c76e429c0a1f1fd3e0836af82e53e4c5d1 Mon Sep 17 00:00:00 2001 From: Ryan Gaus Date: Fri, 20 Nov 2026 13:46:14 +1300 Subject: [PATCH] Port data stream writers to FFI writer handles; add streamText/streamBytes 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). 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 --- AGENTS.md | 2 +- CMakeLists.txt | 1 + include/livekit/data_stream.h | 198 ++++++++-- include/livekit/local_participant.h | 50 ++- src/data_stream.cpp | 390 ++++++++++++++------ src/data_stream_writer_registry.h | 62 ++++ src/ffi_client.cpp | 215 ++++++++--- src/ffi_client.h | 25 +- src/local_participant.cpp | 25 +- src/room_proto_converter.cpp | 145 -------- src/room_proto_converter.h | 15 - src/tests/integration/test_data_streams.cpp | 278 ++++++++++++-- src/tests/unit/test_data_stream.cpp | 92 +++++ src/tests/unit/test_ffi_client.cpp | 25 +- 14 files changed, 1107 insertions(+), 416 deletions(-) create mode 100644 src/data_stream_writer_registry.h diff --git a/AGENTS.md b/AGENTS.md index 9901a51a..a747984a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -78,7 +78,7 @@ All `RoomDelegate` callbacks and stream handler callbacks (e.g., `registerTextSt | `LocalDataTrack::tryPush` | No | Thin `sendRequest` wrapper with no internal synchronization. | | `TextStreamReader` / `ByteStreamReader` | Yes | Fed on the FFI callback thread, read on a user thread (internal `std::mutex` + `condition_variable`). End-of-stream attributes are merged into `info()` on the reading thread, so don't call `info()` concurrently with `readNext()`. | | `LocalParticipant::sendText` / `sendBytes` / `sendFile` | Yes | Independent async FFI requests; each call blocks its caller until its own callback arrives. They throw `DataStreamError` once the room has disconnected rather than reaching the FFI. | -| `TextStreamWriter` / `ByteStreamWriter` | Serialized | `write()` is serialized by an internal `write_mutex_`. | +| `TextStreamWriter` / `ByteStreamWriter` | Serialized | `write()` and `close()` are serialized by an internal mutex and each waits for its FFI callback, so `close()` never overtakes a write. `LocalParticipant` closes its writers on disconnect (via a shared registry, so writers may outlive the participant); later calls throw `DataStreamError`. Create writers with `LocalParticipant::streamText` / `streamBytes`; the constructors are deprecated. | ### Directory Layout Be sure to update the directory layout in this file if the directory layout changes. diff --git a/CMakeLists.txt b/CMakeLists.txt index 17a1fcef..44c487bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -424,6 +424,7 @@ add_library(livekit SHARED src/data_stream.cpp src/data_stream_proto_converter.cpp src/data_stream_proto_converter.h + src/data_stream_writer_registry.h src/data_track_error.cpp src/data_track_schema.cpp src/data_track_stream.cpp diff --git a/include/livekit/data_stream.h b/include/livekit/data_stream.h index 8d02b41c..0e1635e7 100644 --- a/include/livekit/data_stream.h +++ b/include/livekit/data_stream.h @@ -16,6 +16,7 @@ #pragma once +#include #include #include #include @@ -34,9 +35,15 @@ namespace livekit { class LocalParticipant; +namespace detail { +class StreamWriterRegistry; +} // namespace detail + /// Chunk size for data streams (matches Python STREAM_CHUNK_SIZE). -/// Chosen to balance throughput and latency, and to work well with WebRTC data -/// channels. +/// +/// @deprecated The SDK core now chunks data streams itself (at 15 KB); this +/// constant no longer controls anything. +[[deprecated("kStreamChunkSize no longer controls chunking; the SDK core chunks data streams itself")]] constexpr std::size_t kStreamChunkSize = 15'000; // 15 KB /// @brief Error raised by data stream operations. @@ -324,29 +331,58 @@ class LIVEKIT_API ByteStreamReader { std::unique_ptr impl_; }; -/// Base class for sending data streams. -/// Concrete subclasses are TextStreamWriter and ByteStreamWriter. +/// @brief Base class for sending data streams incrementally. +/// +/// Concrete subclasses are TextStreamWriter and ByteStreamWriter; create them +/// with LocalParticipant::streamText() / LocalParticipant::streamBytes(). +/// +/// Thread safety: write() and close() are serialized internally, and each one +/// returns only after the SDK has processed it, so a close() always follows +/// every write() that returned before it. When the room disconnects, the +/// writer is closed and later write() / close() calls throw DataStreamError. +/// +/// Destroying a writer that was opened but not closed closes the stream +/// normally (without waiting), so receivers don't wait forever. class LIVEKIT_API BaseStreamWriter { public: - virtual ~BaseStreamWriter() = default; + virtual ~BaseStreamWriter(); + + BaseStreamWriter(const BaseStreamWriter&) = delete; + BaseStreamWriter& operator=(const BaseStreamWriter&) = delete; - /// Stream id assigned to this writer. + /// @brief Stream id assigned to this writer. + /// @return The stream id. const std::string& streamId() const noexcept { return stream_id_; } - /// Topic of this stream. + /// @brief Topic of this stream. + /// @return The topic. const std::string& topic() const noexcept { return topic_; } - /// MIME type for this stream. + /// @brief MIME type for this stream. + /// @return The MIME type. const std::string& mimeType() const noexcept { return mime_type_; } - /// Timestamp (ms) when the stream was created. + /// @brief Timestamp (ms) when the stream was created. + /// + /// Updated to the SDK core's timestamp once the stream is opened. + /// @return Creation timestamp in milliseconds since the Unix epoch. std::int64_t timestampMs() const noexcept { return timestamp_ms_; } - /// Whether the stream has been closed. + /// @brief Whether the stream has been closed (explicitly or because the room + /// disconnected). + /// @return True if closed. bool isClosed() const noexcept { return closed_; } - /// Close the stream with optional reason and attributes. - /// Throws on FFI error or if already closed. + /// @brief Close the stream. + /// + /// Waits for any in-progress write() to finish first. A non-empty @p reason + /// closes the stream abnormally: receivers' reads throw DataStreamError + /// carrying the reason. + /// + /// @param reason Empty for a normal close; otherwise the error reason. + /// @param attributes Attributes merged into the receiver's stream info. + /// @throws DataStreamError if the stream is already closed, the room has + /// disconnected, or the SDK reports an error. void close(const std::string& reason = "", const std::map& attributes = {}); protected: @@ -369,51 +405,141 @@ class LIVEKIT_API BaseStreamWriter { std::vector destination_identities_; std::string sender_identity_; - bool closed_ = false; + std::atomic closed_{false}; + /// True once the stream has been opened with the SDK core. bool header_sent_ = false; + /// Unused; chunking is done by the SDK core. std::uint64_t next_chunk_index_ = 0; StreamKind kind_ = StreamKind::kUnknown; std::string reply_to_id_; std::string byte_name_; // Used by ByteStreamWriter - /// Ensure the header has been sent once. - /// Throws on error. + /// @brief Open the stream if it isn't open yet. + /// @deprecated The SDK opens the stream on the first write() or close(). + /// @throws DataStreamError on error. + [[deprecated("BaseStreamWriter::ensureHeaderSent is deprecated; the stream opens on the first write or close")]] void ensureHeaderSent(); - /// Send a raw chunk of bytes. - /// Throws on error or if stream is closed. + /// @brief Write raw bytes to the stream (UTF-8 text for a text stream). + /// @deprecated Use TextStreamWriter::write() or ByteStreamWriter::write() instead. + /// @param content Payload to write. + /// @throws std::runtime_error if the stream is closed; DataStreamError on error. + [[deprecated( + "BaseStreamWriter::sendChunk is deprecated; use TextStreamWriter::write or ByteStreamWriter::write " + "instead")]] void sendChunk(const std::vector& content); - /// Send the trailer with given reason and attributes. - /// Throws on error. + /// @brief Close the stream with the given reason and attributes. + /// @deprecated Use close() instead. + /// @param reason Empty for a normal close; otherwise the error reason. + /// @param attributes Attributes merged into the receiver's stream info. + /// @throws DataStreamError on error. + [[deprecated("BaseStreamWriter::sendTrailer is deprecated; use BaseStreamWriter::close instead")]] void sendTrailer(const std::string& reason, const std::map& attributes); + + /// @brief Open the stream now (requires @c kind_ to be set). Serialized with + /// write() and close(). + /// @throws DataStreamError on error. + void open(); + + /// @brief Write a payload (text for kText, bytes for kByte), opening first if + /// needed. Serialized with other operations. + /// @param data Payload bytes. + /// @param size Payload size. + /// @param closed_message Message thrown if the writer is already closed. + /// @throws DataStreamError on error. + void writePayload(const char* data, std::size_t size, const char* closed_message); + +private: + friend class detail::StreamWriterRegistry; + + void openLocked(); + void closeLocked(const std::string& reason, const std::map& attributes); + void applyOpenedInfo(std::int64_t timestamp_ms, const std::string& mime_type); + std::uint64_t takeHandle() noexcept; + void onParticipantDisconnected() noexcept; + [[noreturn]] void throwNotConnected() const; + + /// Serializes open/write/close. Held across FFI waits. + std::mutex op_mutex_; + /// Guards writer_handle_ and participant_gone_. Never held across FFI calls. + mutable std::mutex state_mutex_; + /// Owned FFI writer handle; 0 before open and after close consumes it. + std::uint64_t writer_handle_ = 0; + bool participant_gone_ = false; + std::shared_ptr registry_; }; -/// Writer for outgoing text streams. +/// @brief Writer for outgoing text streams. +/// +/// Create one with LocalParticipant::streamText(). Text is sent as UTF-8; the +/// SDK splits it into chunks on code point boundaries. class LIVEKIT_API TextStreamWriter : public BaseStreamWriter { public: + /// @brief Construct a text stream writer. + /// + /// The stream is opened lazily on the first write() or close(). + /// + /// @deprecated Use LocalParticipant::streamText() instead. + /// @param local_participant Participant that sends the stream; must outlive the writer. + /// @param topic Topic receivers register handlers on. + /// @param attributes Attributes sent with the stream header. + /// @param stream_id Stream id; generated when empty. + /// @param total_size Recorded locally in info() only; text streams can't transmit a size. + /// @param reply_to_id Id of the stream this one replies to, or empty. + /// @param destination_identities Recipients; empty for everyone. + /// @param sender_identity Identity to send as; defaults to the local participant. + [[deprecated("Constructing TextStreamWriter directly is deprecated; use LocalParticipant::streamText instead")]] TextStreamWriter(LocalParticipant& local_participant, const std::string& topic = "", const std::map& attributes = {}, const std::string& stream_id = "", std::optional total_size = std::nullopt, const std::string& reply_to_id = "", const std::vector& destination_identities = {}, const std::string& sender_identity = ""); - /// Write a UTF-8 string to the stream. - /// Data will be split into chunks of at most kStreamChunkSize bytes. - /// Throws on error or if the stream is closed. + /// @brief Write a UTF-8 string to the stream. + /// + /// Blocks until the SDK has sent it. + /// + /// @param text UTF-8 text to append. + /// @throws DataStreamError if the stream is closed, the room has + /// disconnected, or the SDK reports an error. void write(const std::string& text); - /// Metadata associated with this stream. + /// @brief Metadata associated with this stream. + /// @return Stream metadata; updated with the SDK core's info once opened. const TextStreamInfo& info() const noexcept { return info_; } private: + friend class BaseStreamWriter; + friend class LocalParticipant; + struct FactoryTag {}; + + TextStreamWriter(FactoryTag, LocalParticipant& local_participant, const StreamTextOptions& options); + TextStreamInfo info_; - std::mutex write_mutex_; + std::vector attached_stream_ids_; }; -/// Writer for outgoing byte streams. +/// @brief Writer for outgoing byte streams. +/// +/// Create one with LocalParticipant::streamBytes(). class LIVEKIT_API ByteStreamWriter : public BaseStreamWriter { public: + /// @brief Construct a byte stream writer. + /// + /// The stream is opened lazily on the first write() or close(). + /// + /// @deprecated Use LocalParticipant::streamBytes() instead. + /// @param local_participant Participant that sends the stream; must outlive the writer. + /// @param name Name of the payload (for example a file name). + /// @param topic Topic receivers register handlers on. + /// @param attributes Attributes sent with the stream header. + /// @param stream_id Stream id; generated when empty. + /// @param total_size Total payload size, if known. + /// @param mime_type MIME type of the payload. + /// @param destination_identities Recipients; empty for everyone. + /// @param sender_identity Identity to send as; defaults to the local participant. + [[deprecated("Constructing ByteStreamWriter directly is deprecated; use LocalParticipant::streamBytes instead")]] ByteStreamWriter(LocalParticipant& local_participant, const std::string& name, const std::string& topic = "", const std::map& attributes = {}, const std::string& stream_id = "", std::optional total_size = std::nullopt, @@ -421,17 +547,27 @@ class LIVEKIT_API ByteStreamWriter : public BaseStreamWriter { const std::vector& destination_identities = {}, const std::string& sender_identity = ""); - /// Write binary data to the stream. - /// Data will be chunked into kStreamChunkSize-sized chunks. - /// Throws on error or if the stream is closed. + /// @brief Write binary data to the stream. + /// + /// Blocks until the SDK has sent it. + /// + /// @param data Bytes to append. + /// @throws DataStreamError if the stream is closed, the room has + /// disconnected, or the SDK reports an error. void write(const std::vector& data); - /// Metadata associated with this stream. + /// @brief Metadata associated with this stream. + /// @return Stream metadata; updated with the SDK core's info once opened. const ByteStreamInfo& info() const noexcept { return info_; } private: + friend class BaseStreamWriter; + friend class LocalParticipant; + struct FactoryTag {}; + + ByteStreamWriter(FactoryTag, LocalParticipant& local_participant, const StreamByteOptions& options); + ByteStreamInfo info_; - std::mutex write_mutex_; }; /// Callback invoked when a new incoming text stream is opened. diff --git a/include/livekit/local_participant.h b/include/livekit/local_participant.h index c9ff7c10..4d764ae7 100644 --- a/include/livekit/local_participant.h +++ b/include/livekit/local_participant.h @@ -47,6 +47,9 @@ struct ParticipantTrackPermission; class FfiClient; class Track; +namespace detail { +class StreamWriterRegistry; +} // namespace detail class LocalTrackPublication; /// @brief Data passed to a registered RPC method handler. @@ -76,6 +79,12 @@ class LIVEKIT_API LocalParticipant : public Participant { LocalParticipant(FfiHandle handle, std::string sid, std::string name, std::string identity, std::string metadata, std::unordered_map attributes, ParticipantKind kind, DisconnectReason reason); + ~LocalParticipant() override; + + LocalParticipant(const LocalParticipant&) = delete; + LocalParticipant& operator=(const LocalParticipant&) = delete; + LocalParticipant(LocalParticipant&&) = delete; + LocalParticipant& operator=(LocalParticipant&&) = delete; /// Track publications for this participant, keyed by publication SID. /// @@ -137,6 +146,36 @@ class LIVEKIT_API LocalParticipant : public Participant { /// if the room is no longer connected or the SDK isn't initialized. ByteStreamInfo sendFile(const std::string& path, const StreamByteOptions& options); + /// @brief Open a text stream for incremental writes. + /// + /// Blocks until the stream is open. Write with TextStreamWriter::write() and + /// finish with TextStreamWriter::close(). Incremental streams are never + /// compressed; @c options.compress is ignored. Use sendText() when the whole + /// text is available up front. + /// + /// @param options Topic (required), destinations, attributes, and so on. + /// @return An open writer. It must not outlive this participant's room + /// connection in a meaningful way: once the room disconnects, its + /// write() and close() throw DataStreamError. + /// @throws DataStreamError if the open fails, or if the room is no longer + /// connected or the SDK isn't initialized. + std::unique_ptr streamText(const StreamTextOptions& options); + + /// @brief Open a byte stream for incremental writes. + /// + /// Blocks until the stream is open. Write with ByteStreamWriter::write() and + /// finish with ByteStreamWriter::close(). Incremental streams are never + /// compressed; @c options.compress is ignored. Use sendBytes() or sendFile() + /// when the whole payload is available up front. + /// + /// @param options Topic (required), name, MIME type, total length, + /// destinations, and so on. + /// @return An open writer. Once the room disconnects, its write() and + /// close() throw DataStreamError. + /// @throws DataStreamError if the open fails, or if the room is no longer + /// connected or the SDK isn't initialized. + std::unique_ptr streamBytes(const StreamByteOptions& options); + /// Publish a SIP DTMF (phone keypad) tone into the room. /// /// Only meaningful when a SIP trunk is bridging a phone call into the @@ -346,9 +385,12 @@ class LIVEKIT_API LocalParticipant : public Participant { /// prune expired @c weak_ptr entries. mutable TrackMap published_tracks_by_sid_; + friend class BaseStreamWriter; + /// Marks the participant as disconnected so data stream sends fail fast in - /// C++ instead of reaching an FFI participant whose session is gone. - void markDisconnected() noexcept { disconnected_.store(true, std::memory_order_release); } + /// C++ instead of reaching an FFI participant whose session is gone, and + /// closes every open stream writer. + void markDisconnected() noexcept; /// Returns the FFI handle id for a data stream operation, or throws /// DataStreamError if the participant can no longer send. @@ -360,6 +402,10 @@ class LIVEKIT_API LocalParticipant : public Participant { /// Room::lock_ so send paths never need the room lock. std::atomic disconnected_{false}; + /// Stream writers created for this participant; shared with the writers so + /// they never touch a destroyed participant. + std::shared_ptr stream_writers_; + // Shared state for RPC invocation tracking. Using shared_ptr so the state // can outlive the LocalParticipant if there are in-flight invocations when // the participant is destroyed. diff --git a/src/data_stream.cpp b/src/data_stream.cpp index 0d88da8e..efb12ecd 100644 --- a/src/data_stream.cpp +++ b/src/data_stream.cpp @@ -24,6 +24,8 @@ #include #include +#include "data_stream_proto_converter.h" +#include "data_stream_writer_registry.h" #include "ffi_client.h" #include "livekit/local_participant.h" #include "lk_log.h" @@ -48,44 +50,6 @@ std::string generateRandomId(std::size_t bytes = 16) { return out; } -// Split UTF-8 string into chunks of at most max_bytes, not breaking codepoints. -std::vector splitUtf8(const std::string& s, std::size_t max_bytes) { - std::vector result; - if (s.empty()) { - return result; - } - - std::size_t i = 0; - const std::size_t n = s.size(); - - while (i < n) { - std::size_t end = std::min(i + max_bytes, n); - - // If end points into a UTF-8 continuation byte, walk back to a lead byte. - // NOTE: end is an index into the string; ensure end < n before indexing. - while (end > i && end < n && (static_cast(s[end]) & 0xC0) == 0x80) { - --end; - } - // If end==n, we may still have landed on a continuation byte due to min(). - while (end > i && end == n && (static_cast(s[end - 1]) & 0xC0) == 0x80) { - // Walk back until we hit a lead byte boundary. - // This isn't perfect but is consistent with your earlier intent. - --end; - // If we backed up too far, fall through to fallback. - } - - if (end == i) { - // Fallback: avoid infinite loop if bytes are pathological. - end = std::min(i + max_bytes, n); - } - - result.emplace_back(s.substr(i, end - i)); - i = end; - } - - return result; -} - void fillBaseInfo(BaseStreamInfo& dst, const std::string& stream_id, const std::string& mime_type, const std::string& topic, std::int64_t timestamp_ms, const std::optional& total_size, const std::map& attrs) { @@ -254,9 +218,56 @@ std::vector ByteStreamReader::readAll() { void ByteStreamReader::close() { impl_->close(); } // ===================================================================== -// Writer implementation (uses your future-based FfiClient) +// Writer registry // ===================================================================== +namespace detail { + +bool StreamWriterRegistry::add(BaseStreamWriter* writer) { + const std::scoped_lock lock(mutex_); + if (closed_) { + return false; + } + writers_.insert(writer); + return true; +} + +void StreamWriterRegistry::remove(BaseStreamWriter* writer) { + const std::scoped_lock lock(mutex_); + writers_.erase(writer); +} + +std::uint64_t StreamWriterRegistry::participantHandle() const { + const std::scoped_lock lock(mutex_); + if (closed_) { + throw DataStreamError("stream writer: not connected"); + } + return participant_handle_; +} + +void StreamWriterRegistry::closeAll() noexcept { + // Held while notifying writers so a writer being destroyed (which calls + // remove()) stays alive until it has been notified. + const std::scoped_lock lock(mutex_); + closed_ = true; + for (auto* writer : writers_) { + writer->onParticipantDisconnected(); + } + writers_.clear(); +} + +} // namespace detail + +// ===================================================================== +// Writer implementation +// ===================================================================== + +namespace { + +constexpr char kWriterNotConnected[] = "stream writer: not connected"; + +} // namespace + BaseStreamWriter::BaseStreamWriter(LocalParticipant& local_participant, std::string topic, std::map attributes, std::string stream_id, std::optional total_size, std::string mime_type, @@ -271,81 +282,234 @@ BaseStreamWriter::BaseStreamWriter(LocalParticipant& local_participant, std::str total_size_(total_size), attributes_(std::move(attributes)), destination_identities_(std::move(destination_identities)), - sender_identity_(std::move(sender_identity)) { + sender_identity_(std::move(sender_identity)), + registry_(local_participant.stream_writers_) { if (sender_identity_.empty()) { sender_identity_ = local_participant_.identity(); } + if (!registry_->add(this)) { + // Created after the room disconnected: usable only to fail cleanly. + participant_gone_ = true; + closed_ = true; + } } -void BaseStreamWriter::ensureHeaderSent() { - if (header_sent_) { +BaseStreamWriter::~BaseStreamWriter() { + // After remove() returns, closeAll() can no longer reach this writer. + registry_->remove(this); + + const std::uint64_t handle = takeHandle(); + if (handle == 0) { return; } - proto::DataStream::Header header; - header.set_stream_id(stream_id_); - header.set_timestamp(timestamp_ms_); - header.set_mime_type(mime_type_); - header.set_topic(topic_); - - if (total_size_.has_value()) { - header.set_total_length(static_cast(*total_size_)); + // Opened but never closed. Rust's writer sends a trailer when dropped, but + // only from a Tokio thread; dropping the handle here (a C++ thread) would + // skip it and leave receivers waiting. Close explicitly, without waiting. + try { + if (kind_ == StreamKind::kText) { + (void)FfiClient::instance().textStreamCloseAsync(handle, std::nullopt, {}); + } else { + (void)FfiClient::instance().byteStreamCloseAsync(handle, std::nullopt, {}); + } + } catch (const std::exception& e) { + LK_LOG_DEBUG("Stream writer {} could not be closed on destruction: {}", stream_id_, e.what()); + (void)dropFfiHandle(handle); + } catch (...) { + (void)dropFfiHandle(handle); } - for (const auto& kv : attributes_) { - (*header.mutable_attributes())[kv.first] = kv.second; +} + +std::uint64_t BaseStreamWriter::takeHandle() noexcept { + const std::scoped_lock lock(state_mutex_); + const std::uint64_t handle = writer_handle_; + writer_handle_ = 0; + return handle; +} + +void BaseStreamWriter::onParticipantDisconnected() noexcept { + std::uint64_t handle = 0; + { + const std::scoped_lock lock(state_mutex_); + participant_gone_ = true; + closed_ = true; + handle = writer_handle_; + writer_handle_ = 0; } - if (kind_ == StreamKind::kText) { - auto* th = header.mutable_text_header(); - th->set_operation_type(proto::DataStream::OperationType::DataStream_OperationType_CREATE); - if (!reply_to_id_.empty()) { - th->set_reply_to_stream_id(reply_to_id_); - } - } else if (kind_ == StreamKind::kByte) { - header.mutable_byte_header()->set_name(byte_name_); + // The room is gone; there's nobody to send a trailer to. + if (handle != 0) { + (void)dropFfiHandle(handle); } +} + +void BaseStreamWriter::throwNotConnected() const { throw DataStreamError(kWriterNotConnected); } - FfiClient::instance() - .sendStreamHeaderAsync(local_participant_.ffiHandleId(), header, destination_identities_, sender_identity_) - .get(); +void BaseStreamWriter::applyOpenedInfo(std::int64_t timestamp_ms, const std::string& mime_type) { + timestamp_ms_ = timestamp_ms; + mime_type_ = mime_type; header_sent_ = true; } -void BaseStreamWriter::sendChunk(const std::vector& content) { - if (closed_) { - throw std::runtime_error("Cannot send chunk after stream is closed"); +void BaseStreamWriter::openLocked() { + { + const std::scoped_lock lock(state_mutex_); + if (participant_gone_) { + throwNotConnected(); + } + if (writer_handle_ != 0) { + return; + } } - ensureHeaderSent(); - - proto::DataStream::Chunk chunk; - chunk.set_stream_id(stream_id_); - chunk.set_chunk_index(next_chunk_index_++); // ✅ uses the new member - chunk.set_content(content.data(), content.size()); + const std::uint64_t participant_handle = registry_->participantHandle(); + std::uint64_t handle = 0; + if (kind_ == StreamKind::kText) { + auto* self = static_cast(this); + proto::StreamTextOptions options; + options.set_topic(topic_); + for (const auto& kv : attributes_) { + (*options.mutable_attributes())[kv.first] = kv.second; + } + for (const auto& identity : destination_identities_) { + options.add_destination_identities(identity); + } + options.set_id(stream_id_); + if (!reply_to_id_.empty()) { + options.set_reply_to_stream_id(reply_to_id_); + } + for (const auto& id : self->attached_stream_ids_) { + options.add_attached_stream_ids(id); + } + options.set_sender_identity(sender_identity_); + + const auto owned = FfiClient::instance().textStreamOpenAsync(participant_handle, options).get(); + handle = owned.handle().id(); + TextStreamInfo info = fromProto(owned.info()); + info.size = total_size_; // Text streams can't transmit a size; keep the local one. + self->info_ = std::move(info); + applyOpenedInfo(self->info_.timestamp, self->info_.mime_type); + } else if (kind_ == StreamKind::kByte) { + auto* self = static_cast(this); + proto::StreamByteOptions options; + options.set_topic(topic_); + for (const auto& kv : attributes_) { + (*options.mutable_attributes())[kv.first] = kv.second; + } + for (const auto& identity : destination_identities_) { + options.add_destination_identities(identity); + } + options.set_id(stream_id_); + if (!byte_name_.empty()) { + options.set_name(byte_name_); + } + if (!mime_type_.empty()) { + options.set_mime_type(mime_type_); + } + if (total_size_) { + options.set_total_length(static_cast(*total_size_)); + } + options.set_sender_identity(sender_identity_); + + const auto owned = FfiClient::instance().byteStreamOpenAsync(participant_handle, options).get(); + handle = owned.handle().id(); + self->info_ = fromProto(owned.info()); + applyOpenedInfo(self->info_.timestamp, self->info_.mime_type); + } else { + throw DataStreamError("stream writer: unknown stream kind"); + } - FfiClient::instance() - .sendStreamChunkAsync(local_participant_.ffiHandleId(), chunk, destination_identities_, sender_identity_) - .get(); + bool gone = false; + { + const std::scoped_lock lock(state_mutex_); + gone = participant_gone_; + if (!gone) { + writer_handle_ = handle; + } + } + if (gone) { + // The room disconnected while the open was in flight. + (void)dropFfiHandle(handle); + throwNotConnected(); + } } -void BaseStreamWriter::sendTrailer(const std::string& reason, const std::map& attributes) { - ensureHeaderSent(); +void BaseStreamWriter::open() { + const std::scoped_lock op(op_mutex_); + openLocked(); +} - proto::DataStream::Trailer trailer; - trailer.set_stream_id(stream_id_); - trailer.set_reason(reason); +void BaseStreamWriter::writePayload(const char* data, std::size_t size, const char* closed_message) { + const std::scoped_lock op(op_mutex_); + if (closed_) { + const std::scoped_lock lock(state_mutex_); + if (participant_gone_) { + throwNotConnected(); + } + throw DataStreamError(closed_message); + } + if (size == 0) { + return; + } + openLocked(); - for (const auto& kv : attributes) { - (*trailer.mutable_attributes())[kv.first] = kv.second; + std::uint64_t handle = 0; + { + const std::scoped_lock lock(state_mutex_); + if (participant_gone_) { + throwNotConnected(); + } + handle = writer_handle_; } + // Wait for each write before the next one: the SDK core runs every write + // request as its own task, so unawaited writes could reorder or land after + // the trailer. + if (kind_ == StreamKind::kText) { + FfiClient::instance().textStreamWriteAsync(handle, std::string(data, size)).get(); + } else { + FfiClient::instance().byteStreamWriteAsync(handle, reinterpret_cast(data), size).get(); + } +} - FfiClient::instance().sendStreamTrailerAsync(local_participant_.ffiHandleId(), trailer, sender_identity_).get(); +void BaseStreamWriter::closeLocked(const std::string& reason, const std::map& attributes) { + openLocked(); + // close consumes the handle; it must not be dropped afterwards. + const std::uint64_t handle = takeHandle(); + if (handle == 0) { + throwNotConnected(); + } + const std::optional close_reason = reason.empty() ? std::nullopt : std::optional(reason); + if (kind_ == StreamKind::kText) { + FfiClient::instance().textStreamCloseAsync(handle, close_reason, attributes).get(); + } else { + FfiClient::instance().byteStreamCloseAsync(handle, close_reason, attributes).get(); + } } void BaseStreamWriter::close(const std::string& reason, const std::map& attributes) { - if (closed_) { - throw std::runtime_error("Stream already closed"); + // Taking op_mutex_ orders the close after any in-flight write. + const std::scoped_lock op(op_mutex_); + { + const std::scoped_lock lock(state_mutex_); + if (participant_gone_) { + throwNotConnected(); + } } + if (closed_.exchange(true)) { + throw DataStreamError("Stream already closed"); + } + closeLocked(reason, attributes); +} + +void BaseStreamWriter::ensureHeaderSent() { open(); } + +void BaseStreamWriter::sendChunk(const std::vector& content) { + writePayload(reinterpret_cast(content.data()), content.size(), + "Cannot send chunk after stream is closed"); +} + +void BaseStreamWriter::sendTrailer(const std::string& reason, const std::map& attributes) { + const std::scoped_lock op(op_mutex_); closed_ = true; - sendTrailer(reason, attributes); + closeLocked(reason, attributes); } TextStreamWriter::TextStreamWriter(LocalParticipant& local_participant, const std::string& topic, @@ -357,25 +521,27 @@ TextStreamWriter::TextStreamWriter(LocalParticipant& local_participant, const st /*mime_type=*/"text/plain", destination_identities, sender_identity) { kind_ = StreamKind::kText; reply_to_id_ = reply_to_id; - // ✅ Canonical user-facing metadata comes from BaseStreamWriter fields. fillBaseInfo(info_, stream_id_, mime_type_, topic_, timestamp_ms_, total_size_, attributes_); if (!reply_to_id.empty()) { info_.reply_to_stream_id = reply_to_id; } } -void TextStreamWriter::write(const std::string& text) { - const std::scoped_lock lock(write_mutex_); - if (closed_) { - throw std::runtime_error("Cannot write to closed TextStreamWriter"); - } +TextStreamWriter::TextStreamWriter(FactoryTag /*tag*/, LocalParticipant& local_participant, + const StreamTextOptions& options) + : BaseStreamWriter(local_participant, options.topic, options.attributes, options.stream_id.value_or(""), + std::nullopt, /*mime_type=*/"text/plain", options.destination_identities, + options.sender_identity.value_or("")), + attached_stream_ids_(options.attached_stream_ids) { + kind_ = StreamKind::kText; + reply_to_id_ = options.reply_to_stream_id.value_or(""); + fillBaseInfo(info_, stream_id_, mime_type_, topic_, timestamp_ms_, total_size_, attributes_); + info_.reply_to_stream_id = options.reply_to_stream_id; + info_.attachments = attached_stream_ids_; +} - for (const auto& chunk_str : splitUtf8(text, kStreamChunkSize)) { - const auto* p = reinterpret_cast(chunk_str.data()); - const std::vector bytes(p, p + chunk_str.size()); - LK_LOG_DEBUG("sending chunk"); - sendChunk(bytes); - } +void TextStreamWriter::write(const std::string& text) { + writePayload(text.data(), text.size(), "Cannot write to closed TextStreamWriter"); } ByteStreamWriter::ByteStreamWriter(LocalParticipant& local_participant, const std::string& name, @@ -391,20 +557,22 @@ ByteStreamWriter::ByteStreamWriter(LocalParticipant& local_participant, const st info_.name = name; } -void ByteStreamWriter::write(const std::vector& data) { - const std::scoped_lock lock(write_mutex_); - if (closed_) { - throw std::runtime_error("Cannot write to closed ByteStreamWriter"); - } +ByteStreamWriter::ByteStreamWriter(FactoryTag /*tag*/, LocalParticipant& local_participant, + const StreamByteOptions& options) + : BaseStreamWriter(local_participant, options.topic, options.attributes, options.stream_id.value_or(""), + options.total_length + ? std::optional(static_cast(*options.total_length)) + : std::nullopt, + options.mime_type.value_or("application/octet-stream"), options.destination_identities, + options.sender_identity.value_or("")) { + kind_ = StreamKind::kByte; + byte_name_ = options.name.value_or(""); + fillBaseInfo(info_, stream_id_, mime_type_, topic_, timestamp_ms_, total_size_, attributes_); + info_.name = byte_name_; +} - std::size_t offset = 0; - while (offset < data.size()) { - const std::size_t n = std::min(kStreamChunkSize, data.size() - offset); - auto it = data.begin() + static_cast(offset); - const std::vector chunk(it, it + static_cast(n)); - sendChunk(chunk); - offset += n; - } +void ByteStreamWriter::write(const std::vector& data) { + writePayload(reinterpret_cast(data.data()), data.size(), "Cannot write to closed ByteStreamWriter"); } } // namespace livekit diff --git a/src/data_stream_writer_registry.h b/src/data_stream_writer_registry.h new file mode 100644 index 00000000..584c5d94 --- /dev/null +++ b/src/data_stream_writer_registry.h @@ -0,0 +1,62 @@ +/* + * 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. + */ + +#pragma once + +#include +#include +#include + +namespace livekit { + +class BaseStreamWriter; + +namespace detail { + +/// @brief Tracks a LocalParticipant's stream writers so they can be closed when +/// the participant disconnects or is destroyed. +/// +/// Shared between the participant and its writers, so a writer that outlives +/// its participant never touches the participant itself. +class StreamWriterRegistry { +public: + /// @param participant_handle FFI handle id of the local participant. + explicit StreamWriterRegistry(std::uint64_t participant_handle) : participant_handle_(participant_handle) {} + + /// @brief Register a writer. + /// @return False if the participant is already disconnected. + bool add(BaseStreamWriter* writer); + + /// @brief Unregister a writer. Blocks while closeAll() is running. + void remove(BaseStreamWriter* writer); + + /// @brief Participant handle for a writer FFI request. + /// @throws DataStreamError if the participant is disconnected. + std::uint64_t participantHandle() const; + + /// @brief Mark the participant disconnected and close every registered + /// writer. Idempotent. + void closeAll() noexcept; + +private: + mutable std::mutex mutex_; + std::uint64_t participant_handle_; + bool closed_ = false; + std::unordered_set writers_; +}; + +} // namespace detail +} // namespace livekit diff --git a/src/ffi_client.cpp b/src/ffi_client.cpp index ccb70300..5599ac94 100644 --- a/src/ffi_client.cpp +++ b/src/ffi_client.cpp @@ -1265,43 +1265,115 @@ void FfiClient::byteStreamReadIncremental(std::uint64_t reader_handle) { } } -std::future FfiClient::sendStreamHeaderAsync(std::uint64_t local_participant_handle, - const proto::DataStream::Header& header, - const std::vector& destination_identities, - const std::string& sender_identity) { - // Generate client-side async_id first +std::future FfiClient::textStreamOpenAsync(std::uint64_t local_participant_handle, + const proto::StreamTextOptions& options) { + const AsyncId async_id = generateAsyncId(); + + auto fut = registerAsync( + async_id, + [async_id](const proto::FfiEvent& e) { + return e.has_text_stream_open() && e.text_stream_open().async_id() == async_id; + }, + [](const proto::FfiEvent& e, std::promise& pr) { + const auto& cb = e.text_stream_open(); + if (cb.has_error()) { + pr.set_exception(std::make_exception_ptr(DataStreamError(cb.error().description()))); + return; + } + if (!cb.has_writer()) { + pr.set_exception(std::make_exception_ptr(DataStreamError("text_stream_open callback missing writer"))); + return; + } + pr.set_value(cb.writer()); + }); + + proto::FfiRequest req; + auto* msg = req.mutable_text_stream_open(); + msg->set_local_participant_handle(local_participant_handle); + *msg->mutable_options() = options; + msg->set_request_async_id(async_id); + + try { + const proto::FfiResponse resp = sendRequest(req); + if (!resp.has_text_stream_open()) { + logAndThrow("FfiResponse missing text_stream_open"); + } + } catch (...) { + cancelPendingByAsyncId(async_id); + throw; + } + + return fut; +} + +std::future FfiClient::byteStreamOpenAsync(std::uint64_t local_participant_handle, + const proto::StreamByteOptions& options) { + const AsyncId async_id = generateAsyncId(); + + auto fut = registerAsync( + async_id, + [async_id](const proto::FfiEvent& e) { + return e.has_byte_stream_open() && e.byte_stream_open().async_id() == async_id; + }, + [](const proto::FfiEvent& e, std::promise& pr) { + const auto& cb = e.byte_stream_open(); + if (cb.has_error()) { + pr.set_exception(std::make_exception_ptr(DataStreamError(cb.error().description()))); + return; + } + if (!cb.has_writer()) { + pr.set_exception(std::make_exception_ptr(DataStreamError("byte_stream_open callback missing writer"))); + return; + } + pr.set_value(cb.writer()); + }); + + proto::FfiRequest req; + auto* msg = req.mutable_byte_stream_open(); + msg->set_local_participant_handle(local_participant_handle); + *msg->mutable_options() = options; + msg->set_request_async_id(async_id); + + try { + const proto::FfiResponse resp = sendRequest(req); + if (!resp.has_byte_stream_open()) { + logAndThrow("FfiResponse missing byte_stream_open"); + } + } catch (...) { + cancelPendingByAsyncId(async_id); + throw; + } + + return fut; +} + +std::future FfiClient::textStreamWriteAsync(std::uint64_t writer_handle, const std::string& text) { const AsyncId async_id = generateAsyncId(); - // Register the async handler BEFORE sending the request auto fut = registerAsync( async_id, [async_id](const proto::FfiEvent& e) { - return e.has_send_stream_header() && e.send_stream_header().async_id() == async_id; + return e.has_text_stream_writer_write() && e.text_stream_writer_write().async_id() == async_id; }, [](const proto::FfiEvent& e, std::promise& pr) { - const auto& cb = e.send_stream_header(); - if (!cb.error().empty()) { - pr.set_exception(std::make_exception_ptr(std::runtime_error(cb.error()))); + const auto& cb = e.text_stream_writer_write(); + if (cb.has_error()) { + pr.set_exception(std::make_exception_ptr(DataStreamError(cb.error().description()))); return; } pr.set_value(); }); - // Build and send the request proto::FfiRequest req; - auto* msg = req.mutable_send_stream_header(); - msg->set_local_participant_handle(local_participant_handle); - *msg->mutable_header() = header; - msg->set_sender_identity(sender_identity); + auto* msg = req.mutable_text_stream_write(); + msg->set_writer_handle(writer_handle); + msg->set_text(text); msg->set_request_async_id(async_id); - for (const auto& id : destination_identities) { - msg->add_destination_identities(id); - } try { const proto::FfiResponse resp = sendRequest(req); - if (!resp.has_send_stream_header()) { - logAndThrow("FfiResponse missing send_stream_header"); + if (!resp.has_text_stream_write()) { + logAndThrow("FfiResponse missing text_stream_write"); } } catch (...) { cancelPendingByAsyncId(async_id); @@ -1311,43 +1383,76 @@ std::future FfiClient::sendStreamHeaderAsync(std::uint64_t local_participa return fut; } -std::future FfiClient::sendStreamChunkAsync(std::uint64_t local_participant_handle, - const proto::DataStream::Chunk& chunk, - const std::vector& destination_identities, - const std::string& sender_identity) { - // Generate client-side async_id first +std::future FfiClient::byteStreamWriteAsync(std::uint64_t writer_handle, const std::uint8_t* data, + std::size_t size) { const AsyncId async_id = generateAsyncId(); - // Register the async handler BEFORE sending the request auto fut = registerAsync( async_id, [async_id](const proto::FfiEvent& e) { - return e.has_send_stream_chunk() && e.send_stream_chunk().async_id() == async_id; + return e.has_byte_stream_writer_write() && e.byte_stream_writer_write().async_id() == async_id; }, [](const proto::FfiEvent& e, std::promise& pr) { - const auto& cb = e.send_stream_chunk(); - if (!cb.error().empty()) { - pr.set_exception(std::make_exception_ptr(std::runtime_error(cb.error()))); + const auto& cb = e.byte_stream_writer_write(); + if (cb.has_error()) { + pr.set_exception(std::make_exception_ptr(DataStreamError(cb.error().description()))); return; } pr.set_value(); }); - // Build and send the request proto::FfiRequest req; - auto* msg = req.mutable_send_stream_chunk(); - msg->set_local_participant_handle(local_participant_handle); - *msg->mutable_chunk() = chunk; - msg->set_sender_identity(sender_identity); + auto* msg = req.mutable_byte_stream_write(); + msg->set_writer_handle(writer_handle); + msg->set_bytes(data, size); msg->set_request_async_id(async_id); - for (const auto& id : destination_identities) { - msg->add_destination_identities(id); + + try { + const proto::FfiResponse resp = sendRequest(req); + if (!resp.has_byte_stream_write()) { + logAndThrow("FfiResponse missing byte_stream_write"); + } + } catch (...) { + cancelPendingByAsyncId(async_id); + throw; + } + + return fut; +} + +std::future FfiClient::textStreamCloseAsync(std::uint64_t writer_handle, const std::optional& reason, + const std::map& attributes) { + const AsyncId async_id = generateAsyncId(); + + auto fut = registerAsync( + async_id, + [async_id](const proto::FfiEvent& e) { + return e.has_text_stream_writer_close() && e.text_stream_writer_close().async_id() == async_id; + }, + [](const proto::FfiEvent& e, std::promise& pr) { + const auto& cb = e.text_stream_writer_close(); + if (cb.has_error()) { + pr.set_exception(std::make_exception_ptr(DataStreamError(cb.error().description()))); + return; + } + pr.set_value(); + }); + + proto::FfiRequest req; + auto* msg = req.mutable_text_stream_close(); + msg->set_writer_handle(writer_handle); + if (reason) { + msg->set_reason(*reason); + } + for (const auto& kv : attributes) { + (*msg->mutable_attributes())[kv.first] = kv.second; } + msg->set_request_async_id(async_id); try { const proto::FfiResponse resp = sendRequest(req); - if (!resp.has_send_stream_chunk()) { - logAndThrow("FfiResponse missing send_stream_chunk"); + if (!resp.has_text_stream_close()) { + logAndThrow("FfiResponse missing text_stream_close"); } } catch (...) { cancelPendingByAsyncId(async_id); @@ -1357,39 +1462,39 @@ std::future FfiClient::sendStreamChunkAsync(std::uint64_t local_participan return fut; } -std::future FfiClient::sendStreamTrailerAsync(std::uint64_t local_participant_handle, - const proto::DataStream::Trailer& trailer, - const std::string& sender_identity) { - // Generate client-side async_id first +std::future FfiClient::byteStreamCloseAsync(std::uint64_t writer_handle, const std::optional& reason, + const std::map& attributes) { const AsyncId async_id = generateAsyncId(); - // Register the async handler BEFORE sending the request auto fut = registerAsync( async_id, [async_id](const proto::FfiEvent& e) { - return e.has_send_stream_trailer() && e.send_stream_trailer().async_id() == async_id; + return e.has_byte_stream_writer_close() && e.byte_stream_writer_close().async_id() == async_id; }, [](const proto::FfiEvent& e, std::promise& pr) { - const auto& cb = e.send_stream_trailer(); - if (!cb.error().empty()) { - pr.set_exception(std::make_exception_ptr(std::runtime_error(cb.error()))); + const auto& cb = e.byte_stream_writer_close(); + if (cb.has_error()) { + pr.set_exception(std::make_exception_ptr(DataStreamError(cb.error().description()))); return; } pr.set_value(); }); - // Build and send the request proto::FfiRequest req; - auto* msg = req.mutable_send_stream_trailer(); - msg->set_local_participant_handle(local_participant_handle); - *msg->mutable_trailer() = trailer; - msg->set_sender_identity(sender_identity); + auto* msg = req.mutable_byte_stream_close(); + msg->set_writer_handle(writer_handle); + if (reason) { + msg->set_reason(*reason); + } + for (const auto& kv : attributes) { + (*msg->mutable_attributes())[kv.first] = kv.second; + } msg->set_request_async_id(async_id); try { const proto::FfiResponse resp = sendRequest(req); - if (!resp.has_send_stream_trailer()) { - logAndThrow("FfiResponse missing send_stream_trailer"); + if (!resp.has_byte_stream_close()) { + logAndThrow("FfiResponse missing byte_stream_close"); } } catch (...) { cancelPendingByAsyncId(async_id); diff --git a/src/ffi_client.h b/src/ffi_client.h index 1359f891..0be0eb58 100644 --- a/src/ffi_client.h +++ b/src/ffi_client.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -57,7 +58,6 @@ class FfiRequest; class OwnedTrackPublication; class OwnedLocalDataTrack; class OwnedDataTrackStream; -class DataStream; } // namespace proto @@ -176,17 +176,18 @@ class LIVEKIT_INTERNAL_API FfiClient { void textStreamReadIncremental(std::uint64_t reader_handle); void byteStreamReadIncremental(std::uint64_t reader_handle); - // Low-level data stream functionalities (legacy v1 framing) - std::future sendStreamHeaderAsync(std::uint64_t local_participant_handle, - const proto::DataStream::Header& header, - const std::vector& destination_identities, - const std::string& sender_identity); - std::future sendStreamChunkAsync(std::uint64_t local_participant_handle, const proto::DataStream::Chunk& chunk, - const std::vector& destination_identities, - const std::string& sender_identity); - std::future sendStreamTrailerAsync(std::uint64_t local_participant_handle, - const proto::DataStream::Trailer& trailer, - const std::string& sender_identity); + // Incremental writers. Open creates an owned writer handle; write borrows + // it; close consumes it (never drop the handle after calling close). + std::future textStreamOpenAsync(std::uint64_t local_participant_handle, + const proto::StreamTextOptions& options); + std::future byteStreamOpenAsync(std::uint64_t local_participant_handle, + const proto::StreamByteOptions& options); + std::future textStreamWriteAsync(std::uint64_t writer_handle, const std::string& text); + std::future byteStreamWriteAsync(std::uint64_t writer_handle, const std::uint8_t* data, std::size_t size); + std::future textStreamCloseAsync(std::uint64_t writer_handle, const std::optional& reason, + const std::map& attributes); + std::future byteStreamCloseAsync(std::uint64_t writer_handle, const std::optional& reason, + const std::map& attributes); // Generic function for sending a request to the Rust FFI. // Note: For asynchronous requests, use the dedicated async functions instead diff --git a/src/local_participant.cpp b/src/local_participant.cpp index e93e3fcc..f6816223 100644 --- a/src/local_participant.cpp +++ b/src/local_participant.cpp @@ -22,6 +22,7 @@ #include #include "data_stream_proto_converter.h" +#include "data_stream_writer_registry.h" #include "data_track.pb.h" #include "ffi.pb.h" #include "ffi_client.h" @@ -66,7 +67,15 @@ LocalParticipant::LocalParticipant(FfiHandle handle, std::string sid, std::strin std::string metadata, std::unordered_map attributes, ParticipantKind kind, DisconnectReason reason) : Participant(std::move(handle), std::move(sid), std::move(name), std::move(identity), std::move(metadata), - std::move(attributes), kind, reason) {} + std::move(attributes), kind, reason), + stream_writers_(std::make_shared(static_cast(ffiHandleId()))) {} + +LocalParticipant::~LocalParticipant() { stream_writers_->closeAll(); } + +void LocalParticipant::markDisconnected() noexcept { + disconnected_.store(true, std::memory_order_release); + stream_writers_->closeAll(); +} void LocalParticipant::publishData(const std::vector& payload, bool reliable, const std::vector& destination_identities, const std::string& topic) { @@ -132,6 +141,20 @@ ByteStreamInfo LocalParticipant::sendFile(const std::string& path, const StreamB return fromProto(fut.get()); } +std::unique_ptr LocalParticipant::streamText(const StreamTextOptions& options) { + (void)dataStreamHandle("streamText"); + std::unique_ptr writer(new TextStreamWriter(TextStreamWriter::FactoryTag{}, *this, options)); + writer->open(); + return writer; +} + +std::unique_ptr LocalParticipant::streamBytes(const StreamByteOptions& options) { + (void)dataStreamHandle("streamBytes"); + std::unique_ptr writer(new ByteStreamWriter(ByteStreamWriter::FactoryTag{}, *this, options)); + writer->open(); + return writer; +} + void LocalParticipant::publishDtmf(int code, const std::string& digit) { auto handle_id = ffiHandleId(); if (handle_id == 0) { diff --git a/src/room_proto_converter.cpp b/src/room_proto_converter.cpp index b72710fe..c6e753a8 100644 --- a/src/room_proto_converter.cpp +++ b/src/room_proto_converter.cpp @@ -246,78 +246,6 @@ AttributeEntry fromProto(const proto::AttributesEntry& in) { return a; } -DataStreamHeaderData fromProto(const proto::DataStream_Header& in) { - DataStreamHeaderData out; - out.stream_id = in.stream_id(); - out.timestamp = in.timestamp(); - out.mime_type = in.mime_type(); - out.topic = in.topic(); - if (in.has_total_length()) { - out.total_length = in.total_length(); - } - for (const auto& kv : in.attributes()) { - out.attributes.emplace(kv.first, kv.second); - } - - // content_header oneof - switch (in.content_header_case()) { - case proto::DataStream_Header::kTextHeader: { - out.content_type = DataStreamHeaderData::ContentType::Text; - const auto& t = in.text_header(); - out.operation_type = static_cast(t.operation_type()); - if (t.has_version()) { - out.version = t.version(); - } - if (t.has_reply_to_stream_id()) { - out.reply_to_stream_id = t.reply_to_stream_id(); - } - for (const auto& id : t.attached_stream_ids()) { - out.attached_stream_ids.push_back(id); - } - if (t.has_generated()) { - out.generated = t.generated(); - } - break; - } - case proto::DataStream_Header::kByteHeader: { - out.content_type = DataStreamHeaderData::ContentType::Byte; - const auto& b = in.byte_header(); - out.name = b.name(); - break; - } - case proto::DataStream_Header::CONTENT_HEADER_NOT_SET: - default: - out.content_type = DataStreamHeaderData::ContentType::None; - break; - } - - return out; -} - -DataStreamChunkData fromProto(const proto::DataStream_Chunk& in) { - DataStreamChunkData out; - out.stream_id = in.stream_id(); - out.chunk_index = in.chunk_index(); - out.content.assign(in.content().begin(), in.content().end()); - if (in.has_version()) { - out.version = in.version(); - } - if (in.has_iv()) { - out.iv.assign(in.iv().begin(), in.iv().end()); - } - return out; -} - -DataStreamTrailerData fromProto(const proto::DataStream_Trailer& in) { - DataStreamTrailerData out; - out.stream_id = in.stream_id(); - out.reason = in.reason(); - for (const auto& kv : in.attributes()) { - out.attributes.emplace(kv.first, kv.second); - } - return out; -} - // --------- event conversions --------- RoomSidChangedEvent fromProto(const proto::RoomSidChanged& in) { @@ -350,27 +278,6 @@ TokenRefreshedEvent fromProto(const proto::TokenRefreshed& in) { RoomEosEvent fromProto(const proto::RoomEOS& /*in*/) { return RoomEosEvent{}; } -DataStreamHeaderReceivedEvent fromProto(const proto::DataStreamHeaderReceived& in) { - DataStreamHeaderReceivedEvent ev; - ev.participant_identity = in.participant_identity(); - ev.header = fromProto(in.header()); - return ev; -} - -DataStreamChunkReceivedEvent fromProto(const proto::DataStreamChunkReceived& in) { - DataStreamChunkReceivedEvent ev; - ev.participant_identity = in.participant_identity(); - ev.chunk = fromProto(in.chunk()); - return ev; -} - -DataStreamTrailerReceivedEvent fromProto(const proto::DataStreamTrailerReceived& in) { - DataStreamTrailerReceivedEvent ev; - ev.participant_identity = in.participant_identity(); - ev.trailer = fromProto(in.trailer()); - return ev; -} - DataChannelBufferedAmountLowThresholdChangedEvent fromProto( const proto::DataChannelBufferedAmountLowThresholdChanged& in) { DataChannelBufferedAmountLowThresholdChangedEvent ev; @@ -617,56 +524,4 @@ SipDtmfReceivedEvent sipDtmfFromProto(const proto::DataPacketReceived& in, Remot return ev; } -std::map toAttrMap(const proto::DataStream::Trailer& trailer) { - std::map out; - for (const auto& kv : trailer.attributes()) { - out.emplace(kv.first, kv.second); - } - return out; -} - -TextStreamInfo makeTextInfo(const proto::DataStream::Header& header) { - TextStreamInfo info; - info.stream_id = header.stream_id(); - info.mime_type = header.mime_type(); - info.topic = header.topic(); - info.timestamp = header.timestamp(); - if (header.text_header().has_reply_to_stream_id()) { - info.reply_to_stream_id = header.text_header().reply_to_stream_id(); - } - - if (header.has_total_length()) { - info.size = static_cast(header.total_length()); - } - - for (const auto& kv : header.attributes()) { - info.attributes.emplace(kv.first, kv.second); - } - - for (const auto& id : header.text_header().attached_stream_ids()) { - info.attachments.push_back(id); - } - - return info; -} - -ByteStreamInfo makeByteInfo(const proto::DataStream::Header& header) { - ByteStreamInfo info; - info.stream_id = header.stream_id(); - info.mime_type = header.mime_type(); - info.topic = header.topic(); - info.timestamp = header.timestamp(); - - if (header.has_total_length()) { - info.size = static_cast(header.total_length()); - } - - for (const auto& kv : header.attributes()) { - info.attributes.emplace(kv.first, kv.second); - } - - info.name = header.byte_header().name(); - return info; -} - } // namespace livekit diff --git a/src/room_proto_converter.h b/src/room_proto_converter.h index b834d262..8d2c2666 100644 --- a/src/room_proto_converter.h +++ b/src/room_proto_converter.h @@ -28,9 +28,7 @@ namespace livekit { enum class RpcErrorCode; class RemoteParticipant; -struct ByteStreamInfo; struct RoomOptions; -struct TextStreamInfo; // --------- basic helper conversions --------- @@ -44,10 +42,6 @@ LIVEKIT_INTERNAL_API UserPacketData fromProto(const proto::UserPacket& in); LIVEKIT_INTERNAL_API SipDtmfData fromProto(const proto::SipDTMF& in); LIVEKIT_INTERNAL_API RoomInfoData fromProto(const proto::RoomInfo& in); -LIVEKIT_INTERNAL_API DataStreamHeaderData fromProto(const proto::DataStream_Header& in); -LIVEKIT_INTERNAL_API DataStreamChunkData fromProto(const proto::DataStream_Chunk& in); -LIVEKIT_INTERNAL_API DataStreamTrailerData fromProto(const proto::DataStream_Trailer& in); - // --------- event conversions (RoomEvent.oneof message) --------- LIVEKIT_INTERNAL_API RoomSidChangedEvent fromProto(const proto::RoomSidChanged& in); @@ -59,10 +53,6 @@ LIVEKIT_INTERNAL_API ReconnectedEvent fromProto(const proto::Reconnected& in); LIVEKIT_INTERNAL_API TokenRefreshedEvent fromProto(const proto::TokenRefreshed& in); LIVEKIT_INTERNAL_API RoomEosEvent fromProto(const proto::RoomEOS& in); -LIVEKIT_INTERNAL_API DataStreamHeaderReceivedEvent fromProto(const proto::DataStreamHeaderReceived& in); -LIVEKIT_INTERNAL_API DataStreamChunkReceivedEvent fromProto(const proto::DataStreamChunkReceived& in); -LIVEKIT_INTERNAL_API DataStreamTrailerReceivedEvent fromProto(const proto::DataStreamTrailerReceived& in); - LIVEKIT_INTERNAL_API DataChannelBufferedAmountLowThresholdChangedEvent fromProto(const proto::DataChannelBufferedAmountLowThresholdChanged& in); @@ -94,9 +84,4 @@ LIVEKIT_INTERNAL_API UserDataPacketEvent userDataPacketFromProto(const proto::Da LIVEKIT_INTERNAL_API SipDtmfReceivedEvent sipDtmfFromProto(const proto::DataPacketReceived& in, RemoteParticipant* participant); -// --------- room Data Stream conversions --------- -LIVEKIT_INTERNAL_API std::map toAttrMap(const proto::DataStream::Trailer& trailer); -LIVEKIT_INTERNAL_API ByteStreamInfo makeByteInfo(const proto::DataStream::Header& header); -LIVEKIT_INTERNAL_API TextStreamInfo makeTextInfo(const proto::DataStream::Header& header); - } // namespace livekit diff --git a/src/tests/integration/test_data_streams.cpp b/src/tests/integration/test_data_streams.cpp index 2d73e7c3..d6645a28 100644 --- a/src/tests/integration/test_data_streams.cpp +++ b/src/tests/integration/test_data_streams.cpp @@ -16,7 +16,7 @@ // End-to-end coverage for text/byte data streams (registerTextStreamHandler / // registerByteStreamHandler + LocalParticipant::sendText / sendBytes / sendFile -// and TextStreamWriter / ByteStreamWriter). Requires a local SFU; see +// and streamText / streamBytes). Requires a local SFU; see // test_data_track.cpp for setup instructions. Run with: // ./build-debug/bin/livekit_integration_tests --gtest_filter=*DataStream* @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -208,6 +209,17 @@ std::string unicodeText(std::size_t size) { return out; } +/// Incompressible bytes. +std::vector randomBytes(std::size_t size) { + std::mt19937 rng(1234); + std::uniform_int_distribution dist(0, 255); + std::vector out(size); + for (auto& b : out) { + b = static_cast(dist(rng)); + } + return out; +} + /// Records the reader handle of every opened data stream, to probe whether the /// SDK released them. class OpenedStreamHandleRecorder { @@ -268,10 +280,12 @@ TEST_F(DataStreamE2ETest, TextStreamRoundTripEndToEnd) { TextStreamCollector collector; collector.registerOn(*receiver_room, topic); - TextStreamWriter writer(*sender, topic); - writer.write("hello, "); - writer.write("world!"); - writer.close(); + StreamTextOptions options; + options.topic = topic; + auto writer = sender->streamText(options); + writer->write("hello, "); + writer->write("world!"); + writer->close(); ASSERT_TRUE(collector.wait(kStreamWaitTimeout)) << "Timed out waiting for text stream"; EXPECT_EQ(collector.text(), "hello, world!"); @@ -283,8 +297,8 @@ TEST_F(DataStreamE2ETest, TextStreamRoundTripEndToEnd) { // Regression coverage for the `text_header.reply_to_stream_id` field: writing // a text stream with a reply-to id set should surface that id on the // receiving side's TextStreamInfo. If this fails while the C++-side -// conversion (room_proto_converter.cpp: makeTextInfo) looks correct, the gap -// is upstream in the Rust FFI layer not forwarding the field. +// conversion (data_stream_proto_converter.cpp) looks correct, the gap is +// upstream in the Rust FFI layer not forwarding the field. TEST_F(DataStreamE2ETest, TextStreamReplyToStreamIdIsRoutedEndToEnd) { const auto topic = makeTopic("text_reply"); const std::string reply_to_id = "original-stream-" + std::to_string(getTimestampUs()); @@ -296,10 +310,12 @@ TEST_F(DataStreamE2ETest, TextStreamReplyToStreamIdIsRoutedEndToEnd) { TextStreamCollector collector; collector.registerOn(*receiver_room, topic); - TextStreamWriter writer(*lockLocalParticipant(*sender_room), topic, /*attributes=*/{}, /*stream_id=*/"", - /*total_size=*/std::nullopt, reply_to_id); - writer.write("reply payload"); - writer.close(); + StreamTextOptions options; + options.topic = topic; + options.reply_to_stream_id = reply_to_id; + auto writer = lockLocalParticipant(*sender_room)->streamText(options); + writer->write("reply payload"); + writer->close(); ASSERT_TRUE(collector.wait(kStreamWaitTimeout)) << "Timed out waiting for text stream"; EXPECT_EQ(collector.text(), "reply payload"); @@ -320,9 +336,12 @@ TEST_F(DataStreamE2ETest, ByteStreamRoundTripEndToEnd) { ByteStreamCollector collector; collector.registerOn(*receiver_room, topic); - ByteStreamWriter writer(*lockLocalParticipant(*sender_room), /*name=*/"payload.bin", topic); - writer.write(payload); - writer.close(); + StreamByteOptions options; + options.topic = topic; + options.name = "payload.bin"; + auto writer = lockLocalParticipant(*sender_room)->streamBytes(options); + writer->write(payload); + writer->close(); ASSERT_TRUE(collector.wait(kStreamWaitTimeout)) << "Timed out waiting for byte stream"; EXPECT_EQ(collector.content(), payload); @@ -515,9 +534,12 @@ TEST_F(DataStreamE2ETest, TrailerAttributesAreMergedIntoInfo) { TextStreamCollector collector; collector.registerOn(*rooms[1], topic); - TextStreamWriter writer(*lockLocalParticipant(*rooms[0]), topic, {{"header_key", "h"}}); - writer.write("body"); - writer.close("", {{"trailer_key", "t"}}); + StreamTextOptions options; + options.topic = topic; + options.attributes = {{"header_key", "h"}}; + auto writer = lockLocalParticipant(*rooms[0])->streamText(options); + writer->write("body"); + writer->close("", {{"trailer_key", "t"}}); ASSERT_TRUE(collector.wait(kStreamWaitTimeout)) << "Timed out waiting for text stream"; EXPECT_FALSE(collector.error().has_value()) << *collector.error(); @@ -533,9 +555,12 @@ TEST_F(DataStreamE2ETest, AbnormalCloseSurfacesError) { ByteStreamCollector collector; collector.registerOn(*rooms[1], topic); - ByteStreamWriter writer(*lockLocalParticipant(*rooms[0]), "partial.bin", topic); - writer.write({1, 2, 3}); - writer.close("boom"); + StreamByteOptions options; + options.topic = topic; + options.name = "partial.bin"; + auto writer = lockLocalParticipant(*rooms[0])->streamBytes(options); + writer->write({1, 2, 3}); + writer->close("boom"); ASSERT_TRUE(collector.wait(kStreamWaitTimeout)) << "Timed out waiting for byte stream"; ASSERT_TRUE(collector.error().has_value()) << "a close with a reason must surface an error"; @@ -550,15 +575,17 @@ TEST_F(DataStreamE2ETest, ReceiverDisconnectMidStreamWakesReader) { TextStreamCollector collector; collector.registerOn(*rooms[1], topic); - TextStreamWriter writer(*lockLocalParticipant(*rooms[0]), topic); - writer.write("first part"); + StreamTextOptions options; + options.topic = topic; + auto writer = lockLocalParticipant(*rooms[0])->streamText(options); + writer->write("first part"); // Give the receiver time to open the reader and block on the next chunk. std::this_thread::sleep_for(1s); ASSERT_TRUE(rooms[1]->disconnect()); ASSERT_TRUE(collector.wait(kStreamWaitTimeout)) << "A blocked reader must wake when its room disconnects"; EXPECT_TRUE(collector.error().has_value()) << "an interrupted stream must surface an error"; - writer.close(); + writer->close(); } TEST_F(DataStreamE2ETest, ReaderCloseWakesPendingRead) { @@ -574,8 +601,10 @@ TEST_F(DataStreamE2ETest, ReaderCloseWakesPendingRead) { cv.notify_all(); }); - TextStreamWriter writer(*lockLocalParticipant(*rooms[0]), topic); - writer.write("first"); + StreamTextOptions options; + options.topic = topic; + auto writer = lockLocalParticipant(*rooms[0])->streamText(options); + writer->write("first"); { std::unique_lock lock(mutex); ASSERT_TRUE(cv.wait_for(lock, kStreamWaitTimeout, [&] { return reader != nullptr; })); @@ -597,7 +626,7 @@ TEST_F(DataStreamE2ETest, ReaderCloseWakesPendingRead) { read_thread.join(); EXPECT_TRUE(finished); EXPECT_EQ(chunks.load(), 1); - writer.close(); + writer->close(); } TEST_F(DataStreamE2ETest, StreamHandlesAreReleased) { @@ -635,4 +664,201 @@ TEST_F(DataStreamE2ETest, StreamHandlesAreReleased) { } } +// --------------------------------------------------------------------------- +// Incremental writers (LocalParticipant::streamText / streamBytes) +// --------------------------------------------------------------------------- + +TEST_F(DataStreamE2ETest, StreamTextIncrementalUnicode) { + const auto topic = makeTopic("stream_text_unicode"); + auto rooms = testRooms(2); + + TextStreamCollector collector; + collector.registerOn(*rooms[1], topic); + + StreamTextOptions options; + options.topic = topic; + auto writer = lockLocalParticipant(*rooms[0])->streamText(options); + EXPECT_EQ(writer->info().mime_type, "text/plain"); + std::string expected; + for (int i = 0; i < 4; ++i) { + // Each write is bigger than one 15 KB chunk. + const auto part = unicodeText(20 * 1024); + writer->write(part); + expected += part; + } + writer->close(); + EXPECT_TRUE(writer->isClosed()); + + ASSERT_TRUE(collector.wait(kStreamWaitTimeout)) << "Timed out waiting for text stream"; + EXPECT_FALSE(collector.error().has_value()) << *collector.error(); + EXPECT_EQ(collector.text(), expected); + EXPECT_EQ(collector.info().stream_id, writer->streamId()); +} + +TEST_F(DataStreamE2ETest, StreamBytesLargePayloads) { + auto rooms = testRooms(2); + auto sender = lockLocalParticipant(*rooms[0]); + + for (const bool incompressible : {false, true}) { + const auto topic = makeTopic(incompressible ? "stream_bytes_random" : "stream_bytes_pattern"); + std::vector payload; + if (incompressible) { + payload = randomBytes(1024 * 1024); + } else { + payload.resize(1024 * 1024); + for (std::size_t i = 0; i < payload.size(); ++i) { + payload[i] = static_cast(i % 251); + } + } + + ByteStreamCollector collector; + collector.registerOn(*rooms[1], topic); + + StreamByteOptions options; + options.topic = topic; + options.name = "big.bin"; + options.total_length = payload.size(); + auto writer = sender->streamBytes(options); + const std::size_t half = payload.size() / 2; + writer->write(std::vector(payload.begin(), payload.begin() + static_cast(half))); + writer->write(std::vector(payload.begin() + static_cast(half), payload.end())); + writer->close(); + + ASSERT_TRUE(collector.wait(std::chrono::seconds(30))) << "Timed out waiting for byte stream"; + EXPECT_FALSE(collector.error().has_value()) << *collector.error(); + EXPECT_EQ(collector.content().size(), payload.size()); + EXPECT_TRUE(collector.content() == payload) << "payload mismatch (incompressible=" << incompressible << ")"; + ASSERT_TRUE(collector.info().size.has_value()); + EXPECT_EQ(*collector.info().size, payload.size()); + rooms[1]->unregisterByteStreamHandler(topic); + } +} + +TEST_F(DataStreamE2ETest, CloseWaitsForInFlightWrite) { + const auto topic = makeTopic("close_waits"); + const auto payload = randomBytes(2 * 1024 * 1024); + auto rooms = testRooms(2); + + ByteStreamCollector collector; + collector.registerOn(*rooms[1], topic); + + StreamByteOptions options; + options.topic = topic; + auto writer = lockLocalParticipant(*rooms[0])->streamBytes(options); + + std::atomic write_started{false}; + std::thread write_thread([&] { + write_started = true; + writer->write(payload); + }); + while (!write_started) { + std::this_thread::yield(); + } + std::this_thread::sleep_for(5ms); + writer->close(); // Must not overtake the write. + write_thread.join(); + + ASSERT_TRUE(collector.wait(std::chrono::seconds(30))) << "Timed out waiting for byte stream"; + EXPECT_FALSE(collector.error().has_value()) << *collector.error(); + EXPECT_TRUE(collector.content() == payload) + << "received " << collector.content().size() << " of " << payload.size() << " bytes"; +} + +TEST_F(DataStreamE2ETest, WriteAfterCloseThrows) { + auto rooms = testRooms(2); + StreamTextOptions options; + options.topic = makeTopic("write_after_close"); + auto writer = lockLocalParticipant(*rooms[0])->streamText(options); + writer->write("once"); + writer->close(); + EXPECT_THROW(writer->write("twice"), DataStreamError); + EXPECT_THROW(writer->close(), DataStreamError) << "double close throws"; +} + +TEST_F(DataStreamE2ETest, WriteAfterDisconnectThrows) { + const auto topic = makeTopic("write_after_disconnect"); + auto rooms = testRooms(2); + + TextStreamCollector collector; + collector.registerOn(*rooms[1], topic); + + StreamTextOptions options; + options.topic = topic; + auto writer = lockLocalParticipant(*rooms[0])->streamText(options); + writer->write("before"); + ASSERT_TRUE(rooms[0]->disconnect()); + + EXPECT_TRUE(writer->isClosed()); + EXPECT_THROW(writer->write("after"), DataStreamError); + EXPECT_THROW(writer->close(), DataStreamError); + + // The receiver learns the sender left mid-stream. + ASSERT_TRUE(collector.wait(std::chrono::seconds(30))) << "receiver should not hang after the sender disconnects"; + EXPECT_TRUE(collector.error().has_value()); +} + +TEST_F(DataStreamE2ETest, WriterDestroyedWithoutCloseEndsStream) { + const auto topic = makeTopic("writer_destroyed"); + auto rooms = testRooms(2); + + TextStreamCollector collector; + collector.registerOn(*rooms[1], topic); + + { + StreamTextOptions options; + options.topic = topic; + auto writer = lockLocalParticipant(*rooms[0])->streamText(options); + writer->write("unfinished"); + } // Destroyed without close(). + + ASSERT_TRUE(collector.wait(kStreamWaitTimeout)) << "receiver must not wait forever for a dropped writer"; + EXPECT_FALSE(collector.error().has_value()) << *collector.error(); + EXPECT_EQ(collector.text(), "unfinished"); +} + +// The writer constructors are deprecated in favour of streamText / +// streamBytes, but must keep working. +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#elif defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4996) +#endif +TEST_F(DataStreamE2ETest, DeprecatedWriterConstructorsStillWork) { + const auto text_topic = makeTopic("deprecated_text"); + const auto byte_topic = makeTopic("deprecated_bytes"); + auto rooms = testRooms(2); + auto sender = lockLocalParticipant(*rooms[0]); + + TextStreamCollector text_collector; + text_collector.registerOn(*rooms[1], text_topic); + ByteStreamCollector byte_collector; + byte_collector.registerOn(*rooms[1], byte_topic); + + TextStreamWriter text_writer(*sender, text_topic, {{"k", "v"}}); + const std::string stream_id = text_writer.streamId(); + EXPECT_FALSE(stream_id.empty()) << "the id is known before the stream opens"; + text_writer.write("legacy "); + text_writer.write("text"); + text_writer.close(); + + ByteStreamWriter byte_writer(*sender, "legacy.bin", byte_topic); + byte_writer.write({9, 8, 7}); + byte_writer.close(); + + ASSERT_TRUE(text_collector.wait(kStreamWaitTimeout)); + EXPECT_EQ(text_collector.text(), "legacy text"); + EXPECT_EQ(text_collector.info().stream_id, stream_id); + EXPECT_EQ(text_collector.info().attributes.at("k"), "v"); + ASSERT_TRUE(byte_collector.wait(kStreamWaitTimeout)); + EXPECT_EQ(byte_collector.content(), (std::vector{9, 8, 7})); + EXPECT_EQ(byte_collector.info().name, "legacy.bin"); +} +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#elif defined(_MSC_VER) +#pragma warning(pop) +#endif + } // namespace livekit::test diff --git a/src/tests/unit/test_data_stream.cpp b/src/tests/unit/test_data_stream.cpp index c399e422..73669aac 100644 --- a/src/tests/unit/test_data_stream.cpp +++ b/src/tests/unit/test_data_stream.cpp @@ -96,7 +96,20 @@ TEST(DataStreamTest, ByteStreamReaderConstructAndInfo) { TEST(DataStreamTest, WriterTypesAreDerivedFromBase) { static_assert(std::is_base_of_v); static_assert(std::is_base_of_v); + // Deprecated, but must keep compiling for existing callers. +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#elif defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4996) +#endif EXPECT_GT(kStreamChunkSize, 0u); +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#elif defined(_MSC_VER) +#pragma warning(pop) +#endif } // --------------------------------------------------------------------------- @@ -363,4 +376,83 @@ TEST(DataStreamSendTest, FfiHelpersThrowWhenNotInitialized) { EXPECT_THROW(FfiClient::instance().byteStreamReadIncremental(1), std::runtime_error); } +// --------------------------------------------------------------------------- +// Writers without a live room +// --------------------------------------------------------------------------- + +namespace { + +std::shared_ptr detachedParticipant() { + return std::make_shared(FfiHandle(0), "sid", "name", "identity", "", + std::unordered_map{}, ParticipantKind::Standard, + DisconnectReason::Unknown); +} + +} // namespace + +TEST(DataStreamWriterTest, FactoriesThrowWhenNotInitialized) { + ASSERT_FALSE(FfiClient::instance().isInitialized()); + auto participant = detachedParticipant(); + StreamTextOptions text_options; + text_options.topic = "t"; + StreamByteOptions byte_options; + byte_options.topic = "t"; + EXPECT_THROW((void)participant->streamText(text_options), DataStreamError); + EXPECT_THROW((void)participant->streamBytes(byte_options), DataStreamError); +} + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#elif defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4996) +#endif + +TEST(DataStreamWriterTest, DeprecatedConstructorsKeepMetadata) { + auto participant = detachedParticipant(); + TextStreamWriter text(*participant, "chat", {{"k", "v"}}, "my-id", 42, "reply-id"); + EXPECT_EQ(text.streamId(), "my-id"); + EXPECT_EQ(text.topic(), "chat"); + EXPECT_EQ(text.mimeType(), "text/plain"); + EXPECT_FALSE(text.isClosed()); + EXPECT_EQ(text.info().attributes.at("k"), "v"); + ASSERT_TRUE(text.info().size.has_value()); + EXPECT_EQ(*text.info().size, 42u); + EXPECT_EQ(text.info().reply_to_stream_id.value_or(""), "reply-id"); + + ByteStreamWriter bytes(*participant, "file.bin", "files"); + EXPECT_EQ(bytes.streamId().size(), 32u) << "a stream id is generated when none is given"; + EXPECT_EQ(bytes.mimeType(), "application/octet-stream"); + EXPECT_EQ(bytes.info().name, "file.bin"); +} + +TEST(DataStreamWriterTest, WritersCloseWhenParticipantGoes) { + auto participant = detachedParticipant(); + auto writer = std::make_unique(*participant, "chat"); + ASSERT_FALSE(writer->isClosed()); + + participant.reset(); // Closes registered writers. + EXPECT_TRUE(writer->isClosed()); + EXPECT_THROW(writer->write("late"), DataStreamError); + EXPECT_THROW(writer->close(), DataStreamError); + writer.reset(); // Must not touch the destroyed participant. +} + +TEST(DataStreamWriterTest, WriteToClosedWriterThrowsWithoutReachingFfi) { + auto participant = detachedParticipant(); + ByteStreamWriter writer(*participant, "file.bin", "files"); + // An empty write is a no-op and must not open the stream. + EXPECT_NO_THROW(writer.write({})); + // Without an initialized FFI the open fails, and the writer stays usable + // only to report errors. + EXPECT_THROW(writer.write({1, 2, 3}), std::runtime_error); +} + +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#elif defined(_MSC_VER) +#pragma warning(pop) +#endif + } // namespace livekit::test diff --git a/src/tests/unit/test_ffi_client.cpp b/src/tests/unit/test_ffi_client.cpp index 4edf3243..9b51f712 100644 --- a/src/tests/unit/test_ffi_client.cpp +++ b/src/tests/unit/test_ffi_client.cpp @@ -544,25 +544,16 @@ TEST_F(FfiClientTest, NotInitialized_SubscribeDataTrackFails) { EXPECT_EQ(result.error().code, SubscribeDataTrackErrorCode::INTERNAL); } -TEST_F(FfiClientTest, NotInitialized_SendStreamHeaderAsyncThrows) { +TEST_F(FfiClientTest, NotInitialized_StreamWriterHelpersThrow) { ASSERT_FALSE(FfiClient::instance().isInitialized()); - proto::DataStream::Header header; - EXPECT_THROW(FfiClient::instance().sendStreamHeaderAsync(1, header, {}, "sender"), std::runtime_error); -} - -TEST_F(FfiClientTest, NotInitialized_SendStreamChunkAsyncThrows) { - ASSERT_FALSE(FfiClient::instance().isInitialized()); - - proto::DataStream::Chunk chunk; - EXPECT_THROW(FfiClient::instance().sendStreamChunkAsync(1, chunk, {}, "sender"), std::runtime_error); -} - -TEST_F(FfiClientTest, NotInitialized_SendStreamTrailerAsyncThrows) { - ASSERT_FALSE(FfiClient::instance().isInitialized()); - - proto::DataStream::Trailer trailer; - EXPECT_THROW(FfiClient::instance().sendStreamTrailerAsync(1, trailer, "sender"), std::runtime_error); + const std::uint8_t byte = 0; + EXPECT_THROW(FfiClient::instance().textStreamOpenAsync(1, proto::StreamTextOptions{}), std::runtime_error); + EXPECT_THROW(FfiClient::instance().byteStreamOpenAsync(1, proto::StreamByteOptions{}), std::runtime_error); + EXPECT_THROW(FfiClient::instance().textStreamWriteAsync(1, "x"), std::runtime_error); + EXPECT_THROW(FfiClient::instance().byteStreamWriteAsync(1, &byte, 1), std::runtime_error); + EXPECT_THROW(FfiClient::instance().textStreamCloseAsync(1, std::nullopt, {}), std::runtime_error); + EXPECT_THROW(FfiClient::instance().byteStreamCloseAsync(1, std::string("reason"), {}), std::runtime_error); } } // namespace livekit::test