| 1 | /* librist. Copyright © 2026 SipRadius LLC. All right reserved. |
| 2 | * Author: Sergio Ammirata <[email protected]> |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-2-Clause |
| 5 | */ |
| 6 | |
| 7 | /* Constant-bitrate output pacing: a rate estimator and a schedule derived from |
| 8 | * it, for spacing output that would otherwise be released in batches. |
| 9 | * |
| 10 | * Takes byte counts and returns times; it never inspects payload contents, so it |
| 11 | * suits any CBR payload. Callers that can read a clock reference out of their |
| 12 | * payload should use it to derive a rate and pass that in. |
| 13 | * |
| 14 | * Schedules but owns no thread, so a caller with its own wait keeps using it. |
| 15 | * Not thread-safe; the estimator and the scheduler share no state, so a caller |
| 16 | * may run them on separate threads and move the rate across itself. |
| 17 | */ |
| 18 | |
| 19 | #ifndef RIST_PACER_H |
| 20 | #define RIST_PACER_H |
| 21 | |
| 22 | #include <stdint.h> |
| 23 | #include <stddef.h> |
| 24 | |
| 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | |
| 29 | /* Rate estimator: 1 s windows, 8x EWMA, mirroring librist's |
| 30 | * rist_calculate_bitrate so a stream measured here and there agrees. */ |
| 31 | #define RIST_PACER_RATE_WINDOW_US 1000000ULL |
| 32 | /* A window this many times over-length means data resumed after an outage; it is |
| 33 | * discarded so the rate freezes rather than decaying towards zero. */ |
| 34 | #define RIST_PACER_RATE_STALL_FACTOR 3ULL |
| 35 | |
| 36 | /* Behind by more than this is an outage, not drift: resynchronise to now rather |
| 37 | * than catch up, which would emit the arrears as a burst. */ |
| 38 | #define RIST_PACER_MAX_SLIP_NS 50000000ULL |
| 39 | |
| 40 | /* Occupancy-trim gain, and the clamp on the resulting correction. */ |
| 41 | #define RIST_PACER_FEEDBACK_K 0.5 |
| 42 | #define RIST_PACER_FEEDBACK_MIN 0.80 |
| 43 | #define RIST_PACER_FEEDBACK_MAX 1.20 |
| 44 | |
| 45 | /* ---------------------------------------------------------------- estimator */ |
| 46 | |
| 47 | struct rist_pacer_rate { |
| 48 | uint64_t last_calc_us; |
| 49 | uint64_t bytes; |
| 50 | uint64_t eight_times; /* 8x EWMA of the bitrate, as librist keeps it */ |
| 51 | uint64_t bps; /* published estimate */ |
| 52 | }; |
| 53 | |
| 54 | /* seed_bps 0 when nothing is known: reports 0 until the first full window, which |
| 55 | * callers must treat as "do not pace" rather than guess a rate. */ |
| 56 | void rist_pacer_rate_init(struct rist_pacer_rate *r, uint64_t seed_bps); |
| 57 | |
| 58 | /* Feed one datagram. now_us must come from a monotonic clock. */ |
| 59 | void rist_pacer_rate_add(struct rist_pacer_rate *r, size_t bytes, uint64_t now_us); |
| 60 | |
| 61 | static inline uint64_t rist_pacer_rate_bps(const struct rist_pacer_rate *r) |
| 62 | { |
| 63 | return r->bps; |
| 64 | } |
| 65 | |
| 66 | /* ---------------------------------------------------------------- scheduler */ |
| 67 | |
| 68 | struct rist_pacer { |
| 69 | uint64_t next_due_ns; /* 0 until the first datagram establishes it */ |
| 70 | uint64_t max_slip_ns; |
| 71 | double target_latency_ms; /* occupancy feedback target; 0 disables it */ |
| 72 | |
| 73 | /* Bound rist_pacer_sleep_ns() only, never the per-datagram interval. */ |
| 74 | uint64_t min_sleep_ns; |
| 75 | uint64_t max_sleep_ns; |
| 76 | |
| 77 | uint64_t underruns; /* schedule resyncs from slip */ |
| 78 | }; |
| 79 | |
| 80 | /* min_sleep_ns 0 suits a dedicated pacing thread, where a floor would coarsen the |
| 81 | * spacing it exists to hold; a shared event loop wants one to bound its wakeup |
| 82 | * rate. target_latency_ms 0 disables the occupancy trim. */ |
| 83 | void rist_pacer_init(struct rist_pacer *p, double target_latency_ms, |
| 84 | uint64_t min_sleep_ns, uint64_t max_sleep_ns); |
| 85 | |
| 86 | /* Re-anchor on the next datagram, keeping the configuration. For a discontinuity |
| 87 | * the caller already knows about, which slip would otherwise count as an |
| 88 | * underrun. */ |
| 89 | void rist_pacer_reset(struct rist_pacer *p); |
| 90 | |
| 91 | /* Time a datagram of `bytes` occupies at `rate_bps`, trimmed towards |
| 92 | * target_latency_ms by `occupancy` (the caller's queue depth in datagrams). The |
| 93 | * trim corrects source-versus-local clock drift without a payload clock. Returns |
| 94 | * 0 when rate_bps is 0, meaning do not pace. |
| 95 | * |
| 96 | * Never clamped to min_sleep_ns: raising the interval to a sleep floor would pace |
| 97 | * the stream below its own rate. The floor belongs on the sleep. */ |
| 98 | uint64_t rist_pacer_interval_ns(const struct rist_pacer *p, uint64_t rate_bps, |
| 99 | size_t bytes, size_t occupancy); |
| 100 | |
| 101 | /* Absolute time the next datagram may be sent, establishing the schedule on the |
| 102 | * first call and resynchronising past max_slip_ns. */ |
| 103 | uint64_t rist_pacer_due_ns(struct rist_pacer *p, uint64_t now_ns); |
| 104 | |
| 105 | /* Move the schedule on by one datagram. Call after sending. */ |
| 106 | void rist_pacer_advance(struct rist_pacer *p, uint64_t interval_ns); |
| 107 | |
| 108 | /* How many datagrams are due by now_ns, up to max, advancing past each; for a |
| 109 | * caller that wakes on a timer and releases a batch. More than one per wake is |
| 110 | * normal and is what keeps a sleep floor coarser than the interval honest. */ |
| 111 | unsigned rist_pacer_quota(struct rist_pacer *p, uint64_t now_ns, |
| 112 | uint64_t interval_ns, unsigned max); |
| 113 | |
| 114 | /* Time until the next datagram is due, clamped to [min_sleep_ns, max_sleep_ns], |
| 115 | * or max_sleep_ns when nothing is scheduled yet. */ |
| 116 | uint64_t rist_pacer_sleep_ns(const struct rist_pacer *p, uint64_t now_ns); |
| 117 | |
| 118 | #ifdef __cplusplus |
| 119 | } |
| 120 | #endif |
| 121 | |
| 122 | #endif /* RIST_PACER_H */ |