fix(video): try to optimize memory leaks
This commit is contained in:
@@ -99,11 +99,14 @@ impl iced_wgpu::Primitive for VideoFrame {
|
|||||||
if video.ready.load(std::sync::atomic::Ordering::SeqCst) {
|
if video.ready.load(std::sync::atomic::Ordering::SeqCst) {
|
||||||
let now = std::time::Instant::now();
|
let now = std::time::Instant::now();
|
||||||
let frame = self.frame.lock().expect("BUG: Mutex poisoned");
|
let frame = self.frame.lock().expect("BUG: Mutex poisoned");
|
||||||
let frame = frame
|
let buffer = frame
|
||||||
.buffer()
|
.buffer()
|
||||||
.and_then(|b| b.map_readable().ok())
|
|
||||||
.expect("BUG: Failed to get frame data from gst::Sample");
|
.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
|
video
|
||||||
.ready
|
.ready
|
||||||
.store(false, std::sync::atomic::Ordering::SeqCst);
|
.store(false, std::sync::atomic::Ordering::SeqCst);
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ use std::sync::{Arc, Mutex, atomic::AtomicBool};
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct VideoSource {
|
pub struct VideoSource {
|
||||||
pub(crate) playbin: Playbin3,
|
pub(crate) playbin: Playbin3,
|
||||||
pub(crate) videoconvert: VideoConvert,
|
// pub(crate) videoconvert: VideoConvert,
|
||||||
pub(crate) appsink: AppSink,
|
pub(crate) appsink: AppSink,
|
||||||
pub(crate) bus: Bus,
|
pub(crate) bus: Bus,
|
||||||
pub(crate) ready: Arc<AtomicBool>,
|
pub(crate) ready: Arc<AtomicBool>,
|
||||||
@@ -26,22 +26,25 @@ impl VideoSource {
|
|||||||
/// now.
|
/// now.
|
||||||
pub fn new(url: impl AsRef<str>) -> Result<Self> {
|
pub fn new(url: impl AsRef<str>) -> Result<Self> {
|
||||||
Gst::new();
|
Gst::new();
|
||||||
let videoconvert = VideoConvert::new("iced-video-convert")
|
// let videoconvert = VideoConvert::new("iced-video-convert")
|
||||||
// .change_context(Error)?
|
// // .change_context(Error)?
|
||||||
// .with_output_format(gst::plugins::videoconvertscale::VideoFormat::Rgba)
|
// // .with_output_format(gst::plugins::videoconvertscale::VideoFormat::Rgba)
|
||||||
.change_context(Error)?;
|
// .change_context(Error)?;
|
||||||
let appsink = AppSink::new("iced-video-sink")
|
let appsink = AppSink::new("iced-video-sink")
|
||||||
.change_context(Error)?
|
.change_context(Error)?
|
||||||
.with_caps(
|
.with_drop(true);
|
||||||
Caps::builder(CapsType::Video)
|
// .with_caps(
|
||||||
.field("format", "RGBA")
|
// Caps::builder(CapsType::Video)
|
||||||
.build(),
|
// .field("format", "RGBA")
|
||||||
);
|
// .build(),
|
||||||
let video_sink = videoconvert.link(&appsink).change_context(Error)?;
|
// );
|
||||||
|
// let video_sink = videoconvert.link(&appsink).change_context(Error)?;
|
||||||
let playbin = gst::plugins::playback::Playbin3::new("iced-video")
|
let playbin = gst::plugins::playback::Playbin3::new("iced-video")
|
||||||
.change_context(Error)?
|
.change_context(Error)?
|
||||||
.with_uri(url.as_ref())
|
.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)?;
|
let bus = playbin.bus().change_context(Error)?;
|
||||||
playbin.pause().change_context(Error)?;
|
playbin.pause().change_context(Error)?;
|
||||||
let ready = Arc::new(AtomicBool::new(false));
|
let ready = Arc::new(AtomicBool::new(false));
|
||||||
@@ -57,12 +60,8 @@ impl VideoSource {
|
|||||||
};
|
};
|
||||||
{
|
{
|
||||||
let mut guard = frame.lock().expect("BUG: Mutex poisoned");
|
let mut guard = frame.lock().expect("BUG: Mutex poisoned");
|
||||||
let old_sample = core::mem::replace(&mut *guard, sample);
|
core::mem::replace(&mut *guard, sample);
|
||||||
// dbg!(old_sample.caps());
|
|
||||||
// dbg!(old_sample.inner.buffer_list());
|
|
||||||
// drop(old_sample);
|
|
||||||
ready.store(true, std::sync::atomic::Ordering::Relaxed);
|
ready.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||||
drop(guard);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -71,7 +70,7 @@ impl VideoSource {
|
|||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
playbin,
|
playbin,
|
||||||
videoconvert,
|
// videoconvert,
|
||||||
appsink,
|
appsink,
|
||||||
bus,
|
bus,
|
||||||
ready,
|
ready,
|
||||||
|
|||||||
@@ -196,6 +196,7 @@
|
|||||||
lld
|
lld
|
||||||
lldb
|
lldb
|
||||||
cargo-audit
|
cargo-audit
|
||||||
|
(crates.buildCrate "cargo-with" {doCheck = false;})
|
||||||
]
|
]
|
||||||
++ (lib.optionals pkgs.stdenv.isDarwin [
|
++ (lib.optionals pkgs.stdenv.isDarwin [
|
||||||
apple-sdk_26
|
apple-sdk_26
|
||||||
|
|||||||
@@ -40,6 +40,11 @@ impl AppSink {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_drop(self, drop: bool) -> Self {
|
||||||
|
self.inner.set_property("drop", drop);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn with_caps(self, caps: Caps) -> Self {
|
pub fn with_caps(self, caps: Caps) -> Self {
|
||||||
self.inner.set_property("caps", caps.inner);
|
self.inner.set_property("caps", caps.inner);
|
||||||
self
|
self
|
||||||
|
|||||||
@@ -25,6 +25,24 @@ impl Playbin3 {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_buffer_duration(self, duration: impl Into<Option<core::time::Duration>>) -> Self {
|
||||||
|
let duration = match duration.into() {
|
||||||
|
Some(dur) => dur.as_secs() as i64,
|
||||||
|
None => -1,
|
||||||
|
};
|
||||||
|
self.inner.set_property("buffer-duration", duration);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_buffer_size(self, size: impl Into<Option<u32>>) -> Self {
|
||||||
|
let size = match size.into() {
|
||||||
|
Some(size) => size as i32,
|
||||||
|
None => -1,
|
||||||
|
};
|
||||||
|
self.inner.set_property("buffer-size", size);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn with_video_sink(self, video_sink: &impl ChildOf<Element>) -> Self {
|
pub fn with_video_sink(self, video_sink: &impl ChildOf<Element>) -> Self {
|
||||||
self.inner
|
self.inner
|
||||||
.set_property("video-sink", &video_sink.upcast_ref().inner);
|
.set_property("video-sink", &video_sink.upcast_ref().inner);
|
||||||
|
|||||||
Reference in New Issue
Block a user