+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
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_find.c - Network stream discovery CLI |
| 7 | */ |
| 8 | |
| 9 | #define _GNU_SOURCE |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <stdbool.h> |
| 14 | #include <stdint.h> |
| 15 | #include "avx_platform.h" |
| 16 | |
| 17 | #include "openavx.h" |
| 18 | #include "openavx_cli.h" |
| 19 | |
| 20 | static void print_usage(const char* prog) { |
| 21 | printf("OpenAVX Network Source Finder\n"); |
| 22 | printf("Usage: %s [options]\n\n", prog); |
| 23 | printf("Options:\n"); |
| 24 | printf(" -w, --wait <ms> Discovery search duration in ms (default: 1500)\n"); |
| 25 | printf(" -g, --groups <names> Comma-separated list of groups to query\n"); |
| 26 | printf(" -e, --extra-ips <ips> Comma-separated list of extra unicast IPs to scan\n"); |
| 27 | printf(" -j, --json Output discovered sources as machine-readable JSON\n"); |
| 28 | printf(" -q, --quiet Only output found source URLs/names (suppress logs)\n"); |
| 29 | printf(" -h, --help Display this help message\n\n"); |
| 30 | printf("Examples:\n"); |
| 31 | printf(" %s # Scan LAN over zero-conf mDNS for 1.5s\n", prog); |
| 32 | printf(" %s --wait 3000 # Scan LAN with extended 3-second timeout\n", prog); |
| 33 | printf(" %s --extra-ips 10.0.0.50 # Scan specific remote unicast IP\n", prog); |
| 34 | printf(" %s --json # Output results formatted as JSON array\n", prog); |
| 35 | } |
| 36 | |
| 37 | int openavx_cmd_find(int argc, char** argv) { |
| 38 | optind = 1; |
| 39 | int timeout_ms = 1500; |
| 40 | const char* groups = NULL; |
| 41 | const char* extra_ips = NULL; |
| 42 | bool json_output = false; |
| 43 | bool quiet_output = false; |
| 44 | |
| 45 | static struct option long_opts[] = { |
| 46 | {"wait", required_argument, 0, 'w'}, |
| 47 | {"groups", required_argument, 0, 'g'}, |
| 48 | {"extra-ips", required_argument, 0, 'e'}, |
| 49 | {"json", no_argument, 0, 'j'}, |
| 50 | {"quiet", no_argument, 0, 'q'}, |
| 51 | {"help", no_argument, 0, 'h'}, |
| 52 | {0, 0, 0, 0} |
| 53 | }; |
| 54 | |
| 55 | int opt; |
| 56 | while ((opt = getopt_long(argc, argv, "w:g:e:jqh", long_opts, NULL)) != -1) { |
| 57 | switch (opt) { |
| 58 | case 'w': |
| 59 | timeout_ms = atoi(optarg); |
| 60 | if (timeout_ms <= 0) timeout_ms = 1500; |
| 61 | break; |
| 62 | case 'g': |
| 63 | groups = optarg; |
| 64 | break; |
| 65 | case 'e': |
| 66 | extra_ips = optarg; |
| 67 | break; |
| 68 | case 'j': |
| 69 | json_output = true; |
| 70 | break; |
| 71 | case 'q': |
| 72 | quiet_output = true; |
| 73 | break; |
| 74 | case 'h': |
| 75 | print_usage(argv[0]); |
| 76 | return 0; |
| 77 | default: |
| 78 | print_usage(argv[0]); |
| 79 | return 1; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | openavx_cli_set_quiet(quiet_output || json_output); |
| 84 | |
| 85 | if (extra_ips && extra_ips[0] != '\0') { |
| 86 | setenv("OPENAVX_DISCOVERY_SERVER", extra_ips, 1); |
| 87 | } |
| 88 | |
| 89 | oavx_context_t *ctx = oavx_context_create(); |
| 90 | if (!ctx) { |
| 91 | if (!quiet_output && !json_output) { |
| 92 | oavx_log_write(OAVX_LOG_ERROR, "find", "Failed to initialize OpenAVX context engine"); |
| 93 | } |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | oavx_discovery_t *p_find = oavx_discovery_create(ctx, groups); |
| 98 | if (!p_find) { |
| 99 | if (!quiet_output && !json_output) { |
| 100 | oavx_log_write(OAVX_LOG_ERROR, "find", "Failed to create discovery instance"); |
| 101 | } |
| 102 | oavx_context_destroy(ctx); |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | if (!quiet_output && !json_output) { |
| 107 | openavx_cli_header("find"); |
| 108 | openavx_cli_kv("groups", "%s", groups ? groups : "public"); |
| 109 | openavx_cli_kv("timeout", "%d ms", timeout_ms); |
| 110 | oavx_log_write(OAVX_LOG_INFO, "find", "Scanning network for active OpenAVX sources..."); |
| 111 | } |
| 112 | |
| 113 | /* Wait for mDNS multicast responses and discovery server queries */ |
| 114 | oavx_discovery_wait(p_find, (uint32_t)timeout_ms); |
| 115 | |
| 116 | oavx_endpoint_t sources[128]; |
| 117 | size_t num_sources = oavx_discovery_get_sources(p_find, sources, 128); |
| 118 | |
| 119 | if (json_output) { |
| 120 | printf("[\n"); |
| 121 | for (size_t i = 0; i < num_sources; ++i) { |
| 122 | const char* name = sources[i].name ? sources[i].name : ""; |
| 123 | const char* url = sources[i].address ? sources[i].address : ""; |
| 124 | printf(" {\"name\": \"%s\", \"url\": \"%s\"}%s\n", |
| 125 | name, url, (i + 1 < num_sources) ? "," : ""); |
| 126 | } |
| 127 | printf("]\n"); |
| 128 | } else if (quiet_output) { |
| 129 | for (size_t i = 0; i < num_sources; ++i) { |
| 130 | if (sources[i].name && sources[i].name[0]) { |
| 131 | printf("%s\n", sources[i].name); |
| 132 | } else if (sources[i].address) { |
| 133 | printf("%s\n", sources[i].address); |
| 134 | } |
| 135 | } |
| 136 | } else { |
| 137 | if (num_sources == 0) { |
| 138 | oavx_log_write(OAVX_LOG_INFO, "find", "No active OpenAVX sources found"); |
| 139 | } else { |
| 140 | printf("Found %zu active OpenAVX source%s:\n", num_sources, num_sources == 1 ? "" : "s"); |
| 141 | for (size_t i = 0; i < num_sources; ++i) { |
| 142 | printf(" [%zu] %-30s -> %s\n", |
| 143 | i, |
| 144 | sources[i].name ? sources[i].name : "(Unnamed)", |
| 145 | sources[i].address ? sources[i].address : "mDNS"); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | oavx_discovery_destroy(p_find); |
| 151 | oavx_context_destroy(ctx); |
| 152 | |
| 153 | return (num_sources > 0) ? 0 : 2; |
| 154 | } |