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) {
|
||||
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);
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -196,6 +196,7 @@
|
||||
lld
|
||||
lldb
|
||||
cargo-audit
|
||||
(crates.buildCrate "cargo-with" {doCheck = false;})
|
||||
]
|
||||
++ (lib.optionals pkgs.stdenv.isDarwin [
|
||||
apple-sdk_26
|
||||
|
||||
@@ -40,6 +40,11 @@ impl AppSink {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_drop(self, drop: bool) -> Self {
|
||||
self.inner.set_property("drop", drop);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_caps(self, caps: Caps) -> Self {
|
||||
self.inner.set_property("caps", caps.inner);
|
||||
self
|
||||
|
||||
@@ -25,6 +25,24 @@ impl Playbin3 {
|
||||
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 {
|
||||
self.inner
|
||||
.set_property("video-sink", &video_sink.upcast_ref().inner);
|
||||
|
||||
Reference in New Issue
Block a user