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