From f5b0c718dea7ce747e7a6d6c8207a0bb58cfa2a3 Mon Sep 17 00:00:00 2001 From: fufesou Date: Fri, 22 Nov 2024 21:09:54 +0800 Subject: [PATCH] feat: macos, screencapturekit 1. Add new feature `screencapturekit`. 2. Remove `unwrap()` in mod `screencapturekit`. No all `unwrap()` in this crate are removed. 3. Add selector check before calling it. Signed-off-by: fufesou --- Cargo.toml | 11 +++++- src/host/mod.rs | 2 +- src/host/screencapturekit/enumerate.rs | 7 ++-- src/host/screencapturekit/mod.rs | 47 +++++++++++++++++++------- src/platform/mod.rs | 4 +-- 5 files changed, 52 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 71c16c2..8870955 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ rust-version = "1.70" [features] asio = ["asio-sys", "num-traits"] # Only available on Windows. See README for setup instructions. oboe-shared-stdcxx = ["oboe/shared-stdcxx"] # Only available on Android. See README for what it does. +screencapturekit = ["cidre"] [dependencies] dasp_sample = "0.11" @@ -53,7 +54,15 @@ mach2 = "0.4" # For access to mach_timebase type. [target.'cfg(target_os = "macos")'.dependencies] coreaudio-rs = { version = "0.11", default-features = false, features = ["audio_unit", "core_audio"] } -cidre = { git = "https://github.com/yury/cidre.git", rev = "f05c428" } +cidre = { git = "https://github.com/yury/cidre.git", rev = "f05c428", features = [ + "blocks", + "async", + "dispatch", + "av", + "sc", + "macos_12_3", +], default-features = false, optional = true } + [target.'cfg(target_os = "ios")'.dependencies] coreaudio-rs = { version = "0.11", default-features = false, features = ["audio_unit", "core_audio", "audio_toolbox"] } diff --git a/src/host/mod.rs b/src/host/mod.rs index 8b4c17c..3302edb 100644 --- a/src/host/mod.rs +++ b/src/host/mod.rs @@ -24,7 +24,7 @@ pub(crate) mod jack; pub(crate) mod null; #[cfg(target_os = "android")] pub(crate) mod oboe; -#[cfg(target_os = "macos")] +#[cfg(all(target_os = "macos", feature = "screencapturekit"))] pub(crate) mod screencapturekit; #[cfg(windows)] pub(crate) mod wasapi; diff --git a/src/host/screencapturekit/enumerate.rs b/src/host/screencapturekit/enumerate.rs index 09154af..0b7de89 100644 --- a/src/host/screencapturekit/enumerate.rs +++ b/src/host/screencapturekit/enumerate.rs @@ -23,9 +23,12 @@ impl Devices { description: "Failed to get current shareable content".to_string(), }) }; - tx.send(res).unwrap(); + // Can't be failed + tx.send(res).ok(); }); - let sc_shareable_content = rx.recv().unwrap()?; + let sc_shareable_content = rx.recv().map_err(|e| BackendSpecificError { + description: format!("Failed to receive shareable content result: {e}"), + })??; let mut res = Vec::new(); for display in sc_shareable_content.displays().iter() { diff --git a/src/host/screencapturekit/mod.rs b/src/host/screencapturekit/mod.rs index 5e45543..6a53b57 100644 --- a/src/host/screencapturekit/mod.rs +++ b/src/host/screencapturekit/mod.rs @@ -10,7 +10,8 @@ use crate::{ use cidre::{ arc::Retained, - cm, define_obj_type, dispatch, ns, objc, + cm, define_obj_type, dispatch, ns, + objc::{self, Obj}, sc::{self, StreamOutput, StreamOutputImpl}, }; pub use enumerate::{ @@ -35,8 +36,16 @@ impl HostTrait for Host { type Device = Device; fn is_available() -> bool { - // Assume screencapturekit is always available - true + unsafe { + let cfg = sc::StreamCfg::new(); + for name in [c"setCapturesAudio:", c"setExcludesCurrentProcessAudio:"] { + let sel = objc::sel_reg_name(name.as_ptr() as _); + if !cfg.responds_to_sel(sel) { + return false; + } + } + true + } } fn devices(&self) -> Result { @@ -185,8 +194,10 @@ impl Device { { let queue = dispatch::Queue::serial_with_ar_pool(); let mut cfg = sc::StreamCfg::new(); - cfg.set_captures_audio(true); - cfg.set_excludes_current_process_audio(false); + unsafe { + cfg.set_captures_audio(true); + cfg.set_excludes_current_process_audio(false); + } let windows = ns::Array::new(); let filter = sc::ContentFilter::with_display_excluding_windows(&self.display, &windows); let sc_stream = sc::Stream::new(&filter, &cfg); @@ -260,9 +271,12 @@ impl StreamTrait for Stream { } else { Result::Ok(()) }; - tx.send(res).unwrap(); + // Can't be failed + tx.send(res).ok(); }); - rx.recv().unwrap()?; + rx.recv().map_err(|e| BackendSpecificError { + description: format!("Failed to receive stream play result: {e}"), + })??; stream.playing = true; } Ok(()) @@ -280,9 +294,12 @@ impl StreamTrait for Stream { } else { Result::Ok(()) }; - tx.send(res).unwrap(); + // Can't be failed + tx.send(res).ok(); }); - rx.recv().unwrap()?; + rx.recv().map_err(|e| BackendSpecificError { + description: format!("Failed to receive stream pause result: {e}"), + })??; stream.playing = false; } Ok(()) @@ -343,10 +360,14 @@ impl CapturerInner { let capture = host_time_to_stream_instant(sample_buf.pts()); let duration = frames_to_duration(buf_len, self.config.sample_rate); let elapsed = start.elapsed(); - let callback = capture.add(duration).unwrap().add(elapsed).unwrap(); - let timestamp = crate::InputStreamTimestamp { callback, capture }; - let info = InputCallbackInfo { timestamp }; - (self.data_callback)(&data, &info); + if let Some(callback) = capture + .add(duration) + .and_then(|capture| capture.add(elapsed)) + { + let timestamp = crate::InputStreamTimestamp { callback, capture }; + let info = InputCallbackInfo { timestamp }; + (self.data_callback)(&data, &info); + } } } diff --git a/src/platform/mod.rs b/src/platform/mod.rs index e32af0b..892b858 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -625,7 +625,7 @@ mod platform_impl { Stream as CoreAudioStream, SupportedInputConfigs as CoreAudioSupportedInputConfigs, SupportedOutputConfigs as CoreAudioSupportedOutputConfigs, }; - #[cfg(any(target_os = "macos"))] + #[cfg(all(target_os = "macos", feature = "screencapturekit"))] pub use crate::host::screencapturekit::{ Device as ScreenCaptureKitDevice, Devices as ScreenCaptureKitDevices, Host as ScreenCaptureKitHost, Stream as ScreenCaptureKitStream, @@ -633,7 +633,7 @@ mod platform_impl { SupportedOutputConfigs as ScreenCaptureKitSupportedOutputConfigs, }; - impl_platform_host!(CoreAudio coreaudio "CoreAudio", #[cfg(any(target_os = "macos"))] ScreenCaptureKit screencapturekit "ScreenCaptureKit"); + impl_platform_host!(CoreAudio coreaudio "CoreAudio", #[cfg(all(target_os = "macos", feature = "screencapturekit"))] ScreenCaptureKit screencapturekit "ScreenCaptureKit"); /// The default host for the current compilation target platform. pub fn default_host() -> Host {