node-av 3.0.6 → 3.1.0

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