stream: answer args are the bare SDP, not {sdp}

TeamSpeak.dll's inbound signaling dispatcher LEAs a key literal in every
branch that reads an object out of `args` (decision/offer for joinResponse,
sdp/mid/mLine for iceCandidate) and none in the answer and offer branches,
whose handler ends in StreamP2P::SetAnswerString(const std::string&).

That is static evidence only - TS6 has never had an answer from us to react
to, and it drops an unreadable payload silently - so the viewer re-sends the
object form after 5s if nothing connected. The first live test settles it.

Inbound offer/reconnectOffer now accept both shapes.

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 6107501445
commit 087b8a90c5
1 file changed
+69 -7
+69 -7
View File
@@ -21,10 +21,17 @@ export interface StreamEvent {
args: Record<string, string>;
}
/** The payload shape inside `notifystreamsignaling`'s `json` argument. */
/**
* The payload shape inside `notifystreamsignaling`'s `json` argument.
*
* `args` is not always an object: TS6's dispatcher reads it as a bare string
* for `offer`/`reconnectOffer`/`answer` and as an object only for
* `iceCandidate` (`sdp`/`mid`/`mLine`) and `joinResponse`
* (`decision`/`offer`) - see the note on ANSWER_ARGS_ARE_A_BARE_STRING.
*/
interface SignalingMessage {
cmd: string;
args: Record<string, unknown>;
args: unknown;
}
export interface StreamInfo {
@@ -54,6 +61,23 @@ export interface StreamViewerOptions {
iceServers?: RTCIceServer[];
}
/**
* Whether `{cmd:"answer"}` carries the SDP as `args` itself rather than as
* `args.sdp`.
*
* Derived from TeamSpeak.dll: the inbound dispatcher LEAs a key literal in
* every branch that reads an object (`decision`/`offer` for joinResponse,
* `sdp`/`mid`/`mLine` for iceCandidate) and none at all in the `answer` and
* `offer` branches, whose handler ends in `StreamP2P::SetAnswerString(const
* std::string&)`. That is static evidence, not a capture - TS6 has never had
* an answer from us to react to - so the viewer falls back to the object form
* if the connection has not come up in time (see ANSWER_FALLBACK_MS).
*/
const ANSWER_ARGS_ARE_A_BARE_STRING = true;
/** How long to wait before retrying the answer in the other shape. */
const ANSWER_FALLBACK_MS = 5000;
const DEFAULT_ICE_SERVERS: RTCIceServer[] = [
{ urls: "stun:turn.teamspeak.com:3478" },
{ urls: "stun:turn2.teamspeak.com:3478" },
@@ -90,6 +114,7 @@ export class StreamViewer {
private pendingRemoteCandidates: RTCIceCandidateInit[] = [];
private remoteDescriptionSet = false;
private closed = false;
private answerFallbackTimer: ReturnType<typeof setTimeout> | null = null;
readonly streamId: string;
readonly peerClientId: number;
@@ -153,12 +178,35 @@ export class StreamViewer {
const answer = await pc.createAnswer();
await pc.setLocalDescription(answer);
this.signal("answer", { sdp: answer.sdp });
this.sendAnswer(answer.sdp ?? "");
} catch (err) {
this.fail(`Could not answer the stream offer: ${describe(err)}`);
}
}
/**
* Sends the answer in the shape the DLL suggests, and re-sends it in the
* other one if that produced no connection. TS6 drops a payload it cannot
* read without complaining, so a silent stall is the only symptom of
* guessing wrong here.
*/
private sendAnswer(sdp: string): void {
this.clearAnswerFallback();
this.signal("answer", ANSWER_ARGS_ARE_A_BARE_STRING ? sdp : { sdp });
this.answerFallbackTimer = setTimeout(() => {
this.answerFallbackTimer = null;
if (this.closed || this.pc?.connectionState === "connected") return;
console.warn("[stream] no connection after the answer; retrying in the other args shape");
this.signal("answer", ANSWER_ARGS_ARE_A_BARE_STRING ? { sdp } : sdp);
}, ANSWER_FALLBACK_MS);
}
private clearAnswerFallback(): void {
if (this.answerFallbackTimer === null) return;
clearTimeout(this.answerFallbackTimer);
this.answerFallbackTimer = null;
}
private async handleSignaling(args: Record<string, string>): Promise<void> {
let msg: SignalingMessage;
try {
@@ -170,7 +218,10 @@ export class StreamViewer {
switch (msg.cmd) {
case "iceCandidate": {
const candidate = candidateFromArgs(msg.args);
const candidate =
typeof msg.args === "object" && msg.args !== null
? candidateFromArgs(msg.args as Record<string, unknown>)
: null;
if (!candidate) return;
if (!this.remoteDescriptionSet) {
// TS6 starts trickling immediately, so these routinely beat the
@@ -188,10 +239,19 @@ export class StreamViewer {
break;
}
case "offer":
case "reconnectOffer":
case "reconnectOffer": {
// Renegotiation from the publisher, e.g. after it switches source.
if (typeof msg.args.sdp === "string") await this.acceptOffer(msg.args.sdp);
// Accept both shapes: the bare string is what the DLL reads, the
// object is cheap to tolerate and costs nothing if it never arrives.
const sdp =
typeof msg.args === "string"
? msg.args
: typeof (msg.args as { sdp?: unknown } | null)?.sdp === "string"
? (msg.args as { sdp: string }).sdp
: null;
if (sdp) await this.acceptOffer(sdp);
break;
}
default:
break;
}
@@ -232,6 +292,7 @@ export class StreamViewer {
};
pc.onconnectionstatechange = () => {
if (pc.connectionState === "connected") this.clearAnswerFallback();
this.opts.onStateChange?.(pc.connectionState);
if (pc.connectionState === "failed") this.fail("The stream connection failed");
};
@@ -240,7 +301,7 @@ export class StreamViewer {
return pc;
}
private signal(cmd: string, args: Record<string, unknown>): void {
private signal(cmd: string, args: unknown): void {
this.opts.send({
type: "streamSignal",
streamId: this.streamId,
@@ -257,6 +318,7 @@ export class StreamViewer {
close(): void {
if (this.closed) return;
this.closed = true;
this.clearAnswerFallback();
this.opts.send({
type: "leaveStream",