@tradalab/mediakit 1.1.0 → 1.3.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.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { BgFilter, Effects } from './recipe.js';
2
- export { NO_EFFECTS, Rgb, TOUCH_UP, needsFace, needsMask, touchUp, touchUpAmount } from './recipe.js';
2
+ export { NO_EFFECTS, Rgb, TOUCH_UP, needsFace, needsMask, needsSegmentation, touchUp, touchUpAmount } from './recipe.js';
3
+ import { Color, SceneLayer, TransitionKind } from './layout.js';
4
+ export { BLEND_MODES, BlendMode, Rect, TRANSITIONS, hitTest, layouts, parseColor } from './layout.js';
3
5
 
4
6
  /**
5
7
  * One mediakit pipeline: canvas plus WebGL2 plus the Go core plus the detectors, which is
@@ -47,6 +49,10 @@ declare class Engine {
47
49
  private backgroundPath;
48
50
  /** The LUT the core holds, by path, so a slider drag does not decode it again. */
49
51
  private lutPath;
52
+ /** The makeup texture the core holds, by path - see setRecipe. */
53
+ private makeupPath;
54
+ /** Whether the core holds the pink whitening table. */
55
+ private skinLutState;
50
56
  private closed;
51
57
  /**
52
58
  * The segmenter loads LAZILY - only once a recipe actually needs a person mask.
@@ -56,6 +62,11 @@ declare class Engine {
56
62
  */
57
63
  private segmenter;
58
64
  private segmenterLoading;
65
+ /** The multiclass segmenter, for recipes that tell skin from clothes (`needsSkinClasses`). */
66
+ private classSegmenter;
67
+ private classLoading;
68
+ /** The segmenter whose result this frame borrowed, to give it back after process(). */
69
+ private used;
59
70
  /**
60
71
  * The landmark detector, also lazy and independent of the segmenter.
61
72
  *
@@ -72,9 +83,14 @@ declare class Engine {
72
83
  last: FrameStats;
73
84
  private constructor();
74
85
  /** Main thread: makes its own canvas. A worker calls `on` with an OffscreenCanvas. */
75
- static create(width: number, height: number, filter: BgFilter, effects?: Effects): Promise<Engine>;
76
- /** `background`: see {@link Engine.setRecipe}. */
77
- static on(canvas: EngineCanvas, filter: BgFilter, effects?: Effects, background?: ImageBitmap): Promise<Engine>;
86
+ static create(width: number, height: number, filter: BgFilter, effects?: Effects, alpha?: boolean): Promise<Engine>;
87
+ /**
88
+ * `background`: see {@link Engine.setRecipe}. `alpha` keeps the output's alpha channel, which
89
+ * only a `cutout` background writes; it is fixed for the life of the context, so a host that
90
+ * may switch to `cutout` later has to ask for it here.
91
+ */
92
+ static on(canvas: EngineCanvas, filter: BgFilter, effects?: Effects, background?: ImageBitmap, alpha?: boolean): Promise<Engine>;
93
+ get loading(): boolean;
78
94
  get outputCanvas(): EngineCanvas;
79
95
  /**
80
96
  * Change the recipe without rebuilding the pipeline, so dragging a slider does not flash.
@@ -85,6 +101,7 @@ declare class Engine {
85
101
  */
86
102
  setRecipe(filter: BgFilter, effects?: Effects, background?: ImageBitmap): Promise<void>;
87
103
  private ensureFace;
104
+ private ensureClassSegmenter;
88
105
  private ensureSegmenter;
89
106
  /**
90
107
  * Draw one source frame onto the output canvas. `false` means nothing was drawn - no mask
@@ -109,7 +126,7 @@ declare class Engine {
109
126
  * for the governor and the graph to decide with. See the ToMicros comment in
110
127
  * `core/clock.go`.
111
128
  */
112
- capture(timestampUs: number): VideoFrame;
129
+ capture(timestampUs: number, keepAlpha?: boolean): VideoFrame;
113
130
  close(): void;
114
131
  }
115
132
 
@@ -140,12 +157,43 @@ declare class Engine {
140
157
  * 3. No WebCodecs needed: the `<video>` element goes straight into `texImage2D`.
141
158
  */
142
159
  type Transport = "worker" | "insertable" | "canvas";
160
+ interface PipelineOptions {
161
+ /** Keep the output's alpha channel, for `subscribeFrames` with a `cutout` background. */
162
+ alpha?: boolean;
163
+ /** No processed frame for 2 s while the camera is live (20 s while a detector loads). */
164
+ onStall?: (info: {
165
+ transport: Transport;
166
+ sinceMs: number;
167
+ }) => void;
168
+ /** The pipeline replaced `processedTrack` mid-session. */
169
+ onTrackReplaced?: (track: MediaStreamTrack, previous: MediaStreamTrack | undefined) => void;
170
+ }
171
+ /** What `health` reports. */
172
+ interface PipelineHealth {
173
+ transport: Transport;
174
+ /** Frames out since start, or since the last transport change. */
175
+ frames: number;
176
+ /** Milliseconds since a frame last came out. */
177
+ sinceMs: number;
178
+ stalled: boolean;
179
+ }
143
180
  /** Whether mediakit can run in this browser at all. */
144
181
  declare function mediakitSupported(): boolean;
145
182
  declare class VideoPipeline {
146
183
  processedTrack?: MediaStreamTrack;
147
184
  private filter;
148
185
  private effects;
186
+ private opts;
187
+ private source;
188
+ private size;
189
+ private lastBeat;
190
+ private frames;
191
+ private stalled;
192
+ private watchdog;
193
+ private movedOff;
194
+ /** What the worker last said about loading detectors. */
195
+ private workerLoading;
196
+ private frameSubs;
149
197
  private engine;
150
198
  private transport;
151
199
  private abort;
@@ -156,11 +204,22 @@ declare class VideoPipeline {
156
204
  private video;
157
205
  private rvfcHandle;
158
206
  private stopped;
159
- constructor(filter: BgFilter, effects?: Effects);
207
+ constructor(filter: BgFilter, effects?: Effects, opts?: PipelineOptions);
208
+ /** Whether frames are coming out, and how long since the last one. */
209
+ get health(): PipelineHealth;
210
+ /** Every processed frame. */
211
+ subscribeFrames(cb: (frame: VideoFrame) => void): () => void;
212
+ private deliver;
213
+ private beat;
214
+ private startWatchdog;
215
+ private stopWatchdog;
216
+ /** The worker failed or stalled AFTER the host published its track. */
217
+ private moveOffWorker;
160
218
  get stats(): FrameStats | null;
161
219
  get transportName(): Transport;
162
220
  /** Take over `source` and publish the result on {@link processedTrack}. */
163
221
  start(source: MediaStreamTrack): Promise<void>;
222
+ private startMain;
164
223
  /** Swap the background in place - no pipeline rebuild, so the picture does not flash. */
165
224
  switchTo(filter: BgFilter): Promise<void>;
166
225
  /** Change the effects outside the background. */
@@ -203,8 +262,157 @@ declare class VideoPipeline {
203
262
  * not get this path and stays on the canvas transport until someone can test that.
204
263
  */
205
264
  private startWorker;
265
+ /** Safari's dialect (see `startTrack` in worker/entry.ts). */
266
+ private startWorkerWithTrack;
267
+ /** Starts the worker and waits for its "ready". */
268
+ private attachWorker;
206
269
  private startInsertable;
207
270
  private startCanvas;
208
271
  }
209
272
 
210
- export { BgFilter, Effects, Engine, type EngineCanvas, type FrameStats, type Transport, VideoPipeline, mediakitSupported };
273
+ /**
274
+ * Layers several sources onto one output, OBS style: cameras (each with its own mediakit
275
+ * recipe, `cutout` included), screen shares, video files, images, text and any canvas.
276
+ *
277
+ * Main thread only. The layout maths, animations and transitions run in the Go core
278
+ * (`core/scene`); this class owns the sources and their lifetimes and hands each frame's
279
+ * pictures over.
280
+ *
281
+ * Each camera with effects runs its own {@link Engine} on its own WebGL context, and its finished
282
+ * picture is uploaded here like any other source. Browsers cap live contexts at 8-16, so a scene
283
+ * holds at most a handful of processed cameras.
284
+ */
285
+ type SceneSource =
286
+ /** A camera, processed by its own Engine. No filter and no effects means none is created. */
287
+ {
288
+ kind: "camera";
289
+ track: MediaStreamTrack;
290
+ filter?: BgFilter;
291
+ effects?: Effects;
292
+ }
293
+ /** Any video track shown as it is: a screen share, a remote participant, a second camera. */
294
+ | {
295
+ kind: "track";
296
+ track: MediaStreamTrack;
297
+ } | {
298
+ kind: "video";
299
+ url: string;
300
+ loop?: boolean;
301
+ } | {
302
+ kind: "image";
303
+ url: string;
304
+ } | {
305
+ kind: "image";
306
+ bitmap: ImageBitmap;
307
+ } | {
308
+ kind: "text";
309
+ text: string;
310
+ style?: TextStyle;
311
+ }
312
+ /** Uploaded every frame: for anything the host draws itself. */
313
+ | {
314
+ kind: "canvas";
315
+ canvas: HTMLCanvasElement | OffscreenCanvas;
316
+ };
317
+ /** Sizes are in pixels of the text's own picture; the layer then scales it. */
318
+ interface TextStyle {
319
+ /** A CSS font. Default `600 48px sans-serif`. */
320
+ font?: string;
321
+ color?: Color;
322
+ background?: Color;
323
+ padding?: number;
324
+ radius?: number;
325
+ align?: "left" | "center" | "right";
326
+ /** Wraps at this width. Default: no wrapping. */
327
+ maxWidth?: number;
328
+ /** A multiple of the font size. Default 1.25. */
329
+ lineHeight?: number;
330
+ outline?: {
331
+ width: number;
332
+ color: Color;
333
+ };
334
+ }
335
+ interface SceneOptions {
336
+ width: number;
337
+ height: number;
338
+ /** The render loop's rate. Default 30. */
339
+ fps?: number;
340
+ background?: Color;
341
+ }
342
+ interface SceneStats {
343
+ glCalls: number;
344
+ /** Time spent on the last frame, sources included, in milliseconds. */
345
+ frameMs: number;
346
+ sources: number;
347
+ layers: number;
348
+ }
349
+ declare class Scene {
350
+ readonly canvas: HTMLCanvasElement;
351
+ private core;
352
+ private gl;
353
+ private sources;
354
+ private current;
355
+ private fps;
356
+ private timer;
357
+ private closed;
358
+ private recorder;
359
+ private recorded;
360
+ last: SceneStats;
361
+ private constructor();
362
+ static create(opts: SceneOptions): Promise<Scene>;
363
+ setBackground(c: Color): void;
364
+ /** Add a source, or replace the one with this id; it keeps its slot, so layers stay put. */
365
+ addSource(id: string, spec: SceneSource): Promise<void>;
366
+ /**
367
+ * Change a source without replacing it: new text or style for text, a new recipe for a
368
+ * camera. Anything else is the same as {@link addSource}.
369
+ */
370
+ updateSource(id: string, spec: SceneSource): Promise<void>;
371
+ removeSource(id: string): void;
372
+ get layers(): readonly SceneLayer[];
373
+ /** Change the layout in place. With `animate` (ms) layers glide to their new state. */
374
+ setLayers(layers: SceneLayer[], opts?: {
375
+ animate?: number;
376
+ }): void;
377
+ /** Move to another layout through a transition. */
378
+ switchTo(layers: SceneLayer[], transition?: {
379
+ kind: TransitionKind;
380
+ duration?: number;
381
+ }): void;
382
+ /** The topmost layer under a point of the output, in fractions, for picking and dragging. */
383
+ layerAt(x: number, y: number): SceneLayer | undefined;
384
+ start(): void;
385
+ stop(): void;
386
+ /** Draw one frame now. The loop calls this; a host with its own loop can instead. */
387
+ render(): number;
388
+ /** The output as a live stream, for a call, a recorder or a `<video>`. */
389
+ captureStream(fps?: number): MediaStream;
390
+ /** The current frame as an image file. */
391
+ snapshot(type?: string): Promise<Blob>;
392
+ /** Record the output, optionally with an audio track, until {@link stopRecording}. */
393
+ startRecording(opts?: {
394
+ audio?: MediaStreamTrack;
395
+ mimeType?: string;
396
+ videoBitsPerSecond?: number;
397
+ }): void;
398
+ stopRecording(): Promise<Blob>;
399
+ close(): void;
400
+ private freeSlot;
401
+ private coreLayers;
402
+ private push;
403
+ private open;
404
+ /**
405
+ * Build, update or drop a camera's Engine to match its recipe and its current size. An
406
+ * Engine is sized at creation, so a camera that changes resolution needs a new one.
407
+ */
408
+ private ensureEngine;
409
+ private feed;
410
+ private dispose;
411
+ }
412
+ /** Draw text onto its own canvas, sized to fit it. */
413
+ declare function renderText(text: string, style?: TextStyle): HTMLCanvasElement;
414
+
415
+ /** Where each of the face landmarker's first 468 landmarks lies on the canonical face texture. */
416
+ declare const FACE_UV: Float32Array<ArrayBuffer>;
417
+
418
+ export { BgFilter, Color, Effects, Engine, type EngineCanvas, FACE_UV, type FrameStats, type PipelineHealth, type PipelineOptions, Scene, SceneLayer, type SceneOptions, type SceneSource, type SceneStats, type TextStyle, TransitionKind, type Transport, VideoPipeline, mediakitSupported, renderText };