mediascale-sdk 1.0.8 → 1.0.10
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 +23 -2
- package/package.json +1 -1
- package/src/client.js +218 -42
- package/src/record.js +27 -8
package/docs/integration.md
CHANGED
|
@@ -180,7 +180,27 @@ await client.replaceMicrophoneTrack(nextMicTrack);
|
|
|
180
180
|
|
|
181
181
|
`replaceCameraTrack` / `replaceMicrophoneTrack` swap media on the **existing producer** (no unpublish). Use these for smooth device switching while live.
|
|
182
182
|
|
|
183
|
-
|
|
183
|
+
### Publish bitrate caps (camera / screen)
|
|
184
|
+
|
|
185
|
+
The SDK limits **outgoing** video encode bitrate via WebRTC `encodings.maxBitrate`:
|
|
186
|
+
|
|
187
|
+
| Stream | Default | Env (portal) |
|
|
188
|
+
| --- | --- | --- |
|
|
189
|
+
| Camera (`webcam`) | **1 Mbps** | `CAMERA_MAX_BITRATE_BPS` |
|
|
190
|
+
| Screen share, whiteboard, PDF, other custom video | **1.5 Mbps** | `SCREEN_MAX_BITRATE_BPS` |
|
|
191
|
+
|
|
192
|
+
Flow: portal reads those env vars → includes `publishLimits: { cameraBps, screenBps }` on the `join` ack → SDK applies them on `produce`. Set a value to `0` to disable that SDK-side cap. Optional override on the client:
|
|
193
|
+
|
|
194
|
+
```js
|
|
195
|
+
createClient({
|
|
196
|
+
// …
|
|
197
|
+
publishLimits: { cameraBps: 1_000_000, screenBps: 1_500_000 },
|
|
198
|
+
});
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
`createClient({ publishLimits })` wins over the portal values. Mic / screen-audio are unchanged.
|
|
202
|
+
|
|
203
|
+
Separately, the SFU origin still caps each publisher’s uplink at **2500 Kbps** (`MAX_PUBLISH_BITRATE_BPS`) via `setMaxIncomingBitrate` on the send transport.
|
|
184
204
|
|
|
185
205
|
Toggle recording during the session:
|
|
186
206
|
|
|
@@ -195,7 +215,7 @@ await client.end(); // combines every segment into one file
|
|
|
195
215
|
|
|
196
216
|
| Function | What it does |
|
|
197
217
|
| --- | --- |
|
|
198
|
-
| `createClient({ endpoint, sessionId, token, recording, iceServers, ...callbacks })` | Prepares the client. `sessionId` and `token` are required. `endpoint` defaults to `http://localhost:8080`. `recording` defaults to **false**.
|
|
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. |
|
|
199
219
|
| `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. |
|
|
200
220
|
| `subscribe()` | Starts playing the other people. Call this once after `connect()`. |
|
|
201
221
|
| `startCamera({ cameraId, resolution, track })` | Publishes the camera. Pass `track` to use your own live video track (SDK clones it). |
|
|
@@ -215,6 +235,7 @@ await client.end(); // combines every segment into one file
|
|
|
215
235
|
| `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. |
|
|
216
236
|
| `setRecordingStage(people)` | **Required for stage-accurate recording.** Who appears on the recording stage: `[{ id, name, local? }]`. Avatars until that peer’s webcam arrives. See [Recording must match the live stage](#recording-must-match-the-live-stage). |
|
|
217
237
|
| `getPeerId()` | Socket peer id for this browser after `connect()`. |
|
|
238
|
+
| `getPublishLimits()` | Current `{ cameraBps, screenBps }` used for outgoing video encode caps. |
|
|
218
239
|
| `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. |
|
|
219
240
|
| `end()` | Leaves the session. If this person is the one recording, every segment is combined and saved. |
|
|
220
241
|
| `listDevices()` | Returns `{ cameras, microphones }`. Each item is `{ id, label }`. |
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -11,6 +11,24 @@ const SIZES = {
|
|
|
11
11
|
/** Built-in sources that have dedicated start/stop helpers. */
|
|
12
12
|
const RESERVED_SOURCES = new Set(["webcam", "mic", "screen", "screen-audio"]);
|
|
13
13
|
|
|
14
|
+
/** Default SDK publish caps (bps). Portal join / createClient can override. */
|
|
15
|
+
const DEFAULT_CAMERA_MAX_BITRATE_BPS = 1_000_000;
|
|
16
|
+
const DEFAULT_SCREEN_MAX_BITRATE_BPS = 1_500_000;
|
|
17
|
+
|
|
18
|
+
function parseBitrateBps(raw, fallback) {
|
|
19
|
+
if (raw == null || raw === "") return fallback;
|
|
20
|
+
const n = Number(raw);
|
|
21
|
+
if (!Number.isFinite(n)) return fallback;
|
|
22
|
+
return Math.max(0, Math.floor(n));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function normalizePublishLimits(raw = {}) {
|
|
26
|
+
return {
|
|
27
|
+
cameraBps: parseBitrateBps(raw.cameraBps ?? raw.camera, DEFAULT_CAMERA_MAX_BITRATE_BPS),
|
|
28
|
+
screenBps: parseBitrateBps(raw.screenBps ?? raw.screen, DEFAULT_SCREEN_MAX_BITRATE_BPS),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
14
32
|
function readToken(token) {
|
|
15
33
|
const body = String(token || "").split(".")[0];
|
|
16
34
|
if (!body) throw new Error("token is required");
|
|
@@ -81,10 +99,17 @@ export function createClient(options = {}) {
|
|
|
81
99
|
|| (options.recording !== false && claims.recording === true && claims.role === "host");
|
|
82
100
|
|
|
83
101
|
const displayName = String(claims.name || "Guest").slice(0, 40);
|
|
102
|
+
// createClient({ publishLimits }) wins over portal join values when set.
|
|
103
|
+
const publishLimitsOverride = options.publishLimits != null
|
|
104
|
+
? normalizePublishLimits(options.publishLimits)
|
|
105
|
+
: null;
|
|
106
|
+
let publishLimits = publishLimitsOverride || normalizePublishLimits({});
|
|
84
107
|
let socket = null;
|
|
85
108
|
let device = null;
|
|
86
109
|
let sendTransport = null;
|
|
87
110
|
let recvTransport = null;
|
|
111
|
+
/** Single-flight when portal parks this viewer and later emits edgeChanged. */
|
|
112
|
+
let recvRecovering = null;
|
|
88
113
|
let iceServers = iceOverride || [];
|
|
89
114
|
let closed = false;
|
|
90
115
|
let subscribed = false;
|
|
@@ -106,9 +131,11 @@ export function createClient(options = {}) {
|
|
|
106
131
|
|
|
107
132
|
function userOnStage(user) {
|
|
108
133
|
if (!user) return false;
|
|
109
|
-
if (user.onStage != null) return Boolean(user.onStage);
|
|
110
134
|
const role = String(user.stageRole || "").trim().toLowerCase();
|
|
111
|
-
|
|
135
|
+
// Host / co-host are always on stage (screen / whiteboard / cam for everyone).
|
|
136
|
+
if (role === "host" || role === "co-host") return true;
|
|
137
|
+
if (user.onStage != null) return Boolean(user.onStage);
|
|
138
|
+
return role === "speaker";
|
|
112
139
|
}
|
|
113
140
|
|
|
114
141
|
function normalizeStagePerson(person) {
|
|
@@ -311,11 +338,126 @@ export function createClient(options = {}) {
|
|
|
311
338
|
if (socket?.connected) await emit(socket, "closeProducer", { producerId }).catch(() => {});
|
|
312
339
|
}
|
|
313
340
|
|
|
341
|
+
function maxBitrateFor(source, kind) {
|
|
342
|
+
if (kind !== "video") return 0;
|
|
343
|
+
if (source === "webcam") return publishLimits.cameraBps;
|
|
344
|
+
// screen + custom video (whiteboard, pdf, …)
|
|
345
|
+
return publishLimits.screenBps;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function applyPublishLimitsFromJoin(raw) {
|
|
349
|
+
if (publishLimitsOverride) return;
|
|
350
|
+
if (!raw || typeof raw !== "object") return;
|
|
351
|
+
publishLimits = normalizePublishLimits({
|
|
352
|
+
cameraBps: raw.cameraBps ?? raw.camera,
|
|
353
|
+
screenBps: raw.screenBps ?? raw.screen,
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function bindSendTransport(transport) {
|
|
358
|
+
transport.on("connect", ({ dtlsParameters }, callback, errback) => {
|
|
359
|
+
emit(socket, "connectTransport", { transportId: transport.id, dtlsParameters }).then(() => callback()).catch(errback);
|
|
360
|
+
});
|
|
361
|
+
transport.on("produce", ({ kind, rtpParameters, appData }, callback, errback) => {
|
|
362
|
+
emit(socket, "produce", {
|
|
363
|
+
transportId: transport.id,
|
|
364
|
+
kind,
|
|
365
|
+
rtpParameters,
|
|
366
|
+
source: appData.source,
|
|
367
|
+
}).then((result) => callback({ id: result.id })).catch(errback);
|
|
368
|
+
});
|
|
369
|
+
transport.on("connectionstatechange", (state) => {
|
|
370
|
+
if (state === "failed" || state === "disconnected") onStatus("Network unstable");
|
|
371
|
+
else if (state === "connected") onStatus("Live");
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function bindRecvTransport(transport) {
|
|
376
|
+
transport.on("connect", ({ dtlsParameters }, callback, errback) => {
|
|
377
|
+
emit(socket, "connectTransport", { transportId: transport.id, dtlsParameters }).then(() => callback()).catch(errback);
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function clearRemoteTiles() {
|
|
382
|
+
for (const [producerId, tile] of [...tiles.entries()]) {
|
|
383
|
+
if (tile.local) continue;
|
|
384
|
+
tiles.delete(producerId);
|
|
385
|
+
onTrackEnded({ producerId });
|
|
386
|
+
}
|
|
387
|
+
syncRecording();
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* After packChannel parks this viewer, portal emits releaseConsumers then later
|
|
392
|
+
* edgeChanged. Rebuild the receive transport and pull every producer again
|
|
393
|
+
* (including a host screen share that started while we were parked).
|
|
394
|
+
*/
|
|
395
|
+
async function recoverRecvTransport() {
|
|
396
|
+
if (closed || !device || !socket?.connected) return;
|
|
397
|
+
if (recvRecovering) return recvRecovering;
|
|
398
|
+
recvRecovering = (async () => {
|
|
399
|
+
try {
|
|
400
|
+
onStatus("Reconnecting media…");
|
|
401
|
+
pending.length = 0;
|
|
402
|
+
try { recvTransport?.close(); } catch { /* ignore */ }
|
|
403
|
+
recvTransport = null;
|
|
404
|
+
clearRemoteTiles();
|
|
405
|
+
|
|
406
|
+
const recvParams = await emit(socket, "createTransport", { direction: "recv" });
|
|
407
|
+
if (closed) return;
|
|
408
|
+
recvTransport = device.createRecvTransport({ ...recvParams, iceServers });
|
|
409
|
+
bindRecvTransport(recvTransport);
|
|
410
|
+
|
|
411
|
+
const started = await emit(socket, "ready", { rtpCapabilities: device.rtpCapabilities });
|
|
412
|
+
for (const params of started.consumers || []) await play(params);
|
|
413
|
+
if (!closed) onStatus("Connected");
|
|
414
|
+
} catch (error) {
|
|
415
|
+
if (!closed) {
|
|
416
|
+
onError(error);
|
|
417
|
+
onStatus(error.message || "Media reconnect failed");
|
|
418
|
+
}
|
|
419
|
+
} finally {
|
|
420
|
+
recvRecovering = null;
|
|
421
|
+
}
|
|
422
|
+
})();
|
|
423
|
+
return recvRecovering;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/** Recreate send transport on the next vacant origin after the current one fills. */
|
|
427
|
+
async function recreateSendTransport() {
|
|
428
|
+
try { sendTransport?.close(); } catch { /* ignore */ }
|
|
429
|
+
sendTransport = null;
|
|
430
|
+
const sendParams = await emit(socket, "createTransport", { direction: "send" });
|
|
431
|
+
sendTransport = device.createSendTransport({ ...sendParams, iceServers });
|
|
432
|
+
bindSendTransport(sendTransport);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function shouldRecreateSendTransport(error) {
|
|
436
|
+
const msg = String(error?.message || error || "");
|
|
437
|
+
return /out of credits|transport not found|ORIGIN_FULL|Origin is full/i.test(msg);
|
|
438
|
+
}
|
|
439
|
+
|
|
314
440
|
// Camera inputs use an SDK-owned clone. Custom inputs keep upstream direct ownership.
|
|
315
441
|
async function produce(source, track, original = track) {
|
|
316
442
|
let producer;
|
|
317
443
|
try {
|
|
318
|
-
|
|
444
|
+
const options = { track, stopTracks: false, appData: { source } };
|
|
445
|
+
const maxBitrate = maxBitrateFor(source, track.kind);
|
|
446
|
+
if (maxBitrate > 0) {
|
|
447
|
+
options.encodings = [{ maxBitrate }];
|
|
448
|
+
// Chrome start bitrate is kbps; keep a modest ramp under the cap.
|
|
449
|
+
options.codecOptions = {
|
|
450
|
+
videoGoogleStartBitrate: Math.max(100, Math.min(1000, Math.round(maxBitrate / 2000))),
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
if (!sendTransport) throw new Error("The session is not ready");
|
|
454
|
+
try {
|
|
455
|
+
producer = await sendTransport.produce(options);
|
|
456
|
+
} catch (error) {
|
|
457
|
+
if (!shouldRecreateSendTransport(error)) throw error;
|
|
458
|
+
await recreateSendTransport();
|
|
459
|
+
producer = await sendTransport.produce(options);
|
|
460
|
+
}
|
|
319
461
|
if (closed) throw new Error("The session has ended");
|
|
320
462
|
if (track.readyState === "ended" || original.readyState === "ended") throw new Error("The source track ended");
|
|
321
463
|
} catch (error) {
|
|
@@ -374,7 +516,8 @@ export function createClient(options = {}) {
|
|
|
374
516
|
if (!closed) onStatus("Network unstable");
|
|
375
517
|
});
|
|
376
518
|
socket.on("sessionEnded", () => {
|
|
377
|
-
|
|
519
|
+
// Whole room is ending — always finalize if we hold the recorder (never hand off).
|
|
520
|
+
end({ finalize: true }).catch((error) => onError(error));
|
|
378
521
|
});
|
|
379
522
|
socket.on("releaseRecording", () => {
|
|
380
523
|
const current = recorder;
|
|
@@ -392,7 +535,8 @@ export function createClient(options = {}) {
|
|
|
392
535
|
const current = recorder;
|
|
393
536
|
if (!current) return;
|
|
394
537
|
recorder = null;
|
|
395
|
-
|
|
538
|
+
// Background remux — do not surface a blocking status in the meet UI.
|
|
539
|
+
current.finish({ waitFinalize: false })
|
|
396
540
|
.then(() => onRecording("stopped"))
|
|
397
541
|
.catch((error) => onError(error));
|
|
398
542
|
});
|
|
@@ -424,6 +568,30 @@ export function createClient(options = {}) {
|
|
|
424
568
|
socket.on("userBlocked", (payload) => {
|
|
425
569
|
if (!closed) onUserBlocked(payload || {});
|
|
426
570
|
});
|
|
571
|
+
// Capacity pack while someone publishes (e.g. host screen) may park this viewer.
|
|
572
|
+
socket.on("waiting", (payload = {}) => {
|
|
573
|
+
if (!closed) onStatus(payload.message || "Please wait, we will connect you soon");
|
|
574
|
+
});
|
|
575
|
+
socket.on("releaseConsumers", () => {
|
|
576
|
+
if (closed) return;
|
|
577
|
+
pending.length = 0;
|
|
578
|
+
try { recvTransport?.close(); } catch { /* ignore */ }
|
|
579
|
+
recvTransport = null;
|
|
580
|
+
clearRemoteTiles();
|
|
581
|
+
});
|
|
582
|
+
socket.on("edgeChanged", () => {
|
|
583
|
+
recoverRecvTransport().catch((error) => onError(error));
|
|
584
|
+
});
|
|
585
|
+
socket.on("newConsumer", (params) => {
|
|
586
|
+
play(params).catch((error) => onError(error));
|
|
587
|
+
});
|
|
588
|
+
socket.on("producerClosed", ({ producerId }) => {
|
|
589
|
+
for (let i = pending.length - 1; i >= 0; i -= 1) {
|
|
590
|
+
if (pending[i].producerId === producerId) pending.splice(i, 1);
|
|
591
|
+
}
|
|
592
|
+
if (tiles.delete(producerId)) syncRecording();
|
|
593
|
+
onTrackEnded({ producerId });
|
|
594
|
+
});
|
|
427
595
|
socket.on("connect", async () => {
|
|
428
596
|
try {
|
|
429
597
|
if (closed) return;
|
|
@@ -436,6 +604,7 @@ export function createClient(options = {}) {
|
|
|
436
604
|
});
|
|
437
605
|
selfPeerId = joined.peerId || null;
|
|
438
606
|
iceServers = iceOverride || joined.iceServers || [];
|
|
607
|
+
applyPublishLimitsFromJoin(joined.publishLimits);
|
|
439
608
|
if (Array.isArray(joined.users)) {
|
|
440
609
|
ingestRosterUsers(joined.users);
|
|
441
610
|
onUserList(joined.users);
|
|
@@ -445,37 +614,11 @@ export function createClient(options = {}) {
|
|
|
445
614
|
|
|
446
615
|
const sendParams = await emit(socket, "createTransport", { direction: "send" });
|
|
447
616
|
sendTransport = device.createSendTransport({ ...sendParams, iceServers });
|
|
448
|
-
sendTransport
|
|
449
|
-
emit(socket, "connectTransport", { transportId: sendTransport.id, dtlsParameters }).then(() => callback()).catch(errback);
|
|
450
|
-
});
|
|
451
|
-
sendTransport.on("produce", ({ kind, rtpParameters, appData }, callback, errback) => {
|
|
452
|
-
emit(socket, "produce", {
|
|
453
|
-
transportId: sendTransport.id,
|
|
454
|
-
kind,
|
|
455
|
-
rtpParameters,
|
|
456
|
-
source: appData.source,
|
|
457
|
-
}).then((result) => callback({ id: result.id })).catch(errback);
|
|
458
|
-
});
|
|
459
|
-
sendTransport.on("connectionstatechange", (state) => {
|
|
460
|
-
if (state === "failed" || state === "disconnected") onStatus("Network unstable");
|
|
461
|
-
else if (state === "connected") onStatus("Live");
|
|
462
|
-
});
|
|
617
|
+
bindSendTransport(sendTransport);
|
|
463
618
|
|
|
464
619
|
const recvParams = await emit(socket, "createTransport", { direction: "recv" });
|
|
465
620
|
recvTransport = device.createRecvTransport({ ...recvParams, iceServers });
|
|
466
|
-
recvTransport
|
|
467
|
-
emit(socket, "connectTransport", { transportId: recvTransport.id, dtlsParameters }).then(() => callback()).catch(errback);
|
|
468
|
-
});
|
|
469
|
-
socket.on("newConsumer", (params) => {
|
|
470
|
-
play(params).catch((error) => onError(error));
|
|
471
|
-
});
|
|
472
|
-
socket.on("producerClosed", ({ producerId }) => {
|
|
473
|
-
for (let i = pending.length - 1; i >= 0; i -= 1) {
|
|
474
|
-
if (pending[i].producerId === producerId) pending.splice(i, 1);
|
|
475
|
-
}
|
|
476
|
-
if (tiles.delete(producerId)) syncRecording();
|
|
477
|
-
onTrackEnded({ producerId });
|
|
478
|
-
});
|
|
621
|
+
bindRecvTransport(recvTransport);
|
|
479
622
|
const started = await emit(socket, "ready", { rtpCapabilities: device.rtpCapabilities });
|
|
480
623
|
for (const params of started.consumers || []) await play(params);
|
|
481
624
|
// Fresh roster after ready (covers anyone who joined during transport setup).
|
|
@@ -659,8 +802,16 @@ export function createClient(options = {}) {
|
|
|
659
802
|
}
|
|
660
803
|
try {
|
|
661
804
|
const screen = await produce("screen", videoTrack);
|
|
805
|
+
// Screen audio is optional — never tear down a live video share if mic capture fails.
|
|
662
806
|
const audioTrack = stream.getAudioTracks()[0];
|
|
663
|
-
if (audioTrack)
|
|
807
|
+
if (audioTrack) {
|
|
808
|
+
try {
|
|
809
|
+
await produce("screen", audioTrack);
|
|
810
|
+
} catch (audioError) {
|
|
811
|
+
try { audioTrack.stop(); } catch { /* ignore */ }
|
|
812
|
+
onError(audioError);
|
|
813
|
+
}
|
|
814
|
+
}
|
|
664
815
|
return screen;
|
|
665
816
|
} catch (error) {
|
|
666
817
|
stream.getTracks().forEach((track) => track.stop());
|
|
@@ -742,12 +893,13 @@ export function createClient(options = {}) {
|
|
|
742
893
|
}
|
|
743
894
|
if (startingRecorder) return startingRecorder;
|
|
744
895
|
startingRecorder = (async () => {
|
|
745
|
-
await
|
|
896
|
+
const recordingId = await recordingIdFor(sessionId);
|
|
897
|
+
await emit(socket, "claimRecording", { recordingId });
|
|
746
898
|
if (closed || recorder) return;
|
|
747
899
|
const next = new CanvasRecorder({
|
|
748
900
|
webinarId: recordingWebinarId(sessionId),
|
|
749
901
|
webinarName: sessionId,
|
|
750
|
-
recordingId
|
|
902
|
+
recordingId,
|
|
751
903
|
endpoint,
|
|
752
904
|
token,
|
|
753
905
|
selfName: displayName,
|
|
@@ -788,27 +940,50 @@ export function createClient(options = {}) {
|
|
|
788
940
|
onRecording("stopped");
|
|
789
941
|
}
|
|
790
942
|
|
|
791
|
-
|
|
943
|
+
/**
|
|
944
|
+
* Leave the session.
|
|
945
|
+
* options.finalize / options.endSession: start remux+upload now (no handoff).
|
|
946
|
+
* options.endSession: also ask the portal to disconnect everyone else — does not wait for remux.
|
|
947
|
+
*/
|
|
948
|
+
async function end(options = {}) {
|
|
792
949
|
if (closed) return;
|
|
793
950
|
closed = true;
|
|
794
951
|
settleReady?.(new Error("Ended"));
|
|
952
|
+
// Finalize only on explicit stop-session / finalize. Browser close uses disconnect handoff.
|
|
953
|
+
const forceFinalize = options.finalize === true || options.endSession === true;
|
|
795
954
|
const current = recorder;
|
|
796
955
|
recorder = null;
|
|
797
956
|
try {
|
|
798
957
|
if (current) {
|
|
799
958
|
let handoff = false;
|
|
800
|
-
if (
|
|
959
|
+
if (forceFinalize) {
|
|
960
|
+
// Stop-session: start remux immediately, do not await, do not change meet status.
|
|
961
|
+
if (socket?.connected) {
|
|
962
|
+
void emit(socket, "leaveRecording", { ending: true }).catch(() => {});
|
|
963
|
+
}
|
|
964
|
+
current.kickFinalize();
|
|
965
|
+
void current.finish({ waitFinalize: false })
|
|
966
|
+
.then(() => onRecording("stopped"))
|
|
967
|
+
.catch((error) => onError(error));
|
|
968
|
+
onRecording("stopped");
|
|
969
|
+
} else if (socket?.connected) {
|
|
801
970
|
try {
|
|
802
971
|
const result = await emit(socket, "leaveRecording", {});
|
|
803
972
|
handoff = Boolean(result?.handoff);
|
|
804
973
|
} catch {
|
|
805
974
|
handoff = false;
|
|
806
975
|
}
|
|
976
|
+
if (handoff) await current.handoff();
|
|
977
|
+
else await current.finish();
|
|
978
|
+
onRecording("stopped");
|
|
979
|
+
} else {
|
|
980
|
+
await current.finish();
|
|
981
|
+
onRecording("stopped");
|
|
807
982
|
}
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
983
|
+
}
|
|
984
|
+
if (options.endSession === true && socket?.connected) {
|
|
985
|
+
// Do not block leave/redirect on room teardown ack.
|
|
986
|
+
void emit(socket, "endSession", {}).catch(() => {});
|
|
812
987
|
}
|
|
813
988
|
} finally {
|
|
814
989
|
for (const entry of locals.values()) {
|
|
@@ -868,6 +1043,7 @@ export function createClient(options = {}) {
|
|
|
868
1043
|
updateSelf: guard(updateSelf),
|
|
869
1044
|
stageAction: guard(stageAction),
|
|
870
1045
|
getPeerId: () => selfPeerId,
|
|
1046
|
+
getPublishLimits: () => ({ ...publishLimits }),
|
|
871
1047
|
end: guard(end),
|
|
872
1048
|
listDevices: guard(listDevices),
|
|
873
1049
|
};
|
package/src/record.js
CHANGED
|
@@ -149,10 +149,9 @@ export class CanvasRecorder {
|
|
|
149
149
|
this.untilStop = new Promise((resolve) => {
|
|
150
150
|
this.release = resolve;
|
|
151
151
|
});
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
};
|
|
152
|
+
// No beforeunload prompt — browser close hands off via portal disconnect;
|
|
153
|
+
// End session redirects without a leave-site warning.
|
|
154
|
+
this.onBeforeUnload = null;
|
|
156
155
|
}
|
|
157
156
|
|
|
158
157
|
async start(audio) {
|
|
@@ -310,7 +309,11 @@ export class CanvasRecorder {
|
|
|
310
309
|
await this.drainRecorder(rec);
|
|
311
310
|
await this.pump();
|
|
312
311
|
// Skip finalize when capture never started (e.g. prepare failed before beginCapture).
|
|
313
|
-
if (options.finalize !== false && didCapture)
|
|
312
|
+
if (options.finalize !== false && didCapture) {
|
|
313
|
+
// waitFinalize:false — kick remux/upload without blocking session teardown.
|
|
314
|
+
if (options.waitFinalize === false) void this.finalize({ wait: false }).catch(() => {});
|
|
315
|
+
else await this.finalize();
|
|
316
|
+
}
|
|
314
317
|
} finally {
|
|
315
318
|
this.release?.();
|
|
316
319
|
this.release = undefined;
|
|
@@ -411,7 +414,6 @@ export class CanvasRecorder {
|
|
|
411
414
|
this.capture = video;
|
|
412
415
|
this.captureStream = new MediaStream([video, this.dest.stream.getAudioTracks()[0]]);
|
|
413
416
|
this.prepared = true;
|
|
414
|
-
window.addEventListener("beforeunload", this.onPageHide);
|
|
415
417
|
}
|
|
416
418
|
|
|
417
419
|
async beginCapture() {
|
|
@@ -825,8 +827,25 @@ export class CanvasRecorder {
|
|
|
825
827
|
return true;
|
|
826
828
|
}
|
|
827
829
|
|
|
828
|
-
|
|
830
|
+
/** Fire remux/upload without waiting (survives tab redirect via keepalive). */
|
|
831
|
+
kickFinalize() {
|
|
832
|
+
if (!this.startedAt) return;
|
|
833
|
+
const durationMs = Date.now() - this.startedAt;
|
|
834
|
+
fetch(this.url(`/api/chunks/${this.recordingId}/finalize`), {
|
|
835
|
+
method: "POST",
|
|
836
|
+
headers: this.authHeaders({ "content-type": "application/json" }),
|
|
837
|
+
body: JSON.stringify({ durationMs, force: true }),
|
|
838
|
+
keepalive: true,
|
|
839
|
+
}).catch(() => {});
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
async finalize(options = {}) {
|
|
829
843
|
const durationMs = this.startedAt ? Date.now() - this.startedAt : 0;
|
|
844
|
+
// Background path for stop-session: do not wait for remux; keepalive survives teardown.
|
|
845
|
+
if (options.wait === false) {
|
|
846
|
+
this.kickFinalize();
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
830
849
|
for (let round = 0; round < 3; round += 1) {
|
|
831
850
|
const res = await fetch(this.url(`/api/chunks/${this.recordingId}/finalize`), {
|
|
832
851
|
method: "POST",
|
|
@@ -868,7 +887,7 @@ export class CanvasRecorder {
|
|
|
868
887
|
|
|
869
888
|
teardown() {
|
|
870
889
|
this.canvas?.remove();
|
|
871
|
-
window.removeEventListener("beforeunload", this.
|
|
890
|
+
if (this.onBeforeUnload) window.removeEventListener("beforeunload", this.onBeforeUnload);
|
|
872
891
|
if (this._audioUnlock) {
|
|
873
892
|
for (const type of ["pointerdown", "keydown", "touchstart", "click"]) {
|
|
874
893
|
window.removeEventListener(type, this._audioUnlock, true);
|