fix: harden TeamSpeak detection and clean up departed speakers

This commit is contained in:
illusia committed 2026-10-17 14:55:25 +08:00
1 parent 84bdf63514
commit 180bfeb52f
11 files changed
+133 -10

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
import assert from "node:assert/strict";
import test from "node:test";
import { detectTeamSpeakProtocol } from "./teamspeak-adapter.js";
test("detects TeamSpeak 3 from the leading version number", () => {
assert.equal(detectTeamSpeakProtocol("3.13.7 build=1645123456 platform=Linux"), "ts3");
assert.equal(detectTeamSpeakProtocol("3.0.0"), "ts3");
});
test("does not misclassify a TeamSpeak 3 minor as TeamSpeak 6", () => {
assert.equal(detectTeamSpeakProtocol("3.6.7 build=1645123456 platform=Linux"), "ts3");
assert.equal(detectTeamSpeakProtocol("3.6.0"), "ts3");
assert.equal(detectTeamSpeakProtocol("3.16.0"), "ts3");
});
test("detects TeamSpeak 6 from the leading version number or product name", () => {
assert.equal(detectTeamSpeakProtocol("6.0.0-beta.1"), "ts6");
assert.equal(detectTeamSpeakProtocol("6.2.0"), "ts6");
assert.equal(detectTeamSpeakProtocol("TeamSpeak 6.0.0"), "ts6");
assert.equal(detectTeamSpeakProtocol("TeamSpeak 3 Server 3.13.7"), "ts3");
});
test("returns null for unknown or unsupported version formats", () => {
assert.equal(detectTeamSpeakProtocol(""), null);
assert.equal(detectTeamSpeakProtocol("build=1645123456 platform=Linux"), null);
assert.equal(detectTeamSpeakProtocol("5.0.0-beta"), null);
assert.equal(detectTeamSpeakProtocol("7.0.0"), null);
});
+9 -3
View File
@@ -152,8 +152,14 @@ export class TeamSpeakAdapter {
}
export function detectTeamSpeakProtocol(versionText: string): TeamSpeakProtocol | null {
const text = versionText.trim().toLocaleLowerCase();
if (/teamspeak\s*6|(?:^|\D)6\.\d+/.test(text)) return "ts6";
if (/teamspeak\s*3|(?:^|\D)3\.\d+/.test(text)) return "ts3";
const text = versionText.trim().toLowerCase();
// The `version` command starts with the server version, optionally preceded
// by the product name. Match only that leading major version: scanning the
// whole string mistakes TeamSpeak 3.6.7 for TeamSpeak 6.
const match = /^(?:teamspeak\s*)?(\d+)/.exec(text);
if (!match) return null;
const major = Number(match[1]);
if (major === 6) return "ts6";
if (major === 3) return "ts3";
return null;
}
+1 -1
View File
@@ -475,7 +475,7 @@ export class VoiceBridge {
sendJson, addEvent: addServerEvent, onDirectoryReady: sendInitialState,
onClientLeave: id => {
this.screenShares.onClientLeave(entry!, id);
entry!.webrtc?.setMemberVolume(id, 1);
entry!.webrtc?.removeClient(id);
},
onClientMove: (id, channelId) => this.screenShares.onClientMove(entry!, id, channelId),
onNotification: notification => this.screenShares.handleNotification(entry!, notification),
+32
View File
@@ -201,3 +201,35 @@ test("an unavailable mixer encoder closes the half-built peer before the constru
}
assert.equal(closedPeers.size, 1, "the constructor must roll back the peer it already created");
});
test("removeClient releases only the leaving speaker's decoder and buffered state", () => {
const f = fixture();
f.resources.pendingFrames.set(2, []);
f.resources.memberVolumes.set(2, 0.5);
f.resources.partialPcmByClient.set(2, Buffer.alloc(2));
f.resources.activeSpeakerIds.add(2);
f.session.removeClient(1);
assert.deepEqual(f.released, ["decoder-1"]);
assert.equal(f.resources.decoderByClient.has(1), false);
assert.equal(f.resources.pendingFrames.has(1), false);
assert.equal(f.resources.partialPcmByClient.has(1), false);
assert.equal(f.resources.memberVolumes.has(1), false);
assert.equal(f.resources.activeSpeakerIds.has(1), false);
assert.equal(f.resources.decoderByClient.has(2), true);
assert.equal(f.resources.pendingFrames.has(2), true);
assert.equal(f.resources.partialPcmByClient.has(2), true);
assert.equal(f.resources.memberVolumes.has(2), true);
assert.equal(f.resources.activeSpeakerIds.has(2), true);
});
test("removeClient is idempotent and tolerates a throwing decoder disposal", () => {
const f = fixture();
f.session.removeClient(1);
f.session.removeClient(1);
assert.deepEqual(f.released, ["decoder-1"]);
assert.equal(f.resources.decoderByClient.has(1), false);
});
+12
View File
@@ -441,6 +441,18 @@ export class WebRtcAudioSession {
else this.memberVolumes.set(clientId, normalized);
}
removeClient(clientId: number): void {
if (!Number.isInteger(clientId) || clientId <= 0 || clientId > 65535) return;
this.pendingFrames.delete(clientId);
this.partialPcmByClient.delete(clientId);
this.memberVolumes.delete(clientId);
this.activeSpeakerIds.delete(clientId);
const decoder = this.decoderByClient.get(clientId);
if (!decoder) return;
this.decoderByClient.delete(clientId);
try { decoder.dispose(); } catch { /* continue after codec cleanup failures */ }
}
private calculateRms(pcm: Buffer): number {
let sum = 0;
let samples = 0;