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.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // @flow
  2. import { batch } from 'react-redux';
  3. import UIEvents from '../../../../service/UI/UIEvents';
  4. import { showModeratedNotification } from '../../av-moderation/actions';
  5. import { shouldShowModeratedNotification } from '../../av-moderation/functions';
  6. import { _RESET_BREAKOUT_ROOMS } from '../../breakout-rooms/actionTypes';
  7. import { hideNotification, isModerationNotificationDisplayed } from '../../notifications';
  8. import { isPrejoinPageVisible } from '../../prejoin/functions';
  9. import { getCurrentConference } from '../conference/functions';
  10. import { getMultipleVideoSendingSupportFeatureFlag } from '../config';
  11. import { getAvailableDevices } from '../devices/actions';
  12. import {
  13. CAMERA_FACING_MODE,
  14. MEDIA_TYPE,
  15. SET_AUDIO_MUTED,
  16. SET_CAMERA_FACING_MODE,
  17. SET_VIDEO_MUTED,
  18. VIDEO_MUTISM_AUTHORITY,
  19. TOGGLE_CAMERA_FACING_MODE,
  20. toggleCameraFacingMode,
  21. SET_SCREENSHARE_MUTED,
  22. VIDEO_TYPE,
  23. setScreenshareMuted,
  24. SCREENSHARE_MUTISM_AUTHORITY
  25. } from '../media';
  26. import { MiddlewareRegistry, StateListenerRegistry } from '../redux';
  27. import {
  28. TOGGLE_SCREENSHARING,
  29. TRACK_ADDED,
  30. TRACK_MUTE_UNMUTE_FAILED,
  31. TRACK_NO_DATA_FROM_SOURCE,
  32. TRACK_REMOVED,
  33. TRACK_STOPPED,
  34. TRACK_UPDATED
  35. } from './actionTypes';
  36. import {
  37. createLocalTracksA,
  38. destroyLocalTracks,
  39. showNoDataFromSourceVideoError,
  40. toggleScreensharing,
  41. trackMuteUnmuteFailed,
  42. trackRemoved,
  43. trackNoDataFromSourceNotificationInfoChanged
  44. } from './actions';
  45. import {
  46. getLocalTrack,
  47. getTrackByJitsiTrack,
  48. isUserInteractionRequiredForUnmute,
  49. setTrackMuted
  50. } from './functions';
  51. import './subscriber';
  52. declare var APP: Object;
  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 TOGGLE_SCREENSHARING:
  139. if (typeof APP === 'object') {
  140. // check for A/V Moderation when trying to start screen sharing
  141. if ((action.enabled || action.enabled === undefined)
  142. && shouldShowModeratedNotification(MEDIA_TYPE.VIDEO, store.getState())) {
  143. if (!isModerationNotificationDisplayed(MEDIA_TYPE.PRESENTER, store.getState())) {
  144. store.dispatch(showModeratedNotification(MEDIA_TYPE.PRESENTER));
  145. }
  146. return;
  147. }
  148. const { enabled, audioOnly, ignoreDidHaveVideo } = action;
  149. if (!getMultipleVideoSendingSupportFeatureFlag(store.getState())) {
  150. APP.UI.emitEvent(UIEvents.TOGGLE_SCREENSHARING,
  151. {
  152. enabled,
  153. audioOnly,
  154. ignoreDidHaveVideo
  155. });
  156. }
  157. }
  158. break;
  159. case TRACK_MUTE_UNMUTE_FAILED: {
  160. const { jitsiTrack } = action.track;
  161. const muted = action.wasMuted;
  162. const isVideoTrack = jitsiTrack.getType() !== MEDIA_TYPE.AUDIO;
  163. if (typeof APP !== 'undefined') {
  164. if (isVideoTrack && jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP
  165. && getMultipleVideoSendingSupportFeatureFlag(store.getState())) {
  166. store.dispatch(setScreenshareMuted(!muted));
  167. } else if (isVideoTrack) {
  168. APP.conference.setVideoMuteStatus();
  169. } else {
  170. APP.conference.setAudioMuteStatus(!muted);
  171. }
  172. }
  173. break;
  174. }
  175. case TRACK_STOPPED: {
  176. const { jitsiTrack } = action.track;
  177. if (typeof APP !== 'undefined'
  178. && getMultipleVideoSendingSupportFeatureFlag(store.getState())
  179. && jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP) {
  180. store.dispatch(toggleScreensharing(false));
  181. }
  182. break;
  183. }
  184. case TRACK_UPDATED: {
  185. // TODO Remove the following calls to APP.UI once components interested
  186. // in track mute changes are moved into React and/or redux.
  187. if (typeof APP !== 'undefined') {
  188. const result = next(action);
  189. const state = store.getState();
  190. if (isPrejoinPageVisible(state)) {
  191. return result;
  192. }
  193. const { jitsiTrack } = action.track;
  194. const muted = jitsiTrack.isMuted();
  195. const participantID = jitsiTrack.getParticipantId();
  196. const isVideoTrack = jitsiTrack.type !== MEDIA_TYPE.AUDIO;
  197. if (isVideoTrack) {
  198. // Do not change the video mute state for local presenter tracks.
  199. if (jitsiTrack.type === MEDIA_TYPE.PRESENTER) {
  200. APP.conference.mutePresenter(muted);
  201. } else if (jitsiTrack.isLocal() && !(jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP)) {
  202. APP.conference.setVideoMuteStatus();
  203. } else if (jitsiTrack.isLocal() && muted && jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP) {
  204. !getMultipleVideoSendingSupportFeatureFlag(state)
  205. && store.dispatch(toggleScreensharing(false, false, true));
  206. } else {
  207. APP.UI.setVideoMuted(participantID);
  208. }
  209. } else if (jitsiTrack.isLocal()) {
  210. APP.conference.setAudioMuteStatus(muted);
  211. } else {
  212. APP.UI.setAudioMuted(participantID, muted);
  213. }
  214. return result;
  215. }
  216. const { jitsiTrack } = action.track;
  217. if (jitsiTrack.isMuted()
  218. && jitsiTrack.type === MEDIA_TYPE.VIDEO && jitsiTrack.videoType === VIDEO_TYPE.DESKTOP) {
  219. store.dispatch(toggleScreensharing(false));
  220. }
  221. break;
  222. }
  223. }
  224. return next(action);
  225. });
  226. /**
  227. * Set up state change listener to perform maintenance tasks when the conference
  228. * is left or failed, remove all tracks from the store.
  229. */
  230. StateListenerRegistry.register(
  231. state => getCurrentConference(state),
  232. (conference, { dispatch, getState }, prevConference) => {
  233. const { authRequired, error } = getState()['features/base/conference'];
  234. // conference keep flipping while we are authenticating, skip clearing while we are in that process
  235. if (prevConference && !conference && !authRequired && !error) {
  236. // Clear all tracks.
  237. const remoteTracks = getState()['features/base/tracks'].filter(t => !t.local);
  238. batch(() => {
  239. dispatch(destroyLocalTracks());
  240. for (const track of remoteTracks) {
  241. dispatch(trackRemoved(track.jitsiTrack));
  242. }
  243. dispatch({ type: _RESET_BREAKOUT_ROOMS });
  244. });
  245. }
  246. });
  247. /**
  248. * Handles no data from source errors.
  249. *
  250. * @param {Store} store - The redux store in which the specified action is
  251. * dispatched.
  252. * @param {Action} action - The redux action dispatched in the specified store.
  253. * @private
  254. * @returns {void}
  255. */
  256. function _handleNoDataFromSourceErrors(store, action) {
  257. const { getState, dispatch } = store;
  258. const track = getTrackByJitsiTrack(getState()['features/base/tracks'], action.track.jitsiTrack);
  259. if (!track || !track.local) {
  260. return;
  261. }
  262. const { jitsiTrack } = track;
  263. if (track.mediaType === MEDIA_TYPE.AUDIO && track.isReceivingData) {
  264. _removeNoDataFromSourceNotification(store, action.track);
  265. }
  266. if (track.mediaType === MEDIA_TYPE.VIDEO) {
  267. const { noDataFromSourceNotificationInfo = {} } = track;
  268. if (track.isReceivingData) {
  269. if (noDataFromSourceNotificationInfo.timeout) {
  270. clearTimeout(noDataFromSourceNotificationInfo.timeout);
  271. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  272. }
  273. // try to remove the notification if there is one.
  274. _removeNoDataFromSourceNotification(store, action.track);
  275. } else {
  276. if (noDataFromSourceNotificationInfo.timeout) {
  277. return;
  278. }
  279. const timeout = setTimeout(() => dispatch(showNoDataFromSourceVideoError(jitsiTrack)), 5000);
  280. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, { timeout }));
  281. }
  282. }
  283. }
  284. /**
  285. * Gets the local track associated with a specific {@code MEDIA_TYPE} in a
  286. * specific redux store.
  287. *
  288. * @param {Store} store - The redux store from which the local track associated
  289. * with the specified {@code mediaType} is to be retrieved.
  290. * @param {MEDIA_TYPE} mediaType - The {@code MEDIA_TYPE} of the local track to
  291. * be retrieved from the specified {@code store}.
  292. * @param {boolean} [includePending] - Indicates whether a local track is to be
  293. * returned if it is still pending. A local track is pending if
  294. * {@code getUserMedia} is still executing to create it and, consequently, its
  295. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  296. * track is not returned.
  297. * @private
  298. * @returns {Track} The local {@code Track} associated with the specified
  299. * {@code mediaType} in the specified {@code store}.
  300. */
  301. function _getLocalTrack(
  302. { getState }: { getState: Function },
  303. mediaType: MEDIA_TYPE,
  304. includePending: boolean = false) {
  305. return (
  306. getLocalTrack(
  307. getState()['features/base/tracks'],
  308. mediaType,
  309. includePending));
  310. }
  311. /**
  312. * Removes the no data from source notification associated with the JitsiTrack if displayed.
  313. *
  314. * @param {Store} store - The redux store.
  315. * @param {Track} track - The redux action dispatched in the specified store.
  316. * @returns {void}
  317. */
  318. function _removeNoDataFromSourceNotification({ getState, dispatch }, track) {
  319. const t = getTrackByJitsiTrack(getState()['features/base/tracks'], track.jitsiTrack);
  320. const { jitsiTrack, noDataFromSourceNotificationInfo = {} } = t || {};
  321. if (noDataFromSourceNotificationInfo && noDataFromSourceNotificationInfo.uid) {
  322. dispatch(hideNotification(noDataFromSourceNotificationInfo.uid));
  323. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  324. }
  325. }
  326. /**
  327. * Mutes or unmutes a local track with a specific media type.
  328. *
  329. * @param {Store} store - The redux store in which the specified action is
  330. * dispatched.
  331. * @param {Action} action - The redux action dispatched in the specified store.
  332. * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
  333. * which is being muted or unmuted.
  334. * @private
  335. * @returns {void}
  336. */
  337. async function _setMuted(store, { ensureTrack, authority, muted }, mediaType: MEDIA_TYPE) {
  338. const { dispatch, getState } = store;
  339. const localTrack = _getLocalTrack(store, mediaType, /* includePending */ true);
  340. const state = getState();
  341. if (mediaType === MEDIA_TYPE.SCREENSHARE
  342. && getMultipleVideoSendingSupportFeatureFlag(state)
  343. && !muted) {
  344. return;
  345. }
  346. if (localTrack) {
  347. // The `jitsiTrack` property will have a value only for a localTrack for which `getUserMedia` has already
  348. // completed. If there's no `jitsiTrack`, then the `muted` state will be applied once the `jitsiTrack` is
  349. // created.
  350. const { jitsiTrack } = localTrack;
  351. const isAudioOnly = (mediaType === MEDIA_TYPE.VIDEO && authority === VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY)
  352. || (mediaType === MEDIA_TYPE.SCREENSHARE && authority === SCREENSHARE_MUTISM_AUTHORITY.AUDIO_ONLY);
  353. // Screenshare cannot be unmuted using the video mute button unless it is muted by audioOnly in the legacy
  354. // screensharing mode.
  355. if (jitsiTrack && (
  356. jitsiTrack.videoType !== 'desktop' || isAudioOnly || getMultipleVideoSendingSupportFeatureFlag(state))
  357. ) {
  358. setTrackMuted(jitsiTrack, muted, state).catch(() => dispatch(trackMuteUnmuteFailed(localTrack, muted)));
  359. }
  360. } else if (!muted && ensureTrack && (typeof APP === 'undefined' || isPrejoinPageVisible(state))) {
  361. // FIXME: This only runs on mobile now because web has its own way of
  362. // creating local tracks. Adjust the check once they are unified.
  363. dispatch(createLocalTracksA({ devices: [ mediaType ] }));
  364. }
  365. }