fix #14740: record detected calls from the output device
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -2,10 +2,9 @@
|
||||
//!
|
||||
//! - Microphone: the default *communications* capture endpoint, or the
|
||||
//! endpoint with the given device id.
|
||||
//! - Output, `Process`: application loopback of the call app's process tree
|
||||
//! (Windows 10 2004+), so only the call's audio is recorded.
|
||||
//! - Output, `DefaultRender` / `Device`: loopback of the default
|
||||
//! communications render endpoint / of the given render endpoint.
|
||||
//! - Output: loopback of the default communications render endpoint
|
||||
//! (`DefaultRender`) or of the render endpoint with the given id
|
||||
//! (`Device`).
|
||||
//!
|
||||
//! All streams are delivered as interleaved `f32`. When the endpoint goes
|
||||
//! away mid-recording (`AUDCLNT_E_DEVICE_INVALIDATED`, e.g. a headset was
|
||||
@@ -28,15 +27,8 @@ use wasapi::{
|
||||
use crate::capture_thread::{CaptureThread, Ready};
|
||||
use crate::stats::CaptureStats;
|
||||
|
||||
use super::sessions;
|
||||
|
||||
/// `AUDCLNT_E_DEVICE_INVALIDATED`: the endpoint was removed or disabled.
|
||||
const AUDCLNT_E_DEVICE_INVALIDATED: i32 = 0x8889_0004_u32 as i32;
|
||||
/// Format requested for application loopback, which has no mix format.
|
||||
const PROCESS_LOOPBACK_FORMAT: StreamFormat = StreamFormat {
|
||||
rate: 48_000,
|
||||
channels: 2,
|
||||
};
|
||||
/// Shared-mode buffer size (100 ns units): 200 ms of headroom so a slow
|
||||
/// disk write in the recorder does not overflow the capture buffer.
|
||||
const BUFFER_DURATION_HNS: i64 = 2_000_000;
|
||||
@@ -57,7 +49,6 @@ impl Endpoint {
|
||||
fn fallback(&self) -> Endpoint {
|
||||
match self {
|
||||
Endpoint::Mic(_) => Endpoint::Mic(None),
|
||||
Endpoint::Output(OutputTarget::Process { .. }) => self.clone(),
|
||||
Endpoint::Output(_) => Endpoint::Output(OutputTarget::DefaultRender),
|
||||
}
|
||||
}
|
||||
@@ -188,67 +179,44 @@ fn device_by_id(enumerator: &DeviceEnumerator, id: &str) -> Result<Device, Captu
|
||||
}
|
||||
|
||||
/// Describes the endpoint `open` just opened, for the one-line log on a
|
||||
/// successful open. For process loopback that is the root pid and the exe
|
||||
/// name of that process (looked up the same way call detection does);
|
||||
/// "unknown" if the process cannot be resolved. For a device endpoint it is
|
||||
/// the friendly name, the device id and whether it was picked as the
|
||||
/// communications default or by an explicit id.
|
||||
fn describe_endpoint(endpoint: &Endpoint, device: Option<&Device>, role: &str) -> String {
|
||||
match endpoint {
|
||||
Endpoint::Output(OutputTarget::Process { root_pid }) => {
|
||||
let exe = sessions::image_name(*root_pid).unwrap_or_else(|| "unknown".to_string());
|
||||
format!("process tree pid {root_pid} ({exe})")
|
||||
}
|
||||
_ => {
|
||||
let device = device.expect("device endpoint always has a Device");
|
||||
let name = device
|
||||
.get_friendlyname()
|
||||
.unwrap_or_else(|_| "unknown".to_string());
|
||||
let id = device.get_id().unwrap_or_else(|_| "unknown".to_string());
|
||||
format!("{name} ({id}, {role})")
|
||||
}
|
||||
}
|
||||
/// successful open: the friendly name, the device id and whether it was
|
||||
/// picked as the communications default or by an explicit id.
|
||||
fn describe_endpoint(device: &Device, role: &str) -> String {
|
||||
let name = device
|
||||
.get_friendlyname()
|
||||
.unwrap_or_else(|_| "unknown".to_string());
|
||||
let id = device.get_id().unwrap_or_else(|_| "unknown".to_string());
|
||||
format!("{name} ({id}, {role})")
|
||||
}
|
||||
|
||||
/// Opens `endpoint` and starts streaming. `format` forces a stream format
|
||||
/// (used when reopening); otherwise the endpoint's mix format is used, with
|
||||
/// the samples converted to `f32` by the audio engine.
|
||||
fn open(endpoint: &Endpoint, format: Option<StreamFormat>) -> Result<Stream, CaptureError> {
|
||||
let (mut client, description) = match endpoint {
|
||||
Endpoint::Output(OutputTarget::Process { root_pid }) => {
|
||||
let client =
|
||||
AudioClient::new_application_loopback_client(*root_pid, true).map_err(backend)?;
|
||||
let description = describe_endpoint(endpoint, None, "");
|
||||
(client, description)
|
||||
}
|
||||
_ => {
|
||||
let enumerator = DeviceEnumerator::new().map_err(backend)?;
|
||||
let (device, role) = match endpoint {
|
||||
Endpoint::Mic(Some(id)) | Endpoint::Output(OutputTarget::Device(id)) => {
|
||||
(device_by_id(&enumerator, id)?, "explicit id")
|
||||
}
|
||||
Endpoint::Mic(None) => (
|
||||
enumerator
|
||||
.get_default_device_for_role(&Direction::Capture, &Role::Communications)
|
||||
.map_err(backend)?,
|
||||
"communications default",
|
||||
),
|
||||
Endpoint::Output(_) => (
|
||||
enumerator
|
||||
.get_default_device_for_role(&Direction::Render, &Role::Communications)
|
||||
.map_err(backend)?,
|
||||
"communications default",
|
||||
),
|
||||
};
|
||||
let description = describe_endpoint(endpoint, Some(&device), role);
|
||||
(device.get_iaudioclient().map_err(backend)?, description)
|
||||
let enumerator = DeviceEnumerator::new().map_err(backend)?;
|
||||
let (device, role) = match endpoint {
|
||||
Endpoint::Mic(Some(id)) | Endpoint::Output(OutputTarget::Device(id)) => {
|
||||
(device_by_id(&enumerator, id)?, "explicit id")
|
||||
}
|
||||
Endpoint::Mic(None) => (
|
||||
enumerator
|
||||
.get_default_device_for_role(&Direction::Capture, &Role::Communications)
|
||||
.map_err(backend)?,
|
||||
"communications default",
|
||||
),
|
||||
Endpoint::Output(OutputTarget::DefaultRender) => (
|
||||
enumerator
|
||||
.get_default_device_for_role(&Direction::Render, &Role::Communications)
|
||||
.map_err(backend)?,
|
||||
"communications default",
|
||||
),
|
||||
};
|
||||
let description = describe_endpoint(&device, role);
|
||||
let mut client = device.get_iaudioclient().map_err(backend)?;
|
||||
|
||||
let format = match (format, endpoint) {
|
||||
(Some(format), _) => format,
|
||||
(None, Endpoint::Output(OutputTarget::Process { .. })) => PROCESS_LOOPBACK_FORMAT,
|
||||
(None, _) => {
|
||||
let format = match format {
|
||||
Some(format) => format,
|
||||
None => {
|
||||
let mix = client.get_mixformat().map_err(backend)?;
|
||||
StreamFormat {
|
||||
rate: mix.get_samplespersec(),
|
||||
@@ -268,8 +236,8 @@ fn open(endpoint: &Endpoint, format: Option<StreamFormat>) -> Result<Stream, Cap
|
||||
autoconvert: true,
|
||||
buffer_duration_hns: BUFFER_DURATION_HNS,
|
||||
};
|
||||
// Capture on a capture endpoint records it; on a render endpoint (and on
|
||||
// the application loopback client) it sets up loopback capture.
|
||||
// Capture on a capture endpoint records it; on a render endpoint it sets
|
||||
// up loopback capture.
|
||||
client
|
||||
.initialize_client(&wave_format, &Direction::Capture, &mode)
|
||||
.map_err(backend)?;
|
||||
|
||||
@@ -1,18 +1,21 @@
|
||||
//! Which processes hold a microphone right now (drives call detection).
|
||||
//! Which processes hold a microphone (drives call detection) and which play
|
||||
//! on which output device (chooses the device a detected call is recorded
|
||||
//! from).
|
||||
//!
|
||||
//! Every active capture endpoint is asked for its audio sessions
|
||||
//! Every active endpoint of a direction 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, which is the
|
||||
//! tree application loopback records. Teams helper processes are reported
|
||||
//! as their Teams ancestor (see [`crate::process_tree`]).
|
||||
//! of its streams runs, i.e. while the app actually records the mic or plays
|
||||
//! sound. The process's exe comes from `QueryFullProcessImageNameW`; its
|
||||
//! `root_pid` is the top of its same-exe parent chain in a ToolHelp
|
||||
//! snapshot. Teams helper processes are reported as their Teams ancestor
|
||||
//! (see [`crate::process_tree`]).
|
||||
|
||||
use std::path::Path;
|
||||
use std::thread;
|
||||
|
||||
use recorder_core::detect::MicSession;
|
||||
use wasapi::{deinitialize, initialize_mta, DeviceEnumerator, Direction, SessionState};
|
||||
use wasapi::{deinitialize, initialize_mta, Device, DeviceEnumerator, Direction, SessionState};
|
||||
use windows::core::PWSTR;
|
||||
use windows::Win32::Foundation::{CloseHandle, FILETIME, HANDLE};
|
||||
use windows::Win32::System::Diagnostics::ToolHelp::{
|
||||
@@ -24,6 +27,7 @@ use windows::Win32::System::Threading::{
|
||||
};
|
||||
|
||||
use crate::process_tree::{attribute, ProcessTable};
|
||||
use crate::render_sessions::RenderSession;
|
||||
|
||||
/// Sessions on every active capture endpoint, one entry per session.
|
||||
///
|
||||
@@ -37,24 +41,45 @@ pub fn mic_sessions() -> Vec<MicSession> {
|
||||
log::debug!("mic sessions: COM initialisation failed: {err}");
|
||||
return Vec::new();
|
||||
}
|
||||
let sessions = enumerate();
|
||||
let sessions = enumerate_mic();
|
||||
// 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();
|
||||
/// Sessions on every active render endpoint, in endpoint enumeration
|
||||
/// order, one entry per session.
|
||||
///
|
||||
/// Runs on a fresh thread so COM is initialised the way this code expects,
|
||||
/// independent of the caller's apartment. Skips the same sessions as
|
||||
/// [`mic_sessions`]; errors yield fewer or no sessions.
|
||||
pub fn render_sessions() -> Vec<RenderSession> {
|
||||
thread::spawn(|| {
|
||||
if let Err(err) = initialize_mta().ok() {
|
||||
log::debug!("render sessions: COM initialisation failed: {err}");
|
||||
return Vec::new();
|
||||
}
|
||||
let sessions = enumerate_render();
|
||||
// Every COM object is dropped by now.
|
||||
deinitialize();
|
||||
sessions
|
||||
})
|
||||
.join()
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn enumerate_mic() -> Vec<MicSession> {
|
||||
let raw: Vec<(u32, bool)> = endpoint_sessions(&Direction::Capture)
|
||||
.into_iter()
|
||||
.flat_map(|(_, sessions)| sessions)
|
||||
.collect();
|
||||
if raw.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
let table = process_table();
|
||||
raw.into_iter()
|
||||
.filter_map(|(pid, active)| {
|
||||
let exe = image_name(pid)?;
|
||||
let (exe, root_pid) = attribute(&table, pid, &exe, creation_time);
|
||||
let (exe, root_pid) = resolve(&table, pid)?;
|
||||
Some(MicSession {
|
||||
exe,
|
||||
pid,
|
||||
@@ -65,17 +90,57 @@ fn enumerate() -> Vec<MicSession> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn capture_sessions() -> Vec<(u32, bool)> {
|
||||
fn enumerate_render() -> Vec<RenderSession> {
|
||||
let endpoints = endpoint_sessions(&Direction::Render);
|
||||
if endpoints.iter().all(|(_, sessions)| sessions.is_empty()) {
|
||||
return Vec::new();
|
||||
}
|
||||
let table = process_table();
|
||||
let mut result = Vec::new();
|
||||
for (device, sessions) in endpoints {
|
||||
let Ok(device_id) = device.get_id() else {
|
||||
continue;
|
||||
};
|
||||
let device_name = device
|
||||
.get_friendlyname()
|
||||
.unwrap_or_else(|_| "unknown".to_string());
|
||||
for (pid, active) in sessions {
|
||||
let Some((exe, root_pid)) = resolve(&table, pid) else {
|
||||
continue;
|
||||
};
|
||||
result.push(RenderSession {
|
||||
exe,
|
||||
pid,
|
||||
root_pid,
|
||||
active,
|
||||
device_id: device_id.clone(),
|
||||
device_name: device_name.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// The app `pid` belongs to, `(exe, root_pid)`; `None` if the process
|
||||
/// cannot be opened.
|
||||
fn resolve(table: &ProcessTable, pid: u32) -> Option<(String, u32)> {
|
||||
let exe = image_name(pid)?;
|
||||
Some(attribute(table, pid, &exe, creation_time))
|
||||
}
|
||||
|
||||
/// Every active endpoint of `direction` with the (pid, active) of each of
|
||||
/// its non-system sessions.
|
||||
fn endpoint_sessions(direction: &Direction) -> Vec<(Device, Vec<(u32, bool)>)> {
|
||||
let collection = match DeviceEnumerator::new()
|
||||
.and_then(|enumerator| enumerator.get_device_collection(&Direction::Capture))
|
||||
.and_then(|enumerator| enumerator.get_device_collection(direction))
|
||||
{
|
||||
Ok(collection) => collection,
|
||||
Err(err) => {
|
||||
log::debug!("mic sessions: cannot enumerate capture endpoints: {err}");
|
||||
log::debug!("audio sessions: cannot enumerate {direction} endpoints: {err}");
|
||||
return Vec::new();
|
||||
}
|
||||
};
|
||||
let mut sessions = Vec::new();
|
||||
let mut endpoints = Vec::new();
|
||||
for device in &collection {
|
||||
let Ok(device) = device else { continue };
|
||||
let Ok(enumerator) = device
|
||||
@@ -84,6 +149,7 @@ fn capture_sessions() -> Vec<(u32, bool)> {
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let mut sessions = Vec::new();
|
||||
let count = enumerator.get_count().unwrap_or(0);
|
||||
for index in 0..count {
|
||||
let Ok(control) = enumerator.get_session(index) else {
|
||||
@@ -96,14 +162,14 @@ fn capture_sessions() -> Vec<(u32, bool)> {
|
||||
sessions.push((pid, state == SessionState::Active));
|
||||
}
|
||||
}
|
||||
endpoints.push((device, sessions));
|
||||
}
|
||||
sessions
|
||||
endpoints
|
||||
}
|
||||
|
||||
/// The file name of `pid`'s executable, or `None` if the process cannot be
|
||||
/// opened (exited, or protected). Also used by `windows::capture` to name
|
||||
/// the process behind a process-loopback endpoint in its diagnostic log.
|
||||
pub(crate) fn image_name(pid: u32) -> Option<String> {
|
||||
/// 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 {
|
||||
@@ -155,7 +221,7 @@ fn process_table() -> ProcessTable {
|
||||
let snapshot = match CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) {
|
||||
Ok(snapshot) => snapshot,
|
||||
Err(err) => {
|
||||
log::debug!("mic sessions: process snapshot failed: {err}");
|
||||
log::debug!("audio sessions: process snapshot failed: {err}");
|
||||
return table;
|
||||
}
|
||||
};
|
||||
@@ -188,6 +254,6 @@ 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}");
|
||||
log::debug!("audio sessions: CloseHandle failed: {err}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user