Raw ↗
+ +
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 Library
6 * test/openavx_cpp_test.cpp - Modern C++11 RAII Verification Suite (namespace oavx)
7 */
8  
9 #include "openavx.hpp"
10 #include <iostream>
11 #include <vector>
12 #include <cassert>
13 #include <cstring>
14 #include <chrono>
15 #include <thread>
16  
17 static void test_context() {
18 std::cout << "[*] 1. Testing oavx::Context RAII and version info..." << std::endl;
19 const char* ver = oavx::version();
20 assert(ver != nullptr);
21 assert(std::strlen(ver) > 0);
22 std::cout << " Version: " << ver << std::endl;
23  
24 assert(oavx::is_supported_cpu());
25 std::cout << " CPU Supported: true" << std::endl;
26  
27 {
28 oavx::Context ctx;
29 assert(ctx.is_valid());
30 assert((bool)ctx);
31  
32 // Move semantics
33 oavx::Context moved_ctx = std::move(ctx);
34 assert(!ctx.is_valid());
35 assert(moved_ctx.is_valid());
36  
37 // Wire profile configuration
38 oavx_wire_profile_t prof = {};
39 moved_ctx.get_wire_profile(prof);
40 assert(prof.base_port != 0);
41 prof.base_port = 7980;
42 moved_ctx.set_wire_profile(prof);
43 oavx_wire_profile_t updated_prof = {};
44 moved_ctx.get_wire_profile(updated_prof);
45 assert(updated_prof.base_port == 7980);
46 }
47 std::cout << " [+] Context RAII passed." << std::endl;
48 }
49  
50 static void test_stream_tx_rx() {
51 std::cout << "[*] 2. Testing oavx::Stream RAII (Transmit & Receive)..." << std::endl;
52 oavx::Context ctx;
53 assert(ctx.is_valid());
54  
55 // 1. Transmit Stream
56 oavx::Stream tx = oavx::Stream::create_transmitter(ctx, "CPP_STREAM_FEED");
57 assert(tx.is_valid());
58  
59 // Move semantics on stream
60 oavx::Stream moved_tx = std::move(tx);
61 assert(!tx.is_valid());
62 assert(moved_tx.is_valid());
63  
64 // 1b. Audio Compression Control
65 bool comp_ok = moved_tx.set_audio_compression(OAVX_AUDIO_COMPRESSION_MANUAL, 128);
66 assert(comp_ok);
67 assert(moved_tx.set_audio_compression(OAVX_AUDIO_COMPRESSION_ADAPTIVE, 96));
68 assert(moved_tx.set_audio_compression(OAVX_AUDIO_COMPRESSION_OFF, 0));
69  
70 // 1c. Embedded HTTP/fMP4 Web Preview
71 bool web_ok = moved_tx.enable_web_preview(0);
72 assert(web_ok);
73 uint16_t web_port = moved_tx.get_web_preview_port();
74 assert(web_port > 0);
75 std::string web_url = moved_tx.get_web_preview_url();
76 assert(web_url.find("http://") != std::string::npos);
77 assert(moved_tx.get_web_client_count() == 0);
78 moved_tx.disable_web_preview();
79 assert(moved_tx.get_web_preview_port() == 0);
80  
81 // 1d. Native RTSP 1.0 Server
82 bool rtsp_ok = moved_tx.enable_rtsp_server(0, "/live");
83 assert(rtsp_ok);
84 uint16_t rtsp_port = moved_tx.get_rtsp_port();
85 assert(rtsp_port > 0);
86 assert(moved_tx.get_rtsp_client_count() == 0);
87 moved_tx.disable_rtsp_server();
88 assert(moved_tx.get_rtsp_port() == 0);
89  
90 std::this_thread::sleep_for(std::chrono::milliseconds(50));
91  
92 // 2. Receive Stream
93 oavx::Stream rx = oavx::Stream::create_receiver(ctx, "CPP_STREAM_FEED", "127.0.0.1:7960");
94 assert(rx.is_valid());
95  
96 std::this_thread::sleep_for(std::chrono::milliseconds(50));
97  
98 // 3. Send video frame
99 const uint32_t width = 640;
100 const uint32_t height = 360;
101 std::vector<uint8_t> vbuf(width * height * 2, 128); // UYVY
102 oavx_video_frame_t vf = {};
103 vf.width = width;
104 vf.height = height;
105 vf.fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY;
106 vf.framerate.num = 60000;
107 vf.framerate.den = 1000;
108 vf.aspect_ratio.num = 16;
109 vf.aspect_ratio.den = 9;
110 vf.scan_type = OAVX_SCAN_PROGRESSIVE;
111 vf.planes[0] = vbuf.data();
112 vf.strides[0] = width * 2;
113  
114 int v_sent = moved_tx.send_video(vf);
115 assert(v_sent >= 0);
116  
117 // 4. Send audio frame
118 const uint32_t sample_rate = 48000;
119 const uint16_t channels = 2;
120 const uint32_t samples = 1024;
121 std::vector<float> abuf(channels * samples, 0.5f);
122 oavx_audio_frame_t af = {};
123 af.sample_rate = sample_rate;
124 af.channels = channels;
125 af.frames = samples;
126 af.format = OAVX_AUDIO_FMT_FLT_PLANAR;
127 af.planes[0] = abuf.data();
128 af.planes[1] = abuf.data() + samples;
129 af.stride_bytes = samples * sizeof(float);
130  
131 int a_sent = moved_tx.send_audio(af);
132 assert(a_sent >= 0);
133  
134 // 5. Send metadata frame
135 oavx_metadata_frame_t mf = {};
136 char meta_str[] = "<oavx_test>stream_payload</oavx_test>";
137 mf.data = meta_str;
138 mf.length = (int)std::strlen(meta_str);
139  
140 int m_sent = moved_tx.send_metadata(mf);
141 assert(m_sent >= 0);
142  
143 // 6. Receive frames
144 oavx_video_frame_t rx_v = {};
145 oavx_audio_frame_t rx_a = {};
146 oavx_metadata_frame_t rx_m = {};
147 oavx_frame_type_t ft = rx.receive(&rx_v, &rx_a, &rx_m, 200);
148 if (ft == OAVX_FRAME_TYPE_VIDEO) {
149 rx.release_video(rx_v);
150 } else if (ft == OAVX_FRAME_TYPE_AUDIO) {
151 rx.release_audio(rx_a);
152 } else if (ft == OAVX_FRAME_TYPE_METADATA) {
153 rx.release_metadata(rx_m);
154 }
155  
156 // 7. Test Tally
157 oavx_tally_t tally = { OAVX_TALLY_PROGRAM };
158 rx.set_tally(tally);
159  
160 oavx_tally_t tx_tally = {};
161 moved_tx.get_tally(tx_tally, 10);
162  
163 // 8. Test PTZ controls
164 oavx_ptz_state_t ptz = { 0.5f, -0.2f, 1.5f, 0.8f };
165 rx.set_ptz(ptz);
166  
167 // 9. Test Record triggers
168 rx.record_trigger("start", "test_rec.mp4");
169 rx.record_set_audio_level(-6.0f);
170 rx.record_trigger("stop");
171  
172 // 10. Performance & Queue inspection
173 oavx_performance_t perf_total = {};
174 oavx_performance_t perf_dropped = {};
175 rx.get_performance(perf_total, perf_dropped);
176  
177 oavx_queue_t q = {};
178 rx.get_queue(q);
179  
180 // 11. Full Stream Observability Telemetry inspection
181 oavx::Telemetry telem = {};
182 bool telem_ok = rx.get_telemetry(telem);
183 assert(telem_ok);
184 std::string json = rx.get_telemetry_json();
185 assert(!json.empty());
186 assert(json.find("\"connected\":") != std::string::npos);
187  
188 // 12. Failover and Metadata capture controls
189 oavx_endpoint_t fo_ep = {};
190 fo_ep.name = "BACKUP_FEED";
191 fo_ep.address = "127.0.0.1:7961";
192 rx.set_failover(fo_ep);
193 assert(!rx.is_on_failover());
194 rx.set_failover_timeout(1000);
195 rx.set_auto_revert(true, 500);
196  
197 oavx_metadata_frame_t cap_meta = {};
198 rx.capture_metadata(cap_meta, 10);
199  
200 std::cout << " [+] Stream transmit, receive and telemetry passed." << std::endl;
201 }
202  
203 static void test_discovery() {
204 std::cout << "[*] 3. Testing oavx::Discovery RAII..." << std::endl;
205 oavx::Context ctx;
206 assert(ctx.is_valid());
207  
208 {
209 oavx::Discovery disc(ctx, "Public");
210 assert(disc.is_valid());
211  
212 // Move semantics
213 oavx::Discovery moved_disc = std::move(disc);
214 assert(!disc.is_valid());
215 assert(moved_disc.is_valid());
216  
217 moved_disc.wait(50);
218 std::vector<oavx_endpoint_t> sources = moved_disc.get_sources(32);
219 std::cout << " Discovered sources count: " << sources.size() << std::endl;
220 }
221 std::cout << " [+] Discovery RAII passed." << std::endl;
222 }
223  
224 static void test_router() {
225 std::cout << "[*] 4. Testing oavx::Router RAII..." << std::endl;
226 oavx::Context ctx;
227 assert(ctx.is_valid());
228  
229 {
230 oavx::Router router(ctx, "OAVX_MATRIX_ROUTER");
231 assert(router.is_valid());
232  
233 // Move semantics
234 oavx::Router moved_router = std::move(router);
235 assert(!router.is_valid());
236 assert(moved_router.is_valid());
237  
238 oavx_endpoint_t target = {};
239 target.name = "STUDIO_CAM_1";
240 target.address = "127.0.0.1:7960";
241 bool routed = moved_router.route(target);
242 assert(routed);
243  
244 oavx_endpoint_t cur = moved_router.get_source();
245 assert(cur.name != nullptr);
246  
247 bool cleared = moved_router.clear();
248 assert(cleared);
249 }
250 std::cout << " [+] Router RAII passed." << std::endl;
251 }
252  
253 static void test_framesync() {
254 std::cout << "[*] 5. Testing oavx::FrameSync RAII..." << std::endl;
255 oavx::Context ctx;
256 assert(ctx.is_valid());
257  
258 oavx::Stream rx = oavx::Stream::create_receiver(ctx, "FS_TEST_FEED", "127.0.0.1:7960");
259 assert(rx.is_valid());
260  
261 {
262 oavx::FrameSync fs(rx);
263 assert(fs.is_valid());
264  
265 // Move semantics
266 oavx::FrameSync moved_fs = std::move(fs);
267 assert(!fs.is_valid());
268 assert(moved_fs.is_valid());
269  
270 oavx_video_frame_t vf = {};
271 moved_fs.capture_video(vf, 0);
272 moved_fs.free_video(vf);
273  
274 oavx_audio_frame_t af = {};
275 moved_fs.capture_audio(af, 48000, 2, 1024);
276 moved_fs.free_audio(af);
277  
278 int q_depth = moved_fs.audio_queue_depth();
279 assert(q_depth >= 0);
280 }
281 std::cout << " [+] FrameSync RAII passed." << std::endl;
282 }
283  
284 static void test_utilities() {
285 std::cout << "[*] 6. Testing oavx utilities..." << std::endl;
286  
287 const uint32_t samples = 256;
288 const uint16_t channels = 2;
289 std::vector<float> planar(channels * samples, 0.25f);
290 oavx_audio_frame_t src_audio = {};
291 src_audio.sample_rate = 48000;
292 src_audio.channels = channels;
293 src_audio.frames = samples;
294 src_audio.planes[0] = planar.data();
295 src_audio.planes[1] = planar.data() + samples;
296  
297 std::vector<int16_t> interleaved_16(channels * samples, 0);
298 oavx_audio_frame_interleaved_16s_t dst_16 = {};
299 dst_16.data = interleaved_16.data();
300 dst_16.reference_level = 0;
301  
302 oavx::audio_to_interleaved_16s(&src_audio, &dst_16);
303 assert(dst_16.channels == channels);
304 assert(dst_16.frames == samples);
305  
306 oavx_audio_frame_t recovered_audio = {};
307 std::vector<float> rec_buf(channels * samples, 0.0f);
308 recovered_audio.planes[0] = rec_buf.data();
309 recovered_audio.planes[1] = rec_buf.data() + samples;
310  
311 oavx::audio_from_interleaved_16s(&dst_16, &recovered_audio);
312 assert(recovered_audio.channels == channels);
313 assert(recovered_audio.frames == samples);
314  
315 std::cout << " [+] Audio interleaved utilities passed." << std::endl;
316  
317 // SpeedHQ quality configuration (clamped [20, 98])
318 oavx::speedhq_set_quality(80);
319 assert(oavx::speedhq_get_quality() == 80);
320 oavx::speedhq_set_quality(98);
321 assert(oavx::speedhq_get_quality() == 98);
322 oavx::speedhq_set_quality(100);
323 assert(oavx::speedhq_get_quality() == 98); // clamped to 98
324 oavx::speedhq_set_quality(10);
325 assert(oavx::speedhq_get_quality() == 20); // clamped to 20
326 std::cout << " [+] SpeedHQ dynamic quality passed." << std::endl;
327 }
328  
329 int main() {
330 std::cout << "=== OpenAVX C++11 Wrapper Tests ===\n\n";
331  
332 test_context();
333 test_stream_tx_rx();
334 test_discovery();
335 test_router();
336 test_framesync();
337 test_utilities();
338  
339 std::cout << "\n[PASS] openavx_cpp_test\n";
340 return 0;
341 }