+ +
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_tally.c - Broadcast Tally monitor and control CLI utility |
| 7 | */ |
| 8 | |
| 9 | #define _GNU_SOURCE |
| 10 | #define _POSIX_C_SOURCE 200809L |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #ifndef _WIN32 |
| 16 | #include <strings.h> |
| 17 | #endif |
| 18 | #include <stdbool.h> |
| 19 | #include <stdint.h> |
| 20 | #include <signal.h> |
| 21 | #include <time.h> |
| 22 | #include <errno.h> |
| 23 | |
| 24 | #include "avx_platform.h" |
| 25 | #include "openavx.h" |
| 26 | #include "openavx_cli.h" |
| 27 | |
| 28 | #if defined(AVX_OS_POSIX) |
| 29 | #include <unistd.h> |
| 30 | #include <sys/socket.h> |
| 31 | #include <netinet/in.h> |
| 32 | #include <netinet/tcp.h> |
| 33 | #include <arpa/inet.h> |
| 34 | #include <netdb.h> |
| 35 | #elif defined(AVX_OS_WINDOWS) |
| 36 | #include <winsock2.h> |
| 37 | #include <ws2tcpip.h> |
| 38 | #include <io.h> |
| 39 | #define isatty _isatty |
| 40 | #define fileno _fileno |
| 41 | #endif |
| 42 | |
| 43 | static volatile sig_atomic_t g_stop = 0; |
| 44 | |
| 45 | static void handle_sigint(int sig) { |
| 46 | (void)sig; |
| 47 | g_stop = 1; |
| 48 | } |
| 49 | |
| 50 | static void print_usage(const char* prog) { |
| 51 | printf("OpenAVX Tally Light Monitor & Remote Controller\n"); |
| 52 | printf("Usage: %s [options] <stream-name | ip:port | http://url>\n\n", prog); |
| 53 | printf("Modes:\n"); |
| 54 | printf(" Monitor Mode (Default) Watch live tally state changes with real-time ANSI indicators\n"); |
| 55 | printf(" Control Mode (--set) Command remote camera tally lights (PGM, PVW, REC, OFF)\n\n"); |
| 56 | printf("Options:\n"); |
| 57 | printf(" -s, --set <flags> Set tally state: 'pgm', 'pvw', 'rec', 'off' (or combo 'pgm+rec')\n"); |
| 58 | printf(" -p, --poll Query tally state once, output result, and exit immediately\n"); |
| 59 | printf(" -j, --json Output machine-readable JSON format\n"); |
| 60 | printf(" -t, --text Output plain status token (PROGRAM, PREVIEW, RECORDING, OFF)\n"); |
| 61 | printf(" -w, --wait <ms> Timeout for initial response/connection (default: 3000 ms)\n"); |
| 62 | printf(" -d, --duration <sec> Hold tally connection for <sec> seconds (default: until Ctrl+C)\n"); |
| 63 | printf(" -1, --oneshot Command tally and disconnect immediately without holding\n"); |
| 64 | printf(" -q, --quiet Quiet mode (suppress headers and banners)\n"); |
| 65 | printf(" -h, --help Display this help message\n\n"); |
| 66 | printf("Examples:\n"); |
| 67 | printf(" %s \"STUDIO-CAM1\" # Monitor studio camera tally in real time\n", prog); |
| 68 | printf(" %s http://192.168.1.50:8080 # Monitor tally over HTTP SSE stream\n", prog); |
| 69 | printf(" %s \"CAM1\" --set pgm # Switch CAM1 to Program (ON AIR / Red Tally)\n", prog); |
| 70 | printf(" %s \"CAM1\" --set \"pvw+rec\" # Switch CAM1 to Preview cued with ISO Recording\n", prog); |
| 71 | printf(" %s \"CAM1\" --set off --oneshot # Clear tally on CAM1 and exit immediately\n", prog); |
| 72 | printf(" %s http://localhost:8080 --poll # Poll local tally via REST JSON\n", prog); |
| 73 | } |
| 74 | |
| 75 | static uint8_t parse_tally_tokens(const char* str) { |
| 76 | if (!str) return OAVX_TALLY_NONE; |
| 77 | if (strcasecmp(str, "off") == 0 || strcasecmp(str, "none") == 0 || strcasecmp(str, "clear") == 0) { |
| 78 | return OAVX_TALLY_NONE; |
| 79 | } |
| 80 | uint8_t flags = OAVX_TALLY_NONE; |
| 81 | if (strcasestr(str, "program") || strcasestr(str, "pgm") || strcasestr(str, "onair")) { |
| 82 | flags |= OAVX_TALLY_PROGRAM; |
| 83 | } |
| 84 | if (strcasestr(str, "preview") || strcasestr(str, "pvw") || strcasestr(str, "cued")) { |
| 85 | flags |= OAVX_TALLY_PREVIEW; |
| 86 | } |
| 87 | if (strcasestr(str, "recording") || strcasestr(str, "rec") || strcasestr(str, "iso")) { |
| 88 | flags |= OAVX_TALLY_RECORDING; |
| 89 | } |
| 90 | if (flags == OAVX_TALLY_NONE) { |
| 91 | int n = atoi(str); |
| 92 | if (n > 0) flags = (uint8_t)n; |
| 93 | } |
| 94 | return flags; |
| 95 | } |
| 96 | |
| 97 | static const char* tally_to_text(uint8_t flags) { |
| 98 | bool prog = (flags & OAVX_TALLY_PROGRAM) != 0; |
| 99 | bool prev = (flags & OAVX_TALLY_PREVIEW) != 0; |
| 100 | bool rec = (flags & OAVX_TALLY_RECORDING) != 0; |
| 101 | |
| 102 | if (prog && rec) return "PROGRAM+RECORDING"; |
| 103 | if (prog) return "PROGRAM"; |
| 104 | if (prev && rec) return "PREVIEW+RECORDING"; |
| 105 | if (prev) return "PREVIEW"; |
| 106 | if (rec) return "RECORDING"; |
| 107 | return "OFF"; |
| 108 | } |
| 109 | |
| 110 | static void print_tally_result(const char* target, uint8_t flags, bool json_out, bool text_out, bool use_ansi) { |
| 111 | bool prog = (flags & OAVX_TALLY_PROGRAM) != 0; |
| 112 | bool prev = (flags & OAVX_TALLY_PREVIEW) != 0; |
| 113 | bool rec = (flags & OAVX_TALLY_RECORDING) != 0; |
| 114 | |
| 115 | if (json_out) { |
| 116 | printf("{\"stream\":\"%s\",\"program\":%s,\"preview\":%s,\"recording\":%s,\"flags\":%u}\n", |
| 117 | target ? target : "", |
| 118 | prog ? "true" : "false", |
| 119 | prev ? "true" : "false", |
| 120 | rec ? "true" : "false", |
| 121 | flags); |
| 122 | fflush(stdout); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | if (text_out) { |
| 127 | printf("%s\n", tally_to_text(flags)); |
| 128 | fflush(stdout); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | time_t now = time(NULL); |
| 133 | struct tm tm_buf; |
| 134 | #if defined(_WIN32) |
| 135 | localtime_s(&tm_buf, &now); |
| 136 | #else |
| 137 | localtime_r(&now, &tm_buf); |
| 138 | #endif |
| 139 | |
| 140 | if (use_ansi) { |
| 141 | char badge[256]; |
| 142 | if (prog && rec) { |
| 143 | snprintf(badge, sizeof(badge), "\033[1;41;37m ● ON AIR \033[0m \033[1;43;30m ● REC \033[0m"); |
| 144 | } else if (prog) { |
| 145 | snprintf(badge, sizeof(badge), "\033[1;41;37m ● ON AIR (PROGRAM) \033[0m"); |
| 146 | } else if (prev && rec) { |
| 147 | snprintf(badge, sizeof(badge), "\033[1;42;30m ● CUED \033[0m \033[1;43;30m ● REC \033[0m"); |
| 148 | } else if (prev) { |
| 149 | snprintf(badge, sizeof(badge), "\033[1;42;30m ● CUED (PREVIEW) \033[0m"); |
| 150 | } else if (rec) { |
| 151 | snprintf(badge, sizeof(badge), "\033[1;43;30m ● RECORDING (ISO) \033[0m"); |
| 152 | } else { |
| 153 | snprintf(badge, sizeof(badge), "\033[90m ○ OFF AIR \033[0m"); |
| 154 | } |
| 155 | |
| 156 | fprintf(stdout, "\r\033[K[%02d:%02d:%02d] %s \033[1;37m%-24s\033[0m", |
| 157 | tm_buf.tm_hour, tm_buf.tm_min, tm_buf.tm_sec, badge, target ? target : ""); |
| 158 | fflush(stdout); |
| 159 | } else { |
| 160 | fprintf(stdout, "[%02d:%02d:%02d] [%s] %s\n", |
| 161 | tm_buf.tm_hour, tm_buf.tm_min, tm_buf.tm_sec, |
| 162 | tally_to_text(flags), target ? target : ""); |
| 163 | fflush(stdout); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* ========================================================================= */ |
| 168 | /* HTTP REST & SSE CLIENT ENGINE */ |
| 169 | /* ========================================================================= */ |
| 170 | |
| 171 | static bool parse_http_url(const char* url, char* host_out, size_t host_sz, uint16_t* port_out, char* path_out, size_t path_sz) { |
| 172 | if (!url || strncmp(url, "http://", 7) != 0) return false; |
| 173 | const char* p = url + 7; |
| 174 | const char* slash = strchr(p, '/'); |
| 175 | const char* colon = strchr(p, ':'); |
| 176 | |
| 177 | if (colon && (!slash || colon < slash)) { |
| 178 | size_t hlen = (size_t)(colon - p); |
| 179 | if (hlen >= host_sz) hlen = host_sz - 1; |
| 180 | strncpy(host_out, p, hlen); |
| 181 | host_out[hlen] = '\0'; |
| 182 | *port_out = (uint16_t)atoi(colon + 1); |
| 183 | } else if (slash) { |
| 184 | size_t hlen = (size_t)(slash - p); |
| 185 | if (hlen >= host_sz) hlen = host_sz - 1; |
| 186 | strncpy(host_out, p, hlen); |
| 187 | host_out[hlen] = '\0'; |
| 188 | *port_out = 80; |
| 189 | } else { |
| 190 | snprintf(host_out, host_sz, "%s", p); |
| 191 | *port_out = 80; |
| 192 | } |
| 193 | |
| 194 | if (slash) { |
| 195 | snprintf(path_out, path_sz, "%s", slash); |
| 196 | } else { |
| 197 | snprintf(path_out, path_sz, "/api/tally"); |
| 198 | } |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | static avx_socket_t http_connect(const char* host, uint16_t port, uint32_t timeout_ms) { |
| 203 | avx_platform_sockets_init(); |
| 204 | char port_str[16]; |
| 205 | snprintf(port_str, sizeof(port_str), "%u", port); |
| 206 | |
| 207 | struct addrinfo hints, *res = NULL; |
| 208 | memset(&hints, 0, sizeof(hints)); |
| 209 | hints.ai_family = AF_UNSPEC; |
| 210 | hints.ai_socktype = SOCK_STREAM; |
| 211 | |
| 212 | if (getaddrinfo(host, port_str, &hints, &res) != 0 || !res) { |
| 213 | return AVX_INVALID_SOCKET; |
| 214 | } |
| 215 | |
| 216 | avx_socket_t fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
| 217 | if (!avx_socket_is_valid(fd)) { |
| 218 | freeaddrinfo(res); |
| 219 | return AVX_INVALID_SOCKET; |
| 220 | } |
| 221 | |
| 222 | #if defined(AVX_OS_POSIX) |
| 223 | struct timeval tv; |
| 224 | tv.tv_sec = (time_t)(timeout_ms / 1000); |
| 225 | tv.tv_usec = (suseconds_t)((timeout_ms % 1000) * 1000); |
| 226 | setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const void*)&tv, sizeof(tv)); |
| 227 | setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (const void*)&tv, sizeof(tv)); |
| 228 | #endif |
| 229 | |
| 230 | if (connect(fd, res->ai_addr, (socklen_t)res->ai_addrlen) < 0) { |
| 231 | avx_socket_close(fd); |
| 232 | freeaddrinfo(res); |
| 233 | return AVX_INVALID_SOCKET; |
| 234 | } |
| 235 | |
| 236 | freeaddrinfo(res); |
| 237 | return fd; |
| 238 | } |
| 239 | |
| 240 | static int execute_http_tally( |
| 241 | const char* url, |
| 242 | bool is_set, |
| 243 | uint8_t set_flags, |
| 244 | bool poll_once, |
| 245 | bool json_out, |
| 246 | bool text_out, |
| 247 | uint32_t wait_ms, |
| 248 | bool quiet) |
| 249 | { |
| 250 | char host[128]; |
| 251 | char path[256]; |
| 252 | uint16_t port = 80; |
| 253 | |
| 254 | if (!parse_http_url(url, host, sizeof(host), &port, path, sizeof(path))) { |
| 255 | fprintf(stderr, "Error: Invalid HTTP URL '%s'\n", url); |
| 256 | return 1; |
| 257 | } |
| 258 | |
| 259 | bool is_tty = isatty(fileno(stdout)) != 0; |
| 260 | bool use_ansi = is_tty && !json_out && !text_out; |
| 261 | |
| 262 | if (!quiet && !json_out && !text_out) { |
| 263 | openavx_cli_header("tally"); |
| 264 | openavx_cli_kv("endpoint", "http://%s:%u%s", host, port, path); |
| 265 | openavx_cli_kv("transport", "HTTP REST / Server-Sent Events (SSE)"); |
| 266 | } |
| 267 | |
| 268 | /* 1. Control Mode: HTTP POST /api/tally */ |
| 269 | if (is_set) { |
| 270 | avx_socket_t fd = http_connect(host, port, wait_ms); |
| 271 | if (!avx_socket_is_valid(fd)) { |
| 272 | fprintf(stderr, "Error: Failed to connect to HTTP host %s:%u\n", host, port); |
| 273 | return 1; |
| 274 | } |
| 275 | |
| 276 | char post_path[256]; |
| 277 | if (strcmp(path, "/") == 0 || strcmp(path, "/camera") == 0) { |
| 278 | snprintf(post_path, sizeof(post_path), "/api/tally"); |
| 279 | } else { |
| 280 | snprintf(post_path, sizeof(post_path), "%s", path); |
| 281 | } |
| 282 | |
| 283 | char body[128]; |
| 284 | snprintf(body, sizeof(body), "{\"flags\":%u}\n", set_flags); |
| 285 | |
| 286 | char req[512]; |
| 287 | int req_len = snprintf(req, sizeof(req), |
| 288 | "POST %s HTTP/1.1\r\n" |
| 289 | "Host: %s:%u\r\n" |
| 290 | "User-Agent: openavx-tally/%s\r\n" |
| 291 | "Content-Type: application/json\r\n" |
| 292 | "Content-Length: %zu\r\n" |
| 293 | "Connection: close\r\n\r\n" |
| 294 | "%s", |
| 295 | post_path, host, port, oavx_version(), strlen(body), body); |
| 296 | |
| 297 | if (send(fd, req, req_len, 0) < 0) { |
| 298 | fprintf(stderr, "Error: Failed to send HTTP POST request\n"); |
| 299 | avx_socket_close(fd); |
| 300 | return 1; |
| 301 | } |
| 302 | |
| 303 | char resp[1024]; |
| 304 | ssize_t n = recv(fd, resp, sizeof(resp) - 1, 0); |
| 305 | avx_socket_close(fd); |
| 306 | |
| 307 | if (n <= 0) { |
| 308 | fprintf(stderr, "Error: No response from HTTP server\n"); |
| 309 | return 1; |
| 310 | } |
| 311 | resp[n] = '\0'; |
| 312 | |
| 313 | if (strstr(resp, "200 OK") != NULL) { |
| 314 | if (json_out) { |
| 315 | printf("{\"ok\":true,\"stream\":\"%s\",\"flags\":%u,\"status\":\"%s\"}\n", |
| 316 | url, set_flags, tally_to_text(set_flags)); |
| 317 | } else if (text_out) { |
| 318 | printf("%s\n", tally_to_text(set_flags)); |
| 319 | } else { |
| 320 | printf("\033[1;32m[OK]\033[0m Successfully set tally -> %s on %s\n", |
| 321 | tally_to_text(set_flags), url); |
| 322 | } |
| 323 | return 0; |
| 324 | } else { |
| 325 | fprintf(stderr, "Error: HTTP server returned error response\n"); |
| 326 | return 1; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /* 2. Poll Once Mode: HTTP GET /api/tally */ |
| 331 | if (poll_once) { |
| 332 | avx_socket_t fd = http_connect(host, port, wait_ms); |
| 333 | if (!avx_socket_is_valid(fd)) { |
| 334 | fprintf(stderr, "Error: Failed to connect to HTTP host %s:%u\n", host, port); |
| 335 | return 1; |
| 336 | } |
| 337 | |
| 338 | char get_path[256]; |
| 339 | if (strcmp(path, "/") == 0 || strcmp(path, "/camera") == 0) { |
| 340 | snprintf(get_path, sizeof(get_path), "/api/tally"); |
| 341 | } else { |
| 342 | snprintf(get_path, sizeof(get_path), "%s", path); |
| 343 | } |
| 344 | |
| 345 | char req[512]; |
| 346 | int req_len = snprintf(req, sizeof(req), |
| 347 | "GET %s HTTP/1.1\r\n" |
| 348 | "Host: %s:%u\r\n" |
| 349 | "User-Agent: openavx-tally/%s\r\n" |
| 350 | "Accept: application/json, text/plain\r\n" |
| 351 | "Connection: close\r\n\r\n", |
| 352 | get_path, host, port, oavx_version()); |
| 353 | |
| 354 | send(fd, req, req_len, 0); |
| 355 | |
| 356 | char resp[2048]; |
| 357 | ssize_t n = recv(fd, resp, sizeof(resp) - 1, 0); |
| 358 | avx_socket_close(fd); |
| 359 | |
| 360 | if (n <= 0) { |
| 361 | fprintf(stderr, "Error: Empty response from HTTP server\n"); |
| 362 | return 1; |
| 363 | } |
| 364 | resp[n] = '\0'; |
| 365 | |
| 366 | const char* body_start = strstr(resp, "\r\n\r\n"); |
| 367 | if (body_start) body_start += 4; |
| 368 | else body_start = resp; |
| 369 | |
| 370 | uint8_t flags = OAVX_TALLY_NONE; |
| 371 | const char* f_ptr = strstr(body_start, "\"flags\":"); |
| 372 | if (f_ptr) { |
| 373 | flags = (uint8_t)atoi(f_ptr + 8); |
| 374 | } else { |
| 375 | flags = parse_tally_tokens(body_start); |
| 376 | } |
| 377 | |
| 378 | print_tally_result(url, flags, json_out, text_out, false); |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | /* 3. Monitor Mode: HTTP SSE Streaming (GET /api/tally/sse) */ |
| 383 | char sse_path[256]; |
| 384 | if (strstr(path, "sse") || strstr(path, "events")) { |
| 385 | snprintf(sse_path, sizeof(sse_path), "%s", path); |
| 386 | } else { |
| 387 | snprintf(sse_path, sizeof(sse_path), "/api/tally/sse"); |
| 388 | } |
| 389 | |
| 390 | while (!g_stop) { |
| 391 | avx_socket_t fd = http_connect(host, port, wait_ms); |
| 392 | if (!avx_socket_is_valid(fd)) { |
| 393 | if (g_stop) break; |
| 394 | if (!quiet && !json_out && !text_out) { |
| 395 | fprintf(stderr, "\r\033[K[tally] Connecting to %s:%u... (retrying in 1s)", host, port); |
| 396 | fflush(stderr); |
| 397 | } |
| 398 | avx_sleep_ms(1000); |
| 399 | continue; |
| 400 | } |
| 401 | |
| 402 | char req[512]; |
| 403 | int req_len = snprintf(req, sizeof(req), |
| 404 | "GET %s HTTP/1.1\r\n" |
| 405 | "Host: %s:%u\r\n" |
| 406 | "User-Agent: openavx-tally/%s\r\n" |
| 407 | "Accept: text/event-stream\r\n" |
| 408 | "Cache-Control: no-cache\r\n" |
| 409 | "Connection: keep-alive\r\n\r\n", |
| 410 | sse_path, host, port, oavx_version()); |
| 411 | |
| 412 | send(fd, req, req_len, 0); |
| 413 | |
| 414 | char sse_buf[4096]; |
| 415 | size_t sse_len = 0; |
| 416 | |
| 417 | while (!g_stop) { |
| 418 | ssize_t n = recv(fd, sse_buf + sse_len, sizeof(sse_buf) - 1 - sse_len, 0); |
| 419 | if (n <= 0) break; |
| 420 | sse_len += (size_t)n; |
| 421 | sse_buf[sse_len] = '\0'; |
| 422 | |
| 423 | char* line = sse_buf; |
| 424 | char* next_line = NULL; |
| 425 | |
| 426 | while ((next_line = strstr(line, "\n")) != NULL) { |
| 427 | *next_line = '\0'; |
| 428 | char* cur = line; |
| 429 | line = next_line + 1; |
| 430 | |
| 431 | if (strncmp(cur, "data:", 5) == 0) { |
| 432 | char* json = cur + 5; |
| 433 | while (*json == ' ') json++; |
| 434 | const char* f_ptr = strstr(json, "\"flags\":"); |
| 435 | if (f_ptr) { |
| 436 | uint8_t flags = (uint8_t)atoi(f_ptr + 8); |
| 437 | print_tally_result(url, flags, json_out, text_out, use_ansi); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | size_t rem = strlen(line); |
| 443 | if (rem > 0 && line != sse_buf) { |
| 444 | memmove(sse_buf, line, rem); |
| 445 | } |
| 446 | sse_len = rem; |
| 447 | } |
| 448 | |
| 449 | avx_socket_close(fd); |
| 450 | if (!g_stop) { |
| 451 | avx_sleep_ms(1000); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | if (use_ansi) { |
| 456 | fputc('\n', stdout); |
| 457 | fflush(stdout); |
| 458 | } |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | /* ========================================================================= */ |
| 463 | /* OPENAVX NATIVE MEDIA STREAM TALLY ENGINE */ |
| 464 | /* ========================================================================= */ |
| 465 | |
| 466 | static int execute_native_tally( |
| 467 | const char* target, |
| 468 | bool is_set, |
| 469 | uint8_t set_flags, |
| 470 | bool poll_once, |
| 471 | bool json_out, |
| 472 | bool text_out, |
| 473 | uint32_t wait_ms, |
| 474 | uint32_t hold_sec, |
| 475 | bool oneshot, |
| 476 | bool quiet) |
| 477 | { |
| 478 | bool is_tty = isatty(fileno(stdout)) != 0; |
| 479 | bool use_ansi = is_tty && !json_out && !text_out; |
| 480 | |
| 481 | oavx_context_t* ctx = oavx_context_create(); |
| 482 | if (!ctx) { |
| 483 | fprintf(stderr, "Error: Failed to initialize OpenAVX context\n"); |
| 484 | return 1; |
| 485 | } |
| 486 | |
| 487 | oavx_stream_config_t cfg = { |
| 488 | .name = target ? target : "OPENAVX_TALLY_CLI", |
| 489 | .direction = OAVX_DIR_RECEIVE, |
| 490 | .bandwidth = OAVX_BANDWIDTH_METADATA_ONLY, |
| 491 | .color_format = OAVX_COLOR_FORMAT_RGBX_RGBA, |
| 492 | .clock_sync = false |
| 493 | }; |
| 494 | |
| 495 | /* Check if target looks like ip:port */ |
| 496 | if (target && strchr(target, ':') != NULL && !strchr(target, ' ')) { |
| 497 | cfg.endpoint = target; |
| 498 | } |
| 499 | |
| 500 | if (!quiet && !json_out && !text_out) { |
| 501 | openavx_cli_header("tally"); |
| 502 | openavx_cli_kv("source", "%s", target ? target : "(auto)"); |
| 503 | openavx_cli_kv("mode", "%s", is_set ? "Controller (Inject)" : (poll_once ? "Poll Once" : "Continuous Monitor")); |
| 504 | openavx_cli_kv("transport", "OpenAVX Native Reverse Metadata"); |
| 505 | } |
| 506 | |
| 507 | oavx_stream_t* stream = oavx_stream_open(ctx, &cfg); |
| 508 | if (!stream) { |
| 509 | fprintf(stderr, "Error: Failed to connect to stream '%s'\n", target ? target : ""); |
| 510 | oavx_context_destroy(ctx); |
| 511 | return 1; |
| 512 | } |
| 513 | |
| 514 | /* 1. Control Mode: Command tally state to remote sender */ |
| 515 | if (is_set) { |
| 516 | oavx_tally_t set_tally = { .flags = set_flags }; |
| 517 | bool ok = oavx_stream_set_tally(stream, &set_tally); |
| 518 | if (!ok) { |
| 519 | fprintf(stderr, "Error: Failed to command tally to stream '%s'\n", target ? target : ""); |
| 520 | oavx_stream_close(stream); |
| 521 | oavx_context_destroy(ctx); |
| 522 | return 1; |
| 523 | } |
| 524 | |
| 525 | if (json_out) { |
| 526 | printf("{\"ok\":true,\"stream\":\"%s\",\"flags\":%u,\"status\":\"%s\"}\n", |
| 527 | target ? target : "", set_flags, tally_to_text(set_flags)); |
| 528 | fflush(stdout); |
| 529 | } else if (text_out) { |
| 530 | printf("%s\n", tally_to_text(set_flags)); |
| 531 | fflush(stdout); |
| 532 | } else { |
| 533 | printf("\033[1;32m[OK]\033[0m Commanded tally %s to '%s'\n", |
| 534 | tally_to_text(set_flags), target ? target : ""); |
| 535 | } |
| 536 | |
| 537 | if (oneshot) { |
| 538 | avx_sleep_ms(150); |
| 539 | oavx_stream_close(stream); |
| 540 | oavx_context_destroy(ctx); |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | if (!quiet && !json_out && !text_out) { |
| 545 | if (hold_sec > 0) { |
| 546 | printf("[tally] Holding tally state for %u seconds...\n", hold_sec); |
| 547 | } else { |
| 548 | printf("[tally] Holding active tally connection... (Press Ctrl+C to release)\n"); |
| 549 | } |
| 550 | fflush(stdout); |
| 551 | } |
| 552 | |
| 553 | int64_t start_ms = avx_time_ns() / 1000000LL; |
| 554 | while (!g_stop) { |
| 555 | avx_sleep_ms(100); |
| 556 | if (hold_sec > 0) { |
| 557 | int64_t elapsed_ms = (avx_time_ns() / 1000000LL) - start_ms; |
| 558 | if (elapsed_ms >= (int64_t)(hold_sec * 1000)) break; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | oavx_stream_close(stream); |
| 563 | oavx_context_destroy(ctx); |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /* 2. Poll Once Mode */ |
| 568 | if (poll_once) { |
| 569 | oavx_tally_t tally = {0}; |
| 570 | bool got = oavx_stream_get_tally(stream, &tally, wait_ms); |
| 571 | if (!got) { |
| 572 | /* If no immediate tally packet, poll once more with fallback */ |
| 573 | tally.flags = OAVX_TALLY_NONE; |
| 574 | } |
| 575 | print_tally_result(target, tally.flags, json_out, text_out, false); |
| 576 | oavx_stream_close(stream); |
| 577 | oavx_context_destroy(ctx); |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | /* 3. Monitor Mode (Continuous) */ |
| 582 | uint8_t last_flags = 0xFF; |
| 583 | while (!g_stop) { |
| 584 | oavx_tally_t tally = {0}; |
| 585 | bool got = oavx_stream_get_tally(stream, &tally, 200); |
| 586 | if (got || last_flags == 0xFF) { |
| 587 | if (tally.flags != last_flags) { |
| 588 | print_tally_result(target, tally.flags, json_out, text_out, use_ansi); |
| 589 | last_flags = tally.flags; |
| 590 | } |
| 591 | } |
| 592 | avx_sleep_ms(50); |
| 593 | } |
| 594 | |
| 595 | if (use_ansi) { |
| 596 | fputc('\n', stdout); |
| 597 | fflush(stdout); |
| 598 | } |
| 599 | |
| 600 | oavx_stream_close(stream); |
| 601 | oavx_context_destroy(ctx); |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | /* ========================================================================= */ |
| 606 | /* MAIN CLI SUBCOMMAND ENTRY POINT */ |
| 607 | /* ========================================================================= */ |
| 608 | |
| 609 | int openavx_cmd_tally(int argc, char** argv) { |
| 610 | optind = 1; |
| 611 | signal(SIGINT, handle_sigint); |
| 612 | signal(SIGTERM, handle_sigint); |
| 613 | |
| 614 | const char* set_arg = NULL; |
| 615 | bool poll_once = false; |
| 616 | bool json_out = false; |
| 617 | bool text_out = false; |
| 618 | bool oneshot = false; |
| 619 | bool quiet = false; |
| 620 | uint32_t wait_ms = 3000; |
| 621 | uint32_t hold_sec = 0; |
| 622 | |
| 623 | static struct option long_opts[] = { |
| 624 | {"set", required_argument, 0, 's'}, |
| 625 | {"poll", no_argument, 0, 'p'}, |
| 626 | {"json", no_argument, 0, 'j'}, |
| 627 | {"text", no_argument, 0, 't'}, |
| 628 | {"wait", required_argument, 0, 'w'}, |
| 629 | {"duration", required_argument, 0, 'd'}, |
| 630 | {"oneshot", no_argument, 0, '1'}, |
| 631 | {"quiet", no_argument, 0, 'q'}, |
| 632 | {"help", no_argument, 0, 'h'}, |
| 633 | {0, 0, 0, 0} |
| 634 | }; |
| 635 | |
| 636 | int opt; |
| 637 | while ((opt = getopt_long(argc, argv, "s:pjtw:d:1qh", long_opts, NULL)) != -1) { |
| 638 | switch (opt) { |
| 639 | case 's': |
| 640 | set_arg = optarg; |
| 641 | break; |
| 642 | case 'p': |
| 643 | poll_once = true; |
| 644 | break; |
| 645 | case 'j': |
| 646 | json_out = true; |
| 647 | break; |
| 648 | case 't': |
| 649 | text_out = true; |
| 650 | break; |
| 651 | case 'w': |
| 652 | wait_ms = (uint32_t)atoi(optarg); |
| 653 | if (wait_ms == 0) wait_ms = 3000; |
| 654 | break; |
| 655 | case 'd': |
| 656 | hold_sec = (uint32_t)atoi(optarg); |
| 657 | break; |
| 658 | case '1': |
| 659 | oneshot = true; |
| 660 | break; |
| 661 | case 'q': |
| 662 | quiet = true; |
| 663 | break; |
| 664 | case 'h': |
| 665 | print_usage(argv[0]); |
| 666 | return 0; |
| 667 | default: |
| 668 | print_usage(argv[0]); |
| 669 | return 1; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | if (optind >= argc) { |
| 674 | fprintf(stderr, "Error: Missing target stream name or URL\n\n"); |
| 675 | print_usage(argv[0]); |
| 676 | return 1; |
| 677 | } |
| 678 | |
| 679 | const char* target = argv[optind]; |
| 680 | bool is_set = (set_arg != NULL); |
| 681 | uint8_t set_flags = is_set ? parse_tally_tokens(set_arg) : OAVX_TALLY_NONE; |
| 682 | |
| 683 | if (strncmp(target, "http://", 7) == 0 || strncmp(target, "https://", 8) == 0) { |
| 684 | return execute_http_tally(target, is_set, set_flags, poll_once, json_out, text_out, wait_ms, quiet); |
| 685 | } else { |
| 686 | return execute_native_tally(target, is_set, set_flags, poll_once, json_out, text_out, wait_ms, hold_sec, oneshot, quiet); |
| 687 | } |
| 688 | } |