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
6 * include/openavx.hpp - C++11 RAII wrapper for OpenAVX
7 */
8  
9 #pragma once
10 #ifndef OPENAVX_HPP
11 #define OPENAVX_HPP
12  
13 #include "openavx.h"
14 #include <string>
15 #include <vector>
16 #include <utility>
17  
18 namespace oavx {
19  
20 /**
21 * Free query functions matching clean modern oavx_* C API
22 */
23 inline const char* version() {
24 return oavx_version();
25 }
26  
27 inline bool is_supported_cpu() {
28 return oavx_is_supported_cpu();
29 }
30  
31 inline const char* status_to_string(oavx_status_t status) {
32 return oavx_status_to_string(status);
33 }
34  
35 typedef oavx_telemetry_t Telemetry;
36  
37 /**
38 * RAII Context Engine Guard
39 */
40 class Context {
41 public:
42 Context() : m_ctx(oavx_context_create()) {}
43 ~Context() {
44 if (m_ctx) {
45 oavx_context_destroy(m_ctx);
46 m_ctx = nullptr;
47 }
48 }
49  
50 bool is_valid() const { return m_ctx != nullptr; }
51 explicit operator bool() const { return is_valid(); }
52 oavx_context_t* get() const { return m_ctx; }
53  
54 Context(const Context&) = delete;
55 Context& operator=(const Context&) = delete;
56  
57 Context(Context&& other) noexcept : m_ctx(other.m_ctx) {
58 other.m_ctx = nullptr;
59 }
60  
61 Context& operator=(Context&& other) noexcept {
62 if (this != &other) {
63 if (m_ctx) {
64 oavx_context_destroy(m_ctx);
65 }
66 m_ctx = other.m_ctx;
67 other.m_ctx = nullptr;
68 }
69 return *this;
70 }
71  
72 /* Wire Profile Configuration */
73 void set_wire_profile(const oavx_wire_profile_t& profile) {
74 oavx_context_set_wire_profile(m_ctx, &profile);
75 }
76  
77 void get_wire_profile(oavx_wire_profile_t& out_profile) const {
78 oavx_context_get_wire_profile(m_ctx, &out_profile);
79 }
80  
81 private:
82 oavx_context_t* m_ctx;
83 };
84  
85 /**
86 * RAII Unified Media Stream (Transmit & Receive)
87 */
88 class Stream {
89 public:
90 Stream() : m_stream(nullptr) {}
91  
92 /* Config Constructor */
93 Stream(Context& ctx, const oavx_stream_config_t& config) {
94 m_stream = oavx_stream_open(ctx.get(), &config);
95 }
96  
97 /* Named Direction Constructor */
98 Stream(Context& ctx, const std::string& name, oavx_direction_t direction,
99 const std::string& endpoint = "", const std::string& groups = "") {
100 oavx_stream_config_t cfg = {};
101 cfg.name = name.c_str();
102 cfg.direction = direction;
103 cfg.endpoint = endpoint.empty() ? nullptr : endpoint.c_str();
104 cfg.groups = groups.empty() ? nullptr : groups.c_str();
105 cfg.color_format = OAVX_COLOR_FORMAT_UYVY_RGBA;
106 cfg.bandwidth = OAVX_BANDWIDTH_HIGHEST;
107 m_stream = oavx_stream_open(ctx.get(), &cfg);
108 }
109  
110 /* Static Factory Methods */
111 static Stream create_transmitter(Context& ctx, const std::string& name, const std::string& groups = "") {
112 return Stream(ctx, name, OAVX_DIR_TRANSMIT, "", groups);
113 }
114  
115 static Stream create_receiver(Context& ctx, const std::string& name, const std::string& endpoint,
116 oavx_color_format_e color = OAVX_COLOR_FORMAT_UYVY_RGBA,
117 oavx_bandwidth_e bw = OAVX_BANDWIDTH_HIGHEST,
118 const std::string& groups = "") {
119 oavx_stream_config_t cfg = {};
120 cfg.name = name.c_str();
121 cfg.endpoint = endpoint.c_str();
122 cfg.direction = OAVX_DIR_RECEIVE;
123 cfg.color_format = color;
124 cfg.bandwidth = bw;
125 cfg.groups = groups.empty() ? nullptr : groups.c_str();
126 return Stream(ctx, cfg);
127 }
128  
129 ~Stream() {
130 if (m_stream) {
131 oavx_stream_close(m_stream);
132 m_stream = nullptr;
133 }
134 }
135  
136 bool is_valid() const { return m_stream != nullptr; }
137 explicit operator bool() const { return is_valid(); }
138 oavx_stream_t* get() const { return m_stream; }
139  
140 Stream(const Stream&) = delete;
141 Stream& operator=(const Stream&) = delete;
142  
143 Stream(Stream&& other) noexcept : m_stream(other.m_stream) {
144 other.m_stream = nullptr;
145 }
146  
147 Stream& operator=(Stream&& other) noexcept {
148 if (this != &other) {
149 if (m_stream) {
150 oavx_stream_close(m_stream);
151 }
152 m_stream = other.m_stream;
153 other.m_stream = nullptr;
154 }
155 return *this;
156 }
157  
158 /* Data Transmission */
159 oavx_status_t send_video(const oavx_video_frame_t& frame) {
160 return oavx_stream_send_video(m_stream, &frame);
161 }
162  
163 oavx_status_t send_audio(const oavx_audio_frame_t& frame) {
164 return oavx_stream_send_audio(m_stream, &frame);
165 }
166  
167 oavx_status_t send_metadata(const oavx_metadata_frame_t& meta) {
168 return oavx_stream_send_metadata(m_stream, &meta);
169 }
170  
171 /* Audio Compression Engine */
172 bool set_audio_compression(oavx_audio_compression_mode_e mode, int target_bitrate_kbps = 128) {
173 return oavx_stream_set_audio_compression(m_stream, mode, target_bitrate_kbps) == OAVX_SUCCESS;
174 }
175  
176 /* Video Compression Engine */
177 bool set_video_compression(uint32_t target_fourcc, uint32_t bitrate_kbps = 0) {
178 return oavx_stream_set_video_compression(m_stream, target_fourcc, bitrate_kbps) == OAVX_SUCCESS;
179 }
180  
181 uint32_t get_video_compression() const {
182 return oavx_stream_get_video_compression(m_stream);
183 }
184  
185 bool set_video_quality(uint8_t quality) {
186 return oavx_stream_set_video_quality(m_stream, quality) == OAVX_SUCCESS;
187 }
188  
189 uint8_t get_video_quality() const {
190 return oavx_stream_get_video_quality(m_stream);
191 }
192  
193 /* Native RTSP 1.0 Server */
194 bool enable_rtsp_server(uint16_t port = 0, const std::string& path = "/live") {
195 return oavx_stream_enable_rtsp_server(m_stream, port, path.c_str()) == OAVX_SUCCESS;
196 }
197  
198 void disable_rtsp_server() {
199 oavx_stream_disable_rtsp_server(m_stream);
200 }
201  
202 uint16_t get_rtsp_port() const {
203 return oavx_stream_get_rtsp_port(m_stream);
204 }
205  
206 int get_rtsp_client_count() const {
207 return oavx_stream_get_rtsp_client_count(m_stream);
208 }
209  
210 /* Embedded HTTP / fMP4 Low-Latency Web Preview */
211 bool enable_web_preview(uint16_t port = 0) {
212 return oavx_stream_enable_web_preview(m_stream, port) == OAVX_SUCCESS;
213 }
214  
215 void disable_web_preview() {
216 oavx_stream_disable_web_preview(m_stream);
217 }
218  
219 uint16_t get_web_preview_port() const {
220 return oavx_stream_get_web_preview_port(m_stream);
221 }
222  
223 int get_web_client_count() const {
224 return oavx_stream_get_web_client_count(m_stream);
225 }
226  
227 std::string get_web_preview_url() const {
228 char buf[256] = {0};
229 const char* u = oavx_stream_get_web_preview_url(m_stream, buf, sizeof(buf));
230 return u ? std::string(u) : std::string();
231 }
232  
233 /* Data Reception */
234 /**
235 * Receives next available frame. Call release_video() after consuming returned video frames.
236 */
237 oavx_frame_type_t receive(oavx_video_frame_t* v,
238 oavx_audio_frame_t* a,
239 oavx_metadata_frame_t* m,
240 uint32_t timeout_ms) {
241 return oavx_stream_receive(m_stream, v, a, m, timeout_ms);
242 }
243  
244 void release_video(const oavx_video_frame_t& frame) {
245 oavx_stream_release_video(m_stream, &frame);
246 }
247  
248 void release_audio(const oavx_audio_frame_t& frame) {
249 oavx_stream_release_audio(m_stream, &frame);
250 }
251  
252 void release_metadata(const oavx_metadata_frame_t& meta) {
253 oavx_stream_release_metadata(m_stream, &meta);
254 }
255  
256 /* PTZ & Camera Controls */
257 bool set_ptz(const oavx_ptz_state_t& ptz) {
258 return oavx_stream_set_ptz(m_stream, &ptz);
259 }
260  
261 /* Tally Controls */
262 bool set_tally(const oavx_tally_t& tally) {
263 return oavx_stream_set_tally(m_stream, &tally);
264 }
265  
266 bool get_tally(oavx_tally_t& tally, uint32_t timeout_ms = 0) const {
267 return oavx_stream_get_tally(m_stream, &tally, timeout_ms);
268 }
269  
270 /* Recording Triggers */
271 bool record_trigger(const std::string& action, const std::string& filename_hint = "") {
272 return oavx_stream_record_trigger(m_stream, action.c_str(), filename_hint.empty() ? nullptr : filename_hint.c_str());
273 }
274  
275 bool record_status(bool& is_recording, std::string& filename, std::string& error) const {
276 const char* fn = nullptr;
277 const char* err = nullptr;
278 bool ok = oavx_stream_record_status(m_stream, &is_recording, &fn, &err);
279 filename = fn ? fn : "";
280 error = err ? err : "";
281 return ok;
282 }
283  
284 bool record_set_audio_level(float level_db) {
285 return oavx_stream_record_set_audio_level(m_stream, level_db);
286 }
287  
288 /* Endpoint & Connection Control */
289 void connect(const oavx_endpoint_t& endpoint) {
290 oavx_stream_connect(m_stream, &endpoint);
291 }
292  
293 const oavx_endpoint_t* get_endpoint() const {
294 return oavx_stream_get_endpoint(m_stream);
295 }
296  
297 int get_connection_count(uint32_t timeout_ms = 0) const {
298 return oavx_stream_get_connection_count(m_stream, timeout_ms);
299 }
300  
301 bool get_performance(oavx_performance_t& total, oavx_performance_t& dropped) const {
302 return oavx_stream_get_performance(m_stream, &total, &dropped);
303 }
304  
305 bool get_queue(oavx_queue_t& queue) const {
306 return oavx_stream_get_queue(m_stream, &queue);
307 }
308  
309 bool get_telemetry(oavx_telemetry_t& telemetry) const {
310 return oavx_stream_get_telemetry(m_stream, &telemetry);
311 }
312  
313 std::string get_telemetry_json() const {
314 oavx_telemetry_t telem = {};
315 if (!oavx_stream_get_telemetry(m_stream, &telem)) {
316 return "{}";
317 }
318 char buf[2048];
319 oavx_telemetry_to_json(&telem, buf, sizeof(buf));
320 return std::string(buf);
321 }
322  
323 const char* get_web_control() const {
324 return oavx_stream_get_web_control(m_stream);
325 }
326  
327 void clear_connection_metadata() {
328 oavx_stream_clear_connection_metadata(m_stream);
329 }
330  
331 void add_connection_metadata(const oavx_metadata_frame_t& meta) {
332 oavx_stream_add_connection_metadata(m_stream, &meta);
333 }
334  
335 void set_failover(const oavx_endpoint_t& failover_endpoint) {
336 oavx_stream_set_failover(m_stream, &failover_endpoint);
337 }
338  
339 bool is_on_failover() const {
340 return oavx_stream_is_on_failover(m_stream);
341 }
342  
343 void set_failover_timeout(uint32_t timeout_ms) {
344 oavx_stream_set_failover_timeout(m_stream, timeout_ms);
345 }
346  
347 void set_auto_revert(bool auto_revert, uint32_t hold_time_ms = 0) {
348 oavx_stream_set_auto_revert(m_stream, auto_revert, hold_time_ms);
349 }
350  
351 oavx_frame_type_t capture_metadata(oavx_metadata_frame_t& meta, uint32_t timeout_ms = 0) {
352 return oavx_stream_capture_metadata(m_stream, &meta, timeout_ms);
353 }
354  
355 int get_fd() const {
356 return oavx_stream_get_fd(m_stream);
357 }
358  
359 private:
360 oavx_stream_t* m_stream;
361 };
362  
363 /**
364 * RAII Source Discovery Grid
365 */
366 class Discovery {
367 public:
368 Discovery() : m_disc(nullptr) {}
369  
370 Discovery(Context& ctx, const std::string& groups = "") {
371 m_disc = oavx_discovery_create(ctx.get(), groups.empty() ? nullptr : groups.c_str());
372 }
373  
374 ~Discovery() {
375 if (m_disc) {
376 oavx_discovery_destroy(m_disc);
377 m_disc = nullptr;
378 }
379 }
380  
381 bool is_valid() const { return m_disc != nullptr; }
382 explicit operator bool() const { return is_valid(); }
383 oavx_discovery_t* get() const { return m_disc; }
384  
385 Discovery(const Discovery&) = delete;
386 Discovery& operator=(const Discovery&) = delete;
387  
388 Discovery(Discovery&& other) noexcept : m_disc(other.m_disc) {
389 other.m_disc = nullptr;
390 }
391  
392 Discovery& operator=(Discovery&& other) noexcept {
393 if (this != &other) {
394 if (m_disc) {
395 oavx_discovery_destroy(m_disc);
396 }
397 m_disc = other.m_disc;
398 other.m_disc = nullptr;
399 }
400 return *this;
401 }
402  
403 std::vector<oavx_endpoint_t> get_sources(size_t max_count = 64) const {
404 std::vector<oavx_endpoint_t> vec(max_count);
405 size_t count = oavx_discovery_get_sources(m_disc, vec.data(), max_count);
406 vec.resize(count);
407 return vec;
408 }
409  
410 bool wait(uint32_t timeout_ms) {
411 return oavx_discovery_wait(m_disc, timeout_ms);
412 }
413  
414 int get_fd() const {
415 return oavx_discovery_get_fd(m_disc);
416 }
417  
418 private:
419 oavx_discovery_t* m_disc;
420 };
421  
422 /**
423 * RAII Dynamic Matrix Router
424 */
425 class Router {
426 public:
427 Router() : m_router(nullptr) {}
428  
429 Router(Context& ctx, const std::string& name, const std::string& groups = "") {
430 m_router = oavx_router_create(ctx.get(), name.c_str(), groups.empty() ? nullptr : groups.c_str());
431 }
432  
433 ~Router() {
434 if (m_router) {
435 oavx_router_destroy(m_router);
436 m_router = nullptr;
437 }
438 }
439  
440 bool is_valid() const { return m_router != nullptr; }
441 explicit operator bool() const { return is_valid(); }
442 oavx_router_t* get() const { return m_router; }
443  
444 Router(const Router&) = delete;
445 Router& operator=(const Router&) = delete;
446  
447 Router(Router&& other) noexcept : m_router(other.m_router) {
448 other.m_router = nullptr;
449 }
450  
451 Router& operator=(Router&& other) noexcept {
452 if (this != &other) {
453 if (m_router) {
454 oavx_router_destroy(m_router);
455 }
456 m_router = other.m_router;
457 other.m_router = nullptr;
458 }
459 return *this;
460 }
461  
462 /**
463 * Switches routed source dynamically without disconnecting downstream clients.
464 */
465 bool route(const oavx_endpoint_t& source) {
466 return oavx_router_route(m_router, &source);
467 }
468  
469 bool clear() {
470 return oavx_router_clear(m_router);
471 }
472  
473 oavx_endpoint_t get_source() const {
474 return oavx_router_get_source(m_router);
475 }
476  
477 private:
478 oavx_router_t* m_router;
479 };
480  
481 /**
482 * RAII Frame Synchronizer & Clock Locker
483 */
484 class FrameSync {
485 public:
486 FrameSync() : m_fs(nullptr) {}
487  
488 explicit FrameSync(Stream& stream) {
489 m_fs = oavx_framesync_create(stream.get());
490 }
491  
492 ~FrameSync() {
493 if (m_fs) {
494 oavx_framesync_destroy(m_fs);
495 m_fs = nullptr;
496 }
497 }
498  
499 bool is_valid() const { return m_fs != nullptr; }
500 explicit operator bool() const { return is_valid(); }
501 oavx_framesync_t* get() const { return m_fs; }
502  
503 FrameSync(const FrameSync&) = delete;
504 FrameSync& operator=(const FrameSync&) = delete;
505  
506 FrameSync(FrameSync&& other) noexcept : m_fs(other.m_fs) {
507 other.m_fs = nullptr;
508 }
509  
510 FrameSync& operator=(FrameSync&& other) noexcept {
511 if (this != &other) {
512 if (m_fs) {
513 oavx_framesync_destroy(m_fs);
514 }
515 m_fs = other.m_fs;
516 other.m_fs = nullptr;
517 }
518 return *this;
519 }
520  
521 /**
522 * Pulls latest synchronized video frame. Must call free_video() when finished.
523 */
524 void capture_video(oavx_video_frame_t& frame, int field_type = 0) {
525 oavx_framesync_capture_video(m_fs, &frame, field_type);
526 }
527  
528 void free_video(oavx_video_frame_t& frame) {
529 oavx_framesync_free_video(m_fs, &frame);
530 }
531  
532 void capture_audio(oavx_audio_frame_t& frame, uint32_t sample_rate, uint16_t channels, uint32_t num_samples) {
533 oavx_framesync_capture_audio(m_fs, &frame, sample_rate, channels, num_samples);
534 }
535  
536 void free_audio(oavx_audio_frame_t& frame) {
537 oavx_framesync_free_audio(m_fs, &frame);
538 }
539  
540 int audio_queue_depth() const {
541 return oavx_framesync_audio_queue_depth(m_fs);
542 }
543  
544 private:
545 oavx_framesync_t* m_fs;
546 };
547  
548 /* Unified Media Utilities */
549 inline void audio_to_interleaved_16s(const oavx_audio_frame_t* src, oavx_audio_frame_interleaved_16s_t* dst) {
550 oavx_util_audio_to_interleaved_16s(src, dst);
551 }
552 inline void audio_from_interleaved_16s(const oavx_audio_frame_interleaved_16s_t* src, oavx_audio_frame_t* dst) {
553 oavx_util_audio_from_interleaved_16s(src, dst);
554 }
555 inline void audio_to_interleaved_32f(const oavx_audio_frame_t* src, oavx_audio_frame_interleaved_32f_t* dst) {
556 oavx_util_audio_to_interleaved_32f(src, dst);
557 }
558 inline void audio_from_interleaved_32f(const oavx_audio_frame_interleaved_32f_t* src, oavx_audio_frame_t* dst) {
559 oavx_util_audio_from_interleaved_32f(src, dst);
560 }
561 inline void audio_to_interleaved_32s(const oavx_audio_frame_t* src, oavx_audio_frame_interleaved_32s_t* dst) {
562 oavx_util_audio_to_interleaved_32s(src, dst);
563 }
564 inline void audio_from_interleaved_32s(const oavx_audio_frame_interleaved_32s_t* src, oavx_audio_frame_t* dst) {
565 oavx_util_audio_from_interleaved_32s(src, dst);
566 }
567 inline void V210_to_P216(const oavx_video_frame_t* src, oavx_video_frame_t* dst) {
568 oavx_util_V210_to_P216(src, dst);
569 }
570 inline void P216_to_V210(const oavx_video_frame_t* src, oavx_video_frame_t* dst) {
571 oavx_util_P216_to_V210(src, dst);
572 }
573  
574 /* SpeedHQ Dynamic Quantization & Bitrate Control */
575 inline void speedhq_set_quality(uint8_t quality) {
576 oavx_speedhq_set_quality(quality);
577 }
578  
579 inline uint8_t speedhq_get_quality() {
580 return oavx_speedhq_get_quality();
581 }
582  
583 } // namespace oavx
584  
585 #endif /* OPENAVX_HPP */