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
fix: stop listing unrecordable output devices on macOS, add backoff to device monitor
Louis Beaumontaaf531f377b4
diff --git a/crates/screenpipe-audio/src/audio_manager/device_monitor.rs b/crates/screenpipe-audio/src/audio_manager/device_monitor.rsindex 80097d05d..e4a31dfe0 100644--- a/crates/screenpipe-audio/src/audio_manager/device_monitor.rs+++ b/crates/screenpipe-audio/src/audio_manager/device_monitor.rs@@ -1,8 +1,12 @@-use std::{collections::HashSet, sync::Arc, time::Duration};+use std::{+ collections::{HashMap, HashSet},+ sync::Arc,+ time::{Duration, Instant},+};use anyhow::Result;use tokio::{sync::Mutex, task::JoinHandle, time::sleep};-use tracing::{debug, error, info};+use tracing::{debug, error, info, warn};use crate::{core::device::{default_input_device, default_output_device, parse_audio_device, DeviceType},@@ -64,6 +68,11 @@ pub async fn start_device_monitor(let mut disconnected_devices: HashSet<String> = HashSet::new();let mut default_tracker = SystemDefaultTracker::new();+ // Track devices that repeatedly fail to start so we don't spam errors+ // every 2 seconds. After a failure, back off for increasing durations.+ let mut failed_devices: HashMap<String, (u32, Instant)> = HashMap::new();+ const MAX_FAIL_BACKOFF_SECS: u64 = 300; // cap at 5 minutes+// Initialize tracker with current defaultslet _ = default_tracker.check_input_changed();let _ = default_tracker.check_output_changed().await;@@ -116,17 +125,27 @@ pub async fn start_device_monitor(}}- // Start the new default output device+ // Start the new default output device (reset cooldown on change)if let Ok(new_device) = parse_audio_device(&new_default_output) {+ failed_devices.remove(&new_default_output);match audio_manager.start_device(&new_device).await {- Ok(()) => info!(- "switched to new system default output: {}",- new_default_output- ),- Err(e) => error!(- "failed to start new default output {}: {}",- new_default_output, e- ),+ Ok(()) => {+ info!(+ "switched to new system default output: {}",+ new_default_output+ );+ }+ Err(e) => {+ let count = failed_devices+ .entry(new_default_output.clone())+ .or_insert((0, Instant::now()));+ count.0 += 1;+ count.1 = Instant::now();+ error!(+ "failed to start new default output {}: {} (will back off)",+ new_default_output, e+ );+ }}}}@@ -144,20 +163,47 @@ pub async fn start_device_monitor(if !has_output {if let Ok(default_output) = default_output_device().await {let device_name = default_output.to_string();- info!(- "no output device running, starting default: {}",- device_name- );- match audio_manager.start_device(&default_output).await {- Ok(()) => {- default_tracker.last_output = Some(device_name.clone());- info!("started missing output device: {}", device_name);- }- Err(e) => {- debug!(- "could not start output device {}: {}",- device_name, e- );++ // Skip if this device is in cooldown from previous failures+ let should_skip = if let Some((fails, last_fail)) =+ failed_devices.get(&device_name)+ {+ let backoff = Duration::from_secs(+ (2u64.pow((*fails).min(8))).min(MAX_FAIL_BACKOFF_SECS),+ );+ last_fail.elapsed() < backoff+ } else {+ false+ };++ if !should_skip {+ info!(+ "no output device running, starting default: {}",+ device_name+ );+ match audio_manager.start_device(&default_output).await {+ Ok(()) => {+ failed_devices.remove(&device_name);+ default_tracker.last_output =+ Some(device_name.clone());+ info!(+ "started missing output device: {}",+ device_name+ );+ }+ Err(e) => {+ let count = failed_devices+ .entry(device_name.clone())+ .or_insert((0, Instant::now()));+ count.0 += 1;+ count.1 = Instant::now();+ let backoff_secs = (2u64.pow(count.0.min(8)))+ .min(MAX_FAIL_BACKOFF_SECS);+ warn!(+ "could not start output device {} ({} failures, next retry in {}s): {}",+ device_name, count.0, backoff_secs, e+ );+ }}}}diff --git a/crates/screenpipe-audio/src/core/device.rs b/crates/screenpipe-audio/src/core/device.rsindex 566cf5933..e2b45922c 100644--- a/crates/screenpipe-audio/src/core/device.rs+++ b/crates/screenpipe-audio/src/core/device.rs@@ -310,7 +310,11 @@ pub async fn list_audio_devices() -> Result<Vec<AudioDevice>> {}}- // add default output device - on macos think of custom virtual devices+ // Add physical output devices — but NOT on macOS where only SCK display+ // devices can actually be recorded from. Physical output devices (headphones,+ // speakers) appear recordable in the UI but always fail with "device not found"+ // in get_cpal_device_and_config because SCK's input_devices() doesn't include them.+ #[cfg(not(target_os = "macos"))]for device in host.output_devices()? {if let Ok(name) = device.name() {if should_include_output_device(&name) {@@ -319,7 +323,9 @@ pub async fn list_audio_devices() -> Result<Vec<AudioDevice>> {}}- // last, add devices that are listed in .devices() which are not already in the devices vector+ // Last, add devices that are listed in .devices() which are not already in the+ // devices vector. Skip on macOS — only SCK display devices are valid output sources.+ #[cfg(not(target_os = "macos"))]if let Ok(other_devices) = host.devices() {for device in other_devices {let name = match device.name() {@@ -464,8 +470,11 @@ pub async fn default_output_device() -> Result<AudioDevice> {#[cfg(target_os = "macos")]{- // ! see https://github.com/RustAudio/cpal/pull/894- // Try to get device from ScreenCaptureKit first+ // On macOS, output audio capture ONLY works via ScreenCaptureKit display+ // audio. Physical output devices (headphones, speakers) cannot be recorded+ // from — they always fail with "Audio device not found" in+ // get_cpal_device_and_config because SCK input_devices() doesn't include them.+ // See https://github.com/RustAudio/cpal/pull/894match get_screen_capture_host().await {Ok(host) => {if let Some(device) = host.default_input_device() {@@ -473,28 +482,19 @@ pub async fn default_output_device() -> Result<AudioDevice> {return Ok(AudioDevice::new(name, DeviceType::Output));}}- tracing::warn!("ScreenCaptureKit host available but no default input device found");+ return Err(anyhow!(+ "ScreenCaptureKit available but no display audio device found — \+ output audio capture requires a display device"+ ));}Err(e) => {- tracing::warn!(+ return Err(anyhow!("ScreenCaptureKit unavailable for output audio capture: {} — \- falling back to physical output device (may not capture system audio)",+ output audio capture is not possible without it",e- );+ ));}}-- // Fall back to default output device- let host = cpal::default_host();- let device = host- .default_output_device()- .ok_or_else(|| anyhow!("No default output device found"))?;- let name = device.name()?;- tracing::warn!(- "using fallback physical output device: '{}' (not ScreenCaptureKit display audio)",- name- );- Ok(AudioDevice::new(name, DeviceType::Output))}// Linux without pulseaudio feature