@midsummer-ai/sdk 0.1.0-staging.4555 → 0.1.0-staging.4558

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.d.mts CHANGED
@@ -6,6 +6,34 @@ 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
+ /** What `layerWatch` keeps of one received video between its checks. */
10
+ interface LayerWatch {
11
+ /** The frames decoded when last counted, and when that count last moved. */
12
+ readonly frames: number;
13
+ readonly movedAt: number;
14
+ /** Set while the video is on the small layer because the middle one stalled: when to try the middle one again, and how long the wait was. */
15
+ readonly fell?: {
16
+ readonly climbAt: number;
17
+ readonly waited: number;
18
+ };
19
+ /** Set while the middle layer is being tried again: when it was asked for. */
20
+ readonly climbing?: {
21
+ readonly since: number;
22
+ readonly waited: number;
23
+ };
24
+ }
25
+ /**
26
+ * Whether a received video's layer has stopped, decided once a second from how many frames it has decoded. The small
27
+ * and middle layers are sent exactly as asked, so a viewer on the middle one would freeze if its sender stopped
28
+ * sending it, as a browser on a weak connection does: stalled for `STALLED_MS` it is asked for at the small layer
29
+ * (`ask: "q"`), and once that moves, the middle one is tried again after a wait that doubles each time it stalls
30
+ * (`ask: "h"`). The small layer is always sent, and the full one falls by itself, so neither is watched. `layer` is
31
+ * the layer the page asked for.
32
+ */
33
+ declare function layerWatch(layer: Layer, frames: number, now: number, before?: LayerWatch): {
34
+ readonly next: LayerWatch;
35
+ readonly ask?: "q" | "h";
36
+ };
9
37
  /**
10
38
  * 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
39
  * of which it sends two layers at most, the largest a quarter of that size.
@@ -157,4 +185,4 @@ interface Call {
157
185
  */
158
186
  declare function join(room: string, options: JoinOptions): Promise<Call>;
159
187
  //#endregion
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 };
188
+ export { CAMERA, Call, CallError, type CallErrorCode, CallEvents, CallFailure, CallState, type CallToken, CallTokenInput, JoinOptions, type Json, type Layer, LayerWatch, LocalTrack, MAX_FULL_VIDEOS, MAX_MESSAGE_BYTES, MAX_VIDEOS, Participant, Rtc, TokenSource, type TrackName, callToken, identityProblem, join, layerWatch, roomProblem };
package/calls/index.mjs CHANGED
@@ -84,6 +84,76 @@ const CONNECT_WITHIN_MS = 15e3;
84
84
  const KEY_FRAME = "msc:key ";
85
85
  /** The least time between two key frames a sender makes because it was asked. */
86
86
  const KEY_FRAME_EVERY_MS = 1e3;
87
+ /** How long a received video may show no new frame before its layer is taken to have stopped. */
88
+ const STALLED_MS = 2500;
89
+ /** How long a video that fell to the small layer stays there before the middle one is tried again, at first; doubled each time it stalls again, up to two minutes. */
90
+ const CLIMB_AFTER_MS = 15e3;
91
+ const CLIMB_AFTER_MAX_MS = 12e4;
92
+ /**
93
+ * Whether a received video's layer has stopped, decided once a second from how many frames it has decoded. The small
94
+ * and middle layers are sent exactly as asked, so a viewer on the middle one would freeze if its sender stopped
95
+ * sending it, as a browser on a weak connection does: stalled for `STALLED_MS` it is asked for at the small layer
96
+ * (`ask: "q"`), and once that moves, the middle one is tried again after a wait that doubles each time it stalls
97
+ * (`ask: "h"`). The small layer is always sent, and the full one falls by itself, so neither is watched. `layer` is
98
+ * the layer the page asked for.
99
+ */
100
+ function layerWatch(layer, frames, now, before) {
101
+ const moved = before === void 0 || frames !== before.frames;
102
+ const state = {
103
+ ...before,
104
+ frames,
105
+ movedAt: moved ? now : before.movedAt
106
+ };
107
+ if (layer !== "h") return { next: {
108
+ frames,
109
+ movedAt: state.movedAt
110
+ } };
111
+ const stalled = !moved && now - state.movedAt >= STALLED_MS;
112
+ if (state.climbing) {
113
+ if (stalled) return {
114
+ next: {
115
+ frames,
116
+ movedAt: now,
117
+ fell: {
118
+ climbAt: now + Math.min(state.climbing.waited * 2, CLIMB_AFTER_MAX_MS),
119
+ waited: Math.min(state.climbing.waited * 2, CLIMB_AFTER_MAX_MS)
120
+ }
121
+ },
122
+ ask: "q"
123
+ };
124
+ if (moved && now - state.climbing.since >= STALLED_MS * 2) return { next: {
125
+ frames,
126
+ movedAt: now
127
+ } };
128
+ return { next: state };
129
+ }
130
+ if (state.fell) {
131
+ if (moved && now >= state.fell.climbAt) return {
132
+ next: {
133
+ frames,
134
+ movedAt: now,
135
+ climbing: {
136
+ since: now,
137
+ waited: state.fell.waited
138
+ }
139
+ },
140
+ ask: "h"
141
+ };
142
+ return { next: state };
143
+ }
144
+ if (stalled) return {
145
+ next: {
146
+ frames,
147
+ movedAt: now,
148
+ fell: {
149
+ climbAt: now + CLIMB_AFTER_MS,
150
+ waited: CLIMB_AFTER_MS
151
+ }
152
+ },
153
+ ask: "q"
154
+ };
155
+ return { next: state };
156
+ }
87
157
  /** A camera's three layers, smallest first. */
88
158
  const SIMULCAST = [
89
159
  {
@@ -422,6 +492,39 @@ function sfuMedia(ctx, auto, declined, offer) {
422
492
  timer: setTimeout(make, wait)
423
493
  });
424
494
  };
495
+ /**
496
+ * Once a second, each camera received on the middle layer is looked at (`layerWatch`): one whose frames stopped is
497
+ * asked for at the small layer, and at the middle one again later. The page's own ask stays what it was: `layer` on
498
+ * the track is still the middle one, so the tile's next ask changes nothing it didn't mean to.
499
+ */
500
+ let watching = false;
501
+ const watchLayers = async () => {
502
+ if (closed || watching || typeof pc.getStats !== "function") return;
503
+ watching = true;
504
+ try {
505
+ const decoded = /* @__PURE__ */ new Map();
506
+ (await pc.getStats()).forEach((stat) => {
507
+ if (stat.type === "inbound-rtp" && stat.kind === "video" && stat.mid !== void 0) decoded.set(stat.mid, stat.framesDecoded ?? 0);
508
+ });
509
+ for (const [mid, pull] of pulled) {
510
+ const frames = decoded.get(mid);
511
+ if (closed || pull.name !== "camera" || !pull.track || frames === void 0) continue;
512
+ const { next, ask } = layerWatch(pull.layer ?? "h", frames, Date.now(), pull.watch);
513
+ pull.watch = next;
514
+ if (!ask) continue;
515
+ ctx.post("layer", {
516
+ participant: ctx.me,
517
+ mid,
518
+ layer: ask
519
+ }).then(() => askKey(pull.of, pull.name), () => void 0);
520
+ }
521
+ } catch {} finally {
522
+ watching = false;
523
+ }
524
+ };
525
+ const layerTimer = setInterval(() => {
526
+ watchLayers();
527
+ }, 1e3);
425
528
  /** 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
529
  const heard = (of, text) => {
427
530
  if (!text.startsWith(KEY_FRAME)) return ctx.message(of, text);
@@ -655,6 +758,7 @@ function sfuMedia(ctx, auto, declined, offer) {
655
758
  layer
656
759
  });
657
760
  pulledTrack.layer = layer;
761
+ delete pulledTrack.watch;
658
762
  askKey(of, name);
659
763
  },
660
764
  async stopReceiving(of, name) {
@@ -677,6 +781,7 @@ function sfuMedia(ctx, auto, declined, offer) {
677
781
  closed = true;
678
782
  clearTimeout(connectBy);
679
783
  for (const { timer } of keyed.values()) clearTimeout(timer);
784
+ clearInterval(layerTimer);
680
785
  for (const channel of channels.values()) channel.close();
681
786
  own?.close();
682
787
  pc.close();
@@ -1370,4 +1475,4 @@ function callErrorOf(error) {
1370
1475
  return new CallError("unavailable", `The call's connection couldn't be made: ${messageOf(error)}`);
1371
1476
  }
1372
1477
  //#endregion
1373
- export { CAMERA, CallError, MAX_FULL_VIDEOS, MAX_MESSAGE_BYTES, MAX_VIDEOS, callToken, identityProblem, join, roomProblem };
1478
+ export { CAMERA, CallError, MAX_FULL_VIDEOS, MAX_MESSAGE_BYTES, MAX_VIDEOS, callToken, identityProblem, join, layerWatch, roomProblem };
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.4555";
15
+ const SDK_VERSION = "0.1.0-staging.4558";
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.4555",
3
+ "version": "0.1.0-staging.4558",
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.",