1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
macro_rules! impl_enum_frameintervals {
() => {
fn enum_frameintervals(
&self,
fourcc: FourCC,
width: u32,
height: u32,
) -> (Vec<FrameInterval>, io::Result<()>) {
let mut frameintervals = Vec::new();
let mut v4l2_struct = v4l2_frmivalenum {
index: 0,
pixel_format: fourcc.into(),
width,
height,
..unsafe { mem::zeroed() }
};
loop {
let ret = unsafe {
v4l2::ioctl(
self.handle().fd(),
v4l2::vidioc::VIDIOC_ENUM_FRAMEINTERVALS,
&mut v4l2_struct as *mut _ as *mut std::os::raw::c_void,
)
};
if ret.is_err() {
return (
frameintervals,
if v4l2_struct.index == 0 {
Err(ret.err().unwrap())
} else {
Ok(())
}
)
}
if let Ok(frame_interval) = FrameInterval::try_from(v4l2_struct) {
frameintervals.push(frame_interval);
}
v4l2_struct.index += 1;
}
}
};
}
macro_rules! impl_enum_framesizes {
() => {
fn enum_framesizes(&self, fourcc: FourCC) -> (Vec<FrameSize>, io::Result<()>) {
let mut framesizes = Vec::new();
let mut v4l2_struct = v4l2_frmsizeenum {
index: 0,
pixel_format: fourcc.into(),
..unsafe { mem::zeroed() }
};
loop {
let ret = unsafe {
v4l2::ioctl(
self.handle().fd(),
v4l2::vidioc::VIDIOC_ENUM_FRAMESIZES,
&mut v4l2_struct as *mut _ as *mut std::os::raw::c_void,
)
};
if ret.is_err() {
return (
framesizes,
if v4l2_struct.index == 0 {
Err(ret.err().unwrap())
} else {
Ok(())
}
)
}
if let Ok(frame_size) = FrameSize::try_from(v4l2_struct) {
framesizes.push(frame_size);
}
v4l2_struct.index += 1;
}
}
};
}
macro_rules! impl_enum_formats {
($typ:expr) => {
fn enum_formats(&self) -> (Vec<FormatDescription>, io::Result<()>) {
let mut formats: Vec<FormatDescription> = Vec::new();
let mut v4l2_fmt = v4l2_fmtdesc {
index: 0,
type_: $typ as u32,
..unsafe { mem::zeroed() }
};
let mut ret: io::Result<()>;
unsafe {
ret = v4l2::ioctl(
self.handle().fd(),
v4l2::vidioc::VIDIOC_ENUM_FMT,
&mut v4l2_fmt as *mut _ as *mut std::os::raw::c_void,
);
}
if ret.is_err() {
// FIXME: when does this ever return an error?
// Enumerating the first format (at index 0) failed, so there are no formats available
// for this device. Just return an empty vec in this case.
return (Vec::new(), Ok(()))
}
while ret.is_ok() {
formats.push(FormatDescription::from(v4l2_fmt));
v4l2_fmt.index += 1;
unsafe {
v4l2_fmt.description = mem::zeroed();
}
unsafe {
ret = v4l2::ioctl(
self.handle().fd(),
v4l2::vidioc::VIDIOC_ENUM_FMT,
&mut v4l2_fmt as *mut _ as *mut std::os::raw::c_void,
);
}
}
(formats, Ok(()))
}
};
}
macro_rules! impl_format {
($typ:expr) => {
fn format(&self) -> io::Result<Format> {
unsafe {
let mut v4l2_fmt = v4l2_format {
type_: $typ as u32,
..mem::zeroed()
};
v4l2::ioctl(
self.handle().fd(),
v4l2::vidioc::VIDIOC_G_FMT,
&mut v4l2_fmt as *mut _ as *mut std::os::raw::c_void,
)?;
Ok(Format::from(v4l2_fmt.fmt.pix))
}
}
};
}
macro_rules! impl_set_format {
($typ:expr) => {
fn set_format(&self, fmt: &Format) -> io::Result<Format> {
unsafe {
let mut v4l2_fmt = v4l2_format {
type_: $typ as u32,
fmt: v4l2_format__bindgen_ty_1 { pix: (*fmt).into() },
};
v4l2::ioctl(
self.handle().fd(),
v4l2::vidioc::VIDIOC_S_FMT,
&mut v4l2_fmt as *mut _ as *mut std::os::raw::c_void,
)?;
}
self.format()
}
};
}