node-av 3.0.6 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,331 @@
1
+ import type { AVHWDeviceType } from '../constants/constants.js';
2
+ /**
3
+ * Target codec strings for fMP4 streaming.
4
+ */
5
+ export declare const FMP4_CODECS: {
6
+ readonly H264: "avc1.640029";
7
+ readonly H265: "hvc1.1.6.L153.B0";
8
+ readonly AV1: "av01.0.00M.08";
9
+ readonly AAC: "mp4a.40.2";
10
+ readonly FLAC: "flac";
11
+ readonly OPUS: "opus";
12
+ };
13
+ /**
14
+ * Options for configuring fMP4 streaming.
15
+ */
16
+ export interface FMP4StreamOptions {
17
+ /**
18
+ * Callback invoked for each fMP4 chunk.
19
+ * Use this to send chunks to your client (WebSocket, HTTP, etc.).
20
+ *
21
+ * @param chunk - fMP4 data chunk
22
+ */
23
+ onChunk?: (chunk: Buffer) => void;
24
+ /**
25
+ * Supported codec strings from client.
26
+ * Comma-separated list (e.g., "avc1.640029,hvc1.1.6.L153.B0,mp4a.40.2,flac,opus").
27
+ *
28
+ * @example "avc1.640029,mp4a.40.2"
29
+ */
30
+ supportedCodecs?: string;
31
+ /**
32
+ * Fragment duration in seconds.
33
+ * Smaller values reduce latency but increase overhead.
34
+ *
35
+ * @default 1
36
+ */
37
+ fragDuration?: number;
38
+ /**
39
+ * Hardware acceleration configuration for video transcoding.
40
+ *
41
+ * - `'auto'` - Automatically detect and use available hardware acceleration
42
+ * - Object with deviceType - Manually specify hardware acceleration type
43
+ *
44
+ * @default { deviceType: AV_HWDEVICE_TYPE_NONE }
45
+ */
46
+ hardware?: 'auto' | {
47
+ deviceType: AVHWDeviceType;
48
+ device?: string;
49
+ options?: Record<string, string>;
50
+ };
51
+ /**
52
+ * FFmpeg input options passed directly to the input.
53
+ *
54
+ * @default { flags: 'low_delay' }
55
+ */
56
+ inputOptions?: Record<string, string | number | boolean | null | undefined>;
57
+ }
58
+ /**
59
+ * High-level fMP4 streaming with automatic codec detection and transcoding.
60
+ *
61
+ * Provides fragmented MP4 streaming for clients.
62
+ * Automatically transcodes video to H.264 and audio to AAC if not supported by client.
63
+ * Client sends supported codecs, server transcodes accordingly.
64
+ * Essential component for building adaptive streaming servers.
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * import { FMP4Stream } from 'node-av/api';
69
+ *
70
+ * // Client sends supported codecs
71
+ * const supportedCodecs = 'avc1.640029,hvc1.1.6.L153.B0,mp4a.40.2,flac';
72
+ *
73
+ * // Create stream with codec negotiation
74
+ * const stream = await FMP4Stream.create('rtsp://camera.local/stream', {
75
+ * supportedCodecs,
76
+ * onChunk: (chunk) => ws.send(chunk)
77
+ * });
78
+ *
79
+ * // Start streaming (auto-transcodes if needed)
80
+ * await stream.start();
81
+ * ```
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * // Stream with hardware acceleration
86
+ * const stream = await FMP4Stream.create('input.mp4', {
87
+ * supportedCodecs: 'avc1.640029,mp4a.40.2',
88
+ * hardware: 'auto',
89
+ * fragDuration: 1,
90
+ * onChunk: (chunk) => sendToClient(chunk)
91
+ * });
92
+ *
93
+ * await stream.start();
94
+ * stream.stop();
95
+ * stream.dispose();
96
+ * ```
97
+ *
98
+ * @see {@link MediaInput} For input media handling
99
+ * @see {@link MediaOutput} For fMP4 generation
100
+ * @see {@link HardwareContext} For GPU acceleration
101
+ */
102
+ export declare class FMP4Stream implements Disposable {
103
+ private input;
104
+ private options;
105
+ private output;
106
+ private hardwareContext;
107
+ private videoDecoder;
108
+ private videoEncoder;
109
+ private audioDecoder;
110
+ private audioFilter;
111
+ private audioEncoder;
112
+ private streamActive;
113
+ private supportedCodecs;
114
+ /**
115
+ * @param input - Media input source
116
+ *
117
+ * @param options - Stream configuration options
118
+ *
119
+ * Use {@link create} factory method
120
+ *
121
+ * @internal
122
+ */
123
+ private constructor();
124
+ /**
125
+ * Create a fMP4 stream from a media source.
126
+ *
127
+ * Opens the input media, detects video and audio codecs, and prepares
128
+ * transcoding pipelines based on client-supported codecs.
129
+ * Automatically transcodes to H.264 and AAC if necessary.
130
+ *
131
+ * @param inputUrl - Media source URL (RTSP, file path, HTTP, etc.)
132
+ *
133
+ * @param options - Stream configuration options with supported codecs
134
+ *
135
+ * @returns Configured fMP4 stream instance
136
+ *
137
+ * @throws {Error} If no video stream found in input
138
+ *
139
+ * @throws {FFmpegError} If input cannot be opened
140
+ *
141
+ * @example
142
+ * ```typescript
143
+ * // Stream from file with codec negotiation
144
+ * const stream = await FMP4Stream.create('video.mp4', {
145
+ * supportedCodecs: 'avc1.640029,mp4a.40.2',
146
+ * onChunk: (chunk) => ws.send(chunk)
147
+ * });
148
+ * ```
149
+ *
150
+ * @example
151
+ * ```typescript
152
+ * // Stream from RTSP with auto hardware acceleration
153
+ * const stream = await FMP4Stream.create('rtsp://camera.local/stream', {
154
+ * supportedCodecs: 'avc1.640029,hvc1.1.6.L153.B0,mp4a.40.2',
155
+ * hardware: 'auto',
156
+ * fragDuration: 0.5
157
+ * });
158
+ * ```
159
+ */
160
+ static create(inputUrl: string, options?: FMP4StreamOptions): Promise<FMP4Stream>;
161
+ /**
162
+ * Get the codec string that will be used by client.
163
+ *
164
+ * Returns the MIME type codec string based on input codecs and transcoding decisions.
165
+ * Call this after creating the stream to know what codec string to use for addSourceBuffer().
166
+ *
167
+ * @returns MIME type codec string (e.g., "avc1.640029,mp4a.40.2")
168
+ *
169
+ * @example
170
+ * ```typescript
171
+ * const stream = await FMP4Stream.create('input.mp4', {
172
+ * supportedCodecs: 'avc1.640029,mp4a.40.2'
173
+ * });
174
+ *
175
+ * const codecString = stream.getCodecString();
176
+ * console.log(codecString); // "avc1.640029,mp4a.40.2"
177
+ * // Use this for: sourceBuffer = mediaSource.addSourceBuffer(`video/mp4; codecs="${codecString}"`);
178
+ * ```
179
+ */
180
+ getCodecString(): string;
181
+ /**
182
+ * Get the resolution of the input video stream.
183
+ *
184
+ * @returns Object with width and height properties
185
+ *
186
+ * @example
187
+ * ```typescript
188
+ * const stream = await FMP4Stream.create('input.mp4', {
189
+ * supportedCodecs: 'avc1.640029,mp4a.40.2'
190
+ * });
191
+ *
192
+ * const resolution = stream.getResolution();
193
+ * console.log(`Width: ${resolution.width}, Height: ${resolution.height}`);
194
+ * ```
195
+ */
196
+ getResolution(): {
197
+ width: number;
198
+ height: number;
199
+ };
200
+ /**
201
+ * Start streaming media to fMP4 chunks.
202
+ *
203
+ * Begins the media processing pipeline, reading packets from input,
204
+ * transcoding based on supported codecs, and generating fMP4 chunks.
205
+ * Video transcodes to H.264 if H.264/H.265 not supported.
206
+ * Audio transcodes to AAC if AAC/FLAC/Opus not supported.
207
+ * This method blocks until streaming completes or {@link stop} is called.
208
+ *
209
+ * @returns Promise that resolves when streaming completes
210
+ *
211
+ * @throws {FFmpegError} If transcoding or muxing fails
212
+ *
213
+ * @example
214
+ * ```typescript
215
+ * const stream = await FMP4Stream.create('input.mp4', {
216
+ * supportedCodecs: 'avc1.640029,mp4a.40.2',
217
+ * onChunk: (chunk) => sendToClient(chunk)
218
+ * });
219
+ *
220
+ * // Start streaming (blocks until complete)
221
+ * await stream.start();
222
+ * ```
223
+ *
224
+ * @example
225
+ * ```typescript
226
+ * // Non-blocking start with background promise
227
+ * const stream = await FMP4Stream.create('input.mp4', {
228
+ * supportedCodecs: 'avc1.640029,mp4a.40.2'
229
+ * });
230
+ * const streamPromise = stream.start();
231
+ *
232
+ * // Later: stop streaming
233
+ * stream.stop();
234
+ * await streamPromise;
235
+ * ```
236
+ */
237
+ start(): Promise<void>;
238
+ /**
239
+ * Stop streaming gracefully.
240
+ *
241
+ * Signals the streaming loop to exit after the current packet is processed.
242
+ * Does not immediately close resources - use {@link dispose} for cleanup.
243
+ * Safe to call multiple times.
244
+ *
245
+ * @example
246
+ * ```typescript
247
+ * const stream = await FMP4Stream.create('input.mp4', {
248
+ * supportedCodecs: 'avc1.640029,mp4a.40.2'
249
+ * });
250
+ * const streamPromise = stream.start();
251
+ *
252
+ * // Stop after 10 seconds
253
+ * setTimeout(() => stream.stop(), 10000);
254
+ *
255
+ * await streamPromise; // Resolves when stopped
256
+ * stream.dispose();
257
+ * ```
258
+ */
259
+ stop(): void;
260
+ /**
261
+ * Clean up all resources and close the stream.
262
+ *
263
+ * Stops streaming if active and releases all FFmpeg resources including
264
+ * decoders, encoders, filters, output, and input. Should be called when
265
+ * done with the stream to prevent memory leaks.
266
+ * Safe to call multiple times.
267
+ *
268
+ * @example
269
+ * ```typescript
270
+ * const stream = await FMP4Stream.create('input.mp4', {
271
+ * supportedCodecs: 'avc1.640029,mp4a.40.2'
272
+ * });
273
+ * await stream.start();
274
+ * stream.dispose();
275
+ * ```
276
+ *
277
+ * @example
278
+ * ```typescript
279
+ * // Using automatic cleanup
280
+ * {
281
+ * await using stream = await FMP4Stream.create('input.mp4', {
282
+ * supportedCodecs: 'avc1.640029,mp4a.40.2'
283
+ * });
284
+ * await stream.start();
285
+ * } // Automatically disposed
286
+ * ```
287
+ */
288
+ dispose(): void;
289
+ /**
290
+ * Check if video codec is supported.
291
+ *
292
+ * @param codecId - Codec ID
293
+ *
294
+ * @returns True if H.264, H.265, or AV1 is in supported codecs
295
+ *
296
+ * @internal
297
+ */
298
+ private isVideoCodecSupported;
299
+ /**
300
+ * Check if audio codec is supported.
301
+ *
302
+ * @param codecId - Codec ID
303
+ *
304
+ * @returns True if AAC, FLAC, or Opus is in supported codecs
305
+ *
306
+ * @internal
307
+ */
308
+ private isAudioCodecSupported;
309
+ /**
310
+ * Flush video encoder pipeline.
311
+ *
312
+ * @param videoStreamIndex - Output video stream index
313
+ *
314
+ * @internal
315
+ */
316
+ private flushVideo;
317
+ /**
318
+ * Flush audio encoder pipeline.
319
+ *
320
+ * @param audioStreamIndex - Output audio stream index
321
+ *
322
+ * @internal
323
+ */
324
+ private flushAudio;
325
+ /**
326
+ * Symbol.dispose implementation for automatic cleanup.
327
+ *
328
+ * @internal
329
+ */
330
+ [Symbol.dispose](): void;
331
+ }