/** * Device Detection Utilities * * Detect browser capabilities, device type, and WebRTC support * for the voice demo application. */ export interface BrowserInfo { name: 'Chrome' | 'Firefox' | 'Safari' | 'Edge' | 'Unknown'; version: string; supportsWebRTC: boolean; supportsMediaDevices: boolean; isHttps: boolean; isMobile: boolean; } export interface DeviceCapabilities { hasMicrophone: boolean; hasCamera: boolean; canAccessMedia: boolean; browserInfo: BrowserInfo; } /** * Detect the browser name and version */ export function detectBrowser(): Pick { const userAgent = navigator.userAgent; // Chrome if (/Chrome/.test(userAgent) && /Google Inc/.test(navigator.vendor)) { const match = userAgent.match(/Chrome\/(\d+)/); return { name: 'Chrome', version: match ? match[1] : 'unknown', }; } // Edge (Chromium-based) if (/Edg/.test(userAgent)) { const match = userAgent.match(/Edg\/(\d+)/); return { name: 'Edge', version: match ? match[1] : 'unknown', }; } // Firefox if (/Firefox/.test(userAgent)) { const match = userAgent.match(/Firefox\/(\d+)/); return { name: 'Firefox', version: match ? match[1] : 'unknown', }; } // Safari if (/Safari/.test(userAgent) && !/Chrome/.test(userAgent)) { const match = userAgent.match(/Version\/(\d+)/); return { name: 'Safari', version: match ? match[1] : 'unknown', }; } return { name: 'Unknown', version: 'unknown', }; } /** * Check if the browser supports WebRTC */ export function supportsWebRTC(): boolean { return !!( window.RTCPeerConnection || (window as unknown as { webkitRTCPeerConnection?: unknown }).webkitRTCPeerConnection || (window as unknown as { mozRTCPeerConnection?: unknown }).mozRTCPeerConnection ); } /** * Check if the browser supports MediaDevices API */ export function supportsMediaDevices(): boolean { return !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia); } /** * Check if the current page is served over HTTPS * (required for microphone access except on localhost) */ export function isHttps(): boolean { return ( window.location.protocol === 'https:' || window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1' ); } /** * Detect if the device is mobile */ export function isMobileDevice(): boolean { return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test( navigator.userAgent ); } /** * Get comprehensive browser information */ export function getBrowserInfo(): BrowserInfo { const { name, version } = detectBrowser(); return { name, version, supportsWebRTC: supportsWebRTC(), supportsMediaDevices: supportsMediaDevices(), isHttps: isHttps(), isMobile: isMobileDevice(), }; } /** * Check if microphone is available * * @returns Promise resolving to true if microphone is available */ export async function checkMicrophoneAvailability(): Promise { if (!supportsMediaDevices()) { console.warn('[DeviceDetection] MediaDevices API not supported'); return false; } try { const devices = await navigator.mediaDevices.enumerateDevices(); const hasMicrophone = devices.some((device) => device.kind === 'audioinput'); console.log('[DeviceDetection] Microphone check:', { hasMicrophone, audioInputCount: devices.filter((d) => d.kind === 'audioinput').length, }); return hasMicrophone; } catch (error) { console.error('[DeviceDetection] Failed to enumerate devices:', error); return false; } } /** * Check if camera is available * * @returns Promise resolving to true if camera is available */ export async function checkCameraAvailability(): Promise { if (!supportsMediaDevices()) { return false; } try { const devices = await navigator.mediaDevices.enumerateDevices(); return devices.some((device) => device.kind === 'videoinput'); } catch (error) { console.error('[DeviceDetection] Failed to enumerate devices:', error); return false; } } /** * Request microphone permission * * @returns Promise resolving to true if permission granted */ export async function requestMicrophonePermission(): Promise { if (!supportsMediaDevices()) { console.error('[DeviceDetection] MediaDevices API not supported'); return false; } try { const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); // Stop the stream immediately - we just needed to get permission stream.getTracks().forEach((track) => track.stop()); console.log('[DeviceDetection] Microphone permission granted'); return true; } catch (error) { console.error('[DeviceDetection] Microphone permission denied:', error); return false; } } /** * Get comprehensive device capabilities * * @returns Promise resolving to device capabilities */ export async function getDeviceCapabilities(): Promise { const browserInfo = getBrowserInfo(); const hasMicrophone = await checkMicrophoneAvailability(); const hasCamera = await checkCameraAvailability(); const canAccessMedia = browserInfo.supportsMediaDevices && browserInfo.supportsWebRTC && browserInfo.isHttps; console.log('[DeviceDetection] Device capabilities:', { browserInfo, hasMicrophone, hasCamera, canAccessMedia, }); return { hasMicrophone, hasCamera, canAccessMedia, browserInfo, }; } /** * Get user-friendly browser instructions for enabling microphone */ export function getMicrophoneInstructions(browserName: BrowserInfo['name']): string { switch (browserName) { case 'Chrome': return 'Click the camera icon in the address bar, then allow microphone access.'; case 'Firefox': return 'Click the microphone icon in the address bar, then select "Allow".'; case 'Safari': return 'Go to Safari > Settings > Websites > Microphone, then allow access for this site.'; case 'Edge': return 'Click the lock icon in the address bar, then allow microphone access.'; default: return 'Please check your browser settings to allow microphone access for this site.'; } } /** * Validate that the environment supports voice demo * * @throws Error if environment is not supported */ export function validateEnvironment(): void { const browserInfo = getBrowserInfo(); if (!browserInfo.supportsWebRTC) { throw new Error( 'Your browser does not support WebRTC. Please use a modern browser like Chrome, Firefox, Safari, or Edge.' ); } if (!browserInfo.supportsMediaDevices) { throw new Error( 'Your browser does not support media devices. Please update to the latest version of your browser.' ); } if (!browserInfo.isHttps) { throw new Error( 'Voice demo requires a secure connection (HTTPS). Please access this page over HTTPS.' ); } }