livekit-dgn/pages/rooms/[name].tsx

178 lines
4.6 KiB
TypeScript
Raw Normal View History

'use client';
import {
LiveKitRoom,
LocalUserChoices,
VideoConference,
2023-01-20 19:09:26 +01:00
formatChatMessageLinks,
useToken,
} from '@livekit/components-react';
import {
ExternalE2EEKeyProvider,
LogLevel,
Room,
RoomConnectOptions,
RoomOptions,
VideoPresets,
} from 'livekit-client';
2022-10-24 17:03:12 +02:00
import type { NextPage } from 'next';
import dynamic from 'next/dynamic';
import Head from 'next/head';
import { useRouter } from 'next/router';
import { useMemo, useState } from 'react';
import { DebugMode } from '../../lib/Debug';
import {
decodePassphrase,
encodePassphrase,
randomString,
useServerUrl,
} from '../../lib/client-utils';
const PreJoinNoSSR = dynamic(
async () => {
return (await import('@livekit/components-react')).PreJoin;
},
{ ssr: false },
);
2022-10-24 17:03:12 +02:00
const Home: NextPage = () => {
2022-10-24 17:03:12 +02:00
const router = useRouter();
const { name: roomName } = router.query;
const e2eePassphrase =
typeof window !== 'undefined' && decodePassphrase(location.hash.substring(1));
2022-10-24 17:03:12 +02:00
const [preJoinChoices, setPreJoinChoices] = useState<LocalUserChoices | undefined>(undefined);
function handlePreJoinSubmit(values: LocalUserChoices) {
if (values.e2ee) {
location.hash = encodePassphrase(values.sharedPassphrase);
}
setPreJoinChoices(values);
}
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%' }}>
<PreJoinNoSSR
onError={(err) => console.log('error while setting up prejoin', err)}
defaults={{
username: '',
videoEnabled: true,
audioEnabled: true,
e2ee: !!e2eePassphrase,
sharedPassphrase: e2eePassphrase || randomString(64),
}}
onSubmit={handlePreJoinSubmit}
showE2EEOptions={true}
></PreJoinNoSSR>
</div>
)}
</main>
</>
);
2022-10-24 17:03:12 +02:00
};
export default Home;
2022-10-24 17:03:12 +02:00
type ActiveRoomProps = {
userChoices: LocalUserChoices;
roomName: string;
region?: string;
onLeave?: () => void;
};
const ActiveRoom = ({ roomName, userChoices, onLeave }: ActiveRoomProps) => {
2023-02-14 09:40:08 +01:00
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 worker =
typeof window !== 'undefined' &&
userChoices.e2ee &&
new Worker(new URL('livekit-client/e2ee-worker', import.meta.url));
const e2eeEnabled = !!(userChoices.e2ee && worker);
2023-08-30 19:48:27 +02:00
const keyProvider = new ExternalE2EEKeyProvider();
const roomOptions = useMemo((): RoomOptions => {
2022-10-24 17:03:12 +02:00
return {
videoCaptureDefaults: {
deviceId: userChoices.videoDeviceId ?? undefined,
resolution: hq === 'true' ? VideoPresets.h2160 : VideoPresets.h720,
},
publishDefaults: {
dtx: false,
videoSimulcastLayers:
hq === 'true'
? [VideoPresets.h1080, VideoPresets.h720]
: [VideoPresets.h540, VideoPresets.h216],
red: !e2eeEnabled,
// videoCodec: 'vp9',
},
audioCaptureDefaults: {
deviceId: userChoices.audioDeviceId ?? undefined,
2022-10-24 17:03:12 +02:00
},
adaptiveStream: { pixelDensity: 'screen' },
dynacast: true,
2023-08-30 19:48:27 +02:00
e2ee: e2eeEnabled
? {
keyProvider,
worker,
}
: undefined,
2022-10-24 17:03:12 +02:00
};
}, [userChoices, hq]);
2022-10-24 17:03:12 +02:00
const room = useMemo(() => new Room(roomOptions), []);
2023-08-30 19:48:27 +02:00
if (e2eeEnabled) {
keyProvider.setKey(decodePassphrase(userChoices.sharedPassphrase));
2023-08-30 19:48:27 +02:00
room.setE2EEEnabled(true);
}
2023-08-17 19:01:59 +02:00
const connectOptions = useMemo((): RoomConnectOptions => {
return {
2023-09-14 18:53:44 +02:00
autoSubscribe: true,
2023-08-17 19:01:59 +02:00
};
}, []);
return (
<>
{liveKitUrl && (
<LiveKitRoom
room={room}
token={token}
serverUrl={liveKitUrl}
2023-08-17 19:01:59 +02:00
connectOptions={connectOptions}
video={userChoices.videoEnabled}
audio={userChoices.audioEnabled}
onDisconnected={onLeave}
>
2023-01-20 19:09:26 +01:00
<VideoConference chatMessageFormatter={formatChatMessageLinks} />
<DebugMode logLevel={LogLevel.info} />
</LiveKitRoom>
)}
</>
);
2022-10-24 17:03:12 +02:00
};