mediascale-sdk 1.0.10 → 1.0.11
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 +51 -8
- package/package.json +1 -1
- package/src/client.js +75 -30
- package/src/record-compositor.js +3 -3
- package/src/record-frame-compositor.js +211 -9
- package/src/record-worker.js +1 -1
- package/src/record.js +186 -71
package/docs/integration.md
CHANGED
|
@@ -98,13 +98,14 @@ client.setRecordingStage([
|
|
|
98
98
|
| Field | Meaning |
|
|
99
99
|
| --- | --- |
|
|
100
100
|
| `id` | Stable peer id for that person (use the same id you already use in your app; when they publish webcam, the SDK matches the tile by this id / publisher id) |
|
|
101
|
-
| `name` | Display name on the avatar tile |
|
|
101
|
+
| `name` | Display name on the avatar / video tile and its name pill |
|
|
102
102
|
| `local` | `true` for this browser’s own tile |
|
|
103
103
|
|
|
104
104
|
Rules:
|
|
105
105
|
|
|
106
106
|
- Call `setRecordingStage` on **add to stage**, **remove from stage**, and when names change — keep it identical to what participants see.
|
|
107
|
-
- Everyone in the list gets an **avatar** until their webcam is published; then that tile becomes **video**.
|
|
107
|
+
- Everyone in the list gets an **avatar** until their webcam is published; then that tile becomes **video**. The same `name` is drawn on the bottom-left name pill.
|
|
108
|
+
- At most **six** people are drawn on the canvas (list order); audio for everyone else is still mixed.
|
|
108
109
|
- Pass `[]` to clear the stage list; you normally keep at least the people currently on stage.
|
|
109
110
|
- `onStageChanged(people)` is optional if you only care about driving the recorder from your own stage state.
|
|
110
111
|
|
|
@@ -122,7 +123,7 @@ Copy `sdk/react-sdk/sample/app/room.js` into your app. The comments in that file
|
|
|
122
123
|
6. **Record** (host) starts automatically when enabled on the token / `createClient`, or from the toolbar via `startRecording()` / `stopRecording()`. You can toggle any time; the final file includes every segment and mirrors the live stage layout.
|
|
123
124
|
7. **End** leaves the session and saves the recording if this person was recording.
|
|
124
125
|
|
|
125
|
-
`recording` defaults to **false**. Set `createClient({ recording: true })` **or** put `recording: true` on the host JWT when the host should already be recording when they join (Stream Control “Enable recording” does this). Chunks start **immediately** on connect / `startRecording()` — mic and camera are not required. Until video is live, the recording canvas shows the
|
|
126
|
+
`recording` defaults to **false**. Set `createClient({ recording: true })` **or** put `recording: true` on the host JWT when the host should already be recording when they join (Stream Control “Enable recording” does this). Chunks start **immediately** on connect / `startRecording()` — mic and camera are not required. Until video is live, the recording canvas shows the host placeholder (magenta field, circular letter avatar, bottom-left **name pill**).
|
|
126
127
|
|
|
127
128
|
The room must run in the browser. `app/page.js`
|
|
128
129
|
|
|
@@ -215,7 +216,7 @@ await client.end(); // combines every segment into one file
|
|
|
215
216
|
|
|
216
217
|
| Function | What it does |
|
|
217
218
|
| --- | --- |
|
|
218
|
-
| `createClient({ endpoint, sessionId, token, recording, iceServers, publishLimits, ...callbacks })` | Prepares the client. `sessionId` and `token` are required. `endpoint` defaults to `http://localhost:8080`. `recording` defaults to **false**. Optional `publishLimits: { cameraBps, screenBps }` overrides portal join caps. |
|
|
219
|
+
| `createClient({ endpoint, sessionId, token, recording, recordingCanvasBackground, iceServers, publishLimits, ...callbacks })` | Prepares the client. `sessionId` and `token` are required. `endpoint` defaults to `http://localhost:8080`. `recording` defaults to **false**. Optional `recordingCanvasBackground` (CSS color, default `#2563eb`) is the base for the radial + wave canvas backdrop and the local/presenter accent border. Optional `publishLimits: { cameraBps, screenBps }` overrides portal join caps. |
|
|
219
220
|
| `connect()` | Checks the token and joins the session. If recording is enabled for a **host**, recording starts immediately in the background (same as `startRecording()`). Join does not wait for capture to finish preparing. |
|
|
220
221
|
| `subscribe()` | Starts playing the other people. Call this once after `connect()`. |
|
|
221
222
|
| `startCamera({ cameraId, resolution, track })` | Publishes the camera. Pass `track` to use your own live video track (SDK clones it). |
|
|
@@ -233,7 +234,7 @@ await client.end(); // combines every segment into one file
|
|
|
233
234
|
| `startRecording()` | Starts recording for this person (or resumes after `stopRecording()`). Chunks upload immediately — mic/camera not required. Empty stage uses the host placeholder tile until real video is published. Also used when the host left and a cohost should continue the same file. |
|
|
234
235
|
| `stopRecording()` | Pauses recording without saving. Call `startRecording()` again to continue; `end()` still combines all segments into one file. |
|
|
235
236
|
| `setRecordingCrop(source, crop)` | Crops a video source in the recording layout. `crop` is `{ x, y, w, h }` in 0..1 (same as subscriber whiteboard crop). Pass `null` to clear. |
|
|
236
|
-
| `setRecordingStage(people)` | **Required for stage-accurate recording.** Who appears on the recording stage: `[{ id, name, local? }]
|
|
237
|
+
| `setRecordingStage(people)` | **Required for stage-accurate recording.** Who appears on the recording stage: `[{ id, name, local? }]` (max six on canvas). Avatars + name pills until that peer’s webcam arrives. See [Recording must match the live stage](#recording-must-match-the-live-stage). |
|
|
237
238
|
| `getPeerId()` | Socket peer id for this browser after `connect()`. |
|
|
238
239
|
| `getPublishLimits()` | Current `{ cameraBps, screenBps }` used for outgoing video encode caps. |
|
|
239
240
|
| `getUserList()` / `updateSelf(...)` / `stageAction(...)` | Optional portal roster helpers used by the sample meet UI. Skip these if your app already manages users and stage on its own. |
|
|
@@ -295,7 +296,7 @@ Publish any `MediaStreamTrack` under your own id. Subscribers get that same id a
|
|
|
295
296
|
|
|
296
297
|
Reserved ids (use the dedicated APIs instead): `webcam`, `mic`, `screen`.
|
|
297
298
|
|
|
298
|
-
Custom **video** sources (for example `whiteboard`) use the large featured layout slot in the recording compositor, like screen share.
|
|
299
|
+
Custom **video** sources (for example `whiteboard`) use the large featured layout slot in the recording compositor, like screen share (cameras stack from the top in the side column; up to six).
|
|
299
300
|
|
|
300
301
|
### Whiteboard example
|
|
301
302
|
|
|
@@ -357,8 +358,8 @@ client.setRecordingCrop("whiteboard", null); // clear
|
|
|
357
358
|
### What gets recorded
|
|
358
359
|
|
|
359
360
|
- Chunks upload as soon as recording starts — **mic and camera are not required**.
|
|
360
|
-
- With no live video, the canvas
|
|
361
|
-
- When webcam, screen, or a featured custom video (for example whiteboard) is published, those tiles replace the placeholder
|
|
361
|
+
- With no live video, the canvas shows the empty-stage placeholder: magenta radial field, circular letter avatar, and a bottom-left **name pill** (display name from the host JWT / stage). Recording continues on that placeholder until real media appears.
|
|
362
|
+
- When webcam, screen, or a featured custom video (for example whiteboard) is published, those tiles replace the placeholder (see [Recording layout](#recording-layout) below).
|
|
362
363
|
- Local + remote mics (and screen audio) are mixed when available. If the browser leaves `AudioContext` suspended, silence is recorded until the first click/key in the tab unlocks audio mixing.
|
|
363
364
|
- `stopRecording()` only pauses. It does not upload a finished file. `startRecording()` afterward opens a new segment under the same recording id.
|
|
364
365
|
- `end()` (or idle finalize when everybody has left) combines every segment into **one** playable file.
|
|
@@ -366,6 +367,48 @@ client.setRecordingCrop("whiteboard", null); // clear
|
|
|
366
367
|
- The file keeps going when another person calls `startRecording()` after a handoff. Ask them to start before the previous recorder calls `end()`.
|
|
367
368
|
- If everybody leaves, the recording is still saved after a few minutes of silence.
|
|
368
369
|
|
|
370
|
+
### Recording layout
|
|
371
|
+
|
|
372
|
+
Canvas is **1280×720**. Video tiles and audio follow these rules:
|
|
373
|
+
|
|
374
|
+
| Case | Layout |
|
|
375
|
+
| --- | --- |
|
|
376
|
+
| Single camera or avatar (no screen / featured video) | **Full-bleed** — tile fills the canvas (no outer margin). **No border radius.** Name pill still drawn. Brand canvas background is not visible as a letterbox. |
|
|
377
|
+
| Two or more cameras (no screen) | Grid with even outer padding and gutters (`16px`), rounded tiles (`14px` radius), light border. Local / presenter tile gets an accent glow from `recordingCanvasBackground`. |
|
|
378
|
+
| Screen share or featured custom video (e.g. whiteboard) | Large main pane on the left; up to **six** cameras / avatars in a **right column stacked from the top** (fixed tile height so fewer cameras sit at the top, not stretched apart). Extra stage video beyond six is omitted from the canvas. |
|
|
379
|
+
| Audio | **Uncapped** — every mic and screen-audio track is mixed even if that person is not among the six video tiles. |
|
|
380
|
+
|
|
381
|
+
**Name pills**
|
|
382
|
+
|
|
383
|
+
- Every camera / avatar tile gets a bottom-left **name-only** pill (no mic or signal icons).
|
|
384
|
+
- Label comes from `setRecordingStage` `name`, else the track `displayName`, else the local host JWT name (`Guest` / `Host` as fallback).
|
|
385
|
+
- The pill is drawn even while the first video frame is still loading (dark tile + name).
|
|
386
|
+
- The large screen / whiteboard pane does **not** get a name pill.
|
|
387
|
+
|
|
388
|
+
Use `setRecordingStage([{ id, name, local? }, ...])` so avatar tiles and name pills match the live stage. Without it, only live tracks appear (plus a single host placeholder when nobody is publishing).
|
|
389
|
+
|
|
390
|
+
### Recording canvas background
|
|
391
|
+
|
|
392
|
+
The canvas behind multi-tile / screen layouts defaults to `#2563eb`. Pass any CSS color on `createClient`; the SDK paints a **radial gradient with soft wave glows** from that color:
|
|
393
|
+
|
|
394
|
+
```js
|
|
395
|
+
const client = createClient({
|
|
396
|
+
endpoint,
|
|
397
|
+
sessionId,
|
|
398
|
+
token,
|
|
399
|
+
recording: true,
|
|
400
|
+
// Base color for the radial + wave canvas background.
|
|
401
|
+
// Default: "#2563eb"
|
|
402
|
+
recordingCanvasBackground: "#6d28d9",
|
|
403
|
+
});
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
| Option | Default | Meaning |
|
|
407
|
+
| --- | --- | --- |
|
|
408
|
+
| `recordingCanvasBackground` | `#2563eb` | Base CSS color for the recording canvas gradient / wave backdrop and the local/presenter accent border (multi-tile layouts). |
|
|
409
|
+
|
|
410
|
+
Set this once at `createClient`. It applies to both the worker compositor and the foreground canvas fallback. On a **single full-bleed** camera/avatar tile the background is covered by the tile itself.
|
|
411
|
+
|
|
369
412
|
### Background-safe compositing
|
|
370
413
|
|
|
371
414
|
Recording prefers a **worker / OffscreenCanvas** compositor so frames keep flowing when the tab is backgrounded. If the browser cannot probe that path, it falls back to the foreground canvas compositor.
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -29,6 +29,12 @@ function normalizePublishLimits(raw = {}) {
|
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
/** CSS color for the recording canvas fill. Empty / invalid → #2563eb. */
|
|
33
|
+
function normalizeCanvasBackground(raw) {
|
|
34
|
+
const color = String(raw ?? "").trim();
|
|
35
|
+
return color || "#2563eb";
|
|
36
|
+
}
|
|
37
|
+
|
|
32
38
|
function readToken(token) {
|
|
33
39
|
const body = String(token || "").split(".")[0];
|
|
34
40
|
if (!body) throw new Error("token is required");
|
|
@@ -97,6 +103,8 @@ export function createClient(options = {}) {
|
|
|
97
103
|
// Explicit option wins; otherwise honor recording flag embedded in the host token.
|
|
98
104
|
const recording = options.recording === true
|
|
99
105
|
|| (options.recording !== false && claims.recording === true && claims.role === "host");
|
|
106
|
+
// Main recording canvas fill (letterbox / empty areas). Defaults to #2563eb.
|
|
107
|
+
const recordingCanvasBackground = normalizeCanvasBackground(options.recordingCanvasBackground);
|
|
100
108
|
|
|
101
109
|
const displayName = String(claims.name || "Guest").slice(0, 40);
|
|
102
110
|
// createClient({ publishLimits }) wins over portal join values when set.
|
|
@@ -115,6 +123,8 @@ export function createClient(options = {}) {
|
|
|
115
123
|
let subscribed = false;
|
|
116
124
|
let recorder = null;
|
|
117
125
|
let startingRecorder = null;
|
|
126
|
+
/** Sticky id for stop/start segments; replaced after a take is finalized/failed. */
|
|
127
|
+
let activeRecordingId = null;
|
|
118
128
|
let selfPeerId = null;
|
|
119
129
|
const locals = new Map();
|
|
120
130
|
const tiles = new Map();
|
|
@@ -497,6 +507,23 @@ export function createClient(options = {}) {
|
|
|
497
507
|
}
|
|
498
508
|
}
|
|
499
509
|
|
|
510
|
+
async function ensureRecorderAffinity() {
|
|
511
|
+
const webinarId = recordingWebinarId(sessionId);
|
|
512
|
+
try {
|
|
513
|
+
const response = await fetch(
|
|
514
|
+
`${endpoint}/recorder/api/v1/affinity?webinarId=${encodeURIComponent(webinarId)}`,
|
|
515
|
+
{ credentials: "include" },
|
|
516
|
+
);
|
|
517
|
+
if (!response.ok) return;
|
|
518
|
+
const body = await response.json().catch(() => null);
|
|
519
|
+
if (body?.peer && typeof document !== "undefined") {
|
|
520
|
+
document.cookie = `ms_recorder=${encodeURIComponent(body.peer)}; Path=/; Max-Age=86400; SameSite=Lax`;
|
|
521
|
+
}
|
|
522
|
+
} catch {
|
|
523
|
+
/* gateway may still route to the seed recorder */
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
500
527
|
async function connect() {
|
|
501
528
|
if (readyPromise) return readyPromise;
|
|
502
529
|
readyPromise = (async () => {
|
|
@@ -882,6 +909,44 @@ export function createClient(options = {}) {
|
|
|
882
909
|
onRecording(next.capturing ? "recording" : "armed");
|
|
883
910
|
}
|
|
884
911
|
|
|
912
|
+
async function openRecorder(recordingId) {
|
|
913
|
+
await ensureRecorderAffinity();
|
|
914
|
+
await emit(socket, "claimRecording", { recordingId });
|
|
915
|
+
if (closed || recorder) return;
|
|
916
|
+
const next = new CanvasRecorder({
|
|
917
|
+
webinarId: recordingWebinarId(sessionId),
|
|
918
|
+
webinarName: sessionId,
|
|
919
|
+
recordingId,
|
|
920
|
+
endpoint,
|
|
921
|
+
token,
|
|
922
|
+
selfName: displayName,
|
|
923
|
+
canvasBackground: recordingCanvasBackground,
|
|
924
|
+
// Chunks start immediately; empty stage uses the same host placeholder as live UI.
|
|
925
|
+
onCaptureChange: () => notifyRecording(next),
|
|
926
|
+
onFailure: (error) => {
|
|
927
|
+
if (recorder !== next) return;
|
|
928
|
+
recorder = null;
|
|
929
|
+
onRecording("stopped");
|
|
930
|
+
onError(error);
|
|
931
|
+
},
|
|
932
|
+
});
|
|
933
|
+
recorder = next;
|
|
934
|
+
next.wanted = true;
|
|
935
|
+
notifyRecording(next);
|
|
936
|
+
for (const [source, crop] of recordingCrops) next.setVideoCrop(source, crop);
|
|
937
|
+
next.sync(tracksForRecording(), stagePeopleForRecording());
|
|
938
|
+
try {
|
|
939
|
+
await next.start();
|
|
940
|
+
} catch (error) {
|
|
941
|
+
if (recorder === next) recorder = null;
|
|
942
|
+
onRecording("stopped");
|
|
943
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
944
|
+
}
|
|
945
|
+
if (recorder !== next) return;
|
|
946
|
+
activeRecordingId = recordingId;
|
|
947
|
+
notifyRecording(next);
|
|
948
|
+
}
|
|
949
|
+
|
|
885
950
|
async function startRecording() {
|
|
886
951
|
if (closed) throw new Error("The session has ended");
|
|
887
952
|
if (!sendTransport) await connect();
|
|
@@ -893,39 +958,18 @@ export function createClient(options = {}) {
|
|
|
893
958
|
}
|
|
894
959
|
if (startingRecorder) return startingRecorder;
|
|
895
960
|
startingRecorder = (async () => {
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
const next = new CanvasRecorder({
|
|
900
|
-
webinarId: recordingWebinarId(sessionId),
|
|
901
|
-
webinarName: sessionId,
|
|
902
|
-
recordingId,
|
|
903
|
-
endpoint,
|
|
904
|
-
token,
|
|
905
|
-
selfName: displayName,
|
|
906
|
-
// Chunks start immediately; empty stage uses the same host placeholder as live UI.
|
|
907
|
-
onCaptureChange: () => notifyRecording(next),
|
|
908
|
-
onFailure: (error) => {
|
|
909
|
-
if (recorder !== next) return;
|
|
910
|
-
recorder = null;
|
|
911
|
-
onRecording("stopped");
|
|
912
|
-
onError(error);
|
|
913
|
-
},
|
|
914
|
-
});
|
|
915
|
-
recorder = next;
|
|
916
|
-
next.wanted = true;
|
|
917
|
-
notifyRecording(next);
|
|
918
|
-
for (const [source, crop] of recordingCrops) next.setVideoCrop(source, crop);
|
|
919
|
-
next.sync(tracksForRecording(), stagePeopleForRecording());
|
|
961
|
+
// Prefer the session's stable id (handoff). If that take already finalized/failed,
|
|
962
|
+
// mint a new UUID so the host can record again in the same room.
|
|
963
|
+
let recordingId = activeRecordingId || await recordingIdFor(sessionId);
|
|
920
964
|
try {
|
|
921
|
-
await
|
|
965
|
+
await openRecorder(recordingId);
|
|
922
966
|
} catch (error) {
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
967
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
968
|
+
if (!/recording is already finished/i.test(message)) throw error;
|
|
969
|
+
recordingId = crypto.randomUUID();
|
|
970
|
+
activeRecordingId = recordingId;
|
|
971
|
+
await openRecorder(recordingId);
|
|
926
972
|
}
|
|
927
|
-
if (recorder !== next) return;
|
|
928
|
-
notifyRecording(next);
|
|
929
973
|
})();
|
|
930
974
|
try {
|
|
931
975
|
await startingRecorder;
|
|
@@ -953,6 +997,7 @@ export function createClient(options = {}) {
|
|
|
953
997
|
const forceFinalize = options.finalize === true || options.endSession === true;
|
|
954
998
|
const current = recorder;
|
|
955
999
|
recorder = null;
|
|
1000
|
+
if (forceFinalize) activeRecordingId = null;
|
|
956
1001
|
try {
|
|
957
1002
|
if (current) {
|
|
958
1003
|
let handoff = false;
|
package/src/record-compositor.js
CHANGED
|
@@ -30,7 +30,7 @@ function bounded(promise, timeoutMs) {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
/** Probe real frame transfer before selecting the background-capable compositor. */
|
|
33
|
-
export async function createWorkerCompositor({ width, height, fps, onError = () => {} }) {
|
|
33
|
+
export async function createWorkerCompositor({ width, height, fps, background = "#2563eb", onError = () => {} }) {
|
|
34
34
|
let worker, output, probeInput, probeClone, outputProbe, reader, inputWriter, probeFrame;
|
|
35
35
|
const deadline = Date.now() + 3000;
|
|
36
36
|
const remaining = () => Math.max(1, deadline - Date.now());
|
|
@@ -40,7 +40,7 @@ export async function createWorkerCompositor({ width, height, fps, onError = ()
|
|
|
40
40
|
worker = new Worker(new URL("./record-worker.js", import.meta.url), { type: "module" });
|
|
41
41
|
const { standard } = await ask({ type: "capabilities" });
|
|
42
42
|
if (standard) {
|
|
43
|
-
const ready = await ask({ type: "init", standard: true, width, height, fps });
|
|
43
|
+
const ready = await ask({ type: "init", standard: true, width, height, fps, background });
|
|
44
44
|
output = ready.track;
|
|
45
45
|
const canvas = document.createElement("canvas");
|
|
46
46
|
canvas.width = canvas.height = 2;
|
|
@@ -52,7 +52,7 @@ export async function createWorkerCompositor({ width, height, fps, onError = ()
|
|
|
52
52
|
} else {
|
|
53
53
|
if (typeof MediaStreamTrackProcessor !== "function" || typeof MediaStreamTrackGenerator !== "function") throw new Error("Native recording track processor/generator are unavailable");
|
|
54
54
|
output = new MediaStreamTrackGenerator({ kind: "video" });
|
|
55
|
-
await ask({ type: "init", width, height, fps, writable: output.writable }, [output.writable]);
|
|
55
|
+
await ask({ type: "init", width, height, fps, background, writable: output.writable }, [output.writable]);
|
|
56
56
|
outputProbe = output.clone();
|
|
57
57
|
reader = new MediaStreamTrackProcessor({ track: outputProbe, maxBufferSize: 1 }).readable.getReader();
|
|
58
58
|
const received = reader.read();
|
|
@@ -1,12 +1,188 @@
|
|
|
1
|
+
/** Parse `#rgb` / `#rrggbb` / `rgb()` / `rgba()` into `{ r, g, b }`. */
|
|
2
|
+
export function parseCssColor(input) {
|
|
3
|
+
const raw = String(input || "").trim();
|
|
4
|
+
if (!raw) return null;
|
|
5
|
+
const hex = raw.match(/^#([0-9a-f]{3}|[0-9a-f]{6})$/i);
|
|
6
|
+
if (hex) {
|
|
7
|
+
const h = hex[1];
|
|
8
|
+
const full = h.length === 3 ? h.split("").map((c) => c + c).join("") : h;
|
|
9
|
+
return {
|
|
10
|
+
r: Number.parseInt(full.slice(0, 2), 16),
|
|
11
|
+
g: Number.parseInt(full.slice(2, 4), 16),
|
|
12
|
+
b: Number.parseInt(full.slice(4, 6), 16),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
const rgb = raw.match(/^rgba?\(\s*([0-9.]+)\s*,\s*([0-9.]+)\s*,\s*([0-9.]+)/i);
|
|
16
|
+
if (rgb) {
|
|
17
|
+
return {
|
|
18
|
+
r: Math.round(Number(rgb[1])),
|
|
19
|
+
g: Math.round(Number(rgb[2])),
|
|
20
|
+
b: Math.round(Number(rgb[3])),
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function shadeRgb({ r, g, b }, factor) {
|
|
27
|
+
return {
|
|
28
|
+
r: Math.round(Math.min(255, Math.max(0, r * factor))),
|
|
29
|
+
g: Math.round(Math.min(255, Math.max(0, g * factor))),
|
|
30
|
+
b: Math.round(Math.min(255, Math.max(0, b * factor))),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function cssRgb({ r, g, b }, alpha = 1) {
|
|
35
|
+
if (alpha >= 1) return `rgb(${r}, ${g}, ${b})`;
|
|
36
|
+
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function softBlob(ctx, cx, cy, rx, ry, color, alpha) {
|
|
40
|
+
const g = ctx.createRadialGradient(cx, cy, 0, cx, cy, Math.max(rx, ry));
|
|
41
|
+
g.addColorStop(0, cssRgb(color, alpha));
|
|
42
|
+
g.addColorStop(0.55, cssRgb(color, alpha * 0.35));
|
|
43
|
+
g.addColorStop(1, cssRgb(color, 0));
|
|
44
|
+
ctx.fillStyle = g;
|
|
45
|
+
ctx.beginPath();
|
|
46
|
+
ctx.ellipse(cx, cy, rx, ry, 0, 0, Math.PI * 2);
|
|
47
|
+
ctx.fill();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Brand canvas backdrop: deep radial field + soft corner waves from `color`.
|
|
52
|
+
*/
|
|
53
|
+
export function fillRadialBackground(ctx, x, y, w, h, color = "#2563eb") {
|
|
54
|
+
const base = parseCssColor(color) || { r: 37, g: 99, b: 235 };
|
|
55
|
+
const deep = shadeRgb(base, 0.28);
|
|
56
|
+
const mid = shadeRgb(base, 0.72);
|
|
57
|
+
const glow = shadeRgb(base, 1.35);
|
|
58
|
+
const cx = x + w / 2;
|
|
59
|
+
const cy = y + h / 2;
|
|
60
|
+
|
|
61
|
+
ctx.fillStyle = cssRgb(deep);
|
|
62
|
+
ctx.fillRect(x, y, w, h);
|
|
63
|
+
|
|
64
|
+
const field = ctx.createRadialGradient(cx, cy - h * 0.08, 0, cx, cy, Math.max(w, h) * 0.9);
|
|
65
|
+
field.addColorStop(0, cssRgb(glow, 0.42));
|
|
66
|
+
field.addColorStop(0.4, cssRgb(mid, 0.55));
|
|
67
|
+
field.addColorStop(1, cssRgb(deep, 1));
|
|
68
|
+
ctx.fillStyle = field;
|
|
69
|
+
ctx.fillRect(x, y, w, h);
|
|
70
|
+
|
|
71
|
+
// Soft wave-like glows (top-left / bottom-right), matching the reference stage look.
|
|
72
|
+
softBlob(ctx, x + w * 0.18, y + h * 0.1, w * 0.42, h * 0.32, glow, 0.38);
|
|
73
|
+
softBlob(ctx, x + w * 0.82, y + h * 0.88, w * 0.4, h * 0.3, glow, 0.3);
|
|
74
|
+
softBlob(ctx, x + w * 0.7, y + h * 0.18, w * 0.28, h * 0.22, mid, 0.22);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function roundedRectPath(ctx, x, y, w, h, radius) {
|
|
78
|
+
const r = Math.max(0, Math.min(radius, w / 2, h / 2));
|
|
79
|
+
ctx.beginPath();
|
|
80
|
+
ctx.moveTo(x + r, y);
|
|
81
|
+
ctx.arcTo(x + w, y, x + w, y + h, r);
|
|
82
|
+
ctx.arcTo(x + w, y + h, x, y + h, r);
|
|
83
|
+
ctx.arcTo(x, y + h, x, y, r);
|
|
84
|
+
ctx.arcTo(x, y, x + w, y, r);
|
|
85
|
+
ctx.closePath();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Bottom-left name pill — text only (no mic / signal icons). */
|
|
89
|
+
export function drawNamePill(ctx, x, y, w, h, label) {
|
|
90
|
+
const text = String(label || "").trim().slice(0, 40);
|
|
91
|
+
if (!text) return;
|
|
92
|
+
const padX = Math.max(10, Math.round(Math.min(w, h) * 0.04));
|
|
93
|
+
const padY = Math.max(8, Math.round(Math.min(w, h) * 0.035));
|
|
94
|
+
const namePx = Math.round(Math.min(Math.max(Math.min(w, h) * 0.085, 11), 16));
|
|
95
|
+
ctx.save();
|
|
96
|
+
ctx.font = `600 ${namePx}px "Segoe UI", Roboto, Helvetica, Arial, sans-serif`;
|
|
97
|
+
ctx.textAlign = "left";
|
|
98
|
+
ctx.textBaseline = "middle";
|
|
99
|
+
const textW = ctx.measureText(text).width;
|
|
100
|
+
const pillH = namePx + padY;
|
|
101
|
+
const pillW = Math.min(w - padX * 2, textW + padX * 1.6);
|
|
102
|
+
const pillX = x + padX;
|
|
103
|
+
const pillY = y + h - padY - pillH;
|
|
104
|
+
const radius = pillH / 2;
|
|
105
|
+
roundedRectPath(ctx, pillX, pillY, pillW, pillH, radius);
|
|
106
|
+
ctx.fillStyle = "rgba(15, 23, 42, 0.72)";
|
|
107
|
+
ctx.fill();
|
|
108
|
+
ctx.fillStyle = "#ffffff";
|
|
109
|
+
ctx.fillText(text, pillX + padX * 0.75, pillY + pillH / 2 + 0.5, pillW - padX * 1.2);
|
|
110
|
+
ctx.restore();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function strokeTileBorder(ctx, x, y, w, h, radius, { accent = false, accentColor = "#a855f7" } = {}) {
|
|
114
|
+
if (!(radius > 0) && !accent) return;
|
|
115
|
+
const base = parseCssColor(accentColor) || { r: 168, g: 85, b: 247 };
|
|
116
|
+
ctx.save();
|
|
117
|
+
if (radius > 0) roundedRectPath(ctx, x + 0.5, y + 0.5, w - 1, h - 1, radius);
|
|
118
|
+
else {
|
|
119
|
+
ctx.beginPath();
|
|
120
|
+
ctx.rect(x + 0.5, y + 0.5, w - 1, h - 1);
|
|
121
|
+
}
|
|
122
|
+
if (accent) {
|
|
123
|
+
ctx.strokeStyle = cssRgb(shadeRgb(base, 1.15));
|
|
124
|
+
ctx.lineWidth = 3;
|
|
125
|
+
ctx.shadowColor = cssRgb(base, 0.85);
|
|
126
|
+
ctx.shadowBlur = 10;
|
|
127
|
+
} else {
|
|
128
|
+
ctx.strokeStyle = "rgba(255, 255, 255, 0.45)";
|
|
129
|
+
ctx.lineWidth = 1.5;
|
|
130
|
+
ctx.shadowBlur = 0;
|
|
131
|
+
}
|
|
132
|
+
ctx.stroke();
|
|
133
|
+
ctx.restore();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Draw a video/image source into a rounded tile with border + name pill.
|
|
138
|
+
* `source` may be an HTMLVideoElement or VideoFrame-like canvas image source.
|
|
139
|
+
*/
|
|
140
|
+
export function paintRoundedTile(ctx, {
|
|
141
|
+
source,
|
|
142
|
+
rect,
|
|
143
|
+
sx = 0,
|
|
144
|
+
sy = 0,
|
|
145
|
+
sw,
|
|
146
|
+
sh,
|
|
147
|
+
label = "",
|
|
148
|
+
accent = false,
|
|
149
|
+
accentColor = "#a855f7",
|
|
150
|
+
radius = 14,
|
|
151
|
+
fillStyle = "#0f172a",
|
|
152
|
+
}) {
|
|
153
|
+
const { x, y, w, h } = rect;
|
|
154
|
+
ctx.save();
|
|
155
|
+
if (radius > 0) {
|
|
156
|
+
roundedRectPath(ctx, x, y, w, h, radius);
|
|
157
|
+
ctx.clip();
|
|
158
|
+
}
|
|
159
|
+
ctx.fillStyle = fillStyle;
|
|
160
|
+
ctx.fillRect(x, y, w, h);
|
|
161
|
+
if (source && sw > 0 && sh > 0) {
|
|
162
|
+
const scale = Math.min(w / sw, h / sh);
|
|
163
|
+
const dw = sw * scale;
|
|
164
|
+
const dh = sh * scale;
|
|
165
|
+
const prev = ctx.imageSmoothingEnabled;
|
|
166
|
+
ctx.imageSmoothingEnabled = scale < 0.98;
|
|
167
|
+
ctx.drawImage(source, sx, sy, sw, sh, x + (w - dw) / 2, y + (h - dh) / 2, dw, dh);
|
|
168
|
+
ctx.imageSmoothingEnabled = prev;
|
|
169
|
+
}
|
|
170
|
+
ctx.restore();
|
|
171
|
+
if (radius > 0) strokeTileBorder(ctx, x, y, w, h, radius, { accent, accentColor });
|
|
172
|
+
if (label) drawNamePill(ctx, x, y, w, h, label);
|
|
173
|
+
}
|
|
174
|
+
|
|
1
175
|
/** Owns at most one input frame per source and one output write. */
|
|
2
176
|
export class FrameCompositor {
|
|
3
177
|
constructor({ canvas, ctx = canvas.getContext("2d"), writer, fps = 15,
|
|
178
|
+
background = "#2563eb",
|
|
4
179
|
now = () => performance.now(), makeFrame = (surface, init) => new VideoFrame(surface, init),
|
|
5
180
|
onError = () => {} }) {
|
|
6
181
|
this.canvas = canvas;
|
|
7
182
|
this.ctx = ctx;
|
|
8
183
|
this.writer = writer;
|
|
9
184
|
this.interval = 1000 / fps;
|
|
185
|
+
this.background = String(background || "").trim() || "#2563eb";
|
|
10
186
|
this.now = now;
|
|
11
187
|
this.makeFrame = makeFrame;
|
|
12
188
|
this.onError = onError;
|
|
@@ -51,11 +227,29 @@ export class FrameCompositor {
|
|
|
51
227
|
flush() {
|
|
52
228
|
const now = this.now();
|
|
53
229
|
if (!this.active || !this.dirty || this.writing || now - this.lastDraw < this.interval) return;
|
|
54
|
-
this.ctx.
|
|
55
|
-
|
|
56
|
-
|
|
230
|
+
fillRadialBackground(this.ctx, 0, 0, this.canvas.width, this.canvas.height, this.background);
|
|
231
|
+
for (const source of this.sources) {
|
|
232
|
+
const { key, rect, crop, label, accent, radius } = source;
|
|
57
233
|
const frame = this.frames.get(key);
|
|
58
|
-
if (!
|
|
234
|
+
if (!rect) continue;
|
|
235
|
+
const tileRadius = radius == null ? 14 : radius;
|
|
236
|
+
if (!frame) {
|
|
237
|
+
// Still show the name pill while the first video frame is arriving.
|
|
238
|
+
if (label) {
|
|
239
|
+
paintRoundedTile(this.ctx, {
|
|
240
|
+
source: null,
|
|
241
|
+
rect,
|
|
242
|
+
sw: 0,
|
|
243
|
+
sh: 0,
|
|
244
|
+
label,
|
|
245
|
+
accent: Boolean(accent),
|
|
246
|
+
accentColor: this.background,
|
|
247
|
+
radius: tileRadius,
|
|
248
|
+
fillStyle: "#0f172a",
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
59
253
|
const width = frame.displayWidth || frame.codedWidth || rect.w;
|
|
60
254
|
const height = frame.displayHeight || frame.codedHeight || rect.h;
|
|
61
255
|
let sx = 0;
|
|
@@ -70,11 +264,19 @@ export class FrameCompositor {
|
|
|
70
264
|
sh = c.h * height;
|
|
71
265
|
if (sw <= 1 || sh <= 1) continue;
|
|
72
266
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
267
|
+
paintRoundedTile(this.ctx, {
|
|
268
|
+
source: frame,
|
|
269
|
+
rect,
|
|
270
|
+
sx,
|
|
271
|
+
sy,
|
|
272
|
+
sw,
|
|
273
|
+
sh,
|
|
274
|
+
label: label || "",
|
|
275
|
+
accent: Boolean(accent),
|
|
276
|
+
accentColor: this.background,
|
|
277
|
+
radius: tileRadius,
|
|
278
|
+
fillStyle: "#0f172a",
|
|
279
|
+
});
|
|
78
280
|
}
|
|
79
281
|
this.timestamp = Math.max(this.timestamp + 1, Math.round((now - this.startedAt) * 1000));
|
|
80
282
|
const frame = this.makeFrame(this.canvas, { timestamp: this.timestamp });
|
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, onError: error });
|
|
49
|
+
compositor = new FrameCompositor({ canvas, writer, fps: data.fps, background: data.background, 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] : []);
|
package/src/record.js
CHANGED
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
import { createWorkerCompositor } from "./record-compositor.js";
|
|
2
|
-
|
|
2
|
+
import {
|
|
3
|
+
fillRadialBackground,
|
|
4
|
+
paintRoundedTile,
|
|
5
|
+
drawNamePill,
|
|
6
|
+
strokeTileBorder,
|
|
7
|
+
roundedRectPath,
|
|
8
|
+
} from "./record-frame-compositor.js";
|
|
9
|
+
export { FrameCompositor, fillRadialBackground } from "./record-frame-compositor.js";
|
|
3
10
|
|
|
4
11
|
const W = 1280;
|
|
5
12
|
const H = 720;
|
|
6
13
|
const COL = 240;
|
|
7
|
-
|
|
8
|
-
const
|
|
14
|
+
/** Match reference stage: even outer margin and gutters between tiles. */
|
|
15
|
+
const PAD = 16;
|
|
16
|
+
const GAP = 16;
|
|
17
|
+
const TILE_RADIUS = 14;
|
|
9
18
|
const FPS = 15;
|
|
10
19
|
const SLICE_MS = 5000;
|
|
20
|
+
/** Hard cap for on-canvas camera / avatar tiles (host included). Audio is uncapped. */
|
|
21
|
+
const MAX_STAGE_CAMERAS = 6;
|
|
11
22
|
/** Higher bitrate keeps sharp avatar/text edges; 2 Mbps made placeholders look blurry. */
|
|
12
23
|
const BITRATE = 4_500_000;
|
|
13
24
|
const mime = "video/webm;codecs=vp8,opus";
|
|
@@ -18,29 +29,37 @@ export function recordingWebinarId(channelId) {
|
|
|
18
29
|
return cleaned || "webinar";
|
|
19
30
|
}
|
|
20
31
|
|
|
21
|
-
/** Equal tiles, or one screenshare with cameras stacked in a
|
|
32
|
+
/** Equal tiles, or one screenshare with cameras stacked from the top in a side column. Up to 6 cameras. */
|
|
22
33
|
export function layoutSlots(cameras, screen) {
|
|
34
|
+
const n = Math.max(0, Math.min(MAX_STAGE_CAMERAS, Number(cameras) || 0));
|
|
35
|
+
// One tile only — fill the canvas so recordingCanvasBackground is not visible as a border.
|
|
36
|
+
if (screen && n === 0) {
|
|
37
|
+
return { screen: { x: 0, y: 0, w: W, h: H }, cameras: [] };
|
|
38
|
+
}
|
|
39
|
+
if (!screen && n === 1) {
|
|
40
|
+
return { cameras: [{ x: 0, y: 0, w: W, h: H }] };
|
|
41
|
+
}
|
|
23
42
|
if (screen) {
|
|
24
43
|
const mainW = W - COL - PAD * 2 - GAP;
|
|
25
44
|
const mainH = H - PAD * 2;
|
|
26
|
-
|
|
27
|
-
const
|
|
45
|
+
// Fixed tile height so up to 6 fit; fewer cameras stack from the top (not stretched apart).
|
|
46
|
+
const tileH = Math.floor((mainH - GAP * (MAX_STAGE_CAMERAS - 1)) / MAX_STAGE_CAMERAS);
|
|
28
47
|
return {
|
|
29
48
|
screen: { x: PAD, y: PAD, w: mainW, h: mainH },
|
|
30
|
-
cameras: Array.from({ length:
|
|
49
|
+
cameras: Array.from({ length: n }, (_, i) => ({
|
|
31
50
|
x: PAD + mainW + GAP,
|
|
32
|
-
y: PAD + i * (
|
|
51
|
+
y: PAD + i * (tileH + GAP),
|
|
33
52
|
w: COL,
|
|
34
|
-
h:
|
|
53
|
+
h: tileH,
|
|
35
54
|
})),
|
|
36
55
|
};
|
|
37
56
|
}
|
|
38
|
-
const cols =
|
|
39
|
-
const rows =
|
|
57
|
+
const cols = n <= 1 ? 1 : n <= 2 ? 2 : n <= 4 ? 2 : 3;
|
|
58
|
+
const rows = Math.max(1, Math.ceil(n / cols));
|
|
40
59
|
const cellW = Math.floor((W - PAD * 2 - GAP * (cols - 1)) / cols);
|
|
41
60
|
const cellH = Math.floor((H - PAD * 2 - GAP * (rows - 1)) / rows);
|
|
42
61
|
return {
|
|
43
|
-
cameras: Array.from({ length:
|
|
62
|
+
cameras: Array.from({ length: n }, (_, i) => ({
|
|
44
63
|
x: PAD + (i % cols) * (cellW + GAP),
|
|
45
64
|
y: PAD + Math.floor(i / cols) * (cellH + GAP),
|
|
46
65
|
w: cellW,
|
|
@@ -73,6 +92,7 @@ export function tracksFromTiles(tiles) {
|
|
|
73
92
|
if (!item?.stream) continue;
|
|
74
93
|
const participantKey = item.local ? "local" : (item.publisherId || item.displayName || item.producerId);
|
|
75
94
|
const crop = normalizeVideoCrop(item.crop);
|
|
95
|
+
const displayName = String(item.displayName || "").trim().slice(0, 40);
|
|
76
96
|
for (const track of item.stream.getTracks()) {
|
|
77
97
|
if (track.readyState === "ended") continue;
|
|
78
98
|
const source = item.source === "screen" && track.kind === "audio" ? "screen-audio" : item.source;
|
|
@@ -82,6 +102,7 @@ export function tracksFromTiles(tiles) {
|
|
|
82
102
|
kind: track.kind,
|
|
83
103
|
isLocal: Boolean(item.local),
|
|
84
104
|
track,
|
|
105
|
+
...(displayName ? { displayName } : {}),
|
|
85
106
|
...(crop && track.kind === "video" ? { crop } : {}),
|
|
86
107
|
});
|
|
87
108
|
}
|
|
@@ -89,6 +110,23 @@ export function tracksFromTiles(tiles) {
|
|
|
89
110
|
return out;
|
|
90
111
|
}
|
|
91
112
|
|
|
113
|
+
function resolveTileLabel(slot, selfName) {
|
|
114
|
+
const fromSlot = String(slot?.displayName || "").trim();
|
|
115
|
+
if (fromSlot) return fromSlot.slice(0, 40);
|
|
116
|
+
if (slot?.isLocal) return String(selfName || "Host").slice(0, 40);
|
|
117
|
+
const key = String(slot?.participantKey || "").trim();
|
|
118
|
+
if (key && key !== "local") return key.slice(0, 40);
|
|
119
|
+
return "Guest";
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function isFullBleedRect(rect) {
|
|
123
|
+
return rect
|
|
124
|
+
&& rect.x <= 0
|
|
125
|
+
&& rect.y <= 0
|
|
126
|
+
&& rect.w >= W - 1
|
|
127
|
+
&& rect.h >= H - 1;
|
|
128
|
+
}
|
|
129
|
+
|
|
92
130
|
/** Screen share and custom video (e.g. whiteboard) use the large featured slot. */
|
|
93
131
|
function isFeaturedVideoSource(source) {
|
|
94
132
|
return source === "screen" || (source && source !== "webcam");
|
|
@@ -113,7 +151,7 @@ export class CanvasRecorder {
|
|
|
113
151
|
return typeof MediaRecorder !== "undefined" && MediaRecorder.isTypeSupported(mime);
|
|
114
152
|
}
|
|
115
153
|
|
|
116
|
-
constructor({ webinarId, webinarName, recordingId, endpoint, token, selfName, onCaptureChange, onFailure }) {
|
|
154
|
+
constructor({ webinarId, webinarName, recordingId, endpoint, token, selfName, canvasBackground, onCaptureChange, onFailure }) {
|
|
117
155
|
this.webinarId = webinarId;
|
|
118
156
|
this.webinarName = webinarName || webinarId;
|
|
119
157
|
this.recordingId = recordingId;
|
|
@@ -122,6 +160,7 @@ export class CanvasRecorder {
|
|
|
122
160
|
this.stagePeople = null;
|
|
123
161
|
this.token = token || "";
|
|
124
162
|
this.selfName = String(selfName || "Host").slice(0, 40);
|
|
163
|
+
this.canvasBackground = String(canvasBackground || "").trim() || "#2563eb";
|
|
125
164
|
this.onCaptureChange = typeof onCaptureChange === "function" ? onCaptureChange : () => {};
|
|
126
165
|
this.onFailure = typeof onFailure === "function" ? onFailure : () => {};
|
|
127
166
|
this.segment = 0;
|
|
@@ -188,9 +227,18 @@ export class CanvasRecorder {
|
|
|
188
227
|
try {
|
|
189
228
|
// Start chunks immediately (placeholder stage + silence until real media appears).
|
|
190
229
|
await this.prepare(this.audio);
|
|
191
|
-
if (this.finishing)
|
|
230
|
+
if (this.finishing) {
|
|
231
|
+
markReady();
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
192
234
|
await this.updateCapture();
|
|
193
|
-
if (this.finishing)
|
|
235
|
+
if (this.finishing) {
|
|
236
|
+
markReady();
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
// Resolve start() only after the first segment opens (or prepare finishes when paused),
|
|
240
|
+
// so "recording is already finished" and similar errors reach startRecording().
|
|
241
|
+
markReady();
|
|
194
242
|
await this.untilStop;
|
|
195
243
|
} catch (err) {
|
|
196
244
|
fail(err);
|
|
@@ -205,11 +253,9 @@ export class CanvasRecorder {
|
|
|
205
253
|
this.teardown();
|
|
206
254
|
return;
|
|
207
255
|
}
|
|
208
|
-
markReady();
|
|
209
256
|
await run();
|
|
210
257
|
});
|
|
211
258
|
} else {
|
|
212
|
-
markReady();
|
|
213
259
|
void run();
|
|
214
260
|
}
|
|
215
261
|
await running;
|
|
@@ -358,6 +404,7 @@ export class CanvasRecorder {
|
|
|
358
404
|
async api(method, path, body) {
|
|
359
405
|
const res = await fetch(this.url(path), {
|
|
360
406
|
method,
|
|
407
|
+
credentials: "include",
|
|
361
408
|
headers: this.authHeaders({ "content-type": "application/json" }),
|
|
362
409
|
body: body === undefined ? "{}" : JSON.stringify(body),
|
|
363
410
|
});
|
|
@@ -387,11 +434,17 @@ export class CanvasRecorder {
|
|
|
387
434
|
this.clock.connect(silence);
|
|
388
435
|
silence.connect(this.dest);
|
|
389
436
|
this.clock.start();
|
|
390
|
-
const { compositor, reason } = await createWorkerCompositor({
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
437
|
+
const { compositor, reason } = await createWorkerCompositor({
|
|
438
|
+
width: W,
|
|
439
|
+
height: H,
|
|
440
|
+
fps: FPS,
|
|
441
|
+
background: this.canvasBackground,
|
|
442
|
+
onError: (failure) => {
|
|
443
|
+
this.compositorFailure = failure;
|
|
444
|
+
window.dispatchEvent(new CustomEvent("mediascale:recording-error", { detail: { message: failure.message } }));
|
|
445
|
+
void this.setWanted(false).catch(() => {});
|
|
446
|
+
},
|
|
447
|
+
});
|
|
395
448
|
if (this.finishing) { compositor?.close(); return; }
|
|
396
449
|
this.compositor = compositor;
|
|
397
450
|
let video = compositor?.track;
|
|
@@ -400,8 +453,7 @@ export class CanvasRecorder {
|
|
|
400
453
|
this.canvas.width = W;
|
|
401
454
|
this.canvas.height = H;
|
|
402
455
|
this.ctx = this.canvas.getContext("2d");
|
|
403
|
-
this.ctx
|
|
404
|
-
this.ctx.fillRect(0, 0, W, H);
|
|
456
|
+
fillRadialBackground(this.ctx, 0, 0, W, H, this.canvasBackground);
|
|
405
457
|
const url = URL.createObjectURL(new Blob([`let tick; onmessage = ({data}) => { clearInterval(tick); if (data) tick = setInterval(() => postMessage(0), ${Math.round(1000 / FPS)}); };`], { type: "text/javascript" }));
|
|
406
458
|
const worker = new Worker(url);
|
|
407
459
|
worker.onmessage = () => this.draw();
|
|
@@ -539,6 +591,7 @@ export class CanvasRecorder {
|
|
|
539
591
|
isLocal: item.isLocal,
|
|
540
592
|
participantKey: item.participantKey,
|
|
541
593
|
source: item.source,
|
|
594
|
+
displayName: String(item.displayName || "").trim().slice(0, 40),
|
|
542
595
|
order: this.orderOf(key),
|
|
543
596
|
video: this.compositor ? null : this.videoFor(key, item.track),
|
|
544
597
|
track: item.track,
|
|
@@ -548,7 +601,7 @@ export class CanvasRecorder {
|
|
|
548
601
|
}
|
|
549
602
|
const screen = [...screens.values()].find((slot) => slot.isLocal)
|
|
550
603
|
?? [...screens.values()].sort((a, b) => a.order - b.order)[0];
|
|
551
|
-
const maxCams =
|
|
604
|
+
const maxCams = MAX_STAGE_CAMERAS;
|
|
552
605
|
const stage = Array.isArray(this.stagePeople) ? this.stagePeople : null;
|
|
553
606
|
let chosen = [];
|
|
554
607
|
const keepPlaceholders = new Set();
|
|
@@ -602,7 +655,12 @@ export class CanvasRecorder {
|
|
|
602
655
|
const remotes = [...cameras.values()]
|
|
603
656
|
.filter((slot) => !slot.isLocal)
|
|
604
657
|
.sort((a, b) => a.order - b.order);
|
|
605
|
-
chosen = (localCam ? [localCam, ...remotes] : remotes)
|
|
658
|
+
chosen = (localCam ? [localCam, ...remotes] : remotes)
|
|
659
|
+
.slice(0, maxCams)
|
|
660
|
+
.map((slot) => ({
|
|
661
|
+
...slot,
|
|
662
|
+
displayName: resolveTileLabel(slot, this.selfName),
|
|
663
|
+
}));
|
|
606
664
|
if (chosen.length === 0) {
|
|
607
665
|
const name = this.selfName || "Host";
|
|
608
666
|
const phKey = "placeholder|local";
|
|
@@ -626,11 +684,32 @@ export class CanvasRecorder {
|
|
|
626
684
|
if (this.compositor) {
|
|
627
685
|
const layout = layoutSlots(chosen.length, Boolean(screen));
|
|
628
686
|
this.compositor.setSources([
|
|
629
|
-
...(screen ? [{
|
|
630
|
-
|
|
687
|
+
...(screen ? [{
|
|
688
|
+
key: screen.key,
|
|
689
|
+
track: screen.track,
|
|
690
|
+
rect: layout.screen,
|
|
691
|
+
crop: screen.crop || null,
|
|
692
|
+
label: "",
|
|
693
|
+
accent: false,
|
|
694
|
+
}] : []),
|
|
695
|
+
...chosen.map((slot, index) => {
|
|
696
|
+
const rect = layout.cameras[index];
|
|
697
|
+
const label = resolveTileLabel(slot, this.selfName);
|
|
698
|
+
const fullBleed = isFullBleedRect(rect);
|
|
699
|
+
return {
|
|
700
|
+
key: slot.key,
|
|
701
|
+
track: slot.track,
|
|
702
|
+
rect,
|
|
703
|
+
crop: null,
|
|
704
|
+
label,
|
|
705
|
+
// Presenter / local host gets the accent glow border from the brand color.
|
|
706
|
+
accent: !fullBleed && (Boolean(slot.isLocal) || /presenter/i.test(label)),
|
|
707
|
+
radius: fullBleed ? 0 : TILE_RADIUS,
|
|
708
|
+
};
|
|
709
|
+
}),
|
|
631
710
|
]);
|
|
632
711
|
}
|
|
633
|
-
// Mix every mic (and screen audio).
|
|
712
|
+
// Mix every mic (and screen audio). Video layout caps at MAX_STAGE_CAMERAS; audio has no cap.
|
|
634
713
|
this.mixAudio(chosen, screen, mics, screenAudio);
|
|
635
714
|
const liveKeys = new Set(this.slots.filter((slot) => slot.video).map((slot) => slot.key));
|
|
636
715
|
for (const key of [...this.videos.keys()]) {
|
|
@@ -660,7 +739,8 @@ export class CanvasRecorder {
|
|
|
660
739
|
const track = stream.getVideoTracks()[0];
|
|
661
740
|
entry = { track, canvas, name, isLocal, redraw: null, timer: null };
|
|
662
741
|
entry.redraw = () => {
|
|
663
|
-
|
|
742
|
+
// Full-frame synthetic track: avatar only. Tile chrome/name are drawn on the layout rect.
|
|
743
|
+
drawStagePlaceholder(ctx, 0, 0, canvas.width, canvas.height, entry.name, false, "#2563eb", { chrome: false });
|
|
664
744
|
track.requestFrame?.();
|
|
665
745
|
};
|
|
666
746
|
entry.timer = setInterval(entry.redraw, Math.round(1000 / FPS));
|
|
@@ -750,25 +830,34 @@ export class CanvasRecorder {
|
|
|
750
830
|
const cameras = this.slots.filter((slot) => slot.kind === "camera");
|
|
751
831
|
const screen = this.slots.find((slot) => slot.kind === "screen");
|
|
752
832
|
const layout = layoutSlots(cameras.length, Boolean(screen));
|
|
753
|
-
this.ctx
|
|
754
|
-
|
|
755
|
-
|
|
833
|
+
fillRadialBackground(this.ctx, 0, 0, W, H, this.canvasBackground);
|
|
834
|
+
if (screen && layout.screen) {
|
|
835
|
+
paint(this.ctx, screen.video, layout.screen, screen.crop, {
|
|
836
|
+
label: "",
|
|
837
|
+
accent: false,
|
|
838
|
+
accentColor: this.canvasBackground,
|
|
839
|
+
});
|
|
840
|
+
}
|
|
756
841
|
cameras.forEach((slot, index) => {
|
|
757
842
|
const rect = layout.cameras[index];
|
|
758
843
|
if (!rect) return;
|
|
844
|
+
const label = resolveTileLabel(slot, this.selfName);
|
|
845
|
+
const fullBleed = isFullBleedRect(rect);
|
|
846
|
+
const accent = !fullBleed && (Boolean(slot.isLocal) || /presenter/i.test(label));
|
|
847
|
+
const radius = fullBleed ? 0 : TILE_RADIUS;
|
|
759
848
|
if (slot.placeholder) {
|
|
760
|
-
drawStagePlaceholder(
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
rect.w,
|
|
765
|
-
rect.h,
|
|
766
|
-
slot.displayName || this.selfName,
|
|
767
|
-
Boolean(slot.isLocal),
|
|
768
|
-
);
|
|
849
|
+
drawStagePlaceholder(this.ctx, rect.x, rect.y, rect.w, rect.h, label, accent, this.canvasBackground, {
|
|
850
|
+
chrome: true,
|
|
851
|
+
radius,
|
|
852
|
+
});
|
|
769
853
|
return;
|
|
770
854
|
}
|
|
771
|
-
paint(this.ctx, slot.video, rect, null
|
|
855
|
+
paint(this.ctx, slot.video, rect, null, {
|
|
856
|
+
label,
|
|
857
|
+
accent,
|
|
858
|
+
accentColor: this.canvasBackground,
|
|
859
|
+
radius,
|
|
860
|
+
});
|
|
772
861
|
});
|
|
773
862
|
this.capture?.requestFrame();
|
|
774
863
|
}
|
|
@@ -817,6 +906,7 @@ export class CanvasRecorder {
|
|
|
817
906
|
const checksum = await sha256(row.blob);
|
|
818
907
|
const res = await fetch(this.url(`/api/chunks/${this.recordingId}/${row.segment}/${row.seq}`), {
|
|
819
908
|
method: "PUT",
|
|
909
|
+
credentials: "include",
|
|
820
910
|
headers: this.authHeaders({ "content-type": "application/octet-stream", "x-checksum": checksum }),
|
|
821
911
|
body: row.blob,
|
|
822
912
|
});
|
|
@@ -833,6 +923,7 @@ export class CanvasRecorder {
|
|
|
833
923
|
const durationMs = Date.now() - this.startedAt;
|
|
834
924
|
fetch(this.url(`/api/chunks/${this.recordingId}/finalize`), {
|
|
835
925
|
method: "POST",
|
|
926
|
+
credentials: "include",
|
|
836
927
|
headers: this.authHeaders({ "content-type": "application/json" }),
|
|
837
928
|
body: JSON.stringify({ durationMs, force: true }),
|
|
838
929
|
keepalive: true,
|
|
@@ -849,6 +940,7 @@ export class CanvasRecorder {
|
|
|
849
940
|
for (let round = 0; round < 3; round += 1) {
|
|
850
941
|
const res = await fetch(this.url(`/api/chunks/${this.recordingId}/finalize`), {
|
|
851
942
|
method: "POST",
|
|
943
|
+
credentials: "include",
|
|
852
944
|
headers: this.authHeaders({ "content-type": "application/json" }),
|
|
853
945
|
body: JSON.stringify({ durationMs }),
|
|
854
946
|
});
|
|
@@ -865,6 +957,7 @@ export class CanvasRecorder {
|
|
|
865
957
|
if (!still) {
|
|
866
958
|
await fetch(this.url(`/api/chunks/${this.recordingId}/finalize`), {
|
|
867
959
|
method: "POST",
|
|
960
|
+
credentials: "include",
|
|
868
961
|
headers: this.authHeaders({ "content-type": "application/json" }),
|
|
869
962
|
body: JSON.stringify({ durationMs, force: true }),
|
|
870
963
|
});
|
|
@@ -873,6 +966,7 @@ export class CanvasRecorder {
|
|
|
873
966
|
}
|
|
874
967
|
await fetch(this.url(`/api/chunks/${this.recordingId}/finalize`), {
|
|
875
968
|
method: "POST",
|
|
969
|
+
credentials: "include",
|
|
876
970
|
headers: this.authHeaders({ "content-type": "application/json" }),
|
|
877
971
|
body: JSON.stringify({ durationMs, force: true }),
|
|
878
972
|
});
|
|
@@ -924,12 +1018,18 @@ function placeholderLetter(name) {
|
|
|
924
1018
|
}
|
|
925
1019
|
|
|
926
1020
|
/**
|
|
927
|
-
* Empty-stage tile
|
|
928
|
-
* maroon radial field, solid #e30096 name round (no border), white initial, name top-left.
|
|
1021
|
+
* Empty-stage tile: magenta avatar field. With chrome: rounded border + name pill (no mic/signal icons).
|
|
929
1022
|
*/
|
|
930
|
-
function drawStagePlaceholder(ctx, x, y, w, h, name,
|
|
1023
|
+
function drawStagePlaceholder(ctx, x, y, w, h, name, accent = false, accentColor = "#2563eb", options = {}) {
|
|
1024
|
+
const chrome = options.chrome !== false;
|
|
1025
|
+
const corner = options.radius ?? TILE_RADIUS;
|
|
931
1026
|
const cx = Math.round(x + w / 2);
|
|
932
1027
|
const cy = Math.round(y + h / 2);
|
|
1028
|
+
ctx.save();
|
|
1029
|
+
if (chrome && corner > 0) {
|
|
1030
|
+
roundedRectPath(ctx, x, y, w, h, corner);
|
|
1031
|
+
ctx.clip();
|
|
1032
|
+
}
|
|
933
1033
|
const span = Math.max(w, h) * 0.75;
|
|
934
1034
|
const field = ctx.createRadialGradient(cx, cy, 0, cx, cy, span);
|
|
935
1035
|
field.addColorStop(0, "rgb(152, 62, 107)");
|
|
@@ -938,35 +1038,45 @@ function drawStagePlaceholder(ctx, x, y, w, h, name, _isLocal) {
|
|
|
938
1038
|
ctx.fillStyle = field;
|
|
939
1039
|
ctx.fillRect(x, y, w, h);
|
|
940
1040
|
|
|
941
|
-
const diameter = Math.round(Math.min(128, Math.max(
|
|
942
|
-
const
|
|
1041
|
+
const diameter = Math.round(Math.min(128, Math.max(72, Math.min(w, h) * 0.28)));
|
|
1042
|
+
const avatarR = diameter / 2;
|
|
943
1043
|
ctx.beginPath();
|
|
944
|
-
ctx.arc(cx, cy,
|
|
1044
|
+
ctx.arc(cx, cy, avatarR, 0, Math.PI * 2);
|
|
945
1045
|
ctx.fillStyle = "#e30096";
|
|
946
1046
|
ctx.fill();
|
|
947
1047
|
|
|
948
|
-
const letterPx = Math.round(Math.min(Math.max(diameter * 0.42,
|
|
1048
|
+
const letterPx = Math.round(Math.min(Math.max(diameter * 0.42, 22), 45));
|
|
949
1049
|
ctx.fillStyle = "#ffffff";
|
|
950
1050
|
ctx.font = `600 ${letterPx}px "Segoe UI", Roboto, Helvetica, Arial, sans-serif`;
|
|
951
1051
|
ctx.textAlign = "center";
|
|
952
1052
|
ctx.textBaseline = "middle";
|
|
953
1053
|
ctx.fillText(placeholderLetter(name), cx, cy + 1);
|
|
1054
|
+
ctx.restore();
|
|
954
1055
|
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
const namePx = Math.round(Math.min(Math.max(h * 0.028, 14), 18));
|
|
959
|
-
ctx.textAlign = "left";
|
|
960
|
-
ctx.textBaseline = "top";
|
|
961
|
-
ctx.font = `500 ${namePx}px "Segoe UI", Roboto, Helvetica, Arial, sans-serif`;
|
|
962
|
-
ctx.fillStyle = "#ffffff";
|
|
963
|
-
ctx.fillText(label, x + padX, y + padY);
|
|
1056
|
+
if (!chrome) return;
|
|
1057
|
+
if (corner > 0) strokeTileBorder(ctx, x, y, w, h, corner, { accent, accentColor });
|
|
1058
|
+
drawNamePill(ctx, x, y, w, h, name);
|
|
964
1059
|
}
|
|
965
1060
|
|
|
966
|
-
function paint(ctx, video, rect, crop) {
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
1061
|
+
function paint(ctx, video, rect, crop, options = {}) {
|
|
1062
|
+
const label = options.label || "";
|
|
1063
|
+
const accent = Boolean(options.accent);
|
|
1064
|
+
const accentColor = options.accentColor || "#2563eb";
|
|
1065
|
+
const radius = options.radius ?? TILE_RADIUS;
|
|
1066
|
+
if (!video || video.readyState < 2 || !video.videoWidth) {
|
|
1067
|
+
paintRoundedTile(ctx, {
|
|
1068
|
+
source: null,
|
|
1069
|
+
rect,
|
|
1070
|
+
sw: 0,
|
|
1071
|
+
sh: 0,
|
|
1072
|
+
label,
|
|
1073
|
+
accent,
|
|
1074
|
+
accentColor,
|
|
1075
|
+
radius,
|
|
1076
|
+
fillStyle: "#0f172a",
|
|
1077
|
+
});
|
|
1078
|
+
return;
|
|
1079
|
+
}
|
|
970
1080
|
let sx = 0;
|
|
971
1081
|
let sy = 0;
|
|
972
1082
|
let sw = video.videoWidth;
|
|
@@ -979,14 +1089,19 @@ function paint(ctx, video, rect, crop) {
|
|
|
979
1089
|
sh = cropNorm.h * video.videoHeight;
|
|
980
1090
|
if (sw <= 1 || sh <= 1) return;
|
|
981
1091
|
}
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1092
|
+
paintRoundedTile(ctx, {
|
|
1093
|
+
source: video,
|
|
1094
|
+
rect,
|
|
1095
|
+
sx,
|
|
1096
|
+
sy,
|
|
1097
|
+
sw,
|
|
1098
|
+
sh,
|
|
1099
|
+
label,
|
|
1100
|
+
accent,
|
|
1101
|
+
accentColor,
|
|
1102
|
+
radius,
|
|
1103
|
+
fillStyle: "#0f172a",
|
|
1104
|
+
});
|
|
990
1105
|
}
|
|
991
1106
|
|
|
992
1107
|
function sleep(ms) {
|