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

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