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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { LIB_DISPOSED, LIB_INITIALIZED } from '../lib-jitsi-meet';
  2. import {
  3. MEDIA_TYPE,
  4. SET_AUDIO_MUTED,
  5. SET_CAMERA_FACING_MODE,
  6. SET_VIDEO_MUTED,
  7. setAudioMuted,
  8. setVideoMuted
  9. } from '../media';
  10. import { MiddlewareRegistry } from '../redux';
  11. import { createLocalTracks, destroyLocalTracks } from './actions';
  12. import { TRACK_UPDATED } from './actionTypes';
  13. import { getLocalTrack, setTrackMuted } from './functions';
  14. /**
  15. * Middleware that captures LIB_INITIALIZED and LIB_DISPOSED actions
  16. * and respectively creates/destroys local media tracks. Also listens to media-
  17. * related actions and performs corresponding operations with tracks.
  18. *
  19. * @param {Store} store - Redux store.
  20. * @returns {Function}
  21. */
  22. MiddlewareRegistry.register(store => next => action => {
  23. switch (action.type) {
  24. case LIB_INITIALIZED:
  25. store.dispatch(createLocalTracks());
  26. break;
  27. case LIB_DISPOSED:
  28. store.dispatch(destroyLocalTracks());
  29. break;
  30. case SET_AUDIO_MUTED:
  31. _setMuted(store, action, MEDIA_TYPE.AUDIO);
  32. break;
  33. case SET_CAMERA_FACING_MODE:
  34. store.dispatch(
  35. createLocalTracks({
  36. devices: [ MEDIA_TYPE.VIDEO ],
  37. facingMode: action.cameraFacingMode
  38. })
  39. );
  40. break;
  41. case SET_VIDEO_MUTED:
  42. _setMuted(store, action, MEDIA_TYPE.VIDEO);
  43. break;
  44. case TRACK_UPDATED:
  45. return _trackUpdated(store, next, action);
  46. }
  47. return next(action);
  48. });
  49. /**
  50. * Gets the local track associated with a specific <tt>MEDIA_TYPE</tt> in a
  51. * specific Redux store.
  52. *
  53. * @param {Store} store - The Redux store from which the local track associated
  54. * with the specified <tt>mediaType</tt> is to be retrieved.
  55. * @param {MEDIA_TYPE} mediaType - The <tt>MEDIA_TYPE</tt> of the local track to
  56. * be retrieved from the specified <tt>store</tt>.
  57. * @private
  58. * @returns {Track} The local <tt>Track</tt> associated with the specified
  59. * <tt>mediaType</tt> in the specified <tt>store</tt>.
  60. */
  61. function _getLocalTrack(store, mediaType) {
  62. return getLocalTrack(store.getState()['features/base/tracks'], mediaType);
  63. }
  64. /**
  65. * Mutes or unmutes a local track with a specific media type.
  66. *
  67. * @param {Store} store - The Redux store in which the specified action is
  68. * dispatched.
  69. * @param {Action} action - The Redux action dispatched in the specified store.
  70. * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
  71. * which is being muted or unmuted.
  72. * @private
  73. * @returns {void}
  74. */
  75. function _setMuted(store, action, mediaType) {
  76. const localTrack = _getLocalTrack(store, mediaType);
  77. localTrack && setTrackMuted(localTrack.jitsiTrack, action.muted);
  78. }
  79. /**
  80. * Intercepts the action <tt>TRACK_UPDATED</tt> in order to synchronize the
  81. * muted states of the local tracks of features/base/tracks with the muted
  82. * states of features/base/media.
  83. *
  84. * @param {Store} store - The Redux store in which the specified <tt>action</tt>
  85. * is being dispatched.
  86. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  87. * specified <tt>action</tt> to the specified <tt>store</tt>.
  88. * @param {Action} action - The Redux action <tt>TRACK_UPDATED</tt> which is
  89. * being dispatched in the specified <tt>store</tt>.
  90. * @private
  91. * @returns {Object} The new state that is the result of the reduction of the
  92. * specified <tt>action</tt>.
  93. */
  94. function _trackUpdated(store, next, action) {
  95. // Determine the muted state of the local track before the update.
  96. const track = action.track;
  97. let mediaType;
  98. let oldMuted;
  99. if ('muted' in track) {
  100. // XXX The return value of JitsiTrack.getType() is of type MEDIA_TYPE
  101. // that happens to be compatible with the type MEDIA_TYPE defined by
  102. // jitsi-meet-react.
  103. mediaType = track.jitsiTrack.getType();
  104. const localTrack = _getLocalTrack(store, mediaType);
  105. if (localTrack) {
  106. oldMuted = localTrack.muted;
  107. }
  108. }
  109. const result = next(action);
  110. if (typeof oldMuted !== 'undefined') {
  111. // Determine the muted state of the local track after the update. If the
  112. // muted states before and after the update differ, then the respective
  113. // media state should by synchronized.
  114. const localTrack = _getLocalTrack(store, mediaType);
  115. if (localTrack) {
  116. const newMuted = localTrack.muted;
  117. if (oldMuted !== newMuted) {
  118. switch (mediaType) {
  119. case MEDIA_TYPE.AUDIO:
  120. store.dispatch(setAudioMuted(newMuted));
  121. break;
  122. case MEDIA_TYPE.VIDEO:
  123. store.dispatch(setVideoMuted(newMuted));
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. return result;
  130. }