Skip to content
Merged
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
23 changes: 21 additions & 2 deletions src/lib/stream/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ use crate::{
types::CaptureConfiguration,
webrtc::signalling_protocol::BindAnswer,
},
video::{types::VideoSourceType, video_source},
video::{
types::{Format, VideoSourceType},
video_source::{self, VideoSourceFormats},
},
video_stream::types::VideoAndStreamInformation,
};

Expand Down Expand Up @@ -151,8 +154,24 @@ pub async fn update_devices(
continue;
};

// Only resolve formats for candidates sharing the source name so that mismatched
// candidates (which are filtered out by name first) don't trigger device probing.
let mut formats: HashMap<String, Vec<Format>> = HashMap::new();
for candidate in candidates.iter() {
let VideoSourceType::Local(camera) = candidate else {
continue;
};
if camera.name != source.name {
continue;
}
formats.insert(
candidate.inner().source_string().to_string(),
candidate.formats().await,
);
}
Comment on lines +160 to +171

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for candidate in candidates.iter() {
let VideoSourceType::Local(camera) = candidate else {
continue;
};
if camera.name != source.name {
continue;
}
formats.insert(
candidate.inner().source_string().to_string(),
candidate.formats().await,
);
}
for candidate in candidates.iter().filter(|candidate| {
matches!(candidate, VideoSourceType::Local(camera) if camera.name == source.name)
}) {
formats.insert(
candidate.inner().source_string().to_string(),
candidate.formats().await,
);
}


match source
.try_identify_device(capture_configuration, candidates)
.try_identify_device(capture_configuration, candidates, &formats)
.await
{
Ok(Some(candidate_source_string)) => {
Expand Down
111 changes: 111 additions & 0 deletions src/lib/video/gst_device_monitor.rs
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(())
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have a comment for this string:

# device.api is an inner string defined by gst for which api this device is accessed with: Ex: alsa, v4l..

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")
}
Loading
Loading