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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.setAudioMuteStatus(!muted);
  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 muted = jitsiTrack.isMuted();
  88. const participantID = jitsiTrack.getParticipantId();
  89. const isVideoTrack = jitsiTrack.type !== MEDIA_TYPE.AUDIO;
  90. if (isVideoTrack) {
  91. if (jitsiTrack.isLocal() && !(jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP)) {
  92. APP.conference.setVideoMuteStatus();
  93. } else if (!jitsiTrack.isLocal()) {
  94. APP.UI.setVideoMuted(participantID);
  95. }
  96. } else if (jitsiTrack.isLocal()) {
  97. APP.conference.setAudioMuteStatus(muted);
  98. } else {
  99. APP.UI.setAudioMuted(participantID, muted);
  100. }
  101. return result;
  102. }
  103. }
  104. return next(action);
  105. });
  106. /**
  107. * Handles no data from source errors.
  108. *
  109. * @param {Store} store - The redux store in which the specified action is
  110. * dispatched.
  111. * @param {Action} action - The redux action dispatched in the specified store.
  112. * @private
  113. * @returns {void}
  114. */
  115. function _handleNoDataFromSourceErrors(store: IStore, action: AnyAction) {
  116. const { getState, dispatch } = store;
  117. const track = getTrackByJitsiTrack(getState()['features/base/tracks'], action.track.jitsiTrack);
  118. if (!track?.local) {
  119. return;
  120. }
  121. const { jitsiTrack } = track;
  122. if (track.mediaType === MEDIA_TYPE.AUDIO && track.isReceivingData) {
  123. _removeNoDataFromSourceNotification(store, action.track);
  124. }
  125. if (track.mediaType === MEDIA_TYPE.VIDEO) {
  126. const { noDataFromSourceNotificationInfo = {} } = track;
  127. if (track.isReceivingData) {
  128. if (noDataFromSourceNotificationInfo.timeout) {
  129. clearTimeout(noDataFromSourceNotificationInfo.timeout);
  130. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  131. }
  132. // try to remove the notification if there is one.
  133. _removeNoDataFromSourceNotification(store, action.track);
  134. } else {
  135. if (noDataFromSourceNotificationInfo.timeout) {
  136. return;
  137. }
  138. const timeout = setTimeout(() => dispatch(showNoDataFromSourceVideoError(jitsiTrack)), 5000);
  139. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, { timeout }));
  140. }
  141. }
  142. }
  143. /**
  144. * Removes the no data from source notification associated with the JitsiTrack if displayed.
  145. *
  146. * @param {Store} store - The redux store.
  147. * @param {Track} track - The redux action dispatched in the specified store.
  148. * @returns {void}
  149. */
  150. function _removeNoDataFromSourceNotification({ getState, dispatch }: IStore, track: ITrack) {
  151. const t = getTrackByJitsiTrack(getState()['features/base/tracks'], track.jitsiTrack);
  152. const { jitsiTrack, noDataFromSourceNotificationInfo = {} } = t || {};
  153. if (noDataFromSourceNotificationInfo?.uid) {
  154. dispatch(hideNotification(noDataFromSourceNotificationInfo.uid));
  155. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  156. }
  157. }