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 * openavx_router_test.c - Automated Virtual Matrix Router Integration Verification
7 */
8  
9 #include "openavx.h"
10 #include "avx_routing.h"
11 #include "avx_internal.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdbool.h>
16 #include <assert.h>
17 #include <unistd.h>
18 #include <time.h>
19  
20 static inline void sleep_ms(int ms) {
21 struct timespec ts = {
22 .tv_sec = ms / 1000,
23 .tv_nsec = (long)(ms % 1000) * 1000000L
24 };
25 nanosleep(&ts, NULL);
26 }
27  
28 int main(void) {
29 printf("=== OpenAVX Virtual Matrix Router Tests ===\n\n");
30  
31 /* 1. Initialize openavx context */
32 printf("[*] 1. Initializing OpenAVX Context Engine...\n");
33 oavx_context_t *ctx = oavx_context_create();
34 assert(ctx != NULL);
35  
36 /* 2. Create Matrix Router */
37 printf("[*] 2. Creating Virtual Matrix Router destination 'MATRIX_PGM_ROUTER'...\n");
38 oavx_router_t *p_router = oavx_router_create(ctx, "MATRIX_PGM_ROUTER", "public");
39 assert(p_router != NULL);
40  
41 oavx_endpoint_t router_adv = oavx_router_get_source(p_router);
42 printf(" [+] Router online: %s @ %s\n", router_adv.name, router_adv.address);
43 assert(router_adv.address != NULL);
44  
45 /* 3. Create Upstream Camera Senders */
46 printf("[*] 3. Creating upstream feeds: 'CAM_ALPHA' and 'CAM_BETA'...\n");
47 oavx_stream_config_t cam1_set = { .name = "CAM_ALPHA", .groups = "public", .direction = OAVX_DIR_TRANSMIT, .clock_sync = true };
48 oavx_stream_config_t cam2_set = { .name = "CAM_BETA", .groups = "public", .direction = OAVX_DIR_TRANSMIT, .clock_sync = true };
49  
50 oavx_stream_t *p_cam1 = oavx_stream_open(ctx, &cam1_set);
51 oavx_stream_t *p_cam2 = oavx_stream_open(ctx, &cam2_set);
52 assert(p_cam1 && p_cam2);
53  
54 const oavx_endpoint_t *ep1 = oavx_stream_get_endpoint(p_cam1);
55 const oavx_endpoint_t *ep2 = oavx_stream_get_endpoint(p_cam2);
56  
57 oavx_endpoint_t src_cam1 = { .name = "CAM_ALPHA", .address = ep1 ? ep1->address : "127.0.0.1:7960" };
58 oavx_endpoint_t src_cam2 = { .name = "CAM_BETA", .address = ep2 ? ep2->address : "127.0.0.1:7961" };
59  
60 /* 4. Connect Receiver to Router */
61 printf("[*] 4. Connecting Receiver to Router 'MATRIX_PGM_ROUTER'...\n");
62 oavx_stream_config_t recv_settings = {
63 .name = "Matrix PGM Monitor",
64 .endpoint = router_adv.address,
65 .color_format = OAVX_COLOR_FORMAT_BEST,
66 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
67 .direction = OAVX_DIR_RECEIVE
68 };
69 oavx_stream_t *p_recv = oavx_stream_open(ctx, &recv_settings);
70 assert(p_recv != NULL);
71 sleep_ms(150);
72  
73 /* Allocate test frames */
74 const int w = 320, h = 180;
75 const size_t sz = (size_t)w * h * 2;
76 uint8_t* f1 = (uint8_t*)malloc(sz);
77 uint8_t* f2 = (uint8_t*)malloc(sz);
78 assert(f1 && f2);
79 memset(f1, 0x33, sz); /* CAM_ALPHA signature */
80 memset(f2, 0x77, sz); /* CAM_BETA signature */
81  
82 oavx_video_frame_t vf1 = {
83 .width = w, .height = h, .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY,
84 .scan_type = OAVX_SCAN_PROGRESSIVE,
85 .planes = { f1, NULL, NULL, NULL },
86 .strides = { (size_t)w * 2, 0, 0, 0 },
87 .framerate = { 30000, 1000 },
88 .aspect_ratio = { 16, 9 },
89 .pts_ns = 0, .dts_ns = 0,
90 .metadata = NULL, .metadata_len = 0,
91 .user_ctx = NULL
92 };
93 oavx_video_frame_t vf2 = vf1;
94 vf2.planes[0] = f2;
95  
96 /* 5. Route CAM_ALPHA and verify through router */
97 printf("[*] 5. Switching matrix route to CAM_ALPHA (0x33 signature)...\n");
98 bool route_ok = oavx_router_route(p_router, &src_cam1);
99 assert(route_ok);
100 for (int wp = 0; wp < 50 && oavx_stream_get_connection_count(p_cam1, 10) == 0; ++wp) sleep_ms(20);
101  
102 /* Pump frames into CAM_ALPHA */
103 for (int i = 0; i < 10; ++i) {
104 oavx_stream_send_video(p_cam1, &vf1);
105 sleep_ms(33);
106 }
107  
108 oavx_video_frame_t cap_v = {0};
109 oavx_audio_frame_t cap_a = {0};
110 oavx_metadata_frame_t cap_m = {0};
111  
112 bool got_cam1 = false;
113 for (int t = 0; t < 20 && !got_cam1; ++t) {
114 if (oavx_stream_receive(p_recv, &cap_v, &cap_a, &cap_m, 100) == OAVX_FRAME_TYPE_VIDEO) {
115 got_cam1 = true;
116 assert(cap_v.planes[0][0] == 0x33);
117 printf(" [+] Router output verified for CAM_ALPHA (0x33 matched)!\n");
118 oavx_stream_release_video(p_recv, &cap_v);
119 }
120 }
121 assert(got_cam1);
122  
123 /* 6. Dynamic Switch to CAM_BETA and verify seamless transition */
124 printf("[*] 6. Dynamically switching route to CAM_BETA (0x77 signature)...\n");
125 route_ok = oavx_router_route(p_router, &src_cam2);
126 assert(route_ok);
127 for (int wp = 0; wp < 50 && oavx_stream_get_connection_count(p_cam2, 10) == 0; ++wp) sleep_ms(20);
128  
129 /* Pump frames into CAM_BETA */
130 for (int i = 0; i < 10; ++i) {
131 oavx_stream_send_video(p_cam2, &vf2);
132 sleep_ms(33);
133 }
134  
135 bool got_cam2 = false;
136 for (int t = 0; t < 20 && !got_cam2; ++t) {
137 if (oavx_stream_receive(p_recv, &cap_v, &cap_a, &cap_m, 100) == OAVX_FRAME_TYPE_VIDEO) {
138 if (cap_v.planes[0][0] == 0x77) {
139 got_cam2 = true;
140 printf(" [+] Router dynamically switched to CAM_BETA (0x77 matched)!\n");
141 }
142 oavx_stream_release_video(p_recv, &cap_v);
143 }
144 }
145 assert(got_cam2);
146  
147 /* 7. Clear Route */
148 printf("[*] 7. Clearing route (idle state)...\n");
149 assert(oavx_router_clear(p_router));
150  
151 /* Teardown */
152 oavx_stream_close(p_recv);
153 oavx_router_destroy(p_router);
154 oavx_stream_close(p_cam1);
155 oavx_stream_close(p_cam2);
156 free(f1);
157 free(f2);
158 oavx_context_destroy(ctx);
159  
160 printf("\n[PASS] openavx_router_test\n");
161 return 0;
162 }