rtc

3 modules

  • rtc/rtc_mesh.svelte.ts

    A full-mesh call over one room: RtcMesh keeps an RtcPeer to every other member of the room while the caller holds a seat in it, carrying the peers' signals as room payloads (rtc/rtc_signal.ts) and fanning the local tracks out to every peer.

    The room is any RtcMeshRoom — RoomClient's RoomState is one. The mesh follows its seat: when a seat is taken it connects to each member the join answered, then to each member who joins after; a member who leaves has its peer closed; when the seat ends (left, lost, or the socket closed) every peer closes, and a seat taken again (a rejoin after a reconnect, with a new member id) connects afresh. The local tracks outlive the seats: each new peer starts with them.

    Only the mesh topology is connected: a seat whose join answers another one sets error and connects nothing, since an SFU member needs a different client. A peer that can't be made (new RTCPeerConnection refusing a malformed ICE server, say) sets error too, and the mesh carries on without it.

    A connection serving several actors hears each room message once, whoever it is for, so a message addressed to another member is not this mesh's and is dropped.

    Sends, one at a time per peer

    The server dispatches a socket's requests concurrently, so two room_send requests can relay out of order — a candidate ahead of the offer it belongs to. Each peer's signals therefore go through one chain: a send waits for the previous one's answer. Peers don't wait on each other.

    ICE servers

    ice_servers is a list, or a function of the seat, so servers the join answer carries in its extra (short-lived TURN credentials, minted per join) can be read there.

  • rtc/rtc_peer.svelte.ts

    One WebRTC connection to one remote member: RtcPeer, over an injected RTCPeerConnection, exchanging signals through a send function and an on_signal entry rather than any transport, so two peers cross-wire in a test and a room carries them in the app (rtc/rtc_mesh.svelte.ts).

    Perfect negotiation

    Either end may need to renegotiate at any time (an ICE restart, say), so the two follow the perfect negotiation pattern: each sends an offer whenever its connection says negotiation is needed, and when two offers cross, the polite end rolls its own back and answers the other's, while the impolite end ignores the offer it receives. Which end is polite is a pure function of the two member ids (rtc_peer_is_polite), so both agree without a word: the lower id is polite.

    Candidates that arrive before the remote description is set are queued and added once it is — a candidate is meaningless without one, and the connection refuses it. Candidates that arrive while the impolite end is ignoring an offer may belong to it and fail to add, queued or not; that failure is expected and dropped.

    Inbound signals are handled one at a time, in the order they arrive. A caller that delivers them over a channel that can reorder (concurrently dispatched requests) serializes its sends per peer, as RtcMesh does. Since no signal is handled while another's remote answer is being applied, the standard pattern's "setting a remote answer" flag would never read true here, and there is none.

    Once close is called, nothing more reaches the connection, and a failure of work in flight at the close is expected and not recorded.

    Fixed transceiver slots

    The media a peer carries is a fixed list of slots (RtcSlot), each one sendrecv transceiver of one kind: RTC_VOICE_SLOTS is the microphone's audio; camera, screen, and screen audio are further slots of the same shape, added to the list when they land. Every slot exists from the start, so turning a source on or off is set_track — a replaceTrack on the slot's sender, with no renegotiation — and a slot with no track sends nothing.

    The impolite end creates the transceivers, in slot order, and its first offer carries them; the polite end adopts the transceivers that offer creates on its side, by position, sets them sendrecv, and answers. Both ends creating their own would give each side two transceivers per slot, since a transceiver made by addTransceiver is never matched to a remote offer's media section. So both ends must use the same slot list.

    Two ends whose lists differ degrade rather than fail: a slot the offer has no media section for stays empty (no transceiver, its track kept unsent), a media section past the polite end's slots stays recvonly and unnamed, and a section whose kind differs from its slot's is logged and skipped, that slot staying empty. The slot list is protocol: changing its shape (the slots, their order, or their kinds) bumps RTC_SIGNAL_VERSION (rtc/rtc_signal.ts), so mismatched ends refuse each other's signals instead of degrading.

    Remote media is remote_tracks, by slot name: the receiver's track for each slot once the far end's description reaches it (muted while the far end sends nothing on it).

    ICE restart

    When ICE fails, the peer restarts it (restartIce, which renegotiates with new credentials through the same pattern). A connection that stays disconnected past ice_disconnected_restart_ms is restarted too, since a disconnect is often a network change that ICE alone won't recover from.

  • rtc/rtc_signal.ts

    The WebRTC signal envelope: what RtcPeer sends its far end, carried by RtcMesh as a room payload (room_send's payload, a string the server relays without reading).

    A signal is one of two things, the two that perfect negotiation exchanges: a session description (an offer or an answer) or a trickled ICE candidate. On the wire it is the JSON text of one object whose rtc key is the envelope's version, RTC_SIGNAL_VERSION, beside exactly one of description or candidate:

    {"rtc":1,"description":{"type":"offer","sdp":"v=0\r\n…"}} {"rtc":1,"candidate":{"candidate":"candidate:…","sdpMid":"0","sdpMLineIndex":0,"usernameFragment":"…"}}

    candidate is RTCIceCandidate.toJSON(). The end of candidates is never sent: trickle works without it. The rtc key also tells a signal apart from the room's other payloads (a room's text, say): decode_rtc_signal answers not_rtc_signal for any payload without it, so a room's other listeners can read their own. A newer envelope bumps the version, and an older client refuses it as unsupported_version rather than misreading it.