Files
call-recorder/crates/audio/src/render_sessions.rs
T
2026-11-06 10:49:02 +01:00

140 lines
4.9 KiB
Rust

//! Which render endpoint a detected call plays on.
//!
//! A detected call's far end is recorded by loopback of an output device.
//! The device is the one on which the call app has an active audio session;
//! a session belongs to the call if its process is attributed to the call's
//! exe (Teams helpers count as Teams, see [`crate::process_tree`]) or sits in
//! the call's process tree. Windows enumerates the sessions; the choice is a
//! pure function over that list so it can be tested anywhere.
/// One audio session on a render endpoint (an app playing, or able to play,
/// on an output device).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RenderSession {
/// The session process's exe, after Teams-helper attribution.
pub exe: String,
pub pid: u32,
/// Top of the process's same-exe parent chain (or its Teams root).
pub root_pid: u32,
/// Whether a stream of the session is running.
pub active: bool,
pub device_id: String,
pub device_name: String,
}
/// The first active session (in enumeration order) that belongs to the call
/// app `exe` (case-insensitive) or to its process tree `root_pid`; its
/// device is where the call's audio plays. `None` if the call app has no
/// active render session.
pub fn render_endpoint_for<'a>(
sessions: &'a [RenderSession],
exe: &str,
root_pid: u32,
) -> Option<&'a RenderSession> {
sessions
.iter()
.find(|s| s.active && (s.exe.eq_ignore_ascii_case(exe) || s.root_pid == root_pid))
}
/// "exe pid N root N on device" per active session, in enumeration order;
/// for seeing in the log which process really plays a call's audio.
pub fn describe_render_sessions(sessions: &[RenderSession]) -> String {
sessions
.iter()
.filter(|s| s.active)
.map(|s| {
format!(
"{} pid {} root {} on {}",
s.exe, s.pid, s.root_pid, s.device_name
)
})
.collect::<Vec<_>>()
.join(", ")
}
#[cfg(test)]
mod tests {
use super::*;
fn session(exe: &str, pid: u32, root_pid: u32, active: bool, device: &str) -> RenderSession {
RenderSession {
exe: exe.to_string(),
pid,
root_pid,
active,
device_id: format!("{{id-{device}}}"),
device_name: device.to_string(),
}
}
fn chosen<'a>(sessions: &'a [RenderSession], exe: &str, root_pid: u32) -> Option<&'a str> {
render_endpoint_for(sessions, exe, root_pid).map(|s| s.device_id.as_str())
}
#[test]
fn a_session_of_the_call_exe_chooses_its_device() {
let sessions = [
session("spotify.exe", 50, 50, true, "Lautsprecher"),
session("MS-Teams.exe", 300, 120, true, "Headset"),
];
assert_eq!(chosen(&sessions, "ms-teams.exe", 100), Some("{id-Headset}"));
}
#[test]
fn a_session_in_the_call_process_tree_chooses_its_device() {
let sessions = [
session("spotify.exe", 50, 50, true, "Lautsprecher"),
session("audiohost.exe", 300, 100, true, "Headset"),
];
assert_eq!(chosen(&sessions, "ms-teams.exe", 100), Some("{id-Headset}"));
}
#[test]
fn inactive_sessions_are_ignored() {
let sessions = [
session("ms-teams.exe", 100, 100, false, "Lautsprecher"),
session("ms-teams.exe", 200, 100, true, "Headset"),
];
assert_eq!(chosen(&sessions, "ms-teams.exe", 100), Some("{id-Headset}"));
assert_eq!(chosen(&sessions[..1], "ms-teams.exe", 100), None);
}
#[test]
fn no_session_of_the_call_app_chooses_nothing() {
let sessions = [
session("spotify.exe", 50, 50, true, "Lautsprecher"),
session("chrome.exe", 70, 60, true, "Headset"),
];
assert_eq!(chosen(&sessions, "ms-teams.exe", 100), None);
assert_eq!(chosen(&[], "ms-teams.exe", 100), None);
}
#[test]
fn the_first_matching_endpoint_wins() {
let sessions = [
session("ms-teams.exe", 100, 100, true, "Headset"),
session("ms-teams.exe", 100, 100, true, "Lautsprecher"),
];
assert_eq!(chosen(&sessions, "ms-teams.exe", 100), Some("{id-Headset}"));
let reversed = [sessions[1].clone(), sessions[0].clone()];
assert_eq!(
chosen(&reversed, "ms-teams.exe", 100),
Some("{id-Lautsprecher}")
);
}
#[test]
fn describe_lists_active_sessions_in_order() {
let sessions = [
session("ms-teams.exe", 300, 100, true, "Headset"),
session("spotify.exe", 50, 50, false, "Lautsprecher"),
session("chrome.exe", 70, 60, true, "Lautsprecher"),
];
assert_eq!(
describe_render_sessions(&sessions),
"ms-teams.exe pid 300 root 100 on Headset, chrome.exe pid 70 root 60 on Lautsprecher"
);
assert_eq!(describe_render_sessions(&[]), "");
}
}