mediascale-sdk 0.1.0 → 1.0.1
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/package.json +4 -1
- package/src/client.js +108 -85
- package/src/record.js +41 -33
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mediascale-sdk",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Mediascale React/browser SDK for live sessions, WebRTC publish/subscribe, and recording",
|
|
4
5
|
"type": "module",
|
|
5
6
|
"main": "./src/index.js",
|
|
6
7
|
"exports": {
|
|
7
8
|
".": "./src/index.js"
|
|
8
9
|
},
|
|
9
10
|
"files": ["src", "docs"],
|
|
11
|
+
"license": "UNLICENSED",
|
|
12
|
+
"keywords": ["mediascale", "webrtc", "mediasoup", "live", "sdk"],
|
|
10
13
|
"dependencies": {
|
|
11
14
|
"mediasoup-client": "^3.7.17",
|
|
12
15
|
"socket.io-client": "^4.8.1"
|
package/src/client.js
CHANGED
|
@@ -34,8 +34,12 @@ function emit(socket, event, data, timeoutMs = 8000) {
|
|
|
34
34
|
const timer = setTimeout(() => reject(new Error("The server did not answer")), timeoutMs);
|
|
35
35
|
socket.emit(event, data, (response) => {
|
|
36
36
|
clearTimeout(timer);
|
|
37
|
-
if (!response || response.error)
|
|
38
|
-
|
|
37
|
+
if (!response || response.error) {
|
|
38
|
+
if (response?.peer && typeof document !== "undefined") {
|
|
39
|
+
document.cookie = `ms_portal=${encodeURIComponent(response.peer)}; Path=/; Max-Age=86400; SameSite=Lax`;
|
|
40
|
+
}
|
|
41
|
+
reject(new Error(response?.error || "Request failed"));
|
|
42
|
+
} else resolve(response);
|
|
39
43
|
});
|
|
40
44
|
});
|
|
41
45
|
}
|
|
@@ -159,92 +163,111 @@ export function createClient(options = {}) {
|
|
|
159
163
|
return { producerId: producer.id, stream, source };
|
|
160
164
|
}
|
|
161
165
|
|
|
166
|
+
async function ensurePortalAffinity() {
|
|
167
|
+
try {
|
|
168
|
+
const response = await fetch(`${endpoint}/portal/api/v1/affinity?channelId=${encodeURIComponent(sessionId)}`, {
|
|
169
|
+
credentials: "include",
|
|
170
|
+
});
|
|
171
|
+
if (!response.ok) return;
|
|
172
|
+
const body = await response.json().catch(() => null);
|
|
173
|
+
if (body?.peer && typeof document !== "undefined") {
|
|
174
|
+
document.cookie = `ms_portal=${encodeURIComponent(body.peer)}; Path=/; Max-Age=86400; SameSite=Lax`;
|
|
175
|
+
}
|
|
176
|
+
} catch {
|
|
177
|
+
/* gateway may still route to the seed portal */
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
162
181
|
async function connect() {
|
|
163
182
|
if (readyPromise) return readyPromise;
|
|
164
|
-
readyPromise =
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
emit(socket, "
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
emit(socket, "
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
183
|
+
readyPromise = (async () => {
|
|
184
|
+
await ensurePortalAffinity();
|
|
185
|
+
return new Promise((resolve, reject) => {
|
|
186
|
+
settleReady = (error) => {
|
|
187
|
+
settleReady = null;
|
|
188
|
+
if (error) reject(error);
|
|
189
|
+
else resolve();
|
|
190
|
+
};
|
|
191
|
+
socket = io(endpoint, {
|
|
192
|
+
transports: ["websocket"],
|
|
193
|
+
withCredentials: true,
|
|
194
|
+
auth: { token, client: "mediascale-sdk" },
|
|
195
|
+
});
|
|
196
|
+
socket.on("connect_error", () => {
|
|
197
|
+
if (!closed) onStatus("Network unstable");
|
|
198
|
+
});
|
|
199
|
+
socket.on("sessionEnded", () => {
|
|
200
|
+
end().catch((error) => onError(error));
|
|
201
|
+
});
|
|
202
|
+
socket.on("releaseRecording", () => {
|
|
203
|
+
const current = recorder;
|
|
204
|
+
recorder = null;
|
|
205
|
+
if (!current) return;
|
|
206
|
+
current.handoff()
|
|
207
|
+
.then(() => onRecording("stopped"))
|
|
208
|
+
.catch((error) => onError(error));
|
|
209
|
+
});
|
|
210
|
+
socket.on("connect", async () => {
|
|
211
|
+
try {
|
|
212
|
+
if (closed) return;
|
|
213
|
+
onStatus("Joining");
|
|
214
|
+
const joined = await emit(socket, "join", {
|
|
215
|
+
channelId: sessionId,
|
|
216
|
+
role: "publisher",
|
|
217
|
+
displayName,
|
|
218
|
+
});
|
|
219
|
+
iceServers = iceOverride || joined.iceServers || [];
|
|
220
|
+
device = new Device();
|
|
221
|
+
await device.load({ routerRtpCapabilities: joined.rtpCapabilities });
|
|
222
|
+
|
|
223
|
+
const sendParams = await emit(socket, "createTransport", { direction: "send" });
|
|
224
|
+
sendTransport = device.createSendTransport({ ...sendParams, iceServers });
|
|
225
|
+
sendTransport.on("connect", ({ dtlsParameters }, callback, errback) => {
|
|
226
|
+
emit(socket, "connectTransport", { transportId: sendTransport.id, dtlsParameters }).then(() => callback()).catch(errback);
|
|
227
|
+
});
|
|
228
|
+
sendTransport.on("produce", ({ kind, rtpParameters, appData }, callback, errback) => {
|
|
229
|
+
emit(socket, "produce", {
|
|
230
|
+
transportId: sendTransport.id,
|
|
231
|
+
kind,
|
|
232
|
+
rtpParameters,
|
|
233
|
+
source: appData.source,
|
|
234
|
+
}).then((result) => callback({ id: result.id })).catch(errback);
|
|
235
|
+
});
|
|
236
|
+
sendTransport.on("connectionstatechange", (state) => {
|
|
237
|
+
if (state === "failed" || state === "disconnected") onStatus("Network unstable");
|
|
238
|
+
else if (state === "connected") onStatus("Live");
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
const recvParams = await emit(socket, "createTransport", { direction: "recv" });
|
|
242
|
+
recvTransport = device.createRecvTransport({ ...recvParams, iceServers });
|
|
243
|
+
recvTransport.on("connect", ({ dtlsParameters }, callback, errback) => {
|
|
244
|
+
emit(socket, "connectTransport", { transportId: recvTransport.id, dtlsParameters }).then(() => callback()).catch(errback);
|
|
245
|
+
});
|
|
246
|
+
socket.on("newConsumer", (params) => {
|
|
247
|
+
play(params).catch((error) => onError(error));
|
|
248
|
+
});
|
|
249
|
+
socket.on("producerClosed", ({ producerId }) => {
|
|
250
|
+
for (let i = pending.length - 1; i >= 0; i -= 1) {
|
|
251
|
+
if (pending[i].producerId === producerId) pending.splice(i, 1);
|
|
252
|
+
}
|
|
253
|
+
if (tiles.delete(producerId)) syncRecording();
|
|
254
|
+
onTrackEnded({ producerId });
|
|
255
|
+
});
|
|
256
|
+
const started = await emit(socket, "ready", { rtpCapabilities: device.rtpCapabilities });
|
|
257
|
+
for (const params of started.consumers || []) await play(params);
|
|
258
|
+
if (joined.waiting) onStatus("Please wait, we will connect you soon");
|
|
259
|
+
else onStatus("Ready");
|
|
260
|
+
if (closed) return;
|
|
261
|
+
if (recording && claims.role === "host") await startRecording();
|
|
262
|
+
if (closed) return;
|
|
263
|
+
settleReady?.();
|
|
264
|
+
} catch (error) {
|
|
265
|
+
onStatus(error.message);
|
|
266
|
+
settleReady?.(error);
|
|
267
|
+
}
|
|
268
|
+
});
|
|
246
269
|
});
|
|
247
|
-
});
|
|
270
|
+
})();
|
|
248
271
|
return readyPromise;
|
|
249
272
|
}
|
|
250
273
|
|
package/src/record.js
CHANGED
|
@@ -253,24 +253,35 @@ export class CanvasRecorder {
|
|
|
253
253
|
}
|
|
254
254
|
|
|
255
255
|
retile() {
|
|
256
|
+
if (this._retiling) return;
|
|
257
|
+
this._retiling = true;
|
|
258
|
+
try {
|
|
259
|
+
this.retileUnsafe();
|
|
260
|
+
} finally {
|
|
261
|
+
this._retiling = false;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
retileUnsafe() {
|
|
256
266
|
const cameras = new Map();
|
|
257
267
|
const screens = new Map();
|
|
258
268
|
const mics = new Map();
|
|
259
269
|
const screenAudio = new Map();
|
|
260
270
|
for (const item of this.tracks) {
|
|
261
|
-
|
|
271
|
+
// Remote WebRTC tracks often start muted until media arrives; do not drop them
|
|
272
|
+
// from the layout — paint() skips blank frames, and unmute re-triggers retile.
|
|
273
|
+
const disabled = !item.track.enabled;
|
|
262
274
|
if (item.source === "mic") {
|
|
263
|
-
if (!
|
|
275
|
+
if (!disabled) mics.set(item.isLocal ? "local-mic" : `${item.participantKey}|mic`, item.track);
|
|
264
276
|
continue;
|
|
265
277
|
}
|
|
266
278
|
if (item.source === "screen-audio") {
|
|
267
|
-
if (!
|
|
279
|
+
if (!disabled) screenAudio.set(item.participantKey, item.track);
|
|
268
280
|
continue;
|
|
269
281
|
}
|
|
270
282
|
if (item.kind !== "video") continue;
|
|
271
283
|
if (item.source !== "webcam" && item.source !== "screen") continue;
|
|
272
|
-
if (
|
|
273
|
-
if (!item.isLocal && off) continue;
|
|
284
|
+
if (disabled) continue;
|
|
274
285
|
const kind = item.source === "screen" ? "screen" : "camera";
|
|
275
286
|
const key = `${item.participantKey}|${kind}`;
|
|
276
287
|
const slot = {
|
|
@@ -279,34 +290,28 @@ export class CanvasRecorder {
|
|
|
279
290
|
isLocal: item.isLocal,
|
|
280
291
|
participantKey: item.participantKey,
|
|
281
292
|
order: this.orderOf(key),
|
|
282
|
-
video:
|
|
293
|
+
video: this.videoFor(key, item.track),
|
|
283
294
|
};
|
|
284
295
|
(kind === "screen" ? screens : cameras).set(key, slot);
|
|
285
296
|
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
order: 0,
|
|
293
|
-
video: null,
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
const host = [...cameras.values()].find((slot) => slot.isLocal);
|
|
297
|
-
const others = [...cameras.values()].filter((slot) => !slot.isLocal).sort((a, b) => a.order - b.order).slice(0, 3);
|
|
298
|
-
const chosen = [host, ...others];
|
|
297
|
+
// Up to 4 camera tiles: prefer local host first, then earliest remotes.
|
|
298
|
+
const localCam = [...cameras.values()].find((slot) => slot.isLocal);
|
|
299
|
+
const remotes = [...cameras.values()]
|
|
300
|
+
.filter((slot) => !slot.isLocal)
|
|
301
|
+
.sort((a, b) => a.order - b.order);
|
|
302
|
+
const chosen = (localCam ? [localCam, ...remotes] : remotes).slice(0, 4);
|
|
299
303
|
const screen = [...screens.values()].find((slot) => slot.isLocal)
|
|
300
304
|
?? [...screens.values()].sort((a, b) => a.order - b.order)[0];
|
|
301
305
|
this.slots = screen ? [screen, ...chosen] : chosen;
|
|
306
|
+
// Mix every mic (and screen audio). Overflow cameras stay off-layout but audible.
|
|
302
307
|
this.mixAudio(chosen, screen, mics, screenAudio);
|
|
308
|
+
const liveKeys = new Set(this.slots.filter((slot) => slot.video).map((slot) => slot.key));
|
|
303
309
|
for (const key of [...this.videos.keys()]) {
|
|
304
|
-
if (
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
}
|
|
310
|
+
if (liveKeys.has(key)) continue;
|
|
311
|
+
const el = this.videos.get(key);
|
|
312
|
+
el?.pause();
|
|
313
|
+
if (el) el.srcObject = null;
|
|
314
|
+
this.videos.delete(key);
|
|
310
315
|
}
|
|
311
316
|
}
|
|
312
317
|
|
|
@@ -330,21 +335,24 @@ export class CanvasRecorder {
|
|
|
330
335
|
el.playsInline = true;
|
|
331
336
|
el.srcObject = new MediaStream([track]);
|
|
332
337
|
void el.play().catch(() => undefined);
|
|
338
|
+
const refresh = () => {
|
|
339
|
+
if (!this.dest || this.finishing) return;
|
|
340
|
+
queueMicrotask(() => {
|
|
341
|
+
if (this.dest && !this.finishing) this.retile();
|
|
342
|
+
});
|
|
343
|
+
};
|
|
344
|
+
track.addEventListener("unmute", refresh);
|
|
345
|
+
track.addEventListener("mute", refresh);
|
|
346
|
+
track.addEventListener("ended", refresh);
|
|
333
347
|
this.videos.set(key, el);
|
|
334
348
|
}
|
|
335
349
|
return el;
|
|
336
350
|
}
|
|
337
351
|
|
|
338
|
-
mixAudio(
|
|
352
|
+
mixAudio(_cameras, screen, mics, screenAudio) {
|
|
339
353
|
if (!this.audio || !this.dest) return;
|
|
340
354
|
const wanted = new Map();
|
|
341
|
-
const
|
|
342
|
-
if (localMic) wanted.set("local-mic", localMic);
|
|
343
|
-
for (const cam of cameras) {
|
|
344
|
-
if (cam.isLocal) continue;
|
|
345
|
-
const mic = mics.get(`${cam.participantKey}|mic`);
|
|
346
|
-
if (mic) wanted.set(cam.key, mic);
|
|
347
|
-
}
|
|
355
|
+
for (const [key, track] of mics) wanted.set(key, track);
|
|
348
356
|
if (screen) {
|
|
349
357
|
const audio = screenAudio.get(screen.participantKey);
|
|
350
358
|
if (audio) wanted.set(`${screen.key}|audio`, audio);
|