Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(scap-direct3d): handle optional COM results without unwrap

Richie McIlroyf9a7da267717
diff --git a/crates/scap-direct3d/src/lib.rs b/crates/scap-direct3d/src/lib.rs
index 2c313a773..dbe425667 100644
--- a/crates/scap-direct3d/src/lib.rs
+++ b/crates/scap-direct3d/src/lib.rs
@@ -180,7 +180,10 @@ impl StagingTexturePool {
width: u32,
height: u32,
) -> windows::core::Result<ID3D11Texture2D> {
- let mut textures = self.textures.lock().unwrap();
+ let mut textures = self
+ .textures
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner());
let index = self.next_index.fetch_add(1, Ordering::Relaxed) % STAGING_POOL_SIZE;
@@ -213,7 +216,7 @@ impl StagingTexturePool {
.CreateTexture2D(&texture_desc, None, Some(&mut texture))?;
};
- let texture = texture.unwrap();
+ let texture = texture.ok_or_else(windows::core::Error::from_win32)?;
if index < textures.len() {
textures[index] = PooledStagingTexture {
@@ -301,7 +304,8 @@ fn create_d3d_device_with_warp_fallback() -> windows::core::Result<(ID3D11Device
adapter = %description,
"scap-direct3d: D3D11 device created on pinned hardware adapter"
);
- return Ok((device.unwrap(), false));
+ let device = device.ok_or_else(windows::core::Error::from_win32)?;
+ return Ok((device, false));
}
Err(e) => {
tracing::warn!(
@@ -314,11 +318,11 @@ fn create_d3d_device_with_warp_fallback() -> windows::core::Result<(ID3D11Device
let result = create_d3d_device_with_type(D3D_DRIVER_TYPE_HARDWARE, flags, &mut device);
match result {
- Ok(()) => Ok((device.unwrap(), false)),
+ Ok(()) => Ok((device.ok_or_else(windows::core::Error::from_win32)?, false)),
Err(e) if e.code() == DXGI_ERROR_UNSUPPORTED => {
tracing::info!("Hardware D3D11 device unavailable, attempting WARP fallback");
create_d3d_device_with_type(D3D_DRIVER_TYPE_WARP, flags, &mut device)?;
- Ok((device.unwrap(), true))
+ Ok((device.ok_or_else(windows::core::Error::from_win32)?, true))
}
Err(e) => Err(e),
}
@@ -515,19 +519,21 @@ impl Capturer {
.map_err(NewCapturerError::CaptureSession)?;
if let Some(border_required) = settings.is_border_required {
- session.SetIsBorderRequired(border_required).unwrap();
+ session
+ .SetIsBorderRequired(border_required)
+ .map_err(NewCapturerError::Other)?;
}
if let Some(cursor_capture_enabled) = settings.is_cursor_capture_enabled {
session
.SetIsCursorCaptureEnabled(cursor_capture_enabled)
- .unwrap();
+ .map_err(NewCapturerError::Other)?;
}
if let Some(min_update_interval) = settings.min_update_interval {
session
.SetMinUpdateInterval(min_update_interval.into())
- .unwrap();
+ .map_err(NewCapturerError::Other)?;
}
let crop_data = settings
@@ -553,7 +559,11 @@ impl Capturer {
unsafe { d3d_device.CreateTexture2D(&desc, None, Some(&mut texture)) }
.map_err(NewCapturerError::CropTexture)?;
- Ok::<_, NewCapturerError>((texture.unwrap(), crop))
+ let texture = texture.ok_or_else(|| {
+ NewCapturerError::CropTexture(windows::core::Error::from_win32())
+ })?;
+
+ Ok::<_, NewCapturerError>((texture, crop))
})
.transpose()?;
@@ -573,10 +583,11 @@ impl Capturer {
return Ok(());
}
- let frame = frame_pool
- .as_ref()
- .expect("FrameArrived parameter was None")
- .TryGetNextFrame()?;
+ let Some(frame_pool) = frame_pool.as_ref() else {
+ return Ok(());
+ };
+
+ let frame = frame_pool.TryGetNextFrame()?;
let size = frame.ContentSize()?;