diff --git a/web/src/App.tsx b/web/src/App.tsx index 2c6dc49..1f9eaeb 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -137,6 +137,15 @@ import { } from "./publish"; import { DEFAULT_ICE_SERVERS, StreamViewer, parseStreamInfo, type StreamEvent, type StreamInfo } from "./stream"; +type PublishSource = StreamPublishOptions["source"]; + +interface PublishTile { + state: PublishState; + viewers: number[]; + pending: [number, string][]; + error: string | null; +} + interface WatchedStream { info: StreamInfo; media: MediaStream | null; @@ -2756,17 +2765,19 @@ function SegmentedChoice({ function StreamSettingsDialog({ ownName, + initialSource, onStart, onCancel, }: { ownName: string; + initialSource: "display" | "camera"; onStart: (options: StreamPublishOptions) => void; onCancel: () => void; }) { const t = useT(); const backdrop = useBackdropDismiss(onCancel); const [preset, setPreset] = useState("720"); - const [source, setSource] = useState<"display" | "camera">("display"); + const [source, setSource] = useState<"display" | "camera">(initialSource); const [audio, setAudio] = useState(true); const [accessibility, setAccessibility] = useState(StreamAccess.PUBLIC); const [advanced, setAdvanced] = useState(false); @@ -9023,12 +9034,9 @@ function AppInner() { /** Separate browser window the stream grid is portalled into, if popped out. */ const [streamPopout, setStreamPopout] = useState(null); const [popoutSize, setPopoutSize] = useState({ width: 0, height: 0 }); - const [publishState, setPublishState] = useState("idle"); - const [publishViewers, setPublishViewers] = useState([]); - const [publishPending, setPublishPending] = useState<[number, string][]>([]); - const [publishError, setPublishError] = useState(null); - const publisherRef = useRef(null); - const publishPreviewRef = useRef(null); + /** Our own broadcasts, at most one per source, so screen and camera can run together. */ + const [publishes, setPublishes] = useState>>({}); + const publishersRef = useRef>(new Map()); const [streamSettingsOpen, setStreamSettingsOpen] = useState(false); /** Clients we already pulled a `notifystreaminfo` for, so we ask only once. */ const requestedStreamInfoRef = useRef>(new Set()); @@ -10405,7 +10413,7 @@ function AppInner() { } } for (const viewer of streamViewersRef.current.values()) viewer.handleEvent(streamEvent); - publisherRef.current?.handleEvent(streamEvent); + for (const publisher of publishersRef.current.values()) publisher.handleEvent(streamEvent); break; } case "poke": { @@ -10484,11 +10492,7 @@ function AppInner() { handleStopWatchingStream(); setStreamInfos({}); requestedStreamInfoRef.current.clear(); - publisherRef.current?.stop(); - publisherRef.current = null; - setPublishState("idle"); - setPublishViewers([]); - setPublishPending([]); + handleStopPublishing(); removeSession(sessionId, { skipSocketClose: true }); break; } @@ -12150,17 +12154,10 @@ function AppInner() { }); }, [clients]); - 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. - const video = publishPreviewRef.current; - if (!video) return; - const media = publisherRef.current?.previewStream ?? null; - if (video.srcObject !== media) video.srcObject = media; - }, [publishState, publishViewers, streamPopout]); - - const streamTileCount = - Object.keys(watchedStreams).length + (publishState !== "idle" && publishState !== "stopped" ? 1 : 0); + const activePublishes = (Object.entries(publishes) as [PublishSource, PublishTile][]).filter( + ([, tile]) => tile.state !== "idle" && tile.state !== "stopped", + ); + const streamTileCount = Object.keys(watchedStreams).length + activePublishes.length; const openStreamPopout = () => { const win = window.open("", "webspeak3-streams", "popup,width=1280,height=760"); @@ -12236,16 +12233,29 @@ function AppInner() { viewer.join(); }; - const handleStopPublishing = () => { - publisherRef.current?.stop(); - publisherRef.current = null; - setPublishState("idle"); - setPublishViewers([]); - setPublishPending([]); + /** Stops one broadcast, or all of them without a source. */ + const handleStopPublishing = (source?: PublishSource) => { + const publishers = publishersRef.current; + for (const key of source ? [source] : [...publishers.keys()]) { + const publisher = publishers.get(key); + publishers.delete(key); + publisher?.stop(); + } + setPublishes((prev) => { + if (!source) return {}; + const next = { ...prev }; + delete next[source]; + return next; + }); }; + const publishingAll = publishersRef.current.size >= 2; + // Two publishers waiting for notifystreamstarted at once could not tell + // whose id it is, so a second one waits until the first is live. + const publishStarting = activePublishes.some(([, tile]) => tile.state === "starting"); + const handleStreamButton = () => { - if (publisherRef.current) { + if (publishingAll) { handleStopPublishing(); return; } @@ -12256,28 +12266,32 @@ function AppInner() { clients.find((c) => c.id === clid)?.name ?? `#${clid}`; const handleStartPublishing = (options: StreamPublishOptions) => { - if (ownClientId === null) return; + if (ownClientId === null || publishStarting) return; setStreamSettingsOpen(false); - setPublishError(null); + const source = options.source; + // Same source again replaces the running one. + if (publishersRef.current.has(source)) handleStopPublishing(source); + const patch = (change: Partial) => + setPublishes((prev) => (prev[source] ? { ...prev, [source]: { ...prev[source], ...change } } : prev)); const publisher = new StreamPublisher({ send: (message) => socketRef.current?.send(JSON.stringify(message)), ownClientId, onStateChange: (state) => { - setPublishState(state); // "stopped" is also how the browser's own stop-sharing bar reports in, - // so the ref has to be dropped here and not only in the click handler. + // so the publisher has to be dropped here and not only on the ✕. if (state === "stopped" || state === "idle") { - publisherRef.current = null; - setPublishViewers([]); - setPublishPending([]); + if (publishersRef.current.get(source) === publisher) handleStopPublishing(source); + return; } + patch({ state }); }, - onViewersChange: setPublishViewers, - onPendingChange: setPublishPending, - onError: setPublishError, + onViewersChange: (viewers) => patch({ viewers }), + onPendingChange: (pending) => patch({ pending }), + onError: (error) => patch({ error }), iceServers: [...DEFAULT_ICE_SERVERS, ...turnServers], }); - publisherRef.current = publisher; + publishersRef.current.set(source, publisher); + setPublishes((prev) => ({ ...prev, [source]: { state: "idle", viewers: [], pending: [], error: null } })); void publisher.start(options); }; @@ -12865,7 +12879,7 @@ function AppInner() { const micLabel = !micOn ? t("toolbar.micEnable") : inputMuted ? t("toolbar.micUnmute") : t("toolbar.micMute"); const streamLabel = DEMO_MODE ? t("publish.demoUnavailable") - : `${publishState === "live" ? t("publish.stop") : t("publish.start")} (${t("publish.alpha")})`; + : `${publishingAll ? t("publish.stop") : t("publish.start")} (${t("publish.alpha")})`; const themeClassName = `ts-app ts-theme-${theme}${designThemeClassName(designTheme)}${ activeCustomTheme ? " ts-design-custom" : "" @@ -13689,16 +13703,16 @@ function AppInner() { )} {TAB_AUDIO_SUPPORTED && ( {/* muted: this is our own capture playing back locally */} -