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

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