@midsummer-ai/sdk 0.1.0-staging.4543 → 0.1.0-staging.4549

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.
@@ -1,4 +1,4 @@
1
- import { a as CallToken } from "../protocol-_2SNN1Nz.mjs";
1
+ import { a as CallToken } from "../protocol-CFLp4co-.mjs";
2
2
  import { CallTokenInput } from "./index.mjs";
3
3
  //#region src/calls/contract.d.ts
4
4
  interface CallsWorld {
package/calls/index.d.mts CHANGED
@@ -1,5 +1,5 @@
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-_2SNN1Nz.mjs";
2
+ import { a as CallToken, c as Layer, d as TrackName, r as CallErrorCode } from "../protocol-CFLp4co-.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;
package/calls/index.mjs CHANGED
@@ -77,6 +77,13 @@ const TRACK_ORDER = [
77
77
  ];
78
78
  /** How long a connection has to reach the media, from its first offer answered, before the network is taken to block it. */
79
79
  const CONNECT_WITHIN_MS = 15e3;
80
+ /**
81
+ * What a viewer sends on its own channel to ask whoever sends a video for a key frame, followed by the sender's id and
82
+ * the track's name. It isn't JSON, so an SDK from before it takes it for no message at all.
83
+ */
84
+ const KEY_FRAME = "msc:key ";
85
+ /** The least time between two key frames a sender makes because it was asked. */
86
+ const KEY_FRAME_EVERY_MS = 1e3;
80
87
  /** A camera's three layers, smallest first. */
81
88
  const SIMULCAST = [
82
89
  {
@@ -340,10 +347,87 @@ function sfuMedia(ctx, auto, declined, offer) {
340
347
  timed();
341
348
  }
342
349
  };
343
- const set = offer ? serial(() => answer(offer)).then(() => true, (error) => {
350
+ /**
351
+ * The tracks asked for before the join's offer is answered, which the room's first reading does: they go with that
352
+ * answer, in one trip, where a pull of their own waited for it and then made another. Undefined once it is sent.
353
+ */
354
+ let early = offer ? [] : void 0;
355
+ let earlyDone;
356
+ const first = async (offered) => {
357
+ let asked = [];
358
+ try {
359
+ await pc.setRemoteDescription(offered);
360
+ await pc.setLocalDescription(await pc.createAnswer());
361
+ asked = early ?? [];
362
+ early = void 0;
363
+ const chunk = asked.filter((track) => publishes(track.of, track.name) && !find(track.of, track.name)).slice(0, PULL_MAX);
364
+ const were = new Map(chunk.map((track) => [track.of, connections.get(track.of)]));
365
+ const answered = await ctx.post("answer", {
366
+ participant: ctx.me,
367
+ answer: sdpOf(pc.localDescription),
368
+ ...chunk.length ? { pull: chunk } : {}
369
+ });
370
+ timed();
371
+ if (chunk.length && answered.offer && answered.tracks) await took(chunk, were, {
372
+ offer: answered.offer,
373
+ tracks: answered.tracks
374
+ });
375
+ } finally {
376
+ early = void 0;
377
+ timed();
378
+ for (const track of asked) pending.delete(key(track.of, track.name));
379
+ }
380
+ const rest = asked.filter((track) => publishes(track.of, track.name) && !find(track.of, track.name));
381
+ if (rest.length && !closed) pull(rest).catch(ctx.failed);
382
+ if (missed) {
383
+ missed = false;
384
+ want(false);
385
+ }
386
+ };
387
+ const set = offer ? (earlyDone = serial(() => first(offer))).then(() => true, (error) => {
344
388
  ctx.failed(error);
345
389
  return false;
346
390
  }) : Promise.resolve(true);
391
+ /**
392
+ * Asks whoever sends a video for a key frame, on this call's own channel, which they receive. A viewer is moved to
393
+ * another layer at that layer's next key frame, and the media server asks the sender for one only every ten seconds
394
+ * or so, so a tile that changed size would stay soft that long. With no channel open yet nothing is asked, and the
395
+ * media server's own ask stands.
396
+ */
397
+ const askKey = (of, name) => {
398
+ if (!closed && own?.readyState === "open" && TRACK_KINDS[name] === "video") own.send(`${KEY_FRAME}${of} ${name}`);
399
+ };
400
+ /** When each video sent last made a key frame because it was asked, and the one waiting its turn. */
401
+ const keyed = /* @__PURE__ */ new Map();
402
+ /**
403
+ * Makes a key frame on a video this call sends, on every layer, because a viewer asked: at most one a second,
404
+ * however many ask, with one more at the second's end for those who asked meanwhile. A browser that can't make one
405
+ * when asked ignores it.
406
+ */
407
+ const makeKey = (name) => {
408
+ const sender = sent.get(name)?.sender;
409
+ if (closed || !sender || TRACK_KINDS[name] !== "video" || typeof sender.getParameters !== "function") return;
410
+ const last = keyed.get(name);
411
+ if (last?.timer !== void 0) return;
412
+ const make = () => {
413
+ keyed.set(name, { at: Date.now() });
414
+ if (closed || sent.get(name)?.sender !== sender) return;
415
+ const parameters = sender.getParameters();
416
+ sender.setParameters(parameters, { encodingOptions: (parameters.encodings ?? []).map(() => ({ keyFrame: true })) }).catch(() => void 0);
417
+ };
418
+ const wait = last ? last.at + KEY_FRAME_EVERY_MS - Date.now() : 0;
419
+ if (wait <= 0) make();
420
+ else keyed.set(name, {
421
+ at: last.at,
422
+ timer: setTimeout(make, wait)
423
+ });
424
+ };
425
+ /** A message on someone's channel: a viewer's ask for a key frame, acted on when it is for a video of this call's, or their words. */
426
+ const heard = (of, text) => {
427
+ if (!text.startsWith(KEY_FRAME)) return ctx.message(of, text);
428
+ const [who, name] = text.slice(8).split(" ");
429
+ if (who === ctx.me && name !== void 0 && Object.hasOwn(TRACK_KINDS, name)) makeKey(name);
430
+ };
347
431
  /** The people whose channel isn't received yet. */
348
432
  const unheard = () => others.filter((other) => other.kind === "person" && !channels.has(other.id)).map((other) => other.id);
349
433
  /** 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. */
@@ -372,7 +456,7 @@ function sfuMedia(ctx, auto, declined, offer) {
372
456
  negotiated: true,
373
457
  id
374
458
  });
375
- channel.addEventListener("message", (event) => ctx.message(of, String(event.data)));
459
+ channel.addEventListener("message", (event) => heard(of, String(event.data)));
376
460
  channels.set(of, channel);
377
461
  }
378
462
  return from;
@@ -396,39 +480,46 @@ function sfuMedia(ctx, auto, declined, offer) {
396
480
  ctx.failed(error);
397
481
  });
398
482
  }
483
+ /** Takes what a pull brought: keeps each track's mid, answers the offer, and drops what came from a connection its sender has since left. */
484
+ async function took(chunk, asked, pulledNow) {
485
+ for (const track of pulledNow.tracks) {
486
+ const layer = chunk.find((each) => each.of === track.of && each.name === track.name)?.layer;
487
+ pulled.set(track.mid, {
488
+ of: track.of,
489
+ name: track.name,
490
+ ...layer ? { layer } : {}
491
+ });
492
+ }
493
+ await answer(pulledNow.offer);
494
+ for (const track of pulledNow.tracks) askKey(track.of, track.name);
495
+ const old = pulledNow.tracks.filter((track) => pulled.has(track.mid) && connections.get(track.of) !== asked.get(track.of));
496
+ for (const track of old) {
497
+ if (pulled.get(track.mid)?.track) ctx.ended(track.of, track.name);
498
+ pulled.delete(track.mid);
499
+ }
500
+ if (old.length) {
501
+ missed = true;
502
+ await ctx.post("unpull", {
503
+ participant: ctx.me,
504
+ mids: old.map((track) => track.mid)
505
+ });
506
+ }
507
+ }
399
508
  const pull = (tracks) => {
400
509
  for (const track of tracks) pending.add(key(track.of, track.name));
510
+ if (early && earlyDone) {
511
+ early.push(...tracks);
512
+ return earlyDone;
513
+ }
401
514
  return serial(async () => {
402
515
  try {
403
516
  const still = tracks.filter((track) => publishes(track.of, track.name) && !find(track.of, track.name));
404
517
  for (let at = 0; at < still.length && !closed; at += PULL_MAX) {
405
518
  const chunk = still.slice(at, at + PULL_MAX);
406
- const asked = new Map(chunk.map((track) => [track.of, connections.get(track.of)]));
407
- const pulledNow = await ctx.post("pull", {
519
+ await took(chunk, new Map(chunk.map((track) => [track.of, connections.get(track.of)])), await ctx.post("pull", {
408
520
  participant: ctx.me,
409
521
  tracks: chunk
410
- });
411
- for (const track of pulledNow.tracks) {
412
- const layer = chunk.find((each) => each.of === track.of && each.name === track.name)?.layer;
413
- pulled.set(track.mid, {
414
- of: track.of,
415
- name: track.name,
416
- ...layer ? { layer } : {}
417
- });
418
- }
419
- await answer(pulledNow.offer);
420
- const old = pulledNow.tracks.filter((track) => pulled.has(track.mid) && connections.get(track.of) !== asked.get(track.of));
421
- for (const track of old) {
422
- if (pulled.get(track.mid)?.track) ctx.ended(track.of, track.name);
423
- pulled.delete(track.mid);
424
- }
425
- if (old.length) {
426
- missed = true;
427
- await ctx.post("unpull", {
428
- participant: ctx.me,
429
- mids: old.map((track) => track.mid)
430
- });
431
- }
522
+ }));
432
523
  }
433
524
  } finally {
434
525
  for (const track of tracks) pending.delete(key(track.of, track.name));
@@ -561,6 +652,7 @@ function sfuMedia(ctx, auto, declined, offer) {
561
652
  layer
562
653
  });
563
654
  pulledTrack.layer = layer;
655
+ askKey(of, name);
564
656
  },
565
657
  async stopReceiving(of, name) {
566
658
  declined.add(key(of, name));
@@ -581,6 +673,7 @@ function sfuMedia(ctx, auto, declined, offer) {
581
673
  close() {
582
674
  closed = true;
583
675
  clearTimeout(connectBy);
676
+ for (const { timer } of keyed.values()) clearTimeout(timer);
584
677
  for (const channel of channels.values()) channel.close();
585
678
  own?.close();
586
679
  pc.close();
@@ -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-_2SNN1Nz.mjs";
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-CFLp4co-.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-_2SNN1Nz.mjs";
1
+ import { d as TrackName } from "../protocol-CFLp4co-.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
@@ -1,4 +1,4 @@
1
- import { a as CallToken } from "../protocol-_2SNN1Nz.mjs";
1
+ import { a as CallToken } from "../protocol-CFLp4co-.mjs";
2
2
  import { CallTokenInput } from "./index.mjs";
3
3
  //#region src/calls/stand-in.d.ts
4
4
  interface CallsStandIn {
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.4543";
15
+ const SDK_VERSION = "0.1.0-staging.4549";
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.4543",
3
+ "version": "0.1.0-staging.4549",
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.",
@@ -107,12 +107,25 @@ interface CallWire {
107
107
  }>;
108
108
  };
109
109
  };
110
+ /** With `pull`, the tracks are received as `pull` does once the answer is applied: the next offer and their mids, in one trip. A platform from before it returns neither, and the tracks are asked for with `pull`. */
110
111
  answer: {
111
112
  body: {
112
113
  participant: string;
113
114
  answer: Sdp;
115
+ pull?: ReadonlyArray<{
116
+ of: string;
117
+ name: TrackName;
118
+ layer?: Layer;
119
+ }>;
120
+ };
121
+ answer: {
122
+ offer?: Sdp;
123
+ tracks?: ReadonlyArray<{
124
+ of: string;
125
+ name: TrackName;
126
+ mid: string;
127
+ }>;
114
128
  };
115
- answer: Record<string, never>;
116
129
  };
117
130
  layer: {
118
131
  body: {