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

middleware.js 9.7KB

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