@tradalab/mediakit 1.0.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/chunk-5KXLFDFV.js +62 -0
- package/dist/chunk-5KXLFDFV.js.map +1 -0
- package/dist/index.d.ts +189 -0
- package/dist/index.js +722 -0
- package/dist/index.js.map +1 -0
- package/dist/mediakit.wasm +0 -0
- package/dist/recipe.d.ts +99 -0
- package/dist/recipe.js +21 -0
- package/dist/recipe.js.map +1 -0
- package/dist/wasm_exec.js +575 -0
- package/dist/worker.js +4538 -0
- package/dist/worker.js.map +1 -0
- package/package.json +48 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// src/recipe.ts
|
|
2
|
+
var NO_EFFECTS = {
|
|
3
|
+
beautify: 0,
|
|
4
|
+
exposure: 0,
|
|
5
|
+
contrast: 0,
|
|
6
|
+
saturation: 0,
|
|
7
|
+
warmth: 0,
|
|
8
|
+
autoframe: false,
|
|
9
|
+
teeth: 0,
|
|
10
|
+
eyeBright: 0,
|
|
11
|
+
underEye: 0,
|
|
12
|
+
slimFace: 0,
|
|
13
|
+
enlargeEyes: 0
|
|
14
|
+
};
|
|
15
|
+
var MODE_NONE = 0;
|
|
16
|
+
var MODE_BLUR = 1;
|
|
17
|
+
var MODE_IMAGE = 2;
|
|
18
|
+
function backgroundMode(f) {
|
|
19
|
+
switch (f.kind) {
|
|
20
|
+
case "blur":
|
|
21
|
+
return MODE_BLUR;
|
|
22
|
+
case "image":
|
|
23
|
+
return MODE_IMAGE;
|
|
24
|
+
default:
|
|
25
|
+
return MODE_NONE;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function needsMask(f, e) {
|
|
29
|
+
return f.kind !== "none" || e.beautify > 0 || e.autoframe;
|
|
30
|
+
}
|
|
31
|
+
function needsFace(e) {
|
|
32
|
+
return e.teeth > 0 || e.eyeBright > 0 || e.underEye > 0 || e.slimFace > 0 || e.enlargeEyes > 0;
|
|
33
|
+
}
|
|
34
|
+
function coreRecipe(f, e) {
|
|
35
|
+
return {
|
|
36
|
+
background: backgroundMode(f),
|
|
37
|
+
blurRadius: f.kind === "blur" ? f.blurRadius : 0,
|
|
38
|
+
beautify: e.beautify,
|
|
39
|
+
exposure: e.exposure,
|
|
40
|
+
contrast: e.contrast,
|
|
41
|
+
saturation: e.saturation,
|
|
42
|
+
warmth: e.warmth,
|
|
43
|
+
autoframe: e.autoframe,
|
|
44
|
+
teeth: e.teeth,
|
|
45
|
+
eyeBright: e.eyeBright,
|
|
46
|
+
underEye: e.underEye,
|
|
47
|
+
slimFace: e.slimFace,
|
|
48
|
+
enlargeEyes: e.enlargeEyes
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export {
|
|
53
|
+
NO_EFFECTS,
|
|
54
|
+
MODE_NONE,
|
|
55
|
+
MODE_BLUR,
|
|
56
|
+
MODE_IMAGE,
|
|
57
|
+
backgroundMode,
|
|
58
|
+
needsMask,
|
|
59
|
+
needsFace,
|
|
60
|
+
coreRecipe
|
|
61
|
+
};
|
|
62
|
+
//# sourceMappingURL=chunk-5KXLFDFV.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/recipe.ts"],"sourcesContent":["/**\n * The bridge between what the user picked and the recipe the Go core takes.\n *\n * Both halves live here because both map onto `core.Recipe` field for field: BgFilter carries\n * the background mode and its one parameter, Effects carries everything else. They are two\n * types rather than one because the UI edits them from two different places - a preset picker\n * and a slider panel - not because they belong to different layers.\n */\n\n/**\n * What replaces the background.\n *\n * `imagePath` is a URL the consuming app serves: a path under its own public root, or a data\n * URL for something the user uploaded. The Engine fetches it and cover-crops it to the frame\n * aspect, so any shape is safe to pass.\n */\nexport type BgFilter =\n | { kind: \"none\" }\n | { kind: \"blur\"; blurRadius: number }\n | { kind: \"image\"; imagePath: string }\n\nexport interface Effects {\n /** Skin smoothing, 0..1. Needs the person mask, so it pulls in the segmentation cost. */\n beautify: number\n /** Exposure, -1..1. Positive lifts the shadows more than the highlights. */\n exposure: number\n /** Contrast, -1..1. */\n contrast: number\n /** Saturation, -1..1. */\n saturation: number\n /** Warm (positive) or cool (negative), -1..1. */\n warmth: number\n /** Keep the person framed. Needs the person mask. */\n autoframe: boolean\n\n // ── Need FACE landmarks ──────────────────────────────────────────────────────────\n /** Teeth whitening, 0..1. */\n teeth: number\n /** Brighten the whites of the eyes, 0..1. */\n eyeBright: number\n /** Reduce under-eye shadow, 0..1. */\n underEye: number\n\n /**\n * Jawline slimming, 0..1.\n *\n * Deliberately NOT offered in meet: Zoom, Google Meet and Teams all ship without face\n * reshaping, and in a work setting it reads as unserious. The field is here because the\n * second consumer of mediakit is slate, where reshaping is ordinary.\n */\n slimFace: number\n /** Eye enlargement, 0..1. Withheld from meet for the same reason as slimFace. */\n enlargeEyes: number\n}\n\nexport const NO_EFFECTS: Effects = {\n beautify: 0,\n exposure: 0,\n contrast: 0,\n saturation: 0,\n warmth: 0,\n autoframe: false,\n teeth: 0,\n eyeBright: 0,\n underEye: 0,\n slimFace: 0,\n enlargeEyes: 0,\n}\n\n/** Go-side `BackgroundMode`. Must match `core/recipe.go`. */\nexport const MODE_NONE = 0\nexport const MODE_BLUR = 1\nexport const MODE_IMAGE = 2\n\nexport function backgroundMode(f: BgFilter): number {\n switch (f.kind) {\n case \"blur\":\n return MODE_BLUR\n case \"image\":\n return MODE_IMAGE\n default:\n return MODE_NONE\n }\n}\n\n/**\n * Whether this recipe needs the person mask.\n *\n * Must match `Recipe.NeedsMask` on the Go side. This is the single biggest saving in the\n * module: a colour-only recipe skips MediaPipe entirely - the most expensive part of a frame,\n * and 9 MB of wasm the user then never downloads.\n *\n * The Go version is authoritative; this one exists so the JS side knows whether to load the\n * segmenter at all, BEFORE it can ask Go.\n */\nexport function needsMask(f: BgFilter, e: Effects): boolean {\n return f.kind !== \"none\" || e.beautify > 0 || e.autoframe\n}\n\n/**\n * Whether this recipe needs face landmarks.\n *\n * Must match `Recipe.NeedsFace` on the Go side. Split from {@link needsMask} because the two\n * detectors are independent costs, and the landmark model is 15 times the size of the\n * segmentation one - making someone who only wants a blurred background download it is waste\n * that shows up on the first load.\n */\nexport function needsFace(e: Effects): boolean {\n return (\n e.teeth > 0 || e.eyeBright > 0 || e.underEye > 0 || e.slimFace > 0 || e.enlargeEyes > 0\n )\n}\n\n/** The recipe in the shape the Go core takes. Field names must match `setRecipe` in wasmjs/main.go. */\nexport interface CoreRecipe {\n background: number\n blurRadius: number\n beautify: number\n exposure: number\n contrast: number\n saturation: number\n warmth: number\n autoframe: boolean\n teeth: number\n eyeBright: number\n underEye: number\n slimFace: number\n enlargeEyes: number\n}\n\nexport function coreRecipe(f: BgFilter, e: Effects): CoreRecipe {\n return {\n background: backgroundMode(f),\n blurRadius: f.kind === \"blur\" ? f.blurRadius : 0,\n beautify: e.beautify,\n exposure: e.exposure,\n contrast: e.contrast,\n saturation: e.saturation,\n warmth: e.warmth,\n autoframe: e.autoframe,\n teeth: e.teeth,\n eyeBright: e.eyeBright,\n underEye: e.underEye,\n slimFace: e.slimFace,\n enlargeEyes: e.enlargeEyes,\n }\n}\n"],"mappings":";AAuDO,IAAM,aAAsB;AAAA,EAC/B,UAAU;AAAA,EACV,UAAU;AAAA,EACV,UAAU;AAAA,EACV,YAAY;AAAA,EACZ,QAAQ;AAAA,EACR,WAAW;AAAA,EACX,OAAO;AAAA,EACP,WAAW;AAAA,EACX,UAAU;AAAA,EACV,UAAU;AAAA,EACV,aAAa;AACjB;AAGO,IAAM,YAAY;AAClB,IAAM,YAAY;AAClB,IAAM,aAAa;AAEnB,SAAS,eAAe,GAAqB;AAChD,UAAQ,EAAE,MAAM;AAAA,IACZ,KAAK;AACD,aAAO;AAAA,IACX,KAAK;AACD,aAAO;AAAA,IACX;AACI,aAAO;AAAA,EACf;AACJ;AAYO,SAAS,UAAU,GAAa,GAAqB;AACxD,SAAO,EAAE,SAAS,UAAU,EAAE,WAAW,KAAK,EAAE;AACpD;AAUO,SAAS,UAAU,GAAqB;AAC3C,SACI,EAAE,QAAQ,KAAK,EAAE,YAAY,KAAK,EAAE,WAAW,KAAK,EAAE,WAAW,KAAK,EAAE,cAAc;AAE9F;AAmBO,SAAS,WAAW,GAAa,GAAwB;AAC5D,SAAO;AAAA,IACH,YAAY,eAAe,CAAC;AAAA,IAC5B,YAAY,EAAE,SAAS,SAAS,EAAE,aAAa;AAAA,IAC/C,UAAU,EAAE;AAAA,IACZ,UAAU,EAAE;AAAA,IACZ,UAAU,EAAE;AAAA,IACZ,YAAY,EAAE;AAAA,IACd,QAAQ,EAAE;AAAA,IACV,WAAW,EAAE;AAAA,IACb,OAAO,EAAE;AAAA,IACT,WAAW,EAAE;AAAA,IACb,UAAU,EAAE;AAAA,IACZ,UAAU,EAAE;AAAA,IACZ,aAAa,EAAE;AAAA,EACnB;AACJ;","names":[]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { BgFilter, Effects } from './recipe.js';
|
|
2
|
+
export { NO_EFFECTS, needsFace, needsMask } from './recipe.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* One mediakit pipeline: canvas plus WebGL2 plus the Go core plus the detectors, which is
|
|
6
|
+
* everything needed to turn a source frame into a processed one.
|
|
7
|
+
*
|
|
8
|
+
* The Engine does NOT know where a frame came from or where it goes - that is the transport's
|
|
9
|
+
* job (`pipeline.ts`). They are separate because three browsers need three transports but all
|
|
10
|
+
* three drive this same engine.
|
|
11
|
+
*/
|
|
12
|
+
interface FrameStats {
|
|
13
|
+
/** Total time spent on one frame, in milliseconds. */
|
|
14
|
+
totalMs: number;
|
|
15
|
+
/** How much of that was segmentation. */
|
|
16
|
+
segMs: number;
|
|
17
|
+
/** WebGL call count - the P0.2 gate in doc/apps/mediakit/PLAN.md. */
|
|
18
|
+
glCalls: number;
|
|
19
|
+
/** Whether this frame ran segmentation or reused the previous mask. */
|
|
20
|
+
segmented: boolean;
|
|
21
|
+
/** The resolution ratios the governor settled on. */
|
|
22
|
+
maskScale: number;
|
|
23
|
+
workScale: number;
|
|
24
|
+
/** The autoframe window: centre and zoom, in UV coordinates. */
|
|
25
|
+
cropCX: number;
|
|
26
|
+
cropCY: number;
|
|
27
|
+
cropZoom: number;
|
|
28
|
+
/** Whether the current recipe needs a person mask. */
|
|
29
|
+
needsMask: boolean;
|
|
30
|
+
/** Whether a face is currently being tracked. */
|
|
31
|
+
faceFound: boolean;
|
|
32
|
+
/** Face size as a fraction of frame height. */
|
|
33
|
+
faceSize: number;
|
|
34
|
+
/** Whether the current recipe needs face landmarks. */
|
|
35
|
+
needsFace: boolean;
|
|
36
|
+
}
|
|
37
|
+
type EngineCanvas = HTMLCanvasElement | OffscreenCanvas;
|
|
38
|
+
declare class Engine {
|
|
39
|
+
private canvas;
|
|
40
|
+
private gl;
|
|
41
|
+
private core;
|
|
42
|
+
private background;
|
|
43
|
+
private backgroundPath;
|
|
44
|
+
private closed;
|
|
45
|
+
/**
|
|
46
|
+
* The segmenter loads LAZILY - only once a recipe actually needs a person mask.
|
|
47
|
+
*
|
|
48
|
+
* The MediaPipe runtime is ~9 MB of wasm plus a model. Someone who only wants a brighter
|
|
49
|
+
* picture has no reason to download that.
|
|
50
|
+
*/
|
|
51
|
+
private segmenter;
|
|
52
|
+
private segmenterLoading;
|
|
53
|
+
/**
|
|
54
|
+
* The landmark detector, also lazy and independent of the segmenter.
|
|
55
|
+
*
|
|
56
|
+
* Its model is ~3.7 MB, fifteen times the segmentation one. Someone who only replaces
|
|
57
|
+
* their background has no reason to download it, and the other way round.
|
|
58
|
+
*/
|
|
59
|
+
private face;
|
|
60
|
+
private faceLoading;
|
|
61
|
+
private lmBuf;
|
|
62
|
+
private filter;
|
|
63
|
+
private effects;
|
|
64
|
+
private prevTotalMs;
|
|
65
|
+
private prevSegMs;
|
|
66
|
+
last: FrameStats;
|
|
67
|
+
private constructor();
|
|
68
|
+
/** Main thread: makes its own canvas. A worker calls `on` with an OffscreenCanvas. */
|
|
69
|
+
static create(width: number, height: number, filter: BgFilter, effects?: Effects): Promise<Engine>;
|
|
70
|
+
static on(canvas: EngineCanvas, filter: BgFilter, effects?: Effects): Promise<Engine>;
|
|
71
|
+
get outputCanvas(): EngineCanvas;
|
|
72
|
+
/** Change the recipe without rebuilding the pipeline, so dragging a slider does not flash. */
|
|
73
|
+
setRecipe(filter: BgFilter, effects?: Effects): Promise<void>;
|
|
74
|
+
private ensureFace;
|
|
75
|
+
private ensureSegmenter;
|
|
76
|
+
/**
|
|
77
|
+
* Draw one source frame onto the output canvas. `false` means nothing was drawn - no mask
|
|
78
|
+
* yet, the segmenter is still loading, or the recipe is inactive; the transport must
|
|
79
|
+
* forward the original frame rather than drop it.
|
|
80
|
+
*
|
|
81
|
+
* ── Why it takes `TexImageSource` and not just `VideoFrame` ───────────────────────
|
|
82
|
+
* `texImage2D` accepts a `VideoFrame` and an `HTMLVideoElement` alike, and so does
|
|
83
|
+
* MediaPipe. That is what lets the fallback transport (Firefox, and Safari before the
|
|
84
|
+
* worker path) work with NO WebCodecs at all: it passes the `<video>` element straight in.
|
|
85
|
+
*
|
|
86
|
+
* LiveKit's reference implementation requires a `VideoFrame` even on its fallback path, so
|
|
87
|
+
* it constructs one only to destroy it again.
|
|
88
|
+
*/
|
|
89
|
+
render(source: TexImageSource, timestampUs: number): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Wrap the output canvas as a new VideoFrame.
|
|
92
|
+
*
|
|
93
|
+
* The timestamp is the SOURCE frame's, carried through unchanged. It is not rebuilt from
|
|
94
|
+
* ticks: whole microseconds cannot represent a frame boundary at 30fps (a frame is
|
|
95
|
+
* 33333.33 us), so every tick-to-microsecond round trip loses information. Ticks are only
|
|
96
|
+
* for the governor and the graph to decide with. See the ToMicros comment in
|
|
97
|
+
* `core/clock.go`.
|
|
98
|
+
*/
|
|
99
|
+
capture(timestampUs: number): VideoFrame;
|
|
100
|
+
close(): void;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* A camera track in, a processed camera track out.
|
|
105
|
+
*
|
|
106
|
+
* This is the transport layer: it owns nothing about pixels, only about how frames get to the
|
|
107
|
+
* {@link Engine} and back. The host app wraps it in whatever seam its SDK wants - in meet that
|
|
108
|
+
* is a LiveKit `TrackProcessor`, roughly thirty lines.
|
|
109
|
+
*
|
|
110
|
+
* ── The three transports ─────────────────────────────────────────────────────────────
|
|
111
|
+
*
|
|
112
|
+
* worker preferred - same APIs as `insertable`, but the frame streams are transferred
|
|
113
|
+
* into a worker so the Engine runs off the main thread entirely
|
|
114
|
+
* insertable MediaStreamTrackProcessor + MediaStreamTrackGenerator, on this thread
|
|
115
|
+
* canvas anything else - <video> + captureStream
|
|
116
|
+
*
|
|
117
|
+
* The worker path is what clears defect #1 of the LiveKit implementation
|
|
118
|
+
* (doc/apps/mediakit/PLAN.md section 1): MediaPipe's `segmentForVideo` is a SYNCHRONOUS call
|
|
119
|
+
* tens of milliseconds long, so on the main thread it blocks the event loop and the UI
|
|
120
|
+
* stutters the moment background blur goes on.
|
|
121
|
+
*
|
|
122
|
+
* The `canvas` path here still beats LiveKit's on three measurable points:
|
|
123
|
+
* 1. `requestVideoFrameCallback` instead of polling `currentTime` inside
|
|
124
|
+
* `requestAnimationFrame` - on the real frame rhythm, so no jitter.
|
|
125
|
+
* 2. `captureStream` reads the canvas mediakit already drew, dropping one full-frame
|
|
126
|
+
* `drawImage` per frame.
|
|
127
|
+
* 3. No WebCodecs needed: the `<video>` element goes straight into `texImage2D`.
|
|
128
|
+
*/
|
|
129
|
+
type Transport = "worker" | "insertable" | "canvas";
|
|
130
|
+
/** Whether mediakit can run in this browser at all. */
|
|
131
|
+
declare function mediakitSupported(): boolean;
|
|
132
|
+
declare class VideoPipeline {
|
|
133
|
+
processedTrack?: MediaStreamTrack;
|
|
134
|
+
private filter;
|
|
135
|
+
private effects;
|
|
136
|
+
private engine;
|
|
137
|
+
private transport;
|
|
138
|
+
private abort;
|
|
139
|
+
private worker;
|
|
140
|
+
private workerStats;
|
|
141
|
+
private video;
|
|
142
|
+
private rvfcHandle;
|
|
143
|
+
private stopped;
|
|
144
|
+
constructor(filter: BgFilter, effects?: Effects);
|
|
145
|
+
get stats(): FrameStats | null;
|
|
146
|
+
get transportName(): Transport;
|
|
147
|
+
/** Take over `source` and publish the result on {@link processedTrack}. */
|
|
148
|
+
start(source: MediaStreamTrack): Promise<void>;
|
|
149
|
+
/** Swap the background in place - no pipeline rebuild, so the picture does not flash. */
|
|
150
|
+
switchTo(filter: BgFilter): Promise<void>;
|
|
151
|
+
/** Change the effects outside the background. */
|
|
152
|
+
setEffects(effects: Effects): Promise<void>;
|
|
153
|
+
private pushRecipe;
|
|
154
|
+
stop(): Promise<void>;
|
|
155
|
+
private send;
|
|
156
|
+
private teardownWorker;
|
|
157
|
+
/**
|
|
158
|
+
* Give the worker the frame STREAMS, and keep the track on this thread.
|
|
159
|
+
*
|
|
160
|
+
* ── Why not transfer the track ───────────────────────────────────────────────────
|
|
161
|
+
* The obvious shape is to transfer the camera track into the worker and let it build its
|
|
162
|
+
* own processor and generator there. It does not work, for two independent reasons, and
|
|
163
|
+
* both were measured on 2026-09-26:
|
|
164
|
+
*
|
|
165
|
+
* 1. Chrome 154 does not treat a MediaStreamTrack as transferable at all:
|
|
166
|
+
* `DataCloneError: Value at index 0 does not have a transferable type`. So the whole
|
|
167
|
+
* path fell back to the main thread on every start, on the one browser that has all
|
|
168
|
+
* the APIs it needs.
|
|
169
|
+
* 2. On a browser where the transfer DID succeed it would be worse than useless. A
|
|
170
|
+
* transfer detaches the main-thread object, and a host keeps using it: LiveKit's
|
|
171
|
+
* `setProcessor` calls `attachToElement(_mediaStreamTrack, ...)`, `detachTrack` and
|
|
172
|
+
* registers an `ended` listener on that same object AFTER `init()` returns. Stealing
|
|
173
|
+
* it breaks the camera, not just the effect.
|
|
174
|
+
*
|
|
175
|
+
* A ReadableStream and a WritableStream are both transferable, so the split below moves
|
|
176
|
+
* every frame into the worker with no copy while ownership of the track never leaves this
|
|
177
|
+
* thread. The Engine, MediaPipe and the whole pass chain still run off the main thread,
|
|
178
|
+
* which was the point.
|
|
179
|
+
*
|
|
180
|
+
* The cost: this needs `MediaStreamTrackProcessor` and `MediaStreamTrackGenerator` HERE,
|
|
181
|
+
* so it is the Chrome dialect. A browser that only has them inside a worker (Safari) does
|
|
182
|
+
* not get this path and stays on the canvas transport until someone can test that.
|
|
183
|
+
*/
|
|
184
|
+
private startWorker;
|
|
185
|
+
private startInsertable;
|
|
186
|
+
private startCanvas;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export { BgFilter, Effects, Engine, type EngineCanvas, type FrameStats, type Transport, VideoPipeline, mediakitSupported };
|