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/openavx_discovery_udp_test.c - Automated Discovery and UDP Transport verification
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 <unistd.h>
16 #include <pthread.h>
17 #include "avx_platform.h"
18  
19 #include "openavx.h"
20 #include "avx_udp.h"
21 #include "avx_wire.h"
22  
23 #define DISCOVERY_WIDTH 1920
24 #define DISCOVERY_HEIGHT 1080
25  
26 typedef struct {
27 oavx_stream_t *p_send;
28 volatile bool running;
29 uint8_t* frame_buf;
30 } sender_bg_arg_t;
31  
32 static void* sender_bg_worker(void* arg) {
33 sender_bg_arg_t* s_arg = (sender_bg_arg_t*)arg;
34  
35 oavx_video_frame_t video_frame = {
36 .width = DISCOVERY_WIDTH,
37 .height = DISCOVERY_HEIGHT,
38 .fourcc = OAVX_FOURCC_VIDEO_TYPE_SHQ2,
39 .scan_type = OAVX_SCAN_PROGRESSIVE,
40 .planes = { s_arg->frame_buf, NULL, NULL, NULL },
41 .strides = { (size_t)DISCOVERY_WIDTH * 2, 0, 0, 0 },
42 .framerate = { 60000, 1000 },
43 .aspect_ratio = { 16, 9 },
44 .pts_ns = 11223344,
45 .dts_ns = 0,
46 .metadata = NULL,
47 .metadata_len = 0,
48 .user_ctx = NULL
49 };
50  
51 while (s_arg->running) {
52 struct timespec ts = { .tv_sec = 0, .tv_nsec = 15000000L };
53 nanosleep(&ts, NULL);
54 oavx_stream_send_video(s_arg->p_send, &video_frame);
55 }
56 return NULL;
57 }
58  
59 int main(void) {
60 printf("\n=======================================================\n");
61 printf(" OpenAVX Discovery & UDP Transport Test\n");
62 printf("=======================================================\n\n");
63  
64 /* 1. Initialize openavx */
65 printf("[*] 1. Initializing OpenAVX Context...\n");
66 oavx_context_t *ctx = oavx_context_create();
67 assert(ctx != NULL);
68  
69 /* 2. Create Sender */
70 printf("[*] 2. Spawning Sender \"STUDIO_CAMERA_1\"...\n");
71 oavx_stream_config_t send_create = {
72 .name = "STUDIO_CAMERA_1",
73 .groups = "public",
74 .direction = OAVX_DIR_TRANSMIT,
75 .clock_sync = true
76 };
77 oavx_stream_t *p_send = oavx_stream_open(ctx, &send_create);
78 assert(p_send != NULL);
79  
80 size_t raw_size = (size_t)DISCOVERY_WIDTH * DISCOVERY_HEIGHT * 2;
81 uint8_t* raw_uyvy = malloc(raw_size);
82 assert(raw_uyvy != NULL);
83 memset(raw_uyvy, 0x55, raw_size);
84  
85 sender_bg_arg_t s_arg = {
86 .p_send = p_send,
87 .running = true,
88 .frame_buf = raw_uyvy
89 };
90 pthread_t s_thread;
91 pthread_create(&s_thread, NULL, sender_bg_worker, &s_arg);
92  
93 /* 3. Test Dynamic Discovery */
94 printf("[*] 3. Testing OpenAVX Finder Auto-Discovery...\n");
95 oavx_discovery_t *p_find = oavx_discovery_create(ctx, "public");
96 assert(p_find != NULL);
97  
98 bool found = oavx_discovery_wait(p_find, 1000);
99 assert(found);
100  
101 oavx_endpoint_t sources[16];
102 size_t num_sources = oavx_discovery_get_sources(p_find, sources, 16);
103 assert(num_sources >= 1);
104  
105 printf(" [+] Discovered %zu Active OpenAVX Source(s):\n", num_sources);
106 int match_idx = -1;
107 for (size_t i = 0; i < num_sources; ++i) {
108 printf(" [%zu] Name: \"%s\" | URL: \"%s\"\n",
109 i, sources[i].name, sources[i].address);
110 if (strcmp(sources[i].name, "STUDIO_CAMERA_1") == 0) {
111 match_idx = (int)i;
112 }
113 }
114 assert(match_idx >= 0);
115  
116 /* 4. Connect Receiver directly to discovered URL */
117 printf("[*] 4. Connecting Receiver to Discovered Source (\"%s\")...\n",
118 sources[match_idx].address);
119 oavx_stream_config_t recv_create = {
120 .name = "Auto_Discovered_Client",
121 .endpoint = sources[match_idx].address,
122 .direction = OAVX_DIR_RECEIVE,
123 .color_format = OAVX_COLOR_FORMAT_UYVY_RGBA,
124 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
125 .clock_sync = false
126 };
127 oavx_stream_t *p_recv = oavx_stream_open(ctx, &recv_create);
128 assert(p_recv != NULL);
129  
130 oavx_video_frame_t v_frame = {0};
131 oavx_audio_frame_t a_frame = {0};
132 oavx_metadata_frame_t m_frame = {0};
133  
134 bool video_captured = false;
135 for (int attempts = 0; attempts < 50 && !video_captured; ++attempts) {
136 oavx_frame_type_t ftype = oavx_stream_receive(p_recv, &v_frame, &a_frame, &m_frame, 200);
137 if (ftype == OAVX_FRAME_TYPE_VIDEO) {
138 video_captured = true;
139 printf(" [+] Live Stream successfully captured via Auto-Discovery!\n");
140 printf(" - Resolution: %u x %u | FourCC: 0x%08X\n",
141 v_frame.width, v_frame.height, v_frame.fourcc);
142 assert(v_frame.width == DISCOVERY_WIDTH);
143 assert(v_frame.height == DISCOVERY_HEIGHT);
144 oavx_stream_release_video(p_recv, &v_frame);
145 }
146 }
147 assert(video_captured);
148  
149 /* Clean up discovery / streaming sessions */
150 s_arg.running = false;
151 pthread_join(s_thread, NULL);
152 oavx_stream_close(p_recv);
153 oavx_discovery_destroy(p_find);
154 oavx_stream_close(p_send);
155  
156 /* 5. Test High-Throughput UDP Datagram Fragmenter & Reassembler */
157 printf("[*] 5. Testing UDP Datagram Transport Engine...\n");
158 const uint16_t udp_port = 6970;
159 int rx_sock = avx_udp_create_socket(udp_port, true);
160 assert(rx_sock >= 0);
161  
162 int tx_sock = avx_udp_create_socket(0, false);
163 assert(tx_sock >= 0);
164  
165 struct sockaddr_in dest_addr;
166 memset(&dest_addr, 0, sizeof(dest_addr));
167 dest_addr.sin_family = AF_INET;
168 dest_addr.sin_port = htons(udp_port);
169 inet_pton(AF_INET, "127.0.0.1", &dest_addr.sin_addr);
170  
171 /* Generate a 250 KB packet stream (fragmented into ~180 datagrams) */
172 const size_t test_udp_payload_len = 256 * 1024;
173 uint8_t* tx_payload = malloc(test_udp_payload_len);
174 uint8_t* rx_payload = malloc(test_udp_payload_len + 4096);
175 assert(tx_payload && rx_payload);
176  
177 for (size_t i = 0; i < test_udp_payload_len; ++i) {
178 tx_payload[i] = (uint8_t)(i & 0xFF);
179 }
180  
181 uint32_t test_seq = 42;
182 uint32_t test_fourcc = 0x32514853; /* 'SHQ2' */
183 int64_t test_timecode = 9988776655LL;
184  
185 int send_ret = avx_udp_send_frame(tx_sock, &dest_addr, test_seq, test_fourcc, test_timecode,
186 tx_payload, test_udp_payload_len);
187 assert(send_ret == 0);
188 printf(" [+] Fragmented and sent %zu bytes over UDP datagram bursts...\n", test_udp_payload_len);
189  
190 size_t rx_assembled_size = 0;
191 uint32_t rx_fourcc = 0;
192 int64_t rx_timecode = 0;
193  
194 int recv_ret = avx_udp_recv_frame(rx_sock, rx_payload, test_udp_payload_len + 4096,
195 &rx_assembled_size, &rx_fourcc, &rx_timecode, 1000);
196 assert(recv_ret == 0);
197 assert(rx_assembled_size == test_udp_payload_len);
198 assert(rx_fourcc == test_fourcc);
199 assert(rx_timecode == test_timecode);
200 assert(memcmp(tx_payload, rx_payload, test_udp_payload_len) == 0);
201  
202 printf(" [+] Reassembled %zu bytes with 100%% bitwise integrity!\n", rx_assembled_size);
203 printf(" [+] FourCC (0x%08X) and Timecode (%lld) matched!\n", rx_fourcc, (long long)rx_timecode);
204  
205 /* Also verify custom wire profile UDP datagram magic configuration */
206 oavx_wire_profile_t custom_prof = *avx_wire_get_default_profile();
207 custom_prof.magic_udp = 0x54534554; /* 'TEST' */
208 avx_wire_set_active_profile(&custom_prof);
209  
210 uint8_t custom_pkt[sizeof(avx_udp_header_t) + 64];
211 avx_udp_header_t *c_hdr = (avx_udp_header_t*)custom_pkt;
212 c_hdr->magic = 0x54534554;
213 c_hdr->frame_seq = 43;
214 c_hdr->frag_idx = 0;
215 c_hdr->frag_count = 1;
216 c_hdr->fourcc = 0x30514853;
217 c_hdr->timecode = 12345LL;
218 c_hdr->payload_len = 64;
219 c_hdr->reserved = 0;
220 memset(custom_pkt + sizeof(avx_udp_header_t), 0xAA, 64);
221 sendto(tx_sock, (const char*)custom_pkt, sizeof(custom_pkt), 0, (const struct sockaddr*)&dest_addr, sizeof(dest_addr));
222  
223 uint8_t custom_rx[512];
224 recv_ret = avx_udp_recv_frame(rx_sock, custom_rx, sizeof(custom_rx), &rx_assembled_size, &rx_fourcc, &rx_timecode, 1000);
225 assert(recv_ret == 0);
226 assert(rx_assembled_size == 64);
227 assert(custom_rx[0] == 0xAA);
228 printf(" [+] Wire profile extensibility verified: custom UDP magic ('TEST') dynamically accepted!\n");
229  
230 /* Reset to default profile */
231 avx_wire_set_active_profile(avx_wire_get_default_profile());
232  
233 close(rx_sock);
234 close(tx_sock);
235 free(tx_payload);
236 free(rx_payload);
237 free(raw_uyvy);
238 oavx_context_destroy(ctx);
239  
240 printf("\n[PASS] openavx_discovery_udp_test\n\n");
241 return 0;
242 }