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.

subscriber.js 4.0KB

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