Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

middleware.web.ts 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { IStore } from '../../app/types';
  2. import { PREJOIN_INITIALIZED } from '../../prejoin/actionTypes';
  3. import { getJwtName } from '../jwt/functions';
  4. import { MEDIA_TYPE } from '../media/constants';
  5. import MiddlewareRegistry from '../redux/MiddlewareRegistry';
  6. import { TRACK_ADDED } from '../tracks/actionTypes';
  7. import { ITrack } from '../tracks/types';
  8. import { updateSettings } from './actions';
  9. import logger from './logger';
  10. import './middleware.any';
  11. /**
  12. * The middleware of the feature base/settings. Distributes changes to the state
  13. * of base/settings to the states of other features computed from the state of
  14. * base/settings.
  15. *
  16. * @param {Store} store - The redux store.
  17. * @returns {Function}
  18. */
  19. MiddlewareRegistry.register(store => next => action => {
  20. const result = next(action);
  21. switch (action.type) {
  22. case PREJOIN_INITIALIZED:
  23. _maybeUpdateDisplayName(store);
  24. break;
  25. case TRACK_ADDED:
  26. _maybeUpdateDeviceId(store, action.track);
  27. break;
  28. }
  29. return result;
  30. });
  31. /**
  32. * Updates the display name to the one in JWT if there is one.
  33. *
  34. * @param {Store} store - The redux store.
  35. * @private
  36. * @returns {void}
  37. */
  38. function _maybeUpdateDisplayName({ dispatch, getState }: IStore) {
  39. const state = getState();
  40. const hasJwt = Boolean(state['features/base/jwt'].jwt);
  41. if (hasJwt) {
  42. const displayName = getJwtName(state);
  43. if (displayName) {
  44. dispatch(updateSettings({
  45. displayName
  46. }));
  47. }
  48. }
  49. }
  50. /**
  51. * Maybe update the camera or mic device id when local track is added or updated.
  52. *
  53. * @param {Store} store - The redux store.
  54. * @param {ITrack} track - The potential local track.
  55. * @private
  56. * @returns {void}
  57. */
  58. function _maybeUpdateDeviceId({ dispatch, getState }: IStore, track: ITrack) {
  59. if (track.local) {
  60. const { cameraDeviceId, micDeviceId } = getState()['features/base/settings'];
  61. const deviceId = track.jitsiTrack.getDeviceId();
  62. if (track.mediaType === MEDIA_TYPE.VIDEO && track.videoType === 'camera' && cameraDeviceId !== deviceId) {
  63. dispatch(updateSettings({
  64. cameraDeviceId: track.jitsiTrack.getDeviceId()
  65. }));
  66. logger.info(`switched local video device to: ${deviceId}`);
  67. } else if (track.mediaType === MEDIA_TYPE.AUDIO && micDeviceId !== deviceId) {
  68. dispatch(updateSettings({
  69. micDeviceId: track.jitsiTrack.getDeviceId()
  70. }));
  71. logger.info(`switched local audio input device to: ${deviceId}`);
  72. }
  73. }
  74. }