diff --git a/web/src/App.tsx b/web/src/App.tsx index 0db965d..ebbd40d 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -135,6 +135,13 @@ import { type StreamPublishOptions, } from "./publish"; import { DEFAULT_ICE_SERVERS, StreamViewer, parseStreamInfo, type StreamEvent, type StreamInfo } from "./stream"; + +interface WatchedStream { + info: StreamInfo; + media: MediaStream | null; + state: RTCPeerConnectionState; + error: string | null; +} import { DEMO_HOST, DEMO_MODE, DemoSocket } from "./demoMode"; import { DEFAULT_SYNC_SERVER, @@ -9007,11 +9014,9 @@ function AppInner() { const [clientVolumes, setClientVolumes] = useState>(loadClientVolumes); /** Stream metadata per publishing client id, keyed by clid. */ const [streamInfos, setStreamInfos] = useState>({}); - const [watchedStream, setWatchedStream] = useState(null); - const [streamMedia, setStreamMedia] = useState(null); - const [streamState, setStreamState] = useState("new"); - const [streamError, setStreamError] = useState(null); - const streamViewerRef = useRef(null); + /** Streams being watched, keyed by the publisher's clid - several at once, like Discord. */ + const [watchedStreams, setWatchedStreams] = useState>({}); + const streamViewersRef = useRef>(new Map()); const [publishState, setPublishState] = useState("idle"); const [publishViewers, setPublishViewers] = useState([]); const [publishPending, setPublishPending] = useState<[number, string][]>([]); @@ -9019,7 +9024,6 @@ function AppInner() { const publisherRef = useRef(null); const publishPreviewRef = useRef(null); const [streamSettingsOpen, setStreamSettingsOpen] = useState(false); - const streamVideoRef = useRef(null); /** Clients we already pulled a `notifystreaminfo` for, so we ask only once. */ const requestedStreamInfoRef = useRef>(new Set()); /** Stable self id from the connector (`own_client_id`); nickname match is fallback only. */ @@ -10394,7 +10398,7 @@ function AppInner() { }); } } - streamViewerRef.current?.handleEvent(streamEvent); + for (const viewer of streamViewersRef.current.values()) viewer.handleEvent(streamEvent); publisherRef.current?.handleEvent(streamEvent); break; } @@ -10471,10 +10475,7 @@ function AppInner() { appendLog({ text: `Disconnected: ${data.reason}`, kind: "info" }); logClient("info", "Connection", `Disconnected: ${data.reason}`); if (wasConnected) void notify("CONNECTION_DISCONNECTED"); - streamViewerRef.current?.close(); - streamViewerRef.current = null; - setWatchedStream(null); - setStreamMedia(null); + handleStopWatchingStream(); setStreamInfos({}); requestedStreamInfoRef.current.clear(); publisherRef.current?.stop(); @@ -12143,11 +12144,6 @@ function AppInner() { }); }, [clients]); - useEffect(() => { - const video = streamVideoRef.current; - if (video && video.srcObject !== streamMedia) video.srcObject = streamMedia; - }, [streamMedia, watchedStream]); - useEffect(() => { // The publisher owns the capture; the preview only mirrors it, so it is // read off the peer connection's senders rather than kept in state. @@ -12157,35 +12153,43 @@ function AppInner() { if (video.srcObject !== media) video.srcObject = media; }, [publishState, publishViewers]); - const handleStopWatchingStream = () => { - // close() sends leaveStream and fires onClosed, which clears the rest. - streamViewerRef.current?.close(); - streamViewerRef.current = null; - setWatchedStream(null); - setStreamMedia(null); - setStreamState("new"); + /** Stops one watched stream, or all of them without a clid. */ + const handleStopWatchingStream = (clientId?: number) => { + const viewers = streamViewersRef.current; + const ids = clientId === undefined ? [...viewers.keys()] : [clientId]; + for (const id of ids) { + // close() sends leaveStream; delete first so its onClosed is a no-op. + const viewer = viewers.get(id); + viewers.delete(id); + viewer?.close(); + } + setWatchedStreams((prev) => { + if (clientId === undefined) return {}; + const next = { ...prev }; + delete next[clientId]; + return next; + }); }; const handleWatchStream = (clientId: number) => { const info = streamInfos[clientId]; - if (!info) return; - handleStopWatchingStream(); - setStreamError(null); + if (!info || streamViewersRef.current.has(clientId)) return; + const patch = (change: Partial) => + setWatchedStreams((prev) => (prev[clientId] ? { ...prev, [clientId]: { ...prev[clientId], ...change } } : prev)); const viewer = new StreamViewer(info.id, info.clientId, { send: (message) => socketRef.current?.send(JSON.stringify(message)), - onTrack: setStreamMedia, + onTrack: (media) => patch({ media }), iceServers: [...DEFAULT_ICE_SERVERS, ...turnServers], - onStateChange: setStreamState, - onError: setStreamError, + onStateChange: (state) => patch({ state }), + onError: (error) => patch({ error }), onClosed: () => { // Also reached when the publisher stops, not just via the close button. - streamViewerRef.current = null; - setWatchedStream(null); - setStreamMedia(null); + if (streamViewersRef.current.get(clientId) !== viewer) return; + handleStopWatchingStream(clientId); }, }); - streamViewerRef.current = viewer; - setWatchedStream(info); + streamViewersRef.current.set(clientId, viewer); + setWatchedStreams((prev) => ({ ...prev, [clientId]: { info, media: null, state: "new", error: null } })); viewer.join(); }; @@ -14270,7 +14274,7 @@ function AppInner() { onCancel={() => setStreamSettingsOpen(false)} /> )} - {(watchedStream || (publishState !== "idle" && publishState !== "stopped")) && ( + {(Object.keys(watchedStreams).length > 0 || (publishState !== "idle" && publishState !== "stopped")) && (
{publishState !== "idle" && publishState !== "stopped" && (
@@ -14320,28 +14324,36 @@ function AppInner() { {publishError &&
{publishError}
}
)} - {watchedStream && ( -
+ {Object.entries(watchedStreams).map(([clid, watched]) => ( +
- 📺 {watchedStream.name || t("stream.untitled")} + 📺 {watched.info.name || t("stream.untitled")} - {streamState === "connected" ? t("stream.live") : t(`stream.state.${streamState}`)} + {watched.state === "connected" ? t("stream.live") : t(`stream.state.${watched.state}`)}
{/* muted so autoplay is never blocked; the user unmutes via the controls */} -
- )} + ))}
)} {clientContextMenu && (