diff --git a/assets/public/custom/rtcPatcher.js b/assets/public/custom/rtcPatcher.js index 276c7498..52c77fe0 100644 --- a/assets/public/custom/rtcPatcher.js +++ b/assets/public/custom/rtcPatcher.js @@ -64,4 +64,51 @@ function addFingerprintToSdp(sdp) { } return result.join("\n"); +} + +if (window.location.pathname.startsWith("/channel")) { + const observer = new MutationObserver(() => { + const screenshareButton = document.querySelector('[aria-label="Share your screen"]'); + if (screenshareButton) { + screenshareButton.addEventListener("click", async () => { + try { + const displayStream = await navigator.mediaDevices.getDisplayMedia({ + video: { + width: { ideal: 1920 }, + height: { ideal: 1080 }, + frameRate: { ideal: 30 } + }, + audio: false + }); + + const audioStream = await navigator.mediaDevices.getUserMedia({ + audio: { + echoCancellation: { ideal: true }, + autoGainControl: { ideal: true }, + noiseSuppression: { ideal: true } + } + }); + + const combinedStream = new MediaStream(); + + displayStream.getVideoTracks().forEach(track => { + combinedStream.addTrack(track); + }); + + audioStream.getAudioTracks().forEach(track => { + combinedStream.addTrack(track); + }); + + displayStream.getVideoTracks()[0].onended = () => { + audioStream.getTracks().forEach(track => track.stop()); + }; + } catch (error) { + console.error("Screenshare failed:", error); + } + }, { once: true }); + observer.disconnect(); + } + }); + + observer.observe(document.body, { childList: true, subtree: true }); } \ No newline at end of file