Skip to main content

zaun_community_protocol/command/object/
voice.rs

1//! Voice objects (specification ยง8.12, "Voice channels"): participants, ICE servers and the
2//! host's ICE policy.
3//!
4//! The host never touches media. It authorises who may be in a voice channel, relays the
5//! WebRTC signalling and the members' media keys, and tells clients which ICE servers to use.
6//! Key packages and wrapped keys are opaque bytes to the host.
7
8use std::fmt;
9
10use serde::{Deserialize, Serialize};
11
12use crate::ids::{MemberId, VoiceSessionId};
13use crate::keys::PublicKey;
14
15/// Opaque bytes, base64url on the wire (key packages, wrapped keys, signalling data).
16#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
17pub struct Opaque(#[serde(with = "crate::b64::bytes")] pub Vec<u8>);
18
19impl Opaque {
20    /// The bytes.
21    #[must_use]
22    pub fn as_bytes(&self) -> &[u8] {
23        &self.0
24    }
25
26    /// Their length.
27    #[must_use]
28    pub fn len(&self) -> usize {
29        self.0.len()
30    }
31
32    /// Whether there are none.
33    #[must_use]
34    pub fn is_empty(&self) -> bool {
35        self.0.is_empty()
36    }
37}
38
39impl fmt::Debug for Opaque {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        write!(f, "Opaque({} bytes)", self.0.len())
42    }
43}
44
45/// One member in a voice channel.
46#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
47pub struct VoiceParticipant {
48    /// The member.
49    pub member_id: MemberId,
50    /// This stay in the channel; a rejoin gets a new one.
51    pub voice_session: VoiceSessionId,
52    /// The member's key package for this stay (opaque to the host). Only participants of the
53    /// same channel receive it; other viewers see the participant without it.
54    #[serde(default, skip_serializing_if = "Option::is_none")]
55    pub key_package: Option<Opaque>,
56    /// The member's public key, sent with `key_package`: the key that signed it (ADR-0018).
57    /// Participants verify the key package and the member's sender keys under it.
58    #[serde(default, skip_serializing_if = "Option::is_none")]
59    pub member_public_key: Option<PublicKey>,
60    /// Muted by the member.
61    pub self_mute: bool,
62    /// Deafened by the member.
63    pub self_deaf: bool,
64    /// Muted by a moderator (`MUTE_MEMBERS`); kept across stays.
65    pub server_mute: bool,
66    /// Deafened by a moderator (`DEAFEN_MEMBERS`); kept across stays.
67    pub server_deaf: bool,
68    /// Whether the member holds `SPEAK` in the channel. Clients do not play the audio of a
69    /// participant who may not speak or is server-muted.
70    pub can_speak: bool,
71}
72
73/// One ICE server, in the shape of WebRTC's `RTCIceServer`.
74#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
75pub struct IceServer {
76    /// `stun:`, `turn:` or `turns:` URLs.
77    pub urls: Vec<String>,
78    /// TURN username, from the operator's credentials file.
79    #[serde(default, skip_serializing_if = "Option::is_none")]
80    pub username: Option<String>,
81    /// TURN credential, from the operator's credentials file.
82    #[serde(default, skip_serializing_if = "Option::is_none")]
83    pub credential: Option<String>,
84}
85
86impl fmt::Debug for IceServer {
87    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88        f.debug_struct("IceServer")
89            .field("urls", &self.urls)
90            .field("username", &self.username.as_ref().map(|_| "<set>"))
91            .field(
92                "credential",
93                &self.credential.as_ref().map(|_| "<redacted>"),
94            )
95            .finish()
96    }
97}
98
99/// Which ICE candidates clients may use.
100///
101/// Peer-to-peer media shows each participant's IP address to the others. Only `relay` hides
102/// it: every packet goes through a TURN server, which sees addresses but not media.
103#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
104#[serde(rename_all = "snake_case")]
105pub enum IcePolicy {
106    /// TURN relays only (`iceTransportPolicy: "relay"`): no participant learns another's
107    /// address.
108    Relay,
109    /// Relays and direct paths: direct paths reveal addresses to other participants.
110    All,
111    /// The host has no TURN server: media can only go peer to peer, which reveals each
112    /// participant's IP address to the others. Clients must ask the user before joining.
113    Direct,
114}