From c907559282bb5f831b88008360da92027176c91a Mon Sep 17 00:00:00 2001 From: Miroslav Pejic Date: Mon, 5 Feb 2024 21:23:13 +0100 Subject: [PATCH] [mirotalksfu] - fix audio --- public/js/Room.js | 9 +++++---- public/js/RoomClient.js | 18 +++++++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/public/js/Room.js b/public/js/Room.js index 397ba1ea..1473d86d 100644 --- a/public/js/Room.js +++ b/public/js/Room.js @@ -73,6 +73,9 @@ const wbHeight = 600; const swalImageUrl = '../images/pricing-illustration.svg'; +// Media +const sinkId = 'sinkId' in HTMLMediaElement.prototype; + // #################################################### // LOCAL STORAGE // #################################################### @@ -447,7 +450,6 @@ async function enumerateAudioDevices(stream) { .then(async () => { await stopTracks(stream); isEnumerateAudioDevices = true; - const sinkId = 'sinkId' in HTMLMediaElement.prototype; speakerSelect.disabled = !sinkId; // Check if there is speakers if (!sinkId || initSpeakerSelect.options.length === 0) { @@ -1606,8 +1608,7 @@ async function setSelectsInit() { current: speakerSelect.value, }); - // TODO: Fix me, keep for now always default speaker... - if (true || !initSpeakerExist || !speakerExist) { + if (!initSpeakerExist || !speakerExist) { console.log('04.2 ----> Speaker devices seems changed, use default index 0'); initSpeakerSelect.selectedIndex = 0; speakerSelect.selectedIndex = 0; @@ -1765,7 +1766,7 @@ function handleSelects() { refreshLsDevices(); }; speakerSelect.onchange = () => { - rc.attachSinkId(rc.myAudioEl, speakerSelect.value); + rc.changeAudioDestination(); refreshLsDevices(); }; switchPushToTalk.onchange = (e) => { diff --git a/public/js/RoomClient.js b/public/js/RoomClient.js index a443eb2a..ade1cc87 100644 --- a/public/js/RoomClient.js +++ b/public/js/RoomClient.js @@ -1697,9 +1697,6 @@ class RoomClient { this.myAudioEl = elem; this.localAudioEl.appendChild(elem); this.attachMediaStream(elem, stream, type, 'Producer'); - if (this.isAudioAllowed && !this._moderator.audio_start_muted && !speakerSelect.disabled) { - this.attachSinkId(elem, speakerSelect.value); - } console.log('[addProducer] audio-element-count', this.localAudioEl.childElementCount); break; default: @@ -2104,6 +2101,9 @@ class RoomClient { this.handlePV(id + '___' + audioConsumerId); this.setPeerAudio(remotePeerId, remotePeerAudio); } + if (sinkId && speakerSelect.value) { + this.changeAudioDestination(elem); + } console.log('[Add audioConsumers]', this.audioConsumers); break; default: @@ -2377,6 +2377,18 @@ class RoomClient { console.log(who + ' Success attached media ' + type); } + async changeAudioDestination(audioElement = false) { + const audioDestination = speakerSelect.value; + if (audioElement) { + await this.attachSinkId(audioElement, audioDestination); + } else { + const audioElements = this.remoteAudioEl.querySelectorAll('audio'); + audioElements.forEach(async (audioElement) => { + await this.attachSinkId(audioElement, audioDestination); + }); + } + } + async attachSinkId(elem, sinkId) { if (typeof elem.sinkId !== 'undefined') { elem.setSinkId(sinkId)