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

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