mediascale-sdk 1.0.13 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/integration.md +98 -13
- package/package.json +1 -1
- package/src/client.js +579 -78
- package/src/gradient.js +317 -0
- package/src/index.js +16 -2
- package/src/live.js +70 -3
- package/src/record-compositor.js +10 -4
- package/src/record-frame-compositor.js +124 -11
- package/src/record-worker.js +7 -3
- package/src/record.js +95 -53
package/src/client.js
CHANGED
|
@@ -9,7 +9,14 @@ import {
|
|
|
9
9
|
resolveRecorderAffinity,
|
|
10
10
|
tracksFromTiles,
|
|
11
11
|
} from "./record.js";
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
DEFAULT_RTMP_FPS,
|
|
14
|
+
LiveCompositor,
|
|
15
|
+
clampRtmpBitrate,
|
|
16
|
+
clampRtmpKeyframeInterval,
|
|
17
|
+
normalizeRtmpResolution,
|
|
18
|
+
} from "./live.js";
|
|
19
|
+
import { gradientToCss, normalizeGradient } from "./gradient.js";
|
|
13
20
|
|
|
14
21
|
const SIZES = {
|
|
15
22
|
"480p": { width: 854, height: 480 },
|
|
@@ -20,6 +27,28 @@ const SIZES = {
|
|
|
20
27
|
/** Built-in sources that have dedicated start/stop helpers. */
|
|
21
28
|
const RESERVED_SOURCES = new Set(["webcam", "mic", "screen", "screen-audio"]);
|
|
22
29
|
|
|
30
|
+
/**
|
|
31
|
+
* WebRTC video codec preference. VP8 is the default; the next codec in the list is used
|
|
32
|
+
* only when the browser cannot send the previous one (or the server does not offer it).
|
|
33
|
+
* Override with createClient({ videoCodec: "h264" }) or an ordered list. The live (RTMP)
|
|
34
|
+
* stage is always H264 and is not affected by this setting.
|
|
35
|
+
*/
|
|
36
|
+
export const DEFAULT_VIDEO_CODECS = ["vp8", "h264", "vp9", "av1"];
|
|
37
|
+
|
|
38
|
+
export function normalizeVideoCodecs(raw) {
|
|
39
|
+
const wanted = (Array.isArray(raw) ? raw : raw == null ? [] : [raw])
|
|
40
|
+
.map((item) => String(item || "").trim().toLowerCase().replace(/^video\//, ""))
|
|
41
|
+
.filter(Boolean);
|
|
42
|
+
const out = [];
|
|
43
|
+
for (const name of [...wanted, ...DEFAULT_VIDEO_CODECS]) {
|
|
44
|
+
if (!out.includes(name)) out.push(name);
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** How long the live mix must be unnecessary before the host goes back to one Opus stream. */
|
|
50
|
+
const LIVE_MIX_RELEASE_MS = 10_000;
|
|
51
|
+
|
|
23
52
|
/** Default SDK publish caps (bps). Portal join / createClient can override. */
|
|
24
53
|
const DEFAULT_CAMERA_MAX_BITRATE_BPS = 1_000_000;
|
|
25
54
|
const DEFAULT_SCREEN_MAX_BITRATE_BPS = 1_500_000;
|
|
@@ -38,10 +67,29 @@ function normalizePublishLimits(raw = {}) {
|
|
|
38
67
|
};
|
|
39
68
|
}
|
|
40
69
|
|
|
41
|
-
|
|
70
|
+
const DEFAULT_CANVAS_BACKGROUND = "#0A0036";
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Stage backdrop for recording + RTMP: a CSS color (legacy glow look) or any gradient
|
|
74
|
+
* (`linear-gradient(...)`, `radial-gradient(...)`, `{ type, angle, stops }`). Invalid → default.
|
|
75
|
+
*/
|
|
42
76
|
function normalizeCanvasBackground(raw) {
|
|
43
|
-
|
|
44
|
-
|
|
77
|
+
if (raw == null || raw === "") return DEFAULT_CANVAS_BACKGROUND;
|
|
78
|
+
if (!normalizeGradient(raw)) {
|
|
79
|
+
console.warn("[mediascale] canvasBackground not understood, using the default", raw);
|
|
80
|
+
return DEFAULT_CANVAS_BACKGROUND;
|
|
81
|
+
}
|
|
82
|
+
return typeof raw === "string" ? raw.trim() : raw;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Camera-off tile gradient: one spec or a list (picked by participant name). null → built-in tones. */
|
|
86
|
+
function normalizeAvatarBackground(raw) {
|
|
87
|
+
if (raw == null || raw === "") return null;
|
|
88
|
+
if (Array.isArray(raw)) {
|
|
89
|
+
const list = raw.filter((item) => normalizeGradient(item));
|
|
90
|
+
return list.length ? list : null;
|
|
91
|
+
}
|
|
92
|
+
return normalizeGradient(raw) ? raw : null;
|
|
45
93
|
}
|
|
46
94
|
|
|
47
95
|
function readToken(token) {
|
|
@@ -137,6 +185,9 @@ export function createClient(options = {}) {
|
|
|
137
185
|
const onUserBlocked = options.onUserBlocked || (() => {});
|
|
138
186
|
const onStageChanged = options.onStageChanged || (() => {});
|
|
139
187
|
const onRtmpStatus = options.onRtmpStatus || (() => {});
|
|
188
|
+
const onMediaDemand = options.onMediaDemand || (() => {});
|
|
189
|
+
/** Ordered WebRTC video codec preference (VP8 first unless overridden). */
|
|
190
|
+
const videoCodecs = normalizeVideoCodecs(options.videoCodec ?? options.videoCodecs);
|
|
140
191
|
|
|
141
192
|
if (!sessionId) throw new Error("sessionId is required");
|
|
142
193
|
if (!token) throw new Error("token is required");
|
|
@@ -146,12 +197,21 @@ export function createClient(options = {}) {
|
|
|
146
197
|
const recording = options.recording === true
|
|
147
198
|
|| (options.recording !== false && claims.recording === true && claims.role === "host");
|
|
148
199
|
// Main recording canvas fill (letterbox / empty areas). Defaults to design navy.
|
|
149
|
-
|
|
200
|
+
// `canvasBackground` (gradient or color) wins; `recordingCanvasBackground` is the older name.
|
|
201
|
+
let canvasBackground = normalizeCanvasBackground(options.canvasBackground ?? options.recordingCanvasBackground);
|
|
202
|
+
let avatarBackground = normalizeAvatarBackground(options.avatarBackground);
|
|
150
203
|
// Frame rates are independent: recording (MediaRecorder) vs RTMP live output.
|
|
151
204
|
let recordingFps = clampFps(options.recordingFps, DEFAULT_RECORDING_FPS);
|
|
152
205
|
const recordingBitrate = Number(options.recordingBitrate) > 0 ? Math.round(Number(options.recordingBitrate)) : undefined;
|
|
153
|
-
|
|
154
|
-
const
|
|
206
|
+
// RTMP output quality. Flat options or `rtmpQuality: { resolution, fps, bitrate, keyframeInterval }`.
|
|
207
|
+
const rtmpQualityOption = options.rtmpQuality && typeof options.rtmpQuality === "object" ? options.rtmpQuality : {};
|
|
208
|
+
let rtmpFps = clampFps(options.rtmpFps ?? rtmpQualityOption.fps, DEFAULT_RTMP_FPS);
|
|
209
|
+
/** bps, capped at 2500 kbps. */
|
|
210
|
+
let rtmpBitrate = clampRtmpBitrate(options.rtmpBitrate ?? rtmpQualityOption.bitrate);
|
|
211
|
+
/** "480p" | "540p" | "720p" | "1080p". */
|
|
212
|
+
let rtmpResolution = normalizeRtmpResolution(options.rtmpResolution ?? rtmpQualityOption.resolution);
|
|
213
|
+
/** Seconds between keyframes (GOP). Sent to the portal on Go Live. */
|
|
214
|
+
let rtmpKeyframeInterval = clampRtmpKeyframeInterval(options.rtmpKeyframeInterval ?? rtmpQualityOption.keyframeInterval);
|
|
155
215
|
/**
|
|
156
216
|
* Hosts can always open Live. `claims.rtmp` means destinations were prepared at
|
|
157
217
|
* webinar create — auto Go Live after join when those destinations are present.
|
|
@@ -195,10 +255,30 @@ export function createClient(options = {}) {
|
|
|
195
255
|
let settleReady = null;
|
|
196
256
|
/** Session-scoped portal peer (query param). Avoids browser-wide cookie collisions. */
|
|
197
257
|
let portalPeer = readStoredPortalPeer(sessionId);
|
|
198
|
-
/** RTMP: stage compositor + hidden H264
|
|
258
|
+
/** RTMP: stage compositor + hidden H264 producer (+ live audio) on the origin. */
|
|
199
259
|
let live = null;
|
|
200
|
-
|
|
260
|
+
/** Hidden `rtmp-video` (H264 stage). */
|
|
261
|
+
let liveVideo = null;
|
|
262
|
+
/**
|
|
263
|
+
* RTMP audio. `shared: true` → it is my `mic` producer (one Opus stream for WebRTC + RTMP).
|
|
264
|
+
* `shared: false` → a hidden producer carrying the stage mix (co-host mics, screen audio…).
|
|
265
|
+
* The same producer moves between the two (portal re-labels it), so RTMP is never re-wired.
|
|
266
|
+
*/
|
|
267
|
+
let liveAudio = null;
|
|
268
|
+
/** Serializes everything that touches the mic producer or the live audio. */
|
|
269
|
+
let audioQueue = Promise.resolve();
|
|
270
|
+
let liveAudioCheckTimer = null;
|
|
271
|
+
let liveMixIdleSince = 0;
|
|
201
272
|
let startingLive = null;
|
|
273
|
+
/**
|
|
274
|
+
* WebRTC demand from the portal. Live-stream-first rooms start with `false`: camera /
|
|
275
|
+
* screen / custom tracks stay local (the RTMP stage still shows them) until somebody
|
|
276
|
+
* joins to watch over WebRTC. Every other room is always `true` (publish immediately).
|
|
277
|
+
*/
|
|
278
|
+
let webrtcDemand = true;
|
|
279
|
+
let demandQueue = Promise.resolve();
|
|
280
|
+
/** Portal capabilities from the join ack (older portals send none). */
|
|
281
|
+
let portalFeatures = {};
|
|
202
282
|
let rtmpState = { enabled: rtmpEnabled, state: "idle", live: false, destinations: [] };
|
|
203
283
|
|
|
204
284
|
function userOnStage(user) {
|
|
@@ -215,10 +295,14 @@ export function createClient(options = {}) {
|
|
|
215
295
|
const id = String(person.id || "").trim();
|
|
216
296
|
const name = String(person.name || "Guest").trim().slice(0, 40) || "Guest";
|
|
217
297
|
if (!id && !name) return null;
|
|
298
|
+
const avatar = person.avatarBackground != null && normalizeGradient(person.avatarBackground)
|
|
299
|
+
? person.avatarBackground
|
|
300
|
+
: null;
|
|
218
301
|
return {
|
|
219
302
|
id,
|
|
220
303
|
name,
|
|
221
304
|
local: Boolean(person.local) || Boolean(id && selfPeerId && id === selfPeerId),
|
|
305
|
+
...(avatar ? { avatarBackground: avatar } : {}),
|
|
222
306
|
};
|
|
223
307
|
}
|
|
224
308
|
|
|
@@ -311,6 +395,7 @@ export function createClient(options = {}) {
|
|
|
311
395
|
const people = stagePeopleForRecording();
|
|
312
396
|
recorder?.sync(tracks, people);
|
|
313
397
|
live?.sync(tracks, people);
|
|
398
|
+
scheduleLiveAudioCheck();
|
|
314
399
|
}
|
|
315
400
|
|
|
316
401
|
/**
|
|
@@ -420,17 +505,42 @@ export function createClient(options = {}) {
|
|
|
420
505
|
if (entry.original !== track) entry.original.addEventListener("ended", entry.onEnded);
|
|
421
506
|
}
|
|
422
507
|
|
|
508
|
+
/**
|
|
509
|
+
* Local entries are keyed by a stable id: the producer id when published right away
|
|
510
|
+
* (unchanged behavior), or a `local-…` id while the track is kept local (no WebRTC demand).
|
|
511
|
+
* `entry.producer` may be null or change (mic shared with RTMP) — never key by it.
|
|
512
|
+
*/
|
|
423
513
|
async function unpublish(producerId) {
|
|
424
514
|
const entry = locals.get(producerId);
|
|
425
515
|
if (!entry) return;
|
|
516
|
+
if (entry.source === "mic") return withAudioLock(() => unpublishNow(producerId));
|
|
517
|
+
return unpublishNow(producerId);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
async function unpublishNow(producerId) {
|
|
521
|
+
const entry = locals.get(producerId);
|
|
522
|
+
if (!entry) return;
|
|
523
|
+
// My mic is also the RTMP audio: hand that producer to the live mix instead of closing
|
|
524
|
+
// it, so the platforms keep their audio input.
|
|
525
|
+
if (liveAudio?.shared && entry.producer && entry.producer === liveAudio.producer) {
|
|
526
|
+
try {
|
|
527
|
+
await splitLiveAudio({ keepMic: false });
|
|
528
|
+
} catch (error) {
|
|
529
|
+
onError(error);
|
|
530
|
+
liveAudio = null;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
426
533
|
locals.delete(producerId);
|
|
427
534
|
tiles.delete(producerId);
|
|
428
535
|
detachEnded(entry);
|
|
429
536
|
entry.track.stop();
|
|
430
|
-
entry.producer
|
|
537
|
+
const producer = entry.producer;
|
|
538
|
+
entry.producer = null;
|
|
539
|
+
producer?.close();
|
|
431
540
|
syncRecording();
|
|
432
541
|
onTrackEnded({ producerId, local: true, source: entry.source });
|
|
433
|
-
if (socket?.connected) await emit(socket, "closeProducer", { producerId }).catch(() => {});
|
|
542
|
+
if (producer && socket?.connected) await emit(socket, "closeProducer", { producerId: producer.id }).catch(() => {});
|
|
543
|
+
if (entry.source === "mic" && live && !liveAudio) await repairLiveAudio();
|
|
434
544
|
}
|
|
435
545
|
|
|
436
546
|
function maxBitrateFor(source, kind) {
|
|
@@ -532,27 +642,67 @@ export function createClient(options = {}) {
|
|
|
532
642
|
return /out of credits|transport not found|ORIGIN_FULL|Origin is full/i.test(msg);
|
|
533
643
|
}
|
|
534
644
|
|
|
645
|
+
/** First codec in the preference list that both this browser and the server can use. */
|
|
646
|
+
function pickVideoCodec() {
|
|
647
|
+
const offered = device?.rtpCapabilities?.codecs || [];
|
|
648
|
+
const sendable = typeof RTCRtpSender !== "undefined" && RTCRtpSender.getCapabilities
|
|
649
|
+
? RTCRtpSender.getCapabilities("video")?.codecs || null
|
|
650
|
+
: null;
|
|
651
|
+
for (const name of videoCodecs) {
|
|
652
|
+
const mime = `video/${name}`;
|
|
653
|
+
if (sendable && !sendable.some((codec) => String(codec.mimeType).toLowerCase() === mime)) continue;
|
|
654
|
+
const codec = offered.find((item) => item.kind === "video" && String(item.mimeType).toLowerCase() === mime);
|
|
655
|
+
if (codec) return codec;
|
|
656
|
+
}
|
|
657
|
+
return undefined; // let mediasoup pick, as before
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/** Send one local track to the origin (WebRTC). */
|
|
661
|
+
async function sendProduce(source, track) {
|
|
662
|
+
const options = { track, stopTracks: false, appData: { source } };
|
|
663
|
+
const maxBitrate = maxBitrateFor(source, track.kind);
|
|
664
|
+
if (maxBitrate > 0) {
|
|
665
|
+
options.encodings = [{ maxBitrate }];
|
|
666
|
+
// Chrome start bitrate is kbps; keep a modest ramp under the cap.
|
|
667
|
+
options.codecOptions = {
|
|
668
|
+
videoGoogleStartBitrate: Math.max(100, Math.min(1000, Math.round(maxBitrate / 2000))),
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
if (track.kind === "video") {
|
|
672
|
+
const codec = pickVideoCodec();
|
|
673
|
+
if (codec) options.codec = codec;
|
|
674
|
+
}
|
|
675
|
+
if (!sendTransport) throw new Error("The session is not ready");
|
|
676
|
+
try {
|
|
677
|
+
return await sendTransport.produce(options);
|
|
678
|
+
} catch (error) {
|
|
679
|
+
if (!shouldRecreateSendTransport(error)) throw error;
|
|
680
|
+
await recreateSendTransport();
|
|
681
|
+
return sendTransport.produce(options);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/** The mic is always published (it is also the RTMP audio); everything else follows demand. */
|
|
686
|
+
function followsDemand(source) {
|
|
687
|
+
return source !== "mic";
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
function localKey() {
|
|
691
|
+
const random = typeof crypto !== "undefined" && crypto.randomUUID
|
|
692
|
+
? crypto.randomUUID()
|
|
693
|
+
: Math.random().toString(36).slice(2);
|
|
694
|
+
return `local-${random}`;
|
|
695
|
+
}
|
|
696
|
+
|
|
535
697
|
// Camera inputs use an SDK-owned clone. Custom inputs keep upstream direct ownership.
|
|
536
698
|
async function produce(source, track, original = track) {
|
|
537
|
-
let producer;
|
|
699
|
+
let producer = null;
|
|
700
|
+
const deferred = followsDemand(source) && !webrtcDemand;
|
|
538
701
|
try {
|
|
539
|
-
const options = { track, stopTracks: false, appData: { source } };
|
|
540
|
-
const maxBitrate = maxBitrateFor(source, track.kind);
|
|
541
|
-
if (maxBitrate > 0) {
|
|
542
|
-
options.encodings = [{ maxBitrate }];
|
|
543
|
-
// Chrome start bitrate is kbps; keep a modest ramp under the cap.
|
|
544
|
-
options.codecOptions = {
|
|
545
|
-
videoGoogleStartBitrate: Math.max(100, Math.min(1000, Math.round(maxBitrate / 2000))),
|
|
546
|
-
};
|
|
547
|
-
}
|
|
548
702
|
if (!sendTransport) throw new Error("The session is not ready");
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
if (!shouldRecreateSendTransport(error)) throw error;
|
|
553
|
-
await recreateSendTransport();
|
|
554
|
-
producer = await sendTransport.produce(options);
|
|
555
|
-
}
|
|
703
|
+
// No WebRTC viewer yet (live-stream-first room): keep the track local. The preview
|
|
704
|
+
// and the RTMP stage use it; it is published the moment demand turns on.
|
|
705
|
+
if (!deferred) producer = await sendProduce(source, track);
|
|
556
706
|
if (closed) throw new Error("The session has ended");
|
|
557
707
|
if (track.readyState === "ended" || original.readyState === "ended") throw new Error("The source track ended");
|
|
558
708
|
} catch (error) {
|
|
@@ -560,11 +710,12 @@ export function createClient(options = {}) {
|
|
|
560
710
|
track.stop();
|
|
561
711
|
throw error;
|
|
562
712
|
}
|
|
713
|
+
const key = producer ? producer.id : localKey();
|
|
563
714
|
const stream = new MediaStream([track]);
|
|
564
715
|
const entry = { producer, track, original, source };
|
|
565
|
-
locals.set(
|
|
566
|
-
tiles.set(
|
|
567
|
-
producerId:
|
|
716
|
+
locals.set(key, entry);
|
|
717
|
+
tiles.set(key, {
|
|
718
|
+
producerId: key,
|
|
568
719
|
publisherId: "local",
|
|
569
720
|
source,
|
|
570
721
|
kind: track.kind,
|
|
@@ -572,9 +723,11 @@ export function createClient(options = {}) {
|
|
|
572
723
|
stream,
|
|
573
724
|
local: true,
|
|
574
725
|
});
|
|
575
|
-
attachEnded(
|
|
726
|
+
attachEnded(key, entry);
|
|
576
727
|
syncRecording();
|
|
577
|
-
|
|
728
|
+
// Demand may have turned on while this track was being prepared.
|
|
729
|
+
if (deferred && webrtcDemand) queueDemandReconcile();
|
|
730
|
+
return { producerId: key, stream, source };
|
|
578
731
|
}
|
|
579
732
|
|
|
580
733
|
async function ensurePortalAffinity({ force = false } = {}) {
|
|
@@ -649,6 +802,10 @@ export function createClient(options = {}) {
|
|
|
649
802
|
socket.on("rtmpStopped", () => {
|
|
650
803
|
stopLiveOutput().catch(() => {});
|
|
651
804
|
});
|
|
805
|
+
// Live-stream-first rooms: publish camera / screen only while someone watches over WebRTC.
|
|
806
|
+
socket.on("mediaDemand", (payload = {}) => {
|
|
807
|
+
if (!closed) setWebrtcDemand(payload.webrtc !== false);
|
|
808
|
+
});
|
|
652
809
|
socket.on("activeSpeaker", (payload = {}) => {
|
|
653
810
|
if (closed) return;
|
|
654
811
|
const peerId = payload.peerId || null;
|
|
@@ -756,6 +913,10 @@ export function createClient(options = {}) {
|
|
|
756
913
|
...clientHints(),
|
|
757
914
|
});
|
|
758
915
|
selfPeerId = joined.peerId || null;
|
|
916
|
+
portalFeatures = joined.features && typeof joined.features === "object" ? joined.features : {};
|
|
917
|
+
// Older portals send no mediaDemand: always publish, exactly as before.
|
|
918
|
+
webrtcDemand = joined.mediaDemand?.webrtc !== false;
|
|
919
|
+
onMediaDemand({ webrtc: webrtcDemand });
|
|
759
920
|
if (joined.rtmp) setRtmpState(joined.rtmp);
|
|
760
921
|
iceServers = iceOverride || joined.iceServers || [];
|
|
761
922
|
applyPublishLimitsFromJoin(joined.publishLimits);
|
|
@@ -926,13 +1087,19 @@ export function createClient(options = {}) {
|
|
|
926
1087
|
|
|
927
1088
|
async function replaceLocalTrack(source, track, kind) {
|
|
928
1089
|
requireLiveTrack(track, kind);
|
|
1090
|
+
if (source === "mic") return withAudioLock(() => replaceLocalTrackNow(source, track, kind));
|
|
1091
|
+
return replaceLocalTrackNow(source, track, kind);
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
async function replaceLocalTrackNow(source, track, kind) {
|
|
929
1095
|
if (closed) throw new Error("The session has ended");
|
|
930
1096
|
const current = [...locals].find(([, entry]) => entry.source === source);
|
|
931
1097
|
if (!current) throw new Error(source === "webcam" ? "The camera is not started" : "The microphone is not started");
|
|
932
1098
|
const [producerId, entry] = current;
|
|
933
1099
|
const replacement = track.clone();
|
|
934
1100
|
try {
|
|
935
|
-
|
|
1101
|
+
// A track kept local (no WebRTC demand) has no producer yet — just swap it.
|
|
1102
|
+
if (entry.producer) await entry.producer.replaceTrack({ track: replacement });
|
|
936
1103
|
if (closed || locals.get(producerId) !== entry) {
|
|
937
1104
|
throw new Error(source === "webcam" ? "The camera is not started" : "The microphone is not started");
|
|
938
1105
|
}
|
|
@@ -1021,6 +1188,8 @@ export function createClient(options = {}) {
|
|
|
1021
1188
|
stream.getTracks().forEach((track) => track.stop());
|
|
1022
1189
|
throw new Error("No screen was selected");
|
|
1023
1190
|
}
|
|
1191
|
+
// Text / slides: the encoder keeps full resolution and drops frames instead of blurring.
|
|
1192
|
+
try { videoTrack.contentHint = "detail"; } catch { /* optional */ }
|
|
1024
1193
|
try {
|
|
1025
1194
|
const screen = await produce("screen", videoTrack);
|
|
1026
1195
|
// Screen audio is optional — never tear down a live video share if mic capture fails.
|
|
@@ -1119,7 +1288,8 @@ export function createClient(options = {}) {
|
|
|
1119
1288
|
endpoint,
|
|
1120
1289
|
token,
|
|
1121
1290
|
selfName: displayName,
|
|
1122
|
-
canvasBackground
|
|
1291
|
+
canvasBackground,
|
|
1292
|
+
avatarBackground,
|
|
1123
1293
|
recorderPeer: peer,
|
|
1124
1294
|
fps: recordingFps,
|
|
1125
1295
|
bitrate: recordingBitrate,
|
|
@@ -1219,6 +1389,235 @@ export function createClient(options = {}) {
|
|
|
1219
1389
|
return LiveCompositor.supported() && Boolean(h264Codec());
|
|
1220
1390
|
}
|
|
1221
1391
|
|
|
1392
|
+
// ---- WebRTC demand (live-stream-first rooms) --------------------------------
|
|
1393
|
+
|
|
1394
|
+
function setWebrtcDemand(next) {
|
|
1395
|
+
const value = Boolean(next);
|
|
1396
|
+
if (value === webrtcDemand) return;
|
|
1397
|
+
webrtcDemand = value;
|
|
1398
|
+
onMediaDemand({ webrtc: value });
|
|
1399
|
+
queueDemandReconcile();
|
|
1400
|
+
}
|
|
1401
|
+
|
|
1402
|
+
function queueDemandReconcile() {
|
|
1403
|
+
const run = demandQueue.then(reconcileDemand, reconcileDemand);
|
|
1404
|
+
demandQueue = run.catch((error) => onError(error));
|
|
1405
|
+
return run;
|
|
1406
|
+
}
|
|
1407
|
+
|
|
1408
|
+
/** Publish (or stop publishing) every local non-mic track to match `webrtcDemand`. */
|
|
1409
|
+
async function reconcileDemand() {
|
|
1410
|
+
for (const [key, entry] of [...locals]) {
|
|
1411
|
+
if (closed || !sendTransport) return;
|
|
1412
|
+
if (!followsDemand(entry.source)) continue;
|
|
1413
|
+
if (webrtcDemand && !entry.producer) {
|
|
1414
|
+
const track = entry.track;
|
|
1415
|
+
let producer = null;
|
|
1416
|
+
try {
|
|
1417
|
+
producer = await sendProduce(entry.source, track);
|
|
1418
|
+
} catch (error) {
|
|
1419
|
+
onError(error);
|
|
1420
|
+
continue;
|
|
1421
|
+
}
|
|
1422
|
+
if (closed || locals.get(key) !== entry || !webrtcDemand || entry.producer) {
|
|
1423
|
+
producer.close();
|
|
1424
|
+
if (socket?.connected) await emit(socket, "closeProducer", { producerId: producer.id }).catch(() => {});
|
|
1425
|
+
continue;
|
|
1426
|
+
}
|
|
1427
|
+
entry.producer = producer;
|
|
1428
|
+
// The track was swapped (device change) while publishing.
|
|
1429
|
+
if (entry.track !== track) await producer.replaceTrack({ track: entry.track }).catch((error) => onError(error));
|
|
1430
|
+
} else if (!webrtcDemand && entry.producer) {
|
|
1431
|
+
const producer = entry.producer;
|
|
1432
|
+
entry.producer = null;
|
|
1433
|
+
producer.close();
|
|
1434
|
+
if (socket?.connected) await emit(socket, "closeProducer", { producerId: producer.id }).catch(() => {});
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
// ---- Live (RTMP) audio: one Opus stream unless a mix is needed -----------------
|
|
1440
|
+
|
|
1441
|
+
function withAudioLock(job) {
|
|
1442
|
+
const run = audioQueue.then(job, job);
|
|
1443
|
+
audioQueue = run.then(() => {}, () => {});
|
|
1444
|
+
return run;
|
|
1445
|
+
}
|
|
1446
|
+
|
|
1447
|
+
function micEntry() {
|
|
1448
|
+
for (const entry of locals.values()) {
|
|
1449
|
+
if (entry.source === "mic" && entry.track.kind === "audio") return entry;
|
|
1450
|
+
}
|
|
1451
|
+
return null;
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
/**
|
|
1455
|
+
* The RTMP audio needs the compositor mix when anything besides my own mic is audible
|
|
1456
|
+
* on the stage (co-host / guest mics, screen or tab audio) or when my mic is off.
|
|
1457
|
+
* Mute state does not matter, so muting never flips the audio path.
|
|
1458
|
+
*/
|
|
1459
|
+
function needsLiveMix() {
|
|
1460
|
+
const mic = micEntry();
|
|
1461
|
+
if (!mic?.producer) return true;
|
|
1462
|
+
for (const tile of tiles.values()) {
|
|
1463
|
+
if (tile.kind !== "audio") continue;
|
|
1464
|
+
if (tile.local && tile.source === "mic") continue;
|
|
1465
|
+
return true;
|
|
1466
|
+
}
|
|
1467
|
+
return false;
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
function canShareMic() {
|
|
1471
|
+
return portalFeatures.rtmpSharedAudio === true;
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
/** On Go Live: use my mic producer directly when nothing needs mixing, else a hidden mix. */
|
|
1475
|
+
async function setupLiveAudio(compositor) {
|
|
1476
|
+
const mic = micEntry();
|
|
1477
|
+
if (canShareMic() && mic?.producer && !needsLiveMix()) {
|
|
1478
|
+
try {
|
|
1479
|
+
await emit(socket, "rtmpAudio", { action: "shared", producerId: mic.producer.id });
|
|
1480
|
+
liveAudio = { producer: mic.producer, shared: true };
|
|
1481
|
+
liveMixIdleSince = 0;
|
|
1482
|
+
return;
|
|
1483
|
+
} catch (error) {
|
|
1484
|
+
console.warn("[mediascale] live audio: mic sharing refused, using the mix", error?.message || error);
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
const producer = await produceHidden("rtmp-audio", compositor.audioTrack);
|
|
1488
|
+
liveAudio = { producer, shared: false };
|
|
1489
|
+
liveMixIdleSince = 0;
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
async function teardownLiveAudio() {
|
|
1493
|
+
const current = liveAudio;
|
|
1494
|
+
liveAudio = null;
|
|
1495
|
+
liveMixIdleSince = 0;
|
|
1496
|
+
if (liveAudioCheckTimer) {
|
|
1497
|
+
clearTimeout(liveAudioCheckTimer);
|
|
1498
|
+
liveAudioCheckTimer = null;
|
|
1499
|
+
}
|
|
1500
|
+
// Shared: it is my mic — it keeps serving WebRTC; the portal already dropped it from RTMP.
|
|
1501
|
+
if (!current || current.shared) return;
|
|
1502
|
+
try { current.producer.close(); } catch { /* ignore */ }
|
|
1503
|
+
if (socket?.connected) await emit(socket, "closeProducer", { producerId: current.producer.id }).catch(() => {});
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1506
|
+
/** RTMP lost its audio producer (e.g. mic sharing failed): publish a hidden mix. */
|
|
1507
|
+
async function repairLiveAudio() {
|
|
1508
|
+
if (!live || liveAudio || closed) return;
|
|
1509
|
+
try {
|
|
1510
|
+
const producer = await produceHidden("rtmp-audio", live.audioTrack);
|
|
1511
|
+
liveAudio = { producer, shared: false };
|
|
1512
|
+
} catch (error) {
|
|
1513
|
+
onError(error);
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
|
|
1517
|
+
/**
|
|
1518
|
+
* shared → mix: the producer RTMP uses switches to the stage mix (portal hides it from
|
|
1519
|
+
* WebRTC), and my plain mic goes out as a new producer for WebRTC viewers.
|
|
1520
|
+
*/
|
|
1521
|
+
async function splitLiveAudio({ keepMic = true } = {}) {
|
|
1522
|
+
const current = liveAudio;
|
|
1523
|
+
const compositor = live;
|
|
1524
|
+
if (!current?.shared || !compositor) return;
|
|
1525
|
+
const producer = current.producer;
|
|
1526
|
+
await emit(socket, "rtmpAudio", { action: "split", producerId: producer.id });
|
|
1527
|
+
try {
|
|
1528
|
+
await producer.replaceTrack({ track: compositor.audioTrack });
|
|
1529
|
+
} catch (error) {
|
|
1530
|
+
onError(error);
|
|
1531
|
+
}
|
|
1532
|
+
liveAudio = { producer, shared: false };
|
|
1533
|
+
liveMixIdleSince = 0;
|
|
1534
|
+
const mic = micEntry();
|
|
1535
|
+
if (mic && mic.producer === producer) {
|
|
1536
|
+
mic.producer = null;
|
|
1537
|
+
if (keepMic) {
|
|
1538
|
+
try {
|
|
1539
|
+
mic.producer = await sendProduce("mic", mic.track);
|
|
1540
|
+
} catch (error) {
|
|
1541
|
+
onError(error);
|
|
1542
|
+
}
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
/**
|
|
1548
|
+
* mix → shared: the RTMP producer carries my plain mic again and becomes my visible mic;
|
|
1549
|
+
* the separate WebRTC mic closes first (a short gap beats viewers hearing me twice).
|
|
1550
|
+
*/
|
|
1551
|
+
async function mergeLiveAudio() {
|
|
1552
|
+
const current = liveAudio;
|
|
1553
|
+
const mic = micEntry();
|
|
1554
|
+
if (!current || current.shared || !mic?.producer || mic.producer === current.producer) return;
|
|
1555
|
+
const producer = current.producer;
|
|
1556
|
+
const webrtcMic = mic.producer;
|
|
1557
|
+
await producer.replaceTrack({ track: mic.track });
|
|
1558
|
+
mic.producer = null;
|
|
1559
|
+
webrtcMic.close();
|
|
1560
|
+
if (socket?.connected) await emit(socket, "closeProducer", { producerId: webrtcMic.id }).catch(() => {});
|
|
1561
|
+
try {
|
|
1562
|
+
await emit(socket, "rtmpAudio", { action: "merge", producerId: producer.id });
|
|
1563
|
+
} catch (error) {
|
|
1564
|
+
// Stay on the mix and bring the WebRTC mic back.
|
|
1565
|
+
if (live) await producer.replaceTrack({ track: live.audioTrack }).catch(() => {});
|
|
1566
|
+
try {
|
|
1567
|
+
if (micEntry() === mic && !mic.producer) mic.producer = await sendProduce("mic", mic.track);
|
|
1568
|
+
} catch (produceError) {
|
|
1569
|
+
onError(produceError);
|
|
1570
|
+
}
|
|
1571
|
+
throw error;
|
|
1572
|
+
}
|
|
1573
|
+
mic.producer = producer;
|
|
1574
|
+
liveAudio = { producer, shared: true };
|
|
1575
|
+
liveMixIdleSince = 0;
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
async function reconcileLiveAudio() {
|
|
1579
|
+
if (closed || !live || !liveAudio || !canShareMic() || !socket?.connected) return;
|
|
1580
|
+
const mic = micEntry();
|
|
1581
|
+
// A WebRTC mic that failed to publish during a split: try again.
|
|
1582
|
+
if (mic && !mic.producer && sendTransport) {
|
|
1583
|
+
try {
|
|
1584
|
+
mic.producer = await sendProduce("mic", mic.track);
|
|
1585
|
+
} catch (error) {
|
|
1586
|
+
onError(error);
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
const need = needsLiveMix();
|
|
1590
|
+
if (liveAudio.shared) {
|
|
1591
|
+
liveMixIdleSince = 0;
|
|
1592
|
+
if (need) await splitLiveAudio();
|
|
1593
|
+
return;
|
|
1594
|
+
}
|
|
1595
|
+
if (need) {
|
|
1596
|
+
liveMixIdleSince = 0;
|
|
1597
|
+
return;
|
|
1598
|
+
}
|
|
1599
|
+
// Go back to one Opus stream only after the mix has been unnecessary for a while,
|
|
1600
|
+
// so a guest briefly joining / leaving the stage does not flip the audio back and forth.
|
|
1601
|
+
const now = Date.now();
|
|
1602
|
+
if (!liveMixIdleSince) liveMixIdleSince = now;
|
|
1603
|
+
const left = LIVE_MIX_RELEASE_MS - (now - liveMixIdleSince);
|
|
1604
|
+
if (left > 0) {
|
|
1605
|
+
scheduleLiveAudioCheck(left + 50);
|
|
1606
|
+
return;
|
|
1607
|
+
}
|
|
1608
|
+
liveMixIdleSince = 0;
|
|
1609
|
+
await mergeLiveAudio();
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
function scheduleLiveAudioCheck(delayMs = 300) {
|
|
1613
|
+
if (closed || !live || !liveAudio || !canShareMic()) return;
|
|
1614
|
+
if (liveAudioCheckTimer) clearTimeout(liveAudioCheckTimer);
|
|
1615
|
+
liveAudioCheckTimer = setTimeout(() => {
|
|
1616
|
+
liveAudioCheckTimer = null;
|
|
1617
|
+
withAudioLock(reconcileLiveAudio).catch((error) => onError(error));
|
|
1618
|
+
}, delayMs);
|
|
1619
|
+
}
|
|
1620
|
+
|
|
1222
1621
|
async function produceHidden(source, track, extra = {}) {
|
|
1223
1622
|
const options = { track, stopTracks: false, appData: { source }, ...extra };
|
|
1224
1623
|
try {
|
|
@@ -1230,20 +1629,47 @@ export function createClient(options = {}) {
|
|
|
1230
1629
|
}
|
|
1231
1630
|
}
|
|
1232
1631
|
|
|
1632
|
+
function liveEncoding() {
|
|
1633
|
+
return {
|
|
1634
|
+
maxBitrate: rtmpBitrate,
|
|
1635
|
+
maxFramerate: rtmpFps,
|
|
1636
|
+
scaleResolutionDownBy: 1,
|
|
1637
|
+
priority: "high",
|
|
1638
|
+
networkPriority: "high",
|
|
1639
|
+
};
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
/** Bitrate / fps cap + keep the frame size fixed (platforms dislike resolution changes). */
|
|
1643
|
+
async function applyLiveSenderParams(video) {
|
|
1644
|
+
const sender = video?.rtpSender;
|
|
1645
|
+
if (!sender?.getParameters) return;
|
|
1646
|
+
try {
|
|
1647
|
+
const params = sender.getParameters();
|
|
1648
|
+
params.degradationPreference = "maintain-resolution";
|
|
1649
|
+
if (params.encodings?.[0]) Object.assign(params.encodings[0], liveEncoding());
|
|
1650
|
+
await sender.setParameters(params);
|
|
1651
|
+
} catch {
|
|
1652
|
+
/* optional: older browsers ignore some fields */
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
|
|
1233
1656
|
async function closeLiveProducers() {
|
|
1234
|
-
const
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
try {
|
|
1238
|
-
if (socket?.connected) await emit(socket, "closeProducer", { producerId:
|
|
1657
|
+
const video = liveVideo;
|
|
1658
|
+
liveVideo = null;
|
|
1659
|
+
if (video) {
|
|
1660
|
+
try { video.close(); } catch { /* ignore */ }
|
|
1661
|
+
if (socket?.connected) await emit(socket, "closeProducer", { producerId: video.id }).catch(() => {});
|
|
1239
1662
|
}
|
|
1663
|
+
await withAudioLock(teardownLiveAudio);
|
|
1240
1664
|
}
|
|
1241
1665
|
|
|
1242
1666
|
async function createLiveCompositor() {
|
|
1243
1667
|
const next = new LiveCompositor({
|
|
1244
1668
|
fps: rtmpFps,
|
|
1669
|
+
resolution: rtmpResolution,
|
|
1245
1670
|
selfName: displayName,
|
|
1246
|
-
canvasBackground
|
|
1671
|
+
canvasBackground,
|
|
1672
|
+
avatarBackground,
|
|
1247
1673
|
onFailure: (error) => {
|
|
1248
1674
|
if (live !== next) return;
|
|
1249
1675
|
onError(error);
|
|
@@ -1260,7 +1686,10 @@ export function createClient(options = {}) {
|
|
|
1260
1686
|
return next;
|
|
1261
1687
|
}
|
|
1262
1688
|
|
|
1263
|
-
/**
|
|
1689
|
+
/**
|
|
1690
|
+
* Compose the stage and publish hidden rtmp-video (H264) on the origin. Audio is my mic
|
|
1691
|
+
* producer while nothing needs mixing (one Opus stream), else a hidden rtmp-audio mix.
|
|
1692
|
+
*/
|
|
1264
1693
|
async function startLiveOutput() {
|
|
1265
1694
|
if (closed) throw new Error("The session has ended");
|
|
1266
1695
|
if (live) return;
|
|
@@ -1276,22 +1705,13 @@ export function createClient(options = {}) {
|
|
|
1276
1705
|
try {
|
|
1277
1706
|
const video = await produceHidden("rtmp-video", next.videoTrack, {
|
|
1278
1707
|
codec,
|
|
1279
|
-
encodings: [
|
|
1280
|
-
|
|
1708
|
+
encodings: [liveEncoding()],
|
|
1709
|
+
// Start near the target so the first seconds on the platform are not blurry.
|
|
1710
|
+
codecOptions: { videoGoogleStartBitrate: Math.round(rtmpBitrate / 1000 * 0.8) },
|
|
1281
1711
|
});
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
const params = video.rtpSender?.getParameters();
|
|
1286
|
-
if (params) {
|
|
1287
|
-
params.degradationPreference = "maintain-resolution";
|
|
1288
|
-
await video.rtpSender.setParameters(params);
|
|
1289
|
-
}
|
|
1290
|
-
} catch {
|
|
1291
|
-
/* optional */
|
|
1292
|
-
}
|
|
1293
|
-
const audio = await produceHidden("rtmp-audio", next.audioTrack);
|
|
1294
|
-
liveProducers.push(audio);
|
|
1712
|
+
liveVideo = video;
|
|
1713
|
+
await applyLiveSenderParams(video);
|
|
1714
|
+
await withAudioLock(() => setupLiveAudio(next));
|
|
1295
1715
|
} catch (error) {
|
|
1296
1716
|
await closeLiveProducers();
|
|
1297
1717
|
if (live === next) live = null;
|
|
@@ -1299,6 +1719,8 @@ export function createClient(options = {}) {
|
|
|
1299
1719
|
throw error;
|
|
1300
1720
|
}
|
|
1301
1721
|
setRtmpState({});
|
|
1722
|
+
// The stage may have changed while going live (a co-host joined, screen audio…).
|
|
1723
|
+
scheduleLiveAudioCheck();
|
|
1302
1724
|
})();
|
|
1303
1725
|
try {
|
|
1304
1726
|
await startingLive;
|
|
@@ -1326,7 +1748,10 @@ export function createClient(options = {}) {
|
|
|
1326
1748
|
if (!canLiveStream()) {
|
|
1327
1749
|
throw new Error("This browser cannot encode H264, which live streaming needs. Use Chrome, Edge, or Safari.");
|
|
1328
1750
|
}
|
|
1329
|
-
const result = await emit(socket, "startRtmp", {
|
|
1751
|
+
const result = await emit(socket, "startRtmp", {
|
|
1752
|
+
destinations,
|
|
1753
|
+
quality: { keyframeInterval: rtmpKeyframeInterval },
|
|
1754
|
+
}, 15000);
|
|
1330
1755
|
setRtmpState(result.rtmp);
|
|
1331
1756
|
try {
|
|
1332
1757
|
await startLiveOutput();
|
|
@@ -1367,28 +1792,91 @@ export function createClient(options = {}) {
|
|
|
1367
1792
|
return recordingFps;
|
|
1368
1793
|
}
|
|
1369
1794
|
|
|
1370
|
-
/**
|
|
1371
|
-
async function
|
|
1372
|
-
|
|
1373
|
-
if (next === rtmpFps) return rtmpFps;
|
|
1374
|
-
rtmpFps = next;
|
|
1375
|
-
if (!live || liveProducers.length < 2) return rtmpFps;
|
|
1795
|
+
/** Rebuild the live compositor (new fps / resolution) and swap tracks without leaving the platforms. */
|
|
1796
|
+
async function restartLiveCompositor() {
|
|
1797
|
+
if (!live || !liveVideo || !liveAudio) return;
|
|
1376
1798
|
const previous = live;
|
|
1377
1799
|
const replacement = await createLiveCompositor();
|
|
1378
|
-
|
|
1379
|
-
await
|
|
1380
|
-
await
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
if (
|
|
1384
|
-
|
|
1385
|
-
await video.rtpSender.setParameters(params);
|
|
1386
|
-
}
|
|
1387
|
-
} catch {
|
|
1388
|
-
/* optional */
|
|
1389
|
-
}
|
|
1390
|
-
live = replacement;
|
|
1800
|
+
await liveVideo.replaceTrack({ track: replacement.videoTrack });
|
|
1801
|
+
await applyLiveSenderParams(liveVideo);
|
|
1802
|
+
await withAudioLock(async () => {
|
|
1803
|
+
live = replacement;
|
|
1804
|
+
// Shared mic audio is not from the compositor; only a mix producer follows it.
|
|
1805
|
+
if (liveAudio && !liveAudio.shared) await liveAudio.producer.replaceTrack({ track: replacement.audioTrack });
|
|
1806
|
+
});
|
|
1391
1807
|
await previous.finish().catch(() => {});
|
|
1808
|
+
}
|
|
1809
|
+
|
|
1810
|
+
function getRtmpQuality() {
|
|
1811
|
+
return {
|
|
1812
|
+
resolution: rtmpResolution.label,
|
|
1813
|
+
width: rtmpResolution.width,
|
|
1814
|
+
height: rtmpResolution.height,
|
|
1815
|
+
fps: rtmpFps,
|
|
1816
|
+
bitrate: rtmpBitrate,
|
|
1817
|
+
keyframeInterval: rtmpKeyframeInterval,
|
|
1818
|
+
};
|
|
1819
|
+
}
|
|
1820
|
+
|
|
1821
|
+
/**
|
|
1822
|
+
* Update RTMP output quality. Any subset of
|
|
1823
|
+
* `{ resolution: "480p"|"540p"|"720p"|"1080p", fps: 1–60, bitrate: bps (≤ 2 500 000), keyframeInterval: s (1–10) }`.
|
|
1824
|
+
* While live: bitrate applies instantly; fps / resolution restart the compositor in place;
|
|
1825
|
+
* keyframeInterval applies on the next Go Live.
|
|
1826
|
+
*/
|
|
1827
|
+
async function setRtmpQuality(next = {}) {
|
|
1828
|
+
const fps = next.fps != null ? clampFps(next.fps, rtmpFps) : rtmpFps;
|
|
1829
|
+
const resolution = next.resolution != null ? normalizeRtmpResolution(next.resolution, rtmpResolution) : rtmpResolution;
|
|
1830
|
+
const bitrate = next.bitrate != null ? clampRtmpBitrate(next.bitrate, rtmpBitrate) : rtmpBitrate;
|
|
1831
|
+
if (next.keyframeInterval != null) {
|
|
1832
|
+
rtmpKeyframeInterval = clampRtmpKeyframeInterval(next.keyframeInterval, rtmpKeyframeInterval);
|
|
1833
|
+
}
|
|
1834
|
+
const restart = fps !== rtmpFps
|
|
1835
|
+
|| resolution.width !== rtmpResolution.width
|
|
1836
|
+
|| resolution.height !== rtmpResolution.height;
|
|
1837
|
+
const rebitrate = bitrate !== rtmpBitrate;
|
|
1838
|
+
rtmpFps = fps;
|
|
1839
|
+
rtmpResolution = resolution;
|
|
1840
|
+
rtmpBitrate = bitrate;
|
|
1841
|
+
if (live && liveVideo) {
|
|
1842
|
+
if (restart) await restartLiveCompositor();
|
|
1843
|
+
else if (rebitrate) await applyLiveSenderParams(liveVideo);
|
|
1844
|
+
}
|
|
1845
|
+
return getRtmpQuality();
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1848
|
+
/**
|
|
1849
|
+
* Stage look for recording + RTMP (applied live, no restart):
|
|
1850
|
+
* `{ canvasBackground, avatarBackground }` — colors / gradients (see createClient options).
|
|
1851
|
+
* Pass `null` to reset one to the default.
|
|
1852
|
+
*/
|
|
1853
|
+
function setAppearance(next = {}) {
|
|
1854
|
+
const update = {};
|
|
1855
|
+
if (next.canvasBackground !== undefined) {
|
|
1856
|
+
canvasBackground = normalizeCanvasBackground(next.canvasBackground);
|
|
1857
|
+
update.canvasBackground = canvasBackground;
|
|
1858
|
+
}
|
|
1859
|
+
if (next.avatarBackground !== undefined) {
|
|
1860
|
+
avatarBackground = normalizeAvatarBackground(next.avatarBackground);
|
|
1861
|
+
update.avatarBackground = avatarBackground;
|
|
1862
|
+
}
|
|
1863
|
+
recorder?.setAppearance?.(update);
|
|
1864
|
+
live?.setAppearance?.(update);
|
|
1865
|
+
return getAppearance();
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1868
|
+
function getAppearance() {
|
|
1869
|
+
return {
|
|
1870
|
+
canvasBackground,
|
|
1871
|
+
avatarBackground,
|
|
1872
|
+
/** Ready-to-use CSS `background` for your own stage element. */
|
|
1873
|
+
canvasBackgroundCss: gradientToCss(canvasBackground),
|
|
1874
|
+
};
|
|
1875
|
+
}
|
|
1876
|
+
|
|
1877
|
+
/** RTMP frame rate (1–60). Applied live: the compositor restarts and swaps tracks in place. */
|
|
1878
|
+
async function setRtmpFps(fps) {
|
|
1879
|
+
await setRtmpQuality({ fps });
|
|
1392
1880
|
return rtmpFps;
|
|
1393
1881
|
}
|
|
1394
1882
|
|
|
@@ -1404,6 +1892,10 @@ export function createClient(options = {}) {
|
|
|
1404
1892
|
clearInterval(stageDebugTimer);
|
|
1405
1893
|
stageDebugTimer = null;
|
|
1406
1894
|
}
|
|
1895
|
+
if (liveAudioCheckTimer) {
|
|
1896
|
+
clearTimeout(liveAudioCheckTimer);
|
|
1897
|
+
liveAudioCheckTimer = null;
|
|
1898
|
+
}
|
|
1407
1899
|
settleReady?.(new Error("Ended"));
|
|
1408
1900
|
// Finalize only on explicit stop-session / finalize. Browser close uses disconnect handoff.
|
|
1409
1901
|
const forceFinalize = options.finalize === true || options.endSession === true;
|
|
@@ -1448,7 +1940,8 @@ export function createClient(options = {}) {
|
|
|
1448
1940
|
for (const entry of locals.values()) {
|
|
1449
1941
|
detachEnded(entry);
|
|
1450
1942
|
entry.track.stop();
|
|
1451
|
-
entry.producer
|
|
1943
|
+
entry.producer?.close();
|
|
1944
|
+
entry.producer = null;
|
|
1452
1945
|
}
|
|
1453
1946
|
locals.clear();
|
|
1454
1947
|
try { sendTransport?.close(); } catch { /* ignore */ }
|
|
@@ -1509,8 +2002,16 @@ export function createClient(options = {}) {
|
|
|
1509
2002
|
removeRtmpDestination: guard(removeRtmpDestination),
|
|
1510
2003
|
setRtmpFps: guard(setRtmpFps),
|
|
1511
2004
|
getRtmpFps: () => rtmpFps,
|
|
2005
|
+
setRtmpQuality: guard(setRtmpQuality),
|
|
2006
|
+
getRtmpQuality,
|
|
2007
|
+
setAppearance,
|
|
2008
|
+
getAppearance,
|
|
1512
2009
|
canLiveStream,
|
|
1513
2010
|
isRtmpEnabled: () => Boolean(rtmpState.enabled),
|
|
2011
|
+
/** `{ webrtc }` — false while a live-stream-first room has no WebRTC viewer (tracks stay local). */
|
|
2012
|
+
getMediaDemand: () => ({ webrtc: webrtcDemand }),
|
|
2013
|
+
/** Ordered WebRTC video codec preference in use (VP8 first by default). */
|
|
2014
|
+
getVideoCodecs: () => [...videoCodecs],
|
|
1514
2015
|
setRecordingCrop,
|
|
1515
2016
|
setRecordingStage,
|
|
1516
2017
|
getUserList: guard(getUserList),
|