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

+140 -18
View File
@@ -11,6 +11,8 @@
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <poll.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -990,10 +992,22 @@ void rs_present_kms(void) {
kms_damage = (Box){ 0, 0, 0, 0 };
}
/* ---- screen recording: the frame in flight stays here ---- */
/* ---- screen recording: a short queue of frames and a writer thread ----
* rs_grab_recording copies a frame into a free slot (a Matroska Cluster header with the time it
* was taken, then the pixels; recorder.mbt's mkv_frame_header writes the same 42 bytes) and the
* writer thread sends the slots to ffmpeg's socket in order, so a frame never waits on the main
* loop. A full queue means ffmpeg is behind: that frame is not taken. */
#define REC_SLOTS 3
#define REC_HEAD 42
static uint8_t *rec_slot[REC_SLOTS];
static size_t rec_cap[REC_SLOTS], rec_size[REC_SLOTS];
static int rec_first, rec_count; /* the queue: slots rec_first.. (mod REC_SLOTS), rec_count of them */
static int rec_fd = -1, rec_running, rec_stop, rec_failed;
static pthread_t rec_thread;
static pthread_mutex_t rec_mu = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t rec_cv = PTHREAD_COND_INITIALIZER;
static uint8_t *rec_px;
static size_t rec_len, rec_sent;
static void rec_fill(int32_t w, int32_t h, int32_t x, int32_t y, int32_t rw, int32_t rh, uint8_t v) {
for (int32_t j = imax(y, 0); j < imin(y + rh, h); j++)
@@ -1016,33 +1030,141 @@ static void rec_pointer(int32_t w, int32_t h, int32_t x, int32_t y) {
}
}
/* An EBML ID and an 8-byte size. */
static uint8_t *rec_id_size(uint8_t *o, const uint8_t *id, int idn, uint64_t n) {
memcpy(o, id, idn); o += idn;
*o++ = 0x01;
for (int i = 6; i >= 0; i--) *o++ = (uint8_t)(n >> (i * 8));
return o;
}
static void rec_frame_header(uint8_t *o, uint64_t ms, size_t n) {
static const uint8_t cluster[] = { 0x1f, 0x43, 0xb6, 0x75 }, tc[] = { 0xe7 }, block[] = { 0xa3 };
o = rec_id_size(o, cluster, 4, 30 + n);
o = rec_id_size(o, tc, 1, 8);
for (int i = 7; i >= 0; i--) *o++ = (uint8_t)(ms >> (i * 8));
o = rec_id_size(o, block, 1, 4 + n);
o[0] = 0x81; o[1] = 0; o[2] = 0; o[3] = 0x80;
}
static uint64_t rec_now_ms(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
}
/* Write all of buf; ffmpeg not taking anything for 5 s counts as a failure (so stopping never
* hangs on a stuck encoder). */
static int rec_write_all(int fd, const uint8_t *buf, size_t len) {
size_t done = 0;
while (done < len) {
ssize_t w = send(fd, buf + done, len - done, MSG_NOSIGNAL);
if (w < 0 && errno == ENOTSOCK) w = write(fd, buf + done, len - done);
if (w < 0) {
if (errno == EINTR) continue;
if (errno == EAGAIN || errno == EWOULDBLOCK) {
struct pollfd p = { .fd = fd, .events = POLLOUT };
int r = poll(&p, 1, 5000);
if (r < 0 && errno == EINTR) continue;
if (r <= 0) { fprintf(stderr, "recording: ffmpeg took nothing for 5 s\n"); return -1; }
continue;
}
fprintf(stderr, "recording: writing to ffmpeg failed: %s\n", strerror(errno));
return -1;
}
done += (size_t)w;
}
return 0;
}
static void *rec_writer(void *arg) {
(void)arg;
pthread_mutex_lock(&rec_mu);
for (;;) {
while (rec_count == 0 && !rec_stop) pthread_cond_wait(&rec_cv, &rec_mu);
if (rec_count == 0) break; /* stopping, and all of it went out */
int i = rec_first;
pthread_mutex_unlock(&rec_mu);
int bad = rec_write_all(rec_fd, rec_slot[i], rec_size[i]) < 0;
pthread_mutex_lock(&rec_mu);
if (bad) { rec_failed = 1; break; }
rec_first = (rec_first + 1) % REC_SLOTS;
rec_count--;
}
pthread_mutex_unlock(&rec_mu);
return NULL;
}
int32_t rs_grab_recording(int32_t x, int32_t y, int32_t w, int32_t h, int32_t pointer, int32_t px, int32_t py) {
if (!fb || w <= 0 || h <= 0 || x < 0 || y < 0 || x + w > fb_w || y + h > fb_h) return 0;
pthread_mutex_lock(&rec_mu);
int full = rec_count == REC_SLOTS || rec_failed;
int i = (rec_first + rec_count) % REC_SLOTS;
pthread_mutex_unlock(&rec_mu);
if (full) return 0;
/* Slot i is not in the queue, so the writer doesn't touch it while it is filled. */
size_t n = (size_t)w * h * 4;
if (n != rec_len) { free(rec_px); rec_px = malloc(n); rec_len = n; }
for (int32_t j = 0; j < h; j++) memcpy(rec_px + (size_t)j * w * 4, fb + ((size_t)(y + j) * fb_w + x) * 4, (size_t)w * 4);
for (size_t i = 3; i < n; i += 4) rec_px[i] = 0xff;
if (rec_cap[i] < REC_HEAD + n) {
free(rec_slot[i]);
rec_slot[i] = malloc(REC_HEAD + n);
if (!rec_slot[i]) { rec_cap[i] = 0; fprintf(stderr, "recording: out of memory for a frame\n"); return 0; }
rec_cap[i] = REC_HEAD + n;
}
rec_frame_header(rec_slot[i], rec_now_ms(), n);
rec_px = rec_slot[i] + REC_HEAD;
/* Copy and make opaque in one pass (alpha is the high byte of each little-endian pixel). */
for (int32_t j = 0; j < h; j++) {
const uint8_t *src = fb + ((size_t)(y + j) * fb_w + x) * 4;
uint8_t *dst = rec_px + (size_t)j * w * 4;
for (int32_t k = 0; k < w; k++) {
uint32_t v;
memcpy(&v, src + (size_t)k * 4, 4);
v |= 0xff000000u;
memcpy(dst + (size_t)k * 4, &v, 4);
}
}
if (pointer) rec_pointer(w, h, px - x, py - y);
rec_sent = 0;
rec_size[i] = REC_HEAD + n;
pthread_mutex_lock(&rec_mu);
rec_count++;
pthread_cond_signal(&rec_cv);
pthread_mutex_unlock(&rec_mu);
return 1;
}
/* fd >= 0: the writer sends to fd (started on the first call): the frames still queued, or -1
* once a write failed. fd < 0: send what is queued, stop the writer (0, or -1 when it failed). */
int32_t rs_send_recording(int32_t fd) {
if (!rec_px) return 0;
int fl = fcntl(fd, F_GETFL);
if (fl >= 0 && !(fl & O_NONBLOCK)) fcntl(fd, F_SETFL, fl | O_NONBLOCK);
while (rec_sent < rec_len) {
ssize_t w = send(fd, rec_px + rec_sent, rec_len - rec_sent, MSG_NOSIGNAL);
if (w < 0 && errno == ENOTSOCK) w = write(fd, rec_px + rec_sent, rec_len - rec_sent);
if (w < 0) {
if (errno == EINTR) continue;
if (errno == EAGAIN || errno == EWOULDBLOCK) break;
if (fd >= 0) {
if (!rec_running) {
rec_fd = fd; rec_stop = 0; rec_failed = 0; rec_first = 0; rec_count = 0;
int fl = fcntl(fd, F_GETFL);
if (fl >= 0 && !(fl & O_NONBLOCK)) fcntl(fd, F_SETFL, fl | O_NONBLOCK);
if (pthread_create(&rec_thread, NULL, rec_writer, NULL) != 0) {
fprintf(stderr, "recording: can't start the writer thread\n");
return -1;
}
rec_running = 1;
} else if (fd != rec_fd) {
fprintf(stderr, "recording: a second stream while one is running\n");
return -1;
}
rec_sent += (size_t)w;
pthread_mutex_lock(&rec_mu);
int32_t r = rec_failed ? -1 : rec_count;
pthread_mutex_unlock(&rec_mu);
return r;
}
size_t left = rec_len - rec_sent;
return left > 0x7fffffff ? 0x7fffffff : (int32_t)left;
if (!rec_running) return 0;
pthread_mutex_lock(&rec_mu);
rec_stop = 1;
pthread_cond_signal(&rec_cv);
pthread_mutex_unlock(&rec_mu);
pthread_join(rec_thread, NULL);
rec_running = 0;
rec_fd = -1;
int32_t r = rec_failed ? -1 : 0;
rec_count = 0;
return r;
}
/* ---- screenshots: the frozen screen stays here ---- */