Raw ↗
+ +
6452af3 fix(ci): add socket recv timeouts and ctest watchdog to prevent runner deadlocks
saifmukhtar · 2026-11-25
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_simulcast_test.c - Lazy On-Demand Hardware Simulcast & Codec Negotiation 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 <arpa/inet.h>
15 #include <unistd.h>
16  
17 #include "openavx.h"
18 #include "avx_internal.h"
19 #include "avx_send_internal.h"
20 #include "avx_video_codec.h"
21  
22 /* -------------------------------------------------------------------------- */
23 /* Test 1: Network Address Classification (LAN vs WAN) */
24 /* -------------------------------------------------------------------------- */
25 static void test_address_classification(void) {
26 printf("[*] 1. Testing LAN vs WAN Network Address Classification...\n");
27  
28 struct sockaddr_storage ss;
29  
30 /* Loopback 127.0.0.1 -> LAN */
31 struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
32 memset(&ss, 0, sizeof(ss));
33 sin->sin_family = AF_INET;
34 inet_pton(AF_INET, "127.0.0.1", &sin->sin_addr);
35 assert(avx_is_lan_sockaddr(&ss) == true);
36  
37 /* 10.1.2.3 -> LAN (RFC 1918) */
38 inet_pton(AF_INET, "10.1.2.3", &sin->sin_addr);
39 assert(avx_is_lan_sockaddr(&ss) == true);
40  
41 /* 192.168.1.100 -> LAN (RFC 1918) */
42 inet_pton(AF_INET, "192.168.1.100", &sin->sin_addr);
43 assert(avx_is_lan_sockaddr(&ss) == true);
44  
45 /* 172.20.0.5 -> LAN (RFC 1918) */
46 inet_pton(AF_INET, "172.20.0.5", &sin->sin_addr);
47 assert(avx_is_lan_sockaddr(&ss) == true);
48  
49 /* 169.254.10.20 -> LAN (Link-Local) */
50 inet_pton(AF_INET, "169.254.10.20", &sin->sin_addr);
51 assert(avx_is_lan_sockaddr(&ss) == true);
52  
53 /* 8.8.8.8 -> WAN (Public Internet) */
54 inet_pton(AF_INET, "8.8.8.8", &sin->sin_addr);
55 assert(avx_is_lan_sockaddr(&ss) == false);
56  
57 /* 1.1.1.1 -> WAN (Public Internet) */
58 inet_pton(AF_INET, "1.1.1.1", &sin->sin_addr);
59 assert(avx_is_lan_sockaddr(&ss) == false);
60  
61 /* IPv6 ::1 -> LAN */
62 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
63 memset(&ss, 0, sizeof(ss));
64 sin6->sin6_family = AF_INET6;
65 inet_pton(AF_INET6, "::1", &sin6->sin6_addr);
66 assert(avx_is_lan_sockaddr(&ss) == true);
67  
68 /* IPv6 ULA fd00::1 -> LAN */
69 inet_pton(AF_INET6, "fd00::1", &sin6->sin6_addr);
70 assert(avx_is_lan_sockaddr(&ss) == true);
71  
72 /* IPv6 Public 2001:4860:4860::8888 -> WAN */
73 inet_pton(AF_INET6, "2001:4860:4860::8888", &sin6->sin6_addr);
74 assert(avx_is_lan_sockaddr(&ss) == false);
75  
76 /* IPv4-mapped IPv6 ::ffff:127.0.0.1 -> LAN */
77 inet_pton(AF_INET6, "::ffff:127.0.0.1", &sin6->sin6_addr);
78 assert(avx_is_lan_sockaddr(&ss) == true);
79  
80 /* IPv4-mapped IPv6 ::ffff:8.8.8.8 -> WAN */
81 inet_pton(AF_INET6, "::ffff:8.8.8.8", &sin6->sin6_addr);
82 assert(avx_is_lan_sockaddr(&ss) == false);
83  
84 printf(" -> RFC 1918, RFC 3927, and RFC 4193 classification verified.\n");
85 }
86  
87 /* -------------------------------------------------------------------------- */
88 /* Test 2: Codec Capability Auto-Negotiation */
89 /* -------------------------------------------------------------------------- */
90 static void test_codec_negotiation(void) {
91 printf("[*] 2. Testing Receiver Codec Capability Auto-Negotiation...\n");
92  
93 /* Default negotiation with no receiver preference */
94 uint32_t def_codec = avx_negotiate_best_codec(NULL);
95 assert(def_codec != 0);
96 printf(" -> Default negotiated codec: 0x%08X (%.4s)\n", def_codec, (const char *)&def_codec);
97  
98 /* Explicit H.264 preference */
99 uint32_t h264_codec = avx_negotiate_best_codec("h264");
100 assert(h264_codec == OAVX_FOURCC_VIDEO_TYPE_H264);
101  
102 /* Multi-codec preference order */
103 uint32_t multi_codec = avx_negotiate_best_codec("av1,hevc,h264");
104 assert(multi_codec == OAVX_FOURCC_VIDEO_TYPE_AV01 || multi_codec == OAVX_FOURCC_VIDEO_TYPE_HEVC ||
105 multi_codec == OAVX_FOURCC_VIDEO_TYPE_H264);
106 printf(" -> Multi-codec matched preference: 0x%08X (%.4s)\n", multi_codec, (const char *)&multi_codec);
107  
108 printf(" -> Codec capability auto-negotiation verified.\n");
109 }
110  
111 /* -------------------------------------------------------------------------- */
112 /* Test 3: Lazy On-Demand Hardware Simulcast Lifecycle */
113 /* -------------------------------------------------------------------------- */
114 static void test_lazy_simulcast_lifecycle(void) {
115 printf("[*] 3. Testing Lazy On-Demand Hardware Simulcast Lifecycle...\n");
116  
117 avx_tx_config_t cfg;
118 memset(&cfg, 0, sizeof(cfg));
119 cfg.name = "SimulcastTestStream";
120 cfg.transport = OAVX_TRANSPORT_TCP;
121  
122 avx_tx_instance_t tx = avx_tx_create(&cfg);
123 assert(tx != NULL);
124  
125 /* Initially: 0 WAN clients, 0 hardware simulcast encoders running */
126 assert(avx_tx_get_wan_client_count(tx) == 0);
127 assert(avx_tx_is_wan_encoder_active(tx) == false);
128  
129 /* Prepare dummy test video frame (1280x720 UYVY) */
130 size_t frame_bytes = 1280 * 720 * 2;
131 uint8_t *frame_buf = (uint8_t *)calloc(1, frame_bytes);
132 assert(frame_buf != NULL);
133  
134 oavx_video_frame_t vf = {.width = 1280,
135 .height = 720,
136 .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY,
137 .scan_type = OAVX_SCAN_PROGRESSIVE,
138 .planes = {frame_buf, NULL, NULL, NULL},
139 .strides = {1280 * 2, 0, 0, 0},
140 .framerate = {60, 1},
141 .aspect_ratio = {16, 9},
142 .pts_ns = 1000000000LL,
143 .dts_ns = 1000000000LL,
144 .metadata = NULL,
145 .metadata_len = 0,
146 .user_ctx = NULL};
147  
148 /* Send frame with no clients: hardware encoder MUST NOT be allocated */
149 avx_tx_send_video(tx, &vf);
150 assert(avx_tx_is_wan_encoder_active(tx) == false);
151  
152 /* Get transmitter port from endpoint info */
153 const oavx_endpoint_t *ep = avx_tx_get_source(tx);
154 assert(ep != NULL);
155 uint16_t port = ep->port;
156 assert(port > 0);
157  
158 /* Connect a test client socket to transmitter */
159 int client_fd = socket(AF_INET, SOCK_STREAM, 0);
160 assert(client_fd >= 0);
161 avx_socket_set_recv_timeout(client_fd, 3000);
162  
163 struct sockaddr_in sin;
164 memset(&sin, 0, sizeof(sin));
165 sin.sin_family = AF_INET;
166 sin.sin_port = htons(port);
167 inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr);
168  
169 int conn_rc = connect(client_fd, (struct sockaddr *)&sin, sizeof(sin));
170 assert(conn_rc == 0);
171  
172 /* Read initial transmitter handshake XML */
173 uint32_t xml_len = 0;
174 size_t len_read = 0;
175 while (len_read < sizeof(xml_len)) {
176 ssize_t r1 = recv(client_fd, (char*)&xml_len + len_read, sizeof(xml_len) - len_read, 0);
177 assert(r1 > 0);
178 len_read += (size_t)r1;
179 }
180 assert(xml_len > 0 && xml_len < 4096);
181  
182 char *xml_buf = (char *)malloc(xml_len + 1);
183 assert(xml_buf != NULL);
184 size_t total_read = 0;
185 while (total_read < xml_len) {
186 ssize_t r2 = recv(client_fd, xml_buf + total_read, xml_len - total_read, 0);
187 assert(r2 > 0);
188 total_read += (size_t)r2;
189 }
190 xml_buf[xml_len] = '\0';
191 free(xml_buf);
192  
193 /* Send reverse XML requesting WAN profile with multi-codec capabilities */
194 char rev_xml[256];
195 int rev_len =
196 snprintf(rev_xml, sizeof(rev_xml), "<transport mode=\"tcp\" codecs=\"h264,hevc,av1\" profile=\"wan\"/>\n");
197  
198 AVX_PACKED_START
199 struct {
200 uint32_t magic;
201 int64_t timecode;
202 uint32_t payload_len;
203 } AVX_PACKED mhdr;
204 AVX_PACKED_END
205 mhdr.magic = OAVX_WIRE_MAGIC_METADATA;
206 mhdr.timecode = 0;
207 mhdr.payload_len = (uint32_t)rev_len;
208  
209 send(client_fd, (const char*)&mhdr, sizeof(mhdr), 0);
210 send(client_fd, rev_xml, rev_len, 0);
211  
212 /* Allow reverse receiver worker thread to process client XML */
213 for (int retry = 0; retry < 50 && avx_tx_get_wan_client_count(tx) != 1; ++retry) {
214 avx_sleep_ms(20);
215 }
216  
217 /* WAN client count should now be 1 */
218 int wan_clients = avx_tx_get_wan_client_count(tx);
219 printf(" -> WAN clients registered: %d\n", wan_clients);
220 assert(wan_clients == 1);
221  
222 /* Send frame: hardware encoder MUST now be lazily spawned! */
223 avx_tx_send_video(tx, &vf);
224  
225 bool enc_active = avx_tx_is_wan_encoder_active(tx);
226 uint32_t wan_codec = avx_tx_get_wan_codec_fourcc(tx);
227 printf(" -> Lazy hardware simulcast encoder active: %s (codec: %.4s)\n", enc_active ? "YES" : "NO",
228 (const char *)&wan_codec);
229 assert(wan_codec != 0);
230 if (avx_video_encoder_has_backend(wan_codec)) {
231 assert(enc_active == true);
232 } else {
233 printf(" -> (Notice: No hardware/software encoder backend for %.4s available; skipping active assertion)\n",
234 (const char *)&wan_codec);
235 }
236  
237 /* Now disconnect the client */
238 avx_socket_close(client_fd);
239  
240 /* Allow listener/worker to detect disconnect */
241 for (int retry = 0; retry < 50 && avx_tx_get_wan_client_count(tx) != 0; ++retry) {
242 avx_sleep_ms(20);
243 }
244  
245 /* Send frame to trigger refcount and lifecycle check */
246 avx_tx_send_video(tx, &vf);
247  
248 /* WAN client count should return to 0 and hardware encoder MUST be destroyed */
249 int remaining_wan = avx_tx_get_wan_client_count(tx);
250 bool remaining_enc = avx_tx_is_wan_encoder_active(tx);
251 printf(" -> Post-disconnect WAN clients: %d, encoder active: %s\n", remaining_wan, remaining_enc ? "YES" : "NO");
252 assert(remaining_wan == 0);
253 assert(remaining_enc == false);
254  
255 free(frame_buf);
256 avx_tx_destroy(tx);
257 printf(" -> Lazy hardware simulcast lifecycle verified successfully.\n");
258 }
259  
260 /* -------------------------------------------------------------------------- */
261 /* Test 4: Dynamic Re-Negotiation & Seamless In-Flight Migration */
262 /* -------------------------------------------------------------------------- */
263 static void test_inflight_profile_migration(void) {
264 printf("[*] 4. Testing Dynamic Re-Negotiation & Seamless In-Flight Migration...\n");
265  
266 avx_tx_config_t tx_cfg;
267 memset(&tx_cfg, 0, sizeof(tx_cfg));
268 tx_cfg.name = "MigrationTestStream";
269 tx_cfg.transport = OAVX_TRANSPORT_TCP;
270  
271 avx_tx_instance_t tx = avx_tx_create(&tx_cfg);
272 assert(tx != NULL);
273  
274 const oavx_endpoint_t *ep = avx_tx_get_source(tx);
275 assert(ep != NULL);
276  
277 /* Spawn receiver using public avx_rx API */
278 avx_rx_config_t rx_cfg;
279 memset(&rx_cfg, 0, sizeof(rx_cfg));
280 rx_cfg.source_to_connect_to = *ep;
281 rx_cfg.color_format = OAVX_COLOR_FORMAT_FASTEST;
282  
283 avx_rx_instance_t rx = avx_rx_create(&rx_cfg);
284 assert(rx != NULL);
285  
286 /* Allow receiver to connect */
287 avx_sleep_ms(200);
288  
289 /* Send test frame */
290 size_t frame_bytes = 1280 * 720 * 2;
291 uint8_t *frame_buf = (uint8_t *)calloc(1, frame_bytes);
292 assert(frame_buf != NULL);
293  
294 oavx_video_frame_t vf = {
295 .width = 1280,
296 .height = 720,
297 .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY,
298 .scan_type = OAVX_SCAN_PROGRESSIVE,
299 .planes = {frame_buf, NULL, NULL, NULL},
300 .strides = {1280 * 2, 0, 0, 0},
301 .framerate = {60, 1},
302 .aspect_ratio = {16, 9},
303 .pts_ns = 2000000000LL,
304 .dts_ns = 2000000000LL,
305 };
306 avx_tx_send_video(tx, &vf);
307  
308 /* Initial state: Localhost is private LAN -> 0 WAN clients, 0 WAN encoder */
309 assert(avx_tx_get_wan_client_count(tx) == 0);
310 assert(avx_tx_is_wan_encoder_active(tx) == false);
311  
312 /* Now receiver dynamically requests profile migration to WAN */
313 bool req_ok = false;
314 for (int retry = 0; retry < 50 && !req_ok; ++retry) {
315 req_ok = avx_rx_request_profile(rx, "wan");
316 if (!req_ok) avx_sleep_ms(20);
317 }
318 assert(req_ok == true);
319  
320 /* Allow reverse worker to process request */
321 for (int retry = 0; retry < 50 && avx_tx_get_wan_client_count(tx) != 1; ++retry) {
322 avx_sleep_ms(20);
323 }
324  
325 /* WAN client count should now be 1 */
326 assert(avx_tx_get_wan_client_count(tx) == 1);
327  
328 /* Send video frame: lazy WAN hardware encoder activates */
329 avx_tx_send_video(tx, &vf);
330 uint32_t wan_codec = avx_tx_get_wan_codec_fourcc(tx);
331 if (avx_video_encoder_has_backend(wan_codec)) {
332 assert(avx_tx_is_wan_encoder_active(tx) == true);
333 } else {
334 printf(" -> (Notice: No hardware/software encoder backend for %.4s available; skipping active assertion)\n",
335 (const char *)&wan_codec);
336 }
337 printf(" -> In-flight promotion to WAN: encoder active (codec: %.4s)\n", (const char *)&wan_codec);
338  
339 /* Now receiver dynamically switches back to LAN */
340 req_ok = false;
341 for (int retry = 0; retry < 50 && !req_ok; ++retry) {
342 req_ok = avx_rx_request_profile(rx, "lan");
343 if (!req_ok) avx_sleep_ms(20);
344 }
345 assert(req_ok == true);
346  
347 /* Allow reverse worker to process demotion */
348 for (int retry = 0; retry < 50 && (avx_tx_get_wan_client_count(tx) != 0 || avx_tx_is_wan_encoder_active(tx)); ++retry) {
349 avx_sleep_ms(20);
350 }
351  
352 /* WAN client count should now be 0 */
353 assert(avx_tx_get_wan_client_count(tx) == 0);
354 assert(avx_tx_is_wan_encoder_active(tx) == false);
355 printf(" -> In-flight demotion to LAN: encoder destroyed cleanly\n");
356  
357 /* Also test sender-side explicit profile migration API: avx_tx_set_client_profile */
358 bool set_ok = avx_tx_set_client_profile(tx, 0, "wan");
359 assert(set_ok == true);
360 assert(avx_tx_get_wan_client_count(tx) == 1);
361  
362 avx_tx_send_video(tx, &vf);
363 if (avx_video_encoder_has_backend(wan_codec)) {
364 assert(avx_tx_is_wan_encoder_active(tx) == true);
365 }
366  
367 set_ok = avx_tx_set_client_profile(tx, 0, "lan");
368 assert(set_ok == true);
369 assert(avx_tx_get_wan_client_count(tx) == 0);
370 assert(avx_tx_is_wan_encoder_active(tx) == false);
371 printf(" -> Sender-side avx_tx_set_client_profile verified\n");
372  
373 free(frame_buf);
374 avx_rx_destroy(rx);
375 avx_tx_destroy(tx);
376 printf(" -> Dynamic re-negotiation & seamless migration verified successfully.\n");
377 }
378  
379 int main(void) {
380 avx_platform_sockets_init();
381 printf("===================================================================\n");
382 printf(" OpenAVX Lazy On-Demand Hardware Simulcast Test Suite \n");
383 printf("===================================================================\n");
384  
385 test_address_classification();
386 test_codec_negotiation();
387 test_lazy_simulcast_lifecycle();
388 test_inflight_profile_migration();
389  
390 avx_platform_sockets_cleanup();
391 printf("\n>>> ALL SIMULCAST TESTS PASSED SUCCESSFULLY! <<<\n");
392 return 0;
393 }