Raw ↗
+ +
a18b27f fix(windows): resolve Win32 Winsock, signal, and CLI test compatibility
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/oavx_async_fd_test.c - Unit test for oavx_status_t and Async Socket Event-Loop Hooks
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  
17 #if defined(_WIN32)
18 #include <winsock2.h>
19 #include <unistd.h>
20 #else
21 #include <poll.h>
22 #include <unistd.h>
23 #endif
24  
25 int main(void) {
26 printf("=== OpenAVX Async Socket Hooks & Status Domain Tests ===\n\n");
27  
28 /* --------------------------------------------------------------------- */
29 /* 1. Status Domain & Stringifier */
30 /* --------------------------------------------------------------------- */
31 printf("[*] 1. Testing oavx_status_t and oavx_status_to_string()...\n");
32 assert(strcmp(oavx_status_to_string(OAVX_SUCCESS), "Success") == 0);
33 assert(strcmp(oavx_status_to_string(OAVX_ERR_INVALID_ARG), "Invalid argument or null pointer") == 0);
34 assert(strcmp(oavx_status_to_string(OAVX_ERR_NOT_CONNECTED), "Transport not connected") == 0);
35 assert(strcmp(oavx_status_to_string((oavx_status_t)999), "Unknown error code") == 0);
36 printf(" [+] Status domain descriptions verified: SUCCESS=0, ERR < 0\n");
37  
38 /* --------------------------------------------------------------------- */
39 /* 2. Error Code Returns on Invalid Invocations */
40 /* --------------------------------------------------------------------- */
41 printf("[*] 2. Testing error returns on null/invalid parameters...\n");
42 assert(oavx_stream_send_video(NULL, NULL) == OAVX_ERR_INVALID_ARG);
43 assert(oavx_stream_send_audio(NULL, NULL) == OAVX_ERR_INVALID_ARG);
44 assert(oavx_stream_send_metadata(NULL, NULL) == OAVX_ERR_INVALID_ARG);
45 assert(oavx_stream_get_fd(NULL) == -1);
46 assert(oavx_discovery_get_fd(NULL) == -1);
47 printf(" [+] Input validation and error codes verified!\n");
48  
49 /* --------------------------------------------------------------------- */
50 /* 3. Initializing Context & Streams */
51 /* --------------------------------------------------------------------- */
52 printf("[*] 3. Creating context and opening transmit stream on port 7965...\n");
53 oavx_context_t *ctx = oavx_context_create();
54 assert(ctx != NULL);
55  
56 oavx_stream_config_t tx_cfg = {
57 .name = "ASYNC_TX_PROD",
58 .direction = OAVX_DIR_TRANSMIT,
59 .clock_sync = false
60 };
61 oavx_stream_t *tx_stream = oavx_stream_open(ctx, &tx_cfg);
62 assert(tx_stream != NULL);
63  
64 int tx_fd = oavx_stream_get_fd(tx_stream);
65 const oavx_endpoint_t *tx_ep = oavx_stream_get_endpoint(tx_stream);
66 assert(tx_ep != NULL);
67 assert(tx_ep->address != NULL && tx_ep->address[0] != '\0');
68 printf(" [+] Transmitter stream socket file descriptor: %d (endpoint: %s)\n", tx_fd, tx_ep->address);
69 assert(tx_fd >= 0);
70  
71 /* --------------------------------------------------------------------- */
72 /* 4. Receiver Socket Hook & Async poll() Event Loop Wakeup */
73 /* --------------------------------------------------------------------- */
74 printf("[*] 4. Opening receiver stream connecting to %s...\n", tx_ep->address);
75 oavx_stream_config_t rx_cfg = {
76 .name = "ASYNC_RX_PROD",
77 .endpoint = tx_ep->address,
78 .direction = OAVX_DIR_RECEIVE,
79 .color_format = OAVX_COLOR_FORMAT_UYVY_RGBA,
80 .bandwidth = OAVX_BANDWIDTH_HIGHEST
81 };
82 oavx_stream_t *rx_stream = oavx_stream_open(ctx, &rx_cfg);
83 assert(rx_stream != NULL);
84  
85 /* Wait briefly for TCP connection and handshake */
86 usleep(50000);
87  
88 int rx_fd = oavx_stream_get_fd(rx_stream);
89 printf(" [+] Receiver stream socket file descriptor: %d\n", rx_fd);
90 assert(rx_fd >= 0);
91  
92 #if !defined(_WIN32)
93 /* Test non-blocking event-loop poll */
94 struct pollfd pfd = {
95 .fd = rx_fd,
96 .events = POLLIN,
97 .revents = 0
98 };
99  
100 /* Initial poll: buffer is empty, should timeout immediately */
101 int poll_init = poll(&pfd, 1, 0);
102 assert(poll_init == 0);
103 printf(" [+] Initial poll() verified: socket empty (0 events)\n");
104  
105 /* Transmit a video frame */
106 uint8_t dummy_video[640 * 360 * 2];
107 memset(dummy_video, 0x55, sizeof(dummy_video));
108 oavx_video_frame_t tx_frame = {
109 .width = 640,
110 .height = 360,
111 .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY,
112 .scan_type = OAVX_SCAN_PROGRESSIVE,
113 .planes = { dummy_video, NULL, NULL, NULL },
114 .strides = { 640 * 2, 0, 0, 0 },
115 .framerate = { 60, 1 },
116 .aspect_ratio = { 16, 9 },
117 .pts_ns = 5000000000LL
118 };
119  
120 oavx_status_t send_st = oavx_stream_send_video(tx_stream, &tx_frame);
121 assert(send_st == OAVX_SUCCESS);
122 printf(" [+] Transmitted video frame via oavx_stream_send_video: status = %s\n", oavx_status_to_string(send_st));
123  
124 /* Poll receiver socket: should wake up with POLLIN */
125 int poll_active = poll(&pfd, 1, 1000);
126 assert(poll_active > 0);
127 assert(pfd.revents & POLLIN);
128 printf(" [+] Async event loop woke up on POLLIN event (revents = 0x%X)!\n", pfd.revents);
129  
130 /* Non-blocking / low-timeout receive to consume ready frame */
131 oavx_video_frame_t rx_frame;
132 memset(&rx_frame, 0, sizeof(rx_frame));
133 oavx_frame_type_t ftype = oavx_stream_receive(rx_stream, &rx_frame, NULL, NULL, 100);
134 assert(ftype == OAVX_FRAME_TYPE_VIDEO);
135 assert(rx_frame.width == 640 && rx_frame.height == 360);
136 assert(rx_frame.pts_ns == 5000000000LL);
137 printf(" [+] Successfully captured frame without blocking (640x360 @ PTS: %ld ns)\n", (long)rx_frame.pts_ns);
138  
139 oavx_stream_release_video(rx_stream, &rx_frame);
140 #endif
141  
142 /* --------------------------------------------------------------------- */
143 /* 5. Discovery Socket Hook */
144 /* --------------------------------------------------------------------- */
145 printf("[*] 5. Testing oavx_discovery_get_fd()...\n");
146 oavx_discovery_t *disc = oavx_discovery_create(ctx, NULL);
147 assert(disc != NULL);
148  
149 int disc_fd = oavx_discovery_get_fd(disc);
150 printf(" [+] Discovery mDNS socket file descriptor: %d\n", disc_fd);
151 assert(disc_fd >= 0);
152  
153 oavx_discovery_destroy(disc);
154  
155 /* --------------------------------------------------------------------- */
156 /* 6. Clean Teardown */
157 /* --------------------------------------------------------------------- */
158 printf("[*] 6. Tearing down streams and destroying context...\n");
159 oavx_stream_close(rx_stream);
160 oavx_stream_close(tx_stream);
161 oavx_context_destroy(ctx);
162  
163 printf("\n[PASS] oavx_async_fd_test\n");
164 return 0;
165 }