@tradalab/mediakit 2.2.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { BgFilter, Effects } from './recipe.js';
2
- export { EFFECTS_SCHEMA, FieldSpec, NO_EFFECTS, Rgb, TOUCH_UP, needsFace, needsMask, needsSegmentation, normalizeEffects, touchUp, touchUpAmount } from './recipe.js';
1
+ import { BgFilter, Effects, DebugView } from './recipe.js';
2
+ export { DEBUG_VIEWS, EFFECTS_SCHEMA, FieldSpec, NO_EFFECTS, Rgb, TOUCH_UP, needsFace, needsMask, needsSegmentation, normalizeEffects, touchUp, touchUpAmount } from './recipe.js';
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
 
@@ -41,6 +41,12 @@ interface FrameStats {
41
41
  autoExposure: number;
42
42
  /** How many faces are being tracked. */
43
43
  faces: number;
44
+ /** Frames `syncFaces` holds back right now, sized to how long landmarks take; 0 when off. */
45
+ heldFrames: number;
46
+ /** How old the landmarks behind this frame are, from the frame they were found on; 0 without landmarks. */
47
+ faceAgeMs: number;
48
+ /** How old the segmentation mask behind this frame is; 0 without one. */
49
+ maskAgeMs: number;
44
50
  }
45
51
  type EngineCanvas = HTMLCanvasElement | OffscreenCanvas;
46
52
  declare class Engine {
@@ -84,9 +90,17 @@ declare class Engine {
84
90
  private classMaskTs;
85
91
  syncFaces: boolean;
86
92
  renderedTs: number;
87
- private held;
88
- private heldTs;
89
- private heldSlot;
93
+ private queue;
94
+ private spare;
95
+ private holdDepth;
96
+ private depthVotes;
97
+ private gapUs;
98
+ private prevTs;
99
+ private readonly sent;
100
+ private faceLatencyMs;
101
+ private readonly maskLatencyMs;
102
+ private faceTs;
103
+ private maskUsedTs;
90
104
  private lastHeld;
91
105
  private swallowed;
92
106
  private maskWorkerMs;
@@ -120,6 +134,8 @@ declare class Engine {
120
134
  static on(canvas: EngineCanvas, filter: BgFilter, effects?: Effects, background?: ImageBitmap, alpha?: boolean, maxFaces?: number): Promise<Engine>;
121
135
  get loading(): boolean;
122
136
  get outputCanvas(): EngineCanvas;
137
+ /** Draws an inner layer instead of the picture until set back to "off". */
138
+ setDebug(view: DebugView): void;
123
139
  /**
124
140
  * Change the recipe without rebuilding the pipeline, so dragging a slider does not flash.
125
141
  *
@@ -158,6 +174,10 @@ declare class Engine {
158
174
  * it constructs one only to destroy it again.
159
175
  */
160
176
  render(source: TexImageSource, timestampUs: number): boolean;
177
+ private remoteMaskKind;
178
+ private retuneDepth;
179
+ private took;
180
+ private releaseHeld;
161
181
  dropped(): boolean;
162
182
  passthrough(): VideoFrame | null;
163
183
  private syncing;
@@ -212,6 +232,8 @@ interface PipelineOptions {
212
232
  */
213
233
  maxFaces?: number;
214
234
  syncFaces?: boolean;
235
+ /** Starts with an inner layer drawn instead of the picture; see `setDebug`. */
236
+ debug?: DebugView;
215
237
  /** No processed frame for 2 s while the camera is live (20 s while a detector loads). */
216
238
  onStall?: (info: {
217
239
  transport: Transport;
@@ -236,6 +258,7 @@ declare class VideoPipeline {
236
258
  private filter;
237
259
  private effects;
238
260
  private opts;
261
+ private debug;
239
262
  private source;
240
263
  private size;
241
264
  private lastBeat;
@@ -274,6 +297,12 @@ declare class VideoPipeline {
274
297
  private startMain;
275
298
  /** Swap the background in place - no pipeline rebuild, so the picture does not flash. */
276
299
  switchTo(filter: BgFilter): Promise<void>;
300
+ /**
301
+ * Draw an inner layer instead of the picture: "skin" (red smoothing, green whitening, blue face
302
+ * mesh), "mask" (the person or class mask) or "face" (tracked outlines and necks). "off" restores it.
303
+ */
304
+ setDebug(view: DebugView): void;
305
+ get debugView(): DebugView;
277
306
  /** Change the effects outside the background. */
278
307
  setEffects(effects: Effects): Promise<void>;
279
308
  private pushRecipe;
@@ -467,4 +496,4 @@ declare function renderText(text: string, style?: TextStyle): HTMLCanvasElement;
467
496
  /** Where each of the face landmarker's first 468 landmarks lies on the canonical face texture. */
468
497
  declare const FACE_UV: Float32Array<ArrayBuffer>;
469
498
 
470
- export { BgFilter, Color, Effects, Engine, type EngineCanvas, FACE_UV, type FrameStats, type PipelineHealth, type PipelineOptions, Scene, SceneLayer, type SceneOptions, type SceneSource, type SceneStats, type TextStyle, TransitionKind, type Transport, VideoPipeline, mediakitSupported, renderText };
499
+ 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 };
package/dist/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  import {
2
+ DEBUG_VIEWS,
2
3
  EFFECTS_SCHEMA,
3
4
  NO_EFFECTS,
4
5
  TOUCH_UP,
5
6
  coreRecipe,
7
+ debugCode,
6
8
  needsFace,
7
9
  needsMask,
8
10
  needsSegmentation,
@@ -10,7 +12,7 @@ import {
10
12
  normalizeEffects,
11
13
  touchUp,
12
14
  touchUpAmount
13
- } from "./chunk-2AD2GLNR.js";
15
+ } from "./chunk-YW75KJJO.js";
14
16
  import {
15
17
  BLEND_MODES,
16
18
  TRANSITIONS,
@@ -298,8 +300,13 @@ var ZERO_STATS = {
298
300
  faceSize: 0,
299
301
  needsFace: false,
300
302
  autoExposure: 0,
301
- faces: 0
303
+ faces: 0,
304
+ heldFrames: 0,
305
+ faceAgeMs: 0,
306
+ maskAgeMs: 0
302
307
  };
308
+ var MAX_HOLD = 2;
309
+ var DEPTH_VOTES = 15;
303
310
  function sourceSize(source) {
304
311
  if (source instanceof VideoFrame) return [source.displayWidth, source.displayHeight];
305
312
  if (typeof HTMLVideoElement !== "undefined" && source instanceof HTMLVideoElement) return [source.videoWidth, source.videoHeight];
@@ -353,9 +360,17 @@ var Engine = class _Engine {
353
360
  classMaskTs = 0;
354
361
  syncFaces = false;
355
362
  renderedTs = 0;
356
- held = [null, null];
357
- heldTs = -1;
358
- heldSlot = 0;
363
+ queue = [];
364
+ spare = [];
365
+ holdDepth = 1;
366
+ depthVotes = 0;
367
+ gapUs = 33333;
368
+ prevTs = -1;
369
+ sent = /* @__PURE__ */ new Map();
370
+ faceLatencyMs = 0;
371
+ maskLatencyMs = /* @__PURE__ */ new Map();
372
+ faceTs = -1;
373
+ maskUsedTs = -1;
359
374
  lastHeld = null;
360
375
  swallowed = false;
361
376
  maskWorkerMs = 0;
@@ -437,6 +452,11 @@ var Engine = class _Engine {
437
452
  get outputCanvas() {
438
453
  return this.canvas;
439
454
  }
455
+ /** Draws an inner layer instead of the picture until set back to "off". */
456
+ setDebug(view) {
457
+ if (this.closed) return;
458
+ this.core.setDebug(debugCode(view));
459
+ }
440
460
  /**
441
461
  * Change the recipe without rebuilding the pipeline, so dragging a slider does not flash.
442
462
  *
@@ -585,9 +605,17 @@ var Engine = class _Engine {
585
605
  const [lo, hi] = splitMicros(tsUs);
586
606
  this.core.setFace(lo, hi, n);
587
607
  this.faceWorkerMs = ms;
608
+ this.faceTs = tsUs;
609
+ const took = this.took("face", tsUs);
610
+ if (took !== null) this.faceLatencyMs = this.faceLatencyMs ? this.faceLatencyMs + (took - this.faceLatencyMs) * 0.2 : took;
588
611
  },
589
612
  mask: (tsUs, width, height, data, ms) => {
590
613
  if (this.closed) return;
614
+ const took = this.took(key, tsUs);
615
+ if (took !== null) {
616
+ const was = this.maskLatencyMs.get(key);
617
+ this.maskLatencyMs.set(key, was ? was + (took - was) * 0.2 : took);
618
+ }
591
619
  if (key === "person") {
592
620
  this.personMaskTex = this.uploadMaskBytes(this.personMaskTex, width, height, data);
593
621
  this.personMaskFresh = true;
@@ -651,7 +679,9 @@ var Engine = class _Engine {
651
679
  });
652
680
  } catch {
653
681
  }
654
- if (frame) detect.request(kind === "face" ? "face" : "mask", frame, timestampUs);
682
+ if (frame && detect.request(kind === "face" ? "face" : "mask", frame, timestampUs)) {
683
+ this.sent.set(kind, { ts: timestampUs, at: performance.now() });
684
+ }
655
685
  }
656
686
  /** Face and mask asked on the same frame share one draw. */
657
687
  detectCopy(source, timestampUs) {
@@ -720,26 +750,60 @@ var Engine = class _Engine {
720
750
  */
721
751
  render(source, timestampUs) {
722
752
  if (this.closed) return false;
753
+ if (this.lastHeld) this.spare.push(this.lastHeld);
723
754
  this.lastHeld = null;
724
755
  this.swallowed = false;
756
+ if (this.prevTs >= 0 && timestampUs > this.prevTs) {
757
+ this.gapUs += (Math.min(timestampUs - this.prevTs, 2e5) - this.gapUs) * 0.1;
758
+ }
759
+ this.prevTs = timestampUs;
725
760
  if (!this.syncing() || typeof ImageData !== "undefined" && source instanceof ImageData) {
726
- this.heldTs = -1;
761
+ this.releaseHeld();
727
762
  this.renderedTs = timestampUs;
728
763
  return this.draw(source, timestampUs, false);
729
764
  }
730
765
  this.requestRemote("face", source, timestampUs);
731
- const prev = this.heldTs >= 0 ? this.held[this.heldSlot] : null;
732
- const prevTs = this.heldTs;
733
- this.heldSlot ^= 1;
734
- this.hold(source, this.heldSlot);
735
- this.heldTs = timestampUs;
736
- if (!prev) {
766
+ const remoteMask = this.remoteMaskKind();
767
+ if (remoteMask) this.requestRemote(remoteMask, source, timestampUs);
768
+ this.queue.push({ c: this.hold(source), ts: timestampUs });
769
+ this.retuneDepth();
770
+ if (this.queue.length <= this.holdDepth) {
737
771
  this.swallowed = true;
738
772
  return false;
739
773
  }
740
- this.lastHeld = prev;
741
- this.renderedTs = prevTs;
742
- return this.draw(prev, prevTs, true);
774
+ while (this.queue.length > this.holdDepth + 1) this.spare.push(this.queue.shift().c);
775
+ const front = this.queue.shift();
776
+ this.lastHeld = front.c;
777
+ this.renderedTs = front.ts;
778
+ return this.draw(front.c, front.ts, true);
779
+ }
780
+ remoteMaskKind() {
781
+ if (this.personRemote && this.personRemoteWanted(this.filter, this.effects)) return "person";
782
+ if (this.classRemote && needsSkinClasses(this.filter, this.effects)) return "mask";
783
+ return null;
784
+ }
785
+ retuneDepth() {
786
+ const kind = this.remoteMaskKind();
787
+ const latency = Math.max(this.faceLatencyMs, kind ? this.maskLatencyMs.get(kind) ?? 0 : 0);
788
+ const want = latency > 0 ? Math.min(MAX_HOLD, Math.max(1, Math.ceil(latency * 1e3 / this.gapUs))) : 1;
789
+ if (want === this.holdDepth) {
790
+ this.depthVotes = 0;
791
+ return;
792
+ }
793
+ if (++this.depthVotes >= DEPTH_VOTES) {
794
+ this.holdDepth = want;
795
+ this.depthVotes = 0;
796
+ }
797
+ }
798
+ took(kind, tsUs) {
799
+ const s = this.sent.get(kind);
800
+ if (s?.ts !== tsUs) return null;
801
+ this.sent.delete(kind);
802
+ return performance.now() - s.at;
803
+ }
804
+ releaseHeld() {
805
+ for (const h of this.queue) this.spare.push(h.c);
806
+ this.queue = [];
743
807
  }
744
808
  dropped() {
745
809
  return this.swallowed;
@@ -750,14 +814,13 @@ var Engine = class _Engine {
750
814
  syncing() {
751
815
  return this.syncFaces && this.faceRemote && needsFace(this.effects);
752
816
  }
753
- hold(source, slot) {
817
+ hold(source) {
754
818
  const [w, h] = sourceSize(source);
755
- let c = this.held[slot];
756
- if (!c || c.width !== w || c.height !== h) {
757
- c = new OffscreenCanvas(w, h);
758
- this.held[slot] = c;
759
- }
819
+ let c = this.spare.pop();
820
+ while (c && (c.width !== w || c.height !== h)) c = this.spare.pop();
821
+ c ??= new OffscreenCanvas(w, h);
760
822
  c.getContext("2d").drawImage(source, 0, 0, w, h);
823
+ return c;
761
824
  }
762
825
  draw(source, timestampUs, faceDone) {
763
826
  const t0 = performance.now();
@@ -770,8 +833,9 @@ var Engine = class _Engine {
770
833
  let faceMs = 0;
771
834
  this.used = null;
772
835
  let served = false;
773
- if (needSeg && this.personRemote && this.personRemoteWanted(this.filter, this.effects)) {
774
- this.requestRemote("person", source, timestampUs);
836
+ const remoteMask = this.remoteMaskKind();
837
+ if (remoteMask && !faceDone) this.requestRemote(remoteMask, source, timestampUs);
838
+ if (remoteMask === "person") {
775
839
  if (this.personMaskTex) {
776
840
  served = true;
777
841
  if (this.personMaskFresh) {
@@ -782,8 +846,7 @@ var Engine = class _Engine {
782
846
  }
783
847
  }
784
848
  }
785
- if (needSeg && !served && this.classRemote && needsSkinClasses(this.filter, this.effects)) {
786
- this.requestRemote("mask", source, timestampUs);
849
+ if (remoteMask === "mask") {
787
850
  if (this.classMaskTex) {
788
851
  served = true;
789
852
  if (this.classMaskFresh) {
@@ -810,9 +873,11 @@ var Engine = class _Engine {
810
873
  }
811
874
  if (faceDone) {
812
875
  faceMs = this.faceWorkerMs;
876
+ } else if (this.faceRemote && needsFace(this.effects)) {
877
+ this.requestRemote("face", source, timestampUs);
878
+ faceMs = this.faceWorkerMs;
813
879
  } else if (this.core.decideFace() === 1) {
814
880
  if (this.faceRemote) {
815
- this.requestRemote("face", source, timestampUs);
816
881
  faceMs = this.faceWorkerMs;
817
882
  } else if (!this.face) {
818
883
  void this.ensureFace();
@@ -821,8 +886,10 @@ var Engine = class _Engine {
821
886
  const n = this.face.detect(source, timestampUs / 1e3, this.lmBuf);
822
887
  faceMs = performance.now() - f0;
823
888
  this.core.setFace(lo, hi, n);
889
+ this.faceTs = timestampUs;
824
890
  }
825
891
  }
892
+ if (mask) this.maskUsedTs = maskTs;
826
893
  const [maskLo, maskHi] = splitMicros(maskTs);
827
894
  const glCalls = this.core.process(source, mask, this.background, maskKind, maskLo, maskHi);
828
895
  if (mask) this.used?.release();
@@ -846,7 +913,10 @@ var Engine = class _Engine {
846
913
  faceSize: s ? s[9] : 0,
847
914
  needsFace: s ? s[10] > 0.5 : false,
848
915
  autoExposure: s ? s[11] : 0,
849
- faces: s ? s[12] : 0
916
+ faces: s ? s[12] : 0,
917
+ heldFrames: faceDone ? this.holdDepth : 0,
918
+ faceAgeMs: s && s[10] > 0.5 && this.faceTs >= 0 ? (timestampUs - this.faceTs) / 1e3 : 0,
919
+ maskAgeMs: (needSeg || remoteMask !== null) && this.maskUsedTs >= 0 ? (timestampUs - this.maskUsedTs) / 1e3 : 0
850
920
  };
851
921
  return glCalls > 0;
852
922
  }
@@ -881,7 +951,8 @@ var Engine = class _Engine {
881
951
  this.personRemote = false;
882
952
  this.classMaskTex = null;
883
953
  this.lmBuf = null;
884
- this.held = [null, null];
954
+ this.queue = [];
955
+ this.spare = [];
885
956
  this.lastHeld = null;
886
957
  this.core.close();
887
958
  this.background?.close();
@@ -962,6 +1033,7 @@ var VideoPipeline = class {
962
1033
  filter;
963
1034
  effects;
964
1035
  opts;
1036
+ debug = "off";
965
1037
  // Health: a beat per frame out.
966
1038
  source = null;
967
1039
  size = { width: 1280, height: 720, fps: 30 };
@@ -991,6 +1063,7 @@ var VideoPipeline = class {
991
1063
  this.filter = filter;
992
1064
  this.effects = effects;
993
1065
  this.opts = opts;
1066
+ this.debug = opts.debug ?? "off";
994
1067
  }
995
1068
  /** Whether frames are coming out, and how long since the last one. */
996
1069
  get health() {
@@ -1116,6 +1189,7 @@ var VideoPipeline = class {
1116
1189
  const { width, height, fps } = this.size;
1117
1190
  this.engine = await Engine.create(width, height, this.filter, this.effects, this.opts.alpha ?? false, this.opts.maxFaces);
1118
1191
  this.engine.syncFaces = this.opts.syncFaces ?? false;
1192
+ this.engine.setDebug(this.debug);
1119
1193
  this.transport = hasInsertableStreams() ? "insertable" : "canvas";
1120
1194
  this.frames = 0;
1121
1195
  this.lastBeat = performance.now();
@@ -1131,6 +1205,18 @@ var VideoPipeline = class {
1131
1205
  this.filter = filter;
1132
1206
  await this.pushRecipe();
1133
1207
  }
1208
+ /**
1209
+ * Draw an inner layer instead of the picture: "skin" (red smoothing, green whitening, blue face
1210
+ * mesh), "mask" (the person or class mask) or "face" (tracked outlines and necks). "off" restores it.
1211
+ */
1212
+ setDebug(view) {
1213
+ this.debug = view;
1214
+ if (this.worker) this.send({ t: "debug", view });
1215
+ else this.engine?.setDebug(view);
1216
+ }
1217
+ get debugView() {
1218
+ return this.debug;
1219
+ }
1134
1220
  /** Change the effects outside the background. */
1135
1221
  async setEffects(effects) {
1136
1222
  this.effects = effects;
@@ -1241,7 +1327,8 @@ var VideoPipeline = class {
1241
1327
  background,
1242
1328
  alpha: this.opts.alpha,
1243
1329
  maxFaces: this.opts.maxFaces,
1244
- syncFaces: this.opts.syncFaces
1330
+ syncFaces: this.opts.syncFaces,
1331
+ debug: this.debug
1245
1332
  },
1246
1333
  background ? [processor.readable, generator.writable, background] : [processor.readable, generator.writable]
1247
1334
  )
@@ -1267,7 +1354,8 @@ var VideoPipeline = class {
1267
1354
  background,
1268
1355
  alpha: this.opts.alpha,
1269
1356
  maxFaces: this.opts.maxFaces,
1270
- syncFaces: this.opts.syncFaces
1357
+ syncFaces: this.opts.syncFaces,
1358
+ debug: this.debug
1271
1359
  },
1272
1360
  background ? [clone, background] : [clone]
1273
1361
  )
@@ -2769,6 +2857,7 @@ var FACE_UV = new Float32Array([
2769
2857
  ]);
2770
2858
  export {
2771
2859
  BLEND_MODES,
2860
+ DEBUG_VIEWS,
2772
2861
  EFFECTS_SCHEMA,
2773
2862
  Engine,
2774
2863
  FACE_UV,