+ +
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 | * openavx_test_pattern_test.c - Automated Test Pattern & Reference Signal Generator Verification |
| 7 | */ |
| 8 | |
| 9 | #include "openavx.h" |
| 10 | #include "fixtures/smpte_h264_pattern.h" |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <stdbool.h> |
| 15 | #include <assert.h> |
| 16 | #include <unistd.h> |
| 17 | #include <time.h> |
| 18 | #include <math.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 Test Pattern & Signal Generator Tests ===\n\n"); |
| 30 | |
| 31 | /* 1. Initialize openavx */ |
| 32 | printf("[*] 1. Initializing OpenAVX Context...\n"); |
| 33 | oavx_context_t *ctx = oavx_context_create(); |
| 34 | assert(ctx != NULL); |
| 35 | |
| 36 | /* 2. Create Sender for Test Pattern */ |
| 37 | printf("[*] 2. Spawning test pattern sender 'TEST_PATTERN_STREAM'...\n"); |
| 38 | oavx_stream_config_t send_settings = { |
| 39 | .name = "TEST_PATTERN_STREAM", |
| 40 | .groups = "public", |
| 41 | .direction = OAVX_DIR_TRANSMIT, |
| 42 | .clock_sync = true |
| 43 | }; |
| 44 | oavx_stream_t *p_send = oavx_stream_open(ctx, &send_settings); |
| 45 | assert(p_send != NULL); |
| 46 | |
| 47 | /* 3. Connect Receiver */ |
| 48 | printf("[*] 3. Connecting Receiver to 'TEST_PATTERN_STREAM'...\n"); |
| 49 | oavx_stream_config_t recv_settings = { |
| 50 | .name = "TEST_PATTERN_STREAM", |
| 51 | .endpoint = "127.0.0.1:7960", |
| 52 | .color_format = OAVX_COLOR_FORMAT_BEST, |
| 53 | .bandwidth = OAVX_BANDWIDTH_HIGHEST, |
| 54 | .direction = OAVX_DIR_RECEIVE |
| 55 | }; |
| 56 | oavx_stream_t *p_recv = oavx_stream_open(ctx, &recv_settings); |
| 57 | assert(p_recv != NULL); |
| 58 | |
| 59 | /* Wait for connection handshake */ |
| 60 | sleep_ms(150); |
| 61 | |
| 62 | /* 4. Generate and Transmit 720p SMPTE Frame + 1 kHz Audio Tone */ |
| 63 | const int width = 1280; |
| 64 | const int height = 720; |
| 65 | const int stride = width * 2; |
| 66 | uint8_t* p_vframe = (uint8_t*)malloc((size_t)stride * height); |
| 67 | assert(p_vframe != NULL); |
| 68 | |
| 69 | /* Rec. 709 75% SMPTE color values */ |
| 70 | const uint8_t bar_y[8] = { 180, 168, 145, 133, 63, 51, 28, 16 }; |
| 71 | const uint8_t bar_u[8] = { 128, 44, 148, 64, 192, 108, 212, 128 }; |
| 72 | const uint8_t bar_v[8] = { 128, 142, 24, 38, 218, 232, 114, 128 }; |
| 73 | |
| 74 | for (int y = 0; y < height; ++y) { |
| 75 | uint8_t* row = p_vframe + y * stride; |
| 76 | for (int x = 0; x < width; x += 2) { |
| 77 | int b = (x * 8) / width; |
| 78 | if (b >= 8) b = 7; |
| 79 | row[x * 2 + 0] = bar_u[b]; |
| 80 | row[x * 2 + 1] = bar_y[b]; |
| 81 | row[x * 2 + 2] = bar_v[b]; |
| 82 | row[x * 2 + 3] = bar_y[b]; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | oavx_video_frame_t vf = { |
| 87 | .width = width, |
| 88 | .height = height, |
| 89 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY, |
| 90 | .scan_type = OAVX_SCAN_PROGRESSIVE, |
| 91 | .planes = { p_vframe, NULL, NULL, NULL }, |
| 92 | .strides = { (size_t)stride, 0, 0, 0 }, |
| 93 | .framerate = { 60000, 1000 }, |
| 94 | .aspect_ratio = { 16, 9 }, |
| 95 | .pts_ns = 1000, |
| 96 | .dts_ns = 0, |
| 97 | .metadata = NULL, |
| 98 | .metadata_len = 0, |
| 99 | .user_ctx = NULL |
| 100 | }; |
| 101 | |
| 102 | /* 48 kHz stereo 1 kHz tone (800 samples for 60fps) */ |
| 103 | const int a_samples = 800; |
| 104 | float* a_left = (float*)malloc(a_samples * sizeof(float)); |
| 105 | float* a_right = (float*)malloc(a_samples * sizeof(float)); |
| 106 | float* a_planar = (float*)malloc(2 * a_samples * sizeof(float)); |
| 107 | assert(a_left && a_right && a_planar); |
| 108 | |
| 109 | for (int i = 0; i < a_samples; ++i) { |
| 110 | double t = (double)i / 48000.0; |
| 111 | float s = (float)(0.10 * sin(2.0 * 3.141592653589793 * 1000.0 * t)); |
| 112 | a_left[i] = s; |
| 113 | a_right[i] = s; |
| 114 | } |
| 115 | memcpy(a_planar, a_left, a_samples * sizeof(float)); |
| 116 | memcpy(a_planar + a_samples, a_right, a_samples * sizeof(float)); |
| 117 | |
| 118 | oavx_audio_frame_t af = { |
| 119 | .sample_rate = 48000, |
| 120 | .channels = 2, |
| 121 | .frames = a_samples, |
| 122 | .format = OAVX_AUDIO_FMT_FLT_PLANAR, |
| 123 | .planes = { a_planar, a_planar + a_samples, NULL, NULL, NULL, NULL, NULL, NULL }, |
| 124 | .stride_bytes = a_samples * sizeof(float), |
| 125 | .pts_ns = 1000, |
| 126 | .metadata = NULL, |
| 127 | .metadata_len = 0, |
| 128 | .user_ctx = NULL |
| 129 | }; |
| 130 | |
| 131 | /* Transmit frames */ |
| 132 | printf("[*] 4. Transmitting test pattern video and 1 kHz audio frames...\n"); |
| 133 | for (int f = 0; f < 5; ++f) { |
| 134 | oavx_stream_send_video(p_send, &vf); |
| 135 | oavx_stream_send_audio(p_send, &af); |
| 136 | sleep_ms(16); |
| 137 | } |
| 138 | |
| 139 | /* 5. Capture and verify video frame */ |
| 140 | printf("[*] 5. Capturing video frame on receiver...\n"); |
| 141 | oavx_video_frame_t rx_video = {0}; |
| 142 | oavx_audio_frame_t rx_audio = {0}; |
| 143 | oavx_metadata_frame_t rx_meta = {0}; |
| 144 | |
| 145 | bool got_video = false; |
| 146 | for (int tries = 0; tries < 20 && !got_video; ++tries) { |
| 147 | oavx_frame_type_e ft = oavx_stream_receive(p_recv, &rx_video, &rx_audio, &rx_meta, 100); |
| 148 | if (ft == OAVX_FRAME_TYPE_VIDEO) { |
| 149 | got_video = true; |
| 150 | assert(rx_video.width == 1280); |
| 151 | assert(rx_video.height == 720); |
| 152 | assert(rx_video.fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVY); |
| 153 | assert(rx_video.planes[0] != NULL); |
| 154 | |
| 155 | /* Verify color bars across the row */ |
| 156 | const uint8_t* row = rx_video.planes[0] + (360 * rx_video.strides[0]); |
| 157 | for (int b = 0; b < 8; ++b) { |
| 158 | int test_x = (b * width) / 8 + (width / 16); |
| 159 | if (test_x % 2 != 0) test_x--; |
| 160 | uint8_t cap_y = row[test_x * 2 + 1]; |
| 161 | assert(cap_y == bar_y[b]); |
| 162 | } |
| 163 | printf(" [+] Captured 1280x720 UYVY video: SMPTE 8-bar chrominance/luminance bitwise exact!\n"); |
| 164 | oavx_stream_release_video(p_recv, &rx_video); |
| 165 | } else if (ft == OAVX_FRAME_TYPE_AUDIO) { |
| 166 | oavx_stream_release_audio(p_recv, &rx_audio); |
| 167 | } |
| 168 | } |
| 169 | assert(got_video); |
| 170 | |
| 171 | /* 6. Capture and verify audio frame */ |
| 172 | printf("[*] 6. Capturing audio frame on receiver...\n"); |
| 173 | bool got_audio = false; |
| 174 | for (int tries = 0; tries < 20 && !got_audio; ++tries) { |
| 175 | oavx_frame_type_e ft = oavx_stream_receive(p_recv, &rx_video, &rx_audio, &rx_meta, 100); |
| 176 | if (ft == OAVX_FRAME_TYPE_AUDIO) { |
| 177 | got_audio = true; |
| 178 | assert(rx_audio.sample_rate == 48000); |
| 179 | assert(rx_audio.channels == 2); |
| 180 | assert(rx_audio.frames == 800); |
| 181 | assert(rx_audio.planes[0] != NULL); |
| 182 | |
| 183 | /* Check 1 kHz tone energy and zero crossings */ |
| 184 | float* samples = (float*)rx_audio.planes[0]; |
| 185 | double energy = 0.0; |
| 186 | int zero_crossings = 0; |
| 187 | for (uint32_t i = 0; i < rx_audio.frames; ++i) { |
| 188 | energy += samples[i] * samples[i]; |
| 189 | if (i > 0 && ((samples[i - 1] < 0.0f && samples[i] >= 0.0f) || |
| 190 | (samples[i - 1] >= 0.0f && samples[i] < 0.0f))) { |
| 191 | zero_crossings++; |
| 192 | } |
| 193 | } |
| 194 | assert(energy > 0.01); /* Tone has active energy */ |
| 195 | /* In 800 samples at 48kHz (16.66ms), a 1000Hz wave has ~16.6 cycles -> ~33 zero crossings */ |
| 196 | assert(zero_crossings >= 30 && zero_crossings <= 36); |
| 197 | printf(" [+] Captured 48kHz stereo audio: 1 kHz reference tone verified (energy=%.4f, zero_crossings=%d)!\n", |
| 198 | energy, zero_crossings); |
| 199 | |
| 200 | oavx_stream_release_audio(p_recv, &rx_audio); |
| 201 | } else if (ft == OAVX_FRAME_TYPE_VIDEO) { |
| 202 | oavx_stream_release_video(p_recv, &rx_video); |
| 203 | } |
| 204 | } |
| 205 | assert(got_audio); |
| 206 | |
| 207 | /* 7. Verify Embedded H.264 SMPTE Bitstream & Web/RTSP Lifecycle */ |
| 208 | printf("[*] 7. Verifying embedded Annex-B H.264 SMPTE bitstream + Web/RTSP servers...\n"); |
| 209 | assert(g_smpte_h264_1080p_len > 1000); |
| 210 | oavx_status_t web_st = oavx_stream_enable_web_preview(p_send, 0); |
| 211 | assert(web_st == OAVX_SUCCESS); |
| 212 | char web_url_buf[256] = {0}; |
| 213 | const char *web_u = oavx_stream_get_web_preview_url(p_send, web_url_buf, sizeof(web_url_buf)); |
| 214 | assert(web_u != NULL); |
| 215 | assert(strstr(web_url_buf, "http://") != NULL); |
| 216 | assert(oavx_stream_get_web_client_count(p_send) == 0); |
| 217 | |
| 218 | oavx_status_t rtsp_st = oavx_stream_enable_rtsp_server(p_send, 0, "live"); |
| 219 | assert(rtsp_st == OAVX_SUCCESS); |
| 220 | assert(oavx_stream_get_rtsp_port(p_send) > 0); |
| 221 | assert(oavx_stream_get_rtsp_client_count(p_send) == 0); |
| 222 | |
| 223 | oavx_video_frame_t h264_vf = { |
| 224 | .width = 1920, |
| 225 | .height = 1080, |
| 226 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_H264, |
| 227 | .scan_type = OAVX_SCAN_PROGRESSIVE, |
| 228 | .planes = { (uint8_t*)g_smpte_h264_1080p, NULL, NULL, NULL }, |
| 229 | .strides = { g_smpte_h264_1080p_len, 0, 0, 0 }, |
| 230 | .framerate = { 60000, 1000 }, |
| 231 | .aspect_ratio = { 16, 9 }, |
| 232 | .pts_ns = 2000000000ULL, |
| 233 | .dts_ns = 0, |
| 234 | .metadata = NULL, |
| 235 | .metadata_len = 0, |
| 236 | .user_ctx = NULL |
| 237 | }; |
| 238 | oavx_status_t snd_st = oavx_stream_send_video(p_send, &h264_vf); |
| 239 | assert(snd_st == OAVX_SUCCESS); |
| 240 | |
| 241 | oavx_stream_disable_web_preview(p_send); |
| 242 | oavx_stream_disable_rtsp_server(p_send); |
| 243 | assert(oavx_stream_get_web_preview_port(p_send) == 0); |
| 244 | assert(oavx_stream_get_rtsp_port(p_send) == 0); |
| 245 | printf(" [+] Embedded H.264 bitstream (%zu bytes) and Web/RTSP servers validated.\n", g_smpte_h264_1080p_len); |
| 246 | |
| 247 | /* Teardown */ |
| 248 | oavx_stream_close(p_recv); |
| 249 | oavx_stream_close(p_send); |
| 250 | free(p_vframe); |
| 251 | free(a_left); |
| 252 | free(a_right); |
| 253 | free(a_planar); |
| 254 | oavx_context_destroy(ctx); |
| 255 | |
| 256 | printf("\n[PASS] openavx_test_pattern_test\n"); |
| 257 | return 0; |
| 258 | } |