Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
adaptive_fec.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <algorithm>
8#include <array>
9#include <cmath>
10#include <cstddef>
11#include <cstdint>
12#include <optional>
13
14namespace stream {
15
33 public:
35 static constexpr int window_seconds = 10;
37 static constexpr int min_percentage = 1;
38 static constexpr int max_percentage = 50;
40 static constexpr double target_unrecoverable = 0.001;
42 static constexpr int representative_data_shards = 10;
44 static constexpr std::uint64_t min_packets_per_bucket = 100;
46 static constexpr std::int64_t lower_cooldown_ms = 5000;
48 static constexpr std::int64_t bucket_span_ms = 1000;
54 static constexpr std::int64_t min_coverage_ms = bucket_span_ms * 3 / 4;
55
56 private:
57 struct bucket_t {
58 std::int64_t second {-1};
59 std::uint64_t sent {0};
60 std::uint64_t missing {0};
62 std::int64_t covered_ms {0};
63 };
64
65 public:
78 int percentage(std::uint64_t sent_total, std::uint64_t missing_total, std::uint64_t reported_total, std::int64_t now_ms, int configured, int ceiling) {
79 if (reported_total == 0) {
80 // Traffic alone proves nothing: on_sent() counts every block the host transmits,
81 // while only the client's reports say what arrived. Gating on the sent counter
82 // would make a client that never reports look like a perfect link.
83 //
84 // The operator's value stands as configured, even above the ceiling: the ceiling
85 // bounds what the estimate may ask for, and there is no estimate yet.
86 current = std::max(configured, min_percentage);
87 last_second = -1;
88 return current;
89 }
90
91 const auto second = now_ms / 1000;
92
93 if (last_second < 0) {
94 // First call: take a baseline instead of counting the whole session as one second.
95 last_sent = sent_total;
96 last_missing = missing_total;
97 last_second = second;
98 last_rollover_ms = now_ms;
99 required.reset();
100 current = std::max(configured, min_percentage);
101 return current;
102 }
103
104 if (second != last_second) {
105 const auto delta_sent = sent_total - last_sent;
106 const auto delta_missing = missing_total - last_missing;
107 const auto covered_ms = now_ms - last_rollover_ms;
108 last_sent = sent_total;
109 last_missing = missing_total;
110 last_rollover_ms = now_ms;
111
112 // Attribute the whole elapsed second to its own bucket. If the thread stalled
113 // for several seconds, the sample lands in the newest bucket and the older
114 // ones keep whatever they had, so a stall cannot fake a clean window.
115 auto &bucket = buckets[static_cast<std::size_t>(second % window_seconds)];
116 bucket.second = second;
117 bucket.sent = delta_sent;
118 bucket.missing = delta_missing;
119 bucket.covered_ms = covered_ms;
120 last_second = second;
121
122 // The buckets and the window only move at a rollover, so the estimate is worked
123 // out here once a second instead of on every frame of the video thread.
124 if (has_sample(second)) {
125 required = percentage_for(worst_loss(second), ceiling);
126 } else {
127 required.reset();
128 }
129 }
130
131 if (!required) {
132 // Either nothing has been sampled yet, or the last trustworthy second has aged
133 // out of the window. Hold the last decision: silence is not a clean link, and
134 // dropping back to the configured value here would hand back parity without a
135 // cooldown after a stall.
136 return current;
137 }
138
139 if (last_lowered_ms < 0) {
140 // The first trustworthy second starts the clock: the configured value is held
141 // for a full cooldown before anything is given back.
142 last_lowered_ms = now_ms;
143 }
144
145 if (*required > current) {
146 current = *required;
147 // A raise restarts the hold, so the next reduction waits for five quiet
148 // seconds after the last one rather than counting time since the last drop.
149 last_lowered_ms = now_ms;
150 return current;
151 }
152 if (*required < current && now_ms - last_lowered_ms >= lower_cooldown_ms) {
153 current = *required;
154 last_lowered_ms = now_ms;
155 }
156 return current;
157 }
158
160 bool has_sample(std::int64_t second) const {
161 for (const auto &bucket : buckets) {
162 if (trusted(bucket, second)) {
163 return true;
164 }
165 }
166 return false;
167 }
168
170 double worst_loss(std::int64_t second) const {
171 double worst = 0;
172 for (const auto &bucket : buckets) {
173 if (!trusted(bucket, second)) {
174 continue;
175 }
176 const auto ratio = static_cast<double>(bucket.missing) / static_cast<double>(bucket.sent);
177 worst = std::max(worst, ratio);
178 }
179 return worst;
180 }
181
189 static bool trusted(const bucket_t &bucket, std::int64_t second) {
190 if (bucket.second < 0 || bucket.sent < min_packets_per_bucket) {
191 return false;
192 }
193 if (bucket.covered_ms < min_coverage_ms) {
194 return false;
195 }
196 return bucket.second <= second && second - bucket.second < window_seconds;
197 }
198
204 static int percentage_for(double loss, int ceiling) {
205 const auto bound = std::clamp(ceiling, min_percentage, max_percentage);
206 if (!(loss > 0)) {
207 return min_percentage;
208 }
209 const auto n = representative_data_shards;
210 for (auto parity = 1; parity <= bound; ++parity) {
211 if (unrecoverable_chance(n, parity, loss) <= target_unrecoverable) {
212 // Round up so the percentage never undershoots the chosen parity count.
213 const auto percentage = static_cast<int>(std::ceil(100.0 * parity / n));
214 return std::clamp(percentage, min_percentage, bound);
215 }
216 }
217 return bound;
218 }
219
221 static double unrecoverable_chance(int data, int parity, double loss) {
222 const auto total = data + parity;
223 const auto p = std::clamp(loss, 0.0, 1.0);
224 // Sum the upper tail: P(X > parity) for X ~ Binomial(total, p).
225 double tail = 0;
226 for (auto lost = parity + 1; lost <= total; ++lost) {
227 tail += binomial(total, lost) * std::pow(p, lost) * std::pow(1 - p, total - lost);
228 }
229 return tail;
230 }
231
232 private:
233 static double binomial(int n, int k) {
234 double result = 1;
235 for (auto i = 1; i <= k; ++i) {
236 result = result * (n - k + i) / i;
237 }
238 return result;
239 }
240
241 std::array<bucket_t, window_seconds> buckets {};
242 std::uint64_t last_sent {0};
243 std::uint64_t last_missing {0};
244 std::int64_t last_second {-1};
245 std::int64_t last_rollover_ms {0};
247 std::int64_t last_lowered_ms {-1};
249 std::optional<int> required;
250 int current {0};
251 };
252
253} // namespace stream
static constexpr std::uint64_t min_packets_per_bucket
A second needs at least this many sent data packets before its ratio is trusted.
bool has_sample(std::int64_t second) const
True once some second in the window carried enough packets to judge.
static constexpr std::int64_t min_coverage_ms
static bool trusted(const bucket_t &bucket, std::int64_t second)
static constexpr int min_percentage
Never ask for less than this, and never more than the configured ceiling.
double worst_loss(std::int64_t second) const
The worst packet-loss ratio in the window, 0 when no bucket is trustworthy.
int percentage(std::uint64_t sent_total, std::uint64_t missing_total, std::uint64_t reported_total, std::int64_t now_ms, int configured, int ceiling)
static constexpr int window_seconds
Seconds of history the estimate looks at.
static constexpr int representative_data_shards
Data shards assumed per block when turning a target into a percentage.
static constexpr std::int64_t lower_cooldown_ms
How long a lower estimate must hold before it is applied.
static double unrecoverable_chance(int data, int parity, double loss)
P(more than parity packets of an (n + parity) block are lost).
static constexpr std::int64_t bucket_span_ms
A bucket must cover this much time before its ratio is trusted.
static constexpr double target_unrecoverable
Target chance that any one FEC block cannot be rebuilt.
static int percentage_for(double loss, int ceiling)