livekit-datatrack 0.1.12

Data track core 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
408
409
410
411
412
// 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.

use livekit_protocol as proto;
use std::sync::Arc;
use thiserror::Error;

/// Identifier for a data track schema.
///
/// A compound identifier with two components: name and encoding.
///
/// Two IDs are equal only if both components match; the same name with a
/// different encoding refers to a distinct schema. Cloning this type is cheap.
///
/// # Examples
///
/// ```
/// # use livekit_datatrack::api::{DataTrackSchemaId, DataTrackSchemaEncoding};
/// let schema = DataTrackSchemaId::new("my_schema", DataTrackSchemaEncoding::Protobuf);
///
/// assert_eq!(schema.name(), "my_schema");
/// assert_eq!(schema.encoding(), &DataTrackSchemaEncoding::Protobuf);
/// ```
///
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct DataTrackSchemaId {
    inner: Arc<DataTrackSchemaIdInner>,
}

#[derive(Hash, PartialEq, Eq)]
struct DataTrackSchemaIdInner {
    name: String,
    encoding: DataTrackSchemaEncoding,
}

impl std::fmt::Debug for DataTrackSchemaId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DataTrackSchemaId")
            .field("name", &self.inner.name)
            .field("encoding", &self.inner.encoding)
            .finish()
    }
}

impl DataTrackSchemaId {
    /// Creates a new schema ID.
    pub fn new(name: impl Into<String>, encoding: DataTrackSchemaEncoding) -> Self {
        let inner = DataTrackSchemaIdInner { name: name.into(), encoding }.into();
        Self { inner }
    }

    /// Returns the name component of the ID.
    pub fn name(&self) -> &str {
        &self.inner.name
    }

    /// Returns the encoding component of the ID.
    pub fn encoding(&self) -> &DataTrackSchemaEncoding {
        &self.inner.encoding
    }
}

/// Encoding used for a schema definition.
///
/// Identifies the interface definition language the schema is written in (e.g. a
/// `.proto` file for [`Protobuf`]). This in turn dictates the wire format of the
/// frames the schema describes, captured by [`DataTrackFrameEncoding`].
///
/// [`Protobuf`]: DataTrackSchemaEncoding::Protobuf
///
#[non_exhaustive]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(test, derive(fake::Dummy))]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
pub enum DataTrackSchemaEncoding {
    /// Protocol Buffer IDL, describes [`Protobuf`] encoded frames.
    ///
    /// [`Protobuf`]: DataTrackFrameEncoding::Protobuf
    Protobuf,
    /// FlatBuffer IDL, describes [`Flatbuffer`] encoded frames.
    ///
    /// [`Flatbuffer`]: DataTrackFrameEncoding::Flatbuffer
    Flatbuffer,
    /// ROS 1 Message, describes [`Ros1`] encoded frames.
    ///
    /// [`Ros1`]: DataTrackFrameEncoding::Ros1
    Ros1Msg,
    /// ROS 2 Message, describes [`Cdr`] encoded frames.
    ///
    /// [`Cdr`]: DataTrackFrameEncoding::Cdr
    Ros2Msg,
    /// ROS 2 IDL, describes [`Cdr`] encoded frames.
    ///
    /// [`Cdr`]: DataTrackFrameEncoding::Cdr
    Ros2Idl,
    /// OMG IDL, describes [`Cdr`] encoded frames.
    ///
    /// [`Cdr`]: DataTrackFrameEncoding::Cdr
    OmgIdl,
    /// JSON Schema, describes [`Json`] encoded frames.
    ///
    /// [`Json`]: DataTrackFrameEncoding::Json
    JsonSchema,

    /// Another well-known encoding not known to this client version.
    Other,
    /// An application-specific encoding identified by the contained string.
    ///
    /// Prefer using one of the well-known encodings unless the format is not enumerated.
    /// The identifier must be non-empty and not exceed the server's length limit.
    ///
    Custom(String),
}

/// Encoding used for frames pushed on a data track.
///
/// The serialization format of the frame bytes (e.g. [`Protobuf`]); the structure
/// of those bytes is described by a schema, see [`DataTrackSchemaEncoding`].
///
/// [`Protobuf`]: DataTrackFrameEncoding::Protobuf
///
#[non_exhaustive]
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(test, derive(fake::Dummy))]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
pub enum DataTrackFrameEncoding {
    /// ROS 1, must be described by a [`Ros1Msg`] schema.
    ///
    /// [`Ros1Msg`]: DataTrackSchemaEncoding::Ros1Msg
    Ros1,
    /// CDR, must be described by a [`Ros2Msg`], [`Ros2Idl`], or [`OmgIdl`] schema.
    ///
    /// [`Ros2Msg`]: DataTrackSchemaEncoding::Ros2Msg
    /// [`Ros2Idl`]: DataTrackSchemaEncoding::Ros2Idl
    /// [`OmgIdl`]: DataTrackSchemaEncoding::OmgIdl
    Cdr,
    /// Protocol Buffer, must be described by a [`Protobuf`] schema.
    ///
    /// [`Protobuf`]: DataTrackSchemaEncoding::Protobuf
    Protobuf,
    /// FlatBuffer, must be described by a [`Flatbuffer`] schema.
    ///
    /// [`Flatbuffer`]: DataTrackSchemaEncoding::Flatbuffer
    Flatbuffer,
    /// CBOR, self-describing.
    Cbor,
    /// MessagePack, self-describing.
    Msgpack,
    /// JSON, self-describing or described by a [`JsonSchema`] schema.
    ///
    /// [`JsonSchema`]: DataTrackSchemaEncoding::JsonSchema
    Json,

    /// Another well-known encoding not known to this client version.
    Other,
    /// An application-specific encoding identified by the contained string.
    ///
    /// Prefer using one of the well-known encodings unless the format is not enumerated.
    /// The identifier must be non-empty and not exceed the server's length limit.
    ///
    Custom(String),
}

/// An error that can occur when validating data track schema metadata.
#[derive(Debug, Error, PartialEq)]
pub enum DataTrackSchemaError {
    /// Frame encoding is required when providing schema ID.
    #[error("Frame encoding is required when providing schema ID")]
    MissingFrameEncoding,

    /// Schema ID is required for frame encoding that is not self-describing.
    #[error("Schema ID is required for frame encoding that is not self-describing")]
    MissingSchemaId,

    /// Specified schema and frame encodings are incompatible.
    #[error("Specified schema and frame encodings are incompatible")]
    Incompatible,
}

/// Validates that the given frame and schema encodings are compatible.
pub(crate) fn validate_schema(
    frame_encoding: Option<&DataTrackFrameEncoding>,
    schema_encoding: Option<&DataTrackSchemaEncoding>,
) -> Result<(), DataTrackSchemaError> {
    match (frame_encoding, schema_encoding) {
        (None, Some(_)) => Err(DataTrackSchemaError::MissingFrameEncoding),
        (Some(frame_encoding), None) => match frame_encoding.is_self_describing() {
            Some(false) => Err(DataTrackSchemaError::MissingSchemaId),
            _ => Ok(()),
        },
        (Some(frame_encoding), Some(schema_encoding)) => {
            match frame_encoding.is_described_by(schema_encoding) {
                Some(false) => Err(DataTrackSchemaError::Incompatible),
                _ => Ok(()),
            }
        }
        (None, None) => Ok(()), // Not using schema metadata
    }
}

impl DataTrackFrameEncoding {
    /// Returns whether the frame encoding is self-describing (i.e. requires no schema).
    fn is_self_describing(&self) -> Option<bool> {
        match self {
            Self::Cbor | Self::Msgpack | Self::Json => Some(true),
            Self::Other | Self::Custom(_) => None, // Cannot be determined
            _ => Some(false),
        }
    }

    /// Returns whether the frame encoding can be described by the given schema encoding.
    fn is_described_by(&self, schema_encoding: &DataTrackSchemaEncoding) -> Option<bool> {
        use DataTrackSchemaEncoding as SchemaEncoding;
        match (self, schema_encoding) {
            (Self::Ros1, SchemaEncoding::Ros1Msg)
            | (Self::Cdr, SchemaEncoding::Ros2Msg)
            | (Self::Cdr, SchemaEncoding::Ros2Idl)
            | (Self::Cdr, SchemaEncoding::OmgIdl)
            | (Self::Protobuf, SchemaEncoding::Protobuf)
            | (Self::Flatbuffer, SchemaEncoding::Flatbuffer)
            | (Self::Json, SchemaEncoding::JsonSchema) => Some(true),
            (Self::Other, _) | (Self::Custom(_), _) => None, // Cannot be determined
            _ => Some(false),
        }
    }
}

impl From<proto::DataTrackSchemaId> for DataTrackSchemaId {
    fn from(msg: proto::DataTrackSchemaId) -> Self {
        let encoding = msg.encoding.map(Into::into).unwrap_or(DataTrackSchemaEncoding::Other);
        DataTrackSchemaId::new(msg.name, encoding)
    }
}

impl From<DataTrackSchemaId> for proto::DataTrackSchemaId {
    fn from(value: DataTrackSchemaId) -> Self {
        Self { name: value.name().to_string(), encoding: Some(value.encoding().clone().into()) }
    }
}

impl From<proto::DataTrackSchemaEncoding> for DataTrackSchemaEncoding {
    fn from(msg: proto::DataTrackSchemaEncoding) -> Self {
        use proto::data_track_schema_encoding::{Value, WellKnownSchemaEncoding as WellKnown};
        match msg.value {
            Some(Value::WellKnown(value)) => match WellKnown::try_from(value) {
                Ok(WellKnown::Protobuf) => Self::Protobuf,
                Ok(WellKnown::Flatbuffer) => Self::Flatbuffer,
                Ok(WellKnown::Ros1Msg) => Self::Ros1Msg,
                Ok(WellKnown::Ros2Msg) => Self::Ros2Msg,
                Ok(WellKnown::Ros2Idl) => Self::Ros2Idl,
                Ok(WellKnown::OmgIdl) => Self::OmgIdl,
                Ok(WellKnown::JsonSchema) => Self::JsonSchema,
                // Unspecified or a value introduced after this client version.
                Ok(WellKnown::Unspecified) | Err(_) => Self::Other,
            },
            Some(Value::Custom(name)) => Self::Custom(name),
            None => Self::Other,
        }
    }
}

impl From<DataTrackSchemaEncoding> for proto::DataTrackSchemaEncoding {
    fn from(value: DataTrackSchemaEncoding) -> Self {
        use proto::data_track_schema_encoding::{Value, WellKnownSchemaEncoding as WellKnown};
        let well_known = match value {
            DataTrackSchemaEncoding::Protobuf => WellKnown::Protobuf,
            DataTrackSchemaEncoding::Flatbuffer => WellKnown::Flatbuffer,
            DataTrackSchemaEncoding::Ros1Msg => WellKnown::Ros1Msg,
            DataTrackSchemaEncoding::Ros2Msg => WellKnown::Ros2Msg,
            DataTrackSchemaEncoding::Ros2Idl => WellKnown::Ros2Idl,
            DataTrackSchemaEncoding::OmgIdl => WellKnown::OmgIdl,
            DataTrackSchemaEncoding::JsonSchema => WellKnown::JsonSchema,
            DataTrackSchemaEncoding::Other => WellKnown::Unspecified,
            DataTrackSchemaEncoding::Custom(name) => {
                return Self { value: Some(Value::Custom(name)) }
            }
        };
        Self { value: Some(Value::WellKnown(well_known as i32)) }
    }
}

impl From<proto::DataTrackFrameEncoding> for DataTrackFrameEncoding {
    fn from(msg: proto::DataTrackFrameEncoding) -> Self {
        use proto::data_track_frame_encoding::{Value, WellKnownFrameEncoding as WellKnown};
        match msg.value {
            Some(Value::WellKnown(value)) => match WellKnown::try_from(value) {
                Ok(WellKnown::Ros1) => Self::Ros1,
                Ok(WellKnown::Cdr) => Self::Cdr,
                Ok(WellKnown::Protobuf) => Self::Protobuf,
                Ok(WellKnown::Flatbuffer) => Self::Flatbuffer,
                Ok(WellKnown::Cbor) => Self::Cbor,
                Ok(WellKnown::Msgpack) => Self::Msgpack,
                Ok(WellKnown::Json) => Self::Json,
                // Unspecified or a value introduced after this client version.
                Ok(WellKnown::Unspecified) | Err(_) => Self::Other,
            },
            Some(Value::Custom(name)) => Self::Custom(name),
            None => Self::Other,
        }
    }
}

impl From<DataTrackFrameEncoding> for proto::DataTrackFrameEncoding {
    fn from(value: DataTrackFrameEncoding) -> Self {
        use proto::data_track_frame_encoding::{Value, WellKnownFrameEncoding as WellKnown};
        let well_known = match value {
            DataTrackFrameEncoding::Ros1 => WellKnown::Ros1,
            DataTrackFrameEncoding::Cdr => WellKnown::Cdr,
            DataTrackFrameEncoding::Protobuf => WellKnown::Protobuf,
            DataTrackFrameEncoding::Flatbuffer => WellKnown::Flatbuffer,
            DataTrackFrameEncoding::Cbor => WellKnown::Cbor,
            DataTrackFrameEncoding::Msgpack => WellKnown::Msgpack,
            DataTrackFrameEncoding::Json => WellKnown::Json,
            DataTrackFrameEncoding::Other => WellKnown::Unspecified,
            DataTrackFrameEncoding::Custom(name) => {
                return Self { value: Some(Value::Custom(name)) }
            }
        };
        Self { value: Some(Value::WellKnown(well_known as i32)) }
    }
}

impl From<DataTrackSchemaId> for proto::DataBlobKey {
    fn from(id: DataTrackSchemaId) -> Self {
        Self { key: Some(proto::data_blob_key::Key::SchemaId(id.into())) }
    }
}

#[cfg(test)]
impl fake::Dummy<fake::Faker> for DataTrackSchemaId {
    fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &fake::Faker, rng: &mut R) -> Self {
        use fake::{Fake, Faker};
        let name: String = Faker.fake_with_rng(rng);
        let encoding: DataTrackSchemaEncoding = Faker.fake_with_rng(rng);
        Self::new(name, encoding)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validate_schema_not_specified() {
        assert_eq!(validate_schema(None, None), Ok(()));
    }

    #[test]
    fn test_validate_schema_self_describing() {
        assert_eq!(validate_schema(Some(&DataTrackFrameEncoding::Json), None), Ok(()));
    }

    #[test]
    fn test_validate_schema_compatible_encodings() {
        assert_eq!(
            validate_schema(
                Some(&DataTrackFrameEncoding::Cdr),
                Some(&DataTrackSchemaEncoding::Ros2Idl)
            ),
            Ok(())
        );
    }

    #[test]
    fn test_validate_schema_custom() {
        assert_eq!(
            validate_schema(
                Some(&DataTrackFrameEncoding::Custom("my-frame-encoding".to_string())),
                Some(&DataTrackSchemaEncoding::Custom("my-schema-encoding".to_string()))
            ),
            Ok(())
        );
    }

    #[test]
    fn test_validate_schema_missing_frame_encoding() {
        assert_eq!(
            validate_schema(None, Some(&DataTrackSchemaEncoding::Protobuf)),
            Err(DataTrackSchemaError::MissingFrameEncoding)
        );
    }

    #[test]
    fn test_validate_schema_missing_schema_id() {
        assert_eq!(
            validate_schema(Some(&DataTrackFrameEncoding::Protobuf), None),
            Err(DataTrackSchemaError::MissingSchemaId)
        );
    }

    #[test]
    fn test_validate_schema_incompatible() {
        assert_eq!(
            validate_schema(
                Some(&DataTrackFrameEncoding::Json),
                Some(&DataTrackSchemaEncoding::Protobuf)
            ),
            Err(DataTrackSchemaError::Incompatible)
        );
    }
}