@@ -1,10 +1,19 @@
11 import { useCallback, useEffect, useRef, useState } from "react";
2-import { AudioPresets, Room, RoomEvent, Track } from "livekit-client";
2+import {
3+ AudioPresets,
4+ Room,
5+ RoomEvent,
6+ ScreenSharePresets,
7+ Track,
8+ VideoPresets,
9+} from "livekit-client";
310 import type {
411 AudioCaptureOptions,
512 AudioProcessorOptions,
613 Participant,
14+ ScreenShareCaptureOptions,
715 TrackProcessor,
16+ TrackPublishOptions,
817 } from "livekit-client";
918 import { api } from "./api";
1019 import type { Settings } from "./settings";
@@ -163,6 +172,57 @@ function publish(s: Settings) {
163172 return { audioPreset: AudioPresets[s.audioPreset] };
164173 }
165174
175+// screenShare maps the quality setting to a capture resolution and encoding.
176+// The simulcast layers are ordered low to high, below the preset's own layer.
177+const screenSharePresets = {
178+ "720p": ScreenSharePresets.h720fps30,
179+ "1080p": ScreenSharePresets.h1080fps30,
180+};
181+const screenShareLayers = {
182+ "720p": [VideoPresets.h180, VideoPresets.h360],
183+ "1080p": [VideoPresets.h360, VideoPresets.h720],
184+};
185+
186+function screenShare(s: Settings): [ScreenShareCaptureOptions, TrackPublishOptions] {
187+ const preset = screenSharePresets[s.screenShareQuality];
188+ return [
189+ { audio: true, resolution: preset.resolution },
190+ {
191+ screenShareEncoding: preset.encoding,
192+ screenShareSimulcastLayers: screenShareLayers[s.screenShareQuality],
193+ },
194+ ];
195+}
196+
197+// A Share is a screen-share publication, ours or a peer's. LiveKit models it as
198+// a track on a participant rather than a participant, so the sidebar synthesizes
199+// one row per share.
200+export interface Share {
201+ id: string;
202+ name: string;
203+ self: boolean;
204+ track: Track;
205+}
206+
207+function sharesOf(room: Room): Share[] {
208+ const shares: Share[] = [];
209+ const collect = (p: Participant, self: boolean) => {
210+ for (const pub of p.trackPublications.values()) {
211+ if (pub.source === Track.Source.ScreenShare && pub.track?.kind === Track.Kind.Video) {
212+ shares.push({
213+ id: `${p.identity}:${pub.trackSid}`,
214+ name: p.name || p.identity,
215+ self,
216+ track: pub.track,
217+ });
218+ }
219+ }
220+ };
221+ collect(room.localParticipant, true);
222+ for (const p of room.remoteParticipants.values()) collect(p, false);
223+ return shares;
224+}
225+
166226 // micSignature identifies the applied capture + encoding config, so live
167227 // changes can be detected without republishing on every render.
168228 function micSignature(s: Settings): string {
@@ -184,10 +244,13 @@ export interface Voice {
184244 error: string;
185245 peers: string[];
186246 speaking: string[];
247+ shares: Share[];
248+ sharing: boolean;
187249 muted: boolean;
188250 join: (c: Channel) => void;
189251 leave: () => void;
190252 toggleMute: () => void;
253+ toggleShare: () => void;
191254 setPeerVolume: (name: string, volume: number) => void;
192255 micLevel: () => number;
193256 }
@@ -196,15 +259,13 @@ export interface Voice {
196259 // switching panes. Chat holds it and passes it to the sidebar controls.
197260 // Voice settings are read from a ref so `join` always uses the latest values,
198261 // while live changes (device, processing, quality) are applied via effects.
199-export function useVoice(
200- settings: Settings,
201- onVoice?: (channelID: number | null) => void,
202-): Voice {
262+export function useVoice(settings: Settings, onVoice?: (channelID: number | null) => void): Voice {
203263 const [channel, setChannel] = useState<Channel | null>(null);
204264 const [status, setStatus] = useState<VoiceStatus>("idle");
205265 const [error, setError] = useState("");
206266 const [peers, setPeers] = useState<string[]>([]);
207267 const [speaking, setSpeaking] = useState<string[]>([]);
268+ const [shares, setShares] = useState<Share[]>([]);
208269 const [muted, setMuted] = useState(false);
209270 const roomRef = useRef<Room | null>(null);
210271 const audioRef = useRef<HTMLAudioElement[]>([]);
@@ -238,6 +299,7 @@ export function useVoice(
238299 setError("");
239300 setPeers([]);
240301 setSpeaking([]);
302+ setShares([]);
241303 setMuted(false);
242304 onVoiceRef.current?.(null);
243305 }, []);
@@ -257,8 +319,10 @@ export function useVoice(
257319 publishDefaults: publish(s),
258320 });
259321 roomRef.current = room;
260- const sync = () =>
322+ const sync = () => {
261323 setPeers([...room.remoteParticipants.values()].map((p) => p.name || p.identity));
324+ setShares(sharesOf(room));
325+ };
262326 room.on(RoomEvent.ParticipantConnected, () => {
263327 if (readyRef.current && settingsRef.current.joinSound) beep(440);
264328 sync();
@@ -276,6 +340,18 @@ export function useVoice(
276340 el.muted = settingsRef.current.deafened;
277341 audioRef.current.push(el);
278342 });
343+ // Screen shares surface as a track on a participant, so every event that
344+ // can add or drop one — publish, subscribe, unpublish — resyncs the rows.
345+ for (const ev of [
346+ RoomEvent.TrackPublished,
347+ RoomEvent.TrackUnpublished,
348+ RoomEvent.TrackSubscribed,
349+ RoomEvent.TrackUnsubscribed,
350+ RoomEvent.LocalTrackPublished,
351+ RoomEvent.LocalTrackUnpublished,
352+ ]) {
353+ room.on(ev, sync);
354+ }
279355 room.on(RoomEvent.Disconnected, () => {
280356 if (roomRef.current === room) {
281357 roomRef.current = null;
@@ -352,6 +428,20 @@ export function useVoice(
352428 setMuted(next);
353429 }, [muted]);
354430
431+ const toggleShare = useCallback(async () => {
432+ const room = roomRef.current;
433+ if (!room) return;
434+ const on = room.localParticipant.getTrackPublication(Track.Source.ScreenShare);
435+ const [capture, publishOptions] = screenShare(settingsRef.current);
436+ try {
437+ await room.localParticipant.setScreenShareEnabled(!on, capture, publishOptions);
438+ } catch (err) {
439+ // Dismissing the picker rejects with NotAllowedError; that is not a fault.
440+ if ((err as Error).name !== "NotAllowedError") setError((err as Error).message);
441+ }
442+ setShares(sharesOf(room));
443+ }, []);
444+
355445 const setPeerVolume = useCallback((name: string, volume: number) => {
356446 const room = roomRef.current;
357447 if (!room) return;
@@ -373,10 +463,13 @@ export function useVoice(
373463 error,
374464 peers,
375465 speaking,
466+ shares,
467+ sharing: shares.some((s) => s.self),
376468 muted,
377469 join,
378470 leave,
379471 toggleMute,
472+ toggleShare,
380473 setPeerVolume,
381474 micLevel,
382475 };