Skip to main content

livekit_common/
lib.rs

1// Copyright 2026 LiveKit, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Foundational types shared across LiveKit crates: participant identities, the
16//! encryption/capability enums, client-protocol constants, and the remote-participant
17//! registry trait consulted by the data-stream and RPC send paths.
18
19use std::fmt::Display;
20
21use livekit_protocol as proto;
22
23mod enum_dispatch;
24
25// -------------------------------------------------------------------------------------------------
26// Client protocol
27// -------------------------------------------------------------------------------------------------
28
29/// Legacy client.
30pub const CLIENT_PROTOCOL_DEFAULT: i32 = 0;
31
32/// RPC v2 (see RPC spec).
33pub const CLIENT_PROTOCOL_DATA_STREAM_RPC: i32 = 1;
34
35/// Understands inline single-packet data streams (data streams v2).
36pub const CLIENT_PROTOCOL_DATA_STREAM_V2: i32 = 2;
37
38// -------------------------------------------------------------------------------------------------
39// ParticipantIdentity
40// -------------------------------------------------------------------------------------------------
41
42#[derive(Clone, Default, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
43pub struct ParticipantIdentity(pub String);
44
45impl From<String> for ParticipantIdentity {
46    fn from(value: String) -> Self {
47        Self(value)
48    }
49}
50
51impl From<&str> for ParticipantIdentity {
52    fn from(value: &str) -> Self {
53        Self(value.to_string())
54    }
55}
56
57impl From<ParticipantIdentity> for String {
58    fn from(value: ParticipantIdentity) -> Self {
59        value.0
60    }
61}
62
63impl Display for ParticipantIdentity {
64    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65        write!(f, "{}", self.0)
66    }
67}
68
69impl ParticipantIdentity {
70    pub fn as_str(&self) -> &str {
71        &self.0
72    }
73}
74
75// -------------------------------------------------------------------------------------------------
76// EncryptionType
77// -------------------------------------------------------------------------------------------------
78
79#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
80pub enum EncryptionType {
81    #[default]
82    None,
83    Gcm,
84    Custom,
85}
86
87impl From<proto::encryption::Type> for EncryptionType {
88    fn from(value: proto::encryption::Type) -> Self {
89        match value {
90            proto::encryption::Type::None => Self::None,
91            proto::encryption::Type::Gcm => Self::Gcm,
92            proto::encryption::Type::Custom => Self::Custom,
93        }
94    }
95}
96
97impl From<EncryptionType> for proto::encryption::Type {
98    fn from(value: EncryptionType) -> Self {
99        match value {
100            EncryptionType::None => Self::None,
101            EncryptionType::Gcm => Self::Gcm,
102            EncryptionType::Custom => Self::Custom,
103        }
104    }
105}
106
107impl From<EncryptionType> for i32 {
108    fn from(value: EncryptionType) -> Self {
109        match value {
110            EncryptionType::None => 0,
111            EncryptionType::Gcm => 1,
112            EncryptionType::Custom => 2,
113        }
114    }
115}
116
117// -------------------------------------------------------------------------------------------------
118// ClientCapability
119// -------------------------------------------------------------------------------------------------
120
121/// A capability a participant's client advertises, mirroring the `ClientInfo.Capability` protobuf
122/// enum.
123#[derive(Debug, Clone, Copy, Eq, PartialEq)]
124#[non_exhaustive]
125pub enum ClientCapability {
126    Unused,
127    PacketTrailer,
128    CompressionDeflateRaw,
129}
130
131impl TryFrom<i32> for ClientCapability {
132    type Error = &'static str;
133
134    fn try_from(value: i32) -> Result<Self, Self::Error> {
135        match proto::client_info::Capability::try_from(value) {
136            Ok(proto::client_info::Capability::CapPacketTrailer) => Ok(Self::PacketTrailer),
137            Ok(proto::client_info::Capability::CapCompressionDeflateRaw) => {
138                Ok(Self::CompressionDeflateRaw)
139            }
140            Ok(proto::client_info::Capability::CapUnused) => Ok(Self::Unused),
141            Err(_) => Err("unknown client capability"),
142        }
143    }
144}
145
146impl From<ClientCapability> for i32 {
147    fn from(value: ClientCapability) -> Self {
148        match value {
149            ClientCapability::Unused => proto::client_info::Capability::CapUnused as i32,
150            ClientCapability::PacketTrailer => {
151                proto::client_info::Capability::CapPacketTrailer as i32
152            }
153            ClientCapability::CompressionDeflateRaw => {
154                proto::client_info::Capability::CapCompressionDeflateRaw as i32
155            }
156        }
157    }
158}
159
160// -------------------------------------------------------------------------------------------------
161// RemoteParticipantRegistry
162// -------------------------------------------------------------------------------------------------
163
164/// Read access to remote participants' advertised protocol and capabilities.
165///
166/// Used by downstream modules like the the RPC transport (v1/v2 transport selection) and
167/// the data-stream send path (inline / compression eligibility) to determine what level of support
168/// a participant has for protocol level features.
169pub trait RemoteParticipantRegistry: Send + Sync {
170    /// A remote participant's `client_protocol`, or `CLIENT_PROTOCOL_DEFAULT` (0) if unknown.
171    fn remote_client_protocol(&self, identity: &ParticipantIdentity) -> i32;
172
173    /// A remote participant's advertised capabilities, or empty if unknown.
174    fn remote_capabilities(&self, identity: &ParticipantIdentity) -> Vec<ClientCapability>;
175
176    /// The identities of every remote participant, used to resolve a broadcast send.
177    fn remote_identities(&self) -> Vec<ParticipantIdentity>;
178}