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.ts 14KB

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