Selkies
Developer ReferenceWeb client coreLib

lib/webrtc

Peer-connection side of the WebRTC transport.

The server offers and the client answers. The offer arrives through lib/signaling.js; the answer is munged before it becomes the local description (sps-pps-idr-in-keyframe=1 on the H.264 line, and on the Opus line stereo=1 plus a minptime matching the server's a=ptime, which is how audio frames shorter than 10 ms get through) and ICE candidates are exchanged the same way, non-relay ones dropped when forceTurn is set. The server's video and audio arrive as media tracks on the given element; the audio and video m-lines it offers recvonly are reserved as sendonly transceivers for the microphone and the webcam, which are attached later with replaceTrack and no renegotiation.

Everything else rides the one data channel the server creates: input and control upstream through sendDataChannelMessage, JSON messages downstream, routed by type to the on* callbacks (pipeline, gpu_stats, system_stats, cursor, system, ping, latency_measurement, server_settings, display_config_update and clipboard-msg*). Either side may gzip a message once the _gz,1 handshake has been exchanged; the multipart clipboard and server_settings kinds keep their arrival order across asynchronous inflation, the rest route as soon as they are readable.

Classes

WebRTCClient

Defined in: lib/webrtc.js:67

WebRTC client: one peer connection plus its data channel.

Callbacks are assigned as properties: onstatus, ondebug and onerror receive messages, onconnectionstatechange the peer connection state, ondatachannelopen and ondatachannelclose nothing, onplaystreamrequired fires when autoplay was refused and a user gesture is needed, and onclipboardcontent, oncursorchange, onsystemaction, ongpustats, onsystemstats, onlatencymeasurement, onserversettings and ondisplayconfig receive the payload of the data channel message of the same kind.

Constructors

Constructor
new WebRTCClient(
   signaling, 
   element, 
   peer_id
): WebRTCClient;

Defined in: lib/webrtc.js:73

Parameters
ParameterTypeDescription
signalingWebRTCSignalingSignaling connection; its onsdp and onice are taken over.
elementHTMLVideoElementElement the server's stream plays in.
peer_idnumberLocal peer id registered with the signaling server.
Returns

WebRTCClient

Properties

signaling
signaling: WebRTCSignaling;

Defined in: lib/webrtc.js:75

element
element: HTMLVideoElement;

Defined in: lib/webrtc.js:78

peer_id
peer_id: number;

Defined in: lib/webrtc.js:81

forceTurn
forceTurn: boolean;

Defined in: lib/webrtc.js:84

Accept only relay ICE candidates and force iceTransportPolicy to relay.

rtcPeerConfig
rtcPeerConfig: any;

Defined in: lib/webrtc.js:87

Configuration handed to RTCPeerConnection.

peerConnection
peerConnection: RTCPeerConnection;

Defined in: lib/webrtc.js:101

onstatus
onstatus: (arg0) => void;

Defined in: lib/webrtc.js:112

Parameters
ParameterType
arg0string
Returns

void

ondebug
ondebug: (arg0) => void;

Defined in: lib/webrtc.js:115

Parameters
ParameterType
arg0string
Returns

void

onerror
onerror: (arg0) => void;

Defined in: lib/webrtc.js:118

Parameters
ParameterType
arg0string
Returns

void

onconnectionstatechange
onconnectionstatechange: (arg0) => void;

Defined in: lib/webrtc.js:121

Parameters
ParameterType
arg0string
Returns

void

ondatachannelopen
ondatachannelopen: () => void;

Defined in: lib/webrtc.js:124

Returns

void

ondatachannelclose
ondatachannelclose: () => void;

Defined in: lib/webrtc.js:127

Returns

void

ongpustats
ongpustats: (arg0) => void;

Defined in: lib/webrtc.js:130

Parameters
ParameterType
arg0any
Returns

void

onlatencymeasurement
onlatencymeasurement: (arg0) => void;

Defined in: lib/webrtc.js:133

Parameters
ParameterType
arg0number
Returns

void

onplaystreamrequired
onplaystreamrequired: () => void;

Defined in: lib/webrtc.js:136

Returns

void

onclipboardcontent
onclipboardcontent: (arg0) => void | Promise<void>;

Defined in: lib/webrtc.js:139

May return a promise, which the ordered receive queue awaits.

Parameters
ParameterType
arg0any
Returns

void | Promise<void>

onsystemaction
onsystemaction: (arg0) => void;

Defined in: lib/webrtc.js:142

Parameters
ParameterType
arg0string
Returns

void

oncursorchange
oncursorchange: (arg0) => void;

Defined in: lib/webrtc.js:145

Parameters
ParameterType
arg0any
Returns

void

cursor_cache
cursor_cache: Map<any, any>;

Defined in: lib/webrtc.js:148

onsystemstats
onsystemstats: (arg0) => void;

Defined in: lib/webrtc.js:151

Parameters
ParameterType
arg0any
Returns

void

input
input: Input;

Defined in: lib/webrtc.js:169

clipboardcontent
clipboardcontent: any[];

Defined in: lib/webrtc.js:172

onserversettings
onserversettings: (arg0) => void;

Defined in: lib/webrtc.js:175

Parameters
ParameterType
arg0any
Returns

void

ondisplayconfig
ondisplayconfig: (arg0) => void;

Defined in: lib/webrtc.js:178

Parameters
ParameterType
arg0any
Returns

void

streams
streams: any[];

Defined in: lib/webrtc.js:461

Accessors

webcamTrack
Get Signature
get webcamTrack(): MediaStreamTrack;

Defined in: lib/webrtc.js:445

The live camera track sent to the server, or null.

Returns

MediaStreamTrack

Methods

setMicrophone()
setMicrophone(enabled, deviceId?): Promise<boolean>;

Defined in: lib/webrtc.js:355

Enables or disables the microphone: attaches a getUserMedia track to the reserved sendonly transceiver (the browser encodes Opus over RTP), or detaches and stops it.

Parameters
ParameterTypeDefault valueDescription
enabledbooleanundefined-
deviceIdstringnullCapture device, the default one when null.
Returns

Promise<boolean>

False when getUserMedia is unavailable.

Throws

When the server withheld the microphone m-line (the microphone is disabled server-side); raised before prompting for permission so the UI never claims an active mic that streams nothing.

setWebcam()
setWebcam(
   enabled, 
   deviceId?, 
   hints?
): Promise<boolean>;

Defined in: lib/webrtc.js:399

Enables or disables the webcam: attaches a getUserMedia video track to the reserved sendonly transceiver (the browser encodes H.264 or VP8 over RTP and the server's virtual camera decodes it), or detaches and stops it. Disabling also deactivates the sender's encodings, because a null or ended track alone does not silence every engine (Firefox keeps the encoder running on it); enabling re-activates them, all without renegotiation.

Parameters
ParameterTypeDefault valueDescription
enabledbooleanundefined-
deviceIdstringnullCamera, the default one when null.
hints{ width?: number; height?: number; fps?: number; }{}Capture hints.
hints.width?number1280-
hints.height?number720-
hints.fps?number30-
Returns

Promise<boolean>

False when getUserMedia is unavailable.

Throws

When the server withheld the webcam m-line (the webcam is locked off); raised before prompting for permission.

dataChannelBufferedAmount()
dataChannelBufferedAmount(): number;

Defined in: lib/webrtc.js:687

Outbound queue depth of the data channel; bulk senders (clipboard, uploads) throttle on this so they cannot starve input and stats on the same channel.

Returns

number

dataChannelOpen()
dataChannelOpen(): boolean;

Defined in: lib/webrtc.js:698

Whether the data channel can carry a send right now. Bulk senders check this to report "not connected" instead of dropping into sendDataChannelMessage's quiet no-op.

Returns

boolean

waitForDataChannelDrain()
waitForDataChannelDrain(threshold?): Promise<void>;

Defined in: lib/webrtc.js:713

Resolves once queued sends (including the asynchronous gzip queue) have reached the channel and its buffered amount is below threshold. Bulk senders call this between chunks; without it a burst overflows the SCTP send buffer and Chromium closes the channel with OperationError, killing the session. It resumes on the bufferedamountlow event rather than a poll: polling lets the buffer drain to empty between chunks, which collapses throughput, while keeping about threshold bytes queued keeps the pipe full and still yields the channel to input and stats.

Parameters
ParameterTypeDescription
thresholdnumberBytes.
Returns

Promise<void>

sendDataChannelMessage()
sendDataChannelMessage(message): void;

Defined in: lib/webrtc.js:737

Sends a message on the data channel. Nothing is sent while the channel is not open: periodic senders fire before it opens while connecting, and reporting that would mask real failures. Without negotiated gzip the send is synchronous, so the input hot path pays no latency; with it, strings of 512 bytes or more gzip asynchronously and every send passes through an order-preserving queue so a later small message cannot overtake a large one still compressing.

Parameters
ParameterTypeDescription
messagestring | ArrayBuffer-
Returns

void

onGamepadDisconnect()
onGamepadDisconnect(gp_num): void;

Defined in: lib/webrtc.js:767

Reports a gamepad disconnect as a status message.

Parameters
ParameterTypeDescription
gp_numnumberGamepad slot.
Returns

void

getConnectionStats()
getConnectionStats(): Promise<any>;

Defined in: lib/webrtc.js:786

Collects connection statistics from getStats().

general comes from the transport and candidate-pair reports (its connectionType through the selected pair's remote candidate), video and audio from the inbound-rtp reports (their codecName through the linked codec report; video.decoder reads unknown outside a capturing media context, the only state that exposes decoderImplementation), and data from the data-channel report; the raw reports are attached as reports and allReports. The audio section's NetEQ concealment counters are the RED acceptance metric; Chrome reports opus+red under the codec name opus, so RED presence is confirmed from the SDP or the packet size, never from codecName.

Returns

Promise<any>

playStream()
playStream(): void;

Defined in: lib/webrtc.js:969

Starts playback of the element. Engines refuse autoplay before a user gesture; that refusal is reported through onplaystreamrequired.

Returns

void

connect()
connect(): void;

Defined in: lib/webrtc.js:990

Creates the peer connection (relay-only when forceTurn is set) and connects the signaling client, which starts the offer/answer exchange.

Returns

void

reset()
reset(): void;

Defined in: lib/webrtc.js:1017

Resets the connection: forgets the cursor cache, closes the data channel and the peer connection, and reconnects, after a three-second pause when signaling was not stable.

Returns

void

On this page

Edit on GitHub