mediasfu-reactnative
Version:
MediaSFU Prebuilt React Native SDK
179 lines • 9.07 kB
JavaScript
/**
* Handles the click event for toggling audio in a media session.
*
* @param {ClickAudioOptions} parameters - The parameters required for handling the audio click event.
* @returns {Promise<void>} A promise that resolves when the audio click event has been handled.
*
* The function performs the following actions:
* - If the event is audio-only, it shows an alert and exits.
* - If the audio is already on, it handles the logic for turning it off, including checking recording states and permissions.
* - If the audio is off, it checks for admin restrictions, user permissions, and handles the logic for turning the audio on.
* - It updates various states and emits socket events as necessary.
*
* @example
* ```typescript
* clickAudio({ parameters });
* ```
*/
export const clickAudio = async ({ parameters }) => {
let { checkMediaPermission, hasAudioPermission, audioPaused, audioAlreadyOn, audioOnlyRoom, recordStarted, recordResumed, recordPaused, recordStopped, recordingMediaOptions, islevel, youAreCoHost, adminRestrictSetting, audioRequestState, audioRequestTime, member, socket, localSocket, roomName, userDefaultAudioInputDevice, micAction, localStream, audioSetting, videoSetting, screenshareSetting, chatSetting, updateRequestIntervalSeconds, participants, mediaDevices, showAlert, transportCreated, transportCreatedAudio, updateAudioAlreadyOn, updateAudioRequestState, updateAudioPaused, updateLocalStream, updateParticipants, updateTransportCreated, updateTransportCreatedAudio, updateMicAction, checkPermission, streamSuccessAudio, requestPermissionAudio, resumeSendTransportAudio, disconnectSendTransportAudio, } = parameters;
if (audioOnlyRoom) {
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: 'You cannot turn on your camera in an audio-only event.',
type: 'danger',
duration: 3000,
});
return;
}
if (audioAlreadyOn) {
if (islevel === '2' && (recordStarted || recordResumed)) {
if (!(recordPaused || recordStopped)) {
if (recordingMediaOptions === 'audio') {
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: 'You cannot turn off your audio while recording, please pause or stop recording first.',
type: 'danger',
duration: 3000,
});
return;
}
}
}
audioAlreadyOn = false;
updateAudioAlreadyOn(audioAlreadyOn);
if (localStream) {
localStream.getAudioTracks()[0].enabled = false;
updateLocalStream(localStream);
}
await disconnectSendTransportAudio({ parameters }); // disconnect function here actuall calls audioProducer.pause() instead of close() as in mediasfu
audioPaused = true;
updateAudioPaused(audioPaused);
}
else {
if (adminRestrictSetting) {
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: 'You cannot turn on your microphone. Access denied by host.',
type: 'danger',
duration: 3000,
});
return;
}
let response = 2;
if (!micAction && islevel !== '2' && !youAreCoHost) {
response = await checkPermission({
permissionType: 'audioSetting',
audioSetting, videoSetting, screenshareSetting, chatSetting,
});
}
else {
response = 0;
}
switch (response) {
case 1:
{
if (audioRequestState === 'pending') {
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: 'A request is pending. Please wait for the host to respond.',
type: 'danger',
duration: 3000,
});
return;
}
if (audioRequestState === 'rejected' && Date.now() - audioRequestTime < updateRequestIntervalSeconds * 1000) {
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: `A request was rejected. Please wait for ${updateRequestIntervalSeconds} seconds before sending another request.`,
type: 'danger',
duration: 3000,
});
return;
}
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: 'Request sent to host.',
type: 'success',
duration: 3000,
});
audioRequestState = 'pending';
updateAudioRequestState(audioRequestState);
//create a request and add to the request list and send to host
const userRequest = {
id: socket.id,
name: member,
icon: 'fa-microphone',
};
socket.emit('participantRequest', { userRequest, roomName });
break;
}
case 2:
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: 'You cannot turn on your microphone. Access denied by host.',
type: 'danger',
duration: 3000,
});
break;
case 0:
//allow
if (audioPaused) {
if (localStream && localStream.getAudioTracks().length > 0) {
localStream.getAudioTracks()[0].enabled = true;
updateLocalStream(localStream);
}
updateAudioAlreadyOn(true);
await resumeSendTransportAudio({ parameters });
socket.emit('resumeProducerAudio', { mediaTag: 'audio', roomName });
try {
if (localSocket && localSocket.id) {
localSocket.emit('resumeProducerAudio', { mediaTag: 'audio', roomName });
}
}
catch (error) {
console.log('Error in resumeProducerAudio', error);
}
updateLocalStream(localStream);
if (micAction === true) {
micAction = false;
updateMicAction(micAction);
}
participants.forEach((participant) => {
if (participant.socketId === socket.id && participant.name === member) {
participant.muted = false;
}
});
updateParticipants(participants);
transportCreated = true;
updateTransportCreated(transportCreated);
transportCreatedAudio = true;
updateTransportCreatedAudio(transportCreatedAudio);
}
else {
if (!hasAudioPermission && checkMediaPermission) {
const statusMic = await requestPermissionAudio();
if (statusMic !== 'granted') {
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: 'Allow access to your microphone or check if your microphone is not being used by another application.',
type: 'danger',
duration: 3000,
});
return;
}
}
const mediaConstraints = userDefaultAudioInputDevice
? { audio: { deviceId: userDefaultAudioInputDevice }, video: false }
: { audio: true, video: false };
try {
const stream = await mediaDevices.getUserMedia(mediaConstraints);
await streamSuccessAudio({ stream, parameters });
}
catch (error) {
console.error(error);
showAlert === null || showAlert === void 0 ? void 0 : showAlert({
message: 'Allow access to your microphone or check if your microphone is not being used by another application.',
type: 'danger',
duration: 3000,
});
}
}
break;
default:
}
}
};
//# sourceMappingURL=clickAudio.js.map