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

+100
View File
@@ -1464,6 +1464,106 @@
font-size: 0.85em;
}
/* TS6's stream dialog, rebuilt: labelled rows with a segmented control on the
right. The segments wrap on narrow screens rather than overflowing. */
.ts-stream-settings {
width: min(680px, calc(100vw - 2rem));
}
.ts-stream-settings-section {
margin: 0.9rem 0 0.4rem;
color: var(--accent);
font-size: 1rem;
font-weight: 600;
}
.ts-stream-settings-expand {
display: flex;
width: 100%;
justify-content: space-between;
align-items: center;
padding: 0;
background: none;
border: none;
cursor: pointer;
text-align: left;
}
.ts-stream-settings-row {
display: flex;
gap: 0.75rem;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
padding: 0.35rem 0;
}
.ts-stream-settings-row > span:first-child {
opacity: 0.85;
}
.ts-segmented {
display: flex;
flex-wrap: wrap;
gap: 2px;
padding: 2px;
background: var(--bg-toolbar-2, rgba(0, 0, 0, 0.2));
border: 1px solid var(--border-soft);
border-radius: 999px;
}
.ts-segmented button {
padding: 0.25rem 0.7rem;
background: none;
border: none;
border-radius: 999px;
color: inherit;
cursor: pointer;
white-space: nowrap;
}
.ts-segmented button:disabled {
opacity: 0.4;
cursor: not-allowed;
}
.ts-segmented button.ts-segmented-on {
background: color-mix(in srgb, var(--accent) 25%, transparent);
box-shadow: inset 0 0 0 1px var(--accent);
}
.ts-stream-settings-stepper {
display: flex;
gap: 0.35rem;
align-items: center;
}
.ts-stream-settings-stepper input {
width: 5.5rem;
text-align: center;
}
.ts-stream-settings-stepper button {
width: 1.9rem;
border-radius: 999px;
}
.ts-stream-settings-unit {
opacity: 0.7;
}
.ts-stream-settings-toggle input {
width: 2.4rem;
height: 1.3rem;
}
.ts-stream-settings-go {
border-radius: 999px;
background: var(--accent);
color: #fff;
padding-inline: 1.2rem;
}
.ts-stream-panel-publish {
border-color: var(--accent);
}
+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" && (
+44
View File
@@ -382,6 +382,28 @@ const translations: Record<Lang, Record<string, string>> = {
"publish.screen": "Bildschirm",
"publish.starting": "Wird gestartet…",
"publish.viewers": "{count} Zuschauer",
"publish.dialog.title": "Stream-Einstellungen",
"publish.dialog.basic": "Grundeinstellungen",
"publish.dialog.advanced": "Erweiterte Einstellungen",
"publish.dialog.golive": "Live gehen",
"publish.dialog.cancel": "Zurück",
"publish.preset": "Voreinstellung",
"publish.preset.presentation": "Präsentation",
"publish.audio": "Audio aufnehmen",
"publish.privacy": "Privatsphäre",
"publish.privacy.public": "Öffentlich",
"publish.privacy.contacts": "Kontakte",
"publish.privacy.private": "Privat",
"publish.resolution": "Auflösung",
"publish.res.source": "Quelle",
"publish.fps": "FPS",
"publish.bitrate": "Bitrate",
"publish.audioBitrate": "Audio-Bitrate [Kbps]",
"publish.viewerLimit": "Zuschauerbegrenzung",
"publish.mode": "Verbindungsmodus",
"publish.mode.p2p": "P2P",
"publish.mode.server": "Server",
"publish.mode.serverUnavailable": "Der Server-Modus läuft über TS6s SFU und ist noch nicht implementiert.",
"stream.untitled": "Stream",
"stream.stop": "Stream schließen",
@@ -989,6 +1011,28 @@ const translations: Record<Lang, Record<string, string>> = {
"publish.screen": "Screen",
"publish.starting": "Starting…",
"publish.viewers": "{count} watching",
"publish.dialog.title": "Stream settings",
"publish.dialog.basic": "Basic settings",
"publish.dialog.advanced": "Advanced settings",
"publish.dialog.golive": "Go live",
"publish.dialog.cancel": "Back",
"publish.preset": "Preset",
"publish.preset.presentation": "Presentation",
"publish.audio": "Capture audio",
"publish.privacy": "Privacy",
"publish.privacy.public": "Public",
"publish.privacy.contacts": "Contacts",
"publish.privacy.private": "Private",
"publish.resolution": "Resolution",
"publish.res.source": "Source",
"publish.fps": "FPS",
"publish.bitrate": "Bitrate",
"publish.audioBitrate": "Audio bitrate [Kbps]",
"publish.viewerLimit": "Viewer limit",
"publish.mode": "Connection mode",
"publish.mode.p2p": "P2P",
"publish.mode.server": "Server",
"publish.mode.serverUnavailable": "Server mode goes through TS6's SFU and is not implemented yet.",
"stream.untitled": "Stream",
"stream.stop": "Close stream",
+98 -18
View File
@@ -17,18 +17,32 @@
import type { StreamEvent } from "./stream";
/** Source kind in `setupstream`. A real TS6 screen share sends 3. */
export const STREAM_TYPE_SCREEN = 3;
// The three enums below are read straight out of TS6's own UI bundle
// (main.js), not inferred - see ts6-re/findings §10.
/** `mode` in `setupstream`. 1 is what a real TS6 client sends for P2P. */
export const STREAM_MODE_P2P = 1;
/** `type` in `setupstream`: what is being captured. */
export const StreamSource = {
NONE: 0,
CAMERA: 1,
SCREEN: 2,
WINDOW: 3,
EXISTING_SESSION: 4,
} as const;
/**
* `accessibility` in `setupstream` - TS6's Privacy setting. 1 is what the
* observed client sent; the three UI choices are public / contacts / private,
* and which number maps to which is still unverified.
*/
export const STREAM_ACCESS_DEFAULT = 1;
/** `accessibility` in `setupstream`: TS6's Privacy setting. */
export const StreamAccess = {
NONE: 0,
PUBLIC: 1,
CONTACTS_ONLY: 2,
PRIVATE: 3,
} as const;
/** `mode` in `setupstream`: TS6's Connection Mode. */
export const StreamMode = {
NONE: 0,
P2P: 1,
SFU: 2,
} as const;
const DEFAULT_ICE_SERVERS: RTCIceServer[] = [
{ urls: "stun:turn.teamspeak.com:3478" },
@@ -38,11 +52,25 @@ const DEFAULT_ICE_SERVERS: RTCIceServer[] = [
export interface StreamPublishOptions {
name: string;
bitrate: number;
/** Send the capture's audio track too, when the source has one. */
audio: boolean;
/** One of StreamAccess. */
accessibility: number;
/** One of StreamMode. Only P2P is implemented; SFU needs the mediasoup path. */
mode: number;
/** 0 means unlimited. */
viewerLimit?: number;
viewerLimit: number;
/** Capture height in pixels; 0 keeps the source's own resolution. */
height: number;
/** Capture frame rate; 0 leaves it to the browser. */
fps: number;
videoBitrateKbps: number;
audioBitrateKbps: number;
/**
* What the encoder should protect when it runs out of bitrate. Slides and
* code want "detail" (keep text sharp, drop frames); a game wants "motion".
*/
contentHint: "motion" | "detail";
}
export interface StreamPublisherOptions {
@@ -69,6 +97,7 @@ export class StreamPublisher {
private readonly viewers = new Map<number, Viewer>();
private media: MediaStream | null = null;
private state: PublishState = "idle";
private options: StreamPublishOptions | null = null;
/** Assigned by the server, not by us; null until notifystreamstarted. */
streamId: string | null = null;
@@ -96,9 +125,15 @@ export class StreamPublisher {
}
this.setState("starting");
this.options = options;
const video: MediaTrackConstraints = {};
if (options.height > 0) video.height = { ideal: options.height };
if (options.fps > 0) video.frameRate = { ideal: options.fps };
try {
this.media = await navigator.mediaDevices.getDisplayMedia({
video: true,
video: Object.keys(video).length > 0 ? video : true,
audio: options.audio,
});
} catch (err) {
@@ -107,6 +142,8 @@ export class StreamPublisher {
return;
}
for (const track of this.media.getVideoTracks()) track.contentHint = options.contentHint;
// The browser's own "stop sharing" bar bypasses our UI entirely, so the
// track ending is the authoritative signal that the stream is over.
for (const track of this.media.getTracks()) {
@@ -116,11 +153,14 @@ export class StreamPublisher {
this.opts.send({
type: "setupStream",
name: options.name,
streamType: STREAM_TYPE_SCREEN,
bitrate: options.bitrate,
accessibility: STREAM_ACCESS_DEFAULT,
mode: STREAM_MODE_P2P,
viewerLimit: options.viewerLimit ?? 0,
// The browser's own picker decides whether this is a whole screen or a
// single window, so the type is read back off the track rather than
// asked for up front.
streamType: this.detectSourceType(),
bitrate: options.videoBitrateKbps * 1000,
accessibility: options.accessibility,
mode: options.mode,
viewerLimit: options.viewerLimit,
audio: options.audio && this.media.getAudioTracks().length > 0,
});
}
@@ -184,6 +224,7 @@ export class StreamPublisher {
try {
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
await this.applyBitrates(pc);
this.opts.send({
type: "respondJoinStream",
streamId: this.streamId,
@@ -265,6 +306,44 @@ export class StreamPublisher {
}
}
/**
* Caps each sender at the configured bitrate.
*
* `setupstream`'s `bitrate` is only what the stream *advertises* - the
* server does not even echo our value back unchanged, and it certainly does
* not reach the encoder. The actual limit is a sender parameter, and the
* encodings it lives on only exist once a local description is set.
*/
private async applyBitrates(pc: RTCPeerConnection): Promise<void> {
const options = this.options;
if (!options) return;
for (const sender of pc.getSenders()) {
const kbps = sender.track?.kind === "audio" ? options.audioBitrateKbps : options.videoBitrateKbps;
if (!sender.track || kbps <= 0) continue;
const params = sender.getParameters();
if (!params.encodings || params.encodings.length === 0) params.encodings = [{}];
for (const encoding of params.encodings) encoding.maxBitrate = kbps * 1000;
try {
await sender.setParameters(params);
} catch (err) {
// Not fatal - the stream still runs, just uncapped.
console.warn("[publish] could not apply the bitrate cap:", describe(err));
}
}
}
/**
* Maps the capture to TS6's source enum.
*
* `displaySurface` is what the user actually picked in the browser's own
* share dialog, so it is more truthful than anything we could ask for in
* advance. A browser tab has no TS6 equivalent and reads closest to a window.
*/
private detectSourceType(): number {
const surface = this.media?.getVideoTracks()[0]?.getSettings().displaySurface;
return surface === "monitor" ? StreamSource.SCREEN : StreamSource.WINDOW;
}
private dropViewer(clid: number): void {
const viewer = this.viewers.get(clid);
if (!viewer) return;
@@ -286,6 +365,7 @@ export class StreamPublisher {
for (const track of this.media?.getTracks() ?? []) track.stop();
this.media = null;
this.streamId = null;
this.options = null;
this.notifyViewers();
this.setState(state);
}