hex/crates/capture-core/src/error.rs
Kit Langton bb2f56263c
feat(capture-core): shared macOS screen-recording library (#255)
* feat(capture-core): add shared macOS screen capture core

Records displays and windows with ScreenCaptureKit, one MP4 per source,
on one host clock, and writes a versioned capture.json manifest.

- stop() returns at once; finishing runs on the recording's own thread
- each stream keeps running until its detached recording output finishes
  or fails, then stops within 10 s
- a system stream stop is not an output failure; a finished MP4 is kept
- the final video frame is held to the recorded end
- external microphone PCM through a bounded sink, written as a WAV with
  recorded gaps; the core never opens a microphone in that mode
- host input through a bounded queue or an optional event tap, chord-only
  keys, dropped while secure input is on; ~240 Hz cursor sampling
- optional system audio file and low-rate RGBA frame tap

Kept outside the workspace and built only by the capture feature.

* feat(capture-core): regions, output size, screenshots, nanosecond clock

- Source::Region { display, rect }: a display-local rect applied as the
  stream's sourceRect, trimmed to the display, with exclusions
- SourcePlan::long_edge caps a source's longest side in pixels, keeping
  the aspect and never upscaling; sizes come from points and the
  filter's pixel scale
- screenshot(&Source, &Exclusions, long_edge) through SCScreenshotManager
  with the recording's filter construction
- every timestamp is Mach host time in nanoseconds: the manifest records
  "clock": "mach_host_ns" and anchor_ns; stream starts, audio file starts
  and gaps, cursor samples and clicks, and input events carry *_ns
- objc2-av-foundation and objc2-avf-audio use only the features in use, so
  the lockfile gains no registry crates
2026-10-27 20:08:40 -07:00

93 lines
3.9 KiB
Rust

use std::fmt;
use crate::plan::{Rect, Source};
/// Why a recording could not start, or how a source failed to finish.
#[derive(Debug)]
pub enum Error {
/// `ScreenCaptureKit` could not list shareable content (usually missing permission).
ShareableContent(String),
/// A source in the plan is not on screen.
SourceUnavailable(Source),
InvalidDimensions,
/// A region with no area on its display.
InvalidRegion(Rect),
InvalidFrameRate,
NoSources,
DuplicateSources,
/// File names must be plain, distinct names inside the bundle directory.
InvalidNames(String),
/// Window exclusions can be combined with whole-app exclusions only when the windows belong to
/// those apps: `ScreenCaptureKit` filters a display by windows or by apps, not both.
Exclusion(String),
MicrophoneUnavailable(String),
Output(std::io::Error),
CreateRecordingOutput,
ConfigureRecording(String),
ConfigureStream(String),
Start(String),
/// The recording output reported a failure.
Recording(String),
/// The system stopped the stream. Distinct from [`Error::Recording`]: the output may still
/// finish a complete file.
StreamStopped(String),
Stop(String),
FinishTimeout,
/// `SCScreenshotManager` could not take or read a screenshot.
Screenshot(String),
/// The finishing thread ended without a result.
FinisherLost,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ShareableContent(error) => write!(f, "screen capture access failed: {error}"),
Self::SourceUnavailable(source) => {
write!(f, "capture source is no longer available: {source:?}")
}
Self::InvalidDimensions => f.write_str("capture source has invalid dimensions"),
Self::InvalidRegion(rect) => {
write!(f, "capture region is not on its display: {rect:?}")
}
Self::InvalidFrameRate => f.write_str("frame rate must be greater than zero"),
Self::NoSources => f.write_str("at least one capture source is required"),
Self::DuplicateSources => f.write_str("capture sources must be unique"),
Self::InvalidNames(error) => write!(f, "invalid file names: {error}"),
Self::Exclusion(error) => write!(f, "cannot build the exclusion filter: {error}"),
Self::MicrophoneUnavailable(id) => write!(f, "microphone is not available: {id}"),
Self::Output(error) => write!(f, "failed to prepare recording output: {error}"),
Self::CreateRecordingOutput => {
f.write_str("failed to create a ScreenCaptureKit recording output")
}
Self::ConfigureRecording(error) => {
write!(f, "failed to configure recording output: {error}")
}
Self::ConfigureStream(error) => {
write!(f, "failed to configure capture stream: {error}")
}
Self::Start(error) => write!(f, "failed to start recording: {error}"),
Self::Recording(error) => write!(f, "recording failed: {error}"),
Self::StreamStopped(error) => write!(f, "capture stream stopped: {error}"),
Self::Stop(error) => write!(f, "failed to stop recording: {error}"),
Self::FinishTimeout => f.write_str("timed out waiting for recording finalization"),
Self::Screenshot(error) => write!(f, "screenshot failed: {error}"),
Self::FinisherLost => f.write_str("the finishing thread ended without a result"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Output(error) => Some(error),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Self::Output(error)
}
}