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

src: Get video formats using Device Monitor #281

Draft
wants to merge 1 commit 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
19 changes: 19 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ async fn main() -> Result<(), std::io::Error> {
cli::manager::init();
// Logger should start before everything else to register any log information
logger::manager::init();

video::gst_device_monitor::init().unwrap();
// just for debug
{
use gst_app::prelude::DeviceExt;

dbg!(video::gst_device_monitor::providers().unwrap());

let devices = video::gst_device_monitor::devices().unwrap();
dbg!(&devices);
devices.iter().for_each(|device| {
device.properties().iter().for_each(|property| {
dbg!(&property);
});

dbg!(&device.caps());
});
}

// Settings should start before everybody else to ensure that the CLI are stored
settings::manager::init(None);

Expand Down
118 changes: 118 additions & 0 deletions src/video/gst_device_monitor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
use std::sync::{Arc, Mutex};

use anyhow::{Context, Result};
use gst_app::prelude::DeviceMonitorExt;
use tracing::*;

use gst::{self, prelude::*};

lazy_static! {
static ref MANAGER: Arc<Mutex<Manager>> = Arc::new(Mutex::new(Manager::default()));
}

#[derive(Default)]
struct Manager {
monitor: gst::DeviceMonitor,
}

#[instrument(level = "debug")]
pub fn init() -> Result<()> {
if let Err(error) = gst::init() {
error!("Error! {error}");
};

let monitor = &MANAGER.lock().unwrap().monitor;

let bus: gst::Bus = monitor.bus();
bus.add_watch(move |_bus, message| {
use gst::MessageView::*;

match message.view() {
DeviceAdded(message) => {
let device = message.device();
debug!("Device added: {device:#?}");
}
DeviceRemoved(message) => {
let device = message.device();
debug!("Device removed: {device:#?}");
}
DeviceChanged(message) => {
let (old, new) = message.device_changed();
debug!("Device changed from: {old:#?} to {new:#?}");
}
_ => (),
}

Continue(true)
})?;

monitor.set_show_all_devices(true);
// monitor.set_show_all(true);

monitor.start()?;

Ok(())
}

#[instrument(level = "debug")]
pub fn providers() -> Result<()> {
let monitor = &MANAGER.lock().unwrap().monitor;

let providers = monitor.providers();

providers.iter().for_each(|provider| {
debug!("Provider: {provider:#?}");
});

Ok(())
}

#[instrument(level = "debug")]
pub fn devices() -> Result<Vec<gst::Device>> {
let monitor = &MANAGER.lock().unwrap().monitor;

let devices = monitor.devices().iter().cloned().collect();

Ok(devices)
}

#[instrument(level = "debug")]
pub fn v4l_devices() -> Result<Vec<gst::Device>> {
let devices = devices()?
.iter()
.filter(|device| {
device.properties().iter().any(|s| {
let Ok(api) = s.get::<String>("device.api") else {
return false;
};

api.eq("v4l2")
})
})
.cloned()
.collect();

Ok(devices)
}

#[instrument(level = "debug")]
pub fn device_with_path(device_path: &str) -> Result<gst::Device> {
v4l_devices()?
.iter()
.find(|device| {
device.properties().iter().any(|s| {
let Ok(path) = s.get::<String>("device.path") else {
return false;
};

path.eq(device_path)
})
})
.cloned()
.context("Device not found")
}

#[instrument(level = "debug")]
pub fn device_caps(device: &gst::Device) -> Result<gst::Caps> {
device.caps().context("Caps not found")
}
2 changes: 2 additions & 0 deletions src/video/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ pub mod xml;
pub mod video_source_gst;
pub mod video_source_local;
pub mod video_source_redirect;

pub mod gst_device_monitor;
14 changes: 10 additions & 4 deletions src/video/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub enum VideoSourceType {
Redirect(VideoSourceRedirect),
}

#[derive(Apiv2Schema, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
#[derive(
Apiv2Schema, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize, Hash,
)]
#[serde(rename_all = "UPPERCASE")]
pub enum VideoEncodeType {
Unknown(String),
Expand All @@ -28,14 +30,18 @@ pub struct Format {
pub sizes: Vec<Size>,
}

#[derive(Apiv2Schema, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
#[derive(
Apiv2Schema, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize, Hash,
)]
pub struct Size {
pub width: u32,
pub height: u32,
pub intervals: Vec<FrameInterval>,
}

#[derive(Apiv2Schema, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
#[derive(
Apiv2Schema, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize, Hash,
)]
pub struct FrameInterval {
pub numerator: u32,
pub denominator: u32,
Expand Down Expand Up @@ -108,7 +114,7 @@ impl VideoEncodeType {
match fourcc.as_str() {
"H264" => VideoEncodeType::H264,
"MJPG" => VideoEncodeType::Mjpg,
"YUYV" => VideoEncodeType::Yuyv,
"YUYV" | "YUY2" => VideoEncodeType::Yuyv,
_ => VideoEncodeType::Unknown(fourcc),
}
}
Expand Down
Loading