fix: keep quiet WebRTC audio frames

This commit is contained in:
illusia committed 2026-09-06 14:33:15 +08:00
1 parent 166cb543a1
commit 45410fcc7d
2 files changed
+17 -55

No files matched your search

+3
View File
@@ -11,6 +11,7 @@
- 管理员连接测试改用服务端多次主机 Ping 并显示丢包率,不再创建临时 TeamSpeak 客户端。
- 统一中文、English、Deutsch 的旗帜代码语言菜单。
- 修复 TeamSpeak 使用 TCP 探测导致的误报丢包,并修正语言菜单异常留白。
- 移除 WebRTC 桥接中的 RMS 静音帧过滤,安静帧仅用于发言状态指示,不再丢弃。
### English
@@ -21,6 +22,7 @@
- Updated the administrator connection test to use repeated host pings, report packet loss, and avoid creating temporary TeamSpeak clients.
- Unified the Chinese, English, and German flag/code language menu.
- Fixed false packet-loss reports caused by probing the TeamSpeak UDP service with TCP, and corrected excess space in the language menu.
- Removed RMS-based silence filtering from the WebRTC bridge; quiet frames are now retained for the codec timeline.
### Deutsch
@@ -31,6 +33,7 @@
- Verbindungstest in der Administration auf wiederholte Host-Pings mit Paketverlustanzeige umgestellt, ohne temporäre TeamSpeak-Clients zu erzeugen.
- Einheitliches Sprachmenü mit Flaggen und Sprachcodes für Chinesisch, Englisch und Deutsch ergänzt.
- Falsche Paketverlustmeldungen durch TCP-Prüfung des UDP-Dienstes behoben und übermäßigen Leerraum im Sprachmenü korrigiert.
- RMS-basierte Stillefilterung aus der WebRTC-Brücke entfernt; leise Frames bleiben nun im Codec-Zeitverlauf erhalten.
## [0.1.6] — 2026-09-04
+14 -55
View File
@@ -30,13 +30,10 @@ const MAX_MIXER_QUEUE_FRAMES = 4;
const MAX_MIXER_PACER_LAG_MS = AUDIO_CLOCK_INTERVAL_MS * 2;
const MAX_MIXER_FRAME_AGE_MS = MAX_MIXER_QUEUE_FRAMES * AUDIO_CLOCK_INTERVAL_MS;
const MIXER_UNDERRUN_WINDOW_MS = 200;
// TeamSpeak and browsers can emit valid Opus comfort-noise/silence packets
// while a client is muted. Do not turn those packets into a false "speaking"
// indicator or forward them to TeamSpeak as if they were voice.
const MIN_SPEAKER_RMS = 160;
// Keep this below the speaking threshold so quiet but valid accompaniment is
// forwarded while comfort-noise packets remain filtered.
const MIN_FORWARD_RMS = 24;
// This threshold is used only for the speaking indicator. It must never decide
// whether a media packet is forwarded or queued: quiet/comfort-noise Opus
// packets are still valid media and dropping them can break decoder continuity.
const SPEAKER_ACTIVITY_RMS = 160;
// WebRTC is served by the WebSpeak process itself. The default range is also
// published by the bundled Docker Compose file; advanced administrators can
@@ -112,7 +109,6 @@ export class WebRtcAudioSession {
private readonly onVoiceFrame: (data: Buffer, codec: 4 | 5) => void;
private readonly onVoiceActivity: (clientIds: number[]) => void;
private readonly outgoingTrack: MediaStreamTrack;
private ingressDecoder: { decode(data: Buffer): Buffer } | null = null;
private readonly decoderByClient = new Map<number, { decode(data: Buffer): Buffer }>();
private readonly partialPcmByClient = new Map<number, Buffer>();
private readonly pendingFrames = new Map<number, PendingAudioFrame[]>();
@@ -183,22 +179,11 @@ export class WebRtcAudioSession {
this.stats.webrtcIngressRtpFrames++;
if (!this.opusPayloadTypes.has(rtp.header.payloadType)) return;
const payload = Buffer.from(rtp.payload);
// The track is a browser-side mix of microphone and accompaniment.
// Accompaniment is already a complete Opus stream. Forward it as-is:
// do not decode it for RMS analysis or pass it through any speech
// gate, because either step can discard valid quiet music frames.
if (this.accompanimentActive) {
this.onVoiceFrame(payload, 5);
return;
}
// A normal microphone still uses the low-level comfort-noise gate.
const rms = this.getIngressRms(payload);
if (rms === null) return;
if (rms < MIN_FORWARD_RMS) {
this.stats.webrtcIngressQuietFrames++;
return;
}
this.onVoiceFrame(payload, 4);
// Forward every valid negotiated Opus payload unchanged. Do not
// decode it for RMS analysis or pass it through a speech/silence gate:
// quiet frames are still part of the codec timeline and dropping them
// can cause downstream gaps, clicks, or decoder recovery artifacts.
this.onVoiceFrame(payload, this.accompanimentActive ? 5 : 4);
});
void audio.sender.replaceTrack(this.outgoingTrack).catch((error: unknown) => {
this.logger.warn({ err: error instanceof Error ? error.message : String(error) }, "Could not attach WebRTC output track");
@@ -210,16 +195,11 @@ export class WebRtcAudioSession {
try {
this.encoder = new OpusEncoder(AUDIO_SAMPLE_RATE, 1);
// Use a separate stateful decoder for browser ingress. RTP payloads do
// not identify whether a packet contains microphone energy or DTX/CN
// audio, so the gateway must decode it before forwarding to TS.
this.ingressDecoder = new OpusEncoder(AUDIO_SAMPLE_RATE, 1);
} catch (error: unknown) {
this.encoder = null;
this.ingressDecoder = null;
this.logger.error({ err: error instanceof Error ? error.message : String(error) }, "Could not create WebRTC mixer encoder");
}
if (!this.encoder || !this.ingressDecoder) {
if (!this.encoder) {
this.outgoingTrack.stop();
throw new Error("WebRTC Opus codec is unavailable");
}
@@ -279,12 +259,10 @@ export class WebRtcAudioSession {
const pendingPcm = this.partialPcmByClient.get(data.clientId);
const combined = pendingPcm ? Buffer.concat([pendingPcm, pcm]) : pcm;
const rms = this.calculateRms(pcm);
// A muted TeamSpeak client can still produce comfort-noise packets.
// Do not let those packets turn a single accompaniment stream into a
// synthetic multi-source mix. Music packets use codec 5 and must be
// retained even when their instantaneous RMS is low.
if (data.codec !== 5 && rms < MIN_SPEAKER_RMS) return;
if (rms >= MIN_SPEAKER_RMS) this.activeSpeakerIds.add(data.clientId);
// RMS is used only for the UI speaking indicator. Every decoded frame,
// including quiet/comfort-noise frames, continues into the queue so the
// receiver keeps an uninterrupted codec timeline.
if (rms >= SPEAKER_ACTIVITY_RMS) this.activeSpeakerIds.add(data.clientId);
const canForwardOpus = !pendingPcm && pcm.length === AUDIO_FRAME_BYTES;
let offset = 0;
let enqueued = false;
@@ -329,7 +307,6 @@ export class WebRtcAudioSession {
this.memberVolumes.clear();
this.partialPcmByClient.clear();
this.activeSpeakerIds.clear();
this.ingressDecoder = null;
this.decoderByClient.clear();
this.encoder = null;
this.outgoingTrack.stop();
@@ -446,24 +423,6 @@ export class WebRtcAudioSession {
else this.memberVolumes.set(clientId, normalized);
}
private getIngressRms(data: Buffer): number | null {
const decoder = this.ingressDecoder;
if (!decoder) return null;
try {
const pcm = decoder.decode(data);
if (pcm.length < 2) {
this.stats.webrtcIngressDecodeErrors++;
return null;
}
return this.calculateRms(pcm);
} catch {
// Malformed/transition packets are discarded without interrupting the
// rest of the peer. The next valid Opus packet can recover the decoder.
this.stats.webrtcIngressDecodeErrors++;
return null;
}
}
private calculateRms(pcm: Buffer): number {
let sum = 0;
let samples = 0;