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 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 { setAudioOutputDeviceId } from './functions';
  22. const logger = require('jitsi-meet-logger').getLogger(__filename);
  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. let description = newDevice.label;
  165. const ix = description.lastIndexOf('(');
  166. if (ix !== -1) {
  167. description = description.substr(0, ix);
  168. }
  169. let titleKey;
  170. switch (newDevice.kind) {
  171. case 'videoinput': {
  172. titleKey = 'notify.newDeviceCameraTitle';
  173. break;
  174. }
  175. case 'audioinput' :
  176. case 'audiooutput': {
  177. titleKey = 'notify.newDeviceAudioTitle';
  178. break;
  179. }
  180. }
  181. dispatch(showNotification({
  182. description,
  183. titleKey,
  184. customActionNameKey: 'notify.newDeviceAction',
  185. customActionHandler: _useDevice.bind(undefined, store, devicesArray)
  186. }));
  187. });
  188. }
  189. /**
  190. * Set a device to be currently used, selected by the user.
  191. *
  192. * @param {Store} store - The redux store in which the specified {@code action}
  193. * is being dispatched.
  194. * @param {Array<MediaDeviceInfo|InputDeviceInfo>} devices - The devices to save.
  195. * @returns {boolean} - Returns true in order notifications to be dismissed.
  196. * @private
  197. */
  198. function _useDevice({ dispatch }, devices) {
  199. devices.forEach(device => {
  200. switch (device.kind) {
  201. case 'videoinput': {
  202. dispatch(updateSettings({
  203. userSelectedCameraDeviceId: device.deviceId,
  204. userSelectedCameraDeviceLabel: device.label
  205. }));
  206. dispatch(setVideoInputDevice(device.deviceId));
  207. break;
  208. }
  209. case 'audioinput': {
  210. dispatch(updateSettings({
  211. userSelectedMicDeviceId: device.deviceId,
  212. userSelectedMicDeviceLabel: device.label
  213. }));
  214. dispatch(setAudioInputDevice(device.deviceId));
  215. break;
  216. }
  217. case 'audiooutput': {
  218. setAudioOutputDeviceId(
  219. device.deviceId,
  220. dispatch,
  221. true,
  222. device.label)
  223. .then(() => logger.log('changed audio output device'))
  224. .catch(err => {
  225. logger.warn(
  226. 'Failed to change audio output device.',
  227. 'Default or previously set audio output device will',
  228. ' be used instead.',
  229. err);
  230. });
  231. break;
  232. }
  233. }
  234. });
  235. return true;
  236. }