mediascale-sdk 1.0.12 → 1.0.13

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/src/index.js CHANGED
@@ -1,2 +1,4 @@
1
1
  export { createClient } from "./client.js";
2
2
  export { FrameCompositor } from "./record-frame-compositor.js";
3
+ export { DEFAULT_RECORDING_FPS } from "./record.js";
4
+ export { DEFAULT_RTMP_FPS, DEFAULT_RTMP_BITRATE } from "./live.js";
package/src/live.js ADDED
@@ -0,0 +1,100 @@
1
+ import { CanvasRecorder, clampFps } from "./record.js";
2
+
3
+ /** Default live (RTMP) frame rate. Override with createClient({ rtmpFps }). */
4
+ export const DEFAULT_RTMP_FPS = 30;
5
+ /** Default live (RTMP) video bitrate in bps. Override with createClient({ rtmpBitrate }). */
6
+ export const DEFAULT_RTMP_BITRATE = 2_500_000;
7
+
8
+ /**
9
+ * Stage compositor for RTMP live streaming.
10
+ *
11
+ * Same layout as the recording (screen + up to six cameras, name pills, brand
12
+ * background, mixed audio), but with its own frame rate and no MediaRecorder,
13
+ * IndexedDB, or chunk upload. Its `videoTrack` / `audioTrack` are published to
14
+ * the origin as hidden `rtmp-video` (H264) / `rtmp-audio` (Opus) tracks.
15
+ */
16
+ export class LiveCompositor extends CanvasRecorder {
17
+ static supported() {
18
+ return typeof document !== "undefined"
19
+ && typeof HTMLCanvasElement !== "undefined"
20
+ && typeof HTMLCanvasElement.prototype.captureStream === "function"
21
+ && typeof AudioContext !== "undefined";
22
+ }
23
+
24
+ constructor({ fps = DEFAULT_RTMP_FPS, selfName, canvasBackground, onFailure } = {}) {
25
+ const id = `live-${Math.random().toString(36).slice(2, 10)}`;
26
+ super({
27
+ webinarId: id,
28
+ webinarName: id,
29
+ recordingId: id,
30
+ endpoint: "",
31
+ token: "",
32
+ selfName,
33
+ canvasBackground,
34
+ onFailure,
35
+ fps: clampFps(fps, DEFAULT_RTMP_FPS),
36
+ });
37
+ this.eventPrefix = "live";
38
+ // RTMP platforms expect a constant frame rate even when the stage is static.
39
+ this.steadyFrames = true;
40
+ }
41
+
42
+ get videoTrack() {
43
+ return this.capture || null;
44
+ }
45
+
46
+ get audioTrack() {
47
+ return this.dest?.stream.getAudioTracks()[0] || null;
48
+ }
49
+
50
+ // No local chunk storage or upload for live output.
51
+ async dropOtherWebinars() {}
52
+
53
+ async flushStored() {}
54
+
55
+ async store() {}
56
+
57
+ pump() {
58
+ return Promise.resolve();
59
+ }
60
+
61
+ async upload() {
62
+ return true;
63
+ }
64
+
65
+ kickFinalize() {}
66
+
67
+ async finalize() {}
68
+
69
+ async beginCapture() {
70
+ if (this.finishing || this.capturing || !this.prepared) return;
71
+ this.capturing = true;
72
+ this.compositor?.setActive(true);
73
+ this.setDrawActive?.(true);
74
+ for (const video of this.videos.values()) void video.play().catch(() => {});
75
+ this.onCaptureChange(true);
76
+ }
77
+
78
+ async pauseCapture() {
79
+ if (!this.capturing) return;
80
+ this.capturing = false;
81
+ this.compositor?.setActive(false);
82
+ this.setDrawActive?.(false);
83
+ this.onCaptureChange(false);
84
+ }
85
+
86
+ async finish() {
87
+ if (this.finishing) return;
88
+ this.finishing = true;
89
+ this.wanted = false;
90
+ this.capturing = false;
91
+ this.compositor?.setActive(false);
92
+ this.setDrawActive?.(false);
93
+ this.release?.();
94
+ this.release = undefined;
95
+ }
96
+
97
+ async handoff() {
98
+ await this.finish();
99
+ }
100
+ }
@@ -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, background = "#2563eb", onError = () => {} }) {
33
+ export async function createWorkerCompositor({ width, height, fps, background = "#0A0036", steady = false, 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, background =
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, background });
43
+ const ready = await ask({ type: "init", standard: true, width, height, fps, background, steady });
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, background =
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, background, writable: output.writable }, [output.writable]);
55
+ await ask({ type: "init", width, height, fps, background, steady, 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();
@@ -47,11 +47,15 @@ function softBlob(ctx, cx, cy, rx, ry, color, alpha) {
47
47
  ctx.fill();
48
48
  }
49
49
 
50
+ /** Recording stage backdrop — solid dark navy from the design (no wave chrome). */
51
+ export const STAGE_BACKGROUND = "#0A0036";
52
+
50
53
  /**
51
- * Brand canvas backdrop: deep radial field + soft corner waves from `color`.
54
+ * Canvas backdrop gradient derived from the parent-configured CSS color
55
+ * (`createClient({ recordingCanvasBackground })`).
52
56
  */
53
- export function fillRadialBackground(ctx, x, y, w, h, color = "#2563eb") {
54
- const base = parseCssColor(color) || { r: 37, g: 99, b: 235 };
57
+ export function fillRadialBackground(ctx, x, y, w, h, color = STAGE_BACKGROUND) {
58
+ const base = parseCssColor(color) || { r: 10, g: 0, b: 54 };
55
59
  const deep = shadeRgb(base, 0.28);
56
60
  const mid = shadeRgb(base, 0.72);
57
61
  const glow = shadeRgb(base, 1.35);
@@ -68,12 +72,60 @@ export function fillRadialBackground(ctx, x, y, w, h, color = "#2563eb") {
68
72
  ctx.fillStyle = field;
69
73
  ctx.fillRect(x, y, w, h);
70
74
 
71
- // Soft wave-like glows (top-left / bottom-right), matching the reference stage look.
72
75
  softBlob(ctx, x + w * 0.18, y + h * 0.1, w * 0.42, h * 0.32, glow, 0.38);
73
76
  softBlob(ctx, x + w * 0.82, y + h * 0.88, w * 0.4, h * 0.3, glow, 0.3);
74
77
  softBlob(ctx, x + w * 0.7, y + h * 0.18, w * 0.28, h * 0.22, mid, 0.22);
75
78
  }
76
79
 
80
+ /** Stable per-name camera-off gradients (same name → same color). */
81
+ export const TILE_TONES = [
82
+ { mid: { r: 196, g: 48, b: 168 }, edge: { r: 58, g: 10, b: 70 } }, // magenta
83
+ { mid: { r: 110, g: 95, b: 200 }, edge: { r: 30, g: 24, b: 80 } }, // indigo
84
+ { mid: { r: 180, g: 165, b: 70 }, edge: { r: 55, g: 50, b: 18 } }, // olive
85
+ { mid: { r: 170, g: 45, b: 70 }, edge: { r: 55, g: 12, b: 28 } }, // burgundy
86
+ { mid: { r: 50, g: 140, b: 150 }, edge: { r: 15, g: 45, b: 55 } }, // teal
87
+ { mid: { r: 200, g: 110, b: 50 }, edge: { r: 60, g: 30, b: 15 } }, // amber
88
+ ];
89
+
90
+ export function toneForName(name) {
91
+ const s = String(name || "").trim().toLowerCase();
92
+ let hash = 0;
93
+ for (let i = 0; i < s.length; i += 1) hash = (hash * 31 + s.charCodeAt(i)) >>> 0;
94
+ return TILE_TONES[hash % TILE_TONES.length];
95
+ }
96
+
97
+ export function fillNameTone(ctx, x, y, w, h, name) {
98
+ const tone = toneForName(name) || TILE_TONES[0];
99
+ const mid = tone.mid || TILE_TONES[0].mid;
100
+ const edge = tone.edge || TILE_TONES[0].edge;
101
+ const cx = x + w / 2;
102
+ const cy = y + h / 2;
103
+ const span = Math.max(w, h) * 0.72;
104
+ const field = ctx.createRadialGradient(cx, cy, 0, cx, cy, span);
105
+ field.addColorStop(0, cssRgb(mid));
106
+ field.addColorStop(0.55, cssRgb(shadeRgb(mid, 0.72)));
107
+ field.addColorStop(1, cssRgb(edge));
108
+ ctx.fillStyle = field;
109
+ ctx.fillRect(x, y, w, h);
110
+ }
111
+
112
+ export function drawInitialAvatar(ctx, x, y, w, h, name) {
113
+ const cx = Math.round(x + w / 2);
114
+ const cy = Math.round(y + h / 2);
115
+ const diameter = Math.round(Math.min(Math.max(Math.min(w, h) * 0.22, 28), 72));
116
+ const letter = String(name || "?").trim();
117
+ const ch = letter ? letter[0].toUpperCase() : "?";
118
+ ctx.beginPath();
119
+ ctx.arc(cx, cy, diameter / 2, 0, Math.PI * 2);
120
+ ctx.fillStyle = "rgba(255, 255, 255, 0.22)";
121
+ ctx.fill();
122
+ ctx.fillStyle = "#ffffff";
123
+ ctx.font = `600 ${Math.round(diameter * 0.42)}px "Segoe UI", Roboto, Helvetica, Arial, sans-serif`;
124
+ ctx.textAlign = "center";
125
+ ctx.textBaseline = "middle";
126
+ ctx.fillText(ch, cx, cy + 1);
127
+ }
128
+
77
129
  export function roundedRectPath(ctx, x, y, w, h, radius) {
78
130
  const r = Math.max(0, Math.min(radius, w / 2, h / 2));
79
131
  ctx.beginPath();
@@ -88,94 +140,30 @@ export function roundedRectPath(ctx, x, y, w, h, radius) {
88
140
  /** Neon magenta presenter ring — matches the live stage / webinar UI. */
89
141
  export const PRESENTER_ACCENT = "#e879f9";
90
142
 
91
- function drawSignalBars(ctx, left, cy, iconSize) {
92
- const barW = Math.max(2, Math.round(iconSize * 0.18));
93
- const gap = Math.max(1.5, Math.round(iconSize * 0.12));
94
- const heights = [0.45, 0.7, 1];
95
- ctx.fillStyle = "#22c55e";
96
- for (let i = 0; i < 3; i += 1) {
97
- const h = iconSize * heights[i];
98
- const x = left + i * (barW + gap);
99
- const y = cy - h / 2;
100
- ctx.beginPath();
101
- roundedRectPath(ctx, x, y, barW, h, barW / 2);
102
- ctx.fill();
103
- }
104
- }
105
-
106
- function drawMicMuted(ctx, cx, cy, iconSize) {
107
- const r = iconSize * 0.22;
108
- ctx.strokeStyle = "#f43f5e";
109
- ctx.fillStyle = "#f43f5e";
110
- ctx.lineWidth = Math.max(1.5, iconSize * 0.1);
111
- ctx.lineCap = "round";
112
- // Mic capsule
113
- ctx.beginPath();
114
- ctx.moveTo(cx - r, cy - iconSize * 0.05);
115
- ctx.quadraticCurveTo(cx - r, cy - iconSize * 0.38, cx, cy - iconSize * 0.38);
116
- ctx.quadraticCurveTo(cx + r, cy - iconSize * 0.38, cx + r, cy - iconSize * 0.05);
117
- ctx.lineTo(cx + r, cy + iconSize * 0.02);
118
- ctx.quadraticCurveTo(cx + r, cy + iconSize * 0.22, cx, cy + iconSize * 0.22);
119
- ctx.quadraticCurveTo(cx - r, cy + iconSize * 0.22, cx - r, cy + iconSize * 0.02);
120
- ctx.closePath();
121
- ctx.fill();
122
- // Stem + base
123
- ctx.beginPath();
124
- ctx.arc(cx, cy + iconSize * 0.05, iconSize * 0.28, 0.15 * Math.PI, 0.85 * Math.PI);
125
- ctx.stroke();
126
- ctx.beginPath();
127
- ctx.moveTo(cx, cy + iconSize * 0.32);
128
- ctx.lineTo(cx, cy + iconSize * 0.42);
129
- ctx.moveTo(cx - iconSize * 0.16, cy + iconSize * 0.42);
130
- ctx.lineTo(cx + iconSize * 0.16, cy + iconSize * 0.42);
131
- ctx.stroke();
132
- // Slash
133
- ctx.beginPath();
134
- ctx.moveTo(cx - iconSize * 0.32, cy + iconSize * 0.38);
135
- ctx.lineTo(cx + iconSize * 0.32, cy - iconSize * 0.38);
136
- ctx.stroke();
137
- }
138
-
139
143
  /**
140
- * Bottom-left name pill matching the live stage UI:
141
- * dark pill + green signal bars (live) or red muted mic + optional "(Presenter)".
144
+ * Bottom-left name label (design): plain white text + soft scrim. No mic / signal icons.
142
145
  */
143
- export function drawNamePill(ctx, x, y, w, h, label, { presenter = false, muted = false } = {}) {
144
- let text = String(label || "").trim().slice(0, 40);
146
+ export function drawNamePill(ctx, x, y, w, h, label, _opts = {}) {
147
+ const text = String(label || "").trim().slice(0, 40);
145
148
  if (!text) return;
146
- if (presenter && !/\(\s*presenter\s*\)/i.test(text)) {
147
- text = `${text.slice(0, 28)} (Presenter)`;
148
- }
149
- const padX = Math.max(10, Math.round(Math.min(w, h) * 0.035));
150
- const padY = Math.max(8, Math.round(Math.min(w, h) * 0.03));
151
- const namePx = Math.round(Math.min(Math.max(Math.min(w, h) * 0.08, 11), 15));
152
- const iconSize = Math.round(namePx * 1.15);
149
+ const padX = Math.max(10, Math.round(Math.min(w, h) * 0.04));
150
+ const padY = Math.max(8, Math.round(Math.min(w, h) * 0.035));
151
+ const namePx = Math.round(Math.min(Math.max(Math.min(w, h) * 0.075, 11), 16));
152
+ const scrimH = Math.max(padY * 2 + namePx, Math.round(h * 0.22));
153
153
  ctx.save();
154
- ctx.font = `600 ${namePx}px "Segoe UI", Roboto, Helvetica, Arial, sans-serif`;
154
+ const scrim = ctx.createLinearGradient(x, y + h - scrimH, x, y + h);
155
+ scrim.addColorStop(0, "rgba(0, 0, 0, 0)");
156
+ scrim.addColorStop(0.45, "rgba(0, 0, 0, 0.25)");
157
+ scrim.addColorStop(1, "rgba(0, 0, 0, 0.55)");
158
+ ctx.fillStyle = scrim;
159
+ ctx.fillRect(x, y + h - scrimH, w, scrimH);
160
+ ctx.font = `500 ${namePx}px "Segoe UI", Roboto, Helvetica, Arial, sans-serif`;
155
161
  ctx.textAlign = "left";
156
- ctx.textBaseline = "middle";
157
- const textW = ctx.measureText(text).width;
158
- const iconGap = Math.round(iconSize * 0.45);
159
- const pillH = Math.max(iconSize + padY * 0.7, namePx + padY);
160
- const contentW = iconSize + iconGap + textW;
161
- const pillW = Math.min(w - padX * 2, contentW + padX * 1.5);
162
- const pillX = x + padX;
163
- const pillY = y + h - padY - pillH;
164
- const radius = pillH / 2;
165
- roundedRectPath(ctx, pillX, pillY, pillW, pillH, radius);
166
- ctx.fillStyle = "rgba(15, 23, 42, 0.72)";
167
- ctx.fill();
168
-
169
- const cy = pillY + pillH / 2;
170
- const iconLeft = pillX + padX * 0.55;
171
- if (presenter || !muted) {
172
- drawSignalBars(ctx, iconLeft, cy, iconSize);
173
- } else {
174
- drawMicMuted(ctx, iconLeft + iconSize / 2, cy, iconSize);
175
- }
162
+ ctx.textBaseline = "bottom";
176
163
  ctx.fillStyle = "#ffffff";
177
- const textX = iconLeft + iconSize + iconGap;
178
- ctx.fillText(text, textX, cy + 0.5, Math.max(8, pillW - (textX - pillX) - padX * 0.5));
164
+ ctx.shadowColor = "rgba(0, 0, 0, 0.45)";
165
+ ctx.shadowBlur = 3;
166
+ ctx.fillText(text, x + padX, y + h - padY, Math.max(8, w - padX * 2));
179
167
  ctx.restore();
180
168
  }
181
169
 
@@ -200,10 +188,9 @@ export function strokeTileBorder(ctx, x, y, w, h, radius, { accent = false, acce
200
188
  }
201
189
 
202
190
  /**
203
- * Draw a video/image source into a tile (flush by default) + name/mic pill.
204
- * `fit: "cover"` fills the rect (cameras). `fit: "contain"` letterboxes inside
205
- * the rect (screenshare — no cropping; bars use `fillStyle`, typically brand blue).
206
- * `source` may be an HTMLVideoElement or VideoFrame-like canvas image source.
191
+ * Draw a video/image source into a rounded tile + bottom-left name (no mic icons).
192
+ * `fit: "cover"` fills the rect (cameras). `fit: "contain"` letterboxes screenshare.
193
+ * Without a live source, paints the per-name camera-off gradient + initial avatar.
207
194
  */
208
195
  export function paintRoundedTile(ctx, {
209
196
  source,
@@ -216,9 +203,10 @@ export function paintRoundedTile(ctx, {
216
203
  accent = false,
217
204
  accentColor = PRESENTER_ACCENT,
218
205
  radius = 0,
219
- fillStyle = "#0f172a",
206
+ fillStyle = STAGE_BACKGROUND,
220
207
  muted = false,
221
208
  fit = "cover",
209
+ placeholder = false,
222
210
  }) {
223
211
  const { x, y, w, h } = rect;
224
212
  ctx.save();
@@ -230,9 +218,13 @@ export function paintRoundedTile(ctx, {
230
218
  ctx.rect(x, y, w, h);
231
219
  ctx.clip();
232
220
  }
233
- ctx.fillStyle = fillStyle;
234
- ctx.fillRect(x, y, w, h);
235
- if (source && sw > 0 && sh > 0) {
221
+ const hasVideo = Boolean(source && sw > 0 && sh > 0);
222
+ if (!hasVideo || placeholder) {
223
+ fillNameTone(ctx, x, y, w, h, label);
224
+ drawInitialAvatar(ctx, x, y, w, h, label);
225
+ } else {
226
+ ctx.fillStyle = fillStyle;
227
+ ctx.fillRect(x, y, w, h);
236
228
  const scale = fit === "contain"
237
229
  ? Math.min(w / sw, h / sh)
238
230
  : Math.max(w / sw, h / sh);
@@ -244,21 +236,22 @@ export function paintRoundedTile(ctx, {
244
236
  ctx.imageSmoothingEnabled = prev;
245
237
  }
246
238
  ctx.restore();
247
- if (radius > 0) strokeTileBorder(ctx, x, y, w, h, radius, { accent, accentColor });
248
239
  if (label) drawNamePill(ctx, x, y, w, h, label, { presenter: accent, muted });
249
240
  }
250
241
 
251
242
  /** Owns at most one input frame per source and one output write. */
252
243
  export class FrameCompositor {
253
244
  constructor({ canvas, ctx = canvas.getContext("2d"), writer, fps = 15,
254
- background = "#2563eb",
245
+ background = STAGE_BACKGROUND, steady = false,
255
246
  now = () => performance.now(), makeFrame = (surface, init) => new VideoFrame(surface, init),
256
247
  onError = () => {} }) {
257
248
  this.canvas = canvas;
258
249
  this.ctx = ctx;
259
250
  this.writer = writer;
260
251
  this.interval = 1000 / fps;
261
- this.background = String(background || "").trim() || "#2563eb";
252
+ /** Live output: emit every interval even when nothing changed (encoders need a steady rate). */
253
+ this.steady = Boolean(steady);
254
+ this.background = String(background || "").trim() || STAGE_BACKGROUND;
262
255
  this.now = now;
263
256
  this.makeFrame = makeFrame;
264
257
  this.onError = onError;
@@ -302,10 +295,10 @@ export class FrameCompositor {
302
295
 
303
296
  flush() {
304
297
  const now = this.now();
305
- if (!this.active || !this.dirty || this.writing || now - this.lastDraw < this.interval) return;
298
+ if (!this.active || (!this.dirty && !this.steady) || this.writing || now - this.lastDraw < this.interval) return;
306
299
  fillRadialBackground(this.ctx, 0, 0, this.canvas.width, this.canvas.height, this.background);
307
300
  for (const source of this.sources) {
308
- const { key, rect, crop, label, accent, radius, muted, fit, fillStyle } = source;
301
+ const { key, rect, crop, label, accent, radius, muted, fit, fillStyle, placeholder } = source;
309
302
  const frame = this.frames.get(key);
310
303
  if (!rect) continue;
311
304
  const tileRadius = radius == null ? 0 : radius;
@@ -315,20 +308,20 @@ export class FrameCompositor {
315
308
  accentColor: PRESENTER_ACCENT,
316
309
  radius: tileRadius,
317
310
  muted: Boolean(muted),
318
- fillStyle: fillStyle || "#0f172a",
311
+ fillStyle: fillStyle || STAGE_BACKGROUND,
319
312
  fit: fit === "contain" ? "contain" : "cover",
313
+ placeholder: Boolean(placeholder),
320
314
  };
321
315
  if (!frame) {
322
- // Still show the name pill while the first video frame is arriving.
323
- if (chrome.label) {
324
- paintRoundedTile(this.ctx, {
325
- source: null,
326
- rect,
327
- sw: 0,
328
- sh: 0,
329
- ...chrome,
330
- });
331
- }
316
+ // Camera-off / waiting: gradient + initial + name.
317
+ paintRoundedTile(this.ctx, {
318
+ source: null,
319
+ rect,
320
+ sw: 0,
321
+ sh: 0,
322
+ ...chrome,
323
+ placeholder: true,
324
+ });
332
325
  continue;
333
326
  }
334
327
  const width = frame.displayWidth || frame.codedWidth || rect.w;
@@ -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, onError: error });
49
+ compositor = new FrameCompositor({ canvas, writer, fps: data.fps, background: data.background, 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] : []);