mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-27 07:58:01 +01:00
* capture: stream audio to disk so long captures keep all of it A capture's timeline now writes 16 kHz mono PCM16 to its bundle's audio.wav as it records, with the leading offset and gaps as silence, so memory stays flat and the 20-minute drop is gone. An 8-hour guard bounds disk use only; past it the rest is one recorded gap. A finished capture is transcribed from the WAV in 30 s pieces, the reader hex transcribe already used (lifted to transcription/pieces.rs), and each piece's words move onto the capture clock. The Parakeet, Apple (path-based), and GGUF paths keep every word without holding the session. The pipeline never rewrites a streamed audio.wav from empty samples, an iPhone narration is read back where it must be re-based, and a cancelled capture closes and removes its file. * capture: say where a capture's audio stopped A streamed capture keeps all of its audio, but the 8-hour disk guard (or a failed audio write) still stops it early, and only the process log said so. The timeline now carries the stop point onto RecordedAudio, the bundle stores it as audioStoppedAtMs (serde default, so old bundles load unchanged), and three places say it plainly: summary.md, `hex capture show`, and `hex capture status`. The guard records its point at a 1 s test limit, and the summary line is tested at 12:34 and 8:00:00 so both clock shapes are covered. * capture: fill the replay's stop point from its bundle The 15 s replay builds Written too, on macOS only, so the new field broke the macOS clippy job while Linux never compiled that function. * capture: keep a recording out of recovery until it stops Review fixes for the streamed timeline. A capture now streams to `audio.wav.partial` and renames it to `audio.wav` in `Timeline::finish`, so a bundle with a movie and an in-progress audio file is never mistaken for a capture whose build never ran (recover's orphan rule, Scrawl.app's `scrawl recover`) and retention cleans a crashed one like any other bundle. Cancel and discard remove the partial; a failed publish reads it back into memory rather than pointing at a missing file. Also: a write failure's stop point is the last sample that reached the file, not the end of the failed chunk; a trim failure counts every written sample; the capture's words keep one running frame total instead of adding truncated per-piece millis; an iPhone narration's `since` drains the decoded samples instead of copying them and treats a missing file as no audio; the phone and replay paths carry the audio stop point into their bundles; and empty audio answers `source: "none"`, as `transcribe_with` does. Tests: a movie plus partial audio is never owed, retention trashes a crashed bundle's media, a write failure records its stop point, a missing streamed file shifts to no samples, and the replay shift moves the stop point with the clock. * capture: publish audio.wav only once its build journals the capture Follow-ups from review. A capture now streams to audio.wav.partial and keeps that name after it stops: the partial is renamed to audio.wav right after the build writes unfinished.json, so audio.wav never exists without a journal, and a stopped capture waiting in the finish queue no longer looks like an orphan to Scrawl.app's recover. A partial that cannot take the name is read back and written by the build, and its partial name removed; the replay removes the unshifted partial once the narration is read for shifting. Also: a periodic header flush error is now a write failure (the rest of the capture is a gap) rather than a log line; the midpoint of a failed write records where the file stopped; and the words test cuts pieces at odd sample counts and computes offsets from the running frame total, so per-piece millisecond drift would fail it. Tests: a streamed audio takes its final name only when the build settles it (and falls back when it cannot), a capture that has not been journaled is never owed a rebuild, a mid-capture write failure stops where the file stopped, and the word offsets are exact at odd piece lengths. * capture: consume a streamed recording's partial when it is read Follow-ups from review. `RecordedAudio::since` and `into_memory` now remove the partial once its samples are read: an iPhone narration (capture.rs's `host_narration` path) used to leave the whole streamed file in the finished bundle. The replay relies on the same path instead of removing it by hand, and the light zip leaves `audio.wav.partial` out defensively. A read that fails is an error to `into_memory` (so a build keeps the only copy) while `since` still treats a missing file as no audio for an iPhone. A failed publish now reads the audio back only when it produced the samples the recording had; a failed read keeps the partial and removes any half-copied `audio.wav`. `publish_audio` takes `&mut self` and points the recording at the published file. A journaled bundle whose `audio.wav` is missing adopts `audio.wav.partial` before rebuilding. Docs say the guarantee is a streamed capture's, and that a crash while recording or queued is retention's job, not recovery's. Tests: reading a streamed recording consumes the partial; a finished iPhone bundle keeps none; a streamed audio is published only when its build settles it, including the read-back and unreadable-partial cases; a journaled capture adopts its partial. * capture: keep a failed audio publish recoverable Follow-ups from review. The publish fallback read the partial with `into_memory`, which deletes it, and then ignored whether its own write landed: a failed write left the audio nowhere. It now reads through `read_streamed` (no deletion), writes `audio.wav` and checks the result, and removes the partial only once that write succeeded. A failed read or write removes any half `audio.wav` and fails the build, which is the pipeline's existing failed path: `recover::finished` runs only for an `Ok` build, so `unfinished.json` stays and the next launch's recovery adopts the partial instead of the capture quietly finishing without its audio. `publish_audio` also treats a landed copy as success even when deleting the partial fails, logging it rather than reporting failure and letting the fallback overwrite a good `audio.wav`. Tests: a blocked target keeps the partial and fails the build; a fallback write that lands consumes the partial and writes `audio.wav`; an unreadable partial is kept.
1363 lines
49 KiB
Rust
1363 lines
49 KiB
Rust
use std::path::{Path, PathBuf};
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
use anyhow::{Context, Result};
|
|
|
|
use crate::audio::{self, RecordedAudio};
|
|
use crate::autocrop;
|
|
use crate::capture;
|
|
use crate::handoff;
|
|
use crate::helper;
|
|
use crate::keyframes::select_keyframes;
|
|
use crate::model::{
|
|
self, CaptureMode, DeviceInfo, InteractionEvent, Mark, RgbaFrame, SCALE, ScrawlBundle, Shape,
|
|
Stroke, TargetFrame, TargetInfo, TranscriptData, TranscriptSegment, WordTiming,
|
|
keyframe_budget,
|
|
};
|
|
use crate::recover;
|
|
use crate::render::{self, render_synthetic_demo_frame};
|
|
use crate::sketch::{marks_of, summary_markdown};
|
|
use crate::speech_model;
|
|
use crate::transcribe;
|
|
use crate::windows;
|
|
|
|
pub fn now_epoch_ms() -> u64 {
|
|
SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.unwrap_or_default()
|
|
.as_millis() as u64
|
|
}
|
|
|
|
pub fn make_bundle_id() -> String {
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
// Two IDs in the same millisecond (parallel tests, overlapping ingests) must still differ.
|
|
static COUNT: AtomicU64 = AtomicU64::new(0);
|
|
let ms = now_epoch_ms();
|
|
let pid = std::process::id();
|
|
match COUNT.fetch_add(1, Ordering::Relaxed) {
|
|
0 => format!("scrawl-{ms}-{pid:x}"),
|
|
n => format!("scrawl-{ms}-{pid:x}-{n}"),
|
|
}
|
|
}
|
|
|
|
/// Matches full-resolution decoded frames back to selected keyframes by timestamp (within 250ms),
|
|
/// so a single frame decode failure only leaves that one keyframe at candidate resolution.
|
|
fn merge_full_keyframes(
|
|
selected: Vec<(model::Keyframe, RgbaFrame)>,
|
|
full: Vec<RgbaFrame>,
|
|
) -> (Vec<(model::Keyframe, RgbaFrame)>, usize) {
|
|
let mut pool: Vec<Option<RgbaFrame>> = full.into_iter().map(Some).collect();
|
|
let mut matched = 0;
|
|
let merged = selected
|
|
.into_iter()
|
|
.map(|(key, fallback)| {
|
|
let best = pool
|
|
.iter()
|
|
.enumerate()
|
|
.filter_map(|(idx, f)| f.as_ref().map(|frame| (idx, frame.t_ms.abs_diff(key.t))))
|
|
.filter(|&(_, diff)| diff <= 250)
|
|
.min_by_key(|&(_, diff)| diff)
|
|
.map(|(idx, _)| idx);
|
|
if let Some(idx) = best
|
|
&& let Some(mut full_frame) = pool[idx].take()
|
|
{
|
|
full_frame.t_ms = key.t;
|
|
matched += 1;
|
|
(key, full_frame)
|
|
} else {
|
|
(key, fallback)
|
|
}
|
|
})
|
|
.collect();
|
|
(merged, matched)
|
|
}
|
|
|
|
/// Per-stage timing breakdown logged on every finish (`finish timings: ...`).
|
|
#[derive(Clone, Copy, Debug, Default)]
|
|
pub struct StageTimings {
|
|
pub started_at: Option<std::time::Instant>,
|
|
pub stop_ms: Option<u64>,
|
|
pub audio_ms: Option<u64>,
|
|
pub candidates_ms: Option<u64>,
|
|
pub transcribe_ms: Option<u64>,
|
|
}
|
|
|
|
/// Pre-rendered keyframe visuals and encoded PNGs computed while transcription is running.
|
|
pub struct PreparedVisuals {
|
|
pub keyframes: Vec<model::Keyframe>,
|
|
pub autocrop: Option<model::AutocropInfo>,
|
|
pub contact_sheet_png: Vec<u8>,
|
|
pub frame_pngs: Vec<(String, Vec<u8>)>,
|
|
pub full_sheet_png: Option<Vec<u8>>,
|
|
pub full_frame_pngs: Vec<(String, Vec<u8>)>,
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub fn prepare_visuals(
|
|
selected: Vec<(model::Keyframe, RgbaFrame)>,
|
|
baseline: Option<&RgbaFrame>,
|
|
crop_settings: Option<&autocrop::Settings>,
|
|
target: &TargetInfo,
|
|
duration_ms: u64,
|
|
strokes: &[Stroke],
|
|
marks: &[Mark],
|
|
events: &[InteractionEvent],
|
|
activity: &windows::ActivityLog,
|
|
) -> Option<PreparedVisuals> {
|
|
if selected.is_empty() {
|
|
return None;
|
|
}
|
|
let (mut annotated_frames, mut cropped) = autocrop::annotate_and_crop(
|
|
selected,
|
|
baseline,
|
|
crop_settings,
|
|
target,
|
|
duration_ms,
|
|
strokes,
|
|
marks,
|
|
events,
|
|
&activity.windows,
|
|
&activity.cursor,
|
|
);
|
|
let keyframes: Vec<model::Keyframe> =
|
|
annotated_frames.iter().map(|(kf, _)| kf.clone()).collect();
|
|
let (full_sheet, contact_sheet) = std::thread::scope(|scope| {
|
|
let full_handle = cropped.as_ref().map(|(_, full)| {
|
|
scope.spawn(|| render::render_contact_sheet_for_duration(duration_ms, full))
|
|
});
|
|
let sheet = render::render_contact_sheet_for_duration(duration_ms, &annotated_frames);
|
|
(full_handle.and_then(|h| h.join().ok()), sheet)
|
|
});
|
|
render::stamp_keyframe_badges(&mut annotated_frames);
|
|
if let Some((_, full)) = &mut cropped {
|
|
render::stamp_keyframe_badges(full);
|
|
}
|
|
let contact_sheet_png = render::encode_png(&contact_sheet).ok()?;
|
|
let frame_pngs: Vec<(String, Vec<u8>)> = std::thread::scope(|scope| {
|
|
let workers: Vec<_> = annotated_frames
|
|
.iter()
|
|
.map(|(kf, frame)| {
|
|
scope.spawn(move || render::encode_png(frame).ok().map(|b| (kf.file.clone(), b)))
|
|
})
|
|
.collect();
|
|
workers
|
|
.into_iter()
|
|
.map(|w| w.join().ok().flatten())
|
|
.collect::<Option<Vec<_>>>()
|
|
})?;
|
|
let (autocrop, full_sheet_png, full_frame_pngs) = match (cropped, full_sheet) {
|
|
(Some((info, full)), Some(sheet)) => {
|
|
let sheet_png = render::encode_png(&sheet).ok()?;
|
|
let full_pngs: Vec<(String, Vec<u8>)> = std::thread::scope(|scope| {
|
|
let workers: Vec<_> = full
|
|
.iter()
|
|
.map(|(kf, frame)| {
|
|
scope.spawn(move || {
|
|
let name = std::path::Path::new(&kf.file)
|
|
.file_name()
|
|
.and_then(|n| n.to_str())?
|
|
.to_owned();
|
|
render::encode_png(frame).ok().map(|b| (name, b))
|
|
})
|
|
})
|
|
.collect();
|
|
workers
|
|
.into_iter()
|
|
.map(|w| w.join().ok().flatten())
|
|
.collect::<Option<Vec<_>>>()
|
|
})?;
|
|
(Some(info), Some(sheet_png), full_pngs)
|
|
}
|
|
_ => (None, None, Vec::new()),
|
|
};
|
|
Some(PreparedVisuals {
|
|
keyframes,
|
|
autocrop,
|
|
contact_sheet_png,
|
|
frame_pngs,
|
|
full_sheet_png,
|
|
full_frame_pngs,
|
|
})
|
|
}
|
|
|
|
/// What a source other than this Mac's screen adds to a bundle.
|
|
#[derive(Default)]
|
|
pub struct SourceExtras {
|
|
pub source: Option<String>,
|
|
pub device: Option<DeviceInfo>,
|
|
/// What the recorded app said about itself: values at the start, their changes, and screens.
|
|
pub context: std::collections::BTreeMap<String, String>,
|
|
pub context_changes: Vec<model::ContextChange>,
|
|
pub screens: Vec<model::ScreenSpan>,
|
|
/// Screen mode's window and cursor log.
|
|
pub activity: windows::ActivityLog,
|
|
/// Time-only markers (ms); each becomes a `Flag` mark with keyframe priority.
|
|
pub flags: Vec<u64>,
|
|
/// When the video starts on the bundle's clock (ms).
|
|
pub video_offset_ms: u64,
|
|
/// Screen-capture auto-crop: `None` follows the config, `Some(None)` is off.
|
|
pub autocrop: Option<Option<autocrop::Settings>>,
|
|
/// Whether the `agentContext` summary format is enabled (`None` reads `handoff::engine_config()`).
|
|
pub agent_context: Option<bool>,
|
|
/// Why there is no picture of the screen (`ScreenRecording::no_screen`).
|
|
pub no_screen: Option<String>,
|
|
/// Where the capture's audio stops on the bundle's clock (ms), when Hex had to stop
|
|
/// keeping it (a streamed capture's disk guard, or a failed write).
|
|
pub audio_stopped_at_ms: Option<u64>,
|
|
/// Decodes the chosen keyframes again at full quality, for sources that select on small
|
|
/// candidate frames (phone movies).
|
|
pub full_frames: Option<FullFrames>,
|
|
pub prepared: Option<PreparedVisuals>,
|
|
pub timings: StageTimings,
|
|
}
|
|
|
|
/// Decodes frames at these times at full quality.
|
|
pub type FullFrames = Box<dyn Fn(&[u64]) -> Result<Vec<RgbaFrame>>>;
|
|
|
|
/// Keyframes at native size from a Mac screen recording, whose clock starts `offset_ms` into the
|
|
/// capture. Each frame comes back stamped with the capture time it was asked for.
|
|
pub fn movie_keyframes(movie: PathBuf, offset_ms: u64) -> FullFrames {
|
|
movie_keyframes_with_cache(movie, offset_ms, Vec::new())
|
|
}
|
|
|
|
fn movie_keyframes_with_cache(
|
|
movie: PathBuf,
|
|
offset_ms: u64,
|
|
predecoded: Vec<RgbaFrame>,
|
|
) -> FullFrames {
|
|
let cache = std::sync::Mutex::new(predecoded);
|
|
Box::new(move |times: &[u64]| {
|
|
let mut pool = cache
|
|
.lock()
|
|
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
|
let mut out = Vec::with_capacity(times.len());
|
|
let mut missing = Vec::new();
|
|
for &t in times {
|
|
if let Some(idx) = pool.iter().position(|f| f.t_ms == t) {
|
|
out.push(pool.swap_remove(idx));
|
|
} else {
|
|
missing.push(t);
|
|
}
|
|
}
|
|
drop(pool);
|
|
if !missing.is_empty() {
|
|
out.extend(movie_frames_at(&movie, &missing, offset_ms, 0)?);
|
|
}
|
|
Ok(out)
|
|
})
|
|
}
|
|
|
|
/// Frames of a movie whose clock starts `offset_ms` into the bundle's, at bundle times.
|
|
pub fn movie_frames_at(
|
|
movie: &Path,
|
|
times: &[u64],
|
|
offset_ms: u64,
|
|
max_edge: u32,
|
|
) -> Result<Vec<RgbaFrame>> {
|
|
movie_frames_at_with(helper::frames, movie, times, offset_ms, max_edge)
|
|
}
|
|
|
|
fn movie_frames_at_with(
|
|
decode: fn(&Path, helper::FrameTimes, u32) -> Result<Vec<RgbaFrame>>,
|
|
movie: &Path,
|
|
times: &[u64],
|
|
offset_ms: u64,
|
|
max_edge: u32,
|
|
) -> Result<Vec<RgbaFrame>> {
|
|
let at: Vec<u64> = times.iter().map(|t| t.saturating_sub(offset_ms)).collect();
|
|
let mut frames = decode(movie, helper::FrameTimes::At(&at), max_edge)?;
|
|
// The helper stamps each frame with the time it was asked for, clamped to the movie (the
|
|
// last keyframe can fall just past its end); map it back to the bundle time.
|
|
for frame in &mut frames {
|
|
if let Some(i) = (0..at.len()).min_by_key(|&i| at[i].abs_diff(frame.t_ms)) {
|
|
frame.t_ms = times[i];
|
|
}
|
|
}
|
|
Ok(frames)
|
|
}
|
|
|
|
/// Core pipeline that turns raw frames, strokes, events, and audio/transcript into a complete `.scrawl` bundle on disk.
|
|
#[allow(clippy::too_many_arguments)]
|
|
///
|
|
/// The recording's candidate frames are small; the keyframes are decoded again from its video at
|
|
/// full size, falling back to the candidates if that fails.
|
|
pub fn finalize_capture_bundle(
|
|
bundle_dir: &Path,
|
|
target: TargetInfo,
|
|
recording: capture::ScreenRecording,
|
|
strokes: Vec<Stroke>,
|
|
events: Vec<InteractionEvent>,
|
|
recorded_audio: Option<RecordedAudio>,
|
|
note: Option<String>,
|
|
activity: windows::ActivityLog,
|
|
) -> Result<(PathBuf, ScrawlBundle, String)> {
|
|
finalize_capture_bundle_with(
|
|
bundle_dir,
|
|
target,
|
|
recording,
|
|
strokes,
|
|
events,
|
|
recorded_audio,
|
|
note,
|
|
activity,
|
|
None,
|
|
)
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub fn finalize_capture_bundle_with(
|
|
bundle_dir: &Path,
|
|
target: TargetInfo,
|
|
recording: capture::ScreenRecording,
|
|
strokes: Vec<Stroke>,
|
|
events: Vec<InteractionEvent>,
|
|
recorded_audio: Option<RecordedAudio>,
|
|
note: Option<String>,
|
|
activity: windows::ActivityLog,
|
|
transcriber: Option<&dyn transcribe::Transcriber>,
|
|
) -> Result<(PathBuf, ScrawlBundle, String)> {
|
|
finalize_capture_bundle_with_flags(
|
|
bundle_dir,
|
|
target,
|
|
recording,
|
|
strokes,
|
|
events,
|
|
Vec::new(),
|
|
recorded_audio,
|
|
note,
|
|
activity,
|
|
transcriber,
|
|
)
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub fn finalize_capture_bundle_with_flags(
|
|
bundle_dir: &Path,
|
|
target: TargetInfo,
|
|
recording: capture::ScreenRecording,
|
|
strokes: Vec<Stroke>,
|
|
events: Vec<InteractionEvent>,
|
|
flags: Vec<u64>,
|
|
recorded_audio: Option<RecordedAudio>,
|
|
note: Option<String>,
|
|
activity: windows::ActivityLog,
|
|
transcriber: Option<&dyn transcribe::Transcriber>,
|
|
) -> Result<(PathBuf, ScrawlBundle, String)> {
|
|
finalize_capture_bundle_for_config(
|
|
bundle_dir,
|
|
target,
|
|
recording,
|
|
strokes,
|
|
events,
|
|
flags,
|
|
recorded_audio,
|
|
note,
|
|
activity,
|
|
transcriber,
|
|
None,
|
|
)
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub fn finalize_capture_bundle_for_config(
|
|
bundle_dir: &Path,
|
|
target: TargetInfo,
|
|
recording: capture::ScreenRecording,
|
|
strokes: Vec<Stroke>,
|
|
events: Vec<InteractionEvent>,
|
|
flags: Vec<u64>,
|
|
recorded_audio: Option<RecordedAudio>,
|
|
note: Option<String>,
|
|
activity: windows::ActivityLog,
|
|
transcriber: Option<&dyn transcribe::Transcriber>,
|
|
engine_cfg: Option<&handoff::EngineConfig>,
|
|
) -> Result<(PathBuf, ScrawlBundle, String)> {
|
|
let started_at = activity
|
|
.stop_started
|
|
.unwrap_or_else(std::time::Instant::now);
|
|
let stop_ms = activity.stop_ms;
|
|
let capture::ScreenRecording {
|
|
frames,
|
|
video: movie,
|
|
duration_ms,
|
|
no_screen,
|
|
} = recording;
|
|
// A capture that streamed its samples to a partial file already has the
|
|
// audio: encoding an empty recording here would replace it with a 44-byte
|
|
// one. `settle_streamed_audio` publishes it after the journal is written.
|
|
if let Some(ref audio) = recorded_audio
|
|
&& !audio.is_on_disk()
|
|
&& let Ok(wav_bytes) = audio.wav_bytes_16k()
|
|
&& !wav_bytes.is_empty()
|
|
{
|
|
let wav_path = bundle_dir.join("audio.wav");
|
|
let _ = std::fs::create_dir_all(bundle_dir);
|
|
let _ = crate::routing::undo::write_private(&wav_path, &wav_bytes);
|
|
}
|
|
let wav_path = bundle_dir.join("audio.wav");
|
|
let journal = |video: Option<&Path>, video_offset_ms: u64| {
|
|
recover::write(
|
|
bundle_dir,
|
|
&recover::Unfinished {
|
|
target: target.clone(),
|
|
duration_ms,
|
|
video: video
|
|
.and_then(|v| v.file_name())
|
|
.map(|name| name.to_string_lossy().into_owned()),
|
|
video_offset_ms,
|
|
strokes: strokes.clone(),
|
|
events: events.clone(),
|
|
note: note.clone(),
|
|
windows: activity.windows.clone(),
|
|
cursor: activity.cursor.clone(),
|
|
},
|
|
);
|
|
};
|
|
// The movie may still be finishing: the journal guesses its start until it is done.
|
|
let hint = movie
|
|
.as_ref()
|
|
.map(|m| (m.path().to_path_buf(), m.offset_hint_ms()));
|
|
// The journal comes first: a movie and a finished `audio.wav` with no
|
|
// journal is what recovery rebuilds, so the streamed audio may only take
|
|
// its final name once this bundle is journaled.
|
|
journal(
|
|
hint.as_ref().map(|(path, _)| path.as_path()),
|
|
hint.as_ref().map_or(0, |h| h.1),
|
|
);
|
|
let recorded_audio = settle_streamed_audio(bundle_dir, recorded_audio, &wav_path)?;
|
|
let has_wav = wav_path.exists();
|
|
let crop_settings = (target.mode == CaptureMode::Display)
|
|
.then(|| match engine_cfg {
|
|
Some(cfg) => autocrop::Settings::from_engine_config(cfg),
|
|
None => autocrop::Settings::configured(),
|
|
})
|
|
.flatten();
|
|
let agent_context = engine_cfg.map(|cfg| cfg.experiment(handoff::AGENT_CONTEXT));
|
|
let mut finished = None;
|
|
let ((transcript, transcribe_ms), (predecoded, prepared)) = std::thread::scope(|scope| {
|
|
let transcribe_handle = scope.spawn(|| {
|
|
let t0 = std::time::Instant::now();
|
|
let tr = match transcriber {
|
|
Some(transcriber) => transcribe::transcribe_with(
|
|
has_wav.then_some(wav_path.as_path()),
|
|
recorded_audio.as_ref(),
|
|
transcriber,
|
|
),
|
|
None => transcribe::transcribe(
|
|
has_wav.then_some(wav_path.as_path()),
|
|
recorded_audio.as_ref(),
|
|
),
|
|
};
|
|
(tr, t0.elapsed().as_millis() as u64)
|
|
});
|
|
// Only what reads the movie waits for it to finish.
|
|
finished = movie.and_then(capture::Movie::wait);
|
|
if finished != hint {
|
|
journal(
|
|
finished.as_ref().map(|(path, _)| path.as_path()),
|
|
finished.as_ref().map_or(0, |f| f.1),
|
|
);
|
|
}
|
|
let pre = if let Some((ref movie, video_offset_ms)) = finished
|
|
&& !frames.is_empty()
|
|
{
|
|
let mut preview_marks = marks_of(&strokes);
|
|
for (i, &t) in flags.iter().enumerate() {
|
|
preview_marks.push(model::Mark {
|
|
id: format!("f{}", i + 1),
|
|
strokes: Vec::new(),
|
|
t0: t,
|
|
t1: t,
|
|
box_norm: [0.0, 0.0, 1.0, 1.0],
|
|
shape: Shape::Flag,
|
|
from: None,
|
|
to: None,
|
|
pin: None,
|
|
on: None,
|
|
description: "flagged this moment".into(),
|
|
frame: None,
|
|
});
|
|
}
|
|
let budget = keyframe_budget(duration_ms);
|
|
let preview = select_keyframes(
|
|
&frames,
|
|
&strokes,
|
|
&mut preview_marks,
|
|
&events,
|
|
&TranscriptData::default(),
|
|
budget,
|
|
);
|
|
// Marks alone filled the budget, so speech can't change the selection.
|
|
let marks_fill = preview.len() == budget
|
|
&& preview
|
|
.iter()
|
|
.all(|(k, _)| k.reason == model::KeyframeReason::Mark);
|
|
let times: Vec<u64> = preview.iter().map(|(k, _)| k.t).collect();
|
|
let decoded = movie_frames_at(movie, ×, video_offset_ms, 0).unwrap_or_default();
|
|
if duration_ms >= 20_000 {
|
|
let cache = if marks_fill {
|
|
Vec::new()
|
|
} else {
|
|
decoded.clone()
|
|
};
|
|
let (merged, _) = merge_full_keyframes(preview, decoded);
|
|
let vis = prepare_visuals(
|
|
merged,
|
|
frames.first(),
|
|
crop_settings.as_ref(),
|
|
&target,
|
|
duration_ms,
|
|
&strokes,
|
|
&preview_marks,
|
|
&events,
|
|
&activity,
|
|
);
|
|
(cache, vis)
|
|
} else {
|
|
(decoded, None)
|
|
}
|
|
} else {
|
|
(Vec::new(), None)
|
|
};
|
|
(transcribe_handle.join().unwrap(), pre)
|
|
});
|
|
let (video_path, video_offset_ms) = match finished {
|
|
Some((path, offset_ms)) => (Some(path), offset_ms),
|
|
None => (None, 0),
|
|
};
|
|
|
|
let built = build_bundle_from_parts(
|
|
bundle_dir,
|
|
target,
|
|
duration_ms,
|
|
frames,
|
|
strokes,
|
|
events,
|
|
transcript,
|
|
video_path.clone(),
|
|
note,
|
|
SourceExtras {
|
|
activity,
|
|
flags,
|
|
video_offset_ms,
|
|
autocrop: Some(crop_settings),
|
|
agent_context,
|
|
no_screen,
|
|
audio_stopped_at_ms: recorded_audio
|
|
.as_ref()
|
|
.and_then(RecordedAudio::audio_stopped_at_ms),
|
|
full_frames: video_path
|
|
.map(|movie| movie_keyframes_with_cache(movie, video_offset_ms, predecoded)),
|
|
prepared,
|
|
timings: StageTimings {
|
|
started_at: Some(started_at),
|
|
stop_ms,
|
|
transcribe_ms: Some(transcribe_ms),
|
|
..StageTimings::default()
|
|
},
|
|
..SourceExtras::default()
|
|
},
|
|
);
|
|
if built.is_ok() {
|
|
recover::finished(bundle_dir);
|
|
}
|
|
built
|
|
}
|
|
|
|
/// Gives a streamed capture's audio its final `audio.wav` name, now that the
|
|
/// journal which recovery reads exists. When the file cannot take the name,
|
|
/// its samples are read without consuming it and written by this build; only a
|
|
/// write that lands removes the partial. If the audio cannot be published or
|
|
/// written, the build fails with the journal left in place, so recovery adopts
|
|
/// the partial on the next launch rather than the audio being lost.
|
|
fn settle_streamed_audio(
|
|
bundle_dir: &Path,
|
|
recorded: Option<RecordedAudio>,
|
|
wav_path: &Path,
|
|
) -> Result<Option<RecordedAudio>> {
|
|
let Some(mut audio) = recorded else {
|
|
return Ok(None);
|
|
};
|
|
if !audio.is_on_disk() {
|
|
return Ok(Some(audio));
|
|
}
|
|
if audio.publish_audio(bundle_dir) {
|
|
return Ok(Some(audio));
|
|
}
|
|
crate::log::scrawl_log(format!(
|
|
"audio: could not give {} its final name; writing it from memory",
|
|
wav_path.display()
|
|
));
|
|
// Read it without consuming the partial: only a write that lands lets the
|
|
// partial go, so the audio is never nowhere on disk.
|
|
let memory = match audio.read_streamed() {
|
|
Ok(memory) => memory,
|
|
Err(error) => {
|
|
let _ = std::fs::remove_file(wav_path);
|
|
return Err(anyhow::anyhow!(
|
|
"could not read the capture's audio {}: {error:#}",
|
|
wav_path.display()
|
|
));
|
|
}
|
|
};
|
|
let written = memory
|
|
.wav_bytes_16k()
|
|
.map_err(|error| anyhow::anyhow!("could not encode the capture's audio: {error:#}"))
|
|
.and_then(|bytes| {
|
|
if bytes.is_empty() {
|
|
anyhow::bail!("the capture's audio is empty");
|
|
}
|
|
std::fs::create_dir_all(bundle_dir)?;
|
|
crate::routing::undo::write_private(wav_path, &bytes).map_err(anyhow::Error::from)
|
|
});
|
|
match written {
|
|
Ok(()) => {
|
|
audio.discard_streamed();
|
|
Ok(Some(memory))
|
|
}
|
|
Err(error) => {
|
|
// Leave the journal and the partial for recovery to adopt.
|
|
let _ = std::fs::remove_file(wav_path);
|
|
Err(anyhow::anyhow!(
|
|
"could not write the capture's audio to {}: {error:#}",
|
|
wav_path.display()
|
|
))
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Builds a Screen capture's bundle in `out`, which holds its video and `audio.wav`, from them
|
|
/// and what the recording logged: `scrawl reprocess` and `scrawl recover`.
|
|
pub fn rebuild_screen_bundle(out: &Path, old: recover::Unfinished) -> Result<String> {
|
|
rebuild_screen_bundle_with(out, old, None, &recover::Media::SYSTEM)
|
|
}
|
|
|
|
/// `autocrop` as in `SourceExtras`: `None` follows the config, `Some(None)` keeps the whole frame.
|
|
pub fn rebuild_screen_bundle_with(
|
|
out: &Path,
|
|
old: recover::Unfinished,
|
|
autocrop: Option<Option<autocrop::Settings>>,
|
|
media: &recover::Media,
|
|
) -> Result<String> {
|
|
let started_at = std::time::Instant::now();
|
|
let video = old
|
|
.video
|
|
.as_ref()
|
|
.map(|name| out.join(name))
|
|
.filter(|p| p.is_file())
|
|
.context("The bundle has no video")?;
|
|
let wav = out.join("audio.wav");
|
|
// A crash between the journal and the audio's final name leaves every
|
|
// sample under the partial name: adopt it before rebuilding.
|
|
let partial = out.join(crate::recover::PARTIAL_AUDIO);
|
|
if !wav.is_file() && partial.is_file() {
|
|
let _ = std::fs::rename(&partial, &wav);
|
|
}
|
|
let recorded = if wav.is_file() {
|
|
Some(audio::recorded_audio_from_wav(std::fs::read(&wav)?)?)
|
|
} else {
|
|
None
|
|
};
|
|
let offset = old.video_offset_ms;
|
|
let ((transcript, transcribe_ms), joined) = std::thread::scope(|scope| {
|
|
let frames = scope.spawn(|| {
|
|
let t0 = std::time::Instant::now();
|
|
let res = (media.frames)(&video, helper::FrameTimes::Every(220), 640);
|
|
(res, t0.elapsed().as_millis() as u64)
|
|
});
|
|
let t0 = std::time::Instant::now();
|
|
let tr = (media.transcribe)(recorded.as_ref().map(|_| wav.as_path()), recorded.as_ref());
|
|
((tr, t0.elapsed().as_millis() as u64), frames.join())
|
|
});
|
|
let (frames, candidates_ms) = joined.map_err(|_| anyhow::anyhow!("Frame decoding panicked"))?;
|
|
let mut frames = frames?;
|
|
for frame in &mut frames {
|
|
frame.t_ms += offset;
|
|
}
|
|
// A macOS movie's final frame is held to the recording's stop, which can fall just past the
|
|
// capture's end: those frames repeat the last one.
|
|
frames.retain(|frame| frame.t_ms <= old.duration_ms);
|
|
let (decode, movie) = (media.frames, video.clone());
|
|
let full_frames: Option<FullFrames> = Some(Box::new(move |times: &[u64]| {
|
|
movie_frames_at_with(decode, &movie, times, offset, 0)
|
|
}));
|
|
Ok(build_bundle_from_parts(
|
|
out,
|
|
old.target,
|
|
old.duration_ms,
|
|
frames,
|
|
old.strokes,
|
|
old.events,
|
|
transcript,
|
|
Some(video),
|
|
old.note,
|
|
SourceExtras {
|
|
activity: windows::ActivityLog {
|
|
windows: old.windows,
|
|
cursor: old.cursor,
|
|
..Default::default()
|
|
},
|
|
full_frames,
|
|
video_offset_ms: offset,
|
|
autocrop,
|
|
timings: StageTimings {
|
|
started_at: Some(started_at),
|
|
candidates_ms: Some(candidates_ms),
|
|
transcribe_ms: Some(transcribe_ms),
|
|
..Default::default()
|
|
},
|
|
..SourceExtras::default()
|
|
},
|
|
)?
|
|
.2)
|
|
}
|
|
|
|
/// A bundle without its transcript yet (the speech model was still downloading) gets it later.
|
|
fn mark_transcript_pending(bundle_dir: &Path, bundle: &ScrawlBundle) {
|
|
if bundle.transcript.missing.as_ref().is_some_and(|m| m.retry) {
|
|
speech_model::mark_pending(bundle_dir);
|
|
}
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub fn build_bundle_from_parts(
|
|
bundle_dir: &Path,
|
|
target: TargetInfo,
|
|
duration_ms: u64,
|
|
frames: Vec<RgbaFrame>,
|
|
strokes: Vec<Stroke>,
|
|
events: Vec<InteractionEvent>,
|
|
transcript: TranscriptData,
|
|
video_path: Option<PathBuf>,
|
|
note: Option<String>,
|
|
extras: SourceExtras,
|
|
) -> Result<(PathBuf, ScrawlBundle, String)> {
|
|
let total_started = extras
|
|
.timings
|
|
.started_at
|
|
.unwrap_or_else(std::time::Instant::now);
|
|
let mut marks = marks_of(&strokes);
|
|
for (i, &t) in extras.flags.iter().enumerate() {
|
|
marks.push(Mark {
|
|
id: format!("f{}", i + 1),
|
|
strokes: Vec::new(),
|
|
t0: t,
|
|
t1: t,
|
|
box_norm: [0.0, 0.0, 1.0, 1.0],
|
|
shape: Shape::Flag,
|
|
from: None,
|
|
to: None,
|
|
pin: None,
|
|
on: None,
|
|
description: "flagged this moment".into(),
|
|
frame: None,
|
|
});
|
|
}
|
|
let bench = std::env::var_os("SCRAWL_BENCH").is_some();
|
|
let started = std::time::Instant::now();
|
|
let selected = select_keyframes(
|
|
&frames,
|
|
&strokes,
|
|
&mut marks,
|
|
&events,
|
|
&transcript,
|
|
keyframe_budget(duration_ms),
|
|
);
|
|
let select_only_at = std::time::Instant::now();
|
|
let matches_prepared = extras.prepared.as_ref().is_some_and(|prep| {
|
|
prep.keyframes.len() == selected.len()
|
|
&& prep.keyframes.iter().zip(&selected).all(|(a, (b, _))| {
|
|
a.index == b.index
|
|
&& a.t == b.t
|
|
&& a.mark_id == b.mark_id
|
|
&& a.cursor == b.cursor
|
|
&& a.file == b.file
|
|
})
|
|
});
|
|
enum BundleOutput {
|
|
Prepared(PreparedVisuals),
|
|
Rendered {
|
|
annotated: Vec<(model::Keyframe, RgbaFrame)>,
|
|
cropped: Option<(model::AutocropInfo, Vec<(model::Keyframe, RgbaFrame)>)>,
|
|
full_sheet: Option<RgbaFrame>,
|
|
contact_sheet: RgbaFrame,
|
|
},
|
|
}
|
|
|
|
let mut extras = extras;
|
|
let (keyframes, autocrop, selected_at, annotated_at, rendered_at, output) = if matches_prepared
|
|
{
|
|
drop(frames);
|
|
let mut prep = extras.prepared.take().unwrap();
|
|
let keyframes: Vec<model::Keyframe> = selected.into_iter().map(|(kf, _)| kf).collect();
|
|
let selected_at = std::time::Instant::now();
|
|
let autocrop = prep.autocrop.take();
|
|
(
|
|
keyframes,
|
|
autocrop,
|
|
selected_at,
|
|
selected_at,
|
|
selected_at,
|
|
BundleOutput::Prepared(prep),
|
|
)
|
|
} else {
|
|
// Only the first candidate is needed from here on (auto-crop's baseline); free the rest
|
|
// before the full-size keyframes arrive.
|
|
let baseline = {
|
|
let mut frames = frames;
|
|
frames.truncate(1);
|
|
frames.pop()
|
|
};
|
|
let selected = match &extras.full_frames {
|
|
Some(decode) if !selected.is_empty() => {
|
|
let times: Vec<u64> = selected.iter().map(|(k, _)| k.t).collect();
|
|
match decode(×) {
|
|
Ok(full) => {
|
|
let total = selected.len();
|
|
let (merged, matched) = merge_full_keyframes(selected, full);
|
|
if matched < total {
|
|
crate::log::scrawl_log(format!(
|
|
"Full-quality keyframes: decoded {matched} of {total}"
|
|
));
|
|
}
|
|
merged
|
|
}
|
|
Err(error) => {
|
|
crate::log::scrawl_log(format!("Full-quality keyframes failed: {error:#}"));
|
|
selected
|
|
}
|
|
}
|
|
}
|
|
_ => selected,
|
|
};
|
|
let selected_at = std::time::Instant::now();
|
|
|
|
let crop_settings = match extras.autocrop {
|
|
Some(settings) => settings,
|
|
None if target.mode == CaptureMode::Display => autocrop::Settings::configured(),
|
|
None => None,
|
|
};
|
|
let (mut annotated_frames, mut cropped) = autocrop::annotate_and_crop(
|
|
selected,
|
|
baseline.as_ref(),
|
|
crop_settings.as_ref(),
|
|
&target,
|
|
duration_ms,
|
|
&strokes,
|
|
&marks,
|
|
&events,
|
|
&extras.activity.windows,
|
|
&extras.activity.cursor,
|
|
);
|
|
let keyframes: Vec<model::Keyframe> =
|
|
annotated_frames.iter().map(|(kf, _)| kf.clone()).collect();
|
|
let annotated_at = std::time::Instant::now();
|
|
|
|
let (full_sheet, contact_sheet) = std::thread::scope(|scope| {
|
|
let full_handle = cropped.as_ref().map(|(_, full)| {
|
|
scope.spawn(|| render::render_contact_sheet_for_duration(duration_ms, full))
|
|
});
|
|
let sheet = render::render_contact_sheet_for_duration(duration_ms, &annotated_frames);
|
|
(full_handle.map(|h| h.join().unwrap()), sheet)
|
|
});
|
|
render::stamp_keyframe_badges(&mut annotated_frames);
|
|
if let Some((_, full)) = &mut cropped {
|
|
render::stamp_keyframe_badges(full);
|
|
}
|
|
let rendered_at = std::time::Instant::now();
|
|
let autocrop = cropped.as_ref().map(|(info, _)| info.clone());
|
|
(
|
|
keyframes,
|
|
autocrop,
|
|
selected_at,
|
|
annotated_at,
|
|
rendered_at,
|
|
BundleOutput::Rendered {
|
|
annotated: annotated_frames,
|
|
cropped,
|
|
full_sheet,
|
|
contact_sheet,
|
|
},
|
|
)
|
|
};
|
|
|
|
let id = bundle_dir
|
|
.file_stem()
|
|
.and_then(|s| s.to_str())
|
|
.unwrap_or("scrawl-bundle")
|
|
.to_string();
|
|
let video_rel = video_path.and_then(|p| {
|
|
p.exists()
|
|
.then(|| p.file_name()?.to_str().map(str::to_owned))
|
|
.flatten()
|
|
});
|
|
let audio_rel = bundle_dir
|
|
.join("audio.wav")
|
|
.exists()
|
|
.then(|| "audio.wav".to_string());
|
|
|
|
let bundle = ScrawlBundle {
|
|
version: 1,
|
|
id,
|
|
created_at_ms: now_epoch_ms(),
|
|
duration_ms,
|
|
target,
|
|
transcript,
|
|
note,
|
|
strokes,
|
|
marks,
|
|
events,
|
|
keyframes,
|
|
contact_sheet: "contact-sheet.png".into(),
|
|
video: video_rel,
|
|
video_offset_ms: extras.video_offset_ms,
|
|
audio: audio_rel,
|
|
audio_stopped_at_ms: extras.audio_stopped_at_ms,
|
|
source: extras.source,
|
|
device: extras.device,
|
|
context: extras.context,
|
|
context_changes: extras.context_changes,
|
|
screens: extras.screens,
|
|
windows: extras.activity.windows,
|
|
cursor: extras.activity.cursor,
|
|
autocrop,
|
|
destination: None,
|
|
no_screen: extras.no_screen,
|
|
};
|
|
let summary = match extras.agent_context {
|
|
Some(agent_context) => {
|
|
crate::sketch::summary_markdown_with(&bundle, bundle_dir, agent_context)
|
|
}
|
|
None => summary_markdown(&bundle, bundle_dir),
|
|
};
|
|
|
|
match output {
|
|
BundleOutput::Prepared(prep) => {
|
|
std::fs::create_dir_all(bundle_dir.join("frames"))
|
|
.with_context(|| format!("could not create {}", bundle_dir.display()))?;
|
|
#[cfg(unix)]
|
|
{
|
|
use std::os::unix::fs::PermissionsExt;
|
|
let _ =
|
|
std::fs::set_permissions(bundle_dir, std::fs::Permissions::from_mode(0o700));
|
|
}
|
|
crate::routing::undo::write_private(
|
|
&bundle_dir.join(&bundle.contact_sheet),
|
|
&prep.contact_sheet_png,
|
|
)?;
|
|
for (rel, bytes) in &prep.frame_pngs {
|
|
crate::routing::undo::write_private(&bundle_dir.join(rel), bytes)?;
|
|
}
|
|
if let (Some(info), Some(full_sheet_png)) = (&bundle.autocrop, &prep.full_sheet_png) {
|
|
let full_dir = bundle_dir.join(&info.full_frames);
|
|
std::fs::create_dir_all(&full_dir)?;
|
|
crate::routing::undo::write_private(
|
|
&bundle_dir.join(&info.full_contact_sheet),
|
|
full_sheet_png,
|
|
)?;
|
|
for (name, bytes) in &prep.full_frame_pngs {
|
|
crate::routing::undo::write_private(&full_dir.join(name), bytes)?;
|
|
}
|
|
}
|
|
crate::routing::undo::write_private(
|
|
&bundle_dir.join("summary.md"),
|
|
summary.as_bytes(),
|
|
)?;
|
|
capture::publish_manifest(bundle_dir, &bundle)?;
|
|
}
|
|
BundleOutput::Rendered {
|
|
annotated,
|
|
cropped,
|
|
full_sheet,
|
|
contact_sheet,
|
|
} => {
|
|
std::thread::scope(|scope| -> Result<()> {
|
|
let full_write = match (&cropped, &full_sheet) {
|
|
(Some((info, full)), Some(sheet)) => Some(
|
|
scope.spawn(|| autocrop::write_full_frames(bundle_dir, info, full, sheet)),
|
|
),
|
|
_ => None,
|
|
};
|
|
let written = capture::write_bundle_files(
|
|
bundle_dir,
|
|
&bundle,
|
|
&annotated,
|
|
&contact_sheet,
|
|
&summary,
|
|
);
|
|
if let Some(handle) = full_write {
|
|
handle
|
|
.join()
|
|
.map_err(|_| anyhow::anyhow!("Full-frame writer panicked"))??;
|
|
}
|
|
written
|
|
})?;
|
|
// Last, once everything else is on disk: a bundle with a manifest is complete.
|
|
capture::publish_manifest(bundle_dir, &bundle)?;
|
|
}
|
|
}
|
|
|
|
let finished = std::time::Instant::now();
|
|
let select_ms = (select_only_at - started).as_millis() as u64;
|
|
let decode_ms = (selected_at - select_only_at).as_millis() as u64;
|
|
let autocrop_ms = (annotated_at - selected_at).as_millis() as u64;
|
|
let sheet_ms = (rendered_at - annotated_at).as_millis() as u64;
|
|
let write_ms = (finished - rendered_at).as_millis() as u64;
|
|
let total_ms = (finished - total_started).as_millis() as u64;
|
|
let mut parts = Vec::with_capacity(10);
|
|
if let Some(ms) = extras.timings.stop_ms {
|
|
parts.push(format!("stop {ms}ms"));
|
|
}
|
|
if let Some(ms) = extras.timings.audio_ms {
|
|
parts.push(format!("audio {ms}ms"));
|
|
}
|
|
if let Some(ms) = extras.timings.candidates_ms {
|
|
parts.push(format!("candidates {ms}ms"));
|
|
}
|
|
if let Some(ms) = extras.timings.transcribe_ms {
|
|
parts.push(format!("transcribe {ms}ms"));
|
|
}
|
|
parts.extend([
|
|
format!("select {select_ms}ms"),
|
|
format!("decode {decode_ms}ms"),
|
|
format!("autocrop {autocrop_ms}ms"),
|
|
format!("sheet {sheet_ms}ms"),
|
|
format!("write {write_ms}ms"),
|
|
format!("total {total_ms}ms"),
|
|
]);
|
|
crate::log::scrawl_log(format!("finish timings: {}", parts.join(" · ")));
|
|
if bench && !matches_prepared {
|
|
for (name, elapsed) in [
|
|
("selection_ms", selected_at - started),
|
|
("annotation_ms", annotated_at - selected_at),
|
|
("contact_sheet_ms", rendered_at - annotated_at),
|
|
("artifacts_ms", finished - rendered_at),
|
|
("pipeline_ms", finished - started),
|
|
] {
|
|
eprintln!("BENCH {name}={:.3}", elapsed.as_secs_f64() * 1000.0);
|
|
}
|
|
}
|
|
|
|
mark_transcript_pending(bundle_dir, &bundle);
|
|
Ok((bundle_dir.to_path_buf(), bundle, summary))
|
|
}
|
|
|
|
/// Generates a rich, deterministic multi-frame demo bundle showcasing clicks, circles, arrows, pins, and aligned speech.
|
|
pub fn generate_demo_bundle(out: Option<PathBuf>) -> Result<(PathBuf, ScrawlBundle, String)> {
|
|
let bundle_dir = out.unwrap_or_else(|| {
|
|
handoff::default_captures_dir().join(format!("{}.scrawl", make_bundle_id()))
|
|
});
|
|
let (fw, fh) = (1280_u32, 800_u32);
|
|
let target = TargetInfo {
|
|
mode: CaptureMode::Window,
|
|
app: Some("OpenCode".into()),
|
|
bundle_id: Some("ai.opencode.desktop".into()),
|
|
window_title: Some("session/tool-running — OpenCode".into()),
|
|
window_id: Some(4210),
|
|
display_id: 1,
|
|
frame: TargetFrame {
|
|
x: 100.0,
|
|
y: 60.0,
|
|
width: f64::from(fw),
|
|
height: f64::from(fh),
|
|
scale: 2.0,
|
|
},
|
|
};
|
|
|
|
let frames = vec![
|
|
render_synthetic_demo_frame(fw, fh, 400, 0),
|
|
render_synthetic_demo_frame(fw, fh, 1_500, 1),
|
|
render_synthetic_demo_frame(fw, fh, 3_600, 1),
|
|
render_synthetic_demo_frame(fw, fh, 6_400, 2),
|
|
render_synthetic_demo_frame(fw, fh, 9_200, 2),
|
|
];
|
|
|
|
// 1. Circle around the shifted header band (cx=0.52, cy=0.14, r_x=0.14, r_y=0.06)
|
|
let mut circle_pts = Vec::new();
|
|
for i in 0..=36 {
|
|
let angle = (i as f32 / 36.0) * std::f32::consts::TAU;
|
|
let x = ((0.52 + 0.13 * angle.cos()) * SCALE as f32).round() as i32;
|
|
let y = ((0.15 + 0.055 * angle.sin()) * SCALE as f32).round() as i32;
|
|
circle_pts.extend_from_slice(&[x, y, i * 18]);
|
|
}
|
|
let circle_stroke = Stroke {
|
|
t0: 2_900,
|
|
t1: 2_900 + 36 * 18,
|
|
fw,
|
|
fh,
|
|
on: Some("Band header".into()),
|
|
pin: None,
|
|
laser: false,
|
|
pts: circle_pts,
|
|
};
|
|
|
|
// 2. Arrow pointing from sidebar item to the tool card (shaft + arrowhead)
|
|
let mut shaft_pts = Vec::new();
|
|
for i in 0..=12 {
|
|
let t = i as f32 / 12.0;
|
|
let x = ((0.22 + (0.44 - 0.22) * t) * SCALE as f32).round() as i32;
|
|
let y = ((0.48 + (0.36 - 0.48) * t) * SCALE as f32).round() as i32;
|
|
shaft_pts.extend_from_slice(&[x, y, i * 16]);
|
|
}
|
|
let arrow_shaft = Stroke {
|
|
t0: 5_800,
|
|
t1: 5_800 + 12 * 16,
|
|
fw,
|
|
fh,
|
|
on: Some("Used 12 tools".into()),
|
|
pin: None,
|
|
laser: false,
|
|
pts: shaft_pts,
|
|
};
|
|
let arrow_head = Stroke {
|
|
t0: 6_100,
|
|
t1: 6_160,
|
|
fw,
|
|
fh,
|
|
on: Some("Used 12 tools".into()),
|
|
pin: None,
|
|
laser: false,
|
|
pts: vec![
|
|
(0.41 * SCALE as f32) as i32,
|
|
(0.35 * SCALE as f32) as i32,
|
|
0,
|
|
(0.44 * SCALE as f32) as i32,
|
|
(0.36 * SCALE as f32) as i32,
|
|
30,
|
|
(0.42 * SCALE as f32) as i32,
|
|
(0.40 * SCALE as f32) as i32,
|
|
60,
|
|
],
|
|
};
|
|
|
|
// 3. Numbered pin on the composer send button
|
|
let pin_stroke = Stroke {
|
|
t0: 8_900,
|
|
t1: 8_900,
|
|
fw,
|
|
fh,
|
|
on: Some("Send button".into()),
|
|
pin: Some(1),
|
|
laser: false,
|
|
pts: vec![
|
|
(0.88 * SCALE as f32) as i32,
|
|
(0.88 * SCALE as f32) as i32,
|
|
0,
|
|
],
|
|
};
|
|
|
|
let strokes = vec![circle_stroke, arrow_shaft, arrow_head, pin_stroke];
|
|
let events = vec![
|
|
InteractionEvent::Click {
|
|
t: 1_250,
|
|
x: 0.46,
|
|
y: 0.36,
|
|
button: Some("left".into()),
|
|
label: Some("Used 12 tools".into()),
|
|
role: Some("button".into()),
|
|
},
|
|
InteractionEvent::Scroll {
|
|
t: 4_800,
|
|
x: 0.55,
|
|
y: 0.55,
|
|
dx: 0.0,
|
|
dy: -240.0,
|
|
label: Some("Transcript".into()),
|
|
},
|
|
];
|
|
|
|
let transcript = TranscriptData {
|
|
source: "speech-analyzer".into(),
|
|
model: Some("SpeechTranscriber en_US".into()),
|
|
text: "When I click Used 12 tools this band header jumps down by four pixels, and the row spacing under the card feels too tight.".into(),
|
|
segments: vec![
|
|
TranscriptSegment {
|
|
t0: 600,
|
|
t1: 4_100,
|
|
text: "When I click Used 12 tools this band header jumps down by four pixels,".into(),
|
|
},
|
|
TranscriptSegment {
|
|
t0: 5_400,
|
|
t1: 9_400,
|
|
text: "and the row spacing under the card feels too tight.".into(),
|
|
},
|
|
],
|
|
words: vec![
|
|
WordTiming { t0: 600, t1: 850, text: "When".into() },
|
|
WordTiming { t0: 850, t1: 980, text: "I".into() },
|
|
WordTiming { t0: 980, t1: 1_320, text: "click".into() },
|
|
WordTiming { t0: 1_320, t1: 1_650, text: "Used".into() },
|
|
WordTiming { t0: 1_650, t1: 1_900, text: "12".into() },
|
|
WordTiming { t0: 1_900, t1: 2_300, text: "tools".into() },
|
|
WordTiming { t0: 2_450, t1: 2_700, text: "this".into() },
|
|
WordTiming { t0: 2_700, t1: 3_050, text: "band".into() },
|
|
WordTiming { t0: 3_050, t1: 3_400, text: "header".into() },
|
|
WordTiming { t0: 3_400, t1: 3_700, text: "jumps".into() },
|
|
WordTiming { t0: 3_700, t1: 3_900, text: "down".into() },
|
|
WordTiming { t0: 3_900, t1: 4_100, text: "by four pixels,".into() },
|
|
WordTiming { t0: 5_400, t1: 5_800, text: "and the".into() },
|
|
WordTiming { t0: 5_800, t1: 6_300, text: "row spacing".into() },
|
|
WordTiming { t0: 6_300, t1: 7_200, text: "under the card".into() },
|
|
WordTiming { t0: 7_800, t1: 9_400, text: "feels too tight.".into() },
|
|
],
|
|
estimated: false,
|
|
missing: None,
|
|
};
|
|
|
|
build_bundle_from_parts(
|
|
&bundle_dir,
|
|
target,
|
|
9_800,
|
|
frames,
|
|
strokes,
|
|
events,
|
|
transcript,
|
|
None,
|
|
Some("Header inset shifts on expand".into()),
|
|
SourceExtras::default(),
|
|
)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::capture::write_bundle_artifacts;
|
|
|
|
#[test]
|
|
fn a_streamed_audio_takes_its_final_name_only_when_its_build_settles_it() {
|
|
let dir = std::env::temp_dir().join(format!("hex-pipeline-publish-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
let partial = dir.join(crate::recover::PARTIAL_AUDIO);
|
|
let samples = vec![0.25_f32; 1_600];
|
|
std::fs::write(&partial, crate::audio::encode_wav_16k(&samples).unwrap()).unwrap();
|
|
let wav = dir.join("audio.wav");
|
|
// Before the build settles it, the audio waits under its partial name.
|
|
assert!(!wav.exists() && partial.is_file());
|
|
|
|
let audio = RecordedAudio::on_disk(&partial, samples.len(), 16_000, 0, Vec::new());
|
|
let settled = settle_streamed_audio(&dir, Some(audio), &wav)
|
|
.expect("published")
|
|
.expect("audio kept");
|
|
assert!(settled.is_on_disk());
|
|
assert!(
|
|
wav.is_file() && !partial.exists(),
|
|
"the partial took its final name"
|
|
);
|
|
|
|
// A rename that cannot happen (a non-empty directory holds the name)
|
|
// cannot be written either: the partial stays, and the build fails so
|
|
// recovery adopts it on the next launch.
|
|
std::fs::remove_file(&wav).unwrap();
|
|
std::fs::create_dir_all(wav.join("occupied")).unwrap();
|
|
std::fs::write(&partial, crate::audio::encode_wav_16k(&samples).unwrap()).unwrap();
|
|
let audio = RecordedAudio::on_disk(&partial, samples.len(), 16_000, 0, Vec::new());
|
|
settle_streamed_audio(&dir, Some(audio), &wav).expect_err("the write cannot land");
|
|
assert!(partial.is_file(), "the only copy is kept");
|
|
assert!(wav.is_dir(), "and no half-written audio.wav appears");
|
|
|
|
// When the build's own write does land (here the bundle folder does not
|
|
// exist yet, so the rename cannot), the partial goes.
|
|
let bundle = dir.join("bundle");
|
|
let nested = dir.join("streamed");
|
|
std::fs::create_dir_all(&nested).unwrap();
|
|
let nested_partial = nested.join(crate::recover::PARTIAL_AUDIO);
|
|
std::fs::write(
|
|
&nested_partial,
|
|
crate::audio::encode_wav_16k(&samples).unwrap(),
|
|
)
|
|
.unwrap();
|
|
let nested_wav = bundle.join("audio.wav");
|
|
let audio = RecordedAudio::on_disk(&nested_partial, samples.len(), 16_000, 0, Vec::new());
|
|
let settled = settle_streamed_audio(&bundle, Some(audio), &nested_wav)
|
|
.expect("written")
|
|
.expect("audio kept");
|
|
assert!(!settled.is_on_disk(), "written back from memory");
|
|
assert!(!nested_partial.exists(), "the partial was consumed");
|
|
assert!(nested_wav.is_file(), "audio.wav took the samples");
|
|
|
|
// A partial that cannot be read is kept too: it is the only copy left.
|
|
std::fs::write(&partial, b"not a wav").unwrap();
|
|
let audio = RecordedAudio::on_disk(&partial, 1_600, 16_000, 0, Vec::new());
|
|
settle_streamed_audio(&dir, Some(audio), &wav).expect_err("an unreadable partial fails");
|
|
assert!(partial.is_file(), "kept, and not deleted");
|
|
let _ = std::fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn artifact_failures_do_not_publish_a_manifest() {
|
|
let root = std::env::temp_dir().join(make_bundle_id());
|
|
let (_, mut bundle, _) = generate_demo_bundle(Some(root.join("demo.scrawl"))).unwrap();
|
|
let frame = RgbaFrame::new(0, 1, 1, vec![20, 30, 40, 255]);
|
|
let mut key = bundle.keyframes[0].clone();
|
|
key.file = "frames/missing/frame.png".into();
|
|
let frame_failure = root.join("frame-failure.scrawl");
|
|
assert!(
|
|
write_bundle_artifacts(
|
|
&frame_failure,
|
|
&bundle,
|
|
&[(key, frame.clone())],
|
|
&frame,
|
|
"summary"
|
|
)
|
|
.is_err()
|
|
);
|
|
assert!(!frame_failure.join("scrawl.json").exists());
|
|
|
|
bundle.contact_sheet = "missing/contact-sheet.png".into();
|
|
let sheet_failure = root.join("sheet-failure.scrawl");
|
|
assert!(write_bundle_artifacts(&sheet_failure, &bundle, &[], &frame, "summary").is_err());
|
|
assert!(!sheet_failure.join("scrawl.json").exists());
|
|
|
|
bundle.contact_sheet = "contact-sheet.png".into();
|
|
let empty_frames = root.join("empty-frames.scrawl");
|
|
write_bundle_artifacts(&empty_frames, &bundle, &[], &frame, "summary").unwrap();
|
|
assert!(empty_frames.join("scrawl.json").exists());
|
|
assert!(empty_frames.join("contact-sheet.png").exists());
|
|
#[cfg(unix)]
|
|
{
|
|
use std::os::unix::fs::PermissionsExt;
|
|
for name in ["scrawl.json", "summary.md", "contact-sheet.png"] {
|
|
let mode = std::fs::metadata(empty_frames.join(name))
|
|
.unwrap()
|
|
.permissions()
|
|
.mode()
|
|
& 0o777;
|
|
assert_eq!(mode, 0o600, "{name} should be created 0600");
|
|
}
|
|
}
|
|
std::fs::remove_dir_all(root).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn full_keyframes_match_by_time_and_preserve_partial_decodes() {
|
|
let kf = |index: usize, t: u64| model::Keyframe {
|
|
index,
|
|
t,
|
|
reason: model::KeyframeReason::Click,
|
|
file: format!("frames/frame-{index:02}.png"),
|
|
event: "tap".into(),
|
|
said: None,
|
|
mark_id: None,
|
|
cursor: None,
|
|
};
|
|
let small = |t: u64| RgbaFrame::new(t, 32, 32, vec![10; 32 * 32 * 4]);
|
|
let large = |t: u64| RgbaFrame::new(t, 160, 160, vec![200; 160 * 160 * 4]);
|
|
|
|
let selected = vec![
|
|
(kf(1, 1_000), small(1_000)),
|
|
(kf(2, 2_000), small(2_000)),
|
|
(kf(3, 3_000), small(3_000)),
|
|
];
|
|
// Frame at 2_000 failed to decode and frames arrived out of order (3_000 clamped to 2_999).
|
|
let full = vec![large(2_999), large(1_000)];
|
|
let (merged, matched) = merge_full_keyframes(selected, full);
|
|
assert_eq!(matched, 2);
|
|
assert_eq!(merged[0].1.width, 160);
|
|
assert_eq!(merged[0].1.t_ms, 1_000);
|
|
assert_eq!(merged[1].1.width, 32);
|
|
assert_eq!(merged[1].1.t_ms, 2_000);
|
|
assert_eq!(merged[2].1.width, 160);
|
|
assert_eq!(merged[2].1.t_ms, 3_000);
|
|
}
|
|
}
|