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 * src/avx_virtcam_ipc.c - Cross-platform shared memory IPC for Virtual Camera 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  
18 #include "openavx.h"
19 #include "avx_virtcam_ipc.h"
20 #include "avx_platform.h"
21  
22 #if defined(AVX_OS_WINDOWS)
23 #include <windows.h>
24 #elif defined(AVX_OS_POSIX)
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #endif
30  
31 struct avx_virtcam_producer_t {
32 avx_virtcam_shm_layout_t *layout;
33 size_t map_size;
34 #if defined(AVX_OS_WINDOWS)
35 HANDLE map_handle;
36 #elif defined(AVX_OS_POSIX)
37 int shm_fd;
38 #endif
39 };
40  
41 struct avx_virtcam_consumer_t {
42 avx_virtcam_shm_layout_t *layout;
43 size_t map_size;
44 #if defined(AVX_OS_WINDOWS)
45 HANDLE map_handle;
46 #elif defined(AVX_OS_POSIX)
47 int shm_fd;
48 #endif
49 };
50  
51 /* ========================================================================= */
52 /* 1. IPC PRODUCER (openavx-virtcam engine) */
53 /* ========================================================================= */
54  
55 /* Creates cross-platform shared memory mapping for virtual camera plugin IPC (POSIX shm / Win32 file mapping). */
56 avx_virtcam_producer_t *avx_virtcam_producer_create(void)
57 {
58 avx_virtcam_producer_t *prod = (avx_virtcam_producer_t*)calloc(1, sizeof(avx_virtcam_producer_t));
59 if (!prod) return NULL;
60  
61 prod->map_size = sizeof(avx_virtcam_shm_layout_t);
62  
63 #if defined(AVX_OS_WINDOWS)
64 prod->map_handle = CreateFileMappingA(
65 INVALID_HANDLE_VALUE,
66 NULL,
67 PAGE_READWRITE,
68 (DWORD)((prod->map_size >> 32) & 0xFFFFFFFFU),
69 (DWORD)(prod->map_size & 0xFFFFFFFFU),
70 AVX_VIRTCAM_WIN_SHM_NAME
71 );
72 if (!prod->map_handle) {
73 free(prod);
74 return NULL;
75 }
76 prod->layout = (avx_virtcam_shm_layout_t*)MapViewOfFile(
77 prod->map_handle,
78 FILE_MAP_ALL_ACCESS,
79 0, 0,
80 prod->map_size
81 );
82 if (!prod->layout) {
83 CloseHandle(prod->map_handle);
84 free(prod);
85 return NULL;
86 }
87 #elif defined(AVX_OS_POSIX)
88 prod->shm_fd = shm_open(AVX_VIRTCAM_POSIX_SHM_NAME, O_CREAT | O_RDWR, 0666);
89 if (prod->shm_fd < 0) {
90 free(prod);
91 return NULL;
92 }
93 if (ftruncate(prod->shm_fd, (off_t)prod->map_size) < 0) {
94 close(prod->shm_fd);
95 shm_unlink(AVX_VIRTCAM_POSIX_SHM_NAME);
96 free(prod);
97 return NULL;
98 }
99 prod->layout = (avx_virtcam_shm_layout_t*)mmap(
100 NULL,
101 prod->map_size,
102 PROT_READ | PROT_WRITE,
103 MAP_SHARED,
104 prod->shm_fd,
105 0
106 );
107 if (prod->layout == MAP_FAILED) {
108 close(prod->shm_fd);
109 shm_unlink(AVX_VIRTCAM_POSIX_SHM_NAME);
110 free(prod);
111 return NULL;
112 }
113 #endif
114  
115 memset(prod->layout, 0, prod->map_size);
116 prod->layout->header.magic = AVX_VIRTCAM_MAGIC;
117 prod->layout->header.version = AVX_VIRTCAM_VERSION;
118 return prod;
119 }
120  
121 void avx_virtcam_producer_destroy(avx_virtcam_producer_t *prod)
122 {
123 if (!prod) return;
124  
125 #if defined(AVX_OS_WINDOWS)
126 if (prod->layout) {
127 UnmapViewOfFile(prod->layout);
128 prod->layout = NULL;
129 }
130 if (prod->map_handle) {
131 CloseHandle(prod->map_handle);
132 prod->map_handle = NULL;
133 }
134 #elif defined(AVX_OS_POSIX)
135 if (prod->layout && prod->layout != MAP_FAILED) {
136 munmap(prod->layout, prod->map_size);
137 prod->layout = NULL;
138 }
139 if (prod->shm_fd >= 0) {
140 close(prod->shm_fd);
141 prod->shm_fd = -1;
142 }
143 shm_unlink(AVX_VIRTCAM_POSIX_SHM_NAME);
144 #endif
145  
146 free(prod);
147 }
148  
149 /* Publishes video frame into shared memory layout with sequence counter and memory barrier. */
150 bool avx_virtcam_producer_write_frame(
151 avx_virtcam_producer_t *prod,
152 const uint8_t *data,
153 size_t size,
154 uint32_t width,
155 uint32_t height,
156 uint32_t stride,
157 uint32_t fourcc,
158 uint32_t fps_num,
159 uint32_t fps_den
160 ) {
161 if (!prod || !prod->layout || !data || size == 0 || size > AVX_VIRTCAM_MAX_FRAME_SIZE) {
162 return false;
163 }
164  
165 avx_virtcam_shm_layout_t *lay = prod->layout;
166  
167 memcpy(lay->pixels, data, size);
168  
169 lay->header.width = width;
170 lay->header.height = height;
171 lay->header.stride = (stride > 0) ? stride : (width * 2);
172 lay->header.fourcc = fourcc;
173 lay->header.fps_num = (fps_num > 0) ? fps_num : 30;
174 lay->header.fps_den = (fps_den > 0) ? fps_den : 1;
175 lay->header.data_size = (uint32_t)size;
176 lay->header.timestamp_ns = avx_time_ns();
177 lay->header.sequence++;
178  
179 return true;
180 }
181  
182 bool avx_virtcam_producer_has_clients(const avx_virtcam_producer_t *prod)
183 {
184 return (prod && prod->layout) ? (prod->layout->header.clients_active > 0) : false;
185 }
186  
187 /* ========================================================================= */
188 /* 2. IPC CONSUMER (DirectShow DLL / CoreMediaIO DAL Plugin) */
189 /* ========================================================================= */
190  
191 avx_virtcam_consumer_t *avx_virtcam_consumer_create(void)
192 {
193 avx_virtcam_consumer_t *cons = (avx_virtcam_consumer_t*)calloc(1, sizeof(avx_virtcam_consumer_t));
194 if (!cons) return NULL;
195  
196 cons->map_size = sizeof(avx_virtcam_shm_layout_t);
197  
198 #if defined(AVX_OS_WINDOWS)
199 cons->map_handle = OpenFileMappingA(
200 FILE_MAP_READ | FILE_MAP_WRITE,
201 FALSE,
202 AVX_VIRTCAM_WIN_SHM_NAME
203 );
204 if (!cons->map_handle) {
205 free(cons);
206 return NULL;
207 }
208 cons->layout = (avx_virtcam_shm_layout_t*)MapViewOfFile(
209 cons->map_handle,
210 FILE_MAP_READ | FILE_MAP_WRITE,
211 0, 0,
212 cons->map_size
213 );
214 if (!cons->layout) {
215 CloseHandle(cons->map_handle);
216 free(cons);
217 return NULL;
218 }
219 #elif defined(AVX_OS_POSIX)
220 cons->shm_fd = shm_open(AVX_VIRTCAM_POSIX_SHM_NAME, O_RDWR, 0666);
221 if (cons->shm_fd < 0) {
222 free(cons);
223 return NULL;
224 }
225 cons->layout = (avx_virtcam_shm_layout_t*)mmap(
226 NULL,
227 cons->map_size,
228 PROT_READ | PROT_WRITE,
229 MAP_SHARED,
230 cons->shm_fd,
231 0
232 );
233 if (cons->layout == MAP_FAILED) {
234 close(cons->shm_fd);
235 free(cons);
236 return NULL;
237 }
238 #endif
239  
240 if (cons->layout->header.magic != AVX_VIRTCAM_MAGIC) {
241 avx_virtcam_consumer_destroy(cons);
242 return NULL;
243 }
244  
245 /* Increment active client counter */
246 cons->layout->header.clients_active++;
247 return cons;
248 }
249  
250 void avx_virtcam_consumer_destroy(avx_virtcam_consumer_t *cons)
251 {
252 if (!cons) return;
253  
254 if (cons->layout && cons->layout != (void*)-1) {
255 if (cons->layout->header.clients_active > 0) {
256 cons->layout->header.clients_active--;
257 }
258 #if defined(AVX_OS_WINDOWS)
259 UnmapViewOfFile(cons->layout);
260 cons->layout = NULL;
261 #elif defined(AVX_OS_POSIX)
262 munmap(cons->layout, cons->map_size);
263 cons->layout = NULL;
264 #endif
265 }
266  
267 #if defined(AVX_OS_WINDOWS)
268 if (cons->map_handle) {
269 CloseHandle(cons->map_handle);
270 cons->map_handle = NULL;
271 }
272 #elif defined(AVX_OS_POSIX)
273 if (cons->shm_fd >= 0) {
274 close(cons->shm_fd);
275 cons->shm_fd = -1;
276 }
277 #endif
278  
279 free(cons);
280 }
281  
282 /* Reads video frame from shared memory using sequence check for tear-free lockless consumption. */
283 bool avx_virtcam_consumer_read_frame(
284 avx_virtcam_consumer_t *cons,
285 avx_virtcam_shm_header_t *out_header,
286 uint8_t *out_buffer,
287 size_t buffer_max,
288 uint64_t *inout_last_seq
289 ) {
290 if (!cons || !cons->layout || !out_header || !out_buffer) return false;
291  
292 avx_virtcam_shm_layout_t *lay = cons->layout;
293 if (lay->header.magic != AVX_VIRTCAM_MAGIC) return false;
294  
295 uint64_t cur_seq = lay->header.sequence;
296 if (cur_seq == 0 || (inout_last_seq && cur_seq == *inout_last_seq)) {
297 return false; /* No new frame yet */
298 }
299  
300 uint32_t size = lay->header.data_size;
301 if (size > buffer_max || size == 0) return false;
302  
303 memcpy(out_header, &lay->header, sizeof(avx_virtcam_shm_header_t));
304 memcpy(out_buffer, lay->pixels, size);
305  
306 if (inout_last_seq) {
307 *inout_last_seq = cur_seq;
308 }
309 return true;
310 }