Files
AFFiNE-Mirror/packages/frontend/native/media_capture/src/windows
Peng Xiao 899ffd1ad3 feat(native): windows audio monitoring & recording (#12615)
fix AF-2692

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Added comprehensive Windows support for audio and application capture,
including real-time microphone usage detection, combined microphone and
system audio recording, and application state monitoring.
  - The "meetings" setting is now enabled on Windows as well as macOS.
- Conditional UI styling and attributes introduced for Windows
environments in the Electron renderer.

- **Bug Fixes**
- Enhanced file path handling and validation for Windows in Electron
file requests.

- **Refactor**
- Unified application info handling across platforms by consolidating
types into a single `ApplicationInfo` structure.
- Updated native module APIs by removing deprecated types, refining
method signatures, and improving error messages.
- Streamlined audio tapping APIs to use process IDs and consistent
callback types.

- **Documentation**
- Added detailed documentation for the Windows-specific audio recording
and microphone listener modules.

- **Chores**
  - Updated development dependencies in multiple packages.
- Reorganized and added platform-specific dependencies and configuration
for Windows support.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->





#### PR Dependency Tree


* **PR #12615** 👈

This tree was auto-generated by
[Charcoal](https://github.com/danerwilliams/charcoal)

---------

Co-authored-by: LongYinan <lynweklm@gmail.com>
2025-06-18 13:57:01 +08:00
..

Windows Audio Recording

This module provides Windows-specific audio recording functionality using the Windows Audio Session API (WASAPI).

Features

  • Microphone Activity Detection: Monitor when applications are using the microphone
  • Process Identification: Identify which process is using the microphone
  • Real-time Notifications: Get callbacks when microphone usage starts/stops

Usage

MicrophoneListener

The MicrophoneListener class provides real-time monitoring of microphone usage:

import { MicrophoneListener } from '@affine/native';

const listener = new MicrophoneListener((isRunning: boolean, processName: string) => {
  console.log(`Microphone ${isRunning ? 'started' : 'stopped'} by ${processName}`);
});

// Check current status
console.log('Is microphone currently active:', listener.is_running());

Callback Parameters

The callback receives two parameters:

  • isRunning: boolean - Whether the microphone is currently active
  • processName: string - Name of the process using the microphone

Implementation Details

Audio Session Monitoring

The implementation uses Windows Audio Session API to:

  1. Enumerate Audio Sessions: Get all active audio sessions
  2. Monitor Session State: Track when sessions become active/inactive
  3. Process Identification: Map audio sessions to process names
  4. Event Handling: Provide real-time notifications

COM Initialization

The module automatically initializes COM (Component Object Model) with COINIT_MULTITHREADED for proper Windows API interaction.

Error Handling

All Windows API errors are wrapped in WindowsAudioError enum and converted to NAPI errors for JavaScript consumption.

Cross-Platform Compatibility

This Windows implementation maintains API compatibility with the macOS version, providing the same JavaScript interface while using Windows-specific APIs underneath.

Platform Requirements

  • Windows 10 or later
  • Microphone access permissions
  • Audio devices available

Dependencies

  • windows crate v0.61 with Audio and Process features
  • windows-core crate v0.61
  • napi and napi-derive for JavaScript bindings

Technical Notes

Thread Safety

The implementation uses thread-safe callbacks to JavaScript with ThreadsafeFunction<(bool, String), ()> to ensure proper communication between the Windows audio session monitoring thread and the JavaScript runtime.

Process Name Resolution

Process names are resolved using Windows APIs:

  • GetModuleFileNameExW for full executable path
  • GetProcessImageFileNameW as fallback
  • Automatic extraction of filename from full path

Session Filtering

The implementation automatically filters out system audio sessions (like AudioSrv) to focus on user applications.