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

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