2024-05-02 03:48:12 +08:00
|
|
|
import { useCallback, useMemo } from 'react';
|
|
|
|
import {
|
|
|
|
useSubscription,
|
|
|
|
useReactiveVar,
|
|
|
|
useLazyQuery,
|
|
|
|
useMutation,
|
|
|
|
} from '@apollo/client';
|
2024-04-20 04:34:43 +08:00
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import Auth from '/imports/ui/services/auth';
|
|
|
|
import useCurrentUser from '/imports/ui/core/hooks/useCurrentUser';
|
|
|
|
import useMeeting from '/imports/ui/core/hooks/useMeeting';
|
|
|
|
import { partition } from '/imports/utils/array-utils';
|
|
|
|
import { USER_AGGREGATE_COUNT_SUBSCRIPTION } from '/imports/ui/core/graphql/queries/users';
|
|
|
|
import {
|
|
|
|
getSortingMethod,
|
|
|
|
sortVideoStreams,
|
2024-05-02 03:48:12 +08:00
|
|
|
} from '/imports/ui/components/video-provider/video-provider-graphql/stream-sorting';
|
2024-04-20 04:34:43 +08:00
|
|
|
import {
|
|
|
|
useVideoState,
|
|
|
|
setVideoState,
|
|
|
|
useConnectingStream,
|
|
|
|
streams,
|
|
|
|
getVideoState,
|
|
|
|
} from '../state';
|
|
|
|
import {
|
|
|
|
OWN_VIDEO_STREAMS_QUERY,
|
|
|
|
VIDEO_STREAMS_USERS_SUBSCRIPTION,
|
|
|
|
VIEWERS_IN_WEBCAM_COUNT_SUBSCRIPTION,
|
|
|
|
VideoStreamsUsersResponse,
|
|
|
|
} from '../queries';
|
|
|
|
import videoService from '../service';
|
|
|
|
import { CAMERA_BROADCAST_STOP } from '../mutations';
|
2024-05-02 03:48:12 +08:00
|
|
|
import { GridItem, StreamItem } from '../types';
|
|
|
|
import { DesktopPageSizes, MobilePageSizes } from '/imports/ui/Types/meetingClientSettings';
|
2024-04-20 04:34:43 +08:00
|
|
|
|
|
|
|
const ROLE_MODERATOR = window.meetingClientSettings.public.user.role_moderator;
|
|
|
|
const ROLE_VIEWER = window.meetingClientSettings.public.user.role_viewer;
|
|
|
|
const {
|
|
|
|
desktopPageSizes: DESKTOP_PAGE_SIZES,
|
|
|
|
mobilePageSizes: MOBILE_PAGE_SIZES,
|
|
|
|
desktopGridSizes: DESKTOP_GRID_SIZES,
|
|
|
|
mobileGridSizes: MOBILE_GRID_SIZES,
|
|
|
|
} = window.meetingClientSettings.public.kurento.pagination;
|
|
|
|
const PAGINATION_THRESHOLDS_CONF = window.meetingClientSettings.public.kurento.paginationThresholds;
|
|
|
|
const PAGINATION_THRESHOLDS = PAGINATION_THRESHOLDS_CONF.thresholds.sort(
|
|
|
|
(t1, t2) => t1.users - t2.users,
|
|
|
|
);
|
|
|
|
const PAGINATION_THRESHOLDS_ENABLED = PAGINATION_THRESHOLDS_CONF.enabled;
|
|
|
|
const {
|
|
|
|
paginationSorting: PAGINATION_SORTING,
|
|
|
|
defaultSorting: DEFAULT_SORTING,
|
|
|
|
} = window.meetingClientSettings.public.kurento.cameraSortingModes;
|
|
|
|
|
2024-05-02 03:48:12 +08:00
|
|
|
const FILTER_VIDEO_STATS = [
|
|
|
|
'outbound-rtp',
|
|
|
|
'inbound-rtp',
|
|
|
|
];
|
2024-04-20 04:34:43 +08:00
|
|
|
|
|
|
|
export const useStatus = () => {
|
|
|
|
const videoState = useVideoState()[0];
|
|
|
|
if (videoState.isConnecting) return 'videoConnecting';
|
|
|
|
if (videoState.isConnected) return 'connected';
|
|
|
|
return 'disconnected';
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useDisableReason = () => {
|
|
|
|
const videoLocked = useIsUserLocked();
|
|
|
|
const hasCapReached = useHasCapReached();
|
|
|
|
const hasVideoStream = useHasVideoStream();
|
|
|
|
const locks = {
|
|
|
|
videoLocked,
|
|
|
|
camCapReached: hasCapReached && !hasVideoStream,
|
|
|
|
meteorDisconnected: !Meteor.status().connected,
|
|
|
|
};
|
|
|
|
const locksKeys = Object.keys(locks);
|
|
|
|
const disableReason = locksKeys
|
|
|
|
.filter((i) => locks[i as keyof typeof locks])
|
|
|
|
.shift();
|
|
|
|
return disableReason;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useIsUserLocked = () => {
|
|
|
|
const disableCam = useDisableCam();
|
|
|
|
const { data: currentUser } = useCurrentUser((u) => ({
|
|
|
|
locked: u.locked,
|
|
|
|
isModerator: u.isModerator,
|
|
|
|
}));
|
2024-05-02 03:30:29 +08:00
|
|
|
return !!currentUser?.locked && !currentUser.isModerator && disableCam;
|
2024-04-20 04:34:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const useVideoStreamsCount = () => {
|
|
|
|
const { streams } = useStreams();
|
|
|
|
|
|
|
|
return streams.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useLocalVideoStreamsCount = () => {
|
|
|
|
const { streams } = useStreams();
|
|
|
|
const localStreams = streams.filter((vs) => vs.userId === Auth.userID);
|
|
|
|
|
|
|
|
return localStreams.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useInfo = () => {
|
|
|
|
const { data } = useMeeting((m) => ({
|
|
|
|
voiceSettings: {
|
2024-05-02 03:48:12 +08:00
|
|
|
// @ts-expect-error -> There seems to be a design issue on the projection portion.
|
2024-04-20 04:34:43 +08:00
|
|
|
voiceConf: m.voiceSettings?.voiceConf,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
const voiceBridge = data?.voiceSettings ? data.voiceSettings.voiceConf : null;
|
|
|
|
return {
|
|
|
|
userId: Auth.userID,
|
|
|
|
userName: Auth.fullname,
|
|
|
|
meetingId: Auth.meetingID,
|
|
|
|
sessionToken: Auth.sessionToken,
|
|
|
|
voiceBridge,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useHasCapReached = () => {
|
|
|
|
const { data: meeting } = useMeeting((m) => ({
|
|
|
|
meetingCameraCap: m.meetingCameraCap,
|
|
|
|
usersPolicies: {
|
2024-05-02 03:48:12 +08:00
|
|
|
// @ts-expect-error -> There seems to be a design issue on the projection portion.
|
2024-04-20 04:34:43 +08:00
|
|
|
userCameraCap: m.usersPolicies?.userCameraCap,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
const videoStreamsCount = useVideoStreamsCount();
|
|
|
|
const localVideoStreamsCount = useLocalVideoStreamsCount();
|
|
|
|
|
|
|
|
// If the meeting prop data is unreachable, force a safe return
|
|
|
|
if (
|
|
|
|
meeting?.usersPolicies === undefined
|
|
|
|
|| !meeting?.meetingCameraCap === undefined
|
|
|
|
) return true;
|
|
|
|
const { meetingCameraCap } = meeting;
|
|
|
|
const { userCameraCap } = meeting.usersPolicies;
|
|
|
|
|
2024-05-02 03:48:12 +08:00
|
|
|
// @ts-expect-error -> There seems to be a design issue on the projection portion.
|
2024-04-20 04:34:43 +08:00
|
|
|
const meetingCap = meetingCameraCap !== 0 && videoStreamsCount >= meetingCameraCap;
|
|
|
|
const userCap = userCameraCap !== 0 && localVideoStreamsCount >= userCameraCap;
|
|
|
|
|
|
|
|
return meetingCap || userCap;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useDisableCam = () => {
|
|
|
|
const { data: meeting } = useMeeting((m) => ({
|
|
|
|
lockSettings: {
|
2024-05-02 03:48:12 +08:00
|
|
|
// @ts-expect-error -> There seems to be a design issue on the projection portion.
|
2024-04-20 04:34:43 +08:00
|
|
|
disableCam: m.lockSettings?.disableCam,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
return meeting?.lockSettings ? meeting?.lockSettings.disableCam : false;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const usePageSizeDictionary = () => {
|
|
|
|
const { data: countData } = useSubscription(
|
|
|
|
USER_AGGREGATE_COUNT_SUBSCRIPTION,
|
|
|
|
);
|
|
|
|
const userCount = countData?.user_aggregate?.aggregate?.count || 0;
|
|
|
|
// Dynamic page sizes are disabled. Fetch the stock page sizes.
|
|
|
|
if (!PAGINATION_THRESHOLDS_ENABLED || PAGINATION_THRESHOLDS.length <= 0) {
|
|
|
|
return !videoService.isMobile ? DESKTOP_PAGE_SIZES : MOBILE_PAGE_SIZES;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dynamic page sizes are enabled. Get the user count, isolate the
|
|
|
|
// matching threshold entry, return the val.
|
|
|
|
let targetThreshold;
|
|
|
|
const processThreshold = (
|
2024-05-02 03:48:12 +08:00
|
|
|
threshold: {
|
|
|
|
desktopPageSizes?: DesktopPageSizes,
|
|
|
|
mobilePageSizes?: MobilePageSizes,
|
|
|
|
} = {
|
2024-04-20 04:34:43 +08:00
|
|
|
desktopPageSizes: DESKTOP_PAGE_SIZES,
|
|
|
|
mobilePageSizes: MOBILE_PAGE_SIZES,
|
|
|
|
},
|
|
|
|
) => {
|
|
|
|
// We don't demand that all page sizes should be set in pagination profiles.
|
|
|
|
// That saves us some space because don't necessarily need to scale mobile
|
|
|
|
// endpoints.
|
|
|
|
// If eg mobile isn't set, then return the default value.
|
|
|
|
if (!videoService.isMobile) {
|
|
|
|
return threshold.desktopPageSizes || DESKTOP_PAGE_SIZES;
|
|
|
|
}
|
|
|
|
return threshold.mobilePageSizes || MOBILE_PAGE_SIZES;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Short-circuit: no threshold yet, return stock values (processThreshold has a default arg)
|
|
|
|
if (userCount < PAGINATION_THRESHOLDS[0].users) return processThreshold();
|
|
|
|
|
|
|
|
// Reverse search for the threshold where our participant count is directly equal or great
|
|
|
|
// The PAGINATION_THRESHOLDS config is sorted when imported.
|
|
|
|
for (
|
|
|
|
let mapIndex = PAGINATION_THRESHOLDS.length - 1;
|
|
|
|
mapIndex >= 0;
|
|
|
|
mapIndex -= 1
|
|
|
|
) {
|
|
|
|
targetThreshold = PAGINATION_THRESHOLDS[mapIndex];
|
|
|
|
if (targetThreshold.users <= userCount) {
|
|
|
|
return processThreshold(targetThreshold);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useMyRole = () => {
|
|
|
|
const { data } = useCurrentUser((u) => ({ role: u.role }));
|
|
|
|
return data?.role;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useMyPageSize = () => {
|
|
|
|
const myRole = useMyRole();
|
|
|
|
const pageSizes = usePageSizeDictionary();
|
|
|
|
let size;
|
|
|
|
switch (myRole) {
|
|
|
|
case ROLE_MODERATOR:
|
2024-05-02 03:48:12 +08:00
|
|
|
size = pageSizes?.moderator;
|
2024-04-20 04:34:43 +08:00
|
|
|
break;
|
|
|
|
case ROLE_VIEWER:
|
|
|
|
default:
|
2024-05-02 03:48:12 +08:00
|
|
|
size = pageSizes?.viewer;
|
2024-04-20 04:34:43 +08:00
|
|
|
}
|
|
|
|
|
2024-05-02 03:48:12 +08:00
|
|
|
return size ?? 0;
|
2024-04-20 04:34:43 +08:00
|
|
|
};
|
|
|
|
|
2024-05-02 03:48:12 +08:00
|
|
|
export const useIsPaginationEnabled = (paginationEnabled: boolean) => paginationEnabled && useMyPageSize() > 0;
|
2024-04-20 04:34:43 +08:00
|
|
|
|
|
|
|
export const useStreams = () => {
|
|
|
|
const videoStreams = useReactiveVar(streams);
|
|
|
|
return { streams: videoStreams };
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useStreamUsers = () => {
|
|
|
|
const { data, loading, error } = useSubscription<VideoStreamsUsersResponse>(
|
|
|
|
VIDEO_STREAMS_USERS_SUBSCRIPTION,
|
|
|
|
);
|
|
|
|
const users = useMemo(
|
|
|
|
() => (data
|
|
|
|
? data.user.map((user) => ({
|
|
|
|
...user,
|
|
|
|
pin: user.pinned,
|
|
|
|
sortName: user.nameSortable,
|
|
|
|
}))
|
|
|
|
: []),
|
|
|
|
[data],
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
2024-05-02 03:48:12 +08:00
|
|
|
users,
|
2024-04-20 04:34:43 +08:00
|
|
|
loading,
|
|
|
|
error,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useSharedDevices = () => {
|
|
|
|
const { streams } = useStreams();
|
|
|
|
const devices = streams
|
|
|
|
.filter((s) => s.userId === Auth.userID)
|
|
|
|
.map((vs) => vs.deviceId);
|
|
|
|
|
|
|
|
return devices;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useNumberOfPages = () => {
|
|
|
|
const state = useVideoState()[0];
|
|
|
|
return state.numberOfPages;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useCurrentVideoPageIndex = () => {
|
|
|
|
const state = useVideoState()[0];
|
|
|
|
return state.currentVideoPageIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useGridSize = () => {
|
|
|
|
let size;
|
|
|
|
const myRole = useMyRole();
|
|
|
|
const gridSizes = !videoService.isMobile
|
|
|
|
? DESKTOP_GRID_SIZES
|
|
|
|
: MOBILE_GRID_SIZES;
|
|
|
|
|
|
|
|
switch (myRole) {
|
|
|
|
case ROLE_MODERATOR:
|
|
|
|
size = gridSizes.moderator;
|
|
|
|
break;
|
|
|
|
case ROLE_VIEWER:
|
|
|
|
default:
|
|
|
|
size = gridSizes.viewer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useVideoStreams = (
|
|
|
|
isGridEnabled: boolean,
|
|
|
|
paginationEnabled: boolean,
|
|
|
|
viewParticipantsWebcams: boolean,
|
|
|
|
) => {
|
|
|
|
const [state] = useVideoState();
|
|
|
|
const { currentVideoPageIndex, numberOfPages } = state;
|
|
|
|
const { users } = useStreamUsers();
|
2024-05-02 03:48:12 +08:00
|
|
|
const { streams: videoStreams } = useStreams();
|
2024-04-20 04:34:43 +08:00
|
|
|
const connectingStream = useConnectingStream(videoStreams);
|
|
|
|
const gridSize = useGridSize();
|
|
|
|
const myPageSize = useMyPageSize();
|
|
|
|
const isPaginationEnabled = useIsPaginationEnabled(paginationEnabled);
|
2024-05-02 03:48:12 +08:00
|
|
|
let streams: StreamItem[] = [...videoStreams];
|
|
|
|
let gridUsers: GridItem[] = [];
|
2024-04-20 04:34:43 +08:00
|
|
|
|
|
|
|
if (connectingStream) streams.push(connectingStream);
|
|
|
|
|
|
|
|
if (!viewParticipantsWebcams) {
|
|
|
|
streams = streams.filter((stream) => stream.userId === Auth.userID);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isPaginationEnabled) {
|
2024-05-02 03:48:12 +08:00
|
|
|
const [filtered, others] = partition(
|
|
|
|
streams,
|
|
|
|
(vs: StreamItem) => Auth.userID === vs.userId || (vs.type === 'stream' && vs.pin),
|
|
|
|
);
|
|
|
|
const [pin, mine] = partition(
|
|
|
|
filtered,
|
|
|
|
(vs: StreamItem) => vs.type === 'stream' && vs.pin,
|
|
|
|
);
|
2024-04-20 04:34:43 +08:00
|
|
|
|
|
|
|
if (myPageSize !== 0) {
|
|
|
|
const total = others.length ?? 0;
|
|
|
|
const nOfPages = Math.ceil(total / myPageSize);
|
|
|
|
|
|
|
|
if (nOfPages !== numberOfPages) {
|
|
|
|
setVideoState((curr) => ({
|
|
|
|
...curr,
|
|
|
|
numberOfPages: nOfPages,
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (nOfPages === 0) {
|
|
|
|
setVideoState((curr) => ({
|
|
|
|
...curr,
|
|
|
|
currentVideoPageIndex: 0,
|
|
|
|
}));
|
|
|
|
} else if (currentVideoPageIndex + 1 > nOfPages) {
|
|
|
|
videoService.getPreviousVideoPage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const chunkIndex = currentVideoPageIndex * myPageSize;
|
|
|
|
|
|
|
|
const sortingMethod = (numberOfPages > 1) ? PAGINATION_SORTING : DEFAULT_SORTING;
|
|
|
|
const paginatedStreams = sortVideoStreams(others, sortingMethod)
|
|
|
|
.slice(chunkIndex, (chunkIndex + myPageSize)) || [];
|
|
|
|
|
|
|
|
if (getSortingMethod(sortingMethod).localFirst) {
|
|
|
|
streams = [...pin, ...mine, ...paginatedStreams];
|
|
|
|
} else {
|
|
|
|
streams = [...pin, ...paginatedStreams, ...mine];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
streams = sortVideoStreams(streams, DEFAULT_SORTING);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isGridEnabled) {
|
|
|
|
const streamUsers = streams.map((stream) => stream.userId);
|
|
|
|
|
|
|
|
gridUsers = users
|
|
|
|
.filter(
|
|
|
|
(user) => !user.loggedOut && !user.left && !streamUsers.includes(user.userId),
|
|
|
|
)
|
|
|
|
.map((user) => ({
|
|
|
|
isGridItem: true,
|
2024-05-02 01:14:10 +08:00
|
|
|
type: 'grid' as const,
|
2024-04-20 04:34:43 +08:00
|
|
|
...user,
|
|
|
|
}))
|
|
|
|
.slice(0, gridSize - streams.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
streams,
|
|
|
|
gridUsers,
|
|
|
|
totalNumberOfStreams: streams.length,
|
|
|
|
users,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useHasVideoStream = () => {
|
|
|
|
const { streams } = useStreams();
|
|
|
|
return streams.some((s) => s.userId === Auth.userID);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useExitVideo = () => {
|
|
|
|
const [cameraBroadcastStop] = useMutation(CAMERA_BROADCAST_STOP);
|
|
|
|
const [getOwnVideoStreams] = useLazyQuery(OWN_VIDEO_STREAMS_QUERY, { variables: { userId: Auth.userID } });
|
|
|
|
|
|
|
|
const exitVideo = useCallback(() => {
|
|
|
|
const { isConnected } = getVideoState();
|
|
|
|
|
|
|
|
if (isConnected) {
|
|
|
|
const sendUserUnshareWebcam = (cameraId: string) => {
|
|
|
|
cameraBroadcastStop({ variables: { cameraId } });
|
|
|
|
};
|
|
|
|
|
|
|
|
getOwnVideoStreams().then(({ data }) => {
|
|
|
|
if (!data) return;
|
|
|
|
const streams = data.user_camera || [];
|
2024-05-02 03:48:12 +08:00
|
|
|
streams.forEach((s: { streamId: string }) => sendUserUnshareWebcam(s.streamId));
|
2024-04-20 04:34:43 +08:00
|
|
|
videoService.exitedVideo();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [cameraBroadcastStop]);
|
|
|
|
|
|
|
|
return exitVideo;
|
|
|
|
};
|
|
|
|
|
2024-05-02 03:48:12 +08:00
|
|
|
export const useViewersInWebcamCount = (): number => {
|
2024-04-20 04:34:43 +08:00
|
|
|
const { data } = useSubscription(VIEWERS_IN_WEBCAM_COUNT_SUBSCRIPTION);
|
|
|
|
return data?.user_camera_aggregate?.aggregate?.count || 0;
|
|
|
|
};
|
2024-05-02 03:48:12 +08:00
|
|
|
|
|
|
|
export const useLockUser = () => {
|
|
|
|
const exitVideo = useExitVideo();
|
|
|
|
return useCallback(() => {
|
|
|
|
const { isConnected } = getVideoState();
|
|
|
|
if (isConnected) {
|
|
|
|
exitVideo();
|
|
|
|
}
|
|
|
|
}, [exitVideo]);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useStopVideo = () => {
|
|
|
|
const { streams } = useStreams();
|
|
|
|
const filteredStreams = useMemo(() => streams.filter((s) => s.userId === Auth.userID), [streams]);
|
|
|
|
const [cameraBroadcastStop] = useMutation(CAMERA_BROADCAST_STOP);
|
|
|
|
|
|
|
|
return useCallback((cameraId?: string) => {
|
|
|
|
const hasTargetStream = filteredStreams.some((s) => s.stream === cameraId);
|
|
|
|
const hasOtherStream = filteredStreams.some((s) => s.stream !== cameraId);
|
|
|
|
|
|
|
|
if (hasTargetStream) {
|
|
|
|
cameraBroadcastStop({ variables: { cameraId } });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasOtherStream) {
|
|
|
|
videoService.exitedVideo();
|
|
|
|
} else {
|
|
|
|
videoService.stopConnectingStream();
|
|
|
|
}
|
|
|
|
}, [filteredStreams]);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useActivePeers = (
|
|
|
|
isGridEnabled: boolean,
|
|
|
|
paginationEnabled: boolean,
|
|
|
|
viewParticipantsWebcams: boolean,
|
|
|
|
) => {
|
|
|
|
const videoData = useVideoStreams(
|
|
|
|
isGridEnabled,
|
|
|
|
paginationEnabled,
|
|
|
|
viewParticipantsWebcams,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!videoData) return null;
|
|
|
|
|
|
|
|
const { streams: activeVideoStreams } = videoData;
|
|
|
|
|
|
|
|
if (!activeVideoStreams) return null;
|
|
|
|
|
|
|
|
const activePeers: Record<string, RTCPeerConnection> = {};
|
|
|
|
|
|
|
|
activeVideoStreams.forEach((stream) => {
|
|
|
|
if (videoService.webRtcPeersRef()[stream.stream]) {
|
|
|
|
activePeers[stream.stream] = videoService.webRtcPeersRef()[stream.stream].peerConnection;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return activePeers;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useGetStats = (
|
|
|
|
isGridEnabled: boolean,
|
|
|
|
paginationEnabled: boolean,
|
|
|
|
viewParticipantsWebcams: boolean,
|
|
|
|
) => {
|
|
|
|
const peers = useActivePeers(
|
|
|
|
isGridEnabled,
|
|
|
|
paginationEnabled,
|
|
|
|
viewParticipantsWebcams,
|
|
|
|
);
|
|
|
|
|
|
|
|
return useCallback(async () => {
|
|
|
|
if (!peers) return null;
|
|
|
|
|
|
|
|
const stats: Record<string, unknown> = {};
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
Object.keys(peers).map(async (peerId) => {
|
|
|
|
const peerStats = await peers[peerId].getStats();
|
|
|
|
|
|
|
|
const videoStats: Record<string, unknown> = {};
|
|
|
|
|
|
|
|
peerStats.forEach((stat) => {
|
|
|
|
if (FILTER_VIDEO_STATS.includes(stat.type)) {
|
|
|
|
videoStats[stat.type] = stat;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
stats[peerId] = videoStats;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
return stats;
|
|
|
|
}, [peers]);
|
|
|
|
};
|