//! 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")]pubenumSignalKind {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")]pubenumMediaState {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")]pubenumRoomState {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")]pubenumEndReason {/// 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,}implEndReason {pubfn as_str(self) -> &'static str {matchself {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", } }pubfn parse(text: &str) -> Option<EndReason> { 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")]pubenumMode { #[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")]pubenumMemberState {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)]pubstructMember {pub id: String, #[serde(default)]pub number: Option<String>,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<String>,/// 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<String>, #[serde(default, skip_serializing_if = "Option::is_none")]pub name: Option<String>,}/// 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)]pubstructTrack { #[serde(default, skip_serializing_if = "Option::is_none")]pub mid: Option<String>, #[serde(default, skip_serializing_if = "Option::is_none")]pub member: Option<String>,pub name: String, #[serde(default, skip_serializing_if = "Option::is_none")]pub kind: Option<String>,}/// 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")]pubenumSfuOp {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")]pubenumClientFrame {Signal { kind: SignalKind, #[serde(default, skip_serializing_if = "Option::is_none")] sdp: Option<String>, #[serde(default, skip_serializing_if = "Option::is_none")] candidate: Option<String>, #[serde(default, skip_serializing_if = "Option::is_none")] mid: Option<String>, #[serde(default, skip_serializing_if = "Option::is_none")] mline: Option<u32>, },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<String>, #[serde(default, skip_serializing_if = "Option::is_none")] tracks: Option<Vec<Track>>, #[serde(default, skip_serializing_if = "Option::is_none")] mids: Option<Vec<String>>, },/// Opaque to the room: the hook for frame encryption, relayed member to member.E2ee { #[serde(default, skip_serializing_if = "Option::is_none")] to: Option<String>, epoch: u64, data: String, },Hello { #[serde(default)] e2ee: Vec<String>, },/// 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")]pubenumRoomFrame {State { state: RoomState, #[serde(default, skip_serializing_if = "Option::is_none")] at: Option<serde_json::Value>, },Signal { #[serde(default)] from: String, kind: SignalKind, #[serde(default, skip_serializing_if = "Option::is_none")] sdp: Option<String>, #[serde(default, skip_serializing_if = "Option::is_none")] candidate: Option<String>, #[serde(default, skip_serializing_if = "Option::is_none")] mid: Option<String>, #[serde(default, skip_serializing_if = "Option::is_none")] mline: Option<u32>, },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<String>, #[serde(default, skip_serializing_if = "Option::is_none")] detail: Option<String>, },Mode { mode: Mode },Roster { members: Vec<Member> },Member { member: Member },Tracks { member: String, tracks: Vec<Track> },Speaking { member: String, on: bool },Sfu { op: SfuOp, #[serde(default, skip_serializing_if = "Option::is_none")] sdp: Option<String>,/// 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<Track>, },/// 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<String>, #[serde(default, skip_serializing_if = "Option::is_none")] stopped: Option<String>, #[serde(default, skip_serializing_if = "Option::is_none")] member: Option<String>, #[serde(default, skip_serializing_if = "Vec::is_empty")] members: Vec<String>, },E2ee { #[serde(default)] from: String, epoch: u64, data: String, },Hello { #[serde(default)] from: String, #[serde(default)] e2ee: Vec<String>, },}/// One ICE server as WebRTC takes it.#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]pubstructIceServer {/// One URL or several, as the provider answered.pub urls: serde_json::Value, #[serde(default, skip_serializing_if = "Option::is_none")]pub username: Option<String>, #[serde(default, skip_serializing_if = "Option::is_none")]pub credential: Option<String>,}/// `POST /v1/calls`'s answer.#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]#[serde(rename_all = "camelCase")]pubstructStarted {pub room: String,pub credential: String, #[serde(default)]pub ice_servers: Vec<IceServer>, #[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:<device>`). #[serde(default)]pub me: Option<String>,/// The Cotton group the call was placed from (its random id). #[serde(default, skip_serializing_if = "Option::is_none")]pub group: Option<String>,}/// `POST /v1/calls/<room>/join`'s answer, and `enter`'s.#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]#[serde(rename_all = "camelCase")]pubstructJoined {pub room: String,pub from: String, #[serde(default)]pub video: bool,pub credential: String, #[serde(default)]pub ice_servers: Vec<IceServer>, #[serde(default)]pub ttl: u64, #[serde(default)]pub mode: Mode,/// This device's member id in the room (`caller`, `callee:<device>`). #[serde(default)]pub me: Option<String>,/// The Cotton group the call was placed from (its random id). #[serde(default, skip_serializing_if = "Option::is_none")]pub group: Option<String>,}/// `GET /v1/calls/<room>/status`'s answer: whether a call this person is a member of goes on.#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]pubstructCallStatus {pub live: bool, #[serde(default)]pub video: bool, #[serde(default)]pub group: Option<String>,/// The numbers in the call now. #[serde(default)]pub joined: Vec<String>,/// This person's own state in it. #[serde(default)]pub you: Option<MemberState>,}/// The room ids the Worker mints: 32 lowercase hex.pubfn 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`.pubconst SOCKET_PATH: &str = "/v1/calls/socket";#[cfg(test)]#[path = "wire_tests.rs"]mod tests;