From edd91a8f3ff12bd24f0053fd74c909a775f06f3c Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Sat, 18 Jan 2025 02:36:13 +0100 Subject: [PATCH] Delay opening safariStream until after join. Also close the stream when we leave. --- static/galene.js | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/static/galene.js b/static/galene.js index 6c3d59b..c3c7da3 100644 --- a/static/galene.js +++ b/static/galene.js @@ -352,14 +352,6 @@ function isFirefox() { return ua.indexOf('firefox') >= 0; } -/** - * Under Safari, we request access to the camera at startup in order to - * enable autoplay. The camera stream is stored in safariStream. - * - * @type {MediaStream} - */ -let safariStream = null; - /** * setConnected is called whenever we connect or disconnect to the server. * @@ -376,15 +368,6 @@ function setConnected(connected) { window.onresize = function(e) { scheduleReconsiderDownRate(); } - if(isSafari()) { - /* Safari doesn't allow autoplay and omits host candidates - * unless there is an open device. */ - if(!safariStream) { - navigator.mediaDevices.getUserMedia({audio: true}).then(s => { - safariStream = s; - }); - } - } } else { userbox.classList.add('invisible'); connectionbox.classList.remove('invisible'); @@ -506,6 +489,7 @@ function onPeerConnection() { */ function gotClose(code, reason) { closeUpMedia(); + closeSafariStream(); setConnected(false); if(code != 1000) { console.warn('Socket close', code, reason); @@ -2726,6 +2710,28 @@ function setTitle(title) { set('Galène'); } +/** + * Under Safari, we request access to the camera at startup in order to + * enable autoplay. The camera stream is stored in safariStream. + * + * @type {MediaStream} + */ +let safariStream = null; + +async function openSafariStream() { + if(!isSafari()) + return; + + if(!safariStream) + safariStream = await navigator.mediaDevices.getUserMedia({audio: true}) +} + +async function closeSafariStream() { + if(!safariStream) + return; + stopStream(safariStream); + safariStream = null; +} /** * @this {ServerConnection} @@ -2749,15 +2755,18 @@ async function gotJoined(kind, group, perms, status, data, error, message) { token = null; displayError('The server said: ' + message); } + closeSafariStream(); this.close(); setButtonsVisibility(); return; case 'redirect': + closeSafariStream(); this.close(); token = null; document.location.href = message; return; case 'leave': + closeSafariStream(); this.close(); setButtonsVisibility(); setChangePassword(null); @@ -2768,6 +2777,7 @@ async function gotJoined(kind, group, perms, status, data, error, message) { probingState = 'success'; setVisibility('userform', false); setVisibility('passwordform', false); + closeSafariStream(); this.close(); setButtonsVisibility(); return; @@ -2783,12 +2793,14 @@ async function gotJoined(kind, group, perms, status, data, error, message) { setChangePassword(pwAuth && !!groupStatus.canChangePassword && serverConnection.username ); + openSafariStream(); if(kind === 'change') return; break; default: token = null; displayError('Unknown join message'); + closeSafariStream(); this.close(); return; }