您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.any.ts 8.1KB

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