+ +
2e4f57a fix: resolve windows and macos cross platform build and test failures
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 | * tools/openavx_virtcam.c - Linux V4L2 Virtual Webcam Sink CLI |
| 7 | */ |
| 8 | |
| 9 | #define _GNU_SOURCE |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <signal.h> |
| 14 | #include <stdbool.h> |
| 15 | #include <stdint.h> |
| 16 | |
| 17 | #include "avx_platform.h" |
| 18 | #include "openavx.h" |
| 19 | #include "openavx_cli.h" |
| 20 | #include "avx_v4l2_sink.h" |
| 21 | #include "avx_pipewire_sink.h" |
| 22 | #include "avx_virtcam_ipc.h" |
| 23 | #include "avx_dshow_sink.h" |
| 24 | #include "avx_dal_sink.h" |
| 25 | #include "avx_web.h" |
| 26 | #include "avx_video_codec.h" |
| 27 | #include "avx_internal.h" |
| 28 | |
| 29 | #if defined(AVX_OS_LINUX) |
| 30 | #include <linux/videodev2.h> |
| 31 | #include <sys/utsname.h> |
| 32 | #include <unistd.h> |
| 33 | #endif |
| 34 | |
| 35 | static volatile bool g_running = true; |
| 36 | |
| 37 | static void sigint_handler(int sig) { |
| 38 | (void)sig; |
| 39 | g_running = false; |
| 40 | } |
| 41 | |
| 42 | typedef struct { |
| 43 | avx_v4l2_sink_t *v4l2_sink; |
| 44 | avx_pipewire_sink_t *pw_sink; |
| 45 | avx_virtcam_producer_t *ipc_sink; |
| 46 | avx_video_decoder_t *decoder; |
| 47 | avx_video_decoder_t *mjpeg_dec; |
| 48 | oavx_stream_t *tx_stream; |
| 49 | avx_web_server_t *web_srv; |
| 50 | volatile uint64_t frames_received; |
| 51 | uint32_t last_width; |
| 52 | uint32_t last_height; |
| 53 | char last_codec[32]; |
| 54 | avx_mutex_t lock; |
| 55 | } virtcam_web_ctx_t; |
| 56 | |
| 57 | static void on_virtcam_web_frame_ingress( |
| 58 | const uint8_t *frame_data, |
| 59 | size_t frame_size, |
| 60 | uint32_t fourcc, |
| 61 | uint32_t width, |
| 62 | uint32_t height, |
| 63 | bool is_keyframe, |
| 64 | void *user_ctx |
| 65 | ) { |
| 66 | (void)is_keyframe; |
| 67 | virtcam_web_ctx_t *ctx = (virtcam_web_ctx_t*)user_ctx; |
| 68 | if (!ctx || (!ctx->v4l2_sink && !ctx->pw_sink && !ctx->ipc_sink && !ctx->web_srv && !ctx->tx_stream) || !frame_data || frame_size == 0) return; |
| 69 | |
| 70 | avx_mutex_lock(&ctx->lock); |
| 71 | ctx->last_width = width; |
| 72 | ctx->last_height = height; |
| 73 | ctx->frames_received++; |
| 74 | |
| 75 | avx_video_decoder_t *active_dec = NULL; |
| 76 | if (fourcc == OAVX_FOURCC_VIDEO_TYPE_H264) { |
| 77 | snprintf(ctx->last_codec, sizeof(ctx->last_codec), "H.264"); |
| 78 | if (ctx->web_srv) { |
| 79 | avx_web_server_feed_frame(ctx->web_srv, frame_data, frame_size, 0, is_keyframe); |
| 80 | } |
| 81 | active_dec = ctx->decoder; |
| 82 | } else if (fourcc == 0x47504A4D || fourcc == 0x4A50474D) { /* 'MJPG' / 'MPGJ' */ |
| 83 | snprintf(ctx->last_codec, sizeof(ctx->last_codec), "JPEG"); |
| 84 | if (!ctx->mjpeg_dec) { |
| 85 | avx_video_dec_cfg_t mjpg_cfg = { |
| 86 | .fourcc = 0x47504A4D, |
| 87 | .preferred_fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY |
| 88 | }; |
| 89 | ctx->mjpeg_dec = avx_video_decoder_create(&mjpg_cfg); |
| 90 | } |
| 91 | active_dec = ctx->mjpeg_dec; |
| 92 | } |
| 93 | |
| 94 | if (active_dec) { |
| 95 | oavx_video_frame_t out_frame; |
| 96 | memset(&out_frame, 0, sizeof(out_frame)); |
| 97 | if (avx_video_decoder_decode(active_dec, frame_data, frame_size, &out_frame) == 0) { |
| 98 | if (ctx->pw_sink) { |
| 99 | size_t sz = out_frame.strides[0] > 0 ? out_frame.strides[0] * out_frame.height : (size_t)out_frame.width * out_frame.height * 2; |
| 100 | avx_pipewire_sink_push_frame( |
| 101 | ctx->pw_sink, |
| 102 | out_frame.planes[0], |
| 103 | sz, |
| 104 | out_frame.width, |
| 105 | out_frame.height, |
| 106 | out_frame.fourcc |
| 107 | ); |
| 108 | } |
| 109 | if (ctx->v4l2_sink) { |
| 110 | avx_v4l2_sink_write_frame(ctx->v4l2_sink, &out_frame); |
| 111 | } |
| 112 | if (ctx->ipc_sink) { |
| 113 | size_t sz = out_frame.strides[0] > 0 ? out_frame.strides[0] * out_frame.height : (size_t)out_frame.width * out_frame.height * 2; |
| 114 | avx_virtcam_producer_write_frame( |
| 115 | ctx->ipc_sink, |
| 116 | out_frame.planes[0], |
| 117 | sz, |
| 118 | out_frame.width, |
| 119 | out_frame.height, |
| 120 | out_frame.strides[0], |
| 121 | out_frame.fourcc, |
| 122 | 30, |
| 123 | 1 |
| 124 | ); |
| 125 | } |
| 126 | if (ctx->tx_stream) { |
| 127 | oavx_stream_send_video(ctx->tx_stream, &out_frame); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | avx_mutex_unlock(&ctx->lock); |
| 132 | } |
| 133 | |
| 134 | #if defined(AVX_OS_LINUX) |
| 135 | static int virtcam_run_doctor(void) { |
| 136 | printf("\n=======================================================\n"); |
| 137 | printf(" OpenAVX Virtual Camera Diagnostic (Linux System Doctor)\n"); |
| 138 | printf("=======================================================\n\n"); |
| 139 | |
| 140 | /* 1. OS & Kernel */ |
| 141 | char os_name[128] = "Linux"; |
| 142 | FILE *f_os = fopen("/etc/os-release", "r"); |
| 143 | if (f_os) { |
| 144 | char line[256]; |
| 145 | while (fgets(line, sizeof(line), f_os)) { |
| 146 | if (strncmp(line, "PRETTY_NAME=", 12) == 0) { |
| 147 | char *val = line + 12; |
| 148 | if (*val == '"') val++; |
| 149 | size_t l = strlen(val); |
| 150 | if (l > 0 && val[l - 1] == '\n') val[--l] = '\0'; |
| 151 | if (l > 0 && val[l - 1] == '"') val[--l] = '\0'; |
| 152 | snprintf(os_name, sizeof(os_name), "%s", val); |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | fclose(f_os); |
| 157 | } |
| 158 | printf("[*] Operating System : %s\n", os_name); |
| 159 | |
| 160 | char kernel_rel[256] = "Unknown"; |
| 161 | struct utsname u; |
| 162 | if (uname(&u) == 0) { |
| 163 | snprintf(kernel_rel, sizeof(kernel_rel), "%s (%s)", u.release, u.machine); |
| 164 | } |
| 165 | printf("[*] Kernel Release : %s\n", kernel_rel); |
| 166 | |
| 167 | /* 2. Secure Boot Status */ |
| 168 | bool sb_enabled = false; |
| 169 | FILE *f_sb = popen("mokutil --sb-state 2>/dev/null", "r"); |
| 170 | if (f_sb) { |
| 171 | char sb_buf[128] = ""; |
| 172 | if (fgets(sb_buf, sizeof(sb_buf), f_sb)) { |
| 173 | if (strcasestr(sb_buf, "enabled")) { |
| 174 | sb_enabled = true; |
| 175 | } |
| 176 | } |
| 177 | pclose(f_sb); |
| 178 | } |
| 179 | printf("[*] Secure Boot : %s\n", sb_enabled ? "\033[33mENABLED (Requires MOK signing for out-of-tree modules)\033[0m" : "\033[32mDisabled / Inactive (Module loading unblocked)\033[0m"); |
| 180 | |
| 181 | /* 3. PipeWire User-Space Video */ |
| 182 | bool pw_avail = avx_pipewire_is_available(); |
| 183 | printf("[*] PipeWire Daemon : %s\n", pw_avail ? "\033[32mACTIVE (User-space virtual camera is supported)\033[0m" : "\033[33mUnavailable (libpipewire not loaded or daemon stopped)\033[0m"); |
| 184 | |
| 185 | /* 4. V4L2 Loopback Module */ |
| 186 | bool v4l2_mod_loaded = (access("/sys/module/v4l2loopback", F_OK) == 0); |
| 187 | printf("[*] V4L2 Loopback Mod: %s\n", v4l2_mod_loaded ? "\033[32mLOADED\033[0m" : "\033[31mNOT LOADED\033[0m"); |
| 188 | |
| 189 | if (v4l2_mod_loaded) { |
| 190 | char exc_caps[64] = "unknown"; |
| 191 | FILE *f_exc = fopen("/sys/module/v4l2loopback/parameters/exclusive_caps", "r"); |
| 192 | if (f_exc) { |
| 193 | if (fgets(exc_caps, sizeof(exc_caps), f_exc)) { |
| 194 | size_t l = strlen(exc_caps); |
| 195 | if (l > 0 && exc_caps[l - 1] == '\n') exc_caps[l - 1] = '\0'; |
| 196 | } |
| 197 | fclose(f_exc); |
| 198 | } |
| 199 | printf(" -> exclusive_caps: %s (%s)\n", exc_caps, (exc_caps[0] == 'Y' || exc_caps[0] == '1') ? "\033[32mOptimal for browsers\033[0m" : "\033[33mWarning: May trigger 'in-use' lockouts\033[0m"); |
| 200 | } |
| 201 | |
| 202 | /* 5. Virtual Camera Device Node */ |
| 203 | char dev_path[64] = "/dev/video10"; |
| 204 | bool dev_exists = (access(dev_path, F_OK) == 0); |
| 205 | if (!dev_exists) { |
| 206 | char probed[64] = ""; |
| 207 | if (avx_v4l2_sink_probe_loopback(probed, sizeof(probed))) { |
| 208 | snprintf(dev_path, sizeof(dev_path), "%s", probed); |
| 209 | dev_exists = true; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (dev_exists) { |
| 214 | char card_name[128] = "Unknown"; |
| 215 | char sys_name_path[128]; |
| 216 | const char *base_dev = strrchr(dev_path, '/'); |
| 217 | if (base_dev) base_dev++; else base_dev = dev_path; |
| 218 | snprintf(sys_name_path, sizeof(sys_name_path), "/sys/class/video4linux/%s/name", base_dev); |
| 219 | FILE *f_card = fopen(sys_name_path, "r"); |
| 220 | if (f_card) { |
| 221 | if (fgets(card_name, sizeof(card_name), f_card)) { |
| 222 | size_t l = strlen(card_name); |
| 223 | if (l > 0 && card_name[l - 1] == '\n') card_name[l - 1] = '\0'; |
| 224 | } |
| 225 | fclose(f_card); |
| 226 | } |
| 227 | bool can_write = (access(dev_path, W_OK) == 0); |
| 228 | printf("[*] Device Node : \033[32m%s\033[0m (%s)\n", dev_path, card_name); |
| 229 | printf("[*] Write Permission : %s\n", can_write ? "\033[32mYES (Ready for virtual camera streaming)\033[0m" : "\033[31mNO (Permission denied - add user to 'video' group)\033[0m"); |
| 230 | } else { |
| 231 | printf("[*] Device Node : \033[31mNONE FOUND (/dev/video10 does not exist)\033[0m\n"); |
| 232 | } |
| 233 | |
| 234 | /* 6. Persistence */ |
| 235 | bool perm_load = (access("/etc/modules-load.d/openavx-v4l2loopback.conf", F_OK) == 0 || |
| 236 | access("/etc/modules-load.d/v4l2loopback.conf", F_OK) == 0); |
| 237 | bool perm_opts = (access("/etc/modprobe.d/openavx-v4l2loopback.conf", F_OK) == 0 || |
| 238 | access("/etc/modprobe.d/v4l2loopback.conf", F_OK) == 0); |
| 239 | printf("[*] Boot Persistence : %s\n", (perm_load && perm_opts) ? "\033[32mCONFIGURED (Persists across reboots)\033[0m" : "\033[33mEphemeral only (Will vanish after reboot)\033[0m"); |
| 240 | |
| 241 | printf("\n-------------------------------------------------------\n"); |
| 242 | if (dev_exists && v4l2_mod_loaded) { |
| 243 | printf("\033[32mVerdict: Fully Ready for Chrome, Firefox, Meet, Zoom & Teams!\033[0m\n"); |
| 244 | printf("Launch feed: openavx cam --web 8080 -d %s\n", dev_path); |
| 245 | } else { |
| 246 | printf("\033[33mVerdict: V4L2 kernel device is not ready.\033[0m\n"); |
| 247 | printf("To install and configure permanently, run:\n"); |
| 248 | printf(" \033[1mopenavx cam install\033[0m\n"); |
| 249 | } |
| 250 | printf("=======================================================\n\n"); |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static int virtcam_run_install(void) { |
| 255 | printf("\n=======================================================\n"); |
| 256 | printf(" OpenAVX Virtual Camera Auto-Installer & Configurator\n"); |
| 257 | printf("=======================================================\n\n"); |
| 258 | |
| 259 | /* 1. Detect Linux Distro */ |
| 260 | char distro_id[64] = "unknown"; |
| 261 | FILE *f_os = fopen("/etc/os-release", "r"); |
| 262 | if (f_os) { |
| 263 | char line[256]; |
| 264 | while (fgets(line, sizeof(line), f_os)) { |
| 265 | if (strncmp(line, "ID=", 3) == 0) { |
| 266 | char *val = line + 3; |
| 267 | if (*val == '"') val++; |
| 268 | size_t l = strlen(val); |
| 269 | if (l > 0 && val[l - 1] == '\n') val[--l] = '\0'; |
| 270 | if (l > 0 && val[l - 1] == '"') val[--l] = '\0'; |
| 271 | snprintf(distro_id, sizeof(distro_id), "%s", val); |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | fclose(f_os); |
| 276 | } |
| 277 | printf("[*] Detected Linux Distribution: \033[1m%s\033[0m\n", distro_id); |
| 278 | |
| 279 | /* 2. Determine Package Manager & Commands */ |
| 280 | const char *check_pkg_cmd = NULL; |
| 281 | const char *install_pkg_cmd = NULL; |
| 282 | const char *build_mod_cmd = NULL; |
| 283 | |
| 284 | if (strcmp(distro_id, "fedora") == 0 || strcmp(distro_id, "ultramarine") == 0 || |
| 285 | strcmp(distro_id, "rhel") == 0 || strcmp(distro_id, "centos") == 0 || |
| 286 | strcmp(distro_id, "rocky") == 0 || strcmp(distro_id, "almalinux") == 0) { |
| 287 | check_pkg_cmd = "rpm -q akmod-v4l2loopback >/dev/null 2>&1"; |
| 288 | install_pkg_cmd = "sudo dnf install -y akmod-v4l2loopback kernel-devel help2man"; |
| 289 | build_mod_cmd = "sudo akmods --akmod v4l2loopback --force"; |
| 290 | } else if (strcmp(distro_id, "ubuntu") == 0 || strcmp(distro_id, "debian") == 0 || |
| 291 | strcmp(distro_id, "linuxmint") == 0 || strcmp(distro_id, "pop") == 0) { |
| 292 | check_pkg_cmd = "dpkg -s v4l2loopback-dkms >/dev/null 2>&1"; |
| 293 | install_pkg_cmd = "sudo apt-get update && sudo apt-get install -y v4l2loopback-dkms linux-headers-$(uname -r)"; |
| 294 | } else if (strcmp(distro_id, "arch") == 0 || strcmp(distro_id, "manjaro") == 0 || |
| 295 | strcmp(distro_id, "endeavouros") == 0) { |
| 296 | check_pkg_cmd = "pacman -Q v4l2loopback-dkms >/dev/null 2>&1"; |
| 297 | install_pkg_cmd = "sudo pacman -S --noconfirm v4l2loopback-dkms linux-headers"; |
| 298 | } else { |
| 299 | printf("\033[33m[!] Generic Linux distro detected. Assuming standard dkms or package manager.\033[0m\n"); |
| 300 | } |
| 301 | |
| 302 | /* 3. Check if module is already on disk */ |
| 303 | bool mod_installed = (system("modinfo v4l2loopback >/dev/null 2>&1") == 0); |
| 304 | if (!mod_installed && check_pkg_cmd) { |
| 305 | mod_installed = (system(check_pkg_cmd) == 0); |
| 306 | } |
| 307 | |
| 308 | if (!mod_installed) { |
| 309 | if (!install_pkg_cmd) { |
| 310 | fprintf(stderr, "\033[31m[ERROR] Please install 'v4l2loopback' using your distribution's package manager.\033[0m\n"); |
| 311 | return 1; |
| 312 | } |
| 313 | printf("[*] Package 'v4l2loopback' not found on system.\n"); |
| 314 | printf("[*] Installing required kernel packages and build tools...\n"); |
| 315 | printf(" Executing: \033[36m%s\033[0m\n", install_pkg_cmd); |
| 316 | int rc = system(install_pkg_cmd); |
| 317 | if (rc != 0) { |
| 318 | fprintf(stderr, "\033[31m[ERROR] Package installation failed with exit code %d.\033[0m\n", rc); |
| 319 | if (strcmp(distro_id, "fedora") == 0 || strcmp(distro_id, "ultramarine") == 0) { |
| 320 | fprintf(stderr, "Hint: On Fedora, ensure RPM Fusion is enabled: 'sudo dnf install https://mirrors.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %%fedora).noarch.rpm'\n"); |
| 321 | } |
| 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | if (build_mod_cmd) { |
| 326 | printf("[*] Building kernel module for current kernel...\n"); |
| 327 | printf(" Executing: \033[36m%s\033[0m\n", build_mod_cmd); |
| 328 | int brc = system(build_mod_cmd); |
| 329 | if (brc != 0) { |
| 330 | fprintf(stderr, "\033[31m[ERROR] Kernel module compilation failed. Check kernel headers.\033[0m\n"); |
| 331 | return 1; |
| 332 | } |
| 333 | } |
| 334 | } else { |
| 335 | printf("[*] Kernel module 'v4l2loopback' is present on disk.\n"); |
| 336 | } |
| 337 | |
| 338 | /* 4. Write Persistence Configuration */ |
| 339 | printf("[*] Configuring boot persistence...\n"); |
| 340 | int w1 = system("echo 'v4l2loopback' | sudo tee /etc/modules-load.d/openavx-v4l2loopback.conf >/dev/null"); |
| 341 | int w2 = system("echo 'options v4l2loopback devices=1 video_nr=10 card_label=\"OpenAVX Virtual Camera\" exclusive_caps=1' | sudo tee /etc/modprobe.d/openavx-v4l2loopback.conf >/dev/null"); |
| 342 | if (w1 != 0 || w2 != 0) { |
| 343 | fprintf(stderr, "\033[33m[!] Warning: Could not write /etc/modules-load.d or /etc/modprobe.d (sudo failed).\033[0m\n"); |
| 344 | } else { |
| 345 | printf(" -> Created \033[32m/etc/modules-load.d/openavx-v4l2loopback.conf\033[0m\n"); |
| 346 | printf(" -> Created \033[32m/etc/modprobe.d/openavx-v4l2loopback.conf\033[0m\n"); |
| 347 | } |
| 348 | |
| 349 | /* 5. Load Module */ |
| 350 | printf("[*] Loading v4l2loopback into Linux kernel...\n"); |
| 351 | system("sudo modprobe -r v4l2loopback 2>/dev/null || true"); |
| 352 | int lrc = system("sudo modprobe v4l2loopback devices=1 video_nr=10 card_label=\"OpenAVX Virtual Camera\" exclusive_caps=1"); |
| 353 | if (lrc != 0) { |
| 354 | fprintf(stderr, "\033[31m[ERROR] Failed to load v4l2loopback module into kernel.\033[0m\n"); |
| 355 | return 1; |
| 356 | } |
| 357 | |
| 358 | /* 6. Verify Device Node Exists */ |
| 359 | if (access("/dev/video10", F_OK) == 0) { |
| 360 | printf("\n\033[32m=======================================================\n"); |
| 361 | printf(" [SUCCESS] OpenAVX Virtual Camera is installed and ready!\n"); |
| 362 | printf("=======================================================\033[0m\n"); |
| 363 | printf(" Device Node : \033[1m/dev/video10\033[0m\n"); |
| 364 | printf(" Card Label : \033[1mOpenAVX Virtual Camera\033[0m\n"); |
| 365 | printf(" Browser Ready : Chrome, Chromium, Firefox, Zoom, Teams, Meet\n"); |
| 366 | printf(" Persistence : Module will load automatically on every system boot.\n\n"); |
| 367 | printf("To start streaming, run:\n"); |
| 368 | printf(" \033[36mopenavx cam --web\033[0m\n\n"); |
| 369 | return 0; |
| 370 | } else { |
| 371 | fprintf(stderr, "\033[31m[ERROR] Module loaded but /dev/video10 did not appear.\033[0m\n"); |
| 372 | return 1; |
| 373 | } |
| 374 | } |
| 375 | #else |
| 376 | static int virtcam_run_doctor(void) { |
| 377 | printf("Virtcam doctor is currently supported on Linux.\n"); |
| 378 | return 0; |
| 379 | } |
| 380 | static int virtcam_run_install(void) { |
| 381 | printf("Virtcam automatic installation is currently configured for Linux.\n"); |
| 382 | return 0; |
| 383 | } |
| 384 | #endif |
| 385 | |
| 386 | static void print_usage(const char* prog) { |
| 387 | printf("OpenAVX Virtual Camera (Zero-Sudo PipeWire, DirectShow & DAL Webcam Sink)\n"); |
| 388 | printf("Feeds smartphone cameras or OpenAVX video streams into Zoom, Teams, Meet & Discord.\n\n"); |
| 389 | printf("Usage: %s [options|subcommand]\n\n", prog); |
| 390 | printf("Subcommands:\n"); |
| 391 | printf(" doctor Diagnose virtual camera drivers, PipeWire & Secure Boot\n"); |
| 392 | printf(" install, setup Install & configure persistent V4L2 virtual camera (/dev/video10)\n\n"); |
| 393 | printf("Options:\n"); |
| 394 | printf(" -W, --web [PORT] Start Web Camera Ingress hub for phone streaming (default: 8080)\n"); |
| 395 | printf(" -s, --source <SOURCE> Upstream source name or IP:port to ingest (default: auto-discover)\n"); |
| 396 | printf(" -P, --pipewire Force PipeWire virtual camera sink (default on modern Linux)\n"); |
| 397 | printf(" -d, --device <DEVICE> V4L2 loopback device path (e.g. /dev/video10)\n"); |
| 398 | printf(" -p, --passphrase <KEY> Passphrase for native E2EE encrypted stream\n"); |
| 399 | printf(" -f, --format <FORMAT> Output format: yuyv (default), uyvy, bgr24\n"); |
| 400 | printf(" -g, --groups <GROUPS> Discovery / finder groups (default: public)\n"); |
| 401 | printf(" --doctor Run virtual camera diagnostic\n"); |
| 402 | printf(" --install, --setup Install & configure persistent Linux kernel loopback\n"); |
| 403 | printf(" --register (Windows) Register DirectShow COM filter into HKCU (Zero Admin)\n"); |
| 404 | printf(" --unregister (Windows) Unregister DirectShow COM filter from HKCU\n"); |
| 405 | printf(" --install-dal (macOS) Install CoreMediaIO DAL virtual camera plugin (Zero Root)\n"); |
| 406 | printf(" --uninstall-dal (macOS) Uninstall CoreMediaIO DAL virtual camera plugin\n"); |
| 407 | printf(" -q, --quiet Suppress info banners and live progress logs\n"); |
| 408 | printf(" -h, --help Display this help message and exit\n\n"); |
| 409 | printf("Driverless / Zero-Sudo Architecture:\n"); |
| 410 | printf(" - Linux: Publishes user-space PipeWire video node (\"OpenAVX Virtual Camera\").\n"); |
| 411 | printf(" - Windows: Self-registers in HKCU DirectShow Video Input Category without admin rights.\n"); |
| 412 | printf(" - macOS: Installs into ~/Library/CoreMediaIO/Plug-Ins/DAL/ without sudo or root.\n\n"); |
| 413 | printf("Examples:\n"); |
| 414 | printf(" %s doctor # Diagnose webcam subsystems\n", prog); |
| 415 | printf(" %s install # Install and configure persistent /dev/video10\n", prog); |
| 416 | printf(" %s --web # Host phone camera capture page at http://<ip>:8080/camera\n", prog); |
| 417 | printf(" %s --web 8088 --pipewire # Explicitly run on PipeWire port 8088\n", prog); |
| 418 | printf(" %s --source \"Phone_Camera\" # Ingest LAN OpenAVX stream into Virtual Camera\n", prog); |
| 419 | } |
| 420 | |
| 421 | int openavx_cmd_virtcam(int argc, char** argv) { |
| 422 | if (argc >= 2) { |
| 423 | if (strcmp(argv[1], "doctor") == 0 || strcmp(argv[1], "check") == 0 || |
| 424 | strcmp(argv[1], "--doctor") == 0) { |
| 425 | return virtcam_run_doctor(); |
| 426 | } |
| 427 | if (strcmp(argv[1], "install") == 0 || strcmp(argv[1], "setup") == 0 || |
| 428 | strcmp(argv[1], "--install") == 0 || strcmp(argv[1], "--setup") == 0) { |
| 429 | return virtcam_run_install(); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | optind = 1; |
| 434 | g_running = true; |
| 435 | |
| 436 | char source_arg[256] = ""; |
| 437 | char device_arg[128] = ""; |
| 438 | char passphrase_arg[128] = ""; |
| 439 | char format_arg[32] = "yuyv"; |
| 440 | char groups[256] = "public"; |
| 441 | bool quiet = false; |
| 442 | bool enable_web = false; |
| 443 | bool force_pipewire = false; |
| 444 | uint16_t web_port = 8080; |
| 445 | |
| 446 | static struct option long_opts[] = { |
| 447 | { "web", optional_argument, 0, 'W' }, |
| 448 | { "source", required_argument, 0, 's' }, |
| 449 | { "pipewire", no_argument, 0, 'P' }, |
| 450 | { "device", required_argument, 0, 'd' }, |
| 451 | { "passphrase", required_argument, 0, 'p' }, |
| 452 | { "format", required_argument, 0, 'f' }, |
| 453 | { "groups", required_argument, 0, 'g' }, |
| 454 | { "doctor", no_argument, 0, 1005 }, |
| 455 | { "install", no_argument, 0, 1006 }, |
| 456 | { "setup", no_argument, 0, 1006 }, |
| 457 | { "register", no_argument, 0, 1001 }, |
| 458 | { "unregister", no_argument, 0, 1002 }, |
| 459 | { "install-dal", no_argument, 0, 1003 }, |
| 460 | { "uninstall-dal", no_argument, 0, 1004 }, |
| 461 | { "quiet", no_argument, 0, 'q' }, |
| 462 | { "help", no_argument, 0, 'h' }, |
| 463 | { 0, 0, 0, 0 } |
| 464 | }; |
| 465 | |
| 466 | int opt; |
| 467 | while ((opt = getopt_long(argc, argv, "W::s:Pd:p:f:g:qh", long_opts, NULL)) != -1) { |
| 468 | switch (opt) { |
| 469 | case 'W': |
| 470 | enable_web = true; |
| 471 | if (optarg && optarg[0] != '\0') { |
| 472 | web_port = (uint16_t)atoi(optarg); |
| 473 | } else if (optind < argc && argv[optind] && argv[optind][0] != '-') { |
| 474 | web_port = (uint16_t)atoi(argv[optind++]); |
| 475 | } |
| 476 | if (web_port == 0) web_port = 8080; |
| 477 | break; |
| 478 | case 's': |
| 479 | snprintf(source_arg, sizeof(source_arg), "%s", optarg); |
| 480 | break; |
| 481 | case 'P': |
| 482 | force_pipewire = true; |
| 483 | break; |
| 484 | case 'd': |
| 485 | snprintf(device_arg, sizeof(device_arg), "%s", optarg); |
| 486 | break; |
| 487 | case 'p': |
| 488 | snprintf(passphrase_arg, sizeof(passphrase_arg), "%s", optarg); |
| 489 | break; |
| 490 | case 'f': |
| 491 | snprintf(format_arg, sizeof(format_arg), "%s", optarg); |
| 492 | break; |
| 493 | case 'g': |
| 494 | snprintf(groups, sizeof(groups), "%s", optarg); |
| 495 | break; |
| 496 | case 1001: |
| 497 | if (avx_dshow_register_filter(NULL)) { |
| 498 | printf("Successfully registered OpenAVX Virtual Camera (DirectShow COM) in HKCU (Zero Admin).\n"); |
| 499 | return 0; |
| 500 | } else { |
| 501 | fprintf(stderr, "Failed to register OpenAVX Virtual Camera DirectShow filter.\n"); |
| 502 | return 1; |
| 503 | } |
| 504 | case 1002: |
| 505 | if (avx_dshow_unregister_filter()) { |
| 506 | printf("Successfully unregistered OpenAVX Virtual Camera from HKCU.\n"); |
| 507 | return 0; |
| 508 | } else { |
| 509 | fprintf(stderr, "Failed to unregister OpenAVX Virtual Camera DirectShow filter.\n"); |
| 510 | return 1; |
| 511 | } |
| 512 | case 1003: |
| 513 | if (avx_dal_install_plugin()) { |
| 514 | printf("Successfully installed OpenAVX CoreMediaIO DAL virtual camera plugin (Zero Root).\n"); |
| 515 | return 0; |
| 516 | } else { |
| 517 | fprintf(stderr, "Failed to install OpenAVX DAL virtual camera plugin.\n"); |
| 518 | return 1; |
| 519 | } |
| 520 | case 1004: |
| 521 | if (avx_dal_uninstall_plugin()) { |
| 522 | printf("Successfully uninstalled OpenAVX CoreMediaIO DAL virtual camera plugin.\n"); |
| 523 | return 0; |
| 524 | } else { |
| 525 | fprintf(stderr, "Failed to uninstall OpenAVX DAL virtual camera plugin.\n"); |
| 526 | return 1; |
| 527 | } |
| 528 | case 1005: |
| 529 | return virtcam_run_doctor(); |
| 530 | case 1006: |
| 531 | return virtcam_run_install(); |
| 532 | case 'q': |
| 533 | quiet = true; |
| 534 | break; |
| 535 | case 'h': |
| 536 | default: |
| 537 | print_usage(argv[0]); |
| 538 | return (opt == 'h') ? 0 : 1; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | openavx_cli_set_quiet(quiet); |
| 543 | |
| 544 | /* Setup signal handling */ |
| 545 | signal(SIGINT, sigint_handler); |
| 546 | signal(SIGTERM, sigint_handler); |
| 547 | |
| 548 | /* 1. Resolve Sink Backend: PipeWire (Linux), DirectShow (Windows), DAL (macOS), or V4L2 */ |
| 549 | avx_pipewire_sink_t *pw_sink = NULL; |
| 550 | avx_v4l2_sink_t *v4l2_sink = NULL; |
| 551 | avx_virtcam_producer_t *ipc_sink = avx_virtcam_producer_create(); |
| 552 | char resolved_sink_desc[128] = ""; |
| 553 | char resolved_dev[128] = ""; |
| 554 | |
| 555 | #if defined(AVX_OS_WINDOWS) |
| 556 | if (!avx_dshow_is_registered()) { |
| 557 | avx_dshow_register_filter(NULL); |
| 558 | } |
| 559 | snprintf(resolved_sink_desc, sizeof(resolved_sink_desc), "DirectShow (HKCU Zero-Admin)"); |
| 560 | #elif defined(AVX_OS_MACOS) |
| 561 | if (!avx_dal_is_installed()) { |
| 562 | avx_dal_install_plugin(); |
| 563 | } |
| 564 | snprintf(resolved_sink_desc, sizeof(resolved_sink_desc), "CoreMediaIO DAL (User-Space)"); |
| 565 | #elif defined(AVX_OS_LINUX) |
| 566 | if (device_arg[0] != '\0' && !force_pipewire) { |
| 567 | /* User explicitly passed --device /dev/videoX */ |
| 568 | snprintf(resolved_dev, sizeof(resolved_dev), "%s", device_arg); |
| 569 | uint32_t sink_pixfmt = V4L2_PIX_FMT_YUYV; |
| 570 | if (strcasecmp(format_arg, "uyvy") == 0) sink_pixfmt = V4L2_PIX_FMT_UYVY; |
| 571 | else if (strcasecmp(format_arg, "bgr24") == 0) sink_pixfmt = V4L2_PIX_FMT_BGR24; |
| 572 | |
| 573 | avx_v4l2_sink_config_t sink_cfg = { |
| 574 | .device_path = resolved_dev, |
| 575 | .width = 1920, |
| 576 | .height = 1080, |
| 577 | .pixelformat = sink_pixfmt, |
| 578 | .fps_num = 30, |
| 579 | .fps_den = 1 |
| 580 | }; |
| 581 | v4l2_sink = avx_v4l2_sink_create(&sink_cfg); |
| 582 | if (!v4l2_sink) { |
| 583 | oavx_log_write(OAVX_LOG_ERROR, "virtcam", "Failed to open V4L2 loopback device '%s' (check permissions or sudo usermod -aG video $USER)", resolved_dev); |
| 584 | if (ipc_sink) avx_virtcam_producer_destroy(ipc_sink); |
| 585 | return 1; |
| 586 | } |
| 587 | snprintf(resolved_sink_desc, sizeof(resolved_sink_desc), "V4L2 (%s)", resolved_dev); |
| 588 | } else { |
| 589 | /* Default: Try modern user-space PipeWire virtual camera (zero sudo, driverless) */ |
| 590 | if (avx_pipewire_is_available()) { |
| 591 | avx_pipewire_sink_config_t pw_cfg = { |
| 592 | .node_name = "OpenAVX Virtual Camera", |
| 593 | .node_description = "OpenAVX Virtual Camera Feed", |
| 594 | .width = 1920, |
| 595 | .height = 1080, |
| 596 | .fps_num = 30, |
| 597 | .fps_den = 1, |
| 598 | .preferred_fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY |
| 599 | }; |
| 600 | pw_sink = avx_pipewire_sink_create(&pw_cfg); |
| 601 | if (pw_sink) { |
| 602 | snprintf(resolved_sink_desc, sizeof(resolved_sink_desc), "PipeWire (User-Space, Driverless)"); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | /* If PipeWire not available and user didn't force it, try V4L2 loopback probe fallback */ |
| 607 | if (!pw_sink && !force_pipewire) { |
| 608 | if (avx_v4l2_sink_probe_loopback(resolved_dev, sizeof(resolved_dev))) { |
| 609 | avx_v4l2_sink_config_t sink_cfg = { |
| 610 | .device_path = resolved_dev, |
| 611 | .width = 1920, |
| 612 | .height = 1080, |
| 613 | .pixelformat = V4L2_PIX_FMT_YUYV, |
| 614 | .fps_num = 30, |
| 615 | .fps_den = 1 |
| 616 | }; |
| 617 | v4l2_sink = avx_v4l2_sink_create(&sink_cfg); |
| 618 | if (v4l2_sink) { |
| 619 | snprintf(resolved_sink_desc, sizeof(resolved_sink_desc), "V4L2 (%s)", resolved_dev); |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | if (!pw_sink && !v4l2_sink && ipc_sink) { |
| 625 | snprintf(resolved_sink_desc, sizeof(resolved_sink_desc), "IPC Shared Memory (/openavx_virtcam_shm)"); |
| 626 | } |
| 627 | } |
| 628 | #else |
| 629 | if (ipc_sink) { |
| 630 | snprintf(resolved_sink_desc, sizeof(resolved_sink_desc), "IPC Shared Memory"); |
| 631 | } |
| 632 | #endif |
| 633 | |
| 634 | if (!pw_sink && !v4l2_sink && !ipc_sink) { |
| 635 | openavx_cli_header("virtcam"); |
| 636 | oavx_log_write(OAVX_LOG_ERROR, "virtcam", |
| 637 | "No virtual camera sink available:\n" |
| 638 | " - PipeWire: daemon not responding or library missing.\n" |
| 639 | " - V4L2 Loopback: no device found in /dev/video*.\n"); |
| 640 | return 1; |
| 641 | } |
| 642 | |
| 643 | /* If neither source nor web specified, default to web camera hub */ |
| 644 | if (source_arg[0] == '\0') { |
| 645 | enable_web = true; |
| 646 | } |
| 647 | |
| 648 | /* ===================================================================== */ |
| 649 | /* MODE A: SMARTPHONE WEB CAMERA INGRESS HUB */ |
| 650 | /* ===================================================================== */ |
| 651 | if (enable_web) { |
| 652 | char local_ip[64] = "127.0.0.1"; |
| 653 | (void)avx_net_get_default_ip(local_ip, sizeof(local_ip)); |
| 654 | |
| 655 | avx_video_codec_init(); |
| 656 | avx_video_dec_cfg_t dec_cfg = { |
| 657 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_H264, |
| 658 | .preferred_fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY |
| 659 | }; |
| 660 | avx_video_decoder_t *dec = avx_video_decoder_create(&dec_cfg); |
| 661 | |
| 662 | virtcam_web_ctx_t web_ctx; |
| 663 | memset(&web_ctx, 0, sizeof(web_ctx)); |
| 664 | web_ctx.v4l2_sink = v4l2_sink; |
| 665 | web_ctx.pw_sink = pw_sink; |
| 666 | web_ctx.ipc_sink = ipc_sink; |
| 667 | web_ctx.decoder = dec; |
| 668 | avx_mutex_init(&web_ctx.lock); |
| 669 | |
| 670 | oavx_context_t *ctx = oavx_context_create(); |
| 671 | if (ctx) { |
| 672 | oavx_stream_config_t tx_cfg = { |
| 673 | .name = "Phone Camera", |
| 674 | .direction = OAVX_DIR_TRANSMIT, |
| 675 | .color_format = OAVX_COLOR_FORMAT_FASTEST, |
| 676 | .groups = groups, |
| 677 | .passphrase = passphrase_arg[0] ? passphrase_arg : NULL |
| 678 | }; |
| 679 | web_ctx.tx_stream = oavx_stream_open(ctx, &tx_cfg); |
| 680 | } |
| 681 | |
| 682 | avx_web_server_config_t web_cfg = { |
| 683 | .port = web_port, |
| 684 | .stream_name = "OpenAVX Phone Camera", |
| 685 | .codec = AVX_RTSP_CODEC_H264, |
| 686 | .on_ingress_frame = on_virtcam_web_frame_ingress, |
| 687 | .user_ctx = &web_ctx |
| 688 | }; |
| 689 | avx_web_server_t *web_srv = avx_web_server_create(&web_cfg); |
| 690 | if (!web_srv) { |
| 691 | oavx_log_write(OAVX_LOG_ERROR, "virtcam", "Failed to start web server on port %u", web_port); |
| 692 | if (dec) avx_video_decoder_destroy(dec); |
| 693 | if (web_ctx.mjpeg_dec) avx_video_decoder_destroy(web_ctx.mjpeg_dec); |
| 694 | if (v4l2_sink) avx_v4l2_sink_destroy(v4l2_sink); |
| 695 | if (pw_sink) avx_pipewire_sink_destroy(pw_sink); |
| 696 | if (ipc_sink) avx_virtcam_producer_destroy(ipc_sink); |
| 697 | if (ctx) oavx_context_destroy(ctx); |
| 698 | return 1; |
| 699 | } |
| 700 | web_port = avx_web_server_get_port(web_srv); |
| 701 | avx_mutex_lock(&web_ctx.lock); |
| 702 | web_ctx.web_srv = web_srv; |
| 703 | avx_mutex_unlock(&web_ctx.lock); |
| 704 | |
| 705 | openavx_cli_header("virtcam"); |
| 706 | openavx_cli_kv("sink backend", "%s", resolved_sink_desc); |
| 707 | openavx_cli_kv("format", "%s", format_arg); |
| 708 | openavx_cli_kv("phone camera url", "http://%s:%u/camera", local_ip, web_port); |
| 709 | openavx_cli_kv("pc preview url", "http://localhost:%u/", web_port); |
| 710 | openavx_cli_kv("usb cable (adb)", "adb reverse tcp:%u tcp:%u -> http://localhost:%u/camera", web_port, web_port, web_port); |
| 711 | if (passphrase_arg[0]) { |
| 712 | openavx_cli_kv("e2ee", "ChaCha20-Poly1305 Active (LAN restream)"); |
| 713 | } |
| 714 | oavx_log_write(OAVX_LOG_INFO, "virtcam", "Virtual Camera Hub ready! Open http://%s:%u/camera on your phone (Ctrl+C to stop)", local_ip, web_port); |
| 715 | |
| 716 | int64_t last_stat_time = avx_time_ms(); |
| 717 | while (g_running) { |
| 718 | avx_sleep_ms(100); |
| 719 | if (oavx_get_log_level() >= OAVX_LOG_INFO) { |
| 720 | int64_t now = avx_time_ms(); |
| 721 | if (now - last_stat_time >= 1000) { |
| 722 | avx_mutex_lock(&web_ctx.lock); |
| 723 | uint64_t fc = web_ctx.frames_received; |
| 724 | uint32_t w = web_ctx.last_width; |
| 725 | uint32_t h = web_ctx.last_height; |
| 726 | const char *c = web_ctx.last_codec[0] ? web_ctx.last_codec : "waiting"; |
| 727 | avx_mutex_unlock(&web_ctx.lock); |
| 728 | |
| 729 | openavx_cli_status("virtcam", "sink=%s web=%u frames=%llu %ux%u codec=%s", |
| 730 | resolved_sink_desc, web_port, (unsigned long long)fc, w, h, c); |
| 731 | last_stat_time = now; |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | openavx_cli_status_end(); |
| 737 | oavx_log_write(OAVX_LOG_INFO, "virtcam", "Releasing Virtual Camera Hub..."); |
| 738 | avx_mutex_lock(&web_ctx.lock); |
| 739 | web_ctx.web_srv = NULL; |
| 740 | avx_mutex_unlock(&web_ctx.lock); |
| 741 | avx_web_server_destroy(web_srv); |
| 742 | if (web_ctx.tx_stream) oavx_stream_close(web_ctx.tx_stream); |
| 743 | if (dec) avx_video_decoder_destroy(dec); |
| 744 | if (web_ctx.mjpeg_dec) avx_video_decoder_destroy(web_ctx.mjpeg_dec); |
| 745 | if (v4l2_sink) avx_v4l2_sink_destroy(v4l2_sink); |
| 746 | if (pw_sink) avx_pipewire_sink_destroy(pw_sink); |
| 747 | if (ipc_sink) avx_virtcam_producer_destroy(ipc_sink); |
| 748 | if (ctx) oavx_context_destroy(ctx); |
| 749 | avx_mutex_destroy(&web_ctx.lock); |
| 750 | oavx_log_write(OAVX_LOG_INFO, "virtcam", "Virtual camera hub stopped cleanly"); |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | /* ===================================================================== */ |
| 755 | /* MODE B: UPSTREAM OPENAVX STREAM SINK */ |
| 756 | /* ===================================================================== */ |
| 757 | oavx_context_t *ctx = oavx_context_create(); |
| 758 | if (!ctx) { |
| 759 | oavx_log_write(OAVX_LOG_ERROR, "virtcam", "Failed to initialize OpenAVX context"); |
| 760 | if (v4l2_sink) avx_v4l2_sink_destroy(v4l2_sink); |
| 761 | if (pw_sink) avx_pipewire_sink_destroy(pw_sink); |
| 762 | if (ipc_sink) avx_virtcam_producer_destroy(ipc_sink); |
| 763 | return 1; |
| 764 | } |
| 765 | |
| 766 | char target_name[256] = ""; |
| 767 | char target_url[256] = ""; |
| 768 | |
| 769 | if (source_arg[0] != '\0') { |
| 770 | if (strchr(source_arg, '.') || strchr(source_arg, ':')) { |
| 771 | snprintf(target_url, sizeof(target_url), "%s", source_arg); |
| 772 | } else { |
| 773 | snprintf(target_name, sizeof(target_name), "%s", source_arg); |
| 774 | } |
| 775 | } else { |
| 776 | /* Auto-discover first available OpenAVX stream */ |
| 777 | openavx_cli_header("virtcam"); |
| 778 | oavx_log_write(OAVX_LOG_INFO, "virtcam", "Scanning network for active OpenAVX streams..."); |
| 779 | |
| 780 | oavx_discovery_t *p_find = oavx_discovery_create(ctx, groups); |
| 781 | if (!p_find) { |
| 782 | oavx_log_write(OAVX_LOG_ERROR, "virtcam", "Failed to start stream discovery"); |
| 783 | if (v4l2_sink) avx_v4l2_sink_destroy(v4l2_sink); |
| 784 | if (pw_sink) avx_pipewire_sink_destroy(pw_sink); |
| 785 | if (ipc_sink) avx_virtcam_producer_destroy(ipc_sink); |
| 786 | oavx_context_destroy(ctx); |
| 787 | return 1; |
| 788 | } |
| 789 | |
| 790 | oavx_endpoint_t sources[32]; |
| 791 | size_t count = 0; |
| 792 | int retries = 30; /* Wait up to 3 seconds */ |
| 793 | while (retries-- > 0 && count == 0 && g_running) { |
| 794 | oavx_discovery_wait(p_find, 100); |
| 795 | count = oavx_discovery_get_sources(p_find, sources, 32); |
| 796 | } |
| 797 | |
| 798 | if (count == 0) { |
| 799 | oavx_log_write(OAVX_LOG_ERROR, "virtcam", "No active OpenAVX streams found on local network or groups '%s'", groups); |
| 800 | oavx_discovery_destroy(p_find); |
| 801 | if (v4l2_sink) avx_v4l2_sink_destroy(v4l2_sink); |
| 802 | if (pw_sink) avx_pipewire_sink_destroy(pw_sink); |
| 803 | if (ipc_sink) avx_virtcam_producer_destroy(ipc_sink); |
| 804 | oavx_context_destroy(ctx); |
| 805 | return 1; |
| 806 | } |
| 807 | |
| 808 | if (sources[0].name) snprintf(target_name, sizeof(target_name), "%s", sources[0].name); |
| 809 | if (sources[0].address) snprintf(target_url, sizeof(target_url), "%s", sources[0].address); |
| 810 | oavx_log_write(OAVX_LOG_INFO, "virtcam", "Auto-selected upstream stream: %s (%s)", |
| 811 | target_name, target_url[0] ? target_url : "mDNS"); |
| 812 | oavx_discovery_destroy(p_find); |
| 813 | } |
| 814 | |
| 815 | oavx_stream_config_t rx_cfg = { |
| 816 | .name = target_name[0] ? target_name : "OpenAVX Virtual Camera Ingress", |
| 817 | .endpoint = target_url[0] ? target_url : NULL, |
| 818 | .direction = OAVX_DIR_RECEIVE, |
| 819 | .color_format = OAVX_COLOR_FORMAT_FASTEST, |
| 820 | .groups = groups, |
| 821 | .passphrase = passphrase_arg[0] ? passphrase_arg : NULL |
| 822 | }; |
| 823 | |
| 824 | oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg); |
| 825 | if (!rx) { |
| 826 | oavx_log_write(OAVX_LOG_ERROR, "virtcam", "Failed to connect to stream '%s'", |
| 827 | target_name[0] ? target_name : target_url); |
| 828 | if (v4l2_sink) avx_v4l2_sink_destroy(v4l2_sink); |
| 829 | if (pw_sink) avx_pipewire_sink_destroy(pw_sink); |
| 830 | if (ipc_sink) avx_virtcam_producer_destroy(ipc_sink); |
| 831 | oavx_context_destroy(ctx); |
| 832 | return 1; |
| 833 | } |
| 834 | |
| 835 | openavx_cli_header("virtcam"); |
| 836 | openavx_cli_kv("sink backend", "%s", resolved_sink_desc); |
| 837 | openavx_cli_kv("format", "%s", format_arg); |
| 838 | openavx_cli_kv("source", "%s%s%s", |
| 839 | target_name[0] ? target_name : target_url, |
| 840 | (target_name[0] && target_url[0]) ? " @ " : "", |
| 841 | (target_name[0] && target_url[0]) ? target_url : ""); |
| 842 | if (passphrase_arg[0]) { |
| 843 | openavx_cli_kv("e2ee", "ChaCha20-Poly1305 Active"); |
| 844 | } |
| 845 | oavx_log_write(OAVX_LOG_INFO, "virtcam", "Virtual camera active: %s is feeding video to Zoom/Teams/Meet (Ctrl+C to stop)", resolved_sink_desc); |
| 846 | |
| 847 | uint64_t frame_count = 0; |
| 848 | int64_t last_stat_time = avx_time_ms(); |
| 849 | uint32_t cur_w = 0, cur_h = 0; |
| 850 | |
| 851 | while (g_running) { |
| 852 | oavx_video_frame_t vf; |
| 853 | oavx_audio_frame_t af; |
| 854 | oavx_metadata_frame_t mf; |
| 855 | |
| 856 | oavx_frame_type_t ft = oavx_stream_receive(rx, &vf, &af, &mf, 100); |
| 857 | if (ft == OAVX_FRAME_TYPE_VIDEO) { |
| 858 | if (pw_sink) { |
| 859 | size_t sz = vf.strides[0] > 0 ? vf.strides[0] * vf.height : (size_t)vf.width * vf.height * 2; |
| 860 | avx_pipewire_sink_push_frame( |
| 861 | pw_sink, |
| 862 | vf.planes[0], |
| 863 | sz, |
| 864 | vf.width, |
| 865 | vf.height, |
| 866 | vf.fourcc |
| 867 | ); |
| 868 | } |
| 869 | if (v4l2_sink) { |
| 870 | avx_v4l2_sink_write_frame(v4l2_sink, &vf); |
| 871 | } |
| 872 | if (ipc_sink) { |
| 873 | size_t sz = vf.strides[0] > 0 ? vf.strides[0] * vf.height : (size_t)vf.width * vf.height * 2; |
| 874 | avx_virtcam_producer_write_frame( |
| 875 | ipc_sink, |
| 876 | vf.planes[0], |
| 877 | sz, |
| 878 | vf.width, |
| 879 | vf.height, |
| 880 | vf.strides[0], |
| 881 | vf.fourcc, |
| 882 | 30, |
| 883 | 1 |
| 884 | ); |
| 885 | } |
| 886 | cur_w = vf.width; |
| 887 | cur_h = vf.height; |
| 888 | frame_count++; |
| 889 | oavx_stream_release_video(rx, &vf); |
| 890 | } else if (ft == OAVX_FRAME_TYPE_AUDIO) { |
| 891 | oavx_stream_release_audio(rx, &af); |
| 892 | } else if (ft == OAVX_FRAME_TYPE_METADATA) { |
| 893 | oavx_stream_release_metadata(rx, &mf); |
| 894 | } |
| 895 | |
| 896 | if (oavx_get_log_level() >= OAVX_LOG_INFO) { |
| 897 | int64_t now = avx_time_ms(); |
| 898 | if (now - last_stat_time >= 1000) { |
| 899 | openavx_cli_status("virtcam", "sink=%s frames=%llu %ux%u format=%s", |
| 900 | resolved_sink_desc, (unsigned long long)frame_count, cur_w, cur_h, format_arg); |
| 901 | last_stat_time = now; |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | openavx_cli_status_end(); |
| 907 | oavx_log_write(OAVX_LOG_INFO, "virtcam", "Stopping virtual camera..."); |
| 908 | oavx_stream_close(rx); |
| 909 | if (v4l2_sink) avx_v4l2_sink_destroy(v4l2_sink); |
| 910 | if (pw_sink) avx_pipewire_sink_destroy(pw_sink); |
| 911 | if (ipc_sink) avx_virtcam_producer_destroy(ipc_sink); |
| 912 | oavx_context_destroy(ctx); |
| 913 | oavx_log_write(OAVX_LOG_INFO, "virtcam", "Virtual camera released cleanly"); |
| 914 | |
| 915 | return 0; |
| 916 | } |