From 2f5e27f0522ac0d4c1551c028d303886b4284d8b Mon Sep 17 00:00:00 2001 From: Miroslav Pejic Date: Sat, 22 Jul 2023 18:32:29 +0200 Subject: [PATCH] [mirotalksfu] - save init audio/video status --- public/js/LocalStorage.js | 54 ++++++++++++++++++++++++++++------- public/js/Room.js | 60 +++++++++++++++++++++++++++------------ 2 files changed, 86 insertions(+), 28 deletions(-) diff --git a/public/js/LocalStorage.js b/public/js/LocalStorage.js index 7f187fa6..08e7adb1 100644 --- a/public/js/LocalStorage.js +++ b/public/js/LocalStorage.js @@ -5,9 +5,16 @@ class LocalStorage { this.MEDIA_TYPE = { audio: 'audio', video: 'video', + audioVideo: 'audioVideo', speaker: 'speaker', }; + this.INIT_CONFIG = { + audio: true, + video: true, + audioVideo: true, + }; + this.DEVICES_COUNT = { audio: 0, speaker: 0, @@ -33,6 +40,33 @@ class LocalStorage { }; } + // #################################################### + // SET LOCAL STORAGE + // #################################################### + + setItemLocalStorage(key, value) { + localStorage.setItem(key, value); + } + + setObjectLocalStorage(name, object) { + localStorage.setItem(name, JSON.stringify(object)); + } + + setInitConfig(type, status) { + switch (type) { + case this.MEDIA_TYPE.audio: + this.INIT_CONFIG.audio = status; + break; + case this.MEDIA_TYPE.video: + this.INIT_CONFIG.video = status; + break; + case this.MEDIA_TYPE.audioVideo: + this.INIT_CONFIG.audioVideo = status; + break; + } + this.setObjectLocalStorage('INIT_CONFIG', this.INIT_CONFIG); + } + setLocalStorageDevices(type, index, select) { switch (type) { case this.MEDIA_TYPE.audio: @@ -53,25 +87,25 @@ class LocalStorage { default: break; } - localStorage.setItem('LOCAL_STORAGE_DEVICES', JSON.stringify(this.LOCAL_STORAGE_DEVICES)); + this.setObjectLocalStorage('LOCAL_STORAGE_DEVICES', this.LOCAL_STORAGE_DEVICES); + } + + // #################################################### + // GET LOCAL STORAGE + // #################################################### + + getInitConfig() { + return this.getObjectLocalStorage('INIT_CONFIG'); } getLocalStorageDevices() { - return JSON.parse(localStorage.getItem('LOCAL_STORAGE_DEVICES')); - } - - setItemLocalStorage(key, value) { - localStorage.setItem(key, value); + return this.getObjectLocalStorage('LOCAL_STORAGE_DEVICES'); } getItemLocalStorage(key) { localStorage.getItem(key); } - setObjectLocalStorage(name, object) { - localStorage.setItem(name, JSON.stringify(object)); - } - getObjectLocalStorage(name) { return JSON.parse(localStorage.getItem(name)); } diff --git a/public/js/Room.js b/public/js/Room.js index 6d3b9612..137c8039 100644 --- a/public/js/Room.js +++ b/public/js/Room.js @@ -254,18 +254,10 @@ async function initEnumerateDevices() { if (navigator.getDisplayMedia || navigator.mediaDevices.getDisplayMedia) { BUTTONS.main.startScreenButton && show(initStartScreenButton); } - whoAreYou(); - if (!isVideoAllowed) { - hide(initVideo); - hide(initVideoSelect); - } - if (!isAudioAllowed) { - hide(initMicrophoneSelect); - hide(initSpeakerSelect); - } if (!isAudioAllowed && !isVideoAllowed && !joinRoomWithoutAudioVideo) { openURL(`/permission?room_id=${room_id}&message=Not allowed both Audio and Video`); } else { + whoAreYou(); setButtonsInit(); setSelectsInit(); handleSelectsInit(); @@ -463,6 +455,23 @@ function getRoomPassword() { } } +// #################################################### +// INIT CONFIG +// #################################################### + +function checkInitConfig() { + const initConfig = lS.getInitConfig(); + console.log('04.5 ----> Get init config', initConfig); + if (initConfig) { + if (isAudioVideoAllowed && !initConfig.audioVideo) { + handleAudioVideo(); + } else { + if (isAudioAllowed && !initConfig.audio) handleAudio(); + if (isVideoAllowed && !initConfig.video) handleVideo(); + } + } +} + // #################################################### // SOME PEER INFO // #################################################### @@ -552,36 +561,50 @@ function whoAreYou() { getPeerInfo(); joinRoom(peer_name, room_id); }); + + if (!isVideoAllowed) { + hide(initVideo); + hide(initVideoSelect); + } + if (!isAudioAllowed) { + hide(initMicrophoneSelect); + hide(initSpeakerSelect); + } } -function handleAudio(e) { +function handleAudio() { isAudioAllowed = isAudioAllowed ? false : true; - e.target.className = 'fas fa-microphone' + (isAudioAllowed ? '' : '-slash'); - setColor(e.target, isAudioAllowed ? 'white' : 'red'); + initAudioButton.className = 'fas fa-microphone' + (isAudioAllowed ? '' : '-slash'); + setColor(initAudioButton, isAudioAllowed ? 'white' : 'red'); setColor(startAudioButton, isAudioAllowed ? 'white' : 'red'); checkInitAudio(isAudioAllowed); + lS.setInitConfig(lS.MEDIA_TYPE.audio, isAudioAllowed); } -function handleVideo(e) { +function handleVideo() { isVideoAllowed = isVideoAllowed ? false : true; - e.target.className = 'fas fa-video' + (isVideoAllowed ? '' : '-slash'); - setColor(e.target, isVideoAllowed ? 'white' : 'red'); + initVideoButton.className = 'fas fa-video' + (isVideoAllowed ? '' : '-slash'); + setColor(initVideoButton, isVideoAllowed ? 'white' : 'red'); setColor(startVideoButton, isVideoAllowed ? 'white' : 'red'); checkInitVideo(isVideoAllowed); + lS.setInitConfig(lS.MEDIA_TYPE.video, isVideoAllowed); } -function handleAudioVideo(e) { +function handleAudioVideo() { isAudioVideoAllowed = isAudioVideoAllowed ? false : true; isAudioAllowed = isAudioVideoAllowed; isVideoAllowed = isAudioVideoAllowed; + lS.setInitConfig(lS.MEDIA_TYPE.audio, isAudioVideoAllowed); + lS.setInitConfig(lS.MEDIA_TYPE.video, isAudioVideoAllowed); + lS.setInitConfig(lS.MEDIA_TYPE.audioVideo, isAudioVideoAllowed); initAudioButton.className = 'fas fa-microphone' + (isAudioVideoAllowed ? '' : '-slash'); initVideoButton.className = 'fas fa-video' + (isAudioVideoAllowed ? '' : '-slash'); + initAudioVideoButton.className = 'fas fa-eye' + (isAudioVideoAllowed ? '' : '-slash'); if (!isAudioVideoAllowed) { hide(initAudioButton); hide(initVideoButton); } - e.target.className = 'fas fa-eye' + (isAudioVideoAllowed ? '' : '-slash'); - setColor(e.target, isAudioVideoAllowed ? 'white' : 'red'); + setColor(initAudioVideoButton, isAudioVideoAllowed ? 'white' : 'red'); setColor(initAudioButton, isAudioAllowed ? 'white' : 'red'); setColor(initVideoButton, isVideoAllowed ? 'white' : 'red'); setColor(startAudioButton, isAudioAllowed ? 'white' : 'red'); @@ -1247,6 +1270,7 @@ async function changeCamera(deviceId) { '04.5 ----> Success attached init cam video stream', initStream.getVideoTracks()[0].getSettings(), ); + checkInitConfig(); }) .catch((err) => { console.error('[Error] changeCamera', err);