web: TS6's stream-start options in the publish dialog

TS6 shows a settings dialog before going live; this builds the same set
into webspeak3, split into Basic (preset, capture audio, privacy) and an
Advanced section (resolution, FPS, bitrate, audio bitrate, viewer limit,
connection mode).

The three numeric setupstream fields are enums lifted out of TS6's own
main.js rather than guessed (findings §10): source NONE/CAMERA/SCREEN/
WINDOW/EXISTING_SESSION, accessibility NONE/PUBLIC/CONTACTS_ONLY/PRIVATE,
mode NONE/P2P/SFU. We had been sending type=3 (WINDOW) unconditionally;
detectSourceType() now picks SCREEN for displaySurface === "monitor".

Resolution, FPS and the two bitrates are not protocol fields. They are
applied locally: capture constraints, track.contentHint, and
sender.setParameters() with encoding.maxBitrate. bitrate= on the wire is
advisory - the server echoes back its own number either way.

Connection mode "Server" (SFU) is rendered disabled with a tooltip. That
path is a separate transport (requeststreamsfuaccessinfo -> mediasoup)
that we have not built, and announcing mode=2 without it would publish a
stream nobody could watch.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
MoepchiandClaude Opus 5 committed 2026-09-11 05:26:33 +00:00
1 parent 7083e8e29f
commit 115cd7150e
4 files changed
+537 -27

No files matched your search

+295 -9
View File
@@ -26,7 +26,13 @@ import {
pickAudioOutputDevice,
} from "./voice";
import { LanguageProvider, useLanguage, useT, type LangPref } from "./i18n";
import { StreamPublisher, type PublishState } from "./publish";
import {
StreamAccess,
StreamMode,
StreamPublisher,
type PublishState,
type StreamPublishOptions,
} from "./publish";
import { StreamViewer, parseStreamInfo, type StreamEvent, type StreamInfo } from "./stream";
import { DEMO_HOST, DEMO_MODE, DemoSocket } from "./demoMode";
import {
@@ -1586,6 +1592,277 @@ function ChannelPasswordDialog({
);
}
/**
* Presets from TS6's own stream dialog. Each one fills in the advanced
* fields; "source" keeps the capture's native resolution and "presentation"
* trades frame rate for sharp text, which is what the encoder's content hint
* is for.
*/
const STREAM_PRESETS: Record<
string,
{ height: number; fps: number; videoBitrateKbps: number; contentHint: "motion" | "detail" }
> = {
"360": { height: 360, fps: 30, videoBitrateKbps: 1000, contentHint: "motion" },
"480": { height: 480, fps: 30, videoBitrateKbps: 1500, contentHint: "motion" },
"720": { height: 720, fps: 30, videoBitrateKbps: 2500, contentHint: "motion" },
"1080": { height: 1080, fps: 30, videoBitrateKbps: 4000, contentHint: "motion" },
"1440": { height: 1440, fps: 30, videoBitrateKbps: 6000, contentHint: "motion" },
source: { height: 0, fps: 60, videoBitrateKbps: 8000, contentHint: "motion" },
presentation: { height: 0, fps: 5, videoBitrateKbps: 3000, contentHint: "detail" },
};
const RESOLUTION_CHOICES = [360, 480, 720, 1080, 1440, 0];
const FPS_CHOICES = [5, 30, 60];
const AUDIO_BITRATE_CHOICES = [64, 96, 128, 192, 256, 320];
function SegmentedChoice<T extends string | number>({
value,
options,
onChange,
disabled,
}: {
value: T;
options: { value: T; label: string; title?: string; disabled?: boolean }[];
onChange: (value: T) => void;
disabled?: boolean;
}) {
return (
<div className="ts-segmented">
{options.map((option) => (
<button
key={String(option.value)}
type="button"
className={option.value === value ? "ts-segmented-on" : ""}
disabled={disabled || option.disabled}
title={option.title}
onClick={() => onChange(option.value)}
>
{option.label}
</button>
))}
</div>
);
}
function StreamSettingsDialog({
defaultName,
onStart,
onCancel,
}: {
defaultName: string;
onStart: (options: StreamPublishOptions) => void;
onCancel: () => void;
}) {
const t = useT();
const backdrop = useBackdropDismiss(onCancel);
const [preset, setPreset] = useState<string | null>("720");
const [audio, setAudio] = useState(true);
const [accessibility, setAccessibility] = useState<number>(StreamAccess.PUBLIC);
const [advanced, setAdvanced] = useState(false);
const [height, setHeight] = useState(STREAM_PRESETS["720"]!.height);
const [fps, setFps] = useState(STREAM_PRESETS["720"]!.fps);
const [videoBitrateKbps, setVideoBitrateKbps] = useState(STREAM_PRESETS["720"]!.videoBitrateKbps);
const [audioBitrateKbps, setAudioBitrateKbps] = useState(128);
const [viewerLimit, setViewerLimit] = useState(0);
const [mode, setMode] = useState<number>(StreamMode.P2P);
const [contentHint, setContentHint] = useState<"motion" | "detail">("motion");
const applyPreset = (key: string) => {
const p = STREAM_PRESETS[key];
if (!p) return;
setPreset(key);
setHeight(p.height);
setFps(p.fps);
setVideoBitrateKbps(p.videoBitrateKbps);
setContentHint(p.contentHint);
};
// Any hand-tuned value means the result is no longer one of the presets, so
// the highlight is dropped rather than left lying about what will be sent.
const custom = <T,>(setter: (value: T) => void) => (value: T) => {
setter(value);
setPreset(null);
};
const resolutionLabel = (value: number) => (value === 0 ? t("publish.res.source") : String(value));
return (
<div className="ts-dialog-backdrop" {...backdrop}>
<div className="ts-dialog ts-stream-settings" onClick={(e) => e.stopPropagation()}>
<div className="ts-dialog-titlebar">
<span>{t("publish.dialog.title")}</span>
<button onClick={onCancel} title={t("dialog.close")}>
✕
</button>
</div>
<div className="ts-dialog-body">
<h4 className="ts-stream-settings-section">{t("publish.dialog.basic")}</h4>
<div className="ts-stream-settings-row">
<span>{t("publish.preset")}</span>
<SegmentedChoice
value={preset ?? ""}
onChange={applyPreset}
options={[
...["360", "480", "720", "1080", "1440"].map((k) => ({ value: k, label: k })),
{ value: "source", label: t("publish.res.source") },
{ value: "presentation", label: t("publish.preset.presentation") },
]}
/>
</div>
<div className="ts-stream-settings-row">
<span>{t("publish.audio")}</span>
<label className="ts-stream-settings-toggle">
<input type="checkbox" checked={audio} onChange={(e) => setAudio(e.target.checked)} />
<span />
</label>
</div>
<div className="ts-stream-settings-row">
<span>{t("publish.privacy")}</span>
<SegmentedChoice
value={accessibility}
onChange={setAccessibility}
options={[
{ value: StreamAccess.PUBLIC, label: `🌐 ${t("publish.privacy.public")}` },
{ value: StreamAccess.CONTACTS_ONLY, label: `👥 ${t("publish.privacy.contacts")}` },
{ value: StreamAccess.PRIVATE, label: `🔒 ${t("publish.privacy.private")}` },
]}
/>
</div>
<button
type="button"
className="ts-stream-settings-section ts-stream-settings-expand"
onClick={() => setAdvanced((v) => !v)}
>
{t("publish.dialog.advanced")} <span>{advanced ? "⌄" : "›"}</span>
</button>
{advanced && (
<>
<div className="ts-stream-settings-row">
<span>{t("publish.resolution")}</span>
<SegmentedChoice
value={height}
onChange={custom(setHeight)}
options={RESOLUTION_CHOICES.map((v) => ({ value: v, label: resolutionLabel(v) }))}
/>
</div>
<div className="ts-stream-settings-row">
<span>{t("publish.fps")}</span>
<SegmentedChoice
value={fps}
onChange={custom(setFps)}
options={FPS_CHOICES.map((v) => ({ value: v, label: String(v) }))}
/>
</div>
<div className="ts-stream-settings-row">
<span>{t("publish.bitrate")}</span>
<div className="ts-stream-settings-stepper">
<button type="button" onClick={() => custom(setVideoBitrateKbps)(Math.max(200, videoBitrateKbps - 500))}>
−
</button>
<input
type="number"
min={200}
step={100}
value={videoBitrateKbps}
onChange={(e) => custom(setVideoBitrateKbps)(Math.max(200, Number(e.target.value) || 200))}
/>
<span className="ts-stream-settings-unit">Kbps</span>
<button type="button" onClick={() => custom(setVideoBitrateKbps)(videoBitrateKbps + 500)}>
+
</button>
</div>
</div>
<div className="ts-stream-settings-row">
<span>{t("publish.audioBitrate")}</span>
<SegmentedChoice
value={audioBitrateKbps}
onChange={custom(setAudioBitrateKbps)}
disabled={!audio}
options={AUDIO_BITRATE_CHOICES.map((v) => ({ value: v, label: String(v) }))}
/>
</div>
<div className="ts-stream-settings-row">
<span>{t("publish.viewerLimit")}</span>
<div className="ts-stream-settings-stepper">
<button type="button" onClick={() => setViewerLimit(Math.max(0, viewerLimit - 1))}>
−
</button>
<input
type="number"
min={0}
value={viewerLimit}
onChange={(e) => setViewerLimit(Math.max(0, Number(e.target.value) || 0))}
/>
<button type="button" onClick={() => setViewerLimit(viewerLimit + 1)}>
+
</button>
</div>
</div>
<div className="ts-stream-settings-row">
<span>{t("publish.mode")}</span>
<SegmentedChoice
value={mode}
onChange={setMode}
options={[
{ value: StreamMode.P2P, label: `👥 ${t("publish.mode.p2p")}` },
{
value: StreamMode.SFU,
label: `🖧 ${t("publish.mode.server")}`,
// The SFU path is a different transport entirely
// (requestsfuaccessinfo + mediasoup) and is not built
// yet; offering it would announce a stream nobody can
// watch.
disabled: true,
title: t("publish.mode.serverUnavailable"),
},
]}
/>
</div>
</>
)}
</div>
<div className="ts-dialog-buttons">
<div className="ts-dialog-buttons-right">
<button type="button" onClick={onCancel}>
{t("publish.dialog.cancel")}
</button>
<button
type="button"
className="ts-stream-settings-go"
onClick={() =>
onStart({
name: defaultName,
audio,
accessibility,
mode,
viewerLimit,
height,
fps,
videoBitrateKbps,
audioBitrateKbps,
contentHint,
})
}
>
{t("publish.dialog.golive")}
</button>
</div>
</div>
</div>
</div>
);
}
function UpdatePrompt() {
const t = useT();
const {
@@ -5386,6 +5663,7 @@ function AppInner() {
const [publishError, setPublishError] = useState<string | null>(null);
const publisherRef = useRef<StreamPublisher | null>(null);
const publishPreviewRef = useRef<HTMLVideoElement | null>(null);
const [streamSettingsOpen, setStreamSettingsOpen] = useState(false);
const streamVideoRef = useRef<HTMLVideoElement | null>(null);
/** Clients we already pulled a `notifystreaminfo` for, so we ask only once. */
const requestedStreamInfoRef = useRef<Set<number>>(new Set());
@@ -7655,12 +7933,17 @@ function AppInner() {
setPublishViewers([]);
};
const handleStartPublishing = () => {
if (ownClientId === null) return;
const handleStreamButton = () => {
if (publisherRef.current) {
handleStopPublishing();
return;
}
setStreamSettingsOpen(true);
};
const handleStartPublishing = (options: StreamPublishOptions) => {
if (ownClientId === null) return;
setStreamSettingsOpen(false);
setPublishError(null);
const publisher = new StreamPublisher({
send: (message) => socketRef.current?.send(JSON.stringify(message)),
@@ -7678,11 +7961,7 @@ function AppInner() {
onError: setPublishError,
});
publisherRef.current = publisher;
void publisher.start({
name: `${ownClient?.name ?? "WebSpeak3"} - ${t("publish.screen")}`,
bitrate: 1_500_000,
audio: true,
});
void publisher.start(options);
};
const handleShowClientConnectionInfo = (clientId: number, clientName: string) => {
@@ -8785,7 +9064,7 @@ function AppInner() {
</label>
<button
className={`ts-icon-button${publishState === "live" ? " ts-mic-on" : ""}`}
onClick={handleStartPublishing}
onClick={handleStreamButton}
disabled={!connected || ownClientId === null}
title={publishState === "live" ? t("publish.stop") : t("publish.start")}
>
@@ -9329,6 +9608,13 @@ function AppInner() {
</div>
))}
{streamSettingsOpen && (
<StreamSettingsDialog
defaultName={`${ownClient?.name ?? "WebSpeak3"} - ${t("publish.screen")}`}
onStart={handleStartPublishing}
onCancel={() => setStreamSettingsOpen(false)}
/>
)}
{(watchedStream || (publishState !== "idle" && publishState !== "stopped")) && (
<div className="ts-stream-panels">
{publishState !== "idle" && publishState !== "stopped" && (