您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 9.0KB

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