fix: validate merged voice fallback and host toolbar features

This commit is contained in:
illusia committed 2026-11-27 08:53:26 +08:00
1 parent 02509ea551
commit 001457d004
32 files changed
+319 -102

No files matched your search

+26 -4
View File
@@ -8,7 +8,7 @@ import type { TSVoiceData } from "./ts-client.js";
function fixture() {
const state = { current: true, ready: true, now: 0, targets: null as number[] | null, disposed: 0,
peer: null as { pushTeamSpeakVoice(data: TSVoiceData): void } | null };
peer: null as { isConnected: boolean; pushTeamSpeakVoice(data: TSVoiceData): void } | null };
const audio = createAudioFlowStats();
const packets: Buffer[] = [];
const encoded: Buffer[] = [];
@@ -146,7 +146,7 @@ test("tagged PCM and pre-encoded Opus frames share the uplink without double enc
test("WebRTC ingress excludes queued PCM and fallback re-enables PCM", () => {
const f = fixture();
f.state.peer = { pushTeamSpeakVoice() {} };
f.state.peer = { isConnected: true, pushTeamSpeakVoice() {} };
f.transport.receivePcm(Buffer.alloc(1_920));
const opus = Buffer.from([3, 2, 1]);
f.transport.receiveWebRtc(opus, 5);
@@ -188,7 +188,7 @@ test("backpressure drops bursts above 4096 bytes and resumes without a retained
test("WebRTC downlink owns egress without a duplicate WebSocket packet", () => {
const f = fixture();
const received: TSVoiceData[] = [];
f.state.peer = { pushTeamSpeakVoice: data => { received.push(data); } };
f.state.peer = { isConnected: true, pushTeamSpeakVoice: data => { received.push(data); } };
f.transport.receiveTeamSpeak(f.remote());
assert.equal(received.length, 1);
assert.equal(f.packets.length, 0);
@@ -198,6 +198,28 @@ test("WebRTC downlink owns egress without a duplicate WebSocket packet", () => {
assert.equal(f.packets.length, 1);
});
test("negotiating WebRTC keeps both compatibility directions until the media path connects", () => {
const f = fixture();
const received: TSVoiceData[] = [];
f.state.peer = { isConnected: false, pushTeamSpeakVoice: data => received.push(data) };
f.transport.receivePcm(Buffer.alloc(1920));
f.transport.receiveTeamSpeak(f.remote());
assert.equal(f.forwarded.length, 1);
assert.equal(f.packets.length, 1);
assert.equal(received.length, 0);
f.state.peer.isConnected = true;
f.transport.receivePcm(Buffer.alloc(1920));
f.transport.receiveTeamSpeak(f.remote());
assert.equal(f.forwarded.length, 1);
assert.equal(f.packets.length, 1);
assert.equal(received.length, 1);
f.state.peer.isConnected = false;
f.transport.receivePcm(Buffer.alloc(1920));
f.transport.receiveTeamSpeak(f.remote());
assert.equal(f.forwarded.length, 2);
assert.equal(f.packets.length, 2);
});
test("encoding failures are counted and reported at most once per five seconds", t => {
const f = fixture();
t.mock.method(f.encoder, "encode", () => { throw new Error("Codec failed"); });
@@ -220,7 +242,7 @@ test("SDK and downlink failures cannot escape the audio callbacks", t => {
assert.equal(f.notices.length, 0, "SDK failure must not be reported as an encoder failure");
t.mock.method(f.socket, "send", () => { throw new Error("Socket closed"); });
f.transport.receiveTeamSpeak(f.remote());
f.state.peer = { pushTeamSpeakVoice() { throw new Error("Peer closed"); } };
f.state.peer = { isConnected: true, pushTeamSpeakVoice() { throw new Error("Peer closed"); } };
f.transport.receiveTeamSpeak(f.remote());
assert.equal(f.audio.egressDroppedFrames, 2);
});
+11 -4
View File
@@ -41,7 +41,7 @@ export interface SessionAudioOptions {
isCurrent(): boolean;
isReady(): boolean;
selfId(): number;
peer(): Pick<WebRtcAudioSession, "pushTeamSpeakVoice"> | null;
peer(): Pick<WebRtcAudioSession, "pushTeamSpeakVoice" | "isConnected"> | null;
whisperTargets(): readonly number[] | null;
sendJson(message: ServerMessage): void;
/** Tests that assert synchronous SDK calls can opt out; production keeps pacing. */
@@ -64,7 +64,9 @@ export class SessionAudioTransport {
this.encoder = encoder;
this.logger = options.logger ?? null;
this.pacer = options.pacing === false ? null : new AudioPacer(
(data, codec) => this.sendVoice(data, codec),
(data, codec) => {
if (!this.mediaPeer()) this.sendVoice(data, codec);
},
{
now: this.rateClock,
onStats: stats => {
@@ -81,6 +83,11 @@ export class SessionAudioTransport {
return !this.closed && this.options.isCurrent() && this.options.socket.readyState === WebSocket.OPEN;
}
private mediaPeer() {
const peer = this.options.peer();
return peer?.isConnected ? peer : null;
}
private recordIngress(): void {
const stats = this.options.audio;
const now = this.clock();
@@ -153,7 +160,7 @@ export class SessionAudioTransport {
// route is down is discarded whatever its size, so calling it malformed would
// misattribute the drop and raise a spurious client error in the browser.
// Late TCP frames from before a WebRTC switch must not duplicate RTP audio.
if (!this.options.isReady() || this.options.peer()) { stats.ingressDroppedFrames++; return; }
if (!this.options.isReady() || this.mediaPeer()) { stats.ingressDroppedFrames++; return; }
const legacyPcm = frame.length === AUDIO_FRAME_BYTES ? frame : null;
const taggedPcm = frame.length === TAGGED_PCM_FRAME_BYTES && frame[0] === PCM_FRAME_TAG ? frame.subarray(1) : null;
const opus = frame.length > 1 && frame.length <= MAX_OPUS_FRAME_BYTES + 1 && frame[0] === OPUS_FRAME_TAG
@@ -227,7 +234,7 @@ export class SessionAudioTransport {
stats.egressLastAt = now;
const key = String(data.clientId);
stats.egressFramesByClient[key] = (stats.egressFramesByClient[key] ?? 0) + 1;
const peer = this.options.peer();
const peer = this.mediaPeer();
if (peer) {
try { peer.pushTeamSpeakVoice(data); stats.egressFrames++; }
catch { stats.egressDroppedFrames++; }
@@ -131,11 +131,11 @@ test("schema 8 settings and invites survive the nickname migration", async (cont
});
database.close();
const legacy = new DatabaseSync(file);
legacy.exec("DROP TABLE ts_admin_settings; ALTER TABLE relay_nodes DROP COLUMN voice_endpoint; ALTER TABLE settings DROP COLUMN ts_target; ALTER TABLE managed_invites DROP COLUMN target_text; DROP TABLE workspace_invites; ALTER TABLE settings DROP COLUMN voice_profile; ALTER TABLE settings DROP COLUMN voice_turn_urls; ALTER TABLE settings DROP COLUMN voice_turn_secret_encrypted; ALTER TABLE settings DROP COLUMN voice_turn_ttl_seconds; ALTER TABLE settings DROP COLUMN voice_opus_ws; ALTER TABLE settings DROP COLUMN voice_jitter_max_ms; PRAGMA user_version = 8;");
legacy.exec("DROP TABLE ts_admin_settings; ALTER TABLE relay_nodes DROP COLUMN voice_endpoint; ALTER TABLE settings DROP COLUMN ts_target; ALTER TABLE managed_invites DROP COLUMN target_text; DROP TABLE workspace_invites; PRAGMA user_version = 8;");
legacy.close();
const migrated = new WebSpeakDatabase(file);
try {
assert.equal(migrated.schemaVersion, 14);
assert.equal(migrated.schemaVersion, 13);
assert.equal(migrated.getSettings().tsHost, "127.0.0.1");
assert.equal(migrated.getSettings().tsTarget, null);
const invite = migrated.consumeManagedInvite("legacy-token");
+1
View File
@@ -18,6 +18,7 @@ function deferred() {
}
class PeerStub {
isConnected = true;
closed = false;
closeResult = Promise.resolve();
answerResult = Promise.resolve();
+4
View File
@@ -258,6 +258,10 @@ export class WebRtcAudioSession {
};
}
get isConnected(): boolean {
return !this.closed && this.peer.connectionState === "connected";
}
getStats(): WebRtcAudioStats {
return { ...this.stats, webrtcQueueCurrentFrames: this.getQueueFrameCount() };
}