fix(video): try to optimize memory leaks
Some checks failed
build / checks-matrix (push) Has been cancelled
build / codecov (push) Has been cancelled
docs / docs (push) Has been cancelled
build / checks-build (push) Has been cancelled

This commit is contained in:
uttarayan21
2025-12-25 06:28:52 +05:30
parent 5a0bdae84b
commit a2491695b3
5 changed files with 47 additions and 21 deletions

View File

@@ -99,11 +99,14 @@ impl iced_wgpu::Primitive for VideoFrame {
if video.ready.load(std::sync::atomic::Ordering::SeqCst) {
let now = std::time::Instant::now();
let frame = self.frame.lock().expect("BUG: Mutex poisoned");
let frame = frame
let buffer = frame
.buffer()
.and_then(|b| b.map_readable().ok())
.expect("BUG: Failed to get frame data from gst::Sample");
queue.write_buffer(&video.buffer, 0, &frame);
let data = buffer
.map_readable()
.expect("BUG: Failed to map gst::Buffer readable");
queue.write_buffer(&video.buffer, 0, &data);
video
.ready
.store(false, std::sync::atomic::Ordering::SeqCst);

View File

@@ -13,7 +13,7 @@ use std::sync::{Arc, Mutex, atomic::AtomicBool};
#[derive(Debug, Clone)]
pub struct VideoSource {
pub(crate) playbin: Playbin3,
pub(crate) videoconvert: VideoConvert,
// pub(crate) videoconvert: VideoConvert,
pub(crate) appsink: AppSink,
pub(crate) bus: Bus,
pub(crate) ready: Arc<AtomicBool>,
@@ -26,22 +26,25 @@ impl VideoSource {
/// now.
pub fn new(url: impl AsRef<str>) -> Result<Self> {
Gst::new();
let videoconvert = VideoConvert::new("iced-video-convert")
// .change_context(Error)?
// .with_output_format(gst::plugins::videoconvertscale::VideoFormat::Rgba)
.change_context(Error)?;
// let videoconvert = VideoConvert::new("iced-video-convert")
// // .change_context(Error)?
// // .with_output_format(gst::plugins::videoconvertscale::VideoFormat::Rgba)
// .change_context(Error)?;
let appsink = AppSink::new("iced-video-sink")
.change_context(Error)?
.with_caps(
Caps::builder(CapsType::Video)
.field("format", "RGBA")
.build(),
);
let video_sink = videoconvert.link(&appsink).change_context(Error)?;
.with_drop(true);
// .with_caps(
// Caps::builder(CapsType::Video)
// .field("format", "RGBA")
// .build(),
// );
// let video_sink = videoconvert.link(&appsink).change_context(Error)?;
let playbin = gst::plugins::playback::Playbin3::new("iced-video")
.change_context(Error)?
.with_uri(url.as_ref())
.with_video_sink(&video_sink);
.with_buffer_duration(core::time::Duration::from_secs(2))
.with_buffer_size(2000000)
.with_video_sink(&appsink);
let bus = playbin.bus().change_context(Error)?;
playbin.pause().change_context(Error)?;
let ready = Arc::new(AtomicBool::new(false));
@@ -57,12 +60,8 @@ impl VideoSource {
};
{
let mut guard = frame.lock().expect("BUG: Mutex poisoned");
let old_sample = core::mem::replace(&mut *guard, sample);
// dbg!(old_sample.caps());
// dbg!(old_sample.inner.buffer_list());
// drop(old_sample);
core::mem::replace(&mut *guard, sample);
ready.store(true, std::sync::atomic::Ordering::Relaxed);
drop(guard);
}
Ok(())
@@ -71,7 +70,7 @@ impl VideoSource {
Ok(Self {
playbin,
videoconvert,
// videoconvert,
appsink,
bus,
ready,