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.