From c32cb20906346a5763edeedd84422389eb51c199 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Fri, 6 Sep 2024 10:35:05 +0300 Subject: [PATCH] mpegtslivesrc: Check if old compared to new PCR clock estimation is too far off It the difference between the two estimations is more than 1s then consider this a discontinuity too. Part-of: --- net/mpegtslive/src/mpegtslive/imp.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/net/mpegtslive/src/mpegtslive/imp.rs b/net/mpegtslive/src/mpegtslive/imp.rs index d28987aa..33555ed7 100644 --- a/net/mpegtslive/src/mpegtslive/imp.rs +++ b/net/mpegtslive/src/mpegtslive/imp.rs @@ -185,7 +185,33 @@ impl MpegTSLiveSourceState { (self.base_pcr, self.base_monotonic, self.last_seen_pcr) { gst::trace!(CAT, "pcr:{pcr}, monotonic_time:{monotonic_time}"); - if let Some(handled_pcr) = MpegTsPcr::new_with_reference(pcr, &last_seen_pcr) { + + let mut handled_pcr = MpegTsPcr::new_with_reference(pcr, &last_seen_pcr); + if let Some(new_pcr) = handled_pcr { + // First check if this is more than 1s off from the current clock calibration and + // if so consider it a discontinuity too. + let (internal, external, num, denom) = self.external_clock.calibration(); + + let expected_external = gst::Clock::adjust_with_calibration( + monotonic_time, + internal, + external, + num, + denom, + ); + let new_external = + gst::ClockTime::from(new_pcr.saturating_sub(base_pcr)) + base_monotonic; + if expected_external.absdiff(new_external) >= gst::ClockTime::SECOND { + gst::warning!( + CAT, + "New PCR clock estimation {new_external} too far from old estimation {expected_external}: {}", + new_external.into_positive() - expected_external, + ); + handled_pcr = None; + } + } + + if let Some(handled_pcr) = handled_pcr { new_pcr = handled_pcr; gst::trace!( CAT,