Skip to content

Commit

Permalink
🔧 adds camera device to config
Browse files Browse the repository at this point in the history
  • Loading branch information
chriamue committed Jan 1, 2024
1 parent 03afb57 commit bc6b3b9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ctrlc = false
signals = ["term", "hup"]

[default.camera]
device = "0"
width = 640
height = 480
fps = 30
Expand Down
18 changes: 13 additions & 5 deletions src/capture/webcam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ pub struct WebCam {
}

impl WebCam {
pub fn new(width: u32, height: u32, fps: u32) -> Result<Self, Box<dyn Error>> {
pub fn new(
width: u32,
height: u32,
fps: u32,
device: Option<String>,
) -> Result<Self, Box<dyn Error>> {
let running = Arc::new(Mutex::new(false));
nokhwa_initialize(|granted| {
log::debug!("Camera access granted {}", granted);
Expand All @@ -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()?;
Expand Down Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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());
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl Default for Shutdown {

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Camera {
pub device: Option<String>,
pub width: u32,
pub height: u32,
pub fps: u32,
Expand All @@ -31,6 +32,7 @@ pub struct Camera {
impl Default for Camera {
fn default() -> Self {
Camera {
device: None,
width: 640,
height: 480,
fps: 30,
Expand Down Expand Up @@ -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());
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ async fn main() {

#[cfg(feature = "webcam")]
let capture: Arc<Mutex<WebCam>> = 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);
Expand Down

0 comments on commit bc6b3b9

Please sign in to comment.