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.8KB

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