Skip to main content

gosuto_livekit/
proto.rs

1// Copyright 2025 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
15use livekit_protocol::*;
16
17use crate::{
18    e2ee::EncryptionType, participant, room::ChatMessage as RoomChatMessage, track, DataPacketKind,
19};
20
21// Conversions
22impl From<ConnectionQuality> for participant::ConnectionQuality {
23    fn from(value: ConnectionQuality) -> Self {
24        match value {
25            ConnectionQuality::Excellent => Self::Excellent,
26            ConnectionQuality::Good => Self::Good,
27            ConnectionQuality::Poor => Self::Poor,
28            ConnectionQuality::Lost => Self::Lost,
29        }
30    }
31}
32
33impl From<DisconnectReason> for participant::DisconnectReason {
34    fn from(value: DisconnectReason) -> Self {
35        match value {
36            DisconnectReason::UnknownReason => Self::UnknownReason,
37            DisconnectReason::ClientInitiated => Self::ClientInitiated,
38            DisconnectReason::DuplicateIdentity => Self::DuplicateIdentity,
39            DisconnectReason::ServerShutdown => Self::ServerShutdown,
40            DisconnectReason::ParticipantRemoved => Self::ParticipantRemoved,
41            DisconnectReason::RoomDeleted => Self::RoomDeleted,
42            DisconnectReason::StateMismatch => Self::StateMismatch,
43            DisconnectReason::JoinFailure => Self::JoinFailure,
44            DisconnectReason::Migration => Self::Migration,
45            DisconnectReason::SignalClose => Self::SignalClose,
46            DisconnectReason::RoomClosed => Self::RoomClosed,
47            DisconnectReason::UserUnavailable => Self::UserUnavailable,
48            DisconnectReason::UserRejected => Self::UserRejected,
49            DisconnectReason::SipTrunkFailure => Self::SipTrunkFailure,
50            DisconnectReason::ConnectionTimeout => Self::ConnectionTimeout,
51            DisconnectReason::MediaFailure => Self::MediaFailure,
52        }
53    }
54}
55
56impl TryFrom<TrackType> for track::TrackKind {
57    type Error = &'static str;
58
59    fn try_from(r#type: TrackType) -> Result<Self, Self::Error> {
60        match r#type {
61            TrackType::Audio => Ok(Self::Audio),
62            TrackType::Video => Ok(Self::Video),
63            TrackType::Data => Err("data tracks are not implemented yet"),
64        }
65    }
66}
67
68impl From<track::TrackKind> for TrackType {
69    fn from(kind: track::TrackKind) -> Self {
70        match kind {
71            track::TrackKind::Audio => Self::Audio,
72            track::TrackKind::Video => Self::Video,
73        }
74    }
75}
76
77impl From<TrackSource> for track::TrackSource {
78    fn from(source: TrackSource) -> Self {
79        match source {
80            TrackSource::Camera => Self::Camera,
81            TrackSource::Microphone => Self::Microphone,
82            TrackSource::ScreenShare => Self::Screenshare,
83            TrackSource::ScreenShareAudio => Self::ScreenshareAudio,
84            TrackSource::Unknown => Self::Unknown,
85        }
86    }
87}
88
89impl From<track::TrackSource> for TrackSource {
90    fn from(source: track::TrackSource) -> Self {
91        match source {
92            track::TrackSource::Camera => Self::Camera,
93            track::TrackSource::Microphone => Self::Microphone,
94            track::TrackSource::Screenshare => Self::ScreenShare,
95            track::TrackSource::ScreenshareAudio => Self::ScreenShareAudio,
96            track::TrackSource::Unknown => Self::Unknown,
97        }
98    }
99}
100
101impl From<DataPacketKind> for data_packet::Kind {
102    fn from(kind: DataPacketKind) -> Self {
103        match kind {
104            DataPacketKind::Lossy => Self::Lossy,
105            DataPacketKind::Reliable => Self::Reliable,
106        }
107    }
108}
109
110impl From<data_packet::Kind> for DataPacketKind {
111    fn from(kind: data_packet::Kind) -> Self {
112        match kind {
113            data_packet::Kind::Lossy => Self::Lossy,
114            data_packet::Kind::Reliable => Self::Reliable,
115        }
116    }
117}
118
119impl From<encryption::Type> for EncryptionType {
120    fn from(value: livekit_protocol::encryption::Type) -> Self {
121        match value {
122            livekit_protocol::encryption::Type::None => Self::None,
123            livekit_protocol::encryption::Type::Gcm => Self::Gcm,
124            livekit_protocol::encryption::Type::Custom => Self::Custom,
125        }
126    }
127}
128
129impl From<EncryptionType> for encryption::Type {
130    fn from(value: EncryptionType) -> Self {
131        match value {
132            EncryptionType::None => Self::None,
133            EncryptionType::Gcm => Self::Gcm,
134            EncryptionType::Custom => Self::Custom,
135        }
136    }
137}
138
139impl From<EncryptionType> for i32 {
140    fn from(value: EncryptionType) -> Self {
141        match value {
142            EncryptionType::None => 0,
143            EncryptionType::Gcm => 1,
144            EncryptionType::Custom => 2,
145        }
146    }
147}
148
149impl From<participant_info::Kind> for participant::ParticipantKind {
150    fn from(value: participant_info::Kind) -> Self {
151        match value {
152            participant_info::Kind::Standard => participant::ParticipantKind::Standard,
153            participant_info::Kind::Ingress => participant::ParticipantKind::Ingress,
154            participant_info::Kind::Egress => participant::ParticipantKind::Egress,
155            participant_info::Kind::Sip => participant::ParticipantKind::Sip,
156            participant_info::Kind::Agent => participant::ParticipantKind::Agent,
157            participant_info::Kind::Connector => participant::ParticipantKind::Connector,
158            participant_info::Kind::Bridge => participant::ParticipantKind::Bridge,
159        }
160    }
161}
162
163impl From<participant_info::KindDetail> for participant::ParticipantKindDetail {
164    fn from(value: participant_info::KindDetail) -> Self {
165        match value {
166            participant_info::KindDetail::CloudAgent => {
167                participant::ParticipantKindDetail::CloudAgent
168            }
169            participant_info::KindDetail::Forwarded => {
170                participant::ParticipantKindDetail::Forwarded
171            }
172            participant_info::KindDetail::ConnectorWhatsapp => {
173                participant::ParticipantKindDetail::ConnectorWhatsapp
174            }
175            participant_info::KindDetail::ConnectorTwilio => {
176                participant::ParticipantKindDetail::ConnectorTwilio
177            }
178            participant_info::KindDetail::BridgeRtsp => {
179                participant::ParticipantKindDetail::BridgeRtsp
180            }
181        }
182    }
183}
184
185impl From<ChatMessage> for RoomChatMessage {
186    fn from(proto_msg: ChatMessage) -> Self {
187        RoomChatMessage {
188            id: proto_msg.id,
189            message: proto_msg.message,
190            timestamp: proto_msg.timestamp,
191            edit_timestamp: proto_msg.edit_timestamp,
192            deleted: proto_msg.deleted.into(),
193            generated: proto_msg.generated.into(),
194        }
195    }
196}
197
198impl From<RoomChatMessage> for ChatMessage {
199    fn from(msg: RoomChatMessage) -> Self {
200        ChatMessage {
201            id: msg.id,
202            message: msg.message,
203            timestamp: msg.timestamp,
204            edit_timestamp: msg.edit_timestamp,
205            deleted: msg.deleted.unwrap_or(false),
206            generated: msg.generated.unwrap_or(false),
207        }
208    }
209}
210
211impl From<participant::ParticipantTrackPermission> for TrackPermission {
212    fn from(perm: participant::ParticipantTrackPermission) -> Self {
213        TrackPermission {
214            participant_identity: perm.participant_identity.to_string(),
215            participant_sid: String::new(),
216            all_tracks: perm.allow_all,
217            track_sids: perm.allowed_track_sids.iter().map(|sid| sid.to_string()).collect(),
218        }
219    }
220}
221
222impl From<TrackPermission> for participant::ParticipantTrackPermission {
223    fn from(perm: TrackPermission) -> Self {
224        participant::ParticipantTrackPermission {
225            participant_identity: perm.participant_identity.into(),
226            allow_all: perm.all_tracks,
227            allowed_track_sids: perm
228                .track_sids
229                .into_iter()
230                .map(|sid| sid.try_into().unwrap())
231                .collect(),
232        }
233    }
234}