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

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