Calls: Show notification when missing access to camera or mic (#2157)

This commit is contained in:
Alexander Zinchuk 2022-11-28 15:27:17 +01:00
parent 59a0d28b65
commit 74d88e80d5
2 changed files with 49 additions and 2 deletions

View File

@ -6,7 +6,7 @@ import { updateChat } from '../../reducers';
import { ARE_CALLS_SUPPORTED } from '../../../util/environment';
import { notifyAboutCall } from '../../../util/notifications';
import { selectPhoneCallUser } from '../../selectors/calls';
import { initializeSoundsForSafari } from '../ui/calls';
import { checkNavigatorUserMediaPermissions, initializeSoundsForSafari } from '../ui/calls';
import { onTickEnd } from '../../../util/schedulers';
addActionHandler('apiUpdate', (global, actions, update) => {
@ -96,6 +96,7 @@ addActionHandler('apiUpdate', (global, actions, update) => {
});
void initializeSoundsForSafari();
void checkNavigatorUserMediaPermissions(call.isVideo);
return {
...global,
phoneCall: call,

View File

@ -1,4 +1,6 @@
import { addActionHandler, getGlobal, setGlobal } from '../../index';
import {
addActionHandler, getActions, getGlobal, setGlobal,
} from '../../index';
import { selectActiveGroupCall, selectChatGroupCall, selectGroupCall } from '../../selectors/calls';
import { callApi } from '../../../api/gramjs';
import { selectChat, selectUser } from '../../selectors';
@ -217,6 +219,8 @@ addActionHandler('joinGroupCall', async (global, actions, payload) => {
createAudioElement();
await initializeSoundsForSafari();
void checkNavigatorUserMediaPermissions(true);
const { groupCalls: { activeGroupCallId } } = global;
let groupCall = id ? selectGroupCall(global, id) : selectChatGroupCall(global, chatId);
@ -319,6 +323,7 @@ addActionHandler('requestCall', async (global, actions, payload) => {
}
await initializeSoundsForSafari();
void checkNavigatorUserMediaPermissions(isVideo);
setGlobal({
...getGlobal(),
@ -365,3 +370,44 @@ export function removeGroupCallAudioElement() {
audioContext = undefined;
audioElement = undefined;
}
// This method is used instead of a navigator.permissions.query to determine permission to use a microphone,
// because Firefox does not have support for 'microphone' and 'camera' permissions
// https://github.com/mozilla/standards-positions/issues/19#issuecomment-370158947
export function checkNavigatorUserMediaPermissions(isVideo?: boolean) {
if (isVideo) {
navigator.mediaDevices.getUserMedia({ video: true })
.then((stream) => {
if (stream.getVideoTracks().length === 0) {
getActions().showNotification({
message: langProvider.getTranslation('Call.Camera.Error'),
});
} else {
checkMicrophonePermission();
}
})
.catch(() => {
getActions().showNotification({
message: langProvider.getTranslation('Call.Camera.Error'),
});
});
} else {
checkMicrophonePermission();
}
}
function checkMicrophonePermission() {
navigator.mediaDevices.getUserMedia({ audio: true })
.then((stream) => {
if (stream.getAudioTracks().length === 0) {
getActions().showNotification({
message: langProvider.getTranslation('RequestAcces.Error.HaveNotAccess.Call'),
});
}
})
.catch(() => {
getActions().showNotification({
message: langProvider.getTranslation('RequestAcces.Error.HaveNotAccess.Call'),
});
});
}