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.

view source

Declarations
#

8 declarations

create_rtc_peer_deps
#

rtc/rtc_peer.svelte.ts view source

(log?: Logger | null | undefined): RtcPeerDeps import {create_rtc_peer_deps} from '@fuzdev/fuz_app/rtc/rtc_peer.svelte.js';

RtcPeerDeps over the browser's own globals, each read when it's called.

log?

type Logger | null
optional

returns

RtcPeerDeps

RTC_ICE_DISCONNECTED_RESTART_MS
#

rtc/rtc_peer.svelte.ts view source

5000 import {RTC_ICE_DISCONNECTED_RESTART_MS} from '@fuzdev/fuz_app/rtc/rtc_peer.svelte.js';

Default RtcPeerOptions.ice_disconnected_restart_ms: long enough for a brief network blip to recover on its own.

rtc_peer_is_polite
#

rtc/rtc_peer.svelte.ts view source

(local_member_id: Branded<string, "Uuid">, remote_member_id: Branded<string, "Uuid">): boolean import {rtc_peer_is_polite} from '@fuzdev/fuz_app/rtc/rtc_peer.svelte.js';

Whether the local end is the polite one of a pair: the lower member id is polite. Both ends compute it alike, so they always disagree on who yields.

local_member_id

this end's member id

type Branded

remote_member_id

the far end's

type Branded

returns

boolean

whether this end yields when offers cross

RTC_VOICE_SLOTS
#

rtc/rtc_peer.svelte.ts view source

readonly RtcSlot[]

type readonly RtcSlot[]

import {RTC_VOICE_SLOTS} from '@fuzdev/fuz_app/rtc/rtc_peer.svelte.js';

The slots of a voice call: the microphone's audio. A call with video adds slots after it (camera, screen, screen audio), in an order both ends share.

RtcPeer
#

rtc/rtc_peer.svelte.ts view source

import {RtcPeer} from '@fuzdev/fuz_app/rtc/rtc_peer.svelte.js';

One connection to one remote member. See the module doc.

remote_member_id

type Branded

readonly

polite

type boolean

readonly

slots

type readonly RtcSlot[]

readonly

connection_state

The connection's connectionState.

type RTCPeerConnectionState

$state.raw

ice_connection_state

The connection's iceConnectionState.

type RTCIceConnectionState

$state.raw

signaling_state

The connection's signalingState.

type RTCSignalingState

$state.raw

remote_tracks

The far end's track for each slot, by slot name, null until its description reaches it.

type Readonly<Record<string, MediaStreamTrack | null>>

$state.raw

closed

Whether close was called.

type boolean

$state.raw

last_error

The last negotiation failure, for diagnostics.

type string | null

$state.raw

constructor

type new (deps: RtcPeerDeps, options: RtcPeerOptions): RtcPeer

deps

options

on_signal

Take a signal from the far end. Signals are handled one at a time, in call order; a failure is logged and kept in last_error, never thrown.

type (signal: RtcSignal): Promise<void>

signal

returns Promise<void>

resolves once this signal is handled

set_track

Send track in slot (null sends nothing) — a replaceTrack, never a renegotiation. On the polite end before the first offer, the track is kept and sent once the slot's transceiver arrives.

type (slot: string, track: MediaStreamTrack | null): Promise<void>

slot

the slot's name

type string

track

the track, of the slot's kind, or null

type MediaStreamTrack | null
returns Promise<void>

throws

  • Error - when no slot has that name

restart_ice

Restart ICE: a renegotiation with new credentials.

type (): void

returns void

close

Close the connection. Idempotent; local tracks are the caller's and stay live.

type (): void

returns void

mutates

  • this — sets `closed`, clears `remote_tracks`

RtcPeerDeps
#

rtc/rtc_peer.svelte.ts view source

RtcPeerDeps import type {RtcPeerDeps} from '@fuzdev/fuz_app/rtc/rtc_peer.svelte.js';

The side effects a peer runs on.

create_peer_connection

new RTCPeerConnection(configuration).

type (configuration: RTCConfiguration): RTCPeerConnection

configuration

type RTCConfiguration
returns RTCPeerConnection

set_timeout

setTimeout.

type (callback: () => void, ms: number): unknown

callback

type () => void

ms

type number
returns unknown

clear_timeout

clearTimeout, given what set_timeout returned.

type (handle: unknown): void

handle

type unknown
returns void

log?

Where negotiation diagnostics go; null or omitted, nowhere.

type Logger | null

RtcPeerOptions
#

rtc/rtc_peer.svelte.ts view source

RtcPeerOptions import type {RtcPeerOptions} from '@fuzdev/fuz_app/rtc/rtc_peer.svelte.js';

Options for RtcPeer.

remote_member_id

The far end's member id.

type Branded

polite

Whether this end yields when offers cross — rtc_peer_is_polite.

type boolean

slots?

The media slots, the same list on both ends.

type readonly RtcSlot[]

ice_servers?

The ICE servers (STUN, TURN); none, host candidates only.

type readonly RTCIceServer[]

send

Deliver a signal to the far end's on_signal, in the order given. Called synchronously; it must not throw.

type (signal: RtcSignal): void

signal

returns void

ice_disconnected_restart_ms?

How long ICE may stay disconnected before the peer restarts it; null restarts only on failed.

type number | null

default RTC_ICE_DISCONNECTED_RESTART_MS

RtcSlot
#

rtc/rtc_peer.svelte.ts view source

RtcSlot import type {RtcSlot} from '@fuzdev/fuz_app/rtc/rtc_peer.svelte.js';

One media slot of a peer connection: a name, and the kind of track it carries.

name

type string

kind

type "audio" | "video"

Depends on
#

Imported by
#