Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

middleware.ts 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. import { batch } from 'react-redux';
  2. import { IStore } from '../../app/types';
  3. import { _RESET_BREAKOUT_ROOMS } from '../../breakout-rooms/actionTypes';
  4. import { hideNotification } from '../../notifications/actions';
  5. import { isPrejoinPageVisible } from '../../prejoin/functions';
  6. import { getCurrentConference } from '../conference/functions';
  7. import { getMultipleVideoSendingSupportFeatureFlag } from '../config/functions.any';
  8. import { getAvailableDevices } from '../devices/actions';
  9. import {
  10. SET_AUDIO_MUTED,
  11. SET_CAMERA_FACING_MODE,
  12. SET_SCREENSHARE_MUTED,
  13. SET_VIDEO_MUTED,
  14. TOGGLE_CAMERA_FACING_MODE
  15. } from '../media/actionTypes';
  16. import { setScreenshareMuted, toggleCameraFacingMode } from '../media/actions';
  17. import {
  18. CAMERA_FACING_MODE,
  19. MEDIA_TYPE,
  20. MediaType,
  21. SCREENSHARE_MUTISM_AUTHORITY,
  22. VIDEO_MUTISM_AUTHORITY,
  23. VIDEO_TYPE
  24. } from '../media/constants';
  25. import MiddlewareRegistry from '../redux/MiddlewareRegistry';
  26. import StateListenerRegistry from '../redux/StateListenerRegistry';
  27. import {
  28. TRACK_ADDED,
  29. TRACK_MUTE_UNMUTE_FAILED,
  30. TRACK_NO_DATA_FROM_SOURCE,
  31. TRACK_REMOVED,
  32. TRACK_STOPPED,
  33. TRACK_UPDATED
  34. } from './actionTypes';
  35. import {
  36. createLocalTracksA,
  37. destroyLocalTracks,
  38. showNoDataFromSourceVideoError,
  39. toggleScreensharing,
  40. trackMuteUnmuteFailed,
  41. trackNoDataFromSourceNotificationInfoChanged,
  42. trackRemoved
  43. } from './actions';
  44. import {
  45. getLocalTrack,
  46. getTrackByJitsiTrack,
  47. isUserInteractionRequiredForUnmute,
  48. setTrackMuted
  49. } from './functions';
  50. import { ITrack } from './reducer';
  51. import './subscriber';
  52. /**
  53. * Middleware that captures LIB_DID_DISPOSE and LIB_DID_INIT actions and,
  54. * respectively, creates/destroys local media tracks. Also listens to
  55. * media-related actions and performs corresponding operations with tracks.
  56. *
  57. * @param {Store} store - The redux store.
  58. * @returns {Function}
  59. */
  60. MiddlewareRegistry.register(store => next => action => {
  61. switch (action.type) {
  62. case TRACK_ADDED: {
  63. const { local } = action.track;
  64. // The devices list needs to be refreshed when no initial video permissions
  65. // were granted and a local video track is added by umuting the video.
  66. if (local) {
  67. store.dispatch(getAvailableDevices());
  68. }
  69. break;
  70. }
  71. case TRACK_NO_DATA_FROM_SOURCE: {
  72. const result = next(action);
  73. _handleNoDataFromSourceErrors(store, action);
  74. return result;
  75. }
  76. case TRACK_REMOVED: {
  77. _removeNoDataFromSourceNotification(store, action.track);
  78. break;
  79. }
  80. case SET_AUDIO_MUTED:
  81. if (!action.muted
  82. && isUserInteractionRequiredForUnmute(store.getState())) {
  83. return;
  84. }
  85. _setMuted(store, action, MEDIA_TYPE.AUDIO);
  86. break;
  87. case SET_CAMERA_FACING_MODE: {
  88. // XXX The camera facing mode of a MediaStreamTrack can be specified
  89. // only at initialization time and then it can only be toggled. So in
  90. // order to set the camera facing mode, one may destroy the track and
  91. // then initialize a new instance with the new camera facing mode. But
  92. // that is inefficient on mobile at least so the following relies on the
  93. // fact that there are 2 camera facing modes and merely toggles between
  94. // them to (hopefully) get the camera in the specified state.
  95. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  96. let jitsiTrack;
  97. if (localTrack
  98. && (jitsiTrack = localTrack.jitsiTrack)
  99. && jitsiTrack.getCameraFacingMode()
  100. !== action.cameraFacingMode) {
  101. store.dispatch(toggleCameraFacingMode());
  102. }
  103. break;
  104. }
  105. case SET_SCREENSHARE_MUTED:
  106. _setMuted(store, action, action.mediaType);
  107. break;
  108. case SET_VIDEO_MUTED:
  109. if (!action.muted
  110. && isUserInteractionRequiredForUnmute(store.getState())) {
  111. return;
  112. }
  113. _setMuted(store, action, action.mediaType);
  114. break;
  115. case TOGGLE_CAMERA_FACING_MODE: {
  116. const localTrack = _getLocalTrack(store, MEDIA_TYPE.VIDEO);
  117. let jitsiTrack;
  118. if (localTrack && (jitsiTrack = localTrack.jitsiTrack)) {
  119. // XXX MediaStreamTrack._switchCamera is a custom function
  120. // implemented in react-native-webrtc for video which switches
  121. // between the cameras via a native WebRTC library implementation
  122. // without making any changes to the track.
  123. jitsiTrack._switchCamera();
  124. // Don't mirror the video of the back/environment-facing camera.
  125. const mirror
  126. = jitsiTrack.getCameraFacingMode() === CAMERA_FACING_MODE.USER;
  127. store.dispatch({
  128. type: TRACK_UPDATED,
  129. track: {
  130. jitsiTrack,
  131. mirror
  132. }
  133. });
  134. }
  135. break;
  136. }
  137. case TRACK_MUTE_UNMUTE_FAILED: {
  138. const { jitsiTrack } = action.track;
  139. const muted = action.wasMuted;
  140. const isVideoTrack = jitsiTrack.getType() !== MEDIA_TYPE.AUDIO;
  141. if (typeof APP !== 'undefined') {
  142. if (isVideoTrack && jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP
  143. && getMultipleVideoSendingSupportFeatureFlag(store.getState())) {
  144. store.dispatch(setScreenshareMuted(!muted));
  145. } else if (isVideoTrack) {
  146. APP.conference.setVideoMuteStatus();
  147. } else {
  148. APP.conference.setAudioMuteStatus(!muted);
  149. }
  150. }
  151. break;
  152. }
  153. case TRACK_STOPPED: {
  154. const { jitsiTrack } = action.track;
  155. if (typeof APP !== 'undefined'
  156. && getMultipleVideoSendingSupportFeatureFlag(store.getState())
  157. && jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP) {
  158. store.dispatch(toggleScreensharing(false));
  159. }
  160. break;
  161. }
  162. case TRACK_UPDATED: {
  163. // TODO Remove the following calls to APP.UI once components interested
  164. // in track mute changes are moved into React and/or redux.
  165. if (typeof APP !== 'undefined') {
  166. const result = next(action);
  167. const state = store.getState();
  168. if (isPrejoinPageVisible(state)) {
  169. return result;
  170. }
  171. const { jitsiTrack } = action.track;
  172. const muted = jitsiTrack.isMuted();
  173. const participantID = jitsiTrack.getParticipantId();
  174. const isVideoTrack = jitsiTrack.type !== MEDIA_TYPE.AUDIO;
  175. if (isVideoTrack) {
  176. // Do not change the video mute state for local presenter tracks.
  177. if (jitsiTrack.type === MEDIA_TYPE.PRESENTER) {
  178. APP.conference.mutePresenter(muted);
  179. } else if (jitsiTrack.isLocal() && !(jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP)) {
  180. APP.conference.setVideoMuteStatus();
  181. } else if (jitsiTrack.isLocal() && muted && jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP) {
  182. !getMultipleVideoSendingSupportFeatureFlag(state)
  183. && store.dispatch(toggleScreensharing(false, false, true));
  184. } else {
  185. APP.UI.setVideoMuted(participantID);
  186. }
  187. } else if (jitsiTrack.isLocal()) {
  188. APP.conference.setAudioMuteStatus(muted);
  189. } else {
  190. APP.UI.setAudioMuted(participantID, muted);
  191. }
  192. return result;
  193. }
  194. // Mobile.
  195. const { jitsiTrack, local } = action.track;
  196. if (local && jitsiTrack.isMuted()
  197. && jitsiTrack.type === MEDIA_TYPE.VIDEO && jitsiTrack.videoType === VIDEO_TYPE.DESKTOP) {
  198. store.dispatch(toggleScreensharing(false));
  199. }
  200. break;
  201. }
  202. }
  203. return next(action);
  204. });
  205. /**
  206. * Set up state change listener to perform maintenance tasks when the conference
  207. * is left or failed, remove all tracks from the store.
  208. */
  209. StateListenerRegistry.register(
  210. state => getCurrentConference(state),
  211. (conference, { dispatch, getState }, prevConference) => {
  212. const { authRequired, error } = getState()['features/base/conference'];
  213. // conference keep flipping while we are authenticating, skip clearing while we are in that process
  214. if (prevConference && !conference && !authRequired && !error) {
  215. // Clear all tracks.
  216. const remoteTracks = getState()['features/base/tracks'].filter(t => !t.local);
  217. batch(() => {
  218. dispatch(destroyLocalTracks());
  219. for (const track of remoteTracks) {
  220. dispatch(trackRemoved(track.jitsiTrack));
  221. }
  222. dispatch({ type: _RESET_BREAKOUT_ROOMS });
  223. });
  224. }
  225. });
  226. /**
  227. * Handles no data from source errors.
  228. *
  229. * @param {Store} store - The redux store in which the specified action is
  230. * dispatched.
  231. * @param {Action} action - The redux action dispatched in the specified store.
  232. * @private
  233. * @returns {void}
  234. */
  235. function _handleNoDataFromSourceErrors(store: IStore, action: any) {
  236. const { getState, dispatch } = store;
  237. const track = getTrackByJitsiTrack(getState()['features/base/tracks'], action.track.jitsiTrack);
  238. if (!track || !track.local) {
  239. return;
  240. }
  241. const { jitsiTrack } = track;
  242. if (track.mediaType === MEDIA_TYPE.AUDIO && track.isReceivingData) {
  243. _removeNoDataFromSourceNotification(store, action.track);
  244. }
  245. if (track.mediaType === MEDIA_TYPE.VIDEO) {
  246. const { noDataFromSourceNotificationInfo = {} } = track;
  247. if (track.isReceivingData) {
  248. if (noDataFromSourceNotificationInfo.timeout) {
  249. clearTimeout(noDataFromSourceNotificationInfo.timeout);
  250. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  251. }
  252. // try to remove the notification if there is one.
  253. _removeNoDataFromSourceNotification(store, action.track);
  254. } else {
  255. if (noDataFromSourceNotificationInfo.timeout) {
  256. return;
  257. }
  258. const timeout = setTimeout(() => dispatch(showNoDataFromSourceVideoError(jitsiTrack)), 5000);
  259. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, { timeout }));
  260. }
  261. }
  262. }
  263. /**
  264. * Gets the local track associated with a specific {@code MEDIA_TYPE} in a
  265. * specific redux store.
  266. *
  267. * @param {Store} store - The redux store from which the local track associated
  268. * with the specified {@code mediaType} is to be retrieved.
  269. * @param {MEDIA_TYPE} mediaType - The {@code MEDIA_TYPE} of the local track to
  270. * be retrieved from the specified {@code store}.
  271. * @param {boolean} [includePending] - Indicates whether a local track is to be
  272. * returned if it is still pending. A local track is pending if
  273. * {@code getUserMedia} is still executing to create it and, consequently, its
  274. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  275. * track is not returned.
  276. * @private
  277. * @returns {Track} The local {@code Track} associated with the specified
  278. * {@code mediaType} in the specified {@code store}.
  279. */
  280. function _getLocalTrack(
  281. { getState }: { getState: Function; },
  282. mediaType: MediaType,
  283. includePending = false) {
  284. return (
  285. getLocalTrack(
  286. getState()['features/base/tracks'],
  287. mediaType,
  288. includePending));
  289. }
  290. /**
  291. * Removes the no data from source notification associated with the JitsiTrack if displayed.
  292. *
  293. * @param {Store} store - The redux store.
  294. * @param {Track} track - The redux action dispatched in the specified store.
  295. * @returns {void}
  296. */
  297. function _removeNoDataFromSourceNotification({ getState, dispatch }: IStore, track: ITrack) {
  298. const t = getTrackByJitsiTrack(getState()['features/base/tracks'], track.jitsiTrack);
  299. const { jitsiTrack, noDataFromSourceNotificationInfo = {} } = t || {};
  300. if (noDataFromSourceNotificationInfo?.uid) {
  301. dispatch(hideNotification(noDataFromSourceNotificationInfo.uid));
  302. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  303. }
  304. }
  305. /**
  306. * Mutes or unmutes a local track with a specific media type.
  307. *
  308. * @param {Store} store - The redux store in which the specified action is
  309. * dispatched.
  310. * @param {Action} action - The redux action dispatched in the specified store.
  311. * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
  312. * which is being muted or unmuted.
  313. * @private
  314. * @returns {void}
  315. */
  316. async function _setMuted(store: IStore, { ensureTrack, authority, muted }: {
  317. authority: number; ensureTrack: boolean; muted: boolean; }, mediaType: MediaType) {
  318. const { dispatch, getState } = store;
  319. const localTrack = _getLocalTrack(store, mediaType, /* includePending */ true);
  320. const state = getState();
  321. if (mediaType === MEDIA_TYPE.SCREENSHARE
  322. && getMultipleVideoSendingSupportFeatureFlag(state)
  323. && !muted) {
  324. return;
  325. }
  326. if (localTrack) {
  327. // The `jitsiTrack` property will have a value only for a localTrack for which `getUserMedia` has already
  328. // completed. If there's no `jitsiTrack`, then the `muted` state will be applied once the `jitsiTrack` is
  329. // created.
  330. const { jitsiTrack } = localTrack;
  331. const isAudioOnly = (mediaType === MEDIA_TYPE.VIDEO && authority === VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY)
  332. || (mediaType === MEDIA_TYPE.SCREENSHARE && authority === SCREENSHARE_MUTISM_AUTHORITY.AUDIO_ONLY);
  333. // Screenshare cannot be unmuted using the video mute button unless it is muted by audioOnly in the legacy
  334. // screensharing mode.
  335. if (jitsiTrack && (
  336. jitsiTrack.videoType !== 'desktop' || isAudioOnly || getMultipleVideoSendingSupportFeatureFlag(state))
  337. ) {
  338. setTrackMuted(jitsiTrack, muted, state).catch(() => dispatch(trackMuteUnmuteFailed(localTrack, muted)));
  339. }
  340. } else if (!muted && ensureTrack && (typeof APP === 'undefined' || isPrejoinPageVisible(state))) {
  341. // FIXME: This only runs on mobile now because web has its own way of
  342. // creating local tracks. Adjust the check once they are unified.
  343. dispatch(createLocalTracksA({ devices: [ mediaType ] }));
  344. }
  345. }