livekit-data-stream 0.2.0

Data stream core logic for LiveKit
Documentation
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Copyright 2026 LiveKit, Inc.
//
// 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.

//! Outgoing data streams: the [`manager::Manager`] and the writers it produces.

use livekit_common::ParticipantIdentity;
use std::{collections::HashMap, fmt, sync::Arc};

use crate::{
    types::{OperationType, StreamId, TextHeader},
    utils::StreamProgress,
};

pub(crate) mod manager;

mod constants;
mod raw_stream;
mod stream_writer;

pub use stream_writer::{ByteStreamWriter, StreamWriter, TextStreamWriter};

/// Receives outgoing send progress on the thread polling the send; callbacks should return quickly.
///
/// Clones share the callback, and equality compares callback identity rather than behavior.
/// `Debug` prints the placeholder `StreamProgressCallback` without inspecting the closure.
#[derive(Clone)]
pub struct StreamProgressCallback(Arc<dyn Fn(StreamProgress) + Send + Sync>);

impl StreamProgressCallback {
    /// Wraps a callback that receives progress before compression.
    pub fn new(callback: impl Fn(StreamProgress) + Send + Sync + 'static) -> Self {
        Self(Arc::new(callback))
    }

    /// Invokes the callback with the current progress.
    pub fn call(&self, progress: StreamProgress) {
        (self.0)(progress);
    }
}

impl fmt::Debug for StreamProgressCallback {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("StreamProgressCallback")
    }
}

impl PartialEq for StreamProgressCallback {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

impl Eq for StreamProgressCallback {}

macro_rules! common_setters {
    () => {
        /// Sets the topic the stream is published to, accepting any value convertible to `String`.
        pub fn with_topic(mut self, topic: impl Into<String>) -> Self {
            self.topic = topic.into();
            self
        }
        /// Replaces all attributes attached to the stream.
        pub fn with_attributes(mut self, attributes: HashMap<String, String>) -> Self {
            self.attributes = attributes;
            self
        }
        /// Adds a single attribute to the stream, overwriting any existing value for `key`.
        pub fn with_attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
            self.attributes.insert(key.into(), value.into());
            self
        }
        /// Replaces the set of participant identities the stream is delivered to.
        /// An empty list delivers to all participants in the room.
        pub fn with_destination_identities(
            mut self,
            destination_identities: Vec<ParticipantIdentity>,
        ) -> Self {
            self.destination_identities = destination_identities;
            self
        }
        /// Adds a single participant identity to the stream's destinations.
        pub fn with_destination_identity(
            mut self,
            identity: impl Into<ParticipantIdentity>,
        ) -> Self {
            self.destination_identities.push(identity.into());
            self
        }
        /// Sets an explicit stream id. If unset, a new uuid is generated per call.
        pub fn with_id(mut self, id: impl Into<String>) -> Self {
            self.id = Some(id.into());
            self
        }
        /// Sets the identity the stream's packets are attributed to. Only participants with the
        /// appropriate permission (e.g. agents) may impersonate another identity.
        pub fn with_sender_identity(mut self, identity: impl Into<ParticipantIdentity>) -> Self {
            self.sender_identity = Some(identity.into());
            self
        }
    };
}

macro_rules! text_setters {
    () => {
        pub(crate) fn text_header(&self) -> TextHeader {
            TextHeader {
                operation_type: self.operation_type.unwrap_or_default(),
                version: self.version.unwrap_or_default(),
                reply_to_stream_id: self.reply_to_stream_id.clone().map(StreamId::from),
                attached_stream_ids: self
                    .attached_stream_ids
                    .clone()
                    .into_iter()
                    .map(StreamId::from)
                    .collect(),
                generated: self.generated.unwrap_or_default(),
            }
        }

        /// Sets the operation this text stream represents (e.g. create or update).
        pub fn with_operation_type(mut self, operation_type: OperationType) -> Self {
            self.operation_type = Some(operation_type);
            self
        }
        /// Sets the version of the text, used to order updates to the same content.
        pub fn with_version(mut self, version: i32) -> Self {
            self.version = Some(version);
            self
        }
        /// Sets the id of the stream this text is a reply to.
        pub fn with_reply_to_stream_id(mut self, reply_to_stream_id: impl Into<String>) -> Self {
            self.reply_to_stream_id = Some(reply_to_stream_id.into());
            self
        }
        /// Replaces the set of stream ids attached to this text (e.g. referenced files).
        pub fn with_attached_stream_ids(mut self, attached_stream_ids: Vec<String>) -> Self {
            self.attached_stream_ids = attached_stream_ids;
            self
        }
        /// Adds a single attached stream id to this text.
        pub fn with_attached_stream_id(mut self, attached_stream_id: impl Into<String>) -> Self {
            self.attached_stream_ids.push(attached_stream_id.into());
            self
        }
        /// Sets whether the text was machine-generated (e.g. by an agent).
        pub fn with_generated(mut self, generated: bool) -> Self {
            self.generated = Some(generated);
            self
        }
    };
}

macro_rules! byte_setters {
    () => {
        /// Sets the MIME type describing the stream's payload.
        pub fn with_mime_type(mut self, mime_type: impl Into<String>) -> Self {
            self.mime_type = Some(mime_type.into());
            self
        }
        /// Sets a human-readable name for the stream (e.g. a file name).
        pub fn with_name(mut self, name: impl Into<String>) -> Self {
            self.name = Some(name.into());
            self
        }
    };
}

macro_rules! send_setters {
    () => {
        /// Sets whether to deflate-raw compress when all recipients support it.
        /// Defaults to `true` (compression opt-out).
        pub fn with_compress(mut self, compress: bool) -> Self {
            self.compress = Some(compress);
            self
        }
        /// Sets the progress callback; see [`Self::on_progress`] for reporting semantics.
        pub fn with_on_progress(
            mut self,
            callback: impl Fn(StreamProgress) + Send + Sync + 'static,
        ) -> Self {
            self.on_progress = Some(StreamProgressCallback::new(callback));
            self
        }
    };
}

macro_rules! stream_setters {
    () => {
        /// Sets the total byte length of the payload, when known ahead of time.
        pub fn with_total_length(mut self, total_length: u64) -> Self {
            self.total_length = Some(total_length);
            self
        }
    };
}

/// Options for [`send_text`](manager::Manager::send_text). Progress is reported once after the complete send is acknowledged.
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SendTextOptions {
    /// Topic the stream is published to.
    pub topic: String,
    /// Attributes attached to the stream.
    pub attributes: HashMap<String, String>,
    /// Recipients; an empty list delivers to all participants.
    pub destination_identities: Vec<ParticipantIdentity>,
    /// Explicit stream id, or a new UUID per call when unspecified.
    pub id: Option<String>,
    /// Operation this text stream represents.
    pub operation_type: Option<OperationType>,
    /// Version used to order updates to the same content.
    pub version: Option<i32>,
    /// Id of the stream this text replies to.
    pub reply_to_stream_id: Option<String>,
    /// Ids of streams attached to this text.
    pub attached_stream_ids: Vec<String>,
    /// Whether the text was machine-generated.
    pub generated: Option<bool>,
    /// Identity attributed to the packets, or the sending participant when unspecified.
    /// Impersonating another identity requires the appropriate permission (e.g. agents).
    pub sender_identity: Option<ParticipantIdentity>,
    /// Whether to deflate-raw compress when all recipients support it. Defaults to true.
    pub compress: Option<bool>,
    /// Reports payload byte length once after the complete send is acknowledged.
    pub on_progress: Option<StreamProgressCallback>,
}

impl SendTextOptions {
    /// Creates options for the given topic. Use the `with_*` setters to customize them.
    pub fn new_with_topic(topic: impl Into<String>) -> Self {
        Self {
            topic: topic.into(),
            attributes: HashMap::new(),
            destination_identities: vec![],
            id: None,
            operation_type: None,
            version: None,
            reply_to_stream_id: None,
            attached_stream_ids: vec![],
            generated: None,
            sender_identity: None,
            compress: None,
            on_progress: None,
        }
    }

    common_setters!();
    text_setters!();
    send_setters!();
}

/// Options for [`send_bytes`](manager::Manager::send_bytes) and [`send_file`](manager::Manager::send_file).
///
/// See [`Self::on_progress`] for progress reporting semantics.
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SendByteOptions {
    /// Topic the stream is published to.
    pub topic: String,
    /// Attributes attached to the stream.
    pub attributes: HashMap<String, String>,
    /// Recipients; an empty list delivers to all participants.
    pub destination_identities: Vec<ParticipantIdentity>,
    /// Explicit stream id, or a new UUID per call when unspecified.
    pub id: Option<String>,
    /// MIME type describing the payload.
    pub mime_type: Option<String>,
    /// Human-readable name; `send_file` defaults to the file name.
    pub name: Option<String>,
    /// Identity attributed to the packets, or the sending participant when unspecified.
    /// Impersonating another identity requires the appropriate permission (e.g. agents).
    pub sender_identity: Option<ParticipantIdentity>,
    /// Whether to deflate-raw compress when all recipients support it. Defaults to true.
    pub compress: Option<bool>,
    /// Byte sends report once after the complete send is acknowledged. Files report input bytes
    /// before compression, against the file size, after each chunk is sent or each encoder drain.
    /// Compressed progress can run ahead of chunks sent; the whole file is reported only after
    /// its last chunk was sent.
    pub on_progress: Option<StreamProgressCallback>,
}

impl SendByteOptions {
    /// Creates options for the given topic. Use the `with_*` setters to customize them.
    pub fn new_with_topic(topic: impl Into<String>) -> Self {
        Self {
            topic: topic.into(),
            attributes: HashMap::new(),
            destination_identities: vec![],
            id: None,
            mime_type: None,
            name: None,
            sender_identity: None,
            compress: None,
            on_progress: None,
        }
    }

    common_setters!();
    byte_setters!();
    send_setters!();
}

/// Options for [`stream_text`](manager::Manager::stream_text), which never compresses or inlines content.
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StreamTextOptions {
    /// Topic the stream is published to.
    pub topic: String,
    /// Attributes attached to the stream.
    pub attributes: HashMap<String, String>,
    /// Recipients; an empty list delivers to all participants.
    pub destination_identities: Vec<ParticipantIdentity>,
    /// Explicit stream id, or a new UUID per call when unspecified.
    pub id: Option<String>,
    /// Operation this text stream represents.
    pub operation_type: Option<OperationType>,
    /// Version used to order updates to the same content.
    pub version: Option<i32>,
    /// Id of the stream this text replies to.
    pub reply_to_stream_id: Option<String>,
    /// Ids of streams attached to this text.
    pub attached_stream_ids: Vec<String>,
    /// Whether the text was machine-generated.
    pub generated: Option<bool>,
    /// Identity attributed to the packets, or the sending participant when unspecified.
    /// Impersonating another identity requires the appropriate permission (e.g. agents).
    pub sender_identity: Option<ParticipantIdentity>,
    /// Declared total byte length, when known. The receiver fails the stream on a mismatch.
    pub total_length: Option<u64>,
}

impl StreamTextOptions {
    /// Creates options for the given topic. Use the `with_*` setters to customize them.
    pub fn new_with_topic(topic: impl Into<String>) -> Self {
        Self {
            topic: topic.into(),
            attributes: HashMap::new(),
            destination_identities: vec![],
            id: None,
            operation_type: None,
            version: None,
            reply_to_stream_id: None,
            attached_stream_ids: vec![],
            generated: None,
            sender_identity: None,
            total_length: None,
        }
    }

    common_setters!();
    text_setters!();
    stream_setters!();
}

/// Options for [`stream_bytes`](manager::Manager::stream_bytes), which never compresses or inlines content.
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StreamByteOptions {
    /// Topic the stream is published to.
    pub topic: String,
    /// Attributes attached to the stream.
    pub attributes: HashMap<String, String>,
    /// Recipients; an empty list delivers to all participants.
    pub destination_identities: Vec<ParticipantIdentity>,
    /// Explicit stream id, or a new UUID per call when unspecified.
    pub id: Option<String>,
    /// MIME type describing the payload.
    pub mime_type: Option<String>,
    /// Human-readable name for the stream.
    pub name: Option<String>,
    /// Identity attributed to the packets, or the sending participant when unspecified.
    /// Impersonating another identity requires the appropriate permission (e.g. agents).
    pub sender_identity: Option<ParticipantIdentity>,
    /// Declared total byte length, when known. The receiver fails the stream on a mismatch.
    pub total_length: Option<u64>,
}

impl StreamByteOptions {
    /// Creates options for the given topic. Use the `with_*` setters to customize them.
    pub fn new_with_topic(topic: impl Into<String>) -> Self {
        Self {
            topic: topic.into(),
            attributes: HashMap::new(),
            destination_identities: vec![],
            id: None,
            mime_type: None,
            name: None,
            sender_identity: None,
            total_length: None,
        }
    }

    common_setters!();
    byte_setters!();
    stream_setters!();
}