Refactor livekit disconnect to use an effect hook. (#1925)
--------- Signed-off-by: Timo K <toger5@hotmail.de>
This commit is contained in:
@@ -44,10 +44,9 @@ import { useRoomAvatar } from "./useRoomAvatar";
|
|||||||
import { useRoomName } from "./useRoomName";
|
import { useRoomName } from "./useRoomName";
|
||||||
import { useJoinRule } from "./useJoinRule";
|
import { useJoinRule } from "./useJoinRule";
|
||||||
import { InviteModal } from "./InviteModal";
|
import { InviteModal } from "./InviteModal";
|
||||||
import { E2EEConfig, useLiveKit } from "../livekit/useLiveKit";
|
import { E2EEConfig } from "../livekit/useLiveKit";
|
||||||
import { useUrlParams } from "../UrlParams";
|
import { useUrlParams } from "../UrlParams";
|
||||||
import { E2eeType } from "../e2ee/e2eeType";
|
import { E2eeType } from "../e2ee/e2eeType";
|
||||||
import { useOpenIDSFU } from "../livekit/openIDSFU";
|
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
@@ -136,13 +135,6 @@ export const GroupCallView: FC<Props> = ({
|
|||||||
return { mode: E2eeType.NONE };
|
return { mode: E2eeType.NONE };
|
||||||
}
|
}
|
||||||
}, [perParticipantE2EE, e2eeSharedKey]);
|
}, [perParticipantE2EE, e2eeSharedKey]);
|
||||||
const sfuConfig = useOpenIDSFU(client, rtcSession);
|
|
||||||
const { livekitRoom, connState } = useLiveKit(
|
|
||||||
rtcSession,
|
|
||||||
muteStates,
|
|
||||||
sfuConfig,
|
|
||||||
e2eeConfig,
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// this effect is only if we don't want to show the lobby (skipLobby = true)
|
// this effect is only if we don't want to show the lobby (skipLobby = true)
|
||||||
@@ -238,7 +230,8 @@ export const GroupCallView: FC<Props> = ({
|
|||||||
sendInstantly,
|
sendInstantly,
|
||||||
);
|
);
|
||||||
|
|
||||||
await leaveRTCSession(rtcSession, livekitRoom);
|
// Only sends matrix leave event. The Livekit session will disconnect once the ActiveCall-view unmounts.
|
||||||
|
await leaveRTCSession(rtcSession);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!isPasswordlessUser &&
|
!isPasswordlessUser &&
|
||||||
@@ -248,7 +241,7 @@ export const GroupCallView: FC<Props> = ({
|
|||||||
history.push("/");
|
history.push("/");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[rtcSession, livekitRoom, isPasswordlessUser, confineToRoom, history],
|
[rtcSession, isPasswordlessUser, confineToRoom, history],
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -257,14 +250,15 @@ export const GroupCallView: FC<Props> = ({
|
|||||||
ev: CustomEvent<IWidgetApiRequest>,
|
ev: CustomEvent<IWidgetApiRequest>,
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
widget!.api.transport.reply(ev.detail, {});
|
widget!.api.transport.reply(ev.detail, {});
|
||||||
await leaveRTCSession(rtcSession, livekitRoom);
|
// Only sends matrix leave event. The Livekit session will disconnect once the ActiveCall-view unmounts.
|
||||||
|
await leaveRTCSession(rtcSession);
|
||||||
};
|
};
|
||||||
widget.lazyActions.once(ElementWidgetActions.HangupCall, onHangup);
|
widget.lazyActions.once(ElementWidgetActions.HangupCall, onHangup);
|
||||||
return () => {
|
return () => {
|
||||||
widget!.lazyActions.off(ElementWidgetActions.HangupCall, onHangup);
|
widget!.lazyActions.off(ElementWidgetActions.HangupCall, onHangup);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}, [isJoined, livekitRoom, rtcSession]);
|
}, [isJoined, rtcSession]);
|
||||||
|
|
||||||
const onReconnect = useCallback(() => {
|
const onReconnect = useCallback(() => {
|
||||||
setLeft(false);
|
setLeft(false);
|
||||||
@@ -326,13 +320,11 @@ export const GroupCallView: FC<Props> = ({
|
|||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
if (isJoined && livekitRoom) {
|
if (isJoined) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{shareModal}
|
{shareModal}
|
||||||
<ActiveCall
|
<ActiveCall
|
||||||
livekitRoom={livekitRoom}
|
|
||||||
connState={connState}
|
|
||||||
client={client}
|
client={client}
|
||||||
matrixInfo={matrixInfo}
|
matrixInfo={matrixInfo}
|
||||||
rtcSession={rtcSession}
|
rtcSession={rtcSession}
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ import { OTelGroupCallMembership } from "../otel/OTelGroupCallMembership";
|
|||||||
import { SettingsModal } from "../settings/SettingsModal";
|
import { SettingsModal } from "../settings/SettingsModal";
|
||||||
import { useRageshakeRequestModal } from "../settings/submit-rageshake";
|
import { useRageshakeRequestModal } from "../settings/submit-rageshake";
|
||||||
import { RageshakeRequestModal } from "./RageshakeRequestModal";
|
import { RageshakeRequestModal } from "./RageshakeRequestModal";
|
||||||
import { E2EEConfig } from "../livekit/useLiveKit";
|
import { E2EEConfig, useLiveKit } from "../livekit/useLiveKit";
|
||||||
import { useFullscreen } from "./useFullscreen";
|
import { useFullscreen } from "./useFullscreen";
|
||||||
import { useLayoutStates } from "../video-grid/Layout";
|
import { useLayoutStates } from "../video-grid/Layout";
|
||||||
import { useWakeLock } from "../useWakeLock";
|
import { useWakeLock } from "../useWakeLock";
|
||||||
@@ -85,6 +85,7 @@ import {
|
|||||||
ECAddonConnectionState,
|
ECAddonConnectionState,
|
||||||
ECConnectionState,
|
ECConnectionState,
|
||||||
} from "../livekit/useECConnectionState";
|
} from "../livekit/useECConnectionState";
|
||||||
|
import { useOpenIDSFU } from "../livekit/openIDSFU";
|
||||||
|
|
||||||
const canScreenshare = "getDisplayMedia" in (navigator.mediaDevices ?? {});
|
const canScreenshare = "getDisplayMedia" in (navigator.mediaDevices ?? {});
|
||||||
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
|
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
|
||||||
@@ -92,22 +93,32 @@ const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
|
|||||||
// How long we wait after a focus switch before showing the real participant list again
|
// How long we wait after a focus switch before showing the real participant list again
|
||||||
const POST_FOCUS_PARTICIPANT_UPDATE_DELAY_MS = 3000;
|
const POST_FOCUS_PARTICIPANT_UPDATE_DELAY_MS = 3000;
|
||||||
|
|
||||||
export interface ActiveCallProps extends InCallViewProps {
|
export interface ActiveCallProps
|
||||||
|
extends Omit<InCallViewProps, "livekitRoom" | "connState"> {
|
||||||
e2eeConfig: E2EEConfig;
|
e2eeConfig: E2EEConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ActiveCall: FC<ActiveCallProps> = (props) => {
|
export const ActiveCall: FC<ActiveCallProps> = (props) => {
|
||||||
if (!props.livekitRoom) {
|
const sfuConfig = useOpenIDSFU(props.client, props.rtcSession);
|
||||||
return null;
|
const { livekitRoom, connState } = useLiveKit(
|
||||||
}
|
props.rtcSession,
|
||||||
|
props.muteStates,
|
||||||
|
sfuConfig,
|
||||||
|
props.e2eeConfig,
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
livekitRoom?.disconnect();
|
||||||
|
};
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (!livekitRoom) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<RoomContext.Provider value={props.livekitRoom}>
|
<RoomContext.Provider value={livekitRoom}>
|
||||||
<InCallView
|
<InCallView {...props} livekitRoom={livekitRoom} connState={connState} />
|
||||||
{...props}
|
|
||||||
livekitRoom={props.livekitRoom}
|
|
||||||
connState={props.connState}
|
|
||||||
/>
|
|
||||||
</RoomContext.Provider>
|
</RoomContext.Provider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -193,14 +204,6 @@ export const InCallView: FC<InCallViewProps> = ({
|
|||||||
(muted) => muteStates.audio.setEnabled?.(!muted),
|
(muted) => muteStates.audio.setEnabled?.(!muted),
|
||||||
);
|
);
|
||||||
|
|
||||||
const onLeavePress = useCallback(() => {
|
|
||||||
// Disconnect from the room. We don't do this in onLeave because that's
|
|
||||||
// also called on an unintentional disconnect. Plus we don't have the
|
|
||||||
// livekit room in onLeave anyway.
|
|
||||||
livekitRoom.disconnect();
|
|
||||||
onLeave();
|
|
||||||
}, [livekitRoom, onLeave]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
widget?.api.transport.send(
|
widget?.api.transport.send(
|
||||||
layout === "grid"
|
layout === "grid"
|
||||||
@@ -380,7 +383,9 @@ export const InCallView: FC<InCallViewProps> = ({
|
|||||||
buttons.push(
|
buttons.push(
|
||||||
<HangupButton
|
<HangupButton
|
||||||
key="6"
|
key="6"
|
||||||
onPress={onLeavePress}
|
onPress={function (): void {
|
||||||
|
onLeave();
|
||||||
|
}}
|
||||||
data-testid="incall_leave"
|
data-testid="incall_leave"
|
||||||
/>,
|
/>,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ limitations under the License.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession";
|
import { MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession";
|
||||||
import { Room } from "livekit-client";
|
|
||||||
|
|
||||||
import { PosthogAnalytics } from "./analytics/PosthogAnalytics";
|
import { PosthogAnalytics } from "./analytics/PosthogAnalytics";
|
||||||
import { LivekitFocus } from "./livekit/LivekitFocus";
|
import { LivekitFocus } from "./livekit/LivekitFocus";
|
||||||
@@ -69,10 +68,8 @@ const widgetPostHangupProcedure = async (
|
|||||||
|
|
||||||
export async function leaveRTCSession(
|
export async function leaveRTCSession(
|
||||||
rtcSession: MatrixRTCSession,
|
rtcSession: MatrixRTCSession,
|
||||||
livekitRoom: Room | undefined,
|
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await rtcSession.leaveRoomSession();
|
await rtcSession.leaveRoomSession();
|
||||||
await livekitRoom?.disconnect();
|
|
||||||
if (widget) {
|
if (widget) {
|
||||||
await widgetPostHangupProcedure(widget);
|
await widgetPostHangupProcedure(widget);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user