+ +
a18b27f fix(windows): resolve Win32 Winsock, signal, and CLI test compatibility
saifmukhtar · 2026-11-24
main
| 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/oavx_multiclient_test.c - Unit test suite for Multi-Client Fan-Out & Head-of-Line Protection |
| 7 | */ |
| 8 | |
| 9 | #define _DEFAULT_SOURCE |
| 10 | #define _POSIX_C_SOURCE 200809L |
| 11 | #include "openavx.h" |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <assert.h> |
| 16 | #include <unistd.h> |
| 17 | #include <sys/socket.h> |
| 18 | #include <netinet/in.h> |
| 19 | #include <arpa/inet.h> |
| 20 | #include <time.h> |
| 21 | |
| 22 | int main(void) { |
| 23 | printf("=== OpenAVX Multi-Client Fan-Out & Tally Tests ===\n\n"); |
| 24 | |
| 25 | oavx_context_t *ctx = oavx_context_create(); |
| 26 | assert(ctx != NULL); |
| 27 | |
| 28 | /* --------------------------------------------------------------------- */ |
| 29 | /* 1. Configurable Capacity Enforcement */ |
| 30 | /* --------------------------------------------------------------------- */ |
| 31 | printf("[*] 1. Testing Configurable Client Limits (max_connections = 3)...\n"); |
| 32 | |
| 33 | oavx_stream_config_t tx_limited_cfg = { |
| 34 | .name = "FANOUT_LIMITED_TX", |
| 35 | .direction = OAVX_DIR_TRANSMIT, |
| 36 | .clock_sync = false, |
| 37 | .max_connections = 3 |
| 38 | }; |
| 39 | oavx_stream_t *tx_limited = oavx_stream_open(ctx, &tx_limited_cfg); |
| 40 | assert(tx_limited != NULL); |
| 41 | |
| 42 | const oavx_endpoint_t *ep_limited = oavx_stream_get_endpoint(tx_limited); |
| 43 | assert(ep_limited && ep_limited->address); |
| 44 | |
| 45 | /* Connect 3 allowed receivers */ |
| 46 | oavx_stream_t *rx_lim[3]; |
| 47 | for (int i = 0; i < 3; ++i) { |
| 48 | oavx_stream_config_t rx_cfg = { |
| 49 | .name = "FANOUT_LIMITED_RX", |
| 50 | .endpoint = ep_limited->address, |
| 51 | .direction = OAVX_DIR_RECEIVE, |
| 52 | .color_format = OAVX_COLOR_FORMAT_UYVY_RGBA, |
| 53 | .bandwidth = OAVX_BANDWIDTH_HIGHEST |
| 54 | }; |
| 55 | rx_lim[i] = oavx_stream_open(ctx, &rx_cfg); |
| 56 | assert(rx_lim[i] != NULL); |
| 57 | } |
| 58 | |
| 59 | usleep(50000); /* 50 ms for connections to settle */ |
| 60 | int count_limited = oavx_stream_get_connection_count(tx_limited, 100); |
| 61 | printf(" [+] Connected %d clients to limited sender (target 3)\n", count_limited); |
| 62 | assert(count_limited == 3); |
| 63 | |
| 64 | /* 4th receiver should be rejected / dropped by listener */ |
| 65 | oavx_stream_config_t rx_extra_cfg = { |
| 66 | .name = "FANOUT_EXTRA_RX", |
| 67 | .endpoint = ep_limited->address, |
| 68 | .direction = OAVX_DIR_RECEIVE, |
| 69 | .color_format = OAVX_COLOR_FORMAT_UYVY_RGBA, |
| 70 | .bandwidth = OAVX_BANDWIDTH_HIGHEST |
| 71 | }; |
| 72 | oavx_stream_t *rx_extra = oavx_stream_open(ctx, &rx_extra_cfg); |
| 73 | usleep(50000); |
| 74 | int count_after_extra = oavx_stream_get_connection_count(tx_limited, 100); |
| 75 | printf(" [+] Connection count after 4th client attempt: %d (Enforced <= 3)\n", count_after_extra); |
| 76 | assert(count_after_extra == 3); |
| 77 | |
| 78 | if (rx_extra) oavx_stream_close(rx_extra); |
| 79 | for (int i = 0; i < 3; ++i) { |
| 80 | oavx_stream_close(rx_lim[i]); |
| 81 | } |
| 82 | oavx_stream_close(tx_limited); |
| 83 | printf(" [+] Capacity enforcement verified!\n\n"); |
| 84 | |
| 85 | /* --------------------------------------------------------------------- */ |
| 86 | /* 2. Multi-Client Fan-Out Broadcast (6 Simultaneous Receivers) */ |
| 87 | /* --------------------------------------------------------------------- */ |
| 88 | printf("[*] 2. Testing Fan-Out Broadcast to 6 Parallel Receivers...\n"); |
| 89 | |
| 90 | oavx_stream_config_t tx_cfg = { |
| 91 | .name = "STUDIO_CAMERA_1_FANOUT", |
| 92 | .direction = OAVX_DIR_TRANSMIT, |
| 93 | .clock_sync = false, |
| 94 | .max_connections = 16 |
| 95 | }; |
| 96 | oavx_stream_t *tx = oavx_stream_open(ctx, &tx_cfg); |
| 97 | assert(tx != NULL); |
| 98 | |
| 99 | const oavx_endpoint_t *ep = oavx_stream_get_endpoint(tx); |
| 100 | assert(ep && ep->address); |
| 101 | printf(" [+] Sender active on %s\n", ep->address); |
| 102 | |
| 103 | #define NUM_VIEWERS 6 |
| 104 | oavx_stream_t *rx[NUM_VIEWERS]; |
| 105 | for (int i = 0; i < NUM_VIEWERS; ++i) { |
| 106 | char rx_name[64]; |
| 107 | snprintf(rx_name, sizeof(rx_name), "VIEWER_%d", i + 1); |
| 108 | oavx_stream_config_t rx_cfg = { |
| 109 | .name = rx_name, |
| 110 | .endpoint = ep->address, |
| 111 | .direction = OAVX_DIR_RECEIVE, |
| 112 | .color_format = OAVX_COLOR_FORMAT_UYVY_RGBA, |
| 113 | .bandwidth = OAVX_BANDWIDTH_HIGHEST |
| 114 | }; |
| 115 | rx[i] = oavx_stream_open(ctx, &rx_cfg); |
| 116 | assert(rx[i] != NULL); |
| 117 | } |
| 118 | |
| 119 | usleep(100000); /* 100 ms to establish all 6 TCP sessions */ |
| 120 | int active_conns = oavx_stream_get_connection_count(tx, 100); |
| 121 | printf(" [+] Active connection count on sender: %d (expected %d)\n", active_conns, NUM_VIEWERS); |
| 122 | assert(active_conns == NUM_VIEWERS); |
| 123 | |
| 124 | /* Allocate and transmit a test video frame */ |
| 125 | const uint32_t width = 320; |
| 126 | const uint32_t height = 240; |
| 127 | size_t line_stride = (size_t)width * 2; |
| 128 | size_t frame_bytes = line_stride * height; |
| 129 | uint8_t *vbuf = (uint8_t*)malloc(frame_bytes); |
| 130 | assert(vbuf != NULL); |
| 131 | memset(vbuf, 0x5A, frame_bytes); |
| 132 | |
| 133 | oavx_video_frame_t vframe = { |
| 134 | .width = width, |
| 135 | .height = height, |
| 136 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY, |
| 137 | .scan_type = OAVX_SCAN_PROGRESSIVE, |
| 138 | .planes = { vbuf, NULL, NULL, NULL }, |
| 139 | .strides = { line_stride, 0, 0, 0 }, |
| 140 | .framerate = { .num = 60000, .den = 1000 }, |
| 141 | .aspect_ratio = { .num = 16, .den = 9 }, |
| 142 | .pts_ns = 1234567890LL |
| 143 | }; |
| 144 | |
| 145 | printf(" [+] Broadcasting video frame (320x240 UYVY) to all %d viewers...\n", NUM_VIEWERS); |
| 146 | int tx_res = oavx_stream_send_video(tx, &vframe); |
| 147 | assert(tx_res == 0); |
| 148 | |
| 149 | /* Verify each of the 6 receivers captures the identical frame */ |
| 150 | for (int i = 0; i < NUM_VIEWERS; ++i) { |
| 151 | oavx_video_frame_t cap_v = {0}; |
| 152 | oavx_frame_type_t ftype = oavx_stream_receive(rx[i], &cap_v, NULL, NULL, 500); |
| 153 | assert(ftype == OAVX_FRAME_TYPE_VIDEO); |
| 154 | assert(cap_v.width == width); |
| 155 | assert(cap_v.height == height); |
| 156 | assert(cap_v.pts_ns == 1234567890LL); |
| 157 | assert(cap_v.planes[0] != NULL); |
| 158 | assert(((uint8_t*)cap_v.planes[0])[0] == 0x5A); |
| 159 | assert(((uint8_t*)cap_v.planes[0])[frame_bytes - 1] == 0x5A); |
| 160 | oavx_stream_release_video(rx[i], &cap_v); |
| 161 | } |
| 162 | printf(" [+] All %d parallel receivers captured the broadcast video frame with 100%% integrity!\n\n", NUM_VIEWERS); |
| 163 | |
| 164 | /* Allocate and transmit an audio frame */ |
| 165 | const uint32_t sample_rate = 48000; |
| 166 | const uint16_t channels = 2; |
| 167 | const uint32_t samples = 1024; |
| 168 | float *abuf = (float*)malloc(samples * channels * sizeof(float)); |
| 169 | assert(abuf != NULL); |
| 170 | for (size_t s = 0; s < (size_t)channels * samples; ++s) { |
| 171 | abuf[s] = 0.75f; |
| 172 | } |
| 173 | |
| 174 | oavx_audio_frame_t aframe = { |
| 175 | .sample_rate = sample_rate, |
| 176 | .channels = channels, |
| 177 | .frames = samples, |
| 178 | .format = OAVX_AUDIO_FMT_FLT_PLANAR, |
| 179 | .planes = { (uint8_t*)abuf, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, |
| 180 | .stride_bytes = samples * sizeof(float), |
| 181 | .pts_ns = 2000000000LL |
| 182 | }; |
| 183 | |
| 184 | printf(" [+] Broadcasting audio frame (48kHz stereo) to all %d viewers...\n", NUM_VIEWERS); |
| 185 | int atx_res = oavx_stream_send_audio(tx, &aframe); |
| 186 | assert(atx_res == 0); |
| 187 | |
| 188 | /* Verify each of the 6 receivers captures the audio frame */ |
| 189 | for (int i = 0; i < NUM_VIEWERS; ++i) { |
| 190 | oavx_audio_frame_t cap_a = {0}; |
| 191 | oavx_frame_type_t ftype = oavx_stream_receive(rx[i], NULL, &cap_a, NULL, 500); |
| 192 | assert(ftype == OAVX_FRAME_TYPE_AUDIO); |
| 193 | assert(cap_a.sample_rate == sample_rate); |
| 194 | assert(cap_a.channels == channels); |
| 195 | assert(cap_a.frames == samples); |
| 196 | float *samples_in = (float*)cap_a.planes[0]; |
| 197 | assert(samples_in != NULL); |
| 198 | assert(samples_in[0] == 0.75f); |
| 199 | oavx_stream_release_audio(rx[i], &cap_a); |
| 200 | } |
| 201 | printf(" [+] All %d parallel receivers captured the broadcast audio frame!\n\n", NUM_VIEWERS); |
| 202 | |
| 203 | /* --------------------------------------------------------------------- */ |
| 204 | /* 3. Dynamic Tally Aggregation */ |
| 205 | /* --------------------------------------------------------------------- */ |
| 206 | printf("[*] 3. Testing Dynamic Tally Aggregation...\n"); |
| 207 | |
| 208 | /* Viewer 1 is on Program (OBS live output) */ |
| 209 | oavx_tally_t t1 = { .flags = OAVX_TALLY_PROGRAM }; |
| 210 | assert(oavx_stream_set_tally(rx[0], &t1)); |
| 211 | |
| 212 | /* Viewer 2 is on Preview (Director multiviewer) */ |
| 213 | oavx_tally_t t2 = { .flags = OAVX_TALLY_PREVIEW }; |
| 214 | assert(oavx_stream_set_tally(rx[1], &t2)); |
| 215 | |
| 216 | /* Viewers 3..6 are inactive/recording */ |
| 217 | oavx_tally_t t_none = { .flags = OAVX_TALLY_NONE }; |
| 218 | for (int i = 2; i < NUM_VIEWERS; ++i) { |
| 219 | assert(oavx_stream_set_tally(rx[i], &t_none)); |
| 220 | } |
| 221 | |
| 222 | usleep(100000); /* 100 ms for reverse tally packets to reach sender */ |
| 223 | |
| 224 | oavx_tally_t query_tally = {0}; |
| 225 | assert(oavx_stream_get_tally(tx, &query_tally, 200)); |
| 226 | printf(" [+] Sender Aggregated Tally Flags: 0x%02X (Program=%d, Preview=%d)\n", |
| 227 | query_tally.flags, |
| 228 | (query_tally.flags & OAVX_TALLY_PROGRAM) ? 1 : 0, |
| 229 | (query_tally.flags & OAVX_TALLY_PREVIEW) ? 1 : 0); |
| 230 | assert(query_tally.flags & OAVX_TALLY_PROGRAM); |
| 231 | assert(query_tally.flags & OAVX_TALLY_PREVIEW); |
| 232 | |
| 233 | /* --------------------------------------------------------------------- */ |
| 234 | /* 4. Client Disconnection & Dynamic Recalculation */ |
| 235 | /* --------------------------------------------------------------------- */ |
| 236 | printf("[*] 4. Testing Disconnection & Dynamic Recalculation...\n"); |
| 237 | printf(" [-] Disconnecting Viewer 1 (Program viewer)...\n"); |
| 238 | oavx_stream_close(rx[0]); |
| 239 | rx[0] = NULL; |
| 240 | |
| 241 | usleep(150000); /* 150 ms for reverse reader to detect hangup and recalculate */ |
| 242 | |
| 243 | int remaining_conns = oavx_stream_get_connection_count(tx, 100); |
| 244 | printf(" [+] Sender connection count after disconnect: %d (expected %d)\n", |
| 245 | remaining_conns, NUM_VIEWERS - 1); |
| 246 | assert(remaining_conns == (NUM_VIEWERS - 1)); |
| 247 | |
| 248 | memset(&query_tally, 0, sizeof(query_tally)); |
| 249 | assert(oavx_stream_get_tally(tx, &query_tally, 200)); |
| 250 | printf(" [+] Sender Tally Flags after Program viewer left: 0x%02X (Program=%d, Preview=%d)\n", |
| 251 | query_tally.flags, |
| 252 | (query_tally.flags & OAVX_TALLY_PROGRAM) ? 1 : 0, |
| 253 | (query_tally.flags & OAVX_TALLY_PREVIEW) ? 1 : 0); |
| 254 | assert(!(query_tally.flags & OAVX_TALLY_PROGRAM)); |
| 255 | assert(query_tally.flags & OAVX_TALLY_PREVIEW); |
| 256 | |
| 257 | /* Broadcast another frame to verify surviving 5 viewers receive it undisturbed */ |
| 258 | vframe.pts_ns = 3000000000LL; |
| 259 | vbuf[0] = 0xBE; |
| 260 | vbuf[frame_bytes - 1] = 0xEF; |
| 261 | assert(oavx_stream_send_video(tx, &vframe) == 0); |
| 262 | |
| 263 | for (int i = 1; i < NUM_VIEWERS; ++i) { |
| 264 | oavx_video_frame_t cap_v2 = {0}; |
| 265 | oavx_frame_type_t ftype = oavx_stream_receive(rx[i], &cap_v2, NULL, NULL, 500); |
| 266 | assert(ftype == OAVX_FRAME_TYPE_VIDEO); |
| 267 | assert(((uint8_t*)cap_v2.planes[0])[0] == 0xBE); |
| 268 | assert(((uint8_t*)cap_v2.planes[0])[frame_bytes - 1] == 0xEF); |
| 269 | oavx_stream_release_video(rx[i], &cap_v2); |
| 270 | } |
| 271 | printf(" [+] Surviving %d viewers streamed undisturbed!\n\n", NUM_VIEWERS - 1); |
| 272 | |
| 273 | /* --------------------------------------------------------------------- */ |
| 274 | /* 5. Telemetry Wire Fan-Out Verification */ |
| 275 | /* --------------------------------------------------------------------- */ |
| 276 | printf("[*] 5. Verifying Telemetry Wire Accounting...\n"); |
| 277 | oavx_telemetry_t telem; |
| 278 | assert(oavx_stream_get_telemetry(tx, &telem)); |
| 279 | printf(" [+] Transmitter Telemetry: is_connected=%d, video_frames=%llu, total_bytes=%llu\n", |
| 280 | telem.is_connected ? 1 : 0, |
| 281 | (unsigned long long)telem.video_frames_total, |
| 282 | (unsigned long long)telem.total_wire_bytes); |
| 283 | assert(telem.is_connected); |
| 284 | assert(telem.video_frames_total == 2); |
| 285 | assert(telem.total_wire_bytes > 0); |
| 286 | |
| 287 | /* --------------------------------------------------------------------- */ |
| 288 | /* 6. Head-of-Line Blocking Protection (Async TX Isolation) */ |
| 289 | /* --------------------------------------------------------------------- */ |
| 290 | printf("[*] 6. Testing Head-of-Line Blocking Isolation (Slow Viewer vs Fast Viewer)...\n"); |
| 291 | |
| 292 | /* Close previous fanout session first */ |
| 293 | for (int i = 1; i < NUM_VIEWERS; ++i) { |
| 294 | if (rx[i]) { |
| 295 | oavx_stream_close(rx[i]); |
| 296 | rx[i] = NULL; |
| 297 | } |
| 298 | } |
| 299 | oavx_stream_close(tx); |
| 300 | tx = NULL; |
| 301 | usleep(50000); /* 50 ms for port recycling */ |
| 302 | |
| 303 | oavx_stream_config_t tx_hol_cfg = { |
| 304 | .name = "FANOUT_HOL_TX", |
| 305 | .direction = OAVX_DIR_TRANSMIT, |
| 306 | .clock_sync = false |
| 307 | }; |
| 308 | oavx_stream_t *tx_hol = oavx_stream_open(ctx, &tx_hol_cfg); |
| 309 | assert(tx_hol != NULL); |
| 310 | |
| 311 | const oavx_endpoint_t *ep_hol = oavx_stream_get_endpoint(tx_hol); |
| 312 | assert(ep_hol && ep_hol->address); |
| 313 | |
| 314 | /* Fast Viewer 1: normal receiver reading all frames */ |
| 315 | oavx_stream_config_t rx_fast_cfg = { |
| 316 | .name = "FAST_VIEWER_1", |
| 317 | .endpoint = ep_hol->address, |
| 318 | .direction = OAVX_DIR_RECEIVE, |
| 319 | .color_format = OAVX_COLOR_FORMAT_UYVY_RGBA, |
| 320 | .bandwidth = OAVX_BANDWIDTH_HIGHEST |
| 321 | }; |
| 322 | oavx_stream_t *rx_fast = oavx_stream_open(ctx, &rx_fast_cfg); |
| 323 | assert(rx_fast != NULL); |
| 324 | |
| 325 | /* Slow Viewer 2: connect raw TCP socket with tiny buffer and NEVER read */ |
| 326 | int slow_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 327 | assert(slow_fd >= 0); |
| 328 | int rcvbuf = 4096; |
| 329 | setsockopt(slow_fd, SOL_SOCKET, SO_RCVBUF, (const char*)&rcvbuf, sizeof(rcvbuf)); |
| 330 | |
| 331 | struct sockaddr_in saddr; |
| 332 | memset(&saddr, 0, sizeof(saddr)); |
| 333 | saddr.sin_family = AF_INET; |
| 334 | saddr.sin_port = htons((uint16_t)ep_hol->port); |
| 335 | inet_pton(AF_INET, "127.0.0.1", &saddr.sin_addr); |
| 336 | int conn_res = connect(slow_fd, (struct sockaddr*)&saddr, sizeof(saddr)); |
| 337 | assert(conn_res == 0); |
| 338 | |
| 339 | /* Wait for connections to be registered (up to 1 second) */ |
| 340 | int hol_conns = 0; |
| 341 | for (int wait = 0; wait < 100; ++wait) { |
| 342 | hol_conns = oavx_stream_get_connection_count(tx_hol, 0); |
| 343 | if (hol_conns >= 2) break; |
| 344 | usleep(10000); /* 10 ms */ |
| 345 | } |
| 346 | printf(" [+] Sender active with %d viewers (1 Fast Viewer + 1 Slow Frozen Viewer)\n", hol_conns); |
| 347 | assert(hol_conns >= 2); |
| 348 | |
| 349 | /* Pump 130 video frames (130 * 153.6KB = ~20MB, exceeding 16MB kernel socket buffer) */ |
| 350 | int fast_received_count = 0; |
| 351 | double max_send_ms = 0.0; |
| 352 | |
| 353 | for (int f = 0; f < 130; ++f) { |
| 354 | vframe.pts_ns = 5000000000LL + (int64_t)f * 16666666LL; |
| 355 | vbuf[0] = (uint8_t)(0x10 + (f & 0xFF)); |
| 356 | |
| 357 | struct timespec t_start, t_end; |
| 358 | clock_gettime(CLOCK_MONOTONIC, &t_start); |
| 359 | int send_ret = oavx_stream_send_video(tx_hol, &vframe); |
| 360 | clock_gettime(CLOCK_MONOTONIC, &t_end); |
| 361 | assert(send_ret == 0); |
| 362 | |
| 363 | double elapsed_ms = (t_end.tv_sec - t_start.tv_sec) * 1000.0 + |
| 364 | (t_end.tv_nsec - t_start.tv_nsec) / 1000000.0; |
| 365 | if (elapsed_ms > max_send_ms) max_send_ms = elapsed_ms; |
| 366 | |
| 367 | /* Fast viewer receives frame */ |
| 368 | oavx_video_frame_t fast_cap = {0}; |
| 369 | oavx_frame_type_t ftype = oavx_stream_receive(rx_fast, &fast_cap, NULL, NULL, 50); |
| 370 | if (ftype == OAVX_FRAME_TYPE_VIDEO) { |
| 371 | fast_received_count++; |
| 372 | oavx_stream_release_video(rx_fast, &fast_cap); |
| 373 | } |
| 374 | |
| 375 | usleep(2000); /* 2 ms pacing */ |
| 376 | } |
| 377 | |
| 378 | printf(" [+] Decoupled Render Loop: Max send() execution time: %.3f ms (Target: < 2.0 ms)\n", max_send_ms); |
| 379 | printf(" [+] Fast Viewer captured %d/130 frames without being blocked!\n", fast_received_count); |
| 380 | assert(fast_received_count >= 30); |
| 381 | |
| 382 | oavx_telemetry_t hol_telem; |
| 383 | assert(oavx_stream_get_telemetry(tx_hol, &hol_telem)); |
| 384 | printf(" [+] Transmitter Telemetry: total_dropped_frames = %llu (Head-of-Line protection active)\n", |
| 385 | (unsigned long long)hol_telem.dropped_frames_total); |
| 386 | assert(hol_telem.dropped_frames_total > 0); |
| 387 | |
| 388 | close(slow_fd); |
| 389 | oavx_stream_close(rx_fast); |
| 390 | oavx_stream_close(tx_hol); |
| 391 | printf(" [+] Head-of-Line Blocking Isolation successfully verified!\n\n"); |
| 392 | |
| 393 | /* --------------------------------------------------------------------- */ |
| 394 | /* 7. Clean Teardown */ |
| 395 | /* --------------------------------------------------------------------- */ |
| 396 | printf("[*] 7. Closing context and freeing buffers...\n"); |
| 397 | oavx_context_destroy(ctx); |
| 398 | free(vbuf); |
| 399 | free(abuf); |
| 400 | |
| 401 | printf("\n[PASS] oavx_multiclient_test\n"); |
| 402 | |
| 403 | return 0; |
| 404 | } |