YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
feat(audio): windows process tap wiring
Ezra Ellette23d1b571b404
diff --git a/Local Disk (C) - Shortcut.lnk b/Local Disk (C) - Shortcut.lnknew file mode 100644index 000000000..cd5d10446Binary files /dev/null and b/Local Disk (C) - Shortcut.lnk differdiff --git a/TESTING.md b/TESTING.mdindex 672e6e86e..0cfef87de 100644--- a/TESTING.md+++ b/TESTING.md@@ -111,11 +111,11 @@ commits: `28e5c247`- [ ] **piggyback fallback is backend-neutral** — repeat the tap-build-failure test with `experimentalCoreaudioSystemAudio` ON: the fallback System Audio stream comes up as the CoreAudio global tap (log shows a Process Tap gen start, not SCK), proving the piggyback fallback re-derives the backend from user settings at start time rather than hardcoding one. Same expectation for any future backend.- [ ] **piggyback fallback: app quits mid-meeting** — kill the meeting app: tap stream tears down within ~2s, global output resumes (unsuspended) until the watcher ends the meeting; no crash, no rebuild storm.- [ ] **piggyback fallback: no pid (manual/ui_scan/reattach)** — a meeting without a detected pid behaves exactly like flag-off (stable path).-- [ ] **piggyback on unsupported OS** — flag on, macOS <14.4: one-time log "using the stable capture path", capture identical to flag-off. Windows <20348: per-process tap degrades to full-endpoint loopback (whole system mix) with a warning.+- [ ] **piggyback on unsupported OS** — flag on, macOS <14.4 / Windows <20348: one-time log "using the stable capture path", capture identical to flag-off. Windows must fail the PID-specific tap and let the normal configured/default output capture path run; the Meeting Tap stream itself must not silently downgrade to endpoint-wide loopback.- [ ] **mid-call mic switch in the app** — change Zoom's mic mid-call: capture follows within ~4s (old session mic stops, new one starts); changing the OS default while Zoom is pinned elsewhere does NOT switch capture.- [ ] **mic-silent alert discipline** — muting in the meeting app for 5+ min produces NO notification (app not recording ⇒ real silence); a genuinely broken resolved-mic capture (zeros while the app records) first restarts silently, then notifies "screenpipe may not be hearing your mic" after ~4 min total, ≤1 per 30 min.- [ ] **per-pid tap silence watchdog (macOS)** — during a piggybacked call, tap silent 45s+ while the app renders audio → one rebuild (60s→960s backoff), never a rebuild while the app is genuinely idle.-- [ ] **Windows supervisor DEFERRED** — the Windows per-process tap currently has no target-exit detection, endpoint re-anchor, or silence watchdog (plan Task 3, deferred 2026-07-01; WIP in git stash). Before enabling the piggyback flag for Windows users, complete the supervisor and verify on a real Windows machine: app-quit mid-meeting, endpoint switch, silent-capture rebuild.+- [ ] **Windows per-process tap supervisor** — Windows 20348+: verify target-exit detection (app quits -> Meeting Tap stops and stable capture resumes), default-endpoint loopback re-anchor for explicit global loopback, and per-pid silence watchdog (target rendering + no tap packets for 45s -> rebuild with 60s->960s backoff). Confirm tap startup failure falls back through the user's normal/default audio settings, not through an implicit low-level endpoint-loopback downgrade inside the Meeting Tap.- [ ] **piggyback telemetry** — flag on, meetings-only: end a piggybacked meeting → one `piggyback_meeting_summary` appears in PostHog Live Events with the correct outcome; mic-silent incidents appear as `audio_capture_health_mic_*` events; analytics toggle OFF → nothing is sent (engine `TELEMETRY_ENABLED` gate).diff --git a/crates/screenpipe-audio/src/audio_manager/meeting_piggyback.rs b/crates/screenpipe-audio/src/audio_manager/meeting_piggyback.rsindex 58b37e6e5..a3d04f54d 100644--- a/crates/screenpipe-audio/src/audio_manager/meeting_piggyback.rs+++ b/crates/screenpipe-audio/src/audio_manager/meeting_piggyback.rs@@ -111,10 +111,10 @@ pub(crate) struct PiggybackObservation {pub suspended: HashSet<String>,/// Running non-session INPUT device display names.pub running_inputs: Vec<String>,- /// The stable far-end device name to suspend/resume ("System Audio (output)"- /// on macOS; empty on Windows where windows_output_follow owns the far end- /// and is skipped via the sweep's return value instead).- pub stable_output: Option<String>,+ /// Stable/global far-end captures to suspend only while the Meeting Tap is+ /// streaming. macOS has one synthetic System Audio device; Windows may have+ /// one or more endpoint loopbacks currently running.+ pub stable_outputs: Vec<String>,pub tap_strikes: u32,pub tap_cooldown_elapsed: bool,}@@ -165,9 +165,9 @@ pub(crate) fn decide_piggyback(obs: &PiggybackObservation) -> Vec<PiggybackActioif tap_registered && !tap_streaming {// Tap died (app-quit exit sets is_disconnected; supervisor gave up).actions.push(PiggybackAction::StopSessionDevice(tap.clone()));- if let Some(stable) = &obs.stable_output {- if obs.suspended.contains(stable) {- actions.push(PiggybackAction::Resume(stable.clone()));+ for dev in &obs.suspended {+ if dev.ends_with("(output)") {+ actions.push(PiggybackAction::Resume(dev.clone()));}}actions.push(PiggybackAction::NoteTapFailure);@@ -176,15 +176,15 @@ pub(crate) fn decide_piggyback(obs: &PiggybackObservation) -> Vec<PiggybackActioactions.push(PiggybackAction::StartTap { pids: vec![pid] });}// Stable output stays live (or resumes) while the tap isn't delivering.- if let Some(stable) = &obs.stable_output {- if obs.suspended.contains(stable) {- actions.push(PiggybackAction::Resume(stable.clone()));+ for dev in &obs.suspended {+ if dev.ends_with("(output)") {+ actions.push(PiggybackAction::Resume(dev.clone()));}}} else {// Tap is streaming: the stable global capture is redundant (double// transcription) — suspend it for the meeting's duration.- if let Some(stable) = &obs.stable_output {+ for stable in &obs.stable_outputs {if !obs.suspended.contains(stable) {actions.push(PiggybackAction::Suspend(stable.clone()));}@@ -494,40 +494,49 @@ fn fold_volatile_state(telemetry: &mut MeetingTelemetry, state: &PiggybackState)/// only exists on macOS and Windows; elsewhere the tap can't be built, so the/// observation simply never piggybacks and rides the stable path.///-/// Windows is force-gated OFF here: the Windows per-process capture supervisor-/// (target-exit detection, endpoint re-anchor, silence watchdog) was deferred-/// (see TESTING.md "Windows supervisor DEFERRED" — the re-enable checklist).-/// Without it the per-pid tap can zombie or double-capture, so a Windows user-/// with the flag on must land on WarnUnavailableOnce + the stable path, i.e.-/// exactly today's behavior. Re-enable by returning the real availability once-/// the supervisor lands and its checklist is verified on a real machine.-#[cfg(target_os = "macos")]+#[cfg(any(target_os = "macos", target_os = "windows"))]fn tap_available() -> bool {crate::core::process_tap::is_process_tap_available()}-#[cfg(not(target_os = "macos"))]+#[cfg(not(any(target_os = "macos", target_os = "windows")))]fn tap_available() -> bool {false}-/// The stable far-end device name the sweep suspends while the tap streams and-/// resumes on any gap. This is a device NAME, not a backend: when it restarts,-/// `AudioStream::from_device` picks the capture method from the user's own-/// settings (SCK default / CoreAudio global tap if their flag is on / whatever-/// exists in the future). macOS captures system audio as a single global-/// device; on Windows `windows_output_follow` owns the render endpoints, so-/// there is no single name to suspend — the sweep's return value skips that-/// sweep instead (see `run_output_follow_sweep`'s leading guard).+/// Stable far-end device names the sweep suspends while the tap streams and+/// resumes on any gap. These are device NAMES, not backend choices: restarting+/// them re-enters the user's normal settings/default-device path. macOS has one+/// synthetic System Audio device; Windows snapshots the currently running+/// non-session output endpoints.#[cfg(target_os = "macos")]-fn stable_output_name() -> Option<String> {- Some(format!(+fn stable_output_names(+ _audio_manager: &super::AudioManager,+ _session_devices: &HashSet<String>,+) -> Vec<String> {+ vec![format!("{} (output)",crate::core::device::MACOS_OUTPUT_AUDIO_DEVICE_NAME- ))+ )]}-#[cfg(not(target_os = "macos"))]-fn stable_output_name() -> Option<String> {- None+#[cfg(target_os = "windows")]+fn stable_output_names(+ audio_manager: &super::AudioManager,+ session_devices: &HashSet<String>,+) -> Vec<String> {+ audio_manager+ .current_devices()+ .into_iter()+ .filter(|d| d.device_type == crate::core::device::DeviceType::Output)+ .map(|d| d.to_string())+ .filter(|name| !session_devices.contains(name))+ .collect()+}+#[cfg(not(any(target_os = "macos", target_os = "windows")))]+fn stable_output_names(+ _audio_manager: &super::AudioManager,+ _session_devices: &HashSet<String>,+) -> Vec<String> {+ Vec::new()}/// Side-effecting wrapper around [`decide_piggyback`]. Snapshots the relevant@@ -612,6 +621,7 @@ pub(crate) async fn run_meeting_piggyback_sweep(let cooldown_elapsed = state.last_tap_attempt.is_none_or(|t| t.elapsed() >= std::time::Duration::from_secs(TAP_RETRY_COOLDOWN_SECS));+ let stable_outputs = stable_output_names(audio_manager, &session_devices);let obs = PiggybackObservation {flag_on,@@ -623,7 +633,7 @@ pub(crate) async fn run_meeting_piggyback_sweep(session_streaming: session_streaming.clone(),suspended,running_inputs,- stable_output: stable_output_name(),+ stable_outputs,tap_strikes: state.tap_strikes,tap_cooldown_elapsed: cooldown_elapsed,};@@ -887,7 +897,7 @@ mod tests {meetings_only: true,tap_available: true,meeting: Some(Some(4242)),- stable_output: Some("System Audio (output)".to_string()),+ stable_outputs: vec!["System Audio (output)".to_string()],tap_cooldown_elapsed: true,..Default::default()}@@ -976,6 +986,22 @@ mod tests {)));}+ #[test]+ fn streaming_tap_suspends_all_stable_outputs() {+ let mut obs = base();+ obs.stable_outputs = vec![+ "Speakers (Realtek) (output)".to_string(),+ "Headset (output)".to_string(),+ ];+ obs.session_devices = [tap_name()].into();+ obs.session_streaming = [tap_name()].into();+ let actions = decide_piggyback(&obs);+ assert!(actions.contains(&PiggybackAction::Suspend(+ "Speakers (Realtek) (output)".to_string()+ )));+ assert!(actions.contains(&PiggybackAction::Suspend("Headset (output)".to_string())));+ }+#[test]fn dead_tap_falls_back_stopping_session_and_resuming_stable() {let mut obs = base();@@ -990,6 +1016,21 @@ mod tests {assert!(actions.contains(&PiggybackAction::NoteTapFailure));}+ #[test]+ fn tap_retry_gap_resumes_suspended_output() {+ let mut obs = base();+ obs.tap_strikes = 1;+ obs.tap_cooldown_elapsed = false;+ obs.suspended = ["Speakers (Realtek) (output)".to_string()].into();+ let actions = decide_piggyback(&obs);+ assert!(actions.contains(&PiggybackAction::Resume(+ "Speakers (Realtek) (output)".to_string()+ )));+ assert!(!actions+ .iter()+ .any(|a| matches!(a, PiggybackAction::StartTap { .. })));+ }+#[test]fn strikes_exhausted_stops_retrying_this_meeting() {let mut obs = base();diff --git a/crates/screenpipe-audio/src/core/process_tap/windows.rs b/crates/screenpipe-audio/src/core/process_tap/windows.rsindex f7cc63bc5..70c7c87dc 100644--- a/crates/screenpipe-audio/src/core/process_tap/windows.rs+++ b/crates/screenpipe-audio/src/core/process_tap/windows.rs@@ -35,7 +35,7 @@ use std::time::Duration;use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt};use tokio::sync::broadcast;use tracing::{debug, info, warn};-use windows::core::{implement, IUnknown, Interface, HRESULT, PCWSTR};+use windows::core::{implement, IUnknown, Interface, HRESULT, PCWSTR, PWSTR};use windows::Win32::Foundation::{CloseHandle, HANDLE, RPC_E_CHANGED_MODE, WAIT_OBJECT_0, WAIT_TIMEOUT,};@@ -51,14 +51,15 @@ use windows::Win32::Media::Audio::{VIRTUAL_AUDIO_DEVICE_PROCESS_LOOPBACK, WAVEFORMATEX, WAVE_FORMAT_PCM,};use windows::Win32::System::Com::{- CoCreateInstance, CoInitializeEx, CoUninitialize, IAgileObject, IAgileObject_Impl, CLSCTX_ALL,- COINIT_MULTITHREADED,+ CoCreateInstance, CoInitializeEx, CoTaskMemFree, CoUninitialize, IAgileObject,+ IAgileObject_Impl, CLSCTX_ALL, COINIT_MULTITHREADED,};use windows::Win32::System::Threading::{- CreateEventW, GetCurrentThread, SetThreadPriority, WaitForSingleObject,- THREAD_PRIORITY_TIME_CRITICAL,+ CreateEventW, GetCurrentThread, OpenProcess, SetThreadPriority, WaitForSingleObject,+ PROCESS_SYNCHRONIZE, THREAD_PRIORITY_TIME_CRITICAL,};+use super::{classify_silence, SilenceVerdict, WatchdogCounters};use crate::core::stream::AudioStreamConfig;use crate::utils::audio::audio_to_mono;@@ -66,6 +67,11 @@ const PROCESS_LOOPBACK_MIN_BUILD: u32 = 20_348;const ACTIVATION_TIMEOUT: Duration = Duration::from_secs(5);const STARTUP_TIMEOUT: Duration = Duration::from_secs(8);const CAPTURE_WAIT_MS: u32 = 250;+const WATCHDOG_SILENCE_SECS: u64 = 45;+const SILENCE_AMP_EPS: f32 = 0.002;+const REBUILD_COOLDOWN_SECS: u64 = 60;+const REBUILD_BACKOFF_CAP: u32 = 4;+const MAX_CONSECUTIVE_REBUILD_FAILURES: u32 = 3;const SAMPLE_RATE: u32 = 48_000;const CHANNELS: u16 = 2;const BITS_PER_SAMPLE: u16 = 16;@@ -206,24 +212,119 @@ fn spawn_wasapi_loopback(let thread_label = label.clone();let handle = tokio::task::spawn_blocking(move || {- let result = (|| -> Result<()> {- let _com = ComApartment::enter()?;- let mut capture = unsafe { build_capture(target)? };- let config = capture.config.clone();- let _ = ready_tx.send(Ok(config));- info!(- "Windows WASAPI loopback capture started ({}, {} Hz, {} ch)",- thread_label,- capture.config.sample_rate().0,- capture.channels+ let _com = match ComApartment::enter() {+ Ok(com) => com,+ Err(error) => {+ let _ = ready_tx.send(Err(error));+ return;+ }+ };++ let (mut capture, mut watchdog) = match unsafe { build_wasapi_capture(target) } {+ Ok(built) => built,+ Err(error) => {+ let _ = ready_tx.send(Err(error));+ return;+ }+ };+ let config = capture.config.clone();+ let _ = ready_tx.send(Ok(config));+ info!(+ "Windows WASAPI loopback capture started ({}, {} Hz, {} ch)",+ thread_label,+ capture.config.sample_rate().0,+ capture.channels+ );++ let target_watch = match target {+ LoopbackTarget::ProcessTree(pid) => TargetProcessWatch::open(pid),+ LoopbackTarget::DefaultEndpoint => None,+ };+ let probe_pid = match target {+ LoopbackTarget::ProcessTree(pid) => Some(pid as i32),+ LoopbackTarget::DefaultEndpoint => None,+ };+ let mut rebuild_streak = 0u32;+ let mut rebuild_failures = 0u32;++ loop {+ let endpoint_baseline = match target {+ LoopbackTarget::DefaultEndpoint => current_default_render_endpoint_id(),+ LoopbackTarget::ProcessTree(_) => None,+ };+ let exit = run_capture_loop(+ &mut capture,+ &tx,+ &is_disconnected,+ &thread_label,+ target_watch.as_ref(),+ endpoint_baseline.as_deref(),+ watchdog.as_ref(),+ probe_pid,);- run_capture_loop(&mut capture, tx, is_disconnected, &thread_label);- Ok(())- })();+ unsafe {+ let _ = capture.audio_client.0.Stop();+ }- if let Err(error) = result {- let _ = ready_tx.send(Err(error));+ let step = supervisor_policy(exit);+ if step == SupervisorStep::Stop {+ break;+ }++ drop(capture);+ let mut cooldown = if step == SupervisorStep::RebuildNow {+ Duration::ZERO+ } else {+ let delay = rebuild_cooldown(rebuild_streak);+ rebuild_streak = rebuild_streak.saturating_add(1);+ warn!(+ "Windows loopback ({thread_label}): {exit:?}; rebuilding in {}s",+ delay.as_secs()+ );+ delay+ };++ loop {+ if wait_for_disconnect(&is_disconnected, cooldown) {+ is_disconnected.store(true, Ordering::Relaxed);+ debug!("Windows WASAPI loopback supervisor exited ({thread_label})");+ return;+ }++ match unsafe { build_wasapi_capture(target) } {+ Ok((new_capture, new_watchdog)) => {+ capture = new_capture;+ watchdog = new_watchdog;+ rebuild_failures = 0;+ if step == SupervisorStep::RebuildNow {+ rebuild_streak = 0;+ }+ break;+ }+ Err(error) => {+ rebuild_failures = rebuild_failures.saturating_add(1);+ if rebuild_failures >= MAX_CONSECUTIVE_REBUILD_FAILURES {+ warn!(+ "Windows loopback rebuild failed {rebuild_failures} consecutive times ({thread_label}); giving up so stable capture can resume: {error}"+ );+ is_disconnected.store(true, Ordering::Relaxed);+ debug!("Windows WASAPI loopback supervisor exited ({thread_label})");+ return;+ }+ let delay = rebuild_cooldown(rebuild_streak);+ rebuild_streak = rebuild_streak.saturating_add(1);+ warn!(+ "Windows loopback rebuild failed ({thread_label}): {error}; retrying in {}s",+ delay.as_secs()+ );+ cooldown = delay;+ }+ }+ }}++ is_disconnected.store(true, Ordering::Relaxed);+ debug!("Windows WASAPI loopback supervisor exited ({thread_label})");});match ready_rx.recv_timeout(STARTUP_TIMEOUT) {@@ -241,7 +342,9 @@ fn spawn_wasapi_loopback(}}-unsafe fn build_capture(target: LoopbackTarget) -> Result<WasapiLoopbackCapture> {+unsafe fn build_wasapi_capture(+ target: LoopbackTarget,+) -> Result<(WasapiLoopbackCapture, Arc<WatchdogCounters>)> {let audio_client = match target {LoopbackTarget::ProcessTree(pid) => activate_process_loopback_client(pid)?,LoopbackTarget::DefaultEndpoint => activate_default_endpoint_loopback_client()?,@@ -273,13 +376,14 @@ unsafe fn build_capture(target: LoopbackTarget) -> Result<WasapiLoopbackCapture>.Start().map_err(|e| anyhow!("failed to start WASAPI loopback client: {e}"))?;- Ok(WasapiLoopbackCapture {+ let capture = WasapiLoopbackCapture {audio_client,capture_client,sample_ready,config: AudioStreamConfig::new(SAMPLE_RATE, CHANNELS),channels: CHANNELS,- })+ };+ Ok((capture, WatchdogCounters::new()))}unsafe fn activate_default_endpoint_loopback_client() -> Result<AudioClientSend> {@@ -329,43 +433,138 @@ unsafe fn activate_process_loopback_client(root_pid: u32) -> Result<AudioClientS}}+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+enum CaptureExit {+ Disconnected,+ TargetExited,+ EndpointChanged,+ SilentBroken,+ WaitFailed,+ DrainFailed,+}++#[derive(Debug, Clone, Copy, PartialEq, Eq)]+enum SupervisorStep {+ Stop,+ RebuildNow,+ RebuildAfterCooldown,+}++fn supervisor_policy(exit: CaptureExit) -> SupervisorStep {+ match exit {+ CaptureExit::Disconnected | CaptureExit::TargetExited => SupervisorStep::Stop,+ CaptureExit::EndpointChanged => SupervisorStep::RebuildNow,+ CaptureExit::SilentBroken | CaptureExit::WaitFailed | CaptureExit::DrainFailed => {+ SupervisorStep::RebuildAfterCooldown+ }+ }+}++fn rebuild_cooldown(streak: u32) -> Duration {+ Duration::from_secs(+ REBUILD_COOLDOWN_SECS * 2u64.saturating_pow(streak.min(REBUILD_BACKOFF_CAP)),+ )+}++fn wait_for_disconnect(is_disconnected: &AtomicBool, duration: Duration) -> bool {+ let started = std::time::Instant::now();+ while started.elapsed() < duration {+ if is_disconnected.load(Ordering::Relaxed) {+ return true;+ }+ std::thread::sleep(Duration::from_millis(250));+ }+ is_disconnected.load(Ordering::Relaxed)+}+fn run_capture_loop(capture: &mut WasapiLoopbackCapture,- tx: broadcast::Sender<Vec<f32>>,- is_disconnected: Arc<AtomicBool>,+ tx: &broadcast::Sender<Vec<f32>>,+ is_disconnected: &Arc<AtomicBool>,label: &str,-) {+ target_watch: Option<&TargetProcessWatch>,+ endpoint_baseline: Option<&str>,+ watchdog: &WatchdogCounters,+ probe_pid: Option<i32>,+) -> CaptureExit {// Insulates this capture thread from the process's BELOW_NORMAL priority// class (and foreground contention generally) the same way cpal's WASAPI// backend does for its own capture threads.unsafe {let _ = SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);}+ let mut silence_started: Option<std::time::Instant> = None;while !is_disconnected.load(Ordering::Relaxed) {+ if target_watch.is_some_and(TargetProcessWatch::has_exited) {+ info!("Windows WASAPI loopback target exited ({label})");+ return CaptureExit::TargetExited;+ }+ if endpoint_baseline.is_some_and(|baseline| {+ current_default_render_endpoint_id().is_some_and(|current| current != baseline)+ }) {+ info!("Windows default render endpoint changed; rebuilding loopback ({label})");+ return CaptureExit::EndpointChanged;+ }+let wait = unsafe { WaitForSingleObject(capture.sample_ready.0, CAPTURE_WAIT_MS) };- if wait == WAIT_TIMEOUT {+ if wait != WAIT_TIMEOUT && wait != WAIT_OBJECT_0 {+ warn!("Windows WASAPI loopback wait failed ({label}): {wait:?}");+ return CaptureExit::WaitFailed;+ }+ if wait == WAIT_OBJECT_0 {+ if let Err(error) = unsafe { drain_capture_packets(capture, tx, watchdog) } {+ warn!("Windows WASAPI loopback packet drain failed ({label}): {error}");+ return CaptureExit::DrainFailed;+ }+ }++ let (window_callbacks, window_peak) = watchdog.drain();+ if window_callbacks > 0 && window_peak > SILENCE_AMP_EPS {+ silence_started = None;continue;}- if wait != WAIT_OBJECT_0 {- warn!("Windows WASAPI loopback wait failed ({label}): {wait:?}");- break;+ silence_started.get_or_insert_with(std::time::Instant::now);+ let silent_long_enough = silence_started+ .map(|t| t.elapsed().as_secs() >= WATCHDOG_SILENCE_SECS)+ .unwrap_or(false);+ if !silent_long_enough {+ continue;}- if let Err(error) = unsafe { drain_capture_packets(capture, &tx) } {- warn!("Windows WASAPI loopback packet drain failed ({label}): {error}");- break;++ let target_output_active = probe_pid.and_then(|pid| {+ crate::core::meeting_audio::process_audio_activity(pid).map(|a| a.output_active)+ });+ match classify_silence(+ window_callbacks,+ window_peak,+ SILENCE_AMP_EPS,+ false,+ target_output_active,+ ) {+ SilenceVerdict::SilentBroken => {+ warn!(+ "Windows WASAPI loopback silent for {}s while target is rendering ({label})",+ WATCHDOG_SILENCE_SECS+ );+ return CaptureExit::SilentBroken;+ }+ SilenceVerdict::Healthy => {+ silence_started = None;+ }+ SilenceVerdict::SilentIdle | SilenceVerdict::Inconclusive => {+ silence_started = Some(std::time::Instant::now());+ }}}- unsafe {- let _ = capture.audio_client.0.Stop();- }- debug!("Windows WASAPI loopback capture stopped ({label})");+ CaptureExit::Disconnected}unsafe fn drain_capture_packets(capture: &WasapiLoopbackCapture,tx: &broadcast::Sender<Vec<f32>>,+ watchdog: &WatchdogCounters,) -> Result<()> {loop {let frames = capture@@ -386,6 +585,7 @@ unsafe fn drain_capture_packets(.GetBuffer(&mut data, &mut frames_available, &mut flags, None, None).map_err(|e| anyhow!("GetBuffer failed: {e}"))?;+ watchdog.count_callback();let sample_count = frames_available as usize * capture.channels as usize;let interleaved = if (flags & AUDCLNT_BUFFERFLAGS_SILENT.0 as u32) != 0 || data.is_null() {vec![0.0; sample_count]@@ -402,10 +602,13 @@ unsafe fn drain_capture_packets(.map_err(|e| anyhow!("ReleaseBuffer failed: {e}"))?;let mono = audio_to_mono(&interleaved, capture.channels);+ let peak = mono+ .iter()+ .fold(0.0_f32, |max, sample| max.max(sample.abs()));+ watchdog.record_peak(peak);let _ = tx.send(mono);}}-fn pcm_i16_stereo_format() -> WAVEFORMATEX {let block_align = CHANNELS * BYTES_PER_SAMPLE;WAVEFORMATEX {@@ -525,6 +728,85 @@ impl Drop for ComApartment {}}+struct CoTaskMemPwstr(PWSTR);++impl CoTaskMemPwstr {+ fn to_string(&self) -> Option<String> {+ unsafe { self.0.to_string().ok() }+ }+}++impl Drop for CoTaskMemPwstr {+ fn drop(&mut self) {+ if !self.0.is_null() {+ unsafe { CoTaskMemFree(Some(self.0.as_ptr() as _)) };+ }+ }+}++struct TargetProcessWatch {+ handle: HANDLE,+ pid: u32,+}++impl TargetProcessWatch {+ fn open(pid: u32) -> Option<Self> {+ match unsafe { OpenProcess(PROCESS_SYNCHRONIZE, false, pid) } {+ Ok(handle) if !handle.is_invalid() => Some(Self { handle, pid }),+ Ok(handle) => {+ if !handle.is_invalid() {+ unsafe {+ let _ = CloseHandle(handle);+ }+ }+ warn!("Windows process loopback: could not open target pid {pid} for exit watch");+ None+ }+ Err(error) => {+ warn!(+ "Windows process loopback: could not open target pid {pid} for exit watch: {error}"+ );+ None+ }+ }+ }++ fn has_exited(&self) -> bool {+ let wait = unsafe { WaitForSingleObject(self.handle, 0) };+ if wait == WAIT_OBJECT_0 {+ return true;+ }+ if wait == WAIT_TIMEOUT {+ return false;+ }+ warn!(+ "Windows process loopback: target pid {} wait failed ({wait:?}); stopping tap",+ self.pid+ );+ true+ }+}++impl Drop for TargetProcessWatch {+ fn drop(&mut self) {+ if !self.handle.is_invalid() {+ unsafe {+ let _ = CloseHandle(self.handle);+ }+ }+ }+}++fn current_default_render_endpoint_id() -> Option<String> {+ unsafe {+ let enumerator: IMMDeviceEnumerator =+ CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL).ok()?;+ let endpoint = enumerator.GetDefaultAudioEndpoint(eRender, eConsole).ok()?;+ let id = CoTaskMemPwstr(endpoint.GetId().ok()?);+ id.to_string()+ }+}+fn select_target_root_pid(pids: &[i32]) -> Result<u32> {let mut roots = pids.iter()@@ -658,4 +940,40 @@ mod tests {assert_eq!(block_align, 4);assert_eq!(avg_bytes_per_sec, 192_000);}++ #[test]+ fn capture_exit_reasons_map_to_supervisor_policy() {+ assert_eq!(+ supervisor_policy(CaptureExit::Disconnected),+ SupervisorStep::Stop+ );+ assert_eq!(+ supervisor_policy(CaptureExit::TargetExited),+ SupervisorStep::Stop+ );+ assert_eq!(+ supervisor_policy(CaptureExit::EndpointChanged),+ SupervisorStep::RebuildNow+ );+ assert_eq!(+ supervisor_policy(CaptureExit::SilentBroken),+ SupervisorStep::RebuildAfterCooldown+ );+ assert_eq!(+ supervisor_policy(CaptureExit::WaitFailed),+ SupervisorStep::RebuildAfterCooldown+ );+ assert_eq!(+ supervisor_policy(CaptureExit::DrainFailed),+ SupervisorStep::RebuildAfterCooldown+ );+ }++ #[test]+ fn rebuild_cooldown_backs_off_and_caps() {+ assert_eq!(rebuild_cooldown(0).as_secs(), 60);+ assert_eq!(rebuild_cooldown(1).as_secs(), 120);+ assert_eq!(rebuild_cooldown(4).as_secs(), 960);+ assert_eq!(rebuild_cooldown(9).as_secs(), 960, "cap at 2^4");+ }}