diff --git a/src/server/voice-bridge.ts b/src/server/voice-bridge.ts index c95cbc9..84f44db 100644 --- a/src/server/voice-bridge.ts +++ b/src/server/voice-bridge.ts @@ -11,7 +11,7 @@ import { JoinTicketStore, type JoinTicketPayload } from "./join-ticket.js"; import { SessionManager, type ManagedSession, type SessionTeardownReason } from "./session-manager.js"; import { parseClientCommand, type ClientCommand } from "./voice-protocol.js"; import { isRecoverable, reconnectDelayMs, reconnectWindowOpen } from "./reconnect-policy.js"; -import { WebRtcAudioSession, type WebRtcAudioOptions, type WebRtcSessionDescription } from "./webrtc-audio.js"; +import { WebRtcAudioSession, type WebRtcAudioOptions, type WebRtcAudioStats, type WebRtcSessionDescription } from "./webrtc-audio.js"; const require = createRequire(import.meta.url); const { OpusEncoder } = require("@discordjs/opus") as { @@ -72,6 +72,19 @@ export interface AudioFlowStats { egressSentMaxGapMs: number; egressPeakBufferedBytes: number; egressFramesByClient: Record; + webrtcIngressRtpFrames: number; + webrtcIngressRtpFirstAt: number | null; + webrtcIngressRtpLastAt: number | null; + webrtcIngressRtpMaxGapMs: number; + webrtcEgressRtpFrames: number; + webrtcEgressRtpFirstAt: number | null; + webrtcEgressRtpLastAt: number | null; + webrtcEgressRtpMaxGapMs: number; + webrtcQueuePeakFrames: number; + webrtcQueueDroppedFrames: number; + webrtcQueueUnderrunTicks: number; + webrtcPacerLateTicks: number; + webrtcQueueCurrentFrames: number; } interface ChannelMember { @@ -658,7 +671,7 @@ export class VoiceBridge { tsClientId, channelId, memberCount: entry.members.size, - audio: { ...entry.audio }, + audio: snapshotAudioStats(entry), }; }); } @@ -684,6 +697,7 @@ export class VoiceBridge { entry.webrtc = null; if (webRtc) { try { await webRtc.close(); } catch { /* peer teardown is idempotent */ } + Object.assign(entry.audio, webRtc.getStats()); } entry.whisperTargetIds.clear(); entry.whisperActive = false; @@ -752,7 +766,13 @@ export class VoiceBridge { try { if (entry.whisperActive && entry.whisperTargetIds.size) entry.tsClient.sendWhisper(data, [...entry.whisperTargetIds], 4); else entry.tsClient.sendVoice(data, 4); + const sentAt = Date.now(); + if (entry.audio.tsSendLastAt !== null) entry.audio.tsSendMaxGapMs = Math.max(entry.audio.tsSendMaxGapMs, sentAt - entry.audio.tsSendLastAt); + entry.audio.tsSendFirstAt ??= sentAt; + entry.audio.tsSendLastAt = sentAt; + entry.audio.tsSendFrames++; } catch { + entry.audio.tsSendErrors++; // A packet arriving while the TeamSpeak session is being replaced // is discarded; the WebRTC peer remains independently closable. } @@ -949,9 +969,29 @@ function createAudioFlowStats(): AudioFlowStats { egressSentMaxGapMs: 0, egressPeakBufferedBytes: 0, egressFramesByClient: {}, + webrtcIngressRtpFrames: 0, + webrtcIngressRtpFirstAt: null, + webrtcIngressRtpLastAt: null, + webrtcIngressRtpMaxGapMs: 0, + webrtcEgressRtpFrames: 0, + webrtcEgressRtpFirstAt: null, + webrtcEgressRtpLastAt: null, + webrtcEgressRtpMaxGapMs: 0, + webrtcQueuePeakFrames: 0, + webrtcQueueDroppedFrames: 0, + webrtcQueueUnderrunTicks: 0, + webrtcPacerLateTicks: 0, + webrtcQueueCurrentFrames: 0, }; } +function snapshotAudioStats(entry: WebClientEntry): AudioFlowStats { + const stats = { ...entry.audio }; + const webRtcStats: WebRtcAudioStats | undefined = entry.webrtc?.getStats(); + if (webRtcStats) Object.assign(stats, webRtcStats); + return stats; +} + function mapChannelTree(snapshot: TSDirectorySnapshot): unknown[] { return snapshot.channels.map((channel) => ({ id: String(channel.id), diff --git a/src/server/webrtc-audio.ts b/src/server/webrtc-audio.ts index 167f9a2..d5bbd8f 100644 --- a/src/server/webrtc-audio.ts +++ b/src/server/webrtc-audio.ts @@ -26,6 +26,10 @@ const AUDIO_FRAME_BYTES = AUDIO_FRAME_SAMPLES * 2; const DEFAULT_WEBRTC_OPUS_PAYLOAD_TYPE = 111; const AUDIO_CLOCK_INTERVAL_MS = 20; const SPEAKER_ACTIVITY_INTERVAL_MS = 100; +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. @@ -55,14 +59,35 @@ export interface WebRtcAudioSessionOptions { onVoiceActivity: (clientIds: number[]) => void; } +export interface WebRtcAudioStats { + webrtcIngressRtpFrames: number; + webrtcIngressRtpFirstAt: number | null; + webrtcIngressRtpLastAt: number | null; + webrtcIngressRtpMaxGapMs: number; + webrtcEgressRtpFrames: number; + webrtcEgressRtpFirstAt: number | null; + webrtcEgressRtpLastAt: number | null; + webrtcEgressRtpMaxGapMs: number; + webrtcQueuePeakFrames: number; + webrtcQueueDroppedFrames: number; + webrtcQueueUnderrunTicks: number; + webrtcPacerLateTicks: number; + webrtcQueueCurrentFrames: number; +} + +interface PendingAudioFrame { + pcm: Buffer; + receivedAt: number; +} + /** * One low-latency WebRTC audio session for one browser connection. * * TeamSpeak provides one Opus payload per speaker. WebRTC has one negotiated - * audio track in this first transport, so this class mixes only the newest - * 20 ms frame from each active speaker and packetizes the result as RTP. The - * map is deliberately bounded to one frame per speaker; an old frame is - * replaced instead of becoming a playback queue. + * audio track in this first transport, so this class mixes one 20 ms frame + * from each active speaker and packetizes the result as RTP. Each speaker has + * a very small bounded queue so bursts from the TeamSpeak adapter can be + * smoothed without turning into an ever-growing playback delay. */ export class WebRtcAudioSession { readonly peer: RTCPeerConnection; @@ -72,10 +97,10 @@ export class WebRtcAudioSession { private readonly outgoingTrack: MediaStreamTrack; private ingressDecoder: { decode(data: Buffer): Buffer } | null = null; private readonly decoderByClient = new Map(); - private readonly pendingFrames = new Map(); + private readonly pendingFrames = new Map(); private readonly activeSpeakerIds = new Set(); private readonly opusPayloadTypes = new Set(); - private readonly audioTimer: ReturnType; + private audioTimer: ReturnType | null = null; private readonly activityTimer: ReturnType; private encoder: { encode(data: Buffer): Buffer } | null; private microphoneMuted: boolean; @@ -84,6 +109,25 @@ export class WebRtcAudioSession { private readonly ssrc = randomInt(1, 0x1_0000_0000) >>> 0; private outgoingPayloadType = DEFAULT_WEBRTC_OPUS_PAYLOAD_TYPE; private closed = false; + private nextAudioDeadline = 0; + private lastIngressRtpAt: number | null = null; + private lastEgressRtpAt: number | null = null; + private lastQueueEnqueuedAt: number | null = null; + private readonly stats: WebRtcAudioStats = { + webrtcIngressRtpFrames: 0, + webrtcIngressRtpFirstAt: null, + webrtcIngressRtpLastAt: null, + webrtcIngressRtpMaxGapMs: 0, + webrtcEgressRtpFrames: 0, + webrtcEgressRtpFirstAt: null, + webrtcEgressRtpLastAt: null, + webrtcEgressRtpMaxGapMs: 0, + webrtcQueuePeakFrames: 0, + webrtcQueueDroppedFrames: 0, + webrtcQueueUnderrunTicks: 0, + webrtcPacerLateTicks: 0, + webrtcQueueCurrentFrames: 0, + }; constructor(options: WebRtcAudioSessionOptions) { this.logger = options.logger.child({ component: "webrtc-audio", connectionId: options.connectionId }); @@ -106,7 +150,14 @@ export class WebRtcAudioSession { const audio = this.peer.addTransceiver("audio", { direction: "sendrecv" }); audio.onTrack.subscribe((track) => { track.onReceiveRtp.subscribe((rtp) => { - if (this.closed || this.microphoneMuted || !this.opusPayloadTypes.has(rtp.header.payloadType)) return; + if (this.closed) return; + const receivedAt = performance.now(); + if (this.lastIngressRtpAt !== null) this.stats.webrtcIngressRtpMaxGapMs = Math.max(this.stats.webrtcIngressRtpMaxGapMs, Math.round(receivedAt - this.lastIngressRtpAt)); + this.lastIngressRtpAt = receivedAt; + this.stats.webrtcIngressRtpFirstAt ??= Date.now(); + this.stats.webrtcIngressRtpLastAt = Date.now(); + this.stats.webrtcIngressRtpFrames++; + if (this.microphoneMuted || !this.opusPayloadTypes.has(rtp.header.payloadType)) return; const payload = Buffer.from(rtp.payload); // A disabled browser MediaStreamTrack normally still produces RTP // comfort-noise packets. TeamSpeak treats any received voice packet @@ -139,8 +190,8 @@ export class WebRtcAudioSession { throw new Error("WebRTC Opus codec is unavailable"); } - this.audioTimer = setInterval(() => this.flushAudio(), AUDIO_CLOCK_INTERVAL_MS); - this.audioTimer.unref?.(); + this.nextAudioDeadline = performance.now(); + this.scheduleAudioTick(); this.activityTimer = setInterval(() => this.flushSpeakerActivity(), SPEAKER_ACTIVITY_INTERVAL_MS); this.activityTimer.unref?.(); this.peer.onconnectionstatechange = () => { @@ -148,6 +199,10 @@ export class WebRtcAudioSession { }; } + getStats(): WebRtcAudioStats { + return { ...this.stats, webrtcQueueCurrentFrames: this.getQueueFrameCount() }; + } + async createAnswer(offer: WebRtcSessionDescription): Promise { if (this.closed) throw new Error("WebRTC session is closed"); this.setOpusPayloadTypes(offer.sdp); @@ -188,7 +243,18 @@ export class WebRtcAudioSession { sum += sample * sample; } if (Math.sqrt(sum / AUDIO_FRAME_SAMPLES) < MIN_SPEAKER_RMS) return; - this.pendingFrames.set(data.clientId, pcm.subarray(0, AUDIO_FRAME_BYTES)); + let queue = this.pendingFrames.get(data.clientId); + if (!queue) { + queue = []; + this.pendingFrames.set(data.clientId, queue); + } + if (queue.length >= MAX_MIXER_QUEUE_FRAMES) { + queue.shift(); + this.stats.webrtcQueueDroppedFrames++; + } + queue.push({ pcm: Buffer.from(pcm.subarray(0, AUDIO_FRAME_BYTES)), receivedAt: performance.now() }); + this.lastQueueEnqueuedAt = performance.now(); + this.stats.webrtcQueuePeakFrames = Math.max(this.stats.webrtcQueuePeakFrames, this.getQueueFrameCount()); this.activeSpeakerIds.add(data.clientId); } catch { // A malformed or codec-transition packet is discarded without touching @@ -199,7 +265,8 @@ export class WebRtcAudioSession { async close(): Promise { if (this.closed) return; this.closed = true; - clearInterval(this.audioTimer); + if (this.audioTimer) clearTimeout(this.audioTimer); + this.audioTimer = null; clearInterval(this.activityTimer); this.pendingFrames.clear(); this.activeSpeakerIds.clear(); @@ -210,15 +277,43 @@ export class WebRtcAudioSession { await this.peer.close(); } + private scheduleAudioTick(): void { + if (this.closed) return; + const delay = Math.max(0, this.nextAudioDeadline - performance.now()); + this.audioTimer = setTimeout(() => this.flushAudio(), delay); + this.audioTimer.unref?.(); + } + private flushAudio(): void { - if (this.closed || !this.encoder || !this.pendingFrames.size) return; + if (this.closed) return; + const now = performance.now(); + if (now > this.nextAudioDeadline + MAX_MIXER_PACER_LAG_MS) { + this.stats.webrtcPacerLateTicks++; + this.nextAudioDeadline = now; + this.trimQueuesAfterLateTick(); + } + this.nextAudioDeadline += AUDIO_CLOCK_INTERVAL_MS; + this.audioTimer = null; + this.mixAndSendAudio(); + this.scheduleAudioTick(); + } + + private mixAndSendAudio(): void { + if (!this.encoder || !this.pendingFrames.size) { + if (this.lastQueueEnqueuedAt !== null && performance.now() - this.lastQueueEnqueuedAt <= MIXER_UNDERRUN_WINDOW_MS) { + this.stats.webrtcQueueUnderrunTicks++; + } + return; + } const mixed = new Int32Array(AUDIO_FRAME_SAMPLES); let sourceCount = 0; - for (const pcm of this.pendingFrames.values()) { + for (const [clientId, queue] of this.pendingFrames) { + const frame = queue.shift(); + if (queue.length === 0) this.pendingFrames.delete(clientId); + if (!frame) continue; sourceCount++; - for (let index = 0; index < AUDIO_FRAME_SAMPLES; index++) mixed[index] += pcm.readInt16LE(index * 2); + for (let index = 0; index < AUDIO_FRAME_SAMPLES; index++) mixed[index] += frame.pcm.readInt16LE(index * 2); } - this.pendingFrames.clear(); if (!sourceCount) return; const pcm = Buffer.allocUnsafe(AUDIO_FRAME_BYTES); @@ -239,6 +334,12 @@ export class WebRtcAudioSession { this.sequenceNumber = (this.sequenceNumber + 1) & 0xffff; this.timestamp = (this.timestamp + AUDIO_FRAME_SAMPLES) >>> 0; this.outgoingTrack.writeRtp(packet); + const sentAt = performance.now(); + if (this.lastEgressRtpAt !== null) this.stats.webrtcEgressRtpMaxGapMs = Math.max(this.stats.webrtcEgressRtpMaxGapMs, Math.round(sentAt - this.lastEgressRtpAt)); + this.lastEgressRtpAt = sentAt; + this.stats.webrtcEgressRtpFirstAt ??= Date.now(); + this.stats.webrtcEgressRtpLastAt = Date.now(); + this.stats.webrtcEgressRtpFrames++; } catch { // A peer closing concurrently may reject a packet; teardown owns the // session lifecycle and no per-frame error needs to reach the logs. @@ -275,6 +376,27 @@ export class WebRtcAudioSession { } } + private trimQueuesAfterLateTick(): void { + const now = performance.now(); + for (const [clientId, queue] of this.pendingFrames) { + while (queue.length > 1) { + queue.shift(); + this.stats.webrtcQueueDroppedFrames++; + } + if (queue[0] && now - queue[0].receivedAt > MAX_MIXER_FRAME_AGE_MS) { + queue.shift(); + this.stats.webrtcQueueDroppedFrames++; + } + if (queue.length === 0) this.pendingFrames.delete(clientId); + } + } + + private getQueueFrameCount(): number { + let count = 0; + for (const queue of this.pendingFrames.values()) count += queue.length; + return count; + } + private setOpusPayloadTypes(sdp: string): void { this.opusPayloadTypes.clear(); for (const match of sdp.matchAll(/^a=rtpmap:(\d+)\s+opus\/48000(?:\/\d+)?/gim)) {