node-av / webrtc / WebRTCStream
Class: WebRTCStream
Defined in: src/api/webrtc-stream.ts:124
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
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();// 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:444
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
// In signaling message handler
if (msg.type === 'candidate') {
session.addIceCandidate(msg.value);
}getCodecs()
getCodecs():
WebRTCCodecInfo
Defined in: src/api/webrtc-stream.ts:275
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
Codec configuration for video and audio streams
Example
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:333
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.
Parameters
offerSdp
string
SDP offer string from remote WebRTC peer
Returns
Promise<string>
SDP answer string to send back to remote peer
Example
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:227
Start streaming media to WebRTC peer connection.
Begins the media processing pipeline, reading packets from input, transcoding if necessary, and sending RTP packets to media tracks. Note: The stream is automatically started by setOffer, so calling this method explicitly is optional. This method is provided for cases where you need explicit control over when streaming begins.
Returns
Promise<void>
Promise that resolves when streaming completes
Throws
If transcoding or muxing fails
Example
const session = await WebRTCStream.create('input.mp4');
session.onIceCandidate = (c) => sendToRemote(c);
const answer = await session.setOffer(remoteOffer);
sendToRemote(answer);
// Stream is already started by setOfferstop()
stop():
Promise<void>
Defined in: src/api/webrtc-stream.ts:247
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
const session = await WebRTCStream.create('input.mp4');
await session.start();
// Stop after 10 seconds
setTimeout(async () => await session.stop(), 10000);create()
staticcreate(input,options?):WebRTCStream
Defined in: src/api/webrtc-stream.ts:179
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
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
const session = WebRTCStream.create('rtsp://camera.local/stream', {
mtu: 1200,
hardware: 'auto',
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
});// Session from a pre-opened Demuxer (e.g. screen capture)
const demuxer = await Device.openScreen({ frameRate: 30 });
const session = WebRTCStream.create(demuxer, {
hardware: 'auto'
});