const PROCESS_STD_DEV: f64 = 0.0;
const DRIFT_PROCESS_STD_DEV: f64 = 1e-11;
const FORGET_FACTOR: f64 = 2.0;
const ADAPTIVE_CUTOFF: f64 = 3.0;
const MAX_ERROR_SCALE: f64 = 0.5;
const DRIFT_SIGNIFICANCE_THRESHOLD: f64 = 2.0;
const ADAPTIVE_FORGETTING_AFTER: usize = 100;
pub const READY_AFTER_SAMPLES: usize = 3;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimeSample {
pub client_sent_us: i64,
pub server_received_us: i64,
pub server_sent_us: i64,
pub client_received_us: i64,
}
#[derive(Debug, Clone)]
pub struct ClockFilter {
offset: f64,
drift: f64,
p_oo: f64,
p_od: f64,
p_dd: f64,
last_accepted_us: i64,
count: usize,
use_drift: bool,
}
impl Default for ClockFilter {
fn default() -> Self {
Self::new()
}
}
impl ClockFilter {
pub fn new() -> Self {
Self {
offset: 0.0,
drift: 0.0,
p_oo: f64::INFINITY,
p_od: 0.0,
p_dd: 0.0,
last_accepted_us: 0,
count: 0,
use_drift: false,
}
}
pub fn push(&mut self, sample: TimeSample) {
let t1 = sample.client_sent_us as i128;
let t2 = sample.server_received_us as i128;
let t3 = sample.server_sent_us as i128;
let t4 = sample.client_received_us as i128;
let measurement_i = ((t2 - t1) + (t3 - t4)) / 2;
let max_error_i = ((t4 - t1) - (t3 - t2)) / 2;
if sample.client_received_us <= self.last_accepted_us || max_error_i <= 0 {
return;
}
let measurement = measurement_i as f64;
let max_error = max_error_i as f64;
let dt = sample
.client_received_us
.saturating_sub(self.last_accepted_us) as f64;
self.last_accepted_us = sample.client_received_us;
let r = (MAX_ERROR_SCALE * max_error).powi(2);
if self.count == 0 {
self.offset = measurement;
self.p_oo = r;
self.drift = 0.0;
self.count = 1;
return;
}
if self.count == 1 {
self.drift = (measurement - self.offset) / dt;
self.p_dd = (self.p_oo + r) / (dt * dt);
self.offset = measurement;
self.p_oo = r;
self.count = 2;
return;
}
let offset_p = self.offset + self.drift * dt;
let mut p_oo_p =
self.p_oo + 2.0 * self.p_od * dt + self.p_dd * dt * dt + PROCESS_STD_DEV.powi(2) * dt;
let mut p_od_p = self.p_od + self.p_dd * dt;
let mut p_dd_p = self.p_dd + DRIFT_PROCESS_STD_DEV.powi(2) * dt;
let residual = measurement - offset_p;
if self.count >= ADAPTIVE_FORGETTING_AFTER && residual.abs() > ADAPTIVE_CUTOFF * max_error {
let scale = FORGET_FACTOR.powi(2);
p_oo_p *= scale;
p_od_p *= scale;
p_dd_p *= scale;
}
let s = p_oo_p + r;
let k_o = p_oo_p / s;
let k_d = p_od_p / s;
self.offset = offset_p + k_o * residual;
self.drift += k_d * residual;
self.p_oo = p_oo_p - k_o * p_oo_p;
self.p_od = p_od_p - k_d * p_oo_p;
self.p_dd = p_dd_p - k_d * p_od_p;
self.count = self.count.saturating_add(1);
self.use_drift = self.drift * self.drift > DRIFT_SIGNIFICANCE_THRESHOLD.powi(2) * self.p_dd;
}
#[must_use]
pub fn is_ready(&self) -> bool {
self.count >= READY_AFTER_SAMPLES
}
#[must_use]
pub fn now_us(&self, local_us: i64) -> Option<i64> {
if !self.is_ready() {
return None;
}
let d = if self.use_drift { self.drift } else { 0.0 };
Some(round_away_from_zero(
local_us as f64 + self.offset + d * (local_us as f64 - self.last_accepted_us as f64),
))
}
#[must_use]
pub fn client_time_us(&self, server_us: i64) -> Option<i64> {
if !self.is_ready() {
return None;
}
let d = if self.use_drift { self.drift } else { 0.0 };
Some(round_away_from_zero(
(server_us as f64 - self.offset + d * self.last_accepted_us as f64) / (1.0 + d),
))
}
#[must_use]
pub fn offset_us(&self) -> Option<i64> {
if !self.is_ready() {
return None;
}
Some(round_away_from_zero(self.offset))
}
#[must_use]
pub fn last_sample_us(&self) -> i64 {
self.last_accepted_us
}
#[must_use]
pub fn sample_count(&self) -> usize {
self.count
}
pub fn reset(&mut self) {
*self = Self::new();
}
}
fn round_away_from_zero(value: f64) -> i64 {
value.round() as i64
}