You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* global APP */
  2. import UIEvents from '../../../../service/UI/UIEvents';
  3. import { processExternalDeviceRequest } from '../../device-selection';
  4. import { showNotification, showWarningNotification } from '../../notifications';
  5. import { replaceAudioTrackById, replaceVideoTrackById, setDeviceStatusWarning } from '../../prejoin/actions';
  6. import { isPrejoinPageVisible } from '../../prejoin/functions';
  7. import { JitsiTrackErrors } from '../lib-jitsi-meet';
  8. import { MiddlewareRegistry } from '../redux';
  9. import { updateSettings } from '../settings';
  10. import {
  11. CHECK_AND_NOTIFY_FOR_NEW_DEVICE,
  12. NOTIFY_CAMERA_ERROR,
  13. NOTIFY_MIC_ERROR,
  14. SET_AUDIO_INPUT_DEVICE,
  15. SET_VIDEO_INPUT_DEVICE,
  16. UPDATE_DEVICE_LIST
  17. } from './actionTypes';
  18. import {
  19. removePendingDeviceRequests,
  20. setAudioInputDevice,
  21. setVideoInputDevice
  22. } from './actions';
  23. import {
  24. areDeviceLabelsInitialized,
  25. formatDeviceLabel,
  26. groupDevicesByKind,
  27. setAudioOutputDeviceId
  28. } from './functions';
  29. import logger from './logger';
  30. const JITSI_TRACK_ERROR_TO_MESSAGE_KEY_MAP = {
  31. microphone: {
  32. [JitsiTrackErrors.CONSTRAINT_FAILED]: 'dialog.micConstraintFailedError',
  33. [JitsiTrackErrors.GENERAL]: 'dialog.micUnknownError',
  34. [JitsiTrackErrors.NOT_FOUND]: 'dialog.micNotFoundError',
  35. [JitsiTrackErrors.PERMISSION_DENIED]: 'dialog.micPermissionDeniedError'
  36. },
  37. camera: {
  38. [JitsiTrackErrors.CONSTRAINT_FAILED]: 'dialog.cameraConstraintFailedError',
  39. [JitsiTrackErrors.GENERAL]: 'dialog.cameraUnknownError',
  40. [JitsiTrackErrors.NOT_FOUND]: 'dialog.cameraNotFoundError',
  41. [JitsiTrackErrors.PERMISSION_DENIED]: 'dialog.cameraPermissionDeniedError',
  42. [JitsiTrackErrors.UNSUPPORTED_RESOLUTION]: 'dialog.cameraUnsupportedResolutionError'
  43. }
  44. };
  45. /**
  46. * Logs the current device list.
  47. *
  48. * @param {Object} deviceList - Whatever is returned by {@link groupDevicesByKind}.
  49. * @returns {string}
  50. */
  51. function logDeviceList(deviceList) {
  52. const devicesToStr = list => list.map(device => `\t\t${device.label}[${device.deviceId}]`).join('\n');
  53. const audioInputs = devicesToStr(deviceList.audioInput);
  54. const audioOutputs = devicesToStr(deviceList.audioOutput);
  55. const videoInputs = devicesToStr(deviceList.videoInput);
  56. logger.debug('Device list updated:\n'
  57. + `audioInput:\n${audioInputs}\n`
  58. + `audioOutput:\n${audioOutputs}\n`
  59. + `videoInput:\n${videoInputs}`);
  60. }
  61. /**
  62. * Implements the middleware of the feature base/devices.
  63. *
  64. * @param {Store} store - Redux store.
  65. * @returns {Function}
  66. */
  67. // eslint-disable-next-line no-unused-vars
  68. MiddlewareRegistry.register(store => next => action => {
  69. switch (action.type) {
  70. case NOTIFY_CAMERA_ERROR: {
  71. if (typeof APP !== 'object' || !action.error) {
  72. break;
  73. }
  74. const { message, name } = action.error;
  75. const cameraJitsiTrackErrorMsg
  76. = JITSI_TRACK_ERROR_TO_MESSAGE_KEY_MAP.camera[name];
  77. const cameraErrorMsg = cameraJitsiTrackErrorMsg
  78. || JITSI_TRACK_ERROR_TO_MESSAGE_KEY_MAP
  79. .camera[JitsiTrackErrors.GENERAL];
  80. const additionalCameraErrorMsg = cameraJitsiTrackErrorMsg ? null : message;
  81. const titleKey = name === JitsiTrackErrors.PERMISSION_DENIED
  82. ? 'deviceError.cameraPermission' : 'deviceError.cameraError';
  83. store.dispatch(showWarningNotification({
  84. description: additionalCameraErrorMsg,
  85. descriptionKey: cameraErrorMsg,
  86. titleKey
  87. }));
  88. if (isPrejoinPageVisible(store.getState())) {
  89. store.dispatch(setDeviceStatusWarning(titleKey));
  90. }
  91. break;
  92. }
  93. case NOTIFY_MIC_ERROR: {
  94. if (typeof APP !== 'object' || !action.error) {
  95. break;
  96. }
  97. const { message, name } = action.error;
  98. const micJitsiTrackErrorMsg
  99. = JITSI_TRACK_ERROR_TO_MESSAGE_KEY_MAP.microphone[name];
  100. const micErrorMsg = micJitsiTrackErrorMsg
  101. || JITSI_TRACK_ERROR_TO_MESSAGE_KEY_MAP
  102. .microphone[JitsiTrackErrors.GENERAL];
  103. const additionalMicErrorMsg = micJitsiTrackErrorMsg ? null : message;
  104. const titleKey = name === JitsiTrackErrors.PERMISSION_DENIED
  105. ? 'deviceError.microphonePermission'
  106. : 'deviceError.microphoneError';
  107. store.dispatch(showWarningNotification({
  108. description: additionalMicErrorMsg,
  109. descriptionKey: micErrorMsg,
  110. titleKey
  111. }));
  112. if (isPrejoinPageVisible(store.getState())) {
  113. store.dispatch(setDeviceStatusWarning(titleKey));
  114. }
  115. break;
  116. }
  117. case SET_AUDIO_INPUT_DEVICE:
  118. if (isPrejoinPageVisible(store.getState())) {
  119. store.dispatch(replaceAudioTrackById(action.deviceId));
  120. } else {
  121. APP.UI.emitEvent(UIEvents.AUDIO_DEVICE_CHANGED, action.deviceId);
  122. }
  123. break;
  124. case SET_VIDEO_INPUT_DEVICE:
  125. if (isPrejoinPageVisible(store.getState())) {
  126. store.dispatch(replaceVideoTrackById(action.deviceId));
  127. } else {
  128. APP.UI.emitEvent(UIEvents.VIDEO_DEVICE_CHANGED, action.deviceId);
  129. }
  130. break;
  131. case UPDATE_DEVICE_LIST:
  132. logDeviceList(groupDevicesByKind(action.devices));
  133. if (areDeviceLabelsInitialized(store.getState())) {
  134. return _processPendingRequests(store, next, action);
  135. }
  136. break;
  137. case CHECK_AND_NOTIFY_FOR_NEW_DEVICE:
  138. _checkAndNotifyForNewDevice(store, action.newDevices, action.oldDevices);
  139. break;
  140. }
  141. return next(action);
  142. });
  143. /**
  144. * Does extra sync up on properties that may need to be updated after the
  145. * conference was joined.
  146. *
  147. * @param {Store} store - The redux store in which the specified {@code action}
  148. * is being dispatched.
  149. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  150. * specified {@code action} to the specified {@code store}.
  151. * @param {Action} action - The redux action {@code CONFERENCE_JOINED} which is
  152. * being dispatched in the specified {@code store}.
  153. * @private
  154. * @returns {Object} The value returned by {@code next(action)}.
  155. */
  156. function _processPendingRequests({ dispatch, getState }, next, action) {
  157. const result = next(action);
  158. const state = getState();
  159. const { pendingRequests } = state['features/base/devices'];
  160. if (!pendingRequests || pendingRequests.length === 0) {
  161. return result;
  162. }
  163. pendingRequests.forEach(request => {
  164. processExternalDeviceRequest(
  165. dispatch,
  166. getState,
  167. request,
  168. request.responseCallback);
  169. });
  170. dispatch(removePendingDeviceRequests());
  171. return result;
  172. }
  173. /**
  174. * Finds a new device by comparing new and old array of devices and dispatches
  175. * notification with the new device. For new devices with same groupId only one
  176. * notification will be shown, this is so to avoid showing multiple notifications
  177. * for audio input and audio output devices.
  178. *
  179. * @param {Store} store - The redux store in which the specified {@code action}
  180. * is being dispatched.
  181. * @param {MediaDeviceInfo[]} newDevices - The array of new devices we received.
  182. * @param {MediaDeviceInfo[]} oldDevices - The array of the old devices we have.
  183. * @private
  184. * @returns {void}
  185. */
  186. function _checkAndNotifyForNewDevice(store, newDevices, oldDevices) {
  187. const { dispatch } = store;
  188. // let's intersect both newDevices and oldDevices and handle thew newly
  189. // added devices
  190. const onlyNewDevices = newDevices.filter(
  191. nDevice => !oldDevices.find(
  192. device => device.deviceId === nDevice.deviceId));
  193. // we group devices by groupID which normally is the grouping by physical device
  194. // plugging in headset we provide normally two device, one input and one output
  195. // and we want to show only one notification for this physical audio device
  196. const devicesGroupBy = onlyNewDevices.reduce((accumulated, value) => {
  197. accumulated[value.groupId] = accumulated[value.groupId] || [];
  198. accumulated[value.groupId].push(value);
  199. return accumulated;
  200. }, {});
  201. Object.values(devicesGroupBy).forEach(devicesArray => {
  202. if (devicesArray.length < 1) {
  203. return;
  204. }
  205. // let's get the first device as a reference, we will use it for
  206. // label and type
  207. const newDevice = devicesArray[0];
  208. // we want to strip any device details that are not very
  209. // user friendly, like usb ids put in brackets at the end
  210. const description = formatDeviceLabel(newDevice.label);
  211. let titleKey;
  212. switch (newDevice.kind) {
  213. case 'videoinput': {
  214. titleKey = 'notify.newDeviceCameraTitle';
  215. break;
  216. }
  217. case 'audioinput' :
  218. case 'audiooutput': {
  219. titleKey = 'notify.newDeviceAudioTitle';
  220. break;
  221. }
  222. }
  223. dispatch(showNotification({
  224. description,
  225. titleKey,
  226. customActionNameKey: 'notify.newDeviceAction',
  227. customActionHandler: _useDevice.bind(undefined, store, devicesArray)
  228. }));
  229. });
  230. }
  231. /**
  232. * Set a device to be currently used, selected by the user.
  233. *
  234. * @param {Store} store - The redux store in which the specified {@code action}
  235. * is being dispatched.
  236. * @param {Array<MediaDeviceInfo|InputDeviceInfo>} devices - The devices to save.
  237. * @returns {boolean} - Returns true in order notifications to be dismissed.
  238. * @private
  239. */
  240. function _useDevice({ dispatch }, devices) {
  241. devices.forEach(device => {
  242. switch (device.kind) {
  243. case 'videoinput': {
  244. dispatch(updateSettings({
  245. userSelectedCameraDeviceId: device.deviceId,
  246. userSelectedCameraDeviceLabel: device.label
  247. }));
  248. dispatch(setVideoInputDevice(device.deviceId));
  249. break;
  250. }
  251. case 'audioinput': {
  252. dispatch(updateSettings({
  253. userSelectedMicDeviceId: device.deviceId,
  254. userSelectedMicDeviceLabel: device.label
  255. }));
  256. dispatch(setAudioInputDevice(device.deviceId));
  257. break;
  258. }
  259. case 'audiooutput': {
  260. setAudioOutputDeviceId(
  261. device.deviceId,
  262. dispatch,
  263. true,
  264. device.label)
  265. .then(() => logger.log('changed audio output device'))
  266. .catch(err => {
  267. logger.warn(
  268. 'Failed to change audio output device.',
  269. 'Default or previously set audio output device will',
  270. ' be used instead.',
  271. err);
  272. });
  273. break;
  274. }
  275. }
  276. });
  277. return true;
  278. }