feat: Modify gst crate to add lot of more granularity

This commit is contained in:
uttarayan21
2025-12-22 13:27:30 +05:30
parent d42ef3b550
commit 043d1e99f0
23 changed files with 947 additions and 392 deletions

View File

@@ -1,5 +1,6 @@
use crate::*;
use crate::priv_prelude::*;
#[derive(Debug, Clone)]
pub struct AppSink {
inner: gstreamer::Element,
}
@@ -32,9 +33,9 @@ impl AppSink {
Ok(AppSink { inner })
}
pub fn with_caps(mut self, caps: &gstreamer::Caps) -> Self {
pub fn with_caps(mut self, caps: Caps) -> Self {
use gstreamer::prelude::*;
// self.inner.set_caps(Some(caps));
self.inner.set_property("caps", caps.inner);
self
}
@@ -43,7 +44,7 @@ impl AppSink {
Ok(())
}
pub fn pull_sample(&self, timeout: impl Into<Option<core::time::Duration>>) -> Result<Sample> {
pub fn pull_sample(&self) -> Result<Sample> {
use gstreamer::prelude::*;
self.appsink()
.pull_sample()
@@ -62,7 +63,7 @@ impl AppSink {
.map(From::from))
}
pub fn pull_preroll(&self, timeout: impl Into<Option<core::time::Duration>>) -> Result<Sample> {
pub fn pull_preroll(&self) -> Result<Sample> {
use gstreamer::prelude::*;
self.appsink()
.pull_preroll()
@@ -83,26 +84,106 @@ impl AppSink {
}
}
fn duration_to_clocktime(
timeout: impl Into<Option<core::time::Duration>>,
) -> Result<Option<gstreamer::ClockTime>> {
match timeout.into() {
Some(dur) => {
let clocktime = gstreamer::ClockTime::try_from(dur)
.change_context(Error)
.attach("Failed to convert duration to ClockTime")?;
Ok(Some(clocktime))
}
None => Ok(None),
}
}
pub struct Sample {
inner: gstreamer::Sample,
}
impl From<gstreamer::Sample> for Sample {
fn from(inner: gstreamer::Sample) -> Self {
Sample { inner }
}
}
#[repr(transparent)]
pub struct Sample {
inner: gstreamer::Sample,
}
use gstreamer::BufferRef;
impl Sample {
pub fn buffer(&self) -> Option<&BufferRef> {
self.inner.buffer()
}
pub fn caps(&self) -> Option<&gstreamer::CapsRef> {
self.inner.caps()
}
}
#[test]
fn test_appsink() {
use gstreamer::prelude::*;
use tracing_subscriber::prelude::*;
tracing_subscriber::registry()
.with(
tracing_subscriber::fmt::layer()
.with_thread_ids(true)
.with_file(true),
)
.init();
tracing::info!("Linking videoconvert to appsink");
Gst::new();
let playbin3 = playback::Playbin3::new("pppppppppppppppppppppppppppppp").unwrap().with_uri("https://jellyfin.tsuba.darksailor.dev/Items/6010382cf25273e624d305907010d773/Download?api_key=036c140222464878862231ef66a2bc9c");
let video_convert = plugins::videoconvertscale::VideoConvert::new("vcvcvcvcvcvcvcvcvcvcvcvcvc")
.expect("Create videoconvert");
let appsink = app::AppSink::new("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
.expect("Create appsink")
.with_caps(
Caps::builder(CapsType::Video)
.field("format", "RGB")
.build(),
);
let mut video_sink = video_convert
.link(&appsink)
.expect("Link videoconvert to appsink");
let playbin3 = playbin3.with_video_sink(&video_sink);
let bus = playbin3.bus().unwrap();
// playbin3.play().expect("Play playbin3");
std::thread::spawn({
let playbin3 = playbin3.clone();
move || {
loop {
std::thread::sleep(std::time::Duration::from_secs(5));
playbin3.play();
playbin3.wait_non_null_sync();
let sample = appsink
.try_pull_sample(core::time::Duration::from_secs(5))
.expect("Pull sample from appsink")
.expect("No sample received from appsink");
dbg!(sample.caps());
playbin3.play();
tracing::info!("Played");
}
}
});
for msg in bus.iter_timed(None) {
match msg.view() {
gstreamer::MessageView::Eos(..) => {
tracing::info!("End of stream reached");
break;
}
gstreamer::MessageView::Error(err) => {
tracing::error!(
"Error from {:?}: {} ({:?})",
err.src().map(|s| s.path_string()),
err.error(),
err.debug()
);
break;
}
gstreamer::MessageView::StateChanged(state) => {
eprintln!(
"State changed from {:?} to \x1b[33m{:?}\x1b[0m for {:?}",
state.old(),
state.current(),
state.src().map(|s| s.path_string())
);
}
_ => {}
}
// tracing::info!("{:#?}", &msg.view());
}
// std::thread::sleep(std::time::Duration::from_secs(5));
}