Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid extra ioctl by allowing control query by description #62

Merged
merged 6 commits into from
Nov 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 10 additions & 23 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use std::{io, mem};

use libc;

use crate::control;
use crate::capability::Capabilities;
use crate::control::{self, Control, Description};
use crate::v4l2;
use crate::v4l2::videodev::v4l2_ext_controls;
use crate::v4l_sys::*;
use crate::{capability::Capabilities, control::Control};

/// Linux capture device abstraction
pub struct Device {
Expand Down Expand Up @@ -92,7 +92,7 @@ impl Device {
}

/// Returns the supported controls for a device such as gain, focus, white balance, etc.
pub fn query_controls(&self) -> io::Result<Vec<control::Description>> {
donkeyteethUX marked this conversation as resolved.
Show resolved Hide resolved
pub fn query_controls(&self) -> io::Result<Vec<Description>> {
let mut controls = Vec::new();
unsafe {
let mut v4l2_ctrl: v4l2_query_ext_ctrl = mem::zeroed();
Expand All @@ -107,7 +107,7 @@ impl Device {
) {
Ok(_) => {
// get the basic control information
let mut control = control::Description::from(v4l2_ctrl);
let mut control = Description::from(v4l2_ctrl);

// if this is a menu control, enumerate its items
if control.typ == control::Type::Menu
Expand Down Expand Up @@ -167,29 +167,16 @@ impl Device {
Ok(controls)
}

/// Returns the control value for an ID
/// Returns the current control value from its [`Description`]
///
/// # Arguments
///
/// * `id` - Control identifier
pub fn control(&self, id: u32) -> io::Result<Control> {
/// * `desc` - Control description
pub fn control(&self, desc: &Description) -> io::Result<Control> {
unsafe {
let mut queryctrl = v4l2_query_ext_ctrl {
id,
..mem::zeroed()
};
v4l2::ioctl(
self.handle().fd(),
v4l2::vidioc::VIDIOC_QUERY_EXT_CTRL,
&mut queryctrl as *mut _ as *mut std::os::raw::c_void,
)?;

// determine the control type
let description = control::Description::from(queryctrl);

// query the actual control value
let mut v4l2_ctrl = v4l2_ext_control {
id,
id: desc.id,
..mem::zeroed()
};
let mut v4l2_ctrls = v4l2_ext_controls {
Expand All @@ -203,7 +190,7 @@ impl Device {
&mut v4l2_ctrls as *mut _ as *mut std::os::raw::c_void,
)?;

let value = match description.typ {
let value = match desc.typ {
control::Type::Integer64 => {
control::Value::Integer(v4l2_ctrl.__bindgen_anon_1.value64)
}
Expand All @@ -221,7 +208,7 @@ impl Device {
}
};

Ok(Control { id, value })
Ok(Control { id: desc.id, value })
}
}

Expand Down
Loading