2023-08-18 09:03:21 +01:00
|
|
|
/*
|
2024-09-06 10:22:13 +02:00
|
|
|
Copyright 2023, 2024 New Vector Ltd.
|
2023-08-18 09:03:21 +01:00
|
|
|
|
2025-02-18 17:59:58 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-06 10:22:13 +02:00
|
|
|
Please see LICENSE in the repository root for full details.
|
2023-08-18 09:03:21 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import {
|
2024-12-11 09:27:55 +00:00
|
|
|
type MatrixRTCSession,
|
2023-08-18 09:03:21 +01:00
|
|
|
MatrixRTCSessionEvent,
|
2025-03-13 13:58:43 +01:00
|
|
|
} from "matrix-js-sdk/lib/matrixrtc";
|
2025-06-25 19:35:50 +02:00
|
|
|
import { useCallback, useRef } from "react";
|
2025-03-13 16:58:14 +01:00
|
|
|
import { deepCompare } from "matrix-js-sdk/lib/utils";
|
2025-03-13 13:58:43 +01:00
|
|
|
import { logger } from "matrix-js-sdk/lib/logger";
|
2025-03-13 16:58:14 +01:00
|
|
|
import { type LivekitFocus, isLivekitFocus } from "matrix-js-sdk/lib/matrixrtc";
|
2023-08-18 09:03:21 +01:00
|
|
|
|
2025-06-05 07:54:57 -04:00
|
|
|
import { useTypedEventEmitterState } from "../useEvents";
|
|
|
|
|
|
2023-08-18 09:03:21 +01:00
|
|
|
/**
|
|
|
|
|
* Gets the currently active (livekit) focus for a MatrixRTC session
|
|
|
|
|
* This logic is specific to livekit foci where the whole call must use one
|
|
|
|
|
* and the same focus.
|
|
|
|
|
*/
|
2024-06-19 16:41:52 +02:00
|
|
|
export function useActiveLivekitFocus(
|
2023-10-11 10:42:04 -04:00
|
|
|
rtcSession: MatrixRTCSession,
|
2023-08-18 09:03:21 +01:00
|
|
|
): LivekitFocus | undefined {
|
2025-06-25 19:35:50 +02:00
|
|
|
const prevActiveFocus = useRef<LivekitFocus | undefined>(undefined);
|
|
|
|
|
return useTypedEventEmitterState(
|
2025-06-05 07:54:57 -04:00
|
|
|
rtcSession,
|
|
|
|
|
MatrixRTCSessionEvent.MembershipsChanged,
|
|
|
|
|
useCallback(() => {
|
|
|
|
|
const f = rtcSession.getActiveFocus();
|
|
|
|
|
// Only handle foci with type="livekit" for now.
|
2025-06-25 19:35:50 +02:00
|
|
|
if (f && isLivekitFocus(f) && !deepCompare(f, prevActiveFocus.current)) {
|
|
|
|
|
const oldestMembership = rtcSession.getOldestMembership();
|
|
|
|
|
logger.info(
|
|
|
|
|
`Got new active focus from membership: ${oldestMembership?.sender}/${oldestMembership?.deviceId}.
|
|
|
|
|
Updated focus (focus switch) from ${JSON.stringify(prevActiveFocus.current)} to ${JSON.stringify(f)}`,
|
|
|
|
|
);
|
|
|
|
|
prevActiveFocus.current = f;
|
|
|
|
|
}
|
|
|
|
|
return prevActiveFocus.current;
|
2025-06-05 07:54:57 -04:00
|
|
|
}, [rtcSession]),
|
|
|
|
|
);
|
2023-08-18 09:03:21 +01:00
|
|
|
}
|