2024-05-06 20:19:51 +08:00
|
|
|
import {
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
2024-07-29 21:42:00 +08:00
|
|
|
useRef,
|
2024-05-06 20:19:51 +08:00
|
|
|
} from 'react';
|
2024-05-02 03:48:12 +08:00
|
|
|
import {
|
|
|
|
useReactiveVar,
|
|
|
|
useLazyQuery,
|
|
|
|
useMutation,
|
2024-07-19 20:52:55 +08:00
|
|
|
useSubscription,
|
2024-05-02 03:48:12 +08:00
|
|
|
} from '@apollo/client';
|
2024-04-20 04:34:43 +08:00
|
|
|
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-06-17 19:54:03 +08:00
|
|
|
} from '/imports/ui/components/video-provider/stream-sorting';
|
2024-04-20 04:34:43 +08:00
|
|
|
import {
|
|
|
|
useVideoState,
|
2024-06-07 20:52:16 +08:00
|
|
|
getConnectingStream,
|
2024-04-20 04:34:43 +08:00
|
|
|
setVideoState,
|
|
|
|
useConnectingStream,
|
|
|
|
streams,
|
|
|
|
getVideoState,
|
|
|
|
} from '../state';
|
|
|
|
import {
|
|
|
|
OWN_VIDEO_STREAMS_QUERY,
|
2024-05-03 20:58:27 +08:00
|
|
|
GRID_USERS_SUBSCRIPTION,
|
2024-04-20 04:34:43 +08:00
|
|
|
VIEWERS_IN_WEBCAM_COUNT_SUBSCRIPTION,
|
|
|
|
} from '../queries';
|
|
|
|
import videoService from '../service';
|
|
|
|
import { CAMERA_BROADCAST_STOP } from '../mutations';
|
2024-06-29 03:58:29 +08:00
|
|
|
import {
|
|
|
|
GridItem,
|
|
|
|
StreamItem,
|
|
|
|
GridUsersResponse,
|
|
|
|
OwnVideoStreamsResponse,
|
|
|
|
} from '../types';
|
2024-05-02 03:48:12 +08:00
|
|
|
import { DesktopPageSizes, MobilePageSizes } from '/imports/ui/Types/meetingClientSettings';
|
2024-05-03 20:58:27 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
2024-06-04 21:40:54 +08:00
|
|
|
import useDeduplicatedSubscription from '/imports/ui/core/hooks/useDeduplicatedSubscription';
|
2024-06-14 21:30:48 +08:00
|
|
|
import { useMeetingIsBreakout } from '/imports/ui/components/app/service';
|
2024-07-19 20:52:55 +08:00
|
|
|
import useSettings from '/imports/ui/services/settings/hooks/useSettings';
|
|
|
|
import { SETTINGS } from '/imports/ui/services/settings/enums';
|
|
|
|
import { useStorageKey } from '/imports/ui/services/storage/hooks';
|
|
|
|
import ConnectionStatus from '/imports/ui/core/graphql/singletons/connectionStatus';
|
|
|
|
import { VIDEO_TYPES } from '/imports/ui/components/video-provider/enums';
|
2024-04-20 04:34:43 +08:00
|
|
|
|
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 = () => {
|
2024-07-19 20:52:55 +08:00
|
|
|
const { isConnected, isConnecting } = useVideoState();
|
|
|
|
if (isConnecting) return 'videoConnecting';
|
|
|
|
if (isConnected) return 'connected';
|
2024-04-20 04:34:43 +08:00
|
|
|
return 'disconnected';
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useDisableReason = () => {
|
|
|
|
const videoLocked = useIsUserLocked();
|
|
|
|
const hasCapReached = useHasCapReached();
|
|
|
|
const hasVideoStream = useHasVideoStream();
|
2024-07-19 20:52:55 +08:00
|
|
|
const connected = useReactiveVar(ConnectionStatus.getConnectedStatusVar());
|
2024-04-20 04:34:43 +08:00
|
|
|
const locks = {
|
|
|
|
videoLocked,
|
|
|
|
camCapReached: hasCapReached && !hasVideoStream,
|
2024-07-19 20:52:55 +08:00
|
|
|
disconnected: !connected,
|
2024-04-20 04:34:43 +08:00
|
|
|
};
|
|
|
|
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();
|
2024-05-08 05:05:36 +08:00
|
|
|
const localStreams = streams.filter((vs) => videoService.isLocalStream(vs.stream));
|
2024-04-20 04:34:43 +08:00
|
|
|
|
|
|
|
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 {
|
2024-06-06 21:50:03 +08:00
|
|
|
userId: Auth.userID as string,
|
|
|
|
userName: Auth.fullname as string,
|
|
|
|
meetingId: Auth.meetingID as string,
|
|
|
|
sessionToken: Auth.sessionToken as string,
|
2024-04-20 04:34:43 +08:00
|
|
|
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 = () => {
|
2024-05-29 21:26:11 +08:00
|
|
|
const {
|
|
|
|
desktopPageSizes: DESKTOP_PAGE_SIZES,
|
|
|
|
mobilePageSizes: MOBILE_PAGE_SIZES,
|
|
|
|
} = window.meetingClientSettings.public.kurento.pagination;
|
|
|
|
|
|
|
|
const PAGINATION_THRESHOLDS_CONF = window.meetingClientSettings.public.kurento.paginationThresholds;
|
|
|
|
const PAGINATION_THRESHOLDS_ENABLED = PAGINATION_THRESHOLDS_CONF.enabled;
|
|
|
|
const PAGINATION_THRESHOLDS = PAGINATION_THRESHOLDS_CONF.thresholds.sort(
|
|
|
|
(t1, t2) => t1.users - t2.users,
|
|
|
|
);
|
|
|
|
|
2024-06-04 21:40:54 +08:00
|
|
|
const { data: countData } = useDeduplicatedSubscription(
|
2024-04-20 04:34:43 +08:00
|
|
|
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();
|
2024-05-29 21:26:11 +08:00
|
|
|
const ROLE_MODERATOR = videoService.getRoleModerator();
|
|
|
|
const ROLE_VIEWER = videoService.getRoleViewer();
|
2024-04-20 04:34:43 +08:00
|
|
|
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-07-19 20:52:55 +08:00
|
|
|
const actualSize = size ?? 0;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setVideoState({ pageSize: actualSize });
|
|
|
|
}, [actualSize]);
|
|
|
|
|
|
|
|
return actualSize;
|
2024-04-20 04:34:43 +08:00
|
|
|
};
|
|
|
|
|
2024-07-19 20:52:55 +08:00
|
|
|
export const useIsPaginationEnabled = () => {
|
|
|
|
const myPageSize = useMyPageSize();
|
|
|
|
const { paginationEnabled } = useSettings(SETTINGS.APPLICATION) as { paginationEnabled?: boolean };
|
|
|
|
return myPageSize > 0 && paginationEnabled;
|
|
|
|
};
|
2024-04-20 04:34:43 +08:00
|
|
|
|
|
|
|
export const useStreams = () => {
|
|
|
|
const videoStreams = useReactiveVar(streams);
|
|
|
|
return { streams: videoStreams };
|
|
|
|
};
|
|
|
|
|
2024-07-29 21:37:38 +08:00
|
|
|
export const useGridUsers = (visibleStreamCount: number) => {
|
2024-05-03 20:58:27 +08:00
|
|
|
const gridSize = useGridSize();
|
2024-07-19 20:52:55 +08:00
|
|
|
const isGridEnabled = useStorageKey('isGridEnabled');
|
2024-07-29 21:42:00 +08:00
|
|
|
const gridItems = useRef<GridItem[]>([]);
|
2024-07-13 01:47:17 +08:00
|
|
|
|
2024-05-03 20:58:27 +08:00
|
|
|
const {
|
|
|
|
data: gridData,
|
|
|
|
error: gridError,
|
2024-07-29 21:42:00 +08:00
|
|
|
loading: gridLoading,
|
2024-07-19 20:52:55 +08:00
|
|
|
} = useSubscription<GridUsersResponse>(
|
2024-05-03 20:58:27 +08:00
|
|
|
GRID_USERS_SUBSCRIPTION,
|
|
|
|
{
|
2024-07-29 21:37:38 +08:00
|
|
|
variables: { limit: Math.max(gridSize - visibleStreamCount, 0) },
|
2024-05-03 20:58:27 +08:00
|
|
|
skip: !isGridEnabled,
|
|
|
|
},
|
2024-04-20 04:34:43 +08:00
|
|
|
);
|
2024-05-03 20:58:27 +08:00
|
|
|
|
2024-07-29 21:42:00 +08:00
|
|
|
if (gridLoading) return gridItems.current;
|
|
|
|
|
2024-07-19 20:52:55 +08:00
|
|
|
if (gridError) {
|
|
|
|
logger.error({
|
|
|
|
logCode: 'grid_users_sub_error',
|
|
|
|
extraInfo: {
|
|
|
|
errorName: gridError.name,
|
|
|
|
errorMessage: gridError.message,
|
|
|
|
},
|
|
|
|
}, 'Grid users subscription failed.');
|
|
|
|
}
|
2024-05-03 20:58:27 +08:00
|
|
|
|
2024-07-19 20:52:55 +08:00
|
|
|
if (gridData) {
|
|
|
|
const newGridUsers = gridData.user.map((user) => ({
|
|
|
|
...user,
|
|
|
|
type: VIDEO_TYPES.GRID,
|
|
|
|
}));
|
2024-07-29 21:42:00 +08:00
|
|
|
gridItems.current = newGridUsers;
|
|
|
|
} else {
|
|
|
|
gridItems.current = [];
|
2024-07-19 20:52:55 +08:00
|
|
|
}
|
2024-04-20 04:34:43 +08:00
|
|
|
|
2024-07-29 21:42:00 +08:00
|
|
|
return gridItems.current;
|
2024-04-20 04:34:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const useSharedDevices = () => {
|
|
|
|
const { streams } = useStreams();
|
|
|
|
const devices = streams
|
2024-05-08 05:05:36 +08:00
|
|
|
.filter((s) => videoService.isLocalStream(s.stream))
|
2024-04-20 04:34:43 +08:00
|
|
|
.map((vs) => vs.deviceId);
|
|
|
|
|
|
|
|
return devices;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useNumberOfPages = () => {
|
2024-07-19 20:52:55 +08:00
|
|
|
const { numberOfPages } = useVideoState();
|
|
|
|
return numberOfPages;
|
2024-04-20 04:34:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const useCurrentVideoPageIndex = () => {
|
2024-07-19 20:52:55 +08:00
|
|
|
const { currentVideoPageIndex } = useVideoState();
|
|
|
|
return currentVideoPageIndex;
|
2024-04-20 04:34:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const useGridSize = () => {
|
|
|
|
let size;
|
|
|
|
const myRole = useMyRole();
|
2024-05-29 21:26:11 +08:00
|
|
|
const ROLE_MODERATOR = videoService.getRoleModerator();
|
|
|
|
const ROLE_VIEWER = videoService.getRoleViewer();
|
|
|
|
const {
|
|
|
|
desktopGridSizes: DESKTOP_GRID_SIZES,
|
|
|
|
mobileGridSizes: MOBILE_GRID_SIZES,
|
|
|
|
} = window.meetingClientSettings.public.kurento.pagination;
|
|
|
|
|
2024-04-20 04:34:43 +08:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2024-07-19 20:52:55 +08:00
|
|
|
export const useVideoStreams = () => {
|
|
|
|
const { viewParticipantsWebcams } = useSettings(SETTINGS.DATA_SAVING) as { viewParticipantsWebcams?: boolean };
|
|
|
|
const { currentVideoPageIndex, numberOfPages } = useVideoState();
|
|
|
|
const { streams: videoStreams } = useStreams();
|
2024-04-20 04:34:43 +08:00
|
|
|
const connectingStream = useConnectingStream(videoStreams);
|
|
|
|
const myPageSize = useMyPageSize();
|
2024-07-19 20:52:55 +08:00
|
|
|
const isPaginationEnabled = useIsPaginationEnabled();
|
2024-05-02 03:48:12 +08:00
|
|
|
let streams: StreamItem[] = [...videoStreams];
|
2024-07-19 20:52:55 +08:00
|
|
|
let totalNumberOfOtherStreams: number | undefined;
|
2024-04-20 04:34:43 +08:00
|
|
|
|
2024-05-29 21:26:11 +08:00
|
|
|
const {
|
|
|
|
paginationSorting: PAGINATION_SORTING,
|
|
|
|
defaultSorting: DEFAULT_SORTING,
|
|
|
|
} = window.meetingClientSettings.public.kurento.cameraSortingModes;
|
|
|
|
|
2024-04-20 04:34:43 +08:00
|
|
|
if (connectingStream) streams.push(connectingStream);
|
|
|
|
|
|
|
|
if (!viewParticipantsWebcams) {
|
2024-05-08 05:05:36 +08:00
|
|
|
streams = streams.filter((vs) => videoService.isLocalStream(vs.stream));
|
2024-04-20 04:34:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isPaginationEnabled) {
|
2024-05-02 03:48:12 +08:00
|
|
|
const [filtered, others] = partition(
|
|
|
|
streams,
|
2024-07-19 20:52:55 +08:00
|
|
|
(vs: StreamItem) => videoService.isLocalStream(vs.stream) || (vs.type === VIDEO_TYPES.STREAM && vs.user.pinned),
|
2024-05-02 03:48:12 +08:00
|
|
|
);
|
|
|
|
const [pin, mine] = partition(
|
|
|
|
filtered,
|
2024-07-19 20:52:55 +08:00
|
|
|
(vs: StreamItem) => vs.type === VIDEO_TYPES.STREAM && vs.user.pinned,
|
2024-05-02 03:48:12 +08:00
|
|
|
);
|
2024-04-20 04:34:43 +08:00
|
|
|
|
2024-07-19 20:52:55 +08:00
|
|
|
totalNumberOfOtherStreams = others.length;
|
2024-04-20 04:34:43 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-07-29 21:37:38 +08:00
|
|
|
const gridUsers = useGridUsers(streams.length);
|
2024-07-19 20:52:55 +08:00
|
|
|
|
2024-04-20 04:34:43 +08:00
|
|
|
return {
|
|
|
|
streams,
|
|
|
|
gridUsers,
|
|
|
|
totalNumberOfStreams: streams.length,
|
2024-07-19 20:52:55 +08:00
|
|
|
totalNumberOfOtherStreams,
|
2024-04-20 04:34:43 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useHasVideoStream = () => {
|
|
|
|
const { streams } = useStreams();
|
2024-06-20 22:42:44 +08:00
|
|
|
const connectingStream = useConnectingStream();
|
|
|
|
return !!connectingStream || streams.some((s) => videoService.isLocalStream(s.stream));
|
2024-04-20 04:34:43 +08:00
|
|
|
};
|
|
|
|
|
2024-05-02 22:22:24 +08:00
|
|
|
const useOwnVideoStreamsQuery = () => useLazyQuery<OwnVideoStreamsResponse>(
|
|
|
|
OWN_VIDEO_STREAMS_QUERY,
|
2024-05-08 05:05:36 +08:00
|
|
|
{
|
|
|
|
variables: {
|
|
|
|
userId: Auth.userID,
|
|
|
|
streamIdPrefix: `${videoService.getPrefix()}%`,
|
|
|
|
},
|
|
|
|
},
|
2024-05-02 22:22:24 +08:00
|
|
|
);
|
2024-04-20 04:34:43 +08:00
|
|
|
|
2024-05-02 22:22:24 +08:00
|
|
|
export const useExitVideo = (forceExit = false) => {
|
2024-04-20 04:34:43 +08:00
|
|
|
const [cameraBroadcastStop] = useMutation(CAMERA_BROADCAST_STOP);
|
2024-05-02 22:22:24 +08:00
|
|
|
const [getOwnVideoStreams] = useOwnVideoStreamsQuery();
|
2024-04-20 04:34:43 +08:00
|
|
|
|
2024-05-02 22:22:24 +08:00
|
|
|
const exitVideo = useCallback(async () => {
|
2024-04-20 04:34:43 +08:00
|
|
|
const { isConnected } = getVideoState();
|
|
|
|
|
2024-05-02 22:22:24 +08:00
|
|
|
if (isConnected || forceExit) {
|
2024-04-20 04:34:43 +08:00
|
|
|
const sendUserUnshareWebcam = (cameraId: string) => {
|
2024-05-21 23:31:17 +08:00
|
|
|
return cameraBroadcastStop({ variables: { cameraId } });
|
2024-04-20 04:34:43 +08:00
|
|
|
};
|
|
|
|
|
2024-05-21 23:31:17 +08:00
|
|
|
return getOwnVideoStreams().then(async ({ data }) => {
|
2024-05-02 22:22:24 +08:00
|
|
|
if (data) {
|
|
|
|
const streams = data.user_camera || [];
|
2024-05-21 23:31:17 +08:00
|
|
|
const results = streams.map((s) => sendUserUnshareWebcam(s.streamId));
|
|
|
|
return Promise.all(results).then(() => {
|
|
|
|
videoService.exitedVideo();
|
|
|
|
return true;
|
|
|
|
}).catch((e) => {
|
|
|
|
logger.warn({
|
|
|
|
logCode: 'exit_audio',
|
|
|
|
extraInfo: e,
|
|
|
|
}, 'Exiting audio');
|
|
|
|
return false;
|
|
|
|
});
|
2024-05-02 22:22:24 +08:00
|
|
|
}
|
2024-05-21 23:31:17 +08:00
|
|
|
return true;
|
2024-04-20 04:34:43 +08:00
|
|
|
});
|
|
|
|
}
|
2024-05-21 23:31:17 +08:00
|
|
|
return true;
|
2024-04-20 04:34:43 +08:00
|
|
|
}, [cameraBroadcastStop]);
|
|
|
|
|
|
|
|
return exitVideo;
|
|
|
|
};
|
|
|
|
|
2024-05-02 03:48:12 +08:00
|
|
|
export const useViewersInWebcamCount = (): number => {
|
2024-06-04 21:40:54 +08:00
|
|
|
const { data } = useDeduplicatedSubscription(VIEWERS_IN_WEBCAM_COUNT_SUBSCRIPTION);
|
2024-04-20 04:34:43 +08:00
|
|
|
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 = () => {
|
2024-04-20 04:34:43 +08:00
|
|
|
const [cameraBroadcastStop] = useMutation(CAMERA_BROADCAST_STOP);
|
2024-05-02 22:22:24 +08:00
|
|
|
const [getOwnVideoStreams] = useOwnVideoStreamsQuery();
|
2024-04-20 04:34:43 +08:00
|
|
|
|
2024-05-02 22:22:24 +08:00
|
|
|
return useCallback(async (cameraId?: string) => {
|
|
|
|
const { data } = await getOwnVideoStreams();
|
|
|
|
const streams = data?.user_camera ?? [];
|
2024-06-07 20:52:16 +08:00
|
|
|
const connectingStream = getConnectingStream();
|
2024-05-02 22:22:24 +08:00
|
|
|
const hasTargetStream = streams.some((s) => s.streamId === cameraId);
|
|
|
|
const hasOtherStream = streams.some((s) => s.streamId !== cameraId);
|
2024-04-20 04:34:43 +08:00
|
|
|
|
2024-05-02 03:48:12 +08:00
|
|
|
if (hasTargetStream) {
|
|
|
|
cameraBroadcastStop({ variables: { cameraId } });
|
|
|
|
}
|
2024-04-20 04:34:43 +08:00
|
|
|
|
2024-06-07 20:52:16 +08:00
|
|
|
if (!hasOtherStream && !connectingStream) {
|
2024-05-02 03:48:12 +08:00
|
|
|
videoService.exitedVideo();
|
|
|
|
} else {
|
2024-06-07 20:52:16 +08:00
|
|
|
videoService.stopConnectingStream();
|
2024-04-20 04:34:43 +08:00
|
|
|
}
|
|
|
|
}, [cameraBroadcastStop]);
|
2024-05-02 03:48:12 +08:00
|
|
|
};
|
2024-04-20 04:34:43 +08:00
|
|
|
|
2024-07-19 20:52:55 +08:00
|
|
|
export const useActivePeers = () => {
|
|
|
|
const videoData = useVideoStreams();
|
2024-05-02 03:48:12 +08:00
|
|
|
|
|
|
|
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;
|
2024-04-20 04:34:43 +08:00
|
|
|
};
|
|
|
|
|
2024-07-19 20:52:55 +08:00
|
|
|
export const useGetStats = () => {
|
|
|
|
const peers = useActivePeers();
|
2024-05-02 03:48:12 +08:00
|
|
|
|
|
|
|
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]);
|
2024-04-20 04:34:43 +08:00
|
|
|
};
|
2024-05-06 22:24:45 +08:00
|
|
|
|
|
|
|
export const useShouldRenderPaginationToggle = () => {
|
|
|
|
const myPageSize = useMyPageSize();
|
2024-05-29 21:26:11 +08:00
|
|
|
const {
|
|
|
|
paginationToggleEnabled: PAGINATION_TOGGLE_ENABLED,
|
|
|
|
} = window.meetingClientSettings.public.kurento.pagination;
|
|
|
|
|
2024-05-06 22:24:45 +08:00
|
|
|
return PAGINATION_TOGGLE_ENABLED && myPageSize > 0;
|
|
|
|
};
|
2024-06-14 21:30:48 +08:00
|
|
|
|
|
|
|
export const useIsVideoPinEnabledForCurrentUser = (isModerator: boolean) => {
|
|
|
|
const isBreakout = useMeetingIsBreakout();
|
|
|
|
const isPinEnabled = videoService.isPinEnabled();
|
|
|
|
|
|
|
|
return !!(isModerator && isPinEnabled && !isBreakout);
|
|
|
|
};
|