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// Must sit at the crate root: it defines `crate::UniFfiTag`, which the registrations in
26// `ffi_types` resolve against.
27#[cfg(feature = "uniffi")]
28uniffi::setup_scaffolding!();
29
30#[cfg(feature = "uniffi")]
31mod ffi_types;
32
33// -------------------------------------------------------------------------------------------------
34// Client protocol
35// -------------------------------------------------------------------------------------------------
36
37/// Legacy client.
38pub const CLIENT_PROTOCOL_DEFAULT: i32 = 0;
39
40/// RPC v2 (see RPC spec).
41pub const CLIENT_PROTOCOL_DATA_STREAM_RPC: i32 = 1;
42
43/// Understands inline single-packet data streams (data streams v2).
44pub const CLIENT_PROTOCOL_DATA_STREAM_V2: i32 = 2;
45
46// -------------------------------------------------------------------------------------------------
47// ParticipantIdentity
48// -------------------------------------------------------------------------------------------------
49
50#[derive(Clone, Default, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
51pub struct ParticipantIdentity(pub String);
52
53impl From<String> for ParticipantIdentity {
54 fn from(value: String) -> Self {
55 Self(value)
56 }
57}
58
59impl From<&str> for ParticipantIdentity {
60 fn from(value: &str) -> Self {
61 Self(value.to_string())
62 }
63}
64
65impl From<ParticipantIdentity> for String {
66 fn from(value: ParticipantIdentity) -> Self {
67 value.0
68 }
69}
70
71impl Display for ParticipantIdentity {
72 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73 write!(f, "{}", self.0)
74 }
75}
76
77impl ParticipantIdentity {
78 pub fn as_str(&self) -> &str {
79 &self.0
80 }
81}
82
83// -------------------------------------------------------------------------------------------------
84// EncryptionType
85// -------------------------------------------------------------------------------------------------
86
87#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
88pub enum EncryptionType {
89 #[default]
90 None,
91 Gcm,
92 Custom,
93}
94
95impl From<proto::encryption::Type> for EncryptionType {
96 fn from(value: proto::encryption::Type) -> Self {
97 match value {
98 proto::encryption::Type::None => Self::None,
99 proto::encryption::Type::Gcm => Self::Gcm,
100 proto::encryption::Type::Custom => Self::Custom,
101 }
102 }
103}
104
105impl From<EncryptionType> for proto::encryption::Type {
106 fn from(value: EncryptionType) -> Self {
107 match value {
108 EncryptionType::None => Self::None,
109 EncryptionType::Gcm => Self::Gcm,
110 EncryptionType::Custom => Self::Custom,
111 }
112 }
113}
114
115impl From<EncryptionType> for i32 {
116 fn from(value: EncryptionType) -> Self {
117 match value {
118 EncryptionType::None => 0,
119 EncryptionType::Gcm => 1,
120 EncryptionType::Custom => 2,
121 }
122 }
123}
124
125// -------------------------------------------------------------------------------------------------
126// ClientCapability
127// -------------------------------------------------------------------------------------------------
128
129/// A capability a participant's client advertises, mirroring the `ClientInfo.Capability` protobuf
130/// enum.
131#[derive(Debug, Clone, Copy, Eq, PartialEq)]
132#[non_exhaustive]
133pub enum ClientCapability {
134 Unused,
135 PacketTrailer,
136 CompressionDeflateRaw,
137}
138
139impl TryFrom<i32> for ClientCapability {
140 type Error = &'static str;
141
142 fn try_from(value: i32) -> Result<Self, Self::Error> {
143 match proto::client_info::Capability::try_from(value) {
144 Ok(proto::client_info::Capability::CapPacketTrailer) => Ok(Self::PacketTrailer),
145 Ok(proto::client_info::Capability::CapCompressionDeflateRaw) => {
146 Ok(Self::CompressionDeflateRaw)
147 }
148 Ok(proto::client_info::Capability::CapUnused) => Ok(Self::Unused),
149 Err(_) => Err("unknown client capability"),
150 }
151 }
152}
153
154impl From<ClientCapability> for i32 {
155 fn from(value: ClientCapability) -> Self {
156 match value {
157 ClientCapability::Unused => proto::client_info::Capability::CapUnused as i32,
158 ClientCapability::PacketTrailer => {
159 proto::client_info::Capability::CapPacketTrailer as i32
160 }
161 ClientCapability::CompressionDeflateRaw => {
162 proto::client_info::Capability::CapCompressionDeflateRaw as i32
163 }
164 }
165 }
166}
167
168// -------------------------------------------------------------------------------------------------
169// RemoteParticipantRegistry
170// -------------------------------------------------------------------------------------------------
171
172/// Read access to remote participants' advertised protocol and capabilities.
173///
174/// Used by downstream modules like the the RPC transport (v1/v2 transport selection) and
175/// the data-stream send path (inline / compression eligibility) to determine what level of support
176/// a participant has for protocol level features.
177pub trait RemoteParticipantRegistry: Send + Sync {
178 /// A remote participant's `client_protocol`, or `CLIENT_PROTOCOL_DEFAULT` (0) if unknown.
179 fn remote_client_protocol(&self, identity: &ParticipantIdentity) -> i32;
180
181 /// A remote participant's advertised capabilities, or empty if unknown.
182 fn remote_capabilities(&self, identity: &ParticipantIdentity) -> Vec<ClientCapability>;
183
184 /// The identities of every remote participant, used to resolve a broadcast send.
185 fn remote_identities(&self) -> Vec<ParticipantIdentity>;
186}