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 enum, and client-protocol constants.
17
18use std::fmt::Display;
19
20use livekit_protocol as proto;
21
22mod enum_dispatch;
23
24// -------------------------------------------------------------------------------------------------
25// Client protocol
26// -------------------------------------------------------------------------------------------------
27
28/// Legacy client.
29pub const CLIENT_PROTOCOL_DEFAULT: i32 = 0;
30
31/// RPC v2 (see RPC spec).
32pub const CLIENT_PROTOCOL_DATA_STREAM_RPC: i32 = 1;
33
34// -------------------------------------------------------------------------------------------------
35// ParticipantIdentity
36// -------------------------------------------------------------------------------------------------
37
38#[derive(Clone, Default, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
39pub struct ParticipantIdentity(pub String);
40
41impl From<String> for ParticipantIdentity {
42    fn from(value: String) -> Self {
43        Self(value)
44    }
45}
46
47impl From<&str> for ParticipantIdentity {
48    fn from(value: &str) -> Self {
49        Self(value.to_string())
50    }
51}
52
53impl From<ParticipantIdentity> for String {
54    fn from(value: ParticipantIdentity) -> Self {
55        value.0
56    }
57}
58
59impl Display for ParticipantIdentity {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        write!(f, "{}", self.0)
62    }
63}
64
65impl ParticipantIdentity {
66    pub fn as_str(&self) -> &str {
67        &self.0
68    }
69}
70
71// -------------------------------------------------------------------------------------------------
72// EncryptionType
73// -------------------------------------------------------------------------------------------------
74
75#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
76pub enum EncryptionType {
77    #[default]
78    None,
79    Gcm,
80    Custom,
81}
82
83impl From<proto::encryption::Type> for EncryptionType {
84    fn from(value: proto::encryption::Type) -> Self {
85        match value {
86            proto::encryption::Type::None => Self::None,
87            proto::encryption::Type::Gcm => Self::Gcm,
88            proto::encryption::Type::Custom => Self::Custom,
89        }
90    }
91}
92
93impl From<EncryptionType> for proto::encryption::Type {
94    fn from(value: EncryptionType) -> Self {
95        match value {
96            EncryptionType::None => Self::None,
97            EncryptionType::Gcm => Self::Gcm,
98            EncryptionType::Custom => Self::Custom,
99        }
100    }
101}
102
103impl From<EncryptionType> for i32 {
104    fn from(value: EncryptionType) -> Self {
105        match value {
106            EncryptionType::None => 0,
107            EncryptionType::Gcm => 1,
108            EncryptionType::Custom => 2,
109        }
110    }
111}