@midsummer-ai/sdk 0.1.0-staging.4510 → 0.1.0-staging.4516
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/calls/contract.d.mts +1 -1
- package/calls/index.d.mts +7 -2
- package/calls/index.mjs +199 -68
- package/calls/protocol.d.mts +1 -1
- package/calls/react.d.mts +1 -1
- package/calls/stand-in.d.mts +1 -1
- package/calls/ui.mjs +11 -7
- package/here-C2jhTheQ.mjs +1 -1
- package/package.json +1 -1
- package/{protocol-BeBvbfKf.d.mts → protocol-_2SNN1Nz.d.mts} +5 -0
package/calls/contract.d.mts
CHANGED
package/calls/index.d.mts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { s as Json } from "../index-c616Dbux.mjs";
|
|
2
|
-
import { a as CallToken, c as Layer, d as TrackName, r as CallErrorCode } from "../protocol-
|
|
2
|
+
import { a as CallToken, c as Layer, d as TrackName, r as CallErrorCode } from "../protocol-_2SNN1Nz.mjs";
|
|
3
3
|
//#region src/calls/index.d.ts
|
|
4
4
|
/** The most a message sent with `send` carries, as JSON. */
|
|
5
5
|
declare const MAX_MESSAGE_BYTES: number;
|
|
6
6
|
/** The most videos a participant receives at once, and how many of them at the full layer. */
|
|
7
7
|
declare const MAX_VIDEOS = 9;
|
|
8
8
|
declare const MAX_FULL_VIDEOS = 2;
|
|
9
|
+
/**
|
|
10
|
+
* What a call asks a camera for: 1280×720, which its three layers are cut from. A browser left to choose gives 640×480,
|
|
11
|
+
* of which it sends two layers at most, the largest a quarter of that size.
|
|
12
|
+
*/
|
|
13
|
+
declare const CAMERA: MediaTrackConstraints;
|
|
9
14
|
/** Why a call or a call's request failed, in words a person reads; `code` is the one the wire carries. */
|
|
10
15
|
/**
|
|
11
16
|
* Why a call failed, in the person's terms, where the code alone can't say: `blocked`, the network let no connection
|
|
@@ -152,4 +157,4 @@ interface Call {
|
|
|
152
157
|
*/
|
|
153
158
|
declare function join(room: string, options: JoinOptions): Promise<Call>;
|
|
154
159
|
//#endregion
|
|
155
|
-
export { Call, CallError, type CallErrorCode, CallEvents, CallFailure, CallState, type CallToken, CallTokenInput, JoinOptions, type Json, type Layer, LocalTrack, MAX_FULL_VIDEOS, MAX_MESSAGE_BYTES, MAX_VIDEOS, Participant, Rtc, TokenSource, type TrackName, callToken, identityProblem, join, roomProblem };
|
|
160
|
+
export { CAMERA, Call, CallError, type CallErrorCode, CallEvents, CallFailure, CallState, type CallToken, CallTokenInput, JoinOptions, type Json, type Layer, LocalTrack, MAX_FULL_VIDEOS, MAX_MESSAGE_BYTES, MAX_VIDEOS, Participant, Rtc, TokenSource, type TrackName, callToken, identityProblem, join, roomProblem };
|
package/calls/index.mjs
CHANGED
|
@@ -36,13 +36,17 @@ import { CALL_STATUS, TRACK_KINDS } from "./protocol.mjs";
|
|
|
36
36
|
*
|
|
37
37
|
* A call's `state` is `connecting` while it joins, `waiting` while nobody else is in the room, `in-call` once someone
|
|
38
38
|
* is (whether or not they send anything), `reconnecting` while the connection dropped and the call joins the room again
|
|
39
|
-
* (for up to 30 seconds, as the same participant), `ended` once the room was
|
|
40
|
-
* `error`, a `CallError` whose `code` shows why. Each change is a `state` event.
|
|
39
|
+
* (for up to 30 seconds, as the same participant, and back once its media connects again), `ended` once the room was
|
|
40
|
+
* ended or you left, and `failed` with `error`, a `CallError` whose `code` shows why. Each change is a `state` event.
|
|
41
|
+
* A call whose media can't connect within 15 seconds of joining fails with the reason `blocked`: the network lets
|
|
42
|
+
* nothing through to it.
|
|
41
43
|
*
|
|
42
44
|
* Every audio track in the room is received. Videos are received too, up to 9 at once at the half layer (`h`), unless
|
|
43
45
|
* `autoVideo` is false; a grid that shows fewer tiles asks for what it shows with `receive`, `setLayer` and
|
|
44
|
-
* `stopReceiving`. A camera is sent in three layers: `q` (a quarter of its size, about
|
|
45
|
-
* and `f` (full, 1.5 Mbps).
|
|
46
|
+
* `stopReceiving`. A camera is asked for at 1280×720 and sent in three layers: `q` (a quarter of its size, about
|
|
47
|
+
* 150 kbps), `h` (half, 500 kbps) and `f` (full, 1.5 Mbps). One that gives less than 960×540 is sent in two, its full
|
|
48
|
+
* size as `h` and half as `q`, and asking for `f` gets `h`. A participant receives at most 9 videos, 2 of them at
|
|
49
|
+
* `f`; asking for more is refused.
|
|
46
50
|
*
|
|
47
51
|
* Under `midsummer dev` and in tests there's no media server (`media: "relay"`): each pair of participants connects
|
|
48
52
|
* directly. The participant with the smaller id makes the one offer, with four two-way transceivers in the order mic,
|
|
@@ -71,6 +75,8 @@ const TRACK_ORDER = [
|
|
|
71
75
|
"screen",
|
|
72
76
|
"screen-audio"
|
|
73
77
|
];
|
|
78
|
+
/** How long a connection has to reach the media, from its first offer answered, before the network is taken to block it. */
|
|
79
|
+
const CONNECT_WITHIN_MS = 15e3;
|
|
74
80
|
/** A camera's three layers, smallest first. */
|
|
75
81
|
const SIMULCAST = [
|
|
76
82
|
{
|
|
@@ -89,6 +95,34 @@ const SIMULCAST = [
|
|
|
89
95
|
maxBitrate: 15e5
|
|
90
96
|
}
|
|
91
97
|
];
|
|
98
|
+
/**
|
|
99
|
+
* What a call asks a camera for: 1280×720, which its three layers are cut from. A browser left to choose gives 640×480,
|
|
100
|
+
* of which it sends two layers at most, the largest a quarter of that size.
|
|
101
|
+
*/
|
|
102
|
+
const CAMERA = {
|
|
103
|
+
width: { ideal: 1280 },
|
|
104
|
+
height: { ideal: 720 }
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* The layers a camera is sent in, smallest first. A browser sends three layers only of a picture of 960×540 or more,
|
|
108
|
+
* two of one from 480×270 and one of anything smaller, and leaves out the last it was asked for: so a smaller camera
|
|
109
|
+
* is asked for as many as it sends, its full size among them. They keep the smaller layers' names, and asking for one
|
|
110
|
+
* that isn't sent gets the largest that is.
|
|
111
|
+
*/
|
|
112
|
+
function layersOf(track) {
|
|
113
|
+
const { width, height } = track.getSettings?.() ?? {};
|
|
114
|
+
const pixels = width && height ? width * height : Number.POSITIVE_INFINITY;
|
|
115
|
+
if (pixels >= 518400) return SIMULCAST;
|
|
116
|
+
if (pixels >= 129600) return [{
|
|
117
|
+
rid: "q",
|
|
118
|
+
scaleResolutionDownBy: 2,
|
|
119
|
+
maxBitrate: 15e4
|
|
120
|
+
}, {
|
|
121
|
+
rid: "h",
|
|
122
|
+
scaleResolutionDownBy: 1,
|
|
123
|
+
maxBitrate: 5e5
|
|
124
|
+
}];
|
|
125
|
+
}
|
|
92
126
|
var CallError = class extends Error {
|
|
93
127
|
code;
|
|
94
128
|
reason;
|
|
@@ -239,6 +273,7 @@ const gathered = (pc) => new Promise((resolve) => {
|
|
|
239
273
|
pc.addEventListener("icecandidate", last);
|
|
240
274
|
});
|
|
241
275
|
const sizeProblem = (bytes) => `A message is at most 16 KiB as JSON; this one is ${Math.ceil(bytes / 1024)} KiB.`;
|
|
276
|
+
const audioOnlyProblem = "This room is audio-only, so it takes no camera or screen.";
|
|
242
277
|
const videoCapProblem = `A participant receives at most 9 videos at once. Stop receiving one first, as a grid does for tiles out of view.`;
|
|
243
278
|
const fullCapProblem = `A participant receives at most 2 videos at full quality at once. Ask for h or q for the others.`;
|
|
244
279
|
/** Messages sent before a channel opens wait for it, a few at most. */
|
|
@@ -247,7 +282,7 @@ function channelSend(channel, outbox, text) {
|
|
|
247
282
|
else if (channel.readyState === "connecting" && outbox.length < 64) outbox.push(text);
|
|
248
283
|
}
|
|
249
284
|
/** Media through the media server: one connection, a transceiver for each track published and each received. */
|
|
250
|
-
function sfuMedia(ctx, auto, declined) {
|
|
285
|
+
function sfuMedia(ctx, auto, declined, offer) {
|
|
251
286
|
const pc = new ctx.rtc.RTCPeerConnection(configOf(ctx.iceServers));
|
|
252
287
|
const sent = /* @__PURE__ */ new Map();
|
|
253
288
|
const pulled = /* @__PURE__ */ new Map();
|
|
@@ -256,11 +291,16 @@ function sfuMedia(ctx, auto, declined) {
|
|
|
256
291
|
const channels = /* @__PURE__ */ new Map();
|
|
257
292
|
const outbox = [];
|
|
258
293
|
let own;
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
294
|
+
/** Whether the channels are being asked for now, so one ask goes at a time, and whether the room changed meanwhile. */
|
|
295
|
+
let dataAsking = false;
|
|
296
|
+
let dataAgain = false;
|
|
297
|
+
/** Each person's connection as last seen: what was received from an earlier one is dropped. */
|
|
298
|
+
const connections = /* @__PURE__ */ new Map();
|
|
262
299
|
let others = [];
|
|
263
300
|
let closed = false;
|
|
301
|
+
let connectBy;
|
|
302
|
+
/** Whether the room changed while a track of it was on its way, so the tracks to receive are worked out again once it has come. */
|
|
303
|
+
let missed = false;
|
|
264
304
|
let chain = Promise.resolve();
|
|
265
305
|
const serial = (work) => {
|
|
266
306
|
const next = chain.then(work, work);
|
|
@@ -277,14 +317,42 @@ function sfuMedia(ctx, auto, declined) {
|
|
|
277
317
|
ctx.arrived(pull.of, pull.name, event.track);
|
|
278
318
|
});
|
|
279
319
|
pc.addEventListener("connectionstatechange", () => {
|
|
280
|
-
if (pc.connectionState === "connected")
|
|
320
|
+
if (pc.connectionState === "connected") {
|
|
321
|
+
clearTimeout(connectBy);
|
|
322
|
+
ctx.reached();
|
|
323
|
+
}
|
|
281
324
|
if (!closed && (pc.connectionState === "failed" || pc.connectionState === "disconnected")) ctx.broken();
|
|
282
325
|
});
|
|
283
|
-
|
|
326
|
+
const timed = () => {
|
|
327
|
+
if (!closed && connectBy === void 0 && pc.connectionState !== "connected") connectBy = setTimeout(() => {
|
|
328
|
+
if (!closed && pc.connectionState !== "connected") ctx.broken();
|
|
329
|
+
}, CONNECT_WITHIN_MS);
|
|
330
|
+
};
|
|
331
|
+
const answer = async (offered) => {
|
|
332
|
+
try {
|
|
333
|
+
await pc.setRemoteDescription(offered);
|
|
334
|
+
await pc.setLocalDescription(await pc.createAnswer());
|
|
335
|
+
await ctx.post("answer", {
|
|
336
|
+
participant: ctx.me,
|
|
337
|
+
answer: sdpOf(pc.localDescription)
|
|
338
|
+
});
|
|
339
|
+
} finally {
|
|
340
|
+
timed();
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
if (offer) serial(() => answer(offer)).catch(ctx.failed);
|
|
344
|
+
/** The people whose channel isn't received yet. */
|
|
345
|
+
const unheard = () => others.filter((other) => other.kind === "person" && !channels.has(other.id)).map((other) => other.id);
|
|
346
|
+
/** Opens this call's channel, once, and receives the channel of each person who has one open and isn't received yet: who was asked for. */
|
|
347
|
+
async function openData() {
|
|
348
|
+
const from = unheard();
|
|
349
|
+
if (closed || own && !from.length) return from;
|
|
350
|
+
const were = new Map(from.map((of) => [of, connections.get(of)]));
|
|
284
351
|
const opened = await ctx.post("data", {
|
|
285
352
|
participant: ctx.me,
|
|
286
353
|
from
|
|
287
354
|
});
|
|
355
|
+
if (closed) return from;
|
|
288
356
|
if (opened.own !== void 0 && !own) {
|
|
289
357
|
own = pc.createDataChannel("data", {
|
|
290
358
|
negotiated: true,
|
|
@@ -296,6 +364,7 @@ function sfuMedia(ctx, auto, declined) {
|
|
|
296
364
|
});
|
|
297
365
|
}
|
|
298
366
|
for (const { of, id } of opened.from) {
|
|
367
|
+
if (channels.has(of) || connections.get(of) !== were.get(of)) continue;
|
|
299
368
|
const channel = pc.createDataChannel(`data:${of}`, {
|
|
300
369
|
negotiated: true,
|
|
301
370
|
id
|
|
@@ -303,6 +372,25 @@ function sfuMedia(ctx, auto, declined) {
|
|
|
303
372
|
channel.addEventListener("message", (event) => ctx.message(of, String(event.data)));
|
|
304
373
|
channels.set(of, channel);
|
|
305
374
|
}
|
|
375
|
+
return from;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Asks for the channels, one ask at a time, behind the join's offer being answered. Someone who came while an ask
|
|
379
|
+
* was out is asked for after it; someone whose own isn't open yet is asked for again when the room next changes,
|
|
380
|
+
* which their opening it does; a refusal likewise.
|
|
381
|
+
*/
|
|
382
|
+
function askData() {
|
|
383
|
+
if (dataAsking) dataAgain = true;
|
|
384
|
+
if (dataAsking || closed || own && !unheard().length) return;
|
|
385
|
+
dataAsking = true;
|
|
386
|
+
dataAgain = false;
|
|
387
|
+
serial(openData).then((asked) => {
|
|
388
|
+
dataAsking = false;
|
|
389
|
+
if (dataAgain || unheard().some((of) => !asked.includes(of))) askData();
|
|
390
|
+
}, (error) => {
|
|
391
|
+
dataAsking = false;
|
|
392
|
+
ctx.failed(error);
|
|
393
|
+
});
|
|
306
394
|
}
|
|
307
395
|
const pull = (tracks) => {
|
|
308
396
|
for (const track of tracks) pending.add(key(track.of, track.name));
|
|
@@ -311,37 +399,73 @@ function sfuMedia(ctx, auto, declined) {
|
|
|
311
399
|
const still = tracks.filter((track) => publishes(track.of, track.name) && !find(track.of, track.name));
|
|
312
400
|
for (let at = 0; at < still.length && !closed; at += PULL_MAX) {
|
|
313
401
|
const chunk = still.slice(at, at + PULL_MAX);
|
|
314
|
-
const
|
|
402
|
+
const asked = new Map(chunk.map((track) => [track.of, connections.get(track.of)]));
|
|
403
|
+
const pulledNow = await ctx.post("pull", {
|
|
315
404
|
participant: ctx.me,
|
|
316
405
|
tracks: chunk
|
|
317
406
|
});
|
|
318
|
-
for (const track of
|
|
319
|
-
const layer = chunk.find((
|
|
407
|
+
for (const track of pulledNow.tracks) {
|
|
408
|
+
const layer = chunk.find((each) => each.of === track.of && each.name === track.name)?.layer;
|
|
320
409
|
pulled.set(track.mid, {
|
|
321
410
|
of: track.of,
|
|
322
411
|
name: track.name,
|
|
323
412
|
...layer ? { layer } : {}
|
|
324
413
|
});
|
|
325
414
|
}
|
|
326
|
-
await
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
}
|
|
415
|
+
await answer(pulledNow.offer);
|
|
416
|
+
const old = pulledNow.tracks.filter((track) => pulled.has(track.mid) && connections.get(track.of) !== asked.get(track.of));
|
|
417
|
+
for (const track of old) {
|
|
418
|
+
if (pulled.get(track.mid)?.track) ctx.ended(track.of, track.name);
|
|
419
|
+
pulled.delete(track.mid);
|
|
420
|
+
}
|
|
421
|
+
if (old.length) {
|
|
422
|
+
missed = true;
|
|
423
|
+
await ctx.post("unpull", {
|
|
424
|
+
participant: ctx.me,
|
|
425
|
+
mids: old.map((track) => track.mid)
|
|
426
|
+
});
|
|
427
|
+
}
|
|
332
428
|
}
|
|
333
429
|
} finally {
|
|
334
430
|
for (const track of tracks) pending.delete(key(track.of, track.name));
|
|
431
|
+
if (missed) {
|
|
432
|
+
missed = false;
|
|
433
|
+
want(false);
|
|
434
|
+
}
|
|
335
435
|
}
|
|
336
436
|
});
|
|
337
437
|
};
|
|
438
|
+
/** Receives every sound others publish, and their videos up to 9 unless the page chooses them, that isn't received or on its way. */
|
|
439
|
+
function want(changed) {
|
|
440
|
+
if (closed) return;
|
|
441
|
+
const wanted = [];
|
|
442
|
+
let video = videos().length + [...pending].filter((each) => TRACK_KINDS[each.slice(each.lastIndexOf("/") + 1)] === "video").length;
|
|
443
|
+
for (const other of others) for (const track of other.tracks) {
|
|
444
|
+
const at = key(other.id, track.name);
|
|
445
|
+
if (changed && pending.has(at)) missed = true;
|
|
446
|
+
if (find(other.id, track.name) || pending.has(at) || declined.has(at)) continue;
|
|
447
|
+
if (track.kind === "audio") wanted.push({
|
|
448
|
+
of: other.id,
|
|
449
|
+
name: track.name
|
|
450
|
+
});
|
|
451
|
+
else if (auto && video < 9) {
|
|
452
|
+
wanted.push({
|
|
453
|
+
of: other.id,
|
|
454
|
+
name: track.name,
|
|
455
|
+
layer: "h"
|
|
456
|
+
});
|
|
457
|
+
video += 1;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
if (wanted.length) pull(wanted).catch(ctx.failed);
|
|
461
|
+
}
|
|
338
462
|
return {
|
|
339
463
|
publish: (tracks) => serial(async () => {
|
|
340
464
|
const made = tracks.map(({ name, track }) => ({
|
|
341
465
|
name,
|
|
342
466
|
transceiver: pc.addTransceiver(track, {
|
|
343
467
|
direction: "sendonly",
|
|
344
|
-
...name === "camera" ? { sendEncodings:
|
|
468
|
+
...name === "camera" && layersOf(track) ? { sendEncodings: layersOf(track) } : {},
|
|
345
469
|
...ctx.rtc.MediaStream ? { streams: [new ctx.rtc.MediaStream([track])] } : {}
|
|
346
470
|
})
|
|
347
471
|
}));
|
|
@@ -356,11 +480,15 @@ function sfuMedia(ctx, auto, declined) {
|
|
|
356
480
|
}))
|
|
357
481
|
});
|
|
358
482
|
if (answered.answer) await pc.setRemoteDescription(answered.answer);
|
|
483
|
+
timed();
|
|
359
484
|
for (const { name, transceiver } of made) sent.set(name, transceiver);
|
|
360
485
|
return answered.room;
|
|
361
486
|
} catch (error) {
|
|
362
487
|
if (pc.signalingState === "have-local-offer") await pc.setLocalDescription({ type: "rollback" });
|
|
363
|
-
for (const { transceiver } of made)
|
|
488
|
+
for (const { transceiver } of made) {
|
|
489
|
+
transceiver.direction = "inactive";
|
|
490
|
+
await transceiver.sender.replaceTrack(null);
|
|
491
|
+
}
|
|
364
492
|
throw error;
|
|
365
493
|
}
|
|
366
494
|
}),
|
|
@@ -380,7 +508,10 @@ function sfuMedia(ctx, auto, declined) {
|
|
|
380
508
|
},
|
|
381
509
|
sync(list) {
|
|
382
510
|
others = list;
|
|
383
|
-
const
|
|
511
|
+
const back = new Set(list.filter((other) => connections.has(other.id) && connections.get(other.id) !== (other.connection ?? 1)).map((other) => other.id));
|
|
512
|
+
for (const other of list) connections.set(other.id, other.connection ?? 1);
|
|
513
|
+
for (const of of [...connections.keys()]) if (!list.some((other) => other.id === of)) connections.delete(of);
|
|
514
|
+
const gone = [...pulled].filter(([, pull]) => back.has(pull.of) || !publishes(pull.of, pull.name));
|
|
384
515
|
for (const [mid, pull] of gone) {
|
|
385
516
|
pulled.delete(mid);
|
|
386
517
|
if (pull.track) ctx.ended(pull.of, pull.name);
|
|
@@ -389,36 +520,12 @@ function sfuMedia(ctx, auto, declined) {
|
|
|
389
520
|
participant: ctx.me,
|
|
390
521
|
mids: gone.map(([mid]) => mid)
|
|
391
522
|
}).catch(ctx.failed);
|
|
392
|
-
for (const of of [...
|
|
523
|
+
for (const of of [...channels.keys()]) if (back.has(of) || !list.some((other) => other.id === of)) {
|
|
393
524
|
channels.get(of)?.close();
|
|
394
525
|
channels.delete(of);
|
|
395
|
-
dataFrom.delete(of);
|
|
396
526
|
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
if (!dataAsked || fresh.length) {
|
|
400
|
-
dataAsked = true;
|
|
401
|
-
serial(() => openData(fresh)).catch(ctx.failed);
|
|
402
|
-
}
|
|
403
|
-
const wanted = [];
|
|
404
|
-
let video = videos().length + [...pending].filter((each) => TRACK_KINDS[each.slice(each.lastIndexOf("/") + 1)] === "video").length;
|
|
405
|
-
for (const other of list) for (const track of other.tracks) {
|
|
406
|
-
const at = key(other.id, track.name);
|
|
407
|
-
if (find(other.id, track.name) || pending.has(at) || declined.has(at)) continue;
|
|
408
|
-
if (track.kind === "audio") wanted.push({
|
|
409
|
-
of: other.id,
|
|
410
|
-
name: track.name
|
|
411
|
-
});
|
|
412
|
-
else if (auto && video < 9) {
|
|
413
|
-
wanted.push({
|
|
414
|
-
of: other.id,
|
|
415
|
-
name: track.name,
|
|
416
|
-
layer: "h"
|
|
417
|
-
});
|
|
418
|
-
video += 1;
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
if (wanted.length) pull(wanted).catch(ctx.failed);
|
|
527
|
+
askData();
|
|
528
|
+
want(true);
|
|
422
529
|
},
|
|
423
530
|
signals() {},
|
|
424
531
|
async receive(of, name, layer) {
|
|
@@ -469,6 +576,7 @@ function sfuMedia(ctx, auto, declined) {
|
|
|
469
576
|
},
|
|
470
577
|
close() {
|
|
471
578
|
closed = true;
|
|
579
|
+
clearTimeout(connectBy);
|
|
472
580
|
for (const channel of channels.values()) channel.close();
|
|
473
581
|
own?.close();
|
|
474
582
|
pc.close();
|
|
@@ -656,6 +764,9 @@ var LiveCall = class {
|
|
|
656
764
|
left = false;
|
|
657
765
|
/** Whether media ever connected, which tells a dropped call from a network that let nothing through. */
|
|
658
766
|
connectedOnce = false;
|
|
767
|
+
/** While a dropped call joins again: when it gives up. The call is back once its media connects, not once the room answers. */
|
|
768
|
+
rejoinUntil;
|
|
769
|
+
rejoining = false;
|
|
659
770
|
constructor(room, options, rtc) {
|
|
660
771
|
this.room = room;
|
|
661
772
|
this.options = options;
|
|
@@ -717,12 +828,13 @@ var LiveCall = class {
|
|
|
717
828
|
this.schedule();
|
|
718
829
|
const session = {
|
|
719
830
|
participant: joined.participant.id,
|
|
831
|
+
connects: joined.media === "sfu" && joined.offer !== void 0,
|
|
720
832
|
alive: true,
|
|
721
833
|
changed: -1,
|
|
722
834
|
ack: 0
|
|
723
835
|
};
|
|
724
836
|
const ctx = this.context(session, joined.iceServers);
|
|
725
|
-
session.media = joined.media === "relay" ? relayMedia(ctx, rejoining) : sfuMedia(ctx, this.options.autoVideo !== false, this.declined);
|
|
837
|
+
session.media = joined.media === "relay" ? relayMedia(ctx, rejoining) : sfuMedia(ctx, this.options.autoVideo !== false, this.declined, joined.offer);
|
|
726
838
|
this.session = session;
|
|
727
839
|
this.meWire = joined.participant;
|
|
728
840
|
this.apply(session, joined.room);
|
|
@@ -789,10 +901,13 @@ var LiveCall = class {
|
|
|
789
901
|
});
|
|
790
902
|
},
|
|
791
903
|
broken: () => {
|
|
792
|
-
if (live()) this.reconnect();
|
|
904
|
+
if (live()) this.reconnect(true);
|
|
793
905
|
},
|
|
794
906
|
reached: () => {
|
|
795
907
|
this.connectedOnce = true;
|
|
908
|
+
if (!live() || this.rejoinUntil === void 0) return;
|
|
909
|
+
this.rejoinUntil = void 0;
|
|
910
|
+
this.setState(this.wire.size ? "in-call" : "waiting");
|
|
796
911
|
},
|
|
797
912
|
failed: (error) => {
|
|
798
913
|
if (live()) this.background(error);
|
|
@@ -839,7 +954,7 @@ var LiveCall = class {
|
|
|
839
954
|
for (const id of this.departed.keys()) this.received.delete(id);
|
|
840
955
|
this.departed.clear();
|
|
841
956
|
this.refresh();
|
|
842
|
-
this.setState(others.length ? "in-call" : "waiting");
|
|
957
|
+
if (!(session.connects && this.rejoinUntil !== void 0)) this.setState(others.length ? "in-call" : "waiting");
|
|
843
958
|
}
|
|
844
959
|
snapshotOf(id) {
|
|
845
960
|
const wire = this.wire.get(id) ?? this.departed.get(id);
|
|
@@ -892,23 +1007,37 @@ var LiveCall = class {
|
|
|
892
1007
|
this.received.clear();
|
|
893
1008
|
this.refresh();
|
|
894
1009
|
}
|
|
895
|
-
/**
|
|
896
|
-
|
|
897
|
-
|
|
1010
|
+
/**
|
|
1011
|
+
* Joins the room again after a drop, as the same participant, for up to 30 seconds from the drop: until the media
|
|
1012
|
+
* is connected again, however many joins that takes. `media` says the media's connection is what broke, with the
|
|
1013
|
+
* room still answering: when it was never made, the network lets none through, and joining again can't help.
|
|
1014
|
+
*/
|
|
1015
|
+
async reconnect(media = false) {
|
|
1016
|
+
if (this.left || this.rejoining || this.state === "ended" || this.state === "failed") return;
|
|
1017
|
+
const blocked = new CallError("unavailable", "Your network let no connection through to the call. Try another network, or one that allows UDP or TCP port 80.", "blocked");
|
|
1018
|
+
const dropped = new CallError("unavailable", "The call's connection dropped and couldn't be made again within 30 seconds. Check your network, then join again.", "dropped");
|
|
1019
|
+
if (media && !this.connectedOnce) return this.fail(blocked);
|
|
1020
|
+
const until = this.rejoinUntil ??= Date.now() + REJOIN_FOR_MS;
|
|
1021
|
+
if (Date.now() >= until) return this.fail(dropped);
|
|
1022
|
+
this.rejoining = true;
|
|
898
1023
|
this.setState("reconnecting");
|
|
899
1024
|
this.teardown();
|
|
900
|
-
const until = Date.now() + REJOIN_FOR_MS;
|
|
901
1025
|
let wait = 500;
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
1026
|
+
try {
|
|
1027
|
+
for (;;) try {
|
|
1028
|
+
await this.freshen();
|
|
1029
|
+
await this.connect(true);
|
|
1030
|
+
if (!this.session?.connects) this.rejoinUntil = void 0;
|
|
1031
|
+
return;
|
|
1032
|
+
} catch (error) {
|
|
1033
|
+
if (this.left) return;
|
|
1034
|
+
if (!(error instanceof Unreachable)) return this.fail(error);
|
|
1035
|
+
if (Date.now() + wait > until) return this.fail(this.connectedOnce ? dropped : blocked);
|
|
1036
|
+
await new Promise((resolve) => setTimeout(resolve, wait));
|
|
1037
|
+
wait = Math.min(wait * 2, 4e3);
|
|
1038
|
+
}
|
|
1039
|
+
} finally {
|
|
1040
|
+
this.rejoining = false;
|
|
912
1041
|
}
|
|
913
1042
|
}
|
|
914
1043
|
/** Asks for a new token now when this one runs out within five minutes. */
|
|
@@ -1016,11 +1145,12 @@ var LiveCall = class {
|
|
|
1016
1145
|
}
|
|
1017
1146
|
async publish(asked) {
|
|
1018
1147
|
this.ready();
|
|
1148
|
+
if (this.audioOnly && asked.camera) throw new CallError("invalid", audioOnlyProblem);
|
|
1019
1149
|
const audio = asked.mic === true, video = asked.camera === true;
|
|
1020
1150
|
let captured;
|
|
1021
1151
|
if (audio || video) captured = await this.devices().getUserMedia({
|
|
1022
1152
|
audio,
|
|
1023
|
-
video
|
|
1153
|
+
video: video ? CAMERA : false
|
|
1024
1154
|
}).catch((error) => {
|
|
1025
1155
|
throw new CallError(deviceRefused(error) ? "forbidden" : "unavailable", deviceRefused(error) ? "The browser didn't allow the microphone or camera. Allow them in the site's settings, then try again." : `The microphone or camera couldn't be started: ${messageOf(error)}`);
|
|
1026
1156
|
});
|
|
@@ -1073,6 +1203,7 @@ var LiveCall = class {
|
|
|
1073
1203
|
}
|
|
1074
1204
|
async share() {
|
|
1075
1205
|
this.ready();
|
|
1206
|
+
if (this.audioOnly) throw new CallError("invalid", audioOnlyProblem);
|
|
1076
1207
|
const stream = await this.devices().getDisplayMedia({
|
|
1077
1208
|
video: true,
|
|
1078
1209
|
audio: true
|
|
@@ -1139,4 +1270,4 @@ function callErrorOf(error) {
|
|
|
1139
1270
|
return new CallError("unavailable", `The call's connection couldn't be made: ${messageOf(error)}`);
|
|
1140
1271
|
}
|
|
1141
1272
|
//#endregion
|
|
1142
|
-
export { CallError, MAX_FULL_VIDEOS, MAX_MESSAGE_BYTES, MAX_VIDEOS, callToken, identityProblem, join, roomProblem };
|
|
1273
|
+
export { CAMERA, CallError, MAX_FULL_VIDEOS, MAX_MESSAGE_BYTES, MAX_VIDEOS, callToken, identityProblem, join, roomProblem };
|
package/calls/protocol.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as CallToken, c as Layer, d as TrackName, f as WireParticipant, i as CallOp, l as Sdp, m as WireSignal, n as CALL_STATUS, o as CallWire, p as WireRoom, r as CallErrorCode, s as IceServer, t as CALL_OPS, u as TRACK_KINDS } from "../protocol-
|
|
1
|
+
import { a as CallToken, c as Layer, d as TrackName, f as WireParticipant, i as CallOp, l as Sdp, m as WireSignal, n as CALL_STATUS, o as CallWire, p as WireRoom, r as CallErrorCode, s as IceServer, t as CALL_OPS, u as TRACK_KINDS } from "../protocol-_2SNN1Nz.mjs";
|
|
2
2
|
export { CALL_OPS, CALL_STATUS, CallErrorCode, CallOp, CallToken, CallWire, IceServer, Layer, Sdp, TRACK_KINDS, TrackName, WireParticipant, WireRoom, WireSignal };
|
package/calls/react.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { d as TrackName } from "../protocol-
|
|
1
|
+
import { d as TrackName } from "../protocol-_2SNN1Nz.mjs";
|
|
2
2
|
import { Call, CallError, CallState, LocalTrack, Participant, Rtc, TokenSource } from "./index.mjs";
|
|
3
3
|
import { AudioHTMLAttributes, ReactNode, VideoHTMLAttributes } from "react";
|
|
4
4
|
//#region src/calls/react.d.ts
|
package/calls/stand-in.d.mts
CHANGED
package/calls/ui.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { CallError } from "./index.mjs";
|
|
2
|
+
import { CAMERA, CallError } from "./index.mjs";
|
|
3
3
|
import { Audio, CallProvider, Video, useCall, useCallError, useCallState, useLocalTracks, useParticipants } from "./react.mjs";
|
|
4
4
|
import { a as TYPE, i as MATERIAL, n as FOUNDATION, r as LIGHT, t as DARK } from "../tokens-BpV8U5E1.mjs";
|
|
5
5
|
import { createContext, createElement, useCallback, useContext, useEffect, useId, useMemo, useRef, useState } from "react";
|
|
@@ -412,7 +412,7 @@ function usePreview(state, local) {
|
|
|
412
412
|
try {
|
|
413
413
|
return tracksOf(await devices.getUserMedia({
|
|
414
414
|
audio: true,
|
|
415
|
-
video:
|
|
415
|
+
video: CAMERA
|
|
416
416
|
}));
|
|
417
417
|
} catch (both) {
|
|
418
418
|
try {
|
|
@@ -542,7 +542,8 @@ function CallGrid({ className }) {
|
|
|
542
542
|
else next.set(key, rounded);
|
|
543
543
|
return next;
|
|
544
544
|
}), []);
|
|
545
|
-
const
|
|
545
|
+
const measures = typeof ResizeObserver !== "undefined";
|
|
546
|
+
const wanted = shown.filter((tile) => !tile.self && publishes(tile.participant, tile.video) && (!measures || widths.has(tile.key))).slice(0, 9);
|
|
546
547
|
const full = wanted.filter((tile) => layerFor(widths.get(tile.key) ?? 0) === "f").sort((a, b) => (widths.get(b.key) ?? 0) - (widths.get(a.key) ?? 0)).slice(0, 2).map((tile) => tile.key);
|
|
547
548
|
const plan = wanted.map((tile) => {
|
|
548
549
|
const width = widths.get(tile.key);
|
|
@@ -574,9 +575,7 @@ function CallGrid({ className }) {
|
|
|
574
575
|
for (const entry of asked.values()) {
|
|
575
576
|
const participant = call.participants.find((one) => one.id === entry.participant);
|
|
576
577
|
if (!participant || !publishes(participant, entry.name)) continue;
|
|
577
|
-
if (!participant.received[entry.name]
|
|
578
|
-
else if ((layers.current.get(entry.key) ?? "h") !== entry.layer) await call.setLayer(entry.participant, entry.name, entry.layer).catch(noop);
|
|
579
|
-
layers.current.set(entry.key, entry.layer);
|
|
578
|
+
if (await (!participant.received[entry.name] ? call.receive(entry.participant, entry.name, { layer: entry.layer }) : (layers.current.get(entry.key) ?? "h") !== entry.layer ? call.setLayer(entry.participant, entry.name, entry.layer) : Promise.resolve()).then(() => true, () => false)) layers.current.set(entry.key, entry.layer);
|
|
580
579
|
}
|
|
581
580
|
});
|
|
582
581
|
}, [
|
|
@@ -715,7 +714,7 @@ function CallControls({ publish = false, onLeave, people, className }) {
|
|
|
715
714
|
if (!publish || started.current || !call || !live(state)) return;
|
|
716
715
|
started.current = true;
|
|
717
716
|
setStarting(true);
|
|
718
|
-
const both = () => call.publish({
|
|
717
|
+
const both = () => call.audioOnly ? call.publish({ mic: true }) : call.publish({
|
|
719
718
|
mic: true,
|
|
720
719
|
camera: true
|
|
721
720
|
}).catch((error) => call.publish({ mic: true }).catch(() => {
|
|
@@ -725,6 +724,11 @@ function CallControls({ publish = false, onLeave, people, className }) {
|
|
|
725
724
|
const captured = preview ? await preview.ready() : {};
|
|
726
725
|
if (captured.error) throw captured.error;
|
|
727
726
|
if (!captured.mic && !captured.camera) return both();
|
|
727
|
+
if (call.audioOnly) {
|
|
728
|
+
preview?.drop("camera");
|
|
729
|
+
if (captured.mic) await call.publish({ mic: captured.mic });
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
728
732
|
try {
|
|
729
733
|
await call.publish({
|
|
730
734
|
...captured.mic ? { mic: captured.mic } : {},
|
package/here-C2jhTheQ.mjs
CHANGED
|
@@ -12,7 +12,7 @@ function processEnv() {
|
|
|
12
12
|
* This package's version: `0.0.0-dev` here, and in a published package the version it was published at, which the
|
|
13
13
|
* release writes in its place as it stages the package (`stampSdk` in `scripts/release/packages.ts`).
|
|
14
14
|
*/
|
|
15
|
-
const SDK_VERSION = "0.1.0-staging.
|
|
15
|
+
const SDK_VERSION = "0.1.0-staging.4516";
|
|
16
16
|
/**
|
|
17
17
|
* The header that tells Midsummer which client made each of this package's calls: `Midsummer-Client: sdk/<version>
|
|
18
18
|
* <part>`, `part` the product whose client it is (`database`, `storage`, `queues`, ...), so the platform counts which
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midsummer-ai/sdk",
|
|
3
|
-
"version": "0.1.0-staging.
|
|
3
|
+
"version": "0.1.0-staging.4516",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Midsummer's handles for an app's code: its Database, Storage, Queues, Vector DB, AI, Email and Agents, with stand-ins for tests.",
|
|
@@ -34,6 +34,8 @@ interface WireParticipant {
|
|
|
34
34
|
readonly name: TrackName;
|
|
35
35
|
readonly kind: "audio" | "video";
|
|
36
36
|
}>;
|
|
37
|
+
/** Which of their connections this is: 1, and one more each time they drop and come back. What was received from an earlier one is gone. */
|
|
38
|
+
readonly connection?: number;
|
|
37
39
|
}
|
|
38
40
|
/** A room as a watch sees it; `changed` moves on with every change and is the next watch's `after`. */
|
|
39
41
|
interface WireRoom {
|
|
@@ -52,6 +54,7 @@ interface WireSignal {
|
|
|
52
54
|
}
|
|
53
55
|
/** Each op's body and answer. */
|
|
54
56
|
interface CallWire {
|
|
57
|
+
/** With a media server (`sfu`), `offer` sets the connection up: the browser sends its answer to it with `answer` before anything else. */
|
|
55
58
|
join: {
|
|
56
59
|
body: Record<string, never>;
|
|
57
60
|
answer: {
|
|
@@ -59,6 +62,7 @@ interface CallWire {
|
|
|
59
62
|
room: WireRoom;
|
|
60
63
|
iceServers: ReadonlyArray<IceServer>;
|
|
61
64
|
media: "sfu" | "relay";
|
|
65
|
+
offer?: Sdp;
|
|
62
66
|
expiresAt: number;
|
|
63
67
|
};
|
|
64
68
|
};
|
|
@@ -125,6 +129,7 @@ interface CallWire {
|
|
|
125
129
|
};
|
|
126
130
|
answer: Record<string, never>;
|
|
127
131
|
};
|
|
132
|
+
/** Your data channel's id, and the id of each of `from` whose own is open; the room changes when another opens, and asking again receives it. */
|
|
128
133
|
data: {
|
|
129
134
|
body: {
|
|
130
135
|
participant: string;
|