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

middleware.web.ts 7.1KB

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