Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(camera-effects): keep a missing DirectML from crashing the next blur session (#3161)

Richie McIlroy39af8d5dc5de
diff --git a/crates/camera-effects/src/segmentation.rs b/crates/camera-effects/src/segmentation.rs
index c84023bc2..d8aae6500 100644
--- a/crates/camera-effects/src/segmentation.rs
+++ b/crates/camera-effects/src/segmentation.rs
@@ -359,6 +359,19 @@ fn try_register_coreml(
fn try_register_directml(
builder: ort::session::builder::SessionBuilder,
) -> ort::session::builder::SessionBuilder {
+ // ort 2.0.0-rc.12 caches the DirectML API in a `OnceLock` that, when the
+ // lookup fails, is still marked initialized with nothing stored. Every
+ // later registration in the process then calls through a null pointer, so
+ // the second blur session (the next export, or the editor reopening) ended
+ // the process wherever ONNX Runtime has no DirectML. Ask the runtime once,
+ // ourselves, and only hand registration to ort when DirectML is there.
+ static DIRECTML: std::sync::OnceLock<Result<(), String>> = std::sync::OnceLock::new();
+ if let Err(reason) = DIRECTML.get_or_init(directml_available) {
+ tracing::warn!(
+ "Camera background blur: DirectML EP registration failed, using CPU: {reason}"
+ );
+ return builder;
+ }
match builder.with_execution_providers([
ort::execution_providers::DirectMLExecutionProvider::default()
.build()
@@ -377,6 +390,35 @@ fn try_register_directml(
}
}
+/// Whether the loaded ONNX Runtime provides the DirectML execution provider,
+/// asked without going through ort's cache of the answer.
+#[cfg(target_os = "windows")]
+fn directml_available() -> Result<(), String> {
+ let api = ort::api();
+ let mut provider = std::ptr::null();
+ let status = unsafe { (api.GetExecutionProviderApi)(c"DML".as_ptr(), 0, &mut provider) };
+ if status.0.is_null() {
+ return if provider.is_null() {
+ Err("ONNX Runtime returned no DirectML API".into())
+ } else {
+ Ok(())
+ };
+ }
+ let message = unsafe {
+ let message = (api.GetErrorMessage)(status.0);
+ let message = if message.is_null() {
+ "unknown error".to_string()
+ } else {
+ std::ffi::CStr::from_ptr(message)
+ .to_string_lossy()
+ .into_owned()
+ };
+ (api.ReleaseStatus)(status.0);
+ message
+ };
+ Err(message)
+}
+
#[cfg(test)]
mod tests {
use super::{MODEL_CHANNEL_SIZE, populate_rgb_planes};
diff --git a/crates/verify/tests/export.rs b/crates/verify/tests/export.rs
index a4080e834..a06af620f 100644
--- a/crates/verify/tests/export.rs
+++ b/crates/verify/tests/export.rs
@@ -1493,3 +1493,75 @@ fn recorded_epoch_audio_head() {
}
common::assert_passed(failures);
}
+
+fn camera_blur_and_captions(config: &mut ProjectConfiguration) {
+ config.camera.background_blur.mode = cap_project::BackgroundBlurMode::Light;
+ let segments: Vec<cap_project::CaptionTrackSegment> = (0..12)
+ .map(|i| {
+ serde_json::from_value(serde_json::json!({
+ "id": format!("caption-{i}"),
+ "start": f64::from(i) * 0.6,
+ "end": f64::from(i) * 0.6 + 0.5,
+ "text": format!("Caption number {i} on the timeline"),
+ }))
+ .unwrap()
+ })
+ .collect();
+ config.captions = Some(cap_project::CaptionsData {
+ segments: segments
+ .iter()
+ .map(|segment| cap_project::CaptionSegment {
+ id: segment.id.clone(),
+ start: segment.start as f32,
+ end: segment.end as f32,
+ text: segment.text.clone(),
+ words: Vec::new(),
+ })
+ .collect(),
+ settings: cap_project::CaptionSettings {
+ enabled: true,
+ ..Default::default()
+ },
+ ..Default::default()
+ });
+ config
+ .timeline
+ .as_mut()
+ .expect("verification projects have a timeline")
+ .caption_segments = segments;
+}
+
+/// Every export with camera background blur starts a segmentation session.
+/// Where ONNX Runtime has no DirectML, the second one in a process used to end
+/// it (a null call inside ort's DirectML registration), so a second export, or
+/// an export after the editor preview, crashed on Windows.
+#[test]
+fn camera_blur_exports_twice_in_one_process() {
+ if !report::selected("export/multi-segment/camera-blur-twice") {
+ return;
+ }
+ let project = common::project(&catalog::multi_segment(report::tier()));
+ let mut config = project.config();
+ camera_blur_and_captions(&mut config);
+ let rt = common::runtime();
+ let dir = common::scratch_dir("export/multi-segment/camera-blur-twice");
+ for attempt in 0..2 {
+ let run = ExportRun {
+ custom_bpp: None,
+ fps: 30,
+ resolution: (1920, 1080),
+ format: Format::Mp4,
+ decoder: Decoder::Default,
+ };
+ let path = dir.path().join(format!("attempt-{attempt}.mp4"));
+ let output = rt
+ .block_on(common::export(&project, config.clone(), &run, &path))
+ .unwrap_or_else(|error| panic!("export {attempt} failed: {error}"));
+ let frames = common::read_output(&output, None);
+ assert!(
+ frames.len() > 30,
+ "export {attempt} wrote {} frames",
+ frames.len()
+ );
+ }
+}