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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 {
  7. removePendingDeviceRequests,
  8. setAudioInputDevice,
  9. setVideoInputDevice
  10. } from './actions';
  11. import {
  12. CHECK_AND_NOTIFY_FOR_NEW_DEVICE,
  13. SET_AUDIO_INPUT_DEVICE,
  14. SET_VIDEO_INPUT_DEVICE
  15. } from './actionTypes';
  16. import { showNotification } from '../../notifications';
  17. import { updateSettings } from '../settings';
  18. import { setAudioOutputDeviceId } from './functions';
  19. const logger = require('jitsi-meet-logger').getLogger(__filename);
  20. /**
  21. * Implements the middleware of the feature base/devices.
  22. *
  23. * @param {Store} store - Redux store.
  24. * @returns {Function}
  25. */
  26. // eslint-disable-next-line no-unused-vars
  27. MiddlewareRegistry.register(store => next => action => {
  28. switch (action.type) {
  29. case CONFERENCE_JOINED:
  30. return _conferenceJoined(store, next, action);
  31. case SET_AUDIO_INPUT_DEVICE:
  32. APP.UI.emitEvent(UIEvents.AUDIO_DEVICE_CHANGED, action.deviceId);
  33. break;
  34. case SET_VIDEO_INPUT_DEVICE:
  35. APP.UI.emitEvent(UIEvents.VIDEO_DEVICE_CHANGED, action.deviceId);
  36. break;
  37. case CHECK_AND_NOTIFY_FOR_NEW_DEVICE:
  38. _checkAndNotifyForNewDevice(store, action.newDevices, action.oldDevices);
  39. break;
  40. }
  41. return next(action);
  42. });
  43. /**
  44. * Does extra sync up on properties that may need to be updated after the
  45. * conference was joined.
  46. *
  47. * @param {Store} store - The redux store in which the specified {@code action}
  48. * is being dispatched.
  49. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  50. * specified {@code action} to the specified {@code store}.
  51. * @param {Action} action - The redux action {@code CONFERENCE_JOINED} which is
  52. * being dispatched in the specified {@code store}.
  53. * @private
  54. * @returns {Object} The value returned by {@code next(action)}.
  55. */
  56. function _conferenceJoined({ dispatch, getState }, next, action) {
  57. const result = next(action);
  58. const state = getState();
  59. const { pendingRequests } = state['features/base/devices'];
  60. pendingRequests.forEach(request => {
  61. processExternalDeviceRequest(
  62. dispatch,
  63. getState,
  64. request,
  65. request.responseCallback);
  66. });
  67. dispatch(removePendingDeviceRequests());
  68. return result;
  69. }
  70. /**
  71. * Finds a new device by comparing new and old array of devices and dispatches
  72. * notification with the new device.
  73. *
  74. * @param {Store} store - The redux store in which the specified {@code action}
  75. * is being dispatched.
  76. * @param {MediaDeviceInfo[]} newDevices - The array of new devices we received.
  77. * @param {MediaDeviceInfo[]} oldDevices - The array of the old devices we have.
  78. * @private
  79. * @returns {void}
  80. */
  81. function _checkAndNotifyForNewDevice(store, newDevices, oldDevices) {
  82. const { dispatch } = store;
  83. // let's intersect both newDevices and oldDevices and handle thew newly
  84. // added devices
  85. const onlyNewDevices = newDevices.filter(
  86. nDevice => !oldDevices.find(
  87. device => device.deviceId === nDevice.deviceId));
  88. onlyNewDevices.forEach(newDevice => {
  89. // we want to strip any device details that are not very
  90. // user friendly, like usb ids put in brackets at the end
  91. let description = newDevice.label;
  92. const ix = description.lastIndexOf('(');
  93. if (ix !== -1) {
  94. description = description.substr(0, ix);
  95. }
  96. let titleKey;
  97. switch (newDevice.kind) {
  98. case 'videoinput': {
  99. titleKey = 'notify.newDeviceCameraTitle';
  100. break;
  101. }
  102. case 'audioinput': {
  103. titleKey = 'notify.newDeviceMicTitle';
  104. break;
  105. }
  106. case 'audiooutput': {
  107. titleKey = 'notify.newDeviceCameraTitle';
  108. break;
  109. }
  110. }
  111. dispatch(showNotification({
  112. description,
  113. titleKey,
  114. customActionNameKey: 'notify.newDeviceAction',
  115. customActionHandler: _useDevice.bind(undefined, store, newDevice)
  116. }));
  117. });
  118. }
  119. /**
  120. * Set a device to be currently used, selected by the user.
  121. *
  122. * @param {Store} store - The redux store in which the specified {@code action}
  123. * is being dispatched.
  124. * @param {MediaDeviceInfo} device - The device to save.
  125. * @returns {boolean} - Returns true in order notifications to be dismissed.
  126. * @private
  127. */
  128. function _useDevice({ dispatch }, device) {
  129. switch (device.kind) {
  130. case 'videoinput': {
  131. dispatch(updateSettings({
  132. userSelectedCameraDeviceId: device.deviceId,
  133. userSelectedCameraDeviceLabel: device.label
  134. }));
  135. dispatch(setVideoInputDevice(device.deviceId));
  136. break;
  137. }
  138. case 'audioinput': {
  139. dispatch(updateSettings({
  140. userSelectedMicDeviceId: device.deviceId,
  141. userSelectedMicDeviceLabel: device.label
  142. }));
  143. dispatch(setAudioInputDevice(device.deviceId));
  144. break;
  145. }
  146. case 'audiooutput': {
  147. setAudioOutputDeviceId(
  148. device.deviceId,
  149. dispatch,
  150. true,
  151. device.label)
  152. .then(() => logger.log('changed audio output device'))
  153. .catch(err => {
  154. logger.warn(
  155. 'Failed to change audio output device.',
  156. 'Default or previously set audio output device will',
  157. ' be used instead.',
  158. err);
  159. });
  160. break;
  161. }
  162. }
  163. return true;
  164. }