+ +
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_compressed_loopback_test.c - End-to-end SpeedHQ-2 compressed streaming test |
| 7 | */ |
| 8 | |
| 9 | #define _GNU_SOURCE |
| 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 | #include "openavx.h" |
| 18 | |
| 19 | #define TEST_WIDTH 1920 |
| 20 | #define TEST_HEIGHT 1080 |
| 21 | #define TEST_PORT 7960 |
| 22 | |
| 23 | typedef struct { |
| 24 | oavx_stream_t *p_send; |
| 25 | volatile bool running; |
| 26 | volatile bool frame_sent; |
| 27 | uint8_t* video_data; |
| 28 | } sender_thread_arg_t; |
| 29 | |
| 30 | static void* sender_thread_func(void* arg) { |
| 31 | sender_thread_arg_t* s_arg = (sender_thread_arg_t*)arg; |
| 32 | |
| 33 | oavx_video_frame_t video_frame = { |
| 34 | .width = TEST_WIDTH, |
| 35 | .height = TEST_HEIGHT, |
| 36 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_SHQ2, /* Tell OpenAVX to compress to SpeedHQ-2 */ |
| 37 | .scan_type = OAVX_SCAN_PROGRESSIVE, |
| 38 | .planes = { s_arg->video_data, NULL, NULL, NULL }, |
| 39 | .strides = { TEST_WIDTH * 2, 0, 0, 0 }, |
| 40 | .framerate = { 60000, 1000 }, |
| 41 | .aspect_ratio = { 16, 9 }, |
| 42 | .pts_ns = 99887766, |
| 43 | .dts_ns = 0, |
| 44 | .metadata = NULL, |
| 45 | .metadata_len = 0, |
| 46 | .user_ctx = NULL |
| 47 | }; |
| 48 | |
| 49 | while (s_arg->running) { |
| 50 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 15000000L }; /* ~60 fps cadence */ |
| 51 | nanosleep(&ts, NULL); |
| 52 | oavx_stream_send_video(s_arg->p_send, &video_frame); |
| 53 | s_arg->frame_sent = true; |
| 54 | } |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | int main(void) { |
| 59 | printf("\n=======================================================\n"); |
| 60 | printf(" OpenAVX End-to-End SpeedHQ-2 Compressed Stream Test\n"); |
| 61 | printf("=======================================================\n\n"); |
| 62 | |
| 63 | /* 1. Initialize */ |
| 64 | printf("[*] 1. Initializing OpenAVX...\n"); |
| 65 | oavx_context_t *ctx = oavx_context_create(); |
| 66 | assert(ctx != NULL); |
| 67 | |
| 68 | /* 2. Create Sender */ |
| 69 | printf("[*] 2. Creating Sender with on-the-fly SpeedHQ-2 compression on port %d...\n", TEST_PORT); |
| 70 | oavx_stream_config_t send_create = { |
| 71 | .name = "OPENAVX_COMPRESSED_SOURCE", |
| 72 | .groups = "public", |
| 73 | .direction = OAVX_DIR_TRANSMIT, |
| 74 | .clock_sync = true |
| 75 | }; |
| 76 | oavx_stream_t *p_send = oavx_stream_open(ctx, &send_create); |
| 77 | assert(p_send != NULL); |
| 78 | |
| 79 | /* Allocate test frame: gradient UYVY pattern */ |
| 80 | size_t raw_size = (size_t)TEST_WIDTH * TEST_HEIGHT * 2; |
| 81 | uint8_t* raw_uyvy = malloc(raw_size); |
| 82 | assert(raw_uyvy != NULL); |
| 83 | for (int y = 0; y < TEST_HEIGHT; ++y) { |
| 84 | uint8_t* line = raw_uyvy + (size_t)y * (TEST_WIDTH * 2); |
| 85 | for (int x = 0; x < TEST_WIDTH / 2; ++x) { |
| 86 | line[4 * x + 0] = (uint8_t)((x * 255) / (TEST_WIDTH / 2)); /* U */ |
| 87 | line[4 * x + 1] = (uint8_t)((y * 255) / TEST_HEIGHT); /* Y0 */ |
| 88 | line[4 * x + 2] = 128; /* V */ |
| 89 | line[4 * x + 3] = (uint8_t)((y * 255) / TEST_HEIGHT); /* Y1 */ |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | sender_thread_arg_t s_arg = { |
| 94 | .p_send = p_send, |
| 95 | .running = true, |
| 96 | .frame_sent = false, |
| 97 | .video_data = raw_uyvy |
| 98 | }; |
| 99 | avx_thread_t s_thread; |
| 100 | avx_thread_create(&s_thread, sender_thread_func, &s_arg); |
| 101 | |
| 102 | /* Give sender thread time to start listening */ |
| 103 | avx_sleep_ms(50); |
| 104 | |
| 105 | /* 3. Test Mode A: Receiver requesting uncompressed UYVY (auto-decompressing SpeedHQ) */ |
| 106 | printf("[*] 3. Testing Receiver Mode A: Auto-Decompressing SpeedHQ-2 to UYVY...\n"); |
| 107 | oavx_stream_config_t recv_create = { |
| 108 | .name = "SpeedHQ_Test_Client_A", |
| 109 | .endpoint = "127.0.0.1:7960", |
| 110 | .direction = OAVX_DIR_RECEIVE, |
| 111 | .color_format = OAVX_COLOR_FORMAT_UYVY_RGBA, |
| 112 | .bandwidth = OAVX_BANDWIDTH_HIGHEST, |
| 113 | .clock_sync = false |
| 114 | }; |
| 115 | oavx_stream_t *p_recv = oavx_stream_open(ctx, &recv_create); |
| 116 | assert(p_recv != NULL); |
| 117 | |
| 118 | oavx_video_frame_t v_frame = {0}; |
| 119 | oavx_audio_frame_t a_frame = {0}; |
| 120 | oavx_metadata_frame_t m_frame = {0}; |
| 121 | |
| 122 | int attempts = 0; |
| 123 | bool video_received = false; |
| 124 | while (attempts++ < 50 && !video_received) { |
| 125 | oavx_frame_type_t ftype = oavx_stream_receive(p_recv, &v_frame, &a_frame, &m_frame, 200); |
| 126 | if (ftype == OAVX_FRAME_TYPE_VIDEO) { |
| 127 | video_received = true; |
| 128 | printf(" [+] Captured VIDEO frame in Mode A:\n"); |
| 129 | printf(" - Resolution: %u x %u\n", v_frame.width, v_frame.height); |
| 130 | printf(" - FourCC: 0x%08X (%c%c%c%c)\n", |
| 131 | v_frame.fourcc, |
| 132 | (v_frame.fourcc) & 0xFF, |
| 133 | (v_frame.fourcc >> 8) & 0xFF, |
| 134 | (v_frame.fourcc >> 16) & 0xFF, |
| 135 | (v_frame.fourcc >> 24) & 0xFF); |
| 136 | printf(" - Stride: %zu bytes\n", v_frame.strides[0]); |
| 137 | printf(" - PTS: %ld ns\n", (long)v_frame.pts_ns); |
| 138 | |
| 139 | assert(v_frame.width == TEST_WIDTH); |
| 140 | assert(v_frame.height == TEST_HEIGHT); |
| 141 | assert(v_frame.fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVY); |
| 142 | assert(v_frame.planes[0] != NULL); |
| 143 | assert(v_frame.strides[0] == TEST_WIDTH * 2); |
| 144 | |
| 145 | oavx_stream_release_video(p_recv, &v_frame); |
| 146 | } |
| 147 | } |
| 148 | assert(video_received); |
| 149 | oavx_stream_close(p_recv); |
| 150 | |
| 151 | /* 4. Test Mode B: Receiver requesting raw compressed frames (OAVX_COLOR_FORMAT_COMPRESSED_V1) */ |
| 152 | printf("[*] 4. Testing Receiver Mode B: Raw Compressed Pass-Through (Zero Decode CPU)...\n"); |
| 153 | recv_create.color_format = OAVX_COLOR_FORMAT_COMPRESSED_V1; |
| 154 | recv_create.name = "SpeedHQ_Test_Client_B"; |
| 155 | p_recv = oavx_stream_open(ctx, &recv_create); |
| 156 | assert(p_recv != NULL); |
| 157 | |
| 158 | video_received = false; |
| 159 | attempts = 0; |
| 160 | while (attempts++ < 50 && !video_received) { |
| 161 | oavx_frame_type_t ftype = oavx_stream_receive(p_recv, &v_frame, &a_frame, &m_frame, 200); |
| 162 | if (ftype == OAVX_FRAME_TYPE_VIDEO) { |
| 163 | video_received = true; |
| 164 | printf(" [+] Captured VIDEO frame in Mode B:\n"); |
| 165 | printf(" - Resolution: %u x %u\n", v_frame.width, v_frame.height); |
| 166 | printf(" - FourCC: 0x%08X (%c%c%c%c)\n", |
| 167 | v_frame.fourcc, |
| 168 | (v_frame.fourcc) & 0xFF, |
| 169 | (v_frame.fourcc >> 8) & 0xFF, |
| 170 | (v_frame.fourcc >> 16) & 0xFF, |
| 171 | (v_frame.fourcc >> 24) & 0xFF); |
| 172 | printf(" - Compressed Size: %zu bytes (Wire Savings: %.1f%%)\n", |
| 173 | v_frame.strides[0], |
| 174 | 100.0 - (double)v_frame.strides[0] * 100.0 / (double)raw_size); |
| 175 | |
| 176 | assert(v_frame.width == TEST_WIDTH); |
| 177 | assert(v_frame.height == TEST_HEIGHT); |
| 178 | assert(v_frame.fourcc == OAVX_FOURCC_VIDEO_TYPE_SHQ2); |
| 179 | assert(v_frame.strides[0] < raw_size / 10); /* Must be compressed by >10x */ |
| 180 | |
| 181 | oavx_stream_release_video(p_recv, &v_frame); |
| 182 | } |
| 183 | } |
| 184 | assert(video_received); |
| 185 | |
| 186 | /* 5. Cleanup */ |
| 187 | printf("[*] 5. Tearing down sessions...\n"); |
| 188 | s_arg.running = false; |
| 189 | avx_thread_join(s_thread); |
| 190 | oavx_stream_close(p_recv); |
| 191 | oavx_stream_close(p_send); |
| 192 | oavx_context_destroy(ctx); |
| 193 | free(raw_uyvy); |
| 194 | |
| 195 | printf("\n[PASS] openavx_compressed_loopback_test\n\n"); |
| 196 | return 0; |
| 197 | } |