Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
refactor(audio): clean up system audio capability plumbing
Gabriel Stein620bab354f4a
diff --git a/src/components/IntegrationsView.tsx b/src/components/IntegrationsView.tsxindex d4233a573..ae540dfe5 100644--- a/src/components/IntegrationsView.tsx+++ b/src/components/IntegrationsView.tsx@@ -7,6 +7,7 @@ import { SettingsPanel, SettingsPanelRow } from "./ui/SettingsSection";import { ConfirmDialog } from "./ui/dialog";import { useSettingsStore } from "../stores/settingsStore";import { useSystemAudioPermission } from "../hooks/useSystemAudioPermission";+import { canManageSystemAudioInApp } from "../utils/systemAudioAccess";import googleCalendarIcon from "../assets/icons/google-calendar.svg";export default function IntegrationsView() {@@ -18,10 +19,7 @@ export default function IntegrationsView() {const [showPermissionDialog, setShowPermissionDialog] = useState(false);const systemAudio = useSystemAudioPermission();const hasAccounts = gcalAccounts.length > 0;- const needsSystemAudioGrant =- !systemAudio.granted &&- (systemAudio.mode === "native" ||- (systemAudio.mode === "portal" && systemAudio.supportsOnboardingGrant));+ const needsSystemAudioGrant = !systemAudio.granted && canManageSystemAudioInApp(systemAudio);const startOAuth = useCallback(async () => {setIsConnecting(true);diff --git a/src/components/SettingsPage.tsx b/src/components/SettingsPage.tsxindex 003419997..30c26ee20 100644--- a/src/components/SettingsPage.tsx+++ b/src/components/SettingsPage.tsx@@ -80,6 +80,7 @@ import { cn } from "./lib/utils";import { startMigration, useMigration } from "../stores/noteStore.js";import { formatBytes } from "../utils/formatBytes";import { useSettingsStore } from "../stores/settingsStore";+import { canManageSystemAudioInApp } from "../utils/systemAudioAccess";const formatAmount = (cents: number, currency: string) =>(cents / 100).toLocaleString(undefined, { style: "currency", currency });@@ -2963,17 +2964,19 @@ EOF`,disabled={isHotkeyRegistering}validate={validateDictationHotkey}/>- {effectiveDefaultHotkey && dictationKey && dictationKey !== effectiveDefaultHotkey && (- <button- onClick={() => registerHotkey(effectiveDefaultHotkey)}- disabled={isHotkeyRegistering}- className="mt-2 text-xs text-muted-foreground/70 hover:text-foreground transition-colors disabled:opacity-50"- >- {t("settingsPage.general.hotkey.resetToDefault", {- hotkey: formatHotkeyLabel(effectiveDefaultHotkey),- })}- </button>- )}+ {effectiveDefaultHotkey &&+ dictationKey &&+ dictationKey !== effectiveDefaultHotkey && (+ <button+ onClick={() => registerHotkey(effectiveDefaultHotkey)}+ disabled={isHotkeyRegistering}+ className="mt-2 text-xs text-muted-foreground/70 hover:text-foreground transition-colors disabled:opacity-50"+ >+ {t("settingsPage.general.hotkey.resetToDefault", {+ hotkey: formatHotkeyLabel(effectiveDefaultHotkey),+ })}+ </button>+ )}</SettingsPanelRow>{!isUsingNativeShortcut && (@@ -3498,8 +3501,7 @@ EOF`,buttonText={t("settingsPage.permissions.grantAccess")}/>- {(platform === "darwin" ||- (platform === "linux" && systemAudio.supportsOnboardingGrant)) && (+ {(platform === "darwin" || canManageSystemAudioInApp(systemAudio)) && (<>{platform === "darwin" && (<PermissionCard@@ -3511,8 +3513,7 @@ EOF`,buttonText={t("settingsPage.permissions.grantAccess")}/>)}- {(systemAudio.mode === "native" ||- (systemAudio.mode === "portal" && systemAudio.supportsOnboardingGrant)) && (+ {canManageSystemAudioInApp(systemAudio) && (<PermissionCardicon={Monitor}title={t("settingsPage.permissions.systemAudioTitle")}diff --git a/src/components/UpcomingMeetings.tsx b/src/components/UpcomingMeetings.tsxindex 77f4c1004..0fa1e45a6 100644--- a/src/components/UpcomingMeetings.tsx+++ b/src/components/UpcomingMeetings.tsx@@ -7,6 +7,7 @@ import type { CalendarEvent } from "../types/calendar";import { formatUpcomingDateGroup } from "../utils/dateFormatting";import { useSystemAudioPermission } from "../hooks/useSystemAudioPermission";import { useSettingsStore } from "../stores/settingsStore";+import { canManageSystemAudioInApp } from "../utils/systemAudioAccess";interface UpcomingMeetingsProps {events: CalendarEvent[];@@ -41,10 +42,7 @@ export default function UpcomingMeetings({ events, isLoading }: UpcomingMeetingsconst [hoveredEventId, setHoveredEventId] = useState<string | null>(null);const systemAudio = useSystemAudioPermission();const isSignedIn = useSettingsStore((s) => s.isSignedIn);- const needsSystemAudioGrant =- !systemAudio.granted &&- (systemAudio.mode === "native" ||- (systemAudio.mode === "portal" && systemAudio.supportsOnboardingGrant));+ const needsSystemAudioGrant = !systemAudio.granted && canManageSystemAudioInApp(systemAudio);const now = useMemo(() => new Date(), []);diff --git a/src/components/notes/NotesOnboarding.tsx b/src/components/notes/NotesOnboarding.tsxindex 5fbdfeaee..af9eb9145 100644--- a/src/components/notes/NotesOnboarding.tsx+++ b/src/components/notes/NotesOnboarding.tsx@@ -16,6 +16,7 @@ import { useDialogs } from "../../hooks/useDialogs";import { AlertDialog } from "../ui/dialog";import ReasoningModelSelector from "../ReasoningModelSelector";import { useSystemAudioPermission } from "../../hooks/useSystemAudioPermission";+import { canManageSystemAudioInApp } from "../../utils/systemAudioAccess";interface NotesOnboardingProps {onComplete: () => void;@@ -58,9 +59,10 @@ export default function NotesOnboarding({ onComplete }: NotesOnboardingProps) {request: requestSystemAudio,} = useSystemAudioPermission();const [isRequestingSystemAudio, setIsRequestingSystemAudio] = useState(false);- const shouldShowSystemAudioPermission =- systemAudioMode === "native" ||- (systemAudioMode === "portal" && systemAudioSupportsOnboardingGrant);+ const shouldShowSystemAudioPermission = canManageSystemAudioInApp({+ mode: systemAudioMode,+ supportsOnboardingGrant: systemAudioSupportsOnboardingGrant,+ });const handleGrantSystemAudio = useCallback(async () => {setIsRequestingSystemAudio(true);diff --git a/src/components/ui/PermissionsSection.tsx b/src/components/ui/PermissionsSection.tsxindex f899d672d..bfa52fc6c 100644--- a/src/components/ui/PermissionsSection.tsx+++ b/src/components/ui/PermissionsSection.tsx@@ -5,6 +5,7 @@ import MicPermissionWarning from "./MicPermissionWarning";import PasteToolsInfo from "./PasteToolsInfo";import type { UsePermissionsReturn } from "../../hooks/usePermissions";import type { SystemAudioAccessResult } from "../../types/electron";+import { canManageSystemAudioInApp } from "../../utils/systemAudioAccess";interface PermissionsSectionProps {permissions: UsePermissionsReturn;@@ -17,9 +18,7 @@ export default function PermissionsSection({ permissions, systemAudio }: Permissconst { t } = useTranslation();const platform = permissions.pasteToolsInfo?.platform;const isMacOS = platform === "darwin";- const shouldShowSystemAudioPermission =- systemAudio.mode === "native" ||- (systemAudio.mode === "portal" && systemAudio.supportsOnboardingGrant);+ const shouldShowSystemAudioPermission = canManageSystemAudioInApp(systemAudio);return (<>diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.jsindex ee2efdf97..da3c05519 100644--- a/src/helpers/ipcHandlers.js+++ b/src/helpers/ipcHandlers.js@@ -1600,9 +1600,13 @@ class IPCHandlers {if (!info?.hotkey) continue;if (!usesNativeListener(info.hotkey)) {- debugLogger.log(`[IPC] Unregistering globalShortcut "${info.hotkey}" (slot "${slot}") for capture mode`);+ debugLogger.log(+ `[IPC] Unregistering globalShortcut "${info.hotkey}" (slot "${slot}") for capture mode`+ );const { globalShortcut } = require("electron");- try { globalShortcut.unregister(info.hotkey); } catch {}+ try {+ globalShortcut.unregister(info.hotkey);+ } catch {}}}@@ -1615,7 +1619,9 @@ class IPCHandlers {// On GNOME, unregister all native keybindings during captureif (hotkeyManager.isUsingGnome() && hotkeyManager.gnomeManager) {for (const slot of [...hotkeyManager.gnomeManager.registeredSlots]) {- debugLogger.log(`[IPC] Unregistering GNOME keybinding (slot "${slot}") for capture mode`);+ debugLogger.log(+ `[IPC] Unregistering GNOME keybinding (slot "${slot}") for capture mode`+ );await hotkeyManager.gnomeManager.unregisterKeybinding(slot).catch((err) => {debugLogger.warn(`[IPC] Failed to unregister GNOME slot "${slot}":`, err.message);});@@ -1642,7 +1648,10 @@ class IPCHandlers {} else {// Exiting capture mode - re-register globalShortcut if not already registered// Skip for KDE/GNOME/Hyprland — updateHotkey handles re-registration via native path- const usesNativePath = hotkeyManager.isUsingKDE() || hotkeyManager.isUsingGnome() || hotkeyManager.isUsingHyprland();+ const usesNativePath =+ hotkeyManager.isUsingKDE() ||+ hotkeyManager.isUsingGnome() ||+ hotkeyManager.isUsingHyprland();if (effectiveHotkey && !usesNativeListener(effectiveHotkey) && !usesNativePath) {const { globalShortcut } = require("electron");const accelerator = effectiveHotkey.startsWith("Fn+")@@ -1710,18 +1719,28 @@ class IPCHandlers {`[IPC] Re-registering KDE keybinding "${effectiveHotkey}" after capture mode`);const callback = this.windowManager.createHotkeyCallback();- const result = await hotkeyManager.kdeManager.registerKeybinding(effectiveHotkey, "dictation", callback);+ const result = await hotkeyManager.kdeManager.registerKeybinding(+ effectiveHotkey,+ "dictation",+ callback+ );if (result === true) {hotkeyManager.currentHotkey = effectiveHotkey;} else {- debugLogger.warn(`[IPC] Failed to re-register KDE keybinding "${effectiveHotkey}" after capture mode`, { result });+ debugLogger.warn(+ `[IPC] Failed to re-register KDE keybinding "${effectiveHotkey}" after capture mode`,+ { result }+ );}}// Re-register non-dictation slots (meeting, agent) that were unregistered on capture enterfor (const [slot, info] of hotkeyManager.slots) {- if (slot === "dictation" || slot === "cancel" || !info?.hotkey || !info?.callback) continue;- debugLogger.log(`[IPC] Re-registering slot "${slot}" ("${info.hotkey}") after capture mode`);+ if (slot === "dictation" || slot === "cancel" || !info?.hotkey || !info?.callback)+ continue;+ debugLogger.log(+ `[IPC] Re-registering slot "${slot}" ("${info.hotkey}") after capture mode`+ );await hotkeyManager.registerSlot(slot, info.hotkey, info.callback).catch((err) => {debugLogger.warn(`[IPC] Failed to re-register slot "${slot}":`, err.message);});@@ -2437,6 +2456,8 @@ class IPCHandlers {status: "unsupported",mode: "unsupported",supportsPersistentGrant: false,+ supportsPersistentPortalGrant: false,+ supportsNativeCapture: false,supportsOnboardingGrant: false,requiresRuntimeSharePrompt: false,strategy: "unsupported",@@ -2449,10 +2470,14 @@ class IPCHandlers {const capability = await this.linuxPortalAudioManager?.getCapability().catch((error) => ({available: false,supportsPersistentGrant: false,+ supportsPersistentPortalGrant: false,+ supportsNativeCapture: false,portalVersion: null,error: error.message,}));const supportsPersistentGrant = !!capability?.supportsPersistentGrant;+ const supportsPersistentPortalGrant = !!capability?.supportsPersistentPortalGrant;+ const supportsNativeCapture = !!capability?.supportsNativeCapture;const restoreTokenAvailable =supportsPersistentGrant && !!this.linuxPortalAudioManager?.hasStoredRestoreToken();const helperError =@@ -2470,8 +2495,10 @@ class IPCHandlers {: "unknown",mode: "portal",supportsPersistentGrant,+ supportsPersistentPortalGrant,+ supportsNativeCapture,supportsOnboardingGrant: supportsPersistentGrant,- requiresRuntimeSharePrompt: !restoreTokenAvailable,+ requiresRuntimeSharePrompt: !supportsPersistentGrant || !restoreTokenAvailable,strategy: supportsPersistentGrant ? "portal-helper" : "browser-portal",restoreTokenAvailable,portalVersion: capability?.portalVersion ?? null,@@ -3315,22 +3342,15 @@ class IPCHandlers {debugLogger.debug("Meeting transcription start: reusing warm connections");const win = BrowserWindow.fromWebContents(event.sender);attachMeetingStreamingHandlers(this._meetingMicStreaming, win, "mic");- if (systemAudioMode === "native") {- attachMeetingStreamingHandlers(this._meetingSystemStreaming, win, "system");- await startNativeMeetingSystemAudio(event);- } else if (systemAudioStrategy === "portal-helper") {+ if (systemAudioMode !== "unsupported") {attachMeetingStreamingHandlers(this._meetingSystemStreaming, win, "system");- try {- await startLinuxMeetingSystemAudio(event);- } catch (error) {- debugLogger.warn(- "Linux portal helper failed during warm-start reuse, falling back to browser portal",- { error: error.message },- "meeting"- );- systemAudioStrategy = "browser-portal";- }}+ systemAudioStrategy = await startMeetingSystemAudio(+ event,+ systemAudioMode,+ systemAudioStrategy,+ "during warm-start reuse"+ );return { success: true, systemAudioMode, systemAudioStrategy };}@@ -3346,20 +3366,12 @@ class IPCHandlers {transcribeAllLocalBuffers();}, 5000);- if (systemAudioMode === "native") {- await startNativeMeetingSystemAudio(event);- } else if (systemAudioStrategy === "portal-helper") {- try {- await startLinuxMeetingSystemAudio(event);- } catch (error) {- debugLogger.warn(- "Linux portal helper failed in local meeting mode, falling back to browser portal",- { error: error.message },- "meeting"- );- systemAudioStrategy = "browser-portal";- }- }+ systemAudioStrategy = await startMeetingSystemAudio(+ event,+ systemAudioMode,+ systemAudioStrategy,+ "in local meeting mode"+ );debugLogger.debug("Meeting transcription started in local mode", {provider: meetingLocalProvider,@@ -3375,20 +3387,12 @@ class IPCHandlers {}await connectRealtimeStreaming(event, options);- if (systemAudioMode === "native") {- await startNativeMeetingSystemAudio(event);- } else if (systemAudioStrategy === "portal-helper") {- try {- await startLinuxMeetingSystemAudio(event);- } catch (error) {- debugLogger.warn(- "Linux portal helper failed in realtime mode, falling back to browser portal",- { error: error.message },- "meeting"- );- systemAudioStrategy = "browser-portal";- }- }+ systemAudioStrategy = await startMeetingSystemAudio(+ event,+ systemAudioMode,+ systemAudioStrategy,+ "in realtime mode"+ );return { success: true, systemAudioMode, systemAudioStrategy };} catch (error) {await rollbackMeetingTranscriptionStart();@@ -3463,6 +3467,34 @@ class IPCHandlers {});};+ const startMeetingSystemAudio = async (+ event,+ systemAudioMode,+ systemAudioStrategy,+ context+ ) => {+ if (systemAudioMode === "native") {+ await startNativeMeetingSystemAudio(event);+ return systemAudioStrategy;+ }++ if (systemAudioStrategy !== "portal-helper") {+ return systemAudioStrategy;+ }++ try {+ await startLinuxMeetingSystemAudio(event);+ return systemAudioStrategy;+ } catch (error) {+ debugLogger.warn(+ `Linux portal helper failed ${context}, falling back to browser portal`,+ { error: error.message },+ "meeting"+ );+ return "browser-portal";+ }+ };+ipcMain.on("meeting-transcription-send", (_event, audioBuffer, source) => {sendMeetingAudio(audioBuffer, source);});diff --git a/src/helpers/linuxPortalAudioManager.js b/src/helpers/linuxPortalAudioManager.jsindex 27bec0ff0..c8824cdf1 100644--- a/src/helpers/linuxPortalAudioManager.js+++ b/src/helpers/linuxPortalAudioManager.js@@ -38,7 +38,9 @@ class LinuxPortalAudioManager {if (process.resourcesPath) {candidates.add(path.join(process.resourcesPath, "linux-system-audio-helper"));candidates.add(path.join(process.resourcesPath, "bin", "linux-system-audio-helper"));- candidates.add(path.join(process.resourcesPath, "resources", "bin", "linux-system-audio-helper"));+ candidates.add(+ path.join(process.resourcesPath, "resources", "bin", "linux-system-audio-helper")+ );candidates.add(path.join(process.resourcesPath,@@ -174,11 +176,7 @@ class LinuxPortalAudioManager {await new Promise((resolve, reject) => {let settled = false;const timeout = setTimeout(() => {- finish(- reject,- new Error("Timed out starting Linux portal system audio capture."),- true- );+ finish(reject, new Error("Timed out starting Linux portal system audio capture."), true);}, START_TIMEOUT_MS);const finish = (callback, value, shouldStop = false) => {@@ -351,18 +349,16 @@ class LinuxPortalAudioManager {const supportsSystemAudio = !!result?.supportsSystemAudio;const supportsNativeCapture = !!result?.supportsNativeCapture;const available = !!result?.ok;- const supportsPersistentGrant =- available &&- supportsPersistMode &&- supportsRestoreToken &&- supportsSystemAudio &&- supportsNativeCapture;+ const supportsPersistentPortalGrant =+ available && supportsPersistMode && supportsRestoreToken && supportsSystemAudio;+ const supportsPersistentGrant = supportsPersistentPortalGrant && supportsNativeCapture;return {available,portalVersion,supportsPersistMode,supportsRestoreToken,+ supportsPersistentPortalGrant,supportsNativeCapture,supportsPersistentGrant,supportsSystemAudio,diff --git a/src/hooks/useMeetingTranscription.ts b/src/hooks/useMeetingTranscription.tsindex 508014ac7..700b98d9f 100644--- a/src/hooks/useMeetingTranscription.ts+++ b/src/hooks/useMeetingTranscription.ts@@ -3,6 +3,12 @@ import { getSettings } from "../stores/settingsStore";import { isBuiltInMicrophone } from "../utils/audioDeviceUtils";import { getCachedPlatform } from "../utils/platform";import type { SystemAudioAccessResult, SystemAudioStrategy } from "../types/electron";+import {+ DEFAULT_SYSTEM_AUDIO_ACCESS,+ getDisplayCaptureModeForStrategy,+ getFallbackSystemAudioAccess,+ isRendererSystemAudioStrategy,+} from "../utils/systemAudioAccess";import logger from "../utils/logger";export interface TranscriptSegment {@@ -29,17 +35,6 @@ const MEETING_AUDIO_BUFFER_SIZE = 800;const MEETING_STOP_FLUSH_TIMEOUT_MS = 50;const REALTIME_MODELS = new Set(["gpt-4o-mini-transcribe", "gpt-4o-transcribe"]);-const DEFAULT_SYSTEM_AUDIO_ACCESS: SystemAudioAccessResult = {- granted: false,- status: "unsupported",- mode: "unsupported",- supportsPersistentGrant: false,- supportsOnboardingGrant: false,- requiresRuntimeSharePrompt: false,- strategy: "unsupported",- restoreTokenAvailable: false,- portalVersion: null,-};const getMeetingTranscriptionOptions = () => {const {@@ -70,40 +65,6 @@ const getMeetingTranscriptionOptions = () => {return { provider: "openai-realtime" as const, model, mode };};-const getFallbackSystemAudioAccess = (): SystemAudioAccessResult => {- const platform = getCachedPlatform();-- if (platform === "win32") {- return {- ...DEFAULT_SYSTEM_AUDIO_ACCESS,- granted: true,- status: "granted",- mode: "loopback",- strategy: "loopback",- };- }-- if (platform === "linux") {- return {- ...DEFAULT_SYSTEM_AUDIO_ACCESS,- status: "unknown",- mode: "portal",- requiresRuntimeSharePrompt: true,- strategy: "browser-portal",- };- }-- return DEFAULT_SYSTEM_AUDIO_ACCESS;-};--const isRendererSystemAudioStrategy = (- strategy: SystemAudioStrategy | undefined | null-): strategy is "loopback" | "browser-portal" =>- strategy === "loopback" || strategy === "browser-portal";--const getDisplayCaptureModeForStrategy = (strategy: "loopback" | "browser-portal") =>- strategy === "loopback" ? "loopback" : "portal";-const stopMediaStream = (stream: MediaStream | null) => {try {stream?.getTracks().forEach((track) => track.stop());@@ -145,6 +106,48 @@ const requestSystemAudioDisplayStream = async (mode: "loopback" | "portal") => {}};+const prepareMeetingSystemAudioCapture = (initialSystemAudioAccess: SystemAudioAccessResult) => {+ const initialSystemAudioStrategy = initialSystemAudioAccess.strategy ?? "unsupported";+ const initialDisplayCaptureStrategy = isRendererSystemAudioStrategy(initialSystemAudioStrategy)+ ? initialSystemAudioStrategy+ : null;+ const systemCapturePromise = initialDisplayCaptureStrategy+ ? requestSystemAudioDisplayStream(+ getDisplayCaptureModeForStrategy(initialDisplayCaptureStrategy)+ )+ : Promise.resolve({ stream: null, error: null });++ return {+ initialSystemAudioStrategy,+ initialDisplayCaptureStrategy,+ systemCapturePromise,+ };+};++const ensureRendererSystemAudioCapture = async ({+ allowSystemAudio,+ initialDisplayCaptureStrategy,+ systemAudioStrategy,+ systemCaptureResult,+}: {+ allowSystemAudio: boolean;+ initialDisplayCaptureStrategy: "loopback" | "browser-portal" | null;+ systemAudioStrategy: SystemAudioStrategy;+ systemCaptureResult: { stream: MediaStream | null; error: Error | null };+}) => {+ if (+ !allowSystemAudio ||+ systemCaptureResult.stream ||+ systemCaptureResult.error ||+ !isRendererSystemAudioStrategy(systemAudioStrategy) ||+ initialDisplayCaptureStrategy+ ) {+ return systemCaptureResult;+ }++ return requestSystemAudioDisplayStream(getDisplayCaptureModeForStrategy(systemAudioStrategy));+};+const getMeetingWorkletBlobUrl = (() => {let blobUrl: string | null = null;@@ -449,320 +452,307 @@ export function useMeetingTranscription(): UseMeetingTranscriptionReturn {await promise;}, []);- const startTranscription = useCallback(async (options?: { allowSystemAudio?: boolean }) => {- if (isRecordingRef.current || isStartingRef.current) return;- isStartingRef.current = true;- const allowSystemAudio = options?.allowSystemAudio ?? true;- const systemAudioAccessPromise = allowSystemAudio- ? window.electronAPI?.checkSystemAudioAccess?.()- : Promise.resolve(DEFAULT_SYSTEM_AUDIO_ACCESS);-- if (!allowSystemAudio && getCachedPlatform() === "linux") {- logger.info(- "Linux system audio skipped for non-user-initiated start, continuing with mic only",- {},- "meeting"- );- }-- logger.info("Meeting transcription starting...", {}, "meeting");- setTranscript("");- setPartialTranscript("");- setSegments([]);- setMicPartial("");- setSystemPartial("");- setError(null);-- // Set recording state immediately for instant UI feedback- isRecordingRef.current = true;- setIsRecording(true);-- // Wait for in-flight prepare to reuse the warm connection- if (preparePromiseRef.current) {- logger.debug("Waiting for in-flight prepare to finish...", {}, "meeting");- await preparePromiseRef.current;- }-- try {- const startTime = performance.now();- const initialSystemAudioAccess =- (await systemAudioAccessPromise) ?? getFallbackSystemAudioAccess();- const initialSystemAudioStrategy = initialSystemAudioAccess.strategy ?? "unsupported";- const initialDisplayCaptureStrategy = isRendererSystemAudioStrategy(- initialSystemAudioStrategy- )- ? initialSystemAudioStrategy- : null;- const systemCapturePromise = initialDisplayCaptureStrategy- ? requestSystemAudioDisplayStream(- getDisplayCaptureModeForStrategy(initialDisplayCaptureStrategy)- )- : Promise.resolve({ stream: null, error: null });-- const [startResult, micResult, initialSystemCaptureResult] = await Promise.all([- window.electronAPI?.meetingTranscriptionStart?.({- ...getMeetingTranscriptionOptions(),- allowSystemAudio,- }),- getMeetingMicConstraints().then((constraints) =>- navigator.mediaDevices.getUserMedia(constraints).catch((err) => {- logger.error(- "Mic capture failed, continuing with system audio only",- { error: (err as Error).message },- "meeting"- );- return null;- })- ),- systemCapturePromise,- ]);- let systemCaptureResult = initialSystemCaptureResult;-- const streamsMs = performance.now() - startTime;- // Abort if stop was called during setup- if (!isRecordingRef.current) {- logger.info("Meeting transcription aborted during setup (stop called)", {}, "meeting");- stopMediaStream(micResult);- stopMediaStream(systemCaptureResult.stream);- isStartingRef.current = false;- return;- }+ const startTranscription = useCallback(+ async (options?: { allowSystemAudio?: boolean }) => {+ if (isRecordingRef.current || isStartingRef.current) return;+ isStartingRef.current = true;+ const allowSystemAudio = options?.allowSystemAudio ?? true;+ const systemAudioAccessPromise = allowSystemAudio+ ? window.electronAPI?.checkSystemAudioAccess?.()+ : Promise.resolve(DEFAULT_SYSTEM_AUDIO_ACCESS);- if (!startResult?.success) {- logger.error(- "Meeting transcription IPC start failed",- { error: startResult?.error },+ if (!allowSystemAudio && getCachedPlatform() === "linux") {+ logger.info(+ "Linux system audio skipped for non-user-initiated start, continuing with mic only",+ {},"meeting");- setError(startResult?.error || "Failed to start meeting transcription");- stopMediaStream(micResult);- stopMediaStream(systemCaptureResult.stream);- isRecordingRef.current = false;- isStartingRef.current = false;- setIsRecording(false);- return;}- const systemAudioMode = startResult.systemAudioMode || initialSystemAudioAccess.mode;- let systemAudioStrategy = startResult.systemAudioStrategy || initialSystemAudioStrategy;-- if (- allowSystemAudio &&- !systemCaptureResult.stream &&- !systemCaptureResult.error &&- isRendererSystemAudioStrategy(systemAudioStrategy) &&- !initialDisplayCaptureStrategy- ) {- systemCaptureResult = await requestSystemAudioDisplayStream(- getDisplayCaptureModeForStrategy(systemAudioStrategy)- );+ logger.info("Meeting transcription starting...", {}, "meeting");+ setTranscript("");+ setPartialTranscript("");+ setSegments([]);+ setMicPartial("");+ setSystemPartial("");+ setError(null);++ // Set recording state immediately for instant UI feedback+ isRecordingRef.current = true;+ setIsRecording(true);++ // Wait for in-flight prepare to reuse the warm connection+ if (preparePromiseRef.current) {+ logger.debug("Waiting for in-flight prepare to finish...", {}, "meeting");+ await preparePromiseRef.current;}- const systemAudioHandledInMain =- systemAudioMode !== "unsupported" && !isRendererSystemAudioStrategy(systemAudioStrategy);- const systemCaptureError =- systemAudioHandledInMain ? null : systemCaptureResult.error;-- if (!micResult && !systemCaptureResult.stream && !systemAudioHandledInMain) {- logger.error("Meeting transcription has no available audio source", {}, "meeting");- setError(- systemAudioMode === "unsupported"- ? "No microphone is available and system audio capture is unsupported on this device."- : systemCaptureError?.message ||- "No microphone is available and system audio capture could not be started."- );- await window.electronAPI?.meetingTranscriptionStop?.();- isRecordingRef.current = false;- isStartingRef.current = false;- setIsRecording(false);- return;- }+ try {+ const startTime = performance.now();+ const initialSystemAudioAccess =+ (await systemAudioAccessPromise) ?? getFallbackSystemAudioAccess();+ const { initialSystemAudioStrategy, initialDisplayCaptureStrategy, systemCapturePromise } =+ prepareMeetingSystemAudioCapture(initialSystemAudioAccess);++ const [startResult, micResult, initialSystemCaptureResult] = await Promise.all([+ window.electronAPI?.meetingTranscriptionStart?.({+ ...getMeetingTranscriptionOptions(),+ allowSystemAudio,+ }),+ getMeetingMicConstraints().then((constraints) =>+ navigator.mediaDevices.getUserMedia(constraints).catch((err) => {+ logger.error(+ "Mic capture failed, continuing with system audio only",+ { error: (err as Error).message },+ "meeting"+ );+ return null;+ })+ ),+ systemCapturePromise,+ ]);+ let systemCaptureResult = initialSystemCaptureResult;++ const streamsMs = performance.now() - startTime;+ // Abort if stop was called during setup+ if (!isRecordingRef.current) {+ logger.info("Meeting transcription aborted during setup (stop called)", {}, "meeting");+ stopMediaStream(micResult);+ stopMediaStream(systemCaptureResult.stream);+ isStartingRef.current = false;+ return;+ }- const partialSetters = { mic: setMicPartial, system: setSystemPartial };-- const segmentCleanup = window.electronAPI?.onMeetingTranscriptionSegment?.(- (data: {- text: string;- source: "mic" | "system";- type: "partial" | "final";- timestamp?: number;- }) => {- logger.debug(- "Meeting segment received in renderer",- {- source: data.source,- type: data.type,- text: data.text?.slice(0, 80),- },+ if (!startResult?.success) {+ logger.error(+ "Meeting transcription IPC start failed",+ { error: startResult?.error },"meeting");- const setPartialForSource = partialSetters[data.source];-- if (data.type === "partial") {- setPartialForSource(data.text);- setPartialTranscript(data.text);- } else {- const seg: TranscriptSegment = {- id: `seg-${++segmentCounter}`,- text: data.text,- source: data.source,- timestamp: data.timestamp,- };- setSegments((prev) => {- // Insert in chronological order — scan from the end since most- // segments arrive in order and this is O(1) in the common case.- const ts = seg.timestamp ?? Infinity;- let i = prev.length;- while (i > 0 && (prev[i - 1].timestamp ?? 0) > ts) i--;- if (i === prev.length) return [...prev, seg];- return [...prev.slice(0, i), seg, ...prev.slice(i)];- });- setPartialForSource("");- setTranscript((prev) => (prev ? prev + " " + data.text : data.text));- setPartialTranscript("");- }+ setError(startResult?.error || "Failed to start meeting transcription");+ stopMediaStream(micResult);+ stopMediaStream(systemCaptureResult.stream);+ isRecordingRef.current = false;+ isStartingRef.current = false;+ setIsRecording(false);+ return;}- );- if (segmentCleanup) ipcCleanupsRef.current.push(segmentCleanup);-- const errorCleanup = window.electronAPI?.onMeetingTranscriptionError?.((err) => {- setError(err);- logger.error("Meeting transcription stream error", { error: err }, "meeting");- });- if (errorCleanup) ipcCleanupsRef.current.push(errorCleanup);-- const pendingMicChunks: ArrayBuffer[] = [];- const pendingSystemChunks: ArrayBuffer[] = [];- let socketReady = false;-- let micPipelinePromise: Promise<void> | null = null;- if (micResult) {- micStreamRef.current = micResult;- const micContext = new AudioContext({ sampleRate: 24000 });- await detachFromOutputDevice(micContext);- micContextRef.current = micContext;-- micPipelinePromise = createAudioPipeline({- stream: micResult,- context: micContext,- label: "Meeting mic",- onChunk: (chunk) => {- if (!isRecordingRef.current) return;- if (socketReady) {- window.electronAPI?.meetingTranscriptionSend?.(chunk, "mic");- return;- }- pendingMicChunks.push(chunk.slice(0));- },- }).then(({ source, processor }) => {- micSourceRef.current = source;- micProcessorRef.current = processor;- const micTrack = micResult.getAudioTracks()[0];- logger.info(- "Mic capture started for meeting transcription",- {- label: micTrack?.label,- settings: micTrack?.getSettings(),- },- "meeting"- );+ const systemAudioMode = startResult.systemAudioMode || initialSystemAudioAccess.mode;+ let systemAudioStrategy = startResult.systemAudioStrategy || initialSystemAudioStrategy;+ systemCaptureResult = await ensureRendererSystemAudioCapture({+ allowSystemAudio,+ initialDisplayCaptureStrategy,+ systemAudioStrategy,+ systemCaptureResult,});- }- if (micPipelinePromise) {- await micPipelinePromise;- }+ const systemAudioHandledInMain =+ systemAudioMode !== "unsupported" && !isRendererSystemAudioStrategy(systemAudioStrategy);+ const systemCaptureError = systemAudioHandledInMain ? null : systemCaptureResult.error;++ if (!micResult && !systemCaptureResult.stream && !systemAudioHandledInMain) {+ logger.error("Meeting transcription has no available audio source", {}, "meeting");+ setError(+ systemAudioMode === "unsupported"+ ? "No microphone is available and system audio capture is unsupported on this device."+ : systemCaptureError?.message ||+ "No microphone is available and system audio capture could not be started."+ );+ await window.electronAPI?.meetingTranscriptionStop?.();+ isRecordingRef.current = false;+ isStartingRef.current = false;+ setIsRecording(false);+ return;+ }- if (systemCaptureResult.stream) {- const systemStream = systemCaptureResult.stream;- systemStreamRef.current = systemStream;-- const systemContext = new AudioContext({ sampleRate: 24000 });- await detachFromOutputDevice(systemContext);- systemContextRef.current = systemContext;-- await createAudioPipeline({- stream: systemStream,- context: systemContext,- label: "Meeting system",- onChunk: (chunk) => {- if (!isRecordingRef.current) return;- if (socketReady) {- window.electronAPI?.meetingTranscriptionSend?.(chunk, "system");- return;+ const partialSetters = { mic: setMicPartial, system: setSystemPartial };++ const segmentCleanup = window.electronAPI?.onMeetingTranscriptionSegment?.(+ (data: {+ text: string;+ source: "mic" | "system";+ type: "partial" | "final";+ timestamp?: number;+ }) => {+ logger.debug(+ "Meeting segment received in renderer",+ {+ source: data.source,+ type: data.type,+ text: data.text?.slice(0, 80),+ },+ "meeting"+ );+ const setPartialForSource = partialSetters[data.source];++ if (data.type === "partial") {+ setPartialForSource(data.text);+ setPartialTranscript(data.text);+ } else {+ const seg: TranscriptSegment = {+ id: `seg-${++segmentCounter}`,+ text: data.text,+ source: data.source,+ timestamp: data.timestamp,+ };+ setSegments((prev) => {+ // Insert in chronological order — scan from the end since most+ // segments arrive in order and this is O(1) in the common case.+ const ts = seg.timestamp ?? Infinity;+ let i = prev.length;+ while (i > 0 && (prev[i - 1].timestamp ?? 0) > ts) i--;+ if (i === prev.length) return [...prev, seg];+ return [...prev.slice(0, i), seg, ...prev.slice(i)];+ });+ setPartialForSource("");+ setTranscript((prev) => (prev ? prev + " " + data.text : data.text));+ setPartialTranscript("");}- pendingSystemChunks.push(chunk.slice(0));- },- }).then(({ source, processor }) => {- systemSourceRef.current = source;- systemProcessorRef.current = processor;+ }+ );+ if (segmentCleanup) ipcCleanupsRef.current.push(segmentCleanup);++ const errorCleanup = window.electronAPI?.onMeetingTranscriptionError?.((err) => {+ setError(err);+ logger.error("Meeting transcription stream error", { error: err }, "meeting");});- } else if (systemCaptureError) {- if (systemAudioStrategy === "browser-portal") {- logger.warn(- "Linux system audio capture failed, continuing with mic only",- { error: systemCaptureError.message },- "meeting"- );- } else if (systemAudioStrategy === "loopback") {- logger.warn(- "System audio loopback failed, continuing with mic only",- { error: systemCaptureError.message },+ if (errorCleanup) ipcCleanupsRef.current.push(errorCleanup);++ const pendingMicChunks: ArrayBuffer[] = [];+ const pendingSystemChunks: ArrayBuffer[] = [];+ let socketReady = false;++ let micPipelinePromise: Promise<void> | null = null;+ if (micResult) {+ micStreamRef.current = micResult;+ const micContext = new AudioContext({ sampleRate: 24000 });+ await detachFromOutputDevice(micContext);+ micContextRef.current = micContext;++ micPipelinePromise = createAudioPipeline({+ stream: micResult,+ context: micContext,+ label: "Meeting mic",+ onChunk: (chunk) => {+ if (!isRecordingRef.current) return;+ if (socketReady) {+ window.electronAPI?.meetingTranscriptionSend?.(chunk, "mic");+ return;+ }+ pendingMicChunks.push(chunk.slice(0));+ },+ }).then(({ source, processor }) => {+ micSourceRef.current = source;+ micProcessorRef.current = processor;++ const micTrack = micResult.getAudioTracks()[0];+ logger.info(+ "Mic capture started for meeting transcription",+ {+ label: micTrack?.label,+ settings: micTrack?.getSettings(),+ },+ "meeting"+ );+ });+ }++ if (micPipelinePromise) {+ await micPipelinePromise;+ }++ if (systemCaptureResult.stream) {+ const systemStream = systemCaptureResult.stream;+ systemStreamRef.current = systemStream;++ const systemContext = new AudioContext({ sampleRate: 24000 });+ await detachFromOutputDevice(systemContext);+ systemContextRef.current = systemContext;++ await createAudioPipeline({+ stream: systemStream,+ context: systemContext,+ label: "Meeting system",+ onChunk: (chunk) => {+ if (!isRecordingRef.current) return;+ if (socketReady) {+ window.electronAPI?.meetingTranscriptionSend?.(chunk, "system");+ return;+ }+ pendingSystemChunks.push(chunk.slice(0));+ },+ }).then(({ source, processor }) => {+ systemSourceRef.current = source;+ systemProcessorRef.current = processor;+ });+ } else if (systemCaptureError) {+ if (systemAudioStrategy === "browser-portal") {+ logger.warn(+ "Linux system audio capture failed, continuing with mic only",+ { error: systemCaptureError.message },+ "meeting"+ );+ } else if (systemAudioStrategy === "loopback") {+ logger.warn(+ "System audio loopback failed, continuing with mic only",+ { error: systemCaptureError.message },+ "meeting"+ );+ }+ }++ // Abort if stop was called during pipeline setup+ if (!isRecordingRef.current) {+ logger.info(+ "Meeting transcription aborted during pipeline setup (stop called)",+ {},"meeting");+ isStartingRef.current = false;+ await cleanup();+ return;}- }- // Abort if stop was called during pipeline setup- if (!isRecordingRef.current) {+ isStartingRef.current = false;+ socketReady = true;++ for (const chunk of pendingMicChunks) {+ window.electronAPI?.meetingTranscriptionSend?.(chunk, "mic");+ }+ for (const chunk of pendingSystemChunks) {+ window.electronAPI?.meetingTranscriptionSend?.(chunk, "system");+ }++ const totalMs = performance.now() - startTime;logger.info(- "Meeting transcription aborted during pipeline setup (stop called)",- {},+ "Meeting transcription started successfully",+ {+ systemAudioMode,+ systemAudioStrategy,+ bufferedChunks: pendingMicChunks.length,+ bufferedSystemChunks: pendingSystemChunks.length,+ streamsMs: Math.round(streamsMs),+ totalMs: Math.round(totalMs),+ wasPrepared: isPreparedRef.current,+ },"meeting");+ } catch (err) {+ logger.error(+ "Meeting transcription setup failed",+ { error: (err as Error).message },+ "meeting"+ );+ setError((err as Error).message);+ isRecordingRef.current = false;isStartingRef.current = false;+ setIsRecording(false);await cleanup();- return;- }-- isStartingRef.current = false;- socketReady = true;-- for (const chunk of pendingMicChunks) {- window.electronAPI?.meetingTranscriptionSend?.(chunk, "mic");}- for (const chunk of pendingSystemChunks) {- window.electronAPI?.meetingTranscriptionSend?.(chunk, "system");- }-- const totalMs = performance.now() - startTime;- logger.info(- "Meeting transcription started successfully",- {- systemAudioMode,- systemAudioStrategy,- bufferedChunks: pendingMicChunks.length,- bufferedSystemChunks: pendingSystemChunks.length,- streamsMs: Math.round(streamsMs),- totalMs: Math.round(totalMs),- wasPrepared: isPreparedRef.current,- },- "meeting"- );- } catch (err) {- logger.error(- "Meeting transcription setup failed",- { error: (err as Error).message },- "meeting"- );- setError((err as Error).message);- isRecordingRef.current = false;- isStartingRef.current = false;- setIsRecording(false);- await cleanup();- }- }, [cleanup]);+ },+ [cleanup]+ );useEffect(() => {getMeetingWorkletBlobUrl();diff --git a/src/hooks/useSystemAudioPermission.ts b/src/hooks/useSystemAudioPermission.tsindex 05dc06460..6646216b1 100644--- a/src/hooks/useSystemAudioPermission.ts+++ b/src/hooks/useSystemAudioPermission.ts@@ -1,18 +1,7 @@import { useState, useCallback, useEffect, useRef } from "react";import { getCachedPlatform } from "../utils/platform";import type { SystemAudioAccessResult } from "../types/electron";--const DEFAULT_ACCESS: SystemAudioAccessResult = {- granted: false,- status: "unsupported",- mode: "unsupported",- supportsPersistentGrant: false,- supportsOnboardingGrant: false,- requiresRuntimeSharePrompt: false,- strategy: "unsupported",- restoreTokenAvailable: false,- portalVersion: null,-};+import { DEFAULT_SYSTEM_AUDIO_ACCESS } from "../utils/systemAudioAccess";export function useSystemAudioPermission() {const isMacOS = getCachedPlatform() === "darwin";@@ -26,7 +15,7 @@ export function useSystemAudioPermission() {setIsChecking(true);try {const result = await window.electronAPI?.checkSystemAudioAccess?.();- setAccess(result ?? DEFAULT_ACCESS);+ setAccess(result ?? DEFAULT_SYSTEM_AUDIO_ACCESS);} finally {checkingRef.current = false;setIsChecking(false);@@ -50,7 +39,9 @@ export function useSystemAudioPermission() {const request = useCallback(async (): Promise<boolean> => {const currentAccess =- access ?? (await window.electronAPI?.checkSystemAudioAccess?.()) ?? DEFAULT_ACCESS;+ access ??+ (await window.electronAPI?.checkSystemAudioAccess?.()) ??+ DEFAULT_SYSTEM_AUDIO_ACCESS;if (currentAccess.mode === "loopback") {setAccess(currentAccess);@@ -84,6 +75,8 @@ export function useSystemAudioPermission() {const status = access?.status ?? "unknown";const mode = access?.mode ?? "unsupported";const supportsPersistentGrant = access?.supportsPersistentGrant ?? false;+ const supportsPersistentPortalGrant = access?.supportsPersistentPortalGrant ?? false;+ const supportsNativeCapture = access?.supportsNativeCapture ?? false;const supportsOnboardingGrant = access?.supportsOnboardingGrant ?? false;const requiresRuntimeSharePrompt = access?.requiresRuntimeSharePrompt ?? false;const strategy = access?.strategy ?? "unsupported";@@ -95,6 +88,8 @@ export function useSystemAudioPermission() {status,mode,supportsPersistentGrant,+ supportsPersistentPortalGrant,+ supportsNativeCapture,supportsOnboardingGrant,requiresRuntimeSharePrompt,strategy,diff --git a/src/types/electron.ts b/src/types/electron.tsindex 5f5c369ce..6ac38766d 100644--- a/src/types/electron.ts+++ b/src/types/electron.ts@@ -135,6 +135,8 @@ export interface SystemAudioAccessResult {status: "granted" | "denied" | "not-determined" | "restricted" | "unknown" | "unsupported";mode: SystemAudioMode;supportsPersistentGrant?: boolean;+ supportsPersistentPortalGrant?: boolean;+ supportsNativeCapture?: boolean;supportsOnboardingGrant?: boolean;requiresRuntimeSharePrompt?: boolean;strategy?: SystemAudioStrategy;diff --git a/src/utils/systemAudioAccess.ts b/src/utils/systemAudioAccess.tsnew file mode 100644index 000000000..fbd0723f2--- /dev/null+++ b/src/utils/systemAudioAccess.ts@@ -0,0 +1,62 @@+import type { SystemAudioAccessResult, SystemAudioStrategy } from "../types/electron";+import { getCachedPlatform } from "./platform";++export type RendererSystemAudioStrategy = Extract<+ SystemAudioStrategy,+ "loopback" | "browser-portal"+>;++export const DEFAULT_SYSTEM_AUDIO_ACCESS: SystemAudioAccessResult = {+ granted: false,+ status: "unsupported",+ mode: "unsupported",+ supportsPersistentGrant: false,+ supportsPersistentPortalGrant: false,+ supportsNativeCapture: false,+ supportsOnboardingGrant: false,+ requiresRuntimeSharePrompt: false,+ strategy: "unsupported",+ restoreTokenAvailable: false,+ portalVersion: null,+};++export const getFallbackSystemAudioAccess = (+ platform = getCachedPlatform()+): SystemAudioAccessResult => {+ if (platform === "win32") {+ return {+ ...DEFAULT_SYSTEM_AUDIO_ACCESS,+ granted: true,+ status: "granted",+ mode: "loopback",+ strategy: "loopback",+ };+ }++ if (platform === "linux") {+ return {+ ...DEFAULT_SYSTEM_AUDIO_ACCESS,+ status: "unknown",+ mode: "portal",+ requiresRuntimeSharePrompt: true,+ strategy: "browser-portal",+ };+ }++ return DEFAULT_SYSTEM_AUDIO_ACCESS;+};++export const canManageSystemAudioInApp = ({+ mode,+ supportsOnboardingGrant,+}: Pick<SystemAudioAccessResult, "mode" | "supportsOnboardingGrant">) =>+ mode === "native" || (mode === "portal" && !!supportsOnboardingGrant);++export const isRendererSystemAudioStrategy = (+ strategy: SystemAudioStrategy | undefined | null+): strategy is RendererSystemAudioStrategy =>+ strategy === "loopback" || strategy === "browser-portal";++export const getDisplayCaptureModeForStrategy = (+ strategy: RendererSystemAudioStrategy+): "loopback" | "portal" => (strategy === "loopback" ? "loopback" : "portal");