Skip to content

node-av / webrtc / WebRTCStream

Class: WebRTCStream

Defined in: src/api/webrtc-stream.ts:125

Complete WebRTC session management with werift integration.

Provides end-to-end WebRTC streaming with automatic SDP negotiation, ICE candidate handling, and peer connection management. Built on top of RTPStream for generic RTP streaming with WebRTC-specific protocol details handled automatically. Integrates with werift library for RTCPeerConnection and media track handling. Ideal for building complete WebRTC streaming applications with minimal code.

Examples

typescript
import { WebRTCStream } from 'node-av/api';

// Create session from media source
const session = await WebRTCStream.create('rtsp://camera.local/stream', {
  mtu: 1200,
  hardware: 'auto',
  iceServers: [{ urls: 'stun:stun.l.google.com:19302' }],
  onIceCandidate: (candidate) => {
    sendToClient({ type: 'candidate', value: candidate });
  }
});

// Process SDP offer from client
const answer = await session.setOffer(clientOffer);
sendToClient({ type: 'answer', value: answer });

// Start streaming
await session.start();
typescript
// Complete WebSocket signaling server
import { WebSocket } from 'ws';

ws.on('message', async (data) => {
  const msg = JSON.parse(data);

  if (msg.type === 'offer') {
    const session = await WebRTCStream.create(msg.url, {
      hardware: 'auto',
      onIceCandidate: (candidate) => {
        ws.send(JSON.stringify({ type: 'candidate', value: candidate }));
      }
    });

    const answer = await session.setOffer(msg.value);
    ws.send(JSON.stringify({ type: 'answer', value: answer }));

    await session.start();
  } else if (msg.type === 'candidate') {
    session.addIceCandidate(msg.value);
  }
});

See

Methods

addIceCandidate()

addIceCandidate(candidate): void

Defined in: src/api/webrtc-stream.ts:543

Add ICE candidate from remote peer.

Processes ICE candidates received from the remote peer via signaling channel. Should be called whenever a new candidate message arrives from remote peer. Can be called multiple times as candidates are discovered.

Supports ICE trickling: If called before setOffer, candidates are buffered and applied automatically once the remote description is set.

Parameters

candidate

string

ICE candidate string from remote peer

Returns

void

Example

typescript
// In signaling message handler
if (msg.type === 'candidate') {
  session.addIceCandidate(msg.value);
}

getCodecs()

getCodecs(): WebRTCCodecInfo

Defined in: src/api/webrtc-stream.ts:298

Get detected codec information.

Returns RTP codec parameters and FFmpeg codec IDs for video and audio. Useful for inspecting what codecs will be used in the WebRTC session. The input is automatically opened during create, so codecs can be detected immediately after session creation.

Returns

WebRTCCodecInfo

Codec configuration for video and audio streams

Example

typescript
const session = await WebRTCStream.create('input.mp4');
const codecs = session.getCodecs();

console.log('Video:', codecs.video.mimeType);
console.log('Audio:', codecs.audio?.mimeType);

setOffer()

setOffer(offerSdp): Promise<string>

Defined in: src/api/webrtc-stream.ts:368

Process SDP offer from remote peer and generate SDP answer.

Creates RTCPeerConnection with detected codecs, sets up media tracks, processes the remote SDP offer, and generates a local SDP answer. Also configures ICE candidate handling via onIceCandidate callback in options. Must be called before start. Calling it again renegotiates: the previous peer connection is closed and replaced by a new one built from the new offer, while streaming continues.

Parameters

offerSdp

string

SDP offer string from remote WebRTC peer

Returns

Promise<string>

SDP answer string to send back to remote peer

Example

typescript
const session = await WebRTCStream.create('input.mp4');

// Setup ICE candidate handler first
session.onIceCandidate = (candidate) => {
  sendToRemote({ type: 'candidate', value: candidate });
};

// Process offer and send answer
const answer = await session.setOffer(remoteOffer);
sendToRemote({ type: 'answer', value: answer });

// Start streaming
await session.start();

start()

start(): Promise<void>

Defined in: src/api/webrtc-stream.ts:238

Start streaming media to the WebRTC peer connection.

Begins the media processing pipeline, reading packets from input, transcoding if necessary, and sending RTP packets to the media tracks. setOffer calls this automatically after negotiating, so calling it explicitly is optional. It is idempotent - a useful pattern for demuxer inputs is calling start() before setOffer() so the input is open and its actual codecs are advertised in the answer.

Returns

Promise<void>

Promise that resolves once streaming has started (runs in the background)

Throws

If transcoding or muxing fails

Example

typescript
const session = WebRTCStream.create('rtsp://camera.local/stream');

await session.start(); // open input so getCodecs() sees the real codecs
const answer = await session.setOffer(remoteOffer);
sendToRemote(answer);

See

setOffer Which starts the stream automatically after negotiation


stop()

stop(): Promise<void>

Defined in: src/api/webrtc-stream.ts:258

Stop streaming gracefully and clean up all resources.

Stops the stream, closes peer connection, and releases all resources. Safe to call multiple times. After stopping, you can call start() again to restart the session.

Returns

Promise<void>

Example

typescript
const session = await WebRTCStream.create('input.mp4');
await session.start();

// Stop after 10 seconds
setTimeout(async () => await session.stop(), 10000);

create()

static create(input, options?): WebRTCStream

Defined in: src/api/webrtc-stream.ts:182

Create a WebRTC session from a media source.

Opens the input media, creates internal streaming components, and prepares for WebRTC peer connection negotiation. Does not start streaming yet. Call setOffer to negotiate SDP and start to begin streaming.

Parameters

input

string | Demuxer | MediaFrameSource

Media source URL (RTSP, file path, HTTP, etc.) or a pre-opened Demuxer

options?

WebRTCStreamOptions = {}

Session configuration options

Returns

WebRTCStream

Configured WebRTC session instance

Examples

typescript
const session = WebRTCStream.create('rtsp://camera.local/stream', {
  mtu: 1200,
  hardware: 'auto',
  iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
});
typescript
// Session from a pre-opened Demuxer (e.g. screen capture)
const demuxer = await Device.openScreen({ frameRate: 30 });
const session = WebRTCStream.create(demuxer, {
  hardware: 'auto'
});