Skip to main content

tilo_actions/remote/
call.rs

1//! The call lane an app's connection carries: one outside service's call, carried into a
2//! workspace's call.
3//!
4//! A bridge app — a chat service's, a mail torrent's, any service whose call is not Tilo's —
5//! holds a connection to the device and asks it to carry one call of its service into one of
6//! the workspace's voice channels. The device joins the workspace's live session as the bridge,
7//! publishes each outside person as a participant of their own, hands the app their frames and
8//! the workspace's frames back, and holds everything to the call's rule. Nothing here names a
9//! service: `service` is the app's own word for the one it reaches.
10
11use serde::{Deserialize, Serialize};
12
13/// The most bytes one Opus frame may carry: a 20 ms frame, which a bridge never re-encodes.
14pub const MAX_CALL_AUDIO_BYTES: usize = 4096;
15/// The longest a service's account or name may be, as a call's participants are shown.
16pub const MAX_CALL_TEXT_BYTES: usize = 256;
17
18/// One outside call and the workspace call it is carried into.
19#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
20pub struct BridgedCall {
21    /// The service's id of its call (a voice channel of its service).
22    pub outside: String,
23    /// The workspace's voice channel id it is one call with.
24    pub tilo: String,
25}
26
27/// Who is in one outside call, as the service says.
28#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
29pub struct OutsideCall {
30    /// The service's id of the call.
31    pub call: String,
32    /// Who is in it, the bridge's own account left out.
33    #[serde(default)]
34    pub people: Vec<OutsidePerson>,
35}
36
37/// One person in an outside call.
38#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
39pub struct OutsidePerson {
40    /// Their account on the service.
41    pub account: String,
42    /// The name the service shows for them there.
43    #[serde(default)]
44    pub name: String,
45    /// Where the service serves their picture, when it has one. A bridge that keeps it in the
46    /// workspace's content store names the hash there instead, once it does.
47    #[serde(default)]
48    pub avatar_url: Option<String>,
49    /// BLAKE3 hex of their picture, when the bridge kept it in the workspace's content store.
50    #[serde(default)]
51    pub avatar: Option<String>,
52    /// Whether their microphone is off.
53    #[serde(default)]
54    pub muted: bool,
55    /// Whether they hear nothing of the call. A service that deafens somebody mutes them for
56    /// the call as well.
57    #[serde(default)]
58    pub deafened: bool,
59}
60
61/// The bridge's own place in the outside calls.
62#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
63#[serde(tag = "link", rename_all = "kebab-case")]
64pub enum CallLink {
65    /// In no call.
66    #[default]
67    Idle,
68    /// Joining `call`.
69    Joining { call: String },
70    /// In `call`, carrying audio both ways.
71    Joined { call: String },
72    /// The service will not let the bridge into `call`, for `reason` (a missing permission names
73    /// it): said as it is, never retried until asked again.
74    Refused { call: String, reason: String },
75}
76
77/// What the bridge says of its calls: who is in each, where it stands, and the line its people
78/// see there about who is on the workspace's side.
79#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
80pub struct CallReport {
81    #[serde(default)]
82    pub calls: Vec<OutsideCall>,
83    #[serde(default)]
84    pub link: CallLink,
85    /// What the neighbourhood of its own call shows in the service's own words: who here is
86    /// listening, how many. Empty leaves the service's line as it was.
87    #[serde(default)]
88    pub status: String,
89}
90
91/// One Opus frame a person said, with the account it is theirs.
92#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
93pub struct CallAudio {
94    /// Their account on the service.
95    pub speaker: String,
96    /// The 20 ms Opus frame (48 kHz), untouched.
97    pub opus: Vec<u8>,
98    /// Frames of this speaker's stream dropped just before this one, so the receiver leaves
99    /// their place in its timeline and conceals the gap instead of joining the two sides of it.
100    #[serde(default)]
101    pub skipped: u32,
102}
103
104/// What the device says about the call it carries, for the service's own line there.
105#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
106pub struct CallStatus {
107    /// The outside call this is about, or none.
108    #[serde(default)]
109    pub call: Option<String>,
110    /// The line to show there.
111    #[serde(default)]
112    pub status: Option<String>,
113}
114
115impl CallAudio {
116    /// Whether this frame is one a bridge may carry: a speaker named, and a frame inside the
117    /// bound.
118    #[must_use]
119    pub fn valid(&self) -> bool {
120        !self.speaker.is_empty()
121            && self.speaker.len() <= MAX_CALL_TEXT_BYTES
122            && !self.opus.is_empty()
123            && self.opus.len() <= MAX_CALL_AUDIO_BYTES
124    }
125}
126
127impl CallReport {
128    /// Whether this report is one a bridge may make.
129    #[must_use]
130    pub fn valid(&self) -> bool {
131        self.calls.iter().all(|call| {
132            !call.call.is_empty()
133                && call.call.len() <= MAX_CALL_TEXT_BYTES
134                && call.people.iter().all(|person| {
135                    !person.account.is_empty()
136                        && person.account.len() <= MAX_CALL_TEXT_BYTES
137                        && !person.name.trim().is_empty()
138                        && person.name.chars().count() <= 80
139                        && person
140                            .avatar_url
141                            .as_deref()
142                            .is_none_or(|url| url.len() <= 1024 && !url.trim().is_empty())
143                        && person.avatar.as_deref().is_none_or(|hash| {
144                            hash.len() == 64 && hash.bytes().all(|b| b.is_ascii_hexdigit())
145                        })
146                })
147        })
148    }
149}
150
151impl BridgedCall {
152    /// Whether this is a pair a bridge may ask the device to carry.
153    #[must_use]
154    pub fn valid(&self) -> bool {
155        !self.outside.is_empty()
156            && self.outside.len() <= MAX_CALL_TEXT_BYTES
157            && !self.tilo.is_empty()
158            && self.tilo.len() <= MAX_CALL_TEXT_BYTES
159    }
160}
161
162#[cfg(test)]
163mod tests {
164    #![allow(
165        clippy::unwrap_used,
166        clippy::expect_used,
167        clippy::panic,
168        reason = "in a test these are the assertion"
169    )]
170
171    use super::*;
172
173    #[test]
174    fn a_pair_names_two_calls_and_nothing_else() {
175        assert!(
176            BridgedCall {
177                outside: "458".into(),
178                tilo: "voice-tl".into()
179            }
180            .valid()
181        );
182        assert!(
183            !BridgedCall {
184                outside: String::new(),
185                tilo: "voice-tl".into()
186            }
187            .valid()
188        );
189        assert!(
190            !BridgedCall {
191                outside: "x".repeat(MAX_CALL_TEXT_BYTES + 1),
192                tilo: "voice-tl".into()
193            }
194            .valid()
195        );
196    }
197
198    #[test]
199    fn a_report_names_people_and_their_place() {
200        let report = CallReport {
201            calls: vec![OutsideCall {
202                call: "458".into(),
203                people: vec![OutsidePerson {
204                    account: "7001".into(),
205                    name: "Dana".into(),
206                    avatar_url: Some("https://media.example/a.png".into()),
207                    avatar: None,
208                    muted: true,
209                    deafened: false,
210                }],
211            }],
212            link: CallLink::Joined { call: "458".into() },
213            status: String::new(),
214        };
215        assert!(report.valid());
216        let mut wrong = report.clone();
217        wrong.calls[0].people[0].avatar = Some("not a hash".into());
218        assert!(!wrong.valid(), "a kept picture is named by its BLAKE3 hex");
219        let mut wrong = report;
220        wrong.calls[0].people[0].account = String::new();
221        assert!(!wrong.valid(), "a participant is named by an account");
222    }
223
224    #[test]
225    fn a_frame_is_bounded_and_named() {
226        let frame = CallAudio {
227            speaker: "7001".into(),
228            opus: vec![1, 2, 3],
229            skipped: 0,
230        };
231        assert!(frame.valid());
232        assert!(
233            !CallAudio {
234                speaker: String::new(),
235                opus: vec![1],
236                skipped: 0,
237            }
238            .valid()
239        );
240        assert!(
241            !CallAudio {
242                speaker: "7001".into(),
243                opus: vec![0; MAX_CALL_AUDIO_BYTES + 1],
244                skipped: 0,
245            }
246            .valid()
247        );
248    }
249}