Repository navigation
linux-v4l2: Map framerates saved truncated to the device's interval - #1118
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, andselect timed outmessages 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
int, so only the denominator was saved:"framerate": 60for 1/60 s.v4l2_unpack_tuple()(v4l2-helpers.h) reads a value with the upper 32 bits zero as an old 16-bit tuple: numerator60 >> 16 = 0.VIDIOC_S_PARMthen gets 0/60 s per frame, and uvcvideo picks its shortest interval.60never equalsdata->framerate. Every settings change (any control slider, buffering) restarted the capture inv4l2_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; itsS_PARMpicks the nearest interval like uvcvideo. The scene collection holds the source with the saved value below, then one unrelated change, Brightness, over obs-websocket):"framerate"linux-v4l2.so(C lane)60(truncated 1/60)4294967356, 0 restarts60000(truncated 1001/60000)4299262323296, 0 restarts65566(old 16-bit 1/30)4294967326, 0 restarts4294967346(packed 1/50, control)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:v4l2_init()enumeratesVIDIOC_ENUM_FRAMEINTERVALSfor the format and size it just set, calls it beforev4l2_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
RED → GREEN
RED commit: 3006835
linux-v4l2: Add RED tests for framerates saved truncated. It addsv4l2_framerate_saved()as a stub that returns the setting, and wires it in (no behaviour change).test_v4l2_frameratenow also runs the realv4l2-input.cagainst 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";saved_truncated_framerate_requests_its_interval("saved 30 (from 1/30 s per frame) requests 0/30"),saved_legacy_framerate_is_stored_as_its_intervalandunmatched_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_keptandsource_saved_with_packed_framerate_runs_at_it.GREEN: 2746821 (both on master e2e26a6).
test_v4l2_frameratepasses 11/11 withENABLE_RUST_LIBOBSOFF and ON, and also without a display. Full ctest at-j8(private Xvfb, systemlibobs-openglmasked, TMPDIR unset):ENABLE_RUST_LIBOBSOFF: 266/266. Skips:open_crashes_without_obs_core(intest_audio_ioandtest_video_io, as on master),test_startup_alloc_stableandtest_startup_cycle_stress(obs_startup()fails on this host).open_crashes_without_obs_coreskips.ci/test_registration.pyare clean.The release cases again, with the GREEN
linux-v4l2.soin the released AppImage: see the table above.CI:
test_v4l2_frameratestays registered for Linux only (OS_LINUX AND TARGET OBS::v4l2), now built liketest_v4l2_fd_cloexec(libv4l2 headers, FFmpeg). Its cases need no display, camera or kernel module.Checklist
master-W..., ratchets): noneAI 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