From 12248fed718110333a831762c1e6b9d22f5c3168 Mon Sep 17 00:00:00 2001 From: yunchan Date: Tue, 27 Oct 2026 03:02:12 +0900 Subject: [PATCH] screencapturekit: audio only on macOS 13+, without this process (Kitedesk B283) The host decided availability by sending setCapturesAudio: and setExcludesCurrentProcessAudio: to a new SCStreamConfiguration. In a binary that weak-links ScreenCaptureKit so that it still launches on macOS 10.15, that class is nil before 12.3. Host::new, Host::is_available, Devices::new and build_input_stream now ask the OS version first (capturesAudio and excludesCurrentProcessAudio are macOS 13.0) and touch no ScreenCaptureKit class below it. The stream set excludesCurrentProcessAudio to false, so what the capturing process played itself, a voice call's far end or a remote session's sound, went back to where it came from. It is true now. Test: audio_capture_leaves_out_this_process fails on the old configuration and passes now (macOS 26.5.2, --features screencapturekit); the delegate tests still pass. Co-Authored-By: Claude Opus 5.5 --- src/host/screencapturekit/enumerate.rs | 9 ++++ src/host/screencapturekit/mod.rs | 69 +++++++++++++++++++------- 2 files changed, 59 insertions(+), 19 deletions(-) diff --git a/src/host/screencapturekit/enumerate.rs b/src/host/screencapturekit/enumerate.rs index 0b7de89..63f06fc 100644 --- a/src/host/screencapturekit/enumerate.rs +++ b/src/host/screencapturekit/enumerate.rs @@ -1,3 +1,6 @@ +// Modified by the Kitedesk fork (2026, B283): no display enumeration before macOS 13.0, where a +// weak-linked ScreenCaptureKit may be missing. + use std::vec::IntoIter as VecIntoIter; use cidre::sc; @@ -10,6 +13,12 @@ pub struct Devices(VecIntoIter); impl Devices { pub fn new() -> Result { + if !super::is_supported() { + return Err(BackendSpecificError { + description: "ScreenCaptureKit audio capture needs macOS 13.0 or later".to_string(), + } + .into()); + } let (tx, rx) = std::sync::mpsc::channel(); sc::ShareableContent::current_with_ch(move |sc, e| { let res = if let Some(err) = e { diff --git a/src/host/screencapturekit/mod.rs b/src/host/screencapturekit/mod.rs index 8ce17d7..d110b73 100644 --- a/src/host/screencapturekit/mod.rs +++ b/src/host/screencapturekit/mod.rs @@ -1,3 +1,7 @@ +// Modified by the Kitedesk fork (2026, B283): audio capture only on macOS 13.0 or later, checked +// before any ScreenCaptureKit class is touched so a weak-linked framework is safe on older macOS, +// and the stream leaves out the capturing process's own audio. + use std::{cell::RefCell, rc::Rc, time::Duration}; use crate::{ @@ -10,8 +14,7 @@ use crate::{ use cidre::{ arc::Retained, - cm, define_obj_type, dispatch, ns, - objc::{self, Obj}, + cm, define_obj_type, dispatch, ns, objc, sc::{self, StreamOutput, StreamOutputImpl}, }; pub use enumerate::{ @@ -22,14 +25,12 @@ pub use enumerate::{ pub mod enumerate; mod delegate; -pub fn is_selector_available(cfg: &sc::StreamCfg) -> bool { - for name in [c"setCapturesAudio:", c"setExcludesCurrentProcessAudio:"] { - let sel = unsafe { objc::sel_reg_name(name.as_ptr() as _) }; - if !cfg.responds_to_sel(sel) { - return false; - } - } - true +/// ScreenCaptureKit captures audio from macOS 13.0 (`capturesAudio`, +/// `excludesCurrentProcessAudio`), the framework itself exists from 12.3. Ask the OS version, not +/// the classes: in a binary that weak-links the framework, its classes are nil before 12.3. +/// https://developer.apple.com/documentation/screencapturekit/scstreamconfiguration/capturesaudio +fn is_supported() -> bool { + cidre::api::macos_available("13.0") } #[derive(Debug)] @@ -37,6 +38,9 @@ pub struct Host; impl Host { pub fn new() -> Result { + if !is_supported() { + return Err(crate::HostUnavailable); + } Ok(Host) } } @@ -47,7 +51,7 @@ impl HostTrait for Host { type Device = Device; fn is_available() -> bool { - is_selector_available(&sc::StreamCfg::new()) + is_supported() } fn devices(&self) -> Result { @@ -202,18 +206,13 @@ impl Device { D: FnMut(&Data, &InputCallbackInfo) + Send + 'static, E: FnMut(StreamError) + Send + 'static, { - let queue = dispatch::Queue::serial_with_ar_pool(); - let mut cfg = sc::StreamCfg::new(); // This check should always pass because the caller should check `Host::is_available()` first. // However, we still check it here to be safe. - if !is_selector_available(&cfg) { + if !is_supported() { return Err(BuildStreamError::StreamConfigNotSupported); } - unsafe { - cfg.set_captures_audio(true); - cfg.set_excludes_current_process_audio(false); - cfg.set_shows_cursor(false); - } + let queue = dispatch::Queue::serial_with_ar_pool(); + let cfg = audio_stream_cfg(); let windows = ns::Array::new(); let filter = sc::ContentFilter::with_display_excluding_windows(&self.display, &windows); let error_callback: delegate::ErrorCallback = @@ -430,3 +429,35 @@ fn frames_to_duration(frames: usize, rate: crate::SampleRate) -> std::time::Dura let nanos = ((secsf - secs as f64) * 1_000_000_000.0) as u32; std::time::Duration::new(secs, nanos) } + +/// Audio of the whole display except this process: what the process plays itself (a call's far +/// end, a remote session's sound) would otherwise go back to where it came from. +/// https://developer.apple.com/documentation/screencapturekit/scstreamconfiguration/excludescurrentprocessaudio +fn audio_stream_cfg() -> Retained { + let mut cfg = sc::StreamCfg::new(); + // SAFETY: callers check `is_supported()`, so these macOS 13.0 properties exist. + unsafe { + cfg.set_captures_audio(true); + cfg.set_excludes_current_process_audio(true); + } + cfg.set_shows_cursor(false); + cfg +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn audio_capture_leaves_out_this_process() { + if !is_supported() { + return; + } + let cfg = audio_stream_cfg(); + unsafe { + assert!(cfg.captures_audio()); + assert!(cfg.excludes_current_process_audio()); + } + assert!(!cfg.shows_cursor()); + } +}