Raw ↗
+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
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_virtcam_ipc_test.c - Unit tests for Cross-Platform Virtual Camera IPC & Plugins
7 */
8  
9 #define _DEFAULT_SOURCE
10 #define _POSIX_C_SOURCE 200809L
11  
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdint.h>
15 #include <stdbool.h>
16 #include <string.h>
17 #include <assert.h>
18  
19 #include "openavx.h"
20 #include "avx_virtcam_ipc.h"
21 #include "avx_dshow_sink.h"
22 #include "avx_dal_sink.h"
23  
24 static void test_virtcam_ipc_roundtrip(void)
25 {
26 printf("[test] Testing cross-platform virtual camera shared memory IPC...\n");
27  
28 avx_virtcam_producer_t *prod = avx_virtcam_producer_create();
29 assert(prod != NULL);
30 assert(!avx_virtcam_producer_has_clients(prod));
31  
32 avx_virtcam_consumer_t *cons = avx_virtcam_consumer_create();
33 assert(cons != NULL);
34 assert(avx_virtcam_producer_has_clients(prod));
35  
36 /* Create sample 640x360 test frame */
37 uint32_t width = 640;
38 uint32_t height = 360;
39 uint32_t stride = width * 2;
40 size_t frame_bytes = stride * height;
41 uint8_t *src_pixels = (uint8_t*)malloc(frame_bytes);
42 assert(src_pixels != NULL);
43 memset(src_pixels, 0x42, frame_bytes);
44  
45 /* Write frame from producer */
46 bool wrote = avx_virtcam_producer_write_frame(
47 prod,
48 src_pixels,
49 frame_bytes,
50 width,
51 height,
52 stride,
53 OAVX_FOURCC_VIDEO_TYPE_UYVY,
54 30,
55 1
56 );
57 assert(wrote);
58  
59 /* Read frame from consumer */
60 avx_virtcam_shm_header_t hdr;
61 memset(&hdr, 0, sizeof(hdr));
62 uint8_t *dst_pixels = (uint8_t*)malloc(frame_bytes);
63 assert(dst_pixels != NULL);
64 uint64_t last_seq = 0;
65  
66 bool read_ok = avx_virtcam_consumer_read_frame(cons, &hdr, dst_pixels, frame_bytes, &last_seq);
67 assert(read_ok);
68 assert(hdr.magic == AVX_VIRTCAM_MAGIC);
69 assert(hdr.width == width);
70 assert(hdr.height == height);
71 assert(hdr.fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVY);
72 assert(hdr.sequence == 1);
73 assert(last_seq == 1);
74 assert(memcmp(src_pixels, dst_pixels, frame_bytes) == 0);
75  
76 /* Read again with same sequence must return false (no new frame) */
77 assert(!avx_virtcam_consumer_read_frame(cons, &hdr, dst_pixels, frame_bytes, &last_seq));
78  
79 /* Write second frame */
80 memset(src_pixels, 0x88, frame_bytes);
81 assert(avx_virtcam_producer_write_frame(prod, src_pixels, frame_bytes, width, height, stride, OAVX_FOURCC_VIDEO_TYPE_UYVY, 60, 1));
82 assert(avx_virtcam_consumer_read_frame(cons, &hdr, dst_pixels, frame_bytes, &last_seq));
83 assert(hdr.sequence == 2);
84 assert(last_seq == 2);
85 assert(hdr.fps_num == 60);
86 assert(dst_pixels[0] == 0x88);
87  
88 /* Cleanup consumer */
89 avx_virtcam_consumer_destroy(cons);
90 assert(!avx_virtcam_producer_has_clients(prod));
91  
92 /* Cleanup producer */
93 avx_virtcam_producer_destroy(prod);
94 free(src_pixels);
95 free(dst_pixels);
96 printf(" -> Virtual camera IPC roundtrip passed cleanly.\n");
97 }
98  
99 static void test_virtcam_plugins_defensive(void)
100 {
101 printf("[test] Testing platform virtual camera registration defensive checks...\n");
102 avx_virtcam_producer_destroy(NULL);
103 avx_virtcam_consumer_destroy(NULL);
104 assert(!avx_virtcam_producer_write_frame(NULL, NULL, 0, 0, 0, 0, 0, 0, 0));
105 assert(!avx_virtcam_consumer_read_frame(NULL, NULL, NULL, 0, NULL));
106  
107 /* Check graceful behavior of platform APIs on current OS */
108 #if defined(AVX_OS_WINDOWS)
109 printf(" -> Running on Windows: testing DirectShow registration...\n");
110 #elif defined(AVX_OS_MACOS)
111 printf(" -> Running on macOS: testing DAL plugin check...\n");
112 #else
113 printf(" -> Running on Linux: checking Windows and macOS stubs return false safely...\n");
114 assert(!avx_dshow_is_registered());
115 assert(!avx_dal_is_installed());
116 #endif
117 printf(" -> Defensive checks passed.\n");
118 }
119  
120 int main(void)
121 {
122 printf("===================================================\n");
123 printf("OpenAVX Virtual Camera IPC & Plugins Test Suite\n");
124 printf("===================================================\n");
125  
126 test_virtcam_ipc_roundtrip();
127 test_virtcam_plugins_defensive();
128  
129 printf("===================================================\n");
130 printf("All Virtual Camera IPC & Plugin Tests Passed!\n");
131 printf("===================================================\n");
132 return 0;
133 }