From 831d76c10e7bf036766691e7555ecab0bb777cfc Mon Sep 17 00:00:00 2001 From: Kibi Kelburton Date: Mon, 23 Feb 2026 04:11:46 +0100 Subject: [PATCH] maybe this fixes the poor audio idk loool --- public/viewer.js | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/public/viewer.js b/public/viewer.js index 5eb7564..e7f304a 100644 --- a/public/viewer.js +++ b/public/viewer.js @@ -23,12 +23,12 @@ socket.on('offer', (id, description) => { overlay.classList.add('hidden'); }; - // Auto-unmute when the user interacts with the document to bypass browser// Auto-unmute when the user interacts with the document to bypass browser -document.addEventListener('click', () => { - remoteVideo.muted = false; - // Set sink to max possible volume explicitly avoiding browser gain staging - remoteVideo.volume = 1.0; -}, {once: true}); + // Auto-unmute when the user interacts with the document to bypass browser autoplay restrictions + document.addEventListener('click', () => { + remoteVideo.muted = false; + // Set sink to max possible volume explicitly avoiding browser gain staging + remoteVideo.volume = 1.0; + }, {once: true}); peerConnection.onicecandidate = event => { if (event.candidate) { @@ -39,7 +39,30 @@ document.addEventListener('click', () => { peerConnection .setRemoteDescription(description) .then(() => peerConnection.createAnswer()) - .then(sdp => peerConnection.setLocalDescription(sdp)) + .then(sdp => { + let sdpLines = sdp.sdp.split('\r\n'); + let opusPayloadType = null; + for (let i = 0; i < sdpLines.length; i++) { + if (sdpLines[i].includes('a=rtpmap:') && sdpLines[i].includes('opus/48000/2')) { + const match = sdpLines[i].match(/a=rtpmap:(\d+) /); + if (match) opusPayloadType = match[1]; + } + } + if (opusPayloadType) { + let fmtpFound = false; + for (let i = 0; i < sdpLines.length; i++) { + if (sdpLines[i].startsWith(`a=fmtp:${opusPayloadType}`)) { + sdpLines[i] = `a=fmtp:${opusPayloadType} minptime=10;useinbandfec=1;maxplaybackrate=48000;stereo=1;sprop-stereo=1;maxaveragebitrate=510000;cbr=1`; + fmtpFound = true; + } + } + if (!fmtpFound) { + sdpLines.push(`a=fmtp:${opusPayloadType} minptime=10;useinbandfec=1;maxplaybackrate=48000;stereo=1;sprop-stereo=1;maxaveragebitrate=510000;cbr=1`); + } + } + sdp.sdp = sdpLines.join('\r\n'); + return peerConnection.setLocalDescription(sdp); + }) .then(() => { socket.emit('answer', id, peerConnection.localDescription); });