Skip to content

@seydx/rtsp / MultiSource

Class: MultiSource ​

Defined in: sources/multi.ts:118

Aggregates several independent inputs into one combined source.

Opens each configured input as its own demuxer and presents their streams as a single multi-track source, flattening the per-input stream indices into one global index space. This is the common pattern for cameras that expose their audio and video as separate raw elementary streams, which cannot be opened as a single libav input. While running, every demuxer is pumped concurrently and its packets are interleaved into a shared output stream.

Example ​

typescript
import { MultiSource } from '@seydx/rtsp';

const source = new MultiSource([
  { input: 'rtsp://camera/video' },
  { input: 'rtsp://camera/audio' },
]);

const info = await source.open();
const controller = new AbortController();
for await (const packet of source.packets(controller.signal)) {
  // route packet by its flattened streamIndex
  packet.free();
}
await source.close();

See ​

Source For the source contract this implements

Implements ​

Constructors ​

Constructor ​

new MultiSource(inputs, opts?): MultiSource

Defined in: sources/multi.ts:142

Create a source that combines multiple inputs.

Parameters ​

inputs ​

MultiSourceInput[]

The inputs to open and aggregate, in track-space order

opts? ​

Optional settings

logger? ​

Logger

Logger for non-fatal errors raised while reading inputs

Returns ​

MultiSource

Methods ​

close() ​

close(): Promise<void>

Defined in: sources/multi.ts:355

Close every opened input and release its resources.

Closes all demuxers in parallel; a failure to close any individual input is logged via the configured logger rather than thrown, so teardown always runs to completion.

Returns ​

Promise<void>

A promise that resolves once all inputs have been closed

Example ​

typescript
await source.close();

Implementation of ​

Source.close


open() ​

open(): Promise<StreamInfo>

Defined in: sources/multi.ts:164

Open every input and resolve the combined stream layout.

Opens each configured input as a separate demuxer, assigns it a contiguous offset in the flattened track space, and collects every stream into a single track list that downstream sinks see as one multi-track source.

Returns ​

Promise<StreamInfo>

The combined stream info across all inputs

Throws ​

If any input fails to open

Example ​

typescript
const info = await source.open();
console.log(`${info.tracks.length} tracks across all inputs`);

Implementation of ​

Source.open


packets() ​

packets(signal): AsyncIterable<MediaPacket>

Defined in: sources/multi.ts:232

Yield interleaved packets from every input until aborted or exhausted.

Pumps all opened demuxers concurrently into a shared queue and emits their packets as they arrive, rewriting each packet's stream index into the flattened global space. Iteration ends when every input has been drained or the signal aborts; any unconsumed packets are released. If any demuxer fails while reading and the signal has not aborted, the remaining inputs are aborted and the failure is thrown immediately — a combined source missing one of its inputs is treated as broken as a whole.

Parameters ​

signal ​

AbortSignal

Abort signal that stops iteration and frees pending packets

Returns ​

AsyncIterable<MediaPacket>

Yields ​

Interleaved packets in the flattened track space (each must be freed by the caller)

Throws ​

If an input fails while reading and the signal is not aborted

Example ​

typescript
const controller = new AbortController();
for await (const packet of source.packets(controller.signal)) {
  handle(packet);
  packet.free();
}

Implementation of ​

Source.packets