feat #14740: automatic call detection on windows
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,215 @@
|
||||
//! Output capture that falls back from per-process to device loopback.
|
||||
//!
|
||||
//! Application loopback (`OutputTarget::Process`) needs Windows 10 2004+ and
|
||||
//! a live process; if it cannot be opened, the call is still worth recording
|
||||
//! from the default output. [`crate::Recorder`] keeps a failed track running
|
||||
//! as silence instead of retrying, so the retry happens here, inside
|
||||
//! `start`.
|
||||
|
||||
use std::sync::{Arc, Mutex, PoisonError};
|
||||
|
||||
use recorder_core::capture::{
|
||||
CaptureError, CaptureSource, DataCallback, OutputTarget, StreamFormat,
|
||||
};
|
||||
|
||||
/// Creates the platform source for one output target.
|
||||
pub(crate) type Factory = fn(OutputTarget) -> Box<dyn CaptureSource>;
|
||||
|
||||
/// Tries `target`, and if that is a `Process` target that fails to start,
|
||||
/// `DefaultRender`. `stop` and `failure` act on whichever source started.
|
||||
pub(crate) struct FallbackSource {
|
||||
target: OutputTarget,
|
||||
factory: Factory,
|
||||
active: Option<Box<dyn CaptureSource>>,
|
||||
}
|
||||
|
||||
impl FallbackSource {
|
||||
pub(crate) fn new(target: OutputTarget, factory: Factory) -> Self {
|
||||
Self {
|
||||
target,
|
||||
factory,
|
||||
active: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CaptureSource for FallbackSource {
|
||||
fn start(&mut self, on_data: DataCallback) -> Result<StreamFormat, CaptureError> {
|
||||
self.stop();
|
||||
// A failed start consumes the callback it was given, so each attempt
|
||||
// gets a forwarder to the one shared callback.
|
||||
let on_data = Arc::new(Mutex::new(on_data));
|
||||
let forward = |on_data: &Arc<Mutex<DataCallback>>| -> DataCallback {
|
||||
let on_data = Arc::clone(on_data);
|
||||
Box::new(move |samples| {
|
||||
(on_data.lock().unwrap_or_else(PoisonError::into_inner))(samples)
|
||||
})
|
||||
};
|
||||
|
||||
let mut source = (self.factory)(self.target.clone());
|
||||
let format = match source.start(forward(&on_data)) {
|
||||
Ok(format) => format,
|
||||
Err(err) => {
|
||||
let OutputTarget::Process { root_pid } = self.target else {
|
||||
return Err(err);
|
||||
};
|
||||
log::warn!(
|
||||
"process loopback for pid {root_pid} failed ({err}), \
|
||||
recording the default output instead"
|
||||
);
|
||||
source = (self.factory)(OutputTarget::DefaultRender);
|
||||
source.start(forward(&on_data))?
|
||||
}
|
||||
};
|
||||
self.active = Some(source);
|
||||
Ok(format)
|
||||
}
|
||||
|
||||
fn stop(&mut self) {
|
||||
if let Some(mut source) = self.active.take() {
|
||||
source.stop();
|
||||
}
|
||||
}
|
||||
|
||||
fn failure(&self) -> Option<String> {
|
||||
self.active.as_ref().and_then(|source| source.failure())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const FORMAT: StreamFormat = StreamFormat {
|
||||
rate: 48_000,
|
||||
channels: 2,
|
||||
};
|
||||
|
||||
/// Per-test record of what the fake sources saw. Tests run in parallel,
|
||||
/// so each test gets its own log via a thread-local.
|
||||
#[derive(Default)]
|
||||
struct Log {
|
||||
started: Vec<OutputTarget>,
|
||||
stopped: Vec<OutputTarget>,
|
||||
delivered: Vec<f32>,
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static LOG: Arc<Mutex<Log>> = Arc::new(Mutex::new(Log::default()));
|
||||
}
|
||||
|
||||
fn log() -> Arc<Mutex<Log>> {
|
||||
LOG.with(Arc::clone)
|
||||
}
|
||||
|
||||
/// Fails to start for `Process` targets whose pid is 0 and for
|
||||
/// `Device("broken")`; otherwise delivers one sample (the call count)
|
||||
/// synchronously from `start` and reports a failure for
|
||||
/// `Device("dying")`.
|
||||
struct FakeOutput {
|
||||
target: OutputTarget,
|
||||
log: Arc<Mutex<Log>>,
|
||||
started: bool,
|
||||
}
|
||||
|
||||
fn fake(target: OutputTarget) -> Box<dyn CaptureSource> {
|
||||
Box::new(FakeOutput {
|
||||
target,
|
||||
log: log(),
|
||||
started: false,
|
||||
})
|
||||
}
|
||||
|
||||
impl CaptureSource for FakeOutput {
|
||||
fn start(&mut self, mut on_data: DataCallback) -> Result<StreamFormat, CaptureError> {
|
||||
self.log.lock().unwrap().started.push(self.target.clone());
|
||||
match &self.target {
|
||||
OutputTarget::Process { root_pid: 0 } => {
|
||||
return Err(CaptureError::Backend("no such process".into()))
|
||||
}
|
||||
OutputTarget::Device(id) if id == "broken" => {
|
||||
return Err(CaptureError::DeviceNotFound(id.clone()))
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
self.started = true;
|
||||
on_data(&[1.0]);
|
||||
Ok(FORMAT)
|
||||
}
|
||||
|
||||
fn stop(&mut self) {
|
||||
if std::mem::take(&mut self.started) {
|
||||
self.log.lock().unwrap().stopped.push(self.target.clone());
|
||||
}
|
||||
}
|
||||
|
||||
fn failure(&self) -> Option<String> {
|
||||
(self.started && self.target == OutputTarget::Device("dying".into()))
|
||||
.then(|| "device lost".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn collecting_callback() -> DataCallback {
|
||||
let log = log();
|
||||
Box::new(move |samples| log.lock().unwrap().delivered.extend_from_slice(samples))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_working_process_target_is_used_as_is() {
|
||||
let mut source = FallbackSource::new(OutputTarget::Process { root_pid: 42 }, fake);
|
||||
assert_eq!(source.start(collecting_callback()).unwrap(), FORMAT);
|
||||
source.stop();
|
||||
|
||||
let log = log();
|
||||
let log = log.lock().unwrap();
|
||||
assert_eq!(log.started, vec![OutputTarget::Process { root_pid: 42 }]);
|
||||
assert_eq!(log.stopped, vec![OutputTarget::Process { root_pid: 42 }]);
|
||||
assert_eq!(log.delivered, vec![1.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failing_process_target_falls_back_to_default_render() {
|
||||
let mut source = FallbackSource::new(OutputTarget::Process { root_pid: 0 }, fake);
|
||||
assert_eq!(source.start(collecting_callback()).unwrap(), FORMAT);
|
||||
assert_eq!(source.failure(), None);
|
||||
source.stop();
|
||||
|
||||
let log = log();
|
||||
let log = log.lock().unwrap();
|
||||
assert_eq!(
|
||||
log.started,
|
||||
vec![
|
||||
OutputTarget::Process { root_pid: 0 },
|
||||
OutputTarget::DefaultRender
|
||||
]
|
||||
);
|
||||
assert_eq!(log.stopped, vec![OutputTarget::DefaultRender]);
|
||||
// The callback survives the failed first attempt.
|
||||
assert_eq!(log.delivered, vec![1.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn other_targets_do_not_fall_back() {
|
||||
let mut source = FallbackSource::new(OutputTarget::Device("broken".into()), fake);
|
||||
assert!(matches!(
|
||||
source.start(collecting_callback()),
|
||||
Err(CaptureError::DeviceNotFound(_))
|
||||
));
|
||||
assert_eq!(
|
||||
log().lock().unwrap().started,
|
||||
vec![OutputTarget::Device("broken".into())]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failure_and_stop_act_on_the_started_source() {
|
||||
let mut source = FallbackSource::new(OutputTarget::Device("dying".into()), fake);
|
||||
assert_eq!(source.failure(), None);
|
||||
source.start(collecting_callback()).unwrap();
|
||||
assert_eq!(source.failure().as_deref(), Some("device lost"));
|
||||
source.stop();
|
||||
assert_eq!(source.failure(), None);
|
||||
source.stop();
|
||||
assert_eq!(log().lock().unwrap().stopped.len(), 1);
|
||||
}
|
||||
}
|
||||
+23
-2
@@ -2,10 +2,15 @@
|
||||
//!
|
||||
//! Implements [`recorder_core::capture::CaptureSource`] for the microphone and
|
||||
//! for the call app's audio output: WASAPI on Windows, PulseAudio on Linux.
|
||||
//! The platform-neutral trait, types and the two-track recorder live in
|
||||
//! `recorder-core`.
|
||||
//! Also lists the processes holding the microphone ([`mic_sessions`]), which
|
||||
//! drives call detection on Windows. The platform-neutral trait, types, the
|
||||
//! two-track recorder and the call detector live in `recorder-core`.
|
||||
|
||||
mod capture_thread;
|
||||
mod fallback;
|
||||
mod process_tree;
|
||||
|
||||
use recorder_core::capture::{CaptureSource, OutputTarget};
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
mod linux {
|
||||
@@ -17,6 +22,22 @@ pub use linux::capture::{list_devices, mic_source, output_source};
|
||||
#[cfg(windows)]
|
||||
mod windows {
|
||||
pub mod capture;
|
||||
pub mod sessions;
|
||||
}
|
||||
#[cfg(windows)]
|
||||
pub use windows::capture::{list_devices, mic_source, output_source};
|
||||
#[cfg(windows)]
|
||||
pub use windows::sessions::mic_sessions;
|
||||
|
||||
/// Processes currently holding a microphone. Call detection is Windows-only;
|
||||
/// Linux reports none.
|
||||
#[cfg(target_os = "linux")]
|
||||
pub fn mic_sessions() -> Vec<recorder_core::detect::MicSession> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
/// Output capture for `target`; if `target` is a process whose loopback
|
||||
/// cannot be opened, records the default output device instead.
|
||||
pub fn output_source_with_fallback(target: OutputTarget) -> Box<dyn CaptureSource> {
|
||||
Box::new(fallback::FallbackSource::new(target, output_source))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
//! Finding the root of a multi-process app (Chrome, Edge, Teams).
|
||||
//!
|
||||
//! Browsers and Teams open the microphone from a child process, but the
|
||||
//! call's audio is played by a sibling. Application loopback records a
|
||||
//! process *tree*, so the recorder needs the top-most process of the app:
|
||||
//! walk up the parent chain while the parent runs the same executable.
|
||||
//!
|
||||
//! The walk is a pure function over a process table so it can be tested
|
||||
//! without a live system; Windows builds the table from a ToolHelp snapshot.
|
||||
|
||||
// Only the Windows session enumeration calls into this module.
|
||||
#![cfg_attr(not(windows), allow(dead_code))]
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
/// Upper bound on parent steps, so a corrupt table (or pid reuse forming a
|
||||
/// long chain) can never make the walk run away.
|
||||
const MAX_STEPS: usize = 64;
|
||||
|
||||
/// pid -> (parent pid, executable file name).
|
||||
pub(crate) type ProcessTable = HashMap<u32, (u32, String)>;
|
||||
|
||||
/// The top-most ancestor of `pid` reachable through parents that run the
|
||||
/// same executable (compared case-insensitively). Returns `pid` itself if it
|
||||
/// is unknown or its parent runs something else. Cycles (possible after pid
|
||||
/// reuse) end the walk, and so does [`MAX_STEPS`].
|
||||
pub(crate) fn root_pid(table: &ProcessTable, pid: u32) -> u32 {
|
||||
let Some((_, exe)) = table.get(&pid) else {
|
||||
return pid;
|
||||
};
|
||||
let mut root = pid;
|
||||
let mut seen = HashSet::from([pid]);
|
||||
for _ in 0..MAX_STEPS {
|
||||
let parent = table[&root].0;
|
||||
match table.get(&parent) {
|
||||
Some((_, parent_exe))
|
||||
if parent_exe.eq_ignore_ascii_case(exe) && seen.insert(parent) =>
|
||||
{
|
||||
root = parent;
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
root
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn table(entries: &[(u32, u32, &str)]) -> ProcessTable {
|
||||
entries
|
||||
.iter()
|
||||
.map(|&(pid, parent, exe)| (pid, (parent, exe.to_string())))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_pid_is_its_own_root() {
|
||||
assert_eq!(root_pid(&ProcessTable::new(), 42), 42);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn process_started_by_another_exe_is_its_own_root() {
|
||||
let t = table(&[(4, 0, "explorer.exe"), (10, 4, "MicroSIP.exe")]);
|
||||
assert_eq!(root_pid(&t, 10), 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn walks_up_a_same_exe_chain_to_the_top() {
|
||||
let t = table(&[
|
||||
(4, 0, "explorer.exe"),
|
||||
(100, 4, "chrome.exe"),
|
||||
(200, 100, "chrome.exe"),
|
||||
(300, 200, "chrome.exe"),
|
||||
]);
|
||||
assert_eq!(root_pid(&t, 300), 100);
|
||||
assert_eq!(root_pid(&t, 200), 100);
|
||||
assert_eq!(root_pid(&t, 100), 100);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exe_comparison_ignores_case() {
|
||||
let t = table(&[
|
||||
(4, 0, "explorer.exe"),
|
||||
(100, 4, "ms-teams.exe"),
|
||||
(200, 100, "MS-Teams.EXE"),
|
||||
]);
|
||||
assert_eq!(root_pid(&t, 200), 100);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stops_at_a_parent_missing_from_the_table() {
|
||||
// The parent exited; its pid is no longer in the snapshot.
|
||||
let t = table(&[(200, 100, "chrome.exe"), (300, 200, "chrome.exe")]);
|
||||
assert_eq!(root_pid(&t, 300), 200);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_cycle_ends_the_walk() {
|
||||
// After pid reuse two processes can name each other as parent.
|
||||
let t = table(&[(100, 200, "chrome.exe"), (200, 100, "chrome.exe")]);
|
||||
let root = root_pid(&t, 100);
|
||||
assert!(root == 100 || root == 200, "root {root}");
|
||||
|
||||
let own_parent = table(&[(7, 7, "chrome.exe")]);
|
||||
assert_eq!(root_pid(&own_parent, 7), 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_walk_is_capped() {
|
||||
// pid n's parent is n + 1, all chrome: 1000 steps to the top.
|
||||
let entries: Vec<(u32, u32, &str)> =
|
||||
(1..=1000).map(|pid| (pid, pid + 1, "chrome.exe")).collect();
|
||||
let t = table(&entries);
|
||||
assert_eq!(root_pid(&t, 1), 1 + MAX_STEPS as u32);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
//! Which processes hold a microphone right now (drives call detection).
|
||||
//!
|
||||
//! Every active capture endpoint is asked for its audio sessions
|
||||
//! (`IAudioSessionManager2::GetSessionEnumerator`). A session belongs to one
|
||||
//! process (`IAudioSessionControl2::GetProcessId`) and is active while one
|
||||
//! of its streams runs, i.e. while the app actually records the mic. The
|
||||
//! process's exe comes from `QueryFullProcessImageNameW`; its `root_pid` is
|
||||
//! the top of its same-exe parent chain in a ToolHelp snapshot (see
|
||||
//! [`crate::process_tree`]), which is the tree application loopback records.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use recorder_core::detect::MicSession;
|
||||
use wasapi::{deinitialize, initialize_mta, DeviceEnumerator, Direction, SessionState};
|
||||
use windows::core::PWSTR;
|
||||
use windows::Win32::Foundation::{CloseHandle, HANDLE};
|
||||
use windows::Win32::System::Diagnostics::ToolHelp::{
|
||||
CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W, TH32CS_SNAPPROCESS,
|
||||
};
|
||||
use windows::Win32::System::Threading::{
|
||||
OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32, PROCESS_QUERY_LIMITED_INFORMATION,
|
||||
};
|
||||
|
||||
use crate::process_tree::{root_pid, ProcessTable};
|
||||
|
||||
/// Sessions on every active capture endpoint, one entry per session.
|
||||
///
|
||||
/// Initialises COM (MTA) on the calling thread for the duration of the
|
||||
/// call, so call it from a thread that is not an STA (the detection loop
|
||||
/// has its own thread). Sessions of the system (pid 0) and of processes
|
||||
/// that cannot be opened are skipped; enumeration errors yield fewer or no
|
||||
/// sessions, never an error, because detection simply polls again.
|
||||
pub fn mic_sessions() -> Vec<MicSession> {
|
||||
if let Err(err) = initialize_mta().ok() {
|
||||
log::debug!("mic sessions: COM initialisation failed: {err}");
|
||||
return Vec::new();
|
||||
}
|
||||
let sessions = enumerate();
|
||||
// Every COM object is dropped by now.
|
||||
deinitialize();
|
||||
sessions
|
||||
}
|
||||
|
||||
/// (pid, active) of every non-system session on every active capture
|
||||
/// endpoint, then resolved to exe and root pid.
|
||||
fn enumerate() -> Vec<MicSession> {
|
||||
let raw = capture_sessions();
|
||||
if raw.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
let table = process_table();
|
||||
raw.into_iter()
|
||||
.filter_map(|(pid, active)| {
|
||||
let exe = image_name(pid)?;
|
||||
Some(MicSession {
|
||||
exe,
|
||||
pid,
|
||||
root_pid: root_pid(&table, pid),
|
||||
active,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn capture_sessions() -> Vec<(u32, bool)> {
|
||||
let collection = match DeviceEnumerator::new()
|
||||
.and_then(|enumerator| enumerator.get_device_collection(&Direction::Capture))
|
||||
{
|
||||
Ok(collection) => collection,
|
||||
Err(err) => {
|
||||
log::debug!("mic sessions: cannot enumerate capture endpoints: {err}");
|
||||
return Vec::new();
|
||||
}
|
||||
};
|
||||
let mut sessions = Vec::new();
|
||||
for device in &collection {
|
||||
let Ok(device) = device else { continue };
|
||||
let Ok(enumerator) = device
|
||||
.get_iaudiosessionmanager()
|
||||
.and_then(|manager| manager.get_audiosessionenumerator())
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let count = enumerator.get_count().unwrap_or(0);
|
||||
for index in 0..count {
|
||||
let Ok(control) = enumerator.get_session(index) else {
|
||||
continue;
|
||||
};
|
||||
let (Ok(pid), Ok(state)) = (control.get_process_id(), control.get_state()) else {
|
||||
continue;
|
||||
};
|
||||
if pid != 0 {
|
||||
sessions.push((pid, state == SessionState::Active));
|
||||
}
|
||||
}
|
||||
}
|
||||
sessions
|
||||
}
|
||||
|
||||
/// The file name of `pid`'s executable, or `None` if the process cannot be
|
||||
/// opened (exited, or protected).
|
||||
fn image_name(pid: u32) -> Option<String> {
|
||||
// SAFETY: plain Win32 calls; the handle is closed below and the buffer
|
||||
// outlives the call that fills it.
|
||||
unsafe {
|
||||
let handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid).ok()?;
|
||||
let mut buf = [0u16; 1024];
|
||||
let mut len = buf.len() as u32;
|
||||
let result = QueryFullProcessImageNameW(
|
||||
handle,
|
||||
PROCESS_NAME_WIN32,
|
||||
PWSTR(buf.as_mut_ptr()),
|
||||
&mut len,
|
||||
);
|
||||
close(handle);
|
||||
result.ok()?;
|
||||
let path = String::from_utf16_lossy(&buf[..len as usize]);
|
||||
Path::new(&path)
|
||||
.file_name()
|
||||
.map(|name| name.to_string_lossy().into_owned())
|
||||
}
|
||||
}
|
||||
|
||||
/// pid -> (parent pid, exe) of every process, from one ToolHelp snapshot.
|
||||
/// Empty if the snapshot fails, which makes every session its own root.
|
||||
fn process_table() -> ProcessTable {
|
||||
let mut table = ProcessTable::new();
|
||||
// SAFETY: plain Win32 calls; `entry.dwSize` is set as the API requires
|
||||
// and the snapshot handle is closed below.
|
||||
unsafe {
|
||||
let snapshot = match CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) {
|
||||
Ok(snapshot) => snapshot,
|
||||
Err(err) => {
|
||||
log::debug!("mic sessions: process snapshot failed: {err}");
|
||||
return table;
|
||||
}
|
||||
};
|
||||
let mut entry = PROCESSENTRY32W {
|
||||
dwSize: std::mem::size_of::<PROCESSENTRY32W>() as u32,
|
||||
..Default::default()
|
||||
};
|
||||
let mut next = Process32FirstW(snapshot, &mut entry);
|
||||
while next.is_ok() {
|
||||
let len = entry
|
||||
.szExeFile
|
||||
.iter()
|
||||
.position(|&c| c == 0)
|
||||
.unwrap_or(entry.szExeFile.len());
|
||||
table.insert(
|
||||
entry.th32ProcessID,
|
||||
(
|
||||
entry.th32ParentProcessID,
|
||||
String::from_utf16_lossy(&entry.szExeFile[..len]),
|
||||
),
|
||||
);
|
||||
next = Process32NextW(snapshot, &mut entry);
|
||||
}
|
||||
close(snapshot);
|
||||
}
|
||||
table
|
||||
}
|
||||
|
||||
fn close(handle: HANDLE) {
|
||||
// SAFETY: `handle` was returned by a successful Win32 call and is closed
|
||||
// exactly once.
|
||||
if let Err(err) = unsafe { CloseHandle(handle) } {
|
||||
log::debug!("mic sessions: CloseHandle failed: {err}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user