use serde_json::json;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ClockFit {
pub offset: f64,
pub period: f64,
pub jitter_p50: f64,
pub jitter_p95: f64,
pub packets: usize,
pub nominal: bool,
}
impl ClockFit {
pub fn time(&self, n: f64) -> f64 {
self.offset + self.period * n
}
pub fn to_json(&self) -> serde_json::Value {
json!({
"rate_hz": 1.0 / self.period, "offset_unix_s": self.offset, "packets": self.packets,
"jitter_p50_s": self.jitter_p50, "jitter_p95_s": self.jitter_p95, "nominal_rate_used": self.nominal,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Placed {
pub segment: i64,
pub first_sample: i64,
}
#[derive(Debug, Clone, Copy)]
struct Previous {
arrival: f64,
device_time: u32,
sequence: Option<u8>,
samples: usize,
}
#[derive(Debug, Clone)]
pub struct Segmenter {
rate_hz: f64,
packet_period: f64,
per_second: usize,
previous: Option<Previous>,
segment: i64,
next: i64,
same_second: usize,
pub lost_frames: u64,
pub restarts: u64,
}
impl Segmenter {
pub fn new(rate_hz: f64) -> Self {
Self {
rate_hz,
packet_period: 0.0,
per_second: 0,
previous: None,
segment: 0,
next: 0,
same_second: 0,
lost_frames: 0,
restarts: 0,
}
}
pub fn push(
&mut self,
arrival: f64,
device_time: u32,
sequence: Option<u8>,
samples: usize,
forced: bool,
after_start: bool,
) -> Placed {
let Some(prev) = self.previous else {
self.packet_period = samples as f64 / self.rate_hz;
self.per_second = (1.0 / self.packet_period.max(1e-9)).round() as usize;
self.same_second = 1;
self.previous = Some(Previous {
arrival,
device_time,
sequence,
samples,
});
return Placed {
segment: self.segment,
first_sample: self.next,
};
};
let packet_period = self.packet_period;
let step = match (prev.sequence, sequence) {
(Some(a), Some(b)) => {
let d = i64::from(b.wrapping_sub(a));
if d == 1 {
Some(1)
} else if b == 0 && after_start {
self.restarts += 1;
(arrival - prev.arrival < 4.0 * packet_period + 0.5).then_some(1)
} else if d > 1 && arrival - prev.arrival < (d as f64 + 4.0) * packet_period + 1.0 {
self.lost_frames += (d - 1) as u64;
Some(d)
} else {
None
}
}
_ => {
let gap = arrival - prev.arrival;
let clock_step = i64::from(device_time) - i64::from(prev.device_time);
self.same_second = if clock_step == 0 { self.same_second + 1 } else { 1 };
let burst = self.same_second > self.per_second;
(gap < 2.0 * packet_period.max(0.5) && (0..=1).contains(&clock_step) && !burst).then_some(1)
}
};
let paused = arrival - prev.arrival > 1.0 + 2.0 * packet_period;
match step {
Some(d) if !forced && !paused => self.next += d * prev.samples as i64,
_ => {
self.segment += 1;
self.next = 0;
self.same_second = 1;
}
}
self.previous = Some(Previous {
arrival,
device_time,
sequence,
samples,
});
Placed {
segment: self.segment,
first_sample: self.next,
}
}
}
fn least_squares(points: &[(f64, f64)]) -> Option<(f64, f64)> {
let n = points.len() as f64;
if points.len() < 2 {
return None;
}
let (sx, sy) = points.iter().fold((0.0, 0.0), |(a, b), (x, y)| (a + x, b + y));
let (mx, my) = (sx / n, sy / n);
let (sxx, sxy) = points.iter().fold((0.0, 0.0), |(a, b), (x, y)| {
(a + (x - mx).powi(2), b + (x - mx) * (y - my))
});
if sxx <= 0.0 {
return None;
}
let slope = sxy / sxx;
Some((my - slope * mx, slope))
}
fn percentile(sorted: &[f64], q: f64) -> f64 {
if sorted.is_empty() {
return 0.0;
}
let idx = ((sorted.len() - 1) as f64 * q).round() as usize;
sorted.get(idx).copied().unwrap_or(0.0)
}
pub fn fit_lower_envelope(points: &[(f64, f64)], nominal_rate_hz: f64) -> ClockFit {
let nominal_period = 1.0 / nominal_rate_hz;
let (offset, period, nominal) = if points.len() >= 20 {
let mut fit = least_squares(points).unwrap_or((0.0, nominal_period));
for _ in 0..4 {
let mut residuals: Vec<f64> = points.iter().map(|(x, y)| y - (fit.0 + fit.1 * x)).collect();
residuals.sort_by(f64::total_cmp);
let cut = percentile(&residuals, 0.10);
let low: Vec<(f64, f64)> = points
.iter()
.copied()
.filter(|(x, y)| y - (fit.0 + fit.1 * x) <= cut)
.collect();
match least_squares(&low) {
Some(f) if (f.1 / nominal_period - 1.0).abs() < 0.05 => fit = f,
_ => break,
}
}
(fit.0, fit.1, false)
} else {
(0.0, nominal_period, true)
};
place_on_envelope(points, offset, period, nominal_period, nominal)
}
pub fn fit_nominal(points: &[(f64, f64)], nominal_rate_hz: f64) -> ClockFit {
let nominal_period = 1.0 / nominal_rate_hz;
place_on_envelope(points, 0.0, nominal_period, nominal_period, true)
}
fn place_on_envelope(
points: &[(f64, f64)],
mut offset: f64,
mut period: f64,
nominal_period: f64,
nominal: bool,
) -> ClockFit {
let min_residual = points
.iter()
.map(|(x, y)| y - (offset + period * x))
.fold(f64::INFINITY, f64::min);
if min_residual.is_finite() {
offset += min_residual;
}
if !period.is_finite() || period <= 0.0 {
period = nominal_period;
}
let mut delays: Vec<f64> = points.iter().map(|(x, y)| y - (offset + period * x)).collect();
delays.sort_by(f64::total_cmp);
ClockFit {
offset,
period,
jitter_p50: percentile(&delays, 0.5),
jitter_p95: percentile(&delays, 0.95),
packets: points.len(),
nominal,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn recovers_rate_and_offset_under_one_sided_jitter() {
let true_period = 1.0 / 125.3;
let mut state = 7u64;
let points: Vec<(f64, f64)> = (0..2000)
.map(|k| {
state = state
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
let delay = 0.005 + 0.145 * ((state >> 33) as f64 / (1u64 << 31) as f64).powi(3);
let n = (9 * k + 8) as f64;
(n, 1_800_000_000.0 + true_period * n + delay)
})
.collect();
let fit = fit_lower_envelope(&points, 125.0);
assert!((1.0 / fit.period - 125.3).abs() < 0.01, "rate {}", 1.0 / fit.period);
assert!(
(fit.offset - 1_800_000_000.005).abs() < 0.003,
"offset error {}",
fit.offset - 1_800_000_000.005
);
assert!(points.iter().all(|(x, y)| *y >= fit.time(*x) - 1e-9));
}
#[test]
fn short_segments_use_nominal_rate() {
let points = [(8.0, 10.1), (17.0, 10.2)];
let fit = fit_lower_envelope(&points, 125.0);
assert!(fit.nominal);
assert!((fit.period - 0.008).abs() < 1e-12);
}
}