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, 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 the
surround multiopus taken where it is offered, takeMultiopus) 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 two data channels the server creates: input and
control upstream through sendDataChannelMessage on the ordered input
channel, coalesced pointer motion through sendMotionMessage on the
unordered pointer channel, JSON messages downstream, routed by type to
the on* callbacks (pipeline,
stream_info, stream_stats, cursor, system, ping,
latency_measurement, server_settings, display_settings,
display_config_update, cc_rate, and clipboard-msg*). Either side may
gzip a message once the _gz,1 handshake has been exchanged; the multipart
clipboard, server_settings and display_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:106
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, onstreaminfo,
onstreamstats, onlatencymeasurement, onserversettings,
ondisplaysettings, ondisplayconfig, and onccrate receive the payload of the data channel
message of the same kind, onconnection the server's verdict on this
page's connection, true while it is poor, and onpagevisibility the
server's ask for this page's visibility (page_visibility).
Constructors
Constructor
new WebRTCClient(
signaling,
element,
peer_id
): WebRTCClient;Defined in: lib/webrtc.js:112
Parameters
| Parameter | Type | Description |
|---|---|---|
signaling | WebRTCSignaling | Signaling connection; its onsdp and onice are taken over. |
element | HTMLVideoElement | Element the server's stream plays in. |
peer_id | number | Local peer id registered with the signaling server. |
Returns
Properties
signaling
signaling: WebRTCSignaling;Defined in: lib/webrtc.js:114
element
element: HTMLVideoElement;Defined in: lib/webrtc.js:117
peer_id
peer_id: number;Defined in: lib/webrtc.js:120
forceTurn
forceTurn: boolean;Defined in: lib/webrtc.js:123
Accept only relay ICE candidates and force iceTransportPolicy to relay.
rtcPeerConfig
rtcPeerConfig: any;Defined in: lib/webrtc.js:126
Configuration handed to RTCPeerConnection.
peerConnection
peerConnection: RTCPeerConnection;Defined in: lib/webrtc.js:140
onstatus
onstatus: (arg0) => void;Defined in: lib/webrtc.js:155
Parameters
| Parameter | Type |
|---|---|
arg0 | string |
Returns
void
ondebug
ondebug: (arg0) => void;Defined in: lib/webrtc.js:158
Parameters
| Parameter | Type |
|---|---|
arg0 | string |
Returns
void
onerror
onerror: (arg0) => void;Defined in: lib/webrtc.js:161
Parameters
| Parameter | Type |
|---|---|
arg0 | string |
Returns
void
onconnectionstatechange
onconnectionstatechange: (arg0) => void;Defined in: lib/webrtc.js:164
Parameters
| Parameter | Type |
|---|---|
arg0 | string |
Returns
void
ondatachannelopen
ondatachannelopen: () => void;Defined in: lib/webrtc.js:167
Returns
void
ondatachannelclose
ondatachannelclose: () => void;Defined in: lib/webrtc.js:170
Returns
void
onstreaminfo
onstreaminfo: (arg0) => void;Defined in: lib/webrtc.js:173
Parameters
| Parameter | Type |
|---|---|
arg0 | any |
Returns
void
onlatencymeasurement
onlatencymeasurement: (arg0) => void;Defined in: lib/webrtc.js:176
Parameters
| Parameter | Type |
|---|---|
arg0 | number |
Returns
void
onplaystreamrequired
onplaystreamrequired: () => void;Defined in: lib/webrtc.js:179
Returns
void
onclipboardcontent
onclipboardcontent: (arg0) => void | Promise<void>;Defined in: lib/webrtc.js:182
May return a promise, which the ordered receive queue awaits.
Parameters
| Parameter | Type |
|---|---|
arg0 | any |
Returns
void | Promise<void>
onsystemaction
onsystemaction: (arg0) => void;Defined in: lib/webrtc.js:185
Parameters
| Parameter | Type |
|---|---|
arg0 | string |
Returns
void
oncursorchange
oncursorchange: (arg0) => void;Defined in: lib/webrtc.js:188
Parameters
| Parameter | Type |
|---|---|
arg0 | any |
Returns
void
cursor_cache
cursor_cache: Map<any, any>;Defined in: lib/webrtc.js:191
onstreamstats
onstreamstats: (arg0) => void;Defined in: lib/webrtc.js:194
Parameters
| Parameter | Type |
|---|---|
arg0 | any |
Returns
void
onconnection
onconnection: (arg0) => void;Defined in: lib/webrtc.js:197
Parameters
| Parameter | Type |
|---|---|
arg0 | boolean |
Returns
void
input
input: Input;Defined in: lib/webrtc.js:216
clipboardcontent
clipboardcontent: any[];Defined in: lib/webrtc.js:219
onserversettings
onserversettings: (arg0) => void;Defined in: lib/webrtc.js:222
Parameters
| Parameter | Type |
|---|---|
arg0 | any |
Returns
void
ondisplaysettings
ondisplaysettings: (arg0) => void;Defined in: lib/webrtc.js:225
Parameters
| Parameter | Type |
|---|---|
arg0 | any |
Returns
void
onpagevisibility
onpagevisibility: () => void;Defined in: lib/webrtc.js:228
Returns
void
ondisplayconfig
ondisplayconfig: (arg0) => void;Defined in: lib/webrtc.js:231
Parameters
| Parameter | Type |
|---|---|
arg0 | any |
Returns
void
onprintdocument
onprintdocument: (arg0) => void;Defined in: lib/webrtc.js:234
Parameters
| Parameter | Type |
|---|---|
arg0 | any |
Returns
void
onccrate
onccrate: (arg0) => void;Defined in: lib/webrtc.js:237
Parameters
| Parameter | Type |
|---|---|
arg0 | number |
Returns
void
streams
streams: any[];Defined in: lib/webrtc.js:601
Accessors
webcamTrack
Get Signature
get webcamTrack(): MediaStreamTrack;Defined in: lib/webrtc.js:585
The live camera track sent to the server, or null.
Returns
MediaStreamTrack
Methods
setMicrophone()
setMicrophone(enabled, deviceId?): Promise<boolean>;Defined in: lib/webrtc.js:426
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
| Parameter | Type | Default value | Description |
|---|---|---|---|
enabled | boolean | undefined | - |
deviceId? | string | null | Capture device, the default one when null. |
Returns
Promise<boolean>
False when getUserMedia is unavailable; null when a disable withdrew the enable while its permission request was pending, the stream it got released at once.
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:483
Enables or disables the webcam: attaches a getUserMedia video track to the reserved sendonly transceiver (the browser encodes H.264, VP8, VP9, H.265, or AV1 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
| Parameter | Type | Default value | Description |
|---|---|---|---|
enabled | boolean | undefined | - |
deviceId? | string | null | Camera, the default one when null. |
hints? | { width?: number; height?: number; fps?: number; } | {} | Capture hints. |
hints.width? | number | 1280 | - |
hints.height? | number | 720 | - |
hints.fps? | number | 30 | - |
Returns
Promise<boolean>
False when getUserMedia is unavailable; null when a disable withdrew the enable while its permission request was pending, the camera it got released at once.
Throws
When the server withheld the webcam m-line (the webcam is locked off), raised before prompting for permission; or when the sender refuses the track (its transceiver stopped), the camera released again.
setWebcamCodec()
setWebcamCodec(codec): Promise<boolean>;Defined in: lib/webrtc.js:541
Sends the camera as one codec: the webcam_encoder name (h264, h265,
vp8, vp9, av1) is set on the sender's encoding from the codecs the
answer negotiated, so the browser leaves the negotiated order for it;
auto, mjpeg, or a codec the answer lacks returns to that order. The
codec of an encoding is what the engine offers for this, and an engine
without it keeps the negotiated order, logged once.
Parameters
| Parameter | Type | Description |
|---|---|---|
codec | string | - |
Returns
Promise<boolean>
Whether the named codec is what goes out.
dataChannelBufferedAmount()
dataChannelBufferedAmount(): number;Defined in: lib/webrtc.js:850
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:861
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:876
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
| Parameter | Type | Description |
|---|---|---|
threshold? | number | Bytes. |
Returns
Promise<void>
motionBufferedAmount()
motionBufferedAmount(): number;Defined in: lib/webrtc.js:895
Outbound queue depth of the channel pointer motion goes out on
(sendMotionMessage), which the motion sender slows down on.
Returns
number
sendMotionMessage()
sendMotionMessage(message): void;Defined in: lib/webrtc.js:906
Sends coalesced pointer motion on the unordered pointer channel while it
is open, so a lost sample holds nothing behind it, else on the input channel.
Parameters
| Parameter | Type | Description |
|---|---|---|
message | string | - |
Returns
void
sendDataChannelMessage()
sendDataChannelMessage(message): void;Defined in: lib/webrtc.js:925
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
| Parameter | Type | Description |
|---|---|---|
message | string | ArrayBuffer | - |
Returns
void
onGamepadDisconnect()
onGamepadDisconnect(gp_num): void;Defined in: lib/webrtc.js:955
Reports a gamepad disconnect as a status message.
Parameters
| Parameter | Type | Description |
|---|---|---|
gp_num | number | Gamepad slot. |
Returns
void
getConnectionStats()
getConnectionStats(): Promise<any>;Defined in: lib/webrtc.js:976
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 (with the local candidates and the
outbound-rtp reports of the microphone and webcam, by kind, for
lib/stream-stats.js) 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:1165
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:1186
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:1213
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
Functions
takeMultiopus()
function takeMultiopus(offer, answer): string;Defined in: lib/webrtc.js:68
Answers the server's surround stream with the codec it is offered as.
With more than two audio channels the server offers Chromium's multiopus
ahead of stereo RED and Opus. No engine lists it in an answer of its own,
so its payload type is put first on each audio line the server sends on,
with the offer's rtpmap, fmtp, and rtcp-fb for it. An engine that
decodes it lists it on its receivers and gets the surround stream; for one
that does not, _onSDP sends the answer the engine created itself, and
the server streams that page stereo.
Parameters
| Parameter | Type | Description |
|---|---|---|
offer | string | The server's offer SDP. |
answer | string | The answer SDP the engine created. |
Returns
string
The answer, taking multiopus wherever the offer carries it.