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

actions.web.ts 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* eslint-disable lines-around-comment */
  2. // @ts-expect-error
  3. import { AUDIO_ONLY_SCREEN_SHARE_NO_TRACK } from '../../../../modules/UI/UIErrors';
  4. import { IReduxState, IStore } from '../../app/types';
  5. import { shouldShowModeratedNotification } from '../../av-moderation/functions';
  6. import { setNoiseSuppressionEnabled } from '../../noise-suppression/actions';
  7. import { showNotification } from '../../notifications/actions';
  8. import { NOTIFICATION_TIMEOUT_TYPE } from '../../notifications/constants';
  9. import { stopReceiver } from '../../remote-control/actions';
  10. import { setScreenAudioShareState, setScreenshareAudioTrack } from '../../screen-share/actions';
  11. import { isAudioOnlySharing, isScreenVideoShared } from '../../screen-share/functions';
  12. // @ts-ignore
  13. import { isScreenshotCaptureEnabled, toggleScreenshotCaptureSummary } from '../../screenshot-capture';
  14. // @ts-ignore
  15. import { AudioMixerEffect } from '../../stream-effects/audio-mixer/AudioMixerEffect';
  16. import { getCurrentConference } from '../conference/functions';
  17. import { JitsiTrackErrors, JitsiTrackEvents } from '../lib-jitsi-meet';
  18. import { setScreenshareMuted } from '../media/actions';
  19. import { MEDIA_TYPE, VIDEO_TYPE } from '../media/constants';
  20. /* eslint-enable lines-around-comment */
  21. import {
  22. addLocalTrack,
  23. replaceLocalTrack
  24. } from './actions.any';
  25. import {
  26. createLocalTracksF,
  27. getLocalDesktopTrack,
  28. getLocalJitsiAudioTrack
  29. } from './functions';
  30. import { IShareOptions, IToggleScreenSharingOptions } from './types';
  31. export * from './actions.any';
  32. /**
  33. * Signals that the local participant is ending screensharing or beginning the screensharing flow.
  34. *
  35. * @param {boolean} enabled - The state to toggle screen sharing to.
  36. * @param {boolean} audioOnly - Only share system audio.
  37. * @param {Object} shareOptions - The options to be passed for capturing screenshare.
  38. * @returns {Function}
  39. */
  40. export function toggleScreensharing(
  41. enabled?: boolean,
  42. audioOnly = false,
  43. shareOptions: IShareOptions = {}) {
  44. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  45. // check for A/V Moderation when trying to start screen sharing
  46. if ((enabled || enabled === undefined) && shouldShowModeratedNotification(MEDIA_TYPE.VIDEO, getState())) {
  47. return Promise.reject();
  48. }
  49. return _toggleScreenSharing({
  50. enabled,
  51. audioOnly,
  52. shareOptions
  53. }, {
  54. dispatch,
  55. getState
  56. });
  57. };
  58. }
  59. /**
  60. * Displays a UI notification for screensharing failure based on the error passed.
  61. *
  62. * @private
  63. * @param {Object} error - The error.
  64. * @param {Object} store - The redux store.
  65. * @returns {void}
  66. */
  67. function _handleScreensharingError(
  68. error: Error | AUDIO_ONLY_SCREEN_SHARE_NO_TRACK,
  69. { dispatch }: IStore): void {
  70. if (error.name === JitsiTrackErrors.SCREENSHARING_USER_CANCELED) {
  71. return;
  72. }
  73. let descriptionKey, titleKey;
  74. if (error.name === JitsiTrackErrors.PERMISSION_DENIED) {
  75. descriptionKey = 'dialog.screenSharingPermissionDeniedError';
  76. titleKey = 'dialog.screenSharingFailedTitle';
  77. } else if (error.name === JitsiTrackErrors.CONSTRAINT_FAILED) {
  78. descriptionKey = 'dialog.cameraConstraintFailedError';
  79. titleKey = 'deviceError.cameraError';
  80. } else if (error.name === JitsiTrackErrors.SCREENSHARING_GENERIC_ERROR) {
  81. descriptionKey = 'dialog.screenSharingFailed';
  82. titleKey = 'dialog.screenSharingFailedTitle';
  83. } else if (error === AUDIO_ONLY_SCREEN_SHARE_NO_TRACK) {
  84. descriptionKey = 'notify.screenShareNoAudio';
  85. titleKey = 'notify.screenShareNoAudioTitle';
  86. }
  87. dispatch(showNotification({
  88. titleKey,
  89. descriptionKey
  90. }, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  91. }
  92. /**
  93. * Applies the AudioMixer effect on the local audio track if applicable. If there is no local audio track, the desktop
  94. * audio track is added to the conference.
  95. *
  96. * @private
  97. * @param {JitsiLocalTrack} desktopAudioTrack - The audio track to be added to the conference.
  98. * @param {*} state - The redux state.
  99. * @returns {void}
  100. */
  101. async function _maybeApplyAudioMixerEffect(desktopAudioTrack: any, state: IReduxState): Promise<void> {
  102. const localAudio = getLocalJitsiAudioTrack(state);
  103. const conference = getCurrentConference(state);
  104. if (localAudio) {
  105. // If there is a localAudio stream, mix in the desktop audio stream captured by the screen sharing API.
  106. const mixerEffect = new AudioMixerEffect(desktopAudioTrack);
  107. await localAudio.setEffect(mixerEffect);
  108. } else {
  109. // If no local stream is present ( i.e. no input audio devices) we use the screen share audio
  110. // stream as we would use a regular stream.
  111. await conference.replaceTrack(null, desktopAudioTrack);
  112. }
  113. }
  114. /**
  115. * Toggles screen sharing.
  116. *
  117. * @private
  118. * @param {boolean} enabled - The state to toggle screen sharing to.
  119. * @param {Store} store - The redux store.
  120. * @returns {void}
  121. */
  122. async function _toggleScreenSharing(
  123. {
  124. enabled,
  125. audioOnly = false,
  126. shareOptions = {}
  127. }: IToggleScreenSharingOptions,
  128. store: IStore
  129. ): Promise<void> {
  130. const { dispatch, getState } = store;
  131. const state = getState();
  132. const audioOnlySharing = isAudioOnlySharing(state);
  133. const screenSharing = isScreenVideoShared(state);
  134. const conference = getCurrentConference(state);
  135. const localAudio = getLocalJitsiAudioTrack(state);
  136. const localScreenshare = getLocalDesktopTrack(state['features/base/tracks']);
  137. // Toggle screenshare or audio-only share if the new state is not passed. Happens in the following two cases.
  138. // 1. ShareAudioDialog passes undefined when the user hits continue in the share audio demo modal.
  139. // 2. Toggle screenshare called from the external API.
  140. const enable = audioOnly
  141. ? enabled ?? !audioOnlySharing
  142. : enabled ?? !screenSharing;
  143. const screensharingDetails: { sourceType?: string; } = {};
  144. if (enable) {
  145. let tracks;
  146. // Spot proxy stream.
  147. if (shareOptions.desktopStream) {
  148. tracks = [ shareOptions.desktopStream ];
  149. } else {
  150. const { _desktopSharingSourceDevice } = state['features/base/config'];
  151. if (!shareOptions.desktopSharingSources && _desktopSharingSourceDevice) {
  152. shareOptions.desktopSharingSourceDevice = _desktopSharingSourceDevice;
  153. }
  154. const options = {
  155. devices: [ VIDEO_TYPE.DESKTOP ],
  156. ...shareOptions
  157. };
  158. try {
  159. tracks = await createLocalTracksF(options) as any[];
  160. } catch (error) {
  161. _handleScreensharingError(error as any, store);
  162. throw error;
  163. }
  164. }
  165. const desktopAudioTrack = tracks.find(track => track.getType() === MEDIA_TYPE.AUDIO);
  166. const desktopVideoTrack = tracks.find(track => track.getType() === MEDIA_TYPE.VIDEO);
  167. if (audioOnly) {
  168. // Dispose the desktop track for audio-only screensharing.
  169. desktopVideoTrack.dispose();
  170. if (!desktopAudioTrack) {
  171. _handleScreensharingError(AUDIO_ONLY_SCREEN_SHARE_NO_TRACK, store);
  172. throw new Error(AUDIO_ONLY_SCREEN_SHARE_NO_TRACK);
  173. }
  174. } else if (desktopVideoTrack) {
  175. if (localScreenshare) {
  176. await dispatch(replaceLocalTrack(localScreenshare.jitsiTrack, desktopVideoTrack, conference));
  177. } else {
  178. await dispatch(addLocalTrack(desktopVideoTrack));
  179. }
  180. if (isScreenshotCaptureEnabled(state, false, true)) {
  181. dispatch(toggleScreenshotCaptureSummary(true));
  182. }
  183. screensharingDetails.sourceType = desktopVideoTrack.sourceType;
  184. }
  185. // Apply the AudioMixer effect if there is a local audio track, add the desktop track to the conference
  186. // otherwise without unmuting the microphone.
  187. if (desktopAudioTrack) {
  188. // Noise suppression doesn't work with desktop audio because we can't chain track effects yet, disable it
  189. // first. We need to to wait for the effect to clear first or it might interfere with the audio mixer.
  190. await dispatch(setNoiseSuppressionEnabled(false));
  191. _maybeApplyAudioMixerEffect(desktopAudioTrack, state);
  192. dispatch(setScreenshareAudioTrack(desktopAudioTrack));
  193. // Handle the case where screen share was stopped from the browsers 'screen share in progress' window.
  194. if (audioOnly) {
  195. desktopAudioTrack?.on(
  196. JitsiTrackEvents.LOCAL_TRACK_STOPPED,
  197. () => dispatch(toggleScreensharing(undefined, true)));
  198. }
  199. }
  200. // Show notification about more bandwidth usage in audio-only mode if the user starts screensharing. This
  201. // doesn't apply to audio-only screensharing.
  202. const { enabled: bestPerformanceMode } = state['features/base/audio-only'];
  203. if (bestPerformanceMode && !audioOnly) {
  204. dispatch(showNotification({
  205. titleKey: 'notify.screenSharingAudioOnlyTitle',
  206. descriptionKey: 'notify.screenSharingAudioOnlyDescription'
  207. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  208. }
  209. } else {
  210. const { desktopAudioTrack } = state['features/screen-share'];
  211. dispatch(stopReceiver());
  212. dispatch(toggleScreenshotCaptureSummary(false));
  213. // Mute the desktop track instead of removing it from the conference since we don't want the client to signal
  214. // a source-remove to the remote peer for the screenshare track. Later when screenshare is enabled again, the
  215. // same sender will be re-used without the need for signaling a new ssrc through source-add.
  216. dispatch(setScreenshareMuted(true));
  217. if (desktopAudioTrack) {
  218. if (localAudio) {
  219. localAudio.setEffect(undefined);
  220. } else {
  221. await conference.replaceTrack(desktopAudioTrack, null);
  222. }
  223. desktopAudioTrack.dispose();
  224. dispatch(setScreenshareAudioTrack(null));
  225. }
  226. }
  227. if (audioOnly) {
  228. dispatch(setScreenAudioShareState(enable));
  229. } else {
  230. // Notify the external API.
  231. APP.API.notifyScreenSharingStatusChanged(enable, screensharingDetails);
  232. }
  233. }