import { faBolt } from '@fortawesome/free-solid-svg-icons'; import { createLocalVideoTrack, LocalVideoTrack } from 'livekit-client'; import { AudioSelectButton, ControlButton, VideoSelectButton } from '@livekit/react-components'; import { VideoRenderer } from '@livekit/react-core'; import { ReactElement, useEffect, useState } from 'react'; import { AspectRatio } from 'react-aspect-ratio'; import { useNavigate } from 'react-router-dom'; export const PreJoinPage = () => { // initial state from query parameters const searchParams = new URLSearchParams(window.location.search); const storedUrl = searchParams.get('url') ?? 'ws://localhost:7880'; const storedToken = searchParams.get('token') ?? ''; // state to pass onto room const [url, setUrl] = useState(storedUrl); const [token, setToken] = useState(storedToken); const [simulcast, setSimulcast] = useState(true); const [dynacast, setDynacast] = useState(true); const [adaptiveStream, setAdaptiveStream] = useState(true); const [videoEnabled, setVideoEnabled] = useState(false); const [audioEnabled, setAudioEnabled] = useState(true); // disable connect button unless validated const [connectDisabled, setConnectDisabled] = useState(true); const [videoTrack, setVideoTrack] = useState(); const [audioDevice, setAudioDevice] = useState(); const [videoDevice, setVideoDevice] = useState(); const navigate = useNavigate(); useEffect(() => { if (token && url) { setConnectDisabled(false); } else { setConnectDisabled(true); } }, [token, url]); const toggleVideo = async () => { if (videoTrack) { videoTrack.stop(); setVideoEnabled(false); setVideoTrack(undefined); } else { const track = await createLocalVideoTrack({ deviceId: videoDevice?.deviceId, }); setVideoEnabled(true); setVideoTrack(track); } }; useEffect(() => { // enable video by default createLocalVideoTrack({ deviceId: videoDevice?.deviceId, }).then((track) => { setVideoEnabled(true); setVideoTrack(track); }); }, [videoDevice]); const toggleAudio = () => { if (audioEnabled) { setAudioEnabled(false); } else { setAudioEnabled(true); } }; const selectVideoDevice = (device: MediaDeviceInfo) => { setVideoDevice(device); if (videoTrack) { if (videoTrack.mediaStreamTrack.getSettings().deviceId === device.deviceId) { return; } // stop video videoTrack.stop(); } }; const connectToRoom = async () => { if (videoTrack) { videoTrack.stop(); } if ( window.location.protocol === 'https:' && url.startsWith('ws://') && !url.startsWith('ws://localhost') ) { alert('Unable to connect to insecure websocket from https'); return; } const params: { [key: string]: string } = { url, token, videoEnabled: videoEnabled ? '1' : '0', audioEnabled: audioEnabled ? '1' : '0', simulcast: simulcast ? '1' : '0', dynacast: dynacast ? '1' : '0', adaptiveStream: adaptiveStream ? '1' : '0', }; if (audioDevice) { params.audioDeviceId = audioDevice.deviceId; } if (videoDevice) { params.videoDeviceId = videoDevice.deviceId; } else if (videoTrack) { // pass along current device id to ensure camera device match const deviceId = await videoTrack.getDeviceId(); if (deviceId) { params.videoDeviceId = deviceId; } } navigate({ pathname: '/room', search: '?' + new URLSearchParams(params).toString(), }); }; let videoElement: ReactElement; if (videoTrack) { videoElement = ; } else { videoElement =
; } return (

LiveKit Video


LiveKit URL
setUrl(e.target.value)} />
Token
setToken(e.target.value)} autoFocus={true} />
setSimulcast(e.target.checked)} />
setDynacast(e.target.checked)} />
setAdaptiveStream(e.target.checked)} />
{videoElement}
); };