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 <linlong1266@gmail.com>
This commit is contained in:
fufesou 2024-11-22 21:09:54 +08:00
parent e851e41b6e
commit f5b0c718de
5 changed files with 52 additions and 19 deletions

View file

@ -12,6 +12,7 @@ rust-version = "1.70"
[features] [features]
asio = ["asio-sys", "num-traits"] # Only available on Windows. See README for setup instructions. 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. oboe-shared-stdcxx = ["oboe/shared-stdcxx"] # Only available on Android. See README for what it does.
screencapturekit = ["cidre"]
[dependencies] [dependencies]
dasp_sample = "0.11" dasp_sample = "0.11"
@ -53,7 +54,15 @@ mach2 = "0.4" # For access to mach_timebase type.
[target.'cfg(target_os = "macos")'.dependencies] [target.'cfg(target_os = "macos")'.dependencies]
coreaudio-rs = { version = "0.11", default-features = false, features = ["audio_unit", "core_audio"] } 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] [target.'cfg(target_os = "ios")'.dependencies]
coreaudio-rs = { version = "0.11", default-features = false, features = ["audio_unit", "core_audio", "audio_toolbox"] } coreaudio-rs = { version = "0.11", default-features = false, features = ["audio_unit", "core_audio", "audio_toolbox"] }

View file

@ -24,7 +24,7 @@ pub(crate) mod jack;
pub(crate) mod null; pub(crate) mod null;
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
pub(crate) mod oboe; pub(crate) mod oboe;
#[cfg(target_os = "macos")] #[cfg(all(target_os = "macos", feature = "screencapturekit"))]
pub(crate) mod screencapturekit; pub(crate) mod screencapturekit;
#[cfg(windows)] #[cfg(windows)]
pub(crate) mod wasapi; pub(crate) mod wasapi;

View file

@ -23,9 +23,12 @@ impl Devices {
description: "Failed to get current shareable content".to_string(), 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(); let mut res = Vec::new();
for display in sc_shareable_content.displays().iter() { for display in sc_shareable_content.displays().iter() {

View file

@ -10,7 +10,8 @@ use crate::{
use cidre::{ use cidre::{
arc::Retained, arc::Retained,
cm, define_obj_type, dispatch, ns, objc, cm, define_obj_type, dispatch, ns,
objc::{self, Obj},
sc::{self, StreamOutput, StreamOutputImpl}, sc::{self, StreamOutput, StreamOutputImpl},
}; };
pub use enumerate::{ pub use enumerate::{
@ -35,8 +36,16 @@ impl HostTrait for Host {
type Device = Device; type Device = Device;
fn is_available() -> bool { fn is_available() -> bool {
// Assume screencapturekit is always available unsafe {
true 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<Self::Devices, DevicesError> { fn devices(&self) -> Result<Self::Devices, DevicesError> {
@ -185,8 +194,10 @@ impl Device {
{ {
let queue = dispatch::Queue::serial_with_ar_pool(); let queue = dispatch::Queue::serial_with_ar_pool();
let mut cfg = sc::StreamCfg::new(); let mut cfg = sc::StreamCfg::new();
cfg.set_captures_audio(true); unsafe {
cfg.set_excludes_current_process_audio(false); cfg.set_captures_audio(true);
cfg.set_excludes_current_process_audio(false);
}
let windows = ns::Array::new(); let windows = ns::Array::new();
let filter = sc::ContentFilter::with_display_excluding_windows(&self.display, &windows); let filter = sc::ContentFilter::with_display_excluding_windows(&self.display, &windows);
let sc_stream = sc::Stream::new(&filter, &cfg); let sc_stream = sc::Stream::new(&filter, &cfg);
@ -260,9 +271,12 @@ impl StreamTrait for Stream {
} else { } else {
Result::Ok(()) 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; stream.playing = true;
} }
Ok(()) Ok(())
@ -280,9 +294,12 @@ impl StreamTrait for Stream {
} else { } else {
Result::Ok(()) 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; stream.playing = false;
} }
Ok(()) Ok(())
@ -343,10 +360,14 @@ impl CapturerInner {
let capture = host_time_to_stream_instant(sample_buf.pts()); let capture = host_time_to_stream_instant(sample_buf.pts());
let duration = frames_to_duration(buf_len, self.config.sample_rate); let duration = frames_to_duration(buf_len, self.config.sample_rate);
let elapsed = start.elapsed(); let elapsed = start.elapsed();
let callback = capture.add(duration).unwrap().add(elapsed).unwrap(); if let Some(callback) = capture
let timestamp = crate::InputStreamTimestamp { callback, capture }; .add(duration)
let info = InputCallbackInfo { timestamp }; .and_then(|capture| capture.add(elapsed))
(self.data_callback)(&data, &info); {
let timestamp = crate::InputStreamTimestamp { callback, capture };
let info = InputCallbackInfo { timestamp };
(self.data_callback)(&data, &info);
}
} }
} }

View file

@ -625,7 +625,7 @@ mod platform_impl {
Stream as CoreAudioStream, SupportedInputConfigs as CoreAudioSupportedInputConfigs, Stream as CoreAudioStream, SupportedInputConfigs as CoreAudioSupportedInputConfigs,
SupportedOutputConfigs as CoreAudioSupportedOutputConfigs, SupportedOutputConfigs as CoreAudioSupportedOutputConfigs,
}; };
#[cfg(any(target_os = "macos"))] #[cfg(all(target_os = "macos", feature = "screencapturekit"))]
pub use crate::host::screencapturekit::{ pub use crate::host::screencapturekit::{
Device as ScreenCaptureKitDevice, Devices as ScreenCaptureKitDevices, Device as ScreenCaptureKitDevice, Devices as ScreenCaptureKitDevices,
Host as ScreenCaptureKitHost, Stream as ScreenCaptureKitStream, Host as ScreenCaptureKitHost, Stream as ScreenCaptureKitStream,
@ -633,7 +633,7 @@ mod platform_impl {
SupportedOutputConfigs as ScreenCaptureKitSupportedOutputConfigs, 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. /// The default host for the current compilation target platform.
pub fn default_host() -> Host { pub fn default_host() -> Host {