use std::time::Duration;
pub(crate) const RING: usize = 256;
pub(crate) const MIN_SAMPLES: usize = 128;
pub(crate) const STEP: usize = 32;
pub(crate) const WARMUP: Duration = Duration::from_secs(1);
pub(crate) const PLAUSIBLE_US: i64 = 60_000_000;
pub(crate) const HYSTERESIS_MS: u32 = 20;
pub(crate) const MIN_BUFFER_MS: u32 = 250;
pub(crate) const MAX_BUFFER_MS: u32 = 5_000;
pub(crate) struct BufferEstimator {
pinned: bool,
samples: [i64; RING],
next: usize,
len: usize,
received: usize,
last_evaluated: usize,
current: u32,
streak: i64,
}
impl BufferEstimator {
pub(crate) fn new(pin: Option<u32>) -> Self {
Self {
pinned: pin.is_some(),
samples: [0; RING],
next: 0,
len: 0,
received: 0,
last_evaluated: 0,
current: pin.unwrap_or(MIN_BUFFER_MS),
streak: 0,
}
}
pub(crate) fn sample(&mut self, delay_us: i64) {
if self.pinned || !(0..=PLAUSIBLE_US).contains(&delay_us) {
return;
}
self.samples[self.next] = delay_us;
self.next = (self.next + 1) % RING;
self.len = (self.len + 1).min(RING);
self.received = self.received.saturating_add(1);
}
pub(crate) fn evaluate(&mut self) -> Option<u32> {
if self.pinned || self.received < MIN_SAMPLES || self.received == self.last_evaluated {
return None;
}
if (self.received - MIN_SAMPLES) % STEP != 0 {
return None;
}
self.last_evaluated = self.received;
let candidate = candidate_ms(&self.samples[..self.len]);
self.consider(candidate)
}
pub(crate) fn value(&self) -> u32 {
self.current
}
fn consider(&mut self, candidate: u32) -> Option<u32> {
let delta = i64::from(candidate) - i64::from(self.current);
if delta.abs() < i64::from(HYSTERESIS_MS) {
self.streak = 0;
return None;
}
let direction = delta.signum();
if self.streak == direction {
self.current = candidate;
self.streak = 0;
return Some(candidate);
}
self.streak = direction;
None
}
}
fn candidate_ms(samples: &[i64]) -> u32 {
let us = u64::try_from(p95_us(samples)).unwrap_or(u64::MAX);
let ms = us.saturating_add(9_999) / 10_000 * 10;
u32::try_from(ms)
.unwrap_or(MAX_BUFFER_MS)
.min(MAX_BUFFER_MS)
}
fn p95_us(samples: &[i64]) -> i64 {
let mut sorted = samples.to_vec();
sorted.sort_unstable();
let rank = (95 * sorted.len()).div_ceil(100);
sorted[rank - 1]
}
#[cfg(test)]
mod tests {
use super::*;
fn feed(estimator: &mut BufferEstimator, count: usize, delay_us: i64) -> Vec<u32> {
let mut applied = Vec::new();
for _ in 0..count {
estimator.sample(delay_us);
if let Some(value) = estimator.evaluate() {
applied.push(value);
}
}
applied
}
#[test]
fn the_percentile_is_the_nearest_rank() {
let values: Vec<i64> = (1..=100).collect();
assert_eq!(p95_us(&values), 95);
let values: Vec<i64> = (1..=20).collect();
assert_eq!(p95_us(&values), 19);
let values: Vec<i64> = (1..=128).collect();
assert_eq!(p95_us(&values), 122);
}
#[test]
fn no_candidate_appears_before_the_minimum_sample_count() {
let mut estimator = BufferEstimator::new(None);
assert_eq!(
feed(&mut estimator, MIN_SAMPLES - 1, 300_000),
Vec::<u32>::new(),
"a window short of its minimum must derive nothing"
);
assert_eq!(estimator.value(), MIN_BUFFER_MS);
assert_eq!(
feed(&mut estimator, 1, 300_000),
Vec::<u32>::new(),
"the first evaluation only enters a streak, it does not apply"
);
assert_eq!(estimator.value(), MIN_BUFFER_MS);
assert_eq!(
feed(&mut estimator, STEP, 300_000),
[300],
"the second same-direction evaluation applies the candidate"
);
assert_eq!(estimator.value(), 300);
}
#[test]
fn a_pinned_estimator_never_derives() {
let mut estimator = BufferEstimator::new(Some(400));
assert_eq!(estimator.value(), 400);
assert_eq!(
feed(&mut estimator, MIN_SAMPLES + 2 * STEP, 300_000),
Vec::<u32>::new(),
"a pinned value must survive any number of samples"
);
assert_eq!(estimator.value(), 400);
}
#[test]
fn a_candidate_rounds_up_to_ten_milliseconds() {
let mut estimator = BufferEstimator::new(None);
assert_eq!(
feed(&mut estimator, MIN_SAMPLES + STEP, 20_001),
[30],
"20.001 ms rounds up to 30 ms"
);
assert_eq!(estimator.value(), 30);
}
#[test]
fn a_candidate_is_clamped_to_the_protocol_range() {
let mut estimator = BufferEstimator::new(None);
assert_eq!(
feed(&mut estimator, MIN_SAMPLES + STEP, PLAUSIBLE_US),
[MAX_BUFFER_MS],
"the upper plausibility bound maps to 6000 ms, which the protocol range clamps to 5000"
);
assert_eq!(estimator.value(), MAX_BUFFER_MS);
}
#[test]
fn a_negative_or_implausible_delay_is_discarded() {
for delay_us in [-1, PLAUSIBLE_US + 1] {
let mut estimator = BufferEstimator::new(None);
assert_eq!(
feed(&mut estimator, MIN_SAMPLES + STEP, delay_us),
Vec::<u32>::new(),
"a delay of {delay_us} us is a clock anomaly, not a buffer requirement"
);
assert_eq!(estimator.value(), MIN_BUFFER_MS);
}
}
#[test]
fn an_evaluation_without_new_samples_changes_nothing() {
let mut estimator = BufferEstimator::new(None);
assert_eq!(
feed(&mut estimator, MIN_SAMPLES, 300_000),
Vec::<u32>::new()
);
assert_eq!(
estimator.evaluate(),
None,
"a repeated evaluation must not count as a second same-direction one"
);
assert_eq!(estimator.value(), MIN_BUFFER_MS);
}
#[test]
fn a_direction_change_discards_the_streak() {
let mut estimator = BufferEstimator::new(None);
assert_eq!(
estimator.consider(300),
None,
"the first upward evaluation only starts a streak"
);
assert_eq!(
estimator.consider(200),
None,
"a downward evaluation discards the upward streak"
);
assert_eq!(
estimator.consider(300),
None,
"the discarded streak must not count towards a change"
);
assert_eq!(
estimator.consider(300),
Some(300),
"two consecutive same-direction evaluations apply the candidate"
);
assert_eq!(estimator.value(), 300);
}
#[test]
fn a_candidate_inside_the_hysteresis_never_applies() {
let mut estimator = BufferEstimator::new(None);
for candidate in [269, 269, 269, 231, 231, 231, 250, 250] {
assert_eq!(
estimator.consider(candidate),
None,
"a difference below {HYSTERESIS_MS} ms must not apply"
);
}
assert_eq!(estimator.value(), MIN_BUFFER_MS);
assert_eq!(estimator.consider(270), None);
assert_eq!(estimator.consider(270), Some(270));
assert_eq!(estimator.value(), 270);
}
}