Skip to content

linux-v4l2: Write virtual camera frames in the size the device kept - #213

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/212-vcam-fixed-format
Oct 11, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/212-vcam-fixed-format

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

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() in plugins/linux-v4l2/v4l2-output.c ignored the format VIDIOC_S_FMT returned. It set the video conversion to the OBS output size and wrote width*height*2 bytes 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.

  1. test/cmocka: Add RED virtual camera format negotiation tests: moves the decision, unchanged, into vcam_format_negotiate() in plugins/linux-v4l2/vcam-format.[ch] (try_connect() uses it) and adds test_vcam_format. 3 of 4 fail on this commit:
    • fixed_smaller_device_gets_frames_of_its_size
    • fixed_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_refused
    • the unfixed-device case passes.
  2. linux-v4l2: Write virtual camera frames in the size the device kept: scaling and frame size come from the format S_FMT returned, 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 than bytesperline*height, so a driver that leaves bytesperline at 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_format fails 3 of 4 at the RED commit and passes 4 of 4 at the GREEN commit (built and run on Arch Linux); the linux-v4l2 plugin builds cleanly; clang-format and gersemi are clean.

By hand, headless, with the real linux-v4l2 plugin in libobs and a temporary v4l2loopback 0.15.4 device (in a sandbox where only that device was visible), fixed to YUYV the way v4l2loopback-ctl set-caps does 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:

Device before after
fixed 640x480 FAIL: top quadrants 10800/21600 match (rows interleaved), bottom quadrants 0/21600 PASS
fixed 640x360 FAIL PASS
fixed 1920x1080 FAIL PASS
not fixed PASS PASS

The output is squeezed to the device's aspect ratio (no letterbox or crop), as in #12082.

Not tested:

  • v4l2loopback versions other than 0.15.4.
  • The Rust and Docker lanes of the local gate.

Separately noticed, not changed here: after a partial write, virtual_video() retries the whole frame_size from data[0].

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have read the contributing document.
  • My code has been run through clang-format.
  • My code follows the project's style guidelines
  • My code is not on the master branch.
  • My code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.
  • I have used AI tooling in the creation of this PR
  • I am an AI agent being used to assist with this pull request: Claude Code (Claude Opus 5.5) wrote the code, 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
rdoupe requested a review from zackees as a code owner October 11, 2026 07:14
rdoupe and others added 2 commits October 11, 2026 00:26
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"}
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Verified locally, rebased onto master and stamped. The new head is 07113d088.

  • RED (31a5cc59d): Tier 2 passed 67 of 68. The new test failed ([ FAILED ] tests: 3 test(s), listed below:).
  • GREEN (2d16028e3): Tier 2 passed 68 of 68 with ENABLE_RUST_LIBOBS OFF and 68 of 68 with it ON.
  • Local gate on the rebased head: lint, rust-test and tier2-linux all passed.

Merging once CI is green.

@zackees
zackees force-pushed the fix/212-vcam-fixed-format branch from 2d16028 to 07113d0 Compare October 11, 2026 07:29
@zackees
zackees merged commit 081b788 into obs-rust:master Oct 11, 2026
23 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: virtual camera output distorted on a loopback device with a fixed format (upstream #12081)

2 participants