mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-02 19:42:51 +08:00
fix: guard concurrent remembered identities
This commit is contained in:
1 parent
e964ff460e
commit
b5f8f01301
5 files changed
+60
-68
No files matched your search
@@ -171,40 +171,6 @@ export function useVoiceWebSocket() {
|
||||
// for jitter; stale audio is discarded instead of being played late.
|
||||
const MAX_REMOTE_PLAY_AHEAD_SECONDS = 0.08;
|
||||
const MAX_REMOTE_DECODE_QUEUE_FRAMES = 3;
|
||||
const audioDebugEnabled = typeof location !== "undefined"
|
||||
&& new URLSearchParams(location.search).get("audioDebug") === "1";
|
||||
const audioDiagnostics = reactive({
|
||||
uplinkFrames: 0,
|
||||
uplinkDroppedFrames: 0,
|
||||
downlinkFrames: 0,
|
||||
decoderOutputFrames: 0,
|
||||
playbackStarts: 0,
|
||||
playbackResets: 0,
|
||||
peakBufferedBytes: 0,
|
||||
peakPlayAheadMs: 0,
|
||||
peakDecodeQueueFrames: 0,
|
||||
downlinkFirstAt: null as number | null,
|
||||
lastDownlinkAt: null as number | null,
|
||||
downlinkMaxGapMs: 0,
|
||||
lastDecodeOutputAt: null as number | null,
|
||||
lastPlaybackStartAt: null as number | null,
|
||||
lastPlayAheadMs: 0,
|
||||
});
|
||||
const audioCounters = { ...audioDiagnostics };
|
||||
let nextAudioDiagnosticsPublishAt = 0;
|
||||
|
||||
function publishAudioDiagnostics(force = false): void {
|
||||
const now = Date.now();
|
||||
if (!force && now < nextAudioDiagnosticsPublishAt) return;
|
||||
nextAudioDiagnosticsPublishAt = now + 1_000;
|
||||
Object.assign(audioDiagnostics, audioCounters);
|
||||
if (audioDebugEnabled) {
|
||||
console.info("[WebSpeak audio checkpoint]", JSON.stringify({
|
||||
at: new Date(now).toISOString(),
|
||||
...audioDiagnostics,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
async function saveAudioPreferences(): Promise<void> {
|
||||
await saveLocalPreferences({
|
||||
@@ -427,11 +393,8 @@ export function useVoiceWebSocket() {
|
||||
return;
|
||||
}
|
||||
const bufferedBytes = socket.bufferedAmount;
|
||||
audioCounters.peakBufferedBytes = Math.max(audioCounters.peakBufferedBytes, bufferedBytes);
|
||||
if (bufferedBytes > MAX_AUDIO_BUFFERED_BYTES) {
|
||||
accumLen = 0;
|
||||
audioCounters.uplinkDroppedFrames++;
|
||||
publishAudioDiagnostics();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -450,8 +413,6 @@ export function useVoiceWebSocket() {
|
||||
while (offset + AUDIO_FRAME_SAMPLES <= accumLen && socket.readyState === WebSocket.OPEN && socket.bufferedAmount <= MAX_AUDIO_BUFFERED_BYTES) {
|
||||
socket.send(accumBuf.slice(offset, offset + AUDIO_FRAME_SAMPLES).buffer);
|
||||
offset += AUDIO_FRAME_SAMPLES;
|
||||
audioCounters.uplinkFrames++;
|
||||
publishAudioDiagnostics();
|
||||
}
|
||||
if (offset > 0) markSpeaking(state.tsClientId);
|
||||
accumLen -= offset;
|
||||
@@ -984,7 +945,6 @@ export function useVoiceWebSocket() {
|
||||
const now = ctx.currentTime;
|
||||
const scheduledUntil = remotePlayTimes.get(clientId) ?? now;
|
||||
const decodeQueueSize = decoder?.decodeQueueSize ?? 0;
|
||||
audioCounters.peakDecodeQueueFrames = Math.max(audioCounters.peakDecodeQueueFrames, decodeQueueSize);
|
||||
if (
|
||||
decoder &&
|
||||
(scheduledUntil > now + MAX_REMOTE_PLAY_AHEAD_SECONDS || decodeQueueSize >= MAX_REMOTE_DECODE_QUEUE_FRAMES)
|
||||
@@ -1007,8 +967,6 @@ export function useVoiceWebSocket() {
|
||||
}
|
||||
try {
|
||||
const { sampleRate, numberOfChannels, numberOfFrames } = chunk;
|
||||
audioCounters.decoderOutputFrames++;
|
||||
audioCounters.lastDecodeOutputAt = Date.now();
|
||||
const buffer = ctx.createBuffer(numberOfChannels, numberOfFrames, sampleRate);
|
||||
for (let ch = 0; ch < numberOfChannels; ch++) {
|
||||
const data = new Float32Array(numberOfFrames);
|
||||
@@ -1040,17 +998,7 @@ export function useVoiceWebSocket() {
|
||||
return;
|
||||
}
|
||||
source.start(playTime);
|
||||
audioCounters.playbackStarts++;
|
||||
audioCounters.lastPlaybackStartAt = Date.now();
|
||||
remotePlayTimes.set(clientId, playTime + numberOfFrames / sampleRate);
|
||||
audioCounters.lastPlayAheadMs = Math.max(
|
||||
0,
|
||||
Math.round((playTime + numberOfFrames / sampleRate - ctx.currentTime) * 1_000),
|
||||
);
|
||||
audioCounters.peakPlayAheadMs = Math.max(
|
||||
audioCounters.peakPlayAheadMs,
|
||||
audioCounters.lastPlayAheadMs,
|
||||
);
|
||||
} catch {
|
||||
// A decoder can finish while the audio context is being torn down.
|
||||
}
|
||||
@@ -1073,8 +1021,6 @@ export function useVoiceWebSocket() {
|
||||
const timestamp = remoteDecodeTimestamps.get(clientId) ?? 0;
|
||||
decoder.decode(new EncodedAudioChunk({ type: "key", timestamp, duration: 20_000, data: opusData }));
|
||||
remoteDecodeTimestamps.set(clientId, timestamp + 20_000);
|
||||
audioCounters.downlinkFrames++;
|
||||
publishAudioDiagnostics();
|
||||
} catch {
|
||||
// Ignore malformed frames; the next valid frame can still be decoded.
|
||||
}
|
||||
@@ -1101,8 +1047,6 @@ export function useVoiceWebSocket() {
|
||||
|
||||
function resetRemotePlayback(clientId: number): void {
|
||||
clearRemotePlayback(clientId);
|
||||
audioCounters.playbackResets++;
|
||||
publishAudioDiagnostics();
|
||||
}
|
||||
|
||||
function connect(target: string, channel: string, nickname: string, serverPassword = "", identity = "", rememberIdentity = false, inviteToken = ""): void {
|
||||
@@ -1166,15 +1110,18 @@ export function useVoiceWebSocket() {
|
||||
state.connected = false;
|
||||
state.connecting = false;
|
||||
state.reconnecting = false;
|
||||
if (event.code !== 1000 && !state.error && !state.reconnectFailed) state.error = closeReason(event.code);
|
||||
// Prefer the close code over the generic WebSocket error event. The
|
||||
// gateway uses a dedicated code when a remembered identity is already
|
||||
// active in another browser page.
|
||||
if (event.code !== 1000 && !state.reconnectFailed) state.error = closeReason(event.code);
|
||||
stopWebRtcTransport();
|
||||
stopMicrophone();
|
||||
whisperTargetIds.clear();
|
||||
whisperActive.value = false;
|
||||
};
|
||||
socket.onerror = () => {
|
||||
if (sequence !== connectionSequence) return;
|
||||
state.error = "连接服务器失败,请检查邀请链接或服务器状态";
|
||||
// The following close event contains the actionable close code. Do not
|
||||
// overwrite it with a generic browser WebSocket error first.
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1190,6 +1137,7 @@ export function useVoiceWebSocket() {
|
||||
if (code === 4002) return "TeamSpeak 服务器地址无效";
|
||||
if (code === 4003) return "TeamSpeak 服务器连接失败";
|
||||
if (code === 4004) return "服务器当前已满,请稍后重试";
|
||||
if (code === 4005) return "此 TeamSpeak 身份已在另一个浏览器页面使用,请关闭另一条连接或取消“保持身份”后重试";
|
||||
return "连接已断开";
|
||||
}
|
||||
|
||||
@@ -1389,12 +1337,6 @@ export function useVoiceWebSocket() {
|
||||
if (data.length < 4) return;
|
||||
const clientId = (data[1] << 8) | data[2];
|
||||
if (clientId === state.tsClientId) return;
|
||||
const receivedAt = Date.now();
|
||||
if (audioCounters.lastDownlinkAt !== null) {
|
||||
audioCounters.downlinkMaxGapMs = Math.max(audioCounters.downlinkMaxGapMs, receivedAt - audioCounters.lastDownlinkAt);
|
||||
}
|
||||
audioCounters.downlinkFirstAt ??= receivedAt;
|
||||
audioCounters.lastDownlinkAt = receivedAt;
|
||||
markSpeaking(clientId);
|
||||
playAudioFrame(clientId, data.slice(3));
|
||||
}
|
||||
@@ -1577,7 +1519,6 @@ export function useVoiceWebSocket() {
|
||||
outputDeviceSupported,
|
||||
audioPermission,
|
||||
audioContextState,
|
||||
audioDiagnostics,
|
||||
identityMaterial,
|
||||
micLevel,
|
||||
microphoneTestActive,
|
||||
|
||||
@@ -64,7 +64,7 @@
|
||||
<input id="channel" v-model="channel" :placeholder="t('emptyDefault')" @keyup.enter="doConnect" />
|
||||
</div>
|
||||
|
||||
<details class="identity-options"><summary>{{ t('identityOptions') }}</summary><label class="remember-identity"><input v-model="rememberIdentity" type="checkbox" /><span><strong>{{ t('rememberIdentity') }}</strong><small>{{ t('rememberIdentityHint') }}</small></span></label></details>
|
||||
<details class="identity-options"><summary>{{ t('identityOptions') }}</summary><label class="remember-identity"><input v-model="rememberIdentity" type="checkbox" /><span><strong>{{ t('rememberIdentity') }}</strong><small>{{ t('rememberIdentityHint') }}</small></span></label><p v-if="rememberIdentity" class="identity-warning">{{ t('rememberIdentityConcurrentWarning') }}</p></details>
|
||||
|
||||
<button class="primary-button connect-button" :disabled="!canJoin || serverConfigLoading || !identityReady || voiceState.connecting" type="submit">
|
||||
<span v-if="voiceState.connecting" class="button-spinner"></span>
|
||||
@@ -408,6 +408,7 @@ const translations: Record<Language, Record<string, string>> = {
|
||||
emptyDefault: "留空进入默认频道",
|
||||
rememberIdentity: "记住此设备的 TeamSpeak 身份",
|
||||
rememberIdentityHint: "仅保存在本设备,用于下次连接时保持身份。",
|
||||
rememberIdentityConcurrentWarning: "同一浏览器只能同时使用一条保持身份的连接;要开启第二条,请取消勾选或使用另一个浏览器。",
|
||||
identityOptions: "设备身份选项",
|
||||
localPersistenceUnavailable: "当前浏览器无法使用持久化存储,本次将使用临时身份。",
|
||||
favoriteServers: "常用服务器",
|
||||
@@ -638,6 +639,7 @@ const translations: Record<Language, Record<string, string>> = {
|
||||
emptyDefault: "Leave empty to use the default channel",
|
||||
rememberIdentity: "Remember this TeamSpeak identity on this device",
|
||||
rememberIdentityHint: "Stored only on this device and reused on the next connection.",
|
||||
rememberIdentityConcurrentWarning: "Only one connection can use this identity in the same browser. Clear this option for a second connection or use another browser.",
|
||||
identityOptions: "Device identity options",
|
||||
localPersistenceUnavailable: "Persistent browser storage is unavailable; this session will use an ephemeral identity.",
|
||||
favoriteServers: "Favorites",
|
||||
@@ -861,6 +863,7 @@ function localizedMessage(message: string) {
|
||||
"连接服务器失败,请检查邀请链接或服务器状态": "Could not connect. Check the invite link or server status",
|
||||
"TeamSpeak 连接已断开": "The TeamSpeak connection was closed",
|
||||
"连接已断开": "The connection was closed",
|
||||
"此 TeamSpeak 身份已在另一个浏览器页面使用,请关闭另一条连接或取消“保持身份”后重试": "This TeamSpeak identity is already used by another browser page. Close that connection or clear ‘Remember identity’ and try again",
|
||||
"TeamSpeak 服务器地址无效": "The TeamSpeak server address is invalid",
|
||||
"TeamSpeak 服务器连接失败": "Could not connect to the TeamSpeak server",
|
||||
"服务器当前已满,请稍后重试": "The server is full. Try again shortly",
|
||||
@@ -1593,6 +1596,7 @@ function stopWhisperTalk(): void {
|
||||
.remember-identity strong, .remember-identity small { display: block; }
|
||||
.remember-identity strong { font-size: 11px; font-weight: 700; }
|
||||
.remember-identity small { margin-top: 3px; color: #8b9994; font-size: 10px; line-height: 1.4; }
|
||||
.identity-warning { margin: 8px 0 0 25px; color: #9a6a32; font-size: 10px; line-height: 1.45; }
|
||||
.local-servers { display: grid; gap: 8px; margin: 1px 0 5px; }
|
||||
.local-server-group { display: flex; flex-wrap: wrap; align-items: center; gap: 5px; }
|
||||
.local-server-group > span { width: 100%; color: #87958f; font-size: 10px; font-weight: 700; }
|
||||
|
||||
Reference in new issue
Block a user