diff --git a/src/app/features/call/CallView.tsx b/src/app/features/call/CallView.tsx index d2757b93..005f9500 100644 --- a/src/app/features/call/CallView.tsx +++ b/src/app/features/call/CallView.tsx @@ -15,6 +15,7 @@ import { CallControls } from './CallControls'; import { useLivekitSupport } from '../../hooks/useLivekitSupport'; import { webRTCSupported } from '../../utils/rtc'; import { useClient } from '../../hooks/useClient'; +import { CameraPreview } from './CameraPreview'; function LivekitServerMissingMessage() { return ( @@ -114,6 +115,7 @@ function CallPrescreen() { )} + diff --git a/src/app/features/call/CameraPreview.tsx b/src/app/features/call/CameraPreview.tsx new file mode 100644 index 00000000..9ca8da7a --- /dev/null +++ b/src/app/features/call/CameraPreview.tsx @@ -0,0 +1,137 @@ +import { Box, config, Icon, Icons, Text, toRem } from 'folds'; +import { useAtomValue } from 'jotai'; +import React, { useEffect, useRef, useState } from 'react'; +import { SequenceCard } from '../../components/sequence-card'; +import { CutoutCard } from '../../components/cutout-card'; +import { useCallPreferencesAtom } from '../../state/hooks/callPreferences'; + +type CameraStatus = 'idle' | 'loading' | 'ready' | 'denied' | 'not-found' | 'in-use' | 'error'; + +function useCameraStream(enabled: boolean) { + const [stream, setStream] = useState(null); + const [status, setStatus] = useState('idle'); + + useEffect(() => { + if (!enabled) { + setStream(null); + setStatus('idle'); + return undefined; + } + + let cancelled = false; + let active: MediaStream | undefined; + + setStatus('loading'); + + if (!navigator.mediaDevices?.getUserMedia) { + setStatus('error'); + return undefined; + } + + navigator.mediaDevices + .getUserMedia({ video: true, audio: false }) + .then((s) => { + if (cancelled) { + s.getTracks().forEach((t) => t.stop()); + return; + } + active = s; + setStream(s); + setStatus('ready'); + }) + .catch((e: DOMException) => { + if (cancelled) return; + setStream(null); + switch (e.name) { + case 'NotAllowedError': + case 'SecurityError': + setStatus('denied'); + break; + case 'NotFoundError': + case 'OverconstrainedError': + setStatus('not-found'); + break; + case 'NotReadableError': + case 'AbortError': + setStatus('in-use'); + break; + default: + setStatus('error'); + } + }); + + return () => { + cancelled = true; + active?.getTracks().forEach((t) => t.stop()); + }; + }, [enabled]); + + return { stream, status }; +} + +const STATUS_MESSAGE: Record, string> = { + idle: 'Camera off', + loading: 'Starting camera…', + denied: 'Camera permission denied', + 'not-found': 'No camera found', + 'in-use': 'Camera is in use by another app', + error: 'Unable to access camera', +}; + +export function CameraPreview() { + const callPrefAtom = useCallPreferencesAtom(); + const { video } = useAtomValue(callPrefAtom); + + const { stream, status } = useCameraStream(video); + const videoRef = useRef(null); + + useEffect(() => { + const el = videoRef.current; + if (el) el.srcObject = stream; + }, [stream]); + + return ( + + + {status === 'ready' ? ( + + + ); +}