mute/unmute speaker

This commit is contained in:
Ramón Souza 2024-04-23 11:01:59 -03:00
parent 528817aeab
commit cac0f6cd38
3 changed files with 39 additions and 9 deletions

View File

@ -21,10 +21,13 @@ import useMeeting from '/imports/ui/core/hooks/useMeeting';
import useCurrentUser from '/imports/ui/core/hooks/useCurrentUser';
import { EXTERNAL_VIDEO_STOP } from '../external-video-player/mutations';
import { SET_AWAY } from '/imports/ui/components/user-list/user-list-content/user-participants/user-list-participants/user-actions/mutations';
import { toggleVoice } from '/imports/ui/components/user-list/user-list-content/user-participants/user-list-participants/user-actions/service';
import VideoService from '/imports/ui/components/video-provider/service';
import AudioService from '/imports/ui/components/audio/service';
import useToggleVoice from '/imports/ui/components/audio/audio-graphql/hooks/useToggleVoice';
import {
getSpeakerLevel,
setSpeakerLevel,
toggleMuteMicrophone,
} from '../audio/audio-graphql/audio-controls/input-stream-live-selector/service';
const ActionsBarContainer = (props) => {
const actionsBarStyle = layoutSelectOutput((i) => i.actionBar);
@ -62,18 +65,30 @@ const ActionsBarContainer = (props) => {
const [setAway] = useMutation(SET_AWAY);
const voiceToggle = useToggleVoice();
const prevMutedRef = React.useRef(false);
const prevSpeakerLevel = React.useRef(0);
const muteAway = (away) => {
const isMuted = currentUserData?.voice?.muted;
const prevAwayMuted = prevMutedRef.current;
const prevSpeakerLevelValue = prevSpeakerLevel.current;
// mute/unmute microphone
if (isMuted === away && isMuted === prevAwayMuted) {
AudioService.toggleMuteMicrophone(toggleVoice, voiceToggle);
toggleMuteMicrophone(isMuted, voiceToggle);
prevMutedRef.current = !isMuted;
} else if (!away && !isMuted && prevAwayMuted) {
AudioService.toggleMuteMicrophone(toggleVoice, voiceToggle);
toggleMuteMicrophone(isMuted, voiceToggle);
}
// mute/unmute speaker
if (away) {
setSpeakerLevel(prevSpeakerLevelValue);
} else {
prevSpeakerLevel.current = getSpeakerLevel();
setSpeakerLevel(0);
}
// enable/disable video
VideoService.setTrackEnabled(away);
};

View File

@ -13,6 +13,7 @@ const TOGGLE_MUTE_THROTTLE_TIME = window.meetingClientSettings.public.media.togg
const DEVICE_LABEL_MAX_LENGTH = 40;
const CLIENT_DID_USER_SELECTED_MICROPHONE_KEY = 'clientUserSelectedMicrophone';
const CLIENT_DID_USER_SELECTED_LISTEN_ONLY_KEY = 'clientUserSelectedListenOnly';
const MEDIA_TAG = window.meetingClientSettings.public.media.mediaTag;
export const handleLeaveAudio = (meetingIsBreakout: boolean) => {
if (!meetingIsBreakout) {
@ -81,10 +82,24 @@ export const liveChangeInputDevice = (inputDeviceId: string) => AudioManager.liv
export const liveChangeOutputDevice = (inputDeviceId: string, isLive: boolean) => AudioManager
.changeOutputDevice(inputDeviceId, isLive);
export const getSpeakerLevel = () => {
const audioElement = document.querySelector(MEDIA_TAG) as HTMLMediaElement;
return audioElement ? audioElement.volume : 0;
};
export const setSpeakerLevel = (level: number) => {
const audioElement = document.querySelector(MEDIA_TAG) as HTMLMediaElement;
if (audioElement) {
audioElement.volume = level;
}
};
export default {
handleLeaveAudio,
toggleMuteMicrophone,
truncateDeviceName,
notify,
liveChangeInputDevice,
getSpeakerLevel,
setSpeakerLevel,
};

View File

@ -92,25 +92,25 @@ const isVoiceUser = () => {
return voiceUser ? voiceUser.joined : false;
};
const toggleMuteMicrophone = throttle((toggleVoice, voiceToggle) => {
const toggleMuteMicrophone = throttle((toggleVoice) => {
const user = VoiceUsers.findOne({
userId: Auth.userID,
}, { fields: { muted: 1 } });
Storage.setItem(MUTED_KEY, !user?.muted);
Storage.setItem(MUTED_KEY, !user.muted);
if (user?.muted) {
if (user.muted) {
logger.info({
logCode: 'audiomanager_unmute_audio',
extraInfo: { logType: 'user_action' },
}, 'microphone unmuted by user');
toggleVoice(Auth.userID, voiceToggle);
toggleVoice();
} else {
logger.info({
logCode: 'audiomanager_mute_audio',
extraInfo: { logType: 'user_action' },
}, 'microphone muted by user');
toggleVoice(Auth.userID, voiceToggle);
toggleVoice();
}
}, TOGGLE_MUTE_THROTTLE_TIME);