+ +
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_gateway.c - HTTP/fMP4 and RTSP gateway CLI |
| 7 | */ |
| 8 | |
| 9 | #define _GNU_SOURCE |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <signal.h> |
| 14 | #include <stdbool.h> |
| 15 | #include <stdint.h> |
| 16 | |
| 17 | #include "avx_platform.h" |
| 18 | #include "openavx.h" |
| 19 | #include "openavx_cli.h" |
| 20 | |
| 21 | static volatile bool g_running = true; |
| 22 | |
| 23 | static void sigint_handler(int sig) { |
| 24 | (void)sig; |
| 25 | g_running = false; |
| 26 | } |
| 27 | |
| 28 | static void print_usage(const char* prog) { |
| 29 | printf("OpenAVX Web & RTSP Gateway (Stream Ingress & Bridge)\n"); |
| 30 | printf("Usage: %s [options]\n\n", prog); |
| 31 | printf("Options:\n"); |
| 32 | printf(" -s, --source <SOURCE> Upstream source name or IP:port to ingest\n"); |
| 33 | printf(" -W, --web [PORT] Enable embedded HTTP/fMP4 Web preview (default: 8080)\n"); |
| 34 | printf(" -R, --rtsp [PORT] Enable native RTSP 1.0 streaming server (default: 8554)\n"); |
| 35 | printf(" -p, --path <PATH> RTSP mount point path (default: /live)\n"); |
| 36 | printf(" -n, --name <NAME> Name of gateway re-stream output (default: \"OpenAVX Gateway\")\n"); |
| 37 | printf(" -g, --groups <GROUPS> Broadcast / discovery groups (default: public)\n"); |
| 38 | printf(" -q, --quiet Suppress info banners and live progress logs\n"); |
| 39 | printf(" -h, --help Display this help and exit\n\n"); |
| 40 | printf("Examples:\n"); |
| 41 | printf(" %s # Auto-discover source & serve Web on 8080, RTSP on 8554\n", prog); |
| 42 | printf(" %s --source \"STUDIO_CAM_1\" # Ingest specific camera and bridge to Web & RTSP\n", prog); |
| 43 | printf(" %s --source 127.0.0.1:7960 --web 8088 # Direct connect to port 7960, serve Web on 8088\n", prog); |
| 44 | printf(" %s --rtsp 8554 --path /camera1 # Custom RTSP port and mount path\n", prog); |
| 45 | } |
| 46 | |
| 47 | int openavx_cmd_gateway(int argc, char** argv) { |
| 48 | optind = 1; |
| 49 | g_running = true; |
| 50 | |
| 51 | char source_arg[256] = ""; |
| 52 | char gateway_name[256] = "OpenAVX Gateway"; |
| 53 | char groups[256] = "public"; |
| 54 | char rtsp_path[128] = "/live"; |
| 55 | uint16_t web_port = 8080; |
| 56 | uint16_t rtsp_port = 8554; |
| 57 | bool enable_web = false; |
| 58 | bool enable_rtsp = false; |
| 59 | bool web_explicit = false; |
| 60 | bool rtsp_explicit = false; |
| 61 | bool quiet = false; |
| 62 | |
| 63 | static struct option long_opts[] = { |
| 64 | { "source", required_argument, 0, 's' }, |
| 65 | { "web", optional_argument, 0, 'W' }, |
| 66 | { "rtsp", optional_argument, 0, 'R' }, |
| 67 | { "path", required_argument, 0, 'p' }, |
| 68 | { "name", required_argument, 0, 'n' }, |
| 69 | { "groups", required_argument, 0, 'g' }, |
| 70 | { "quiet", no_argument, 0, 'q' }, |
| 71 | { "help", no_argument, 0, 'h' }, |
| 72 | { 0, 0, 0, 0 } |
| 73 | }; |
| 74 | |
| 75 | int opt; |
| 76 | while ((opt = getopt_long(argc, argv, "s:W::R::p:n:g:qh", long_opts, NULL)) != -1) { |
| 77 | switch (opt) { |
| 78 | case 's': |
| 79 | snprintf(source_arg, sizeof(source_arg), "%s", optarg); |
| 80 | break; |
| 81 | case 'W': |
| 82 | enable_web = true; |
| 83 | web_explicit = true; |
| 84 | if (!optarg && optind < argc && argv[optind][0] != '-') { |
| 85 | optarg = argv[optind++]; |
| 86 | } |
| 87 | if (optarg) { |
| 88 | int p = atoi(optarg); |
| 89 | if (p > 0 && p <= 65535) web_port = (uint16_t)p; |
| 90 | } |
| 91 | break; |
| 92 | case 'R': |
| 93 | enable_rtsp = true; |
| 94 | rtsp_explicit = true; |
| 95 | if (!optarg && optind < argc && argv[optind][0] != '-') { |
| 96 | optarg = argv[optind++]; |
| 97 | } |
| 98 | if (optarg) { |
| 99 | int p = atoi(optarg); |
| 100 | if (p > 0 && p <= 65535) rtsp_port = (uint16_t)p; |
| 101 | } |
| 102 | break; |
| 103 | case 'p': |
| 104 | snprintf(rtsp_path, sizeof(rtsp_path), "%s", optarg); |
| 105 | break; |
| 106 | case 'n': |
| 107 | snprintf(gateway_name, sizeof(gateway_name), "%s", optarg); |
| 108 | break; |
| 109 | case 'g': |
| 110 | snprintf(groups, sizeof(groups), "%s", optarg); |
| 111 | break; |
| 112 | case 'q': |
| 113 | quiet = true; |
| 114 | break; |
| 115 | case 'h': |
| 116 | print_usage(argv[0]); |
| 117 | return 0; |
| 118 | default: |
| 119 | print_usage(argv[0]); |
| 120 | return 1; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | openavx_cli_set_quiet(quiet); |
| 125 | |
| 126 | /* If neither --web nor --rtsp was explicitly passed, enable both by default */ |
| 127 | if (!web_explicit && !rtsp_explicit) { |
| 128 | enable_web = true; |
| 129 | enable_rtsp = true; |
| 130 | } |
| 131 | |
| 132 | signal(SIGINT, sigint_handler); |
| 133 | signal(SIGTERM, sigint_handler); |
| 134 | |
| 135 | oavx_context_t *ctx = oavx_context_create(); |
| 136 | if (!ctx) { |
| 137 | oavx_log_write(OAVX_LOG_ERROR, "gateway", "Failed to initialize OpenAVX context engine"); |
| 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | char target_name[256] = ""; |
| 142 | char target_url[256] = ""; |
| 143 | |
| 144 | if (source_arg[0] != '\0') { |
| 145 | if (strchr(source_arg, ':') && !strchr(source_arg, ' ')) { |
| 146 | snprintf(target_url, sizeof(target_url), "%s", source_arg); |
| 147 | } else { |
| 148 | snprintf(target_name, sizeof(target_name), "%s", source_arg); |
| 149 | } |
| 150 | } else { |
| 151 | oavx_log_write(OAVX_LOG_INFO, "gateway", "No upstream source specified; discovering active sources..."); |
| 152 | oavx_discovery_t *p_find = oavx_discovery_create(ctx, groups); |
| 153 | if (!p_find) { |
| 154 | oavx_log_write(OAVX_LOG_ERROR, "gateway", "Failed to initialize network discovery"); |
| 155 | oavx_context_destroy(ctx); |
| 156 | return 1; |
| 157 | } |
| 158 | oavx_discovery_wait(p_find, 1500); |
| 159 | oavx_endpoint_t sources[64]; |
| 160 | size_t count = oavx_discovery_get_sources(p_find, sources, 64); |
| 161 | if (count == 0) { |
| 162 | oavx_log_write(OAVX_LOG_ERROR, "gateway", |
| 163 | "No active OpenAVX sources found on groups '%s' (pass --source <NAME|IP:PORT>)", |
| 164 | groups); |
| 165 | oavx_discovery_destroy(p_find); |
| 166 | oavx_context_destroy(ctx); |
| 167 | return 1; |
| 168 | } |
| 169 | |
| 170 | oavx_log_write(OAVX_LOG_INFO, "gateway", "Found %zu active source(s):", count); |
| 171 | for (size_t i = 0; i < count; ++i) { |
| 172 | oavx_log_write(OAVX_LOG_INFO, "gateway", " [%zu] %s (%s)", i, |
| 173 | sources[i].name ? sources[i].name : "<unnamed>", |
| 174 | sources[i].address ? sources[i].address : "mDNS"); |
| 175 | } |
| 176 | if (sources[0].name) snprintf(target_name, sizeof(target_name), "%s", sources[0].name); |
| 177 | if (sources[0].address) snprintf(target_url, sizeof(target_url), "%s", sources[0].address); |
| 178 | oavx_log_write(OAVX_LOG_INFO, "gateway", "Auto-selecting source: %s (%s)", |
| 179 | target_name, target_url[0] ? target_url : "mDNS"); |
| 180 | oavx_discovery_destroy(p_find); |
| 181 | } |
| 182 | |
| 183 | /* Configure and open receiver for upstream stream */ |
| 184 | oavx_stream_config_t rx_cfg = { |
| 185 | .name = target_name[0] ? target_name : "OpenAVX Gateway Target", |
| 186 | .endpoint = target_url[0] ? target_url : NULL, |
| 187 | .direction = OAVX_DIR_RECEIVE, |
| 188 | .color_format = OAVX_COLOR_FORMAT_FASTEST, |
| 189 | .groups = groups |
| 190 | }; |
| 191 | |
| 192 | oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg); |
| 193 | if (!rx) { |
| 194 | oavx_log_write(OAVX_LOG_ERROR, "gateway", "Failed to connect receiver to upstream source"); |
| 195 | oavx_context_destroy(ctx); |
| 196 | return 1; |
| 197 | } |
| 198 | |
| 199 | /* Configure and open transmitter for gateway bridge output */ |
| 200 | oavx_stream_config_t tx_cfg = { |
| 201 | .name = gateway_name, |
| 202 | .groups = groups, |
| 203 | .direction = OAVX_DIR_TRANSMIT, |
| 204 | .clock_sync = false |
| 205 | }; |
| 206 | |
| 207 | oavx_stream_t *tx = oavx_stream_open(ctx, &tx_cfg); |
| 208 | if (!tx) { |
| 209 | oavx_log_write(OAVX_LOG_ERROR, "gateway", "Failed to open gateway transmitter '%s'", gateway_name); |
| 210 | oavx_stream_close(rx); |
| 211 | oavx_context_destroy(ctx); |
| 212 | return 1; |
| 213 | } |
| 214 | |
| 215 | /* Enable Web preview server if configured */ |
| 216 | char web_url[256] = ""; |
| 217 | if (enable_web) { |
| 218 | if (oavx_stream_enable_web_preview(tx, web_port) == OAVX_SUCCESS) { |
| 219 | web_port = oavx_stream_get_web_preview_port(tx); |
| 220 | oavx_stream_get_web_preview_url(tx, web_url, sizeof(web_url)); |
| 221 | } else { |
| 222 | oavx_log_write(OAVX_LOG_ERROR, "gateway", "Failed to activate Web Preview server on port %u", web_port); |
| 223 | enable_web = false; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /* Enable RTSP server if configured */ |
| 228 | if (enable_rtsp) { |
| 229 | if (oavx_stream_enable_rtsp_server(tx, rtsp_port, rtsp_path) == OAVX_SUCCESS) { |
| 230 | rtsp_port = oavx_stream_get_rtsp_port(tx); |
| 231 | } else { |
| 232 | oavx_log_write(OAVX_LOG_ERROR, "gateway", "Failed to activate RTSP server on port %u", rtsp_port); |
| 233 | enable_rtsp = false; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | openavx_cli_header("gateway"); |
| 238 | openavx_cli_kv("upstream", "%s%s%s", |
| 239 | target_name[0] ? target_name : target_url, |
| 240 | (target_name[0] && target_url[0]) ? " @ " : "", |
| 241 | (target_name[0] && target_url[0]) ? target_url : ""); |
| 242 | openavx_cli_kv("stream", "%s", gateway_name); |
| 243 | openavx_cli_kv("groups", "%s", groups); |
| 244 | if (enable_web) { |
| 245 | openavx_cli_kv("web", "%s", web_url); |
| 246 | } |
| 247 | if (enable_rtsp) { |
| 248 | openavx_cli_kv("rtsp", "rtsp://127.0.0.1:%u%s", rtsp_port, rtsp_path); |
| 249 | } |
| 250 | oavx_log_write(OAVX_LOG_INFO, "gateway", "Gateway running (Ctrl+C to stop)"); |
| 251 | |
| 252 | bool warned_codec = false; |
| 253 | uint64_t v_count = 0, a_count = 0; |
| 254 | int64_t last_stat_time = avx_time_ms(); |
| 255 | |
| 256 | while (g_running) { |
| 257 | oavx_video_frame_t vf; |
| 258 | oavx_audio_frame_t af; |
| 259 | oavx_metadata_frame_t mf; |
| 260 | |
| 261 | oavx_frame_type_t ft = oavx_stream_receive(rx, &vf, &af, &mf, 100); |
| 262 | if (ft == OAVX_FRAME_TYPE_VIDEO) { |
| 263 | if (!warned_codec && vf.fourcc != OAVX_FOURCC_VIDEO_TYPE_H264 && vf.fourcc != OAVX_FOURCC_VIDEO_TYPE_HEVC) { |
| 264 | openavx_cli_status_end(); |
| 265 | oavx_log_write(OAVX_LOG_WARN, "gateway", |
| 266 | "Upstream video is not H.264/HEVC; browser preview requires H.264/AVC (RTSP/OpenAVX still active)"); |
| 267 | warned_codec = true; |
| 268 | } |
| 269 | oavx_stream_send_video(tx, &vf); |
| 270 | oavx_stream_release_video(rx, &vf); |
| 271 | v_count++; |
| 272 | } else if (ft == OAVX_FRAME_TYPE_AUDIO) { |
| 273 | oavx_stream_send_audio(tx, &af); |
| 274 | oavx_stream_release_audio(rx, &af); |
| 275 | a_count++; |
| 276 | } else if (ft == OAVX_FRAME_TYPE_METADATA) { |
| 277 | oavx_stream_send_metadata(tx, &mf); |
| 278 | oavx_stream_release_metadata(rx, &mf); |
| 279 | } |
| 280 | |
| 281 | if (oavx_get_log_level() >= OAVX_LOG_INFO) { |
| 282 | int64_t now = avx_time_ms(); |
| 283 | if (now - last_stat_time >= 1000) { |
| 284 | int rtsp_clients = enable_rtsp ? oavx_stream_get_rtsp_client_count(tx) : 0; |
| 285 | int web_clients = enable_web ? oavx_stream_get_web_client_count(tx) : 0; |
| 286 | openavx_cli_status("gateway", "video=%lu audio=%lu web_clients=%d rtsp_clients=%d", |
| 287 | (unsigned long)v_count, (unsigned long)a_count, web_clients, rtsp_clients); |
| 288 | last_stat_time = now; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | openavx_cli_status_end(); |
| 294 | oavx_log_write(OAVX_LOG_INFO, "gateway", "Shutting down gateway..."); |
| 295 | if (enable_web) oavx_stream_disable_web_preview(tx); |
| 296 | if (enable_rtsp) oavx_stream_disable_rtsp_server(tx); |
| 297 | oavx_stream_close(tx); |
| 298 | oavx_stream_close(rx); |
| 299 | oavx_context_destroy(ctx); |
| 300 | oavx_log_write(OAVX_LOG_INFO, "gateway", "Gateway stopped cleanly"); |
| 301 | |
| 302 | return 0; |
| 303 | } |