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

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/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
  {
@@ -344,6 +351,46 @@ function sfuMedia(ctx, auto, declined, offer) {
344
351
  ctx.failed(error);
345
352
  return false;
346
353
  }) : Promise.resolve(true);
354
+ /**
355
+ * Asks whoever sends a video for a key frame, on this call's own channel, which they receive. A viewer is moved to
356
+ * another layer at that layer's next key frame, and the media server asks the sender for one only every ten seconds
357
+ * or so, so a tile that changed size would stay soft that long. With no channel open yet nothing is asked, and the
358
+ * media server's own ask stands.
359
+ */
360
+ const askKey = (of, name) => {
361
+ if (!closed && own?.readyState === "open" && TRACK_KINDS[name] === "video") own.send(`${KEY_FRAME}${of} ${name}`);
362
+ };
363
+ /** When each video sent last made a key frame because it was asked, and the one waiting its turn. */
364
+ const keyed = /* @__PURE__ */ new Map();
365
+ /**
366
+ * Makes a key frame on a video this call sends, on every layer, because a viewer asked: at most one a second,
367
+ * however many ask, with one more at the second's end for those who asked meanwhile. A browser that can't make one
368
+ * when asked ignores it.
369
+ */
370
+ const makeKey = (name) => {
371
+ const sender = sent.get(name)?.sender;
372
+ if (closed || !sender || TRACK_KINDS[name] !== "video" || typeof sender.getParameters !== "function") return;
373
+ const last = keyed.get(name);
374
+ if (last?.timer !== void 0) return;
375
+ const make = () => {
376
+ keyed.set(name, { at: Date.now() });
377
+ if (closed || sent.get(name)?.sender !== sender) return;
378
+ const parameters = sender.getParameters();
379
+ sender.setParameters(parameters, { encodingOptions: (parameters.encodings ?? []).map(() => ({ keyFrame: true })) }).catch(() => void 0);
380
+ };
381
+ const wait = last ? last.at + KEY_FRAME_EVERY_MS - Date.now() : 0;
382
+ if (wait <= 0) make();
383
+ else keyed.set(name, {
384
+ at: last.at,
385
+ timer: setTimeout(make, wait)
386
+ });
387
+ };
388
+ /** 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. */
389
+ const heard = (of, text) => {
390
+ if (!text.startsWith(KEY_FRAME)) return ctx.message(of, text);
391
+ const [who, name] = text.slice(8).split(" ");
392
+ if (who === ctx.me && name !== void 0 && Object.hasOwn(TRACK_KINDS, name)) makeKey(name);
393
+ };
347
394
  /** The people whose channel isn't received yet. */
348
395
  const unheard = () => others.filter((other) => other.kind === "person" && !channels.has(other.id)).map((other) => other.id);
349
396
  /** 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 +419,7 @@ function sfuMedia(ctx, auto, declined, offer) {
372
419
  negotiated: true,
373
420
  id
374
421
  });
375
- channel.addEventListener("message", (event) => ctx.message(of, String(event.data)));
422
+ channel.addEventListener("message", (event) => heard(of, String(event.data)));
376
423
  channels.set(of, channel);
377
424
  }
378
425
  return from;
@@ -417,6 +464,7 @@ function sfuMedia(ctx, auto, declined, offer) {
417
464
  });
418
465
  }
419
466
  await answer(pulledNow.offer);
467
+ for (const track of pulledNow.tracks) askKey(track.of, track.name);
420
468
  const old = pulledNow.tracks.filter((track) => pulled.has(track.mid) && connections.get(track.of) !== asked.get(track.of));
421
469
  for (const track of old) {
422
470
  if (pulled.get(track.mid)?.track) ctx.ended(track.of, track.name);
@@ -561,6 +609,7 @@ function sfuMedia(ctx, auto, declined, offer) {
561
609
  layer
562
610
  });
563
611
  pulledTrack.layer = layer;
612
+ askKey(of, name);
564
613
  },
565
614
  async stopReceiving(of, name) {
566
615
  declined.add(key(of, name));
@@ -581,6 +630,7 @@ function sfuMedia(ctx, auto, declined, offer) {
581
630
  close() {
582
631
  closed = true;
583
632
  clearTimeout(connectBy);
633
+ for (const { timer } of keyed.values()) clearTimeout(timer);
584
634
  for (const channel of channels.values()) channel.close();
585
635
  own?.close();
586
636
  pc.close();
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.4546";
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.4546",
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.",