From 6567041a3d2948685368920954eabf0f26d0c0f6 Mon Sep 17 00:00:00 2001 From: "Jan Alexander Steffens (heftig)" Date: Wed, 25 Oct 2023 00:00:53 +0200 Subject: [PATCH] livesync: Improve audio duration fixups - An entirely missing duration is now only logged at debug level instead of pretending the duration was zero and warning about it. - Silently fix up a duration difference up to one sample. - Error when we fail to calculate the duration; don't try to apply the `fallback_duration` to a non-video stream. Part-of: --- utils/livesync/src/livesync/imp.rs | 43 +++++++++++++++++------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/utils/livesync/src/livesync/imp.rs b/utils/livesync/src/livesync/imp.rs index 418862db..15e922ee 100644 --- a/utils/livesync/src/livesync/imp.rs +++ b/utils/livesync/src/livesync/imp.rs @@ -850,20 +850,31 @@ impl LiveSync { } if let Some(audio_info) = &state.in_audio_info { - let buf_duration = buf_mut.duration().unwrap_or_default(); - if let Some(calc_duration) = audio_info - .convert::>(Some(gst::format::Bytes::from_usize( - buf_mut.size(), - ))) + let Some(calc_duration) = audio_info + .convert::>(gst::format::Bytes::from_usize(buf_mut.size())) .flatten() - { + else { + gst::error!( + CAT, + imp: self, + "Failed to calculate duration of {:?}", + buf_mut, + ); + return Err(gst::FlowError::Error); + }; + + if let Some(buf_duration) = buf_mut.duration() { let diff = if buf_duration < calc_duration { calc_duration - buf_duration } else { buf_duration - calc_duration }; - if diff.nseconds() > 1 { + let sample_duration = gst::ClockTime::SECOND + .mul_div_round(1, audio_info.rate().into()) + .unwrap(); + + if diff > sample_duration { gst::warning!( CAT, imp: self, @@ -871,16 +882,15 @@ impl LiveSync { buf_duration, calc_duration, ); - buf_mut.set_duration(calc_duration); } } else { - gst::debug!( - CAT, - imp: self, - "Failed to calculate duration of {:?}", - buf_mut, - ); + gst::debug!(CAT, imp: self, "Incoming buffer without duration"); } + + buf_mut.set_duration(calc_duration); + } else if buf_mut.duration().is_none() { + gst::debug!(CAT, imp: self, "Incoming buffer without duration"); + buf_mut.set_duration(state.fallback_duration); } // At this stage we should really really have a segment @@ -905,11 +915,6 @@ impl LiveSync { buf_mut.set_pts(pts.map(|t| t + state.latency)); } - if buf_mut.duration().is_none() { - gst::debug!(CAT, imp: self, "Incoming buffer without duration"); - buf_mut.set_duration(Some(state.fallback_duration)); - } - if state .out_buffer .as_ref()