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
macro_rules! impl_enum_frameintervals {
    () => {
        fn enum_frameintervals(
            &self,
            fourcc: FourCC,
            width: u32,
            height: u32,
        ) -> io::Result<Vec<FrameInterval>> {
            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() {
                    if v4l2_struct.index == 0 {
                        return Err(ret.err().unwrap());
                    } else {
                        return Ok(frameintervals);
                    }
                }

                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) -> io::Result<Vec<FrameSize>> {
            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() {
                    if v4l2_struct.index == 0 {
                        return Err(ret.err().unwrap());
                    } else {
                        return Ok(framesizes);
                    }
                }

                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) -> io::Result<Vec<FormatDescription>> {
            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() {
                // 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 Ok(Vec::new());
            }

            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,
                    );
                }
            }

            Ok(formats)
        }
    };
}

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()
        }
    };
}