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

middleware.js 9.4KB

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