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.native.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // @flow
  2. import { setPictureInPictureDisabled } from '../../mobile/picture-in-picture/functions';
  3. import { setAudioOnly } from '../audio-only';
  4. import JitsiMeetJS from '../lib-jitsi-meet';
  5. import { MiddlewareRegistry } from '../redux';
  6. import { TOGGLE_SCREENSHARING } from '../tracks/actionTypes';
  7. import { destroyLocalDesktopTrackIfExists, replaceLocalTrack } from '../tracks/actions';
  8. import { getLocalVideoTrack, isLocalVideoTrackDesktop } from '../tracks/functions';
  9. import './middleware.any';
  10. MiddlewareRegistry.register(store => next => action => {
  11. switch (action.type) {
  12. case TOGGLE_SCREENSHARING: {
  13. _toggleScreenSharing(store);
  14. break;
  15. }
  16. }
  17. return next(action);
  18. });
  19. /**
  20. * Toggles screen sharing.
  21. *
  22. * @private
  23. * @param {Store} store - The redux.
  24. * @returns {void}
  25. */
  26. function _toggleScreenSharing(store) {
  27. const { dispatch, getState } = store;
  28. const state = getState();
  29. const isSharing = isLocalVideoTrackDesktop(state);
  30. if (isSharing) {
  31. dispatch(destroyLocalDesktopTrackIfExists());
  32. } else {
  33. _startScreenSharing(dispatch, state);
  34. }
  35. }
  36. /**
  37. * Creates desktop track and replaces the local one.
  38. *
  39. * @private
  40. * @param {Dispatch} dispatch - The redux {@code dispatch} function.
  41. * @param {Object} state - The redux state.
  42. * @returns {void}
  43. */
  44. function _startScreenSharing(dispatch, state) {
  45. setPictureInPictureDisabled(true);
  46. JitsiMeetJS.createLocalTracks({ devices: [ 'desktop' ] })
  47. .then(tracks => {
  48. const track = tracks[0];
  49. const currentLocalTrack = getLocalVideoTrack(state['features/base/tracks']);
  50. const currentJitsiTrack = currentLocalTrack && currentLocalTrack.jitsiTrack;
  51. dispatch(replaceLocalTrack(currentJitsiTrack, track));
  52. const { enabled: audioOnly } = state['features/base/audio-only'];
  53. if (audioOnly) {
  54. dispatch(setAudioOnly(false));
  55. }
  56. })
  57. .catch(error => {
  58. console.log('ERROR creating ScreeSharing stream ', error);
  59. setPictureInPictureDisabled(false);
  60. });
  61. }