use crate::rtpfb::acknowledgement::PacketReport;
use std::collections::VecDeque;
use std::time::{Duration, Instant};
pub const QUEUE_BASE_WINDOW: Duration = Duration::from_secs(30);
pub const QUEUE_HIGH_WINDOW: Duration = Duration::from_secs(5);
pub const QUEUE_AT_LIMIT_SHARE: f64 = 0.75;
pub const QUEUE_AT_LIMIT_MIN_DELAY: Duration = Duration::from_millis(10);
#[derive(Debug, Clone, Default)]
pub struct QueueLimitDetector {
reference: Option<Instant>,
base: VecDeque<(Instant, f64)>,
high: VecDeque<(Instant, f64)>,
}
impl QueueLimitDetector {
pub fn new() -> Self {
Self::default()
}
fn one_way_ms(&mut self, departure: Instant, arrival: Duration) -> f64 {
let reference = *self.reference.get_or_insert(departure);
let sent = match departure.checked_duration_since(reference) {
Some(after) => after.as_secs_f64(),
None => -reference.saturating_duration_since(departure).as_secs_f64(),
};
(arrival.as_secs_f64() - sent) * 1_000.0
}
fn add_base(&mut self, now: Instant, one_way_ms: f64) {
match self.base.back_mut() {
Some((start, lowest))
if now.saturating_duration_since(*start) < Duration::from_secs(1) =>
{
*lowest = lowest.min(one_way_ms);
}
_ => self.base.push_back((now, one_way_ms)),
}
while self
.base
.front()
.is_some_and(|(start, _)| now.saturating_duration_since(*start) > QUEUE_BASE_WINDOW)
{
self.base.pop_front();
}
}
pub fn on_reports(&mut self, now: Instant, reports: &[PacketReport]) -> bool {
let one_way: Vec<f64> = reports
.iter()
.filter(|report| report.arrived)
.filter_map(|report| Some(self.one_way_ms(report.departure, report.arrival?)))
.collect();
for delay in &one_way {
self.add_base(now, *delay);
}
let Some(base) = self.base.iter().map(|(_, lowest)| *lowest).reduce(f64::min) else {
return false;
};
let (Some(lowest), Some(highest)) = (
one_way.iter().copied().reduce(f64::min),
one_way.iter().copied().reduce(f64::max),
) else {
return false;
};
self.high.push_back((now, highest - base));
while self
.high
.front()
.is_some_and(|(at, _)| now.saturating_duration_since(*at) > QUEUE_HIGH_WINDOW)
{
self.high.pop_front();
}
let recent_high = self.high.iter().map(|(_, high)| *high).fold(0.0, f64::max);
let waited = lowest - base;
reports.iter().any(|report| !report.arrived)
&& waited >= QUEUE_AT_LIMIT_MIN_DELAY.as_secs_f64() * 1_000.0
&& waited >= QUEUE_AT_LIMIT_SHARE * recent_high
}
}
#[cfg(test)]
mod tests {
use super::*;
use rtcp::transport_feedbacks::cc_feedback_report::Ecn;
fn batch(
epoch: Instant,
first: u64,
count: u64,
queue_ms: impl Fn(u64) -> u64,
lost: &[u64],
) -> Vec<PacketReport> {
(first..first + count)
.map(|id| {
let arrived = !lost.contains(&id);
PacketReport {
ssrc: 1,
id,
rtp_sequence_number: id as u16,
is_twcc: true,
twcc_sequence_number: id as u16,
size: 1200,
arrived,
departure: epoch + Duration::from_millis(id * 10),
arrival: arrived
.then(|| Duration::from_millis(1_000 + id * 10 + 20 + queue_ms(id))),
ecn: Ecn::default(),
}
})
.collect()
}
#[test]
fn a_loss_out_of_a_full_queue_is_at_the_limit() {
let epoch = Instant::now();
let mut detector = QueueLimitDetector::new();
assert!(!detector.on_reports(epoch, &batch(epoch, 0, 10, |_| 0, &[])));
for round in 1..10u64 {
let at = epoch + Duration::from_millis(round * 100);
let reports = batch(epoch, round * 10, 10, |id| (id * 2).min(200), &[]);
assert!(!detector.on_reports(at, &reports), "no loss, round {round}");
}
let reports = batch(epoch, 100, 10, |_| 200, &[104]);
assert!(detector.on_reports(epoch + Duration::from_secs(1), &reports));
}
#[test]
fn a_loss_with_no_queue_is_not_at_the_limit() {
let epoch = Instant::now();
let mut detector = QueueLimitDetector::new();
for round in 0..10u64 {
let at = epoch + Duration::from_millis(round * 100);
let reports = batch(epoch, round * 10, 10, |_| 0, &[round * 10 + 3]);
assert!(!detector.on_reports(at, &reports), "round {round}");
}
}
#[test]
fn a_loss_on_a_jittery_path_is_not_at_the_limit() {
let epoch = Instant::now();
let mut detector = QueueLimitDetector::new();
for round in 0..20u64 {
let at = epoch + Duration::from_millis(round * 100);
let reports = batch(
epoch,
round * 10,
10,
|id| (id * 37) % 60,
&[round * 10 + 5],
);
assert!(!detector.on_reports(at, &reports), "round {round}");
}
}
}