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_pipewire_sink.c - Native user-space PipeWire virtual camera sink
7 */
8  
9 #define _GNU_SOURCE
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdint.h>
13 #include <stdbool.h>
14 #include <string.h>
15  
16 #include "openavx.h"
17 #include "avx_pipewire_sink.h"
18 #include "avx_color.h"
19 #include "avx_platform.h"
20  
21 #if defined(AVX_OS_LINUX)
22 #include <dlfcn.h>
23 #include <pthread.h>
24  
25 /* Include PipeWire & SPA headers (either system or OpenAVX compat) */
26 #include <pipewire/pipewire.h>
27 #include <spa/param/video/raw.h>
28 #include <spa/param/video/raw-utils.h>
29 #include <spa/pod/builder.h>
30  
31 /* Dynamic PipeWire library loader table */
32 typedef struct {
33 void *handle;
34 void (*pw_init)(int *argc, char **argv[]);
35 void (*pw_deinit)(void);
36 const char *(*pw_get_library_version)(void);
37 struct pw_thread_loop *(*pw_thread_loop_new)(const char *name, const struct spa_dict *props);
38 void (*pw_thread_loop_destroy)(struct pw_thread_loop *loop);
39 struct pw_loop *(*pw_thread_loop_get_loop)(struct pw_thread_loop *loop);
40 int (*pw_thread_loop_start)(struct pw_thread_loop *loop);
41 void (*pw_thread_loop_stop)(struct pw_thread_loop *loop);
42 void (*pw_thread_loop_lock)(struct pw_thread_loop *loop);
43 void (*pw_thread_loop_unlock)(struct pw_thread_loop *loop);
44 void (*pw_thread_loop_signal)(struct pw_thread_loop *loop, bool wait_for_accept);
45 struct pw_properties *(*pw_properties_new)(const char *key, ...);
46 int (*pw_properties_set)(struct pw_properties *properties, const char *key, const char *val);
47 struct pw_stream *(*pw_stream_new_simple)(
48 struct pw_loop *loop,
49 const char *name,
50 struct pw_properties *props,
51 const struct pw_stream_events *events,
52 void *data);
53 void (*pw_stream_destroy)(struct pw_stream *stream);
54 int (*pw_stream_connect)(
55 struct pw_stream *stream,
56 enum pw_direction direction,
57 uint32_t target_id,
58 enum pw_stream_flags flags,
59 const struct spa_pod **params,
60 uint32_t n_params);
61 int (*pw_stream_disconnect)(struct pw_stream *stream);
62 enum pw_stream_state (*pw_stream_get_state)(struct pw_stream *stream, const char **error);
63 struct pw_buffer *(*pw_stream_dequeue_buffer)(struct pw_stream *stream);
64 int (*pw_stream_queue_buffer)(struct pw_stream *stream, struct pw_buffer *buffer);
65 int (*pw_stream_update_params)(struct pw_stream *stream, const struct spa_pod **params, uint32_t n_params);
66 } avx_pw_table_t;
67  
68 static avx_pw_table_t g_pw;
69 static pthread_once_t g_pw_init_once = PTHREAD_ONCE_INIT;
70 static bool g_pw_loaded = false;
71  
72 /* Dynamically binds libpipewire-0.3 symbols at runtime without compile-time hard linking. */
73 static void avx_pipewire_do_init(void) {
74 g_pw.handle = dlopen("libpipewire-0.3.so.0", RTLD_NOW | RTLD_GLOBAL);
75 if (!g_pw.handle) {
76 g_pw.handle = dlopen("libpipewire-0.3.so", RTLD_NOW | RTLD_GLOBAL);
77 }
78 if (!g_pw.handle) {
79 g_pw_loaded = false;
80 return;
81 }
82  
83 #define LOAD_SYM(name) do { \
84 g_pw.name = dlsym(g_pw.handle, #name); \
85 if (!g_pw.name) { \
86 dlclose(g_pw.handle); \
87 g_pw.handle = NULL; \
88 g_pw_loaded = false; \
89 return; \
90 } \
91 } while(0)
92  
93 LOAD_SYM(pw_init);
94 LOAD_SYM(pw_deinit);
95 LOAD_SYM(pw_get_library_version);
96 LOAD_SYM(pw_thread_loop_new);
97 LOAD_SYM(pw_thread_loop_destroy);
98 LOAD_SYM(pw_thread_loop_get_loop);
99 LOAD_SYM(pw_thread_loop_start);
100 LOAD_SYM(pw_thread_loop_stop);
101 LOAD_SYM(pw_thread_loop_lock);
102 LOAD_SYM(pw_thread_loop_unlock);
103 LOAD_SYM(pw_thread_loop_signal);
104 LOAD_SYM(pw_properties_new);
105 LOAD_SYM(pw_properties_set);
106 LOAD_SYM(pw_stream_new_simple);
107 LOAD_SYM(pw_stream_destroy);
108 LOAD_SYM(pw_stream_connect);
109 LOAD_SYM(pw_stream_disconnect);
110 LOAD_SYM(pw_stream_get_state);
111 LOAD_SYM(pw_stream_dequeue_buffer);
112 LOAD_SYM(pw_stream_queue_buffer);
113 LOAD_SYM(pw_stream_update_params);
114 #undef LOAD_SYM
115  
116 g_pw.pw_init(NULL, NULL);
117 g_pw_loaded = true;
118 }
119  
120 static bool avx_pipewire_ensure_loaded(void) {
121 pthread_once(&g_pw_init_once, avx_pipewire_do_init);
122 return g_pw_loaded;
123 }
124  
125 /* PipeWire virtual camera sink internal state */
126 struct avx_pipewire_sink_t {
127 struct pw_thread_loop *loop;
128 struct pw_stream *stream;
129 struct spa_hook stream_listener;
130
131 uint32_t width;
132 uint32_t height;
133 uint32_t fps_num;
134 uint32_t fps_den;
135 uint32_t format; /* SPA_VIDEO_FORMAT_YUY2 or SPA_VIDEO_FORMAT_UYVY */
136
137 bool is_active;
138 bool is_streaming;
139
140 /* Conversion buffer for UYVY <-> YUY2 */
141 uint8_t *conv_buf;
142 size_t conv_buf_cap;
143
144 avx_mutex_t lock;
145 };
146  
147 /* Fast SIMD / bitwise conversion of UYVY (U Y0 V Y1) to YUYV (Y0 U Y1 V) */
148 static void uyvy_to_yuyv_convert(const uint8_t *src, uint8_t *dst, size_t pixel_count)
149 {
150 size_t word_count = pixel_count / 2;
151 const uint32_t *src32 = (const uint32_t*)src;
152 uint32_t *dst32 = (uint32_t*)dst;
153  
154 for (size_t i = 0; i < word_count; i++) {
155 uint32_t in = src32[i];
156 uint32_t u = (in & 0x000000FFU);
157 uint32_t y0 = (in & 0x0000FF00U) >> 8;
158 uint32_t v = (in & 0x00FF0000U) >> 16;
159 uint32_t y1 = (in & 0xFF000000U) >> 24;
160 dst32[i] = y0 | (u << 8) | (y1 << 16) | (v << 24);
161 }
162 }
163  
164 /* PipeWire Stream Event Callbacks */
165 static void on_stream_state_changed(void *data, enum pw_stream_state old, enum pw_stream_state state, const char *error)
166 {
167 avx_pipewire_sink_t *sink = (avx_pipewire_sink_t*)data;
168 (void)old;
169 if (!sink) return;
170  
171 if (state == PW_STREAM_STATE_STREAMING) {
172 sink->is_streaming = true;
173 sink->is_active = true;
174 oavx_log_write(OAVX_LOG_INFO, "pipewire", "Virtual camera stream active and streaming");
175 } else if (state == PW_STREAM_STATE_PAUSED) {
176 sink->is_streaming = false;
177 sink->is_active = true;
178 oavx_log_write(OAVX_LOG_DEBUG, "pipewire", "Virtual camera stream paused (ready)");
179 } else if (state == PW_STREAM_STATE_ERROR) {
180 sink->is_streaming = false;
181 sink->is_active = false;
182 oavx_log_write(OAVX_LOG_ERROR, "pipewire", "Virtual camera stream error: %s", error ? error : "unknown");
183 } else {
184 sink->is_streaming = false;
185 sink->is_active = false;
186 }
187 }
188  
189 static void on_stream_param_changed(void *data, uint32_t id, const struct spa_pod *param)
190 {
191 avx_pipewire_sink_t *sink = (avx_pipewire_sink_t*)data;
192 if (!sink || !param || id != SPA_PARAM_Format) return;
193  
194 struct spa_video_info_raw info;
195 if (spa_format_video_raw_parse(param, &info) < 0) return;
196  
197 sink->width = info.size.width;
198 sink->height = info.size.height;
199 if (info.framerate.denom > 0) {
200 sink->fps_num = info.framerate.num;
201 sink->fps_den = info.framerate.denom;
202 }
203 sink->format = info.format;
204  
205 oavx_log_write(OAVX_LOG_INFO, "pipewire", "Negotiated format: %ux%u @ %u/%u fps (SPA format: %u)",
206 sink->width, sink->height, sink->fps_num, sink->fps_den, sink->format);
207 }
208  
209 static const struct pw_stream_events k_stream_events = {
210 PW_VERSION_STREAM_EVENTS,
211 .state_changed = on_stream_state_changed,
212 .param_changed = on_stream_param_changed,
213 };
214  
215 bool avx_pipewire_is_available(void)
216 {
217 return avx_pipewire_ensure_loaded();
218 }
219  
220 /* Spawns PipeWire thread loop and registers virtual camera stream with SPA video POD format negotiation. */
221 avx_pipewire_sink_t *avx_pipewire_sink_create(const avx_pipewire_sink_config_t *config)
222 {
223 if (!avx_pipewire_ensure_loaded()) {
224 oavx_log_write(OAVX_LOG_WARN, "pipewire", "PipeWire library (libpipewire-0.3.so.0) not found on system");
225 return NULL;
226 }
227  
228 avx_pipewire_sink_t *sink = (avx_pipewire_sink_t*)calloc(1, sizeof(avx_pipewire_sink_t));
229 if (!sink) return NULL;
230  
231 sink->width = (config && config->width > 0) ? config->width : 1920;
232 sink->height = (config && config->height > 0) ? config->height : 1080;
233 sink->fps_num = (config && config->fps_num > 0) ? config->fps_num : 30;
234 sink->fps_den = (config && config->fps_den > 0) ? config->fps_den : 1;
235 sink->format = SPA_VIDEO_FORMAT_YUY2;
236  
237 sink->conv_buf_cap = (size_t)sink->width * sink->height * 4;
238 sink->conv_buf = (uint8_t*)malloc(sink->conv_buf_cap);
239 if (!sink->conv_buf) {
240 free(sink);
241 return NULL;
242 }
243 avx_mutex_init(&sink->lock);
244  
245 sink->loop = g_pw.pw_thread_loop_new("openavx-virtcam-loop", NULL);
246 if (!sink->loop) {
247 oavx_log_write(OAVX_LOG_ERROR, "pipewire", "Failed to create PipeWire thread loop");
248 free(sink->conv_buf);
249 free(sink);
250 return NULL;
251 }
252  
253 const char *node_name = (config && config->node_name) ? config->node_name : "OpenAVX Virtual Camera";
254 const char *node_desc = (config && config->node_description) ? config->node_description : "OpenAVX Virtual Camera Feed";
255  
256 /* Create PipeWire node properties conforming to standard Linux video capture specifications */
257 struct pw_properties *props = g_pw.pw_properties_new(
258 PW_KEY_MEDIA_TYPE, "Video",
259 PW_KEY_MEDIA_CATEGORY, "Capture",
260 PW_KEY_MEDIA_ROLE, "Camera",
261 "media.class", "Video/Source",
262 "node.name", node_name,
263 "node.description", node_desc,
264 "node.nick", "OpenAVX Cam",
265 NULL
266 );
267  
268 sink->stream = g_pw.pw_stream_new_simple(
269 g_pw.pw_thread_loop_get_loop(sink->loop),
270 "openavx-camera-stream",
271 props,
272 &k_stream_events,
273 sink
274 );
275  
276 if (!sink->stream) {
277 oavx_log_write(OAVX_LOG_ERROR, "pipewire", "Failed to create PipeWire stream");
278 g_pw.pw_thread_loop_destroy(sink->loop);
279 free(sink->conv_buf);
280 free(sink);
281 return NULL;
282 }
283  
284 /* Build supported format parameters: YUY2 (primary) and UYVY */
285 uint8_t b1_buf[1024];
286 uint8_t b2_buf[1024];
287 struct spa_pod_builder b1 = SPA_POD_BUILDER_INIT(b1_buf, sizeof(b1_buf));
288 struct spa_pod_builder b2 = SPA_POD_BUILDER_INIT(b2_buf, sizeof(b2_buf));
289  
290 struct spa_video_info_raw info_yuy2;
291 spa_zero(info_yuy2);
292 info_yuy2.format = SPA_VIDEO_FORMAT_YUY2;
293 info_yuy2.size.width = sink->width;
294 info_yuy2.size.height = sink->height;
295 info_yuy2.framerate.num = sink->fps_num;
296 info_yuy2.framerate.denom = sink->fps_den;
297  
298 struct spa_video_info_raw info_uyvy;
299 spa_zero(info_uyvy);
300 info_uyvy.format = SPA_VIDEO_FORMAT_UYVY;
301 info_uyvy.size.width = sink->width;
302 info_uyvy.size.height = sink->height;
303 info_uyvy.framerate.num = sink->fps_num;
304 info_uyvy.framerate.denom = sink->fps_den;
305  
306 const struct spa_pod *params[2];
307 params[0] = spa_format_video_raw_build(&b1, SPA_PARAM_EnumFormat, &info_yuy2);
308 params[1] = spa_format_video_raw_build(&b2, SPA_PARAM_EnumFormat, &info_uyvy);
309  
310 g_pw.pw_thread_loop_lock(sink->loop);
311 int rc = g_pw.pw_stream_connect(
312 sink->stream,
313 PW_DIRECTION_OUTPUT,
314 PW_ID_ANY,
315 PW_STREAM_FLAG_DRIVER | PW_STREAM_FLAG_MAP_BUFFERS,
316 params,
317 2
318 );
319 g_pw.pw_thread_loop_unlock(sink->loop);
320  
321 if (rc < 0) {
322 oavx_log_write(OAVX_LOG_ERROR, "pipewire", "pw_stream_connect failed (code: %d)", rc);
323 g_pw.pw_stream_destroy(sink->stream);
324 g_pw.pw_thread_loop_destroy(sink->loop);
325 free(sink->conv_buf);
326 free(sink);
327 return NULL;
328 }
329  
330 if (g_pw.pw_thread_loop_start(sink->loop) < 0) {
331 oavx_log_write(OAVX_LOG_ERROR, "pipewire", "Failed to start PipeWire thread loop");
332 g_pw.pw_stream_destroy(sink->stream);
333 g_pw.pw_thread_loop_destroy(sink->loop);
334 free(sink->conv_buf);
335 free(sink);
336 return NULL;
337 }
338  
339 oavx_log_write(OAVX_LOG_INFO, "pipewire", "Initialized user-space virtual camera '%s' (PipeWire %s)",
340 node_name, g_pw.pw_get_library_version());
341 return sink;
342 }
343  
344 void avx_pipewire_sink_destroy(avx_pipewire_sink_t *sink)
345 {
346 if (!sink) return;
347  
348 if (sink->loop) {
349 g_pw.pw_thread_loop_stop(sink->loop);
350 }
351 if (sink->stream) {
352 g_pw.pw_stream_destroy(sink->stream);
353 sink->stream = NULL;
354 }
355 if (sink->loop) {
356 g_pw.pw_thread_loop_destroy(sink->loop);
357 sink->loop = NULL;
358 }
359  
360 avx_mutex_destroy(&sink->lock);
361 if (sink->conv_buf) {
362 free(sink->conv_buf);
363 sink->conv_buf = NULL;
364 }
365 free(sink);
366 }
367  
368 /* Dequeues PipeWire shared memory buffer, writes converted video frame, and queues for client consumption. */
369 bool avx_pipewire_sink_push_frame(
370 avx_pipewire_sink_t *sink,
371 const uint8_t *frame_data,
372 size_t frame_size,
373 uint32_t width,
374 uint32_t height,
375 uint32_t fourcc)
376 {
377 if (!sink || !sink->stream || !frame_data || frame_size == 0) return false;
378  
379 g_pw.pw_thread_loop_lock(sink->loop);
380 struct pw_buffer *b = g_pw.pw_stream_dequeue_buffer(sink->stream);
381 if (!b) {
382 g_pw.pw_thread_loop_unlock(sink->loop);
383 return false;
384 }
385  
386 struct spa_buffer *buf = b->buffer;
387 if (!buf || buf->n_datas == 0 || !buf->datas[0].data) {
388 g_pw.pw_stream_queue_buffer(sink->stream, b);
389 g_pw.pw_thread_loop_unlock(sink->loop);
390 return false;
391 }
392  
393 size_t target_stride = width * 2;
394 size_t target_size = target_stride * height;
395  
396 if (buf->datas[0].maxsize < target_size) {
397 g_pw.pw_stream_queue_buffer(sink->stream, b);
398 g_pw.pw_thread_loop_unlock(sink->loop);
399 return false;
400 }
401  
402 uint8_t *dst = (uint8_t*)buf->datas[0].data;
403  
404 if (sink->format == SPA_VIDEO_FORMAT_YUY2) {
405 if (fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVY) {
406 uyvy_to_yuyv_convert(frame_data, dst, (size_t)width * height);
407 } else if (fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBA || fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBX) {
408 if (sink->conv_buf_cap >= target_size * 2) {
409 uint8_t *tmp_uyvy = sink->conv_buf;
410 avx_color_rgba_to_uyvy(frame_data, tmp_uyvy, (int)width, (int)height);
411 uyvy_to_yuyv_convert(tmp_uyvy, dst, (size_t)width * height);
412 }
413 } else if (fourcc == OAVX_FOURCC_VIDEO_TYPE_BGRA || fourcc == OAVX_FOURCC_VIDEO_TYPE_BGRX) {
414 if (sink->conv_buf_cap >= target_size * 2) {
415 uint8_t *tmp_uyvy = sink->conv_buf;
416 avx_color_bgra_to_uyvy(frame_data, tmp_uyvy, (int)width, (int)height);
417 uyvy_to_yuyv_convert(tmp_uyvy, dst, (size_t)width * height);
418 }
419 } else {
420 /* Direct raw copy fallback */
421 memcpy(dst, frame_data, frame_size < target_size ? frame_size : target_size);
422 }
423 } else {
424 /* Direct UYVY or generic match */
425 if (fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVY) {
426 memcpy(dst, frame_data, target_size);
427 } else if (fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBA) {
428 avx_color_rgba_to_uyvy(frame_data, dst, (int)width, (int)height);
429 } else if (fourcc == OAVX_FOURCC_VIDEO_TYPE_BGRA) {
430 avx_color_bgra_to_uyvy(frame_data, dst, (int)width, (int)height);
431 } else {
432 memcpy(dst, frame_data, frame_size < target_size ? frame_size : target_size);
433 }
434 }
435  
436 buf->datas[0].chunk->offset = 0;
437 buf->datas[0].chunk->size = target_size;
438 buf->datas[0].chunk->stride = target_stride;
439  
440 g_pw.pw_stream_queue_buffer(sink->stream, b);
441 g_pw.pw_thread_loop_unlock(sink->loop);
442 return true;
443 }
444  
445 bool avx_pipewire_sink_is_active(const avx_pipewire_sink_t *sink)
446 {
447 return sink ? (sink->is_active || sink->is_streaming) : false;
448 }
449  
450 #else /* Non-Linux or systems without PipeWire support */
451  
452 bool avx_pipewire_is_available(void) { return false; }
453 /* Spawns PipeWire thread loop and registers virtual camera stream with SPA video POD format negotiation. */
454 avx_pipewire_sink_t *avx_pipewire_sink_create(const avx_pipewire_sink_config_t *config) { (void)config; return NULL; }
455 void avx_pipewire_sink_destroy(avx_pipewire_sink_t *sink) { (void)sink; }
456 /* Dequeues PipeWire shared memory buffer, writes converted video frame, and queues for client consumption. */
457 bool avx_pipewire_sink_push_frame(avx_pipewire_sink_t *sink, const uint8_t *f, size_t s, uint32_t w, uint32_t h, uint32_t fc) {
458 (void)sink; (void)f; (void)s; (void)w; (void)h; (void)fc; return false;
459 }
460 bool avx_pipewire_sink_is_active(const avx_pipewire_sink_t *sink) { (void)sink; return false; }
461  
462 #endif