Each sample machine now has a VM check, including new ones for text-console and scripted-initrd. While a check runs, a recorder takes a frame every half second through a second QEMU monitor socket, and record-screencasts encodes the frames into recordings/<machine>.webp with img2webp, dropping the BIOS frames at other resolutions. The guide shows each machine's recording in the section it illustrates, replacing the PNG screenshots. The checks are now named vm-<machine>.
122 lines
3.3 KiB
Rust
122 lines
3.3 KiB
Rust
use std::{
|
|
fs::{self, File},
|
|
io::Read,
|
|
path::{Path, PathBuf},
|
|
};
|
|
|
|
use xshell::{Shell, cmd};
|
|
|
|
use crate::task_error::TaskError;
|
|
|
|
const TAPES: [&str; 2] = [
|
|
"scripted-demos/tapes/scripted-boot.tape",
|
|
"scripted-demos/tapes/scripted-shutdown.tape",
|
|
];
|
|
const RECORDING_FRAME_MILLISECONDS: &str = "500";
|
|
|
|
fn sorted_entries(directory: &Path) -> Result<Vec<PathBuf>, TaskError> {
|
|
let read_failure = |source| TaskError::File {
|
|
path: directory.to_path_buf(),
|
|
source,
|
|
};
|
|
let mut entries = fs::read_dir(directory)
|
|
.map_err(read_failure)?
|
|
.map(|entry| entry.map(|entry| entry.path()))
|
|
.collect::<Result<Vec<_>, _>>()
|
|
.map_err(read_failure)?;
|
|
entries.sort();
|
|
Ok(entries)
|
|
}
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
struct FrameSize {
|
|
width: u32,
|
|
height: u32,
|
|
}
|
|
|
|
fn png_frame_size(frame: &Path) -> Result<FrameSize, TaskError> {
|
|
let mut header = [0_u8; 24];
|
|
File::open(frame)
|
|
.and_then(|mut file| file.read_exact(&mut header))
|
|
.map_err(|source| TaskError::File {
|
|
path: frame.to_path_buf(),
|
|
source,
|
|
})?;
|
|
let big_endian = |offset: usize| {
|
|
u32::from_be_bytes([
|
|
header[offset],
|
|
header[offset + 1],
|
|
header[offset + 2],
|
|
header[offset + 3],
|
|
])
|
|
};
|
|
Ok(FrameSize {
|
|
width: big_endian(16),
|
|
height: big_endian(20),
|
|
})
|
|
}
|
|
|
|
fn frames_at_final_size(frames: Vec<PathBuf>) -> Result<Vec<PathBuf>, TaskError> {
|
|
let Some(final_frame) = frames.last() else {
|
|
return Ok(frames);
|
|
};
|
|
let final_size = png_frame_size(final_frame)?;
|
|
let sizes = frames
|
|
.iter()
|
|
.map(|frame| png_frame_size(frame))
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
Ok(frames
|
|
.into_iter()
|
|
.zip(sizes)
|
|
.filter(|(_, size)| *size == final_size)
|
|
.map(|(frame, _)| frame)
|
|
.collect())
|
|
}
|
|
|
|
fn encode_machine_recording(
|
|
shell: &Shell,
|
|
check_output: &Path,
|
|
recordings: &Path,
|
|
) -> Result<(), TaskError> {
|
|
let Some(machine_name) = check_output.file_name() else {
|
|
return Ok(());
|
|
};
|
|
let frames = frames_at_final_size(
|
|
sorted_entries(&check_output.join("recording"))?
|
|
.into_iter()
|
|
.filter(|frame| {
|
|
frame
|
|
.extension()
|
|
.is_some_and(|extension| extension == "png")
|
|
})
|
|
.collect(),
|
|
)?;
|
|
if frames.is_empty() {
|
|
return Err(TaskError::NoRecordingFrames(check_output.to_path_buf()));
|
|
}
|
|
let animation = recordings.join(machine_name).with_extension("webp");
|
|
let frame_duration = RECORDING_FRAME_MILLISECONDS;
|
|
cmd!(
|
|
shell,
|
|
"img2webp -loop 0 -min_size -mixed -d {frame_duration} {frames...} -o {animation}"
|
|
)
|
|
.quiet()
|
|
.run()?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn record_screencasts(vm_check_outputs: &Path) -> Result<(), TaskError> {
|
|
let recordings = Path::new("recordings");
|
|
if !recordings.is_dir() {
|
|
return Err(TaskError::NotInRepositoryRoot);
|
|
}
|
|
let shell = Shell::new()?;
|
|
TAPES.iter().try_for_each(|tape| {
|
|
let tape_path = Path::new(tape);
|
|
cmd!(shell, "vhs {tape_path}").run()
|
|
})?;
|
|
sorted_entries(vm_check_outputs)?
|
|
.iter()
|
|
.try_for_each(|check_output| encode_machine_recording(&shell, check_output, recordings))
|
|
}
|