Skip to content

linux-v4l2: Apply saved camera controls when the device opens - #364

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:fix/11633-v4l2-controls
Oct 12, 2026
Merged

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

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

Description

The saved camera controls of a Video Capture Device (V4L2), such as zoom, focus, exposure and brightness, are now written to the camera when OBS starts, when the camera is replugged or comes back from suspend, and when a script or obs-websocket changes them.

  • The plugin wrote a control only from the property's modified callback (v4l2_control_changed()), that is, when the user moved the slider in the Properties window. v4l2_create(), v4l2_update() and the reconnect path (device_added() -> v4l2_init()) never wrote them, so the camera ran on its driver's values while the Properties window showed the saved ones.
  • The fix ports v4l2_apply_controls() from GrantAbell's Fix V4L2 nonui apply obsproject/obs-studio#13279. It walks the device's controls and writes those that have a user value in the settings and differ from the device's current value (VIDIOC_G_CTRL). Controls the user never set in OBS are left alone, so values set with v4l2-ctl or guvcview survive.
  • Changes to that PR, each with a case that fails against it as is:
    • it is called in v4l2_init() on the capture's descriptor, so it also runs on reconnect, which does not go through v4l2_update(); updates that keep the capture running apply it on the same descriptor. The PR opened a second descriptor without O_CLOEXEC (bug: linux-v4l2: a camera stays busy while recording because the muxer inherits its fd (upstream #12508) #339) on every update;
    • write-only controls are skipped: a relative control ("Pan, Relative", "Focus, Relative"...) moves the camera each time it is written, and the PR replayed its last value on every load and settings update;
    • inactive or refused controls are retried once after the others: uvcvideo lists "Focus (absolute)" before "Focus, Automatic Continuous", and cameras refuse a manual focus while autofocus is on, so a saved manual focus was never applied.
  • The source keeps a copy of its settings for the reconnect, which runs on the udev thread.
  1. test/cmocka: Add RED V4L2 saved camera controls tests: test_v4l2_controls runs the real v4l2-input.c and v4l2-controls.c against a fake libv4l2 whose controls behave like a UVC webcam's and which records every control write. The replug goes through the plugin's own udev signal handlers. It needs no camera, kernel module or display. The cases are:

    • saved controls (integer, menu) are on the camera after the source is created;
    • they are on it again after a replug that resets the camera to its driver's values;
    • a control changed by obs_source_update() (what SetInputSettings does) reaches the camera without restarting the capture;
    • a saved manual focus is applied after autofocus is turned off, with a driver that flags it inactive and with one that only refuses it;
    • control: a camera that already has the saved values is not written to, and controls without a saved value are never written;
    • a relative control is not replayed on load, update or replug;
    • building the properties writes nothing, and changing Brightness writes Brightness only, leaving a zoom set outside OBS alone.

    5 of 8 fail (the last 3 guard the fix). Fix V4L2 nonui apply obsproject/obs-studio#13279 as is fails 4 of 8: the replug, both manual focus cases and the relative control.

  2. linux-v4l2: Apply saved camera controls when the device opens: 8 of 8 pass.

Motivation and Context

Fixes #363 (upstream obsproject/obs-studio#11633, reported by vpzomtrrfrt). The same problem was reported in obsproject/obs-studio#3092 (ifohancroft; thadeupenna, EduardoRFS and Staacks there too), obsproject/obs-studio#3865 (janakelarsson) and obsproject/obs-studio#5779 (desmondw, Pablomx2), all closed without a fix, and on the OBS forum. In obsproject/obs-studio#3092, kkartaltepe said OBS should not show values that conflict with the device; with this change the camera has the values the Properties window shows.

The fix is GrantAbell's obsproject/obs-studio#13279 (open since 2026-04, no review), credited as co-author, with the changes above.

The second half of obsproject/obs-studio#3092 (opening the properties writes the driver's defaults over values set with v4l2-ctl) does not happen on current master: obs_properties_apply_settings() collects the modified callbacks before device_selected() builds the controls, so none of them runs. The last case keeps it that way.

Why this one: about ten people across four upstream issues since 2020, plus forum threads, hit this with webcams and HDMI grabbers. Today the only workaround is to touch every slider after each start or replug, or to run a separate tool.

How Has This Been Tested?

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

  • test_v4l2_controls: 5 of 8 cases fail at the RED commit and 8 of 8 pass at GREEN. The test is deterministic. ctest -V shows every case run, none skipped, without a display and without xvfb. Disabling the retry pass makes both manual focus cases fail again.
  • The existing V4L2 tests (test_v4l2_fd_cloexec, test_v4l2_corrupt_frame, test_v4l2_framerate, test_v4l2_input_caps) pass.
  • Full ctest at GREEN: 119 of 119 in each lane (rebased on current master).
  • clang-format, gersemi and ci/test_registration.py are clean. The test is Linux only, like the other V4L2 tests, and needs nothing beyond what they need (libv4l2 headers, FFmpeg); it is built and run in the Tier 2 Linux job.

Not tested:

  • A real camera (none here). The fake follows uvcvideo's control order, flags and write-only relative controls.
  • HDMI capture cards. A Windows change that restored DirectShow device settings (win-dshow: Save and restore video device configuration properties obsproject/obs-studio#5480) was reverted in OBS 28.0.1 because it caused issues with some capture cards. This change only writes controls the user set in OBS and that differ from the device, and a refused write is only logged.

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

The camera controls of a Video Capture Device (zoom, focus, brightness,
power line frequency...) are saved with the source but never written to
the camera again: after OBS starts, after the camera is replugged or
comes back from suspend, and after a script or obs-websocket changes them
with SetInputSettings, the camera runs on the driver's values until the
user moves the slider in the Properties window
(obsproject/obs-studio#11633, reported by vpzomtrrfrt, and the closed
obsproject/obs-studio#3092, obs-rust#3865 and obs-rust#5779).

The plugin only writes a control from the property's modified callback
(v4l2_control_changed() in v4l2-controls.c), which runs when the user
changes it in the Properties window. v4l2_create(), v4l2_update() and the
hotplug path (device_added() -> v4l2_init()) never write them.

The test runs the real v4l2-input.c and v4l2-controls.c against a fake
libv4l2 whose controls behave like a UVC webcam's (the manual focus is
enumerated before its automatic switch and refused while autofocus is
on, a relative pan control is write-only) and records every write;
hotplug goes through the plugin's own udev signal handlers. No camera,
kernel module or display is needed.

5 of 8 cases fail: saved controls are not applied when the source is
created, after a replug, after an update from a script, and a saved
manual focus is not applied, whether the driver flags it inactive while
autofocus is on or only refuses it. The other 3 guard the fix: unchanged
controls and controls without a saved value are not written, a relative
control is not replayed, and opening the properties writes no defaults.

Refs obs-rust#363.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@rdoupe
rdoupe requested a review from zackees as a code owner October 12, 2026 12:01
The camera controls of a Video Capture Device were saved with the source
but only written to the camera from the property's modified callback,
when the user changed them in the Properties window. After OBS started,
after the camera was replugged or came back from suspend, and after a
script or obs-websocket changed them with SetInputSettings, the camera
ran on the driver's values (obsproject/obs-studio#11633, reported by
vpzomtrrfrt, and the closed obsproject/obs-studio#3092, obs-rust#3865 and
obs-rust#5779).

Add v4l2_apply_controls(), from GrantAbell's
obsproject/obs-studio#13279: it walks the device's controls and writes
those with a user value in the settings whose current value differs.
Controls the user never set are left alone, so values set with v4l2-ctl
or guvcview survive. On top of that PR:

- Apply in v4l2_init(), on the capture's descriptor, so the controls are
  written on start, after a settings change that restarts the capture
  and on reconnect (device_added() calls v4l2_init(), not
  v4l2_update()); updates that keep the capture running apply them on
  the same descriptor. The PR opened a second descriptor without
  O_CLOEXEC on every update and missed the reconnect.
- Skip write-only controls. A relative control (Pan, Relative...) moves
  the camera each time it is written and cannot be read back, so the PR
  replayed its last value on every load and settings update.
- Retry inactive and refused controls after the others. uvcvideo lists
  Focus (absolute) before Focus, Automatic Continuous, and a camera
  refuses a manual focus while autofocus is on, so a saved manual focus
  was never applied.
- Keep a copy of the settings for the reconnect, which runs on the udev
  thread.

With the fix (both ENABLE_RUST_LIBOBS lanes), 8 of 8 cases pass (3 of 8
before, 4 of 8 with obsproject/obs-studio#13279 as is).

Fixes obs-rust#363.

Co-Authored-By: Grant Abell <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=0aafdf544d50286b9bfc8a200d0e97f673de08df secs=96 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791806800,"gate":"rust/all/fmt","host":"linux-x86_64","key":"52359a054b91579fcc92a8c1fd3cc5cc82518a5fa423216de0d8346945824a10","lane":"lint","parents":["da83909e9abb271e0adfd9f72f9439cee7a1cd1b"],"secs":11,"stamp":"87a775d11b2770a2e70d86a4b4d4a765","tree":"0aafdf544d50286b9bfc8a200d0e97f673de08df","v":1,"via":"run"}
Ci-Attestation: {"at":1791806800,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"52359a054b91579fcc92a8c1fd3cc5cc82518a5fa423216de0d8346945824a10","lane":"lint","parents":["da83909e9abb271e0adfd9f72f9439cee7a1cd1b"],"secs":11,"stamp":"683b86dbf6f7c9a23e83b910f86861f0","tree":"0aafdf544d50286b9bfc8a200d0e97f673de08df","v":1,"via":"run"}
Ci-Attestation: {"at":1791806800,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"4e4f9ccfb279fb590c4f0574ef9e27984da589d0ee863d29d91f0b29f059ab81","lane":"rust-test","parents":["da83909e9abb271e0adfd9f72f9439cee7a1cd1b"],"secs":40,"stamp":"fe5be8eecccfab706267bd1c91b45573","tree":"0aafdf544d50286b9bfc8a200d0e97f673de08df","v":1,"via":"run"}
Ci-Attestation: {"at":1791806800,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"9f5fcf3d2ea4ac58fe7e8e1c7b208b2c61d23ba6e5299f62aa6d5ad40d8f1d92","lane":"tier2-linux","parents":["da83909e9abb271e0adfd9f72f9439cee7a1cd1b"],"secs":46,"stamp":"d94c105ed870ed1b4eced8a7a4297577","tree":"0aafdf544d50286b9bfc8a200d0e97f673de08df","v":1,"via":"run"}
@zackees

zackees commented Oct 12, 2026

Copy link
Copy Markdown
Member

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

  • RED da83909: test_v4l2_controls fails 5 cases (saved_controls_are_applied_when_the_source_is_created, saved_controls_are_applied_again_after_a_replug, a_control_set_from_a_script_is_applied, manual_focus_is_applied_after_autofocus_is_turned_off, manual_focus_is_applied_when_the_driver_only_refuses_it)
  • GREEN 5c4811d: 120/120
  • Rebased on master, local gate passed; pushed as f76ce49.

@zackees
zackees force-pushed the fix/11633-v4l2-controls branch from 5c4811d to f76ce49 Compare October 12, 2026 12:06
@zackees
zackees merged commit 20983a3 into obs-rust:master Oct 12, 2026
23 checks passed
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 12, 2026
Since obs-rust#364 the Video Capture Device (V4L2) source writes its saved
camera controls whenever the device opens (OBS starts, the source
restarts, the camera is replugged) and on every settings update. With an
external PTZ controller, this puts the pan, tilt or zoom last set in OBS
back over the controller's on every start and reconnect (raised by
alerque on obs-rust#363).

Add "Restore camera controls when the camera connects" as the first item
of the Camera Controls group in the source's properties, with a tooltip
that names external PTZ controllers. It is on by default, which keeps
obs-rust#364's behaviour. Off, v4l2_init() and v4l2_update() write no saved
control, as before obs-rust#364: a control is written only when the user changes
it in the Properties window (v4l2_control_changed(), unchanged).

Ticking the option writes nothing by itself: the saved controls are
written the next time the device opens or the settings change.

Fixes obs-rust#385.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=74ceb9ced3a280927c0daea8659b31275a72e616 secs=100 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791820410,"gate":"rust/all/fmt","host":"linux-x86_64","key":"41b971c85d446edeea0a3a45563d800e91be829cbc9ecc5e55e7ead0a8abf542","lane":"lint","parents":["8c7d229d74baa49d32e0668f6911233c8b829117"],"secs":14,"stamp":"4ac26e8605dbfd86713f6160fe230408","tree":"74ceb9ced3a280927c0daea8659b31275a72e616","v":1,"via":"run"}
Ci-Attestation: {"at":1791820410,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"41b971c85d446edeea0a3a45563d800e91be829cbc9ecc5e55e7ead0a8abf542","lane":"lint","parents":["8c7d229d74baa49d32e0668f6911233c8b829117"],"secs":14,"stamp":"a53fa52032ba18e279a853ade09bc785","tree":"74ceb9ced3a280927c0daea8659b31275a72e616","v":1,"via":"run"}
Ci-Attestation: {"at":1791820410,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"f8361c033af5335d4c5c74e844ca019c836f5e124c766c4695b13e5826f8e24c","lane":"rust-test","parents":["8c7d229d74baa49d32e0668f6911233c8b829117"],"secs":40,"stamp":"476727479aab24b6ca7fb72448d020be","tree":"74ceb9ced3a280927c0daea8659b31275a72e616","v":1,"via":"run"}
Ci-Attestation: {"at":1791820410,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"4707aa46545efc1d54aebe2d6a51dc77248d6fff24135f5c30b3fd1ddb3ad82d","lane":"tier2-linux","parents":["8c7d229d74baa49d32e0668f6911233c8b829117"],"secs":47,"stamp":"6244a7ac3506901f5aa80701413f3ee7","tree":"74ceb9ced3a280927c0daea8659b31275a72e616","v":1,"via":"run"}
zackees pushed a commit that referenced this pull request Oct 12, 2026
…386)

* test/cmocka: Add RED V4L2 restore camera controls option tests

Since #364 the Video Capture Device (V4L2) source writes its saved camera
controls whenever the device opens (OBS starts, the source restarts, the
camera is replugged) and on every settings update. With an external PTZ
controller this puts the pan, tilt or zoom last set in OBS back over the
controller's on every start and reconnect (raised by alerque on #363).

The source needs a per-source option, "Restore camera controls when the
camera connects", next to the camera controls in its properties. On (the
default) keeps #364's behaviour. Off is the behaviour before #364: a
control is written only when the user changes it in the Properties
window.

test_v4l2_controls now creates its sources with the source's defaults,
as OBS does, and gains six cases:
- the option exists, is on by default, has a tooltip, and is in the
  Camera Controls group, also once the group is rebuilt for the device;
- off: no saved control is written when the source is created, when the
  camera is replugged, or when a script updates another setting after
  an external controller moved the zoom;
- off: a control changed in the Properties window is still written, and
  only that one;
- ticking or unticking the checkbox writes no control.

6 of 14 cases fail; the 8 cases of #364 still pass with the default.

Refs #385.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* linux-v4l2: Add an option to not restore camera controls on connect

Since #364 the Video Capture Device (V4L2) source writes its saved
camera controls whenever the device opens (OBS starts, the source
restarts, the camera is replugged) and on every settings update. With an
external PTZ controller, this puts the pan, tilt or zoom last set in OBS
back over the controller's on every start and reconnect (raised by
alerque on #363).

Add "Restore camera controls when the camera connects" as the first item
of the Camera Controls group in the source's properties, with a tooltip
that names external PTZ controllers. It is on by default, which keeps
#364's behaviour. Off, v4l2_init() and v4l2_update() write no saved
control, as before #364: a control is written only when the user changes
it in the Properties window (v4l2_control_changed(), unchanged).

Ticking the option writes nothing by itself: the saved controls are
written the next time the device opens or the settings change.

Fixes #385.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Local-Gate: v1 tree=74ceb9ced3a280927c0daea8659b31275a72e616 secs=100 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791820410,"gate":"rust/all/fmt","host":"linux-x86_64","key":"41b971c85d446edeea0a3a45563d800e91be829cbc9ecc5e55e7ead0a8abf542","lane":"lint","parents":["8c7d229d74baa49d32e0668f6911233c8b829117"],"secs":14,"stamp":"4ac26e8605dbfd86713f6160fe230408","tree":"74ceb9ced3a280927c0daea8659b31275a72e616","v":1,"via":"run"}
Ci-Attestation: {"at":1791820410,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"41b971c85d446edeea0a3a45563d800e91be829cbc9ecc5e55e7ead0a8abf542","lane":"lint","parents":["8c7d229d74baa49d32e0668f6911233c8b829117"],"secs":14,"stamp":"a53fa52032ba18e279a853ade09bc785","tree":"74ceb9ced3a280927c0daea8659b31275a72e616","v":1,"via":"run"}
Ci-Attestation: {"at":1791820410,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"f8361c033af5335d4c5c74e844ca019c836f5e124c766c4695b13e5826f8e24c","lane":"rust-test","parents":["8c7d229d74baa49d32e0668f6911233c8b829117"],"secs":40,"stamp":"476727479aab24b6ca7fb72448d020be","tree":"74ceb9ced3a280927c0daea8659b31275a72e616","v":1,"via":"run"}
Ci-Attestation: {"at":1791820410,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"4707aa46545efc1d54aebe2d6a51dc77248d6fff24135f5c30b3fd1ddb3ad82d","lane":"tier2-linux","parents":["8c7d229d74baa49d32e0668f6911233c8b829117"],"secs":47,"stamp":"6244a7ac3506901f5aa80701413f3ee7","tree":"74ceb9ced3a280927c0daea8659b31275a72e616","v":1,"via":"run"}

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 13, 2026
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]>
zackees pushed a commit to rdoupe/obs-studio that referenced this pull request Oct 13, 2026
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 pushed a commit that referenced this pull request Oct 13, 2026
* test/cmocka: Add RED V4L2 camera controls device identity tests

Since #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 #364 and #386 pass.

Refs #459.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* linux-v4l2: Restore saved camera controls only on their camera

Since #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 #363 / #364.

Fixes #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"}

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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 (zoom, focus, exposure) are not applied at start, after a replug or from scripts (upstream #11633)

2 participants