您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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, ignoreDidHaveVideo } = action;
  133. APP.UI.emitEvent(UIEvents.TOGGLE_SCREENSHARING, { enabled,
  134. audioOnly,
  135. ignoreDidHaveVideo });
  136. }
  137. break;
  138. case TRACK_UPDATED: {
  139. // TODO Remove the following calls to APP.UI once components interested
  140. // in track mute changes are moved into React and/or redux.
  141. if (typeof APP !== 'undefined') {
  142. const result = next(action);
  143. if (isPrejoinPageVisible(store.getState())) {
  144. return result;
  145. }
  146. const { jitsiTrack } = action.track;
  147. const muted = jitsiTrack.isMuted();
  148. const participantID = jitsiTrack.getParticipantId();
  149. const isVideoTrack = jitsiTrack.type !== MEDIA_TYPE.AUDIO;
  150. if (isVideoTrack) {
  151. // Do not change the video mute state for local presenter tracks.
  152. if (jitsiTrack.type === MEDIA_TYPE.PRESENTER) {
  153. APP.conference.mutePresenter(muted);
  154. } else if (jitsiTrack.isLocal() && !(jitsiTrack.videoType === VIDEO_TYPE.DESKTOP)) {
  155. APP.conference.setVideoMuteStatus();
  156. } else if (jitsiTrack.isLocal() && muted && jitsiTrack.videoType === VIDEO_TYPE.DESKTOP) {
  157. store.dispatch(toggleScreensharing(false, false, true));
  158. } else {
  159. APP.UI.setVideoMuted(participantID);
  160. }
  161. } else if (jitsiTrack.isLocal()) {
  162. APP.conference.setAudioMuteStatus(muted);
  163. } else {
  164. APP.UI.setAudioMuted(participantID, muted);
  165. }
  166. return result;
  167. }
  168. const { jitsiTrack } = action.track;
  169. if (jitsiTrack.isMuted()
  170. && jitsiTrack.type === MEDIA_TYPE.VIDEO && jitsiTrack.videoType === VIDEO_TYPE.DESKTOP) {
  171. store.dispatch(toggleScreensharing(false));
  172. }
  173. break;
  174. }
  175. }
  176. return next(action);
  177. });
  178. /**
  179. * Handles no data from source errors.
  180. *
  181. * @param {Store} store - The redux store in which the specified action is
  182. * dispatched.
  183. * @param {Action} action - The redux action dispatched in the specified store.
  184. * @private
  185. * @returns {void}
  186. */
  187. function _handleNoDataFromSourceErrors(store, action) {
  188. const { getState, dispatch } = store;
  189. const track = getTrackByJitsiTrack(getState()['features/base/tracks'], action.track.jitsiTrack);
  190. if (!track || !track.local) {
  191. return;
  192. }
  193. const { jitsiTrack } = track;
  194. if (track.mediaType === MEDIA_TYPE.AUDIO && track.isReceivingData) {
  195. _removeNoDataFromSourceNotification(store, action.track);
  196. }
  197. if (track.mediaType === MEDIA_TYPE.VIDEO) {
  198. const { noDataFromSourceNotificationInfo = {} } = track;
  199. if (track.isReceivingData) {
  200. if (noDataFromSourceNotificationInfo.timeout) {
  201. clearTimeout(noDataFromSourceNotificationInfo.timeout);
  202. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  203. }
  204. // try to remove the notification if there is one.
  205. _removeNoDataFromSourceNotification(store, action.track);
  206. } else {
  207. if (noDataFromSourceNotificationInfo.timeout) {
  208. return;
  209. }
  210. const timeout = setTimeout(() => dispatch(showNoDataFromSourceVideoError(jitsiTrack)), 5000);
  211. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, { timeout }));
  212. }
  213. }
  214. }
  215. /**
  216. * Gets the local track associated with a specific {@code MEDIA_TYPE} in a
  217. * specific redux store.
  218. *
  219. * @param {Store} store - The redux store from which the local track associated
  220. * with the specified {@code mediaType} is to be retrieved.
  221. * @param {MEDIA_TYPE} mediaType - The {@code MEDIA_TYPE} of the local track to
  222. * be retrieved from the specified {@code store}.
  223. * @param {boolean} [includePending] - Indicates whether a local track is to be
  224. * returned if it is still pending. A local track is pending if
  225. * {@code getUserMedia} is still executing to create it and, consequently, its
  226. * {@code jitsiTrack} property is {@code undefined}. By default a pending local
  227. * track is not returned.
  228. * @private
  229. * @returns {Track} The local {@code Track} associated with the specified
  230. * {@code mediaType} in the specified {@code store}.
  231. */
  232. function _getLocalTrack(
  233. { getState }: { getState: Function },
  234. mediaType: MEDIA_TYPE,
  235. includePending: boolean = false) {
  236. return (
  237. getLocalTrack(
  238. getState()['features/base/tracks'],
  239. mediaType,
  240. includePending));
  241. }
  242. /**
  243. * Removes the no data from source notification associated with the JitsiTrack if displayed.
  244. *
  245. * @param {Store} store - The redux store.
  246. * @param {Track} track - The redux action dispatched in the specified store.
  247. * @returns {void}
  248. */
  249. function _removeNoDataFromSourceNotification({ getState, dispatch }, track) {
  250. const t = getTrackByJitsiTrack(getState()['features/base/tracks'], track.jitsiTrack);
  251. const { jitsiTrack, noDataFromSourceNotificationInfo = {} } = t || {};
  252. if (noDataFromSourceNotificationInfo && noDataFromSourceNotificationInfo.uid) {
  253. dispatch(hideNotification(noDataFromSourceNotificationInfo.uid));
  254. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  255. }
  256. }
  257. /**
  258. * Mutes or unmutes a local track with a specific media type.
  259. *
  260. * @param {Store} store - The redux store in which the specified action is
  261. * dispatched.
  262. * @param {Action} action - The redux action dispatched in the specified store.
  263. * @param {MEDIA_TYPE} mediaType - The {@link MEDIA_TYPE} of the local track
  264. * which is being muted or unmuted.
  265. * @private
  266. * @returns {void}
  267. */
  268. function _setMuted(store, { ensureTrack, authority, muted }, mediaType: MEDIA_TYPE) {
  269. const localTrack
  270. = _getLocalTrack(store, mediaType, /* includePending */ true);
  271. if (localTrack) {
  272. // The `jitsiTrack` property will have a value only for a localTrack for
  273. // which `getUserMedia` has already completed. If there's no
  274. // `jitsiTrack`, then the `muted` state will be applied once the
  275. // `jitsiTrack` is created.
  276. const { jitsiTrack } = localTrack;
  277. const isAudioOnly = authority === VIDEO_MUTISM_AUTHORITY.AUDIO_ONLY;
  278. // screenshare cannot be muted or unmuted using the video mute button
  279. // anymore, unless it is muted by audioOnly.
  280. jitsiTrack && (jitsiTrack.videoType !== 'desktop' || isAudioOnly)
  281. && setTrackMuted(jitsiTrack, muted);
  282. } else if (!muted && ensureTrack && (typeof APP === 'undefined' || isPrejoinPageVisible(store.getState()))) {
  283. // FIXME: This only runs on mobile now because web has its own way of
  284. // creating local tracks. Adjust the check once they are unified.
  285. store.dispatch(createLocalTracksA({ devices: [ mediaType ] }));
  286. }
  287. }