Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
Improve macOS screen capture error handling
Richie McIlroyf42b27763220
diff --git a/apps/desktop/src-tauri/src/platform/macos/sc_shareable_content.rs b/apps/desktop/src-tauri/src/platform/macos/sc_shareable_content.rsindex 01dba7c59..03ff5b95b 100644--- a/apps/desktop/src-tauri/src/platform/macos/sc_shareable_content.rs+++ b/apps/desktop/src-tauri/src/platform/macos/sc_shareable_content.rs@@ -24,7 +24,17 @@ fn state() -> &'static CacheState {}pub async fn prewarm_shareable_content() -> Result<(), arc::R<ns::Error>> {- if state().cache.read().unwrap().is_some() {+ prewarm_shareable_content_inner(false).await+}++pub async fn refresh_shareable_content() -> Result<(), arc::R<ns::Error>> {+ prewarm_shareable_content_inner(true).await+}++async fn prewarm_shareable_content_inner(force_refresh: bool) -> Result<(), arc::R<ns::Error>> {+ if force_refresh {+ state().cache.write().unwrap().take();+ } else if state().cache.read().unwrap().is_some() {trace!("ScreenCaptureKit shareable content already warmed");return Ok(());}@@ -161,7 +171,11 @@ impl ScreenCapturePrewarmer {}let warm_start = std::time::Instant::now();- let result = crate::platform::prewarm_shareable_content().await;+ let result = if force {+ crate::platform::refresh_shareable_content().await+ } else {+ crate::platform::prewarm_shareable_content().await+ };let mut state = self.state.lock().await;match result {diff --git a/apps/desktop/src-tauri/src/recording.rs b/apps/desktop/src-tauri/src/recording.rsindex e999788c2..3cf8f4854 100644--- a/apps/desktop/src-tauri/src/recording.rs+++ b/apps/desktop/src-tauri/src/recording.rs@@ -9,6 +9,8 @@ use cap_project::{cursor::CursorEvents,};use cap_recording::feeds::camera::CameraFeedLock;+#[cfg(target_os = "macos")]+use cap_recording::sources::screen_capture::SourceError;use cap_recording::{RecordingMode,feeds::{camera, microphone},@@ -99,6 +101,58 @@ unsafe impl Send for SendableShareableContent {}#[cfg(target_os = "macos")]unsafe impl Sync for SendableShareableContent {}+#[cfg(target_os = "macos")]+async fn acquire_shareable_content_for_target(+ capture_target: &ScreenCaptureTarget,+) -> anyhow::Result<SendableShareableContent> {+ let mut refreshed = false;++ loop {+ let shareable_content = SendableShareableContent(+ crate::platform::get_shareable_content()+ .await+ .map_err(|e| anyhow!(format!("GetShareableContent: {e}")))?+ .ok_or_else(|| anyhow!("GetShareableContent/NotAvailable"))?,+ );++ if !shareable_content_missing_target_display(capture_target, shareable_content.retained())+ .await+ {+ return Ok(shareable_content);+ }++ if refreshed {+ return Err(anyhow!("GetShareableContent/DisplayMissing"));+ }++ crate::platform::refresh_shareable_content()+ .await+ .map_err(|e| anyhow!(format!("RefreshShareableContent: {e}")))?;+ refreshed = true;+ }+}++#[cfg(target_os = "macos")]+async fn shareable_content_missing_target_display(+ capture_target: &ScreenCaptureTarget,+ shareable_content: cidre::arc::R<cidre::sc::ShareableContent>,+) -> bool {+ match capture_target.display() {+ Some(display) => display+ .raw_handle()+ .as_sc(shareable_content)+ .await+ .is_none(),+ None => false,+ }+}++#[cfg(target_os = "macos")]+fn is_shareable_content_error(err: &anyhow::Error) -> bool {+ err.downcast_ref::<SourceError>()+ .is_some_and(|source_error| matches!(source_error, SourceError::AsContentFilter))+}+impl InProgressRecording {pub fn capture_target(&self) -> &ScreenCaptureTarget {match self {@@ -486,12 +540,8 @@ pub async fn start_recording(};#[cfg(target_os = "macos")]- let shareable_content = SendableShareableContent(- crate::platform::get_shareable_content()- .await- .map_err(|e| anyhow!(format!("GetShareableContent: {e}")))?- .ok_or_else(|| anyhow!("GetShareableContent/NotAvailable"))?,- );+ let mut shareable_content =+ acquire_shareable_content_for_target(&inputs.capture_target).await?;let common = InProgressRecordingCommon {target_name,@@ -626,6 +676,12 @@ pub async fn start_recording(state.set_current_recording(actor);break done_fut;}+ #[cfg(target_os = "macos")]+ Err(err) if is_shareable_content_error(&err) => {+ shareable_content =+ acquire_shareable_content_for_target(&inputs.capture_target).await?;+ continue;+ }Err(err) if mic_restart_attempts == 0 && mic_actor_not_running(&err) => {mic_restart_attempts += 1;statediff --git a/crates/recording/src/sources/screen_capture/macos.rs b/crates/recording/src/sources/screen_capture/macos.rsindex 0cbab83e4..2f0566a94 100644--- a/crates/recording/src/sources/screen_capture/macos.rs+++ b/crates/recording/src/sources/screen_capture/macos.rs@@ -45,7 +45,7 @@ impl ScreenCaptureFormat for CMSampleBufferCapture {}#[derive(Debug, thiserror::Error)]-enum SourceError {+pub enum SourceError {#[error("NoDisplay: Id '{0}'")]NoDisplay(DisplayId),#[error("AsContentFilter")]