From bc6b3b9de0b371567ca24a16e880de8f50055fd0 Mon Sep 17 00:00:00 2001 From: Christian M Date: Mon, 1 Jan 2024 16:16:22 +0100 Subject: [PATCH] :wrench: adds camera device to config --- config.toml | 1 + src/capture/webcam.rs | 18 +++++++++++++----- src/config.rs | 3 +++ src/main.rs | 8 +++++++- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/config.toml b/config.toml index 68f1aff..2cc0560 100644 --- a/config.toml +++ b/config.toml @@ -3,6 +3,7 @@ ctrlc = false signals = ["term", "hup"] [default.camera] +device = "0" width = 640 height = 480 fps = 30 diff --git a/src/capture/webcam.rs b/src/capture/webcam.rs index 84da3d9..057fe19 100644 --- a/src/capture/webcam.rs +++ b/src/capture/webcam.rs @@ -28,7 +28,12 @@ pub struct WebCam { } impl WebCam { - pub fn new(width: u32, height: u32, fps: u32) -> Result> { + pub fn new( + width: u32, + height: u32, + fps: u32, + device: Option, + ) -> Result> { let running = Arc::new(Mutex::new(false)); nokhwa_initialize(|granted| { log::debug!("Camera access granted {}", granted); @@ -42,8 +47,11 @@ impl WebCam { CameraFormat::new_from(width, height, FrameFormat::MJPEG, fps), )); - // let camera_index = cameras.first().unwrap().index().clone(); // is video1 not video0 - let camera_index = CameraIndex::Index(0 as u32); + let camera_index = if let Some(device) = device { + CameraIndex::String(device) + } else { + CameraIndex::Index(0 as u32) + }; let mut device = CallbackCamera::new(camera_index, format, callback)?; device.open_stream()?; @@ -71,7 +79,7 @@ impl WebCam { impl Default for WebCam { fn default() -> Self { - Self::new(640, 480, 30).unwrap() + Self::new(640, 480, 30, Some("0".to_string())).unwrap() } } @@ -121,7 +129,7 @@ mod tests { #[tokio::test] async fn stream_stopped() { - let mut webcam = WebCam::new(640, 480, 30).unwrap(); + let mut webcam = WebCam::new(640, 480, 30, Some("0".to_string())).unwrap(); webcam.stop(); let stream = webcam.next().await; assert!(stream.is_none()); diff --git a/src/config.rs b/src/config.rs index 4855ee5..239e17f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -23,6 +23,7 @@ impl Default for Shutdown { #[derive(Debug, Clone, Deserialize, Serialize)] pub struct Camera { + pub device: Option, pub width: u32, pub height: u32, pub fps: u32, @@ -31,6 +32,7 @@ pub struct Camera { impl Default for Camera { fn default() -> Self { Camera { + device: None, width: 640, height: 480, fps: 30, @@ -112,6 +114,7 @@ mod tests { let merged = merge_cli_config(&config, &cli); + assert_eq!(merged.camera.device, None); assert_eq!(merged.camera.width, cli.width.unwrap()); assert_eq!(merged.camera.height, cli.height.unwrap()); assert_eq!(merged.camera.fps, cli.fps.unwrap()); diff --git a/src/main.rs b/src/main.rs index 79821f7..30551c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,7 +30,13 @@ async fn main() { #[cfg(feature = "webcam")] let capture: Arc> = Arc::new(Mutex::new( - WebCam::new(config.camera.width, config.camera.height, config.camera.fps).unwrap(), + WebCam::new( + config.camera.width, + config.camera.height, + config.camera.fps, + config.camera.device.clone(), + ) + .unwrap(), )); log::info!("Loaded Config: {:?}", config);