mediascale-sdk 0.1.0 → 1.0.0
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/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mediascale-sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
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
|
|