Skip to content

linux-v4l2: Restore saved camera controls only on their camera - #460

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/v4l2-controls-device-identity
Oct 13, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/v4l2-controls-device-identity

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

Description

A Video Capture Device (V4L2) source now restores its saved camera controls only on the camera they were set on. Since #364 they were written to whatever camera the source opened, so switching a source from camera A to camera B, or two cameras swapping /dev/videoN after a reboot or a replug, wrote A's exposure, focus, zoom or menu modes to B.

  • The source records which camera its saved controls belong to: controls_device holds the card name and bus info from VIDIOC_QUERYCAP, the same text the Device list shows, so two cameras of the same model on different ports differ.
  • When the device opens (start, restart, replug), v4l2_init() writes the saved controls only if they belong to the open camera; otherwise it writes nothing and logs it. "Restore camera controls when the camera connects" (linux-v4l2: Add an option to not restore camera controls on connect #386) works as before: unchecked, nothing is written.
  • Picking another camera in the Properties window (device_selected()) puts the previous camera's controls aside under controls_other_devices and brings back those saved for the new camera, if any. The sliders no longer show another camera's values, and switching back restores the first camera's. This writes nothing to the camera.
  • A script or obs-websocket that changes device_id does not go through the Properties window: the new camera is not written, and the saved controls stay the first camera's until the Properties window is opened on the new one.
  • Settings saved before this change have no camera recorded. Their controls belong to the first camera the source opens, as today, so existing setups keep their restore; that camera is recorded when the scene collection is saved (the source's new save callback) or when its Properties window is opened.
  • A device that does not answer VIDIOC_QUERYCAP (every V4L2 driver must) behaves as before.
  1. test/cmocka: Add RED V4L2 camera controls device identity tests: the fake libv4l2 of test_v4l2_controls gets a second camera of the same model on another USB port, with its own card/bus_info and control values, and a second node so the cameras can swap. Nine cases:

    • a source switched to camera B writes nothing to it;
    • A -> B -> A: A gets its values back, B is never written;
    • the same switch in the Properties window: B is not written, the sliders stop showing A's values, and A's come back with A;
    • a control changed on B after the switch is the only one restored on B;
    • camera B taking the source's node on replug, or at startup, is not written;
    • control: the same camera replugged is still restored;
    • settings saved before this change are applied to the first camera opened and tied to it, also when camera B is picked in the Properties window before the scene collection is saved.

    8 of 23 cases fail; the 14 cases of linux-v4l2: Apply saved camera controls when the device opens #364 and linux-v4l2: Add an option to not restore camera controls on connect #386 pass.

  2. linux-v4l2: Restore saved camera controls only on their camera: 23 of 23 pass.

Motivation and Context

Fixes #459, a follow-up of #363 / #364 found in review. #364 (upstream obsproject/obs-studio#11633, reported by vpzomtrrfrt) made OBS write saved controls when the camera opens, which about ten people across four upstream issues asked for. It did not say which camera the values were for, so it also wrote them to the wrong camera in two ordinary cases: a source switched to another camera, and cameras that swap /dev/videoN (obsproject/obs-studio#3003 is the upstream report of cameras swapping after a reboot). Before #364 OBS never wrote a control on its own, so neither case changed a camera. With this change the restore from #364 and the opt-out from #386 stay as they are, and the values only go back to the camera they came from.

How Has This Been Tested?

Automated, in both libobs lanes (ENABLE_RUST_LIBOBS OFF and ON), on current master:

  • test_v4l2_controls: 8 of 23 cases fail at the RED commit and 23 of 23 pass at GREEN, in each lane. ctest -V shows all 23 cases run, none skipped; the test also passes without a display and without xvfb.
  • The guards are checked by mutation, each making at least one case fail: writing the saved controls whatever the camera (6 cases), not putting controls aside in device_selected() (2), not bringing them back (1), no save callback (1), not tying old settings to the first camera (1), not using the camera found at startup in device_selected() (1).
  • Full ctest at GREEN, rebased on current master: test_v4l2_controls 23 of 23 in each lane; the full suite passes except master's test_video_io / test_video_io_hidden, which collide on Xvfb under parallel load and pass on their own (untouched here).
  • clang-format, gersemi and ci/test_registration.py are clean. No CMake change; the test is already registered and runs in the Tier 2 Linux job.

Not tested:

  • A real camera (none here). The fake reports card and bus_info the way uvcvideo does (usb-<controller>-<port>); two real cameras, a real reboot or replug, and the Qt Properties window were not tried.
  • Moving the same camera to another USB port changes its bus info, so OBS treats it as another camera: its saved controls are not written, and opening the Properties window on it puts them aside (they come back when it is on the old port again, or after setting them again). This is the price of telling two cameras of the same model apart without a serial number.
  • An existing setup whose cameras are already swapped when this version first starts ties the saved controls to the wrong camera, as today. It is corrected by picking the right camera, or by setting the controls again.

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 13, 2026 05:49
rdoupe and others added 2 commits October 12, 2026 22:52
Since obs-rust#364 the Video Capture Device (V4L2) source writes its saved
camera controls whenever the device opens. The controls are saved under
the driver's control names only, with nothing that says which camera
they were set on, so:
- switching a source from camera A to camera B (in the Properties
  window, or from a script or obs-websocket) writes A's exposure, focus,
  zoom or menu modes to B;
- when two cameras swap /dev/videoN nodes (reboot, replug, a USB hub
  that resets), OBS writes A's values to B at startup or on reconnect,
  with no user action.

The fake libv4l2 of test_v4l2_controls gains a second camera of the same
model on another USB port (own card/bus_info from VIDIOC_QUERYCAP and own
control values) and a second node, so the cameras can swap nodes. Nine
cases:
- a source switched to camera B writes nothing to it;
- A -> B -> A: A gets its values back, B is never written;
- the same switch in the Properties window: B is not written, the
  sliders stop showing A's values, and A's come back with A;
- a control changed on B after the switch is the only one restored on B;
- camera B taking the source's node on replug, or at startup, is not
  written;
- control: the same camera replugged is still restored;
- settings saved before this change are applied to the first camera
  opened and tied to it, so they do not follow the node to camera B,
  nor go to camera B when it is picked in the Properties window before
  the scene collection is saved.

8 of 23 cases fail; the 14 cases of obs-rust#364 and obs-rust#386 pass.

Refs obs-rust#459.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since obs-rust#364 the Video Capture Device (V4L2) source writes its saved
camera controls whenever the device opens. They are saved under the
driver's control names only, so a source switched from camera A to
camera B, or two cameras that swap /dev/videoN nodes after a reboot or a
replug, got A's exposure, focus, zoom or menu modes written to B.

The source now records which camera the saved controls belong to:
"controls_device" holds the card name and bus info from VIDIOC_QUERYCAP,
as in the device list, so two cameras of the same model on different
ports differ.
- The device opens (start, restart, replug): the saved controls are
  written only if they were set on that camera. Restore off is
  unchanged: nothing is written.
- Picking another camera in the Properties window puts the controls of
  the previous camera aside, under "controls_other_devices", and brings
  back those saved for the new one, so the sliders do not show another
  camera's values and switching back restores them. Nothing is written
  to the camera by this.
- Settings saved before this change have no camera: the controls belong
  to the first camera the source opens, as before, which is recorded
  when the source is saved (or when its properties are opened).

A camera that cannot be identified behaves as before.

Follow-up of obs-rust#363 / obs-rust#364.

Fixes obs-rust#459.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=87f2f59f1247ef4fbd7439302e624a32c4fe6d9f secs=256 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791870982,"gate":"rust/all/fmt","host":"linux-x86_64","key":"9535bc7082c3374893e25968a8f8f4b95e057f437a12ac6d12a699181f1b3d58","lane":"lint","parents":["2c216e7f448e92a9cd625e0aebc94519f3fb6153"],"secs":77,"stamp":"2f9cd4b02096f22f162ca4f463249809","tree":"87f2f59f1247ef4fbd7439302e624a32c4fe6d9f","v":1,"via":"run"}
Ci-Attestation: {"at":1791870982,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"9535bc7082c3374893e25968a8f8f4b95e057f437a12ac6d12a699181f1b3d58","lane":"lint","parents":["2c216e7f448e92a9cd625e0aebc94519f3fb6153"],"secs":77,"stamp":"7caaba97b5fde22529bdb9d1d6246c69","tree":"87f2f59f1247ef4fbd7439302e624a32c4fe6d9f","v":1,"via":"run"}
Ci-Attestation: {"at":1791870982,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"934a9e9013d6280946151b5007b005f3efd11f420d8fa285852b19bdc099f54d","lane":"rust-test","parents":["2c216e7f448e92a9cd625e0aebc94519f3fb6153"],"secs":115,"stamp":"8574802dd67be6f0be3835115d228972","tree":"87f2f59f1247ef4fbd7439302e624a32c4fe6d9f","v":1,"via":"run"}
Ci-Attestation: {"at":1791870982,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"653fe6b68ade50c85d12e620cd12b57e0ae6f1e95a37b6d5a2c94713cdeed602","lane":"tier2-linux","parents":["2c216e7f448e92a9cd625e0aebc94519f3fb6153"],"secs":64,"stamp":"a20fee5b21d1965f9cbc264e7302c9e0","tree":"87f2f59f1247ef4fbd7439302e624a32c4fe6d9f","v":1,"via":"run"}
@zackees

zackees commented Oct 13, 2026

Copy link
Copy Markdown
Member

Verified locally in the Tier 2 image (OFF and ON):

  • RED 72db9f2: test_v4l2_controls fails 5 new cases (old_settings_are_tied_to_the_first_camera_opened, old_settings_stay_with_the_first_camera_when_switching_in_properties, a_camera_that_takes_the_node_at_startup_is_not_written, a_camera_that_takes_the_node_on_replug_is_not_written, a_control_changed_on_the_new_camera_is_the_only_one_restored)
  • GREEN 9ec0218: 147/147
  • In the edits to the existing test file, the only removed lines are two refactored helpers; no assertions removed. Rebased on master, local gate passed; pushed as a08e650.

@zackees
zackees force-pushed the fix/v4l2-controls-device-identity branch from 9ec0218 to a08e650 Compare October 13, 2026 05:56
@zackees
zackees merged commit 0a171ab into obs-rust:master Oct 13, 2026
18 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: saved camera controls are written to the wrong camera after a device switch or /dev/videoN renumbering (follow-up of #363)

2 participants