mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* 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
103 lines
3.6 KiB
Rust
103 lines
3.6 KiB
Rust
//! Exact track timing of a media file, read through `AVFoundation`.
|
|
|
|
use std::path::Path;
|
|
|
|
use objc2::runtime::AnyObject;
|
|
use objc2_av_foundation::{
|
|
AVMediaTypeAudio, AVMediaTypeVideo, AVURLAsset, AVURLAssetPreferPreciseDurationAndTimingKey,
|
|
};
|
|
use objc2_core_media::{CMTime, CMTimeFlags};
|
|
use objc2_foundation::{NSDictionary, NSNumber, NSString, NSURL};
|
|
|
|
use crate::time::MediaTime;
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub(crate) enum TrackKind {
|
|
Video,
|
|
Audio,
|
|
Other,
|
|
}
|
|
|
|
/// One track's span on the file's timeline.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub(crate) struct Track {
|
|
pub kind: TrackKind,
|
|
pub start: MediaTime,
|
|
pub duration: MediaTime,
|
|
}
|
|
|
|
impl Track {
|
|
pub(crate) fn end(self) -> Option<MediaTime> {
|
|
self.start.checked_add(self.duration)
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub(crate) struct Probe {
|
|
pub tracks: Vec<Track>,
|
|
}
|
|
|
|
impl Probe {
|
|
fn first(&self, kind: TrackKind) -> Option<Track> {
|
|
self.tracks.iter().copied().find(|track| track.kind == kind)
|
|
}
|
|
|
|
pub(crate) fn video_end(&self) -> Option<MediaTime> {
|
|
self.first(TrackKind::Video).and_then(Track::end)
|
|
}
|
|
|
|
pub(crate) fn audio(&self) -> Option<Track> {
|
|
self.first(TrackKind::Audio)
|
|
}
|
|
}
|
|
|
|
/// Reads every track's exact time range, asking `AVFoundation` for precise timing.
|
|
pub(crate) fn probe(path: &Path) -> Result<Probe, String> {
|
|
if !path.exists() {
|
|
return Err(format!("{} does not exist", path.display()));
|
|
}
|
|
let path = path
|
|
.to_str()
|
|
.ok_or_else(|| format!("{} is not valid UTF-8", path.display()))?;
|
|
let url = NSURL::fileURLWithPath(&NSString::from_str(path));
|
|
// SAFETY: Framework constants are immutable process-lifetime objects.
|
|
let precise_key = unsafe { AVURLAssetPreferPreciseDurationAndTimingKey };
|
|
let precise = NSNumber::new_bool(true);
|
|
let options = NSDictionary::from_slices(&[precise_key], &[&*precise as &AnyObject]);
|
|
// SAFETY: The local URL and the boolean option are retained by the asset.
|
|
let asset = unsafe { AVURLAsset::URLAssetWithURL_options(&url, Some(&options)) };
|
|
// SAFETY: Synchronous property access is supported for local file assets.
|
|
#[allow(deprecated)]
|
|
let tracks = unsafe { asset.tracks() };
|
|
// SAFETY: Framework constants are immutable process-lifetime objects.
|
|
let video = unsafe { AVMediaTypeVideo }.ok_or("AVMediaTypeVideo is unavailable")?;
|
|
// SAFETY: Framework constants are immutable process-lifetime objects.
|
|
let audio = unsafe { AVMediaTypeAudio }.ok_or("AVMediaTypeAudio is unavailable")?;
|
|
let mut probed = Vec::with_capacity(tracks.len());
|
|
for track in &tracks {
|
|
// SAFETY: Track metadata is immutable while the asset and track are retained.
|
|
let (range, media_type) = unsafe { (track.timeRange(), track.mediaType()) };
|
|
let kind = if media_type.isEqualToString(video) {
|
|
TrackKind::Video
|
|
} else if media_type.isEqualToString(audio) {
|
|
TrackKind::Audio
|
|
} else {
|
|
TrackKind::Other
|
|
};
|
|
probed.push(Track {
|
|
kind,
|
|
start: exact_time(range.start).ok_or("a track start is not an exact time")?,
|
|
duration: exact_time(range.duration).ok_or("a track duration is not an exact time")?,
|
|
});
|
|
}
|
|
Ok(Probe { tracks: probed })
|
|
}
|
|
|
|
fn exact_time(time: CMTime) -> Option<MediaTime> {
|
|
let numeric = time.flags.contains(CMTimeFlags::Valid)
|
|
&& !time.flags.intersects(CMTimeFlags::ImpliedValueFlagsMask)
|
|
&& time.epoch == 0;
|
|
numeric
|
|
.then(|| MediaTime::new(time.value, time.timescale))
|
|
.flatten()
|
|
}
|