Skip to content

Commit

Permalink
make control api simpler, because most platforms arnt v4l2
Browse files Browse the repository at this point in the history
  • Loading branch information
l1npengtul committed Dec 5, 2024
1 parent 5d79e46 commit 0646b0a
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 715 deletions.
22 changes: 2 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository = "https://github.com/l1npengtul/nokhwa"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[workspace]
members = ["nokhwa-bindings-macos", "nokhwa-bindings-windows", "nokhwa-bindings-linux", "nokhwa-core", "examples/*", "nokhwa-decoder"]
members = ["nokhwa-bindings-macos", "nokhwa-bindings-windows", "nokhwa-bindings-linux", "nokhwa-core", "examples/*"]
exclude = ["examples/jscam"]

[lib]
Expand Down
18 changes: 9 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions nokhwa-bindings-macos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ mod internal {
ffi::{c_float, c_void, CStr},
sync::Arc,
};
use nokhwa_core::properties::{CameraControl, ControlValueDescription, ControlValueSetter, KnownCameraControl};
use nokhwa_core::properties::{CameraControl, ControlValueDescription, ControlValue, KnownCameraControl};

const UTF8_ENCODING: usize = 4;
type CGFloat = c_float;
Expand Down Expand Up @@ -1492,7 +1492,7 @@ mod internal {
pub fn set_control(
&mut self,
id: KnownCameraControl,
value: ControlValueSetter,
value: ControlValue,
) -> Result<(), NokhwaError> {
let rc = self.get_controls()?;
let controls = rc
Expand Down
12 changes: 6 additions & 6 deletions nokhwa-bindings-windows/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub mod wmf {
Arc,
},
};
use nokhwa_core::properties::{CameraControl, ControlValueDescription, ControlValueSetter, KnownCameraControl};
use nokhwa_core::properties::{CameraControl, ControlValueDescription, ControlValue, KnownCameraControl};
use windows::Win32::Media::DirectShow::{CameraControl_Flags_Auto, CameraControl_Flags_Manual};
use windows::Win32::Media::MediaFoundation::{
IMFMediaType, MFCreateSample, MF_SOURCE_READER_FIRST_VIDEO_STREAM,
Expand Down Expand Up @@ -849,7 +849,7 @@ pub mod wmf {
pub fn set_control(
&mut self,
control: KnownCameraControl,
value: ControlValueSetter,
value: ControlValue,
) -> Result<(), NokhwaError> {
let current_value = self.control(control)?;

Expand Down Expand Up @@ -897,8 +897,8 @@ pub mod wmf {
})?;

let ctrl_value = match value {
ControlValueSetter::Integer(i) => i as i32,
ControlValueSetter::Boolean(b) => i32::from(b),
ControlValue::Integer(i) => i as i32,
ControlValue::Boolean(b) => i32::from(b),
v => {
return Err(NokhwaError::StructureError {
structure: format!("ControlValueSetter {}", v),
Expand Down Expand Up @@ -1229,7 +1229,7 @@ pub mod wmf {
CameraFormat, CameraIndex, CameraInfo,
};
use std::borrow::Cow;
use nokhwa_core::properties::{CameraControl, ControlValueSetter, KnownCameraControl};
use nokhwa_core::properties::{CameraControl, ControlValue, KnownCameraControl};

pub fn initialize_mf() -> Result<(), NokhwaError> {
Err(NokhwaError::NotImplementedError(
Expand Down Expand Up @@ -1287,7 +1287,7 @@ pub mod wmf {
pub fn set_control(
&mut self,
_control: KnownCameraControl,
_value: ControlValueSetter,
_value: ControlValue,
) -> Result<(), NokhwaError> {
Err(NokhwaError::NotImplementedError(
"Only on Windows".to_string(),
Expand Down
1 change: 1 addition & 0 deletions nokhwa-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ thiserror = "2.0"
bytes = "1.3"
paste = "1.0"
flume = "0.11"
num-traits = "0.2"

[dependencies.num-rational]
version = "0.4"
Expand Down
1 change: 0 additions & 1 deletion nokhwa-core/src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::error::{NokhwaError, NokhwaResult};
use crate::frame_buffer::FrameBuffer;
use crate::frame_format::FrameFormat;
use crate::properties::{CameraProperties, CameraPropertyId, CameraPropertyValue};
use crate::types::{CameraFormat, CameraIndex, FrameRate, Resolution};
Expand Down
2 changes: 0 additions & 2 deletions nokhwa-core/src/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{error::NokhwaError, frame_buffer::FrameBuffer, frame_format::FrameFormat};
use image::{ImageBuffer, Pixel};
use std::{
error::Error,
fmt::{Debug, Display},
ops::{ControlFlow, Deref},
};

Expand Down
32 changes: 19 additions & 13 deletions nokhwa-core/src/format_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
types::{CameraFormat, FrameRate, Resolution},
};
use std::cmp::Ordering;
use crate::ranges::ValidatableRange;

#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
enum ClosestType {
Expand Down Expand Up @@ -60,24 +61,29 @@ impl FormatRequest {
frame_rate,
frame_format,
} => {
let resolution_point = resolution.map(|x| x.preferred())?;

let frame_rate_point = frame_rate.map(|x| x.preferred())?;
let resolution_point = resolution.map(|x| x.preferred());
let frame_rate_point = frame_rate.map(|x| x.preferred());
// lets calcuate distance in 3 dimensions (add both resolution and frame_rate together)

let mut distances: Vec<(f32, CameraFormat)> = list_of_formats
let mut distances = list_of_formats
.iter()
.filter(|x| frame_format.contains(&x.format()))
.map(|fmt| {
(
(fmt.frame_rate() - frame_rate_point).abs()
+ fmt.resolution().distance_from(&resolution_point) as f32,
fmt,
)
let frame_rate_distance = match frame_rate_point {
Some(f_point) => (fmt.frame_rate() - f_point).approximate_float().unwrap_or(f32::INFINITY).abs(),
None => 0_f32,
};

let resolution_point_distance = match resolution_point {
Some(res_pt) => fmt.resolution().distance_from(&res_pt) as f32,
None => 0_f32,
};

(frame_rate_distance + resolution_point_distance, fmt)
})
.collect::<Vec<_>>();
.collect::<Vec<(f32, &CameraFormat)>>();
distances.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal));
distances.into_iter().map(|x| x.1).collect()
distances.into_iter().map(|x| x.1).copied().collect()
}
FormatRequest::HighestFrameRate {
frame_rate,
Expand All @@ -86,7 +92,7 @@ impl FormatRequest {
let mut formats = list_of_formats
.iter()
.filter(|x| {
frame_format.contains(&x.format()) && frame_rate.in_range(x.frame_rate())
frame_format.contains(&x.format()) && frame_rate.validate(&x.frame_rate()).is_ok()
})
.collect::<Vec<_>>();
formats.sort();
Expand All @@ -99,7 +105,7 @@ impl FormatRequest {
let mut formats = list_of_formats
.iter()
.filter(|x| {
frame_format.contains(&x.format()) && resolution.in_range(x.resolution())
frame_format.contains(&x.format()) && resolution.validate(&x.resolution()).is_ok()
})
.collect::<Vec<_>>();
formats.sort();
Expand Down
Loading

0 comments on commit 0646b0a

Please sign in to comment.