|
| 1 | +/** |
| 2 | + * 进程环回原生模块加载器 |
| 3 | + * |
| 4 | + * @ai-context: 原生 addon 是可选依赖——未编译、非 Windows、或 Windows 版本 |
| 5 | + * 过低时必须优雅降级为"能力不可用",绝不能让加载失败冒泡到应用启动路径 |
| 6 | + * (AGENTS.md 高风险区约定)。故此处用 try/catch 包裹 require 并缓存结果。 |
| 7 | + * @ai-context: 能力探测(isProcessLoopbackSupported)会真实尝试激活一次 |
| 8 | + * WASAPI 接口,有几毫秒开销,因此结果缓存到进程生命周期。 |
| 9 | + */ |
| 10 | + |
| 11 | +import * as path from 'path'; |
| 12 | +import { app } from 'electron'; |
| 13 | +import { logger } from '../logger.js'; |
| 14 | + |
| 15 | +/** 原生模块导出的窗口信息 */ |
| 16 | +export interface NativeWindowInfo { |
| 17 | + hwnd: string; |
| 18 | + pid: number; |
| 19 | + rootPid: number; |
| 20 | + title: string; |
| 21 | + processName: string; |
| 22 | + rootProcessName: string; |
| 23 | +} |
| 24 | + |
| 25 | +/** 原生模块流式回调的负载:音频块或错误 */ |
| 26 | +export interface NativeStreamPayload { |
| 27 | + audioBuffer?: ArrayBuffer; |
| 28 | + sampleRate?: number; |
| 29 | + channels?: number; |
| 30 | + durationMs?: number; |
| 31 | + error?: string; |
| 32 | +} |
| 33 | + |
| 34 | +/** 原生模块导出接口 */ |
| 35 | +export interface ProcessAudioNative { |
| 36 | + listAudioWindows(): NativeWindowInfo[]; |
| 37 | + resolveRootPid(pid: number): number; |
| 38 | + isProcessLoopbackSupported(): boolean; |
| 39 | + startCapture( |
| 40 | + options: { pid: number; sampleRate: number; channels: number; chunkDurationMs: number }, |
| 41 | + callback: (payload: NativeStreamPayload) => void, |
| 42 | + ): { ok: boolean; error: string }; |
| 43 | + stopCapture(): boolean; |
| 44 | +} |
| 45 | + |
| 46 | +/** 加载状态缓存(undefined 表示尚未尝试加载) */ |
| 47 | +let cached: ProcessAudioNative | null | undefined; |
| 48 | +let supportedCache: boolean | undefined; |
| 49 | + |
| 50 | +/** 候选路径:开发态走源码目录构建产物,打包后走 asarUnpack 解出的目录 */ |
| 51 | +function candidatePaths(): string[] { |
| 52 | + const fileName = 'process_audio.node'; |
| 53 | + const appPath = app.getAppPath(); |
| 54 | + return [ |
| 55 | + // 打包后:asarUnpack 将 .node 解到 app.asar.unpacked 下 |
| 56 | + path.join(appPath.replace('app.asar', 'app.asar.unpacked'), 'native', 'process-audio', 'build', 'Release', fileName), |
| 57 | + // 开发态:client/native/process-audio/build/Release |
| 58 | + path.join(appPath, 'native', 'process-audio', 'build', 'Release', fileName), |
| 59 | + ]; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * 加载原生模块;不可用时返回 null(不抛错)。 |
| 64 | + */ |
| 65 | +export function loadProcessAudioNative(): ProcessAudioNative | null { |
| 66 | + if (cached !== undefined) return cached; |
| 67 | + |
| 68 | + if (process.platform !== 'win32') { |
| 69 | + logger.info('[ProcessAudio] 非 Windows 平台,进程环回不可用'); |
| 70 | + cached = null; |
| 71 | + return cached; |
| 72 | + } |
| 73 | + |
| 74 | + // 主进程编译目标为 CommonJS,直接用全局 require 动态加载 .node |
| 75 | + for (const candidate of candidatePaths()) { |
| 76 | + try { |
| 77 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 78 | + const native = require(candidate) as ProcessAudioNative; |
| 79 | + if (typeof native.startCapture === 'function') { |
| 80 | + logger.info(`[ProcessAudio] 原生模块已加载: ${candidate}`); |
| 81 | + cached = native; |
| 82 | + return cached; |
| 83 | + } |
| 84 | + } catch { |
| 85 | + // 继续尝试下一个候选路径 |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + logger.info('[ProcessAudio] 原生模块未找到(未编译或未随包分发),进程环回不可用'); |
| 90 | + cached = null; |
| 91 | + return cached; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * 进程环回是否可用(模块已加载 + 系统 API 可用)。 |
| 96 | + * 结果缓存到进程生命周期。 |
| 97 | + */ |
| 98 | +export function isProcessLoopbackAvailable(): boolean { |
| 99 | + if (supportedCache !== undefined) return supportedCache; |
| 100 | + |
| 101 | + const native = loadProcessAudioNative(); |
| 102 | + if (!native) { |
| 103 | + supportedCache = false; |
| 104 | + return supportedCache; |
| 105 | + } |
| 106 | + try { |
| 107 | + supportedCache = native.isProcessLoopbackSupported(); |
| 108 | + logger.info(`[ProcessAudio] 系统能力探测: ${supportedCache ? '支持' : '不支持'}(需 Windows 10 2004+)`); |
| 109 | + } catch (err) { |
| 110 | + const message = err instanceof Error ? err.message : String(err); |
| 111 | + logger.warn(`[ProcessAudio] 能力探测异常,视为不可用: ${message}`); |
| 112 | + supportedCache = false; |
| 113 | + } |
| 114 | + return supportedCache; |
| 115 | +} |
| 116 | + |
| 117 | +/** 仅测试用:重置缓存 */ |
| 118 | +export function resetProcessAudioCacheForTest(): void { |
| 119 | + cached = undefined; |
| 120 | + supportedCache = undefined; |
| 121 | +} |
0 commit comments