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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.NO_DATA_FROM_SOURCE]: 'dialog.micNotSendingData',
  28. [JitsiTrackErrors.NOT_FOUND]: 'dialog.micNotFoundError',
  29. [JitsiTrackErrors.PERMISSION_DENIED]: 'dialog.micPermissionDeniedError'
  30. },
  31. camera: {
  32. [JitsiTrackErrors.CONSTRAINT_FAILED]: 'dialog.cameraConstraintFailedError',
  33. [JitsiTrackErrors.GENERAL]: 'dialog.cameraUnknownError',
  34. [JitsiTrackErrors.NO_DATA_FROM_SOURCE]: 'dialog.cameraNotSendingData',
  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. APP.UI.emitEvent(UIEvents.AUDIO_DEVICE_CHANGED, action.deviceId);
  92. break;
  93. case SET_VIDEO_INPUT_DEVICE:
  94. APP.UI.emitEvent(UIEvents.VIDEO_DEVICE_CHANGED, action.deviceId);
  95. break;
  96. case CHECK_AND_NOTIFY_FOR_NEW_DEVICE:
  97. _checkAndNotifyForNewDevice(store, action.newDevices, action.oldDevices);
  98. break;
  99. }
  100. return next(action);
  101. });
  102. /**
  103. * Does extra sync up on properties that may need to be updated after the
  104. * conference was joined.
  105. *
  106. * @param {Store} store - The redux store in which the specified {@code action}
  107. * is being dispatched.
  108. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  109. * specified {@code action} to the specified {@code store}.
  110. * @param {Action} action - The redux action {@code CONFERENCE_JOINED} which is
  111. * being dispatched in the specified {@code store}.
  112. * @private
  113. * @returns {Object} The value returned by {@code next(action)}.
  114. */
  115. function _conferenceJoined({ dispatch, getState }, next, action) {
  116. const result = next(action);
  117. const state = getState();
  118. const { pendingRequests } = state['features/base/devices'];
  119. pendingRequests.forEach(request => {
  120. processExternalDeviceRequest(
  121. dispatch,
  122. getState,
  123. request,
  124. request.responseCallback);
  125. });
  126. dispatch(removePendingDeviceRequests());
  127. return result;
  128. }
  129. /**
  130. * Finds a new device by comparing new and old array of devices and dispatches
  131. * notification with the new device. For new devices with same groupId only one
  132. * notification will be shown, this is so to avoid showing multiple notifications
  133. * for audio input and audio output devices.
  134. *
  135. * @param {Store} store - The redux store in which the specified {@code action}
  136. * is being dispatched.
  137. * @param {MediaDeviceInfo[]} newDevices - The array of new devices we received.
  138. * @param {MediaDeviceInfo[]} oldDevices - The array of the old devices we have.
  139. * @private
  140. * @returns {void}
  141. */
  142. function _checkAndNotifyForNewDevice(store, newDevices, oldDevices) {
  143. const { dispatch } = store;
  144. // let's intersect both newDevices and oldDevices and handle thew newly
  145. // added devices
  146. const onlyNewDevices = newDevices.filter(
  147. nDevice => !oldDevices.find(
  148. device => device.deviceId === nDevice.deviceId));
  149. // we group devices by groupID which normally is the grouping by physical device
  150. // plugging in headset we provide normally two device, one input and one output
  151. // and we want to show only one notification for this physical audio device
  152. const devicesGroupBy = onlyNewDevices.reduce((accumulated, value) => {
  153. accumulated[value.groupId] = accumulated[value.groupId] || [];
  154. accumulated[value.groupId].push(value);
  155. return accumulated;
  156. }, {});
  157. Object.values(devicesGroupBy).forEach(devicesArray => {
  158. if (devicesArray.length < 1) {
  159. return;
  160. }
  161. // let's get the first device as a reference, we will use it for
  162. // label and type
  163. const newDevice = devicesArray[0];
  164. // we want to strip any device details that are not very
  165. // user friendly, like usb ids put in brackets at the end
  166. let description = newDevice.label;
  167. const ix = description.lastIndexOf('(');
  168. if (ix !== -1) {
  169. description = description.substr(0, ix);
  170. }
  171. let titleKey;
  172. switch (newDevice.kind) {
  173. case 'videoinput': {
  174. titleKey = 'notify.newDeviceCameraTitle';
  175. break;
  176. }
  177. case 'audioinput' :
  178. case 'audiooutput': {
  179. titleKey = 'notify.newDeviceAudioTitle';
  180. break;
  181. }
  182. }
  183. dispatch(showNotification({
  184. description,
  185. titleKey,
  186. customActionNameKey: 'notify.newDeviceAction',
  187. customActionHandler: _useDevice.bind(undefined, store, devicesArray)
  188. }));
  189. });
  190. }
  191. /**
  192. * Set a device to be currently used, selected by the user.
  193. *
  194. * @param {Store} store - The redux store in which the specified {@code action}
  195. * is being dispatched.
  196. * @param {Array<MediaDeviceInfo|InputDeviceInfo>} devices - The devices to save.
  197. * @returns {boolean} - Returns true in order notifications to be dismissed.
  198. * @private
  199. */
  200. function _useDevice({ dispatch }, devices) {
  201. devices.forEach(device => {
  202. switch (device.kind) {
  203. case 'videoinput': {
  204. dispatch(updateSettings({
  205. userSelectedCameraDeviceId: device.deviceId,
  206. userSelectedCameraDeviceLabel: device.label
  207. }));
  208. dispatch(setVideoInputDevice(device.deviceId));
  209. break;
  210. }
  211. case 'audioinput': {
  212. dispatch(updateSettings({
  213. userSelectedMicDeviceId: device.deviceId,
  214. userSelectedMicDeviceLabel: device.label
  215. }));
  216. dispatch(setAudioInputDevice(device.deviceId));
  217. break;
  218. }
  219. case 'audiooutput': {
  220. setAudioOutputDeviceId(
  221. device.deviceId,
  222. dispatch,
  223. true,
  224. device.label)
  225. .then(() => logger.log('changed audio output device'))
  226. .catch(err => {
  227. logger.warn(
  228. 'Failed to change audio output device.',
  229. 'Default or previously set audio output device will',
  230. ' be used instead.',
  231. err);
  232. });
  233. break;
  234. }
  235. }
  236. });
  237. return true;
  238. }