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

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