您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 3.1KB

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