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

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