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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#[cfg(feature = "input_uvc")]
use crate::backends::capture::UVCCaptureDevice;
#[cfg(feature = "input_v4l")]
use crate::backends::capture::V4LCaptureDevice;
use crate::{
    CameraFormat, CameraInfo, CaptureAPIBackend, CaptureBackendTrait, FrameFormat, NokhwaError,
    Resolution,
};
use image::{ImageBuffer, Rgb};
use std::{cell::RefCell, collections::HashMap};

/// The main `Camera` struct. This is the struct that abstracts over all the backends, providing a simplified interface for use.
/// For more details, please refer to [`CaptureBackendTrait`]
pub struct Camera {
    idx: usize,
    backend: RefCell<Box<dyn CaptureBackendTrait>>,
    backend_api: CaptureAPIBackend,
}

impl Camera {
    /// Create a new camera from an `index`, `format`, and `backend`. `format` can be `None`.
    /// # Errors
    /// This will error if you either have a bad platform configuration (e.g. `input_v4l` but not on linux) or the backend cannot create the camera (e.g. permission denied).
    pub fn new(
        index: usize,
        format: Option<CameraFormat>,
        backend: CaptureAPIBackend,
    ) -> Result<Self, NokhwaError> {
        let platform = std::env::consts::OS;
        let use_backend = match backend {
            CaptureAPIBackend::AUTO => {
                let mut cap = CaptureAPIBackend::AUTO;
                if cfg!(feature = "input_v4l") && platform == "linux" {
                    cap = CaptureAPIBackend::V4L2
                } else if cfg!(feature = "input_uvc") {
                    cap = CaptureAPIBackend::UVC;
                }
                if cap == CaptureAPIBackend::AUTO {
                    return Err(NokhwaError::NotImplemented(
                        "Platform requirements not satisfied.".to_string(),
                    ));
                }
                cap
            }
            CaptureAPIBackend::V4L2 => {
                if !(cfg!(feature = "input_v4l") && platform == "linux") {
                    return Err(NokhwaError::NotImplemented(
                        "V4L Requirements: Linux and `input_v4l`.".to_string(),
                    ));
                }
                CaptureAPIBackend::V4L2
            }
            CaptureAPIBackend::UVC => {
                if !(cfg!(feature = "input_uvc")) {
                    return Err(NokhwaError::NotImplemented(
                        "UVC Requirements: `input_uvc`.".to_string(),
                    ));
                }
                CaptureAPIBackend::UVC
            }
            _ => return Err(NokhwaError::NotImplemented(backend.to_string())),
        };

        let capture_backend = match use_backend {
            CaptureAPIBackend::V4L2 => match init_v4l(index, format) {
                Some(capture) => match capture {
                    Ok(cap_back) => cap_back,
                    Err(why) => return Err(why),
                },
                None => {
                    return Err(NokhwaError::NotImplemented(
                        "Platform requirements not satisfied.".to_string(),
                    ));
                }
            },
            CaptureAPIBackend::UVC => match init_uvc(index, format) {
                Some(capture) => match capture {
                    Ok(cap_back) => cap_back,
                    Err(why) => return Err(why),
                },
                None => {
                    return Err(NokhwaError::NotImplemented(
                        "Platform requirements not satisfied.".to_string(),
                    ));
                }
            },
            _ => {
                return Err(NokhwaError::NotImplemented(
                    "Platform requirements not satisfied.".to_string(),
                ));
            }
        };

        Ok(Camera {
            idx: index,
            backend: RefCell::new(capture_backend),
            backend_api: use_backend,
        })
    }

    /// Create a new `Camera` from raw values.
    /// # Errors
    /// This will error if you either have a bad platform configuration (e.g. `input_v4l` but not on linux) or the backend cannot create the camera (e.g. permission denied).
    pub fn new_with(
        index: usize,
        width: u32,
        height: u32,
        fps: u32,
        fourcc: FrameFormat,
        backend: CaptureAPIBackend,
    ) -> Result<Self, NokhwaError> {
        let camera_format = CameraFormat::new_from(width, height, fourcc, fps);
        Camera::new(index, Some(camera_format), backend)
    }

    /// Gets the current Camera's index.
    pub fn index(&self) -> usize {
        self.idx
    }

    /// Sets the current Camera's index. Note that this re-initializes the camera.
    /// # Errors
    /// The Backend may fail to initialize.
    pub fn set_index(self, new_idx: usize) -> Result<Self, NokhwaError> {
        self.backend.borrow_mut().stop_stream()?;
        let new_camera_format = self.backend.borrow().get_camera_format();
        Camera::new(new_idx, Some(new_camera_format), self.backend_api)
    }

    /// Gets the current Camera's backend
    pub fn backend(&self) -> CaptureAPIBackend {
        self.backend_api
    }

    /// Sets the current Camera's backend. Note that this re-initializes the camera.
    /// # Errors
    /// The new backend may not exist or may fail to initialize the new camera.
    pub fn set_backend(self, new_backend: CaptureAPIBackend) -> Result<Self, NokhwaError> {
        self.backend.borrow_mut().stop_stream()?;
        let new_camera_format = self.backend.borrow().get_camera_format();
        Camera::new(self.idx, Some(new_camera_format), new_backend)
    }

    /// Gets the camera information such as Name and Index as a [`CameraInfo`].
    pub fn get_info(&self) -> CameraInfo {
        self.backend.borrow().get_info()
    }
    /// Gets the current [`CameraFormat`].
    pub fn get_camera_format(&self) -> CameraFormat {
        self.backend.borrow().get_camera_format()
    }
    /// Will set the current [`CameraFormat`]
    /// This will reset the current stream if used while stream is opened.
    /// # Errors
    /// If you started the stream and the camera rejects the new camera format, this will return an error.
    pub fn set_camera_format(&mut self, new_fmt: CameraFormat) -> Result<(), NokhwaError> {
        self.backend.borrow_mut().set_camera_format(new_fmt)
    }
    /// A hashmap of [`Resolution`]s mapped to framerates
    /// # Errors
    /// This will error if the camera is not queryable or a query operation has failed. Some backends will error this out as a Unsupported Operation ([`NokhwaError::UnsupportedOperation`]).
    pub fn get_compatible_list_by_resolution(
        &self,
        fourcc: FrameFormat,
    ) -> Result<HashMap<Resolution, Vec<u32>>, NokhwaError> {
        self.backend
            .borrow()
            .get_compatible_list_by_resolution(fourcc)
    }
    /// A Vector of compatible [`FrameFormat`]s.
    /// # Errors
    /// This will error if the camera is not queryable or a query operation has failed. Some backends will error this out as a Unsupported Operation ([`NokhwaError::UnsupportedOperation`]).
    pub fn get_compatible_fourcc(&mut self) -> Result<Vec<FrameFormat>, NokhwaError> {
        self.backend.borrow_mut().get_compatible_fourcc()
    }
    /// Gets the current camera resolution (See: [`Resolution`], [`CameraFormat`]).
    pub fn get_resolution(&self) -> Resolution {
        self.backend.borrow().get_resolution()
    }
    /// Will set the current [`Resolution`]
    /// This will reset the current stream if used while stream is opened.
    /// # Errors
    /// If you started the stream and the camera rejects the new resolution, this will return an error.
    pub fn set_resolution(&mut self, new_res: Resolution) -> Result<(), NokhwaError> {
        self.backend.borrow_mut().set_resolution(new_res)
    }
    /// Gets the current camera framerate (See: [`CameraFormat`]).
    pub fn get_framerate(&self) -> u32 {
        self.backend.borrow().get_framerate()
    }
    /// Will set the current framerate
    /// This will reset the current stream if used while stream is opened.
    /// # Errors
    /// If you started the stream and the camera rejects the new framerate, this will return an error.
    pub fn set_framerate(&mut self, new_fps: u32) -> Result<(), NokhwaError> {
        self.backend.borrow_mut().set_framerate(new_fps)
    }
    /// Gets the current camera's frame format (See: [`FrameFormat`], [`CameraFormat`]).
    pub fn get_frameformat(&self) -> FrameFormat {
        self.backend.borrow().get_frameformat()
    }
    /// Will set the current [`FrameFormat`]
    /// This will reset the current stream if used while stream is opened.
    /// # Errors
    /// If you started the stream and the camera rejects the new frame foramt, this will return an error.
    pub fn set_frameformat(&mut self, fourcc: FrameFormat) -> Result<(), NokhwaError> {
        self.backend.borrow_mut().set_frameformat(fourcc)
    }
    /// Will open the camera stream with set parameters. This will be called internally if you try and call [`get_frame()`](CaptureBackendTrait::get_frame()) before you call [`open_stream()`](CaptureBackendTrait::open_stream()).
    /// # Errors
    /// If the specific backend fails to open the camera (e.g. already taken, busy, doesn't exist anymore) this will error.
    pub fn open_stream(&mut self) -> Result<(), NokhwaError> {
        self.backend.borrow_mut().open_stream()
    }
    /// Checks if stream if open. If it is, it will return true.
    pub fn is_stream_open(&self) -> bool {
        self.backend.borrow().is_stream_open()
    }
    /// Will get a frame from the camera as a Raw RGB image buffer. Depending on the backend, if you have not called [`open_stream()`](CaptureBackendTrait::open_stream()) before you called this,
    /// it will either return an error.
    /// # Errors
    /// If the backend fails to get the frame (e.g. already taken, busy, doesn't exist anymore), the decoding fails (e.g. MJPEG -> u8), or [`open_stream()`](CaptureBackendTrait::open_stream()) has not been called yet,
    /// this will error.
    pub fn get_frame(&self) -> Result<ImageBuffer<Rgb<u8>, Vec<u8>>, NokhwaError> {
        self.backend.borrow_mut().get_frame()
    }
    /// Will get a frame from the camera **without** any processing applied, meaning you will usually get a frame you need to decode yourself.
    /// # Errors
    /// If the backend fails to get the frame (e.g. already taken, busy, doesn't exist anymore), or [`open_stream()`](CaptureBackendTrait::open_stream()) has not been called yet, this will error.
    pub fn get_frame_raw(&self) -> Result<Vec<u8>, NokhwaError> {
        self.backend.borrow_mut().get_frame_raw()
    }
    /// Will drop the stream.
    /// # Errors
    /// Please check the `Quirks` section of each backend.
    pub fn stop_stream(&self) -> Result<(), NokhwaError> {
        self.backend.borrow_mut().stop_stream()
    }
}

#[cfg(feature = "input_v4l")]
#[allow(clippy::unnecessary_wraps)]
fn init_v4l(
    idx: usize,
    setting: Option<CameraFormat>,
) -> Option<Result<Box<dyn CaptureBackendTrait>, NokhwaError>> {
    match V4LCaptureDevice::new(idx, setting) {
        Ok(cap) => Some(Ok(Box::new(cap))),
        Err(why) => Some(Err(why)),
    }
}

#[cfg(not(feature = "input_v4l"))]
#[allow(clippy::unnecessary_wraps)]
fn init_v4l(
    _idx: usize,
    _setting: Option<CameraFormat>,
) -> Option<Result<Box<dyn CaptureBackendTrait>, NokhwaError>> {
    None
}

#[cfg(feature = "uvc")]
#[allow(clippy::unnecessary_wraps)]
fn init_uvc(
    idx: usize,
    setting: Option<CameraFormat>,
) -> Option<Result<Box<dyn CaptureBackendTrait>, NokhwaError>> {
    match UVCCaptureDevice::create(idx, setting) {
        Ok(cap) => Some(Ok(Box::new(cap))),
        Err(why) => Some(Err(why)),
    }
}

#[cfg(not(feature = "input_uvc"))]
#[allow(clippy::unnecessary_wraps)]
fn init_uvc(
    _idx: usize,
    _setting: Option<CameraFormat>,
) -> Option<Result<Box<dyn CaptureBackendTrait>, NokhwaError>> {
    None
}