From 875366d2581776a15bbe9067b1fd2f0ccad75a85 Mon Sep 17 00:00:00 2001 From: Miroslav Pejic Date: Sat, 3 Feb 2024 19:48:29 +0100 Subject: [PATCH] [mirotalksfu] - fix ls devices --- public/js/Room.js | 38 ++++++++++++++++++++------------------ public/js/RoomClient.js | 4 ++-- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/public/js/Room.js b/public/js/Room.js index 4b7e871f..448ff561 100644 --- a/public/js/Room.js +++ b/public/js/Room.js @@ -1565,15 +1565,15 @@ function handleSelectsInit() { initVideoSelect.onchange = async () => { await changeCamera(initVideoSelect.value); videoSelect.selectedIndex = initVideoSelect.selectedIndex; - lS.setLocalStorageDevices(lS.MEDIA_TYPE.video, initVideoSelect.selectedIndex, initVideoSelect.value); + await refreshLsDevices(); }; - initMicrophoneSelect.onchange = () => { + initMicrophoneSelect.onchange = async () => { microphoneSelect.selectedIndex = initMicrophoneSelect.selectedIndex; - lS.setLocalStorageDevices(lS.MEDIA_TYPE.audio, initMicrophoneSelect.selectedIndex, initMicrophoneSelect.value); + await refreshLsDevices(); }; - initSpeakerSelect.onchange = () => { + initSpeakerSelect.onchange = async () => { speakerSelect.selectedIndex = initSpeakerSelect.selectedIndex; - lS.setLocalStorageDevices(lS.MEDIA_TYPE.speaker, initSpeakerSelect.selectedIndex, initSpeakerSelect.value); + await refreshLsDevices(); }; } @@ -1606,23 +1606,19 @@ async function setSelectsInit() { console.log('04.1 ----> Audio devices seems changed, use default index 0'); initMicrophoneSelect.selectedIndex = 0; microphoneSelect.selectedIndex = 0; - lS.setLocalStorageDevices( - lS.MEDIA_TYPE.audio, - initMicrophoneSelect.selectedIndex, - initMicrophoneSelect.value, - ); + await refreshLsDevices(); } if (lS.DEVICES_COUNT.speaker !== localStorageDevices.speaker.count) { console.log('04.2 ----> Speaker devices seems changed, use default index 0'); initSpeakerSelect.selectedIndex = 0; speakerSelect.selectedIndex = 0; - lS.setLocalStorageDevices(lS.MEDIA_TYPE.speaker, initSpeakerSelect.selectedIndex, initSpeakerSelect.value); + await refreshLsDevices(); } if (lS.DEVICES_COUNT.video !== localStorageDevices.video.count) { console.log('04.3 ----> Video devices seems changed, use default index 0'); initVideoSelect.selectedIndex = 0; videoSelect.selectedIndex = 0; - lS.setLocalStorageDevices(lS.MEDIA_TYPE.video, initVideoSelect.selectedIndex, initVideoSelect.value); + await refreshLsDevices(); } // console.log('04.4 ----> Get Local Storage Devices after', lS.getLocalStorageDevices()); @@ -1630,6 +1626,12 @@ async function setSelectsInit() { if (initVideoSelect.value) await changeCamera(initVideoSelect.value); } +async function refreshLsDevices() { + lS.setLocalStorageDevices(lS.MEDIA_TYPE.video, videoSelect.selectedIndex, videoSelect.value); + lS.setLocalStorageDevices(lS.MEDIA_TYPE.audio, microphoneSelect.selectedIndex, microphoneSelect.value); + lS.setLocalStorageDevices(lS.MEDIA_TYPE.speaker, speakerSelect.selectedIndex, speakerSelect.value); +} + async function changeCamera(deviceId) { if (initStream) { await stopTracks(initStream); @@ -1722,10 +1724,10 @@ function handleCameraMirror(video) { function handleSelects() { // devices options - videoSelect.onchange = () => { + videoSelect.onchange = async () => { videoQuality.selectedIndex = 0; rc.closeThenProduce(RoomClient.mediaType.video, videoSelect.value); - lS.setLocalStorageDevices(lS.MEDIA_TYPE.video, videoSelect.selectedIndex, videoSelect.value); + await refreshLsDevices(); }; videoQuality.onchange = () => { rc.closeThenProduce(RoomClient.mediaType.video, videoSelect.value); @@ -1740,13 +1742,13 @@ function handleSelects() { localStorageSettings.screen_fps = screenFps.selectedIndex; lS.setSettings(localStorageSettings); }; - microphoneSelect.onchange = () => { + microphoneSelect.onchange = async () => { rc.closeThenProduce(RoomClient.mediaType.audio, microphoneSelect.value); - lS.setLocalStorageDevices(lS.MEDIA_TYPE.audio, microphoneSelect.selectedIndex, microphoneSelect.value); + await refreshLsDevices(); }; - speakerSelect.onchange = () => { + speakerSelect.onchange = async () => { rc.attachSinkId(rc.myAudioEl, speakerSelect.value); - lS.setLocalStorageDevices(lS.MEDIA_TYPE.speaker, speakerSelect.selectedIndex, speakerSelect.value); + await refreshLsDevices(); }; switchPushToTalk.onchange = (e) => { const producerExist = rc.producerExist(RoomClient.mediaType.audio); diff --git a/public/js/RoomClient.js b/public/js/RoomClient.js index 1e89233e..2a221fd7 100644 --- a/public/js/RoomClient.js +++ b/public/js/RoomClient.js @@ -2383,7 +2383,7 @@ class RoomClient { .then(() => { console.log(`Success, audio output device attached: ${sinkId}`); }) - .catch((err) => { + .catch(async (err) => { let errorMessage = err; let speakerSelect = this.getId('speakerSelect'); if (err.name === 'SecurityError') @@ -2391,7 +2391,7 @@ class RoomClient { console.error('Attach SinkId error: ', errorMessage); this.userLog('error', errorMessage, 'top-end', 6000); speakerSelect.selectedIndex = 0; - lS.setLocalStorageDevices(lS.MEDIA_TYPE.speaker, 0, speakerSelect.value); + await refreshLsDevices(); }); } else { let error = `Browser seems doesn't support output device selection.`;