Skip to content

linux-v4l2: Map framerates saved truncated to the device's interval - #1118

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/v4l2-framerate-truncation
Oct 16, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/v4l2-framerate-truncation

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1117

Follow-up of #202 (#201): #202 fixed what the frame rate list stores, but scene collections saved before it still hold the truncated value. Upstream obsproject/obs-studio#14036.

What and why

A V4L2 Video Capture Device loaded from a scene collection saved by stock OBS up to 32.2.2 (or by obs-rust before #202) now runs at the frame rate that was picked. Before, it ran at the camera's fastest rate. jrsallee reported this in obsproject/obs-studio#14036: an Elgato 4K X with 60.00 picked ran at 144 fps, the select() timeout was computed from 144 fps, and select timed out messages flooded the log. In the report's last paragraph, jrsallee also pointed out that already-saved scene collections would keep requesting the maximum rate.

Cause

  • Until linux-v4l2: Keep packed framerate in list values #202 the list stored the packed 64-bit (numerator, denominator) interval through an int, so only the denominator was saved: "framerate": 60 for 1/60 s.
  • On load, v4l2_unpack_tuple() (v4l2-helpers.h) reads a value with the upper 32 bits zero as an old 16-bit tuple: numerator 60 >> 16 = 0. VIDIOC_S_PARM then gets 0/60 s per frame, and uvcvideo picks its shortest interval.
  • The device reports the real interval back, so the saved 60 never equals data->framerate. Every settings change (any control slider, buffering) restarted the capture in v4l2_settings_changed(). Old 16-bit tuples (1 << 16 | 30) request the right rate but restart the same way.

In the release (0.1.0-beta.20261014 AppImage, headless: private Xvfb and D-Bus, fresh HOME. An LD_PRELOAD stand-in for libv4l2 offers the reporter's intervals 30/50/59.94/60/120/144 fps and logs each S_PARM; its S_PARM picks the nearest interval like uvcvideo. The scene collection holds the source with the saved value below, then one unrelated change, Brightness, over obs-websocket):

Saved "framerate" 0.1.0-beta.20261014 release + this branch's linux-v4l2.so (C lane)
60 (truncated 1/60) requests 0/60, runs 144 fps, 1 restart on the unrelated change requests 1/60, runs 60 fps, stored as 4294967356, 0 restarts
60000 (truncated 1001/60000) requests 0/60000, runs 144 fps, 1 restart requests 1001/60000, runs 59.94 fps, stored as 4299262323296, 0 restarts
65566 (old 16-bit 1/30) 1/30, 1 restart 1/30, stored as 4294967326, 0 restarts
4294967346 (packed 1/50, control) 1/50, 0 restarts same
"60.00" picked from the list (control, #202) 1/60, 0 restarts same

After quitting, the fixed build's scene collection holds the packed values.

Fix

  • v4l2_framerate_saved() (v4l2-framerate.c, no ioctls, unit-tested) maps a saved value to the interval it came from, using the device's discrete intervals for the selected format and size:
    • a truncated value (= the interval's denominator) becomes that interval;
    • an old 16-bit tuple that matches an interval becomes its 64-bit form;
    • a value that names no rate (0/x or x/0 s per frame) and matches nothing, or a denominator shared by intervals with different numerators (a driver that does not reduce its fractions), becomes "Leave Unchanged" (-1). The device keeps its rate, and the driver no longer picks its fastest one;
    • packed values and -1 are returned as they are.
  • v4l2_init() enumerates VIDIOC_ENUM_FRAMEINTERVALS for the format and size it just set, calls it before v4l2_set_framerate() and logs the mapping. v4l2_update() writes the mapped value into the settings, so the list entry, the saved scene collection and the running device agree from then on.

Not changed

  • The stored representation stays the packed 64-bit tuple. Old values are migrated when the device opens. A source whose device is missing keeps its setting untouched until the device is there.
  • Stepwise/continuous devices: their list has always stored packed values, so they have no truncated values to map.

RED → GREEN

  • RED commit: 3006835 linux-v4l2: Add RED tests for framerates saved truncated. It adds v4l2_framerate_saved() as a stub that returns the setting, and wires it in (no behaviour change). test_v4l2_framerate now also runs the real v4l2-input.c against a fake libv4l2 with the reporter's intervals. 6 of 11 cases fail in both lanes:

    • source_saved_with_truncated_60_fps_runs_at_60: "saved framerate 60: requested 0/60 s per frame, the camera runs 1/144 (144.00 fps), expected 1/60";
    • source_saved_with_truncated_59_94_fps_runs_at_59_94: same with 0/60000;
    • source_saved_with_unmatched_truncated_framerate_keeps_rate: "saved framerate 25 requested 0/25 s per frame, the camera runs 1/144";
    • unit cases saved_truncated_framerate_requests_its_interval ("saved 30 (from 1/30 s per frame) requests 0/30"), saved_legacy_framerate_is_stored_as_its_interval and unmatched_truncated_framerate_leaves_rate_unchanged.

    The 3 bug: linux-v4l2: frame rate truncated to int, wrong fps and restarts on every settings change (upstream #13388, #13896) #201 cases pass, and so do 2 guards: current_framerate_settings_are_kept and source_saved_with_packed_framerate_runs_at_it.

  • GREEN: 2746821 (both on master e2e26a6). test_v4l2_framerate passes 11/11 with ENABLE_RUST_LIBOBS OFF and ON, and also without a display. Full ctest at -j8 (private Xvfb, system libobs-opengl masked, TMPDIR unset):

    • ENABLE_RUST_LIBOBS OFF: 266/266. Skips: open_crashes_without_obs_core (in test_audio_io and test_video_io, as on master), test_startup_alloc_stable and test_startup_cycle_stress (obs_startup() fails on this host).
    • ON: 266/266. Only open_crashes_without_obs_core skips.
    • All PipeWire/Pulse tests ran.
    • clang-format 22.1.3, gersemi 0.25.0 and ci/test_registration.py are clean.
  • The release cases again, with the GREEN linux-v4l2.so in the released AppImage: see the table above.

  • CI: test_v4l2_framerate stays registered for Linux only (OS_LINUX AND TARGET OBS::v4l2), now built like test_v4l2_fd_cloexec (libv4l2 headers, FFmpeg). Its cases need no display, camera or kernel module.

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): none
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): not a port

AI note: Claude Code (Claude Opus 5.5) wrote the code, the tests, the commit messages and this text under the submitter's direction, and ran the tests above on the submitter's machines.

🤖 Generated with Claude Code

rdoupe and others added 2 commits October 16, 2026 15:40
A scene collection saved by OBS up to 32.2 (and by this fork before
obs-rust#201) holds a V4L2 frame rate truncated to the interval's denominator:
"framerate": 60 for 60.00 fps. That still unpacks as 0/60 s per frame,
so the camera runs at its fastest rate (144 fps instead of 60 on the
Elgato 4K X in obsproject/obs-studio#14036, reported by jrsallee), the
select() timeout is computed from that rate, and because the setting
never equals the interval the device reports back, every settings
change restarts the capture. obs-rust#201 fixed what the list stores, not what
was already saved.

Add v4l2_framerate_saved(), which maps a saved setting to the interval
of the device's list it came from, and call it in v4l2_init() with the
device's discrete intervals for the selected format and size; a mapped
value is stored back by v4l2_update(). It returns the setting unchanged
for now, so behaviour is the same.

test_v4l2_framerate now also runs the real v4l2-input.c against a fake
libv4l2 with the reporter's intervals (30, 50, 59.94, 60, 120, 144 fps;
S_PARM picks the nearest one like uvcvideo). 6 of 11 cases fail:
truncated 1/60 and 1001/60000 settings run the camera at 144 fps, an
unmatched truncated value is sent as 0/25 s per frame, and legacy
16-bit tuples are not stored in the form the device reports.

Refs obs-rust#1117.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Map a saved framerate setting to the interval it was picked from, among
the device's discrete intervals for the selected format and size:

- a truncated value (the interval's denominator, as stored by OBS up to
  32.2 and by this fork before obs-rust#201) becomes that interval: "framerate":
  60 runs the camera at 1/60 s again, 60000 at 1001/60000 s;
- a tuple from before the 64-bit packing that matches an interval is
  stored in the 64-bit form the device reports back;
- a value that names no rate (0/x s per frame) and matches no interval,
  or a denominator shared by several intervals, becomes "Leave
  Unchanged" instead of the driver's fastest rate.

v4l2_update() stores the mapped value, so the setting matches the list
entry and the running device from then on and unrelated settings changes
no longer restart the capture. Packed values and "Leave Unchanged" are
kept as they are, and nothing is changed while the device is missing.

This is the follow-up jrsallee suggested in
obsproject/obs-studio#14036.

Turns the 6 failing test_v4l2_framerate cases green.

Fixes obs-rust#1117.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@rdoupe
rdoupe requested a review from zackees as a code owner October 16, 2026 19:41
@zackees
zackees added this pull request to the merge queue Oct 16, 2026
Merged via the queue into obs-rust:master with commit e5a5668 Oct 16, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug: linux-v4l2: a frame rate saved by OBS up to 32.2 still runs the camera at its fastest rate (upstream #14036, follow-up of #201)

2 participants