bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/audio/service.js

174 lines
6.1 KiB
JavaScript
Raw Normal View History

import Users from '/imports/api/users';
2017-04-19 23:01:28 +08:00
import Auth from '/imports/ui/services/auth';
import { throttle } from '/imports/utils/throttle';
2023-08-10 00:06:57 +08:00
import { debounce } from '/imports/utils/debounce';
2017-10-13 03:22:10 +08:00
import AudioManager from '/imports/ui/services/audio-manager';
import Meetings from '/imports/api/meetings';
import VoiceUsers from '/imports/api/voice-users';
import logger from '/imports/startup/client/logger';
import Storage from '../../services/storage/session';
2017-03-28 22:02:23 +08:00
const ROLE_MODERATOR = Meteor.settings.public.user.role_moderator;
const TOGGLE_MUTE_THROTTLE_TIME = Meteor.settings.public.media.toggleMuteThrottleTime;
const SHOW_VOLUME_METER = Meteor.settings.public.media.showVolumeMeter;
const {
enabled: LOCAL_ECHO_TEST_ENABLED,
initialHearingState: LOCAL_ECHO_INIT_HEARING_STATE,
} = Meteor.settings.public.media.localEchoTest;
const MUTED_KEY = 'muted';
2024-01-29 20:49:40 +08:00
const recoverMicState = (toggleVoice) => {
const muted = Storage.getItem(MUTED_KEY);
if ((muted === undefined) || (muted === null)) {
return;
}
logger.debug({
logCode: 'audio_recover_mic_state',
}, `Audio recover previous mic state: muted = ${muted}`);
2024-01-29 20:49:40 +08:00
toggleVoice(null, muted);
};
2024-01-29 20:49:40 +08:00
const audioEventHandler = (toggleVoice) => (event) => {
if (!event) {
return;
}
switch (event.name) {
case 'started':
2024-01-29 20:49:40 +08:00
if (!event.isListenOnly) recoverMicState(toggleVoice);
break;
default:
break;
}
};
2024-01-29 20:49:40 +08:00
const init = (messages, intl, toggleVoice) => {
AudioManager.setAudioMessages(messages, intl);
if (AudioManager.initialized) return Promise.resolve(false);
const meetingId = Auth.meetingID;
2017-04-19 23:01:28 +08:00
const userId = Auth.userID;
2018-03-06 04:00:52 +08:00
const { sessionToken } = Auth;
const User = Users.findOne({ userId }, { fields: { name: 1 } });
2017-07-26 22:31:35 +08:00
const username = User.name;
const Meeting = Meetings.findOne({ meetingId: Auth.meetingID }, { fields: { 'voiceProp.voiceConf': 1 } });
2017-07-18 22:59:05 +08:00
const voiceBridge = Meeting.voiceProp.voiceConf;
2017-04-19 23:01:28 +08:00
// FIX ME
2017-07-18 22:59:05 +08:00
const microphoneLockEnforced = false;
2017-04-19 22:59:57 +08:00
2017-04-19 23:01:28 +08:00
const userData = {
meetingId,
2017-04-19 23:01:28 +08:00
userId,
2017-09-29 21:38:10 +08:00
sessionToken,
2017-04-19 23:01:28 +08:00
username,
voiceBridge,
microphoneLockEnforced,
2017-04-19 23:01:28 +08:00
};
2024-01-29 20:49:40 +08:00
return AudioManager.init(userData, audioEventHandler(toggleVoice));
2017-04-19 23:01:28 +08:00
};
2017-04-19 22:59:57 +08:00
2024-01-29 20:49:40 +08:00
const muteMicrophone = (toggleVoice) => {
const user = VoiceUsers.findOne({
meetingId: Auth.meetingID, intId: Auth.userID,
}, { fields: { muted: 1 } });
if (!user.muted) {
logger.info({
logCode: 'audiomanager_mute_audio',
extraInfo: { logType: 'user_action' },
}, 'User wants to leave conference. Microphone muted');
AudioManager.setSenderTrackEnabled(false);
2024-01-29 20:49:40 +08:00
toggleVoice();
}
};
const isVoiceUser = () => {
const voiceUser = VoiceUsers.findOne({ intId: Auth.userID },
{ fields: { joined: 1 } });
return voiceUser ? voiceUser.joined : false;
};
2024-01-29 20:49:40 +08:00
const toggleMuteMicrophone = throttle((toggleVoice) => {
2019-08-20 08:23:20 +08:00
const user = VoiceUsers.findOne({
2019-07-26 22:35:20 +08:00
meetingId: Auth.meetingID, intId: Auth.userID,
}, { fields: { muted: 1 } });
Storage.setItem(MUTED_KEY, !user.muted);
2019-08-20 08:23:20 +08:00
if (user.muted) {
2019-07-26 22:35:20 +08:00
logger.info({
logCode: 'audiomanager_unmute_audio',
extraInfo: { logType: 'user_action' },
}, 'microphone unmuted by user');
2024-01-29 20:49:40 +08:00
toggleVoice();
2019-07-26 22:35:20 +08:00
} else {
logger.info({
logCode: 'audiomanager_mute_audio',
extraInfo: { logType: 'user_action' },
}, 'microphone muted by user');
2024-01-29 20:49:40 +08:00
toggleVoice();
}
}, TOGGLE_MUTE_THROTTLE_TIME);
export default {
2017-04-19 22:59:57 +08:00
init,
exitAudio: () => AudioManager.exitAudio(),
forceExitAudio: () => AudioManager.forceExitAudio(),
transferCall: () => AudioManager.transferCall(),
joinListenOnly: () => AudioManager.joinListenOnly(),
joinMicrophone: () => AudioManager.joinMicrophone(),
joinEchoTest: () => AudioManager.joinEchoTest(),
2023-08-10 00:06:57 +08:00
toggleMuteMicrophone: debounce(toggleMuteMicrophone, 500, { leading: true, trailing: false }),
changeInputDevice: (inputDeviceId) => AudioManager.changeInputDevice(inputDeviceId),
changeInputStream: (newInputStream) => { AudioManager.inputStream = newInputStream; },
liveChangeInputDevice: (inputDeviceId) => AudioManager.liveChangeInputDevice(inputDeviceId),
changeOutputDevice: (outputDeviceId, isLive) => AudioManager.changeOutputDevice(outputDeviceId, isLive),
isConnectedToBreakout: () => {
const transferStatus = AudioManager.getBreakoutAudioTransferStatus();
if (transferStatus.status
=== AudioManager.BREAKOUT_AUDIO_TRANSFER_STATES.CONNECTED) return true;
return false;
},
isConnected: () => {
const transferStatus = AudioManager.getBreakoutAudioTransferStatus();
if (!!transferStatus.breakoutMeetingId
&& transferStatus.breakoutMeetingId !== Auth.meetingID) return false;
return AudioManager.isConnected;
},
isTalking: () => AudioManager.isTalking,
2017-10-27 01:14:56 +08:00
isHangingUp: () => AudioManager.isHangingUp,
isUsingAudio: () => AudioManager.isUsingAudio(),
2017-11-09 02:41:15 +08:00
isWaitingPermissions: () => AudioManager.isWaitingPermissions,
isMuted: () => AudioManager.isMuted,
isConnecting: () => AudioManager.isConnecting,
isListenOnly: () => AudioManager.isListenOnly,
inputDeviceId: () => AudioManager.inputDeviceId,
outputDeviceId: () => AudioManager.outputDeviceId,
2017-09-29 21:38:10 +08:00
isEchoTest: () => AudioManager.isEchoTest,
error: () => AudioManager.error,
isUserModerator: () => Users.findOne({ userId: Auth.userID },
{ fields: { role: 1 } })?.role === ROLE_MODERATOR,
isVoiceUser,
autoplayBlocked: () => AudioManager.autoplayBlocked,
handleAllowAutoplay: () => AudioManager.handleAllowAutoplay(),
playAlertSound: (url) => AudioManager.playAlertSound(url),
updateAudioConstraints:
(constraints) => AudioManager.updateAudioConstraints(constraints),
recoverMicState,
2024-01-29 20:49:40 +08:00
muteMicrophone: (toggleVoice) => muteMicrophone(toggleVoice),
isReconnecting: () => AudioManager.isReconnecting,
setBreakoutAudioTransferStatus: (status) => AudioManager
.setBreakoutAudioTransferStatus(status),
getBreakoutAudioTransferStatus: () => AudioManager
.getBreakoutAudioTransferStatus(),
getStats: () => AudioManager.getStats(),
localEchoEnabled: LOCAL_ECHO_TEST_ENABLED,
localEchoInitHearingState: LOCAL_ECHO_INIT_HEARING_STATE,
showVolumeMeter: SHOW_VOLUME_METER,
notify: (message, error, icon) => { AudioManager.notify(message, error, icon); },
2017-09-29 21:42:08 +08:00
};