76 lines
2.8 KiB
Rust
Raw Normal View History

2025-06-21 05:21:57 -05:00
// client/src/output/video.rs
use gstreamer as gst;
use gstreamer_app as gst_app;
use gst::prelude::*;
2025-06-24 23:48:06 -05:00
use gst_app::prelude::*;
2025-06-23 07:18:26 -05:00
use lesavka_common::lesavka::VideoPacket;
2025-06-24 23:48:06 -05:00
use winit::window::{Window, WindowAttributes};
use winit::event_loop::EventLoop;
2025-06-21 05:21:57 -05:00
pub struct MonitorWindow {
id: u32,
_window: Window,
src: gst_app::AppSrc,
}
impl MonitorWindow {
pub fn new(id: u32, el: &EventLoop<()>) -> anyhow::Result<Self> {
gst::init()?;
2025-06-24 23:48:06 -05:00
let window = el.create_window(
WindowAttributes::default()
.with_title(format!("Lesavkamonitor{id}"))
.with_decorations(false)
)?;
2025-06-21 05:21:57 -05:00
2025-06-26 16:17:31 -05:00
let caps = gst::Caps::builder("video/x-h264")
.field("stream-format", &"byte-stream")
.field("alignment", &"au")
.build();
2025-06-26 14:05:23 -05:00
let desc = if std::env::var_os("LESAVKA_HW_DEC").is_some() {
2025-06-26 16:17:31 -05:00
"appsrc name=src is-live=true format=time do-timestamp=true block=false ! \
capsfilter caps=video/x-h264,stream-format=byte-stream,alignment=au ! \
queue max-size-buffers=0 max-size-bytes=0 max-size-time=0 leaky=downstream ! \
h264parse ! vaapih264dec low-latency=true ! videoconvert ! \
autovideosink sync=false max-lateness=-1"
2025-06-25 16:23:50 -05:00
} else {
2025-06-26 16:17:31 -05:00
"appsrc name=src is-live=true format=time do-timestamp=true block=false ! \
capsfilter caps=video/x-h264,stream-format=byte-stream,alignment=au ! \
queue max-size-buffers=0 max-size-bytes=0 max-size-time=0 leaky=downstream ! \
h264parse ! decodebin ! videoconvert ! autovideosink sync=false max-lateness=-1"
2025-06-25 16:23:50 -05:00
};
2025-06-24 23:48:06 -05:00
let pipeline = gst::parse::launch(desc)?
.downcast::<gst::Pipeline>()
.unwrap();
2025-06-26 16:17:31 -05:00
let src = pipeline.by_name("src").unwrap()
2025-06-24 23:48:06 -05:00
.downcast::<gst_app::AppSrc>().unwrap();
2025-06-26 16:17:31 -05:00
src.set_caps(Some(&caps));
// use the dedicated helpers from `AppSrcExt` instead of the generic
// `set_property`, which returns `()` (hence the earlier E0277).
src.set_format(gst::Format::Time); // timestamps are in running-time
// “blocksize=0” → deliver whole access-units (no chunking). The generic
// `set_property()` API returns a `Result<(), glib::BoolError>` so we just
// unwrap: this property always exists on AppSrc.
src.set_property("blocksize", &0u32);
2025-06-24 23:48:06 -05:00
src.set_latency(gst::ClockTime::NONE, gst::ClockTime::NONE);
2025-06-26 16:17:31 -05:00
2025-06-21 05:21:57 -05:00
pipeline.set_state(gst::State::Playing)?;
2025-06-24 23:48:06 -05:00
2025-06-21 05:21:57 -05:00
Ok(Self { id, _window: window, src })
}
pub fn push_packet(&self, pkt: VideoPacket) {
2025-06-24 23:48:06 -05:00
let mut buf = gst::Buffer::from_slice(pkt.data);
buf.get_mut().unwrap()
.set_pts(Some(gst::ClockTime::from_useconds(pkt.pts)));
let _ = self.src.push_buffer(buf);
2025-06-21 05:21:57 -05:00
}
}