Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

middleware.web.ts 5.8KB

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