Skip to main content

videocall_types/
lib.rs

1/*
2 * Copyright 2025 Security Union LLC
3 *
4 * Licensed under either of
5 *
6 * * Apache License, Version 2.0
7 *   (http://www.apache.org/licenses/LICENSE-2.0)
8 * * MIT license
9 *   (http://opensource.org/licenses/MIT)
10 *
11 * at your option.
12 *
13 * Unless you explicitly state otherwise, any contribution intentionally
14 * submitted for inclusion in the work by you, as defined in the Apache-2.0
15 * license, shall be dual licensed as above, without any additional terms or
16 * conditions.
17 */
18
19pub mod callback;
20pub mod feature_flags;
21pub mod protos;
22pub mod user_id;
23pub mod validation;
24
25pub use callback::Callback;
26pub use feature_flags::FeatureFlags;
27use protobuf::Message;
28pub use user_id::{is_system_user, to_user_id_bytes, user_id_bytes_to_string};
29
30/// A representation of a value which can be stored and restored as a text.
31pub type Text = Result<String, anyhow::Error>;
32
33/// A representation of a value which can be stored and restored as a binary.
34pub type Binary = Result<Vec<u8>, anyhow::Error>;
35
36/// System user ID used for server-generated messages (meeting info, meeting started/ended).
37/// This is not a real user and should be filtered out in UI/peer management.
38pub const SYSTEM_USER_ID: &str = "system-&^%$#@!";
39
40impl std::fmt::Display for protos::media_packet::media_packet::MediaType {
41    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
42        match self {
43            protos::media_packet::media_packet::MediaType::MEDIA_TYPE_UNKNOWN => {
44                write!(f, "UNKNOWN")
45            }
46            protos::media_packet::media_packet::MediaType::AUDIO => write!(f, "audio"),
47            protos::media_packet::media_packet::MediaType::VIDEO => write!(f, "video"),
48            protos::media_packet::media_packet::MediaType::SCREEN => write!(f, "screen"),
49            protos::media_packet::media_packet::MediaType::HEARTBEAT => write!(f, "heartbeat"),
50            protos::media_packet::media_packet::MediaType::RTT => write!(f, "rtt"),
51            protos::media_packet::media_packet::MediaType::KEYFRAME_REQUEST => {
52                write!(f, "keyframe_request")
53            }
54        }
55    }
56}
57
58impl std::fmt::Display for protos::packet_wrapper::packet_wrapper::PacketType {
59    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
60        match self {
61            protos::packet_wrapper::packet_wrapper::PacketType::PACKET_TYPE_UNKNOWN => {
62                write!(f, "UNKNOWN")
63            }
64            protos::packet_wrapper::packet_wrapper::PacketType::AES_KEY => write!(f, "AES_KEY"),
65            protos::packet_wrapper::packet_wrapper::PacketType::RSA_PUB_KEY => {
66                write!(f, "RSA_PUB_KEY")
67            }
68            protos::packet_wrapper::packet_wrapper::PacketType::MEDIA => write!(f, "MEDIA"),
69            protos::packet_wrapper::packet_wrapper::PacketType::CONNECTION => {
70                write!(f, "CONNECTION")
71            }
72            protos::packet_wrapper::packet_wrapper::PacketType::DIAGNOSTICS => {
73                write!(f, "DIAGNOSTICS")
74            }
75            protos::packet_wrapper::packet_wrapper::PacketType::HEALTH => {
76                write!(f, "HEALTH")
77            }
78            protos::packet_wrapper::packet_wrapper::PacketType::MEETING => {
79                write!(f, "MEETING")
80            }
81            protos::packet_wrapper::packet_wrapper::PacketType::SESSION_ASSIGNED => {
82                write!(f, "SESSION_ASSIGNED")
83            }
84            protos::packet_wrapper::packet_wrapper::PacketType::CONGESTION => {
85                write!(f, "CONGESTION")
86            }
87        }
88    }
89}
90
91impl From<Text> for protos::packet_wrapper::PacketWrapper {
92    fn from(t: Text) -> Self {
93        protos::packet_wrapper::PacketWrapper::parse_from_bytes(&t.unwrap().into_bytes()).unwrap()
94    }
95}
96
97impl From<Binary> for protos::packet_wrapper::PacketWrapper {
98    fn from(bin: Binary) -> Self {
99        protos::packet_wrapper::PacketWrapper::parse_from_bytes(&bin.unwrap()).unwrap()
100    }
101}
102
103pub fn truthy(s: Option<&str>) -> bool {
104    if let Some(s) = s {
105        ["true".to_string(), "1".to_string()].contains(&s.to_lowercase())
106    } else {
107        false
108    }
109}