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_monitor_test.c - Automated Terminal Player & Monitor Verification with Audio
7 */
8  
9 #define _GNU_SOURCE
10 #define _USE_MATH_DEFINES
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <assert.h>
15 #include <math.h>
16 #include <unistd.h>
17 #include <pthread.h>
18 #include "avx_platform.h"
19  
20 #include "openavx.h"
21 #include "avx_internal.h"
22  
23 static volatile bool s_sender_running = true;
24  
25 static int run_monitor_cmd(const char* args) {
26 char cmd[512];
27 const char* candidates[] = {
28 "./bin/openavx_monitor",
29 "./bin/openavx-monitor",
30 "./build/bin/openavx_monitor",
31 "./build_asan/bin/openavx_monitor",
32 "./openavx_monitor",
33 "./openavx-monitor"
34 };
35 int last_ret = -1;
36 for (size_t i = 0; i < sizeof(candidates)/sizeof(candidates[0]); ++i) {
37 if (access(candidates[i], X_OK) == 0) {
38 #ifdef _WIN32
39 snprintf(cmd, sizeof(cmd), "%s %s > NUL 2>&1", candidates[i], args);
40 #else
41 snprintf(cmd, sizeof(cmd), "ASAN_OPTIONS=detect_leaks=0 %s %s > /dev/null 2>&1", candidates[i], args);
42 #endif
43 int ret = system(cmd);
44 if (WIFEXITED(ret) && WEXITSTATUS(ret) == 0) return 0;
45 last_ret = ret;
46 }
47 }
48 const char* multi_candidates[] = {
49 "./bin/openavx",
50 "./bin/openavx.exe",
51 "bin/openavx.exe",
52 "./openavx",
53 "./openavx.exe",
54 "./build/bin/openavx"
55 };
56 for (size_t i = 0; i < sizeof(multi_candidates)/sizeof(multi_candidates[0]); ++i) {
57 if (access(multi_candidates[i], X_OK) == 0) {
58 #ifdef _WIN32
59 snprintf(cmd, sizeof(cmd), "%s monitor %s > NUL 2>&1", multi_candidates[i], args);
60 #else
61 snprintf(cmd, sizeof(cmd), "ASAN_OPTIONS=detect_leaks=0 %s monitor %s > /dev/null 2>&1", multi_candidates[i], args);
62 #endif
63 int ret = system(cmd);
64 if (WIFEXITED(ret) && WEXITSTATUS(ret) == 0) return 0;
65 last_ret = ret;
66 }
67 }
68 return last_ret;
69 }
70  
71 static void* sender_thread(void* arg) {
72 oavx_stream_t* p_send = (oavx_stream_t*)arg;
73 int width = 320;
74 int height = 180;
75 size_t frame_bytes = (size_t)width * height * 4; /* RGBA */
76 uint8_t* rgba = (uint8_t*)malloc(frame_bytes);
77 if (!rgba) return NULL;
78  
79 /* Synthetic gradient pattern */
80 for (int y = 0; y < height; ++y) {
81 for (int x = 0; x < width; ++x) {
82 size_t idx = ((size_t)y * width + x) * 4;
83 rgba[idx + 0] = (uint8_t)((x * 255) / width); /* R */
84 rgba[idx + 1] = (uint8_t)((y * 255) / height); /* G */
85 rgba[idx + 2] = (uint8_t)(255 - (x * 255) / width); /* B */
86 rgba[idx + 3] = 255; /* A */
87 }
88 }
89  
90 oavx_video_frame_t vf = {
91 .width = width,
92 .height = height,
93 .fourcc = OAVX_FOURCC_VIDEO_TYPE_RGBA,
94 .scan_type = OAVX_SCAN_PROGRESSIVE,
95 .planes = { rgba, NULL, NULL, NULL },
96 .strides = { (size_t)width * 4, 0, 0, 0 },
97 .framerate = { 30, 1 },
98 .aspect_ratio = { 16, 9 },
99 .pts_ns = 100,
100 .dts_ns = 0,
101 .metadata = NULL,
102 .metadata_len = 0,
103 .user_ctx = NULL
104 };
105  
106 /* 48000 Hz, stereo 1024 samples per frame (~21.3ms) */
107 int sample_rate = 48000;
108 int channels = 2;
109 int frames = 1024;
110 float* audio_buf = (float*)malloc((size_t)channels * frames * sizeof(float));
111 if (!audio_buf) {
112 free(rgba);
113 return NULL;
114 }
115  
116 /* 440 Hz test sine wave */
117 for (int s = 0; s < frames; ++s) {
118 float sample = (float)sin(2.0 * M_PI * 440.0 * (double)s / (double)sample_rate) * 0.3f;
119 audio_buf[s] = sample; /* Left channel */
120 audio_buf[frames + s] = sample; /* Right channel */
121 }
122  
123 oavx_audio_frame_t af = {
124 .sample_rate = sample_rate,
125 .channels = channels,
126 .frames = frames,
127 .format = OAVX_AUDIO_FMT_FLT_PLANAR,
128 .planes = { audio_buf, audio_buf + frames, NULL, NULL, NULL, NULL, NULL, NULL },
129 .stride_bytes = frames * sizeof(float),
130 .pts_ns = 100,
131 .metadata = NULL,
132 .metadata_len = 0,
133 .user_ctx = NULL
134 };
135  
136 while (s_sender_running) {
137 vf.pts_ns += 33333333;
138 af.pts_ns = vf.pts_ns;
139 oavx_stream_send_video(p_send, &vf);
140 oavx_stream_send_audio(p_send, &af);
141 usleep(33000); /* ~30 fps */
142 }
143  
144 free(rgba);
145 free(audio_buf);
146 return NULL;
147 }
148  
149 int main(void) {
150 printf("\n==========================================================\n");
151 printf(" OpenAVX Standalone Terminal Player & Audio Monitor Test\n");
152 printf("==========================================================\n\n");
153  
154 /* 1. Test CLI Help Banner */
155 printf("[*] 1. Testing CLI Help Banner (--help)...\n");
156 int ret_help = run_monitor_cmd("--help");
157 assert(ret_help == 0);
158 printf(" [+] Help flag on openavx_monitor returned exit code 0.\n");
159  
160 /* 2. Initialize openavx context & Start Test Sender */
161 printf("[*] 2. Spawning Live A/V Test Streamer on port 7960...\n");
162 oavx_context_t *ctx = oavx_context_create();
163 assert(ctx != NULL);
164  
165 oavx_stream_config_t send_cfg = {
166 .name = "MONITOR_AV_CAM",
167 .groups = "public",
168 .direction = OAVX_DIR_TRANSMIT,
169 .clock_sync = true
170 };
171 oavx_stream_t *p_send = oavx_stream_open(ctx, &send_cfg);
172 assert(p_send != NULL);
173  
174 uint16_t port = 7960;
175 const oavx_endpoint_t *ep = oavx_stream_get_endpoint(p_send);
176 if (ep && ep->address) {
177 const char *colon = strchr(ep->address, ':');
178 if (colon) port = (uint16_t)atoi(colon + 1);
179 }
180 char url[64];
181 snprintf(url, sizeof(url), "127.0.0.1:%u", port);
182 printf(" [+] Test A/V stream online at %s\n", url);
183  
184 pthread_t th;
185 s_sender_running = true;
186 pthread_create(&th, NULL, sender_thread, p_send);
187 usleep(500000); /* 500ms for sender to start and bind socket */
188  
189 /* 3. Test TrueColor Half-Block Rendering with Audio Backend: Auto */
190 printf("[*] 3. Testing TrueColor Monitor with Audio Backend (auto)...\n");
191 char args[256];
192 snprintf(args, sizeof(args), "--url %s --frames 4 -W 40 -H 10 --audio auto", url);
193 int ret_auto = run_monitor_cmd(args);
194 assert(ret_auto == 0);
195 printf(" [+] Monitor with auto audio backend executed successfully (exit 0).\n");
196  
197 /* 4. Test Native PulseAudio Backend */
198 printf("[*] 4. Testing Native PulseAudio Backend (--audio pulse)...\n");
199 snprintf(args, sizeof(args), "--url %s --frames 4 -W 40 -H 10 --audio pulse", url);
200 int ret_pulse = run_monitor_cmd(args);
201 assert(ret_pulse == 0);
202 printf(" [+] PulseAudio live audio backend executed successfully (exit 0).\n");
203  
204 /* 5. Test Native ALSA Backend */
205 printf("[*] 5. Testing Native ALSA Backend (--audio alsa)...\n");
206 snprintf(args, sizeof(args), "--url %s --frames 4 -W 40 -H 10 --audio alsa", url);
207 int ret_alsa = run_monitor_cmd(args);
208 assert(ret_alsa == 0);
209 printf(" [+] ALSA live audio backend executed successfully (exit 0).\n");
210  
211 /* 6. Test Muted Audio Mode */
212 printf("[*] 6. Testing Muted Audio Mode (--no-audio)...\n");
213 snprintf(args, sizeof(args), "--url %s --frames 4 -W 40 -H 10 --no-audio", url);
214 int ret_muted = run_monitor_cmd(args);
215 assert(ret_muted == 0);
216 printf(" [+] Muted mode executed successfully (exit 0).\n");
217  
218 /* 7. Test Unicode Braille 2x4 Dot Matrix Mode */
219 printf("[*] 7. Testing Unicode Braille 2x4 Dot Matrix Mode (--braille)...\n");
220 snprintf(args, sizeof(args), "--url %s --frames 4 -W 40 -H 10 --braille", url);
221 int ret_braille = run_monitor_cmd(args);
222 assert(ret_braille == 0);
223 printf(" [+] Braille matrix mode executed successfully (exit 0).\n");
224  
225 /* 8. Teardown */
226 printf("[*] 8. Tearing down Test Streamer...\n");
227 s_sender_running = false;
228 pthread_join(th, NULL);
229 oavx_stream_close(p_send);
230 oavx_context_destroy(ctx);
231  
232 printf("\n[PASS] openavx_monitor_test\n\n");
233 return 0;
234 }