@midsummer-ai/sdk 0.1.0-staging.4718 → 0.1.0-staging.4790

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-eCEy2GzD.mjs";
1
+ import { a as CallToken } from "../protocol-d_NS2wT_.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-eCEy2GzD.mjs";
2
+ import { a as CallToken, c as Layer, d as TrackName, r as CallErrorCode } from "../protocol-d_NS2wT_.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;
@@ -134,6 +134,15 @@ interface JoinOptions {
134
134
  readonly rtc?: Rtc;
135
135
  /** Receive others' videos as they publish them, up to 9 at the half layer: true unless you choose them with `receive`. */
136
136
  readonly autoVideo?: boolean;
137
+ /**
138
+ * What to publish as the call joins, as `publish` takes it: the devices start while the join is on its way, and they
139
+ * go out with its first answer, a trip sooner than a `publish` after it. A device that won't start is said as an
140
+ * `error` event, and the call joins without it.
141
+ */
142
+ readonly publish?: {
143
+ readonly mic?: boolean | MediaStreamTrack;
144
+ readonly camera?: boolean | MediaStreamTrack;
145
+ };
137
146
  }
138
147
  /** A call joined with `join`. */
139
148
  interface Call {
package/calls/index.mjs CHANGED
@@ -414,6 +414,48 @@ function sfuMedia(ctx, auto, declined, offer) {
414
414
  if (!closed && pc.connectionState !== "connected") ctx.broken();
415
415
  }, CONNECT_WITHIN_MS);
416
416
  };
417
+ /** Puts tracks on transceivers of their own and makes the offer that publishes them. */
418
+ const offerTracks = async (tracks) => {
419
+ const made = tracks.map(({ name, track }) => ({
420
+ name,
421
+ track,
422
+ transceiver: pc.addTransceiver(track, {
423
+ direction: "sendonly",
424
+ ...name === "camera" && layersOf(track) ? { sendEncodings: layersOf(track) } : {},
425
+ ...ctx.rtc.MediaStream ? { streams: [new ctx.rtc.MediaStream([track])] } : {}
426
+ })
427
+ }));
428
+ await pc.setLocalDescription(await pc.createOffer());
429
+ const sentIn = (name, track) => name === "camera" && (layersOf(track)?.length ?? 1) < SIMULCAST.length ? { layers: layersOf(track)?.length ?? 1 } : {};
430
+ return {
431
+ made,
432
+ body: {
433
+ offer: sdpOf(pc.localDescription),
434
+ tracks: made.map(({ name, track, transceiver }) => ({
435
+ mid: transceiver.mid ?? "",
436
+ name,
437
+ ...sentIn(name, track)
438
+ }))
439
+ }
440
+ };
441
+ };
442
+ /** Applies the media server's answer to a publish: its tracks are sent from now on. */
443
+ const tookPublished = async (offered, answered) => {
444
+ if (answered.answer) await pc.setRemoteDescription(answered.answer);
445
+ timed();
446
+ for (const { name, transceiver } of offered.made) sent.set(name, transceiver);
447
+ return answered.room;
448
+ };
449
+ /** Takes back a publish that was refused. */
450
+ const unoffer = async (offered) => {
451
+ if (pc.signalingState === "have-local-offer") await pc.setLocalDescription({ type: "rollback" });
452
+ for (const { transceiver } of offered.made) {
453
+ transceiver.direction = "inactive";
454
+ await transceiver.sender.replaceTrack(null);
455
+ }
456
+ };
457
+ /** The first tracks published before the join's offer is answered, which go with that answer. */
458
+ let earlyPublish;
417
459
  const answer = async (offered) => {
418
460
  try {
419
461
  await pc.setRemoteDescription(offered);
@@ -437,16 +479,41 @@ function sfuMedia(ctx, auto, declined, offer) {
437
479
  try {
438
480
  await pc.setRemoteDescription(offered);
439
481
  await pc.setLocalDescription(await pc.createAnswer());
482
+ const answered0 = sdpOf(pc.localDescription);
440
483
  asked = early ?? [];
441
484
  early = void 0;
485
+ const publishing = earlyPublish;
486
+ earlyPublish = void 0;
487
+ const offeredNow = publishing ? await offerTracks(publishing.tracks).catch((error) => {
488
+ publishing.reject(error);
489
+ }) : void 0;
442
490
  const chunk = asked.filter((track) => publishes(track.of, track.name) && !find(track.of, track.name)).slice(0, PULL_MAX);
443
491
  const were = new Map(chunk.map((track) => [track.of, connections.get(track.of)]));
444
- const answered = await ctx.post("answer", {
445
- participant: ctx.me,
446
- answer: sdpOf(pc.localDescription),
447
- ...chunk.length ? { pull: chunk } : {}
448
- });
492
+ let answered;
493
+ try {
494
+ answered = await ctx.post("answer", {
495
+ participant: ctx.me,
496
+ answer: answered0,
497
+ ...chunk.length ? { pull: chunk } : {},
498
+ ...offeredNow ? { publish: offeredNow.body } : {}
499
+ });
500
+ } catch (error) {
501
+ if (offeredNow) {
502
+ await unoffer(offeredNow).catch(() => void 0);
503
+ publishing.reject(error);
504
+ }
505
+ throw error;
506
+ }
449
507
  timed();
508
+ if (offeredNow) try {
509
+ publishing.resolve(await tookPublished(offeredNow, answered.published ?? await ctx.post("publish", {
510
+ participant: ctx.me,
511
+ ...offeredNow.body
512
+ })));
513
+ } catch (error) {
514
+ await unoffer(offeredNow).catch(() => void 0);
515
+ publishing.reject(error);
516
+ }
450
517
  if (chunk.length && answered.offer && answered.tracks) await took(chunk, were, {
451
518
  offer: answered.offer,
452
519
  tracks: answered.tracks
@@ -455,6 +522,10 @@ function sfuMedia(ctx, auto, declined, offer) {
455
522
  early = void 0;
456
523
  timed();
457
524
  for (const track of asked) pending.delete(key(track.of, track.name));
525
+ if (earlyPublish) {
526
+ earlyPublish.reject(new CallError("unavailable", "The call's connection couldn't be made, so nothing was published. Join again."));
527
+ earlyPublish = void 0;
528
+ }
458
529
  }
459
530
  const rest = asked.filter((track) => publishes(track.of, track.name) && !find(track.of, track.name));
460
531
  if (rest.length && !closed) pull(rest).catch(ctx.failed);
@@ -669,41 +740,27 @@ function sfuMedia(ctx, auto, declined, offer) {
669
740
  if (wanted.length) pull(wanted).catch(ctx.failed);
670
741
  }
671
742
  return {
672
- publish: (tracks) => serial(async () => {
673
- const made = tracks.map(({ name, track }) => ({
674
- name,
675
- track,
676
- transceiver: pc.addTransceiver(track, {
677
- direction: "sendonly",
678
- ...name === "camera" && layersOf(track) ? { sendEncodings: layersOf(track) } : {},
679
- ...ctx.rtc.MediaStream ? { streams: [new ctx.rtc.MediaStream([track])] } : {}
680
- })
681
- }));
682
- await pc.setLocalDescription(await pc.createOffer());
683
- try {
684
- const sentIn = (name, track) => name === "camera" && (layersOf(track)?.length ?? 1) < SIMULCAST.length ? { layers: layersOf(track)?.length ?? 1 } : {};
685
- const answered = await ctx.post("publish", {
686
- participant: ctx.me,
687
- offer: sdpOf(pc.localDescription),
688
- tracks: made.map(({ name, track, transceiver }) => ({
689
- mid: transceiver.mid ?? "",
690
- name,
691
- ...sentIn(name, track)
692
- }))
693
- });
694
- if (answered.answer) await pc.setRemoteDescription(answered.answer);
695
- timed();
696
- for (const { name, transceiver } of made) sent.set(name, transceiver);
697
- return answered.room;
698
- } catch (error) {
699
- if (pc.signalingState === "have-local-offer") await pc.setLocalDescription({ type: "rollback" });
700
- for (const { transceiver } of made) {
701
- transceiver.direction = "inactive";
702
- await transceiver.sender.replaceTrack(null);
743
+ publish: (tracks) => {
744
+ if (early !== void 0 && !earlyPublish) return new Promise((resolve, reject) => {
745
+ earlyPublish = {
746
+ tracks,
747
+ resolve,
748
+ reject
749
+ };
750
+ });
751
+ return serial(async () => {
752
+ const made = await offerTracks(tracks);
753
+ try {
754
+ return await tookPublished(made, await ctx.post("publish", {
755
+ participant: ctx.me,
756
+ ...made.body
757
+ }));
758
+ } catch (error) {
759
+ await unoffer(made);
760
+ throw error;
703
761
  }
704
- throw error;
705
- }
706
- }),
762
+ });
763
+ },
707
764
  async replace(name, track) {
708
765
  await sent.get(name)?.sender.replaceTrack(track);
709
766
  },
@@ -1016,6 +1073,8 @@ var LiveCall = class {
1016
1073
  renewal;
1017
1074
  shown = "";
1018
1075
  left = false;
1076
+ /** The devices `publish` in the join's options started, until the join takes them. */
1077
+ capturing;
1019
1078
  /** Whether media ever connected, which tells a dropped call from a network that let nothing through. */
1020
1079
  connectedOnce = false;
1021
1080
  /** While a dropped call joins again: when it gives up. The call is back once its media connects, not once the room answers. */
@@ -1027,6 +1086,10 @@ var LiveCall = class {
1027
1086
  this.rtc = rtc;
1028
1087
  }
1029
1088
  async start() {
1089
+ if (this.options.publish) this.capturing = this.capture(this.options.publish).catch((error) => {
1090
+ this.background(error);
1091
+ return [];
1092
+ });
1030
1093
  try {
1031
1094
  const source = this.options.token;
1032
1095
  this.credential = credentialOf(typeof source === "function" ? await source() : source);
@@ -1080,6 +1143,23 @@ var LiveCall = class {
1080
1143
  expiresAt: joined.expiresAt
1081
1144
  };
1082
1145
  this.schedule();
1146
+ const capturing = this.capturing;
1147
+ this.capturing = void 0;
1148
+ if (capturing) {
1149
+ for (const { name, track, owned } of await capturing) {
1150
+ if (joined.room.audioOnly && TRACK_KINDS[name] === "video") {
1151
+ if (owned) track.stop();
1152
+ this.background(new CallError("invalid", audioOnlyProblem));
1153
+ continue;
1154
+ }
1155
+ this.sending.set(name, {
1156
+ track,
1157
+ owned,
1158
+ muted: false
1159
+ });
1160
+ }
1161
+ this.showLocal();
1162
+ }
1083
1163
  const session = {
1084
1164
  participant: joined.participant.id,
1085
1165
  connects: joined.media === "sfu" && joined.offer !== void 0,
@@ -1406,6 +1486,23 @@ var LiveCall = class {
1406
1486
  async publish(asked) {
1407
1487
  this.ready();
1408
1488
  if (this.audioOnly && asked.camera) throw new CallError("invalid", audioOnlyProblem);
1489
+ const tracks = asked.mic === true || asked.camera === true ? await this.capture(asked) : this.given(asked);
1490
+ await this.put(tracks);
1491
+ }
1492
+ /** The tracks `asked` gives as they are, none started here. */
1493
+ given(asked) {
1494
+ return [...typeof asked.mic === "object" ? [{
1495
+ name: "mic",
1496
+ track: asked.mic,
1497
+ owned: false
1498
+ }] : [], ...typeof asked.camera === "object" ? [{
1499
+ name: "camera",
1500
+ track: asked.camera,
1501
+ owned: false
1502
+ }] : []];
1503
+ }
1504
+ /** The tracks `asked` names: a track given as it is, and `true` started from the browser's devices. */
1505
+ async capture(asked) {
1409
1506
  const audio = asked.mic === true, video = asked.camera === true;
1410
1507
  let captured;
1411
1508
  if (audio || video) captured = await this.devices().getUserMedia({
@@ -1427,7 +1524,7 @@ var LiveCall = class {
1427
1524
  track: camera,
1428
1525
  owned: camera !== asked.camera
1429
1526
  });
1430
- await this.put(tracks);
1527
+ return tracks;
1431
1528
  }
1432
1529
  async unpublish(name) {
1433
1530
  await this.stop([name]);
@@ -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-eCEy2GzD.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-d_NS2wT_.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-eCEy2GzD.mjs";
1
+ import { d as TrackName } from "../protocol-d_NS2wT_.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-eCEy2GzD.mjs";
1
+ import { a as CallToken } from "../protocol-d_NS2wT_.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.4718";
15
+ const SDK_VERSION = "0.1.0-staging.4790";
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.4718",
3
+ "version": "0.1.0-staging.4790",
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.",
@@ -110,6 +110,7 @@ interface CallWire {
110
110
  };
111
111
  };
112
112
  /** 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`. */
113
+ /** With `publish`, the first tracks go with the answer to the join's offer, published once it is applied: `published`. */
113
114
  answer: {
114
115
  body: {
115
116
  participant: string;
@@ -119,6 +120,14 @@ interface CallWire {
119
120
  name: TrackName;
120
121
  layer?: Layer;
121
122
  }>;
123
+ publish?: {
124
+ offer: Sdp;
125
+ tracks: ReadonlyArray<{
126
+ mid: string;
127
+ name: TrackName;
128
+ layers?: number;
129
+ }>;
130
+ };
122
131
  };
123
132
  answer: {
124
133
  offer?: Sdp;
@@ -127,6 +136,7 @@ interface CallWire {
127
136
  name: TrackName;
128
137
  mid: string;
129
138
  }>;
139
+ published?: CallWire["publish"]["answer"];
130
140
  };
131
141
  };
132
142
  /** New relays once the token was renewed, asked for what is left of the token that asks; none where the call gives none. */