+ +
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 | * test/openavx_routing_test.c - Automated Virtual Matrix Router Verification |
| 7 | */ |
| 8 | |
| 9 | #define _GNU_SOURCE |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <assert.h> |
| 14 | #include <unistd.h> |
| 15 | #include <time.h> |
| 16 | |
| 17 | #include "openavx.h" |
| 18 | |
| 19 | int main(void) { |
| 20 | printf("\n=======================================================\n"); |
| 21 | printf(" OpenAVX Virtual Matrix Router Subsystem Test\n"); |
| 22 | printf("=======================================================\n\n"); |
| 23 | |
| 24 | /* 1. Initialize openavx */ |
| 25 | printf("[*] 1. Initializing OpenAVX...\n"); |
| 26 | oavx_context_t *ctx = oavx_context_create(); |
| 27 | assert(ctx != NULL); |
| 28 | |
| 29 | /* 2. Create Matrix Router destination */ |
| 30 | printf("[*] 2. Spawning Matrix Router destination \"STUDIO_PGM_ROUTER\"...\n"); |
| 31 | oavx_router_t *p_router = oavx_router_create(ctx, "STUDIO_PGM_ROUTER", "public"); |
| 32 | assert(p_router != NULL); |
| 33 | |
| 34 | oavx_endpoint_t router_source = oavx_router_get_source(p_router); |
| 35 | printf(" [+] Router listening on URL: %s (Source: %s)\n", |
| 36 | router_source.address, router_source.name); |
| 37 | assert(router_source.address != NULL); |
| 38 | |
| 39 | /* 3. Create two independent camera senders */ |
| 40 | printf("[*] 3. Creating upstream sources: Camera 1 and Camera 2...\n"); |
| 41 | oavx_stream_config_t cam1_settings = { |
| 42 | .name = "STUDIO_CAM_1", |
| 43 | .groups = "public", |
| 44 | .direction = OAVX_DIR_TRANSMIT, |
| 45 | .clock_sync = true |
| 46 | }; |
| 47 | oavx_stream_t *p_cam1 = oavx_stream_open(ctx, &cam1_settings); |
| 48 | assert(p_cam1 != NULL); |
| 49 | |
| 50 | oavx_stream_config_t cam2_settings = { |
| 51 | .name = "STUDIO_CAM_2", |
| 52 | .groups = "public", |
| 53 | .direction = OAVX_DIR_TRANSMIT, |
| 54 | .clock_sync = true |
| 55 | }; |
| 56 | oavx_stream_t *p_cam2 = oavx_stream_open(ctx, &cam2_settings); |
| 57 | assert(p_cam2 != NULL); |
| 58 | |
| 59 | const oavx_endpoint_t *src_cam1 = oavx_stream_get_endpoint(p_cam1); |
| 60 | const oavx_endpoint_t *src_cam2 = oavx_stream_get_endpoint(p_cam2); |
| 61 | assert(src_cam1 != NULL && src_cam2 != NULL); |
| 62 | |
| 63 | printf(" [+] Camera 1 on %s\n", src_cam1->address); |
| 64 | printf(" [+] Camera 2 on %s\n", src_cam2->address); |
| 65 | |
| 66 | /* Allocate video frame buffers */ |
| 67 | int width = 320, height = 180; |
| 68 | size_t frame_bytes = (size_t)width * height * 2; /* UYVY */ |
| 69 | uint8_t *cam1_frame_data = (uint8_t*)malloc(frame_bytes); |
| 70 | uint8_t *cam2_frame_data = (uint8_t*)malloc(frame_bytes); |
| 71 | assert(cam1_frame_data && cam2_frame_data); |
| 72 | oavx_frame_type_e ftype = OAVX_FRAME_TYPE_NONE; |
| 73 | |
| 74 | memset(cam1_frame_data, 0x11, frame_bytes); |
| 75 | memset(cam2_frame_data, 0x22, frame_bytes); |
| 76 | |
| 77 | oavx_video_frame_t vf_cam1 = { |
| 78 | .width = width, |
| 79 | .height = height, |
| 80 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY, |
| 81 | .scan_type = OAVX_SCAN_PROGRESSIVE, |
| 82 | .planes = { cam1_frame_data, NULL, NULL, NULL }, |
| 83 | .strides = { (size_t)width * 2, 0, 0, 0 }, |
| 84 | .framerate = { 60, 1 }, |
| 85 | .aspect_ratio = { 16, 9 }, |
| 86 | .pts_ns = 1001, |
| 87 | .dts_ns = 0, |
| 88 | .metadata = NULL, |
| 89 | .metadata_len = 0, |
| 90 | .user_ctx = NULL |
| 91 | }; |
| 92 | |
| 93 | oavx_video_frame_t vf_cam2 = { |
| 94 | .width = width, |
| 95 | .height = height, |
| 96 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY, |
| 97 | .scan_type = OAVX_SCAN_PROGRESSIVE, |
| 98 | .planes = { cam2_frame_data, NULL, NULL, NULL }, |
| 99 | .strides = { (size_t)width * 2, 0, 0, 0 }, |
| 100 | .framerate = { 60, 1 }, |
| 101 | .aspect_ratio = { 16, 9 }, |
| 102 | .pts_ns = 2002, |
| 103 | .dts_ns = 0, |
| 104 | .metadata = NULL, |
| 105 | .metadata_len = 0, |
| 106 | .user_ctx = NULL |
| 107 | }; |
| 108 | |
| 109 | /* 4. Connect downstream receiver to the Matrix Router */ |
| 110 | printf("[*] 4. Connecting downstream receiver to Matrix Router (\"%s\")...\n", router_source.address); |
| 111 | oavx_stream_config_t recv_create = { |
| 112 | .endpoint = router_source.address, |
| 113 | .name = "Master_Control_Monitor", |
| 114 | .direction = OAVX_DIR_RECEIVE, |
| 115 | .color_format = OAVX_COLOR_FORMAT_FASTEST, |
| 116 | .bandwidth = OAVX_BANDWIDTH_HIGHEST, |
| 117 | .clock_sync = false |
| 118 | }; |
| 119 | oavx_stream_t *p_downstream_recv = oavx_stream_open(ctx, &recv_create); |
| 120 | assert(p_downstream_recv != NULL); |
| 121 | usleep(50000); /* 50ms for TCP handshake */ |
| 122 | |
| 123 | /* 5. Route Camera 1 through Matrix Router */ |
| 124 | printf("[*] 5. Routing Camera 1 (\"%s\") to Router output...\n", src_cam1->name); |
| 125 | bool route_ok = oavx_router_route(p_router, src_cam1); |
| 126 | assert(route_ok); |
| 127 | for (int w = 0; w < 50 && oavx_stream_get_connection_count(p_cam1, 10) == 0; ++w) usleep(20000); |
| 128 | |
| 129 | /* Capture forwarded frame on downstream receiver */ |
| 130 | oavx_video_frame_t rx_frame; |
| 131 | bool got_routed_cam1 = false; |
| 132 | for (int retry = 0; retry < 30 && !got_routed_cam1; ++retry) { |
| 133 | oavx_stream_send_video(p_cam1, &vf_cam1); |
| 134 | oavx_frame_type_e ftype_c = oavx_stream_receive(p_downstream_recv, &rx_frame, NULL, NULL, 100); |
| 135 | if (ftype_c == OAVX_FRAME_TYPE_VIDEO) { |
| 136 | got_routed_cam1 = true; |
| 137 | assert(rx_frame.pts_ns == 1001); |
| 138 | assert(rx_frame.planes[0] != NULL); |
| 139 | assert(rx_frame.planes[0][0] == 0x11); |
| 140 | printf(" [+] Downstream receiver captured Camera 1 feed! (PTS: %ld, Signature: 0x%02X)\n", |
| 141 | (long)rx_frame.pts_ns, rx_frame.planes[0][0]); |
| 142 | oavx_stream_release_video(p_downstream_recv, &rx_frame); |
| 143 | } |
| 144 | } |
| 145 | assert(got_routed_cam1); |
| 146 | |
| 147 | /* 6. Verify Bidirectional Tally Migration */ |
| 148 | printf("[*] 6. Testing Bidirectional Tally synchronization through Router...\n"); |
| 149 | oavx_tally_t tally_pgm = { .flags = OAVX_TALLY_PROGRAM }; |
| 150 | oavx_stream_set_tally(p_downstream_recv, &tally_pgm); |
| 151 | |
| 152 | oavx_tally_t cam1_tally = {0}, cam2_tally = {0}; |
| 153 | for (int t = 0; t < 50 && (!(cam1_tally.flags & OAVX_TALLY_PROGRAM) || (cam2_tally.flags & OAVX_TALLY_PROGRAM)); ++t) { |
| 154 | usleep(20000); |
| 155 | oavx_stream_get_tally(p_cam1, &cam1_tally, 50); |
| 156 | oavx_stream_get_tally(p_cam2, &cam2_tally, 50); |
| 157 | } |
| 158 | |
| 159 | printf(" [+] Camera 1 Tally: flags=0x%x (Expected: 0x1)\n", cam1_tally.flags); |
| 160 | printf(" [+] Camera 2 Tally: flags=0x%x (Expected: 0x0)\n", cam2_tally.flags); |
| 161 | assert((cam1_tally.flags & OAVX_TALLY_PROGRAM) != 0); |
| 162 | assert((cam2_tally.flags & OAVX_TALLY_PROGRAM) == 0); |
| 163 | |
| 164 | /* 7. Seamless Matrix Switch to Camera 2 */ |
| 165 | printf("[*] 7. Matrix switching Router from Camera 1 to Camera 2...\n"); |
| 166 | route_ok = oavx_router_route(p_router, src_cam2); |
| 167 | assert(route_ok); |
| 168 | for (int w = 0; w < 50 && oavx_stream_get_connection_count(p_cam2, 10) == 0; ++w) usleep(20000); |
| 169 | |
| 170 | /* Verify Tally automatically migrated from Camera 1 to Camera 2! */ |
| 171 | for (int t = 0; t < 50 && ((cam1_tally.flags & OAVX_TALLY_PROGRAM) || !(cam2_tally.flags & OAVX_TALLY_PROGRAM)); ++t) { |
| 172 | usleep(20000); |
| 173 | oavx_stream_get_tally(p_cam1, &cam1_tally, 50); |
| 174 | oavx_stream_get_tally(p_cam2, &cam2_tally, 50); |
| 175 | } |
| 176 | |
| 177 | printf(" [+] Tally post-switch: Cam 1 flags=0x%x, Cam 2 flags=0x%x\n", |
| 178 | cam1_tally.flags, cam2_tally.flags); |
| 179 | assert((cam1_tally.flags & OAVX_TALLY_PROGRAM) == 0); /* Camera 1 tally turned off! */ |
| 180 | assert((cam2_tally.flags & OAVX_TALLY_PROGRAM) != 0); /* Camera 2 tally turned on! */ |
| 181 | |
| 182 | /* Downstream receiver captures frame WITHOUT reconnecting */ |
| 183 | bool got_routed_cam2 = false; |
| 184 | for (int retry = 0; retry < 30 && !got_routed_cam2; ++retry) { |
| 185 | oavx_stream_send_video(p_cam2, &vf_cam2); |
| 186 | oavx_frame_type_e ftype_c = oavx_stream_receive(p_downstream_recv, &rx_frame, NULL, NULL, 100); |
| 187 | if (ftype_c == OAVX_FRAME_TYPE_VIDEO) { |
| 188 | got_routed_cam2 = true; |
| 189 | assert(rx_frame.pts_ns == 2002); |
| 190 | assert(rx_frame.planes[0][0] == 0x22); |
| 191 | printf(" [+] Downstream receiver captured Camera 2 feed seamlessly! (PTS: %ld, Signature: 0x%02X)\n", |
| 192 | (long)rx_frame.pts_ns, rx_frame.planes[0][0]); |
| 193 | oavx_stream_release_video(p_downstream_recv, &rx_frame); |
| 194 | } |
| 195 | } |
| 196 | assert(got_routed_cam2); |
| 197 | |
| 198 | /* 8. Clear Routing */ |
| 199 | printf("[*] 8. Clearing Router output via oavx_router_clear...\n"); |
| 200 | bool clear_ok = oavx_router_clear(p_router); |
| 201 | assert(clear_ok); |
| 202 | usleep(50000); |
| 203 | |
| 204 | /* Camera 2 tally should now be off */ |
| 205 | oavx_stream_get_tally(p_cam2, &cam2_tally, 200); |
| 206 | assert((cam2_tally.flags & OAVX_TALLY_PROGRAM) == 0); |
| 207 | printf(" [+] Routing cleared, Camera 2 tally light extinguished.\n"); |
| 208 | |
| 209 | /* Downstream receiver is still connected (returns none on timeout, not error) */ |
| 210 | ftype = oavx_stream_receive(p_downstream_recv, &rx_frame, NULL, NULL, 100); |
| 211 | assert(ftype == OAVX_FRAME_TYPE_NONE); |
| 212 | printf(" [+] Downstream receiver remains connected in idle state.\n"); |
| 213 | |
| 214 | /* 9. Teardown */ |
| 215 | printf("[*] 9. Tearing down Matrix Router and instances...\n"); |
| 216 | oavx_stream_close(p_downstream_recv); |
| 217 | oavx_router_destroy(p_router); |
| 218 | oavx_stream_close(p_cam1); |
| 219 | oavx_stream_close(p_cam2); |
| 220 | free(cam1_frame_data); |
| 221 | free(cam2_frame_data); |
| 222 | oavx_context_destroy(ctx); |
| 223 | |
| 224 | printf("\n[PASS] openavx_routing_test\n\n"); |
| 225 | return 0; |
| 226 | } |