fix: Try to minimize frame latency
This commit is contained in:
@@ -17,7 +17,7 @@ pub struct VideoSource {
|
||||
pub(crate) appsink: AppSink,
|
||||
pub(crate) bus: Bus,
|
||||
pub(crate) ready: Arc<AtomicBool>,
|
||||
pub(crate) frame: Arc<Mutex<Vec<u8>>>,
|
||||
pub(crate) frame: Arc<Mutex<gst::app::Sample>>,
|
||||
}
|
||||
|
||||
impl VideoSource {
|
||||
@@ -45,41 +45,26 @@ impl VideoSource {
|
||||
let bus = playbin.bus().change_context(Error)?;
|
||||
playbin.pause().change_context(Error)?;
|
||||
let ready = Arc::new(AtomicBool::new(false));
|
||||
let frame = Arc::new(Mutex::new(Vec::new()));
|
||||
let frame = Arc::new(Mutex::new(gst::app::Sample::new()));
|
||||
|
||||
let appsink = appsink.on_new_frame({
|
||||
let ready = Arc::clone(&ready);
|
||||
let frame = Arc::clone(&frame);
|
||||
move |appsink| {
|
||||
let Ok(sample) = appsink.pull_sample() else {
|
||||
tracing::error!("Failed to pull video sample from appsink despite being notified of new frame");
|
||||
return Ok(());
|
||||
};
|
||||
let caps = sample.caps().ok_or(gst::gstreamer::FlowError::Error)?;
|
||||
let structure_0 = caps.structure(0).ok_or(gst::gstreamer::FlowError::Error)?;
|
||||
let width = structure_0
|
||||
.get::<i32>("width")
|
||||
.map_err(|_| gst::gstreamer::FlowError::Error)?;
|
||||
let height = structure_0
|
||||
.get::<i32>("height")
|
||||
.map_err(|_| gst::gstreamer::FlowError::Error)?;
|
||||
|
||||
let buffer = sample.buffer().and_then(|b| b.map_readable().ok());
|
||||
if let Some(buffer) = buffer {
|
||||
{
|
||||
let mut frame = frame.lock().expect("BUG: Mutex poisoned");
|
||||
debug_assert_eq!(buffer.size(), (width * height * 4) as usize);
|
||||
if frame.len() != buffer.size() {
|
||||
frame.resize(buffer.size(), 0);
|
||||
}
|
||||
frame.copy_from_slice(buffer.as_slice());
|
||||
ready.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
// if written.is_err() {
|
||||
// tracing::error!("Failed to write video frame to buffer");
|
||||
// } else {
|
||||
// ready.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
// }
|
||||
{
|
||||
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);
|
||||
ready.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
drop(guard);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user