Raw ↗
+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
1 /*
2 * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0
3 * Copyright (c) 2026 Saif Mukhtar
4 *
5 * OpenAVX - Open Audio-Video eXchange
6 * test/openavx_rate_control_test.c - Adaptive Bitrate & Dynamic FEC Controller Test Suite
7 */
8  
9 #define _GNU_SOURCE
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <assert.h>
14  
15 #include "openavx.h"
16 #include "avx_internal.h"
17 #include "avx_rate_control.h"
18 #include "avx_fec.h"
19 #include "avx_udp.h"
20 #include "avx_speedhq.h"
21  
22 /* -------------------------------------------------------------------------- */
23 /* Test 1: Clean Link Probing & Additive Bitrate Increase */
24 /* -------------------------------------------------------------------------- */
25 static void test_clean_link_additive_probing(void) {
26 printf("[*] 1. Testing Clean Link Probing & Additive Bitrate Scaling...\n");
27  
28 avx_rate_controller_config_t cfg = {
29 .min_bitrate_bps = 5000000, /* 5 Mbps */
30 .max_bitrate_bps = 100000000, /* 100 Mbps */
31 .start_bitrate_bps = 30000000, /* 30 Mbps */
32 .target_fps = 60.0f,
33 .min_quality = 30,
34 .max_quality = 96,
35 .fec_auto_tune = true
36 };
37  
38 avx_rate_controller_t* c = avx_rate_controller_create(&cfg);
39 assert(c != NULL);
40  
41 assert(avx_rate_controller_get_target_bitrate(c) == 30000000);
42 assert(avx_rate_controller_get_cadence(c) == 1);
43 assert(avx_rate_controller_get_congestion_state(c) == AVX_WAN_CONG_NORMAL);
44  
45 /* Simulate 5 seconds of clean link telemetry */
46 int64_t now_ms = 1000;
47 avx_rudp_telemetry_t telem;
48 memset(&telem, 0, sizeof(telem));
49 telem.srtt_ms = 25;
50 telem.rttvar_ms = 3;
51  
52 for (int step = 0; step < 50; ++step) {
53 now_ms += 100;
54 telem.packets_sent += 200; /* 200 packets per 100ms */
55 /* 0 drops, 0 retransmissions */
56  
57 avx_rate_controller_update_telemetry(c, &telem, now_ms);
58 }
59  
60 uint32_t final_br = avx_rate_controller_get_target_bitrate(c);
61 uint8_t final_q = avx_rate_controller_get_quality(c);
62 avx_wan_congestion_state_t st = avx_rate_controller_get_congestion_state(c);
63  
64 printf(" -> Final Bitrate: %u bps (%.2f Mbps)\n", final_br, (double)final_br / 1000000.0);
65 printf(" -> Final Quality: %u\n", final_q);
66 printf(" -> Congestion State: %d\n", st);
67  
68 /* Bitrate should have probed upwards from 30 Mbps */
69 assert(final_br > 45000000);
70 assert(final_q >= 60);
71 assert(st == AVX_WAN_CONG_NORMAL);
72  
73 /* Verify Tier 0 clean FEC configuration */
74 avx_fec_config_t fec;
75 avx_rate_controller_get_fec_config(c, &fec);
76 assert(fec.enabled == true);
77 assert(fec.matrix_d == 16);
78 assert(fec.matrix_l == 5);
79 assert(fec.enable_row_fec == false);
80  
81 avx_rate_controller_destroy(c);
82 printf(" -> Clean link probing verified successfully.\n");
83 }
84  
85 /* -------------------------------------------------------------------------- */
86 /* Test 2: GCC Delay-Based Queuing Detection (Bufferbloat Warning) */
87 /* -------------------------------------------------------------------------- */
88 static void test_delay_based_queuing_detection(void) {
89 printf("[*] 2. Testing GCC Delay-Based Queuing Detection...\n");
90  
91 avx_rate_controller_config_t cfg = {
92 .min_bitrate_bps = 5000000,
93 .max_bitrate_bps = 100000000,
94 .start_bitrate_bps = 80000000, /* 80 Mbps */
95 .target_fps = 60.0f,
96 .min_quality = 30,
97 .max_quality = 96,
98 .fec_auto_tune = true
99 };
100  
101 avx_rate_controller_t* c = avx_rate_controller_create(&cfg);
102 assert(c != NULL);
103  
104 int64_t now_ms = 1000;
105 avx_rudp_telemetry_t telem;
106 memset(&telem, 0, sizeof(telem));
107  
108 /* 1. Baseline RTT is 20 ms */
109 telem.srtt_ms = 20;
110 telem.rttvar_ms = 2;
111 telem.packets_sent = 500;
112 avx_rate_controller_update_telemetry(c, &telem, now_ms);
113  
114 /* 2. Bufferbloat occurs in router: RTT spikes to 75 ms (Delta = +55 ms), with ZERO packet loss */
115 for (int step = 0; step < 10; ++step) {
116 now_ms += 100;
117 telem.packets_sent += 200;
118 telem.srtt_ms = 75;
119 telem.rttvar_ms = 15;
120 avx_rate_controller_update_telemetry(c, &telem, now_ms);
121 }
122  
123 avx_wan_congestion_state_t st = avx_rate_controller_get_congestion_state(c);
124 uint32_t choked_br = avx_rate_controller_get_target_bitrate(c);
125 uint8_t choked_q = avx_rate_controller_get_quality(c);
126  
127 printf(" -> Congestion State under bufferbloat: %d (Expected OVERUSE=2)\n", st);
128 printf(" -> Choked Bitrate: %u bps (%.2f Mbps)\n", choked_br, (double)choked_br / 1000000.0);
129 printf(" -> Stepped-down Quality: %u\n", choked_q);
130  
131 assert(st == AVX_WAN_CONG_OVERUSE);
132 assert(choked_br < 65000000); /* Bitrate throttled down proactively to drain queue */
133 assert(choked_q < 90);
134  
135 avx_rate_controller_destroy(c);
136 printf(" -> Delay-based congestion detected and mitigated before packet loss.\n");
137 }
138  
139 /* -------------------------------------------------------------------------- */
140 /* Test 3: Loss-Based Multiplicative Backoff & Emergency Cadence Decimation */
141 /* -------------------------------------------------------------------------- */
142 static void test_loss_backoff_and_cadence(void) {
143 printf("[*] 3. Testing Loss-Based Multiplicative Backoff & Cadence...\n");
144  
145 avx_rate_controller_config_t cfg = {
146 .min_bitrate_bps = 2000000, /* 2 Mbps */
147 .max_bitrate_bps = 50000000, /* 50 Mbps */
148 .start_bitrate_bps = 20000000, /* 20 Mbps */
149 .target_fps = 60.0f,
150 .min_quality = 25,
151 .max_quality = 96,
152 .fec_auto_tune = true
153 };
154  
155 avx_rate_controller_t* c = avx_rate_controller_create(&cfg);
156 assert(c != NULL);
157  
158 int64_t now_ms = 2000;
159 avx_rudp_telemetry_t telem;
160 memset(&telem, 0, sizeof(telem));
161 telem.srtt_ms = 40;
162 telem.rttvar_ms = 10;
163 telem.packets_sent = 1000;
164 avx_rate_controller_update_telemetry(c, &telem, now_ms);
165  
166 /* Inject 15% sustained packet loss across 15 updates */
167 for (int step = 0; step < 15; ++step) {
168 now_ms += 100;
169 telem.packets_sent += 200;
170 telem.packets_lost += 30; /* 30 out of 200 lost = 15% */
171 telem.srtt_ms = 45;
172 avx_rate_controller_update_telemetry(c, &telem, now_ms);
173 }
174  
175 uint32_t choked_br = avx_rate_controller_get_target_bitrate(c);
176 uint32_t cadence = avx_rate_controller_get_cadence(c);
177 uint8_t q = avx_rate_controller_get_quality(c);
178  
179 printf(" -> Choked Bitrate: %u bps (%.2f Mbps)\n", choked_br, (double)choked_br / 1000000.0);
180 printf(" -> Cadence: %u (Expected: 2 for < 8 Mbps)\n", cadence);
181 printf(" -> Quality: %u\n", q);
182  
183 assert(choked_br <= 8000000);
184 assert(cadence == 2);
185 assert(q <= 35);
186  
187 avx_rate_controller_destroy(c);
188 printf(" -> Multiplicative loss backoff and emergency cadence verified.\n");
189 }
190  
191 /* -------------------------------------------------------------------------- */
192 /* Test 4: Dynamic FEC Density Matrix Auto-Tuning */
193 /* -------------------------------------------------------------------------- */
194 static void test_fec_density_auto_tuning(void) {
195 printf("[*] 4. Testing FEC Matrix Density Auto-Tuning Across Loss Tiers...\n");
196  
197 avx_rate_controller_t* c = avx_rate_controller_create(NULL);
198 assert(c != NULL);
199  
200 int64_t now_ms = 3000;
201 avx_rudp_telemetry_t telem;
202 memset(&telem, 0, sizeof(telem));
203 telem.srtt_ms = 30;
204 telem.packets_sent = 1000;
205 avx_rate_controller_update_telemetry(c, &telem, now_ms);
206  
207 avx_fec_config_t fec;
208  
209 /* Tier 0: Clean link (loss = 0) */
210 now_ms += 100;
211 telem.packets_sent += 500;
212 avx_rate_controller_update_telemetry(c, &telem, now_ms);
213 avx_rate_controller_get_fec_config(c, &fec);
214 assert(fec.matrix_d == 16 && fec.matrix_l == 5 && fec.enable_row_fec == false);
215 printf(" -> Tier 0 (Clean): D=%u, L=%u, RowFEC=%d\n", fec.matrix_d, fec.matrix_l, fec.enable_row_fec);
216  
217 /* Tier 1: Mild loss (~2%) */
218 for (int i = 0; i < 5; ++i) {
219 now_ms += 100;
220 telem.packets_sent += 200;
221 telem.packets_lost += 4; /* 2% loss */
222 avx_rate_controller_update_telemetry(c, &telem, now_ms);
223 }
224 avx_rate_controller_get_fec_config(c, &fec);
225 assert(fec.matrix_d == 10 && fec.matrix_l == 5 && fec.enable_row_fec == false);
226 printf(" -> Tier 1 (Mild): D=%u, L=%u, RowFEC=%d\n", fec.matrix_d, fec.matrix_l, fec.enable_row_fec);
227  
228 /* Tier 2: Moderate loss (~5%) */
229 for (int i = 0; i < 5; ++i) {
230 now_ms += 100;
231 telem.packets_sent += 200;
232 telem.packets_lost += 10; /* 5% loss */
233 avx_rate_controller_update_telemetry(c, &telem, now_ms);
234 }
235 avx_rate_controller_get_fec_config(c, &fec);
236 assert(fec.matrix_d == 8 && fec.matrix_l == 4 && fec.enable_row_fec == true);
237 printf(" -> Tier 2 (Burst): D=%u, L=%u, RowFEC=%d\n", fec.matrix_d, fec.matrix_l, fec.enable_row_fec);
238  
239 /* Tier 3: Severe loss (~12%) */
240 for (int i = 0; i < 5; ++i) {
241 now_ms += 100;
242 telem.packets_sent += 200;
243 telem.packets_lost += 24; /* 12% loss */
244 avx_rate_controller_update_telemetry(c, &telem, now_ms);
245 }
246 avx_rate_controller_get_fec_config(c, &fec);
247 assert(fec.matrix_d == 6 && fec.matrix_l == 3 && fec.enable_row_fec == true);
248 printf(" -> Tier 3 (Severe): D=%u, L=%u, RowFEC=%d\n", fec.matrix_d, fec.matrix_l, fec.enable_row_fec);
249  
250 avx_rate_controller_destroy(c);
251 printf(" -> FEC matrix auto-tuning verified across all 4 tiers.\n");
252 }
253  
254 /* -------------------------------------------------------------------------- */
255 /* Test 5: Integration with RUDP Sender & SpeedHQ Dynamic Quantization */
256 /* -------------------------------------------------------------------------- */
257 static void test_rudp_sender_adaptation_integration(void) {
258 printf("[*] 5. Testing Sender Integration with SpeedHQ Dynamic Quantization...\n");
259  
260 avx_socket_t tx_sock = avx_udp_create_socket(0, true);
261 assert(tx_sock != AVX_INVALID_SOCKET);
262  
263 avx_udp_rudp_sender_t* s = avx_udp_rudp_sender_create(tx_sock, 8);
264 assert(s != NULL);
265  
266 avx_rate_controller_t* c = avx_rate_controller_create(NULL);
267 assert(c != NULL);
268  
269 /* Initial state */
270 avx_rate_controller_apply_to_sender(c, s);
271  
272 /* Feed severe loss telemetry and apply to sender */
273 int64_t now_ms = 4000;
274 avx_rudp_telemetry_t telem;
275 memset(&telem, 0, sizeof(telem));
276 telem.srtt_ms = 50;
277 telem.packets_sent = 500;
278 telem.packets_lost = 50; /* 10% loss */
279  
280 for (int i = 0; i < 10; ++i) {
281 now_ms += 100;
282 telem.packets_sent += 200;
283 telem.packets_lost += 20;
284 avx_rate_controller_update_telemetry(c, &telem, now_ms);
285 }
286  
287 /* Apply auto-tuned FEC to sender */
288 avx_rate_controller_apply_to_sender(c, s);
289  
290 /* Apply recommended quality to SpeedHQ encoder */
291 uint8_t rec_q = avx_rate_controller_get_quality(c);
292 oavx_speedhq_set_quality(rec_q);
293 assert(oavx_speedhq_get_quality() == rec_q);
294  
295 printf(" -> SpeedHQ quality tuned dynamically to: %u\n", rec_q);
296  
297 /* Restore default SpeedHQ quality */
298 oavx_speedhq_set_quality(96);
299  
300 avx_rate_controller_destroy(c);
301 avx_udp_rudp_sender_destroy(s);
302 avx_socket_close(tx_sock);
303  
304 printf(" -> Sender adaptation and SpeedHQ quantization integrated successfully.\n");
305 }
306  
307 /* -------------------------------------------------------------------------- */
308 /* Test 6: In-Loop Transmitter Rate Controller Wiring & Live Telemetry */
309 /* -------------------------------------------------------------------------- */
310 static void test_transmitter_in_loop_rate_control(void) {
311 printf("[*] 6. Testing In-Loop Transmitter Rate Controller Integration...\n");
312  
313 avx_tx_config_t tx_cfg;
314 memset(&tx_cfg, 0, sizeof(tx_cfg));
315 tx_cfg.name = "TestTxRateCtl";
316 tx_cfg.transport = OAVX_TRANSPORT_RUDP;
317  
318 avx_tx_instance_t tx = avx_tx_create(&tx_cfg);
319 assert(tx != NULL);
320  
321 avx_rate_controller_t *rc = avx_tx_get_rate_controller(tx);
322 assert(rc != NULL);
323  
324 /* Verify initial baseline */
325 assert(avx_rate_controller_get_cadence(rc) == 1);
326 assert(avx_rate_controller_get_congestion_state(rc) == AVX_WAN_CONG_NORMAL);
327  
328 /* Test telemetry query */
329 oavx_telemetry_t telem;
330 bool ok = avx_tx_get_telemetry(tx, &telem);
331 assert(ok == true);
332  
333 /* Verify destruction releases rate controller without leak */
334 avx_tx_destroy(tx);
335 printf(" -> Transmitter in-loop rate controller verified successfully.\n");
336 }
337  
338 int main(void) {
339 printf("===================================================================\n");
340 printf(" OpenAVX Adaptive Bitrate & Dynamic FEC Controller Suite \n");
341 printf("===================================================================\n");
342  
343 test_clean_link_additive_probing();
344 test_delay_based_queuing_detection();
345 test_loss_backoff_and_cadence();
346 test_fec_density_auto_tuning();
347 test_rudp_sender_adaptation_integration();
348 test_transmitter_in_loop_rate_control();
349  
350 printf("\n>>> ALL RATE CONTROL TESTS PASSED SUCCESSFULLY! <<<\n");
351 return 0;
352 }