2023-07-17 16:53:58 +02:00
|
|
|
import {
|
|
|
|
|
E2EEOptions,
|
|
|
|
|
ExternalE2EEKeyProvider,
|
|
|
|
|
Room,
|
|
|
|
|
RoomOptions,
|
|
|
|
|
} from "livekit-client";
|
2023-07-05 13:12:37 +01:00
|
|
|
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";
|
2023-07-05 13:12:37 +01:00
|
|
|
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;
|
|
|
|
|
enabled: boolean;
|
|
|
|
|
};
|
2023-06-14 19:20:53 +02:00
|
|
|
|
2023-07-17 16:53:58 +02:00
|
|
|
export type E2EEConfig = {
|
|
|
|
|
sharedKey: string;
|
|
|
|
|
};
|
|
|
|
|
|
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]);
|
|
|
|
|
|
2023-06-30 18:21:18 -04:00
|
|
|
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-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-06-02 19:55:41 +02:00
|
|
|
});
|
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
|
|
|
}
|