@nivaro/react 0.2.56 → 0.3.0

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.
Files changed (59) hide show
  1. package/dist/components/slots/TasksSlot.js +31 -29
  2. package/dist/full.css +1 -1
  3. package/dist/hooks/useSessionRecorder.js +1 -1
  4. package/dist/index.d.ts +73 -1
  5. package/dist/index.js +515 -505
  6. package/dist/shared/components/ItemEditForm.js +666 -658
  7. package/dist/shared/components/MyWorkView.js +47 -37
  8. package/dist/shared/components/ProfileView.js +163 -144
  9. package/dist/shared/components/ai/AiPlanCard.js +427 -0
  10. package/dist/shared/components/ai/AiPlanChanges.js +117 -0
  11. package/dist/shared/components/ai/TaskDo.js +1022 -0
  12. package/dist/shared/components/chat/ChatAnalyticsView.js +19 -17
  13. package/dist/shared/components/chat/ChatPanel.js +1356 -1317
  14. package/dist/shared/components/chat/HuddleAnalyticsTab.js +198 -0
  15. package/dist/shared/components/chat/RoomHeaderMenu.js +78 -65
  16. package/dist/shared/components/chat/chat-core.js +273 -264
  17. package/dist/shared/components/chat/huddle/HuddleAudioUi.js +397 -0
  18. package/dist/shared/components/chat/huddle/HuddleCallBar.js +296 -0
  19. package/dist/shared/components/chat/huddle/HuddleCallLayer.js +180 -0
  20. package/dist/shared/components/chat/huddle/HuddleCamera.js +124 -0
  21. package/dist/shared/components/chat/huddle/HuddleCollab.js +320 -0
  22. package/dist/shared/components/chat/huddle/HuddleControls.js +82 -121
  23. package/dist/shared/components/chat/huddle/HuddleDraw.js +231 -0
  24. package/dist/shared/components/chat/huddle/HuddleLiveBadge.js +52 -0
  25. package/dist/shared/components/chat/huddle/HuddlePeoplePicker.js +143 -0
  26. package/dist/shared/components/chat/huddle/HuddleScheduleCard.js +190 -0
  27. package/dist/shared/components/chat/huddle/HuddleScheduleForm.js +188 -0
  28. package/dist/shared/components/chat/huddle/HuddleStage.js +184 -89
  29. package/dist/shared/components/chat/huddle/HuddleStageTools.js +197 -0
  30. package/dist/shared/components/chat/huddle/huddle-audio-logic.js +156 -0
  31. package/dist/shared/components/chat/huddle/huddle-audio.js +69 -0
  32. package/dist/shared/components/chat/huddle/huddle-calls.js +129 -0
  33. package/dist/shared/components/chat/huddle/huddle-camera.js +143 -0
  34. package/dist/shared/components/chat/huddle/huddle-collab-store.js +391 -0
  35. package/dist/shared/components/chat/huddle/huddle-device-prefs.js +55 -0
  36. package/dist/shared/components/chat/huddle/huddle-devices.js +99 -0
  37. package/dist/shared/components/chat/huddle/huddle-draw.js +226 -0
  38. package/dist/shared/components/chat/huddle/huddle-follow-bus.js +96 -0
  39. package/dist/shared/components/chat/huddle/huddle-hold.js +62 -0
  40. package/dist/shared/components/chat/huddle/huddle-link-quality.js +159 -0
  41. package/dist/shared/components/chat/huddle/huddle-live.js +82 -0
  42. package/dist/shared/components/chat/huddle/huddle-pip.js +343 -0
  43. package/dist/shared/components/chat/huddle/huddle-quality.js +22 -0
  44. package/dist/shared/components/chat/huddle/huddle-reactions.js +71 -0
  45. package/dist/shared/components/chat/huddle/huddle-ring-refused.js +34 -0
  46. package/dist/shared/components/chat/huddle/huddle-schedule.js +102 -0
  47. package/dist/shared/components/chat/huddle/huddle-sfu.js +176 -0
  48. package/dist/shared/components/chat/huddle/huddle-shortcuts.js +91 -0
  49. package/dist/shared/components/chat/huddle/huddle-speaking.js +100 -0
  50. package/dist/shared/components/chat/huddle/huddle-store.js +431 -157
  51. package/dist/shared/components/chat/huddle/useHuddle.js +46 -38
  52. package/dist/shared/components/chat/huddle/useHuddleAudio.js +35 -0
  53. package/dist/shared/components/item-edit/GroupSection.js +1 -0
  54. package/dist/shared/components/item-edit/RecordChatActions.js +68 -59
  55. package/dist/shared/components/item-edit/UserChip.js +145 -142
  56. package/dist/shared/components/panels/TaskPanel.js +51 -49
  57. package/dist/shared/components/support/TicketDetailSheet.js +57 -42
  58. package/dist/shared/lib/notification-sound.js +25 -16
  59. package/package.json +2 -1
@@ -1,7 +1,11 @@
1
- var g = Object.defineProperty;
2
- var v = (r, t, s) => t in r ? g(r, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : r[t] = s;
3
- var c = (r, t, s) => v(r, typeof t != "symbol" ? t + "" : t, s);
4
- const u = {
1
+ var S = Object.defineProperty;
2
+ var w = (h, t, e) => t in h ? S(h, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : h[t] = e;
3
+ var c = (h, t, e) => w(h, typeof t != "symbol" ? t + "" : t, e);
4
+ import { micConstraints as y, loadDevicePrefs as T } from "./huddle-device-prefs.js";
5
+ import { huddleLive as M } from "./huddle-live.js";
6
+ import { aggregateQuality as b } from "./huddle-quality.js";
7
+ import { SfuSession as L, MESH_OVERLAP_MS as A } from "./huddle-sfu.js";
8
+ const p = {
5
9
  room: null,
6
10
  status: "idle",
7
11
  error: null,
@@ -10,33 +14,34 @@ const u = {
10
14
  sharing: !1,
11
15
  localScreen: null,
12
16
  startedAt: null,
13
- peers: []
14
- }, S = 15e3;
15
- function k(r, t) {
16
- return r < t;
17
+ peers: [],
18
+ mode: "mesh",
19
+ micLabel: null,
20
+ handAt: null
21
+ }, V = 15e3;
22
+ function O(h, t) {
23
+ return h < t;
17
24
  }
18
- function p(r, t, s) {
19
- const e = [];
25
+ function g(h, t, e) {
26
+ const s = [];
20
27
  for (const i of t) {
21
- if (i.socket === s) continue;
22
- const n = r.find((o) => o.socket === i.socket);
23
- e.push(
24
- n ? { ...n, name: i.name, muted: i.muted, screen: i.screen, user: i.user, since: i.since } : { ...i, audio: null, video: null, connection: "new" }
25
- );
28
+ if (i.socket === e) continue;
29
+ const o = h.find((r) => r.socket === i.socket);
30
+ s.push(o ? { ...o, ...i } : { ...i, audio: null, video: null, connection: "new" });
26
31
  }
27
- return e;
32
+ return s;
28
33
  }
29
- function M(r) {
30
- const t = r.map((s) => s.name || "Someone");
34
+ function R(h) {
35
+ const t = h.map((e) => e.name || "Someone");
31
36
  return t.length === 0 ? "" : t.length === 1 ? t[0] : t.length === 2 ? `${t[0]} and ${t[1]}` : `${t[0]}, ${t[1]} and ${t.length - 2} other${t.length - 2 === 1 ? "" : "s"}`;
32
37
  }
33
- function T(r) {
34
- const t = Math.max(0, Math.floor(r / 1e3)), s = Math.floor(t / 60), e = Math.floor(s / 60), i = String(s % 60).padStart(2, "0"), n = String(t % 60).padStart(2, "0");
35
- return e ? `${e}:${i}:${n}` : `${s}:${n}`;
38
+ function $(h) {
39
+ const t = Math.max(0, Math.floor(h / 1e3)), e = Math.floor(t / 60), s = Math.floor(e / 60), i = String(e % 60).padStart(2, "0"), o = String(t % 60).padStart(2, "0");
40
+ return s ? `${s}:${i}:${o}` : `${e}:${o}`;
36
41
  }
37
- class w {
42
+ class C {
38
43
  constructor() {
39
- c(this, "state", u);
44
+ c(this, "state", p);
40
45
  c(this, "listeners", /* @__PURE__ */ new Set());
41
46
  /** Who is in each room's huddle (rooms I am NOT in too) — from the server's broadcasts. */
42
47
  c(this, "rosters", /* @__PURE__ */ new Map());
@@ -51,6 +56,23 @@ class w {
51
56
  c(this, "ping", null);
52
57
  /** One hidden <audio> per peer, owned here so sound survives any room view unmounting. */
53
58
  c(this, "audio", /* @__PURE__ */ new Map());
59
+ /** #2849 — the latest stats per peer link (from the quality sampler) and the ICE failures seen. */
60
+ c(this, "linkStats", /* @__PURE__ */ new Map());
61
+ c(this, "iceFailed", 0);
62
+ /** #2859: the SFU link while the huddle is large; mesh links close once it carries audio. */
63
+ c(this, "sfu", null);
64
+ c(this, "fetchSfu", null);
65
+ c(this, "meshCloseTimer", null);
66
+ /** Peers whose audio already comes through the SFU (the mesh may close once all do). */
67
+ c(this, "sfuAudio", /* @__PURE__ */ new Set());
68
+ /** Every video stream each peer sends (screen and camera), live or muted, by stream id. */
69
+ c(this, "remoteVideo", /* @__PURE__ */ new Map());
70
+ /** #2858: on the SFU each video arrives labelled — the camera and the screen, per peer. */
71
+ c(this, "sfuVideo", /* @__PURE__ */ new Map());
72
+ c(this, "exts", /* @__PURE__ */ new Set());
73
+ c(this, "extOffs", /* @__PURE__ */ new Map());
74
+ /** Called just before Leave goes out (#2854: a pending note edit is sent first). */
75
+ c(this, "beforeLeave", /* @__PURE__ */ new Set());
54
76
  // ── React plumbing ────────────────────────────────────────────────────────
55
77
  c(this, "subscribe", (t) => (this.listeners.add(t), () => {
56
78
  this.listeners.delete(t);
@@ -59,130 +81,292 @@ class w {
59
81
  c(this, "subscribeRosters", (t) => (this.rosterListeners.add(t), () => {
60
82
  this.rosterListeners.delete(t);
61
83
  }));
84
+ // ── Audio quality and control (#2845–#2848) ───────────────────────────────
85
+ c(this, "sinkId", "");
86
+ c(this, "quality", null);
87
+ }
88
+ onBeforeLeave(t) {
89
+ return this.beforeLeave.add(t), () => {
90
+ this.beforeLeave.delete(t);
91
+ };
92
+ }
93
+ /** The host's socket adapter, for modules that ride the huddle (huddle-collab-store.ts). */
94
+ transportNow() {
95
+ return this.transport;
62
96
  }
63
97
  roster(t) {
64
98
  return this.rosters.get(t);
65
99
  }
66
100
  set(t) {
67
101
  this.state = { ...this.state, ...t };
68
- for (const s of this.listeners) s();
102
+ for (const e of this.listeners) e();
69
103
  }
70
- setRoster(t, s) {
71
- this.rosters.set(t, s);
72
- for (const e of this.rosterListeners) e();
104
+ setRoster(t, e) {
105
+ this.rosters.set(t, e);
106
+ for (const s of this.rosterListeners) s();
73
107
  }
74
108
  /** A REST read of a room's roster (the first paint, before any broadcast). */
75
- seedRoster(t, s) {
76
- this.rosters.has(t) || this.setRoster(t, s);
109
+ seedRoster(t, e) {
110
+ this.rosters.has(t) || this.setRoster(t, e);
77
111
  }
78
112
  // ── Transport ─────────────────────────────────────────────────────────────
79
113
  /** Bind the host's socket adapter once; later calls with the same adapter are no-ops. */
80
- attach(t, s) {
81
- if (this.fetchIce = s, this.transport !== t) {
82
- for (const e of this.offs) e();
83
- this.transport = t, this.offs = [
84
- t.on("huddle:peers", (e) => this.onPeers(e)),
114
+ attach(t, e, s) {
115
+ if (this.fetchIce = e, this.fetchSfu = s ?? null, this.transport !== t) {
116
+ for (const i of this.offs) i();
117
+ this.transport = t, M.attach(t), this.offs = [
118
+ t.on("huddle:peers", (i) => this.onPeers(i)),
85
119
  t.on(
86
120
  "huddle:joined",
87
- (e) => this.onJoined(e)
121
+ (i) => this.onJoined(i)
88
122
  ),
89
- t.on("huddle:signal", (e) => void this.onSignal(e)),
123
+ t.on("huddle:signal", (i) => void this.onSignal(i)),
90
124
  t.on(
91
125
  "huddle:denied",
92
- (e) => this.onDenied(e)
126
+ (i) => this.onDenied(i)
93
127
  ),
128
+ // #2859: the huddle outgrew the mesh — everyone moves to the SFU.
129
+ t.on("huddle:mode", (i) => this.onMode(i)),
94
130
  // A reconnect is a new server socket: join again and rebuild every link.
95
131
  t.on("auth:ok", () => {
96
- var e;
97
- this.state.room && this.state.status === "live" && ((e = this.transport) == null || e.emit("huddle:join", { room: this.state.room }));
132
+ var i;
133
+ this.state.room && this.state.status === "live" && ((i = this.transport) == null || i.emit("huddle:join", { room: this.state.room }));
98
134
  })
99
135
  ];
136
+ for (const i of this.exts) this.attachExt(i);
100
137
  }
101
138
  }
139
+ // ── Extensions ────────────────────────────────────────────────────────────
140
+ /** Plug a feature module in (idempotent per object); returns the unplug. */
141
+ use(t) {
142
+ var e;
143
+ if (!this.exts.has(t) && (this.exts.add(t), this.transport && this.attachExt(t), this.state.status === "live"))
144
+ for (const [s, i] of this.links) (e = t.link) == null || e.call(t, s, i.pc);
145
+ return () => {
146
+ for (const s of this.extOffs.get(t) ?? []) s();
147
+ this.extOffs.delete(t), this.exts.delete(t);
148
+ };
149
+ }
150
+ attachExt(t) {
151
+ for (const e of this.extOffs.get(t) ?? []) e();
152
+ this.extOffs.set(t, this.transport && t.attach ? t.attach(this.transport) : []);
153
+ }
154
+ /** Emit a huddle event for the room I am live in; false when I am in none. */
155
+ send(t, e = {}) {
156
+ return !this.state.room || this.state.status !== "live" || !this.transport ? !1 : (this.transport.emit(t, { ...e, room: this.state.room }), !0);
157
+ }
158
+ /** Every open peer connection, by the peer's socket id. */
159
+ forEachLink(t) {
160
+ for (const [e, s] of this.links) t(e, s.pc);
161
+ }
162
+ /**
163
+ * Close a mesh link and forget it. Quietly: its "closed" state must not read as a failed
164
+ * peer (#2859). `keepAudio` = the SFU already owns this peer's audio element.
165
+ */
166
+ closeLink(t, e, s = !1) {
167
+ var i;
168
+ e.pc.onconnectionstatechange = null, e.pc.ontrack = null, e.pc.onicecandidate = null, e.pc.onnegotiationneeded = null, e.pc.close(), this.links.delete(t), s || this.dropAudio(t), this.remoteVideo.delete(t);
169
+ for (const o of this.exts) (i = o.unlink) == null || i.call(o, t);
170
+ }
102
171
  async iceServers(t) {
103
172
  if (!this.fetchIce) return [];
104
- const s = this.ice ? Math.min(this.ice.cfg.ttl_s, 3600) * 1e3 * 0.8 : 0;
105
- return (!this.ice || this.ice.room !== t || Date.now() - this.ice.at > s) && (this.ice = { room: t, at: Date.now(), cfg: await this.fetchIce(t) }), this.ice.cfg.iceServers;
173
+ const e = this.ice ? Math.min(this.ice.cfg.ttl_s, 3600) * 1e3 * 0.8 : 0;
174
+ return (!this.ice || this.ice.room !== t || Date.now() - this.ice.at > e) && (this.ice = { room: t, at: Date.now(), cfg: await this.fetchIce(t) }), this.ice.cfg.iceServers;
106
175
  }
107
176
  // ── Join / leave ──────────────────────────────────────────────────────────
108
177
  async join(t) {
178
+ var s;
109
179
  if (!this.transport) return;
110
- const s = this.state.status === "joining" || this.state.status === "live";
111
- if (!(this.state.room === t && s)) {
112
- this.state.room && s && this.leave(), this.set({ room: t, status: "joining", error: null, startedAt: null });
180
+ const e = this.state.status === "joining" || this.state.status === "live";
181
+ if (!(this.state.room === t && e)) {
182
+ this.state.room && e && this.leave(), this.linkStats.clear(), this.iceFailed = 0, this.set({ room: t, status: "joining", error: null, startedAt: null });
113
183
  try {
114
184
  this.mic = await navigator.mediaDevices.getUserMedia({
115
- audio: { echoCancellation: !0, noiseSuppression: !0, autoGainControl: !0 }
185
+ audio: y(T())
116
186
  });
117
- } catch (e) {
118
- const i = e == null ? void 0 : e.name;
187
+ } catch (i) {
188
+ const o = i == null ? void 0 : i.name;
119
189
  this.set({
120
190
  status: "error",
121
- error: i === "NotAllowedError" ? "Microphone access was refused. Allow it in the browser and try again." : i === "NotFoundError" ? "No microphone was found." : "The microphone could not be started."
191
+ error: o === "NotAllowedError" ? "Microphone access was refused. Allow it in the browser and try again." : o === "NotFoundError" ? "No microphone was found." : "The microphone could not be started."
122
192
  });
123
193
  return;
124
194
  }
125
- for (const e of this.mic.getAudioTracks()) e.enabled = !this.state.muted;
126
- await this.iceServers(t).catch(() => null), this.transport.emit("huddle:join", { room: t });
195
+ for (const i of this.mic.getAudioTracks()) i.enabled = !this.state.muted;
196
+ this.set({ micLabel: ((s = this.mic.getAudioTracks()[0]) == null ? void 0 : s.label) || null }), await this.iceServers(t).catch(() => null), this.transport.emit("huddle:join", { room: t });
127
197
  }
128
198
  }
129
199
  leave() {
130
- var s;
200
+ var e, s;
131
201
  const t = this.state.room;
132
- t && ((s = this.transport) == null || s.emit("huddle:leave", { room: t })), this.teardown();
202
+ if (t) {
203
+ for (const o of this.beforeLeave) o(t);
204
+ const i = b([...this.linkStats.values()], this.iceFailed);
205
+ i && this.state.status === "live" && ((e = this.transport) == null || e.emit("huddle:quality", { room: t, ...i })), (s = this.transport) == null || s.emit("huddle:leave", { room: t });
206
+ }
207
+ this.teardown();
133
208
  }
134
209
  teardown() {
135
- var t, s;
136
- this.ping && clearInterval(this.ping), this.ping = null;
137
- for (const [e, i] of this.links)
138
- i.pc.close(), this.dropAudio(e);
139
- this.links.clear();
140
- for (const e of ((t = this.mic) == null ? void 0 : t.getTracks()) ?? []) e.stop();
210
+ var t, e, s, i;
211
+ this.ping && clearInterval(this.ping), this.ping = null, this.meshCloseTimer && clearTimeout(this.meshCloseTimer), this.meshCloseTimer = null, (t = this.sfu) == null || t.disconnect(), this.sfu = null, this.sfuAudio.clear(), this.sfuVideo.clear();
212
+ for (const [o, r] of this.links) this.closeLink(o, r);
213
+ for (const o of [...this.audio.keys()]) this.dropAudio(o);
214
+ this.remoteVideo.clear();
215
+ for (const o of ((e = this.mic) == null ? void 0 : e.getTracks()) ?? []) o.stop();
141
216
  this.mic = null;
142
- for (const e of ((s = this.state.localScreen) == null ? void 0 : s.getTracks()) ?? []) e.stop();
143
- this.set({ ...u, muted: this.state.muted });
217
+ for (const o of ((s = this.state.localScreen) == null ? void 0 : s.getTracks()) ?? []) o.stop();
218
+ for (const o of this.exts) (i = o.teardown) == null || i.call(o);
219
+ this.set({ ...p, muted: this.state.muted });
144
220
  }
145
221
  onJoined(t) {
146
- var i;
222
+ var r, l;
147
223
  if (t.room !== this.state.room) return;
148
- if (this.state.self && this.state.self !== t.self) {
149
- for (const [n, o] of this.links)
150
- o.pc.close(), this.dropAudio(n);
151
- this.links.clear();
152
- }
153
- const s = p([], t.peers, t.self), e = Math.min(Date.now(), ...t.peers.map((n) => n.since).filter(Boolean));
154
- this.set({ status: "live", self: t.self, peers: s, startedAt: e, error: null });
155
- for (const n of s) this.link(n.socket);
224
+ const e = !!this.state.self && this.state.self !== t.self;
225
+ if (e)
226
+ for (const [n, f] of this.links) this.closeLink(n, f);
227
+ const s = g([], t.peers, t.self), i = Math.min(Date.now(), ...t.peers.map((n) => n.since).filter(Boolean)), o = t.mode === "sfu" ? "sfu" : "mesh";
228
+ if (this.set({ status: "live", self: t.self, peers: s, startedAt: i, error: null, mode: o }), o === "sfu") this.enterSfu(e);
229
+ else for (const n of s) this.link(n.socket);
156
230
  this.ping && clearInterval(this.ping), this.ping = setInterval(() => {
157
231
  var n;
158
232
  this.state.room && ((n = this.transport) == null || n.emit("huddle:ping", { room: this.state.room }));
159
- }, S), (this.state.muted || this.state.sharing) && ((i = this.transport) == null || i.emit("huddle:state", {
233
+ }, V), (this.state.muted || this.state.sharing) && ((r = this.transport) == null || r.emit("huddle:state", {
160
234
  room: t.room,
161
235
  muted: this.state.muted,
162
236
  screen: this.state.sharing
163
237
  }));
238
+ for (const n of this.exts) (l = n.joined) == null || l.call(n);
164
239
  }
165
240
  onPeers(t) {
166
- var i;
241
+ var o, r, l;
167
242
  if (this.setRoster(t.room, t.peers), t.room !== this.state.room || this.state.status !== "live") return;
168
- const s = this.state.self, e = p(this.state.peers, t.peers, s);
169
- for (const [n, o] of this.links)
170
- e.some((l) => l.socket === n) || (o.pc.close(), this.links.delete(n), this.dropAudio(n));
171
- for (const n of e) {
172
- const o = this.links.get(n.socket);
173
- if (!(!o || n.audio || n.video))
174
- for (const l of o.pc.getReceivers()) {
175
- if (((i = l.track) == null ? void 0 : i.readyState) !== "live") continue;
176
- const d = new MediaStream([l.track]);
177
- l.track.kind === "video" ? n.video = d : n.audio = d;
243
+ t.mode === "sfu" && this.state.mode !== "sfu" && this.enterSfu(!1);
244
+ const e = this.state.self, s = g(this.state.peers, t.peers, e);
245
+ for (const [n, f] of this.links)
246
+ s.some((u) => u.socket === n) || this.closeLink(n, f);
247
+ for (const n of [...this.audio.keys()])
248
+ s.some((f) => f.socket === n) || this.dropAudio(n);
249
+ for (const n of [...this.sfuVideo.keys()])
250
+ s.some((f) => f.socket === n) || this.sfuVideo.delete(n);
251
+ for (const n of s) {
252
+ if (n.audio || n.video) continue;
253
+ const f = this.audio.get(n.socket);
254
+ if (this.sfuAudio.has(n.socket) && (f == null ? void 0 : f.srcObject) instanceof MediaStream) {
255
+ n.audio = f.srcObject, n.connection = "connected";
256
+ continue;
257
+ }
258
+ const u = this.links.get(n.socket);
259
+ if (u)
260
+ for (const m of u.pc.getReceivers()) {
261
+ if (((o = m.track) == null ? void 0 : o.readyState) !== "live") continue;
262
+ const a = new MediaStream([m.track]);
263
+ m.track.kind === "video" ? (r = this.remoteVideo.get(n.socket)) != null && r.size || (n.video = a) : n.audio = a;
178
264
  }
179
265
  }
180
- this.set({ peers: e });
181
- for (const n of e) this.links.has(n.socket) || this.link(n.socket);
266
+ for (const n of s)
267
+ (l = this.remoteVideo.get(n.socket)) != null && l.size && Object.assign(n, this.videoOf(n.socket, n.cam ?? null));
268
+ const i = e ? t.peers.find((n) => n.socket === e) : void 0;
269
+ if (this.set({ peers: s, handAt: (i == null ? void 0 : i.hand_at) ?? null }), this.state.mode !== "sfu")
270
+ for (const n of s) this.links.has(n.socket) || this.link(n.socket);
271
+ }
272
+ // ── Large huddles (#2859) ─────────────────────────────────────────────────
273
+ onMode(t) {
274
+ t.room !== this.state.room || this.state.status !== "live" || t.mode === "sfu" && this.state.mode !== "sfu" && this.enterSfu(!1);
275
+ }
276
+ /**
277
+ * Move this browser onto the SFU: fetch a token for the room, connect, publish
278
+ * the mic (and the screen if sharing), then close the mesh links once the SFU
279
+ * carries audio — at most MESH_OVERLAP_MS later. `fresh` = my socket id changed
280
+ * (a reconnect), so any earlier session is for an identity that no longer exists.
281
+ */
282
+ async enterSfu(t) {
283
+ var o, r, l, n;
284
+ const e = this.state.room, s = this.state.self;
285
+ if (!e || !s || this.sfu && !t || (this.set({ mode: "sfu" }), !this.fetchSfu)) return;
286
+ (o = this.sfu) == null || o.disconnect(), this.sfu = null;
287
+ let i;
288
+ try {
289
+ const f = await this.fetchSfu(e, s);
290
+ if (this.state.room !== e || this.state.self !== s || (i = new L(), this.sfu = i, await i.connect(f, {
291
+ onTrack: (a, d, v) => this.onSfuTrack(a, d, v),
292
+ onTrackEnd: (a, d) => this.onSfuTrackEnd(a, d),
293
+ onReconnecting: () => {
294
+ this.sfu === i && this.iceFailed++;
295
+ },
296
+ onDisconnected: () => {
297
+ this.sfu === i && (this.leave(), this.set({
298
+ status: "error",
299
+ error: "The media server dropped the call. Join again to continue.",
300
+ room: e
301
+ }));
302
+ }
303
+ }), this.sfu !== i)) return;
304
+ const u = (r = this.mic) == null ? void 0 : r.getAudioTracks()[0];
305
+ u && await i.publishMic(u), this.state.muted && i.setMicMuted(!0);
306
+ const m = (l = this.state.localScreen) == null ? void 0 : l.getVideoTracks()[0];
307
+ m && this.state.sharing && await i.publishScreen(m);
308
+ for (const a of this.exts) (n = a.sfu) == null || n.call(a, i);
309
+ } catch {
310
+ if (this.state.room !== e) return;
311
+ this.leave(), this.set({
312
+ status: "error",
313
+ error: "Could not reach the media server for this large huddle.",
314
+ room: e
315
+ });
316
+ return;
317
+ }
318
+ this.meshCloseTimer && clearTimeout(this.meshCloseTimer), this.meshCloseTimer = setTimeout(() => this.closeMeshLinks(), A);
319
+ }
320
+ /** The SFU's labelled video for a peer (#2858): camera tile and shared screen, apart. */
321
+ sfuVideoOf(t) {
322
+ let e = this.sfuVideo.get(t);
323
+ return e || (e = { camera: null, screen: null }, this.sfuVideo.set(t, e)), e;
324
+ }
325
+ onSfuTrack(t, e, s) {
326
+ if (e === "camera") {
327
+ this.sfuVideoOf(t).camera = s, this.updatePeer(t, { camera: s });
328
+ return;
329
+ }
330
+ if (e === "screen") {
331
+ this.sfuVideoOf(t).screen = s, this.updatePeer(t, { video: s });
332
+ return;
333
+ }
334
+ this.sfuAudio.add(t), this.updatePeer(t, { audio: s, connection: "connected" }), this.playAudio(t, s), !this.state.peers.some(
335
+ (o) => this.links.has(o.socket) && !this.sfuAudio.has(o.socket)
336
+ ) && this.links.size && this.closeMeshLinks();
337
+ }
338
+ onSfuTrackEnd(t, e) {
339
+ if (e === "mic") return this.updatePeer(t, { audio: null });
340
+ const s = this.sfuVideo.get(t);
341
+ e === "camera" ? (s && (s.camera = null), this.updatePeer(t, { camera: null })) : (s && (s.screen = null), this.updatePeer(t, { video: null }));
342
+ }
343
+ /**
344
+ * Close the mesh after the switch: the SFU streams already own the audio elements, and
345
+ * the tiles show what the SFU delivers (a mesh-only video would be a frozen frame).
346
+ */
347
+ closeMeshLinks() {
348
+ this.meshCloseTimer && clearTimeout(this.meshCloseTimer), this.meshCloseTimer = null;
349
+ for (const [t, e] of this.links) this.closeLink(t, e, !0);
350
+ this.set({
351
+ peers: this.state.peers.map((t) => {
352
+ var e, s;
353
+ return {
354
+ ...t,
355
+ connection: this.sfuAudio.has(t.socket) ? "connected" : "connecting",
356
+ video: ((e = this.sfuVideo.get(t.socket)) == null ? void 0 : e.screen) ?? null,
357
+ camera: ((s = this.sfuVideo.get(t.socket)) == null ? void 0 : s.camera) ?? null
358
+ };
359
+ })
360
+ });
361
+ }
362
+ /** The SFU link while the huddle is large and connected (#2858 camera, #2848 stats). */
363
+ sfuSession() {
364
+ var t;
365
+ return this.state.mode === "sfu" && ((t = this.sfu) != null && t.connected) ? this.sfu : null;
182
366
  }
183
367
  onDenied(t) {
184
368
  if (t.room && t.room !== this.state.room) return;
185
- const s = {
369
+ const e = {
186
370
  full: "This huddle is full.",
187
371
  "no-access": "You no longer have access to this room.",
188
372
  off: "Huddles are turned off on this instance.",
@@ -192,89 +376,117 @@ class w {
192
376
  };
193
377
  this.teardown(), this.set({
194
378
  status: "error",
195
- error: s[t.reason] ?? "Could not join the huddle.",
379
+ error: e[t.reason] ?? "Could not join the huddle.",
196
380
  room: t.room
197
381
  });
198
382
  }
199
383
  clearError() {
200
- this.state.status === "error" && this.set({ ...u, muted: this.state.muted });
384
+ this.state.status === "error" && this.set({ ...p, muted: this.state.muted });
201
385
  }
202
386
  // ── Peer links ────────────────────────────────────────────────────────────
203
- updatePeer(t, s) {
387
+ updatePeer(t, e) {
204
388
  this.set({
205
- peers: this.state.peers.map((e) => e.socket === t ? { ...e, ...s } : e)
389
+ peers: this.state.peers.map((s) => s.socket === t ? { ...s, ...e } : s)
206
390
  });
207
391
  }
208
392
  async link(t) {
209
- var f, m;
210
- const s = this.links.get(t);
211
- if (s) return s;
212
- const e = this.state.self, i = this.state.room;
213
- if (!e || !i || !this.transport) return null;
214
- const n = await this.iceServers(i).catch(() => []);
393
+ var f, u, m;
394
+ const e = this.links.get(t);
395
+ if (e) return e;
396
+ const s = this.state.self, i = this.state.room;
397
+ if (!s || !i || !this.transport || this.state.mode === "sfu") return null;
398
+ const o = await this.iceServers(i).catch(() => []);
215
399
  if (this.links.has(t)) return this.links.get(t) ?? null;
216
- const o = new RTCPeerConnection({ iceServers: n }), l = {
217
- pc: o,
218
- polite: k(e, t),
400
+ const r = new RTCPeerConnection({ iceServers: o }), l = {
401
+ pc: r,
402
+ polite: O(s, t),
219
403
  makingOffer: !1,
220
404
  ignoreOffer: !1,
221
405
  screenSender: null,
222
406
  queue: Promise.resolve()
223
407
  };
224
408
  this.links.set(t, l);
225
- for (const a of ((f = this.mic) == null ? void 0 : f.getAudioTracks()) ?? []) o.addTrack(a, this.mic);
226
- const d = (m = this.state.localScreen) == null ? void 0 : m.getVideoTracks()[0];
227
- return d && this.state.localScreen && (l.screenSender = o.addTrack(d, this.state.localScreen)), o.onnegotiationneeded = async () => {
409
+ for (const a of ((f = this.mic) == null ? void 0 : f.getAudioTracks()) ?? []) r.addTrack(a, this.mic);
410
+ const n = (u = this.state.localScreen) == null ? void 0 : u.getVideoTracks()[0];
411
+ n && this.state.localScreen && (l.screenSender = r.addTrack(n, this.state.localScreen));
412
+ for (const a of this.exts) (m = a.link) == null || m.call(a, t, r);
413
+ return r.onnegotiationneeded = async () => {
228
414
  try {
229
- l.makingOffer = !0, await o.setLocalDescription(), this.signal(t, { description: o.localDescription });
415
+ l.makingOffer = !0, await r.setLocalDescription(), this.signal(t, { description: r.localDescription });
230
416
  } catch {
231
417
  } finally {
232
418
  l.makingOffer = !1;
233
419
  }
234
- }, o.onicecandidate = (a) => {
420
+ }, r.onicecandidate = (a) => {
235
421
  a.candidate && this.signal(t, { candidate: a.candidate.toJSON() });
236
- }, o.ontrack = (a) => {
237
- const h = a.streams[0] ?? new MediaStream([a.track]);
238
- a.track.kind === "video" ? (this.updatePeer(t, { video: h }), a.track.onended = () => this.updatePeer(t, { video: null }), a.track.onmute = () => this.updatePeer(t, { video: null }), a.track.onunmute = () => this.updatePeer(t, { video: h })) : (this.updatePeer(t, { audio: h }), this.playAudio(t, h));
239
- }, o.onconnectionstatechange = () => {
240
- var h;
241
- const a = o.connectionState;
422
+ }, r.ontrack = (a) => {
423
+ const d = a.streams[0] ?? new MediaStream([a.track]);
424
+ a.track.kind === "video" ? this.trackVideo(t, d, a.track) : (this.updatePeer(t, { audio: d }), this.playAudio(t, d));
425
+ }, r.onconnectionstatechange = () => {
426
+ var d;
427
+ const a = r.connectionState;
242
428
  this.updatePeer(t, {
243
429
  connection: a === "connected" ? "connected" : a === "failed" || a === "closed" ? "failed" : "connecting"
244
- }), a === "failed" && ((h = o.restartIce) == null || h.call(o));
430
+ }), a === "failed" && (this.iceFailed++, (d = r.restartIce) == null || d.call(r));
245
431
  }, l;
246
432
  }
247
- playAudio(t, s) {
433
+ /** A peer's video track: a screen share, or (#2858) the camera the roster names. */
434
+ trackVideo(t, e, s) {
435
+ let i = this.remoteVideo.get(t);
436
+ i || (i = /* @__PURE__ */ new Map(), this.remoteVideo.set(t, i));
437
+ const o = { stream: e, live: !0 };
438
+ i.set(e.id, o);
439
+ const r = () => {
440
+ var n;
441
+ const l = ((n = this.state.peers.find((f) => f.socket === t)) == null ? void 0 : n.cam) ?? null;
442
+ this.updatePeer(t, this.videoOf(t, l));
443
+ };
444
+ s.onended = () => {
445
+ var l;
446
+ (l = this.remoteVideo.get(t)) == null || l.delete(e.id), r();
447
+ }, s.onmute = () => {
448
+ o.live = !1, r();
449
+ }, s.onunmute = () => {
450
+ o.live = !0, r();
451
+ }, r();
452
+ }
453
+ videoOf(t, e) {
454
+ let s = null, i = null;
455
+ for (const [o, r] of this.remoteVideo.get(t) ?? [])
456
+ r.live && (e && o === e ? i = r.stream : s = r.stream);
457
+ return { video: s, camera: i };
458
+ }
459
+ playAudio(t, e) {
248
460
  if (typeof document > "u") return;
249
- let e = this.audio.get(t);
250
- e || (e = document.createElement("audio"), e.autoplay = !0, e.setAttribute("data-huddle-audio", t), e.style.display = "none", document.body.appendChild(e), this.audio.set(t, e)), e.srcObject = s, e.play().catch(() => null);
461
+ let s = this.audio.get(t);
462
+ s || (s = document.createElement("audio"), s.autoplay = !0, s.setAttribute("data-huddle-audio", t), s.style.display = "none", document.body.appendChild(s), this.audio.set(t, s), this.sinkId && k(s, this.sinkId)), s.srcObject = e, s.play().catch(() => null);
251
463
  }
252
464
  dropAudio(t) {
253
- const s = this.audio.get(t);
254
- s && (s.srcObject = null, s.remove(), this.audio.delete(t));
465
+ const e = this.audio.get(t);
466
+ e && (e.srcObject = null, e.remove(), this.audio.delete(t));
255
467
  }
256
- signal(t, s) {
257
- var e;
258
- this.state.room && ((e = this.transport) == null || e.emit("huddle:signal", { room: this.state.room, to: t, data: s }));
468
+ signal(t, e) {
469
+ var s;
470
+ this.state.room && ((s = this.transport) == null || s.emit("huddle:signal", { room: this.state.room, to: t, data: e }));
259
471
  }
260
472
  async onSignal(t) {
261
473
  if (t.room !== this.state.room || this.state.status !== "live") return;
262
- const s = await this.link(t.from);
263
- if (s)
264
- return s.queue = s.queue.then(() => this.applySignal(s, t)).catch(() => {
265
- }), s.queue;
266
- }
267
- async applySignal(t, s) {
268
- const { pc: e } = t;
269
- if (e.signalingState !== "closed")
474
+ const e = await this.link(t.from);
475
+ if (e)
476
+ return e.queue = e.queue.then(() => this.applySignal(e, t)).catch(() => {
477
+ }), e.queue;
478
+ }
479
+ async applySignal(t, e) {
480
+ const { pc: s } = t;
481
+ if (s.signalingState !== "closed")
270
482
  try {
271
- if (s.data.description) {
272
- const i = s.data.description, n = i.type === "offer" && (t.makingOffer || e.signalingState !== "stable");
273
- if (t.ignoreOffer = !t.polite && n, t.ignoreOffer) return;
274
- await e.setRemoteDescription(i), i.type === "offer" && (await e.setLocalDescription(), this.signal(s.from, { description: e.localDescription }));
275
- } else if (s.data.candidate)
483
+ if (e.data.description) {
484
+ const i = e.data.description, o = i.type === "offer" && (t.makingOffer || s.signalingState !== "stable");
485
+ if (t.ignoreOffer = !t.polite && o, t.ignoreOffer) return;
486
+ await s.setRemoteDescription(i), i.type === "offer" && (await s.setLocalDescription(), this.signal(e.from, { description: s.localDescription }));
487
+ } else if (e.data.candidate)
276
488
  try {
277
- await e.addIceCandidate(s.data.candidate);
489
+ await s.addIceCandidate(e.data.candidate);
278
490
  } catch (i) {
279
491
  if (!t.ignoreOffer) throw i;
280
492
  }
@@ -283,12 +495,12 @@ class w {
283
495
  }
284
496
  // ── Controls ──────────────────────────────────────────────────────────────
285
497
  setMuted(t) {
286
- var s, e;
287
- for (const i of ((s = this.mic) == null ? void 0 : s.getAudioTracks()) ?? []) i.enabled = !t;
288
- this.set({ muted: t }), this.state.room && this.state.status === "live" && ((e = this.transport) == null || e.emit("huddle:state", { room: this.state.room, muted: t }));
498
+ var e, s, i;
499
+ for (const o of ((e = this.mic) == null ? void 0 : e.getAudioTracks()) ?? []) o.enabled = !t;
500
+ (s = this.sfu) == null || s.setMicMuted(t), this.set({ muted: t }), this.state.room && this.state.status === "live" && ((i = this.transport) == null || i.emit("huddle:state", { room: this.state.room, muted: t }));
289
501
  }
290
502
  async startShare() {
291
- var e;
503
+ var s, i;
292
504
  if (this.state.status !== "live" || this.state.sharing) return;
293
505
  let t;
294
506
  try {
@@ -299,44 +511,106 @@ class w {
299
511
  } catch {
300
512
  return;
301
513
  }
302
- const s = t.getVideoTracks()[0];
303
- if (s) {
304
- s.onended = () => this.stopShare();
305
- for (const [, i] of this.links) i.screenSender = i.pc.addTrack(s, t);
306
- this.set({ sharing: !0, localScreen: t }), (e = this.transport) == null || e.emit("huddle:state", { room: this.state.room, screen: !0 });
514
+ const e = t.getVideoTracks()[0];
515
+ if (e) {
516
+ if (e.onended = () => this.stopShare(), this.state.mode === "sfu") (s = this.sfu) == null || s.publishScreen(e);
517
+ else for (const [, o] of this.links) o.screenSender = o.pc.addTrack(e, t);
518
+ this.set({ sharing: !0, localScreen: t }), (i = this.transport) == null || i.emit("huddle:state", { room: this.state.room, screen: !0 });
307
519
  }
308
520
  }
309
521
  stopShare() {
310
- var t, s;
522
+ var t, e, s;
311
523
  if (this.state.sharing) {
312
- for (const e of ((t = this.state.localScreen) == null ? void 0 : t.getTracks()) ?? []) e.stop();
313
- for (const [, e] of this.links)
314
- if (e.screenSender) {
524
+ (t = this.sfu) == null || t.unpublishScreen();
525
+ for (const i of ((e = this.state.localScreen) == null ? void 0 : e.getTracks()) ?? []) i.stop();
526
+ for (const [, i] of this.links)
527
+ if (i.screenSender) {
315
528
  try {
316
- e.pc.removeTrack(e.screenSender);
529
+ i.pc.removeTrack(i.screenSender);
317
530
  } catch {
318
531
  }
319
- e.screenSender = null;
532
+ i.screenSender = null;
320
533
  }
321
534
  this.set({ sharing: !1, localScreen: null }), this.state.room && this.state.status === "live" && ((s = this.transport) == null || s.emit("huddle:state", { room: this.state.room, screen: !1 }));
322
535
  }
323
536
  }
537
+ /** The microphone stream I send (null when not in a huddle). */
538
+ localMic() {
539
+ return this.mic;
540
+ }
541
+ /** Each peer link, for stats and sender tuning — read-only use. */
542
+ peerLinks() {
543
+ return [...this.links].map(([t, e]) => ({
544
+ socket: t,
545
+ pc: e.pc,
546
+ screenSender: e.screenSender
547
+ }));
548
+ }
549
+ /**
550
+ * What the one quality sampler reads (#2848/#2849), in either mode: the mesh's peer
551
+ * connections, or (#2859) the SFU link's per-person and own-uplink stats. While the
552
+ * mesh is closing after a switch the SFU is the one that counts.
553
+ */
554
+ async qualitySources() {
555
+ return this.state.mode === "sfu" ? { mode: "sfu", sfu: this.sfu ? await this.sfu.stats().catch(() => null) : null } : { mode: "mesh", links: this.peerLinks() };
556
+ }
557
+ /** The sampler's latest summary of one link, kept for the leave-time report (#2849). */
558
+ recordLinkStats(t, e) {
559
+ this.state.status === "live" && this.linkStats.set(t, e);
560
+ }
561
+ /** Swap the microphone on every link in place (replaceTrack: no renegotiation). */
562
+ async swapMic(t) {
563
+ var i;
564
+ const e = t.getAudioTracks()[0];
565
+ if (!e || !this.state.room || this.state.status === "idle") {
566
+ for (const o of t.getTracks()) o.stop();
567
+ return;
568
+ }
569
+ e.enabled = !this.state.muted;
570
+ const s = this.mic;
571
+ this.mic = t;
572
+ for (const [, o] of this.links)
573
+ for (const r of o.pc.getSenders())
574
+ r === o.screenSender || ((i = r.track) == null ? void 0 : i.kind) !== "audio" || await r.replaceTrack(e).catch(() => null);
575
+ this.sfu && await this.sfu.replaceMic(e).catch(() => null);
576
+ for (const o of (s == null ? void 0 : s.getTracks()) ?? []) o.stop();
577
+ this.set({ micLabel: e.label || null });
578
+ }
579
+ /** Play everyone through this output ('' = the system default), where the browser can. */
580
+ setSpeaker(t) {
581
+ this.sinkId = t;
582
+ for (const [, e] of this.audio) k(e, t);
583
+ }
584
+ speaker() {
585
+ return this.sinkId;
586
+ }
587
+ /** Written by the quality monitor every poll; read by anyone through getHuddleQuality(). */
588
+ reportQuality(t) {
589
+ this.quality = t;
590
+ }
591
+ getHuddleQuality() {
592
+ return this.state.status === "live" ? this.quality : null;
593
+ }
594
+ }
595
+ async function k(h, t) {
596
+ const e = h;
597
+ typeof e.setSinkId == "function" && await e.setSinkId(t).catch(() => null);
324
598
  }
325
- const D = new w();
326
- function b() {
599
+ const q = new C();
600
+ function F() {
327
601
  if (typeof navigator > "u" || typeof window > "u")
328
602
  return { audio: !1, screen: !1 };
329
- const r = navigator.mediaDevices;
603
+ const h = navigator.mediaDevices;
330
604
  return {
331
- audio: !!(r != null && r.getUserMedia) && typeof RTCPeerConnection < "u",
332
- screen: !!(r != null && r.getDisplayMedia)
605
+ audio: !!(h != null && h.getUserMedia) && typeof RTCPeerConnection < "u",
606
+ screen: !!(h != null && h.getDisplayMedia)
333
607
  };
334
608
  }
335
609
  export {
336
- T as formatElapsed,
337
- D as huddleStore,
338
- b as huddleSupport,
339
- p as mergeRoster,
340
- M as peerSummary,
341
- k as politeFor
610
+ $ as formatElapsed,
611
+ q as huddleStore,
612
+ F as huddleSupport,
613
+ g as mergeRoster,
614
+ R as peerSummary,
615
+ O as politeFor
342
616
  };