Repository navigation
linux-v4l2: Write virtual camera frames in the size the device kept - #213
Merged
Merged
Conversation
When a v4l2loopback device has a fixed format of another size, for example after `v4l2loopback-ctl set-caps /dev/video10 "YUYV:640x480"` with OBS outputting 1280x720, the virtual camera shows interleaved, distorted rows and never the bottom of the picture (upstream obsproject/obs-studio#12081, reported by stephematician). VIDIOC_S_FMT answers with the format the device keeps, but try_connect() ignores it and keeps scaling to and writing frames of the OBS output size, so every 1280-pixel row fills two 640-pixel rows on the device. Move the decision of what to write after VIDIOC_S_FMT into vcam-format.c, free of ioctls. The function keeps today's behaviour (use the requested size, accept any pixel format), so 3 of the 4 tests fail: - fixed_smaller_device_gets_frames_of_its_size - fixed_larger_device_gets_frames_of_its_size - device_fixed_to_other_pixel_format_is_refused Refs obs-rust#212. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
VIDIOC_S_FMT answers with the format the device will use, which is not always the one OBS asked for: a v4l2loopback device fixed with `v4l2loopback-ctl set-caps`, or one a reader already holds open, keeps its own size. Scale the output to that size and write frames of that size, and skip a device whose kept pixel format is not YUYV, instead of writing 1280x720 frames into a 640x480 device and getting interleaved, distorted rows (upstream obsproject/obs-studio#12081). Log the scaling when the device size differs from the output size. Based on upstream obsproject/obs-studio#12082 by stephematician. fixed_smaller_device_gets_frames_of_its_size, fixed_larger_device_gets_frames_of_its_size and device_fixed_to_other_pixel_format_is_refused now pass; 4 of 4 tests pass. Fixes obs-rust#212. Co-Authored-By: stephematician <[email protected]> Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Local-Gate: v1 tree=3e5ec05a822538fda3c2eb293c2821db45441718 secs=176 lanes=lint:run,rust-test:run,tier2-linux:run Ci-Attestation: {"at":1791703747,"gate":"rust/all/fmt","host":"linux-x86_64","key":"e74fefb1355913fc11abd8982416ca180c5ba47d93b46c650ef4e7e5841e4978","lane":"lint","parents":["9fb8b52c5606894c153a4151f5ed2a5d33bd0d7c"],"secs":81,"stamp":"ddbdf370eb88d8540703cc21addf8c00","tree":"3e5ec05a822538fda3c2eb293c2821db45441718","v":1,"via":"run"} Ci-Attestation: {"at":1791703747,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"e74fefb1355913fc11abd8982416ca180c5ba47d93b46c650ef4e7e5841e4978","lane":"lint","parents":["9fb8b52c5606894c153a4151f5ed2a5d33bd0d7c"],"secs":81,"stamp":"371315116981320709780d48e9efd45a","tree":"3e5ec05a822538fda3c2eb293c2821db45441718","v":1,"via":"run"} Ci-Attestation: {"at":1791703747,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"a3c4838fde66eb1a851cc0f55e4fed7b87ab331f724289b76dd75d4a67ea0146","lane":"rust-test","parents":["9fb8b52c5606894c153a4151f5ed2a5d33bd0d7c"],"secs":37,"stamp":"491a7bb052e25bb48dac7261d3156e74","tree":"3e5ec05a822538fda3c2eb293c2821db45441718","v":1,"via":"run"} Ci-Attestation: {"at":1791703748,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"5e5d1e3cfe8f9879bcf69c24b7953f60363a4bd8a3d4f1fb03684969d0a35b9d","lane":"tier2-linux","parents":["9fb8b52c5606894c153a4151f5ed2a5d33bd0d7c"],"secs":58,"stamp":"aed308c225e9d49f530eea9c50acba9e","tree":"3e5ec05a822538fda3c2eb293c2821db45441718","v":1,"via":"run"}
Member
|
Verified locally, rebased onto master and stamped. The new head is
Merging once CI is green. |
zackees
force-pushed
the
fix/212-vcam-fixed-format
branch
from
October 11, 2026 07:29
2d16028 to
07113d0
Compare
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.
Description
The Linux virtual camera produced a distorted, interleaved picture when the v4l2loopback device had a fixed format at a size other than the OBS output size (
v4l2loopback-ctl set-caps,keep_format=1). With this change OBS scales to the size the device kept and writes frames of that size.The cause:
try_connect()inplugins/linux-v4l2/v4l2-output.cignored the formatVIDIOC_S_FMTreturned. It set the video conversion to the OBS output size and wrotewidth*height*2bytes of that size per frame, so at 1280x720 into a 640x480 device each 1280-pixel row filled two 640-pixel rows and the bottom half never arrived.test/cmocka: Add RED virtual camera format negotiation tests: moves the decision, unchanged, intovcam_format_negotiate()inplugins/linux-v4l2/vcam-format.[ch](try_connect()uses it) and addstest_vcam_format. 3 of 4 fail on this commit:fixed_smaller_device_gets_frames_of_its_sizefixed_larger_device_gets_frames_of_its_size("virtual camera writes 1280x720 frames of 1843200 bytes, device takes 1920x1080 frames of 4147200 bytes")device_fixed_to_other_pixel_format_is_refusedlinux-v4l2: Write virtual camera frames in the size the device kept: scaling and frame size come from the formatS_FMTreturned, and a device that doesn't keep YUYV is skipped. All 4 tests pass.Motivation and Context
Fixes #212 (upstream obsproject/obs-studio#12081). The fix is stephematician's obsproject/obs-studio#12082 (open upstream since 2025-05), ported by hand and credited as co-author. Differences from #12082: the frame size stays
width*height*2(the packed YUY2 data OBS hands over) rather thanbytesperline*height, so a driver that leavesbytesperlineat 0 doesn't get 0-byte writes (for v4l2loopback the two are equal); and master's ioctl order is kept.Why this one: the virtual camera is how most Linux users get OBS into video calls, fixed-format loopback devices are a common setup for apps that need a stable format, and the result is an unusable picture with no error.
How Has This Been Tested?
Automated:
test_vcam_formatfails 3 of 4 at the RED commit and passes 4 of 4 at the GREEN commit (built and run on Arch Linux); thelinux-v4l2plugin builds cleanly; clang-format and gersemi are clean.By hand, headless, with the real
linux-v4l2plugin in libobs and a temporary v4l2loopback 0.15.4 device (in a sandbox where only that device was visible), fixed to YUYV the wayv4l2loopback-ctl set-capsdoes it. OBS drew a 1280x720 four-colour quadrant pattern (red, green, blue, white) into the virtual camera, and a checker read frames back through the device and compared every YUYV pixel pair in each quadrant with the expected BT.709 values:The output is squeezed to the device's aspect ratio (no letterbox or crop), as in #12082.
Not tested:
Separately noticed, not changed here: after a partial write,
virtual_video()retries the wholeframe_sizefromdata[0].Types of changes
Checklist:
🤖 Generated with Claude Code