19#include <condition_variable>
30#include "livekit/visibility.h"
34class LocalParticipant;
56 std::optional<std::size_t>
size;
105 void onChunkUpdate(
const std::string& text);
109 void onStreamClose(
const std::map<std::string, std::string>& trailer_attrs);
114 std::deque<std::string> queue_;
115 bool closed_ =
false;
118 std::condition_variable cv_;
141 void onChunkUpdate(
const std::vector<std::uint8_t>& bytes);
145 void onStreamClose(
const std::map<std::string, std::string>& trailer_attrs);
149 std::deque<std::vector<std::uint8_t>> queue_;
150 bool closed_ =
false;
153 std::condition_variable cv_;
163 const std::string&
streamId() const noexcept {
return stream_id_; }
166 const std::string&
topic() const noexcept {
return topic_; }
169 const std::string&
mimeType() const noexcept {
return mime_type_; }
172 std::int64_t
timestampMs() const noexcept {
return timestamp_ms_; }
179 void close(
const std::string& reason =
"",
const std::map<std::string, std::string>& attributes = {});
183 std::map<std::string, std::string> attributes = {}, std::string stream_id =
"",
184 std::optional<std::size_t> total_size = std::nullopt, std::string mime_type =
"",
185 std::vector<std::string> destination_identities = {}, std::string sender_identity =
"");
187 enum class StreamKind { kUnknown, kText, kByte };
189 LocalParticipant& local_participant_;
192 std::string stream_id_;
193 std::string mime_type_;
195 std::int64_t timestamp_ms_ = 0;
196 std::optional<std::size_t> total_size_;
197 std::map<std::string, std::string> attributes_;
198 std::vector<std::string> destination_identities_;
199 std::string sender_identity_;
201 bool closed_ =
false;
202 bool header_sent_ =
false;
203 std::uint64_t next_chunk_index_ = 0;
204 StreamKind kind_ = StreamKind::kUnknown;
205 std::string reply_to_id_;
206 std::string byte_name_;
214 void sendChunk(
const std::vector<std::uint8_t>& content);
218 void sendTrailer(
const std::string& reason,
const std::map<std::string, std::string>& attributes);
225 const std::map<std::string, std::string>& attributes = {},
const std::string& stream_id =
"",
226 std::optional<std::size_t> total_size = std::nullopt,
const std::string& reply_to_id =
"",
227 const std::vector<std::string>& destination_identities = {},
228 const std::string& sender_identity =
"");
233 void write(
const std::string& text);
240 std::mutex write_mutex_;
247 const std::map<std::string, std::string>& attributes = {},
const std::string& stream_id =
"",
248 std::optional<std::size_t> total_size = std::nullopt,
249 const std::string& mime_type =
"application/octet-stream",
250 const std::vector<std::string>& destination_identities = {},
251 const std::string& sender_identity =
"");
256 void write(
const std::vector<std::uint8_t>& data);
263 std::mutex write_mutex_;
272 std::function<void(std::shared_ptr<TextStreamReader>,
const std::string& participant_identity)>;
280 std::function<void(std::shared_ptr<ByteStreamReader>,
const std::string& participant_identity)>;
Base class for sending data streams.
Definition data_stream.h:158
void close(const std::string &reason="", const std::map< std::string, std::string > &attributes={})
Close the stream with optional reason and attributes.
void ensureHeaderSent()
Ensure the header has been sent once.
void sendChunk(const std::vector< std::uint8_t > &content)
Send a raw chunk of bytes.
std::int64_t timestampMs() const noexcept
Timestamp (ms) when the stream was created.
Definition data_stream.h:172
const std::string & streamId() const noexcept
Stream id assigned to this writer.
Definition data_stream.h:163
const std::string & topic() const noexcept
Topic of this stream.
Definition data_stream.h:166
const std::string & mimeType() const noexcept
MIME type for this stream.
Definition data_stream.h:169
bool isClosed() const noexcept
Whether the stream has been closed.
Definition data_stream.h:175
void sendTrailer(const std::string &reason, const std::map< std::string, std::string > &attributes)
Send the trailer with given reason and attributes.
Reader for incoming byte streams.
Definition data_stream.h:122
ByteStreamReader(ByteStreamInfo info)
Construct a reader from initial stream metadata.
bool readNext(std::vector< std::uint8_t > &out)
Blocking read of next byte chunk.
const ByteStreamInfo & info() const noexcept
Metadata associated with this stream.
Definition data_stream.h:135
Writer for outgoing byte streams.
Definition data_stream.h:244
void write(const std::vector< std::uint8_t > &data)
Write binary data to the stream.
const ByteStreamInfo & info() const noexcept
Metadata associated with this stream.
Definition data_stream.h:259
Represents the local participant in a room.
Definition local_participant.h:60
Represents a LiveKit room session.
Definition room.h:142
Reader for incoming text streams.
Definition data_stream.h:82
bool readNext(std::string &out)
Blocking read of next text chunk.
const TextStreamInfo & info() const noexcept
Metadata associated with this stream.
Definition data_stream.h:99
TextStreamReader(TextStreamInfo info)
Construct a reader from initial stream metadata.
std::string readAll()
Convenience: read entire stream into a single string.
Writer for outgoing text streams.
Definition data_stream.h:222
const TextStreamInfo & info() const noexcept
Metadata associated with this stream.
Definition data_stream.h:236
void write(const std::string &text)
Write a UTF-8 string to the stream.
Public API for the LiveKit C++ Client SDK.
Definition audio_frame.h:25
constexpr std::size_t kStreamChunkSize
Chunk size for data streams (matches Python STREAM_CHUNK_SIZE).
Definition data_stream.h:39
std::function< void(std::shared_ptr< ByteStreamReader >, const std::string &participant_identity)> ByteStreamHandler
Callback invoked when a new incoming byte stream is opened.
Definition data_stream.h:280
std::function< void(std::shared_ptr< TextStreamReader >, const std::string &participant_identity)> TextStreamHandler
Callback invoked when a new incoming text stream is opened.
Definition data_stream.h:272
Base metadata for any stream (text or bytes).
Definition data_stream.h:42
std::string topic
Application-defined topic name.
Definition data_stream.h:50
std::string mime_type
MIME type of the stream (e.g. "text/plain", "application/octet-stream").
Definition data_stream.h:47
std::map< std::string, std::string > attributes
Arbitrary key–value attributes attached to the stream.
Definition data_stream.h:59
std::int64_t timestamp
Timestamp in milliseconds when the stream was created.
Definition data_stream.h:53
std::optional< std::size_t > size
Total size of the stream in bytes, if known.
Definition data_stream.h:56
std::string stream_id
Unique identifier for this stream.
Definition data_stream.h:44
Metadata for a byte stream.
Definition data_stream.h:71
std::string name
Optional name of the binary object (e.g. filename).
Definition data_stream.h:73
Metadata for a text stream.
Definition data_stream.h:63
std::optional< std::string > reply_to_stream_id
If this stream is a reply to another stream, this field holds its ID.
Definition data_stream.h:67
std::vector< std::string > attachments
IDs of any attached streams (for attached files).
Definition data_stream.h:65