1use std::borrow::Cow;
23
24use serde::{Deserialize, Deserializer, Serialize, de};
25use serde_json::Value;
26
27pub type JsonPayload = Value;
33
34#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
47#[serde(untagged)]
48pub enum UserId {
49 Integer(i64),
50 String(#[serde(deserialize_with = "UserId::deserialize_string")] String),
51}
52
53impl From<i64> for UserId {
54 fn from(value: i64) -> Self {
55 Self::Integer(value)
56 }
57}
58
59impl From<&str> for UserId {
60 fn from(value: &str) -> Self {
61 Self::String(value.to_owned())
62 }
63}
64
65impl From<String> for UserId {
66 fn from(value: String) -> Self {
67 Self::String(value)
68 }
69}
70
71impl UserId {
72 pub const MAX_STRING_BYTES: usize = 256;
74
75 fn deserialize_string<'de, D>(deserializer: D) -> Result<String, D::Error>
76 where
77 D: Deserializer<'de>,
78 {
79 let value = String::deserialize(deserializer)?;
80 if value.len() > Self::MAX_STRING_BYTES {
82 return Err(de::Error::custom("user identity exceeds 256 UTF-8 bytes"));
83 }
84 Ok(value)
85 }
86
87 #[must_use]
90 pub fn path_segment(&self) -> Cow<'_, str> {
91 match self {
92 Self::Integer(value) => Cow::Owned(value.to_string()),
93 Self::String(value) => Cow::Borrowed(value),
94 }
95 }
96
97 #[must_use]
104 pub fn normalized_for_runtime(self) -> Self {
105 match self {
106 Self::String(value) => value
107 .parse::<i64>()
108 .map_or(Self::String(value), Self::Integer),
109 Self::Integer(value) => Self::Integer(value),
110 }
111 }
112
113 #[must_use]
118 pub fn runtime_normalized(&self) -> Self {
119 self.clone().normalized_for_runtime()
120 }
121}
122
123#[expect(
130 clippy::struct_excessive_bools,
131 reason = "feature flags mirror the compatibility startup surface with explicit optional room capabilities"
132)]
133#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
134#[serde(rename_all = "camelCase")]
135pub struct AvailableFeatures {
136 pub rtc: bool,
138 pub transcription: bool,
139 pub audio_recording: bool,
140 pub video_recording: bool,
141}
142
143#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
150#[serde(rename_all = "camelCase")]
151pub struct RecordingState {
152 #[serde(skip_serializing_if = "Option::is_none")]
153 pub recording: Option<bool>,
154 #[serde(skip_serializing_if = "Option::is_none")]
155 pub audio: Option<bool>,
156 #[serde(skip_serializing_if = "Option::is_none")]
157 pub transcription: Option<bool>,
158 #[serde(skip_serializing_if = "Option::is_none")]
159 pub video: Option<bool>,
160}
161
162#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
168pub enum StopCode {
169 #[serde(rename = "user_request")]
170 UserRequest,
171 #[serde(rename = "channel_closed")]
172 ChannelClosed,
173 #[serde(rename = "recording_timeout")]
174 RecordingTimeout,
175 #[serde(rename = "recording_failed")]
176 RecordingFailed,
177 #[serde(rename = "disk_space_exhausted")]
178 DiskSpaceExhausted,
179}
180
181#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
186pub struct RecordingStateUpdate {
187 pub state: RecordingState,
188 #[serde(rename = "stopCode", skip_serializing_if = "Option::is_none")]
190 pub stop_code: Option<StopCode>,
191}
192
193#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
198#[serde(rename_all = "camelCase")]
199pub struct UserPermissions {
200 #[serde(skip_serializing_if = "Option::is_none")]
201 pub transcription: Option<bool>,
202 #[serde(skip_serializing_if = "Option::is_none")]
203 pub audio_recording: Option<bool>,
204 #[serde(skip_serializing_if = "Option::is_none")]
205 pub video_recording: Option<bool>,
206}
207
208#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
216#[serde(rename_all = "camelCase")]
217pub struct UserInfo {
218 #[serde(skip_serializing_if = "Option::is_none")]
219 pub is_talking: Option<bool>,
220 #[serde(skip_serializing_if = "Option::is_none")]
221 pub is_featured: Option<bool>,
222 #[serde(skip_serializing_if = "Option::is_none")]
223 pub is_camera_on: Option<bool>,
224 #[serde(skip_serializing_if = "Option::is_none")]
225 pub is_screen_sharing_on: Option<bool>,
226 #[serde(skip_serializing_if = "Option::is_none")]
227 pub is_self_muted: Option<bool>,
228 #[serde(skip_serializing_if = "Option::is_none")]
229 pub is_deaf: Option<bool>,
230 #[serde(skip_serializing_if = "Option::is_none")]
231 pub is_raising_hand: Option<bool>,
232}
233
234impl UserInfo {
235 #[must_use]
241 pub fn snapshot_complete(self) -> Self {
242 Self {
243 is_talking: Some(self.is_talking.unwrap_or(false)),
244 is_featured: Some(self.is_featured.unwrap_or(false)),
245 is_camera_on: Some(self.is_camera_on.unwrap_or(false)),
246 is_screen_sharing_on: Some(self.is_screen_sharing_on.unwrap_or(false)),
247 is_self_muted: Some(self.is_self_muted.unwrap_or(false)),
248 is_deaf: Some(self.is_deaf.unwrap_or(false)),
249 is_raising_hand: Some(self.is_raising_hand.unwrap_or(false)),
250 }
251 }
252
253 pub fn apply_partial_update(&mut self, update: &Self) {
258 *self = Self {
259 is_talking: update.is_talking.or(self.is_talking),
260 is_featured: update.is_featured.or(self.is_featured),
261 is_camera_on: update.is_camera_on.or(self.is_camera_on),
262 is_screen_sharing_on: update.is_screen_sharing_on.or(self.is_screen_sharing_on),
263 is_self_muted: update.is_self_muted.or(self.is_self_muted),
264 is_deaf: update.is_deaf.or(self.is_deaf),
265 is_raising_hand: update.is_raising_hand.or(self.is_raising_hand),
266 };
267 }
268
269 #[must_use]
271 pub fn with_featured(mut self, is_featured: Option<bool>) -> Self {
272 self.is_featured = is_featured;
273 self
274 }
275}
276
277#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
283pub struct PeerSnapshot {
284 #[serde(rename = "sessionId")]
285 pub user_id: UserId,
286 #[serde(default)]
287 pub info: UserInfo,
288}
289
290#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
297pub struct DownloadStates {
298 #[serde(skip_serializing_if = "Option::is_none")]
299 pub audio: Option<bool>,
300 #[serde(skip_serializing_if = "Option::is_none")]
301 pub camera: Option<bool>,
302 #[serde(skip_serializing_if = "Option::is_none")]
303 pub screen: Option<bool>,
304 #[serde(rename = "cameraLayout", skip_serializing_if = "Option::is_none")]
305 pub camera_layout: Option<VideoLayoutIntent>,
306 #[serde(rename = "screenLayout", skip_serializing_if = "Option::is_none")]
307 pub screen_layout: Option<VideoLayoutIntent>,
308}
309
310impl DownloadStates {
311 pub fn apply_partial_update(&mut self, update: &Self) {
312 *self = Self {
313 audio: update.audio.or(self.audio),
314 camera: update.camera.or(self.camera),
315 screen: update.screen.or(self.screen),
316 camera_layout: update.camera_layout.or(self.camera_layout),
317 screen_layout: update.screen_layout.or(self.screen_layout),
318 };
319 }
320}
321
322#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
328#[serde(rename_all = "snake_case")]
329pub enum VideoLayoutIntent {
330 Default,
335 Featured,
337 Pinned,
339 VisibleThumbnail,
341 Hidden,
343 Overflow,
348}
349
350#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
356pub enum StreamType {
357 #[serde(rename = "audio")]
358 Audio,
359 #[serde(rename = "camera")]
360 Camera,
361 #[serde(rename = "screen")]
362 Screen,
363}
364
365#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
372pub struct RecordingOptions {
373 #[serde(skip_serializing_if = "Option::is_none")]
374 pub audio: Option<bool>,
375 #[serde(skip_serializing_if = "Option::is_none")]
376 pub transcription: Option<bool>,
377 #[serde(skip_serializing_if = "Option::is_none")]
378 pub video: Option<bool>,
379}
380
381#[derive(Debug, Clone, Copy, PartialEq, Eq)]
387#[repr(u16)]
388pub enum WebSocketCloseCode {
389 Clean = 1000,
391 Leaving = 1001,
393 ProtocolError = 1002,
395 Error = 1011,
397
398 AuthFailed = 4106,
400 AuthTimeout = 4107,
402 Kicked = 4108,
404 RoomFull = 4109,
406 Overloaded = 4110,
408}
409
410impl WebSocketCloseCode {
411 #[must_use]
417 pub const fn from_u16(value: u16) -> Option<Self> {
418 match value {
419 1000 => Some(Self::Clean),
420 1001 => Some(Self::Leaving),
421 1002 => Some(Self::ProtocolError),
422 1011 => Some(Self::Error),
423 4106 => Some(Self::AuthFailed),
424 4107 => Some(Self::AuthTimeout),
425 4108 => Some(Self::Kicked),
426 4109 => Some(Self::RoomFull),
427 4110 => Some(Self::Overloaded),
428 _ => None,
429 }
430 }
431}
432
433impl From<WebSocketCloseCode> for u16 {
434 fn from(value: WebSocketCloseCode) -> Self {
435 match value {
436 WebSocketCloseCode::Clean => 1000,
437 WebSocketCloseCode::Leaving => 1001,
438 WebSocketCloseCode::ProtocolError => 1002,
439 WebSocketCloseCode::Error => 1011,
440 WebSocketCloseCode::AuthFailed => 4106,
441 WebSocketCloseCode::AuthTimeout => 4107,
442 WebSocketCloseCode::Kicked => 4108,
443 WebSocketCloseCode::RoomFull => 4109,
444 WebSocketCloseCode::Overloaded => 4110,
445 }
446 }
447}
448
449#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
454pub struct Bitrate(u64);
455
456impl Bitrate {
457 #[must_use]
458 pub const fn from_bps(value: u64) -> Self {
459 Self(value)
460 }
461
462 #[must_use]
463 pub const fn from_kbps(value: u64) -> Self {
464 Self(value.saturating_mul(1_000))
465 }
466
467 #[must_use]
468 pub const fn from_mbps(value: u64) -> Self {
469 Self(value.saturating_mul(1_000_000))
470 }
471
472 #[must_use]
473 pub const fn zero() -> Self {
474 Self(0)
475 }
476
477 #[must_use]
478 pub const fn as_bps(self) -> u64 {
479 self.0
480 }
481
482 #[must_use]
483 pub const fn saturating_add(self, other: Self) -> Self {
484 Self(self.0.saturating_add(other.0))
485 }
486
487 #[must_use]
488 pub const fn saturating_sub(self, other: Self) -> Self {
489 Self(self.0.saturating_sub(other.0))
490 }
491
492 #[must_use]
496 pub const fn divided_by(self, divisor: u64) -> Self {
497 match self.0.checked_div(divisor) {
498 Some(value) => Self(value),
499 None => Self::zero(),
500 }
501 }
502}
503
504#[cfg(test)]
505#[path = "TESTS/lib.rs"]
506mod tests;