Files
WebSpeak-client-for-TeamSpeak/src/shared/screen-share.ts
T
LainHE bcc78177c2 fix(screen-share): keep the announced viewer count in step with the roster
Two smaller gaps left over from removing the inline avatars.

The announced viewerCount summed the viewer entry ids and the native viewer
ids, while the roster silently dropped ids whose participant had vanished
from the entry map. A client that disappears without passing through the
leave path therefore inflated the count for as long as the stream lived.
Prune those ids, and derive the count in the same call that builds the
roster so the two can no longer disagree.

The legacy inline-avatar path capped each entry at 128 KiB but placed no
bound on the roster total, which still allowed 64 x 128 KiB in a single
broadcast. Bound the total as well; entries past the budget fall back to
the client-side member lookup like any other viewer without an avatar.

Both behaviours are covered by new tests: a dropped entry no longer
inflates the count, and eight 128 KiB avatars are trimmed to the budget
(measured at 1048576 bytes without the bound).
2026-10-16 09:50:26 +08:00

253 lines
9.4 KiB
TypeScript

/**
* Screen-share signaling for browser peers and native TeamSpeak peers.
*
* Media never passes through this module. The gateway only authenticates the
* connected TeamSpeak session and relays SDP/ICE messages. Browser peers use
* the configured ICE servers only for candidate discovery/negotiation; this
* module never handles the resulting media packets.
*/
export interface ScreenShareIceServer {
urls: string | string[];
username?: string;
credential?: string;
}
/** TeamSpeak's public ICE services are STUN-only, so media remains peer-to-peer. */
export const DEFAULT_SCREEN_SHARE_ICE_SERVERS: readonly ScreenShareIceServer[] = [
{ urls: "stun:turn.teamspeak.com:3478" },
{ urls: "stun:turn2.teamspeak.com:3478" },
];
const SCREEN_SHARE_ICE_URL_PATTERN = /^(?:stun|stuns|turn|turns):/i;
const MAX_ICE_SERVER_COUNT = 8;
const MAX_ICE_URL_LENGTH = 512;
const MAX_ICE_CREDENTIAL_LENGTH = 512;
/**
* Keep ICE configuration server-owned and bounded before exposing it to a
* browser. TURN is accepted only as an explicitly configured external server;
* the WebSpeak gateway is never inserted into this list.
*/
export function normalizeScreenShareIceServers(value?: readonly unknown[]): ScreenShareIceServer[] {
const source: readonly unknown[] = value ?? DEFAULT_SCREEN_SHARE_ICE_SERVERS;
const normalized: ScreenShareIceServer[] = [];
const seen = new Set<string>();
for (const raw of source) {
if (!raw || typeof raw !== "object" || Array.isArray(raw)) continue;
const candidate = raw as Record<string, unknown>;
const rawUrls = candidate.urls;
const urls = (Array.isArray(rawUrls) ? rawUrls : [rawUrls])
.filter((url): url is string => typeof url === "string" && url.trim().length > 0 && url.trim().length <= MAX_ICE_URL_LENGTH)
.map((url) => url.trim())
.filter((url) => SCREEN_SHARE_ICE_URL_PATTERN.test(url));
const uniqueUrls = [...new Set(urls)];
if (!uniqueUrls.length) continue;
const username = typeof candidate.username === "string" && candidate.username.length <= MAX_ICE_CREDENTIAL_LENGTH
? candidate.username
: undefined;
const credential = typeof candidate.credential === "string" && candidate.credential.length <= MAX_ICE_CREDENTIAL_LENGTH
? candidate.credential
: undefined;
const key = JSON.stringify([uniqueUrls, username ?? ""]);
if (seen.has(key)) continue;
seen.add(key);
normalized.push({
urls: uniqueUrls.length === 1 ? uniqueUrls[0] : uniqueUrls,
...(username !== undefined ? { username } : {}),
...(credential !== undefined ? { credential } : {}),
});
if (normalized.length >= MAX_ICE_SERVER_COUNT) break;
}
if (normalized.length) return normalized;
return DEFAULT_SCREEN_SHARE_ICE_SERVERS.map((server) => ({ ...server }));
}
export type ScreenSharePeerSignal =
| {
kind: "offer" | "answer";
sdp: string;
}
| {
kind: "iceCandidate";
candidate: string;
sdpMid?: string | null;
sdpMLineIndex?: number | null;
}
| {
kind: "close";
};
/**
* Ceiling on the total inline avatar bytes a client accepts from a legacy
* gateway for one viewer roster. Current gateways send client ids instead of
* avatars, so this bounds only the compatibility path; a per-entry limit alone
* would still admit 64 x 128 KiB in a single broadcast.
*/
export const SCREEN_SHARE_LEGACY_AVATAR_BUDGET = 256 * 1024;
export interface ScreenShareViewerDescription {
peerId: string;
nickname: string;
/**
* TeamSpeak client id. The gateway sends this instead of an inline avatar so
* a receiving client can resolve the avatar it already holds for that member
* from the directory, rather than the roster carrying the image again.
*/
clientId?: number;
/**
* Legacy inline avatar. Current gateways do not set this; it is still parsed
* so a newer web client keeps rendering avatars against an older gateway.
*/
avatar?: string;
}
export interface ScreenShareStreamDescription {
streamId: string;
source: "browser" | "teamspeak";
ownerPeerId: string;
/** TeamSpeak client id used to match a stream to the member card. */
ownerClientId?: number;
ownerNickname: string;
name: string;
audio: boolean;
createdAt: number;
viewerCount: number;
viewers: ScreenShareViewerDescription[];
}
export type ScreenShareClientMessage =
| {
type: "screenShareList";
}
| {
type: "screenShareStart";
requestId?: string;
name?: string;
audio?: boolean;
}
| {
type: "screenShareStop";
requestId?: string;
streamId: string;
}
| {
type: "screenShareJoin";
requestId?: string;
streamId: string;
}
| {
type: "screenShareLeave";
requestId?: string;
streamId: string;
}
| {
type: "screenShareSignal";
streamId: string;
targetPeerId: string;
signal: ScreenSharePeerSignal;
};
export type ScreenShareMessageParseResult =
| ScreenShareClientMessage
| { error: { code: string; message: string } };
const MAX_STREAM_ID_LENGTH = 128;
const MAX_PEER_ID_LENGTH = 128;
const MAX_NAME_LENGTH = 120;
const MAX_SDP_LENGTH = 256 * 1024;
const MAX_CANDIDATE_LENGTH = 8 * 1024;
export function parseScreenShareMessage(raw: string): ScreenShareMessageParseResult | null {
let value: unknown;
try {
value = JSON.parse(raw);
} catch {
return null;
}
if (!isRecord(value) || typeof value.type !== "string" || !value.type.startsWith("screenShare")) return null;
const requestId = parseRequestId(value.requestId);
if (requestId === false) return invalid("INVALID_SCREEN_SHARE_REQUEST", "屏幕共享请求标识无效");
switch (value.type) {
case "screenShareList":
return { type: "screenShareList" };
case "screenShareStart": {
const name = value.name === undefined ? undefined : parseName(value.name);
if (name === false) return invalid("INVALID_SCREEN_SHARE_NAME", "屏幕共享名称无效");
if (value.audio !== undefined && typeof value.audio !== "boolean") return invalid("INVALID_SCREEN_SHARE_AUDIO", "共享音频选项无效");
return {
type: "screenShareStart",
...(requestId ? { requestId } : {}),
...(name ? { name } : {}),
...(value.audio === true ? { audio: true } : {}),
};
}
case "screenShareStop":
case "screenShareJoin":
case "screenShareLeave": {
const streamId = parseIdentifier(value.streamId, MAX_STREAM_ID_LENGTH);
if (!streamId) return invalid("INVALID_SCREEN_SHARE_STREAM", "屏幕共享标识无效");
return {
type: value.type,
...(requestId ? { requestId } : {}),
streamId,
};
}
case "screenShareSignal": {
const streamId = parseIdentifier(value.streamId, MAX_STREAM_ID_LENGTH);
const targetPeerId = parseIdentifier(value.targetPeerId, MAX_PEER_ID_LENGTH);
const signal = parseScreenShareSignal(value.signal);
if (!streamId) return invalid("INVALID_SCREEN_SHARE_STREAM", "屏幕共享标识无效");
if (!targetPeerId) return invalid("INVALID_SCREEN_SHARE_PEER", "屏幕共享对端无效");
if (!signal) return invalid("INVALID_SCREEN_SHARE_SIGNAL", "屏幕共享信令无效");
return { type: "screenShareSignal", streamId, targetPeerId, signal };
}
default:
return invalid("UNKNOWN_SCREEN_SHARE_MESSAGE", "不支持的屏幕共享消息");
}
}
export function parseScreenShareSignal(value: unknown): ScreenSharePeerSignal | null {
if (!isRecord(value) || typeof value.kind !== "string") return null;
if (value.kind === "close") return { kind: "close" };
if (value.kind === "offer" || value.kind === "answer") {
if (typeof value.sdp !== "string" || value.sdp.length === 0 || value.sdp.length > MAX_SDP_LENGTH) return null;
return { kind: value.kind, sdp: value.sdp };
}
if (value.kind !== "iceCandidate" || typeof value.candidate !== "string" || value.candidate.length > MAX_CANDIDATE_LENGTH) return null;
if (value.sdpMid !== undefined && value.sdpMid !== null && typeof value.sdpMid !== "string") return null;
const sdpMLineIndex = value.sdpMLineIndex;
if (sdpMLineIndex !== undefined && sdpMLineIndex !== null && (typeof sdpMLineIndex !== "number" || !Number.isInteger(sdpMLineIndex) || sdpMLineIndex < 0 || sdpMLineIndex > 255)) return null;
return {
kind: "iceCandidate",
candidate: value.candidate,
...(value.sdpMid === null || typeof value.sdpMid === "string" ? { sdpMid: value.sdpMid } : {}),
...(sdpMLineIndex === null || typeof sdpMLineIndex === "number" ? { sdpMLineIndex } : {}),
};
}
function parseRequestId(value: unknown): string | undefined | false {
if (value === undefined) return undefined;
return typeof value === "string" && value.length > 0 && value.length <= 64 ? value : false;
}
function parseName(value: unknown): string | false {
if (typeof value !== "string") return false;
const name = value.trim();
return name.length > 0 && name.length <= MAX_NAME_LENGTH ? name : false;
}
function parseIdentifier(value: unknown, maxLength: number): string | null {
if (typeof value !== "string" || value.length === 0 || value.length > maxLength) return null;
return value;
}
function invalid(code: string, message: string): { error: { code: string; message: string } } {
return { error: { code, message } };
}
function isRecord(value: unknown): value is Record<string, unknown> {
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
}