@tradalab/mediakit 2.4.0 → 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 +116 -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 +119 -5
- package/dist/index.js +471 -64
- 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 +299 -49
- package/dist/worker.js.map +1 -1
- package/package.json +11 -9
- package/dist/chunk-YW75KJJO.js.map +0 -1
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,16 +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;
|
|
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
|
+
}
|
|
5110
5226
|
function sourceSize(source) {
|
|
5111
5227
|
if (source instanceof VideoFrame) return [source.displayWidth, source.displayHeight];
|
|
5112
5228
|
if (typeof HTMLVideoElement !== "undefined" && source instanceof HTMLVideoElement) return [source.videoWidth, source.videoHeight];
|
|
5113
5229
|
const s2 = source;
|
|
5114
5230
|
return [s2.width, s2.height];
|
|
5115
5231
|
}
|
|
5232
|
+
var FACE_COLOR_SKIPPED = "face colour shaders skipped: linking one lost the WebGL context on this GPU before";
|
|
5116
5233
|
var Engine = class _Engine {
|
|
5117
5234
|
canvas;
|
|
5118
5235
|
gl;
|
|
@@ -5143,6 +5260,14 @@
|
|
|
5143
5260
|
classLoading = null;
|
|
5144
5261
|
/** The segmenter whose result this frame borrowed, to give it back after process(). */
|
|
5145
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();
|
|
5146
5271
|
/**
|
|
5147
5272
|
* The landmark detector, also lazy and independent of the segmenter.
|
|
5148
5273
|
*
|
|
@@ -5167,6 +5292,7 @@
|
|
|
5167
5292
|
gapUs = 33333;
|
|
5168
5293
|
prevTs = -1;
|
|
5169
5294
|
sent = /* @__PURE__ */ new Map();
|
|
5295
|
+
lastSentAt = /* @__PURE__ */ new Map();
|
|
5170
5296
|
faceLatencyMs = 0;
|
|
5171
5297
|
maskLatencyMs = /* @__PURE__ */ new Map();
|
|
5172
5298
|
faceTs = -1;
|
|
@@ -5201,19 +5327,20 @@
|
|
|
5201
5327
|
this.core = core;
|
|
5202
5328
|
}
|
|
5203
5329
|
/** Main thread: makes its own canvas. A worker calls `on` with an OffscreenCanvas. */
|
|
5204
|
-
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 = {}) {
|
|
5205
5331
|
const canvas = document.createElement("canvas");
|
|
5206
5332
|
canvas.width = width;
|
|
5207
5333
|
canvas.height = height;
|
|
5208
|
-
return _Engine.on(canvas, filter, effects, void 0, alpha, maxFaces);
|
|
5334
|
+
return _Engine.on(canvas, filter, effects, void 0, alpha, maxFaces, guard);
|
|
5209
5335
|
}
|
|
5210
5336
|
/**
|
|
5211
5337
|
* `background`: see {@link Engine.setRecipe}. `alpha` keeps the output's alpha channel, which
|
|
5212
5338
|
* only a `cutout` background writes; it is fixed for the life of the context, so a host that
|
|
5213
5339
|
* may switch to `cutout` later has to ask for it here. `maxFaces` (1-3) is how many faces the
|
|
5214
|
-
* 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.
|
|
5215
5342
|
*/
|
|
5216
|
-
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 = {}) {
|
|
5217
5344
|
const width = canvas.width;
|
|
5218
5345
|
const height = canvas.height;
|
|
5219
5346
|
const gl = canvas.getContext("webgl2", {
|
|
@@ -5223,14 +5350,26 @@
|
|
|
5223
5350
|
depth: false,
|
|
5224
5351
|
stencil: false
|
|
5225
5352
|
});
|
|
5226
|
-
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
|
+
}
|
|
5227
5360
|
let core;
|
|
5228
5361
|
let engine2;
|
|
5229
5362
|
try {
|
|
5230
5363
|
core = await loadCore();
|
|
5231
5364
|
const err = core.init(gl, width, height);
|
|
5232
|
-
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)}]`);
|
|
5233
5370
|
engine2 = new _Engine(canvas, gl, core);
|
|
5371
|
+
engine2.gpu = gpu;
|
|
5372
|
+
if (guard.noFaceColor || known?.faceColor) core.disableFaceColor?.(FACE_COLOR_SKIPPED);
|
|
5234
5373
|
engine2.maxFaces = Math.min(3, Math.max(1, Math.floor(maxFaces) || 3));
|
|
5235
5374
|
await engine2.setRecipe(filter, effects, background);
|
|
5236
5375
|
return engine2;
|
|
@@ -5246,9 +5385,29 @@
|
|
|
5246
5385
|
throw err;
|
|
5247
5386
|
}
|
|
5248
5387
|
}
|
|
5388
|
+
/** The GPU the context reported at creation, "" when the browser hides it. */
|
|
5389
|
+
get gpuName() {
|
|
5390
|
+
return this.gpu;
|
|
5391
|
+
}
|
|
5249
5392
|
get loading() {
|
|
5250
5393
|
return this.segmenterLoading !== null || this.classLoading !== null || this.faceLoading !== null;
|
|
5251
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
|
+
}
|
|
5252
5411
|
get outputCanvas() {
|
|
5253
5412
|
return this.canvas;
|
|
5254
5413
|
}
|
|
@@ -5335,13 +5494,29 @@
|
|
|
5335
5494
|
this.makeupPath = makeupTexture;
|
|
5336
5495
|
}
|
|
5337
5496
|
this.core.setRecipe(coreRecipe(filter, effects));
|
|
5338
|
-
|
|
5497
|
+
this.noteLinking();
|
|
5498
|
+
if (needsSegmentation(filter, effects) && !this.remoteMaskComing(filter, effects)) void this.ensureSegmenter();
|
|
5339
5499
|
if (needsSkinClasses(filter, effects)) void this.ensureClassSegmenter();
|
|
5340
5500
|
if (this.personRemoteWanted(filter, effects)) void this.ensurePersonRemote();
|
|
5341
5501
|
if (needsFace(effects)) void this.ensureFace();
|
|
5342
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
|
+
}
|
|
5343
5518
|
ensureFace() {
|
|
5344
|
-
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();
|
|
5345
5520
|
if (this.faceLoading) return this.faceLoading;
|
|
5346
5521
|
const buf = this.core.faceBuffer();
|
|
5347
5522
|
if (!buf) return Promise.resolve();
|
|
@@ -5350,8 +5525,9 @@
|
|
|
5350
5525
|
this.faceRemote = true;
|
|
5351
5526
|
this.lmBuf = buf;
|
|
5352
5527
|
}).catch((e2) => {
|
|
5528
|
+
if (e2 instanceof LoadTimeout) throw e2;
|
|
5353
5529
|
if (!this.inThread.has("face")) console.warn("mediakit: no detect worker for faces, detecting in this thread", e2);
|
|
5354
|
-
return createFaceDetector(this.canvas, n2).then((f2) => {
|
|
5530
|
+
return withTimeout(createFaceDetector(this.canvas, n2), LOAD_MS, "face detection", (f2) => f2.close()).then((f2) => {
|
|
5355
5531
|
if (this.closed) {
|
|
5356
5532
|
f2.close();
|
|
5357
5533
|
return;
|
|
@@ -5360,7 +5536,9 @@
|
|
|
5360
5536
|
this.lmBuf = buf;
|
|
5361
5537
|
});
|
|
5362
5538
|
}).catch((e2) => {
|
|
5363
|
-
|
|
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);
|
|
5364
5542
|
}).finally(() => {
|
|
5365
5543
|
this.faceLoading = null;
|
|
5366
5544
|
});
|
|
@@ -5371,10 +5549,13 @@
|
|
|
5371
5549
|
return filter.kind === "none" && needsSegmentation(filter, effects) && !needsSkinClasses(filter, effects);
|
|
5372
5550
|
}
|
|
5373
5551
|
ensurePersonRemote() {
|
|
5374
|
-
if (this.personRemote || this.closed) return Promise.resolve();
|
|
5552
|
+
if (this.personRemote || this.failed.has("person") || this.closed) return Promise.resolve();
|
|
5375
5553
|
this.personLoading ??= this.remote("person", (d2) => d2.initMask("person")).then(() => {
|
|
5376
5554
|
this.personRemote = true;
|
|
5377
|
-
}).catch(() => {
|
|
5555
|
+
}).catch((e2) => {
|
|
5556
|
+
if (!(e2 instanceof LoadTimeout)) return;
|
|
5557
|
+
this.failed.add("person");
|
|
5558
|
+
this.gaveUp("background effects", e2);
|
|
5378
5559
|
}).finally(() => {
|
|
5379
5560
|
this.personLoading = null;
|
|
5380
5561
|
});
|
|
@@ -5382,6 +5563,7 @@
|
|
|
5382
5563
|
}
|
|
5383
5564
|
async remote(kind, init) {
|
|
5384
5565
|
if (this.inThread.has(kind) || typeof Worker === "undefined" || typeof VideoFrame === "undefined") {
|
|
5566
|
+
this.inThread.add(kind);
|
|
5385
5567
|
throw new Error("in-thread");
|
|
5386
5568
|
}
|
|
5387
5569
|
let detect = this.detects.get(kind);
|
|
@@ -5392,7 +5574,10 @@
|
|
|
5392
5574
|
try {
|
|
5393
5575
|
await init(detect);
|
|
5394
5576
|
} catch (e2) {
|
|
5395
|
-
|
|
5577
|
+
if (e2 instanceof LoadTimeout) {
|
|
5578
|
+
detect.close();
|
|
5579
|
+
this.detects.delete(kind);
|
|
5580
|
+
} else this.inThread.add(kind);
|
|
5396
5581
|
throw e2;
|
|
5397
5582
|
}
|
|
5398
5583
|
if (this.closed) throw new Error("closed");
|
|
@@ -5471,6 +5656,7 @@
|
|
|
5471
5656
|
requestRemote(kind, source, timestampUs) {
|
|
5472
5657
|
const detect = this.detects.get(kind);
|
|
5473
5658
|
if (!detect?.accepts(kind === "face" ? "face" : "mask")) return;
|
|
5659
|
+
if (!this.dutyAllows(kind)) return;
|
|
5474
5660
|
let frame = null;
|
|
5475
5661
|
try {
|
|
5476
5662
|
const copyable = source instanceof VideoFrame || source instanceof OffscreenCanvas;
|
|
@@ -5480,7 +5666,9 @@
|
|
|
5480
5666
|
} catch {
|
|
5481
5667
|
}
|
|
5482
5668
|
if (frame && detect.request(kind === "face" ? "face" : "mask", frame, timestampUs)) {
|
|
5483
|
-
|
|
5669
|
+
const now = performance.now();
|
|
5670
|
+
this.sent.set(kind, { ts: timestampUs, at: now });
|
|
5671
|
+
this.lastSentAt.set(kind, now);
|
|
5484
5672
|
}
|
|
5485
5673
|
}
|
|
5486
5674
|
/** Face and mask asked on the same frame share one draw. */
|
|
@@ -5501,36 +5689,42 @@
|
|
|
5501
5689
|
return this.detectCanvas;
|
|
5502
5690
|
}
|
|
5503
5691
|
ensureClassSegmenter() {
|
|
5504
|
-
if (this.classSegmenter || this.classRemote || this.closed) return Promise.resolve();
|
|
5692
|
+
if (this.classSegmenter || this.classRemote || this.classFailed || this.closed) return Promise.resolve();
|
|
5505
5693
|
if (this.classLoading) return this.classLoading;
|
|
5506
5694
|
this.classLoading = this.remote("mask", (d2) => d2.initMask("classes")).then(() => {
|
|
5507
5695
|
this.classRemote = true;
|
|
5508
|
-
}).catch(
|
|
5509
|
-
(
|
|
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) => {
|
|
5510
5699
|
if (this.closed) {
|
|
5511
5700
|
s2.close();
|
|
5512
5701
|
return;
|
|
5513
5702
|
}
|
|
5514
5703
|
this.classSegmenter = s2;
|
|
5515
|
-
})
|
|
5516
|
-
).catch((e2) => {
|
|
5517
|
-
|
|
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();
|
|
5518
5710
|
}).finally(() => {
|
|
5519
5711
|
this.classLoading = null;
|
|
5520
5712
|
});
|
|
5521
5713
|
return this.classLoading;
|
|
5522
5714
|
}
|
|
5523
5715
|
ensureSegmenter() {
|
|
5524
|
-
if (this.segmenter || this.closed) return Promise.resolve();
|
|
5716
|
+
if (this.segmenter || this.failed.has("segmenter") || this.closed) return Promise.resolve();
|
|
5525
5717
|
if (this.segmenterLoading) return this.segmenterLoading;
|
|
5526
|
-
this.segmenterLoading = createSegmenter(this.canvas).then((s2) => {
|
|
5718
|
+
this.segmenterLoading = withTimeout(createSegmenter(this.canvas), LOAD_MS, "person segmentation", (s2) => s2.close()).then((s2) => {
|
|
5527
5719
|
if (this.closed) {
|
|
5528
5720
|
s2.close();
|
|
5529
5721
|
return;
|
|
5530
5722
|
}
|
|
5531
5723
|
this.segmenter = s2;
|
|
5532
5724
|
}).catch((e2) => {
|
|
5533
|
-
|
|
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);
|
|
5534
5728
|
}).finally(() => {
|
|
5535
5729
|
this.segmenterLoading = null;
|
|
5536
5730
|
});
|
|
@@ -5549,7 +5743,7 @@
|
|
|
5549
5743
|
* it constructs one only to destroy it again.
|
|
5550
5744
|
*/
|
|
5551
5745
|
render(source, timestampUs) {
|
|
5552
|
-
if (this.closed) return false;
|
|
5746
|
+
if (this.closed || this.lostDuring) return false;
|
|
5553
5747
|
if (this.lastHeld) this.spare.push(this.lastHeld);
|
|
5554
5748
|
this.lastHeld = null;
|
|
5555
5749
|
this.swallowed = false;
|
|
@@ -5595,6 +5789,12 @@
|
|
|
5595
5789
|
this.depthVotes = 0;
|
|
5596
5790
|
}
|
|
5597
5791
|
}
|
|
5792
|
+
dutyAllows(kind) {
|
|
5793
|
+
const took = kind === "face" ? this.faceLatencyMs : this.maskLatencyMs.get(kind) ?? 0;
|
|
5794
|
+
if (took * 1e3 < this.gapUs) return true;
|
|
5795
|
+
const last = this.lastSentAt.get(kind);
|
|
5796
|
+
return last === void 0 || performance.now() - last >= took / DETECT_DUTY;
|
|
5797
|
+
}
|
|
5598
5798
|
took(kind, tsUs) {
|
|
5599
5799
|
const s2 = this.sent.get(kind);
|
|
5600
5800
|
if (s2?.ts !== tsUs) return null;
|
|
@@ -5662,7 +5862,7 @@
|
|
|
5662
5862
|
const classes = this.classSegmenter !== null && needsSkinClasses(this.filter, this.effects);
|
|
5663
5863
|
const seg = classes ? this.classSegmenter : this.segmenter;
|
|
5664
5864
|
if (!seg) {
|
|
5665
|
-
void this.ensureSegmenter();
|
|
5865
|
+
if (!this.remoteMaskComing(this.filter, this.effects)) void this.ensureSegmenter();
|
|
5666
5866
|
} else {
|
|
5667
5867
|
const s0 = performance.now();
|
|
5668
5868
|
mask = seg.segment(source, timestampUs / 1e3);
|
|
@@ -5694,6 +5894,14 @@
|
|
|
5694
5894
|
const glCalls = this.core.process(source, mask, this.background, maskKind, maskLo, maskHi);
|
|
5695
5895
|
if (mask) this.used?.release();
|
|
5696
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
|
+
}
|
|
5697
5905
|
this.prevSegMs = segMs;
|
|
5698
5906
|
this.prevTotalMs = performance.now() - t0;
|
|
5699
5907
|
const s2 = this.core.stats();
|
|
@@ -5716,7 +5924,8 @@
|
|
|
5716
5924
|
faces: s2 ? s2[12] : 0,
|
|
5717
5925
|
heldFrames: faceDone ? this.holdDepth : 0,
|
|
5718
5926
|
faceAgeMs: s2 && s2[10] > 0.5 && this.faceTs >= 0 ? (timestampUs - this.faceTs) / 1e3 : 0,
|
|
5719
|
-
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
|
|
5720
5929
|
};
|
|
5721
5930
|
return glCalls > 0;
|
|
5722
5931
|
}
|
|
@@ -5817,6 +6026,8 @@
|
|
|
5817
6026
|
var post = (m2) => self.postMessage(m2);
|
|
5818
6027
|
var engine = null;
|
|
5819
6028
|
var abort = null;
|
|
6029
|
+
var repipe = null;
|
|
6030
|
+
var inTrack = null;
|
|
5820
6031
|
var framesOn = false;
|
|
5821
6032
|
async function start(m2) {
|
|
5822
6033
|
await run(m2, m2.readable, m2.writable);
|
|
@@ -5827,21 +6038,27 @@
|
|
|
5827
6038
|
m2.track.stop();
|
|
5828
6039
|
throw new Error("mediakit: this worker has no MediaStreamTrackProcessor / VideoTrackGenerator");
|
|
5829
6040
|
}
|
|
6041
|
+
inTrack = m2.track;
|
|
5830
6042
|
const processor = new g2.MediaStreamTrackProcessor({ track: m2.track });
|
|
5831
6043
|
const generator = new g2.VideoTrackGenerator();
|
|
5832
6044
|
await run(m2, processor.readable, generator.writable, generator.track);
|
|
5833
6045
|
}
|
|
5834
6046
|
async function run(m2, readable, writable, outTrack) {
|
|
5835
6047
|
const canvas = new OffscreenCanvas(m2.width, m2.height);
|
|
5836
|
-
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 });
|
|
5837
6049
|
engine.syncFaces = m2.syncFaces ?? false;
|
|
5838
6050
|
engine.setDebug(m2.debug ?? "off");
|
|
5839
|
-
abort = new AbortController();
|
|
5840
6051
|
let n2 = 0;
|
|
5841
6052
|
let lastPost = performance.now();
|
|
5842
6053
|
let lastLoading = engine.loading;
|
|
5843
|
-
|
|
6054
|
+
let lostSent = false;
|
|
6055
|
+
const makeTransform = () => new TransformStream({
|
|
5844
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
|
+
}
|
|
5845
6062
|
const ts2 = frame.timestamp ?? 0;
|
|
5846
6063
|
n2++;
|
|
5847
6064
|
const now = performance.now();
|
|
@@ -5893,14 +6110,41 @@
|
|
|
5893
6110
|
} else {
|
|
5894
6111
|
post({ t: "ready", loading: engine.loading });
|
|
5895
6112
|
}
|
|
5896
|
-
|
|
5897
|
-
|
|
5898
|
-
|
|
5899
|
-
|
|
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();
|
|
5900
6141
|
}
|
|
5901
6142
|
function stop() {
|
|
5902
6143
|
abort?.abort();
|
|
5903
6144
|
abort = null;
|
|
6145
|
+
repipe = null;
|
|
6146
|
+
inTrack?.stop();
|
|
6147
|
+
inTrack = null;
|
|
5904
6148
|
engine?.close();
|
|
5905
6149
|
engine = null;
|
|
5906
6150
|
}
|
|
@@ -5909,10 +6153,16 @@
|
|
|
5909
6153
|
try {
|
|
5910
6154
|
if (m2.t === "start" || m2.t === "startTrack") {
|
|
5911
6155
|
void (m2.t === "start" ? start(m2) : startTrack(m2)).catch((err) => {
|
|
5912
|
-
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) });
|
|
5913
6158
|
});
|
|
5914
6159
|
} else if (m2.t === "frames") {
|
|
5915
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);
|
|
5916
6166
|
} else if (m2.t === "debug") {
|
|
5917
6167
|
engine?.setDebug(m2.view);
|
|
5918
6168
|
} else if (m2.t === "recipe") {
|