2022-09-23 08:13:32 +00:00
|
|
|
import { AnyAction } from 'redux';
|
2017-03-29 17:43:30 +00:00
|
|
|
|
2022-09-23 08:13:32 +00:00
|
|
|
// @ts-ignore
|
2019-03-27 17:30:56 +00:00
|
|
|
import UIEvents from '../../../../service/UI/UIEvents';
|
2022-09-23 08:13:32 +00:00
|
|
|
import { IStore } from '../../app/types';
|
|
|
|
import { processExternalDeviceRequest } from '../../device-selection/functions';
|
|
|
|
import { showNotification, showWarningNotification } from '../../notifications/actions';
|
|
|
|
import { NOTIFICATION_TIMEOUT_TYPE } from '../../notifications/constants';
|
2020-05-20 10:57:03 +00:00
|
|
|
import { replaceAudioTrackById, replaceVideoTrackById, setDeviceStatusWarning } from '../../prejoin/actions';
|
|
|
|
import { isPrejoinPageVisible } from '../../prejoin/functions';
|
2022-09-23 08:13:32 +00:00
|
|
|
import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app/actionTypes';
|
2021-02-02 00:20:39 +00:00
|
|
|
import JitsiMeetJS, { JitsiMediaDevicesEvents, JitsiTrackErrors } from '../lib-jitsi-meet';
|
2022-09-23 08:13:32 +00:00
|
|
|
import MiddlewareRegistry from '../redux/MiddlewareRegistry';
|
|
|
|
import { updateSettings } from '../settings/actions';
|
|
|
|
|
2017-03-29 17:43:30 +00:00
|
|
|
|
2019-05-03 17:25:33 +00:00
|
|
|
import {
|
|
|
|
CHECK_AND_NOTIFY_FOR_NEW_DEVICE,
|
2019-05-29 21:17:07 +00:00
|
|
|
NOTIFY_CAMERA_ERROR,
|
|
|
|
NOTIFY_MIC_ERROR,
|
2017-03-29 17:43:30 +00:00
|
|
|
SET_AUDIO_INPUT_DEVICE,
|
2020-06-30 17:48:06 +00:00
|
|
|
SET_VIDEO_INPUT_DEVICE,
|
|
|
|
UPDATE_DEVICE_LIST
|
2017-03-29 17:43:30 +00:00
|
|
|
} from './actionTypes';
|
2020-05-20 10:57:03 +00:00
|
|
|
import {
|
2021-02-02 00:20:39 +00:00
|
|
|
devicePermissionsChanged,
|
2020-05-20 10:57:03 +00:00
|
|
|
removePendingDeviceRequests,
|
|
|
|
setAudioInputDevice,
|
|
|
|
setVideoInputDevice
|
|
|
|
} from './actions';
|
2020-06-30 17:48:06 +00:00
|
|
|
import {
|
2020-08-25 22:31:38 +00:00
|
|
|
areDeviceLabelsInitialized,
|
2020-06-30 17:48:06 +00:00
|
|
|
formatDeviceLabel,
|
|
|
|
groupDevicesByKind,
|
|
|
|
setAudioOutputDeviceId
|
|
|
|
} from './functions';
|
2019-08-21 14:50:00 +00:00
|
|
|
import logger from './logger';
|
2022-09-23 08:13:32 +00:00
|
|
|
import { IDevicesState } from './reducer';
|
2017-03-29 17:43:30 +00:00
|
|
|
|
2019-05-29 21:17:07 +00:00
|
|
|
const JITSI_TRACK_ERROR_TO_MESSAGE_KEY_MAP = {
|
|
|
|
microphone: {
|
|
|
|
[JitsiTrackErrors.CONSTRAINT_FAILED]: 'dialog.micConstraintFailedError',
|
|
|
|
[JitsiTrackErrors.GENERAL]: 'dialog.micUnknownError',
|
|
|
|
[JitsiTrackErrors.NOT_FOUND]: 'dialog.micNotFoundError',
|
2021-02-02 00:20:39 +00:00
|
|
|
[JitsiTrackErrors.PERMISSION_DENIED]: 'dialog.micPermissionDeniedError',
|
|
|
|
[JitsiTrackErrors.TIMEOUT]: 'dialog.micTimeoutError'
|
2019-05-29 21:17:07 +00:00
|
|
|
},
|
|
|
|
camera: {
|
|
|
|
[JitsiTrackErrors.CONSTRAINT_FAILED]: 'dialog.cameraConstraintFailedError',
|
|
|
|
[JitsiTrackErrors.GENERAL]: 'dialog.cameraUnknownError',
|
|
|
|
[JitsiTrackErrors.NOT_FOUND]: 'dialog.cameraNotFoundError',
|
|
|
|
[JitsiTrackErrors.PERMISSION_DENIED]: 'dialog.cameraPermissionDeniedError',
|
2021-02-02 00:20:39 +00:00
|
|
|
[JitsiTrackErrors.UNSUPPORTED_RESOLUTION]: 'dialog.cameraUnsupportedResolutionError',
|
|
|
|
[JitsiTrackErrors.TIMEOUT]: 'dialog.cameraTimeoutError'
|
2019-05-29 21:17:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-02 00:20:39 +00:00
|
|
|
/**
|
|
|
|
* A listener for device permissions changed reported from lib-jitsi-meet.
|
|
|
|
*/
|
2022-09-23 08:13:32 +00:00
|
|
|
let permissionsListener: Function | undefined;
|
2021-02-02 00:20:39 +00:00
|
|
|
|
2020-06-30 17:48:06 +00:00
|
|
|
/**
|
|
|
|
* Logs the current device list.
|
|
|
|
*
|
|
|
|
* @param {Object} deviceList - Whatever is returned by {@link groupDevicesByKind}.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-09-23 08:13:32 +00:00
|
|
|
function logDeviceList(deviceList: IDevicesState['availableDevices']) {
|
|
|
|
const devicesToStr = (list?: MediaDeviceInfo[]) =>
|
|
|
|
list?.map(device => `\t\t${device.label}[${device.deviceId}]`).join('\n');
|
2020-06-30 17:48:06 +00:00
|
|
|
const audioInputs = devicesToStr(deviceList.audioInput);
|
|
|
|
const audioOutputs = devicesToStr(deviceList.audioOutput);
|
|
|
|
const videoInputs = devicesToStr(deviceList.videoInput);
|
|
|
|
|
|
|
|
logger.debug('Device list updated:\n'
|
|
|
|
+ `audioInput:\n${audioInputs}\n`
|
|
|
|
+ `audioOutput:\n${audioOutputs}\n`
|
|
|
|
+ `videoInput:\n${videoInputs}`);
|
|
|
|
}
|
|
|
|
|
2017-03-29 17:43:30 +00:00
|
|
|
/**
|
|
|
|
* Implements the middleware of the feature base/devices.
|
|
|
|
*
|
|
|
|
* @param {Store} store - Redux store.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
|
|
|
switch (action.type) {
|
2021-02-02 00:20:39 +00:00
|
|
|
case APP_WILL_MOUNT: {
|
2022-09-23 08:13:32 +00:00
|
|
|
const _permissionsListener = (permissions: Object) => {
|
2021-02-02 00:20:39 +00:00
|
|
|
store.dispatch(devicePermissionsChanged(permissions));
|
|
|
|
};
|
|
|
|
const { mediaDevices } = JitsiMeetJS;
|
|
|
|
|
|
|
|
permissionsListener = _permissionsListener;
|
|
|
|
mediaDevices.addEventListener(JitsiMediaDevicesEvents.PERMISSIONS_CHANGED, permissionsListener);
|
|
|
|
Promise.all([
|
|
|
|
mediaDevices.isDevicePermissionGranted('audio'),
|
|
|
|
mediaDevices.isDevicePermissionGranted('video')
|
|
|
|
])
|
|
|
|
.then(results => {
|
|
|
|
_permissionsListener({
|
|
|
|
audio: results[0],
|
|
|
|
video: results[1]
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
// Ignore errors.
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case APP_WILL_UNMOUNT:
|
|
|
|
if (typeof permissionsListener === 'function') {
|
|
|
|
JitsiMeetJS.mediaDevices.removeEventListener(
|
|
|
|
JitsiMediaDevicesEvents.PERMISSIONS_CHANGED, permissionsListener);
|
|
|
|
permissionsListener = undefined;
|
|
|
|
}
|
|
|
|
break;
|
2019-05-29 21:17:07 +00:00
|
|
|
case NOTIFY_CAMERA_ERROR: {
|
|
|
|
if (typeof APP !== 'object' || !action.error) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { message, name } = action.error;
|
|
|
|
|
|
|
|
const cameraJitsiTrackErrorMsg
|
|
|
|
= JITSI_TRACK_ERROR_TO_MESSAGE_KEY_MAP.camera[name];
|
|
|
|
const cameraErrorMsg = cameraJitsiTrackErrorMsg
|
|
|
|
|| JITSI_TRACK_ERROR_TO_MESSAGE_KEY_MAP
|
|
|
|
.camera[JitsiTrackErrors.GENERAL];
|
|
|
|
const additionalCameraErrorMsg = cameraJitsiTrackErrorMsg ? null : message;
|
2020-05-07 07:42:55 +00:00
|
|
|
const titleKey = name === JitsiTrackErrors.PERMISSION_DENIED
|
|
|
|
? 'deviceError.cameraPermission' : 'deviceError.cameraError';
|
2019-05-29 21:17:07 +00:00
|
|
|
|
|
|
|
store.dispatch(showWarningNotification({
|
|
|
|
description: additionalCameraErrorMsg,
|
|
|
|
descriptionKey: cameraErrorMsg,
|
2020-05-07 07:42:55 +00:00
|
|
|
titleKey
|
2021-11-24 11:05:27 +00:00
|
|
|
}, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
|
2019-05-29 21:17:07 +00:00
|
|
|
|
2020-05-07 07:42:55 +00:00
|
|
|
if (isPrejoinPageVisible(store.getState())) {
|
|
|
|
store.dispatch(setDeviceStatusWarning(titleKey));
|
|
|
|
}
|
|
|
|
|
2019-05-29 21:17:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NOTIFY_MIC_ERROR: {
|
|
|
|
if (typeof APP !== 'object' || !action.error) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { message, name } = action.error;
|
|
|
|
|
|
|
|
const micJitsiTrackErrorMsg
|
|
|
|
= JITSI_TRACK_ERROR_TO_MESSAGE_KEY_MAP.microphone[name];
|
|
|
|
const micErrorMsg = micJitsiTrackErrorMsg
|
|
|
|
|| JITSI_TRACK_ERROR_TO_MESSAGE_KEY_MAP
|
|
|
|
.microphone[JitsiTrackErrors.GENERAL];
|
|
|
|
const additionalMicErrorMsg = micJitsiTrackErrorMsg ? null : message;
|
2020-05-07 07:42:55 +00:00
|
|
|
const titleKey = name === JitsiTrackErrors.PERMISSION_DENIED
|
|
|
|
? 'deviceError.microphonePermission'
|
|
|
|
: 'deviceError.microphoneError';
|
2019-05-29 21:17:07 +00:00
|
|
|
|
|
|
|
store.dispatch(showWarningNotification({
|
|
|
|
description: additionalMicErrorMsg,
|
|
|
|
descriptionKey: micErrorMsg,
|
2020-05-07 07:42:55 +00:00
|
|
|
titleKey
|
2021-11-24 11:05:27 +00:00
|
|
|
}, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
|
2019-05-29 21:17:07 +00:00
|
|
|
|
2020-05-07 07:42:55 +00:00
|
|
|
if (isPrejoinPageVisible(store.getState())) {
|
|
|
|
store.dispatch(setDeviceStatusWarning(titleKey));
|
|
|
|
}
|
|
|
|
|
2019-05-29 21:17:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-03-29 17:43:30 +00:00
|
|
|
case SET_AUDIO_INPUT_DEVICE:
|
2020-04-16 10:47:10 +00:00
|
|
|
if (isPrejoinPageVisible(store.getState())) {
|
|
|
|
store.dispatch(replaceAudioTrackById(action.deviceId));
|
|
|
|
} else {
|
|
|
|
APP.UI.emitEvent(UIEvents.AUDIO_DEVICE_CHANGED, action.deviceId);
|
|
|
|
}
|
2017-03-29 17:43:30 +00:00
|
|
|
break;
|
|
|
|
case SET_VIDEO_INPUT_DEVICE:
|
2020-04-16 10:47:10 +00:00
|
|
|
if (isPrejoinPageVisible(store.getState())) {
|
|
|
|
store.dispatch(replaceVideoTrackById(action.deviceId));
|
|
|
|
} else {
|
|
|
|
APP.UI.emitEvent(UIEvents.VIDEO_DEVICE_CHANGED, action.deviceId);
|
|
|
|
}
|
2017-03-29 17:43:30 +00:00
|
|
|
break;
|
2020-06-30 17:48:06 +00:00
|
|
|
case UPDATE_DEVICE_LIST:
|
|
|
|
logDeviceList(groupDevicesByKind(action.devices));
|
2020-08-25 22:31:38 +00:00
|
|
|
if (areDeviceLabelsInitialized(store.getState())) {
|
|
|
|
return _processPendingRequests(store, next, action);
|
|
|
|
}
|
2020-06-30 17:48:06 +00:00
|
|
|
break;
|
2019-05-03 17:25:33 +00:00
|
|
|
case CHECK_AND_NOTIFY_FOR_NEW_DEVICE:
|
|
|
|
_checkAndNotifyForNewDevice(store, action.newDevices, action.oldDevices);
|
|
|
|
break;
|
2017-03-29 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
});
|
2019-03-27 17:30:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Does extra sync up on properties that may need to be updated after the
|
|
|
|
* conference was joined.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store in which the specified {@code action}
|
|
|
|
* is being dispatched.
|
|
|
|
* @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
|
|
|
|
* specified {@code action} to the specified {@code store}.
|
|
|
|
* @param {Action} action - The redux action {@code CONFERENCE_JOINED} which is
|
|
|
|
* being dispatched in the specified {@code store}.
|
|
|
|
* @private
|
|
|
|
* @returns {Object} The value returned by {@code next(action)}.
|
|
|
|
*/
|
2022-09-23 08:13:32 +00:00
|
|
|
function _processPendingRequests({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
|
2019-03-27 17:30:56 +00:00
|
|
|
const result = next(action);
|
|
|
|
const state = getState();
|
|
|
|
const { pendingRequests } = state['features/base/devices'];
|
|
|
|
|
2020-08-25 22:31:38 +00:00
|
|
|
if (!pendingRequests || pendingRequests.length === 0) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-09-23 08:13:32 +00:00
|
|
|
pendingRequests.forEach((request: any) => {
|
2019-03-28 16:29:30 +00:00
|
|
|
processExternalDeviceRequest(
|
2019-03-27 17:30:56 +00:00
|
|
|
dispatch,
|
|
|
|
getState,
|
|
|
|
request,
|
|
|
|
request.responseCallback);
|
|
|
|
});
|
|
|
|
dispatch(removePendingDeviceRequests());
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2019-05-03 17:25:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds a new device by comparing new and old array of devices and dispatches
|
2019-05-20 20:35:42 +00:00
|
|
|
* notification with the new device. For new devices with same groupId only one
|
|
|
|
* notification will be shown, this is so to avoid showing multiple notifications
|
|
|
|
* for audio input and audio output devices.
|
2019-05-03 17:25:33 +00:00
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store in which the specified {@code action}
|
|
|
|
* is being dispatched.
|
|
|
|
* @param {MediaDeviceInfo[]} newDevices - The array of new devices we received.
|
|
|
|
* @param {MediaDeviceInfo[]} oldDevices - The array of the old devices we have.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-23 08:13:32 +00:00
|
|
|
function _checkAndNotifyForNewDevice(store: IStore, newDevices: MediaDeviceInfo[], oldDevices: MediaDeviceInfo[]) {
|
2019-05-03 17:25:33 +00:00
|
|
|
const { dispatch } = store;
|
|
|
|
|
|
|
|
// let's intersect both newDevices and oldDevices and handle thew newly
|
|
|
|
// added devices
|
|
|
|
const onlyNewDevices = newDevices.filter(
|
|
|
|
nDevice => !oldDevices.find(
|
|
|
|
device => device.deviceId === nDevice.deviceId));
|
|
|
|
|
2019-05-20 20:35:42 +00:00
|
|
|
// we group devices by groupID which normally is the grouping by physical device
|
|
|
|
// plugging in headset we provide normally two device, one input and one output
|
|
|
|
// and we want to show only one notification for this physical audio device
|
2022-09-23 08:13:32 +00:00
|
|
|
const devicesGroupBy: {
|
|
|
|
[key: string]: MediaDeviceInfo[];
|
|
|
|
} = onlyNewDevices.reduce((accumulated: any, value) => {
|
2019-05-20 20:35:42 +00:00
|
|
|
accumulated[value.groupId] = accumulated[value.groupId] || [];
|
|
|
|
accumulated[value.groupId].push(value);
|
|
|
|
|
|
|
|
return accumulated;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
Object.values(devicesGroupBy).forEach(devicesArray => {
|
|
|
|
|
|
|
|
if (devicesArray.length < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// let's get the first device as a reference, we will use it for
|
|
|
|
// label and type
|
|
|
|
const newDevice = devicesArray[0];
|
2019-05-03 17:25:33 +00:00
|
|
|
|
|
|
|
// we want to strip any device details that are not very
|
|
|
|
// user friendly, like usb ids put in brackets at the end
|
2019-10-03 08:15:49 +00:00
|
|
|
const description = formatDeviceLabel(newDevice.label);
|
2019-05-03 17:25:33 +00:00
|
|
|
|
|
|
|
let titleKey;
|
|
|
|
|
|
|
|
switch (newDevice.kind) {
|
|
|
|
case 'videoinput': {
|
|
|
|
titleKey = 'notify.newDeviceCameraTitle';
|
|
|
|
break;
|
|
|
|
}
|
2019-05-20 20:35:42 +00:00
|
|
|
case 'audioinput' :
|
2019-05-03 17:25:33 +00:00
|
|
|
case 'audiooutput': {
|
2019-05-20 20:35:42 +00:00
|
|
|
titleKey = 'notify.newDeviceAudioTitle';
|
2019-05-03 17:25:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-05 13:54:29 +00:00
|
|
|
if (!isPrejoinPageVisible(store.getState())) {
|
|
|
|
dispatch(showNotification({
|
|
|
|
description,
|
|
|
|
titleKey,
|
2021-11-10 11:19:40 +00:00
|
|
|
customActionNameKey: [ 'notify.newDeviceAction' ],
|
|
|
|
customActionHandler: [ _useDevice.bind(undefined, store, devicesArray) ]
|
2021-11-24 11:05:27 +00:00
|
|
|
}, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
|
2021-11-05 13:54:29 +00:00
|
|
|
}
|
2019-05-03 17:25:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a device to be currently used, selected by the user.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store in which the specified {@code action}
|
|
|
|
* is being dispatched.
|
2019-05-20 20:35:42 +00:00
|
|
|
* @param {Array<MediaDeviceInfo|InputDeviceInfo>} devices - The devices to save.
|
2019-05-03 17:25:33 +00:00
|
|
|
* @returns {boolean} - Returns true in order notifications to be dismissed.
|
|
|
|
* @private
|
|
|
|
*/
|
2022-09-23 08:13:32 +00:00
|
|
|
function _useDevice({ dispatch }: IStore, devices: MediaDeviceInfo[]) {
|
2019-05-20 20:35:42 +00:00
|
|
|
devices.forEach(device => {
|
|
|
|
switch (device.kind) {
|
|
|
|
case 'videoinput': {
|
|
|
|
dispatch(updateSettings({
|
|
|
|
userSelectedCameraDeviceId: device.deviceId,
|
|
|
|
userSelectedCameraDeviceLabel: device.label
|
|
|
|
}));
|
2019-05-03 17:25:33 +00:00
|
|
|
|
2019-05-20 20:35:42 +00:00
|
|
|
dispatch(setVideoInputDevice(device.deviceId));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'audioinput': {
|
|
|
|
dispatch(updateSettings({
|
|
|
|
userSelectedMicDeviceId: device.deviceId,
|
|
|
|
userSelectedMicDeviceLabel: device.label
|
|
|
|
}));
|
2019-05-03 17:25:33 +00:00
|
|
|
|
2019-05-20 20:35:42 +00:00
|
|
|
dispatch(setAudioInputDevice(device.deviceId));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'audiooutput': {
|
|
|
|
setAudioOutputDeviceId(
|
|
|
|
device.deviceId,
|
|
|
|
dispatch,
|
|
|
|
true,
|
|
|
|
device.label)
|
|
|
|
.then(() => logger.log('changed audio output device'))
|
|
|
|
.catch(err => {
|
|
|
|
logger.warn(
|
|
|
|
'Failed to change audio output device.',
|
|
|
|
'Default or previously set audio output device will',
|
|
|
|
' be used instead.',
|
|
|
|
err);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-05-03 17:25:33 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|