+ +
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 | * tools/openavx_main.c - Main CLI entry point and subcommand dispatcher |
| 7 | */ |
| 8 | |
| 9 | #define _GNU_SOURCE |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <stdarg.h> |
| 13 | #include <string.h> |
| 14 | #include <stdbool.h> |
| 15 | |
| 16 | #include "avx_platform.h" |
| 17 | #include "avx_simd.h" |
| 18 | #include "openavx.h" |
| 19 | #include "avx_audio_codec.h" |
| 20 | #include "avx_video_codec.h" |
| 21 | #include "openavx_cli.h" |
| 22 | |
| 23 | #if defined(AVX_OS_POSIX) |
| 24 | #include <unistd.h> |
| 25 | #endif |
| 26 | |
| 27 | static const char* get_prog_basename(const char* path) { |
| 28 | if (!path) return "openavx"; |
| 29 | const char* p = strrchr(path, '/'); |
| 30 | #ifdef _WIN32 |
| 31 | const char* p2 = strrchr(path, '\\'); |
| 32 | if (p2 && (!p || p2 > p)) p = p2; |
| 33 | #endif |
| 34 | return p ? p + 1 : path; |
| 35 | } |
| 36 | |
| 37 | static bool prog_matches(const char* base, const char* target) { |
| 38 | size_t target_len = strlen(target); |
| 39 | if (strncasecmp(base, target, target_len) == 0) { |
| 40 | const char* rest = base + target_len; |
| 41 | if (*rest == '\0' || strcasecmp(rest, ".exe") == 0) { |
| 42 | return true; |
| 43 | } |
| 44 | } |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | void openavx_cli_init(void) { |
| 49 | avx_platform_sockets_init(); |
| 50 | const char* env = getenv("OPENAVX_LOG_LEVEL"); |
| 51 | if (!env || env[0] == '\0') { |
| 52 | oavx_set_log_level(OAVX_LOG_INFO); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void openavx_cli_set_quiet(bool quiet) { |
| 57 | if (quiet) { |
| 58 | oavx_set_log_level(OAVX_LOG_ERROR); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void openavx_cli_header(const char* tool_name) { |
| 63 | if (oavx_get_log_level() < OAVX_LOG_INFO) return; |
| 64 | fprintf(stderr, "openavx %s (%s)\n", tool_name ? tool_name : "cli", oavx_version()); |
| 65 | } |
| 66 | |
| 67 | void openavx_cli_kv(const char* key, const char* fmt, ...) { |
| 68 | if (oavx_get_log_level() < OAVX_LOG_INFO || !key || !fmt) return; |
| 69 | fprintf(stderr, " %-12s: ", key); |
| 70 | va_list ap; |
| 71 | va_start(ap, fmt); |
| 72 | vfprintf(stderr, fmt, ap); |
| 73 | va_end(ap); |
| 74 | fputc('\n', stderr); |
| 75 | } |
| 76 | |
| 77 | static bool g_status_line_active = false; |
| 78 | |
| 79 | void openavx_cli_status(const char* module, const char* fmt, ...) { |
| 80 | if (!fmt || oavx_get_log_level() < OAVX_LOG_INFO) return; |
| 81 | |
| 82 | char msg[512]; |
| 83 | va_list ap; |
| 84 | va_start(ap, fmt); |
| 85 | vsnprintf(msg, sizeof(msg), fmt, ap); |
| 86 | va_end(ap); |
| 87 | |
| 88 | bool use_tty = false; |
| 89 | #if defined(AVX_OS_POSIX) |
| 90 | use_tty = (isatty(fileno(stderr)) != 0); |
| 91 | #endif |
| 92 | if (use_tty) { |
| 93 | if (module && module[0] != '\0') { |
| 94 | fprintf(stderr, "\r\033[36m[openavx:%s] [info]\033[0m %s\033[K", module, msg); |
| 95 | } else { |
| 96 | fprintf(stderr, "\r\033[36m[openavx] [info]\033[0m %s\033[K", msg); |
| 97 | } |
| 98 | fflush(stderr); |
| 99 | g_status_line_active = true; |
| 100 | } else { |
| 101 | oavx_log_write(OAVX_LOG_INFO, module, "%s", msg); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void openavx_cli_status_end(void) { |
| 106 | if (g_status_line_active) { |
| 107 | fputc('\n', stderr); |
| 108 | fflush(stderr); |
| 109 | g_status_line_active = false; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | static void print_main_usage(const char* prog) { |
| 114 | printf("OpenAVX CLI (%s) - Open Audio-Video eXchange\n", oavx_version()); |
| 115 | printf("Usage: %s <command> [options]\n\n", prog); |
| 116 | printf("Commands:\n"); |
| 117 | printf(" find Discover and scan active OpenAVX streams across the network\n"); |
| 118 | printf(" monitor Terminal video monitor, ASCII/Braille player & audio receiver\n"); |
| 119 | printf(" route Virtual matrix routing switcher (interactive console or daemon)\n"); |
| 120 | printf(" pattern Broadcast test signal & SMPTE pattern generator with audio tone\n"); |
| 121 | printf(" gateway Network stream ingress to HTTP/fMP4 Web Preview & RTSP bridge\n"); |
| 122 | printf(" server Centralized OpenAVX discovery registry daemon\n"); |
| 123 | printf(" virtcam Virtual camera sink & phone capture (alias 'cam', Zoom / Meet)\n"); |
| 124 | printf(" tally Inspect, monitor, and command broadcast tally lights (PGM/PVW/REC)\n\n"); |
| 125 | printf("Global Flags:\n"); |
| 126 | printf(" -v, --version Display version, runtime codecs, SIMD, and build information\n"); |
| 127 | printf(" -h, --help Display this help message\n\n"); |
| 128 | printf("Subcommand Help:\n"); |
| 129 | printf(" %s <find|monitor|route|pattern|gateway|server|virtcam|tally> --help\n\n", prog); |
| 130 | printf("Compatibility Symlinks:\n"); |
| 131 | printf(" openavx-find, openavx-monitor, openavx-router, openavx-test-pattern,\n"); |
| 132 | printf(" openavx-gateway, openavx-web-gateway, openavx-discovery-server, openavx-virtcam,\n"); |
| 133 | printf(" openavx-tally\n"); |
| 134 | } |
| 135 | |
| 136 | static const char* get_libc_name(char* buf, size_t len) { |
| 137 | #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) |
| 138 | snprintf(buf, len, "Windows CRT"); |
| 139 | #elif defined(__APPLE__) && defined(__MACH__) |
| 140 | snprintf(buf, len, "macOS libSystem"); |
| 141 | #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) |
| 142 | snprintf(buf, len, "BSD libc"); |
| 143 | #elif defined(__GLIBC__) |
| 144 | snprintf(buf, len, "glibc %d.%d", __GLIBC__, __GLIBC_MINOR__); |
| 145 | #elif defined(__linux__) |
| 146 | snprintf(buf, len, "musl / non-glibc"); |
| 147 | #else |
| 148 | snprintf(buf, len, "POSIX libc"); |
| 149 | #endif |
| 150 | return buf; |
| 151 | } |
| 152 | |
| 153 | static void print_version_info(void) { |
| 154 | char libc_buf[48]; |
| 155 | bool has_opus = avx_audio_codec_has_opus(); |
| 156 | bool has_h264 = avx_video_decoder_has_backend(OAVX_FOURCC_VIDEO_TYPE_H264); |
| 157 | bool has_hevc = avx_video_decoder_has_backend(OAVX_FOURCC_VIDEO_TYPE_HEVC); |
| 158 | bool has_av1 = avx_video_decoder_has_backend(OAVX_FOURCC_VIDEO_TYPE_AV01); |
| 159 | |
| 160 | char hx_buf[64] = {0}; |
| 161 | if (!has_h264 && !has_hevc && !has_av1) { |
| 162 | snprintf(hx_buf, sizeof(hx_buf), "passthrough-only"); |
| 163 | } else { |
| 164 | snprintf(hx_buf, sizeof(hx_buf), "%s%s%s%s%s", |
| 165 | has_h264 ? "h264" : "", |
| 166 | (has_h264 && (has_hevc || has_av1)) ? "/" : "", |
| 167 | has_hevc ? "hevc" : "", |
| 168 | ((has_h264 || has_hevc) && has_av1) ? "/" : "", |
| 169 | has_av1 ? "av1" : ""); |
| 170 | } |
| 171 | |
| 172 | printf("OpenAVX CLI (%s)\n", oavx_version()); |
| 173 | printf("Platform: %s (%s, %s)\n", |
| 174 | AVX_PLATFORM_NAME, AVX_ARCH_NAME, get_libc_name(libc_buf, sizeof(libc_buf))); |
| 175 | printf("Architecture: %s\n", oavx_is_supported_cpu() ? "Validated" : "Generic"); |
| 176 | printf("SIMD Acceleration: %s\n", avx_simd_get_name()); |
| 177 | printf("Runtime Codecs: SpeedHQ/ADPCM (built-in), video_hx=%s, libopus=%s\n", |
| 178 | hx_buf, has_opus ? "loaded" : "absent"); |
| 179 | printf("Build: C99/C++11, POSIX/Win32/BSD\n"); |
| 180 | } |
| 181 | |
| 182 | int main(int argc, char* argv[]) { |
| 183 | if (argc < 1 || !argv[0]) { |
| 184 | print_main_usage("openavx"); |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | openavx_cli_init(); |
| 189 | |
| 190 | const char* base = get_prog_basename(argv[0]); |
| 191 | |
| 192 | /* 1. Direct Multi-Call Symlink Dispatch */ |
| 193 | if (prog_matches(base, "openavx-find") || prog_matches(base, "openavx_find")) { |
| 194 | return openavx_cmd_find(argc, argv); |
| 195 | } |
| 196 | if (prog_matches(base, "openavx-monitor") || prog_matches(base, "openavx_monitor")) { |
| 197 | return openavx_cmd_monitor(argc, argv); |
| 198 | } |
| 199 | if (prog_matches(base, "openavx-router") || prog_matches(base, "openavx_router") || prog_matches(base, "openavx-route")) { |
| 200 | return openavx_cmd_route(argc, argv); |
| 201 | } |
| 202 | if (prog_matches(base, "openavx-test-pattern") || prog_matches(base, "openavx_test_pattern") || prog_matches(base, "openavx-pattern")) { |
| 203 | return openavx_cmd_pattern(argc, argv); |
| 204 | } |
| 205 | if (prog_matches(base, "openavx-discovery-server") || prog_matches(base, "openavx_discovery_server") || prog_matches(base, "openavx-server")) { |
| 206 | return openavx_cmd_server(argc, argv); |
| 207 | } |
| 208 | if (prog_matches(base, "openavx-gateway") || prog_matches(base, "openavx_gateway") || |
| 209 | prog_matches(base, "openavx-web-gateway") || prog_matches(base, "openavx_web_gateway")) { |
| 210 | return openavx_cmd_gateway(argc, argv); |
| 211 | } |
| 212 | if (prog_matches(base, "openavx-virtcam") || prog_matches(base, "openavx_virtcam") || |
| 213 | prog_matches(base, "openavx-cam") || prog_matches(base, "openavx_cam") || |
| 214 | prog_matches(base, "openavx-v4l2sink") || prog_matches(base, "openavx_v4l2sink")) { |
| 215 | return openavx_cmd_virtcam(argc, argv); |
| 216 | } |
| 217 | if (prog_matches(base, "openavx-tally") || prog_matches(base, "openavx_tally")) { |
| 218 | return openavx_cmd_tally(argc, argv); |
| 219 | } |
| 220 | |
| 221 | /* 2. Top-level 'openavx' command invocation */ |
| 222 | if (argc < 2) { |
| 223 | print_main_usage(argv[0]); |
| 224 | return 1; |
| 225 | } |
| 226 | |
| 227 | const char* cmd = argv[1]; |
| 228 | |
| 229 | if (strcmp(cmd, "-h") == 0 || strcmp(cmd, "--help") == 0 || strcmp(cmd, "help") == 0) { |
| 230 | print_main_usage(argv[0]); |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | if (strcmp(cmd, "-v") == 0 || strcmp(cmd, "--version") == 0 || strcmp(cmd, "version") == 0) { |
| 235 | print_version_info(); |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | /* Subcommand dispatch: adjust argc and argv so argv[0] becomes the subcommand */ |
| 240 | int sub_argc = argc - 1; |
| 241 | char** sub_argv = argv + 1; |
| 242 | |
| 243 | if (strcmp(cmd, "find") == 0) { |
| 244 | return openavx_cmd_find(sub_argc, sub_argv); |
| 245 | } |
| 246 | if (strcmp(cmd, "monitor") == 0) { |
| 247 | return openavx_cmd_monitor(sub_argc, sub_argv); |
| 248 | } |
| 249 | if (strcmp(cmd, "route") == 0 || strcmp(cmd, "router") == 0) { |
| 250 | return openavx_cmd_route(sub_argc, sub_argv); |
| 251 | } |
| 252 | if (strcmp(cmd, "pattern") == 0 || strcmp(cmd, "test-pattern") == 0) { |
| 253 | return openavx_cmd_pattern(sub_argc, sub_argv); |
| 254 | } |
| 255 | if (strcmp(cmd, "server") == 0 || strcmp(cmd, "discovery-server") == 0) { |
| 256 | return openavx_cmd_server(sub_argc, sub_argv); |
| 257 | } |
| 258 | if (strcmp(cmd, "gateway") == 0 || strcmp(cmd, "web-gateway") == 0) { |
| 259 | return openavx_cmd_gateway(sub_argc, sub_argv); |
| 260 | } |
| 261 | if (strcmp(cmd, "virtcam") == 0 || strcmp(cmd, "cam") == 0 || strcmp(cmd, "v4l2sink") == 0 || strcmp(cmd, "virtual-camera") == 0) { |
| 262 | return openavx_cmd_virtcam(sub_argc, sub_argv); |
| 263 | } |
| 264 | if (strcmp(cmd, "tally") == 0) { |
| 265 | return openavx_cmd_tally(sub_argc, sub_argv); |
| 266 | } |
| 267 | |
| 268 | oavx_log_write(OAVX_LOG_ERROR, "cli", "Unknown command '%s'", cmd); |
| 269 | print_main_usage(argv[0]); |
| 270 | return 1; |
| 271 | } |