Skip to main content

buffering_runtime/
policy.rs

1//! Target-buffer policy: need, clamp, asymmetric slew-limited follower and
2//! the resume threshold. All integer (Q8 multipliers, microseconds).
3//!
4//! `max_us` is a hard cap: [`Policy::target_us`], [`Policy::start_us`] and
5//! [`Policy::resume_us`] are never above it, whatever the estimator reports.
6
7use crate::config::{Micros, PolicyConfig, Q8_SHIFT, US_PER_S, mul_q};
8
9/// Target follower and threshold calculator.
10#[derive(Clone, Copy, Debug)]
11pub struct Policy {
12    cfg: PolicyConfig,
13    target: Micros,
14    last_t: Option<u64>,
15}
16
17impl Policy {
18    /// New policy at the floor target.
19    #[must_use]
20    pub fn new(cfg: PolicyConfig) -> Self {
21        Self {
22            cfg,
23            target: cfg.min_us,
24            last_t: None,
25        }
26    }
27
28    /// Unclamped need:
29    /// `max(k1 * baseline + margin, k2 * stall_peak, granularity + margin)`
30    /// (the last term only when `granularity_us > 0`).
31    #[must_use]
32    pub fn need_with_granularity_us(
33        &self,
34        baseline_us: Micros,
35        stall_peak_us: Micros,
36        granularity_us: Micros,
37    ) -> Micros {
38        let a = mul_q(baseline_us, self.cfg.k1_q8, Q8_SHIFT).saturating_add(self.cfg.margin_us);
39        let b = mul_q(stall_peak_us, self.cfg.k2_q8, Q8_SHIFT);
40        let c = self.granularity_need_us(granularity_us);
41        a.max(b).max(c)
42    }
43
44    /// Unclamped need without a granularity floor:
45    /// `max(k1 * baseline + margin, k2 * stall_peak)`.
46    #[must_use]
47    pub fn need_us(&self, baseline_us: Micros, stall_peak_us: Micros) -> Micros {
48        self.need_with_granularity_us(baseline_us, stall_peak_us, 0)
49    }
50
51    /// The delivery-granularity floor: `granularity + margin`, 0 when there is
52    /// no granularity to cover.
53    #[must_use]
54    pub fn granularity_need_us(&self, granularity_us: Micros) -> Micros {
55        if granularity_us == 0 {
56            0
57        } else {
58            granularity_us.saturating_add(self.cfg.margin_us)
59        }
60    }
61
62    /// Clamp into `[min_us, max_us]`.
63    #[must_use]
64    pub fn clamp(&self, v: Micros) -> Micros {
65        v.clamp(self.cfg.min_us, self.cfg.max_us)
66    }
67
68    /// Asymmetric follower: rise instantly, fall at most
69    /// `max_step_down_us_per_s`. Returns the new target.
70    pub fn follow(&mut self, need_us: Micros, now_us: u64) -> Micros {
71        let raw = self.clamp(need_us);
72        let dt = self.last_t.map_or(0, |t| now_us.saturating_sub(t));
73        let fall =
74            u128::from(self.cfg.max_step_down_us_per_s) * u128::from(dt) / u128::from(US_PER_S);
75        let fall = u64::try_from(fall).unwrap_or(u64::MAX);
76        self.target = raw.max(self.target.saturating_sub(fall));
77        self.last_t = Some(now_us);
78        self.target
79    }
80
81    /// Raise (never lower) the target to at least `clamp(need)`.
82    pub fn raise(&mut self, need_us: Micros) {
83        self.target = self.target.max(self.clamp(need_us));
84    }
85
86    /// Back to the floor.
87    pub fn reset(&mut self) {
88        self.target = self.cfg.min_us;
89        self.last_t = None;
90    }
91
92    /// Current target.
93    #[must_use]
94    pub fn target_us(&self) -> Micros {
95        self.target
96    }
97
98    /// First-fill threshold: `max(initial, target)`.
99    #[must_use]
100    pub fn start_us(&self) -> Micros {
101        self.clamp(self.cfg.initial_us.max(self.target))
102    }
103
104    /// Post-stall resume threshold: `clamp(max(target, after_rebuffer * stall_peak))`.
105    #[must_use]
106    pub fn resume_us(&self, stall_peak_us: Micros) -> Micros {
107        self.clamp(
108            self.target
109                .max(mul_q(stall_peak_us, self.cfg.after_rebuffer_q8, Q8_SHIFT)),
110        )
111    }
112
113    /// The configured cap.
114    #[must_use]
115    pub fn max_us(&self) -> Micros {
116        self.cfg.max_us
117    }
118}