+ +
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_loopback_test.c - Basic Loopback Integration Test |
| 7 | */ |
| 8 | |
| 9 | #include "openavx.h" |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <assert.h> |
| 14 | #include <time.h> |
| 15 | #include "avx_platform.h" |
| 16 | |
| 17 | typedef struct { |
| 18 | oavx_stream_t* p_send; |
| 19 | volatile bool running; |
| 20 | size_t frame_bytes; |
| 21 | uint8_t* p_vframe; |
| 22 | size_t audio_bytes; |
| 23 | float* p_aframe; |
| 24 | } sender_thread_arg_t; |
| 25 | |
| 26 | static void* sender_worker(void* arg) { |
| 27 | sender_thread_arg_t* sa = (sender_thread_arg_t*)arg; |
| 28 | |
| 29 | oavx_video_frame_t video_out = { |
| 30 | .width = 1920, |
| 31 | .height = 1080, |
| 32 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY, |
| 33 | .scan_type = OAVX_SCAN_PROGRESSIVE, |
| 34 | .planes = { sa->p_vframe, NULL, NULL, NULL }, |
| 35 | .strides = { 1920 * 2, 0, 0, 0 }, |
| 36 | .framerate = { 60000, 1000 }, |
| 37 | .aspect_ratio = { 16, 9 }, |
| 38 | .pts_ns = 12345678, |
| 39 | .dts_ns = 0, |
| 40 | .metadata = NULL, |
| 41 | .metadata_len = 0, |
| 42 | .user_ctx = NULL |
| 43 | }; |
| 44 | |
| 45 | oavx_audio_frame_t audio_out = { |
| 46 | .sample_rate = 48000, |
| 47 | .channels = 2, |
| 48 | .frames = 1024, |
| 49 | .format = OAVX_AUDIO_FMT_FLT_PLANAR, |
| 50 | .planes = { sa->p_aframe, sa->p_aframe + 1024, NULL, NULL, NULL, NULL, NULL, NULL }, |
| 51 | .stride_bytes = 1024 * sizeof(float), |
| 52 | .pts_ns = 87654321, |
| 53 | .metadata = NULL, |
| 54 | .metadata_len = 0, |
| 55 | .user_ctx = NULL |
| 56 | }; |
| 57 | |
| 58 | while (sa->running) { |
| 59 | /* Only transmit if receiver is connected */ |
| 60 | if (oavx_stream_get_connection_count(sa->p_send, 0) > 0) { |
| 61 | oavx_stream_send_video(sa->p_send, &video_out); |
| 62 | oavx_stream_send_audio(sa->p_send, &audio_out); |
| 63 | } |
| 64 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 16666666L }; /* ~60 FPS */ |
| 65 | nanosleep(&ts, NULL); |
| 66 | } |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | int main(void) { |
| 71 | printf("==========================================\n"); |
| 72 | printf(" OpenAVX End-to-End Loopback Stream Test (oavx_*)\n"); |
| 73 | printf("==========================================\n\n"); |
| 74 | |
| 75 | printf("[*] 1. Initializing OpenAVX Context Engine...\n"); |
| 76 | oavx_context_t *ctx = oavx_context_create(); |
| 77 | assert(ctx != NULL); |
| 78 | |
| 79 | printf("[*] 2. Creating Sender on port 7960...\n"); |
| 80 | oavx_stream_config_t send_settings = { |
| 81 | .name = "OpenAVX (Loopback Stream)", |
| 82 | .groups = NULL, |
| 83 | .direction = OAVX_DIR_TRANSMIT, |
| 84 | .clock_sync = true |
| 85 | }; |
| 86 | oavx_stream_t *p_send = oavx_stream_open(ctx, &send_settings); |
| 87 | assert(p_send != NULL); |
| 88 | printf("[+] Sender stream created!\n"); |
| 89 | |
| 90 | /* Allocate payload buffers */ |
| 91 | size_t frame_bytes = (size_t)1920 * 1080 * 2; |
| 92 | uint8_t* p_vframe = (uint8_t*)malloc(frame_bytes); |
| 93 | assert(p_vframe != NULL); |
| 94 | memset(p_vframe, 0xAA, frame_bytes); /* Fill with known signature 0xAA */ |
| 95 | |
| 96 | size_t audio_bytes = (size_t)2 * 1024 * sizeof(float); |
| 97 | float* p_aframe = (float*)malloc(audio_bytes); |
| 98 | assert(p_aframe != NULL); |
| 99 | for (int i = 0; i < 2 * 1024; ++i) { |
| 100 | p_aframe[i] = 0.5f; |
| 101 | } |
| 102 | |
| 103 | sender_thread_arg_t sarg = { |
| 104 | .p_send = p_send, |
| 105 | .running = true, |
| 106 | .frame_bytes = frame_bytes, |
| 107 | .p_vframe = p_vframe, |
| 108 | .audio_bytes = audio_bytes, |
| 109 | .p_aframe = p_aframe |
| 110 | }; |
| 111 | |
| 112 | avx_thread_t sthread; |
| 113 | int thread_res = avx_thread_create(&sthread, sender_worker, &sarg); |
| 114 | assert(thread_res == 0); |
| 115 | |
| 116 | /* Give sender server time to spin up */ |
| 117 | avx_sleep_ms(100); |
| 118 | |
| 119 | const oavx_endpoint_t* p_src_info = oavx_stream_get_endpoint(p_send); |
| 120 | const char *addr = (p_src_info && p_src_info->address) ? p_src_info->address : "127.0.0.1:7960"; |
| 121 | |
| 122 | printf("[*] 3. Creating Receiver and connecting to %s...\n", addr); |
| 123 | oavx_stream_config_t recv_settings = { |
| 124 | .name = "OpenAVX (Loopback Receiver)", |
| 125 | .endpoint = addr, |
| 126 | .direction = OAVX_DIR_RECEIVE, |
| 127 | .color_format = OAVX_COLOR_FORMAT_UYVY_BGRA, |
| 128 | .bandwidth = OAVX_BANDWIDTH_HIGHEST, |
| 129 | .clock_sync = false |
| 130 | }; |
| 131 | oavx_stream_t *p_recv = oavx_stream_open(ctx, &recv_settings); |
| 132 | assert(p_recv != NULL); |
| 133 | printf("[+] Receiver stream created and connected!\n"); |
| 134 | |
| 135 | int connections = oavx_stream_get_connection_count(p_send, 100); |
| 136 | printf("[+] Active connections on sender: %d (Expected: >= 1)\n", connections); |
| 137 | assert(connections >= 1); |
| 138 | |
| 139 | printf("[*] 4. Capturing Video and Audio Frames on Receiver via oavx_stream_receive...\n"); |
| 140 | oavx_video_frame_t video_in; |
| 141 | oavx_audio_frame_t audio_in; |
| 142 | oavx_metadata_frame_t meta_in; |
| 143 | |
| 144 | bool got_video = false; |
| 145 | bool got_audio = false; |
| 146 | |
| 147 | for (int attempts = 0; attempts < 100 && (!got_video || !got_audio); ++attempts) { |
| 148 | oavx_frame_type_t ftype = oavx_stream_receive(p_recv, &video_in, &audio_in, &meta_in, 100); |
| 149 | if (ftype == OAVX_FRAME_TYPE_VIDEO && !got_video) { |
| 150 | printf("[+] Captured VIDEO frame:\n"); |
| 151 | printf(" - Resolution: %u x %u\n", video_in.width, video_in.height); |
| 152 | printf(" - FourCC: 0x%08X (UYVY)\n", video_in.fourcc); |
| 153 | printf(" - Framerate: %u / %u\n", video_in.framerate.num, video_in.framerate.den); |
| 154 | printf(" - PTS: %ld ns\n", (long)video_in.pts_ns); |
| 155 | assert(video_in.width == 1920); |
| 156 | assert(video_in.height == 1080); |
| 157 | assert(video_in.fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVY); |
| 158 | assert(video_in.planes[0] != NULL); |
| 159 | assert(video_in.planes[0][0] == 0xAA); |
| 160 | assert(video_in.planes[0][frame_bytes - 1] == 0xAA); |
| 161 | printf("[+] Video frame data integrity verified (0xAA signature matched)!\n"); |
| 162 | oavx_stream_release_video(p_recv, &video_in); |
| 163 | got_video = true; |
| 164 | } else if (ftype == OAVX_FRAME_TYPE_AUDIO && !got_audio) { |
| 165 | float* samples = (float*)audio_in.planes[0]; |
| 166 | printf("[+] Captured AUDIO frame:\n"); |
| 167 | printf(" - Sample Rate: %u Hz\n", audio_in.sample_rate); |
| 168 | printf(" - Channels: %u\n", audio_in.channels); |
| 169 | printf(" - Frames: %u\n", audio_in.frames); |
| 170 | printf(" - Sample Val: %.2f\n", samples[0]); |
| 171 | assert(audio_in.sample_rate == 48000); |
| 172 | assert(audio_in.channels == 2); |
| 173 | assert(audio_in.frames == 1024); |
| 174 | assert(samples[0] == 0.5f); |
| 175 | printf("[+] Audio frame data integrity verified (0.5f float matched)!\n"); |
| 176 | oavx_stream_release_audio(p_recv, &audio_in); |
| 177 | got_audio = true; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | assert(got_video); |
| 182 | assert(got_audio); |
| 183 | |
| 184 | printf("[*] 5. Performance Statistics...\n"); |
| 185 | oavx_performance_t perf, dropped; |
| 186 | oavx_stream_get_performance(p_recv, &perf, &dropped); |
| 187 | printf("[+] Receiver performance: %ld video frames, %ld audio frames\n", |
| 188 | (long)perf.video_frames, (long)perf.audio_frames); |
| 189 | assert(perf.video_frames >= 1); |
| 190 | assert(perf.audio_frames >= 1); |
| 191 | |
| 192 | printf("[*] 6. Tearing down Stream Sessions...\n"); |
| 193 | sarg.running = false; |
| 194 | avx_thread_join(sthread); |
| 195 | |
| 196 | free(p_vframe); |
| 197 | free(p_aframe); |
| 198 | |
| 199 | oavx_stream_close(p_recv); |
| 200 | oavx_stream_close(p_send); |
| 201 | oavx_context_destroy(ctx); |
| 202 | |
| 203 | printf("\n[PASS] openavx_loopback_test\n"); |
| 204 | return 0; |
| 205 | } |