openipc-rs: fpv video from a usb wifi adapter to a browser tab

neel saninov 23, 20269 min readrust · wasm · webusb · fpv · wifi

contents

openipc is open-source firmware for cheap ip cameras, and a lot of people use it for digital fpv. the camera on the drone encodes h.264 or h.265, and the video goes to the ground through wfb-ng, which injects raw 802.11 frames in monitor mode instead of joining a network. on the ground you need a realtek usb wifi adapter in monitor mode and something that turns those frames back into video.

openipc-rs is my ground station for that, in rust. the same receive pipeline runs as a desktop app, on android, and in a browser tab, where the usb adapter is driven over webusb. it's live at openipc-rs.neels.dev and the code is on github.

this post follows a frame from the usb cable to the screen, then goes through what broke.

the path#

  1. the adapter hands over usb bulk transfers full of received frames.
  2. a userspace driver parses realtek's receive descriptors and keeps only wfb's 802.11 frames.
  3. the wfb layer decrypts each fragment and uses forward error correction to rebuild missing ones.
  4. the fragments are rtp packets, and rtp becomes h.264 or h.265 frames.
  5. a hardware decoder turns those into pictures.

only the last step is platform-specific, so most of it lives in shared crates:

cratejob
openipc-rtl88xxthe usb wifi driver
openipc-corewfb parsing, fec, crypto, rtp
openipc-videohardware decoding per platform
openipc-webthe wasm bindings, published on npm as @openipc-rs/web
nebulusthe egui app on top: desktop, android, and browser

a wifi driver in user space#

on linux these adapters normally use a kernel driver. a browser can't load one, and it's awkward on macos, windows, and android, so this driver runs in user space and talks to the chip over usb control and bulk transfers: through nusb natively and nusb-webusb in the browser.

it supports twelve realtek chips, from the rtl8812au to the rtl8812eu. the code groups them into three generations (jaguar 1, 2 and 3), which need different init sequences and use different receive descriptor layouts. bringing one up goes:

  • claim interface 0 and find the bulk endpoints
  • probe the chip and download its firmware
  • read the efuse, then load the mac, queue, baseband, and rf tables for that board
  • set the monitor-mode filters, then the band, channel, and width

registers go through a vendor control request (0x05). the firmware and init tables come from openipc's devourer project. the driver isn't a wrapper around devourer, but it follows its changes, and the docs track over 40 devourer commits. one quirk: the usb id isn't enough to identify the chip, because an rtl8812eu can show up with a jaguar 1 id, so the driver reads a system config register (SYS_CFG2) to decide.

finding our frames#

received frames don't come with radiotap headers. the adapter batches them into usb transfers (32 kib each by default, with eight in flight), and every frame starts with a 24-byte realtek rx descriptor holding its length, rssi, and snr. the descriptor layout differs between the three generations.

wfb frames are ordinary 802.11 qos data frames. what marks them is the mac addresses: both start with 57:42 and end with a channel id, which is the link id shifted left by 8 plus a port (video is port 0). everything else on the air is dropped.

/// Marker bytes mirrored in the OpenIPC/WFB source and destination addresses.
pub const WFB_PREFIX: [u8; 2] = [0x57, 0x42];
/// Frame-control bytes for QoS data from station to distribution system.
pub const QOS_DATA_FROM_STA_TO_DS: [u8; 2] = [0x08, 0x01];

decryption#

the air unit and ground station share a key file (gs.key). every so often the air unit sends a session packet sealed with crypto_box. it carries a session key, an epoch, the channel id, and the fec parameters. the receiver rejects a session whose epoch goes backwards or whose channel doesn't match.

data packets are encrypted with chacha20-poly1305 under the session key. the 8-byte nonce doubles as the packet's position in the stream: the block index shifted left by 8, plus the fragment index.

let block_idx = data_nonce >> 8;
let fragment_idx = (data_nonce & 0xff) as usize;

forward error correction#

wfb injects frames without retransmission, so a lost frame stays lost. instead it sends redundancy up front. each block is k fragments of video plus n−k parity fragments, and openipc-rs decodes them with a vandermonde reed-solomon code over GF(2⁸), which can rebuild a block from any k of its n fragments. k and n come from the session packet. the default is 8/12, so any four of every twelve fragments can go missing without losing video, at the cost of 50% more airtime.

fec: rebuilding lost fragments
k/n
starting…
so far: 0 intact · 0 rebuilt · 0 lostat 10%: 57% of blocks miss data without fec, 0.4% can't be rebuilt with 8/12
each block is k fragments of video (gray) plus n−k parity fragments (orange). reed-solomon rebuilds the block from any k of the n, so video is only lost when more than n−k fragments go missing. 8/12 costs 50% more airtime.

the code is fixed for a whole session, so when a session starts the decoder precomputes the inverse matrix for every loss pattern it can recover from. for 8/12 that's 494 patterns, so rebuilding a block later is a table lookup plus a matrix multiply. the multiply uses simd: neon, ssse3, and wasm simd128 in the browser. on an apple silicon machine in july 2026, rebuilding one missing 3996-byte fragment took a median 0.812 µs, against 0.935 µs for zfex, the c library pixelpilot uses. that's only the fec math, not end-to-end latency.

if a whole block is lost, the receiver skips it once later blocks show the stream has moved on, or after 40 buffered blocks, so one bad block doesn't stall the video.

rtp to frames#

after fec, each fragment is an rtp packet: payload type 96 for h.264, 97 for h.265, 98 for opus audio. h.264 uses stap-a and fu-a packets, and h.265 uses ap and fu. a frame ends at the rtp marker bit, or at a timestamp change when the marker is missing. damaged frames are still passed to the decoder, which can usually conceal the damage better than dropping the frame.

decoding is all hardware:

platformdecoder
macosvideotoolbox, with frames imported straight into metal
linuxva-api
windowsmedia foundation with d3d11
androidmediacodec
browserwebcodecs

the browser version#

the browser build is the same rust compiled to wasm. javascript only has to call navigator.usb.requestDevice() from a click. after that the device handle goes into rust, and nusb-webusb claims it the same way the native backend does.

the main thread runs webusb, decryption, and fec. rtp packets go in batches to a worker that assembles frames and passes them over a MessageChannel to a second worker that owns the webcodecs decoder. decoded frames are drawn as webgl2 textures. the queues hold at most three batches and three frames, and a decoded frame isn't handed back until the previous one has been acknowledged, so a slow decoder drops frames instead of building up delay.

what broke#

  • the authentication tag. wfb uses libsodium's original chacha20-poly1305, which has an 8-byte nonce and doesn't pad the data it feeds into poly1305. my first version used the newer ietf layout, which pads both parts to 16 bytes. the encryption is the same in both, so only the tag differs, and with the wrong layout every packet fails authentication. the fix removed the padding and added libsodium's own test vector, so it can't come back.
  • a manufacturing-test table. the rtl8812au's baseband table was a 4-word override from realtek's manufacturing test mode instead of the normal 470-word table. i replaced it and added a test that hashes the table.
  • 20 fps on android. decoded frames were only collected when a usb transfer completed, and with a 50 ms idle wait, a 60 fps stream came out at 20. decoder output is now polled at least every 2 ms, independent of usb.
  • deaf on 2.4 ghz. the rtl8822c received nothing on 2.4 ghz until i added the vendor's last front-end register writes, matching the same fix in devourer.
  • divinus. some openipc cameras run divinus as their streamer. it uses an older h.265 fragmentation format and only sets the rtp marker on keyframes, so both needed handling.
  • the webusb picker. in chromium, closing the device picker quickly can leave requestDevice() pending forever, so a dismissed picker is treated as idle and the app falls back on the window regaining focus.
  • idle timeouts on macos. usb reads now stay posted with no timeout on the device side, which stopped a constant cycle of timeouts on macos when the link was idle. the driver also claims the device before resetting it, so a second app can't reset an adapter that's already in use.

what's left#

there's no glass-to-glass latency number yet. measuring it properly needs the same adapter, stream, decoder, and display filmed with a high-speed camera, and i haven't done that. the other big gap is captured hardware recordings: the mock video skips usb, decryption, and fec, so the full receive path can't be tested without a drone yet.

the earlier version was openipc-zig, a zig and wasm experiment that showed webusb receive could work in a browser at all. openipc-rs is a new implementation rather than a port: it supports twelve chips instead of two, has its own mit-licensed fec instead of the c zfec library, and runs natively and on android too.

questions or corrections? email me.