@azure/communication-react
Version:
React library for building modern communication user experiences utilizing Azure Communication Services
46 lines • 2.48 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Calculates the participants that should be rendered based on the list of dominant
* speakers and currently rendered participants in a call.
* @param args - SmartDominantSpeakerParticipantsArgs
* @returns VideoGalleryRemoteParticipant[] {@link @azure/communication-react#VideoGalleryRemoteParticipant}
*/
export const smartDominantSpeakerParticipants = (args) => {
const { participants, dominantSpeakers = [], currentParticipants = [], maxDominantSpeakers } = args;
// Don't apply any logic if total number of video streams is less than max dominant speakers.
if (participants.length <= maxDominantSpeakers) {
return participants;
}
const participantsMap = participantsById(participants);
// Only use the Max allowed dominant speakers that exist in participants
const dominantSpeakerIds = dominantSpeakers.filter(id => !!participantsMap[id]).slice(0, maxDominantSpeakers);
const newVisibleParticipantIds = currentParticipants.map(p => p.userId).slice(0, maxDominantSpeakers);
const newDominantSpeakerIds = dominantSpeakerIds.filter(id => !newVisibleParticipantIds.includes(id));
// Remove participants that are no longer dominant and replace them with new dominant speakers.
for (let index = 0; index < maxDominantSpeakers; index++) {
const newVisibleParticipantId = newVisibleParticipantIds[index];
if (newVisibleParticipantId === undefined || !dominantSpeakerIds.includes(newVisibleParticipantId)) {
const replacement = newDominantSpeakerIds.shift();
if (!replacement) {
break;
}
newVisibleParticipantIds[index] = replacement;
}
}
let newVisibleParticipants = newVisibleParticipantIds.map(participantId => participantsMap[participantId]).filter(p => p !== undefined);
const newVisibleParticipantIdsSet = new Set(newVisibleParticipantIds);
const remainingParticipants = participants.filter(p => !newVisibleParticipantIdsSet.has(p.userId));
newVisibleParticipants = newVisibleParticipants.concat(remainingParticipants);
return newVisibleParticipants;
};
/**
* maps the participants array to an object with userId as key
* @private
*/
export const participantsById = (participants) => {
const response = {};
participants.forEach(p => response[p.userId] = p);
return response;
};
//# sourceMappingURL=dominantSpeaker.js.map