Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

middleware.web.ts 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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, logTracksForParticipant
  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. break;
  47. }
  48. const result = next(action);
  49. const participantId = action.track?.participantId;
  50. if (participantId) {
  51. logTracksForParticipant(store.getState()['features/base/tracks'], participantId, 'Track added');
  52. }
  53. return result;
  54. }
  55. case TRACK_NO_DATA_FROM_SOURCE: {
  56. const result = next(action);
  57. _handleNoDataFromSourceErrors(store, action);
  58. return result;
  59. }
  60. case TRACK_REMOVED: {
  61. _removeNoDataFromSourceNotification(store, action.track);
  62. const result = next(action);
  63. const participantId = action.track?.jitsiTrack?.getParticipantId();
  64. if (participantId && !action.track?.jitsiTrack?.isLocal()) {
  65. logTracksForParticipant(store.getState()['features/base/tracks'], participantId, 'Track removed');
  66. }
  67. return result;
  68. }
  69. case TRACK_MUTE_UNMUTE_FAILED: {
  70. const { jitsiTrack } = action.track;
  71. const muted = action.wasMuted;
  72. const isVideoTrack = jitsiTrack.getType() !== MEDIA_TYPE.AUDIO;
  73. if (isVideoTrack && jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP) {
  74. store.dispatch(setScreenshareMuted(!muted));
  75. } else if (isVideoTrack) {
  76. APP.conference.setVideoMuteStatus();
  77. } else {
  78. APP.conference.updateAudioIconEnabled();
  79. }
  80. break;
  81. }
  82. case TRACK_STOPPED: {
  83. const { jitsiTrack } = action.track;
  84. if (jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP) {
  85. store.dispatch(toggleScreensharing(false));
  86. }
  87. break;
  88. }
  89. case TRACK_UPDATED: {
  90. // TODO Remove the following calls to APP.UI once components interested
  91. // in track mute changes are moved into React and/or redux.
  92. const result = next(action);
  93. const state = store.getState();
  94. if (isPrejoinPageVisible(state)) {
  95. return result;
  96. }
  97. const { jitsiTrack } = action.track;
  98. const participantID = jitsiTrack.getParticipantId();
  99. const isVideoTrack = jitsiTrack.type !== MEDIA_TYPE.AUDIO;
  100. const local = jitsiTrack.isLocal();
  101. if (isVideoTrack) {
  102. if (local && !(jitsiTrack.getVideoType() === VIDEO_TYPE.DESKTOP)) {
  103. APP.conference.setVideoMuteStatus();
  104. } else if (!local) {
  105. APP.UI.setVideoMuted(participantID);
  106. }
  107. } else if (local) {
  108. APP.conference.updateAudioIconEnabled();
  109. }
  110. if (typeof action.track?.muted !== 'undefined' && participantID && !local) {
  111. logTracksForParticipant(store.getState()['features/base/tracks'], participantID, 'Track updated');
  112. }
  113. return result;
  114. }
  115. }
  116. return next(action);
  117. });
  118. /**
  119. * Handles no data from source errors.
  120. *
  121. * @param {Store} store - The redux store in which the specified action is
  122. * dispatched.
  123. * @param {Action} action - The redux action dispatched in the specified store.
  124. * @private
  125. * @returns {void}
  126. */
  127. function _handleNoDataFromSourceErrors(store: IStore, action: AnyAction) {
  128. const { getState, dispatch } = store;
  129. const track = getTrackByJitsiTrack(getState()['features/base/tracks'], action.track.jitsiTrack);
  130. if (!track?.local) {
  131. return;
  132. }
  133. const { jitsiTrack } = track;
  134. if (track.mediaType === MEDIA_TYPE.AUDIO && track.isReceivingData) {
  135. _removeNoDataFromSourceNotification(store, action.track);
  136. }
  137. if (track.mediaType === MEDIA_TYPE.VIDEO) {
  138. const { noDataFromSourceNotificationInfo = {} } = track;
  139. if (track.isReceivingData) {
  140. if (noDataFromSourceNotificationInfo.timeout) {
  141. clearTimeout(noDataFromSourceNotificationInfo.timeout);
  142. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  143. }
  144. // try to remove the notification if there is one.
  145. _removeNoDataFromSourceNotification(store, action.track);
  146. } else {
  147. if (noDataFromSourceNotificationInfo.timeout) {
  148. return;
  149. }
  150. const timeout = setTimeout(() => dispatch(showNoDataFromSourceVideoError(jitsiTrack)), 5000);
  151. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, { timeout }));
  152. }
  153. }
  154. }
  155. /**
  156. * Removes the no data from source notification associated with the JitsiTrack if displayed.
  157. *
  158. * @param {Store} store - The redux store.
  159. * @param {Track} track - The redux action dispatched in the specified store.
  160. * @returns {void}
  161. */
  162. function _removeNoDataFromSourceNotification({ getState, dispatch }: IStore, track: ITrack) {
  163. const t = getTrackByJitsiTrack(getState()['features/base/tracks'], track.jitsiTrack);
  164. const { jitsiTrack, noDataFromSourceNotificationInfo = {} } = t || {};
  165. if (noDataFromSourceNotificationInfo?.uid) {
  166. dispatch(hideNotification(noDataFromSourceNotificationInfo.uid));
  167. dispatch(trackNoDataFromSourceNotificationInfoChanged(jitsiTrack, undefined));
  168. }
  169. }