-
Notifications
You must be signed in to change notification settings - Fork 50
src: Get video formats using Device Monitor #281
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
Changes from all commits
54a2aba
f7a12bc
3be55f8
20b0bdb
f85a476
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| use std::sync::{Arc, Mutex}; | ||
|
|
||
| use anyhow::{Context, Result}; | ||
| use gst_app::prelude::*; | ||
| use tracing::*; | ||
|
|
||
| lazy_static! { | ||
| static ref MANAGER: Arc<Mutex<Manager>> = { | ||
| // Constructing `gst::DeviceMonitor` requires GStreamer to be initialized; ensure it is so | ||
| // that callers like cameras_available() work even when the binary entry point hasn't run. | ||
| gst::init().expect("Failed to initialize GStreamer"); | ||
| Arc::new(Mutex::new(Manager::default())) | ||
| }; | ||
| } | ||
|
|
||
| #[derive(Default)] | ||
| struct Manager { | ||
| monitor: gst::DeviceMonitor, | ||
| } | ||
|
|
||
| impl Drop for Manager { | ||
| fn drop(&mut self) { | ||
| self.monitor.stop(); | ||
| } | ||
| } | ||
|
|
||
| #[instrument(level = "debug")] | ||
| pub fn init() -> Result<()> { | ||
| let manager_guard = MANAGER.lock().unwrap(); | ||
|
|
||
| manager_guard.monitor.set_show_all_devices(true); | ||
| manager_guard.monitor.set_show_all(true); | ||
| manager_guard.monitor.start()?; | ||
|
|
||
| let providers = manager_guard.monitor.providers(); | ||
| info!("GST Device Providers: {providers:#?}"); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just for curiosity, is there a reason for not having this in the default call of Manager ? |
||
|
|
||
| #[instrument(level = "debug")] | ||
| pub(crate) fn video_devices() -> Result<Vec<glib::WeakRef<gst::Device>>> { | ||
| let monitor = &MANAGER.lock().unwrap().monitor; | ||
|
|
||
| let devices = monitor | ||
| .devices() | ||
| .iter() | ||
| .filter_map(|device| { | ||
| if device.device_class().ne("Video/Source") { | ||
| return None; | ||
| } | ||
|
|
||
| Some(device.downgrade()) | ||
| }) | ||
| .collect(); | ||
|
|
||
| Ok(devices) | ||
| } | ||
|
|
||
| #[instrument(level = "debug")] | ||
| pub fn v4l_devices() -> Result<Vec<glib::WeakRef<gst::Device>>> { | ||
| let devices = video_devices()? | ||
| .iter() | ||
| .filter(|device_weak| { | ||
| let Some(device) = device_weak.upgrade() else { | ||
| return false; | ||
| }; | ||
|
|
||
| device.properties().iter().any(|s| { | ||
| let Ok(api) = s.get::<String>("device.api") else { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to have a comment for this string: It would be even better to have such strings as "device.path" to be defined as a global private property that explains that is a GST property string for this field |
||
| return false; | ||
| }; | ||
|
|
||
| api.eq("v4l2") | ||
| }) | ||
| }) | ||
| .cloned() | ||
| .collect(); | ||
|
|
||
| Ok(devices) | ||
| } | ||
|
|
||
| #[instrument(level = "debug")] | ||
| pub fn v4l_device_with_path(device_path: &str) -> Result<glib::WeakRef<gst::Device>> { | ||
| v4l_devices()? | ||
| .iter() | ||
| .find(|device_weak| { | ||
| let Some(device) = device_weak.upgrade() else { | ||
| return false; | ||
| }; | ||
|
|
||
| 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: &glib::WeakRef<gst::Device>) -> Result<gst::Caps> { | ||
| device | ||
| .upgrade() | ||
| .context("Fail to access device")? | ||
| .caps() | ||
| .context("Caps not found") | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.