@rapidmx/meet-plugin 0.13.0 → 0.14.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 (41) hide show
  1. package/README.md +55 -55
  2. package/apps/meet/[token].tsx +341 -341
  3. package/apps/meet/_CallControls.tsx +366 -394
  4. package/apps/meet/_CallView.tsx +987 -848
  5. package/apps/meet/_DiagnosticsWindow.tsx +80 -0
  6. package/apps/meet/_MeetLobby.tsx +224 -224
  7. package/apps/meet/_ParticipantTile.tsx +169 -169
  8. package/apps/meet/_ParticipantsDrawer.tsx +10 -104
  9. package/apps/meet/_SettingsModal.tsx +152 -0
  10. package/apps/meet/_icons.tsx +80 -82
  11. package/apps/meet/_meetApi.ts +212 -212
  12. package/apps/shared/media/filters/VideoFilterProcessor.ts +408 -408
  13. package/apps/shared/media/filters/mlModels.ts +174 -174
  14. package/apps/shared/push/GuestSignalingClient.ts +359 -359
  15. package/apps/shared/relay/RelayClient.ts +7 -2
  16. package/apps/shared/webrtc/MeshConnectionManager.ts +949 -795
  17. package/apps/shared/webrtc/types.ts +247 -222
  18. package/dist/apps/meet/_CallControls.d.ts +3 -8
  19. package/dist/apps/meet/_CallControls.js +3 -10
  20. package/dist/apps/meet/_CallView.d.ts +15 -2
  21. package/dist/apps/meet/_CallView.js +70 -4
  22. package/dist/apps/meet/_DiagnosticsWindow.d.ts +25 -0
  23. package/dist/apps/meet/_DiagnosticsWindow.js +45 -0
  24. package/dist/apps/meet/_ParticipantsDrawer.d.ts +10 -21
  25. package/dist/apps/meet/_ParticipantsDrawer.js +9 -39
  26. package/dist/apps/meet/_SettingsModal.d.ts +28 -0
  27. package/dist/apps/meet/_SettingsModal.js +59 -0
  28. package/dist/apps/meet/_icons.d.ts +1 -1
  29. package/dist/apps/meet/_icons.js +1 -1
  30. package/dist/apps/shared/relay/RelayClient.d.ts +6 -1
  31. package/dist/apps/shared/relay/RelayClient.js +7 -2
  32. package/dist/apps/shared/webrtc/MeshConnectionManager.d.ts +55 -2
  33. package/dist/apps/shared/webrtc/MeshConnectionManager.js +153 -9
  34. package/dist/apps/shared/webrtc/types.d.ts +25 -1
  35. package/dist/lib/util/RedisRelayBus.js +61 -5
  36. package/dist/lib/util/RedisRelayBus.js.map +1 -1
  37. package/dist/lib/util/RelayHub.js +10 -3
  38. package/dist/lib/util/RelayHub.js.map +1 -1
  39. package/dist/types/util/RedisRelayBus.d.ts +45 -6
  40. package/dist/types/util/RelayHub.d.ts +9 -2
  41. package/package.json +208 -206
@@ -1,848 +1,987 @@
1
- ///////////////////////////////////////////////////////////////////////////////
2
- // Copyright (C) 2026 Jean-Philippe Steinmetz. All rights reserved.
3
- // SPDX-License-Identifier: MPL-2.0
4
- ///////////////////////////////////////////////////////////////////////////////
5
- /**
6
- * The in-call view: connects the full-mesh `MeshConnectionManager` to the meeting's signaling channel
7
- * (`GuestSignalingClient`), mirrors its events into React state, and renders the grid/focused/presentation
8
- * layouts - see `apps/shared/webrtc/MeshConnectionManager.ts`'s doc comment for the who-calls-whom and
9
- * single-presenter rules this view relies on rather than re-deciding. These reusable, non-UI modules live under
10
- * `apps/shared/` rather than this plugin's backend `src/` - `tsconfig.apps.json` builds `apps/` as its own
11
- * program rooted at `apps/`, which cannot reference files outside it (`TS6059`), matching `booking-plugin`'s own
12
- * identical `apps/shared/` convention for reusable frontend-only code.
13
- *
14
- * ## Fitting the window
15
- *
16
- * The call fills the viewport (`fixed inset-0`) as three rows - a slim header, the tiles (which take whatever room
17
- * is left and never scroll the page), and the control bar pinned to the bottom - instead of being laid out inside
18
- * the branded page shell, whose header, footer and padding pushed it off the screen. The local participant's own
19
- * tile is a small tile in the bottom-right corner while anyone else is in the call (above the control bar on a narrower
20
- * window, and at the top on a phone, where the bar wraps onto a second row), and fills the tile area while they are
21
- * alone.
22
- *
23
- * ## Who is heard
24
- *
25
- * A tile never plays sound. Each remote stream is played by its own hidden `<audio>` element, so a participant is
26
- * heard whether or not their camera is on and whichever layout is showing. If the browser refuses to start the
27
- * audio (an autoplay policy), a banner asks for a click, which is the gesture that allows it.
28
- *
29
- * ## Layout precedence
30
- *
31
- * `computeMainUid()` decides who is shown large, in this order: an active presenter always wins (presentation
32
- * mode, forcing a focused-like layout regardless of `viewMode`); otherwise a manually pinned participant; else the
33
- * auto-detected active speaker (silently ignored while presenting, so it never fights the presenter for the main
34
- * slot); else, in focus mode with nobody yet speaking, the first other participant - so focus mode never shows an
35
- * empty main slot once someone else has joined.
36
- *
37
- * ## Talking stick mode
38
- *
39
- * A host-only navbar toggle (`handleToggleTalkingStick()`) that, once on, lets at most one participant's microphone
40
- * be unmuted at a time - the host assigns who from the participants drawer (`handleGiveTalkingStick()`), everyone
41
- * else sees a header chip naming who currently holds it. Purely an in-call runtime state signaled peer-to-peer over
42
- * the mesh (`MeshConnectionManager.setTalkingStick()`/`"talking-stick-changed"`), the same shape as `mute-request`/
43
- * `kicked`: no `VideoMeeting` field, no backend route, and only as "enforced" as a cooperating client makes it (see
44
- * `types.ts`'s `SignalMessage` doc comment on why the signaling layer has no host identity to check against). What
45
- * *is* new here versus `mute-request`'s one-time nudge: becoming or ceasing to be the holder force-toggles this
46
- * tab's own mic to match (`selfHasTalkingStick`'s effect, below), and the mic button itself is disabled
47
- * (`micLocked`, passed to `CallControls`) the whole time a non-holder would otherwise be able to tap it straight
48
- * back on - a real restriction for as long as this tab's own UI is the one in front of the participant, not just a
49
- * suggestion. Turning the mode off touches nobody's mic state, matching every other host toggle's "never
50
- * retroactive" posture. A holder who leaves the call is not specially reassigned - see
51
- * `MeshConnectionManager`'s own doc comment on why that needs no extra code - the header chip then says nobody has
52
- * the floor, until the host picks someone (or themselves) again.
53
- */
54
- import React, { useCallback, useEffect, useRef, useState } from "react";
55
- import { playRaisedHandChime } from "../shared/media/chime.js";
56
- import { pickActiveSpeaker } from "../shared/media/activeSpeaker.js";
57
- import { startLevelMeter, type LevelMeterHandle } from "../shared/media/levelMeter.js";
58
- import { requestDisplayMedia, stopStream } from "../shared/media/deviceMedia.js";
59
- import { NO_SCREEN_TRANSFORM, ScreenTransformProcessor, rotateClockwise, type ScreenTransformState } from "../shared/media/filters/ScreenTransform.js";
60
- import type { LocalMedia } from "../shared/media/useLocalMedia.js";
61
- import { createBrowserPeerConnection } from "../shared/webrtc/realPeerConnection.js";
62
- import { MeshConnectionManager } from "../shared/webrtc/MeshConnectionManager.js";
63
- import type { MeshParticipant, RelayTransportLike } from "../shared/webrtc/types.js";
64
- import { createRelayTransport } from "../shared/relay/RelayTransport.js";
65
- import { GuestSignalingClient } from "../shared/push/GuestSignalingClient.js";
66
- import CallControls, { type CallViewMode } from "./_CallControls.js";
67
- import { BatonIcon } from "./_icons.js";
68
- import {
69
- admitParticipant,
70
- denyParticipant,
71
- kickParticipant,
72
- listWaitingParticipants,
73
- setForceMuteOnJoin as apiSetForceMuteOnJoin,
74
- setMeetingPassword,
75
- setWaitingRoomEnabled as apiSetWaitingRoomEnabled,
76
- type WaitingParticipant,
77
- } from "./_meetApi.js";
78
- import ParticipantTile from "./_ParticipantTile.js";
79
- import ParticipantsDrawer from "./_ParticipantsDrawer.js";
80
-
81
- export interface CallViewProps {
82
- channel: string;
83
- /** Omitted when the caller already authenticated as a real RapidMX identity in `join()` - there is then no
84
- * guest token to hand `GuestSignalingClient`, which relies entirely on the browser's own already-existing
85
- * `jwt` session cookie instead (see its own doc comment). */
86
- token?: string;
87
- selfUid: string;
88
- selfName: string;
89
- meetingTitle: string;
90
- iceServers: RTCIceServer[];
91
- /** Whether the server offers the WebSocket media relay, the last-resort path for a participant neither a direct
92
- * connection nor the TURN server can reach - `VideoMeetingJoinResult.relayEnabled`. Off when absent. */
93
- relayEnabled?: boolean;
94
- /** The meeting's host account uid (`PublicVideoMeeting.hostUid`) - this client's only source of a "host"
95
- * identity, purely for deciding which controls to show (see `BaseVideoMeetingRoute`'s class doc comment on why
96
- * this is a client-side-only check, not a server-enforced one for every moderation action). Absent when the
97
- * server couldn't resolve one, in which case nobody sees host controls. */
98
- hostUid?: string;
99
- /** `PublicVideoMeeting.forceMuteOnJoin` as of this call's own `join()` - the starting value for the host's
100
- * toggle in the participants drawer, which this view then owns and keeps current itself (see
101
- * `handleToggleForceMuteOnJoin()`). Does not update if changed elsewhere while this view is mounted - there is
102
- * no signal for that today, matching every other meeting-settings field's lack of live sync. */
103
- initialForceMuteOnJoin?: boolean;
104
- /** `PublicVideoMeeting.hasPassword` as of this call's own `join()` - the starting value for the host's password
105
- * section in the participants drawer, same "owned and kept current by this view" shape as
106
- * `initialForceMuteOnJoin`. */
107
- initialHasPassword?: boolean;
108
- /** `PublicVideoMeeting.waitingRoomEnabled` as of this call's own `join()` - the starting value for the host's
109
- * waiting-room toggle, same "owned and kept current by this view" shape as `initialForceMuteOnJoin`. */
110
- initialWaitingRoomEnabled?: boolean;
111
- /** The camera and microphone, owned by the page (`[token].tsx`) - the lobby's tracks carried into the call. */
112
- media: LocalMedia;
113
- /** Called once the participant leaves, for any reason. `reason` is set only when the call ended without the
114
- * participant's own action - currently, being kicked by the host - so the caller can show a distinct message
115
- * instead of the ordinary "you left" one; omitted for an ordinary voluntary leave. */
116
- onLeave: (reason?: string) => void;
117
- }
118
-
119
- /** How long a reaction floats on screen. */
120
- const REACTION_MS = 4_000;
121
- /** The most reactions shown at once - a burst beyond this drops the oldest. */
122
- const MAX_REACTIONS = 12;
123
- /** How often the host's drawer refreshes its own waiting-room list while open. */
124
- const DEFAULT_WAITING_POLL_MS = 3_000;
125
-
126
- interface Reaction {
127
- id: number;
128
- emoji: string;
129
- name: string;
130
- }
131
-
132
- /** Who is shown large - see this module's doc comment. `undefined` when nobody but the local participant has
133
- * joined yet (the grid/focus toggle still works, there's just nobody else to focus on). */
134
- export function computeMainUid(options: {
135
- presenterUid?: string;
136
- pinnedUid?: string;
137
- activeSpeakerUid?: string;
138
- otherUids: readonly string[];
139
- }): string | undefined {
140
- if (options.presenterUid) {
141
- return options.presenterUid;
142
- }
143
- if (options.pinnedUid) {
144
- return options.pinnedUid;
145
- }
146
- if (options.activeSpeakerUid) {
147
- return options.activeSpeakerUid;
148
- }
149
- return options.otherUids[0];
150
- }
151
-
152
- /** This tab's identity in the call: the account (or guest) uid plus a random suffix, so the same account joining
153
- * from two devices - or two tabs - is two participants rather than one that ignores itself. It travels as the
154
- * `peer` of each message; `from` stays the exact authenticated uid, which the server requires. */
155
- export function newPeerId(selfUid: string): string {
156
- return `${selfUid}~${globalThis.crypto?.randomUUID?.() ?? Math.random().toString(36).slice(2)}`;
157
- }
158
-
159
- /** The real, server-enforced account uid behind a participant's tab-scoped `MeshParticipant.uid` (`newPeerId()`'s
160
- * `<account uid>~<random>` shape) - what the kick route's ACL grant is actually keyed by, unlike the tab-scoped id
161
- * this view otherwise addresses a specific tab with. Neither a real account uid nor a minted guest uid (see
162
- * `GUEST_UID_PREFIX`/`mintGuestToken()` on the backend) can itself contain "~", so the last one found is always
163
- * exactly this suffix's own separator. */
164
- export function accountUidOf(peerUid: string): string {
165
- const i = peerUid.lastIndexOf("~");
166
- return i === -1 ? peerUid : peerUid.slice(0, i);
167
- }
168
-
169
- export default function CallView({
170
- channel,
171
- token,
172
- selfUid,
173
- selfName,
174
- meetingTitle,
175
- iceServers,
176
- relayEnabled,
177
- hostUid,
178
- initialForceMuteOnJoin,
179
- initialHasPassword,
180
- initialWaitingRoomEnabled,
181
- media,
182
- onLeave,
183
- }: CallViewProps) {
184
- const [peerId] = useState(() => newPeerId(selfUid));
185
- const [connectError, setConnectError] = useState<string | null>(null);
186
- const [participants, setParticipants] = useState<MeshParticipant[]>([]);
187
- const [remoteStreams, setRemoteStreams] = useState<Record<string, MediaStream>>({});
188
- const [levels, setLevels] = useState<Record<string, number>>({});
189
- const [presenterUid, setPresenterUid] = useState<string | undefined>(undefined);
190
- const [screenStream, setScreenStream] = useState<MediaStream | null>(null);
191
- const [screenTransform, setScreenTransform] = useState<ScreenTransformState>(NO_SCREEN_TRANSFORM);
192
- const [handRaised, setHandRaised] = useState(false);
193
- const [viewMode, setViewMode] = useState<CallViewMode>("grid");
194
- const [pinnedUid, setPinnedUid] = useState<string | undefined>(undefined);
195
- const [activeSpeakerUid, setActiveSpeakerUid] = useState<string | undefined>(undefined);
196
- const [reactions, setReactions] = useState<Reaction[]>([]);
197
- const [announcement, setAnnouncement] = useState("");
198
- const [audioBlocked, setAudioBlocked] = useState(false);
199
- const [audioNonce, setAudioNonce] = useState(0);
200
- const [signalingReady, setSignalingReady] = useState(false);
201
- const [drawerOpen, setDrawerOpen] = useState(false);
202
- const [forceMuteOnJoin, setForceMuteOnJoinState] = useState(!!initialForceMuteOnJoin);
203
- const [hasPassword, setHasPassword] = useState(!!initialHasPassword);
204
- const [waitingRoomEnabled, setWaitingRoomEnabledState] = useState(!!initialWaitingRoomEnabled);
205
- const [waitingParticipants, setWaitingParticipants] = useState<WaitingParticipant[]>([]);
206
- const [talkingStickActive, setTalkingStickActive] = useState(false);
207
- const [talkingStickHolder, setTalkingStickHolder] = useState<string | undefined>(undefined);
208
-
209
- const managerRef = useRef<MeshConnectionManager | null>(null);
210
- /** The raw capture from `getDisplayMedia()` - only ever used to stop it (releasing the OS's own share
211
- * indicator) and to notice the participant ending the share from the browser's own UI (`track.onended`). What's
212
- * actually shown and sent is `screenTransformRef`'s output (`screenStream` state) - see `handleToggleShare()`. */
213
- const screenStreamRef = useRef<MediaStream | null>(null);
214
- const screenTransformRef = useRef<ScreenTransformProcessor | null>(null);
215
- const levelMetersRef = useRef<Record<string, LevelMeterHandle>>({});
216
- const reactionSeqRef = useRef(0);
217
- const reactionTimersRef = useRef(new Set<ReturnType<typeof setTimeout>>());
218
- /** The latest `media`, for the mesh-event handler below - that effect runs once at mount (see its own comment),
219
- * so it would otherwise see only the mic state/toggle function from that first render. */
220
- const mediaRef = useRef(media);
221
- useEffect(() => {
222
- mediaRef.current = media;
223
- }, [media]);
224
- const isPresenting = !!screenStream;
225
-
226
- const showReaction = useCallback((emoji: string, name: string) => {
227
- const id = ++reactionSeqRef.current;
228
- setReactions((prev) => [...prev.slice(-(MAX_REACTIONS - 1)), { id, emoji, name }]);
229
- const timer = setTimeout(() => {
230
- reactionTimersRef.current.delete(timer);
231
- setReactions((prev) => prev.filter((reaction) => reaction.id !== id));
232
- }, REACTION_MS);
233
- reactionTimersRef.current.add(timer);
234
- }, []);
235
-
236
- useEffect(() => {
237
- let cancelled = false;
238
- const client = new GuestSignalingClient({ channel, token });
239
- const manager = new MeshConnectionManager({
240
- selfUid,
241
- peerId,
242
- selfName,
243
- iceServers,
244
- channel: client,
245
- createPeerConnection: createBrowserPeerConnection,
246
- relay: relayEnabled ? createRelay(channel, peerId) : undefined,
247
- localAudioTrack: media.audioTrack,
248
- localVideoTrack: media.videoTrack,
249
- localState: { audioOn: media.micOn, videoOn: media.cameraOn },
250
- });
251
- managerRef.current = manager;
252
-
253
- const unsubscribe = manager.onEvent((event) => {
254
- switch (event.type) {
255
- case "participant-joined":
256
- setParticipants((prev) => [...prev.filter((p) => p.uid !== event.participant.uid), event.participant]);
257
- return;
258
- case "participant-updated":
259
- setParticipants((prev) => prev.map((p) => (p.uid === event.participant.uid ? event.participant : p)));
260
- return;
261
- case "participant-left":
262
- setParticipants((prev) => prev.filter((p) => p.uid !== event.uid));
263
- setRemoteStreams((prev) => {
264
- const { [event.uid]: _removed, ...rest } = prev;
265
- return rest;
266
- });
267
- stopLevelMeter(levelMetersRef, event.uid);
268
- setLevels((prev) => {
269
- const { [event.uid]: _removed, ...rest } = prev;
270
- return rest;
271
- });
272
- setPinnedUid((prev) => (prev === event.uid ? undefined : prev));
273
- return;
274
- case "remote-stream":
275
- setRemoteStreams((prev) => ({ ...prev, [event.uid]: event.stream }));
276
- startTrackingLevel(levelMetersRef, event.uid, event.stream, (level) =>
277
- setLevels((prev) => ({ ...prev, [event.uid]: level })),
278
- );
279
- return;
280
- case "hand-raised":
281
- playRaisedHandChime();
282
- setAnnouncement(`${event.name} raised a hand`);
283
- return;
284
- case "reaction":
285
- showReaction(event.emoji, event.name);
286
- return;
287
- case "presenter-changed":
288
- setPresenterUid(event.uid);
289
- if (event.uid !== peerId && screenStreamRef.current) {
290
- // Lost a presenter-claim collision (see `MeshConnectionManager`'s doc comment) - stop our
291
- // own capture without re-sending a release the manager already handled internally.
292
- stopLocalPresentation(false);
293
- }
294
- return;
295
- case "mute-requested":
296
- if (mediaRef.current.micOn) {
297
- void mediaRef.current.toggleMic();
298
- }
299
- return;
300
- case "kicked":
301
- onLeave("The host removed you from this call.");
302
- return;
303
- case "talking-stick-changed":
304
- setTalkingStickActive(event.active);
305
- setTalkingStickHolder(event.holder);
306
- return;
307
- }
308
- });
309
-
310
- client
311
- .connect()
312
- .then(() => {
313
- if (!cancelled) {
314
- manager.start();
315
- setSignalingReady(true);
316
- }
317
- })
318
- .catch((err: Error) => {
319
- if (!cancelled) {
320
- setConnectError(err.message);
321
- }
322
- });
323
-
324
- // A closing tab doesn't unmount React, so say goodbye explicitly - otherwise everyone else keeps a tile for a
325
- // participant who is gone.
326
- const onPageHide = () => manager.stop();
327
- window.addEventListener("pagehide", onPageHide);
328
-
329
- const timers = reactionTimersRef.current;
330
- return () => {
331
- cancelled = true;
332
- window.removeEventListener("pagehide", onPageHide);
333
- unsubscribe();
334
- manager.stop();
335
- client.close();
336
- for (const handle of Object.values(levelMetersRef.current)) {
337
- handle.stop();
338
- }
339
- levelMetersRef.current = {};
340
- // Only the screen capture is this view's to stop - the camera and microphone belong to the page, which
341
- // releases them when the participant leaves.
342
- stopStream(screenStreamRef.current);
343
- screenTransformRef.current?.stop();
344
- for (const timer of timers) {
345
- clearTimeout(timer);
346
- }
347
- timers.clear();
348
- };
349
- // Deliberately runs once - the call's identity (channel/token/selfUid) never changes for the life of this
350
- // component; a real identity change is a new call, which unmounts/remounts this view from `[token].tsx`.
351
- }, []);
352
-
353
- // What is sent follows what the participant has: the camera or the shared screen, and the microphone.
354
- useEffect(() => {
355
- managerRef.current?.setLocalTrack("audio", media.audioTrack);
356
- }, [media.audioTrack]);
357
- useEffect(() => {
358
- managerRef.current?.setLocalTrack("video", screenStream?.getVideoTracks()[0] ?? media.videoTrack);
359
- }, [media.videoTrack, screenStream]);
360
- useEffect(() => {
361
- managerRef.current?.setLocalState({ audioOn: media.micOn, videoOn: media.cameraOn || isPresenting, handRaised });
362
- }, [media.micOn, media.cameraOn, isPresenting, handRaised]);
363
-
364
- // Talking-stick mode forces exactly one microphone on at a time: whoever just became the holder (including the
365
- // host, on activation) is unmuted; everyone else is muted. Unlike `mute-requested`'s one-time nudge, this is
366
- // paired with disabling the mic button itself (`micLocked`, passed to `CallControls` below) while it applies,
367
- // so it isn't merely a suggestion - though still cooperative at the signaling layer, like every other
368
- // moderation feature here (see `MeshConnectionManager`'s doc comment). Turning the mode off touches nobody's
369
- // mic - states are left exactly as they are, the same "never retroactive" posture as every other host toggle.
370
- const selfHasTalkingStick = talkingStickActive && talkingStickHolder === peerId;
371
- const micLocked = talkingStickActive && !selfHasTalkingStick;
372
- useEffect(() => {
373
- if (!talkingStickActive) {
374
- return;
375
- }
376
- if (selfHasTalkingStick && !mediaRef.current.micOn) {
377
- void mediaRef.current.toggleMic();
378
- } else if (!selfHasTalkingStick && mediaRef.current.micOn) {
379
- void mediaRef.current.toggleMic();
380
- }
381
- }, [talkingStickActive, selfHasTalkingStick]);
382
-
383
- useEffect(() => {
384
- if (!presenterUid) {
385
- setActiveSpeakerUid((prev) => pickActiveSpeaker(levels, prev));
386
- }
387
- }, [levels, presenterUid]);
388
-
389
- function stopLocalPresentation(alsoRelease: boolean): void {
390
- if (alsoRelease) {
391
- managerRef.current?.releasePresenter();
392
- }
393
- stopStream(screenStreamRef.current);
394
- screenStreamRef.current = null;
395
- screenTransformRef.current?.stop();
396
- screenTransformRef.current = null;
397
- // The sync effect above swaps the camera back in as the outgoing video track. The rotation/flip is reset
398
- // for the next share - each share starts from the capture the browser hands back, not the last one's fix.
399
- setScreenStream(null);
400
- setScreenTransform(NO_SCREEN_TRANSFORM);
401
- }
402
-
403
- async function handleToggleShare() {
404
- if (screenStreamRef.current) {
405
- stopLocalPresentation(true);
406
- return;
407
- }
408
- const result = await requestDisplayMedia();
409
- if (!result.ok) {
410
- setConnectError(result.error.message);
411
- return;
412
- }
413
- const claimed = managerRef.current?.claimPresenter() ?? false;
414
- if (!claimed) {
415
- stopStream(result.value);
416
- return;
417
- }
418
- screenStreamRef.current = result.value;
419
- const rawTrack = result.value.getVideoTracks()[0];
420
- rawTrack.onended = () => stopLocalPresentation(true);
421
- try {
422
- // Runs the capture through the rotate/flip processor before anyone (presenter included) sees it - see
423
- // ScreenTransform.ts's doc comment on why window capture sometimes needs this correction.
424
- const processor = new ScreenTransformProcessor({ source: rawTrack, state: screenTransform });
425
- screenTransformRef.current = processor;
426
- setScreenStream(new MediaStream([processor.track]));
427
- } catch {
428
- // This browser can't draw the correction - share the raw capture rather than not sharing at all; the
429
- // rotate/flip buttons simply won't do anything (screenTransformRef stays null).
430
- setScreenStream(result.value);
431
- }
432
- }
433
-
434
- function handleRotateScreen() {
435
- setScreenTransform((prev) => {
436
- const next = rotateClockwise(prev);
437
- screenTransformRef.current?.setState(next);
438
- return next;
439
- });
440
- }
441
-
442
- function handleFlipScreen() {
443
- setScreenTransform((prev) => {
444
- const next = { ...prev, flipped: !prev.flipped };
445
- screenTransformRef.current?.setState(next);
446
- return next;
447
- });
448
- }
449
-
450
- function handleToggleHand() {
451
- setHandRaised((prev) => !prev);
452
- }
453
-
454
- function handleReaction(emoji: string) {
455
- if (managerRef.current?.sendReaction(emoji)) {
456
- showReaction(emoji, "You");
457
- }
458
- }
459
-
460
- function togglePin(uid: string) {
461
- setPinnedUid((prev) => (prev === uid ? undefined : uid));
462
- }
463
-
464
- /** Asks `peerUid`'s participant to mute - the host-only button in the participants drawer calls this
465
- * directly, with no confirmation (unlike removing someone, muting is easily undone by the participant
466
- * themselves). */
467
- function handleMuteParticipant(peerUid: string) {
468
- managerRef.current?.sendMuteRequest(peerUid);
469
- }
470
-
471
- /** Removes `peerUid`'s participant from the call: the cooperative signal (immediate, so a cooperating client
472
- * leaves without waiting on the network round trip below) and the enforced server-side revoke together - see
473
- * `kickParticipant()`'s own doc comment for why both matter. The REST call's failure is swallowed rather than
474
- * surfaced: the host already asked them to leave, and there is no useful recovery action to offer from here
475
- * for what is almost always a transient network issue. */
476
- function handleKickParticipant(peerUid: string) {
477
- managerRef.current?.sendKick(peerUid);
478
- kickParticipant(channel, accountUidOf(peerUid)).catch(() => undefined);
479
- }
480
-
481
- /** Flips the host's "mute new participants on join" setting: applied optimistically (the toggle itself is the
482
- * only feedback this small a control needs) and reverted if the save fails, so the drawer never keeps claiming
483
- * a setting that isn't actually persisted. Takes effect for whoever joins next - never retroactive, see
484
- * `setForceMuteOnJoin()`'s own doc comment. */
485
- function handleToggleForceMuteOnJoin() {
486
- const next = !forceMuteOnJoin;
487
- setForceMuteOnJoinState(next);
488
- apiSetForceMuteOnJoin(channel, next).catch(() => setForceMuteOnJoinState(!next));
489
- }
490
-
491
- /** Sets, changes, or removes (`password: null`) the host's join password - unlike the force-mute toggle, this
492
- * is not applied optimistically: the drawer's own `PasswordSection` awaits the result itself (clearing its
493
- * input on success, showing an inline error on failure), so there is nothing here to revert. `hasPassword`
494
- * updates only once the save actually succeeds. */
495
- async function handleSetPassword(password: string | null): Promise<void> {
496
- await setMeetingPassword(channel, password);
497
- setHasPassword(!!password);
498
- }
499
-
500
- /** Flips the host's waiting-room setting - same optimistic-and-revert shape as `handleToggleForceMuteOnJoin()`,
501
- * for the same reason. Takes effect for whoever joins next; nobody already admitted is retroactively gated. */
502
- function handleToggleWaitingRoomEnabled() {
503
- const next = !waitingRoomEnabled;
504
- setWaitingRoomEnabledState(next);
505
- apiSetWaitingRoomEnabled(channel, next).catch(() => setWaitingRoomEnabledState(!next));
506
- }
507
-
508
- /** Starts or stops talking-stick mode - host-only, see this module's doc comment. Starting makes the host the
509
- * initial holder; stopping clears the holder too, leaving mic states exactly as they are rather than
510
- * retroactively unmuting anyone. */
511
- function handleToggleTalkingStick() {
512
- managerRef.current?.setTalkingStick(!talkingStickActive, talkingStickActive ? undefined : peerId);
513
- }
514
-
515
- /** Hands the talking stick to `uid` (a participant's `MeshParticipant.uid`, or this tab's own `peerId` for the
516
- * host taking it back) - host-only, called from the participants drawer's per-row "Give stick" button. */
517
- function handleGiveTalkingStick(uid: string) {
518
- managerRef.current?.setTalkingStick(true, uid);
519
- }
520
-
521
- /** Admits one pending request - optimistically removed from the drawer's own list (the next poll would drop it
522
- * anyway, once the requester's own next poll completes their join; removing it here just avoids the visible
523
- * delay). Re-added if the save itself fails, so a failure doesn't silently lose the request from the list. */
524
- function handleAdmitParticipant(uid: string) {
525
- setWaitingParticipants((prev) => prev.filter((p) => p.uid !== uid));
526
- admitParticipant(channel, uid).catch(() => void listWaitingParticipants(channel).then(setWaitingParticipants, () => undefined));
527
- }
528
-
529
- /** Denies one pending request - same optimistic-removal shape as `handleAdmitParticipant()`. */
530
- function handleDenyParticipant(uid: string) {
531
- setWaitingParticipants((prev) => prev.filter((p) => p.uid !== uid));
532
- denyParticipant(channel, uid).catch(() => void listWaitingParticipants(channel).then(setWaitingParticipants, () => undefined));
533
- }
534
-
535
- function handleAudioBlocked() {
536
- setAudioBlocked(true);
537
- }
538
-
539
- function handleResumeAudio() {
540
- setAudioBlocked(false);
541
- setAudioNonce((prev) => prev + 1);
542
- }
543
-
544
- // A participant's `uid` is their tab-scoped peer id (`<account uid>~<random>` - see `newPeerId()`), while
545
- // `hostUid` is the plain account uid `PublicVideoMeeting.hostUid` names - so telling the host's own tab(s) apart
546
- // needs the prefix match, not equality. Purely a client-side check (see `hostUid`'s own doc comment on this
547
- // prop): nobody's tab is prevented from claiming it, only from being shown host controls for it.
548
- const isHost = !!hostUid && selfUid === hostUid;
549
- const isParticipantHost = (uid: string): boolean => !!hostUid && uid.startsWith(`${hostUid}~`);
550
-
551
- // Refreshes the drawer's own waiting-room list while the host has the drawer open - there is no push signal
552
- // for a newly filed admission request, so polling is the only way the list stays current without the host
553
- // having to close and reopen the drawer. Stops (and the list is dropped) the moment either condition ends.
554
- useEffect(() => {
555
- if (!isHost || !drawerOpen) {
556
- setWaitingParticipants([]);
557
- return;
558
- }
559
- let cancelled = false;
560
- const poll = () => {
561
- listWaitingParticipants(channel).then((list) => {
562
- if (!cancelled) {
563
- setWaitingParticipants(list);
564
- }
565
- }, () => undefined);
566
- };
567
- poll();
568
- const interval = setInterval(poll, DEFAULT_WAITING_POLL_MS);
569
- return () => {
570
- cancelled = true;
571
- clearInterval(interval);
572
- };
573
- }, [isHost, drawerOpen, channel]);
574
- const otherUids = participants.map((p) => p.uid);
575
- // A pin or an active-speaker pick can name someone who has just left, until the state catches up.
576
- const stillHere = (uid: string | undefined) => (uid && otherUids.includes(uid) ? uid : undefined);
577
- const showFocusLayout = !!presenterUid || viewMode === "focus";
578
- const mainUid = showFocusLayout
579
- ? computeMainUid({ presenterUid, pinnedUid: stillHere(pinnedUid), activeSpeakerUid: stillHere(activeSpeakerUid), otherUids })
580
- : undefined;
581
- const presenterName = presenterUid ? (presenterUid === peerId ? selfName : (participants.find((p) => p.uid === presenterUid)?.name ?? "Someone")) : undefined;
582
- const raisedNames = [...(handRaised ? ["You"] : []), ...participants.filter((p) => p.handRaised).map((p) => p.name)];
583
- // Absent (not "Someone") once the holder has left the call - see `MeshConnectionManager`'s doc comment on why
584
- // nothing reassigns it automatically; the header then says so rather than naming someone no longer here.
585
- const talkingStickHolderName = !talkingStickActive
586
- ? undefined
587
- : selfHasTalkingStick
588
- ? "You"
589
- : participants.find((p) => p.uid === talkingStickHolder)?.name;
590
- const alone = participants.length === 0;
591
- // This participant has no working link yet: the signaling channel is still opening, or there is someone in the call
592
- // and every connection to them is still being made. Once any one is up (or nobody else is here) it is not shown,
593
- // so someone already in the call is not told they are "connecting" each time a newcomer arrives.
594
- const selfConnecting = !connectError && (!signalingReady || (!alone && participants.every((p) => p.transport === "connecting")));
595
-
596
- const remoteTile = (p: MeshParticipant, className?: string) => (
597
- <ParticipantTile
598
- key={p.uid}
599
- name={p.name}
600
- stream={remoteStreams[p.uid] ?? null}
601
- cameraOff={!p.videoOn}
602
- micMuted={!p.audioOn}
603
- handRaised={p.handRaised}
604
- transport={p.transport}
605
- status={p.transport === "connecting" ? "Awaiting connection…" : undefined}
606
- isFocused={mainUid === p.uid}
607
- contain={presenterUid === p.uid}
608
- className={className}
609
- onClick={() => togglePin(p.uid)}
610
- />
611
- );
612
-
613
- const selfTile = (className?: string) => (
614
- <ParticipantTile
615
- name={selfName}
616
- stream={media.videoStream}
617
- isLocal
618
- cameraOff={!media.cameraOn}
619
- micMuted={!media.micOn}
620
- handRaised={handRaised}
621
- status={selfConnecting ? "Connecting…" : undefined}
622
- // A custom background is a fixed picture, not a live reflection - mirroring it would show it backwards
623
- // to no one but the participant themselves (see `_ParticipantTile.tsx`'s doc comment).
624
- mirrored={media.filters.background !== "image"}
625
- className={className}
626
- />
627
- );
628
-
629
- const mainParticipant = participants.find((p) => p.uid === mainUid);
630
- const thumbnails = participants.filter((p) => p.uid !== mainUid);
631
-
632
- let stage: React.ReactNode;
633
- if (alone) {
634
- // Nobody else yet: the local participant fills the tile area, presenting or not.
635
- stage = isPresenting ? (
636
- <ParticipantTile name={selfName} stream={screenStream} isLocal contain isFocused className="h-full" />
637
- ) : (
638
- selfTile("h-full")
639
- );
640
- } else if (showFocusLayout && (mainParticipant || isPresenting)) {
641
- stage = (
642
- <div className="h-full flex flex-col gap-2">
643
- <div className="flex-1 min-h-0" data-testid="main-tile">
644
- {mainParticipant ? (
645
- remoteTile(mainParticipant, "h-full")
646
- ) : (
647
- <ParticipantTile name={selfName} stream={screenStream} isLocal contain isFocused className="h-full" />
648
- )}
649
- </div>
650
- {thumbnails.length > 0 && (
651
- <div className="flex gap-2 overflow-x-auto h-24 shrink-0" data-testid="thumbnails">
652
- {thumbnails.map((p) => (
653
- <div key={p.uid} className="w-36 shrink-0">
654
- {remoteTile(p, "h-full")}
655
- </div>
656
- ))}
657
- </div>
658
- )}
659
- </div>
660
- );
661
- } else {
662
- stage = (
663
- <div className="h-full grid gap-2 auto-rows-fr [grid-template-columns:repeat(auto-fit,minmax(min(100%,320px),1fr))]">
664
- {participants.map((p) => remoteTile(p, "h-full"))}
665
- </div>
666
- );
667
- }
668
-
669
- return (
670
- <div className="fixed inset-0 z-50 flex bg-[#202124] text-white overflow-hidden">
671
- <style>{`@keyframes meet-float { 0% { transform: translateY(0) scale(.6); opacity: 0; } 12% { opacity: 1; transform: translateY(-4vh) scale(1); } 100% { transform: translateY(-45vh) scale(1); opacity: 0; } }`}</style>
672
- <div className="relative flex-1 min-w-0 flex flex-col overflow-hidden">
673
- <header className="shrink-0 flex items-center justify-between gap-3 px-4 py-3">
674
- <h1 className="min-w-0 truncate text-base font-medium">{meetingTitle}</h1>
675
- <div className="flex items-center gap-2 shrink-0">
676
- {raisedNames.length > 0 && (
677
- <span className="max-w-[45vw] truncate px-3 py-1.5 rounded-full bg-[#a8c7fa] text-[#062e6f] text-sm font-medium" data-testid="raised-hands">
678
- ✋ {raisedNames.join(", ")}
679
- </span>
680
- )}
681
- {talkingStickActive && (
682
- <span className="max-w-[45vw] truncate px-3 py-1.5 rounded-full bg-[#a8c7fa] text-[#062e6f] text-sm font-medium" data-testid="talking-stick-status">
683
- 🎙️ {talkingStickHolderName ? `${talkingStickHolderName} ${talkingStickHolderName === "You" ? "have" : "has"} the floor` : "Waiting for the host to choose a speaker"}
684
- </span>
685
- )}
686
- {isHost && (
687
- <button
688
- type="button"
689
- className={`w-9 h-9 flex items-center justify-center rounded-full focus:outline-none focus-visible:ring-2 focus-visible:ring-white/80 ${talkingStickActive ? "bg-[#a8c7fa] text-[#062e6f] hover:bg-[#8ab4f8]" : "bg-[#3c4043] hover:bg-[#4b4f53]"}`}
690
- aria-label={talkingStickActive ? "End talking stick" : "Talking stick"}
691
- aria-pressed={talkingStickActive}
692
- title={talkingStickActive ? "End talking stick" : "Start talking stick mode"}
693
- onClick={handleToggleTalkingStick}
694
- >
695
- <BatonIcon />
696
- </button>
697
- )}
698
- <button
699
- type="button"
700
- className="px-3 py-1.5 rounded-full bg-[#3c4043] hover:bg-[#4b4f53] text-sm focus:outline-none focus-visible:ring-2 focus-visible:ring-white/80"
701
- aria-label={`${participants.length + 1} participants`}
702
- aria-haspopup="dialog"
703
- aria-expanded={drawerOpen}
704
- onClick={() => setDrawerOpen((prev) => !prev)}
705
- >
706
- {participants.length + 1}
707
- </button>
708
- </div>
709
- </header>
710
- {connectError && (
711
- <div role="alert" className="shrink-0 mx-3 mb-2 px-3 py-2 rounded-lg bg-[#601410] text-[#f9dedc] text-sm">
712
- {connectError}
713
- </div>
714
- )}
715
- {audioBlocked && (
716
- <button
717
- type="button"
718
- className="shrink-0 mx-3 mb-2 px-3 py-2 rounded-lg bg-[#a8c7fa] text-[#062e6f] text-sm font-medium"
719
- onClick={handleResumeAudio}
720
- >
721
- Click here to turn on sound
722
- </button>
723
- )}
724
- <main className="relative flex-1 min-h-0 px-3 pb-3">
725
- {stage}
726
- {presenterName && presenterUid !== peerId && (
727
- <p className="absolute top-2 left-5 px-2 py-0.5 rounded bg-black/60 text-sm">{presenterName} is presenting</p>
728
- )}
729
- </main>
730
- <footer className="shrink-0 pt-1 pb-[max(0.75rem,env(safe-area-inset-bottom))]">
731
- <CallControls
732
- media={media}
733
- selfName={selfName}
734
- participants={participants}
735
- isPresenting={isPresenting}
736
- presentingElsewhereName={presenterUid && presenterUid !== peerId ? presenterName : undefined}
737
- onToggleShare={() => void handleToggleShare()}
738
- screenTransform={screenTransform}
739
- onRotateScreen={handleRotateScreen}
740
- onFlipScreen={handleFlipScreen}
741
- handRaised={handRaised}
742
- onToggleHand={handleToggleHand}
743
- onReaction={handleReaction}
744
- viewMode={viewMode}
745
- onToggleViewMode={() => setViewMode((prev) => (prev === "grid" ? "focus" : "grid"))}
746
- micLocked={micLocked}
747
- onLeave={onLeave}
748
- />
749
- </footer>
750
-
751
- {!alone && (
752
- <div
753
- className="absolute z-10 right-3 top-14 w-28 sm:top-auto sm:bottom-24 sm:w-52 aspect-video shadow-xl xl:right-4 xl:bottom-4"
754
- data-testid="self-view"
755
- >
756
- {selfTile("h-full")}
757
- </div>
758
- )}
759
-
760
- <div className="pointer-events-none absolute left-4 bottom-28 w-40 h-[45vh]" aria-hidden="true">
761
- {reactions.map((reaction) => (
762
- <div
763
- key={reaction.id}
764
- className="absolute bottom-0 flex flex-col items-center"
765
- style={{ left: `${(reaction.id % 4) * 36}px`, animation: `meet-float ${REACTION_MS}ms ease-out forwards` }}
766
- data-testid="reaction"
767
- >
768
- <span className="text-4xl leading-none">{reaction.emoji}</span>
769
- <span className="mt-1 px-1.5 rounded-full bg-[#a8c7fa] text-[#062e6f] text-xs">{reaction.name}</span>
770
- </div>
771
- ))}
772
- </div>
773
-
774
- {participants.map((p) => {
775
- const stream = remoteStreams[p.uid];
776
- return stream ? <RemoteAudio key={`${p.uid}:${audioNonce}`} stream={stream} onBlocked={handleAudioBlocked} /> : null;
777
- })}
778
- <div role="status" aria-live="polite" className="sr-only">
779
- {announcement}
780
- </div>
781
- </div>
782
-
783
- {drawerOpen && (
784
- <ParticipantsDrawer
785
- selfName={selfName}
786
- micOn={media.micOn}
787
- handRaised={handRaised}
788
- participants={participants}
789
- isSelfHost={isHost}
790
- isParticipantHost={isParticipantHost}
791
- onMute={handleMuteParticipant}
792
- onKick={handleKickParticipant}
793
- forceMuteOnJoin={forceMuteOnJoin}
794
- onToggleForceMuteOnJoin={handleToggleForceMuteOnJoin}
795
- hasPassword={hasPassword}
796
- onSetPassword={handleSetPassword}
797
- waitingRoomEnabled={waitingRoomEnabled}
798
- onToggleWaitingRoomEnabled={handleToggleWaitingRoomEnabled}
799
- waitingParticipants={waitingParticipants}
800
- onAdmit={handleAdmitParticipant}
801
- onDeny={handleDenyParticipant}
802
- talkingStickActive={talkingStickActive}
803
- selfPeerId={peerId}
804
- talkingStickHolder={talkingStickHolder}
805
- onGiveTalkingStick={handleGiveTalkingStick}
806
- onClose={() => setDrawerOpen(false)}
807
- />
808
- )}
809
- </div>
810
- );
811
- }
812
-
813
- /** Plays one remote participant's stream through a hidden `<audio>` element. `onBlocked` fires if the browser
814
- * refuses to start it (an autoplay policy) - the view then asks for a click. */
815
- function RemoteAudio({ stream, onBlocked }: { stream: MediaStream; onBlocked: () => void }) {
816
- const audioRef = useRef<HTMLAudioElement>(null);
817
- useEffect(() => {
818
- const element = audioRef.current!;
819
- element.srcObject = stream;
820
- void Promise.resolve(element.play()).catch(onBlocked);
821
- }, [stream, onBlocked]);
822
- return <audio ref={audioRef} autoPlay data-testid="remote-audio" />;
823
- }
824
-
825
- /** The WebSocket media relay for this call, or `undefined` when this browser cannot run it (no WebCodecs) - the
826
- * mesh then never falls back to it, and says so by marking an unreachable participant `"failed"`. */
827
- function createRelay(meetingUid: string, peerId: string): RelayTransportLike | undefined {
828
- const relay = createRelayTransport({ meetingUid, peerId });
829
- return relay.supported ? relay : undefined;
830
- }
831
-
832
- function stopLevelMeter(ref: React.MutableRefObject<Record<string, LevelMeterHandle>>, uid: string): void {
833
- ref.current[uid]?.stop();
834
- delete ref.current[uid];
835
- }
836
-
837
- function startTrackingLevel(
838
- ref: React.MutableRefObject<Record<string, LevelMeterHandle>>,
839
- uid: string,
840
- stream: MediaStream,
841
- onLevel: (level: number) => void,
842
- ): void {
843
- stopLevelMeter(ref, uid);
844
- const handle = startLevelMeter(stream, onLevel);
845
- if (handle) {
846
- ref.current[uid] = handle;
847
- }
848
- }
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2026 Jean-Philippe Steinmetz. All rights reserved.
3
+ // SPDX-License-Identifier: MPL-2.0
4
+ ///////////////////////////////////////////////////////////////////////////////
5
+ /**
6
+ * The in-call view: connects the full-mesh `MeshConnectionManager` to the meeting's signaling channel
7
+ * (`GuestSignalingClient`), mirrors its events into React state, and renders the grid/focused/presentation
8
+ * layouts - see `apps/shared/webrtc/MeshConnectionManager.ts`'s doc comment for the who-calls-whom and
9
+ * single-presenter rules this view relies on rather than re-deciding. These reusable, non-UI modules live under
10
+ * `apps/shared/` rather than this plugin's backend `src/` - `tsconfig.apps.json` builds `apps/` as its own
11
+ * program rooted at `apps/`, which cannot reference files outside it (`TS6059`), matching `booking-plugin`'s own
12
+ * identical `apps/shared/` convention for reusable frontend-only code.
13
+ *
14
+ * ## Fitting the window
15
+ *
16
+ * The call fills the viewport (`fixed inset-0`) as three rows - a slim header, the tiles (which take whatever room
17
+ * is left and never scroll the page), and the control bar pinned to the bottom - instead of being laid out inside
18
+ * the branded page shell, whose header, footer and padding pushed it off the screen. The local participant's own
19
+ * tile is a small tile in the bottom-right corner while anyone else is in the call (above the control bar on a narrower
20
+ * window, and at the top on a phone, where the bar wraps onto a second row), and fills the tile area while they are
21
+ * alone.
22
+ *
23
+ * ## Who is heard
24
+ *
25
+ * A tile never plays sound. Each remote stream is played by its own hidden `<audio>` element, so a participant is
26
+ * heard whether or not their camera is on and whichever layout is showing. If the browser refuses to start the
27
+ * audio (an autoplay policy), a banner asks for a click, which is the gesture that allows it.
28
+ *
29
+ * ## Layout precedence
30
+ *
31
+ * `computeMainUid()` decides who is shown large, in this order: an active presenter always wins (presentation
32
+ * mode, forcing a focused-like layout regardless of `viewMode`); otherwise a manually pinned participant; else the
33
+ * auto-detected active speaker (silently ignored while presenting, so it never fights the presenter for the main
34
+ * slot); else, in focus mode with nobody yet speaking, the first other participant - so focus mode never shows an
35
+ * empty main slot once someone else has joined.
36
+ *
37
+ * ## Talking stick mode
38
+ *
39
+ * A host-only navbar toggle (`handleToggleTalkingStick()`) that, once on, lets at most one participant's microphone
40
+ * be unmuted at a time - the host assigns who from the participants drawer (`handleGiveTalkingStick()`), everyone
41
+ * else sees a header chip naming who currently holds it. Purely an in-call runtime state signaled peer-to-peer over
42
+ * the mesh (`MeshConnectionManager.setTalkingStick()`/`"talking-stick-changed"`), the same shape as `mute-request`/
43
+ * `kicked`: no `VideoMeeting` field, no backend route, and only as "enforced" as a cooperating client makes it (see
44
+ * `types.ts`'s `SignalMessage` doc comment on why the signaling layer has no host identity to check against). What
45
+ * *is* new here versus `mute-request`'s one-time nudge: becoming or ceasing to be the holder force-toggles this
46
+ * tab's own mic to match (`selfHasTalkingStick`'s effect, below), and the mic button itself is disabled
47
+ * (`micLocked`, passed to `CallControls`) the whole time a non-holder would otherwise be able to tap it straight
48
+ * back on - a real restriction for as long as this tab's own UI is the one in front of the participant, not just a
49
+ * suggestion. Turning the mode off touches nobody's mic state, matching every other host toggle's "never
50
+ * retroactive" posture. A holder who leaves the call is not specially reassigned - see
51
+ * `MeshConnectionManager`'s own doc comment on why that needs no extra code - the header chip then says nobody has
52
+ * the floor, until the host picks someone (or themselves) again.
53
+ *
54
+ * ## The navbar's "…" menu
55
+ *
56
+ * A catch-all for controls that don't need a dedicated button of their own: "Diagnostics" opens
57
+ * `_DiagnosticsWindow.tsx` (a persistent, draggable window - not a popover like everything else here, since
58
+ * troubleshooting a connection is something a participant wants to keep open while they poke at the rest of the
59
+ * call, not something that closes the moment they click elsewhere); "Connection method" is a four-way choice
60
+ * (`TransportMode` - see `MeshConnectionManager.setTransportMode()`'s own doc comment) for forcing how *this tab's*
61
+ * media reaches everyone else, independent of what anyone else has chosen; "Settings" (host-only) opens
62
+ * `_SettingsModal.tsx`, which used to be the participants drawer's own header (mute-on-join, the join password, the
63
+ * waiting-room toggle) - moved out because none of those are about *who* is in the call, which is what the drawer
64
+ * is for. The menu itself closes on Escape, a click outside it, or picking an item; the windows it opens do not
65
+ * close along with it.
66
+ */
67
+ import React, { useCallback, useEffect, useRef, useState } from "react";
68
+ import { playRaisedHandChime } from "../shared/media/chime.js";
69
+ import { pickActiveSpeaker } from "../shared/media/activeSpeaker.js";
70
+ import { startLevelMeter, type LevelMeterHandle } from "../shared/media/levelMeter.js";
71
+ import { requestDisplayMedia, stopStream } from "../shared/media/deviceMedia.js";
72
+ import { NO_SCREEN_TRANSFORM, ScreenTransformProcessor, rotateClockwise, type ScreenTransformState } from "../shared/media/filters/ScreenTransform.js";
73
+ import type { LocalMedia } from "../shared/media/useLocalMedia.js";
74
+ import { createBrowserPeerConnection } from "../shared/webrtc/realPeerConnection.js";
75
+ import { MeshConnectionManager } from "../shared/webrtc/MeshConnectionManager.js";
76
+ import type { MeshParticipant, RelayTransportLike, TransportMode } from "../shared/webrtc/types.js";
77
+ import { createRelayTransport } from "../shared/relay/RelayTransport.js";
78
+ import { GuestSignalingClient } from "../shared/push/GuestSignalingClient.js";
79
+ import CallControls, { type CallViewMode } from "./_CallControls.js";
80
+ import { BatonIcon, OverflowIcon } from "./_icons.js";
81
+ import {
82
+ admitParticipant,
83
+ denyParticipant,
84
+ kickParticipant,
85
+ listWaitingParticipants,
86
+ setForceMuteOnJoin as apiSetForceMuteOnJoin,
87
+ setMeetingPassword,
88
+ setWaitingRoomEnabled as apiSetWaitingRoomEnabled,
89
+ type WaitingParticipant,
90
+ } from "./_meetApi.js";
91
+ import DiagnosticsPanel from "./_DiagnosticsPanel.js";
92
+ import DiagnosticsWindow from "./_DiagnosticsWindow.js";
93
+ import ParticipantTile from "./_ParticipantTile.js";
94
+ import ParticipantsDrawer from "./_ParticipantsDrawer.js";
95
+ import SettingsModal from "./_SettingsModal.js";
96
+
97
+ export interface CallViewProps {
98
+ channel: string;
99
+ /** Omitted when the caller already authenticated as a real RapidMX identity in `join()` - there is then no
100
+ * guest token to hand `GuestSignalingClient`, which relies entirely on the browser's own already-existing
101
+ * `jwt` session cookie instead (see its own doc comment). */
102
+ token?: string;
103
+ selfUid: string;
104
+ selfName: string;
105
+ meetingTitle: string;
106
+ iceServers: RTCIceServer[];
107
+ /** Whether the server offers the WebSocket media relay, the last-resort path for a participant neither a direct
108
+ * connection nor the TURN server can reach - `VideoMeetingJoinResult.relayEnabled`. Off when absent. */
109
+ relayEnabled?: boolean;
110
+ /** The meeting's host account uid (`PublicVideoMeeting.hostUid`) - this client's only source of a "host"
111
+ * identity, purely for deciding which controls to show (see `BaseVideoMeetingRoute`'s class doc comment on why
112
+ * this is a client-side-only check, not a server-enforced one for every moderation action). Absent when the
113
+ * server couldn't resolve one, in which case nobody sees host controls. */
114
+ hostUid?: string;
115
+ /** `PublicVideoMeeting.forceMuteOnJoin` as of this call's own `join()` - the starting value for the host's
116
+ * toggle in the settings modal, which this view then owns and keeps current itself (see
117
+ * `handleToggleForceMuteOnJoin()`). Does not update if changed elsewhere while this view is mounted - there is
118
+ * no signal for that today, matching every other meeting-settings field's lack of live sync. */
119
+ initialForceMuteOnJoin?: boolean;
120
+ /** `PublicVideoMeeting.hasPassword` as of this call's own `join()` - the starting value for the host's password
121
+ * section in the settings modal, same "owned and kept current by this view" shape as
122
+ * `initialForceMuteOnJoin`. */
123
+ initialHasPassword?: boolean;
124
+ /** `PublicVideoMeeting.waitingRoomEnabled` as of this call's own `join()` - the starting value for the host's
125
+ * waiting-room toggle, same "owned and kept current by this view" shape as `initialForceMuteOnJoin`. */
126
+ initialWaitingRoomEnabled?: boolean;
127
+ /** The camera and microphone, owned by the page (`[token].tsx`) - the lobby's tracks carried into the call. */
128
+ media: LocalMedia;
129
+ /** Called once the participant leaves, for any reason. `reason` is set only when the call ended without the
130
+ * participant's own action - currently, being kicked by the host - so the caller can show a distinct message
131
+ * instead of the ordinary "you left" one; omitted for an ordinary voluntary leave. */
132
+ onLeave: (reason?: string) => void;
133
+ }
134
+
135
+ /** How long a reaction floats on screen. */
136
+ const REACTION_MS = 4_000;
137
+ /** The most reactions shown at once - a burst beyond this drops the oldest. */
138
+ const MAX_REACTIONS = 12;
139
+ /** How often the host's drawer refreshes its own waiting-room list while open. */
140
+ const DEFAULT_WAITING_POLL_MS = 3_000;
141
+
142
+ /** The "…" menu's "Connection method" options, in display order - see `TransportMode`'s own doc comment. */
143
+ const TRANSPORT_MODE_OPTIONS: { mode: TransportMode; label: string }[] = [
144
+ { mode: "auto", label: "Auto" },
145
+ { mode: "p2p", label: "P2P" },
146
+ { mode: "relay", label: "Relay" },
147
+ { mode: "websocket", label: "WebSocket Relay" },
148
+ ];
149
+
150
+ interface Reaction {
151
+ id: number;
152
+ emoji: string;
153
+ name: string;
154
+ }
155
+
156
+ /** Who is shown large - see this module's doc comment. `undefined` when nobody but the local participant has
157
+ * joined yet (the grid/focus toggle still works, there's just nobody else to focus on). */
158
+ export function computeMainUid(options: {
159
+ presenterUid?: string;
160
+ pinnedUid?: string;
161
+ activeSpeakerUid?: string;
162
+ otherUids: readonly string[];
163
+ }): string | undefined {
164
+ if (options.presenterUid) {
165
+ return options.presenterUid;
166
+ }
167
+ if (options.pinnedUid) {
168
+ return options.pinnedUid;
169
+ }
170
+ if (options.activeSpeakerUid) {
171
+ return options.activeSpeakerUid;
172
+ }
173
+ return options.otherUids[0];
174
+ }
175
+
176
+ /** This tab's identity in the call: the account (or guest) uid plus a random suffix, so the same account joining
177
+ * from two devices - or two tabs - is two participants rather than one that ignores itself. It travels as the
178
+ * `peer` of each message; `from` stays the exact authenticated uid, which the server requires. */
179
+ export function newPeerId(selfUid: string): string {
180
+ return `${selfUid}~${globalThis.crypto?.randomUUID?.() ?? Math.random().toString(36).slice(2)}`;
181
+ }
182
+
183
+ /** The real, server-enforced account uid behind a participant's tab-scoped `MeshParticipant.uid` (`newPeerId()`'s
184
+ * `<account uid>~<random>` shape) - what the kick route's ACL grant is actually keyed by, unlike the tab-scoped id
185
+ * this view otherwise addresses a specific tab with. Neither a real account uid nor a minted guest uid (see
186
+ * `GUEST_UID_PREFIX`/`mintGuestToken()` on the backend) can itself contain "~", so the last one found is always
187
+ * exactly this suffix's own separator. */
188
+ export function accountUidOf(peerUid: string): string {
189
+ const i = peerUid.lastIndexOf("~");
190
+ return i === -1 ? peerUid : peerUid.slice(0, i);
191
+ }
192
+
193
+ export default function CallView({
194
+ channel,
195
+ token,
196
+ selfUid,
197
+ selfName,
198
+ meetingTitle,
199
+ iceServers,
200
+ relayEnabled,
201
+ hostUid,
202
+ initialForceMuteOnJoin,
203
+ initialHasPassword,
204
+ initialWaitingRoomEnabled,
205
+ media,
206
+ onLeave,
207
+ }: CallViewProps) {
208
+ const [peerId] = useState(() => newPeerId(selfUid));
209
+ const [connectError, setConnectError] = useState<string | null>(null);
210
+ const [participants, setParticipants] = useState<MeshParticipant[]>([]);
211
+ const [remoteStreams, setRemoteStreams] = useState<Record<string, MediaStream>>({});
212
+ const [levels, setLevels] = useState<Record<string, number>>({});
213
+ const [presenterUid, setPresenterUid] = useState<string | undefined>(undefined);
214
+ const [screenStream, setScreenStream] = useState<MediaStream | null>(null);
215
+ const [screenTransform, setScreenTransform] = useState<ScreenTransformState>(NO_SCREEN_TRANSFORM);
216
+ const [handRaised, setHandRaised] = useState(false);
217
+ const [viewMode, setViewMode] = useState<CallViewMode>("grid");
218
+ const [pinnedUid, setPinnedUid] = useState<string | undefined>(undefined);
219
+ const [activeSpeakerUid, setActiveSpeakerUid] = useState<string | undefined>(undefined);
220
+ const [reactions, setReactions] = useState<Reaction[]>([]);
221
+ const [announcement, setAnnouncement] = useState("");
222
+ const [audioBlocked, setAudioBlocked] = useState(false);
223
+ const [audioNonce, setAudioNonce] = useState(0);
224
+ const [signalingReady, setSignalingReady] = useState(false);
225
+ const [drawerOpen, setDrawerOpen] = useState(false);
226
+ const [forceMuteOnJoin, setForceMuteOnJoinState] = useState(!!initialForceMuteOnJoin);
227
+ const [hasPassword, setHasPassword] = useState(!!initialHasPassword);
228
+ const [waitingRoomEnabled, setWaitingRoomEnabledState] = useState(!!initialWaitingRoomEnabled);
229
+ const [waitingParticipants, setWaitingParticipants] = useState<WaitingParticipant[]>([]);
230
+ const [talkingStickActive, setTalkingStickActive] = useState(false);
231
+ const [talkingStickHolder, setTalkingStickHolder] = useState<string | undefined>(undefined);
232
+ const [overflowMenuOpen, setOverflowMenuOpen] = useState(false);
233
+ const [diagnosticsOpen, setDiagnosticsOpen] = useState(false);
234
+ const [settingsOpen, setSettingsOpen] = useState(false);
235
+ const [transportMode, setTransportModeState] = useState<TransportMode>("auto");
236
+
237
+ const managerRef = useRef<MeshConnectionManager | null>(null);
238
+ const overflowMenuRef = useRef<HTMLDivElement>(null);
239
+ /** The raw capture from `getDisplayMedia()` - only ever used to stop it (releasing the OS's own share
240
+ * indicator) and to notice the participant ending the share from the browser's own UI (`track.onended`). What's
241
+ * actually shown and sent is `screenTransformRef`'s output (`screenStream` state) - see `handleToggleShare()`. */
242
+ const screenStreamRef = useRef<MediaStream | null>(null);
243
+ const screenTransformRef = useRef<ScreenTransformProcessor | null>(null);
244
+ const levelMetersRef = useRef<Record<string, LevelMeterHandle>>({});
245
+ const reactionSeqRef = useRef(0);
246
+ const reactionTimersRef = useRef(new Set<ReturnType<typeof setTimeout>>());
247
+ /** The latest `media`, for the mesh-event handler below - that effect runs once at mount (see its own comment),
248
+ * so it would otherwise see only the mic state/toggle function from that first render. */
249
+ const mediaRef = useRef(media);
250
+ useEffect(() => {
251
+ mediaRef.current = media;
252
+ }, [media]);
253
+ const isPresenting = !!screenStream;
254
+
255
+ const showReaction = useCallback((emoji: string, name: string) => {
256
+ const id = ++reactionSeqRef.current;
257
+ setReactions((prev) => [...prev.slice(-(MAX_REACTIONS - 1)), { id, emoji, name }]);
258
+ const timer = setTimeout(() => {
259
+ reactionTimersRef.current.delete(timer);
260
+ setReactions((prev) => prev.filter((reaction) => reaction.id !== id));
261
+ }, REACTION_MS);
262
+ reactionTimersRef.current.add(timer);
263
+ }, []);
264
+
265
+ useEffect(() => {
266
+ let cancelled = false;
267
+ const client = new GuestSignalingClient({ channel, token });
268
+ const manager = new MeshConnectionManager({
269
+ selfUid,
270
+ peerId,
271
+ selfName,
272
+ iceServers,
273
+ channel: client,
274
+ createPeerConnection: createBrowserPeerConnection,
275
+ relay: relayEnabled ? createRelay(channel, peerId) : undefined,
276
+ localAudioTrack: media.audioTrack,
277
+ localVideoTrack: media.videoTrack,
278
+ localState: { audioOn: media.micOn, videoOn: media.cameraOn },
279
+ });
280
+ managerRef.current = manager;
281
+
282
+ const unsubscribe = manager.onEvent((event) => {
283
+ switch (event.type) {
284
+ case "participant-joined":
285
+ setParticipants((prev) => [...prev.filter((p) => p.uid !== event.participant.uid), event.participant]);
286
+ return;
287
+ case "participant-updated":
288
+ setParticipants((prev) => prev.map((p) => (p.uid === event.participant.uid ? event.participant : p)));
289
+ return;
290
+ case "participant-left":
291
+ setParticipants((prev) => prev.filter((p) => p.uid !== event.uid));
292
+ setRemoteStreams((prev) => {
293
+ const { [event.uid]: _removed, ...rest } = prev;
294
+ return rest;
295
+ });
296
+ stopLevelMeter(levelMetersRef, event.uid);
297
+ setLevels((prev) => {
298
+ const { [event.uid]: _removed, ...rest } = prev;
299
+ return rest;
300
+ });
301
+ setPinnedUid((prev) => (prev === event.uid ? undefined : prev));
302
+ return;
303
+ case "remote-stream":
304
+ setRemoteStreams((prev) => ({ ...prev, [event.uid]: event.stream }));
305
+ startTrackingLevel(levelMetersRef, event.uid, event.stream, (level) =>
306
+ setLevels((prev) => ({ ...prev, [event.uid]: level })),
307
+ );
308
+ return;
309
+ case "hand-raised":
310
+ playRaisedHandChime();
311
+ setAnnouncement(`${event.name} raised a hand`);
312
+ return;
313
+ case "reaction":
314
+ showReaction(event.emoji, event.name);
315
+ return;
316
+ case "presenter-changed":
317
+ setPresenterUid(event.uid);
318
+ if (event.uid !== peerId && screenStreamRef.current) {
319
+ // Lost a presenter-claim collision (see `MeshConnectionManager`'s doc comment) - stop our
320
+ // own capture without re-sending a release the manager already handled internally.
321
+ stopLocalPresentation(false);
322
+ }
323
+ return;
324
+ case "mute-requested":
325
+ if (mediaRef.current.micOn) {
326
+ void mediaRef.current.toggleMic();
327
+ }
328
+ return;
329
+ case "kicked":
330
+ onLeave("The host removed you from this call.");
331
+ return;
332
+ case "talking-stick-changed":
333
+ setTalkingStickActive(event.active);
334
+ setTalkingStickHolder(event.holder);
335
+ return;
336
+ }
337
+ });
338
+
339
+ client
340
+ .connect()
341
+ .then(() => {
342
+ if (!cancelled) {
343
+ manager.start();
344
+ setSignalingReady(true);
345
+ }
346
+ })
347
+ .catch((err: Error) => {
348
+ if (!cancelled) {
349
+ setConnectError(err.message);
350
+ }
351
+ });
352
+
353
+ // A closing tab doesn't unmount React, so say goodbye explicitly - otherwise everyone else keeps a tile for a
354
+ // participant who is gone.
355
+ const onPageHide = () => manager.stop();
356
+ window.addEventListener("pagehide", onPageHide);
357
+
358
+ const timers = reactionTimersRef.current;
359
+ return () => {
360
+ cancelled = true;
361
+ window.removeEventListener("pagehide", onPageHide);
362
+ unsubscribe();
363
+ manager.stop();
364
+ client.close();
365
+ for (const handle of Object.values(levelMetersRef.current)) {
366
+ handle.stop();
367
+ }
368
+ levelMetersRef.current = {};
369
+ // Only the screen capture is this view's to stop - the camera and microphone belong to the page, which
370
+ // releases them when the participant leaves.
371
+ stopStream(screenStreamRef.current);
372
+ screenTransformRef.current?.stop();
373
+ for (const timer of timers) {
374
+ clearTimeout(timer);
375
+ }
376
+ timers.clear();
377
+ };
378
+ // Deliberately runs once - the call's identity (channel/token/selfUid) never changes for the life of this
379
+ // component; a real identity change is a new call, which unmounts/remounts this view from `[token].tsx`.
380
+ }, []);
381
+
382
+ // What is sent follows what the participant has: the camera or the shared screen, and the microphone.
383
+ useEffect(() => {
384
+ managerRef.current?.setLocalTrack("audio", media.audioTrack);
385
+ }, [media.audioTrack]);
386
+ useEffect(() => {
387
+ managerRef.current?.setLocalTrack("video", screenStream?.getVideoTracks()[0] ?? media.videoTrack);
388
+ }, [media.videoTrack, screenStream]);
389
+ useEffect(() => {
390
+ managerRef.current?.setLocalState({ audioOn: media.micOn, videoOn: media.cameraOn || isPresenting, handRaised });
391
+ }, [media.micOn, media.cameraOn, isPresenting, handRaised]);
392
+
393
+ // Talking-stick mode forces exactly one microphone on at a time: whoever just became the holder (including the
394
+ // host, on activation) is unmuted; everyone else is muted. Unlike `mute-requested`'s one-time nudge, this is
395
+ // paired with disabling the mic button itself (`micLocked`, passed to `CallControls` below) while it applies,
396
+ // so it isn't merely a suggestion - though still cooperative at the signaling layer, like every other
397
+ // moderation feature here (see `MeshConnectionManager`'s doc comment). Turning the mode off touches nobody's
398
+ // mic - states are left exactly as they are, the same "never retroactive" posture as every other host toggle.
399
+ const selfHasTalkingStick = talkingStickActive && talkingStickHolder === peerId;
400
+ const micLocked = talkingStickActive && !selfHasTalkingStick;
401
+ useEffect(() => {
402
+ if (!talkingStickActive) {
403
+ return;
404
+ }
405
+ if (selfHasTalkingStick && !mediaRef.current.micOn) {
406
+ void mediaRef.current.toggleMic();
407
+ } else if (!selfHasTalkingStick && mediaRef.current.micOn) {
408
+ void mediaRef.current.toggleMic();
409
+ }
410
+ }, [talkingStickActive, selfHasTalkingStick]);
411
+
412
+ // The "…" menu closes on Escape or a press anywhere outside it - matching `_CallControls.tsx`'s own menus -
413
+ // but never the windows it opens (diagnostics, settings), which stay open independently (see this module's
414
+ // doc comment on the "…" menu).
415
+ useEffect(() => {
416
+ if (!overflowMenuOpen) {
417
+ return;
418
+ }
419
+ const onPointerDown = (event: PointerEvent) => {
420
+ if (!overflowMenuRef.current?.contains(event.target as Node)) {
421
+ setOverflowMenuOpen(false);
422
+ }
423
+ };
424
+ const onKeyDown = (event: KeyboardEvent) => {
425
+ if (event.key === "Escape") {
426
+ setOverflowMenuOpen(false);
427
+ }
428
+ };
429
+ document.addEventListener("pointerdown", onPointerDown);
430
+ document.addEventListener("keydown", onKeyDown);
431
+ return () => {
432
+ document.removeEventListener("pointerdown", onPointerDown);
433
+ document.removeEventListener("keydown", onKeyDown);
434
+ };
435
+ }, [overflowMenuOpen]);
436
+
437
+ useEffect(() => {
438
+ if (!presenterUid) {
439
+ setActiveSpeakerUid((prev) => pickActiveSpeaker(levels, prev));
440
+ }
441
+ }, [levels, presenterUid]);
442
+
443
+ function stopLocalPresentation(alsoRelease: boolean): void {
444
+ if (alsoRelease) {
445
+ managerRef.current?.releasePresenter();
446
+ }
447
+ stopStream(screenStreamRef.current);
448
+ screenStreamRef.current = null;
449
+ screenTransformRef.current?.stop();
450
+ screenTransformRef.current = null;
451
+ // The sync effect above swaps the camera back in as the outgoing video track. The rotation/flip is reset
452
+ // for the next share - each share starts from the capture the browser hands back, not the last one's fix.
453
+ setScreenStream(null);
454
+ setScreenTransform(NO_SCREEN_TRANSFORM);
455
+ }
456
+
457
+ async function handleToggleShare() {
458
+ if (screenStreamRef.current) {
459
+ stopLocalPresentation(true);
460
+ return;
461
+ }
462
+ const result = await requestDisplayMedia();
463
+ if (!result.ok) {
464
+ setConnectError(result.error.message);
465
+ return;
466
+ }
467
+ const claimed = managerRef.current?.claimPresenter() ?? false;
468
+ if (!claimed) {
469
+ stopStream(result.value);
470
+ return;
471
+ }
472
+ screenStreamRef.current = result.value;
473
+ const rawTrack = result.value.getVideoTracks()[0];
474
+ rawTrack.onended = () => stopLocalPresentation(true);
475
+ try {
476
+ // Runs the capture through the rotate/flip processor before anyone (presenter included) sees it - see
477
+ // ScreenTransform.ts's doc comment on why window capture sometimes needs this correction.
478
+ const processor = new ScreenTransformProcessor({ source: rawTrack, state: screenTransform });
479
+ screenTransformRef.current = processor;
480
+ setScreenStream(new MediaStream([processor.track]));
481
+ } catch {
482
+ // This browser can't draw the correction - share the raw capture rather than not sharing at all; the
483
+ // rotate/flip buttons simply won't do anything (screenTransformRef stays null).
484
+ setScreenStream(result.value);
485
+ }
486
+ }
487
+
488
+ function handleRotateScreen() {
489
+ setScreenTransform((prev) => {
490
+ const next = rotateClockwise(prev);
491
+ screenTransformRef.current?.setState(next);
492
+ return next;
493
+ });
494
+ }
495
+
496
+ function handleFlipScreen() {
497
+ setScreenTransform((prev) => {
498
+ const next = { ...prev, flipped: !prev.flipped };
499
+ screenTransformRef.current?.setState(next);
500
+ return next;
501
+ });
502
+ }
503
+
504
+ function handleToggleHand() {
505
+ setHandRaised((prev) => !prev);
506
+ }
507
+
508
+ function handleReaction(emoji: string) {
509
+ if (managerRef.current?.sendReaction(emoji)) {
510
+ showReaction(emoji, "You");
511
+ }
512
+ }
513
+
514
+ function togglePin(uid: string) {
515
+ setPinnedUid((prev) => (prev === uid ? undefined : uid));
516
+ }
517
+
518
+ /** Asks `peerUid`'s participant to mute - the host-only button in the participants drawer calls this
519
+ * directly, with no confirmation (unlike removing someone, muting is easily undone by the participant
520
+ * themselves). */
521
+ function handleMuteParticipant(peerUid: string) {
522
+ managerRef.current?.sendMuteRequest(peerUid);
523
+ }
524
+
525
+ /** Removes `peerUid`'s participant from the call: the cooperative signal (immediate, so a cooperating client
526
+ * leaves without waiting on the network round trip below) and the enforced server-side revoke together - see
527
+ * `kickParticipant()`'s own doc comment for why both matter. The REST call's failure is swallowed rather than
528
+ * surfaced: the host already asked them to leave, and there is no useful recovery action to offer from here
529
+ * for what is almost always a transient network issue. */
530
+ function handleKickParticipant(peerUid: string) {
531
+ managerRef.current?.sendKick(peerUid);
532
+ kickParticipant(channel, accountUidOf(peerUid)).catch(() => undefined);
533
+ }
534
+
535
+ /** Flips the host's "mute new participants on join" setting: applied optimistically (the toggle itself is the
536
+ * only feedback this small a control needs) and reverted if the save fails, so the drawer never keeps claiming
537
+ * a setting that isn't actually persisted. Takes effect for whoever joins next - never retroactive, see
538
+ * `setForceMuteOnJoin()`'s own doc comment. */
539
+ function handleToggleForceMuteOnJoin() {
540
+ const next = !forceMuteOnJoin;
541
+ setForceMuteOnJoinState(next);
542
+ apiSetForceMuteOnJoin(channel, next).catch(() => setForceMuteOnJoinState(!next));
543
+ }
544
+
545
+ /** Sets, changes, or removes (`password: null`) the host's join password - unlike the force-mute toggle, this
546
+ * is not applied optimistically: the drawer's own `PasswordSection` awaits the result itself (clearing its
547
+ * input on success, showing an inline error on failure), so there is nothing here to revert. `hasPassword`
548
+ * updates only once the save actually succeeds. */
549
+ async function handleSetPassword(password: string | null): Promise<void> {
550
+ await setMeetingPassword(channel, password);
551
+ setHasPassword(!!password);
552
+ }
553
+
554
+ /** Flips the host's waiting-room setting - same optimistic-and-revert shape as `handleToggleForceMuteOnJoin()`,
555
+ * for the same reason. Takes effect for whoever joins next; nobody already admitted is retroactively gated. */
556
+ function handleToggleWaitingRoomEnabled() {
557
+ const next = !waitingRoomEnabled;
558
+ setWaitingRoomEnabledState(next);
559
+ apiSetWaitingRoomEnabled(channel, next).catch(() => setWaitingRoomEnabledState(!next));
560
+ }
561
+
562
+ /** Starts or stops talking-stick mode - host-only, see this module's doc comment. Starting makes the host the
563
+ * initial holder; stopping clears the holder too, leaving mic states exactly as they are rather than
564
+ * retroactively unmuting anyone. */
565
+ function handleToggleTalkingStick() {
566
+ managerRef.current?.setTalkingStick(!talkingStickActive, talkingStickActive ? undefined : peerId);
567
+ }
568
+
569
+ /** Hands the talking stick to `uid` (a participant's `MeshParticipant.uid`, or this tab's own `peerId` for the
570
+ * host taking it back) - host-only, called from the participants drawer's per-row "Give stick" button. */
571
+ function handleGiveTalkingStick(uid: string) {
572
+ managerRef.current?.setTalkingStick(true, uid);
573
+ }
574
+
575
+ /** Forces this tab's own transport policy - see `MeshConnectionManager.setTransportMode()`'s doc comment for
576
+ * what each mode actually does, and this module's doc comment on the "…" menu for why this is a personal,
577
+ * per-tab choice rather than a meeting setting. */
578
+ function handleSetTransportMode(mode: TransportMode) {
579
+ setTransportModeState(mode);
580
+ managerRef.current?.setTransportMode(mode);
581
+ setOverflowMenuOpen(false);
582
+ }
583
+
584
+ /** Admits one pending request - optimistically removed from the drawer's own list (the next poll would drop it
585
+ * anyway, once the requester's own next poll completes their join; removing it here just avoids the visible
586
+ * delay). Re-added if the save itself fails, so a failure doesn't silently lose the request from the list. */
587
+ function handleAdmitParticipant(uid: string) {
588
+ setWaitingParticipants((prev) => prev.filter((p) => p.uid !== uid));
589
+ admitParticipant(channel, uid).catch(() => void listWaitingParticipants(channel).then(setWaitingParticipants, () => undefined));
590
+ }
591
+
592
+ /** Denies one pending request - same optimistic-removal shape as `handleAdmitParticipant()`. */
593
+ function handleDenyParticipant(uid: string) {
594
+ setWaitingParticipants((prev) => prev.filter((p) => p.uid !== uid));
595
+ denyParticipant(channel, uid).catch(() => void listWaitingParticipants(channel).then(setWaitingParticipants, () => undefined));
596
+ }
597
+
598
+ function handleAudioBlocked() {
599
+ setAudioBlocked(true);
600
+ }
601
+
602
+ function handleResumeAudio() {
603
+ setAudioBlocked(false);
604
+ setAudioNonce((prev) => prev + 1);
605
+ }
606
+
607
+ // A participant's `uid` is their tab-scoped peer id (`<account uid>~<random>` - see `newPeerId()`), while
608
+ // `hostUid` is the plain account uid `PublicVideoMeeting.hostUid` names - so telling the host's own tab(s) apart
609
+ // needs the prefix match, not equality. Purely a client-side check (see `hostUid`'s own doc comment on this
610
+ // prop): nobody's tab is prevented from claiming it, only from being shown host controls for it.
611
+ const isHost = !!hostUid && selfUid === hostUid;
612
+ const isParticipantHost = (uid: string): boolean => !!hostUid && uid.startsWith(`${hostUid}~`);
613
+
614
+ // Refreshes the drawer's own waiting-room list while the host has the drawer open - there is no push signal
615
+ // for a newly filed admission request, so polling is the only way the list stays current without the host
616
+ // having to close and reopen the drawer. Stops (and the list is dropped) the moment either condition ends.
617
+ useEffect(() => {
618
+ if (!isHost || !drawerOpen) {
619
+ setWaitingParticipants([]);
620
+ return;
621
+ }
622
+ let cancelled = false;
623
+ const poll = () => {
624
+ listWaitingParticipants(channel).then((list) => {
625
+ if (!cancelled) {
626
+ setWaitingParticipants(list);
627
+ }
628
+ }, () => undefined);
629
+ };
630
+ poll();
631
+ const interval = setInterval(poll, DEFAULT_WAITING_POLL_MS);
632
+ return () => {
633
+ cancelled = true;
634
+ clearInterval(interval);
635
+ };
636
+ }, [isHost, drawerOpen, channel]);
637
+ const otherUids = participants.map((p) => p.uid);
638
+ // A pin or an active-speaker pick can name someone who has just left, until the state catches up.
639
+ const stillHere = (uid: string | undefined) => (uid && otherUids.includes(uid) ? uid : undefined);
640
+ const showFocusLayout = !!presenterUid || viewMode === "focus";
641
+ const mainUid = showFocusLayout
642
+ ? computeMainUid({ presenterUid, pinnedUid: stillHere(pinnedUid), activeSpeakerUid: stillHere(activeSpeakerUid), otherUids })
643
+ : undefined;
644
+ const presenterName = presenterUid ? (presenterUid === peerId ? selfName : (participants.find((p) => p.uid === presenterUid)?.name ?? "Someone")) : undefined;
645
+ const raisedNames = [...(handRaised ? ["You"] : []), ...participants.filter((p) => p.handRaised).map((p) => p.name)];
646
+ // Absent (not "Someone") once the holder has left the call - see `MeshConnectionManager`'s doc comment on why
647
+ // nothing reassigns it automatically; the header then says so rather than naming someone no longer here.
648
+ const talkingStickHolderName = !talkingStickActive
649
+ ? undefined
650
+ : selfHasTalkingStick
651
+ ? "You"
652
+ : participants.find((p) => p.uid === talkingStickHolder)?.name;
653
+ const alone = participants.length === 0;
654
+ // This participant has no working link yet: the signaling channel is still opening, or there is someone in the call
655
+ // and every connection to them is still being made. Once any one is up (or nobody else is here) it is not shown,
656
+ // so someone already in the call is not told they are "connecting" each time a newcomer arrives.
657
+ const selfConnecting = !connectError && (!signalingReady || (!alone && participants.every((p) => p.transport === "connecting")));
658
+
659
+ const remoteTile = (p: MeshParticipant, className?: string) => (
660
+ <ParticipantTile
661
+ key={p.uid}
662
+ name={p.name}
663
+ stream={remoteStreams[p.uid] ?? null}
664
+ cameraOff={!p.videoOn}
665
+ micMuted={!p.audioOn}
666
+ handRaised={p.handRaised}
667
+ transport={p.transport}
668
+ status={p.transport === "connecting" ? "Awaiting connection…" : undefined}
669
+ isFocused={mainUid === p.uid}
670
+ contain={presenterUid === p.uid}
671
+ className={className}
672
+ onClick={() => togglePin(p.uid)}
673
+ />
674
+ );
675
+
676
+ const selfTile = (className?: string) => (
677
+ <ParticipantTile
678
+ name={selfName}
679
+ stream={media.videoStream}
680
+ isLocal
681
+ cameraOff={!media.cameraOn}
682
+ micMuted={!media.micOn}
683
+ handRaised={handRaised}
684
+ status={selfConnecting ? "Connecting…" : undefined}
685
+ // A custom background is a fixed picture, not a live reflection - mirroring it would show it backwards
686
+ // to no one but the participant themselves (see `_ParticipantTile.tsx`'s doc comment).
687
+ mirrored={media.filters.background !== "image"}
688
+ className={className}
689
+ />
690
+ );
691
+
692
+ const mainParticipant = participants.find((p) => p.uid === mainUid);
693
+ const thumbnails = participants.filter((p) => p.uid !== mainUid);
694
+
695
+ let stage: React.ReactNode;
696
+ if (alone) {
697
+ // Nobody else yet: the local participant fills the tile area, presenting or not.
698
+ stage = isPresenting ? (
699
+ <ParticipantTile name={selfName} stream={screenStream} isLocal contain isFocused className="h-full" />
700
+ ) : (
701
+ selfTile("h-full")
702
+ );
703
+ } else if (showFocusLayout && (mainParticipant || isPresenting)) {
704
+ stage = (
705
+ <div className="h-full flex flex-col gap-2">
706
+ <div className="flex-1 min-h-0" data-testid="main-tile">
707
+ {mainParticipant ? (
708
+ remoteTile(mainParticipant, "h-full")
709
+ ) : (
710
+ <ParticipantTile name={selfName} stream={screenStream} isLocal contain isFocused className="h-full" />
711
+ )}
712
+ </div>
713
+ {thumbnails.length > 0 && (
714
+ <div className="flex gap-2 overflow-x-auto h-24 shrink-0" data-testid="thumbnails">
715
+ {thumbnails.map((p) => (
716
+ <div key={p.uid} className="w-36 shrink-0">
717
+ {remoteTile(p, "h-full")}
718
+ </div>
719
+ ))}
720
+ </div>
721
+ )}
722
+ </div>
723
+ );
724
+ } else {
725
+ stage = (
726
+ <div className="h-full grid gap-2 auto-rows-fr [grid-template-columns:repeat(auto-fit,minmax(min(100%,320px),1fr))]">
727
+ {participants.map((p) => remoteTile(p, "h-full"))}
728
+ </div>
729
+ );
730
+ }
731
+
732
+ return (
733
+ <div className="fixed inset-0 z-50 flex bg-[#202124] text-white overflow-hidden">
734
+ <style>{`@keyframes meet-float { 0% { transform: translateY(0) scale(.6); opacity: 0; } 12% { opacity: 1; transform: translateY(-4vh) scale(1); } 100% { transform: translateY(-45vh) scale(1); opacity: 0; } }`}</style>
735
+ <div className="relative flex-1 min-w-0 flex flex-col overflow-hidden">
736
+ <header className="shrink-0 flex items-center justify-between gap-3 px-4 py-3">
737
+ <h1 className="min-w-0 truncate text-base font-medium">{meetingTitle}</h1>
738
+ <div className="flex items-center gap-2 shrink-0">
739
+ {raisedNames.length > 0 && (
740
+ <span className="max-w-[45vw] truncate px-3 py-1.5 rounded-full bg-[#a8c7fa] text-[#062e6f] text-sm font-medium" data-testid="raised-hands">
741
+ ✋ {raisedNames.join(", ")}
742
+ </span>
743
+ )}
744
+ {talkingStickActive && (
745
+ <span className="max-w-[45vw] truncate px-3 py-1.5 rounded-full bg-[#a8c7fa] text-[#062e6f] text-sm font-medium" data-testid="talking-stick-status">
746
+ 🎙️ {talkingStickHolderName ? `${talkingStickHolderName} ${talkingStickHolderName === "You" ? "have" : "has"} the floor` : "Waiting for the host to choose a speaker"}
747
+ </span>
748
+ )}
749
+ {isHost && (
750
+ <button
751
+ type="button"
752
+ className={`w-9 h-9 flex items-center justify-center rounded-full focus:outline-none focus-visible:ring-2 focus-visible:ring-white/80 ${talkingStickActive ? "bg-[#a8c7fa] text-[#062e6f] hover:bg-[#8ab4f8]" : "bg-[#3c4043] hover:bg-[#4b4f53]"}`}
753
+ aria-label={talkingStickActive ? "End talking stick" : "Talking stick"}
754
+ aria-pressed={talkingStickActive}
755
+ title={talkingStickActive ? "End talking stick" : "Start talking stick mode"}
756
+ onClick={handleToggleTalkingStick}
757
+ >
758
+ <BatonIcon />
759
+ </button>
760
+ )}
761
+ <button
762
+ type="button"
763
+ className="px-3 py-1.5 rounded-full bg-[#3c4043] hover:bg-[#4b4f53] text-sm focus:outline-none focus-visible:ring-2 focus-visible:ring-white/80"
764
+ aria-label={`${participants.length + 1} participants`}
765
+ aria-haspopup="dialog"
766
+ aria-expanded={drawerOpen}
767
+ onClick={() => setDrawerOpen((prev) => !prev)}
768
+ >
769
+ {participants.length + 1}
770
+ </button>
771
+ <div className="relative" ref={overflowMenuRef}>
772
+ <button
773
+ type="button"
774
+ className={`w-9 h-9 flex items-center justify-center rounded-full focus:outline-none focus-visible:ring-2 focus-visible:ring-white/80 ${overflowMenuOpen ? "bg-[#4b4f53]" : "bg-[#3c4043] hover:bg-[#4b4f53]"}`}
775
+ aria-label="More options"
776
+ aria-haspopup="menu"
777
+ aria-expanded={overflowMenuOpen}
778
+ onClick={() => setOverflowMenuOpen((prev) => !prev)}
779
+ >
780
+ <OverflowIcon />
781
+ </button>
782
+ {overflowMenuOpen && (
783
+ <div
784
+ role="menu"
785
+ aria-label="More options"
786
+ className="absolute top-full right-0 mt-2 w-56 rounded-2xl bg-[#2b2d30] text-white shadow-xl py-1 text-sm"
787
+ >
788
+ <button
789
+ type="button"
790
+ role="menuitem"
791
+ className="w-full px-3 py-2 text-left hover:bg-white/10 focus:outline-none focus-visible:ring-2 focus-visible:ring-white/80"
792
+ onClick={() => {
793
+ setDiagnosticsOpen(true);
794
+ setOverflowMenuOpen(false);
795
+ }}
796
+ >
797
+ Diagnostics
798
+ </button>
799
+ <div className="my-1 border-t border-white/10" />
800
+ <p className="px-3 pt-1 pb-0.5 text-xs font-semibold uppercase tracking-wide text-white/60">Connection method</p>
801
+ {TRANSPORT_MODE_OPTIONS.map(({ mode, label }) => (
802
+ <button
803
+ key={mode}
804
+ type="button"
805
+ role="menuitemradio"
806
+ aria-checked={transportMode === mode}
807
+ className="flex w-full items-center gap-2 px-3 py-2 text-left hover:bg-white/10 focus:outline-none focus-visible:ring-2 focus-visible:ring-white/80"
808
+ onClick={() => handleSetTransportMode(mode)}
809
+ >
810
+ <span className="w-4 text-[#8ab4f8]" aria-hidden="true">
811
+ {transportMode === mode ? "✓" : ""}
812
+ </span>
813
+ {label}
814
+ </button>
815
+ ))}
816
+ {isHost && (
817
+ <>
818
+ <div className="my-1 border-t border-white/10" />
819
+ <button
820
+ type="button"
821
+ role="menuitem"
822
+ className="w-full px-3 py-2 text-left hover:bg-white/10 focus:outline-none focus-visible:ring-2 focus-visible:ring-white/80"
823
+ onClick={() => {
824
+ setSettingsOpen(true);
825
+ setOverflowMenuOpen(false);
826
+ }}
827
+ >
828
+ Settings
829
+ </button>
830
+ </>
831
+ )}
832
+ </div>
833
+ )}
834
+ </div>
835
+ </div>
836
+ </header>
837
+ {connectError && (
838
+ <div role="alert" className="shrink-0 mx-3 mb-2 px-3 py-2 rounded-lg bg-[#601410] text-[#f9dedc] text-sm">
839
+ {connectError}
840
+ </div>
841
+ )}
842
+ {audioBlocked && (
843
+ <button
844
+ type="button"
845
+ className="shrink-0 mx-3 mb-2 px-3 py-2 rounded-lg bg-[#a8c7fa] text-[#062e6f] text-sm font-medium"
846
+ onClick={handleResumeAudio}
847
+ >
848
+ Click here to turn on sound
849
+ </button>
850
+ )}
851
+ <main className="relative flex-1 min-h-0 px-3 pb-3">
852
+ {stage}
853
+ {presenterName && presenterUid !== peerId && (
854
+ <p className="absolute top-2 left-5 px-2 py-0.5 rounded bg-black/60 text-sm">{presenterName} is presenting</p>
855
+ )}
856
+ </main>
857
+ <footer className="shrink-0 pt-1 pb-[max(0.75rem,env(safe-area-inset-bottom))]">
858
+ <CallControls
859
+ media={media}
860
+ participants={participants}
861
+ isPresenting={isPresenting}
862
+ presentingElsewhereName={presenterUid && presenterUid !== peerId ? presenterName : undefined}
863
+ onToggleShare={() => void handleToggleShare()}
864
+ screenTransform={screenTransform}
865
+ onRotateScreen={handleRotateScreen}
866
+ onFlipScreen={handleFlipScreen}
867
+ handRaised={handRaised}
868
+ onToggleHand={handleToggleHand}
869
+ onReaction={handleReaction}
870
+ viewMode={viewMode}
871
+ onToggleViewMode={() => setViewMode((prev) => (prev === "grid" ? "focus" : "grid"))}
872
+ micLocked={micLocked}
873
+ onLeave={onLeave}
874
+ />
875
+ </footer>
876
+
877
+ {!alone && (
878
+ <div
879
+ className="absolute z-10 right-3 top-14 w-28 sm:top-auto sm:bottom-24 sm:w-52 aspect-video shadow-xl xl:right-4 xl:bottom-4"
880
+ data-testid="self-view"
881
+ >
882
+ {selfTile("h-full")}
883
+ </div>
884
+ )}
885
+
886
+ <div className="pointer-events-none absolute left-4 bottom-28 w-40 h-[45vh]" aria-hidden="true">
887
+ {reactions.map((reaction) => (
888
+ <div
889
+ key={reaction.id}
890
+ className="absolute bottom-0 flex flex-col items-center"
891
+ style={{ left: `${(reaction.id % 4) * 36}px`, animation: `meet-float ${REACTION_MS}ms ease-out forwards` }}
892
+ data-testid="reaction"
893
+ >
894
+ <span className="text-4xl leading-none">{reaction.emoji}</span>
895
+ <span className="mt-1 px-1.5 rounded-full bg-[#a8c7fa] text-[#062e6f] text-xs">{reaction.name}</span>
896
+ </div>
897
+ ))}
898
+ </div>
899
+
900
+ {participants.map((p) => {
901
+ const stream = remoteStreams[p.uid];
902
+ return stream ? <RemoteAudio key={`${p.uid}:${audioNonce}`} stream={stream} onBlocked={handleAudioBlocked} /> : null;
903
+ })}
904
+ <div role="status" aria-live="polite" className="sr-only">
905
+ {announcement}
906
+ </div>
907
+ </div>
908
+
909
+ {drawerOpen && (
910
+ <ParticipantsDrawer
911
+ selfName={selfName}
912
+ micOn={media.micOn}
913
+ handRaised={handRaised}
914
+ participants={participants}
915
+ isSelfHost={isHost}
916
+ isParticipantHost={isParticipantHost}
917
+ onMute={handleMuteParticipant}
918
+ onKick={handleKickParticipant}
919
+ waitingRoomEnabled={waitingRoomEnabled}
920
+ waitingParticipants={waitingParticipants}
921
+ onAdmit={handleAdmitParticipant}
922
+ onDeny={handleDenyParticipant}
923
+ talkingStickActive={talkingStickActive}
924
+ selfPeerId={peerId}
925
+ talkingStickHolder={talkingStickHolder}
926
+ onGiveTalkingStick={handleGiveTalkingStick}
927
+ onClose={() => setDrawerOpen(false)}
928
+ />
929
+ )}
930
+
931
+ {diagnosticsOpen && (
932
+ <DiagnosticsWindow onClose={() => setDiagnosticsOpen(false)}>
933
+ <DiagnosticsPanel selfName={selfName} micOn={media.micOn} cameraOn={media.cameraOn} participants={participants} />
934
+ </DiagnosticsWindow>
935
+ )}
936
+
937
+ {settingsOpen && isHost && (
938
+ <SettingsModal
939
+ forceMuteOnJoin={forceMuteOnJoin}
940
+ onToggleForceMuteOnJoin={handleToggleForceMuteOnJoin}
941
+ hasPassword={hasPassword}
942
+ onSetPassword={handleSetPassword}
943
+ waitingRoomEnabled={waitingRoomEnabled}
944
+ onToggleWaitingRoomEnabled={handleToggleWaitingRoomEnabled}
945
+ onClose={() => setSettingsOpen(false)}
946
+ />
947
+ )}
948
+ </div>
949
+ );
950
+ }
951
+
952
+ /** Plays one remote participant's stream through a hidden `<audio>` element. `onBlocked` fires if the browser
953
+ * refuses to start it (an autoplay policy) - the view then asks for a click. */
954
+ function RemoteAudio({ stream, onBlocked }: { stream: MediaStream; onBlocked: () => void }) {
955
+ const audioRef = useRef<HTMLAudioElement>(null);
956
+ useEffect(() => {
957
+ const element = audioRef.current!;
958
+ element.srcObject = stream;
959
+ void Promise.resolve(element.play()).catch(onBlocked);
960
+ }, [stream, onBlocked]);
961
+ return <audio ref={audioRef} autoPlay data-testid="remote-audio" />;
962
+ }
963
+
964
+ /** The WebSocket media relay for this call, or `undefined` when this browser cannot run it (no WebCodecs) - the
965
+ * mesh then never falls back to it, and says so by marking an unreachable participant `"failed"`. */
966
+ function createRelay(meetingUid: string, peerId: string): RelayTransportLike | undefined {
967
+ const relay = createRelayTransport({ meetingUid, peerId });
968
+ return relay.supported ? relay : undefined;
969
+ }
970
+
971
+ function stopLevelMeter(ref: React.MutableRefObject<Record<string, LevelMeterHandle>>, uid: string): void {
972
+ ref.current[uid]?.stop();
973
+ delete ref.current[uid];
974
+ }
975
+
976
+ function startTrackingLevel(
977
+ ref: React.MutableRefObject<Record<string, LevelMeterHandle>>,
978
+ uid: string,
979
+ stream: MediaStream,
980
+ onLevel: (level: number) => void,
981
+ ): void {
982
+ stopLevelMeter(ref, uid);
983
+ const handle = startLevelMeter(stream, onLevel);
984
+ if (handle) {
985
+ ref.current[uid] = handle;
986
+ }
987
+ }