Raw ↗
+ +
9a12521 fix(ci): resolve windows strcasecmp linkage and macos test failures
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_congestion_test.c - Congestion Control, Audio Preemption & Bitrate Adaptation 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 #include <time.h>
15 #include "avx_platform.h"
16 #include "openavx.h"
17 #include "avx_internal.h"
18 #include "avx_speedhq.h"
19  
20 #define TEST_CONG_PORT 9090
21  
22 static void test_speedhq_dynamic_quantization(void) {
23 printf("[*] 1. Testing SpeedHQ Dynamic Bitrate & Quantization Scaling...\n");
24  
25 avx_speedhq_init();
26  
27 int width = 1920;
28 int height = 1080;
29 size_t raw_bytes = (size_t)width * height * 2;
30 uint8_t *uyvy = (uint8_t*)malloc(raw_bytes);
31 assert(uyvy != NULL);
32  
33 /* Generate natural test gradient */
34 for (int y = 0; y < height; ++y) {
35 for (int x = 0; x < width; ++x) {
36 uyvy[(y * width + x) * 2 + 0] = (uint8_t)(x & 0xFF); /* U/V */
37 uyvy[(y * width + x) * 2 + 1] = (uint8_t)((x + y) & 0xFF); /* Y */
38 }
39 }
40  
41 size_t max_comp = raw_bytes + 4096;
42 uint8_t *comp_high = (uint8_t*)malloc(max_comp);
43 uint8_t *comp_low = (uint8_t*)malloc(max_comp);
44 uint8_t *decomp = (uint8_t*)malloc(raw_bytes);
45 assert(comp_high && comp_low && decomp);
46  
47 /* 1. High Quality (Default: 96) */
48 oavx_speedhq_set_quality(96);
49 avx_speedhq_set_quality(96);
50 assert(oavx_speedhq_get_quality() == 96);
51 size_t high_size = 0;
52 int ret = avx_speedhq_encode_c99_ex(OAVX_FOURCC_VIDEO_TYPE_SHQ2, width, height,
53 uyvy, NULL, comp_high, max_comp, &high_size);
54 assert(ret == 0 && high_size > 0);
55 printf(" [+] High Quality (q=96): %zu bytes (Ratio: %.1f:1)\n",
56 high_size, (double)raw_bytes / (double)high_size);
57  
58 /* 2. Low Bitrate / High Compression (Quality: 50, qscale=25) */
59 oavx_speedhq_set_quality(50);
60 avx_speedhq_set_quality(50);
61 assert(oavx_speedhq_get_quality() == 50);
62 size_t low_size = 0;
63 ret = avx_speedhq_encode_c99_ex(OAVX_FOURCC_VIDEO_TYPE_SHQ2, width, height,
64 uyvy, NULL, comp_low, max_comp, &low_size);
65 assert(ret == 0 && low_size > 0);
66 printf(" [+] Low Bitrate (q=50): %zu bytes (Ratio: %.1f:1)\n",
67 low_size, (double)raw_bytes / (double)low_size);
68  
69 /* Verify low bitrate achieves substantial bandwidth savings */
70 assert(low_size < high_size);
71 double savings_pct = (1.0 - ((double)low_size / (double)high_size)) * 100.0;
72 printf(" [+] Wire Bandwidth Reduction: %.1f%% savings under congestion!\n", savings_pct);
73 assert(savings_pct >= 30.0);
74  
75 /* Verify Decompression of Low Bitrate Frame */
76 bool has_alpha = false;
77 ret = avx_speedhq_decode_c99_ex(OAVX_FOURCC_VIDEO_TYPE_SHQ2, width, height,
78 comp_low, low_size, decomp, NULL, &has_alpha);
79 assert(ret == 0);
80 printf(" [+] Successfully decoded adaptive low-bitrate SpeedHQ bitstream!\n");
81  
82 /* Restore default quality */
83 oavx_speedhq_set_quality(96);
84 avx_speedhq_set_quality(96);
85  
86 free(uyvy);
87 free(comp_high);
88 free(comp_low);
89 free(decomp);
90 }
91  
92 static void test_audio_priority_preemption(void) {
93 printf("[*] 2. Testing Strict Audio Priority & Video Preemption under Full Queue Saturation...\n");
94  
95 oavx_context_t *ctx = oavx_context_create();
96 assert(ctx != NULL);
97  
98 oavx_stream_config_t tx_cfg = {
99 .name = "AUDIO_PRIORITY_TEST_STREAM",
100 .groups = "public",
101 .direction = OAVX_DIR_TRANSMIT,
102 .clock_sync = false,
103 .bind_ip = "127.0.0.1"
104 };
105  
106 oavx_stream_t *tx = oavx_stream_open(ctx, &tx_cfg);
107 assert(tx != NULL);
108 const oavx_endpoint_t *tx_ep = oavx_stream_get_endpoint(tx);
109 assert(tx_ep != NULL);
110  
111 /* Connect Receiver */
112 oavx_stream_config_t rx_cfg = {
113 .name = "AUDIO_PRIORITY_VIEWER",
114 .endpoint = tx_ep->address,
115 .direction = OAVX_DIR_RECEIVE,
116 .color_format = OAVX_COLOR_FORMAT_UYVY_BGRA,
117 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
118 .clock_sync = false
119 };
120  
121 oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg);
122 assert(rx != NULL);
123  
124 /* Wait for connection */
125 int conns = 0;
126 for (int i = 0; i < 20; ++i) {
127 conns = oavx_stream_get_connection_count(tx, 50);
128 if (conns > 0) break;
129 }
130 assert(conns >= 1);
131  
132 /* Send Video Frames rapidly to create backpressure */
133 size_t v_bytes = 320 * 240 * 2;
134 uint8_t *v_buf = (uint8_t*)malloc(v_bytes);
135 assert(v_buf != NULL);
136 memset(v_buf, 0x77, v_bytes);
137  
138 oavx_video_frame_t vf = {
139 .width = 320,
140 .height = 240,
141 .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY,
142 .scan_type = OAVX_SCAN_PROGRESSIVE,
143 .framerate = { 60, 1 },
144 .aspect_ratio = { 16, 9 },
145 .pts_ns = 1000000LL,
146 .planes = { v_buf, NULL, NULL, NULL },
147 .strides = { 320 * 2, 0, 0, 0 }
148 };
149  
150 /* Interleave 20 video frames and 10 audio frames */
151 float a_samples[480 * 2];
152 for (int i = 0; i < 480 * 2; ++i) a_samples[i] = 0.5f;
153  
154 oavx_audio_frame_t af = {
155 .sample_rate = 48000,
156 .channels = 2,
157 .frames = 480,
158 .format = OAVX_AUDIO_FMT_FLT_PLANAR,
159 .stride_bytes = 480 * sizeof(float),
160 .pts_ns = 1000000LL,
161 .planes = { (uint8_t*)a_samples, (uint8_t*)(a_samples + 480), NULL, NULL, NULL, NULL, NULL, NULL }
162 };
163  
164 int total_audio_sent = 10;
165 int total_video_sent = 30;
166  
167 for (int i = 0; i < total_video_sent; ++i) {
168 vf.pts_ns += 16666666LL;
169 oavx_stream_send_video(tx, &vf);
170 if (i % 3 == 0 && (i / 3) < total_audio_sent) {
171 af.pts_ns += 10000000LL;
172 oavx_stream_send_audio(tx, &af);
173 }
174 }
175  
176 /* Receive all captured frames */
177 oavx_video_frame_t in_v;
178 oavx_audio_frame_t in_a;
179 oavx_metadata_frame_t in_m;
180 int audio_received = 0;
181 int video_received = 0;
182  
183 for (int attempts = 0; attempts < 100; ++attempts) {
184 oavx_frame_type_t ft = oavx_stream_receive(rx, &in_v, &in_a, &in_m, 50);
185 if (ft == OAVX_FRAME_TYPE_AUDIO) {
186 audio_received++;
187 oavx_stream_release_audio(rx, &in_a);
188 } else if (ft == OAVX_FRAME_TYPE_VIDEO) {
189 video_received++;
190 oavx_stream_release_video(rx, &in_v);
191 }
192 }
193  
194 printf(" [+] Total Video Received: %d, Total Audio Received: %d\n", video_received, audio_received);
195 /* Strict audio verification: Audio must be received without starvation */
196 assert(audio_received > 0);
197 printf(" [+] Audio Priority Preemption verified: Audio stream preserved through saturated queue!\n");
198  
199 /* Teardown */
200 oavx_stream_close(rx);
201 oavx_stream_close(tx);
202 oavx_context_destroy(ctx);
203 free(v_buf);
204 }
205  
206 static void test_per_client_temporal_decimation(void) {
207 printf("[*] 3. Testing Per-Client Adaptive Temporal Decimation & Congestion Isolation...\n");
208  
209 oavx_context_t *ctx = oavx_context_create();
210 assert(ctx != NULL);
211  
212 oavx_stream_config_t tx_cfg = {
213 .name = "MULTI_CLIENT_DECIMATION_TEST",
214 .groups = "public",
215 .direction = OAVX_DIR_TRANSMIT,
216 .clock_sync = false,
217 .bind_ip = "127.0.0.1"
218 };
219  
220 oavx_stream_t *tx = oavx_stream_open(ctx, &tx_cfg);
221 assert(tx != NULL);
222 const oavx_endpoint_t *tx_ep = oavx_stream_get_endpoint(tx);
223 assert(tx_ep != NULL);
224  
225 /* 1. Fast Viewer (Active Reader) */
226 oavx_stream_config_t rx_fast_cfg = {
227 .name = "FAST_VIEWER",
228 .endpoint = tx_ep->address,
229 .direction = OAVX_DIR_RECEIVE,
230 .color_format = OAVX_COLOR_FORMAT_UYVY_BGRA,
231 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
232 .clock_sync = false
233 };
234 oavx_stream_t *rx_fast = oavx_stream_open(ctx, &rx_fast_cfg);
235 assert(rx_fast != NULL);
236  
237 /* 2. Slow Viewer (Frozen Reader that does not drain socket) */
238 oavx_stream_config_t rx_slow_cfg = {
239 .name = "SLOW_VIEWER",
240 .endpoint = tx_ep->address,
241 .direction = OAVX_DIR_RECEIVE,
242 .color_format = OAVX_COLOR_FORMAT_UYVY_BGRA,
243 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
244 .clock_sync = false
245 };
246 oavx_stream_t *rx_slow = oavx_stream_open(ctx, &rx_slow_cfg);
247 assert(rx_slow != NULL);
248  
249 int conns = 0;
250 for (int i = 0; i < 20; ++i) {
251 conns = oavx_stream_get_connection_count(tx, 50);
252 if (conns >= 2) break;
253 }
254 assert(conns >= 2);
255 printf(" [+] Two viewers connected to sender simultaneously\n");
256  
257 size_t v_bytes = 320 * 240 * 2;
258 uint8_t *v_buf = (uint8_t*)malloc(v_bytes);
259 assert(v_buf != NULL);
260 memset(v_buf, 0x44, v_bytes);
261  
262 oavx_video_frame_t vf = {
263 .width = 320,
264 .height = 240,
265 .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY,
266 .scan_type = OAVX_SCAN_PROGRESSIVE,
267 .framerate = { 60, 1 },
268 .aspect_ratio = { 16, 9 },
269 .pts_ns = 2000000LL,
270 .planes = { v_buf, NULL, NULL, NULL },
271 .strides = { 320 * 2, 0, 0, 0 }
272 };
273  
274 int fast_received = 0;
275 oavx_video_frame_t in_v;
276 oavx_audio_frame_t in_a;
277 oavx_metadata_frame_t in_m;
278  
279 /* Transmit 60 frames: fast viewer drains continuously, slow viewer is frozen */
280 for (int f = 0; f < 60; ++f) {
281 vf.pts_ns += 16666666LL;
282 oavx_stream_send_video(tx, &vf);
283  
284 /* Fast viewer actively receives */
285 oavx_frame_type_t ft = oavx_stream_receive(rx_fast, &in_v, &in_a, &in_m, 10);
286 if (ft == OAVX_FRAME_TYPE_VIDEO) {
287 fast_received++;
288 oavx_stream_release_video(rx_fast, &in_v);
289 }
290 }
291  
292 /* Drain remainder on fast viewer */
293 for (int i = 0; i < 50; ++i) {
294 oavx_frame_type_t ft = oavx_stream_receive(rx_fast, &in_v, &in_a, &in_m, 50);
295 if (ft == OAVX_FRAME_TYPE_VIDEO) {
296 fast_received++;
297 oavx_stream_release_video(rx_fast, &in_v);
298 }
299 }
300  
301 printf(" [+] Fast Viewer received: %d frames (unblocked by slow viewer)\n", fast_received);
302 assert(fast_received >= 15);
303  
304 /* Telemetry Diagnostics */
305 oavx_telemetry_t telem;
306 bool ok = oavx_stream_get_telemetry(tx, &telem);
307 assert(ok);
308 printf(" [+] Transmitter Telemetry: total_dropped=%lld, is_connected=%d, congestion_level=%u\n",
309 (long long)telem.dropped_frames_total, telem.is_connected, telem.congestion_level);
310 assert(telem.dropped_frames_total > 0); /* Confirms slow viewer was decimated and isolated! */
311  
312 char json_buf[1024];
313 size_t jlen = oavx_telemetry_to_json(&telem, json_buf, sizeof(json_buf));
314 assert(jlen > 0 && strstr(json_buf, "congestion_level") != NULL);
315 printf(" [+] Real-time Telemetry JSON verified with congestion telemetry!\n");
316  
317 /* Teardown */
318 oavx_stream_close(rx_slow);
319 oavx_stream_close(rx_fast);
320 oavx_stream_close(tx);
321 oavx_context_destroy(ctx);
322 free(v_buf);
323 }
324  
325 static void init_mock_tcp_client(avx_client_slot_t *c) {
326 memset(c, 0, sizeof(*c));
327 c->transport = OAVX_TRANSPORT_TCP;
328 c->tx_bytes_per_sec = -1.0f;
329 }
330  
331 static void test_tcp_bdp_congestion_thresholds(void) {
332 printf("[*] 4. Testing BDP- & Frame-Aware TCP TIOCOUTQ Thresholds & Dynamics...\n");
333  
334 uint64_t warn_b = 0, sev_b = 0;
335  
336 /* Case A: Low-BDP LAN with small SD frames -> exact 500 KB / 2 MB floor preserved */
337 avx_send_compute_tcp_thresholds(153600, 10000000.0f, 60.0f, 0.1f, &warn_b, &sev_b);
338 assert(warn_b == 500000ULL);
339 assert(sev_b == 2000000ULL);
340  
341 /* Case B: Raw 4K UYVY frame (16.6 MB) on LAN -> single frame write does not trigger */
342 avx_send_compute_tcp_thresholds(16588800, 1000000000.0f, 60.0f, 0.2f, &warn_b, &sev_b);
343 assert(warn_b >= 16588800ULL * 2ULL);
344 assert(sev_b >= 16588800ULL * 4ULL);
345  
346 /* Case C: Healthy 50 ms RTT WAN @ ~235 Mbps (1080p60 SHQ2 ~477 KB/frame) stays unthrottled (cg=0) */
347 avx_client_slot_t wan_fast;
348 init_mock_tcp_client(&wan_fast);
349 size_t shq_1080 = 477300;
350 int64_t t_ns = 1000000000LL;
351 uint32_t cadence = 0;
352  
353 /* Cold-start before 200ms sample window: ~1.9 MB in flight stays cg=0 */
354 wan_fast.tcp_bytes_submitted = 2400000ULL;
355 uint32_t lv = avx_send_eval_tcp_congestion(&wan_fast, t_ns, shq_1080, 60.0f, 1900000, 0, 50.0f, &cadence);
356 assert(lv == 0 && cadence == 1);
357  
358 /* Steady-state after 250ms: ~7.35 MB submitted & acked (~29.4 MB/s = 235 Mbps), 1.9 MB unsent */
359 t_ns += 250000000LL;
360 wan_fast.tcp_bytes_submitted += 7350000ULL;
361 lv = avx_send_eval_tcp_congestion(&wan_fast, t_ns, shq_1080, 60.0f, 1900000, 0, 50.0f, &cadence);
362 assert(lv == 0 && cadence == 1);
363 assert(wan_fast.tx_bytes_per_sec > 20000000.0f);
364  
365 /* Transient spike pushes unsent to 3.2 MB -> cg=1, then drains to 1.8 MB cleanly -> recovers cg=0 without ratcheting BDP down */
366 t_ns += 50000000LL;
367 wan_fast.tcp_bytes_submitted += 1500000ULL;
368 lv = avx_send_eval_tcp_congestion(&wan_fast, t_ns, shq_1080, 60.0f, 3200000, 0, 50.0f, &cadence);
369 assert(lv == 1 && cadence == 2);
370  
371 t_ns += 210000000LL; /* Drained window while throttled */
372 wan_fast.tcp_bytes_submitted += 2500000ULL;
373 lv = avx_send_eval_tcp_congestion(&wan_fast, t_ns, shq_1080, 60.0f, 1800000, 0, 50.0f, &cadence);
374 assert(lv == 0 && cadence == 1);
375 assert(wan_fast.tx_bytes_per_sec > 20000000.0f); /* Kept unthrottled BDP capacity */
376  
377 /* Case D: Slow 5 Mbps (~625 KB/s) WAN @ 50 ms RTT -> small BDP catches 1.1 MB queue (no 5 MB bufferbloat!) */
378 avx_client_slot_t wan_slow;
379 init_mock_tcp_client(&wan_slow);
380 t_ns = 2000000000LL;
381 wan_slow.tcp_bytes_submitted = 500000ULL;
382 lv = avx_send_eval_tcp_congestion(&wan_slow, t_ns, shq_1080, 60.0f, 400000, 0, 50.0f, &cadence);
383 assert(lv == 0);
384 t_ns += 250000000LL;
385 wan_slow.tcp_bytes_submitted += 856250ULL; /* 700 KB more unsent => only ~156 KB acked in 250 ms (~625 KB/s) */
386 lv = avx_send_eval_tcp_congestion(&wan_slow, t_ns, shq_1080, 60.0f, 1100000, 0, 50.0f, &cadence);
387 assert(lv == 1 && cadence == 2);
388 assert(wan_slow.tx_bytes_per_sec >= 0.0f && wan_slow.tx_bytes_per_sec < 1000000.0f);
389  
390 /* Case E: Stalled link (0 bytes ACKed -> tx_bytes_per_sec hits 0.0f, distinct from < 0 sentinel) */
391 avx_client_slot_t stalled;
392 init_mock_tcp_client(&stalled);
393 t_ns = 3000000000LL;
394 stalled.tcp_bytes_submitted = 1200000ULL;
395 lv = avx_send_eval_tcp_congestion(&stalled, t_ns, shq_1080, 60.0f, 1200000, 0, 50.0f, &cadence);
396 t_ns += 250000000LL;
397 lv = avx_send_eval_tcp_congestion(&stalled, t_ns, shq_1080, 60.0f, 1200000, 0, 50.0f, &cadence);
398 assert(stalled.tx_bytes_per_sec == 0.0f);
399 assert(lv == 1 && cadence == 2);
400  
401 /* Case F: Worker queue backpressure (v_queued >= 1 / >= 2) always triggers regardless of TIOCOUTQ */
402 lv = avx_send_eval_tcp_congestion(&wan_fast, t_ns, shq_1080, 60.0f, 0, 1, 50.0f, &cadence);
403 assert(lv == 1 && cadence == 2);
404 lv = avx_send_eval_tcp_congestion(&wan_fast, t_ns, shq_1080, 60.0f, 0, 2, 50.0f, &cadence);
405 assert(lv == 2 && cadence == 4);
406  
407 printf(" [+] BDP + Frame-Aware TCP TIOCOUTQ verified across LAN, slow WAN, fast WAN, recovery, and stall regimes!\n");
408 }
409  
410 int main(void) {
411 printf("=== OpenAVX Congestion Control & Bitrate Tests ===\n\n");
412  
413 test_speedhq_dynamic_quantization();
414 test_audio_priority_preemption();
415 test_per_client_temporal_decimation();
416 test_tcp_bdp_congestion_thresholds();
417  
418 printf("\n[PASS] openavx_congestion_test\n");
419 return 0;
420 }