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.web.ts 3.2KB

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