current state before improvement pass

This commit is contained in:
2026-05-27 18:56:36 +03:00
parent 5eb27ab779
commit f0775b8ff0
23 changed files with 3563 additions and 1853 deletions
+43 -8
View File
@@ -3,7 +3,7 @@ use std::path::PathBuf;
use std::process::{Child, ChildStdin, Command, Stdio};
use std::time::Instant;
use anyhow::{anyhow, Result};
use anyhow::{anyhow, bail, Context, Result};
use windows_capture::capture::{
CaptureControl, Context as CaptureContext, GraphicsCaptureApiHandler,
};
@@ -114,12 +114,14 @@ impl GraphicsCaptureApiHandler for CaptureHandler {
self.scratch.clear();
let bytes = buf.as_nopadding_buffer(&mut self.scratch);
let expected = (self.width as usize) * (self.height as usize) * 4;
let slice = if bytes.len() >= expected {
&bytes[..expected]
} else {
bytes
};
let _ = stdin.write_all(slice);
if bytes.len() < expected {
tracing::warn!("frame buffer too small: got {} bytes, expected {}", bytes.len(), expected);
return Ok(());
}
stdin.write_all(&bytes[..expected]).map_err(|e| {
tracing::error!("ffmpeg stdin write failed: {e} — encoder may have crashed");
anyhow!("ffmpeg encoder stopped unexpectedly: {e}")
})?;
Ok(())
}
}
@@ -153,6 +155,22 @@ pub fn primary_monitor_dims() -> Result<(u32, u32)> {
Ok((w & !1, h & !1))
}
/// Find the monitor containing the given screen point using the Win32 API.
/// Falls back to the primary monitor if no monitor is found.
fn monitor_at_point(x: i32, y: i32) -> Result<Monitor> {
use windows::Win32::Foundation::POINT;
use windows::Win32::Graphics::Gdi::{MonitorFromPoint, MONITOR_DEFAULTTONEAREST};
let pt = POINT { x, y };
let hmonitor = unsafe { MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST) };
if hmonitor.0.is_null() {
bail!("MonitorFromPoint returned null for ({x}, {y})");
}
// SAFETY: hmonitor is a valid monitor handle from MonitorFromPoint.
let monitor = Monitor::from_raw_hmonitor(hmonitor.0 as *mut _);
Ok(monitor)
}
pub fn start(
output_path: PathBuf,
ffmpeg_path: PathBuf,
@@ -161,7 +179,19 @@ pub fn start(
crop: Option<CaptureRect>,
fps: u32,
) -> Result<CaptureSession> {
let monitor = Monitor::primary().map_err(|e| anyhow!("primary monitor: {e:?}"))?;
// Use the center of the capture region to determine which monitor to capture.
// If the region spans monitors, we pick the one containing the center.
let monitor = if let Some(ref c) = crop {
let cx = c.x.saturating_add(c.width as i32 / 2);
let cy = c.y.saturating_add(c.height as i32 / 2);
tracing::info!("capture center=({cx}, {cy}), selecting monitor");
monitor_at_point(cx, cy).unwrap_or_else(|_| {
tracing::warn!("no monitor at ({cx}, {cy}), falling back to primary");
Monitor::primary().expect("primary monitor always available")
})
} else {
Monitor::primary().expect("primary monitor always available")
};
let raw_width = monitor
.width()
.map_err(|e| anyhow!("monitor width: {e:?}"))?;
@@ -238,6 +268,11 @@ impl CaptureSession {
self.control
.stop()
.map_err(|e| anyhow!("capture stop: {e}"))?;
let meta = std::fs::metadata(&self.output_path)
.with_context(|| format!("checking output file {}", self.output_path.display()))?;
if meta.len() == 0 {
bail!("capture output file is empty — ffmpeg may have crashed during recording");
}
Ok(self.output_path)
}
}