import * as React from 'react'; import { Theme, Button, Callout, Separator, Card, Box, Flex, Text } from '@radix-ui/themes'; import permStyles from '../../../../libs/shared/styles/src/permissions.module.css'; import '@radix-ui/themes/styles.css'; // TODO: Replace with official Tavus components when camera permission is updated // import { MicSelectBtn, CameraSelectBtn } from '../../libs/shared/components/src/native-device-select'; // import styles from '../../libs/shared/components/src/native-device-select/device-select.module.css'; import { applyTheme } from '../../../../libs/ui/themes/src/theme-loader'; import { sendCameraPermissionGranted, sendCameraPermissionDenied } from '../../../../libs/shared/utils/src/extension-messages'; export function App(): React.JSX.Element { React.useEffect(() => { applyTheme('default'); }, []); const [status, setStatus] = React.useState('Idle'); const videoRef = React.useRef(null); const streamRef = React.useRef(null); const [selectedCam, setSelectedCam] = React.useState(undefined); const [selectedMic, setSelectedMic] = React.useState(undefined); const [loadedPrefs, setLoadedPrefs] = React.useState<{ audioInputId?: string; videoInputId?: string } | null>(null); const request = async (): Promise => { console.groupCollapsed('[PermTab] Requesting getUserMedia'); setStatus('Requesting camera & microphone...'); try { // Best-effort: prompt Chrome permissions UI for camera/microphone first try { const chromePermissions = chrome.permissions as { request?: (permissions: { permissions: string[] }) => Promise }; const req = chromePermissions?.request; if (typeof req === 'function') { const granted = await req({ permissions: ['camera', 'microphone'] }); console.log('[PermTab] chrome.permissions.request(camera,microphone):', granted); } } catch (permErr) { console.log('[PermTab] chrome.permissions.request not available/failed:', permErr); } // Build fallback attempts similar to panel behavior const attempts: MediaStreamConstraints[] = []; const prefVideo = loadedPrefs?.videoInputId ? { deviceId: { exact: loadedPrefs.videoInputId } } : true; const prefAudio = loadedPrefs?.audioInputId ? { deviceId: { exact: loadedPrefs.audioInputId } } : true; attempts.push({ video: prefVideo, audio: prefAudio }); attempts.push({ video: true, audio: true }); attempts.push({ video: { facingMode: 'user' }, audio: true }); attempts.push({ video: true, audio: false }); attempts.push({ video: { facingMode: 'user' }, audio: false }); let successStream: MediaStream | null = null; let lastError: unknown = null; for (let i = 0; i < attempts.length; i++) { const c = attempts[i]; try { console.log('[PermTab] getUserMedia attempt', i + 1, c); const stream = await navigator.mediaDevices.getUserMedia(c); successStream = stream; break; } catch (err) { lastError = err; const e = err as DOMException; console.warn('[PermTab] getUserMedia failed', c, e?.name || e); // Continue next attempt on overconstrained/notfound; break on NotAllowedError as user denied if (e && (e.name === 'NotAllowedError' || e.name === 'SecurityError')) { break; } } } if (!successStream) { const e = lastError as DOMException | undefined; if (e?.name === 'NotAllowedError' || e?.name === 'SecurityError') { setStatus('Permission denied. Check Chrome site settings and macOS Privacy → Camera/Microphone.'); } else if (e?.name === 'OverconstrainedError' || e?.name === 'NotFoundError') { setStatus('No matching camera/microphone. Select different devices and retry.'); } else { setStatus('Permission denied or failed. Adjust site/OS settings and retry.'); } console.warn('[PermTab] getUserMedia ultimately failed:', e); try { await sendCameraPermissionDenied(); } catch { // Ignore send errors } console.groupEnd(); return; } console.log('[PermTab] getUserMedia success, tracks:', successStream.getTracks().map(t => ({ kind: t.kind, enabled: t.enabled, muted: (t as MediaStreamTrack & { muted?: boolean }).muted }))); streamRef.current = successStream; if (videoRef.current) { videoRef.current.srcObject = successStream; await new Promise((resolve) => { if (videoRef.current) { videoRef.current.onloadedmetadata = (): void => { if (videoRef.current) { videoRef.current.play().catch(() => { // Ignore play errors }); } resolve(); }; } }); } await refreshDevices(); // If we have saved prefs and the stream didn't honor them, try applying if (loadedPrefs?.videoInputId || loadedPrefs?.audioInputId) { await applyConstraints(loadedPrefs?.videoInputId, loadedPrefs?.audioInputId); } setStatus('Granted. You can close this tab.'); console.log('[PermTab] Sending CAMERA_PERMISSION_GRANTED'); await sendCameraPermissionGranted(); console.groupEnd(); } catch (e) { setStatus('Permission denied or failed. Adjust site/OS settings and retry.'); console.warn('[PermTab] request() unexpected error:', e); try { await sendCameraPermissionDenied(); } catch { // Ignore send errors } console.groupEnd(); } }; async function refreshDevices(): Promise { try { const list = await navigator.mediaDevices.enumerateDevices(); const cams = list.filter(d => d.kind === 'videoinput'); const miclist = list.filter(d => d.kind === 'audioinput'); if (!selectedCam && cams[0]) setSelectedCam(cams[0].deviceId); if (!selectedMic && miclist[0]) setSelectedMic(miclist[0].deviceId); } catch (e) { console.warn('[PermTab] enumerateDevices failed', e); } } React.useEffect(() => { const handler = (): void => void refreshDevices(); try { navigator.mediaDevices.addEventListener('devicechange', handler); } catch { // Ignore addEventListener errors } return (): void => { try { navigator.mediaDevices.removeEventListener('devicechange', handler); } catch { // Ignore removeEventListener errors } }; }, []); // Load saved device prefs on mount and preselect controls React.useEffect(() => { const loadPrefs = async (): Promise => { try { const stored = await chrome.storage.local.get('bt_device_prefs'); const prefs = (stored?.bt_device_prefs ?? {}) as { audioInputId?: string; videoInputId?: string }; if (prefs.audioInputId) setSelectedMic(prefs.audioInputId); if (prefs.videoInputId) setSelectedCam(prefs.videoInputId); setLoadedPrefs(prefs); console.log('[PermTab] Loaded saved device prefs', prefs); } catch (e) { console.warn('[PermTab] Failed to load saved device prefs', e); } }; void loadPrefs(); }, []); async function applyConstraints(newCamId?: string, newMicId?: string): Promise { // Store reference to previous stream and clear the ref immediately const prev = streamRef.current; streamRef.current = null; // Stop previous stream tracks prev?.getTracks().forEach(t => t.stop()); const constraints: MediaStreamConstraints = { video: newCamId ? { deviceId: { exact: newCamId } } : true, audio: newMicId ? { deviceId: { exact: newMicId } } : true, }; try { const stream = await navigator.mediaDevices.getUserMedia(constraints); // Set the ref to the new stream // eslint-disable-next-line require-atomic-updates streamRef.current = stream; const videoElement = videoRef.current; if (videoElement) { videoElement.srcObject = stream; await videoElement.play().catch(() => { // Ignore play errors }); } } catch (e) { console.warn('[PermTab] applyConstraints failed', e); } } return (

Camera & Microphone Permission

Chrome will ask you to allow camera & microphone for this extension.
If you don’t see a prompt, click the camera icon in the URL bar and choose “Allow”. On macOS, enable Chrome under System Settings → Privacy & Security → Camera/Microphone.
{status}
You're all set! Your device is ready. {/* TODO: Replace with official Tavus components when camera permission is updated */} {/* { setSelectedMic(id); void applyConstraints(selectedCam, id); }} /> { setSelectedCam(id); void applyConstraints(id, selectedMic); }} /> */} {/* Toggles are provided in the select components above */}
); }