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

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