LiveKit C++ Client SDK v1.9.0
Real-time audio/video/data SDK for C++
Loading...
Searching...
No Matches
data_stream.h
1/*
2 * Copyright 2025 LiveKit
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an “AS IS” BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <condition_variable>
20#include <cstdint>
21#include <deque>
22#include <functional>
23#include <map>
24#include <memory>
25#include <mutex>
26#include <optional>
27#include <string>
28#include <vector>
29
30#include "livekit/visibility.h"
31
32namespace livekit {
33
34class LocalParticipant;
35
39constexpr std::size_t kStreamChunkSize = 15'000; // 15 KB
40
44 std::string stream_id;
45
47 std::string mime_type;
48
50 std::string topic;
51
53 std::int64_t timestamp = 0;
54
56 std::optional<std::size_t> size;
57
59 std::map<std::string, std::string> attributes;
60};
61
65 std::vector<std::string> attachments;
67 std::optional<std::string> reply_to_stream_id;
68};
69
73 std::string name;
74};
75
76// Readers
77// - TextStreamReader: yields UTF-8 text chunks (std::string)
78// - ByteStreamReader: yields raw bytes (std::vector<uint8_t>)
79
82class LIVEKIT_API TextStreamReader {
83public:
86
87 TextStreamReader(const TextStreamReader&) = delete;
88 TextStreamReader& operator=(const TextStreamReader&) = delete;
89
92 bool readNext(std::string& out);
93
96 std::string readAll();
97
99 const TextStreamInfo& info() const noexcept { return info_; }
100
101private:
102 friend class Room;
103
105 void onChunkUpdate(const std::string& text);
106
109 void onStreamClose(const std::map<std::string, std::string>& trailer_attrs);
110
111 TextStreamInfo info_;
112
113 // Queue of text chunks; empty string with closed_==true means EOS.
114 std::deque<std::string> queue_;
115 bool closed_ = false;
116
117 std::mutex mutex_;
118 std::condition_variable cv_;
119};
120
122class LIVEKIT_API ByteStreamReader {
123public:
126
127 ByteStreamReader(const ByteStreamReader&) = delete;
128 ByteStreamReader& operator=(const ByteStreamReader&) = delete;
129
132 bool readNext(std::vector<std::uint8_t>& out);
133
135 const ByteStreamInfo& info() const noexcept { return info_; }
136
137private:
138 friend class Room;
139
141 void onChunkUpdate(const std::vector<std::uint8_t>& bytes);
142
145 void onStreamClose(const std::map<std::string, std::string>& trailer_attrs);
146
147 ByteStreamInfo info_;
148
149 std::deque<std::vector<std::uint8_t>> queue_;
150 bool closed_ = false;
151
152 std::mutex mutex_;
153 std::condition_variable cv_;
154};
155
158class LIVEKIT_API BaseStreamWriter {
159public:
160 virtual ~BaseStreamWriter() = default;
161
163 const std::string& streamId() const noexcept { return stream_id_; }
164
166 const std::string& topic() const noexcept { return topic_; }
167
169 const std::string& mimeType() const noexcept { return mime_type_; }
170
172 std::int64_t timestampMs() const noexcept { return timestamp_ms_; }
173
175 bool isClosed() const noexcept { return closed_; }
176
179 void close(const std::string& reason = "", const std::map<std::string, std::string>& attributes = {});
180
181protected:
182 BaseStreamWriter(LocalParticipant& local_participant, std::string topic = "",
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 = "");
186
187 enum class StreamKind { kUnknown, kText, kByte };
188
189 LocalParticipant& local_participant_;
190
191 // Public-ish metadata (mirrors BaseStreamInfo, but kept simple here)
192 std::string stream_id_;
193 std::string mime_type_;
194 std::string topic_;
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_;
200
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_; // Used by ByteStreamWriter
207
211
214 void sendChunk(const std::vector<std::uint8_t>& content);
215
218 void sendTrailer(const std::string& reason, const std::map<std::string, std::string>& attributes);
219};
220
222class LIVEKIT_API TextStreamWriter : public BaseStreamWriter {
223public:
224 TextStreamWriter(LocalParticipant& local_participant, const std::string& topic = "",
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 = "");
229
233 void write(const std::string& text);
234
236 const TextStreamInfo& info() const noexcept { return info_; }
237
238private:
239 TextStreamInfo info_;
240 std::mutex write_mutex_;
241};
242
244class LIVEKIT_API ByteStreamWriter : public BaseStreamWriter {
245public:
246 ByteStreamWriter(LocalParticipant& local_participant, const std::string& name, const std::string& topic = "",
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 = "");
252
256 void write(const std::vector<std::uint8_t>& data);
257
259 const ByteStreamInfo& info() const noexcept { return info_; }
260
261private:
262 ByteStreamInfo info_;
263 std::mutex write_mutex_;
264};
265
272 std::function<void(std::shared_ptr<TextStreamReader>, const std::string& participant_identity)>;
273
280 std::function<void(std::shared_ptr<ByteStreamReader>, const std::string& participant_identity)>;
281
282} // namespace livekit
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