@tradalab/mediakit 2.4.1 → 2.6.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
@@ -3,6 +3,29 @@ export { DEBUG_VIEWS, EFFECTS_SCHEMA, FieldSpec, NO_EFFECTS, Rgb, TOUCH_UP, need
3
3
  import { Color, SceneLayer, TransitionKind } from './layout.js';
4
4
  export { BLEND_MODES, BlendMode, Rect, TRANSITIONS, hitTest, layouts, parseColor } from './layout.js';
5
5
 
6
+ /** When a lost WebGL context happened: building the core's shaders, linking a face colour shader, or later. */
7
+ type LossPhase = "init" | "link" | "run";
8
+ /** What this browser remembers about a GPU that lost its context under mediakit. */
9
+ interface GpuRecord {
10
+ /** Lost the context building the core's shaders: mediakit does not start on it. */
11
+ init: boolean;
12
+ /** Lost the context linking a face colour shader: makeup and teeth stay off on it. */
13
+ faceColor: boolean;
14
+ /** When the last loss was recorded, ms since the epoch. */
15
+ at: number;
16
+ }
17
+ /** The record for `gpu`, null when there is none, it expired, or this realm has no localStorage. */
18
+ declare function gpuRecord(gpu: string): GpuRecord | null;
19
+ /** Forget every GPU failure this browser remembers - for a "try again" after a driver update. */
20
+ declare function forgetGpuFailures(): void;
21
+ /** A WebGL context lost while mediakit was building or linking its shaders. */
22
+ declare class GpuLostError extends Error {
23
+ readonly phase: LossPhase;
24
+ readonly gpu: string;
25
+ readonly detail: string;
26
+ constructor(phase: LossPhase, gpu: string, detail?: string);
27
+ }
28
+
6
29
  /**
7
30
  * One mediakit pipeline: canvas plus WebGL2 plus the Go core plus the detectors, which is
8
31
  * everything needed to turn a source frame into a processed one.
@@ -47,8 +70,14 @@ interface FrameStats {
47
70
  faceAgeMs: number;
48
71
  /** How old the segmentation mask behind this frame is; 0 without one. */
49
72
  maskAgeMs: number;
73
+ /** Why makeup and teeth are off while the rest runs (their shader failed to link); "" when none. */
74
+ effectError: string;
50
75
  }
51
76
  type EngineCanvas = HTMLCanvasElement | OffscreenCanvas;
77
+ /** What the Engine skips on a GPU known to fail; a host on the main thread gets it from the GPU guard. */
78
+ interface EngineGuard {
79
+ noFaceColor?: boolean;
80
+ }
52
81
  declare class Engine {
53
82
  private canvas;
54
83
  private gl;
@@ -75,6 +104,14 @@ declare class Engine {
75
104
  private classLoading;
76
105
  /** The segmenter whose result this frame borrowed, to give it back after process(). */
77
106
  private used;
107
+ private programErr;
108
+ private loadErr;
109
+ private gpu;
110
+ private linkSeenAt;
111
+ private links;
112
+ private lostPhase;
113
+ private classFailed;
114
+ private readonly failed;
78
115
  /**
79
116
  * The landmark detector, also lazy and independent of the segmenter.
80
117
  *
@@ -125,15 +162,25 @@ declare class Engine {
125
162
  last: FrameStats;
126
163
  private constructor();
127
164
  /** Main thread: makes its own canvas. A worker calls `on` with an OffscreenCanvas. */
128
- static create(width: number, height: number, filter: BgFilter, effects?: Effects, alpha?: boolean, maxFaces?: number): Promise<Engine>;
165
+ static create(width: number, height: number, filter: BgFilter, effects?: Effects, alpha?: boolean, maxFaces?: number, guard?: EngineGuard): Promise<Engine>;
129
166
  /**
130
167
  * `background`: see {@link Engine.setRecipe}. `alpha` keeps the output's alpha channel, which
131
168
  * only a `cutout` background writes; it is fixed for the life of the context, so a host that
132
169
  * may switch to `cutout` later has to ask for it here. `maxFaces` (1-3) is how many faces the
133
- * landmarker looks for: below 3 it skips re-running its face detector on every call.
170
+ * landmarker looks for: below 3 it skips re-running its face detector on every call. Throws
171
+ * GpuLostError when the context is lost building the shaders, or this GPU is remembered for it.
134
172
  */
135
- static on(canvas: EngineCanvas, filter: BgFilter, effects?: Effects, background?: ImageBitmap, alpha?: boolean, maxFaces?: number): Promise<Engine>;
173
+ static on(canvas: EngineCanvas, filter: BgFilter, effects?: Effects, background?: ImageBitmap, alpha?: boolean, maxFaces?: number, guard?: EngineGuard): Promise<Engine>;
174
+ /** The GPU the context reported at creation, "" when the browser hides it. */
175
+ get gpuName(): string;
136
176
  get loading(): boolean;
177
+ /** The browser took the WebGL context away (GPU process reset or reclaimed); this Engine is done. */
178
+ get lost(): boolean;
179
+ /**
180
+ * When the context was lost: "link" within 3 s of a face colour shader starting or still linking, else "run";
181
+ * null while it is not. The first read decides, and on the main thread it is remembered per GPU.
182
+ */
183
+ get lostDuring(): LossPhase | null;
137
184
  get outputCanvas(): EngineCanvas;
138
185
  /** Draws an inner layer instead of the picture until set back to "off". */
139
186
  setDebug(view: DebugView): void;
@@ -145,6 +192,10 @@ declare class Engine {
145
192
  * cannot be decoded at all, and the main thread has to do it and transfer the bitmap.
146
193
  */
147
194
  setRecipe(filter: BgFilter, effects?: Effects, background?: ImageBitmap): Promise<void>;
195
+ private noteLinking;
196
+ /** A worker, or the multiclass model, will bring the mask: no in-thread person model for it. */
197
+ private remoteMaskComing;
198
+ private gaveUp;
148
199
  private ensureFace;
149
200
  /** The detect worker for `kind`, made on first use; rejects when that kind must stay in-thread. */
150
201
  private personRemoteWanted;
@@ -243,6 +294,28 @@ interface PipelineOptions {
243
294
  }) => void;
244
295
  /** The pipeline replaced `processedTrack` mid-session. */
245
296
  onTrackReplaced?: (track: MediaStreamTrack, previous: MediaStreamTrack | undefined) => void;
297
+ /** Something degraded while running; see {@link IssueCode}. Each code fires once until it clears. */
298
+ onIssue?: (issue: PipelineIssue) => void;
299
+ }
300
+ /** What degraded; each code and what the app should do is in the README, "Handling a weak or failing GPU". */
301
+ type IssueCode = "stall" | "slow" | "effect-off" | "worker-moved" | "gpu-lost";
302
+ interface PipelineIssue {
303
+ code: IssueCode;
304
+ message: string;
305
+ transport: Transport;
306
+ }
307
+ /** `gpu-blocked`: this GPU lost its WebGL context building mediakit's shaders in the last 3 days. */
308
+ type SupportReason = "no-window" | "no-wasm" | "no-webgl2" | "no-transport" | "gpu-blocked";
309
+ interface SupportReport {
310
+ supported: boolean;
311
+ /** Every reason it cannot run, empty when supported. */
312
+ reasons: SupportReason[];
313
+ /** WebGL2 only through a software rasteriser (SwiftShader, llvmpipe): it runs, but slowly. */
314
+ softwareRenderer: boolean;
315
+ /** The GPU the browser reports, "" when it hides it. */
316
+ gpu: string;
317
+ /** What this browser remembers about `gpu` failing under mediakit; see `forgetGpuFailures`. */
318
+ remembered: GpuRecord | null;
246
319
  }
247
320
  /** What `health` reports. */
248
321
  interface PipelineHealth {
@@ -252,8 +325,12 @@ interface PipelineHealth {
252
325
  /** Milliseconds since a frame last came out. */
253
326
  sinceMs: number;
254
327
  stalled: boolean;
328
+ /** Why some effects are off while the picture still runs (see `FrameStats.effectError`); "" when none. */
329
+ effectError: string;
255
330
  }
256
- /** Whether mediakit can run in this browser at all. */
331
+ /** Whether mediakit can run in this browser, and every reason it cannot. Creates and frees two WebGL contexts. */
332
+ declare function checkSupport(): SupportReport;
333
+ /** Whether mediakit can run in this browser at all; {@link checkSupport} says why not. */
257
334
  declare function mediakitSupported(): boolean;
258
335
  declare class VideoPipeline {
259
336
  processedTrack?: MediaStreamTrack;
@@ -268,6 +345,15 @@ declare class VideoPipeline {
268
345
  private stalled;
269
346
  private watchdog;
270
347
  private movedOff;
348
+ private raised;
349
+ private slowTicks;
350
+ private gpuRecoveries;
351
+ private gpu;
352
+ private faceColorLost;
353
+ private recovering;
354
+ private workerDialect;
355
+ private generator;
356
+ private piping;
271
357
  /** What the worker last said about loading detectors. */
272
358
  private workerLoading;
273
359
  private frameSubs;
@@ -287,6 +373,11 @@ declare class VideoPipeline {
287
373
  /** Every processed frame. */
288
374
  subscribeFrames(cb: (frame: VideoFrame) => void): () => void;
289
375
  private deliver;
376
+ private issue;
377
+ private watchQuality;
378
+ private noFaceColor;
379
+ /** A "link" loss rebuilds without face colour, a "run" loss as before; an "init" loss would only repeat. */
380
+ private recoverGpu;
290
381
  private beat;
291
382
  private startWatchdog;
292
383
  private stopWatchdog;
@@ -297,6 +388,13 @@ declare class VideoPipeline {
297
388
  /** Take over `source` and publish the result on {@link processedTrack}. */
298
389
  start(source: MediaStreamTrack): Promise<void>;
299
390
  private startMain;
391
+ /**
392
+ * Feed a different camera into the running pipeline, as Tencent's `resetCore(input)`. At the
393
+ * same resolution {@link processedTrack} stays the same track; otherwise the pipeline rebuilds
394
+ * and `onTrackReplaced` fires. The caller still owns both sources: neither is stopped here.
395
+ */
396
+ replaceSource(next: MediaStreamTrack): Promise<MediaStreamTrack | undefined>;
397
+ private rebuild;
300
398
  /** Swap the background in place - no pipeline rebuild, so the picture does not flash. */
301
399
  switchTo(filter: BgFilter): Promise<void>;
302
400
  /**
@@ -309,6 +407,7 @@ declare class VideoPipeline {
309
407
  setEffects(effects: Effects): Promise<void>;
310
408
  private pushRecipe;
311
409
  stop(): Promise<void>;
410
+ private teardownTransports;
312
411
  /**
313
412
  * The image background decoded HERE, for the worker: it has no `Image`, so it cannot decode
314
413
  * an SVG, which is every background meet bundles. Only when the path changed - the worker
@@ -350,9 +449,22 @@ declare class VideoPipeline {
350
449
  /** Starts the worker and waits for its "ready". */
351
450
  private attachWorker;
352
451
  private startInsertable;
452
+ private pipeInsertable;
353
453
  private startCanvas;
354
454
  }
355
455
 
456
+ /** Which detectors {@link preloadDetectors} fetches; the MediaPipe runtime comes with any of them. */
457
+ interface PreloadParts {
458
+ person?: boolean;
459
+ classes?: boolean;
460
+ face?: boolean;
461
+ }
462
+ /**
463
+ * Warm the HTTP cache with the MediaPipe runtime and the chosen models, at low priority, so the
464
+ * first recipe that needs them builds without waiting on the network. Never rejects.
465
+ */
466
+ declare function preloadDetectors(parts?: PreloadParts): Promise<void>;
467
+
356
468
  /**
357
469
  * Layers several sources onto one output, OBS style: cameras (each with its own mediakit
358
470
  * recipe, `cutout` included), screen shares, video files, images, text and any canvas.
@@ -498,4 +610,4 @@ declare function renderText(text: string, style?: TextStyle): HTMLCanvasElement;
498
610
  /** Where each of the face landmarker's first 468 landmarks lies on the canonical face texture. */
499
611
  declare const FACE_UV: Float32Array<ArrayBuffer>;
500
612
 
501
- export { BgFilter, Color, DebugView, 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 };
613
+ export { BgFilter, Color, DebugView, Effects, Engine, type EngineCanvas, type EngineGuard, FACE_UV, type FrameStats, GpuLostError, type GpuRecord, type IssueCode, type LossPhase, type PipelineHealth, type PipelineIssue, type PipelineOptions, type PreloadParts, Scene, SceneLayer, type SceneOptions, type SceneSource, type SceneStats, type SupportReason, type SupportReport, type TextStyle, TransitionKind, type Transport, VideoPipeline, checkSupport, forgetGpuFailures, gpuRecord, mediakitSupported, preloadDetectors, renderText };