Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
improve camera-avfoundation
Brendan Allan60f8416fff43
diff --git a/Cargo.lock b/Cargo.lockindex c15091962..eb0a08758 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -945,7 +945,7 @@ dependencies = [name = "cap-audio"version = "0.1.0"dependencies = [- "cidre",+ "cidre 0.10.1","cpal 0.15.3 (git+https://github.com/RustAudio/cpal?rev=f43d36e55494993bbbde3299af0c53e5cdf4d4cf)","ffmpeg-next","tokio",@@ -955,7 +955,7 @@ dependencies = [name = "cap-camera-avfoundation"version = "0.1.0"dependencies = [- "cidre",+ "cidre 0.10.1","clap 4.5.41","inquire",]@@ -1152,7 +1152,7 @@ dependencies = ["cap-flags","cap-gpu-converters","cap-project",- "cidre",+ "cidre 0.10.1","cocoa 0.26.0","core-foundation 0.10.0","core-graphics 0.24.0",@@ -1207,7 +1207,7 @@ dependencies = ["cap-project","cap-utils","chrono",- "cidre",+ "cidre 0.10.1","cocoa 0.26.0","device_query 2.1.0","either",@@ -1240,7 +1240,7 @@ dependencies = ["cap-flags","cap-project","cap-video-decode",- "cidre",+ "cidre 0.10.1","ffmpeg-hw-device","ffmpeg-next","ffmpeg-sys-next",@@ -1294,7 +1294,7 @@ dependencies = [name = "cap-video-decode"version = "0.1.0"dependencies = [- "cidre",+ "cidre 0.10.1","ffmpeg-hw-device","ffmpeg-next","ffmpeg-sys-next",@@ -1451,6 +1451,15 @@ dependencies = ["tokio",]+[[package]]+name = "cidre"+version = "0.10.1"+source = "git+https://github.com/CapSoftware/cidre?rev=517d097ae438#517d097ae4387ebb97f36c6e133b3c94dca78e25"+dependencies = [+ "cidre-macros",+ "parking_lot",+]+[[package]]name = "cidre-macros"version = "0.2.0"@@ -6708,7 +6717,7 @@ name = "scap"version = "0.0.8"source = "git+https://github.com/CapSoftware/scap?rev=d69dcb2e653a#d69dcb2e653a714bf91d147333deb5fb06acb940"dependencies = [- "cidre",+ "cidre 0.9.2","cocoa 0.25.0","core-graphics-helmer-fork","cpal 0.15.3 (registry+https://github.com/rust-lang/crates.io-index)",diff --git a/Cargo.toml b/Cargo.tomlindex 4770d46ea..97dbdda0e 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -41,7 +41,16 @@ sentry = { version = "0.34.0", features = [] }tracing = "0.1.41"-cidre = "0.9.2"+cidre = { git = "https://github.com/CapSoftware/cidre", rev = "517d097ae438", features = [+ "macos_13_0",+ "cv",+ "cf",+ "sc",+ "av",+ "blocks",+ "async",+ "dispatch",+], default-features = false }windows = "0.58.0"windows-sys = "0.59.0"diff --git a/crates/camera-avfoundation/Cargo.toml b/crates/camera-avfoundation/Cargo.tomlindex 4fc054025..c59f7b897 100644--- a/crates/camera-avfoundation/Cargo.toml+++ b/crates/camera-avfoundation/Cargo.toml@@ -4,7 +4,7 @@ version = "0.1.0"edition = "2024"[dependencies]-cidre = "0.9.2"+cidre = { workspace = true }[lints]workspace = truediff --git a/crates/camera-avfoundation/examples/cli.rs b/crates/camera-avfoundation/examples/cli.rsindex c4b4ffda2..cbbde6087 100644--- a/crates/camera-avfoundation/examples/cli.rs+++ b/crates/camera-avfoundation/examples/cli.rs@@ -1,4 +1,6 @@-use cap_camera_avfoundation::{YCbCrMatrix, list_video_devices};+use cap_camera_avfoundation::{+ CallbackOutputDelegate, CallbackOutputDelegateInner, YCbCrMatrix, list_video_devices,+};use cidre::{av::capture::{VideoDataOutputSampleBufDelegate, VideoDataOutputSampleBufDelegateImpl},cv::pixel_buffer::LockFlags,@@ -81,7 +83,32 @@ pub fn main() {let input = av::capture::DeviceInput::with_device(&selected_device).unwrap();let queue = dispatch::Queue::new();- let delegate = OutputDelegate::new();+ let delegate = CallbackOutputDelegate::with(CallbackOutputDelegateInner::new(Box::new(+ |_output, sample_buf, _connection| {+ let Some(image_buf) = sample_buf.image_buf() else {+ return;+ };++ let total_bytes = if image_buf.plane_count() > 0 {+ (0..image_buf.plane_count())+ .map(|i| image_buf.plane_bytes_per_row(i) * image_buf.plane_height(i))+ .sum::<usize>()+ } else {+ image_buf.plane_bytes_per_row(0) * image_buf.plane_height(0)+ };++ let mut format = image_buf.pixel_format().0.to_be_bytes();+ let format_fourcc = four_cc_to_str(&mut format);++ println!(+ "New frame: {}x{}, {:.2}pts, {total_bytes} bytes, format={format_fourcc}",+ image_buf.width(),+ image_buf.height(),+ sample_buf.pts().value as f64 / sample_buf.pts().scale as f64,+ )+ },+ )));+let mut output = av::capture::VideoDataOutput::new();let mut session = av::capture::Session::new();@@ -146,47 +173,6 @@ impl Display for Format {}}-define_obj_type!(- OutputDelegate + VideoDataOutputSampleBufDelegateImpl,- (),- OUTPUT_DELEGATE-);--impl VideoDataOutputSampleBufDelegate for OutputDelegate {}--#[objc::add_methods]-impl VideoDataOutputSampleBufDelegateImpl for OutputDelegate {- extern "C" fn impl_capture_output_did_output_sample_buf_from_connection(- &mut self,- _cmd: Option<&cidre::objc::Sel>,- _output: &av::CaptureOutput,- sample_buf: &cm::SampleBuf,- _connection: &av::CaptureConnection,- ) {- let Some(image_buf) = sample_buf.image_buf() else {- return;- };-- let total_bytes = if image_buf.plane_count() > 0 {- (0..image_buf.plane_count())- .map(|i| image_buf.plane_bytes_per_row(i) * image_buf.plane_height(i))- .sum::<usize>()- } else {- image_buf.plane_bytes_per_row(0) * image_buf.plane_height(0)- };-- let mut format = image_buf.pixel_format().0.to_be_bytes();- let format_fourcc = four_cc_to_str(&mut format);-- println!(- "New frame: {}x{}, {:.2}pts, {total_bytes} bytes, format={format_fourcc}",- image_buf.width(),- image_buf.height(),- sample_buf.pts().value as f64 / sample_buf.pts().scale as f64,- )- }-}-struct CaptureDeviceSelectOption<'a>(&'a av::CaptureDevice, usize);impl<'a> Display for CaptureDeviceSelectOption<'a> {diff --git a/crates/camera-avfoundation/src/lib.rs b/crates/camera-avfoundation/src/lib.rsindex cb6df39e4..981cf5af4 100644--- a/crates/camera-avfoundation/src/lib.rs+++ b/crates/camera-avfoundation/src/lib.rs@@ -1,13 +1,23 @@-use std::{fmt::Display, str::FromStr};+use std::fmt::Display;-use cidre::*;+use cidre::{+ av::capture::{VideoDataOutputSampleBufDelegate, VideoDataOutputSampleBufDelegateImpl},+ *,+};pub fn list_video_devices() -> arc::R<ns::Array<av::CaptureDevice>> {- let device_types = ns::Array::from_slice(&[+ let mut device_types = vec![av::CaptureDeviceType::built_in_wide_angle_camera(),- av::CaptureDeviceType::external(),av::CaptureDeviceType::desk_view_camera(),- ]);+ ];++ if api::macos_available("14.0") {+ device_types.push(unsafe { av::CaptureDeviceType::external().unwrap() })+ } else {+ device_types.push(av::CaptureDeviceType::external_unknown())+ }++ let device_types = ns::Array::from_slice(&device_types);let video_discovery_session =av::CaptureDeviceDiscoverySession::with_device_types_media_and_pos(@@ -48,3 +58,39 @@ impl TryFrom<&cf::String> for YCbCrMatrix {})}}++pub type OutputDelegateCallback =+ Box<dyn FnMut(&av::CaptureOutput, &cm::SampleBuf, &av::CaptureConnection)>;++pub struct CallbackOutputDelegateInner {+ callback: OutputDelegateCallback,+}++impl CallbackOutputDelegateInner {+ pub fn new(+ callback: Box<dyn FnMut(&av::CaptureOutput, &cm::SampleBuf, &av::CaptureConnection)>,+ ) -> Self {+ Self { callback }+ }+}++define_obj_type!(+ pub CallbackOutputDelegate + VideoDataOutputSampleBufDelegateImpl,+ CallbackOutputDelegateInner,+ OUTPUT_DELEGATE+);++impl VideoDataOutputSampleBufDelegate for CallbackOutputDelegate {}++#[objc::add_methods]+impl VideoDataOutputSampleBufDelegateImpl for CallbackOutputDelegate {+ extern "C" fn impl_capture_output_did_output_sample_buf_from_connection(+ &mut self,+ _cmd: Option<&cidre::objc::Sel>,+ _output: &av::CaptureOutput,+ sample_buf: &cm::SampleBuf,+ _connection: &av::CaptureConnection,+ ) {+ (self.inner_mut().callback)(_output, sample_buf, _connection);+ }+}diff --git a/crates/media/Cargo.toml b/crates/media/Cargo.tomlindex 6a9e1f760..9ab6acb5f 100644--- a/crates/media/Cargo.toml+++ b/crates/media/Cargo.toml@@ -39,19 +39,7 @@ image = { version = "0.25.2", features = ["gif"] }gif = "0.13.1"[target.'cfg(target_os = "macos")'.dependencies]-cidre = { workspace = true, default-features = false, features = [- "private",-- "blocks",- "async",-- "av",- "ca",- "cm",- "dispatch",-- "macos_13_0",-] }+cidre = { workspace = true }cocoa = "0.26.0"core-graphics = "0.24.0"core-foundation = "0.10.0"