mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-02 19:42:51 +08:00
fix: isolate microphone metering resources and stale ticks
This commit is contained in:
1 parent
1a46ab83a3
commit
f11fe26bfe
5 files changed
+195
-54
No files matched your search
@@ -5,6 +5,7 @@ export type { MicrophoneProcessingSettings } from "../voice/microphone-capture.j
|
||||
import { createAudioSinkRouter } from "../voice/audio-sink.js";
|
||||
import { createAccompaniment, type AccompanimentErrorCode } from "../voice/accompaniment.js";
|
||||
import { createWebRtcInput, type WebRtcInput } from "../voice/webrtc-input.js";
|
||||
import { createMicrophoneMeter, type MicrophoneMeter } from "../voice/microphone-meter.js";
|
||||
import { createScreenShareController } from "../voice/screen-share.js";
|
||||
export type { ScreenShareOutputSettings, ScreenShareCaptureStats, ScreenSharePeerStats, ScreenShareWebRtcStats } from "../voice/screen-share.js";
|
||||
import { parseServerMessage } from "../../../src/shared/server-messages.js";
|
||||
@@ -277,10 +278,7 @@ export function useVoiceWebSocket() {
|
||||
isOpen: () => ws.value?.readyState === WebSocket.OPEN,
|
||||
send: message => ws.value?.send(JSON.stringify(message)),
|
||||
});
|
||||
let webrtcMicMonitorSource: MediaStreamAudioSourceNode | null = null;
|
||||
let webrtcMicMonitorAnalyser: AnalyserNode | null = null;
|
||||
let webrtcMicMonitorGain: GainNode | null = null;
|
||||
let webrtcMicMonitorTimer: ReturnType<typeof setInterval> | null = null;
|
||||
let webrtcMicrophoneMeter: MicrophoneMeter | null = null;
|
||||
const inputDevices = reactive<AudioInputDevice[]>([]);
|
||||
const outputDevices = reactive<AudioOutputDevice[]>([]);
|
||||
const selectedInputDeviceId = ref(typeof localStorage !== "undefined" ? localStorage.getItem("webspeak:input-device") ?? "" : "");
|
||||
@@ -964,45 +962,19 @@ export function useVoiceWebSocket() {
|
||||
}
|
||||
|
||||
function startWebRtcMicMonitor(ctx: AudioContext, stream: MediaStream, track: MediaStreamTrack): void {
|
||||
stopWebRtcMicMonitor(false);
|
||||
try {
|
||||
const source = ctx.createMediaStreamSource(stream);
|
||||
const analyser = ctx.createAnalyser();
|
||||
analyser.fftSize = 512;
|
||||
const silent = ctx.createGain();
|
||||
silent.gain.value = 0;
|
||||
source.connect(analyser);
|
||||
analyser.connect(silent);
|
||||
silent.connect(ctx.destination);
|
||||
const samples = new Float32Array(analyser.fftSize);
|
||||
webrtcMicMonitorSource = source;
|
||||
webrtcMicMonitorAnalyser = analyser;
|
||||
webrtcMicMonitorGain = silent;
|
||||
webrtcMicMonitorTimer = setInterval(() => {
|
||||
if (!webrtcMicMonitorAnalyser) return;
|
||||
webrtcMicMonitorAnalyser.getFloatTimeDomainData(samples);
|
||||
let sum = 0;
|
||||
for (const sample of samples) sum += sample * sample;
|
||||
const rms = Math.sqrt(sum / samples.length);
|
||||
micLevel.value = Math.min(1, rms * 6);
|
||||
if (!microphoneMuted.value && track.enabled && rms >= voxThreshold.value) markSpeaking(state.tsClientId);
|
||||
else if (state.tsClientId) clearSpeaking(state.tsClientId);
|
||||
}, 50);
|
||||
} catch {
|
||||
stopWebRtcMicMonitor(false);
|
||||
}
|
||||
stopWebRtcMicMonitor();
|
||||
webrtcMicrophoneMeter = createMicrophoneMeter(ctx, stream, rms => {
|
||||
micLevel.value = rms === null ? 0 : Math.min(1, rms * 6);
|
||||
if (rms !== null && !microphoneMuted.value && track.enabled && rms >= voxThreshold.value) markSpeaking(state.tsClientId);
|
||||
else if (state.tsClientId) clearSpeaking(state.tsClientId);
|
||||
});
|
||||
}
|
||||
|
||||
function stopWebRtcMicMonitor(resetLevel = true): void {
|
||||
if (webrtcMicMonitorTimer) clearInterval(webrtcMicMonitorTimer);
|
||||
webrtcMicMonitorTimer = null;
|
||||
webrtcMicMonitorSource?.disconnect();
|
||||
webrtcMicMonitorAnalyser?.disconnect();
|
||||
webrtcMicMonitorGain?.disconnect();
|
||||
webrtcMicMonitorSource = null;
|
||||
webrtcMicMonitorAnalyser = null;
|
||||
webrtcMicMonitorGain = null;
|
||||
if (resetLevel) micLevel.value = 0;
|
||||
function stopWebRtcMicMonitor(): void {
|
||||
const meter = webrtcMicrophoneMeter;
|
||||
webrtcMicrophoneMeter = null;
|
||||
meter?.dispose();
|
||||
micLevel.value = 0;
|
||||
}
|
||||
|
||||
function stopCaptureGraph(): void {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
export interface MicrophoneMeter {
|
||||
dispose(): void;
|
||||
}
|
||||
|
||||
// The meter is optional and owns only its nodes and timer, never the capture
|
||||
// stream. A null sample clears presentation when metering stops or fails.
|
||||
export function createMicrophoneMeter(context: AudioContext, stream: MediaStream,
|
||||
onLevel: (rms: number | null) => void): MicrophoneMeter {
|
||||
let source: MediaStreamAudioSourceNode | null = null;
|
||||
let analyser: AnalyserNode | null = null;
|
||||
let silent: GainNode | null = null;
|
||||
let timer: ReturnType<typeof setInterval> | null = null;
|
||||
let disposed = false;
|
||||
const clean = (operation: () => void): void => { try { operation(); } catch { /* release remaining resources */ } };
|
||||
|
||||
function dispose(): void {
|
||||
if (disposed) return;
|
||||
disposed = true;
|
||||
if (timer !== null) clearInterval(timer);
|
||||
timer = null;
|
||||
if (source) clean(() => source!.disconnect());
|
||||
if (analyser) clean(() => analyser!.disconnect());
|
||||
if (silent) clean(() => silent!.disconnect());
|
||||
source = null;
|
||||
analyser = null;
|
||||
silent = null;
|
||||
onLevel(null);
|
||||
}
|
||||
|
||||
try {
|
||||
source = context.createMediaStreamSource(stream);
|
||||
analyser = context.createAnalyser();
|
||||
analyser.fftSize = 512;
|
||||
silent = context.createGain();
|
||||
silent.gain.value = 0;
|
||||
source.connect(analyser);
|
||||
analyser.connect(silent);
|
||||
silent.connect(context.destination);
|
||||
const samples = new Float32Array(analyser.fftSize);
|
||||
timer = setInterval(() => {
|
||||
if (disposed) return;
|
||||
try { analyser!.getFloatTimeDomainData(samples); }
|
||||
catch { dispose(); return; }
|
||||
let sum = 0;
|
||||
for (const sample of samples) sum += sample * sample;
|
||||
onLevel(Math.sqrt(sum / samples.length));
|
||||
}, 50);
|
||||
} catch {
|
||||
dispose();
|
||||
}
|
||||
return { dispose };
|
||||
}
|
||||
Reference in new issue
Block a user