Skip to content

Commit e25d761

Browse files
WIP: cameras_available
1 parent 617fef5 commit e25d761

File tree

1 file changed

+19
-65
lines changed

1 file changed

+19
-65
lines changed

src/lib/video/local/video_source_local_linux.rs

Lines changed: 19 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
};
66

77
use anyhow::{anyhow, Result};
8-
use lazy_static::lazy_static;
8+
use gst::prelude::*;
99
use paperclip::actix::Apiv2Schema;
1010
use regex::Regex;
1111
use serde::{Deserialize, Serialize};
@@ -731,70 +731,24 @@ impl VideoSource for VideoSourceLocal {
731731
impl VideoSourceAvailable for VideoSourceLocal {
732732
#[instrument(level = "debug")]
733733
async fn cameras_available() -> Vec<VideoSourceType> {
734-
let mut cameras: Vec<VideoSourceType> = vec![];
735-
736-
let cameras_path = {
737-
let read_dir = match std::fs::read_dir("/dev/") {
738-
Ok(cameras_path) => cameras_path,
739-
Err(error) => {
740-
error!("Failed to get cameras from \"/dev/\": {error:?}");
741-
return cameras;
742-
}
743-
};
744-
745-
read_dir
746-
.filter_map(|dir_entry| {
747-
let dir_entry = match dir_entry {
748-
Ok(dir_entry) => dir_entry,
749-
Err(error) => {
750-
info!("Failed reading a device: {error:?}");
751-
return None;
752-
}
753-
};
754-
755-
dir_entry.path().to_str().map(String::from)
756-
})
757-
.filter(|entry_str| entry_str.starts_with("/dev/video"))
758-
.collect::<Vec<String>>()
759-
};
760-
761-
for camera_path in &cameras_path {
762-
let Ok(caps) = unpanic({
763-
use v4l::video::Capture;
764-
765-
let camera_path = camera_path.clone();
766-
move || {
767-
let v4l_device = v4l::Device::with_path(&camera_path).inspect_err(|error| {
768-
trace!("Failed to get device {camera_path:?}. Reason: {error:?}")
769-
})?;
770-
771-
let caps = v4l_device.query_caps().inspect_err(|error| {
772-
trace!(
773-
"Failed to capture caps for device: {camera_path:?}. Reason: {error:?}"
774-
)
775-
})?;
776-
777-
v4l_device.format().inspect_err(|error| {
778-
trace!("Failed to capture formats for device: {camera_path:?}. Reason: {error:?}")
779-
})?;
780-
781-
Ok::<_, std::io::Error>(caps)
782-
}
783-
}) else {
784-
continue;
785-
};
786-
787-
let typ = VideoSourceLocalType::from_str(&caps.bus);
788-
789-
let source = VideoSourceLocal {
790-
name: caps.card,
791-
device_path: camera_path.to_string(),
792-
typ,
793-
};
794-
cameras.push(VideoSourceType::Local(source));
795-
}
796-
797-
cameras
734+
gst_device_monitor::v4l_devices()
735+
.unwrap_or_default()
736+
.iter()
737+
.filter_map(|device_weak| {
738+
let device = device_weak.upgrade()?;
739+
let name = device.display_name().to_string();
740+
let properties = device.properties()?;
741+
let device_path = properties.get::<String>("device.path").ok()?;
742+
let bus = properties.get::<String>("v4l2.device.bus_info").ok()?;
743+
744+
let typ = VideoSourceLocalType::Usb(bus.to_string());
745+
Some(VideoSourceType::Local(VideoSourceLocal {
746+
name,
747+
device_path,
748+
typ,
749+
}))
750+
})
751+
.collect()
798752
}
799753
}
800754

0 commit comments

Comments
 (0)