Raw ↗
+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
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 * tools/openavx_router.c - Virtual matrix routing CLI
7 */
8  
9 #define _GNU_SOURCE
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdint.h>
14 #include <stdbool.h>
15 #include <signal.h>
16 #include <time.h>
17 #include "avx_platform.h"
18  
19 #include "openavx.h"
20 #include "avx_routing.h"
21 #include "openavx_cli.h"
22  
23 static volatile bool g_running = true;
24  
25 static void sigint_handler(int sig) {
26 (void)sig;
27 g_running = false;
28 }
29  
30 static inline bool check_stdin_ready(int timeout_ms) {
31 #if defined(AVX_OS_WINDOWS) || defined(_WIN32)
32 HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
33 if (h == NULL || h == INVALID_HANDLE_VALUE) return false;
34 DWORD res = WaitForSingleObject(h, (DWORD)timeout_ms);
35 return (res == WAIT_OBJECT_0);
36 #else
37 struct pollfd pfd = { .fd = STDIN_FILENO, .events = POLLIN, .revents = 0 };
38 int ret = poll(&pfd, 1, timeout_ms);
39 return (ret > 0 && (pfd.revents & POLLIN));
40 #endif
41 }
42  
43 static void print_usage(const char* prog) {
44 printf("OpenAVX Matrix Router (Virtual Matrix Routing Switcher)\n");
45 printf("Usage: %s [options]\n\n", prog);
46 printf("Options:\n");
47 printf(" -n, --name <NAME> Name of router output stream (default: \"OpenAVX Matrix Router\")\n");
48 printf(" -s, --source <SOURCE> Initial upstream source name or IP:port to route\n");
49 printf(" -g, --groups <GROUPS> Broadcast groups (default: public)\n");
50 printf(" -i, --interactive Run interactive routing console on stdin\n");
51 printf(" -l, --list List active network sources and exit\n");
52 printf(" -q, --quiet Suppress banner and informational logs\n");
53 printf(" -h, --help Display this help and exit\n\n");
54 printf("Interactive Commands:\n");
55 printf(" list Scan and list all active upstream OpenAVX sources\n");
56 printf(" switch <INDEX|NAME|URL> Switch router output to specified source\n");
57 printf(" clear Clear routing (output black/idle frame)\n");
58 printf(" status Display current router status and active route\n");
59 printf(" help Display command help\n");
60 printf(" quit / exit Shutdown router\n\n");
61 printf("Examples:\n");
62 printf(" %s --source \"Studio Camera 1\" # Route Camera 1 continuously\n", prog);
63 printf(" %s --interactive # Start interactive matrix switcher\n", prog);
64 }
65  
66 static void do_list_sources(oavx_discovery_t* p_find, oavx_endpoint_t* out_sources, size_t* out_count) {
67 if (!p_find) return;
68  
69 oavx_log_write(OAVX_LOG_INFO, "router", "Scanning network for active OpenAVX sources...");
70 oavx_discovery_wait(p_find, 1000);
71  
72 oavx_endpoint_t sources[64];
73 size_t count = oavx_discovery_get_sources(p_find, sources, 64);
74  
75 if (out_sources) memcpy(out_sources, sources, count * sizeof(oavx_endpoint_t));
76 if (out_count) *out_count = count;
77  
78 if (count == 0) {
79 oavx_log_write(OAVX_LOG_INFO, "router", "No OpenAVX sources discovered on current subnet/groups");
80 return;
81 }
82  
83 printf("----------------------------------------------------------------------\n");
84 printf(" # %-35s %s\n", "SOURCE NAME", "URL ADDRESS");
85 printf("----------------------------------------------------------------------\n");
86 for (size_t i = 0; i < count; ++i) {
87 printf(" [%2zu] %-35s %s\n", i + 1,
88 sources[i].name ? sources[i].name : "<unknown>",
89 sources[i].address ? sources[i].address : "<unknown>");
90 }
91 printf("----------------------------------------------------------------------\n");
92 printf("Found %zu source(s).\n\n", count);
93 }
94  
95 int openavx_cmd_route(int argc, char** argv) {
96 optind = 1;
97 g_running = true;
98 char router_name[256] = "OpenAVX Matrix Router";
99 char initial_source[256] = {0};
100 char groups[256] = "public";
101 bool interactive = false;
102 bool list_and_exit = false;
103 bool quiet = false;
104  
105 static struct option long_opts[] = {
106 { "name", required_argument, 0, 'n' },
107 { "source", required_argument, 0, 's' },
108 { "groups", required_argument, 0, 'g' },
109 { "interactive", no_argument, 0, 'i' },
110 { "list", no_argument, 0, 'l' },
111 { "quiet", no_argument, 0, 'q' },
112 { "help", no_argument, 0, 'h' },
113 { 0, 0, 0, 0 }
114 };
115  
116 int opt;
117 while ((opt = getopt_long(argc, argv, "n:s:g:ilqh", long_opts, NULL)) != -1) {
118 switch (opt) {
119 case 'n':
120 snprintf(router_name, sizeof(router_name), "%s", optarg);
121 break;
122 case 's':
123 snprintf(initial_source, sizeof(initial_source), "%s", optarg);
124 break;
125 case 'g':
126 snprintf(groups, sizeof(groups), "%s", optarg);
127 break;
128 case 'i':
129 interactive = true;
130 break;
131 case 'l':
132 list_and_exit = true;
133 break;
134 case 'q':
135 quiet = true;
136 break;
137 case 'h':
138 print_usage(argv[0]);
139 return 0;
140 default:
141 print_usage(argv[0]);
142 return 1;
143 }
144 }
145  
146 openavx_cli_set_quiet(quiet);
147  
148 signal(SIGINT, sigint_handler);
149 signal(SIGTERM, sigint_handler);
150  
151 oavx_context_t *ctx = oavx_context_create();
152 if (!ctx) {
153 oavx_log_write(OAVX_LOG_ERROR, "router", "Failed to initialize OpenAVX context engine");
154 return 1;
155 }
156  
157 /* Handle --list and exit mode */
158 if (list_and_exit) {
159 oavx_discovery_t *p_find = oavx_discovery_create(ctx, groups);
160 if (p_find) {
161 do_list_sources(p_find, NULL, NULL);
162 oavx_discovery_destroy(p_find);
163 }
164 oavx_context_destroy(ctx);
165 return 0;
166 }
167  
168 /* Create Matrix Router Output */
169 oavx_router_t *p_router = oavx_router_create(ctx, router_name, groups);
170 if (!p_router) {
171 oavx_log_write(OAVX_LOG_ERROR, "router", "Failed to create matrix router '%s'", router_name);
172 oavx_context_destroy(ctx);
173 return 1;
174 }
175  
176 oavx_endpoint_t router_adv = oavx_router_get_source(p_router);
177  
178 openavx_cli_header("route");
179 openavx_cli_kv("stream", "%s", router_name);
180 openavx_cli_kv("url", "%s", router_adv.address ? router_adv.address : "0.0.0.0");
181 openavx_cli_kv("groups", "%s", groups);
182 openavx_cli_kv("mode", "%s", interactive ? "interactive" : "headless");
183  
184 /* Create Finder for source resolution */
185 oavx_discovery_t *p_find = oavx_discovery_create(ctx, groups);
186  
187 char current_route_name[256] = "(none - idle/black)";
188 char current_route_url[256] = {0};
189  
190 /* Switch to initial source if requested */
191 if (initial_source[0] != '\0') {
192 oavx_endpoint_t src = { .name = initial_source, .address = NULL };
193 if (strchr(initial_source, ':') && !strchr(initial_source, ' ')) {
194 src.address = initial_source;
195 }
196 if (oavx_router_route(p_router, &src)) {
197 snprintf(current_route_name, sizeof(current_route_name), "%s", initial_source);
198 oavx_log_write(OAVX_LOG_INFO, "router", "Initial route switched to '%s'", initial_source);
199 } else {
200 oavx_log_write(OAVX_LOG_WARN, "router", "Failed to route initial source '%s'", initial_source);
201 }
202 }
203  
204 if (interactive) {
205 printf("Type 'help' for commands, 'list' to view sources, 'quit' to exit.\n");
206 printf("router> ");
207 fflush(stdout);
208 }
209  
210 char line_buf[512];
211 while (g_running) {
212 if (check_stdin_ready(500)) {
213 if (!fgets(line_buf, sizeof(line_buf), stdin)) {
214 /* EOF on stdin (pipe or terminal closed) */
215 if (interactive) break;
216 /* If headless and stdin closed, keep running daemon until signal */
217 avx_sleep_ms(1000);
218 continue;
219 }
220  
221 /* Strip trailing newline */
222 line_buf[strcspn(line_buf, "\r\n")] = '\0';
223 char* cmd = line_buf;
224 while (*cmd == ' ' || *cmd == '\t') cmd++;
225 if (*cmd == '\0') {
226 if (interactive) { printf("router> "); fflush(stdout); }
227 continue;
228 }
229  
230 if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
231 printf("Commands:\n");
232 printf(" list List available network OpenAVX sources\n");
233 printf(" switch <INDEX | NAME | URL> Switch router active input\n");
234 printf(" clear Clear route (output idle black)\n");
235 printf(" status Show current route and router state\n");
236 printf(" quit / exit Shutdown router\n");
237 } else if (strcmp(cmd, "list") == 0 || strcmp(cmd, "ls") == 0) {
238 do_list_sources(p_find, NULL, NULL);
239 } else if (strncmp(cmd, "switch", 6) == 0) {
240 char* target = cmd + 6;
241 while (*target == ' ' || *target == '\t') target++;
242 if (*target == '\0') {
243 printf("Usage: switch <INDEX | NAME | URL>\n");
244 } else {
245 /* Check if target is a numeric index from list */
246 char* endptr = NULL;
247 long idx = strtol(target, &endptr, 10);
248 bool switched = false;
249  
250 if (endptr && *endptr == '\0' && idx > 0 && p_find) {
251 oavx_endpoint_t p_sources[64];
252 size_t count = oavx_discovery_get_sources(p_find, p_sources, 64);
253 if (idx <= (long)count) {
254 const oavx_endpoint_t* sel = &p_sources[idx - 1];
255 switched = oavx_router_route(p_router, sel);
256 if (switched) {
257 snprintf(current_route_name, sizeof(current_route_name), "%s", sel->name ? sel->name : "<unknown>");
258 if (sel->address) snprintf(current_route_url, sizeof(current_route_url), "%s", sel->address);
259 oavx_log_write(OAVX_LOG_INFO, "router", "Routed [%ld] %s (%s)", idx, current_route_name, current_route_url);
260 }
261 } else {
262 oavx_log_write(OAVX_LOG_ERROR, "router", "Source index %ld out of range (1..%zu); type 'list'", idx, count);
263 }
264 }
265  
266 if (!switched && (endptr == NULL || *endptr != '\0')) {
267 /* Match by name or URL */
268 oavx_endpoint_t src = { .name = target, .address = NULL };
269 if (strchr(target, ':') && !strchr(target, ' ')) {
270 src.address = target;
271 }
272 if (oavx_router_route(p_router, &src)) {
273 snprintf(current_route_name, sizeof(current_route_name), "%s", target);
274 oavx_log_write(OAVX_LOG_INFO, "router", "Routed '%s'", target);
275 } else {
276 oavx_log_write(OAVX_LOG_ERROR, "router", "Failed to switch to source '%s'", target);
277 }
278 }
279 }
280 } else if (strcmp(cmd, "clear") == 0) {
281 oavx_router_clear(p_router);
282 strcpy(current_route_name, "(none - idle/black)");
283 current_route_url[0] = '\0';
284 oavx_log_write(OAVX_LOG_INFO, "router", "Route cleared (idle black output)");
285 } else if (strcmp(cmd, "status") == 0) {
286 printf("Router Status:\n");
287 printf(" Output Stream : %s\n", router_name);
288 printf(" Active Route : %s\n", current_route_name);
289 if (current_route_url[0]) {
290 printf(" Source URL : %s\n", current_route_url);
291 }
292 } else if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "exit") == 0) {
293 oavx_log_write(OAVX_LOG_INFO, "router", "Exiting router...");
294 g_running = false;
295 break;
296 } else {
297 oavx_log_write(OAVX_LOG_WARN, "router", "Unknown command '%s' (type 'help' for options)", cmd);
298 }
299  
300 if (interactive && g_running) {
301 printf("router> ");
302 fflush(stdout);
303 }
304 }
305 }
306  
307 oavx_log_write(OAVX_LOG_INFO, "router", "Stopping matrix router...");
308 if (p_find) oavx_discovery_destroy(p_find);
309 oavx_router_destroy(p_router);
310 oavx_context_destroy(ctx);
311  
312 oavx_log_write(OAVX_LOG_INFO, "router", "Matrix router stopped cleanly");
313 return 0;
314 }