diff --git a/src/server/voice-bridge.ts b/src/server/voice-bridge.ts index a7d0b16..a6525b1 100644 --- a/src/server/voice-bridge.ts +++ b/src/server/voice-bridge.ts @@ -425,17 +425,20 @@ export class VoiceBridge { if (ws.readyState !== WebSocket.OPEN || data.clientId === selfId) return; const webRtc = entry!.webrtc; webRtc?.pushTeamSpeakVoice(data); - // A negotiated WebRTC session owns the browser's realtime audio - // egress. Do not also send the same TeamSpeak packet over the - // reliable WebSocket, otherwise the browser plays two copies and - // the TCP path can still accumulate stale audio behind the peer. - if (webRtc) return; const now = Date.now(); if (entry!.audio.egressLastAt !== null) entry!.audio.egressMaxGapMs = Math.max(entry!.audio.egressMaxGapMs, now - entry!.audio.egressLastAt); entry!.audio.egressFirstAt ??= now; entry!.audio.egressLastAt = now; const sourceKey = String(data.clientId); entry!.audio.egressFramesByClient[sourceKey] = (entry!.audio.egressFramesByClient[sourceKey] ?? 0) + 1; + // A negotiated WebRTC session owns the browser's realtime audio + // egress. Do not also send the same TeamSpeak packet over the + // reliable WebSocket, otherwise the browser plays two copies and + // the TCP path can still accumulate stale audio behind the peer. + if (webRtc) { + entry!.audio.egressFrames++; + return; + } const packet = Buffer.allocUnsafe(3 + data.data.length); packet[0] = data.codec; packet.writeUInt16BE(data.clientId, 1); @@ -690,6 +693,11 @@ export class VoiceBridge { config, logger: this.logger, onVoiceFrame: (data) => { + const now = Date.now(); + if (entry.audio.ingressLastAt !== null) entry.audio.ingressMaxGapMs = Math.max(entry.audio.ingressMaxGapMs, now - entry.audio.ingressLastAt); + entry.audio.ingressFirstAt ??= now; + entry.audio.ingressLastAt = now; + entry.audio.ingressFrames++; try { if (entry.whisperActive && entry.whisperTargetIds.size) entry.tsClient.sendWhisper(data, [...entry.whisperTargetIds], 4); else entry.tsClient.sendVoice(data, 4); diff --git a/src/server/webrtc-audio.ts b/src/server/webrtc-audio.ts index 2336d0a..ca8b645 100644 --- a/src/server/webrtc-audio.ts +++ b/src/server/webrtc-audio.ts @@ -18,7 +18,7 @@ const { OpusDecoder, OpusEncoder } = require("@discordjs/opus") as { const AUDIO_SAMPLE_RATE = 48_000; const AUDIO_FRAME_SAMPLES = 960; const AUDIO_FRAME_BYTES = AUDIO_FRAME_SAMPLES * 2; -const WEBRTC_OPUS_PAYLOAD_TYPE = 111; +const DEFAULT_WEBRTC_OPUS_PAYLOAD_TYPE = 111; const AUDIO_CLOCK_INTERVAL_MS = 20; const SPEAKER_ACTIVITY_INTERVAL_MS = 100; @@ -59,12 +59,14 @@ export class WebRtcAudioSession { private readonly decoderByClient = new Map(); private readonly pendingFrames = new Map(); private readonly activeSpeakerIds = new Set(); + private readonly opusPayloadTypes = new Set(); private readonly audioTimer: ReturnType; private readonly activityTimer: ReturnType; private encoder: { encode(data: Buffer): Buffer } | null; private sequenceNumber = randomInt(0, 65_536); private timestamp = randomInt(0, 0x1_0000_0000) >>> 0; private readonly ssrc = randomInt(1, 0x1_0000_0000) >>> 0; + private outgoingPayloadType = DEFAULT_WEBRTC_OPUS_PAYLOAD_TYPE; private closed = false; constructor(options: WebRtcAudioSessionOptions) { @@ -86,7 +88,7 @@ export class WebRtcAudioSession { const audio = this.peer.addTransceiver("audio", { direction: "sendrecv" }); audio.onTrack.subscribe((track) => { track.onReceiveRtp.subscribe((rtp) => { - if (this.closed || rtp.header.payloadType !== WEBRTC_OPUS_PAYLOAD_TYPE) return; + if (this.closed || !this.opusPayloadTypes.has(rtp.header.payloadType)) return; this.onVoiceFrame(Buffer.from(rtp.payload)); }); void audio.sender.replaceTrack(this.outgoingTrack).catch((error: unknown) => { @@ -119,6 +121,7 @@ export class WebRtcAudioSession { async createAnswer(offer: WebRtcSessionDescription): Promise { if (this.closed) throw new Error("WebRTC session is closed"); + this.setOpusPayloadTypes(offer.sdp); await this.peer.setRemoteDescription(offer); const transceiver = this.peer.getTransceivers().find((candidate) => candidate.kind === "audio"); if (transceiver) await transceiver.sender.replaceTrack(this.outgoingTrack); @@ -185,7 +188,7 @@ export class WebRtcAudioSession { try { const encoded = this.encoder.encode(pcm); const packet = new RtpPacket(new RtpHeader({ - payloadType: WEBRTC_OPUS_PAYLOAD_TYPE, + payloadType: this.outgoingPayloadType, sequenceNumber: this.sequenceNumber, timestamp: this.timestamp, ssrc: this.ssrc, @@ -206,4 +209,15 @@ export class WebRtcAudioSession { this.activeSpeakerIds.clear(); this.onVoiceActivity(ids); } + + private setOpusPayloadTypes(sdp: string): void { + this.opusPayloadTypes.clear(); + for (const match of sdp.matchAll(/^a=rtpmap:(\d+)\s+opus\/48000(?:\/\d+)?/gim)) { + const payloadType = Number(match[1]); + if (!Number.isInteger(payloadType) || payloadType < 0 || payloadType > 127) continue; + this.opusPayloadTypes.add(payloadType); + this.outgoingPayloadType = payloadType; + } + if (!this.opusPayloadTypes.size) this.opusPayloadTypes.add(DEFAULT_WEBRTC_OPUS_PAYLOAD_TYPE); + } }