Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

middleware.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. For new devices with same groupId only one
  73. * notification will be shown, this is so to avoid showing multiple notifications
  74. * for audio input and audio output devices.
  75. *
  76. * @param {Store} store - The redux store in which the specified {@code action}
  77. * is being dispatched.
  78. * @param {MediaDeviceInfo[]} newDevices - The array of new devices we received.
  79. * @param {MediaDeviceInfo[]} oldDevices - The array of the old devices we have.
  80. * @private
  81. * @returns {void}
  82. */
  83. function _checkAndNotifyForNewDevice(store, newDevices, oldDevices) {
  84. const { dispatch } = store;
  85. // let's intersect both newDevices and oldDevices and handle thew newly
  86. // added devices
  87. const onlyNewDevices = newDevices.filter(
  88. nDevice => !oldDevices.find(
  89. device => device.deviceId === nDevice.deviceId));
  90. // we group devices by groupID which normally is the grouping by physical device
  91. // plugging in headset we provide normally two device, one input and one output
  92. // and we want to show only one notification for this physical audio device
  93. const devicesGroupBy = onlyNewDevices.reduce((accumulated, value) => {
  94. accumulated[value.groupId] = accumulated[value.groupId] || [];
  95. accumulated[value.groupId].push(value);
  96. return accumulated;
  97. }, {});
  98. Object.values(devicesGroupBy).forEach(devicesArray => {
  99. if (devicesArray.length < 1) {
  100. return;
  101. }
  102. // let's get the first device as a reference, we will use it for
  103. // label and type
  104. const newDevice = devicesArray[0];
  105. // we want to strip any device details that are not very
  106. // user friendly, like usb ids put in brackets at the end
  107. let description = newDevice.label;
  108. const ix = description.lastIndexOf('(');
  109. if (ix !== -1) {
  110. description = description.substr(0, ix);
  111. }
  112. let titleKey;
  113. switch (newDevice.kind) {
  114. case 'videoinput': {
  115. titleKey = 'notify.newDeviceCameraTitle';
  116. break;
  117. }
  118. case 'audioinput' :
  119. case 'audiooutput': {
  120. titleKey = 'notify.newDeviceAudioTitle';
  121. break;
  122. }
  123. }
  124. dispatch(showNotification({
  125. description,
  126. titleKey,
  127. customActionNameKey: 'notify.newDeviceAction',
  128. customActionHandler: _useDevice.bind(undefined, store, devicesArray)
  129. }));
  130. });
  131. }
  132. /**
  133. * Set a device to be currently used, selected by the user.
  134. *
  135. * @param {Store} store - The redux store in which the specified {@code action}
  136. * is being dispatched.
  137. * @param {Array<MediaDeviceInfo|InputDeviceInfo>} devices - The devices to save.
  138. * @returns {boolean} - Returns true in order notifications to be dismissed.
  139. * @private
  140. */
  141. function _useDevice({ dispatch }, devices) {
  142. devices.forEach(device => {
  143. switch (device.kind) {
  144. case 'videoinput': {
  145. dispatch(updateSettings({
  146. userSelectedCameraDeviceId: device.deviceId,
  147. userSelectedCameraDeviceLabel: device.label
  148. }));
  149. dispatch(setVideoInputDevice(device.deviceId));
  150. break;
  151. }
  152. case 'audioinput': {
  153. dispatch(updateSettings({
  154. userSelectedMicDeviceId: device.deviceId,
  155. userSelectedMicDeviceLabel: device.label
  156. }));
  157. dispatch(setAudioInputDevice(device.deviceId));
  158. break;
  159. }
  160. case 'audiooutput': {
  161. setAudioOutputDeviceId(
  162. device.deviceId,
  163. dispatch,
  164. true,
  165. device.label)
  166. .then(() => logger.log('changed audio output device'))
  167. .catch(err => {
  168. logger.warn(
  169. 'Failed to change audio output device.',
  170. 'Default or previously set audio output device will',
  171. ' be used instead.',
  172. err);
  173. });
  174. break;
  175. }
  176. }
  177. });
  178. return true;
  179. }