Skip to content

Commit

Permalink
Add serde option
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
  • Loading branch information
patrickelectric committed Jan 20, 2020
1 parent 11947bf commit 3c4d909
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ edition = "2018"
derivative = "1.0.3"
libc = "0.2"
thiserror = "1.0.9"
serde = { version = "1.0.104", features = ["derive"], optional = true }

[features]
no_wrapper = []
default = ["serde"]
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ mod v4l2;

pub type Result<T> = result::Result<T, Error>;

#[cfg(feature = "serde")]
extern crate serde;

#[macro_use]
extern crate derivative;

Expand All @@ -62,7 +65,8 @@ pub enum Error {
BadField,
}

#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Config<'a> {
/// The mix of numerator and denominator. v4l2 uses frame intervals instead of frame rates.
/// Default is `(1, 10)`.
Expand Down Expand Up @@ -94,6 +98,7 @@ impl<'a> Default for Config<'a> {
}
}

#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct FormatInfo {
/// FourCC of format (e.g. `b"H264"`).
pub format: [u8; 4],
Expand Down Expand Up @@ -143,6 +148,7 @@ impl fmt::Debug for FormatInfo {
}
}

#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum ResolutionInfo {
Discretes(Vec<(u32, u32)>),
Stepwise {
Expand Down Expand Up @@ -242,18 +248,21 @@ impl Drop for Frame {
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
enum State {
Idle,
Streaming,
Aborted,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Camera {
fd: RawFd,
state: State,
resolution: (u32, u32),
format: [u8; 4],
#[cfg_attr(feature = "serde", serde(skip_serializing))]
buffers: Vec<Arc<MappedRegion>>,
}

Expand Down Expand Up @@ -661,6 +670,7 @@ impl Drop for Camera {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct FormatIter<'a> {
camera: &'a Camera,
index: u32,
Expand Down Expand Up @@ -752,6 +762,7 @@ impl Settable for String {
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Control {
pub id: u32,
pub name: String,
Expand All @@ -761,6 +772,7 @@ pub struct Control {
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub enum CtrlData {
Integer {
value: i32,
Expand Down Expand Up @@ -807,12 +819,14 @@ pub enum CtrlData {
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CtrlMenuItem {
pub index: u32,
pub name: String,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct CtrlIntMenuItem {
pub index: u32,
pub value: i64,
Expand Down
21 changes: 20 additions & 1 deletion src/v4l2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ impl Format {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct PixFormat {
pub width: u32,
Expand Down Expand Up @@ -193,6 +194,7 @@ impl PixFormat {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct RequestBuffers {
pub count: u32,
Expand Down Expand Up @@ -242,6 +244,7 @@ impl Buffer {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct TimeCode {
pub ttype: u32,
Expand All @@ -254,6 +257,7 @@ pub struct TimeCode {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct FmtDesc {
pub index: u32,
Expand Down Expand Up @@ -293,6 +297,7 @@ impl StreamParm {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct CaptureParm {
pub capability: u32,
Expand All @@ -304,13 +309,15 @@ pub struct CaptureParm {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct Fract {
pub numerator: u32,
pub denominator: u32,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct Frmsizeenum {
pub index: u32,
Expand All @@ -337,13 +344,15 @@ impl Frmsizeenum {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct FrmsizeDiscrete {
pub width: u32,
pub height: u32,
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct FrmsizeStepwise {
pub min_width: u32,
Expand All @@ -355,6 +364,7 @@ pub struct FrmsizeStepwise {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct Frmivalenum {
pub index: u32,
Expand Down Expand Up @@ -385,6 +395,7 @@ impl Frmivalenum {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct FrmivalStepwise {
pub min: Fract,
Expand All @@ -393,6 +404,7 @@ pub struct FrmivalStepwise {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct QueryCtrl {
pub id: u32,
Expand All @@ -415,6 +427,7 @@ impl QueryCtrl {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct QueryExtCtrl {
pub id: u32,
Expand Down Expand Up @@ -442,11 +455,14 @@ impl QueryExtCtrl {
}
}

#[derive(Debug, Copy, Clone)]
#[derive(Derivative, Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C, packed)]
pub struct QueryMenu {
pub id: u32,
pub index: u32,
#[derivative(Debug="ignore")]
#[serde(skip_serializing)]
pub data: QueryMenuData,
reserved: u32,
}
Expand Down Expand Up @@ -483,6 +499,7 @@ impl QueryMenuData {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct Control {
pub id: u32,
Expand All @@ -496,6 +513,7 @@ impl Control {
}

#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C, packed)]
pub struct ExtControl {
pub id: u32,
Expand All @@ -516,6 +534,7 @@ impl ExtControl {
}

#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
pub struct ExtControls<'a> {
pub ctrl_class: u32,
Expand Down

0 comments on commit 3c4d909

Please sign in to comment.