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.web.ts 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { AnyAction } from 'redux';
  2. import { IStore } from '../../app/types';
  3. import { hideNotification } from '../../notifications/actions';
  4. import { isPrejoinPageVisible } from '../../prejoin/functions';
  5. import { getAvailableDevices } from '../devices/actions.web';
  6. import { setScreenshareMuted } from '../media/actions';
  7. import {
  8. MEDIA_TYPE,
  9. VIDEO_TYPE
  10. } from '../media/constants';
  11. import MiddlewareRegistry from '../redux/MiddlewareRegistry';
  12. import {
  13. TRACK_ADDED,
  14. TRACK_MUTE_UNMUTE_FAILED,
  15. TRACK_NO_DATA_FROM_SOURCE,
  16. TRACK_REMOVED,
  17. TRACK_STOPPED,
  18. TRACK_UPDATED
  19. } from './actionTypes';
  20. import {
  21. showNoDataFromSourceVideoError,
  22. toggleScreensharing,
  23. trackNoDataFromSourceNotificationInfoChanged
  24. } from './actions.web';
  25. import {
  26. getTrackByJitsiTrack
  27. } from './functions.web';
  28. import { ITrack } from './types';
  29. import './middleware.any';
  30. /**
  31. * Middleware that captures LIB_DID_DISPOSE and LIB_DID_INIT actions and,
  32. * respectively, creates/destroys local media tracks. Also listens to
  33. * media-related actions and performs corresponding operations with tracks.
  34. *
  35. * @param {Store} store - The redux store.
  36. * @returns {Function}
  37. */
  38. MiddlewareRegistry.register(store => next => action => {
  39. switch (action.type) {
  40. case TRACK_ADDED: {
  41. const { local } = action.track;
  42. // The devices list needs to be refreshed when no initial video permissions
  43. // were granted and a local video track is added by umuting the video.
  44. if (local) {
  45. store.dispatch(getAvailableDevices());
  46. }
  47. break;
  48. }
  49. case TRACK_NO_DATA_FROM_SOURCE: {
  50. const result = next(action);
  51. _handleNoDataFromSourceErrors(store, action);
  52. return result;
  53. }
  54. case TRACK_REMOVED: {
  55. _removeNoDataFromSourceNotification(store, action.track);
  56. break;
  57. }
  58. case TRACK_MUTE_UNMUTE_FAILED: {
  59. const { jitsiTrack } = action.track;
  60. const muted = action.wasMuted;
  61. const isVideoTrack = jitsiTrack.getType() !== MEDIA_TYPE.AUDIO;
  62. if (isVideoTrack && jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP) {
  63. store.dispatch(setScreenshareMuted(!muted));
  64. } else if (isVideoTrack) {
  65. APP.conference.setVideoMuteStatus();
  66. } else {
  67. APP.conference.updateAudioIconEnabled();
  68. }
  69. break;
  70. }
  71. case TRACK_STOPPED: {
  72. const { jitsiTrack } = action.track;
  73. if (jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP) {
  74. store.dispatch(toggleScreensharing(false));
  75. }
  76. break;
  77. }
  78. case TRACK_UPDATED: {
  79. // TODO Remove the following calls to APP.UI once components interested
  80. // in track mute changes are moved into React and/or redux.
  81. const result = next(action);
  82. const state = store.getState();
  83. if (isPrejoinPageVisible(state)) {
  84. return result;
  85. }
  86. const { jitsiTrack } = action.track;
  87. const participantID = jitsiTrack.getParticipantId();
  88. const isVideoTrack = jitsiTrack.type !== MEDIA_TYPE.AUDIO;
  89. if (isVideoTrack) {
  90. if (jitsiTrack.isLocal() && !(jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP)) {
  91. APP.conference.setVideoMuteStatus();
  92. } else if (!jitsiTrack.isLocal()) {
  93. APP.UI.setVideoMuted(participantID);
  94. }
  95. } else if (jitsiTrack.isLocal()) {
  96. APP.conference.updateAudioIconEnabled();
  97. }
  98. return result;
  99. }
  100. }
  101. return next(action);
  102. });
  103. /**
  104. * Handles no data from source errors.
  105. *
  106. * @param {Store} store - The redux store in which the specified action is
  107. * dispatched.
  108. * @param {Action} action - The redux action dispatched in the specified store.
  109. * @private
  110. * @returns {void}
  111. */
  112. function _handleNoDataFromSourceErrors(store: IStore, action: AnyAction) {
  113. const { getState, dispatch } = store;
  114. const track = getTrackByJitsiTrack(getState()['features/base/tracks'], action.track.jitsiTrack);
  115. if (!track?.local) {
  116. return;
  117. }
  118. const { jitsiTrack } = track;
  119. if (track.mediaType === MEDIA_TYPE.AUDIO && track.isReceivingData) {
  120. _removeNoDataFromSourceNotification(store, action.track);
  121. }
  122. if (track.mediaType === MEDIA_TYPE.VIDEO) {
  123. const { noDataFromSourceNotificationInfo = {} } = track;
  124. if (track.isReceivingData) {
  125. if (noDataFromSourceNotificationInfo.timeout) {
  126. clearTimeout(noDataFromSourceNotificationInfo.timeout);
  127. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  128. }
  129. // try to remove the notification if there is one.
  130. _removeNoDataFromSourceNotification(store, action.track);
  131. } else {
  132. if (noDataFromSourceNotificationInfo.timeout) {
  133. return;
  134. }
  135. const timeout = setTimeout(() => dispatch(showNoDataFromSourceVideoError(jitsiTrack)), 5000);
  136. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, { timeout }));
  137. }
  138. }
  139. }
  140. /**
  141. * Removes the no data from source notification associated with the JitsiTrack if displayed.
  142. *
  143. * @param {Store} store - The redux store.
  144. * @param {Track} track - The redux action dispatched in the specified store.
  145. * @returns {void}
  146. */
  147. function _removeNoDataFromSourceNotification({ getState, dispatch }: IStore, track: ITrack) {
  148. const t = getTrackByJitsiTrack(getState()['features/base/tracks'], track.jitsiTrack);
  149. const { jitsiTrack, noDataFromSourceNotificationInfo = {} } = t || {};
  150. if (noDataFromSourceNotificationInfo?.uid) {
  151. dispatch(hideNotification(noDataFromSourceNotificationInfo.uid));
  152. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  153. }
  154. }