r/bashonubuntuonwindows • u/santlame • 3h ago
self promotion Reading a Windows webcam in WSL2 at 30 fps — without usbipd or a kernel rebuild
Processing img np6n921e1e2i1...
If you have tried to use a webcam from WSL2 for OpenCV or ROS, you know the routine: attach the camera with usbipd-win, discover the stock kernel has no uvcvideo, rebuild the kernel, and then watch raw 1080p collapse to ~5 fps over USB 2.0 anyway.
I built a different path. hvcam captures the camera on the Windows side with Media Foundation and streams frames into WSL2 over Hyper-V sockets (VMBus) — no IP discovery, no firewall prompts, no NAT. The Linux side is a small C library plus a Python binding; your code pulls the newest frame when it is ready, so nothing stale ever queues up.
Two details made the difference:
- Negotiation is scored, not guessed. Every native media type is ranked once, before streaming: reaching the 30 fps plateau beats resolution, and at ≥720p MJPEG wins ties — a raw 1080p YUY2 frame is 4.15 MB, an MJPEG frame is ~0.2 MB. The camera that negotiated YUY2 @ ~5 fps now negotiates 1920×1080 MJPEG @ 30.
- --reverse is zero-admin. In the normal direction, guest→host vsock needs a one-time registry entry. With --reverse the host dials out to a listener in the guest instead, and no admin step exists at all.
Measured on a USB 2.0 UVC camera (Windows 11 + WSL2): ~19–20 fps delivered with default exposure, and 30.8 real fps with --exposure -6 — the remaining gap was the sensor's low-light shutter stretch, not the bus.
One honest limitation: frames arrive through the hvcam library / Python binding (numpy arrays), not through /dev/video0. If your code reads a V4L2 device today, this replaces the capture source, not the device.
Code, build instructions and the wire protocol are here: https://github.com/santleme/hvcam — happy to answer questions about the vsock/VmId pinning or the media-type scoring.



