@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/README.md +103 -0
- package/dist/{chunk-YW75KJJO.js → chunk-RWUGOTNJ.js} +12 -3
- package/dist/chunk-RWUGOTNJ.js.map +1 -0
- package/dist/detect-worker.js +22 -13
- package/dist/detect-worker.js.map +1 -1
- package/dist/index.d.ts +117 -5
- package/dist/index.js +459 -63
- package/dist/index.js.map +1 -1
- package/dist/mediakit.wasm +0 -0
- package/dist/recipe.d.ts +8 -0
- package/dist/recipe.js +1 -1
- package/dist/worker.js +287 -48
- package/dist/worker.js.map +1 -1
- package/package.json +11 -9
- package/dist/chunk-YW75KJJO.js.map +0 -1
package/dist/mediakit.wasm
CHANGED
|
Binary file
|
package/dist/recipe.d.ts
CHANGED
|
@@ -57,6 +57,8 @@ interface Effects {
|
|
|
57
57
|
vibrance: number;
|
|
58
58
|
/** Film grain, 0..1. Noise costs encoder bits: keep it low on a live stream. */
|
|
59
59
|
grain: number;
|
|
60
|
+
/** Unsharp mask over the whole frame, 0..1; eased on skin while `beautify` smooths it. */
|
|
61
|
+
sharpen: number;
|
|
60
62
|
/** Keep the person framed. Needs the person mask. */
|
|
61
63
|
autoframe: boolean;
|
|
62
64
|
/** Teeth whitening, 0..1. */
|
|
@@ -99,6 +101,9 @@ interface Effects {
|
|
|
99
101
|
skinLight: number;
|
|
100
102
|
/** Local contrast on the face. */
|
|
101
103
|
clarity: number;
|
|
104
|
+
/** Hair dye, 0..1, strands and shading kept. Needs `/mediakit/selfie_multiclass_256x256.tflite`. */
|
|
105
|
+
hair: number;
|
|
106
|
+
hairColor: Rgb;
|
|
102
107
|
/** Where smoothing may land. See {@link SkinScope}. */
|
|
103
108
|
skinScope: SkinScope;
|
|
104
109
|
/**
|
|
@@ -286,6 +291,7 @@ interface CoreRecipe {
|
|
|
286
291
|
tint: number;
|
|
287
292
|
vibrance: number;
|
|
288
293
|
grain: number;
|
|
294
|
+
sharpen: number;
|
|
289
295
|
autoframe: boolean;
|
|
290
296
|
teeth: number;
|
|
291
297
|
eyeBright: number;
|
|
@@ -305,6 +311,8 @@ interface CoreRecipe {
|
|
|
305
311
|
whiten: number;
|
|
306
312
|
skinLight: number;
|
|
307
313
|
clarity: number;
|
|
314
|
+
hair: number;
|
|
315
|
+
hairColor: Rgb;
|
|
308
316
|
skinScope: number;
|
|
309
317
|
grade: number;
|
|
310
318
|
gradeScope: number;
|
package/dist/recipe.js
CHANGED
package/dist/worker.js
CHANGED
|
@@ -4675,15 +4675,58 @@
|
|
|
4675
4675
|
return ec(zc, t2, e2);
|
|
4676
4676
|
}, zc.POSE_CONNECTIONS = Lc, r("module$exports$google3$third_party$mediapipe$tasks$web$vision$pose_landmarker$pose_landmarker.PoseLandmarker.POSE_CONNECTIONS", zc.POSE_CONNECTIONS);
|
|
4677
4677
|
|
|
4678
|
-
// src/
|
|
4679
|
-
var
|
|
4678
|
+
// src/mp.ts
|
|
4679
|
+
var MP_DIR = "/mediakit/mp";
|
|
4680
4680
|
var MODEL_URL = {
|
|
4681
4681
|
person: "/mediakit/selfie_segmenter.tflite",
|
|
4682
|
-
classes: "/mediakit/selfie_multiclass_256x256.tflite"
|
|
4682
|
+
classes: "/mediakit/selfie_multiclass_256x256.tflite",
|
|
4683
|
+
face: "/mediakit/face_landmarker.task"
|
|
4684
|
+
};
|
|
4685
|
+
var LOAD_MS = 6e4;
|
|
4686
|
+
var fileset = null;
|
|
4687
|
+
function visionFileset() {
|
|
4688
|
+
fileset ??= uh.forVisionTasks(MP_DIR).catch((e2) => {
|
|
4689
|
+
fileset = null;
|
|
4690
|
+
throw e2;
|
|
4691
|
+
});
|
|
4692
|
+
return fileset;
|
|
4693
|
+
}
|
|
4694
|
+
var LoadTimeout = class extends Error {
|
|
4695
|
+
constructor(what, ms2) {
|
|
4696
|
+
super(`mediakit: ${what} did not load within ${Math.round(ms2 / 1e3)} s`);
|
|
4697
|
+
this.name = "LoadTimeout";
|
|
4698
|
+
}
|
|
4683
4699
|
};
|
|
4700
|
+
function withTimeout(p2, ms2, what, late) {
|
|
4701
|
+
return new Promise((resolve, reject) => {
|
|
4702
|
+
let done = false;
|
|
4703
|
+
const timer = setTimeout(() => {
|
|
4704
|
+
done = true;
|
|
4705
|
+
reject(new LoadTimeout(what, ms2));
|
|
4706
|
+
}, ms2);
|
|
4707
|
+
p2.then(
|
|
4708
|
+
(v2) => {
|
|
4709
|
+
if (done) late(v2);
|
|
4710
|
+
else {
|
|
4711
|
+
done = true;
|
|
4712
|
+
clearTimeout(timer);
|
|
4713
|
+
resolve(v2);
|
|
4714
|
+
}
|
|
4715
|
+
},
|
|
4716
|
+
(e2) => {
|
|
4717
|
+
if (done) return;
|
|
4718
|
+
done = true;
|
|
4719
|
+
clearTimeout(timer);
|
|
4720
|
+
reject(e2);
|
|
4721
|
+
}
|
|
4722
|
+
);
|
|
4723
|
+
});
|
|
4724
|
+
}
|
|
4725
|
+
|
|
4726
|
+
// src/segmenter.ts
|
|
4684
4727
|
async function createSegmenter(canvas, model = "person") {
|
|
4685
|
-
const
|
|
4686
|
-
const segmenter = await $c.createFromOptions(
|
|
4728
|
+
const fileset2 = await visionFileset();
|
|
4729
|
+
const segmenter = await $c.createFromOptions(fileset2, {
|
|
4687
4730
|
baseOptions: {
|
|
4688
4731
|
modelAssetPath: MODEL_URL[model],
|
|
4689
4732
|
// MediaPipe's GPU delegate runs on WebGL2, which Chrome, Safari and Firefox all
|
|
@@ -4724,13 +4767,11 @@
|
|
|
4724
4767
|
}
|
|
4725
4768
|
|
|
4726
4769
|
// src/face.ts
|
|
4727
|
-
var WASM_DIR2 = "/mediakit/mp";
|
|
4728
|
-
var MODEL_URL2 = "/mediakit/face_landmarker.task";
|
|
4729
4770
|
var FACE_FLOATS = 478 * 3;
|
|
4730
4771
|
async function createFaceDetector(canvas, maxFaces, delegate = "GPU") {
|
|
4731
|
-
const
|
|
4732
|
-
const landmarker = await yc.createFromOptions(
|
|
4733
|
-
baseOptions: { modelAssetPath:
|
|
4772
|
+
const fileset2 = await visionFileset();
|
|
4773
|
+
const landmarker = await yc.createFromOptions(fileset2, {
|
|
4774
|
+
baseOptions: { modelAssetPath: MODEL_URL.face, delegate },
|
|
4734
4775
|
canvas,
|
|
4735
4776
|
runningMode: "VIDEO",
|
|
4736
4777
|
// As many as the core tracks: one landmark pass returns them all, so the cost of a second
|
|
@@ -4764,7 +4805,6 @@
|
|
|
4764
4805
|
|
|
4765
4806
|
// src/detect-remote.ts
|
|
4766
4807
|
var DETECT_WORKER_URL = "/mediakit/detect-worker.js";
|
|
4767
|
-
var READY_MS = 2e4;
|
|
4768
4808
|
var DetectWorker = class {
|
|
4769
4809
|
constructor(on2) {
|
|
4770
4810
|
this.on = on2;
|
|
@@ -4813,7 +4853,7 @@
|
|
|
4813
4853
|
reject(new Error("detect worker gone"));
|
|
4814
4854
|
return;
|
|
4815
4855
|
}
|
|
4816
|
-
const timer = setTimeout(() => this.settle(kind, new
|
|
4856
|
+
const timer = setTimeout(() => this.settle(kind, new LoadTimeout(`${kind} detection`, LOAD_MS)), LOAD_MS);
|
|
4817
4857
|
this.ready.set(kind, { resolve, reject, timer });
|
|
4818
4858
|
this.send(m2);
|
|
4819
4859
|
});
|
|
@@ -4934,6 +4974,68 @@
|
|
|
4934
4974
|
return [lo2, hi2 | 0];
|
|
4935
4975
|
}
|
|
4936
4976
|
|
|
4977
|
+
// src/gpu-guard.ts
|
|
4978
|
+
var KEY = "mediakit-gpu-guard";
|
|
4979
|
+
var TTL_MS = 3 * 24 * 3600 * 1e3;
|
|
4980
|
+
function storage() {
|
|
4981
|
+
try {
|
|
4982
|
+
return typeof localStorage === "undefined" ? null : localStorage;
|
|
4983
|
+
} catch {
|
|
4984
|
+
return null;
|
|
4985
|
+
}
|
|
4986
|
+
}
|
|
4987
|
+
function load() {
|
|
4988
|
+
const s2 = storage();
|
|
4989
|
+
if (!s2) return {};
|
|
4990
|
+
try {
|
|
4991
|
+
const parsed = JSON.parse(s2.getItem(KEY) ?? "{}");
|
|
4992
|
+
return parsed && typeof parsed === "object" ? parsed : {};
|
|
4993
|
+
} catch {
|
|
4994
|
+
return {};
|
|
4995
|
+
}
|
|
4996
|
+
}
|
|
4997
|
+
function save(store) {
|
|
4998
|
+
try {
|
|
4999
|
+
storage()?.setItem(KEY, JSON.stringify(store));
|
|
5000
|
+
} catch {
|
|
5001
|
+
return;
|
|
5002
|
+
}
|
|
5003
|
+
}
|
|
5004
|
+
function gpuRecord(gpu) {
|
|
5005
|
+
if (!gpu) return null;
|
|
5006
|
+
const r2 = load()[gpu];
|
|
5007
|
+
if (!r2 || typeof r2.at !== "number" || Date.now() - r2.at > TTL_MS) return null;
|
|
5008
|
+
return { init: r2.init === true, faceColor: r2.faceColor === true, at: r2.at };
|
|
5009
|
+
}
|
|
5010
|
+
function recordLoss(gpu, phase) {
|
|
5011
|
+
if (!gpu || phase === "run" || !storage()) return;
|
|
5012
|
+
const store = load();
|
|
5013
|
+
const was = gpuRecord(gpu);
|
|
5014
|
+
store[gpu] = {
|
|
5015
|
+
init: phase === "init" || (was?.init ?? false),
|
|
5016
|
+
faceColor: phase === "link" || (was?.faceColor ?? false),
|
|
5017
|
+
at: Date.now()
|
|
5018
|
+
};
|
|
5019
|
+
save(store);
|
|
5020
|
+
}
|
|
5021
|
+
function rendererOf(gl) {
|
|
5022
|
+
if (gl.isContextLost()) return "";
|
|
5023
|
+
const info = gl.getExtension("WEBGL_debug_renderer_info");
|
|
5024
|
+
return String(gl.getParameter(info ? info.UNMASKED_RENDERER_WEBGL : gl.RENDERER) ?? "");
|
|
5025
|
+
}
|
|
5026
|
+
var GpuLostError = class extends Error {
|
|
5027
|
+
constructor(phase, gpu, detail = "") {
|
|
5028
|
+
super(`mediakit: the WebGL context was lost ${phase === "init" ? "building the shaders" : phase === "link" ? "linking a makeup shader" : "while running"}${detail ? ` (${detail})` : ""} [GPU: ${gpu || "unknown"}]`);
|
|
5029
|
+
this.phase = phase;
|
|
5030
|
+
this.gpu = gpu;
|
|
5031
|
+
this.detail = detail;
|
|
5032
|
+
this.name = "GpuLostError";
|
|
5033
|
+
}
|
|
5034
|
+
phase;
|
|
5035
|
+
gpu;
|
|
5036
|
+
detail;
|
|
5037
|
+
};
|
|
5038
|
+
|
|
4937
5039
|
// src/recipe.ts
|
|
4938
5040
|
var DEBUG_VIEWS = ["off", "skin", "mask", "face"];
|
|
4939
5041
|
function debugCode(view) {
|
|
@@ -4948,6 +5050,7 @@
|
|
|
4948
5050
|
tint: 0,
|
|
4949
5051
|
vibrance: 0,
|
|
4950
5052
|
grain: 0,
|
|
5053
|
+
sharpen: 0,
|
|
4951
5054
|
autoframe: false,
|
|
4952
5055
|
teeth: 0,
|
|
4953
5056
|
eyeBright: 0,
|
|
@@ -4967,6 +5070,8 @@
|
|
|
4967
5070
|
whiten: 0,
|
|
4968
5071
|
skinLight: 0,
|
|
4969
5072
|
clarity: 0,
|
|
5073
|
+
hair: 0,
|
|
5074
|
+
hairColor: [0.45, 0.22, 0.12],
|
|
4970
5075
|
skinScope: "person",
|
|
4971
5076
|
lut: "",
|
|
4972
5077
|
grade: 0,
|
|
@@ -5012,7 +5117,7 @@
|
|
|
5012
5117
|
}
|
|
5013
5118
|
}
|
|
5014
5119
|
function needsMask(f2, e2) {
|
|
5015
|
-
return f2.kind !== "none" || e2.beautify > 0 || e2.autoframe || hasSkin(e2) || e2.grade > 0 && (e2.gradeScope ?? "frame") !== "frame";
|
|
5120
|
+
return f2.kind !== "none" || e2.beautify > 0 || e2.autoframe || hasSkin(e2) || e2.grade > 0 && (e2.gradeScope ?? "frame") !== "frame" || (e2.hair ?? 0) > 0;
|
|
5016
5121
|
}
|
|
5017
5122
|
function hasSkin(e2) {
|
|
5018
5123
|
return (e2.whiten ?? 0) > 0 || (e2.skinLight ?? 0) > 0 || (e2.clarity ?? 0) > 0;
|
|
@@ -5021,7 +5126,7 @@
|
|
|
5021
5126
|
return needsMask(f2, e2) && !e2.chromaKey;
|
|
5022
5127
|
}
|
|
5023
5128
|
function needsSkinClasses(_f, e2) {
|
|
5024
|
-
return e2.grade > 0 && (e2.gradeScope ?? "frame") !== "frame";
|
|
5129
|
+
return e2.grade > 0 && (e2.gradeScope ?? "frame") !== "frame" || (e2.hair ?? 0) > 0;
|
|
5025
5130
|
}
|
|
5026
5131
|
function needsFace(e2) {
|
|
5027
5132
|
return e2.teeth > 0 || e2.eyeBright > 0 || e2.underEye > 0 || e2.smileLines > 0 || e2.lips > 0 || e2.blush > 0 || e2.iris > 0 || (e2.eyeShadow ?? 0) > 0 || (e2.lipGloss ?? 0) > 0 || (e2.rosy ?? 0) > 0 || e2.skinScope === "face" && (e2.beautify > 0 || hasSkin(e2)) || // Whitening and skin light measure skin on the face; clarity is held to it.
|
|
@@ -5039,6 +5144,7 @@
|
|
|
5039
5144
|
tint: e2.tint ?? 0,
|
|
5040
5145
|
vibrance: e2.vibrance ?? 0,
|
|
5041
5146
|
grain: e2.grain ?? 0,
|
|
5147
|
+
sharpen: e2.sharpen ?? 0,
|
|
5042
5148
|
autoframe: e2.autoframe,
|
|
5043
5149
|
teeth: e2.teeth,
|
|
5044
5150
|
eyeBright: e2.eyeBright,
|
|
@@ -5058,6 +5164,8 @@
|
|
|
5058
5164
|
whiten: e2.whiten ?? 0,
|
|
5059
5165
|
skinLight: e2.skinLight ?? 0,
|
|
5060
5166
|
clarity: e2.clarity ?? 0,
|
|
5167
|
+
hair: e2.hair ?? 0,
|
|
5168
|
+
hairColor: e2.hairColor ?? NO_EFFECTS.hairColor,
|
|
5061
5169
|
skinScope: SKIN_SCOPE[e2.skinScope] ?? 0,
|
|
5062
5170
|
grade: e2.grade,
|
|
5063
5171
|
gradeScope: GRADE_SCOPE[e2.gradeScope] ?? 0,
|
|
@@ -5103,17 +5211,25 @@
|
|
|
5103
5211
|
faces: 0,
|
|
5104
5212
|
heldFrames: 0,
|
|
5105
5213
|
faceAgeMs: 0,
|
|
5106
|
-
maskAgeMs: 0
|
|
5214
|
+
maskAgeMs: 0,
|
|
5215
|
+
effectError: ""
|
|
5107
5216
|
};
|
|
5108
5217
|
var MAX_HOLD = 2;
|
|
5218
|
+
var LINK_LOSS_WINDOW_MS = 3e3;
|
|
5109
5219
|
var DEPTH_VOTES = 15;
|
|
5110
5220
|
var DETECT_DUTY = 0.6;
|
|
5221
|
+
function gpuName(gl) {
|
|
5222
|
+
const info = gl.getExtension("WEBGL_debug_renderer_info");
|
|
5223
|
+
const name = String(gl.getParameter(info ? info.UNMASKED_RENDERER_WEBGL : gl.RENDERER) ?? "unknown");
|
|
5224
|
+
return gl.isContextLost() ? `${name}, context lost` : name;
|
|
5225
|
+
}
|
|
5111
5226
|
function sourceSize(source) {
|
|
5112
5227
|
if (source instanceof VideoFrame) return [source.displayWidth, source.displayHeight];
|
|
5113
5228
|
if (typeof HTMLVideoElement !== "undefined" && source instanceof HTMLVideoElement) return [source.videoWidth, source.videoHeight];
|
|
5114
5229
|
const s2 = source;
|
|
5115
5230
|
return [s2.width, s2.height];
|
|
5116
5231
|
}
|
|
5232
|
+
var FACE_COLOR_SKIPPED = "face colour shaders skipped: linking one lost the WebGL context on this GPU before";
|
|
5117
5233
|
var Engine = class _Engine {
|
|
5118
5234
|
canvas;
|
|
5119
5235
|
gl;
|
|
@@ -5144,6 +5260,14 @@
|
|
|
5144
5260
|
classLoading = null;
|
|
5145
5261
|
/** The segmenter whose result this frame borrowed, to give it back after process(). */
|
|
5146
5262
|
used = null;
|
|
5263
|
+
programErr = "";
|
|
5264
|
+
loadErr = "";
|
|
5265
|
+
gpu = "";
|
|
5266
|
+
linkSeenAt = -Infinity;
|
|
5267
|
+
links = 0;
|
|
5268
|
+
lostPhase = null;
|
|
5269
|
+
classFailed = false;
|
|
5270
|
+
failed = /* @__PURE__ */ new Set();
|
|
5147
5271
|
/**
|
|
5148
5272
|
* The landmark detector, also lazy and independent of the segmenter.
|
|
5149
5273
|
*
|
|
@@ -5203,19 +5327,20 @@
|
|
|
5203
5327
|
this.core = core;
|
|
5204
5328
|
}
|
|
5205
5329
|
/** Main thread: makes its own canvas. A worker calls `on` with an OffscreenCanvas. */
|
|
5206
|
-
static async create(width, height, filter, effects = NO_EFFECTS, alpha = false, maxFaces = 3) {
|
|
5330
|
+
static async create(width, height, filter, effects = NO_EFFECTS, alpha = false, maxFaces = 3, guard = {}) {
|
|
5207
5331
|
const canvas = document.createElement("canvas");
|
|
5208
5332
|
canvas.width = width;
|
|
5209
5333
|
canvas.height = height;
|
|
5210
|
-
return _Engine.on(canvas, filter, effects, void 0, alpha, maxFaces);
|
|
5334
|
+
return _Engine.on(canvas, filter, effects, void 0, alpha, maxFaces, guard);
|
|
5211
5335
|
}
|
|
5212
5336
|
/**
|
|
5213
5337
|
* `background`: see {@link Engine.setRecipe}. `alpha` keeps the output's alpha channel, which
|
|
5214
5338
|
* only a `cutout` background writes; it is fixed for the life of the context, so a host that
|
|
5215
5339
|
* may switch to `cutout` later has to ask for it here. `maxFaces` (1-3) is how many faces the
|
|
5216
|
-
* landmarker looks for: below 3 it skips re-running its face detector on every call.
|
|
5340
|
+
* landmarker looks for: below 3 it skips re-running its face detector on every call. Throws
|
|
5341
|
+
* GpuLostError when the context is lost building the shaders, or this GPU is remembered for it.
|
|
5217
5342
|
*/
|
|
5218
|
-
static async on(canvas, filter, effects = NO_EFFECTS, background, alpha = false, maxFaces = 3) {
|
|
5343
|
+
static async on(canvas, filter, effects = NO_EFFECTS, background, alpha = false, maxFaces = 3, guard = {}) {
|
|
5219
5344
|
const width = canvas.width;
|
|
5220
5345
|
const height = canvas.height;
|
|
5221
5346
|
const gl = canvas.getContext("webgl2", {
|
|
@@ -5225,14 +5350,26 @@
|
|
|
5225
5350
|
depth: false,
|
|
5226
5351
|
stencil: false
|
|
5227
5352
|
});
|
|
5228
|
-
if (!gl) throw new Error("mediakit: no WebGL2 in this browser");
|
|
5353
|
+
if (!gl) throw new Error("mediakit: no WebGL2 in this browser (none available, blocked, or the GPU process was reset)");
|
|
5354
|
+
const gpu = rendererOf(gl);
|
|
5355
|
+
const known = gpuRecord(gpu);
|
|
5356
|
+
if (known?.init) {
|
|
5357
|
+
gl.getExtension("WEBGL_lose_context")?.loseContext();
|
|
5358
|
+
throw new GpuLostError("init", gpu, `remembered from ${new Date(known.at).toISOString()}, forgetGpuFailures() retries`);
|
|
5359
|
+
}
|
|
5229
5360
|
let core;
|
|
5230
5361
|
let engine2;
|
|
5231
5362
|
try {
|
|
5232
5363
|
core = await loadCore();
|
|
5233
5364
|
const err = core.init(gl, width, height);
|
|
5234
|
-
if (err
|
|
5365
|
+
if (err && gl.isContextLost()) {
|
|
5366
|
+
recordLoss(gpu, "init");
|
|
5367
|
+
throw new GpuLostError("init", gpu, err);
|
|
5368
|
+
}
|
|
5369
|
+
if (err) throw new Error(`mediakit: ${err} [GPU: ${gpuName(gl)}]`);
|
|
5235
5370
|
engine2 = new _Engine(canvas, gl, core);
|
|
5371
|
+
engine2.gpu = gpu;
|
|
5372
|
+
if (guard.noFaceColor || known?.faceColor) core.disableFaceColor?.(FACE_COLOR_SKIPPED);
|
|
5236
5373
|
engine2.maxFaces = Math.min(3, Math.max(1, Math.floor(maxFaces) || 3));
|
|
5237
5374
|
await engine2.setRecipe(filter, effects, background);
|
|
5238
5375
|
return engine2;
|
|
@@ -5248,9 +5385,29 @@
|
|
|
5248
5385
|
throw err;
|
|
5249
5386
|
}
|
|
5250
5387
|
}
|
|
5388
|
+
/** The GPU the context reported at creation, "" when the browser hides it. */
|
|
5389
|
+
get gpuName() {
|
|
5390
|
+
return this.gpu;
|
|
5391
|
+
}
|
|
5251
5392
|
get loading() {
|
|
5252
5393
|
return this.segmenterLoading !== null || this.classLoading !== null || this.faceLoading !== null;
|
|
5253
5394
|
}
|
|
5395
|
+
/** The browser took the WebGL context away (GPU process reset or reclaimed); this Engine is done. */
|
|
5396
|
+
get lost() {
|
|
5397
|
+
return this.gl.isContextLost();
|
|
5398
|
+
}
|
|
5399
|
+
/**
|
|
5400
|
+
* When the context was lost: "link" within 3 s of a face colour shader starting or still linking, else "run";
|
|
5401
|
+
* null while it is not. The first read decides, and on the main thread it is remembered per GPU.
|
|
5402
|
+
*/
|
|
5403
|
+
get lostDuring() {
|
|
5404
|
+
if (this.closed || !this.gl.isContextLost()) return null;
|
|
5405
|
+
if (!this.lostPhase) {
|
|
5406
|
+
this.lostPhase = performance.now() - this.linkSeenAt < LINK_LOSS_WINDOW_MS ? "link" : "run";
|
|
5407
|
+
recordLoss(this.gpu, this.lostPhase);
|
|
5408
|
+
}
|
|
5409
|
+
return this.lostPhase;
|
|
5410
|
+
}
|
|
5254
5411
|
get outputCanvas() {
|
|
5255
5412
|
return this.canvas;
|
|
5256
5413
|
}
|
|
@@ -5337,13 +5494,29 @@
|
|
|
5337
5494
|
this.makeupPath = makeupTexture;
|
|
5338
5495
|
}
|
|
5339
5496
|
this.core.setRecipe(coreRecipe(filter, effects));
|
|
5340
|
-
|
|
5497
|
+
this.noteLinking();
|
|
5498
|
+
if (needsSegmentation(filter, effects) && !this.remoteMaskComing(filter, effects)) void this.ensureSegmenter();
|
|
5341
5499
|
if (needsSkinClasses(filter, effects)) void this.ensureClassSegmenter();
|
|
5342
5500
|
if (this.personRemoteWanted(filter, effects)) void this.ensurePersonRemote();
|
|
5343
5501
|
if (needsFace(effects)) void this.ensureFace();
|
|
5344
5502
|
}
|
|
5503
|
+
noteLinking() {
|
|
5504
|
+
const links = this.core.links?.() ?? 0;
|
|
5505
|
+
if (links !== this.links || (this.core.linking?.() ?? 0) > 0) this.linkSeenAt = performance.now();
|
|
5506
|
+
this.links = links;
|
|
5507
|
+
}
|
|
5508
|
+
/** A worker, or the multiclass model, will bring the mask: no in-thread person model for it. */
|
|
5509
|
+
remoteMaskComing(filter, effects) {
|
|
5510
|
+
if (needsSkinClasses(filter, effects) && !this.classFailed) return true;
|
|
5511
|
+
return this.personRemoteWanted(filter, effects) && !this.inThread.has("person");
|
|
5512
|
+
}
|
|
5513
|
+
gaveUp(what, e2) {
|
|
5514
|
+
if (!(e2 instanceof LoadTimeout)) return;
|
|
5515
|
+
this.loadErr ||= `${e2.message}; ${what} stay off`;
|
|
5516
|
+
console.warn(this.loadErr);
|
|
5517
|
+
}
|
|
5345
5518
|
ensureFace() {
|
|
5346
|
-
if (this.face || this.faceRemote || this.closed) return Promise.resolve();
|
|
5519
|
+
if (this.face || this.faceRemote || this.failed.has("face") || this.closed) return Promise.resolve();
|
|
5347
5520
|
if (this.faceLoading) return this.faceLoading;
|
|
5348
5521
|
const buf = this.core.faceBuffer();
|
|
5349
5522
|
if (!buf) return Promise.resolve();
|
|
@@ -5352,8 +5525,9 @@
|
|
|
5352
5525
|
this.faceRemote = true;
|
|
5353
5526
|
this.lmBuf = buf;
|
|
5354
5527
|
}).catch((e2) => {
|
|
5528
|
+
if (e2 instanceof LoadTimeout) throw e2;
|
|
5355
5529
|
if (!this.inThread.has("face")) console.warn("mediakit: no detect worker for faces, detecting in this thread", e2);
|
|
5356
|
-
return createFaceDetector(this.canvas, n2).then((f2) => {
|
|
5530
|
+
return withTimeout(createFaceDetector(this.canvas, n2), LOAD_MS, "face detection", (f2) => f2.close()).then((f2) => {
|
|
5357
5531
|
if (this.closed) {
|
|
5358
5532
|
f2.close();
|
|
5359
5533
|
return;
|
|
@@ -5362,7 +5536,9 @@
|
|
|
5362
5536
|
this.lmBuf = buf;
|
|
5363
5537
|
});
|
|
5364
5538
|
}).catch((e2) => {
|
|
5365
|
-
|
|
5539
|
+
this.failed.add("face");
|
|
5540
|
+
this.gaveUp("face effects", e2);
|
|
5541
|
+
if (!(e2 instanceof LoadTimeout)) console.error("mediakit: face detector failed to load", e2);
|
|
5366
5542
|
}).finally(() => {
|
|
5367
5543
|
this.faceLoading = null;
|
|
5368
5544
|
});
|
|
@@ -5373,10 +5549,13 @@
|
|
|
5373
5549
|
return filter.kind === "none" && needsSegmentation(filter, effects) && !needsSkinClasses(filter, effects);
|
|
5374
5550
|
}
|
|
5375
5551
|
ensurePersonRemote() {
|
|
5376
|
-
if (this.personRemote || this.closed) return Promise.resolve();
|
|
5552
|
+
if (this.personRemote || this.failed.has("person") || this.closed) return Promise.resolve();
|
|
5377
5553
|
this.personLoading ??= this.remote("person", (d2) => d2.initMask("person")).then(() => {
|
|
5378
5554
|
this.personRemote = true;
|
|
5379
|
-
}).catch(() => {
|
|
5555
|
+
}).catch((e2) => {
|
|
5556
|
+
if (!(e2 instanceof LoadTimeout)) return;
|
|
5557
|
+
this.failed.add("person");
|
|
5558
|
+
this.gaveUp("background effects", e2);
|
|
5380
5559
|
}).finally(() => {
|
|
5381
5560
|
this.personLoading = null;
|
|
5382
5561
|
});
|
|
@@ -5384,6 +5563,7 @@
|
|
|
5384
5563
|
}
|
|
5385
5564
|
async remote(kind, init) {
|
|
5386
5565
|
if (this.inThread.has(kind) || typeof Worker === "undefined" || typeof VideoFrame === "undefined") {
|
|
5566
|
+
this.inThread.add(kind);
|
|
5387
5567
|
throw new Error("in-thread");
|
|
5388
5568
|
}
|
|
5389
5569
|
let detect = this.detects.get(kind);
|
|
@@ -5394,7 +5574,10 @@
|
|
|
5394
5574
|
try {
|
|
5395
5575
|
await init(detect);
|
|
5396
5576
|
} catch (e2) {
|
|
5397
|
-
|
|
5577
|
+
if (e2 instanceof LoadTimeout) {
|
|
5578
|
+
detect.close();
|
|
5579
|
+
this.detects.delete(kind);
|
|
5580
|
+
} else this.inThread.add(kind);
|
|
5398
5581
|
throw e2;
|
|
5399
5582
|
}
|
|
5400
5583
|
if (this.closed) throw new Error("closed");
|
|
@@ -5506,36 +5689,42 @@
|
|
|
5506
5689
|
return this.detectCanvas;
|
|
5507
5690
|
}
|
|
5508
5691
|
ensureClassSegmenter() {
|
|
5509
|
-
if (this.classSegmenter || this.classRemote || this.closed) return Promise.resolve();
|
|
5692
|
+
if (this.classSegmenter || this.classRemote || this.classFailed || this.closed) return Promise.resolve();
|
|
5510
5693
|
if (this.classLoading) return this.classLoading;
|
|
5511
5694
|
this.classLoading = this.remote("mask", (d2) => d2.initMask("classes")).then(() => {
|
|
5512
5695
|
this.classRemote = true;
|
|
5513
|
-
}).catch(
|
|
5514
|
-
(
|
|
5696
|
+
}).catch((e2) => {
|
|
5697
|
+
if (e2 instanceof LoadTimeout) throw e2;
|
|
5698
|
+
return withTimeout(createSegmenter(this.canvas, "classes"), LOAD_MS, "skin segmentation", (s2) => s2.close()).then((s2) => {
|
|
5515
5699
|
if (this.closed) {
|
|
5516
5700
|
s2.close();
|
|
5517
5701
|
return;
|
|
5518
5702
|
}
|
|
5519
5703
|
this.classSegmenter = s2;
|
|
5520
|
-
})
|
|
5521
|
-
).catch((e2) => {
|
|
5522
|
-
|
|
5704
|
+
});
|
|
5705
|
+
}).catch((e2) => {
|
|
5706
|
+
this.classFailed = true;
|
|
5707
|
+
if (!(e2 instanceof LoadTimeout)) console.error("mediakit: multiclass segmenter failed to load", e2);
|
|
5708
|
+
else console.warn(`${e2.message}; the person model stands in`);
|
|
5709
|
+
if (needsSegmentation(this.filter, this.effects)) void this.ensureSegmenter();
|
|
5523
5710
|
}).finally(() => {
|
|
5524
5711
|
this.classLoading = null;
|
|
5525
5712
|
});
|
|
5526
5713
|
return this.classLoading;
|
|
5527
5714
|
}
|
|
5528
5715
|
ensureSegmenter() {
|
|
5529
|
-
if (this.segmenter || this.closed) return Promise.resolve();
|
|
5716
|
+
if (this.segmenter || this.failed.has("segmenter") || this.closed) return Promise.resolve();
|
|
5530
5717
|
if (this.segmenterLoading) return this.segmenterLoading;
|
|
5531
|
-
this.segmenterLoading = createSegmenter(this.canvas).then((s2) => {
|
|
5718
|
+
this.segmenterLoading = withTimeout(createSegmenter(this.canvas), LOAD_MS, "person segmentation", (s2) => s2.close()).then((s2) => {
|
|
5532
5719
|
if (this.closed) {
|
|
5533
5720
|
s2.close();
|
|
5534
5721
|
return;
|
|
5535
5722
|
}
|
|
5536
5723
|
this.segmenter = s2;
|
|
5537
5724
|
}).catch((e2) => {
|
|
5538
|
-
|
|
5725
|
+
this.failed.add("segmenter");
|
|
5726
|
+
this.gaveUp("background and skin effects", e2);
|
|
5727
|
+
if (!(e2 instanceof LoadTimeout)) console.error("mediakit: segmenter failed to load", e2);
|
|
5539
5728
|
}).finally(() => {
|
|
5540
5729
|
this.segmenterLoading = null;
|
|
5541
5730
|
});
|
|
@@ -5554,7 +5743,7 @@
|
|
|
5554
5743
|
* it constructs one only to destroy it again.
|
|
5555
5744
|
*/
|
|
5556
5745
|
render(source, timestampUs) {
|
|
5557
|
-
if (this.closed) return false;
|
|
5746
|
+
if (this.closed || this.lostDuring) return false;
|
|
5558
5747
|
if (this.lastHeld) this.spare.push(this.lastHeld);
|
|
5559
5748
|
this.lastHeld = null;
|
|
5560
5749
|
this.swallowed = false;
|
|
@@ -5673,7 +5862,7 @@
|
|
|
5673
5862
|
const classes = this.classSegmenter !== null && needsSkinClasses(this.filter, this.effects);
|
|
5674
5863
|
const seg = classes ? this.classSegmenter : this.segmenter;
|
|
5675
5864
|
if (!seg) {
|
|
5676
|
-
void this.ensureSegmenter();
|
|
5865
|
+
if (!this.remoteMaskComing(this.filter, this.effects)) void this.ensureSegmenter();
|
|
5677
5866
|
} else {
|
|
5678
5867
|
const s0 = performance.now();
|
|
5679
5868
|
mask = seg.segment(source, timestampUs / 1e3);
|
|
@@ -5705,6 +5894,14 @@
|
|
|
5705
5894
|
const glCalls = this.core.process(source, mask, this.background, maskKind, maskLo, maskHi);
|
|
5706
5895
|
if (mask) this.used?.release();
|
|
5707
5896
|
this.used = null;
|
|
5897
|
+
this.noteLinking();
|
|
5898
|
+
if (!this.programErr) {
|
|
5899
|
+
const err = this.core.programError?.() ?? "";
|
|
5900
|
+
if (err) {
|
|
5901
|
+
this.programErr = `mediakit: ${err}; makeup and teeth stay off, the rest runs [GPU: ${gpuName(this.gl)}]`;
|
|
5902
|
+
console.warn(this.programErr);
|
|
5903
|
+
}
|
|
5904
|
+
}
|
|
5708
5905
|
this.prevSegMs = segMs;
|
|
5709
5906
|
this.prevTotalMs = performance.now() - t0;
|
|
5710
5907
|
const s2 = this.core.stats();
|
|
@@ -5727,7 +5924,8 @@
|
|
|
5727
5924
|
faces: s2 ? s2[12] : 0,
|
|
5728
5925
|
heldFrames: faceDone ? this.holdDepth : 0,
|
|
5729
5926
|
faceAgeMs: s2 && s2[10] > 0.5 && this.faceTs >= 0 ? (timestampUs - this.faceTs) / 1e3 : 0,
|
|
5730
|
-
maskAgeMs: (needSeg || remoteMask !== null) && this.maskUsedTs >= 0 ? (timestampUs - this.maskUsedTs) / 1e3 : 0
|
|
5927
|
+
maskAgeMs: (needSeg || remoteMask !== null) && this.maskUsedTs >= 0 ? (timestampUs - this.maskUsedTs) / 1e3 : 0,
|
|
5928
|
+
effectError: this.programErr || this.loadErr
|
|
5731
5929
|
};
|
|
5732
5930
|
return glCalls > 0;
|
|
5733
5931
|
}
|
|
@@ -5828,6 +6026,8 @@
|
|
|
5828
6026
|
var post = (m2) => self.postMessage(m2);
|
|
5829
6027
|
var engine = null;
|
|
5830
6028
|
var abort = null;
|
|
6029
|
+
var repipe = null;
|
|
6030
|
+
var inTrack = null;
|
|
5831
6031
|
var framesOn = false;
|
|
5832
6032
|
async function start(m2) {
|
|
5833
6033
|
await run(m2, m2.readable, m2.writable);
|
|
@@ -5838,21 +6038,27 @@
|
|
|
5838
6038
|
m2.track.stop();
|
|
5839
6039
|
throw new Error("mediakit: this worker has no MediaStreamTrackProcessor / VideoTrackGenerator");
|
|
5840
6040
|
}
|
|
6041
|
+
inTrack = m2.track;
|
|
5841
6042
|
const processor = new g2.MediaStreamTrackProcessor({ track: m2.track });
|
|
5842
6043
|
const generator = new g2.VideoTrackGenerator();
|
|
5843
6044
|
await run(m2, processor.readable, generator.writable, generator.track);
|
|
5844
6045
|
}
|
|
5845
6046
|
async function run(m2, readable, writable, outTrack) {
|
|
5846
6047
|
const canvas = new OffscreenCanvas(m2.width, m2.height);
|
|
5847
|
-
engine = await Engine.on(canvas, m2.filter, m2.effects, m2.background, m2.alpha ?? false, m2.maxFaces);
|
|
6048
|
+
engine = await Engine.on(canvas, m2.filter, m2.effects, m2.background, m2.alpha ?? false, m2.maxFaces, { noFaceColor: m2.noFaceColor });
|
|
5848
6049
|
engine.syncFaces = m2.syncFaces ?? false;
|
|
5849
6050
|
engine.setDebug(m2.debug ?? "off");
|
|
5850
|
-
abort = new AbortController();
|
|
5851
6051
|
let n2 = 0;
|
|
5852
6052
|
let lastPost = performance.now();
|
|
5853
6053
|
let lastLoading = engine.loading;
|
|
5854
|
-
|
|
6054
|
+
let lostSent = false;
|
|
6055
|
+
const makeTransform = () => new TransformStream({
|
|
5855
6056
|
transform: (frame, controller) => {
|
|
6057
|
+
const phase = engine?.lostDuring;
|
|
6058
|
+
if (phase && !lostSent) {
|
|
6059
|
+
lostSent = true;
|
|
6060
|
+
post({ t: "lost", phase, gpu: engine?.gpuName ?? "" });
|
|
6061
|
+
}
|
|
5856
6062
|
const ts2 = frame.timestamp ?? 0;
|
|
5857
6063
|
n2++;
|
|
5858
6064
|
const now = performance.now();
|
|
@@ -5904,14 +6110,41 @@
|
|
|
5904
6110
|
} else {
|
|
5905
6111
|
post({ t: "ready", loading: engine.loading });
|
|
5906
6112
|
}
|
|
5907
|
-
|
|
5908
|
-
|
|
5909
|
-
|
|
5910
|
-
|
|
6113
|
+
let piping = Promise.resolve();
|
|
6114
|
+
repipe = (input) => {
|
|
6115
|
+
abort?.abort();
|
|
6116
|
+
const mine = new AbortController();
|
|
6117
|
+
abort = mine;
|
|
6118
|
+
piping = piping.then(() => {
|
|
6119
|
+
if (mine.signal.aborted) {
|
|
6120
|
+
void input.cancel();
|
|
6121
|
+
return;
|
|
6122
|
+
}
|
|
6123
|
+
return input.pipeThrough(makeTransform(), { signal: mine.signal }).pipeTo(writable, { signal: mine.signal, preventAbort: true, preventClose: true }).catch((e2) => {
|
|
6124
|
+
if (mine.signal.aborted) return;
|
|
6125
|
+
post({ t: "error", message: e2 instanceof Error ? e2.message : String(e2) });
|
|
6126
|
+
});
|
|
6127
|
+
});
|
|
6128
|
+
};
|
|
6129
|
+
repipe(readable);
|
|
6130
|
+
}
|
|
6131
|
+
function switchTrack(track) {
|
|
6132
|
+
const g2 = globalThis;
|
|
6133
|
+
if (!g2.MediaStreamTrackProcessor || !repipe) {
|
|
6134
|
+
track.stop();
|
|
6135
|
+
return;
|
|
6136
|
+
}
|
|
6137
|
+
const previous = inTrack;
|
|
6138
|
+
inTrack = track;
|
|
6139
|
+
repipe(new g2.MediaStreamTrackProcessor({ track }).readable);
|
|
6140
|
+
previous?.stop();
|
|
5911
6141
|
}
|
|
5912
6142
|
function stop() {
|
|
5913
6143
|
abort?.abort();
|
|
5914
6144
|
abort = null;
|
|
6145
|
+
repipe = null;
|
|
6146
|
+
inTrack?.stop();
|
|
6147
|
+
inTrack = null;
|
|
5915
6148
|
engine?.close();
|
|
5916
6149
|
engine = null;
|
|
5917
6150
|
}
|
|
@@ -5920,10 +6153,16 @@
|
|
|
5920
6153
|
try {
|
|
5921
6154
|
if (m2.t === "start" || m2.t === "startTrack") {
|
|
5922
6155
|
void (m2.t === "start" ? start(m2) : startTrack(m2)).catch((err) => {
|
|
5923
|
-
post({ t: "
|
|
6156
|
+
if (err instanceof GpuLostError) post({ t: "lost", phase: err.phase, gpu: err.gpu, message: err.detail });
|
|
6157
|
+
else post({ t: "error", message: err instanceof Error ? err.message : String(err) });
|
|
5924
6158
|
});
|
|
5925
6159
|
} else if (m2.t === "frames") {
|
|
5926
6160
|
framesOn = m2.on;
|
|
6161
|
+
} else if (m2.t === "source") {
|
|
6162
|
+
if (repipe) repipe(m2.readable);
|
|
6163
|
+
else void m2.readable.cancel();
|
|
6164
|
+
} else if (m2.t === "sourceTrack") {
|
|
6165
|
+
switchTrack(m2.track);
|
|
5927
6166
|
} else if (m2.t === "debug") {
|
|
5928
6167
|
engine?.setDebug(m2.view);
|
|
5929
6168
|
} else if (m2.t === "recipe") {
|