Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge devices from the same bus #125

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/server/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::io::prelude::*;
pub struct ApiVideoSource {
name: String,
source: String,
source_type: VideoSourceType,
formats: Vec<Format>,
controls: Vec<Control>,
}
Expand Down Expand Up @@ -110,22 +111,25 @@ pub async fn v4l() -> Json<Vec<ApiVideoSource>> {
let cameras = video_source::cameras_available();
let cameras: Vec<ApiVideoSource> = cameras
.iter()
.map(|cam| match cam {
.map(|video_source_type| match video_source_type {
VideoSourceType::Local(cam) => ApiVideoSource {
name: cam.name().clone(),
source: cam.source_string().to_string(),
source_type: video_source_type.clone(),
formats: cam.formats(),
controls: cam.controls(),
},
VideoSourceType::Gst(gst) => ApiVideoSource {
name: gst.name().clone(),
source: gst.source_string().to_string(),
source_type: video_source_type.clone(),
formats: gst.formats(),
controls: gst.controls(),
},
VideoSourceType::Redirect(redirect) => ApiVideoSource {
name: redirect.name().clone(),
source: redirect.source_string().to_string(),
source_type: video_source_type.clone(),
formats: redirect.formats(),
controls: redirect.controls(),
},
Expand Down
1 change: 1 addition & 0 deletions src/settings/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ mod tests {
video_source: VideoSourceType::Local(VideoSourceLocal {
name: "Fake Potato Test Video Source Camera".into(),
device_path: "/dev/potatovideo".into(),
v4l_by_path: "".into(),
typ: VideoSourceLocalType::Usb("usb-0420:08:47.42-77".into()),
}),
}];
Expand Down
1 change: 1 addition & 0 deletions src/stream/stream_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ mod tests {
video_source: VideoSourceType::Local(VideoSourceLocal {
name: "PotatoCam".into(),
device_path: "/dev/video42".into(),
v4l_by_path: "".into(),
typ: VideoSourceLocalType::Usb("TestPotatoCam".into()),
}),
});
Expand Down
43 changes: 30 additions & 13 deletions src/video/video_source_local.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::cmp::max;
use std::path::PathBuf;

use super::types::*;
use super::{
Expand All @@ -25,6 +26,7 @@ pub enum VideoSourceLocalType {
pub struct VideoSourceLocal {
pub name: String,
pub device_path: String,
pub v4l_by_path: String,
#[serde(rename = "type")]
pub typ: VideoSourceLocalType,
}
Expand All @@ -43,7 +45,7 @@ impl VideoSourceLocalType {
//
// https://www.kernel.org/doc/html/v4.9/media/uapi/v4l/vidioc-querycap.html#:~:text=__u8-,bus_info,-%5B32%5D

pub fn from_str(description: &str) -> Self {
pub fn from_bus_str(description: &str) -> Self {
if let Some(result) = VideoSourceLocalType::usb_from_str(description) {
return result;
}
Expand Down Expand Up @@ -444,12 +446,24 @@ impl VideoSource for VideoSourceLocal {
}
}

pub fn get_v4l_source_by_path(path: &PathBuf) -> Result<PathBuf, std::io::Error> {
for v4l_path in std::fs::read_dir("/dev/v4l/by-path/")? {
let v4l_path = v4l_path?.path();
let solved_v4l_path = v4l_path.canonicalize()?;
let solved_path = path.canonicalize()?;
if solved_path == solved_v4l_path {
return Ok(v4l_path);
}
}
Err(std::io::Error::new(std::io::ErrorKind::NotFound, "unknown"))
}

impl VideoSourceAvailable for VideoSourceLocal {
fn cameras_available() -> Vec<VideoSourceType> {
let cameras_path: Vec<String> = std::fs::read_dir("/dev/")
let cameras_path: Vec<PathBuf> = std::fs::read_dir("/dev/")
.unwrap()
.map(|f| String::from(f.unwrap().path().to_str().unwrap()))
.filter(|f| f.starts_with("/dev/video"))
.map(|result| result.unwrap().path())
.filter(|path| path.to_str().unwrap().starts_with("/dev/video"))
.collect();

let mut cameras: Vec<VideoSourceType> = vec![];
Expand All @@ -458,28 +472,28 @@ impl VideoSourceAvailable for VideoSourceLocal {
let caps = camera.query_caps();

if let Err(error) = caps {
debug!(
"Failed to capture caps for device: {} {:#?}",
camera_path, error
);
debug!("Failed to capture caps for device: {camera_path:?} {error:#?}");
continue;
}
let caps = caps.unwrap();
debug!("Device {camera_path:#?}, \nCapabilities: {caps:#?}");

if let Err(error) = camera.format() {
if error.kind() != std::io::ErrorKind::InvalidInput {
debug!(
"Failed to capture formats for device: {}\nError: {:#?}",
camera_path, error
"Failed to capture formats for device: {camera_path:?}\nError: {error:#?}"
);
}
continue;
}

let v4l_by_path = get_v4l_source_by_path(&camera_path).unwrap_or_default();

let source = VideoSourceLocal {
name: caps.card,
device_path: camera_path.clone(),
typ: VideoSourceLocalType::from_str(&caps.bus),
device_path: camera_path.to_str().unwrap().to_owned(),
v4l_by_path: v4l_by_path.to_str().unwrap().to_owned(),
typ: VideoSourceLocalType::from_bus_str(&caps.bus),
};
cameras.push(VideoSourceType::Local(source));
}
Expand Down Expand Up @@ -524,7 +538,10 @@ mod tests {
];

for description in descriptions {
assert_eq!(description.0, VideoSourceLocalType::from_str(description.1));
assert_eq!(
description.0,
VideoSourceLocalType::from_bus_str(description.1)
);
}
}

Expand Down