Files
element-call/src/livekit/useLiveKit.ts
T

83 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-07-17 16:53:58 +02:00
import {
E2EEOptions,
ExternalE2EEKeyProvider,
Room,
RoomOptions,
setLogLevel,
2023-07-17 16:53:58 +02:00
} from "livekit-client";
import { useLiveKitRoom } from "@livekit/components-react";
2023-07-17 16:53:58 +02:00
import { useEffect, useMemo } from "react";
import E2EEWorker from "livekit-client/e2ee-worker?worker";
2023-05-30 20:56:25 +02:00
2023-06-16 18:07:13 +02:00
import { defaultLiveKitOptions } from "./options";
import { SFUConfig } from "./openIDSFU";
2023-05-26 20:41:32 +02:00
2023-06-16 18:07:13 +02:00
export type UserChoices = {
audio?: DeviceChoices;
video?: DeviceChoices;
2023-05-26 20:41:32 +02:00
};
2023-06-16 18:07:13 +02:00
export type DeviceChoices = {
selectedId?: string;
2023-06-16 18:07:13 +02:00
enabled: boolean;
};
2023-06-14 19:20:53 +02:00
2023-07-17 16:53:58 +02:00
export type E2EEConfig = {
sharedKey: string;
};
2023-07-17 19:33:29 +01:00
setLogLevel("debug");
2023-06-16 18:07:13 +02:00
export function useLiveKit(
userChoices: UserChoices,
2023-07-17 16:53:58 +02:00
sfuConfig?: SFUConfig,
e2eeConfig?: E2EEConfig
2023-06-16 18:07:13 +02:00
): Room | undefined {
2023-07-17 16:53:58 +02:00
const e2eeOptions = useMemo(() => {
if (!e2eeConfig?.sharedKey) return undefined;
return {
keyProvider: new ExternalE2EEKeyProvider(),
worker: new E2EEWorker(),
} as E2EEOptions;
}, [e2eeConfig]);
useEffect(() => {
if (!e2eeConfig?.sharedKey || !e2eeOptions) return;
(e2eeOptions.keyProvider as ExternalE2EEKeyProvider).setKey(
e2eeConfig?.sharedKey
);
}, [e2eeOptions, e2eeConfig?.sharedKey]);
const roomOptions = useMemo((): RoomOptions => {
2023-06-16 18:07:13 +02:00
const options = defaultLiveKitOptions;
options.videoCaptureDefaults = {
...options.videoCaptureDefaults,
deviceId: userChoices.video?.selectedId,
2023-05-26 20:41:32 +02:00
};
2023-06-16 18:07:13 +02:00
options.audioCaptureDefaults = {
...options.audioCaptureDefaults,
deviceId: userChoices.audio?.selectedId,
2023-05-26 20:41:32 +02:00
};
2023-07-17 16:53:58 +02:00
options.e2ee = e2eeOptions;
2023-06-16 18:07:13 +02:00
return options;
2023-07-17 16:53:58 +02:00
}, [userChoices.video, userChoices.audio, e2eeOptions]);
2023-06-16 18:07:13 +02:00
2023-07-25 15:49:59 +02:00
// We have to create the room manually here due to a bug inside
// @livekit/components-react. JSON.stringify() is used in deps of a
// useEffect() with an argument that references itself, if E2EE is enabled
2023-07-12 17:50:07 +02:00
const roomWithoutProps = useMemo(() => new Room(roomOptions), [roomOptions]);
2023-06-16 18:07:13 +02:00
const { room } = useLiveKitRoom({
2023-06-30 16:43:28 +01:00
token: sfuConfig?.jwt,
serverUrl: sfuConfig?.url,
2023-06-16 18:07:13 +02:00
audio: userChoices.audio?.enabled ?? false,
video: userChoices.video?.enabled ?? false,
2023-07-12 17:50:07 +02:00
room: roomWithoutProps,
});
2023-05-30 20:56:25 +02:00
2023-06-16 18:07:13 +02:00
return room;
2023-05-30 20:56:25 +02:00
}