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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // @flow
  2. import debounce from 'lodash/debounce';
  3. import { pinParticipant, getPinnedParticipant } from '../base/participants';
  4. import { StateListenerRegistry, equals } from '../base/redux';
  5. import { isFollowMeActive } from '../follow-me';
  6. import { selectParticipant } from '../large-video/actions';
  7. import { setRemoteParticipantsWithScreenShare } from './actions';
  8. declare var APP: Object;
  9. declare var interfaceConfig: Object;
  10. let latestScreenshareParticipantId;
  11. /**
  12. * StateListenerRegistry provides a reliable way of detecting changes to
  13. * preferred layout state and dispatching additional actions.
  14. */
  15. StateListenerRegistry.register(
  16. /* selector */ state => state['features/video-layout'].tileViewEnabled,
  17. /* listener */ (tileViewEnabled, store) => {
  18. const { dispatch } = store;
  19. dispatch(selectParticipant());
  20. }
  21. );
  22. /**
  23. * For auto-pin mode, listen for changes to the known media tracks and look
  24. * for updates to screen shares. The listener is debounced to avoid state
  25. * thrashing that might occur, especially when switching in or out of p2p.
  26. */
  27. StateListenerRegistry.register(
  28. /* selector */ state => state['features/base/tracks'],
  29. /* listener */ debounce((tracks, store) => {
  30. if (!_getAutoPinSetting() || isFollowMeActive(store)) {
  31. return;
  32. }
  33. const oldScreenSharesOrder = store.getState()['features/video-layout'].remoteScreenShares || [];
  34. const knownSharingParticipantIds = tracks.reduce((acc, track) => {
  35. if (track.mediaType === 'video' && track.videoType === 'desktop') {
  36. const skipTrack = _getAutoPinSetting() === 'remote-only' && track.local;
  37. if (!skipTrack) {
  38. acc.push(track.participantId);
  39. }
  40. }
  41. return acc;
  42. }, []);
  43. // Filter out any participants which are no longer screen sharing
  44. // by looping through the known sharing participants and removing any
  45. // participant IDs which are no longer sharing.
  46. const newScreenSharesOrder = oldScreenSharesOrder.filter(
  47. participantId => knownSharingParticipantIds.includes(participantId));
  48. // Make sure all new sharing participant get added to the end of the
  49. // known screen shares.
  50. knownSharingParticipantIds.forEach(participantId => {
  51. if (!newScreenSharesOrder.includes(participantId)) {
  52. newScreenSharesOrder.push(participantId);
  53. }
  54. });
  55. if (!equals(oldScreenSharesOrder, newScreenSharesOrder)) {
  56. store.dispatch(
  57. setRemoteParticipantsWithScreenShare(newScreenSharesOrder));
  58. _updateAutoPinnedParticipant(store);
  59. }
  60. }, 100));
  61. /**
  62. * A selector for retrieving the current automatic pinning setting.
  63. *
  64. * @private
  65. * @returns {string|undefined} The string "remote-only" is returned if only
  66. * remote screensharing should be automatically pinned, any other truthy value
  67. * means automatically pin all screenshares. Falsy means do not automatically
  68. * pin any screenshares.
  69. */
  70. function _getAutoPinSetting() {
  71. return typeof interfaceConfig === 'object'
  72. ? interfaceConfig.AUTO_PIN_LATEST_SCREEN_SHARE
  73. : 'remote-only';
  74. }
  75. /**
  76. * Private helper to automatically pin the latest screen share stream or unpin
  77. * if there are no more screen share streams.
  78. *
  79. * @param {Store} store - The redux store.
  80. * @returns {void}
  81. */
  82. function _updateAutoPinnedParticipant({ dispatch, getState }) {
  83. const state = getState();
  84. const remoteScreenShares = state['features/video-layout'].remoteScreenShares;
  85. const pinned = getPinnedParticipant(getState);
  86. // Unpin the screenshare when the screensharing participant leaves. Switch to tile view if no other
  87. // participant was pinned before screenshare was auto-pinned, pin the previously pinned participant otherwise.
  88. if (!remoteScreenShares?.length) {
  89. let participantId = null;
  90. if (pinned && pinned.id !== latestScreenshareParticipantId) {
  91. participantId = pinned.id;
  92. }
  93. latestScreenshareParticipantId = null;
  94. dispatch(pinParticipant(participantId));
  95. return;
  96. }
  97. latestScreenshareParticipantId = remoteScreenShares[remoteScreenShares.length - 1];
  98. if (latestScreenshareParticipantId) {
  99. dispatch(pinParticipant(latestScreenshareParticipantId));
  100. }
  101. }