//! The call room's frames, exactly as the Worker's `CallRoom` speaks them //! (the calling `protocol.md`, "The socket"), and the answers of the //! object's call routes. JSON text, every frame tagged by `t`. use serde::{Deserialize, Serialize}; /// What a signal carries: an SDP offer or answer, one ICE candidate, or an /// ICE-restart offer. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum SignalKind { Offer, Answer, Ice, Restart, } /// The ICE connection as this device sees it. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum MediaState { Connected, Disconnected, Failed, } /// The phase the room says a participant's call is in. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum RoomState { RingingOut, RingingIn, Connecting, Live, Reconnecting, } /// Why a call ended, said from the side it is said to. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum EndReason { /// This side hung up a call that was connecting or live. HungUp, /// The other side hung up. RemoteHungUp, /// The callee declined (on the caller: they declined; on the callee: I did). Declined, /// Another of the callee's devices declined. DeclinedElsewhere, /// The callee was in another call. Busy, /// Nobody answered in time (the caller's word). NoAnswer, /// The caller hung up before anyone answered. Cancelled, /// The ring ended unanswered (the callee's word). Missed, /// Another of the callee's devices answered. AnsweredElsewhere, /// The media never connected. Failed, /// The media dropped and did not come back. Lost, /// This device placed too many calls in the last hour. TooManyCalls, /// The object could not be reached to place the call. UnreachableNetwork, } impl EndReason { pub fn as_str(self) -> &'static str { match self { EndReason::HungUp => "hung_up", EndReason::RemoteHungUp => "remote_hung_up", EndReason::Declined => "declined", EndReason::DeclinedElsewhere => "declined_elsewhere", EndReason::Busy => "busy", EndReason::NoAnswer => "no_answer", EndReason::Cancelled => "cancelled", EndReason::Missed => "missed", EndReason::AnsweredElsewhere => "answered_elsewhere", EndReason::Failed => "failed", EndReason::Lost => "lost", EndReason::TooManyCalls => "too_many_calls", EndReason::UnreachableNetwork => "unreachable_network", } } pub fn parse(text: &str) -> Option { serde_json::from_value(serde_json::Value::String(text.to_string())).ok() } } /// How a room carries its media: peer to peer (two members), or through the /// SFU (a call that rang several people, or gained a third). A room that moved /// to the SFU never moves back. #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum Mode { #[default] P2p, Sfu, } /// Where a member of a room stands. `invited` covers rung, unknown, blocked /// and offline alike: an invitee nobody can reach looks like one who did not /// answer. `ringing` is never shown to the others. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum MemberState { Invited, Ringing, Joined, Left, Declined, NoAnswer, Busy, } /// One member of a room, as the room's roster says them. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Member { pub id: String, #[serde(default)] pub number: Option, pub state: MemberState, #[serde(default)] pub audio: bool, #[serde(default)] pub video: bool, #[serde(default)] pub speaking: bool, /// Taking notes (CONTRACT.md "Take notes"): every phone shows the mark. #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub notes: bool, /// What the member's phone can show (`notes`: the Take notes mark). #[serde(default, skip_serializing_if = "Vec::is_empty")] pub can: Vec, /// Who the number is in this copy (the core's, never the room's). #[serde(default, skip_serializing_if = "Option::is_none")] pub person: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub name: Option, } /// A media track on the SFU: pushed with its transceiver's `mid`, pulled by its member and name. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Track { #[serde(default, skip_serializing_if = "Option::is_none")] pub mid: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub member: Option, pub name: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub kind: Option, } /// An SFU operation: the client's push, pull, renegotiate and close; the room's answer and offer. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum SfuOp { Push, Pull, Renegotiate, Close, Answer, Offer, } /// A frame this device sends the room. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(tag = "t", rename_all = "snake_case")] pub enum ClientFrame { Signal { kind: SignalKind, #[serde(default, skip_serializing_if = "Option::is_none")] sdp: Option, #[serde(default, skip_serializing_if = "Option::is_none")] candidate: Option, #[serde(default, skip_serializing_if = "Option::is_none")] mid: Option, #[serde(default, skip_serializing_if = "Option::is_none")] mline: Option, }, Accept, Decline, Busy, Media { state: MediaState }, End, Ping, /// Leave a group call; the others go on. Leave, Mute { audio: bool, video: bool }, /// This device's own voice activity, on a change only. Speaking { on: bool }, Sfu { op: SfuOp, #[serde(default, skip_serializing_if = "Option::is_none")] sdp: Option, #[serde(default, skip_serializing_if = "Option::is_none")] tracks: Option>, #[serde(default, skip_serializing_if = "Option::is_none")] mids: Option>, }, /// Opaque to the room: the hook for frame encryption, relayed member to member. E2ee { #[serde(default, skip_serializing_if = "Option::is_none")] to: Option, epoch: u64, data: String, }, Hello { #[serde(default)] e2ee: Vec, }, /// This device starts or stops taking notes (Take notes); everyone in the call is told. Notes { on: bool }, } /// A frame the room sends this device. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(tag = "t", rename_all = "snake_case")] pub enum RoomFrame { State { state: RoomState, #[serde(default, skip_serializing_if = "Option::is_none")] at: Option, }, Signal { #[serde(default)] from: String, kind: SignalKind, #[serde(default, skip_serializing_if = "Option::is_none")] sdp: Option, #[serde(default, skip_serializing_if = "Option::is_none")] candidate: Option, #[serde(default, skip_serializing_if = "Option::is_none")] mid: Option, #[serde(default, skip_serializing_if = "Option::is_none")] mline: Option, }, Ended { reason: EndReason }, Pong, /// The room could not do what this device asked (`sfu_unavailable`, `sfu_failed`): which operation, and the SFU's /// words. Taken and let go: the shell logs it; the call goes on. Error { code: String, #[serde(default, skip_serializing_if = "Option::is_none")] op: Option, #[serde(default, skip_serializing_if = "Option::is_none")] detail: Option, }, Mode { mode: Mode }, Roster { members: Vec }, Member { member: Member }, Tracks { member: String, tracks: Vec }, Speaking { member: String, on: bool }, Sfu { op: SfuOp, #[serde(default, skip_serializing_if = "Option::is_none")] sdp: Option, /// An offer's pulled tracks, each by its `mid` and whose it is (`member`, `name`). #[serde(default, skip_serializing_if = "Vec::is_empty")] tracks: Vec, }, /// Take notes, said to the note-taker alone: a start `refused` (`cannot_show`, by `members` who cannot show the /// mark), or notes `stopped` (`cannot_show`) because `member` joined unable to. Who takes notes is the members' /// own `notes`, on the roster and member frames. Notes { #[serde(default, skip_serializing_if = "Option::is_none")] refused: Option, #[serde(default, skip_serializing_if = "Option::is_none")] stopped: Option, #[serde(default, skip_serializing_if = "Option::is_none")] member: Option, #[serde(default, skip_serializing_if = "Vec::is_empty")] members: Vec, }, E2ee { #[serde(default)] from: String, epoch: u64, data: String, }, Hello { #[serde(default)] from: String, #[serde(default)] e2ee: Vec, }, } /// One ICE server as WebRTC takes it. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct IceServer { /// One URL or several, as the provider answered. pub urls: serde_json::Value, #[serde(default, skip_serializing_if = "Option::is_none")] pub username: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub credential: Option, } /// `POST /v1/calls`'s answer. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Started { pub room: String, pub credential: String, #[serde(default)] pub ice_servers: Vec, #[serde(default)] pub ttl: u64, #[serde(default)] pub ring_for: u64, #[serde(default)] pub mode: Mode, /// This device's member id in the room (`caller`, `callee:`). #[serde(default)] pub me: Option, /// The Cotton group the call was placed from (its random id). #[serde(default, skip_serializing_if = "Option::is_none")] pub group: Option, } /// `POST /v1/calls//join`'s answer, and `enter`'s. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Joined { pub room: String, pub from: String, #[serde(default)] pub video: bool, pub credential: String, #[serde(default)] pub ice_servers: Vec, #[serde(default)] pub ttl: u64, #[serde(default)] pub mode: Mode, /// This device's member id in the room (`caller`, `callee:`). #[serde(default)] pub me: Option, /// The Cotton group the call was placed from (its random id). #[serde(default, skip_serializing_if = "Option::is_none")] pub group: Option, } /// `GET /v1/calls//status`'s answer: whether a call this person is a member of goes on. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct CallStatus { pub live: bool, #[serde(default)] pub video: bool, #[serde(default)] pub group: Option, /// The numbers in the call now. #[serde(default)] pub joined: Vec, /// This person's own state in it. #[serde(default)] pub you: Option, } /// The room ids the Worker mints: 32 lowercase hex. pub fn is_room_id(room: &str) -> bool { room.len() == 32 && room.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) } /// The socket's one path; the credential goes on `Authorization: Bearer`. pub const SOCKET_PATH: &str = "/v1/calls/socket"; #[cfg(test)] #[path = "wire_tests.rs"] mod tests;