// client/src/input/camera.rs #![forbid(unsafe_code)] use anyhow::Result; use anyhow::Context; use gstreamer as gst; use gstreamer_app as gst_app; use gst::prelude::*; use lesavka_common::lesavka::VideoPacket; pub struct CameraCapture { pipeline: gst::Pipeline, sink: gst_app::AppSink, } impl CameraCapture { pub fn new(device_fragment: Option<&str>) -> anyhow::Result { gst::init().ok(); // Pick device let dev = device_fragment .and_then(Self::find_device) .unwrap_or_else(|| "/dev/video0".into()); // let (enc, raw_caps) = Self::pick_encoder(); // (NVIDIA → VA-API → software x264). let (enc, kf_prop, kf_val) = Self::choose_encoder(); tracing::info!("📸 using encoder element: {enc}"); // let desc = format!( // "v4l2src device={dev} do-timestamp=true ! {raw_caps},width=1280,height=720 ! \ // videoconvert ! {enc} key-int-max=30 ! \ // h264parse config-interval=-1 ! \ // appsink name=asink emit-signals=true max-buffers=60 drop=true" // ); // tracing::debug!(%desc, "📸 pipeline-desc"); // Build a pipeline that works for any of the three encoders. // * nvh264enc needs NVMM memory caps; // * vaapih264enc wants system-memory caps; // * x264enc needs the usual raw caps. let desc = format!( "v4l2src device={dev} do-timestamp=true ! \ videoconvert ! {enc} {kf_prop}={kf_val} ! \ h264parse config-interval=-1 ! \ appsink name=asink emit-signals=true max-buffers=60 drop=true" ); tracing::info!(%enc, ?desc, "📸 using encoder element"); let pipeline: gst::Pipeline = gst::parse::launch(&desc) .context("gst parse_launch(cam)")? .downcast::() .expect("not a pipeline"); tracing::debug!("📸 pipeline built OK – setting PLAYING…"); let sink: gst_app::AppSink = pipeline .by_name("asink") .expect("appsink element not found") .downcast::() .expect("appsink down‑cast"); pipeline.set_state(gst::State::Playing)?; tracing::info!("📸 webcam pipeline ▶️ device={dev}"); Ok(Self { pipeline, sink }) } pub fn pull(&self) -> Option { let sample = self.sink.pull_sample().ok()?; let buf = sample.buffer()?; let map = buf.map_readable().ok()?; let pts = buf.pts().unwrap_or(gst::ClockTime::ZERO).nseconds() / 1_000; Some(VideoPacket { id: 0, pts, data: map.as_slice().to_vec() }) } /// Fuzzy‑match devices under `/dev/v4l/by-id` fn find_device(substr: &str) -> Option { let dir = std::fs::read_dir("/dev/v4l/by-id").ok()?; for e in dir.flatten() { let p = e.path(); if p.file_name()?.to_string_lossy().contains(substr) { if let Ok(target) = std::fs::read_link(&p) { return Some(format!("/dev/{}", target.file_name()?.to_string_lossy())); } } } None } /// Cheap stub used when the web‑cam is disabled pub fn new_stub() -> Self { let pipeline = gst::Pipeline::new(); let sink: gst_app::AppSink = gst::ElementFactory::make("appsink") .build() .expect("appsink") .downcast::() .unwrap(); Self { pipeline, sink } } fn pick_encoder() -> (&'static str, &'static str) { let encoders = &[ ("nvh264enc", "video/x-raw(memory:NVMM),format=NV12"), ("vaapih264enc","video/x-raw,format=NV12"), ("v4l2h264enc", "video/x-raw"), // RPi, Jetson, etc. ("x264enc", "video/x-raw"), // software ]; for (name, caps) in encoders { if gst::ElementFactory::find(name).is_some() { return (name, caps); } } // last resort – software ("x264enc", "video/x-raw") } /// Return the encoder element *and* the property/key-frame pair we must /// set to get an IDR every 30 frames. fn choose_encoder() -> (&'static str, &'static str, &'static str) { if gst::ElementFactory::find("nvh264enc").is_some() { ("nvh264enc", "gop-size", "30") // NVIDIA NVENC uses `gop-size` :contentReference[oaicite:0]{index=0} } else if gst::ElementFactory::find("vaapih264enc").is_some() { ("vaapih264enc", "keyframe-period", "30")// VA-API uses `keyframe-period` :contentReference[oaicite:1]{index=1} } else { ("x264enc", "key-int-max", "30") // libx264 fallback :contentReference[oaicite:2]{index=2} } } }