Skip to content

feat: add camera controls for DirectShow and V4L2 - #185

Merged
kekyo merged 2 commits into
kekyo:developfrom
Roman3349:feature/camera-controls
Nov 6, 2026
Merged

kekyo merged 2 commits into
kekyo:developfrom
Roman3349:feature/camera-controls

Conversation

@Roman3349

Copy link
Copy Markdown
Contributor

Adds an API to read and set camera controls such as brightness, zoom, exposure, focus and white balance, including their automatic modes. It is implemented for DirectShow and V4L2, and the Avalonia, WPF and Windows Forms samples get a window with the controls.

Related: #14, #112, #118, #122, #64

API

// Controls the device offers, with their ranges in the native unit of the device
CaptureDeviceControl[] controls = await device.GetControlsAsync();

// Current value and whether the device sets it itself
CaptureDeviceControlValue? zoom = await device.GetControlValueAsync(CaptureDeviceControlTypes.Zoom);

// Set a value, or let the device set it itself
await device.SetControlValueAsync(CaptureDeviceControlTypes.Zoom, 200);
await device.SetControlValueAsync(CaptureDeviceControlTypes.Exposure, 0, isAuto: true);
  • CaptureDeviceControlTypes: Brightness, Contrast, Hue, Saturation, Sharpness, Gamma, WhiteBalance, BacklightCompensation, Gain, PowerLineFrequency, Pan, Tilt, Zoom, Exposure, Focus.
  • CaptureDeviceControl: Type, Minimum, Maximum, Step, DefaultValue, SupportsAuto.
  • CaptureDeviceControlValue: Value, IsAuto.
  • F# extensions: getControls, getControlValue (returns an option) and setControlValue.

The values are raw device values, so FlashCap does no unit conversion. For example, the exposure is the binary logarithm of seconds on DirectShow and the time in 100 µs units on V4L2. This is documented on the enum and in the README.

The calls follow the pattern of ShowPropertyPageAsync: a virtual On...Async method on CaptureDevice, called under the device lock, with extension methods in FlashCap.

Implementation

DirectShow

  • IAMVideoProcAmp (brightness ... gain) and IAMCameraControl (pan, tilt, zoom, exposure, focus) are queried from the capture source filter the device already holds in its graph.
  • Calls run in the device's apartment context.
  • Automatic mode uses the Auto / Manual flags.
  • PowerLineFrequency is not exposed by these interfaces, so it is not offered on DirectShow.

V4L2

  • VIDIOC_QUERYCTRL / VIDIOC_G_CTRL / VIDIOC_S_CTRL.
  • The ioctl numbers come from the generated per-architecture interop, which already contains them. v4l2_queryctrl and v4l2_control are declared by hand in NativeMethods_V4L2, because their layout is the same on all architectures. They are blittable classes, so the existing ioctl<T> can pin them.
  • While capturing, the open file descriptor is used. Otherwise the device is opened only for the call.
  • Automatic modes map to V4L2_CID_AUTO_WHITE_BALANCE, V4L2_CID_AUTOGAIN, V4L2_CID_HUE_AUTO, V4L2_CID_FOCUS_AUTO and V4L2_CID_EXPOSURE_AUTO.
  • For the exposure, the aperture priority mode is tried first and the full automatic mode second, since webcams differ in which one they support.
  • Setting a manual value first turns the automatic mode off, because the device ignores the value otherwise.

Other backends

Video for Windows, Media Foundation and AVFoundation return no controls. GetControlValueAsync returns null and SetControlValueAsync returns false for them, as for any control a device does not offer.

Samples

The Avalonia, WPF and Windows Forms samples have a "Camera controls" button which opens a window with the controls of the opened device:

  • a choice for the power line frequency (Disabled, 50 Hz, 60 Hz, Auto, limited to the range the device reports),
  • a check box for controls with the range 0..1, such as the backlight compensation,
  • a slider for the other controls,
  • an auto check box for controls with an automatic mode.

After each change the samples read the value back, since setting a value turns the automatic mode off. The Windows Forms window and its rows are made with the designer, only the rows for the device's controls are added in code.

The last commit fixes a typo in a comment of the Avalonia sample.

Testing

  • Builds for all target frameworks (net35 ... net10.0), the F# project and the samples without warnings.
  • FlashCap.Core.Tests pass.
  • Tested on a Logitech C922 Pro Stream Webcam, both while stopped and while capturing:
    • Linux (V4L2, x86_64, .NET 10): 13 controls listed. Brightness and zoom set and read back. Automatic exposure, focus and white balance switched on and off.
    • Windows 11 (DirectShow, .NET 10): 12 controls listed (no power line frequency), with the same results.
  • A control the device does not offer (gamma on the C922) returns null / false.
  • Samples on Windows 11 with the C922 (DirectShow):
    • Avalonia (.NET 10): brightness and white balance changed from the window.
    • WPF (.NET Framework 4.8): zoom changed from the window.
    • Windows Forms (.NET Framework 4.8): brightness and backlight compensation changed from the window.

Notes

#64 adds IAMCameraControl for DirectShow only. This PR covers both interfaces and V4L2 behind one API. I am happy to adjust the API or to build on #64 if you prefer.

@Roman3349 Roman3349 changed the title Feature/camera controls feat: add camera controls for DirectShow and V4L2 Nov 4, 2026
@Roman3349
Roman3349 force-pushed the feature/camera-controls branch from bbe124c to 3cc38d0 Compare November 4, 2026 22:43
@kekyo

kekyo commented Nov 4, 2026

Copy link
Copy Markdown
Owner

@Roman3349 Thank you - this is a great new feature!
I'll review it and then merge it.

@kekyo
kekyo self-requested a review November 6, 2026 06:23
@kekyo

kekyo commented Nov 6, 2026

Copy link
Copy Markdown
Owner

@Roman3349 Thanks for the PR!
I've also verified basic functionality on both Linux and Windows (Logitech C930e/C922). I'll merge it for now and make a few changes on my end.

@kekyo
kekyo merged commit a63ba63 into kekyo:develop Nov 6, 2026
@Roman3349
Roman3349 deleted the feature/camera-controls branch November 6, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants