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

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