2018-08-08 18:48:23 +00:00
|
|
|
// @flow
|
|
|
|
|
2019-08-12 19:25:06 +00:00
|
|
|
import debounce from 'lodash/debounce';
|
|
|
|
|
2020-07-23 13:12:25 +00:00
|
|
|
import { pinParticipant, getPinnedParticipant } from '../base/participants';
|
2019-04-11 15:53:34 +00:00
|
|
|
import { StateListenerRegistry, equals } from '../base/redux';
|
2020-04-30 21:25:34 +00:00
|
|
|
import { isFollowMeActive } from '../follow-me';
|
2020-09-21 23:54:45 +00:00
|
|
|
import { selectParticipant } from '../large-video/actions';
|
2020-04-30 21:25:34 +00:00
|
|
|
|
2019-04-11 15:53:34 +00:00
|
|
|
import { setParticipantsWithScreenShare } from './actions';
|
|
|
|
|
2019-05-18 20:40:58 +00:00
|
|
|
declare var APP: Object;
|
2019-04-11 15:53:34 +00:00
|
|
|
declare var interfaceConfig: Object;
|
2018-08-08 18:48:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* StateListenerRegistry provides a reliable way of detecting changes to
|
|
|
|
* preferred layout state and dispatching additional actions.
|
|
|
|
*/
|
|
|
|
StateListenerRegistry.register(
|
2020-07-23 13:12:25 +00:00
|
|
|
/* selector */ state => state['features/video-layout'].tileViewEnabled,
|
|
|
|
/* listener */ (tileViewEnabled, store) => {
|
2019-04-11 15:53:34 +00:00
|
|
|
const { dispatch } = store;
|
|
|
|
|
2018-08-08 18:48:23 +00:00
|
|
|
dispatch(selectParticipant());
|
2019-04-11 15:53:34 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For auto-pin mode, listen for changes to the known media tracks and look
|
2019-08-12 19:25:06 +00:00
|
|
|
* for updates to screen shares. The listener is debounced to avoid state
|
|
|
|
* thrashing that might occur, especially when switching in or out of p2p.
|
2019-04-11 15:53:34 +00:00
|
|
|
*/
|
|
|
|
StateListenerRegistry.register(
|
|
|
|
/* selector */ state => state['features/base/tracks'],
|
2019-08-12 19:25:06 +00:00
|
|
|
/* listener */ debounce((tracks, store) => {
|
2020-04-30 21:25:34 +00:00
|
|
|
if (!_getAutoPinSetting() || isFollowMeActive(store)) {
|
2019-04-11 15:53:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-30 21:25:34 +00:00
|
|
|
const oldScreenSharesOrder = store.getState()['features/video-layout'].screenShares || [];
|
2019-04-11 15:53:34 +00:00
|
|
|
const knownSharingParticipantIds = tracks.reduce((acc, track) => {
|
|
|
|
if (track.mediaType === 'video' && track.videoType === 'desktop') {
|
2019-08-22 22:22:09 +00:00
|
|
|
const skipTrack = _getAutoPinSetting() === 'remote-only' && track.local;
|
2019-06-21 21:26:03 +00:00
|
|
|
|
|
|
|
if (!skipTrack) {
|
|
|
|
acc.push(track.participantId);
|
|
|
|
}
|
2019-04-11 15:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// Filter out any participants which are no longer screen sharing
|
|
|
|
// by looping through the known sharing participants and removing any
|
|
|
|
// participant IDs which are no longer sharing.
|
|
|
|
const newScreenSharesOrder = oldScreenSharesOrder.filter(
|
|
|
|
participantId => knownSharingParticipantIds.includes(participantId));
|
|
|
|
|
|
|
|
// Make sure all new sharing participant get added to the end of the
|
|
|
|
// known screen shares.
|
|
|
|
knownSharingParticipantIds.forEach(participantId => {
|
|
|
|
if (!newScreenSharesOrder.includes(participantId)) {
|
|
|
|
newScreenSharesOrder.push(participantId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!equals(oldScreenSharesOrder, newScreenSharesOrder)) {
|
|
|
|
store.dispatch(
|
|
|
|
setParticipantsWithScreenShare(newScreenSharesOrder));
|
|
|
|
|
|
|
|
_updateAutoPinnedParticipant(store);
|
2018-08-08 18:48:23 +00:00
|
|
|
}
|
2019-08-12 19:25:06 +00:00
|
|
|
}, 100));
|
2019-04-11 15:53:34 +00:00
|
|
|
|
2019-08-22 22:22:09 +00:00
|
|
|
/**
|
|
|
|
* A selector for retrieving the current automatic pinning setting.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {string|undefined} The string "remote-only" is returned if only
|
|
|
|
* remote screensharing should be automatically pinned, any other truthy value
|
|
|
|
* means automatically pin all screenshares. Falsy means do not automatically
|
|
|
|
* pin any screenshares.
|
|
|
|
*/
|
|
|
|
function _getAutoPinSetting() {
|
|
|
|
return typeof interfaceConfig === 'object'
|
|
|
|
? interfaceConfig.AUTO_PIN_LATEST_SCREEN_SHARE
|
|
|
|
: 'remote-only';
|
|
|
|
}
|
|
|
|
|
2019-04-11 15:53:34 +00:00
|
|
|
/**
|
|
|
|
* Private helper to automatically pin the latest screen share stream or unpin
|
|
|
|
* if there are no more screen share streams.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function _updateAutoPinnedParticipant({ dispatch, getState }) {
|
|
|
|
const state = getState();
|
|
|
|
const screenShares = state['features/video-layout'].screenShares;
|
|
|
|
|
|
|
|
if (!screenShares) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const latestScreenshareParticipantId
|
|
|
|
= screenShares[screenShares.length - 1];
|
|
|
|
|
2020-07-23 13:12:25 +00:00
|
|
|
const pinned = getPinnedParticipant(getState);
|
|
|
|
|
2019-04-11 15:53:34 +00:00
|
|
|
if (latestScreenshareParticipantId) {
|
|
|
|
dispatch(pinParticipant(latestScreenshareParticipantId));
|
2020-07-23 13:12:25 +00:00
|
|
|
} else if (pinned) {
|
2019-04-11 15:53:34 +00:00
|
|
|
dispatch(pinParticipant(null));
|
|
|
|
}
|
|
|
|
}
|