mediascale-sdk 1.0.13 → 1.0.16
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/docs/integration.md +98 -13
- package/package.json +1 -1
- package/src/client.js +579 -78
- package/src/gradient.js +317 -0
- package/src/index.js +16 -2
- package/src/live.js +70 -3
- package/src/record-compositor.js +10 -4
- package/src/record-frame-compositor.js +124 -11
- package/src/record-worker.js +7 -3
- package/src/record.js +95 -53
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { avatarBackgroundFor, fillGradient, normalizeGradient } from "./gradient.js";
|
|
2
|
+
|
|
1
3
|
/** Parse `#rgb` / `#rrggbb` / `rgb()` / `rgba()` into `{ r, g, b }`. */
|
|
2
4
|
export function parseCssColor(input) {
|
|
3
5
|
const raw = String(input || "").trim();
|
|
@@ -94,7 +96,85 @@ export function toneForName(name) {
|
|
|
94
96
|
return TILE_TONES[hash % TILE_TONES.length];
|
|
95
97
|
}
|
|
96
98
|
|
|
97
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Stage backdrop. A plain color keeps the legacy glow design; a gradient spec
|
|
101
|
+
* (`linear-gradient(...)`, `radial-gradient(...)`, object) is painted as given.
|
|
102
|
+
*/
|
|
103
|
+
export function fillBackdrop(ctx, x, y, w, h, background = STAGE_BACKGROUND) {
|
|
104
|
+
const spec = typeof background === "object" && background?.stops ? background : normalizeGradient(background);
|
|
105
|
+
if (spec && spec.type !== "color" && fillGradient(ctx, x, y, w, h, spec)) return;
|
|
106
|
+
fillRadialBackground(ctx, x, y, w, h, spec?.type === "color" ? spec.color : STAGE_BACKGROUND);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function makeSurface(width, height) {
|
|
110
|
+
if (typeof OffscreenCanvas === "function") return new OffscreenCanvas(width, height);
|
|
111
|
+
if (typeof document !== "undefined") {
|
|
112
|
+
const canvas = document.createElement("canvas");
|
|
113
|
+
canvas.width = width;
|
|
114
|
+
canvas.height = height;
|
|
115
|
+
return canvas;
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Backdrop rendered once per output size and blitted each frame (one drawImage instead of
|
|
122
|
+
* several full-frame gradient fills — keeps 1080p compositing cheap).
|
|
123
|
+
*/
|
|
124
|
+
export class BackdropCache {
|
|
125
|
+
constructor(background) {
|
|
126
|
+
this.set(background);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
set(background) {
|
|
130
|
+
this.background = background;
|
|
131
|
+
this.surface = null;
|
|
132
|
+
this.key = "";
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** `w` / `h` are layout (design) units; `ctx` may carry an output scale transform. */
|
|
136
|
+
paint(ctx, w, h) {
|
|
137
|
+
const canvas = ctx.canvas;
|
|
138
|
+
const cw = canvas?.width;
|
|
139
|
+
const ch = canvas?.height;
|
|
140
|
+
if (!cw || !ch || typeof ctx.drawImage !== "function") {
|
|
141
|
+
fillBackdrop(ctx, 0, 0, w, h, this.background);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const key = `${cw}x${ch}x${w}x${h}`;
|
|
145
|
+
if (this.key !== key) {
|
|
146
|
+
this.key = key;
|
|
147
|
+
this.surface = null;
|
|
148
|
+
try {
|
|
149
|
+
const surface = makeSurface(cw, ch);
|
|
150
|
+
const sctx = surface?.getContext("2d");
|
|
151
|
+
if (sctx) {
|
|
152
|
+
sctx.scale(cw / w, ch / h);
|
|
153
|
+
fillBackdrop(sctx, 0, 0, w, h, this.background);
|
|
154
|
+
this.surface = surface;
|
|
155
|
+
}
|
|
156
|
+
} catch {
|
|
157
|
+
this.surface = null;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (!this.surface) {
|
|
161
|
+
fillBackdrop(ctx, 0, 0, w, h, this.background);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
ctx.save();
|
|
165
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
166
|
+
ctx.drawImage(this.surface, 0, 0);
|
|
167
|
+
ctx.restore();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Camera-off tile fill. `avatarBackground` is one gradient spec / list (picked by name);
|
|
173
|
+
* without it the built-in per-name tones are used.
|
|
174
|
+
*/
|
|
175
|
+
export function fillNameTone(ctx, x, y, w, h, name, avatarBackground = null) {
|
|
176
|
+
const picked = avatarBackgroundFor(name, avatarBackground);
|
|
177
|
+
if (fillGradient(ctx, x, y, w, h, normalizeGradient(picked))) return;
|
|
98
178
|
const tone = toneForName(name) || TILE_TONES[0];
|
|
99
179
|
const mid = tone.mid || TILE_TONES[0].mid;
|
|
100
180
|
const edge = tone.edge || TILE_TONES[0].edge;
|
|
@@ -207,6 +287,7 @@ export function paintRoundedTile(ctx, {
|
|
|
207
287
|
muted = false,
|
|
208
288
|
fit = "cover",
|
|
209
289
|
placeholder = false,
|
|
290
|
+
avatarBackground = null,
|
|
210
291
|
}) {
|
|
211
292
|
const { x, y, w, h } = rect;
|
|
212
293
|
ctx.save();
|
|
@@ -220,20 +301,32 @@ export function paintRoundedTile(ctx, {
|
|
|
220
301
|
}
|
|
221
302
|
const hasVideo = Boolean(source && sw > 0 && sh > 0);
|
|
222
303
|
if (!hasVideo || placeholder) {
|
|
223
|
-
fillNameTone(ctx, x, y, w, h, label);
|
|
304
|
+
fillNameTone(ctx, x, y, w, h, label, avatarBackground);
|
|
224
305
|
drawInitialAvatar(ctx, x, y, w, h, label);
|
|
225
306
|
} else {
|
|
226
|
-
|
|
227
|
-
|
|
307
|
+
// Screenshare (contain) letterbox stays transparent so the stage backdrop shows through
|
|
308
|
+
// as one uniform background. Cover tiles are fully covered by the frame anyway.
|
|
309
|
+
if (fit !== "contain" && fillStyle) {
|
|
310
|
+
ctx.fillStyle = fillStyle;
|
|
311
|
+
ctx.fillRect(x, y, w, h);
|
|
312
|
+
}
|
|
228
313
|
const scale = fit === "contain"
|
|
229
314
|
? Math.min(w / sw, h / sh)
|
|
230
315
|
: Math.max(w / sw, h / sh);
|
|
231
316
|
const dw = sw * scale;
|
|
232
317
|
const dh = sh * scale;
|
|
233
318
|
const prev = ctx.imageSmoothingEnabled;
|
|
234
|
-
|
|
319
|
+
const prevQuality = ctx.imageSmoothingQuality;
|
|
320
|
+
// Effective pixel scale includes the output transform (720p layout drawn at 1080p).
|
|
321
|
+
const t = typeof ctx.getTransform === "function" ? ctx.getTransform() : null;
|
|
322
|
+
const pixelScale = scale * (t ? Math.hypot(t.a, t.b) || 1 : 1);
|
|
323
|
+
// 1:1 stays crisp; any resample (down or up) uses high-quality filtering instead of
|
|
324
|
+
// nearest-neighbour, which made small/low-res camera tiles look blocky.
|
|
325
|
+
ctx.imageSmoothingEnabled = Math.abs(pixelScale - 1) > 0.02;
|
|
326
|
+
if ("imageSmoothingQuality" in ctx) ctx.imageSmoothingQuality = "high";
|
|
235
327
|
ctx.drawImage(source, sx, sy, sw, sh, x + (w - dw) / 2, y + (h - dh) / 2, dw, dh);
|
|
236
328
|
ctx.imageSmoothingEnabled = prev;
|
|
329
|
+
if (prevQuality) ctx.imageSmoothingQuality = prevQuality;
|
|
237
330
|
}
|
|
238
331
|
ctx.restore();
|
|
239
332
|
if (label) drawNamePill(ctx, x, y, w, h, label, { presenter: accent, muted });
|
|
@@ -241,17 +334,24 @@ export function paintRoundedTile(ctx, {
|
|
|
241
334
|
|
|
242
335
|
/** Owns at most one input frame per source and one output write. */
|
|
243
336
|
export class FrameCompositor {
|
|
244
|
-
constructor({ canvas, ctx = canvas.getContext("2d"), writer, fps = 15,
|
|
245
|
-
background = STAGE_BACKGROUND, steady = false,
|
|
337
|
+
constructor({ canvas, ctx = canvas.getContext("2d"), writer, fps = 15, scale = 1,
|
|
338
|
+
background = STAGE_BACKGROUND, avatarBackground = null, steady = false,
|
|
246
339
|
now = () => performance.now(), makeFrame = (surface, init) => new VideoFrame(surface, init),
|
|
247
340
|
onError = () => {} }) {
|
|
248
341
|
this.canvas = canvas;
|
|
249
342
|
this.ctx = ctx;
|
|
343
|
+
/** Layout rects are in design units; draw them onto a larger canvas through a transform. */
|
|
344
|
+
this.scale = Number(scale) > 0 ? Number(scale) : 1;
|
|
345
|
+
this.width = canvas.width / this.scale;
|
|
346
|
+
this.height = canvas.height / this.scale;
|
|
347
|
+
this.ctx.setTransform?.(this.scale, 0, 0, this.scale, 0, 0);
|
|
250
348
|
this.writer = writer;
|
|
251
349
|
this.interval = 1000 / fps;
|
|
252
350
|
/** Live output: emit every interval even when nothing changed (encoders need a steady rate). */
|
|
253
351
|
this.steady = Boolean(steady);
|
|
254
|
-
this.
|
|
352
|
+
this.backdrop = new BackdropCache(background || STAGE_BACKGROUND);
|
|
353
|
+
/** Default camera-off gradient(s); per-source `avatarBackground` wins. */
|
|
354
|
+
this.avatarBackground = avatarBackground || null;
|
|
255
355
|
this.now = now;
|
|
256
356
|
this.makeFrame = makeFrame;
|
|
257
357
|
this.onError = onError;
|
|
@@ -264,6 +364,13 @@ export class FrameCompositor {
|
|
|
264
364
|
this.startedAt = now();
|
|
265
365
|
}
|
|
266
366
|
|
|
367
|
+
/** Change stage backdrop / default avatar gradients without restarting. */
|
|
368
|
+
setAppearance({ background, avatarBackground } = {}) {
|
|
369
|
+
if (background !== undefined) this.backdrop.set(background || STAGE_BACKGROUND);
|
|
370
|
+
if (avatarBackground !== undefined) this.avatarBackground = avatarBackground || null;
|
|
371
|
+
this.dirty = true;
|
|
372
|
+
}
|
|
373
|
+
|
|
267
374
|
setSources(sources) {
|
|
268
375
|
this.sources = sources;
|
|
269
376
|
const keys = new Set(sources.map((source) => source.key));
|
|
@@ -295,10 +402,13 @@ export class FrameCompositor {
|
|
|
295
402
|
|
|
296
403
|
flush() {
|
|
297
404
|
const now = this.now();
|
|
298
|
-
|
|
299
|
-
|
|
405
|
+
// Avatar tiles have no input track: keep emitting at the frame rate while any are shown,
|
|
406
|
+
// so recordings never stall on a stage of camera-off people.
|
|
407
|
+
const idleTiles = this.sources.some((source) => source.rect && !this.frames.has(source.key));
|
|
408
|
+
if (!this.active || (!this.dirty && !this.steady && !idleTiles) || this.writing || now - this.lastDraw < this.interval) return;
|
|
409
|
+
this.backdrop.paint(this.ctx, this.width, this.height);
|
|
300
410
|
for (const source of this.sources) {
|
|
301
|
-
const { key, rect, crop, label, accent, radius, muted, fit, fillStyle, placeholder } = source;
|
|
411
|
+
const { key, rect, crop, label, accent, radius, muted, fit, fillStyle, placeholder, avatarBackground } = source;
|
|
302
412
|
const frame = this.frames.get(key);
|
|
303
413
|
if (!rect) continue;
|
|
304
414
|
const tileRadius = radius == null ? 0 : radius;
|
|
@@ -311,8 +421,11 @@ export class FrameCompositor {
|
|
|
311
421
|
fillStyle: fillStyle || STAGE_BACKGROUND,
|
|
312
422
|
fit: fit === "contain" ? "contain" : "cover",
|
|
313
423
|
placeholder: Boolean(placeholder),
|
|
424
|
+
avatarBackground: avatarBackground || this.avatarBackground,
|
|
314
425
|
};
|
|
315
426
|
if (!frame) {
|
|
427
|
+
// Screenshare before its first frame: keep the uniform backdrop.
|
|
428
|
+
if (chrome.fit === "contain") continue;
|
|
316
429
|
// Camera-off / waiting: gradient + initial + name.
|
|
317
430
|
paintRoundedTile(this.ctx, {
|
|
318
431
|
source: null,
|
package/src/record-worker.js
CHANGED
|
@@ -46,7 +46,7 @@ self.onmessage = async ({ data }) => {
|
|
|
46
46
|
track = generator.track;
|
|
47
47
|
} else writer = data.writable.getWriter();
|
|
48
48
|
const canvas = new OffscreenCanvas(data.width, data.height);
|
|
49
|
-
compositor = new FrameCompositor({ canvas, writer, fps: data.fps, background: data.background, steady: data.steady, onError: error });
|
|
49
|
+
compositor = new FrameCompositor({ canvas, writer, scale: data.scale, fps: data.fps, background: data.background, avatarBackground: data.avatarBackground, steady: data.steady, onError: error });
|
|
50
50
|
// Native input frames drive rendering. This tick only flushes a final dirty frame.
|
|
51
51
|
tick = setInterval(() => compositor.flush(), Math.round(1000 / data.fps));
|
|
52
52
|
postMessage({ requestId: data.requestId, track }, track ? [track] : []);
|
|
@@ -56,7 +56,7 @@ self.onmessage = async ({ data }) => {
|
|
|
56
56
|
const { value, done } = await reader.read();
|
|
57
57
|
if (done) throw new Error("Recording frame probe ended before receiving video");
|
|
58
58
|
try {
|
|
59
|
-
compositor.ctx.drawImage(value, 0, 0, compositor.
|
|
59
|
+
compositor.ctx.drawImage(value, 0, 0, compositor.width, compositor.height);
|
|
60
60
|
const frame = new VideoFrame(compositor.canvas, { timestamp: 0 });
|
|
61
61
|
try { await writer.write(frame); } finally { frame.close(); }
|
|
62
62
|
} finally {
|
|
@@ -69,7 +69,7 @@ self.onmessage = async ({ data }) => {
|
|
|
69
69
|
} else if (data.type === "sources") {
|
|
70
70
|
const keys = new Set(data.sources.map((source) => source.key));
|
|
71
71
|
for (const key of readers.keys()) if (!keys.has(key)) remove(key);
|
|
72
|
-
compositor.setSources(data.sources.map(({ key, rect, crop, label, accent, radius, muted, fit, fillStyle }) => ({
|
|
72
|
+
compositor.setSources(data.sources.map(({ key, rect, crop, label, accent, radius, muted, fit, fillStyle, placeholder, avatarBackground }) => ({
|
|
73
73
|
key,
|
|
74
74
|
rect,
|
|
75
75
|
crop: crop || null,
|
|
@@ -79,12 +79,16 @@ self.onmessage = async ({ data }) => {
|
|
|
79
79
|
muted: Boolean(muted),
|
|
80
80
|
fit: fit === "contain" ? "contain" : "cover",
|
|
81
81
|
...(fillStyle ? { fillStyle } : {}),
|
|
82
|
+
placeholder: Boolean(placeholder),
|
|
83
|
+
avatarBackground: avatarBackground || null,
|
|
82
84
|
})));
|
|
83
85
|
for (const source of data.sources) {
|
|
84
86
|
if (!source.readable && !source.track) continue;
|
|
85
87
|
remove(source.key);
|
|
86
88
|
void consume(source);
|
|
87
89
|
}
|
|
90
|
+
} else if (data.type === "appearance") {
|
|
91
|
+
compositor.setAppearance({ background: data.background, avatarBackground: data.avatarBackground });
|
|
88
92
|
} else if (data.type === "active") {
|
|
89
93
|
compositor.setActive(data.active);
|
|
90
94
|
if (!data.active) for (const key of [...readers.keys()]) remove(key);
|
package/src/record.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createWorkerCompositor } from "./record-compositor.js";
|
|
2
|
+
import { avatarBackgroundFor } from "./gradient.js";
|
|
2
3
|
import {
|
|
3
|
-
|
|
4
|
+
BackdropCache,
|
|
4
5
|
paintRoundedTile,
|
|
5
6
|
drawNamePill,
|
|
6
7
|
roundedRectPath,
|
|
@@ -16,8 +17,40 @@ export {
|
|
|
16
17
|
STAGE_BACKGROUND,
|
|
17
18
|
} from "./record-frame-compositor.js";
|
|
18
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Layout design units: width is always 1280, height follows the output aspect (720 for 16:9).
|
|
22
|
+
* Output canvases scale this up/down (e.g. 1920×1080 → ×1.5) via a transform.
|
|
23
|
+
*/
|
|
19
24
|
const W = 1280;
|
|
20
25
|
const H = 720;
|
|
26
|
+
/** Output limits: keep the client light — never more pixels than 1080p, long edge ≤ 1920. */
|
|
27
|
+
const MAX_OUTPUT_EDGE = 1920;
|
|
28
|
+
const MAX_OUTPUT_PIXELS = 1920 * 1080;
|
|
29
|
+
const MIN_OUTPUT_EDGE = 160;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Output size for the stage. Accepts `{ width, height }`, or a height (16:9 width derived).
|
|
33
|
+
* Keeps the aspect ratio while clamping to the limits; dimensions are even (H264 needs it).
|
|
34
|
+
*/
|
|
35
|
+
export function stageOutputSize(size = H) {
|
|
36
|
+
let width;
|
|
37
|
+
let height;
|
|
38
|
+
if (size && typeof size === "object") {
|
|
39
|
+
width = Number(size.width);
|
|
40
|
+
height = Number(size.height);
|
|
41
|
+
} else {
|
|
42
|
+
height = Number(size);
|
|
43
|
+
}
|
|
44
|
+
if (!Number.isFinite(height) || height <= 0) height = H;
|
|
45
|
+
if (!Number.isFinite(width) || width <= 0) width = (height * W) / H;
|
|
46
|
+
let fit = Math.min(1, MAX_OUTPUT_EDGE / Math.max(width, height), Math.sqrt(MAX_OUTPUT_PIXELS / (width * height)));
|
|
47
|
+
fit = Math.max(fit, MIN_OUTPUT_EDGE / Math.min(width, height));
|
|
48
|
+
const even = (v) => Math.max(2, Math.round(v / 2) * 2);
|
|
49
|
+
const outW = even(Math.min(MAX_OUTPUT_EDGE, width * fit));
|
|
50
|
+
const outH = even(Math.min(MAX_OUTPUT_EDGE, height * fit));
|
|
51
|
+
const scale = outW / W;
|
|
52
|
+
return { width: outW, height: outH, scale, layoutWidth: W, layoutHeight: outH / scale };
|
|
53
|
+
}
|
|
21
54
|
/** Outer margin around the whole layout. */
|
|
22
55
|
const PAD = 28;
|
|
23
56
|
/**
|
|
@@ -121,7 +154,7 @@ function galleryColumns(n) {
|
|
|
121
154
|
* Design layout: padded 16:9 gallery (incomplete last row centered), or screenshare
|
|
122
155
|
* left (~78%) + up to 6 camera tiles stacked on the right. Max 6 cameras.
|
|
123
156
|
*/
|
|
124
|
-
export function layoutSlots(cameras, screen) {
|
|
157
|
+
export function layoutSlots(cameras, screen, W = 1280, H = 720) {
|
|
125
158
|
const n = Math.max(0, Math.min(MAX_STAGE_CAMERAS, Number(cameras) || 0));
|
|
126
159
|
const innerW = W - PAD * 2;
|
|
127
160
|
const innerH = H - PAD * 2;
|
|
@@ -281,12 +314,22 @@ export class CanvasRecorder {
|
|
|
281
314
|
token,
|
|
282
315
|
selfName,
|
|
283
316
|
canvasBackground,
|
|
317
|
+
avatarBackground,
|
|
284
318
|
recorderPeer,
|
|
285
319
|
onCaptureChange,
|
|
286
320
|
onFailure,
|
|
287
321
|
fps,
|
|
288
322
|
bitrate,
|
|
323
|
+
outputHeight,
|
|
324
|
+
outputSize,
|
|
289
325
|
}) {
|
|
326
|
+
/** Canvas pixel size; layout math stays in design units (1280 wide). */
|
|
327
|
+
const output = stageOutputSize(outputSize || outputHeight);
|
|
328
|
+
this.outputWidth = output.width;
|
|
329
|
+
this.outputHeight = output.height;
|
|
330
|
+
this.outputScale = output.scale;
|
|
331
|
+
this.layoutWidth = output.layoutWidth;
|
|
332
|
+
this.layoutHeight = output.layoutHeight;
|
|
290
333
|
/** Compositor + capture frame rate (recording and live output can differ). */
|
|
291
334
|
this.fps = clampFps(fps);
|
|
292
335
|
this.bitrate = Number(bitrate) > 0 ? Math.round(Number(bitrate)) : BITRATE;
|
|
@@ -305,7 +348,11 @@ export class CanvasRecorder {
|
|
|
305
348
|
this.stagePeople = null;
|
|
306
349
|
this.token = token || "";
|
|
307
350
|
this.selfName = String(selfName || "Host").slice(0, 40);
|
|
308
|
-
|
|
351
|
+
/** Color or gradient spec (string / object) for the stage backdrop. */
|
|
352
|
+
this.canvasBackground = canvasBackground || STAGE_BACKGROUND;
|
|
353
|
+
/** Camera-off tile gradient(s): one spec or a list picked by name; null → built-in tones. */
|
|
354
|
+
this.avatarBackground = avatarBackground || null;
|
|
355
|
+
this.backdrop = new BackdropCache(this.canvasBackground);
|
|
309
356
|
this.onCaptureChange = typeof onCaptureChange === "function" ? onCaptureChange : () => {};
|
|
310
357
|
this.onFailure = typeof onFailure === "function" ? onFailure : () => {};
|
|
311
358
|
this.segment = 0;
|
|
@@ -439,6 +486,7 @@ export class CanvasRecorder {
|
|
|
439
486
|
id: String(person?.id || "").trim(),
|
|
440
487
|
name: String(person?.name || "Guest").trim().slice(0, 40) || "Guest",
|
|
441
488
|
local: Boolean(person?.local),
|
|
489
|
+
...(person?.avatarBackground ? { avatarBackground: person.avatarBackground } : {}),
|
|
442
490
|
}))
|
|
443
491
|
.filter((person) => person.id || person.name)
|
|
444
492
|
: null;
|
|
@@ -456,6 +504,22 @@ export class CanvasRecorder {
|
|
|
456
504
|
* Crop a published video source when compositing (same normalized bounds as the sample
|
|
457
505
|
* subscriber whiteboard crop: `{ x, y, w, h }` relative to the full captured frame).
|
|
458
506
|
*/
|
|
507
|
+
/** Update the stage backdrop and/or camera-off gradients while recording / live. */
|
|
508
|
+
setAppearance({ canvasBackground, avatarBackground } = {}) {
|
|
509
|
+
const update = {};
|
|
510
|
+
if (canvasBackground !== undefined) {
|
|
511
|
+
this.canvasBackground = canvasBackground || STAGE_BACKGROUND;
|
|
512
|
+
this.backdrop.set(this.canvasBackground);
|
|
513
|
+
update.background = this.canvasBackground;
|
|
514
|
+
}
|
|
515
|
+
if (avatarBackground !== undefined) {
|
|
516
|
+
this.avatarBackground = avatarBackground || null;
|
|
517
|
+
update.avatarBackground = this.avatarBackground;
|
|
518
|
+
}
|
|
519
|
+
this.compositor?.setAppearance?.(update);
|
|
520
|
+
if (this.prepared) this.retile();
|
|
521
|
+
}
|
|
522
|
+
|
|
459
523
|
setVideoCrop(source, crop) {
|
|
460
524
|
const id = String(source || "").trim();
|
|
461
525
|
if (!id) return;
|
|
@@ -618,11 +682,13 @@ export class CanvasRecorder {
|
|
|
618
682
|
silence.connect(this.dest);
|
|
619
683
|
this.clock.start();
|
|
620
684
|
const { compositor, reason } = await createWorkerCompositor({
|
|
621
|
-
width:
|
|
622
|
-
height:
|
|
685
|
+
width: this.outputWidth,
|
|
686
|
+
height: this.outputHeight,
|
|
687
|
+
scale: this.outputScale,
|
|
623
688
|
fps: this.fps,
|
|
624
689
|
steady: this.steadyFrames,
|
|
625
690
|
background: this.canvasBackground,
|
|
691
|
+
avatarBackground: this.avatarBackground,
|
|
626
692
|
onError: (failure) => {
|
|
627
693
|
this.compositorFailure = failure;
|
|
628
694
|
window.dispatchEvent(new CustomEvent(`mediascale:${this.eventPrefix}-error`, { detail: { message: failure.message } }));
|
|
@@ -634,10 +700,11 @@ export class CanvasRecorder {
|
|
|
634
700
|
let video = compositor?.track;
|
|
635
701
|
if (!video) {
|
|
636
702
|
this.canvas = document.createElement("canvas");
|
|
637
|
-
this.canvas.width =
|
|
638
|
-
this.canvas.height =
|
|
703
|
+
this.canvas.width = this.outputWidth;
|
|
704
|
+
this.canvas.height = this.outputHeight;
|
|
639
705
|
this.ctx = this.canvas.getContext("2d");
|
|
640
|
-
|
|
706
|
+
this.ctx.setTransform(this.outputScale, 0, 0, this.outputScale, 0, 0);
|
|
707
|
+
this.backdrop.paint(this.ctx, this.layoutWidth, this.layoutHeight);
|
|
641
708
|
const url = URL.createObjectURL(new Blob([`let tick; onmessage = ({data}) => { clearInterval(tick); if (data) tick = setInterval(() => postMessage(0), ${Math.round(1000 / this.fps)}); };`], { type: "text/javascript" }));
|
|
642
709
|
const worker = new Worker(url);
|
|
643
710
|
worker.onmessage = () => this.draw();
|
|
@@ -815,22 +882,22 @@ export class CanvasRecorder {
|
|
|
815
882
|
}
|
|
816
883
|
if (live) {
|
|
817
884
|
used.add(live.key);
|
|
818
|
-
chosen.push({ ...live, displayName: name });
|
|
885
|
+
chosen.push({ ...live, displayName: name, avatarBackground: person.avatarBackground || null });
|
|
819
886
|
continue;
|
|
820
887
|
}
|
|
888
|
+
// Camera-off: the compositor paints the avatar itself (no synthetic canvas track).
|
|
821
889
|
const phKey = `placeholder|${isLocal ? "local" : (id || name.toLowerCase())}`;
|
|
822
|
-
keepPlaceholders.add(phKey);
|
|
823
|
-
const track = this.ensurePlaceholderTrack(name, isLocal, phKey);
|
|
824
890
|
chosen.push({
|
|
825
891
|
key: `${phKey}|camera`,
|
|
826
892
|
kind: "camera",
|
|
827
893
|
isLocal,
|
|
828
894
|
participantKey: id || (isLocal ? "local" : name),
|
|
829
895
|
order: this.orderOf(`${phKey}|camera`),
|
|
830
|
-
video:
|
|
831
|
-
track,
|
|
896
|
+
video: null,
|
|
897
|
+
track: null,
|
|
832
898
|
placeholder: true,
|
|
833
899
|
displayName: name,
|
|
900
|
+
avatarBackground: person.avatarBackground || null,
|
|
834
901
|
});
|
|
835
902
|
}
|
|
836
903
|
} else {
|
|
@@ -847,17 +914,14 @@ export class CanvasRecorder {
|
|
|
847
914
|
}));
|
|
848
915
|
if (chosen.length === 0) {
|
|
849
916
|
const name = this.selfName || "Host";
|
|
850
|
-
const phKey = "placeholder|local";
|
|
851
|
-
keepPlaceholders.add(phKey);
|
|
852
|
-
const track = this.ensurePlaceholderTrack(name, true, phKey);
|
|
853
917
|
chosen.push({
|
|
854
918
|
key: "local|camera|placeholder",
|
|
855
919
|
kind: "camera",
|
|
856
920
|
isLocal: true,
|
|
857
921
|
participantKey: "local",
|
|
858
922
|
order: this.orderOf("local|camera|placeholder"),
|
|
859
|
-
video:
|
|
860
|
-
track,
|
|
923
|
+
video: null,
|
|
924
|
+
track: null,
|
|
861
925
|
placeholder: true,
|
|
862
926
|
displayName: name,
|
|
863
927
|
});
|
|
@@ -871,12 +935,14 @@ export class CanvasRecorder {
|
|
|
871
935
|
displayName: label,
|
|
872
936
|
micMuted: !hasLiveMic(slot, mics),
|
|
873
937
|
presenter: isPresenterSlot(slot, label),
|
|
938
|
+
// Resolved per tile so the worker gets a single spec (also used before the first frame).
|
|
939
|
+
avatarBackground: slot.avatarBackground || avatarBackgroundFor(label, this.avatarBackground),
|
|
874
940
|
};
|
|
875
941
|
});
|
|
876
942
|
this.prunePlaceholders(keepPlaceholders);
|
|
877
943
|
this.slots = screen ? [screen, ...chosen] : chosen;
|
|
878
944
|
if (this.compositor) {
|
|
879
|
-
const layout = layoutSlots(chosen.length, Boolean(screen));
|
|
945
|
+
const layout = layoutSlots(chosen.length, Boolean(screen), this.layoutWidth, this.layoutHeight);
|
|
880
946
|
this.compositor.setSources([
|
|
881
947
|
...(screen ? [{
|
|
882
948
|
key: screen.key,
|
|
@@ -887,7 +953,6 @@ export class CanvasRecorder {
|
|
|
887
953
|
accent: false,
|
|
888
954
|
muted: false,
|
|
889
955
|
fit: "contain",
|
|
890
|
-
fillStyle: this.canvasBackground || STAGE_BACKGROUND,
|
|
891
956
|
radius: layout.screen?.radius ?? SCREEN_RADIUS,
|
|
892
957
|
placeholder: false,
|
|
893
958
|
}] : []),
|
|
@@ -904,6 +969,7 @@ export class CanvasRecorder {
|
|
|
904
969
|
muted: false,
|
|
905
970
|
fit: "cover",
|
|
906
971
|
placeholder: Boolean(slot.placeholder),
|
|
972
|
+
avatarBackground: slot.avatarBackground,
|
|
907
973
|
};
|
|
908
974
|
}),
|
|
909
975
|
]);
|
|
@@ -920,34 +986,6 @@ export class CanvasRecorder {
|
|
|
920
986
|
}
|
|
921
987
|
}
|
|
922
988
|
|
|
923
|
-
/** Synthetic webcam matching the live empty-stage avatar tile (native 1280×720, no upscale). */
|
|
924
|
-
ensurePlaceholderTrack(name, isLocal, key = `placeholder|${isLocal ? "local" : name}`) {
|
|
925
|
-
let entry = this.placeholderSources.get(key);
|
|
926
|
-
if (entry) {
|
|
927
|
-
entry.name = name;
|
|
928
|
-
entry.isLocal = isLocal;
|
|
929
|
-
entry.redraw();
|
|
930
|
-
return entry.track;
|
|
931
|
-
}
|
|
932
|
-
const canvas = document.createElement("canvas");
|
|
933
|
-
// Match recording output size so the compositor does not upscale (blur).
|
|
934
|
-
canvas.width = W;
|
|
935
|
-
canvas.height = H;
|
|
936
|
-
const ctx = canvas.getContext("2d", { alpha: false, desynchronized: true });
|
|
937
|
-
const stream = canvas.captureStream(this.fps);
|
|
938
|
-
const track = stream.getVideoTracks()[0];
|
|
939
|
-
entry = { track, canvas, name, isLocal, redraw: null, timer: null };
|
|
940
|
-
entry.redraw = () => {
|
|
941
|
-
// Full-frame synthetic track: avatar only. Tile chrome/name are drawn on the layout rect.
|
|
942
|
-
drawStagePlaceholder(ctx, 0, 0, canvas.width, canvas.height, entry.name, false, STAGE_BACKGROUND, { chrome: false });
|
|
943
|
-
track.requestFrame?.();
|
|
944
|
-
};
|
|
945
|
-
entry.timer = setInterval(entry.redraw, Math.round(1000 / this.fps));
|
|
946
|
-
this.placeholderSources.set(key, entry);
|
|
947
|
-
entry.redraw();
|
|
948
|
-
return track;
|
|
949
|
-
}
|
|
950
|
-
|
|
951
989
|
prunePlaceholders(keepKeys) {
|
|
952
990
|
const keep = keepKeys instanceof Set ? keepKeys : new Set(keepKeys || []);
|
|
953
991
|
for (const [key, entry] of [...this.placeholderSources]) {
|
|
@@ -1028,15 +1066,14 @@ export class CanvasRecorder {
|
|
|
1028
1066
|
if (!this.ctx || !this.capturing) return;
|
|
1029
1067
|
const cameras = this.slots.filter((slot) => slot.kind === "camera");
|
|
1030
1068
|
const screen = this.slots.find((slot) => slot.kind === "screen");
|
|
1031
|
-
const layout = layoutSlots(cameras.length, Boolean(screen));
|
|
1032
|
-
|
|
1069
|
+
const layout = layoutSlots(cameras.length, Boolean(screen), this.layoutWidth, this.layoutHeight);
|
|
1070
|
+
this.backdrop.paint(this.ctx, this.layoutWidth, this.layoutHeight);
|
|
1033
1071
|
if (screen && layout.screen) {
|
|
1034
1072
|
paint(this.ctx, screen.video, layout.screen, null, {
|
|
1035
1073
|
label: "",
|
|
1036
1074
|
accent: false,
|
|
1037
|
-
accentColor: this.canvasBackground,
|
|
1038
1075
|
fit: "contain",
|
|
1039
|
-
fillStyle:
|
|
1076
|
+
fillStyle: null,
|
|
1040
1077
|
radius: layout.screen.radius ?? SCREEN_RADIUS,
|
|
1041
1078
|
});
|
|
1042
1079
|
}
|
|
@@ -1050,6 +1087,7 @@ export class CanvasRecorder {
|
|
|
1050
1087
|
chrome: true,
|
|
1051
1088
|
radius,
|
|
1052
1089
|
muted: false,
|
|
1090
|
+
avatarBackground: slot.avatarBackground,
|
|
1053
1091
|
});
|
|
1054
1092
|
return;
|
|
1055
1093
|
}
|
|
@@ -1059,6 +1097,7 @@ export class CanvasRecorder {
|
|
|
1059
1097
|
accentColor: PRESENTER_ACCENT,
|
|
1060
1098
|
radius,
|
|
1061
1099
|
muted: false,
|
|
1100
|
+
avatarBackground: slot.avatarBackground,
|
|
1062
1101
|
});
|
|
1063
1102
|
});
|
|
1064
1103
|
this.capture?.requestFrame();
|
|
@@ -1241,7 +1280,7 @@ function drawStagePlaceholder(ctx, x, y, w, h, name, _accent = false, _accentCol
|
|
|
1241
1280
|
roundedRectPath(ctx, x, y, w, h, corner);
|
|
1242
1281
|
ctx.clip();
|
|
1243
1282
|
}
|
|
1244
|
-
fillNameTone(ctx, x, y, w, h, name);
|
|
1283
|
+
fillNameTone(ctx, x, y, w, h, name, options.avatarBackground || null);
|
|
1245
1284
|
drawInitialAvatar(ctx, x, y, w, h, name || placeholderLetter(name));
|
|
1246
1285
|
ctx.restore();
|
|
1247
1286
|
if (!chrome) return;
|
|
@@ -1257,6 +1296,8 @@ function paint(ctx, video, rect, crop, options = {}) {
|
|
|
1257
1296
|
const fit = options.fit === "contain" ? "contain" : "cover";
|
|
1258
1297
|
const fillStyle = options.fillStyle || STAGE_BACKGROUND;
|
|
1259
1298
|
if (!video || video.readyState < 2 || !video.videoWidth) {
|
|
1299
|
+
// Screenshare not decoded yet: leave the stage backdrop (no avatar block).
|
|
1300
|
+
if (fit === "contain") return;
|
|
1260
1301
|
paintRoundedTile(ctx, {
|
|
1261
1302
|
source: null,
|
|
1262
1303
|
rect,
|
|
@@ -1270,6 +1311,7 @@ function paint(ctx, video, rect, crop, options = {}) {
|
|
|
1270
1311
|
fit,
|
|
1271
1312
|
fillStyle,
|
|
1272
1313
|
placeholder: true,
|
|
1314
|
+
avatarBackground: options.avatarBackground || null,
|
|
1273
1315
|
});
|
|
1274
1316
|
return;
|
|
1275
1317
|
}
|