FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
client-sdk-cpp/src/data_stream.cpp at main · Soralsei/client-sdk-cpp · GitHub
Soralsei
/
client-sdk-cpp
Public
forked from
livekit/client-sdk-cpp
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
client-sdk-cpp
/
src
/
data_stream.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
338 lines (289 loc) · 11.3 KB
Breadcrumbs
client-sdk-cpp
/
src
/
data_stream.cpp
Copy path
File metadata and controls
338 lines (289 loc) · 11.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* 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.
*/
#
include
"
livekit/data_stream.h
"
#
include
<
algorithm
>
#
include
<
chrono
>
#
include
<
random
>
#
include
<
stdexcept
>
#
include
"
ffi_client.h
"
#
include
"
livekit/local_participant.h
"
#
include
"
lk_log.h
"
#
include
"
room.pb.h
"
namespace
livekit
{
namespace
{
std::string
generateRandomId
(std::
size_t
bytes =
16
) {
static
thread_local
std::mt19937_64 rng{std::random_device{}()};
std::uniform_int_distribution<
int
>
dist
(
0
,
255
);
std::string out;
out.
reserve
(bytes *
2
);
const
char
* hex =
"
0123456789abcdef
"
;
for
(std::
size_t
i =
0
; i < bytes; ++i) {
const
int
v =
dist
(rng);
out.
push_back
(hex[(v >>
4
) &
0xF
]);
out.
push_back
(hex[v &
0xF
]);
}
return
out;
}
//
Split UTF-8 string into chunks of at most max_bytes, not breaking codepoints.
std::vector<std::string>
splitUtf8
(
const
std::string& s, std::
size_t
max_bytes) {
std::vector<std::string> 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
<
unsigned
char
>(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
<
unsigned
char
>(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<std::
size_t
>& total_size,
const
std::map<std::string, std::string>& attrs) {
dst.
stream_id
= stream_id;
dst.
mime_type
= mime_type;
dst.
topic
= topic;
dst.
timestamp
= timestamp_ms;
dst.
size
= total_size;
dst.
attributes
= attrs;
}
}
//
namespace
// =====================================================================
//
Reader implementation
// =====================================================================
TextStreamReader::TextStreamReader
(TextStreamInfo info) : info_(std::move(info)) {}
void
TextStreamReader::onChunkUpdate
(
const
std::string& text) {
{
const
std::scoped_lock<std::mutex>
lock
(mutex_);
if
(closed_) {
return
;
}
queue_.
push_back
(text);
}
cv_.
notify_one
();
}
void
TextStreamReader::onStreamClose
(
const
std::map<std::string, std::string>& trailer_attrs) {
{
const
std::scoped_lock<std::mutex>
lock
(mutex_);
for
(
const
auto
& kv : trailer_attrs) {
info_.
attributes
[kv.
first
] = kv.
second
;
}
closed_ =
true
;
}
cv_.
notify_all
();
}
bool
TextStreamReader::readNext
(std::string& out) {
std::unique_lock<std::mutex>
lock
(mutex_);
cv_.
wait
(lock, [
this
] {
return
!queue_.
empty
() || closed_; });
if
(!queue_.
empty
()) {
out =
std::move
(queue_.
front
());
queue_.
pop_front
();
return
true
;
}
return
false
;
//
closed_ and empty
}
std::string
TextStreamReader::readAll
() {
std::string result;
std::string chunk;
while
(
readNext
(chunk)) {
result += chunk;
}
return
result;
}
ByteStreamReader::ByteStreamReader
(ByteStreamInfo info) : info_(std::move(info)) {}
void
ByteStreamReader::onChunkUpdate
(
const
std::vector<std::
uint8_t
>& bytes) {
{
const
std::scoped_lock<std::mutex>
lock
(mutex_);
if
(closed_) {
return
;
}
queue_.
push_back
(bytes);
}
cv_.
notify_one
();
}
void
ByteStreamReader::onStreamClose
(
const
std::map<std::string, std::string>& trailer_attrs) {
{
const
std::scoped_lock<std::mutex>
lock
(mutex_);
for
(
const
auto
& kv : trailer_attrs) {
info_.
attributes
[kv.
first
] = kv.
second
;
}
closed_ =
true
;
}
cv_.
notify_all
();
}
bool
ByteStreamReader::readNext
(std::vector<std::
uint8_t
>& out) {
std::unique_lock<std::mutex>
lock
(mutex_);
cv_.
wait
(lock, [
this
] {
return
!queue_.
empty
() || closed_; });
if
(!queue_.
empty
()) {
out =
std::move
(queue_.
front
());
queue_.
pop_front
();
return
true
;
}
return
false
;
}
// =====================================================================
//
Writer implementation (uses your future-based FfiClient)
// =====================================================================
BaseStreamWriter::BaseStreamWriter
(LocalParticipant& local_participant, std::string topic,
std::map<std::string, std::string> attributes, std::string stream_id,
std::optional<std::
size_t
> total_size, std::string mime_type,
std::vector<std::string> destination_identities, std::string sender_identity)
: local_participant_(local_participant),
stream_id_
(stream_id.empty() ? generateRandomId() : std::move(stream_id)),
mime_type_(std::move(mime_type)),
topic_(std::move(topic)),
timestamp_ms_(
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
.count()),
total_size_(total_size),
attributes_(std::move(attributes)),
destination_identities_(std::move(destination_identities)),
sender_identity_(std::move(sender_identity)) {
if
(sender_identity_.
empty
()) {
sender_identity_ = local_participant_.
identity
();
}
}
void
BaseStreamWriter::ensureHeaderSent
() {
if
(header_sent_) {
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
<std::
uint64_t
>(*total_size_));
}
for
(
const
auto
& kv : attributes_) {
(*header.
mutable_attributes
())[kv.
first
] = kv.
second
;
}
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_);
}
FfiClient::instance
()
.
sendStreamHeaderAsync
(local_participant_.
ffiHandleId
(), header, destination_identities_, sender_identity_)
.
get
();
header_sent_ =
true
;
}
void
BaseStreamWriter::sendChunk
(
const
std::vector<std::
uint8_t
>& content) {
if
(closed_) {
throw
std::runtime_error
(
"
Cannot send chunk after stream is closed
"
);
}
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
());
FfiClient::instance
()
.
sendStreamChunkAsync
(local_participant_.
ffiHandleId
(), chunk, destination_identities_, sender_identity_)
.
get
();
}
void
BaseStreamWriter::sendTrailer
(
const
std::string& reason,
const
std::map<std::string, std::string>& attributes) {
ensureHeaderSent
();
proto::DataStream::Trailer trailer;
trailer.
set_stream_id
(stream_id_);
trailer.
set_reason
(reason);
for
(
const
auto
& kv : attributes) {
(*trailer.
mutable_attributes
())[kv.
first
] = kv.
second
;
}
FfiClient::instance
().
sendStreamTrailerAsync
(local_participant_.
ffiHandleId
(), trailer, sender_identity_).
get
();
}
void
BaseStreamWriter::close
(
const
std::string& reason,
const
std::map<std::string, std::string>& attributes) {
if
(closed_) {
throw
std::runtime_error
(
"
Stream already closed
"
);
}
closed_ =
true
;
sendTrailer
(reason, attributes);
}
TextStreamWriter::TextStreamWriter
(LocalParticipant& local_participant,
const
std::string& topic,
const
std::map<std::string, std::string>& attributes,
const
std::string& stream_id,
std::optional<std::
size_t
> total_size,
const
std::string& reply_to_id,
const
std::vector<std::string>& destination_identities,
const
std::string& sender_identity)
: BaseStreamWriter(local_participant, topic, attributes, stream_id, total_size,
/*
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<std::mutex>
lock
(write_mutex_);
if
(closed_) {
throw
std::runtime_error
(
"
Cannot write to closed TextStreamWriter
"
);
}
for
(
const
auto
& chunk_str :
splitUtf8
(text,
kStreamChunkSize
)) {
const
auto
* p =
reinterpret_cast
<
const
std::
uint8_t
*>(chunk_str.
data
());
const
std::vector<std::
uint8_t
>
bytes
(p, p + chunk_str.
size
());
LK_LOG_DEBUG
(
"
sending chunk
"
);
sendChunk
(bytes);
}
}
ByteStreamWriter::ByteStreamWriter
(LocalParticipant& local_participant,
const
std::string& name,
const
std::string& topic,
const
std::map<std::string, std::string>& attributes,
const
std::string& stream_id, std::optional<std::
size_t
> total_size,
const
std::string& mime_type,
const
std::vector<std::string>& destination_identities,
const
std::string& sender_identity)
: BaseStreamWriter(local_participant, topic, attributes, stream_id, total_size, mime_type, destination_identities,
sender_identity) {
kind_ = StreamKind::
kByte
;
byte_name_ = name;
fillBaseInfo
(info_, stream_id_, mime_type_, topic_, timestamp_ms_, total_size_, attributes_);
info_.
name
= name;
}
void
ByteStreamWriter::write
(
const
std::vector<std::
uint8_t
>& data) {
const
std::scoped_lock<std::mutex>
lock
(write_mutex_);
if
(closed_) {
throw
std::runtime_error
(
"
Cannot write to closed ByteStreamWriter
"
);
}
std::
size_t
offset =
0
;
while
(offset < data.
size
()) {
const
std::
size_t
n = std::min<std::
size_t
>(
kStreamChunkSize
, data.
size
() - offset);
auto
it = data.
begin
() +
static_cast
<std::
ptrdiff_t
>(offset);
const
std::vector<std::
uint8_t
>
chunk
(it, it +
static_cast
<std::
ptrdiff_t
>(n));
sendChunk
(chunk);
offset += n;
}
}
}
//
namespace livekit
Back
|
FazBrowse Home
|
New Git URL