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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* @flow */
  2. import {
  3. MEDIA_TYPE,
  4. SET_AUDIO_AVAILABLE,
  5. SET_VIDEO_AVAILABLE
  6. } from '../base/media';
  7. import { MiddlewareRegistry } from '../base/redux';
  8. import { isLocalTrackMuted, TRACK_UPDATED } from '../base/tracks';
  9. import { setToolbarButton } from './actions';
  10. import { CLEAR_TOOLBOX_TIMEOUT, SET_TOOLBOX_TIMEOUT } from './actionTypes';
  11. /**
  12. * Middleware which intercepts Toolbox actions to handle changes to the
  13. * visibility timeout of the Toolbox.
  14. *
  15. * @param {Store} store - The redux store.
  16. * @returns {Function}
  17. */
  18. MiddlewareRegistry.register(store => next => action => {
  19. switch (action.type) {
  20. case CLEAR_TOOLBOX_TIMEOUT: {
  21. const { timeoutID } = store.getState()['features/toolbox'];
  22. clearTimeout(timeoutID);
  23. break;
  24. }
  25. case SET_AUDIO_AVAILABLE:
  26. return _setMediaAvailableOrMuted(store, next, action);
  27. case SET_TOOLBOX_TIMEOUT: {
  28. const { timeoutID } = store.getState()['features/toolbox'];
  29. const { handler, timeoutMS } = action;
  30. clearTimeout(timeoutID);
  31. const newTimeoutId = setTimeout(handler, timeoutMS);
  32. action.timeoutID = newTimeoutId;
  33. break;
  34. }
  35. case SET_VIDEO_AVAILABLE:
  36. return _setMediaAvailableOrMuted(store, next, action);
  37. case TRACK_UPDATED:
  38. if (action.track.jitsiTrack.isLocal()) {
  39. return _setMediaAvailableOrMuted(store, next, action);
  40. }
  41. break;
  42. }
  43. return next(action);
  44. });
  45. /**
  46. * Adjusts the state of toolbar's microphone or camera button.
  47. *
  48. * @param {Store} store - The redux store.
  49. * @param {Function} next - The redux function to continue dispatching the
  50. * specified {@code action} in the specified {@code store}.
  51. * @param {Object} action - {@code SET_AUDIO_AVAILABLE},
  52. * {@code SET_VIDEO_AVAILABLE}, or {@code TRACK_UPDATED}.
  53. * @returns {*}
  54. */
  55. function _setMediaAvailableOrMuted({ dispatch, getState }, next, action) {
  56. const result = next(action);
  57. let mediaType;
  58. switch (action.type) {
  59. case SET_AUDIO_AVAILABLE:
  60. mediaType = MEDIA_TYPE.AUDIO;
  61. break;
  62. case SET_VIDEO_AVAILABLE:
  63. mediaType = MEDIA_TYPE.VIDEO;
  64. break;
  65. case TRACK_UPDATED:
  66. mediaType
  67. = action.track.jitsiTrack.isAudioTrack()
  68. ? MEDIA_TYPE.AUDIO
  69. : MEDIA_TYPE.VIDEO;
  70. break;
  71. default:
  72. throw new Error(`Unsupported action ${action}`);
  73. }
  74. const state = getState();
  75. const { audio, video } = state['features/base/media'];
  76. const { available } = mediaType === MEDIA_TYPE.AUDIO ? audio : video;
  77. const i18nKey
  78. = mediaType === MEDIA_TYPE.AUDIO
  79. ? available ? 'mute' : 'micDisabled'
  80. : available ? 'videomute' : 'cameraDisabled';
  81. const tracks = state['features/base/tracks'];
  82. const muted = isLocalTrackMuted(tracks, mediaType);
  83. dispatch(
  84. setToolbarButton(
  85. mediaType === MEDIA_TYPE.AUDIO ? 'microphone' : 'camera',
  86. {
  87. enabled: available,
  88. i18n: `[content]toolbar.${i18nKey}`,
  89. toggled: available ? muted : true
  90. }));
  91. return result;
  92. }