node-av 5.2.0 → 5.2.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,316 @@
1
+ import { Frame } from '../../lib/frame.js';
2
+ import type { AVPixelFormat } from '../../constants/constants.js';
3
+ import type { IRational } from '../../lib/types.js';
4
+ import type { HardwareContext } from '../hardware.js';
5
+ /**
6
+ * Electron SharedTextureHandle (textureInfo.handle).
7
+ *
8
+ * Platform-specific GPU texture handles provided by Electron's offscreen rendering
9
+ * with shared textures enabled.
10
+ */
11
+ export interface SharedTextureHandle {
12
+ ioSurface?: Buffer;
13
+ ntHandle?: Buffer;
14
+ nativePixmap?: {
15
+ planes: {
16
+ fd: number;
17
+ stride: number;
18
+ offset: number;
19
+ size: number;
20
+ }[];
21
+ modifier: string;
22
+ };
23
+ }
24
+ /**
25
+ * Electron textureInfo object.
26
+ *
27
+ * Contains GPU texture metadata and platform-specific handle for zero-copy frame import.
28
+ */
29
+ export interface TextureInfo {
30
+ pixelFormat: string;
31
+ codedSize: {
32
+ width: number;
33
+ height: number;
34
+ };
35
+ handle: SharedTextureHandle;
36
+ }
37
+ /**
38
+ * Options for each frame import via {@link SharedTexture.importTexture} or {@link SharedTexture.importHandle}.
39
+ */
40
+ export interface TextureFrameOptions {
41
+ pts?: bigint;
42
+ timeBase?: IRational;
43
+ }
44
+ /**
45
+ * Options for {@link SharedTexture.create}.
46
+ */
47
+ export interface SharedTextureOptions {
48
+ width?: number;
49
+ height?: number;
50
+ swFormat?: AVPixelFormat;
51
+ }
52
+ /**
53
+ * Properties for importing a raw GPU texture handle via {@link SharedTexture.importHandle}.
54
+ */
55
+ export interface ImportHandleProps {
56
+ width: number;
57
+ height: number;
58
+ pixelFormat?: string | AVPixelFormat;
59
+ pts?: bigint;
60
+ timeBase?: IRational;
61
+ }
62
+ /**
63
+ * High-level GPU texture import for Electron shared textures.
64
+ *
65
+ * Handles platform detection (macOS IOSurface, Windows D3D11,
66
+ * Linux DMA-BUF), HardwareFramesContext lifecycle, and format mapping automatically.
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * import { HardwareContext, SharedTexture } from 'node-av/api';
71
+ *
72
+ * const hw = HardwareContext.auto();
73
+ * using sharedTexture = SharedTexture.create(hw);
74
+ *
75
+ * // In Electron paint event:
76
+ * offscreen.webContents.on('paint', (event) => {
77
+ * const texture = event.texture;
78
+ * if (!texture?.textureInfo) return;
79
+ *
80
+ * using frame = sharedTexture.importTexture(texture.textureInfo, { pts: 0n });
81
+ * // frame is a hardware Frame ready for encoding/filtering
82
+ *
83
+ * texture.release();
84
+ * });
85
+ * ```
86
+ *
87
+ * @see {@link HardwareContext} For hardware acceleration setup
88
+ * @see {@link Frame} For frame operations
89
+ */
90
+ export declare class SharedTexture implements Disposable {
91
+ private _hardware;
92
+ private _framesCtx;
93
+ private _currentWidth;
94
+ private _currentHeight;
95
+ private _swFormat;
96
+ private _isDisposed;
97
+ private _mappingCtx;
98
+ private _mappingHw;
99
+ private constructor();
100
+ /**
101
+ * Create a SharedTexture.
102
+ *
103
+ * @param hardware - Initialized hardware context (from HardwareContext.auto() or HardwareContext.create())
104
+ *
105
+ * @param options - Optional configuration overrides
106
+ *
107
+ * @returns SharedTexture instance
108
+ *
109
+ * @example
110
+ * ```typescript
111
+ * const hw = HardwareContext.auto();
112
+ * using sharedTexture = SharedTexture.create(hw);
113
+ * ```
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * // With explicit software format
118
+ * import { AV_PIX_FMT_NV12 } from 'node-av/constants';
119
+ *
120
+ * using sharedTexture = SharedTexture.create(hw, { swFormat: AV_PIX_FMT_NV12 });
121
+ * ```
122
+ */
123
+ static create(hardware: HardwareContext, options?: SharedTextureOptions): SharedTexture;
124
+ /**
125
+ * The hardware context used by this sharedTexture.
126
+ */
127
+ get hardware(): HardwareContext;
128
+ /**
129
+ * Current width of the cached HardwareFramesContext.
130
+ */
131
+ get width(): number;
132
+ /**
133
+ * Current height of the cached HardwareFramesContext.
134
+ */
135
+ get height(): number;
136
+ /**
137
+ * Whether this sharedTexture has been disposed.
138
+ */
139
+ get isDisposed(): boolean;
140
+ /**
141
+ * Import an Electron textureInfo as a hardware Frame.
142
+ *
143
+ * Automatically detects the platform from the handle contents,
144
+ * manages the HardwareFramesContext, and creates a zero-copy hardware frame.
145
+ *
146
+ * @param textureInfo - Electron's textureInfo object from paint event
147
+ *
148
+ * @param options - Per-frame options (pts, timeBase)
149
+ *
150
+ * @returns Hardware Frame referencing the GPU texture
151
+ *
152
+ * @throws {Error} If disposed, no valid handle found, or import fails
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * offscreen.webContents.on('paint', (event) => {
157
+ * const texture = event.texture;
158
+ * if (!texture?.textureInfo) return;
159
+ *
160
+ * using frame = sharedTexture.importTexture(texture.textureInfo, {
161
+ * pts: BigInt(Date.now()) * 1000n,
162
+ * timeBase: { num: 1, den: 1000000 },
163
+ * });
164
+ *
165
+ * texture.release();
166
+ * });
167
+ * ```
168
+ */
169
+ importTexture(textureInfo: TextureInfo, options?: TextureFrameOptions): Frame;
170
+ /**
171
+ * Import a raw SharedTextureHandle as a hardware Frame.
172
+ *
173
+ * Use this when you have the handle directly without the full textureInfo wrapper.
174
+ *
175
+ * @param handle - Platform-specific GPU texture handle
176
+ *
177
+ * @param props - Dimensions, format, and timing options
178
+ *
179
+ * @returns Hardware Frame referencing the GPU texture
180
+ *
181
+ * @throws {Error} If disposed, no valid handle found, or import fails
182
+ *
183
+ * @example
184
+ * ```typescript
185
+ * const frame = sharedTexture.importHandle(handle, {
186
+ * width: 1920,
187
+ * height: 1080,
188
+ * pixelFormat: 'BGRA',
189
+ * pts: 0n,
190
+ * });
191
+ * ```
192
+ */
193
+ importHandle(handle: SharedTextureHandle, props: ImportHandleProps): Frame;
194
+ /**
195
+ * Map a frame to a different hardware format.
196
+ *
197
+ * Handles HardwareFramesContext creation and caching automatically.
198
+ * The mapping context is cached and reused for subsequent calls with the
199
+ * same target hardware and frame dimensions.
200
+ *
201
+ * @param srcFrame - Source frame (e.g., DRM PRIME from importTexture on Linux)
202
+ *
203
+ * @param targetHw - Target hardware context (e.g., VAAPI, Vulkan)
204
+ *
205
+ * @param flags - Mapping flags
206
+ *
207
+ * @returns Mapped frame in target hardware format
208
+ *
209
+ * @throws {FFmpegError} If mapping fails (e.g., unsupported mapping, invalid frames)
210
+ *
211
+ * @example
212
+ * ```typescript
213
+ * import { HardwareContext, SharedTexture } from 'node-av/api';
214
+ * import { AV_HWDEVICE_TYPE_VAAPI } from 'node-av/constants';
215
+ *
216
+ * // Import DRM PRIME frame from Electron shared texture
217
+ * const drmFrame = sharedTexture.importTexture(textureInfo, { pts: 0n });
218
+ *
219
+ * // Map to VAAPI for encoding
220
+ * const vaapiHw = await HardwareContext.create(AV_HWDEVICE_TYPE_VAAPI);
221
+ * const vaapiFrame = sharedTexture.mapTo(drmFrame, vaapiHw);
222
+ *
223
+ * // Encode with VAAPI encoder
224
+ * encoder.encode(vaapiFrame);
225
+ * ```
226
+ */
227
+ mapTo(srcFrame: Frame, targetHw: HardwareContext, flags?: number): Frame;
228
+ /**
229
+ * Ensure mapping context exists and matches the target hardware and frame dimensions.
230
+ *
231
+ * Re-creates the context if target hardware changes or dimensions change.
232
+ *
233
+ * @param srcFrame - Source frame to get dimensions from
234
+ *
235
+ * @param targetHw - Target hardware context
236
+ *
237
+ * @internal
238
+ */
239
+ private ensureMappingContext;
240
+ /**
241
+ * Release the cached HardwareFramesContext and mapping context.
242
+ *
243
+ * The HardwareContext is NOT disposed — it belongs to the caller.
244
+ * Safe to call multiple times.
245
+ *
246
+ * @example
247
+ * ```typescript
248
+ * sharedTexture.dispose();
249
+ * ```
250
+ */
251
+ dispose(): void;
252
+ /**
253
+ * Core import logic — dispatches to the correct Frame factory based on handle type.
254
+ *
255
+ * @param handle - Platform-specific GPU texture handle
256
+ *
257
+ * @param width - Texture width in pixels
258
+ *
259
+ * @param height - Texture height in pixels
260
+ *
261
+ * @param swFormat - Software pixel format for HardwareFramesContext
262
+ *
263
+ * @param options - Per-frame timing options
264
+ *
265
+ * @returns Hardware Frame referencing the GPU texture
266
+ *
267
+ * @internal
268
+ */
269
+ private importFromHandle;
270
+ /**
271
+ * Ensure HardwareFramesContext is created and matches the requested dimensions/format.
272
+ *
273
+ * Re-creates the context only when dimensions or format change.
274
+ * Windows D3D11 does not use this — it passes HardwareDeviceContext directly.
275
+ *
276
+ * @param width - Frame width in pixels
277
+ *
278
+ * @param height - Frame height in pixels
279
+ *
280
+ * @param swFormat - Software pixel format
281
+ *
282
+ * @returns Initialized HardwareFramesContext matching the requested parameters
283
+ *
284
+ * @internal
285
+ */
286
+ private ensureFramesContext;
287
+ /**
288
+ * Resolve a pixel format string (e.g., 'BGRA') to an AVPixelFormat enum value.
289
+ *
290
+ * Falls back to the configured swFormat if the name is not recognized.
291
+ *
292
+ * @param name - Pixel format name string from Electron
293
+ *
294
+ * @returns Resolved AVPixelFormat enum value
295
+ *
296
+ * @internal
297
+ */
298
+ private resolvePixelFormat;
299
+ /**
300
+ * Dispose of the SharedTexture.
301
+ *
302
+ * Implements the Disposable interface for automatic cleanup.
303
+ * Equivalent to calling dispose().
304
+ *
305
+ * @example
306
+ * ```typescript
307
+ * {
308
+ * using sharedTexture = SharedTexture.create(hw);
309
+ * // Use sharedTexture...
310
+ * } // Automatically disposed
311
+ * ```
312
+ *
313
+ * @see {@link dispose} For manual cleanup
314
+ */
315
+ [Symbol.dispose](): void;
316
+ }