Screen recordings keep up with animations: every drawn frame, up to 60 fps, with its real time
CI / test (push) Canceled after 0s

A recording of an animation looked choppy because frames barely reached ffmpeg: each 8 MB
1080p frame went out only as far as the socket buffer took it on each main loop pass, so a
new frame could be taken only every few hundred ms (V's recording changed picture ~12 times a
second; headless, 2 a second). Frames were also stamped when ffmpeg read them, not when drawn,
and every grab forced a full redraw.

Now:
- Both hosts (wasmhost raster_stub.c, host-wasm main.rs) copy a frame into a 3-slot queue and
  a writer thread sends it, so a frame never waits on the main loop. A full queue (ffmpeg
  behind) means that frame is not taken; the next one is, with its own time.
- The stream is uncompressed RGBA in Matroska (recorder.mbt mkv_stream_header /
  mkv_frame_header; the hosts write the same 42-byte cluster header), each frame stamped
  with the time it was drawn. ffmpeg makes constant 60 fps H.264 from it, under nice -n 10.
- A frame is taken when the desktop draws a changed frame (app.mbt draw_frame,
  compose_frame), at most 60 a second, or when the pointer moves. A still screen sends
  nothing; the REC pill redraws only its own rectangle when its seconds turn.
- ffmpeg going away mid-recording says so in the notification.
- Tests: the cluster header bytes, ffprobe reading the stream with each frame's own time,
  and the recording test now checks a 60 fps file as long as the recording ran. The
  recording tests fail with a clear message when ffmpeg/ffprobe are missing instead of
  returning early; CI installs ffmpeg.

Headless 1080p, one window redrawing at 60 fps (rec-bench.sh in project files
recorder-smooth/): distinct frames in the video 2.1/s -> 58.9/s (MoonBit host), 59.1/s
(Rust host); overview open/close 1.8/s -> 38.7/s (its previews refresh at 5-10/s live).
Compositor kept drawing 57 fps while recording; still screen: compositor 8% CPU.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hjEUQq16Te1LPYMinT
This commit is contained in:
Claude committed 2026-11-26 04:14:28 +00:00
1 parent 14acc5d42e
commit 024f18ce61
11 files changed
+707 -158

No files matched your search

+80 -11
View File
@@ -142,25 +142,87 @@ test "the screenshot module's request waits for a frame, then opens the overlay"
assert_eq(png_size(@sys.read_file_bytes(comp.last_screenshot())), (1280, 720))
}
///| Recording tests need ffmpeg (and ffprobe, from the same package) on PATH.
fn need_ffmpeg() -> Unit raise {
if !@sys.has_exe("ffmpeg") || !@sys.has_exe("ffprobe") {
fail("recording tests need ffmpeg and ffprobe on PATH (apt install ffmpeg)")
}
}
///| Run a shell command to completion and give its stdout (via a file; `spawn` doesn't wait).
fn run_capture(cmd : String, tag : String) -> String {
let out = "/tmp/chandra-rec-" + tag + "-" + @sys.getpid().to_string() + ".txt"
let done = out + ".done"
ignore(@sys.spawn("(" + cmd + ") > '" + out + "' 2>&1; touch '" + done + "'", ""))
let t0 = @sys.monotonic_ms()
while @sys.read_file_bytes(done).length() == 0 && @sys.monotonic_ms() - t0 < 20000UL {
@sys.sleep_ms(20)
}
let bytes = @sys.read_file_bytes(out)
let text = @utf8.decode_lossy(bytes)
@sys.unlink(out)
@sys.unlink(done)
text
}
///|
test "each recorded frame is a 42-byte Matroska cluster header, then the pixels" {
let h = @host.mkv_frame_header(0x0102030405UL, 16)
assert_eq(h.length(), 42)
// Cluster, size 30 + 16.
assert_eq(h[0:12].to_bytes(), b"\x1f\x43\xb6\x75\x01\x00\x00\x00\x00\x00\x00\x2e")
// Timestamp (ms), 8 bytes.
assert_eq(h[12:26].to_bytes(), b"\xe7\x01\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x01\x02")
// SimpleBlock of 4 + 16 bytes: track 1, relative time 0, keyframe.
assert_eq(h[26:42].to_bytes(), b"\x03\x04\x05\xa3\x01\x00\x00\x00\x00\x00\x00\x14\x81\x00\x00\x80")
}
///|
test "ffmpeg reads the recording stream with each frame's own time" {
need_ffmpeg()
let path = "/tmp/chandra-rec-stream-" + @sys.getpid().to_string() + ".mkv"
let buf = @buffer.new()
buf.write_bytes(@host.mkv_stream_header(4, 2))
// Frames drawn at uneven times, as an animation that stalls once.
for t in [5000UL, 5016UL, 5033UL, 5150UL] {
buf.write_bytes(@host.mkv_frame_header(t, 32))
for _ in 0..<8 {
buf.write_bytes(b"\x10\x20\x30\xff")
}
}
ignore(@sys.write_file_bytes(path, buf.to_bytes()))
let out = run_capture(
"ffprobe -v error -show_entries stream=codec_name,pix_fmt,width,height -show_entries frame=pts_time -of csv=p=0 '" +
path + "'",
"probe",
)
@sys.unlink(path)
assert_eq(out, "5.000000\n5.016000\n5.033000\n5.150000\nrawvideo,4,2,rgba\n")
}
///|
test "super+alt+r records the screen to a video with ffmpeg; the REC pill stops it" {
need_ffmpeg()
let (comp, dir) = shot_comp("rec")
chord(comp, [125U, 56U], 64U | 8U, 19U, 1U)
if !@sys.has_exe("ffmpeg") {
assert_false(comp.recording())
assert_true(comp.screenshot_toast_shown())
return
}
assert_true(comp.recording())
assert_eq(comp.recording_rect(), Some(@scene.Rect::new(0, 0, 1280, 720)))
// Run the loop for a little over a second.
// Run the loop for a little over a second, the pointer moving all the while: each step a
// changed picture, so frames are taken as often as the recorder allows.
let t0 = @sys.monotonic_ms()
let mut i = 0
while @sys.monotonic_ms() - t0 < 1500UL {
i = i + 1
comp.handle_input(@nested.InputEvent::PointerMove((300 + i % 400).to_double(), 300.0), 50U + i.reinterpret_as_uint())
comp.step()
ignore(comp.compose_frame())
@sys.sleep_ms(10)
@sys.sleep_ms(5)
}
assert_true(comp.recorded_frames() >= 5)
let frames = comp.recorded_frames()
let elapsed = @sys.monotonic_ms() - t0
// Never more than 60 a second, and not far under it either.
assert_true(frames.to_uint64() <= elapsed * 60UL / 1000UL + 2UL)
assert_true(frames >= 20)
// A click on the pill stops it; ffmpeg then finishes the file.
click_at(comp, 640, 15, 100U)
assert_false(comp.recording())
@@ -178,14 +240,21 @@ test "super+alt+r records the screen to a video with ffmpeg; the REC pill stops
@sys.sleep_ms(50)
}
assert_true(size > 1000)
// Constant 60 fps, as long as the recording ran (the frames carry their own times).
let probe = run_capture(
"ffprobe -v error -select_streams v -show_entries stream=r_frame_rate,duration -of csv=p=0 '" + path + "'",
"dur",
)
let parts = probe.trim_space().split(",").collect()
assert_eq(parts[0].to_string(), "60/1")
let dur_ms = (@string.parse_double(parts[1]) * 1000.0).to_int()
assert_true(dur_ms > 1200 && dur_ms < 2200)
}
///|
test "super+shift+r records a region picked on the overlay" {
need_ffmpeg()
let (comp, _) = shot_comp("recregion")
if !@sys.has_exe("ffmpeg") {
return
}
chord(comp, [125U, 42U], 65U, 19U, 1U)
assert_true(comp.screenshot_active())
drag(comp, 100, 100, 301, 251, 10U)