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

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