/** * Daily.co screenshare integration utilities * Handles screensharing of Slidev presentations hosted on GitHub Pages */ export interface ScreenshareOptions { deckUrl: string; maxQuality?: 'low' | 'medium' | 'high'; width?: number; height?: number; } export interface ScreenshareResult { success: boolean; windowHandle?: Window | null; error?: string; } /** * Start screensharing with a Slidev deck URL * Opens the deck in a popup window and initiates Daily.co screenshare * * @param options - Configuration for the screenshare * @returns Promise with the result of the screenshare operation */ export async function startScreenshare( options: ScreenshareOptions ): Promise { const { deckUrl, maxQuality = 'high', width = 1920, height = 1080, } = options; try { // Validate deckUrl if (!deckUrl || typeof deckUrl !== 'string') { throw new Error('Invalid deck URL provided'); } // Check if Daily.co call frame is available const callFrame = (window as unknown as Record).callFrame; if (!callFrame) { throw new Error('Daily.co call frame not available. Make sure you are in an active call.'); } // Open the deck in a new window for screensharing // Use specific dimensions and features to ensure optimal presentation const windowFeatures = `width=${width},height=${height},menubar=no,toolbar=no,location=no,status=no`; const deckWindow = window.open(deckUrl, '_blank', windowFeatures); if (!deckWindow) { throw new Error( 'Failed to open presentation window. Please allow popups for this site and try again.' ); } // Wait a brief moment for the window to load before starting screenshare await new Promise((resolve) => setTimeout(resolve, 500)); // Start screenshare with high quality settings await (callFrame as { startScreenShare: (options: Record) => Promise }).startScreenShare({ screenVideoSendSettings: { maxQuality: maxQuality, }, displayMediaOptions: { audio: false, // Don't capture audio from the presentation selfBrowserSurface: 'exclude', // Exclude the current browser surface surfaceSwitching: 'include', // Allow switching surfaces video: { width: width, height: height, }, }, }); return { success: true, windowHandle: deckWindow, }; } catch (error) { console.error('Screenshare error:', error); let errorMessage = 'Failed to start screenshare'; if (error instanceof Error) { errorMessage = error.message; } else if (typeof error === 'string') { errorMessage = error; } // Provide more helpful error messages for common issues if (errorMessage.includes('Permission denied') || errorMessage.includes('NotAllowedError')) { errorMessage = 'Screenshare permission denied. Please allow screensharing and try again.'; } else if (errorMessage.includes('popup')) { errorMessage = 'Popup blocked. Please enable popups for this site to share presentations.'; } return { success: false, error: errorMessage, }; } } /** * Stop the current screenshare * * @returns Promise that resolves when screenshare is stopped */ export async function stopScreenshare(): Promise { try { const callFrame = (window as unknown as Record).callFrame; if (!callFrame) { throw new Error('Daily.co call frame not available'); } await (callFrame as { stopScreenShare: () => Promise }).stopScreenShare(); } catch (error) { console.error('Error stopping screenshare:', error); throw error; } } /** * Check if screenshare is currently active * * @returns boolean indicating if screenshare is active */ export function isScreensharing(): boolean { try { const callFrame = (window as unknown as Record).callFrame; if (!callFrame) { return false; } // Check the participants state to see if local user is screensharing const participants = (callFrame as { participants: () => { local?: { screen?: boolean } } }).participants(); const localParticipant = participants?.local; return localParticipant?.screen === true; } catch (error) { console.error('Error checking screenshare status:', error); return false; } } /** * Get the Daily.co call frame if available * * @returns The call frame object or null if not available */ export function getCallFrame(): unknown | null { return (window as unknown as Record).callFrame || null; }