Skip to content

node-av / webrtc / RTPStream

Class: RTPStream ​

Defined in: src/api/rtp-stream.ts:204

Generic RTP streaming with automatic codec detection and transcoding.

Provides library-agnostic RTP streaming for various applications. Automatically detects input codecs and transcodes non-compatible formats. Supports hardware acceleration for video transcoding. Essential component for building RTP streaming servers.

Example ​

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

// Create stream with RTP packet callbacks
const stream = RTPStream.create('rtsp://camera.local/stream', {
  mtu: 1200,
  hardware: 'auto',
  onVideoPacket: (rtp) => {
    // Send RTP packet
    sendRtpPacket(rtp);
  },
  onAudioPacket: (rtp) => {
    sendRtpPacket(rtp);
  }
});

// Start streaming
await stream.start();

See ​

Accessors ​

isStreamActive ​

Get Signature ​

get isStreamActive(): boolean

Defined in: src/api/rtp-stream.ts:356

Check if the stream is active.

Stays true while a requested stop is still unwinding: the pipeline reports itself stopped as soon as stop() is called, but its packets keep flowing and its native resources stay open until completion settles.

Returns ​

boolean

True if the stream is active, false otherwise

Methods ​

getInput() ​

getInput(): Demuxer | undefined

Defined in: src/api/rtp-stream.ts:378

Get the demuxer instance.

Used for accessing the underlying demuxer. Only available after start() is called.

Returns ​

Demuxer | undefined

Demuxer instance or undefined if not started

Example ​

typescript
const stream = RTPStream.create('input.mp4', {
  onVideoPacket: (rtp) => sendRtp(rtp)
});
await stream.start();
const input = stream.getInput();
console.log('Bitrate:', input?.bitRate);

requestVideoKeyframe() ​

requestVideoKeyframe(): void

Defined in: src/api/rtp-stream.ts:398

Request that the next encoded video frame is a keyframe.

Used to answer receiver feedback (e.g. a WebRTC PLI) so a receiver that missed the stream start or lost packets can resync without waiting for the next scheduled GOP keyframe. Currently effective for frame-source inputs (MediaFrameSource); for demuxer inputs the encoder's regular GOP cadence applies.

Returns ​

void

Example ​

typescript
transceiver.sender.onPictureLossIndication.subscribe(() => {
  stream.requestVideoKeyframe();
});

start() ​

start(): Promise<void>

Defined in: src/api/rtp-stream.ts:454

Start streaming media to RTP packets.

Begins the media processing pipeline, reading packets from input, transcoding if necessary, and invoking RTP packet callbacks. Automatically handles video and audio streams in parallel. This method returns immediately after starting the pipeline.

Returns ​

Promise<void>

Promise that resolves when pipeline is started

Throws ​

If no video stream found in input

Throws ​

If setup fails

Example ​

typescript
const stream = RTPStream.create('rtsp://camera.local/stream', {
  onVideoPacket: (rtp) => sendRtp(rtp)
});

// Start streaming (returns immediately)
await stream.start();

// Later: stop streaming
await stream.stop();

stop() ​

stop(): Promise<void>

Defined in: src/api/rtp-stream.ts:1102

Stop streaming gracefully and clean up all resources.

Stops the pipeline, closes output, and releases all FFmpeg resources. Safe to call multiple times. After stopping, you can call start() again to restart the stream. Resolves even if the pipeline had failed; that error is reported through onClose(error).

Returns ​

Promise<void>

Example ​

typescript
const stream = RTPStream.create('input.mp4', {
  onVideoPacket: (rtp) => sendRtp(rtp)
});
await stream.start();

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

create() ​

static create(input, options?): RTPStream

Defined in: src/api/rtp-stream.ts:343

Create an RTP stream from a media source.

Configures the stream with input URL and options. The input is not opened until start() is called, allowing the stream to be reused after stop().

Parameters ​

input ​

string | Demuxer | MediaFrameSource

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

options? ​

RTPStreamOptions = {}

Stream configuration options

Returns ​

RTPStream

Configured RTP stream instance

Examples ​

typescript
// Stream from RTSP camera
const stream = RTPStream.create('rtsp://camera.local/stream', {
  mtu: 1200,
  onVideoPacket: (rtp) => sendPacket(rtp),
  onAudioPacket: (rtp) => sendPacket(rtp)
});
typescript
// Stream file with auto hardware acceleration
const stream = RTPStream.create('video.mp4', {
  hardware: 'auto'
});
typescript
// Stream from a pre-opened Demuxer (e.g. Device.openScreen())
const demuxer = await Device.openScreen({ frameRate: 30 });
const stream = RTPStream.create(demuxer, {
  hardware: 'auto',
  onVideoPacket: (rtp) => sendPacket(rtp)
});