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 * src/avx_routing.c - Virtual matrix routing subsystem
7 */
8  
9 #define _GNU_SOURCE
10 #include "avx_platform.h"
11 #include "avx_internal.h"
12 #include "avx_routing.h"
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <time.h>
17  
18 /*
19 * Background routing worker pumping video, audio, and metadata frames from the routed
20 * source receiver to transmitter while forwarding downstream tally state upstream.
21 */
22 static void* avx_routing_worker(void* arg) {
23 avx_routing_ctx_t* slot = (avx_routing_ctx_t*)arg;
24  
25 while (1) {
26 avx_mutex_lock(&slot->lock);
27 while (!slot->stop_requested && !slot->has_source) {
28 avx_cond_wait(&slot->source_cond, &slot->lock);
29 }
30 if (slot->stop_requested) {
31 avx_mutex_unlock(&slot->lock);
32 break;
33 }
34  
35 avx_rx_instance_t rx = slot->p_recv;
36 avx_tx_instance_t tx = slot->p_send;
37 avx_mutex_unlock(&slot->lock);
38  
39 if (!rx || !tx) {
40 avx_sleep_ms(10);
41 continue;
42 }
43  
44 /* Forward any downstream tally changes upstream to active camera */
45 oavx_tally_t cur_tally;
46 if (avx_tx_get_tally(tx, &cur_tally, 0)) {
47 avx_mutex_lock(&slot->lock);
48 if (cur_tally.flags != slot->last_tally.flags) {
49 slot->last_tally = cur_tally;
50 avx_mutex_unlock(&slot->lock);
51 avx_rx_set_tally(rx, &cur_tally);
52 } else {
53 avx_mutex_unlock(&slot->lock);
54 }
55 }
56  
57 oavx_video_frame_t video;
58 oavx_audio_frame_t audio;
59 oavx_metadata_frame_t meta;
60  
61 oavx_frame_type_t ftype = avx_rx_receive(rx, &video, &audio, &meta, 15);
62  
63 avx_mutex_lock(&slot->lock);
64 bool still_valid = (!slot->stop_requested && slot->has_source && slot->p_recv == rx);
65 avx_mutex_unlock(&slot->lock);
66  
67 if (!still_valid) {
68 if (ftype == OAVX_FRAME_TYPE_VIDEO) avx_rx_free_video(rx, &video);
69 else if (ftype == OAVX_FRAME_TYPE_AUDIO) avx_rx_free_audio(rx, &audio);
70 else if (ftype == OAVX_FRAME_TYPE_METADATA) avx_rx_free_metadata(rx, &meta);
71 continue;
72 }
73  
74 if (ftype == OAVX_FRAME_TYPE_VIDEO) {
75 avx_tx_send_video(tx, &video);
76 avx_rx_free_video(rx, &video);
77 } else if (ftype == OAVX_FRAME_TYPE_AUDIO) {
78 avx_tx_send_audio(tx, &audio);
79 avx_rx_free_audio(rx, &audio);
80 } else if (ftype == OAVX_FRAME_TYPE_METADATA) {
81 avx_tx_send_metadata(tx, &meta);
82 avx_rx_free_metadata(rx, &meta);
83 }
84 }
85 return NULL;
86 }
87  
88 avx_router_instance_t avx_router_create(const avx_router_config_t* p_create_settings) {
89 avx_mutex_lock(&g_avx_state.global_lock);
90 if (!g_avx_state.initialized) {
91 avx_mutex_unlock(&g_avx_state.global_lock);
92 avx_engine_initialize();
93 avx_mutex_lock(&g_avx_state.global_lock);
94 }
95  
96 avx_routing_ctx_t* slot = NULL;
97 for (int i = 0; i < AVX_MAX_SESSIONS; ++i) {
98 if (!g_avx_state.routing_slots[i].in_use) {
99 slot = &g_avx_state.routing_slots[i];
100 memset(slot, 0, sizeof(*slot));
101 slot->in_use = true;
102 break;
103 }
104 }
105 avx_mutex_unlock(&g_avx_state.global_lock);
106  
107 if (!slot) return NULL;
108  
109 avx_mutex_init(&slot->lock);
110 avx_cond_init(&slot->source_cond);
111 slot->p_recv = NULL;
112 slot->has_source = false;
113 slot->stop_requested = false;
114 slot->worker_running = false;
115 slot->last_tally.flags = OAVX_TALLY_NONE;
116  
117 avx_tx_config_t default_settings = {
118 .name = "OpenAVX (Routing)",
119 .groups = NULL,
120 .clock_video = true,
121 .clock_audio = true
122 };
123 avx_tx_config_t send_settings;
124 memset(&send_settings, 0, sizeof(send_settings));
125 if (p_create_settings) {
126 send_settings.name = p_create_settings->name ? p_create_settings->name : default_settings.name;
127 send_settings.groups = p_create_settings->groups;
128 send_settings.clock_video = true;
129 send_settings.clock_audio = true;
130 } else {
131 send_settings = default_settings;
132 }
133 slot->p_send = avx_tx_create(&send_settings);
134 if (!slot->p_send) {
135 avx_mutex_destroy(&slot->lock);
136 avx_cond_destroy(&slot->source_cond);
137 avx_mutex_lock(&g_avx_state.global_lock);
138 slot->in_use = false;
139 avx_mutex_unlock(&g_avx_state.global_lock);
140 return NULL;
141 }
142  
143 slot->worker_running = true;
144 if (avx_thread_create(&slot->worker_thread, avx_routing_worker, slot) != 0) {
145 slot->worker_running = false;
146 avx_tx_destroy(slot->p_send);
147 slot->p_send = NULL;
148 avx_mutex_destroy(&slot->lock);
149 avx_cond_destroy(&slot->source_cond);
150 avx_mutex_lock(&g_avx_state.global_lock);
151 slot->in_use = false;
152 avx_mutex_unlock(&g_avx_state.global_lock);
153 return NULL;
154 }
155  
156 return (avx_router_instance_t)slot;
157 }
158  
159 void avx_router_destroy(avx_router_instance_t p_instance) {
160 if (!p_instance) return;
161 avx_routing_ctx_t* slot = (avx_routing_ctx_t*)p_instance;
162  
163 avx_mutex_lock(&slot->lock);
164 slot->stop_requested = true;
165 avx_cond_broadcast(&slot->source_cond);
166 avx_mutex_unlock(&slot->lock);
167  
168 if (slot->worker_running) {
169 avx_thread_join(slot->worker_thread);
170 slot->worker_running = false;
171 }
172  
173 avx_mutex_lock(&slot->lock);
174 avx_rx_instance_t rx = slot->p_recv;
175 slot->p_recv = NULL;
176 avx_tx_instance_t tx = slot->p_send;
177 slot->p_send = NULL;
178 avx_mutex_unlock(&slot->lock);
179  
180 if (rx) {
181 oavx_tally_t off_tally = { .flags = OAVX_TALLY_NONE };
182 avx_rx_set_tally(rx, &off_tally);
183 avx_rx_destroy(rx);
184 }
185 if (tx) {
186 avx_tx_destroy(tx);
187 }
188  
189 avx_mutex_destroy(&slot->lock);
190 avx_cond_destroy(&slot->source_cond);
191  
192 avx_mutex_lock(&g_avx_state.global_lock);
193 slot->in_use = false;
194 avx_mutex_unlock(&g_avx_state.global_lock);
195 }
196  
197 /* Seamlessly switches routed input source without disconnecting downstream transmitter subscribers. */
198 bool avx_router_route(avx_router_instance_t p_instance, const oavx_endpoint_t* p_source) {
199 if (!p_instance) return false;
200 avx_routing_ctx_t* slot = (avx_routing_ctx_t*)p_instance;
201  
202 if (!p_source || (!p_source->name && !p_source->address)) {
203 return avx_router_clear(p_instance);
204 }
205  
206 /* Check if already routed to this exact source */
207 avx_mutex_lock(&slot->lock);
208 if (slot->has_source) {
209 bool match = true;
210 if (p_source->name && strcmp(slot->current_name, p_source->name) != 0) match = false;
211 if (p_source->address && strcmp(slot->current_url, p_source->address) != 0) match = false;
212 if (match) {
213 avx_mutex_unlock(&slot->lock);
214 return true;
215 }
216 }
217 avx_mutex_unlock(&slot->lock);
218  
219 /* Create new receiver connecting to the new source */
220 avx_rx_config_t recv_create = {
221 .source_to_connect_to = *p_source,
222 .color_format = OAVX_COLOR_FORMAT_FASTEST,
223 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
224 .allow_video_fields = false,
225 .name = "avx_router_rx"
226 };
227 avx_rx_instance_t new_recv = avx_rx_create(&recv_create);
228 if (!new_recv) return false;
229  
230 /* Seamlessly swap receivers */
231 avx_mutex_lock(&slot->lock);
232 avx_rx_instance_t old_recv = slot->p_recv;
233 slot->p_recv = new_recv;
234 slot->has_source = true;
235 if (p_source->name) {
236 strncpy(slot->current_name, p_source->name, sizeof(slot->current_name) - 1);
237 slot->current_name[sizeof(slot->current_name) - 1] = '\0';
238 } else {
239 slot->current_name[0] = '\0';
240 }
241 if (p_source->address) {
242 strncpy(slot->current_url, p_source->address, sizeof(slot->current_url) - 1);
243 slot->current_url[sizeof(slot->current_url) - 1] = '\0';
244 } else {
245 slot->current_url[0] = '\0';
246 }
247 slot->current_source.name = slot->current_name;
248 slot->current_source.address = slot->current_url;
249 oavx_tally_t cur_tally = slot->last_tally;
250 avx_cond_broadcast(&slot->source_cond);
251 avx_mutex_unlock(&slot->lock);
252  
253 /* Disconnect old source cleanly */
254 if (old_recv) {
255 oavx_tally_t off_tally = { .flags = OAVX_TALLY_NONE };
256 avx_rx_set_tally(old_recv, &off_tally);
257 avx_rx_destroy(old_recv);
258 }
259  
260 /* Pass active downstream tally to the newly connected upstream camera */
261 if (cur_tally.flags & (OAVX_TALLY_PROGRAM | OAVX_TALLY_PREVIEW)) {
262 avx_rx_set_tally(new_recv, &cur_tally);
263 }
264  
265 return true;
266 }
267  
268 /* Clears routed source and transitions router to idle standby awaiting new input selection. */
269 bool avx_router_clear(avx_router_instance_t p_instance) {
270 if (!p_instance) return false;
271 avx_routing_ctx_t* slot = (avx_routing_ctx_t*)p_instance;
272  
273 avx_mutex_lock(&slot->lock);
274 avx_rx_instance_t old_recv = slot->p_recv;
275 slot->p_recv = NULL;
276 slot->has_source = false;
277 slot->current_name[0] = '\0';
278 slot->current_url[0] = '\0';
279 slot->current_source.name = NULL;
280 slot->current_source.address = NULL;
281 avx_mutex_unlock(&slot->lock);
282  
283 if (old_recv) {
284 oavx_tally_t off_tally = { .flags = OAVX_TALLY_NONE };
285 avx_rx_set_tally(old_recv, &off_tally);
286 avx_rx_destroy(old_recv);
287 }
288  
289 return true;
290 }
291  
292 oavx_endpoint_t avx_router_get_source(avx_router_instance_t p_instance) {
293 oavx_endpoint_t src = { .name = NULL, .address = NULL, .port = 0, .group = NULL };
294 if (!p_instance) return src;
295 avx_routing_ctx_t* slot = (avx_routing_ctx_t*)p_instance;
296  
297 avx_mutex_lock(&slot->lock);
298 if (slot->p_send) {
299 avx_send_ctx_t* send = (avx_send_ctx_t*)slot->p_send;
300 src.name = send->name;
301 static AVX_THREAD_LOCAL char url_buf[128];
302 snprintf(url_buf, sizeof(url_buf), "127.0.0.1:%u", send->port);
303 src.address = url_buf;
304 src.port = send->port;
305 src.group = send->groups;
306 }
307 avx_mutex_unlock(&slot->lock);
308 return src;
309 }