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

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