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

middleware.js 11KB

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