fix: restore input mute after TeamSpeak flood rejection

This commit is contained in:
illusia committed 2026-10-03 14:06:07 +08:00
1 parent 2c49ee2599
commit 77fef3bed9
3 files changed
+94 -6

No files matched your search

+58 -1
View File
@@ -46,7 +46,64 @@ test("accompaniment remains audible with the microphone muted and stopping resto
await f.client.setInputMuted(true);
await f.client.setAccompanimentActive(false);
await f.client.setInputMuted(false);
assert.deepEqual(f.sdk.inputCommands, [1, 0, 0, 1, 0].map(value => `clientupdate client_input_muted=${value}`));
assert.deepEqual(f.sdk.inputCommands, [1, 0, 1, 0].map(value => `clientupdate client_input_muted=${value}`));
});
test("unchanged wire mute still records microphone and accompaniment changes", async () => {
const f = fixture();
await f.client.setInputMuted(false);
await f.client.setAccompanimentActive(true);
await f.client.setInputMuted(true);
await f.client.setAccompanimentActive(false);
assert.deepEqual(f.sdk.inputCommands, ["clientupdate client_input_muted=0", "clientupdate client_input_muted=1"]);
});
test("stopping accompaniment retries flood rejection before committing restored mute", async t => {
const f = fixture();
await f.client.setInputMuted(true);
await f.client.setAccompanimentActive(true);
t.mock.timers.enable({ apis: ["setTimeout"] });
let attempts = 0;
f.sdk.inputUpdate = async () => { if (++attempts === 1) throw new Error("TeamSpeak server error: client is flooding (id=524)"); };
const pending = f.client.setAccompanimentActive(false);
await nextTurn();
assert.equal(attempts, 1);
t.mock.timers.tick(1000);
await pending;
await f.client.setInputMuted(true);
assert.equal(attempts, 2);
assert.deepEqual(f.sdk.inputCommands.slice(-2), ["clientupdate client_input_muted=1", "clientupdate client_input_muted=1"]);
});
test("input flood retries are bounded and a rejected update does not poison later changes", async t => {
const f = fixture();
t.mock.timers.enable({ apis: ["setTimeout"] });
const flooding = new Error("TeamSpeak server error: client is flooding (id=524)");
f.sdk.inputUpdate = async () => { throw flooding; };
const pending = assert.rejects(f.client.setInputMuted(true), error => error === flooding);
await nextTurn();
t.mock.timers.tick(1000);
await nextTurn();
t.mock.timers.tick(2000);
await pending;
assert.equal(f.sdk.inputCommands.length, 3);
f.sdk.inputUpdate = async () => {};
await f.client.setInputMuted(false);
assert.equal(f.sdk.inputCommands.at(-1), "clientupdate client_input_muted=0");
});
test("a flood retry cannot send into a replaced connection", async t => {
const f = fixture();
t.mock.timers.enable({ apis: ["setTimeout"] });
f.sdk.inputUpdate = async () => { throw new Error("TeamSpeak server error: client is flooding (id=524)"); };
const pending = assert.rejects(f.client.setInputMuted(true), /not ready/);
await nextTurn();
await f.client.disconnect();
f.state.client = f.sdk;
f.state.connected = true;
t.mock.timers.tick(1000);
await pending;
assert.equal(f.sdk.inputCommands.length, 1);
});
test("overlapping microphone and accompaniment changes use the last accepted input state", async () => {
+20 -1
View File
@@ -88,6 +88,7 @@ export class TSClient extends EventEmitter {
private connected = false;
private connectionGeneration = 0;
private inputState = { muted: false, accompaniment: false };
private syncedInputMuted: boolean | null = null;
private inputStateQueue: Promise<void> = Promise.resolve();
private preferredChannelId = 0n;
private accelerationClient: AccelerationRelayClient | null = null;
@@ -105,6 +106,7 @@ export class TSClient extends EventEmitter {
async connect(): Promise<void> {
this.connectionGeneration++;
this.inputState = { muted: false, accompaniment: false };
this.syncedInputMuted = null;
this.inputStateQueue = Promise.resolve();
this.selfLeaveReasonId = null;
if (!this.adapter || !this.client) {
@@ -375,10 +377,27 @@ export class TSClient extends EventEmitter {
const update = this.inputStateQueue.then(async () => {
if (!isCurrent()) throw new Error("TeamSpeak session is not ready");
const next = { ...this.inputState, ...change };
const muted = next.muted && !next.accompaniment;
// TeamSpeak discards all audio from an input-muted client, including
// music. The browser removes the microphone from its accompaniment mix.
await client!.execCommand(`clientupdate client_input_muted=${next.muted && !next.accompaniment ? 1 : 0}`);
if (this.syncedInputMuted !== muted) {
for (let attempt = 0; ; attempt++) {
if (!isCurrent()) throw new Error("TeamSpeak session is not ready");
try {
await client!.execCommand(`clientupdate client_input_muted=${muted ? 1 : 0}`);
break;
} catch (error: unknown) {
// A failed acknowledgement cannot justify skipping a later update.
if (isCurrent()) this.syncedInputMuted = null;
if (!isCurrent() || attempt >= 2 || normalizeTeamSpeakError(error).code !== "flooding") throw error;
// In particular, stopping accompaniment must get a chance to
// restore mute after the server's short flood-control window.
await new Promise<void>(resolve => setTimeout(resolve, (attempt + 1) * 1000));
}
}
}
if (!isCurrent()) throw new Error("TeamSpeak session changed while updating input state");
this.syncedInputMuted = muted;
this.inputState = next;
});
this.inputStateQueue = update.catch(() => undefined);