Try LiveKit Meet for free with our live demo project.
-diff --git a/lib/DummyKeyProvider.ts b/lib/DummyKeyProvider.ts new file mode 100644 index 0000000..b0c5b80 --- /dev/null +++ b/lib/DummyKeyProvider.ts @@ -0,0 +1,16 @@ +// 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/lib/client-utils.ts b/lib/client-utils.ts index a2401c8..a3471ea 100644 --- a/lib/client-utils.ts +++ b/lib/client-utils.ts @@ -19,3 +19,14 @@ export function useServerUrl(region?: string) { }); return serverUrl; } + +export function encodePassphrase(bytes: Uint8Array) { + const binString = Array.from(bytes, (x) => String.fromCodePoint(x)).join(''); + return btoa(binString); +} + +export function decodePassphrase(base64String: string) { + const binString = atob(base64String); + // @ts-ignore + return Uint8Array.from(binString, (m) => m.codePointAt(0)); +} diff --git a/pages/custom/index.tsx b/pages/custom/index.tsx index 3ae4ef1..aeed27a 100644 --- a/pages/custom/index.tsx +++ b/pages/custom/index.tsx @@ -1,11 +1,43 @@ import { formatChatMessageLinks, LiveKitRoom, VideoConference } from '@livekit/components-react'; -import { LogLevel } from 'livekit-client'; +import { ExternalE2EEKeyProvider, LogLevel, Room, RoomOptions, VideoPresets } from 'livekit-client'; import { useRouter } from 'next/router'; import { DebugMode } from '../../lib/Debug'; +import { decodePassphrase } from '../../lib/client-utils'; +import { useMemo } from 'react'; export default function CustomRoomConnection() { const router = useRouter(); 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 roomOptions = useMemo((): RoomOptions => { + return { + publishDefaults: { + videoSimulcastLayers: [VideoPresets.h540, VideoPresets.h216], + }, + adaptiveStream: { pixelDensity: 'screen' }, + dynacast: true, + e2ee: + hash && worker + ? { + keyProvider, + worker, + } + : undefined, + }; + }, []); + + const room = useMemo(() => new Room(roomOptions), []); + if (typeof liveKitUrl !== 'string') { return
Try LiveKit Meet for free with our live demo project.
-