import { AppendOnlyStreamTarget } from 'mediabunny'; import { AudioSample } from 'mediabunny'; import { AudioSampleResource } from 'mediabunny'; import { MaybePromise } from 'mediabunny'; import * as NodeAv from 'node-av'; import { ReadableStreamSource } from 'mediabunny'; import { SetRequired } from 'mediabunny'; import { VideoDataPlane } from 'mediabunny'; import { VideoSample } from 'mediabunny'; import { VideoSampleColorSpace } from 'mediabunny'; import { VideoSampleInit } from 'mediabunny'; import { VideoSamplePixelFormat } from 'mediabunny'; import { VideoSampleResource } from 'mediabunny'; /** * A custom `AudioSampleResource` backed by NodeAV's * [`Frame`](https://seydx.github.io/node-av/api/lib/classes/Frame.html), which in turn is backed by FFmpeg's * [`AVFrame`](https://ffmpeg.org/doxygen/2.7/structAVFrame.html). You can use this resource to create `AudioSample` * instances that are directly backed by FFmpeg's `AVFrame` without data having to be copied. * * When passed, the `Frame` is now owned by resource, meaning it takes care of closing the frame later. If you want to * keep a copy for your own use, clone the frame first. * * @group \@mediabunny/server * @public */ export declare class AvFrameAudioSampleResource extends AudioSampleResource { /** * The NodeAV [`Frame`](https://seydx.github.io/node-av/api/lib/classes/Frame.html) instance backing this resource. * Access throws if the resource has already been closed. */ get frame(): NodeAv.Frame; constructor(frame: NodeAv.Frame); /** WebCodecs sample format mapped from the underlying AvFrame. */ getFormat(): AudioSampleFormat; /** Sample rate in Hz. */ getSampleRate(): number; /** Number of audio channels. */ getNumberOfChannels(): number; /** Number of audio frames (samples per channel) in this resource. */ getNumberOfFrames(): number; /** Presentation timestamp in seconds. */ getTimestamp(): number; /** Release the underlying AvFrame; after this call the resource is unusable. */ close(): void; /** Return the raw bytes for the given plane (planar layouts have one plane per channel). */ getDataPlane(planeIndex: number): Uint8Array; } /** * A custom `VideoSampleResource` backed by NodeAV's * [`Frame`](https://seydx.github.io/node-av/api/lib/classes/Frame.html), which in turn is backed by FFmpeg's * [`AVFrame`](https://ffmpeg.org/doxygen/2.7/structAVFrame.html). You can use this resource to create `VideoSample` * instances that are directly backed by FFmpeg's `AVFrame` without data having to be copied. Since `AVFrame`s can * themselves be backed by data on the GPU, this enables zero-copy hardware-accelerated decode and encode paths. * * When using Electron, you can directly create `Frame` instances without the data having to leave the GPU. For more, * see [NodeAV's docs](https://seydx.github.io/node-av/api/lib/classes/Frame.html). * * When passed, the `Frame` is now owned by resource, meaning it takes care of closing the frame later. If you want to * keep a copy for your own use, clone the frame first. * * @group \@mediabunny/server * @public */ export declare class AvFrameVideoSampleResource extends VideoSampleResource { /** * The NodeAV [`Frame`](https://seydx.github.io/node-av/api/lib/classes/Frame.html) instance backing this resource. * Access throws if the resource has already been closed. */ get frame(): NodeAv.Frame; constructor(frame: NodeAv.Frame); /** WebCodecs pixel format mapped from the underlying AvFrame, or `null` if unsupported. */ getFormat(): VideoSamplePixelFormat | null; /** Coded frame width in pixels (may differ from display width when SAR ≠ 1). */ getCodedWidth(): number; /** Coded frame height in pixels (may differ from display height when SAR ≠ 1). */ getCodedHeight(): number; /** Display width after applying the sample aspect ratio. */ getSquarePixelWidth(): number; /** Display height after applying the sample aspect ratio. */ getSquarePixelHeight(): number; /** Color space (primaries / transfer / matrix / full-range) of the underlying frame. */ getColorSpace(): VideoSampleColorSpace; /** Release the underlying AvFrame; after this call the resource is unusable. */ close(): void; /** Return one entry per plane (`data` + per-line `stride` in bytes). */ getDataPlanes(): MaybePromise; /** Convert the frame to a packed RGBA `VideoSample`. The `colorSpace` argument is currently unused. */ toRgbSample(init: SetRequired, colorSpace: PredefinedColorSpace): Promise; } /** * Milliseconds the reorder buffer holds a packet before giving up on the ones missing before it. * * @group \@mediabunny/server * @public */ export declare const DEFAULT_RIST_BUFFER_MS = 1000; /** * Options for configuring Mediabunny's server-side polyfills. * @group \@mediabunny/server * @public */ export declare type MediabunnyServerOptions = { /** * The hardware context to use for hardware-accelerated decoding and encoding. When set to a * `NodeAv.HardwareContext`, that context is used directly. When set to a function, the function is invoked (with * no caching) every time a hardware decoder or encoder codec is resolved, receiving the codec ID and returning the * context to use for it (or `null` for no hardware acceleration). * * When not set, a context is automatically detected on first use and then cached. */ hardwareContext?: NodeAv.HardwareContext | null | ((codecId: NodeAv.AVCodecID) => MaybePromise); }; /** * An {@link https://mediabunny.dev/guide/writing-media-files#target | output target} that writes **MXF * OP1a**, the operational pattern broadcast delivery specifications are written against — AS-11 (UK DPP) * among them. * * MXF is not muxed by this library. The target takes the **fragmented MP4** an `Output` writes and * remuxes it through FFmpeg's `mxf` muxer, so the file is OP1a by construction rather than by a * hand-written KLV layer. Use `new Mp4OutputFormat({ fastStart: 'fragmented' })`: a non-fragmented * format writes its `moov` only at the end and so cannot be read as it is produced. * * **Audio is one stream per channel.** Delivery specifications generally require discrete mono tracks * rather than an interleaved pair, so add an audio track per channel to the `Output`; each becomes its * own MXF track, frame-wrapped to the video's frame rate. * * Node only, and it writes to a path rather than a stream, because MXF's footer partition and index * tables need the file to be seekable. * * @example * ```ts * const output = new Output({ * format: new Mp4OutputFormat({ fastStart: 'fragmented' }), * target: new MxfTarget('/deliverables/programme.mxf', { startTimecode: '10:00:00:00' }), * }); * ``` * * @group \@mediabunny/server * @public */ export declare class MxfTarget extends AppendOnlyStreamTarget { constructor(path: string, options?: MxfTargetOptions); /** Packets written to the MXF file so far, counted after frame-wrapping rather than before. */ get writtenPackets(): number; } /** * Options for {@link MxfTarget}. * * @group \@mediabunny/server * @public */ export declare type MxfTargetOptions = { /** * Start timecode as `hh:mm:ss:ff`, or `hh:mm:ss;ff` for drop frame, written as the file's timecode * track. Broadcast delivery specifications generally require one; AS-11 does. */ startTimecode?: string; /** * Signal standard to force, by name as FFmpeg's `mxf` muxer takes it — `smpte274m` for 1125-line * HD, `smpte296m` for 750-line progressive. Left unset, the muxer infers one. */ signalStandard?: 'bt601' | 'bt1358' | 'smpte347m' | 'smpte274m' | 'smpte296m' | 'smpte349m' | 'smpte428'; /** Whether to keep the muxer's user-comment metadata. Defaults to true, as FFmpeg's does. */ storeUserComments?: boolean; /** * Writes the same bytes on every run, by setting FFmpeg's `bitexact` flag. * * MXF is the one container here where identical input does not give an identical file: `mxfenc` draws * the UMIDs at random, so two runs differ in those bytes while the essence repeats exactly. The flag * also drops the encoder and library version strings, which is what makes the file comparable. */ bitexact?: boolean; /** * The date written into the file's Preface and Identification sets. Defaults to the epoch, so two * runs of the same job produce the same bytes; `bitexact` alone does not, because it does not reach * these. Pass a `Date` for a real delivery date, or `null` for the wall clock. */ creationTime?: Date | null; /** * Bits per second to state as the video descriptor's `BitRate`, in place of the rate carried over * from the input. RDD 9 wants the rung's nominal rate there — 50000000 for XDCAM HD422 — where what * arrives is the average measured over whatever was written, which only approaches the nominal rate * over a long enough programme. * * Note that `ffprobe` reports `bit_rate=N/A` for an MXF video stream either way: FFmpeg's `mxf` * demuxer does not read that field back, so the only way to see it is to read the descriptor. */ videoBitRate?: number; }; /** * A {@link https://mediabunny.dev/guide/reading-media-files#source | Source} that reads a live network * stream — SRT, or MPEG-TS over UDP, RTP, TCP or HTTP — so an `Input` can read it like any other source. * * Supplies bytes only; Mediabunny does the demuxing. The socket is opened on the first read, so * constructing one never blocks. Listens (`srt://0.0.0.0:9000?mode=listener`) or calls out * (`srt://host:9000`). RTMP is not supported; see FORK.md. * * @example * ```ts * const input = new Input({ * source: new NetworkSource('srt://0.0.0.0:9000?mode=listener'), * formats: ALL_FORMATS, * }); * ``` * * @group \@mediabunny/server * @public */ export declare class NetworkSource extends ReadableStreamSource { constructor(url: string, options?: NetworkSourceOptions); } /** * Options for {@link NetworkSource}. * * @group \@mediabunny/server * @public */ export declare type NetworkSourceOptions = { /** * Seconds to wait for a sender to appear, listening or connecting out. Defaults to 30. Never * infinite: a wait cannot be cancelled once started. */ connectTimeout?: number; /** * Seconds to wait for data from a connected sender before failing. Defaults to 30. When calling * out, the protocol bounds the connect with this same option, so the larger of the two applies. */ readTimeout?: number; /** * AES passphrase for an `srt://` stream, which must match the sender's. SRT requires 10 to 79 * characters. Without one the stream is read unencrypted. * * Given here rather than written into the URL so it stays out of the messages this class throws. */ passphrase?: string; /** AES key length in bits. Defaults to 128. Only meaningful alongside a passphrase. */ keyLength?: 128 | 192 | 256; /** Maximum bytes to keep for re-reads. Passed through to Mediabunny's `ReadableStreamSource`. */ maxCacheSize?: number; }; /** * Registers video and audio decoders and encoders for all codecs, using FFmpeg's libavcodec under the hood. * Additionally, a custom `VideoSample` transformer based on libavfilter is registered to enable resizing, rotation, * flipping and cropping of video frames. * * Make sure to call this function before interacting with Mediabunny. * * The decoders and encoders will automatically detect hardware acceleration support for each codec and platform and * make use of it if applicable. * * You can pass optional {@link MediabunnyServerOptions} for additional configuration. * * @group \@mediabunny/server * @public */ export declare const registerMediabunnyServer: (options?: MediabunnyServerOptions) => void; /** * Reads a RIST Simple Profile stream (VSF TR-06-1) as an MPEG-TS byte stream: RTP on the URL's even port * carries the transport stream, RTCP on the next port carries the NACKs that ask for what was lost. * * Packets are released in sequence order once their predecessors arrive or `bufferMs` runs out, so a * stream with loss beyond recovery continues with a gap rather than stalling. * * @group \@mediabunny/server * @public */ export declare class RistSource extends ReadableStreamSource { constructor(url: string, options?: RistSourceOptions); } /** * Options for {@link RistSource}. * * @group \@mediabunny/server * @public */ export declare type RistSourceOptions = { /** * Whether to bind and wait for a sender (`'listener'`, the default) or to announce this receiver to one * (`'caller'`). A caller binds an ephemeral port pair and sends RTCP to the URL's address until media * arrives, which is what lets a sender behind NAT learn where to send. */ mode?: 'listener' | 'caller'; /** * Milliseconds a packet may wait for the ones before it, which is also how long a lost packet has to be * retransmitted. Defaults to {@link DEFAULT_RIST_BUFFER_MS}. This is the latency knob MediaConnect * states as "max latency". */ bufferMs?: number; /** Seconds without media before the stream ends. Defaults to 30. */ readTimeout?: number; /** Milliseconds between NACKs for the same missing packet. Defaults to 20. */ nackIntervalMs?: number; /** Maximum bytes to keep for re-reads. Passed through to Mediabunny's `ReadableStreamSource`. */ maxCacheSize?: number; }; /** * An {@link https://mediabunny.dev/guide/writing-media-files#target | output target} that sends MPEG-TS * over RIST Simple Profile (VSF TR-06-1), the counterpart to {@link RistSource}. * * RTP goes to the URL's even port and RTCP is read on the next one, where a receiver's NACKs arrive; a * packet asked for is resent from a buffer of what was recently sent. The protocol is spoken directly * rather than through FFmpeg, which has no librist in this build. * * A byte sink, so the format decides what travels — for a live feed, `MpegTsOutputFormat`. Like * {@link SrtTarget} it drops rather than blocking: past * {@link RistTargetOptions.maxQueueBytes} the arriving bytes are dropped and counted. * * @example * ```ts * const output = new Output({ * format: new MpegTsOutputFormat(), * target: new RistTarget('rist://receiver:9000'), * }); * ``` * * Speaks **Simple Profile** only. `@mediabunny/rist` has a `RistTarget` of the same name over librist * which defaults to main profile, supports encryption, and leaves the send queue to librist — the two * are not interchangeable. * * @group \@mediabunny/server * @public */ export declare class RistTarget extends AppendOnlyStreamTarget { constructor(url: string, options?: RistTargetOptions); /** * Sends what is short of a full datagram now rather than waiting for the bytes that would fill it, * the counterpart of {@link SrtTarget.flushPartialDatagram} — see there for the trade. */ flushPartialDatagram(): Promise; /** RTP packets sent, not counting retransmissions. */ get sentPackets(): number; /** Packets sent again because a receiver asked for them. */ get retransmittedPackets(): number; /** Payload bytes handed to the socket so far, not counting retransmissions. */ get sentBytes(): number; /** * Bytes accepted but not yet handed to the socket, against the bound stated by * {@link RistTarget.maxQueueBytes}. This is the headroom a caller has before anything is dropped, * which is the only way to see it coming rather than after. * * Counted down per datagram rather than per chunk handed in, so it means bytes not yet on the wire * rather than chunks not yet picked up. It reads 0 whenever the sender is keeping up, which on a * healthy path is most of the time, and rises when a producer outruns the socket. */ get queuedBytes(): number; /** The bound {@link RistTarget.queuedBytes} is held against, from the options or the default. */ get maxQueueBytes(): number; /** * Bytes dropped because the queue was full. Not a count of what failed to arrive, and 0 unless * `onOverflow: 'drop'` was asked for — the default blocks the writer instead. */ get droppedBytes(): number; /** Datagrams' worth of dropped bytes, which is the unit a receiver sees missing. */ get droppedDatagrams(): number; } /** * Options for {@link RistTarget}. * * @group \@mediabunny/server * @public */ export declare type RistTargetOptions = { /** * Whether to send to a receiver that is listening (`'caller'`, the default) or to bind the URL's * ports and wait for one to announce itself (`'listener'`). * * A caller sends RTP straight to the URL's address. A listener binds those ports and sends to * whichever address its first RTCP arrives from, which is how a receiver behind NAT is reached. */ mode?: 'listener' | 'caller'; /** * **Milliseconds** of sent packets kept for retransmission. Defaults to 1000, matching * `RistSource`'s own buffer: a receiver cannot ask for what it has already given up on. */ retransmitBufferMs?: number; /** Bytes to queue before the target blocks the writer, or drops. Defaults to 2 MiB. */ maxQueueBytes?: number; /** * What to do once {@link RistTargetOptions.maxQueueBytes} is reached: `'block'` (the default) makes * the writer wait for the socket to catch up, `'drop'` discards the write and counts it. * * Blocking is right for a relay, where losing bytes is worse than arriving late. Dropping is right * for a live encode that must not be stalled by a slow receiver, and is what * {@link RistTarget.droppedBytes} counts. */ onOverflow?: 'block' | 'drop'; /** * Send at the rate the stream itself states, read from the MPEG-TS PCR. On by default, and inert on * a payload carrying no PCR — there is then no rate to pace to, and the bytes go out as fast as the * socket takes them. * * An unpaced sender hands a whole segment to the kernel at once, which a receiver absorbs out of its * de-jitter buffer or not at all. */ pacing?: boolean; }; /** * A {@link https://mediabunny.dev/guide/reading-media-files#source | Source} that reads a live RTMP * stream, so an `Input` can read it like any other source. * * The FLV is remuxed to fragmented MP4 in process — no relay, no temporary files — and Mediabunny * demuxes the result. AVC and HEVC pass through without re-encoding. The socket is opened on the first * read, so constructing one never blocks. * * @example * ```ts * const input = new Input({ * source: new RtmpSource('rtmp://0.0.0.0:1935/live/key', { mode: 'listener' }), * formats: ALL_FORMATS, * }); * ``` * * @group \@mediabunny/server * @public */ export declare class RtmpSource extends ReadableStreamSource { constructor(url: string, options?: RtmpSourceOptions); } /** * Options for {@link RtmpSource}. * * @group \@mediabunny/server * @public */ export declare type RtmpSourceOptions = { /** * Whether to accept a publisher (`'listener'`) or dial out to one (`'caller'`). Defaults to * `'caller'`, so a URL names something to connect to unless listening is asked for. */ mode?: 'listener' | 'caller'; /** * Seconds to wait for a publisher to appear while listening. Defaults to 30. Never infinite: a wait * cannot be cancelled once started. * * Applies to `'listener'` mode only. rtmp's `timeout` option implies listen mode, so it cannot be * given to a caller without turning it into a listener; a caller's connect is bounded by the * protocol instead, at around five seconds. See FORK.md. */ connectTimeout?: number; /** * Seconds to wait for data from a connected publisher before ending the stream. Defaults to 30. */ readTimeout?: number; /** * Seconds of media per fMP4 fragment, defaulting to 0.5. Smaller means lower latency and more * container overhead. */ fragmentDuration?: number; /** Maximum bytes to keep for re-reads. Passed through to Mediabunny's `ReadableStreamSource`. */ maxCacheSize?: number; }; /** * An {@link https://mediabunny.dev/guide/writing-media-files#target | output target} that publishes over * RTMP, the counterpart to {@link RtmpSource}. * * RTMP carries FLV, which this library does not mux, so the target takes the **fragmented MP4** an * `Output` writes and remuxes it: `new Mp4OutputFormat({ fastStart: 'fragmented' })`, whose empty * `moov` is what lets the stream be read before it ends. A non-fragmented format cannot be published, * since its `moov` is only written at the end. * * The connection opens on the first write, so constructing one never blocks, and a server that drops * the publish ends the stream rather than erroring — reconnecting is the caller's, by building a new * `Output`. * * @example * ```ts * const output = new Output({ * format: new Mp4OutputFormat({ fastStart: 'fragmented' }), * target: new RtmpTarget('rtmp://server/live/streamkey'), * }); * ``` * * @group \@mediabunny/server * @public */ export declare class RtmpTarget extends AppendOnlyStreamTarget { constructor(url: string, options?: RtmpTargetOptions); /** Packets published so far, which stays 0 until the server has taken the first one. */ get publishedPackets(): number; } /** * Options for {@link RtmpTarget}. * * @group \@mediabunny/server * @public */ export declare type RtmpTargetOptions = Record; /** * An {@link https://mediabunny.dev/guide/writing-media-files#target | output target} that sends bytes over * SRT, so an `Output` can stream to an SRT receiver the way it writes to a file. A byte sink: it carries * whatever the format writes, which for a live feed means `MpegTsOutputFormat`. * * Calls out by default, or listens with `?mode=listener`. The socket is opened on the first write, so * constructing one never blocks, and every wait is bounded by `connectTimeout`. * * Writes are queued and sent by a pump, so a slow receiver is dropped for rather than blocking the * muxer - see {@link SrtTarget.droppedBytes}. A dead peer stops the bytes and errors on close; * reconnecting is the caller's, by building a new `Output`. * * @example * ```ts * const target = new SrtTarget('srt://receiver:9000', { passphrase: 'a-good-passphrase', latency: 200 }); * const output = new Output({ format: new MpegTsOutputFormat(), target }); * ``` * * @group \@mediabunny/server * @public */ export declare class SrtTarget extends AppendOnlyStreamTarget { constructor(url: string, options?: SrtTargetOptions); /** * Sends what is short of a full datagram now rather than waiting for the bytes that would fill it. * * A datagram is packed to 1316 bytes so each one carries whole MPEG-TS packets, which means a tail * shorter than that waits for more data — and at a low bitrate that wait is latency. Calling this * trades the packing for promptness. `TargetWriter.flush()` deliberately does not do it: it is the * normal per-fragment flush, and sending a short datagram at each one would spend the payload * efficiency the packing exists for. */ flushPartialDatagram(): Promise; /** Bytes handed to the socket so far. */ get sentBytes(): number; /** * Bytes waiting to go to the socket. Against {@link SrtTarget.maxQueueBytes} this is the headroom a * caller has before anything is dropped, which is the only way to see it coming rather than after. */ get queuedBytes(): number; /** The bound {@link SrtTarget.queuedBytes} is held against, from the options or the default. */ get maxQueueBytes(): number; /** Bytes dropped because the receiver was not keeping up. Stays 0 for a receiver that keeps up. */ get droppedBytes(): number; /** Datagrams' worth of dropped bytes, which is the unit a receiver sees missing. */ get droppedDatagrams(): number; } /** * Options for {@link SrtTarget}. * * @group \@mediabunny/server * @public */ export declare type SrtTargetOptions = { /** * **Seconds** to wait for a peer - for a caller to answer, or for one to arrive while listening. * Defaults to 30. Never infinite: a wait cannot be cancelled once started. */ connectTimeout?: number; /** AES passphrase. SRT requires 10 to 79 characters. Without one the stream is unencrypted. */ passphrase?: string; /** AES key length in bits. Defaults to 128. Only meaningful alongside a passphrase. */ keyLength?: 128 | 192 | 256; /** * SRT latency budget in **milliseconds**, the window it has to recover lost packets in. * * Named for its unit because the timeouts beside it are in seconds, which is easy to cross. */ latencyMs?: number; /** * SRT latency budget in milliseconds. * * @deprecated Use {@link SrtTargetOptions.latencyMs}, which states its unit. Still milliseconds, so * a value that worked here still works there. */ latency?: number; /** * Bytes to hold for a receiver that is not keeping up, before the target starts dropping. Defaults * to 2 MiB. See {@link SrtTarget} for what dropping means. */ maxQueueBytes?: number; }; /** * Copies a `VideoSample` or `AudioSample` into the given NodeAV * [`Frame`](https://seydx.github.io/node-av/api/lib/classes/Frame.html), setting up the frame's format, media type, * timing and data. When the sample is already backed by an `AVFrame` (via `AvFrameVideoSampleResource` or * `AvFrameAudioSampleResource`), the frame is ref'd instead of copied for zero-copy reuse. * * For video samples, the frame's time base is always set to 1/1000000 (microsecond accuracy). For audio samples, the * frame's time base is always set to 1/sampleRate. * * @group \@mediabunny/server * @public */ export declare const toAvFrame: (sample: VideoSample | AudioSample, frame: NodeAv.Frame) => Promise; export { } export as namespace MediabunnyServer;