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

45 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-06-16 18:07:13 +02:00
import { Room, RoomOptions } from "livekit-client";
import { useLiveKitRoom } from "@livekit/components-react";
import { useMemo } from "react";
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;
enabled: boolean;
};
2023-06-14 19:20:53 +02:00
2023-06-16 18:07:13 +02:00
export function useLiveKit(
userChoices: UserChoices,
2023-06-30 16:43:28 +01:00
sfuConfig?: SFUConfig
2023-06-16 18:07:13 +02:00
): Room | undefined {
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-06-16 18:07:13 +02:00
return options;
}, [userChoices.video, userChoices.audio]);
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,
options: roomOptions,
});
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
}