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.any.ts 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { batch } from 'react-redux';
  2. import { IStore } from '../../app/types';
  3. import { _RESET_BREAKOUT_ROOMS } from '../../breakout-rooms/actionTypes';
  4. import { isPrejoinPageVisible } from '../../prejoin/functions';
  5. import { getCurrentConference } from '../conference/functions';
  6. import {
  7. SET_AUDIO_MUTED,
  8. SET_CAMERA_FACING_MODE,
  9. SET_SCREENSHARE_MUTED,
  10. SET_VIDEO_MUTED,
  11. TOGGLE_CAMERA_FACING_MODE
  12. } from '../media/actionTypes';
  13. import { gumPending, toggleCameraFacingMode } from '../media/actions';
  14. import {
  15. CAMERA_FACING_MODE,
  16. MEDIA_TYPE,
  17. MediaType
  18. } from '../media/constants';
  19. import { IGUMPendingState } from '../media/types';
  20. import MiddlewareRegistry from '../redux/MiddlewareRegistry';
  21. import StateListenerRegistry from '../redux/StateListenerRegistry';
  22. import {
  23. TRACK_UPDATED
  24. } from './actionTypes';
  25. import {
  26. createLocalTracksA,
  27. destroyLocalTracks,
  28. trackMuteUnmuteFailed,
  29. trackRemoved
  30. } from './actions';
  31. import {
  32. getLocalTrack,
  33. isUserInteractionRequiredForUnmute,
  34. setTrackMuted
  35. } from './functions';
  36. import './subscriber';
  37. /**
  38. * Middleware that captures LIB_DID_DISPOSE and LIB_DID_INIT actions and,
  39. * respectively, creates/destroys local media tracks. Also listens to
  40. * media-related actions and performs corresponding operations with tracks.
  41. *
  42. * @param {Store} store - The redux store.
  43. * @returns {Function}
  44. */
  45. MiddlewareRegistry.register(store => next => action => {
  46. switch (action.type) {
  47. case SET_AUDIO_MUTED:
  48. if (!action.muted
  49. && isUserInteractionRequiredForUnmute(store.getState())) {
  50. return;
  51. }
  52. _setMuted(store, action, MEDIA_TYPE.AUDIO);
  53. break;
  54. case SET_CAMERA_FACING_MODE: {
  55. // XXX The camera facing mode of a MediaStreamTrack can be specified
  56. // only at initialization time and then it can only be toggled. So in
  57. // order to set the camera facing mode, one may destroy the track and
  58. // then initialize a new instance with the new camera facing mode. But
  59. // that is inefficient on mobile at least so the following relies on the
  60. // fact that there are 2 camera facing modes and merely toggles between
  61. // them to (hopefully) get the camera in the specified state.
  62. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  63. let jitsiTrack;
  64. if (localTrack
  65. && (jitsiTrack = localTrack.jitsiTrack)
  66. && jitsiTrack.getCameraFacingMode()
  67. !== action.cameraFacingMode) {
  68. store.dispatch(toggleCameraFacingMode());
  69. }
  70. break;
  71. }
  72. case SET_SCREENSHARE_MUTED:
  73. _setMuted(store, action, MEDIA_TYPE.SCREENSHARE);
  74. break;
  75. case SET_VIDEO_MUTED:
  76. if (!action.muted
  77. && isUserInteractionRequiredForUnmute(store.getState())) {
  78. return;
  79. }
  80. _setMuted(store, action, MEDIA_TYPE.VIDEO);
  81. break;
  82. case TOGGLE_CAMERA_FACING_MODE: {
  83. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  84. let jitsiTrack;
  85. if (localTrack && (jitsiTrack = localTrack.jitsiTrack)) {
  86. // XXX MediaStreamTrack._switchCamera is a custom function
  87. // implemented in react-native-webrtc for video which switches
  88. // between the cameras via a native WebRTC library implementation
  89. // without making any changes to the track.
  90. jitsiTrack._switchCamera();
  91. // Don't mirror the video of the back/environment-facing camera.
  92. const mirror
  93. = jitsiTrack.getCameraFacingMode() === CAMERA_FACING_MODE.USER;
  94. store.dispatch({
  95. type: TRACK_UPDATED,
  96. track: {
  97. jitsiTrack,
  98. mirror
  99. }
  100. });
  101. }
  102. break;
  103. }
  104. }
  105. return next(action);
  106. });
  107. /**
  108. * Set up state change listener to perform maintenance tasks when the conference
  109. * is left or failed, remove all tracks from the store.
  110. */
  111. StateListenerRegistry.register(
  112. state => getCurrentConference(state),
  113. (conference, { dispatch, getState }, prevConference) => {
  114. const { authRequired, error } = getState()['features/base/conference'];
  115. // conference keep flipping while we are authenticating, skip clearing while we are in that process
  116. if (prevConference && !conference && !authRequired && !error) {
  117. // Clear all tracks.
  118. const remoteTracks = getState()['features/base/tracks'].filter(t => !t.local);
  119. batch(() => {
  120. dispatch(destroyLocalTracks());
  121. for (const track of remoteTracks) {
  122. dispatch(trackRemoved(track.jitsiTrack));
  123. }
  124. dispatch({ type: _RESET_BREAKOUT_ROOMS });
  125. });
  126. }
  127. });
  128. /**
  129. * Gets the local track associated with a specific {@code MEDIA_TYPE} in a
  130. * specific redux store.
  131. *
  132. * @param {Store} store - The redux store from which the local track associated
  133. * with the specified {@code mediaType} is to be retrieved.
  134. * @param {MEDIA_TYPE} mediaType - The {@code MEDIA_TYPE} of the local track to
  135. * be retrieved from the specified {@code store}.
  136. * @param {boolean} [includePending] - Indicates whether a local track is to be
  137. * returned if it is still pending. A local track is pending if
  138. * {@code getUserMedia} is still executing to create it and, consequently, its
  139. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  140. * track is not returned.
  141. * @private
  142. * @returns {Track} The local {@code Track} associated with the specified
  143. * {@code mediaType} in the specified {@code store}.
  144. */
  145. function _getLocalTrack(
  146. { getState }: { getState: IStore['getState']; },
  147. mediaType: MediaType,
  148. includePending = false) {
  149. return (
  150. getLocalTrack(
  151. getState()['features/base/tracks'],
  152. mediaType,
  153. includePending));
  154. }
  155. /**
  156. * Mutes or unmutes a local track with a specific media type.
  157. *
  158. * @param {Store} store - The redux store in which the specified action is
  159. * dispatched.
  160. * @param {Action} action - The redux action dispatched in the specified store.
  161. * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
  162. * which is being muted or unmuted.
  163. * @private
  164. * @returns {void}
  165. */
  166. function _setMuted(store: IStore, { ensureTrack, muted }: {
  167. ensureTrack: boolean; muted: boolean; }, mediaType: MediaType) {
  168. const { dispatch, getState } = store;
  169. const localTrack = _getLocalTrack(store, mediaType, /* includePending */ true);
  170. const state = getState();
  171. if (mediaType === MEDIA_TYPE.SCREENSHARE && !muted) {
  172. return;
  173. }
  174. if (localTrack) {
  175. // The `jitsiTrack` property will have a value only for a localTrack for which `getUserMedia` has already
  176. // completed. If there's no `jitsiTrack`, then the `muted` state will be applied once the `jitsiTrack` is
  177. // created.
  178. const { jitsiTrack } = localTrack;
  179. if (jitsiTrack) {
  180. setTrackMuted(jitsiTrack, muted, state, dispatch)
  181. .catch(() => dispatch(trackMuteUnmuteFailed(localTrack, muted)));
  182. }
  183. } else if (!muted && ensureTrack && (typeof APP === 'undefined' || isPrejoinPageVisible(state))) {
  184. typeof APP !== 'undefined' && dispatch(gumPending([ mediaType ], IGUMPendingState.PENDING_UNMUTE));
  185. // FIXME: This only runs on mobile now because web has its own way of
  186. // creating local tracks. Adjust the check once they are unified.
  187. dispatch(createLocalTracksA({ devices: [ mediaType ] })).then(() => {
  188. typeof APP !== 'undefined' && dispatch(gumPending([ mediaType ], IGUMPendingState.NONE));
  189. });
  190. }
  191. }