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 3.9KB

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