diff --git a/lib/DummyKeyProvider.ts b/lib/DummyKeyProvider.ts deleted file mode 100644 index b0c5b80..0000000 --- a/lib/DummyKeyProvider.ts +++ /dev/null @@ -1,16 +0,0 @@ -// just for demonstration purposes, extremely insecure - -import { BaseKeyProvider, createKeyMaterialFromString } from 'livekit-client'; - -export class DummyKeyProvider extends BaseKeyProvider { - readonly participantKeys = new Map([ - ['dev1', 'dev1key'], - ['dev2', 'dev2key'], - ]); - - async setKey(participantId: string) { - // @ts-ignore - const cryptoKey = await createKeyMaterialFromString(this.participantKeys.get(participantId)); - this.onSetEncryptionKey(cryptoKey, participantId); - } -} diff --git a/pages/custom/index.tsx b/pages/custom/index.tsx index aeed27a..93b85a8 100644 --- a/pages/custom/index.tsx +++ b/pages/custom/index.tsx @@ -1,5 +1,12 @@ import { formatChatMessageLinks, LiveKitRoom, VideoConference } from '@livekit/components-react'; -import { ExternalE2EEKeyProvider, LogLevel, Room, RoomOptions, VideoPresets } from 'livekit-client'; +import { + ExternalE2EEKeyProvider, + LogLevel, + RoomConnectOptions, + Room, + RoomOptions, + VideoPresets, +} from 'livekit-client'; import { useRouter } from 'next/router'; import { DebugMode } from '../../lib/Debug'; import { decodePassphrase } from '../../lib/client-utils'; @@ -10,14 +17,12 @@ export default function CustomRoomConnection() { const { liveKitUrl, token } = router.query; const hash = typeof window !== 'undefined' && window.location.hash; - const keyProvider = new ExternalE2EEKeyProvider(); - if (hash) { - keyProvider.setKey(decodePassphrase(hash.substring(1))); - } - const worker = typeof window !== 'undefined' && new Worker(new URL('livekit-client/e2ee-worker', import.meta.url)); + const keyProvider = new ExternalE2EEKeyProvider(); + + const e2eeEnabled = !!(hash && worker); const roomOptions = useMemo((): RoomOptions => { return { @@ -26,17 +31,26 @@ export default function CustomRoomConnection() { }, adaptiveStream: { pixelDensity: 'screen' }, dynacast: true, - e2ee: - hash && worker - ? { - keyProvider, - worker, - } - : undefined, + e2ee: e2eeEnabled + ? { + keyProvider, + worker, + } + : undefined, }; }, []); const room = useMemo(() => new Room(roomOptions), []); + if (e2eeEnabled) { + keyProvider.setKey(decodePassphrase(hash.substring(1))); + room.setE2EEEnabled(true); + } + + const connectOptions = useMemo((): RoomConnectOptions => { + return { + autoSubscribe: false, + }; + }, []); if (typeof liveKitUrl !== 'string') { return

Missing LiveKit URL

; @@ -48,7 +62,14 @@ export default function CustomRoomConnection() { return (
{liveKitUrl && ( - + diff --git a/pages/rooms/[name].tsx b/pages/rooms/[name].tsx index cadbd8f..db5c7a5 100644 --- a/pages/rooms/[name].tsx +++ b/pages/rooms/[name].tsx @@ -21,7 +21,6 @@ import { useRouter } from 'next/router'; import { useMemo, useState } from 'react'; import { DebugMode } from '../../lib/Debug'; import { decodePassphrase, useServerUrl } from '../../lib/client-utils'; -import { DummyKeyProvider } from '../../lib/DummyKeyProvider'; const Home: NextPage = () => { const router = useRouter(); @@ -87,15 +86,13 @@ const ActiveRoom = ({ roomName, userChoices, onLeave }: ActiveRoomProps) => { const liveKitUrl = useServerUrl(region as string | undefined); const hash = typeof window !== 'undefined' && window.location.hash; - const keyProvider = new ExternalE2EEKeyProvider(); - if (hash) { - keyProvider.setKey(decodePassphrase(hash.substring(1))); - } - 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: { @@ -113,20 +110,21 @@ const ActiveRoom = ({ roomName, userChoices, onLeave }: ActiveRoomProps) => { }, adaptiveStream: { pixelDensity: 'screen' }, dynacast: true, - e2ee: - hash && worker - ? { - keyProvider, - worker, - } - : undefined, + e2ee: e2eeEnabled + ? { + keyProvider, + worker, + } + : undefined, }; }, [userChoices, hq]); const room = useMemo(() => new Room(roomOptions), []); - room.setE2EEEnabled(true); - + if (e2eeEnabled) { + keyProvider.setKey(decodePassphrase(hash.substring(1))); + room.setE2EEEnabled(true); + } const connectOptions = useMemo((): RoomConnectOptions => { return { autoSubscribe: false,