152 lines
4 KiB
TypeScript
152 lines
4 KiB
TypeScript
import {
|
|
LiveKitRoom,
|
|
PreJoin,
|
|
LocalUserChoices,
|
|
useToken,
|
|
VideoConference,
|
|
formatChatMessageLinks,
|
|
} from '@livekit/components-react';
|
|
import {
|
|
ExternalE2EEKeyProvider,
|
|
LogLevel,
|
|
Room,
|
|
RoomConnectOptions,
|
|
RoomOptions,
|
|
VideoPresets,
|
|
} from 'livekit-client';
|
|
|
|
import type { NextPage } from 'next';
|
|
import Head from 'next/head';
|
|
import { useRouter } from 'next/router';
|
|
import { useMemo, useState } from 'react';
|
|
import { DebugMode } from '../../lib/Debug';
|
|
import { decodePassphrase, useServerUrl } from '../../lib/client-utils';
|
|
|
|
const Home: NextPage = () => {
|
|
const router = useRouter();
|
|
const { name: roomName } = router.query;
|
|
|
|
const [preJoinChoices, setPreJoinChoices] = useState<LocalUserChoices | undefined>(undefined);
|
|
return (
|
|
<>
|
|
<Head>
|
|
<title>LiveKit Meet</title>
|
|
<link rel="icon" href="/favicon.ico" />
|
|
</Head>
|
|
|
|
<main data-lk-theme="default">
|
|
{roomName && !Array.isArray(roomName) && preJoinChoices ? (
|
|
<ActiveRoom
|
|
roomName={roomName}
|
|
userChoices={preJoinChoices}
|
|
onLeave={() => {
|
|
router.push('/');
|
|
}}
|
|
></ActiveRoom>
|
|
) : (
|
|
<div style={{ display: 'grid', placeItems: 'center', height: '100%' }}>
|
|
<PreJoin
|
|
onError={(err) => console.log('error while setting up prejoin', err)}
|
|
defaults={{
|
|
username: '',
|
|
videoEnabled: true,
|
|
audioEnabled: true,
|
|
}}
|
|
onSubmit={(values) => {
|
|
console.log('Joining with: ', values);
|
|
setPreJoinChoices(values);
|
|
}}
|
|
></PreJoin>
|
|
</div>
|
|
)}
|
|
</main>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default Home;
|
|
|
|
type ActiveRoomProps = {
|
|
userChoices: LocalUserChoices;
|
|
roomName: string;
|
|
region?: string;
|
|
onLeave?: () => void;
|
|
};
|
|
const ActiveRoom = ({ roomName, userChoices, onLeave }: ActiveRoomProps) => {
|
|
const token = useToken(process.env.NEXT_PUBLIC_LK_TOKEN_ENDPOINT, roomName, {
|
|
userInfo: {
|
|
identity: userChoices.username,
|
|
name: userChoices.username,
|
|
},
|
|
});
|
|
|
|
const router = useRouter();
|
|
const { region, hq } = router.query;
|
|
|
|
const liveKitUrl = useServerUrl(region as string | undefined);
|
|
|
|
const hash = typeof window !== 'undefined' && window.location.hash;
|
|
const worker =
|
|
typeof window !== 'undefined' &&
|
|
new Worker(new URL('livekit-client/e2ee-worker', import.meta.url));
|
|
|
|
const e2eeEnabled = !!(hash && worker);
|
|
const keyProvider = new ExternalE2EEKeyProvider();
|
|
|
|
const roomOptions = useMemo((): RoomOptions => {
|
|
return {
|
|
videoCaptureDefaults: {
|
|
deviceId: userChoices.videoDeviceId ?? undefined,
|
|
resolution: hq === 'true' ? VideoPresets.h2160 : VideoPresets.h720,
|
|
},
|
|
publishDefaults: {
|
|
videoSimulcastLayers:
|
|
hq === 'true'
|
|
? [VideoPresets.h1080, VideoPresets.h720]
|
|
: [VideoPresets.h540, VideoPresets.h216],
|
|
},
|
|
audioCaptureDefaults: {
|
|
deviceId: userChoices.audioDeviceId ?? undefined,
|
|
},
|
|
adaptiveStream: { pixelDensity: 'screen' },
|
|
dynacast: true,
|
|
e2ee: e2eeEnabled
|
|
? {
|
|
keyProvider,
|
|
worker,
|
|
}
|
|
: undefined,
|
|
};
|
|
}, [userChoices, hq]);
|
|
|
|
const room = useMemo(() => new Room(roomOptions), []);
|
|
|
|
if (e2eeEnabled) {
|
|
keyProvider.setKey(decodePassphrase(hash.substring(1)));
|
|
room.setE2EEEnabled(true);
|
|
}
|
|
const connectOptions = useMemo((): RoomConnectOptions => {
|
|
return {
|
|
autoSubscribe: false,
|
|
};
|
|
}, []);
|
|
|
|
return (
|
|
<>
|
|
{liveKitUrl && (
|
|
<LiveKitRoom
|
|
room={room}
|
|
token={token}
|
|
serverUrl={liveKitUrl}
|
|
connectOptions={connectOptions}
|
|
video={userChoices.videoEnabled}
|
|
audio={userChoices.audioEnabled}
|
|
onDisconnected={onLeave}
|
|
>
|
|
<VideoConference chatMessageFormatter={formatChatMessageLinks} />
|
|
<DebugMode logLevel={LogLevel.info} />
|
|
</LiveKitRoom>
|
|
)}
|
|
</>
|
|
);
|
|
};
|