On embedded Linux, the only hardware-specific code between a MIPI CSI-2 camera and a live video feed in a browser is the V4L2 sensor driver.

Let’s say you have a camera on a custom board (either an image sensor you built, or one you chose and designed around) and you need its video in a browser over the open web. The latency has to be low enough to act on, as in a robot an operator drives remotely, an inspection camera on a production line, or a medical device a clinician monitors from another room. Between that sensor and the browser tab sit a Linux kernel, a capture pipeline, and a handful of standards you now have to satisfy. 

Most of that already exists. Everything above the sensor driver (the debayer, the encoder, and the WebRTC transport that carries the stream to the browser) is standard infrastructure that accepts common pixel formats and works with any sensor upstream. The driver’s job is to make your sensor output those formats, using values from your datasheet.

We built the whole path on a Raspberry Pi 5 with a Sony IMX477 sensor (the sensor in the Raspberry Pi HQ Camera) and put the code on c-camera-driver-webrtc. The Pi is the bench and the IMX477 is the stand-in for your sensor. Every sensor-specific value in the driver (I2C address, lane count, link frequency, register sequence) is the kind of number a sensor vendor publishes.

Writing a Minimal V4L2 Sensor Driver

How a Camera Sensor Driver Fits the V4L2 Graph

On embedded Linux, a camera is not one device. It is a small graph of entities managed by V4L2 (Video4Linux2), the kernel’s API for video devices:

  • the sensor, the only piece specific to your hardware;
  • a CSI-2 receiver that pulls pixels off the wire;
  • an image signal processor (ISP) that turns them into a usable image;
  • a capture node that hands frames to userspace as a /dev/video* device.

The last three live on the board’s SoC, the system-on-a-chip that bundles the CPU with blocks like the CSI-2 receiver and ISP onto one package (on the Pi 5, this is the BCM2712). Your driver contributes just the first one, a V4L2 sub-device representing the sensor, and the kernel wires it to the rest.

A sensor sub-device implements a set of operations the CSI-2 receiver calls, and there are fewer than you might expect. You advertise the formats the sensor can produce, report and accept a format, and start and stop streaming. On a current Raspberry Pi kernel those operations register like this:

static const struct v4l2_subdev_pad_ops minimal_pad_ops = {
	.enum_mbus_code  = minimal_enum_mbus_code,   // which pixel formats the sensor offers
	.enum_frame_size = minimal_enum_frame_size,  // which resolutions, for a given format
	.get_fmt         = minimal_get_fmt,          // report the current format
	.set_fmt         = minimal_set_fmt,          // accept a requested format
	.get_mbus_config = minimal_get_mbus_config,  // CSI-2 bus details for the receiver
	.enable_streams  = minimal_enable_streams,   // start the sensor streaming
	.disable_streams = minimal_disable_streams,  // stop it
};

Each field points at one of the driver’s functions. Filling in this table is how the driver registers its behavior with the kernel, which calls these operations as the pipeline needs them, and filling it in is where your sensor’s datasheet comes in.

You have two ways to get a V4L2 sensor driver for your hardware: write one for your sensor, or extend an existing driver if your sensor is close enough to one the kernel already supports. Our driver is a standalone module, with every moving part in one place, but your starting point depends on your hardware, something we come back to at the end.

Device Tree and Register Values for the Camera Sensor Driver

The hardware-specific values live in two places. The first is a device-tree overlay, which describes the sensor to the kernel:

  • its address on the I2C control bus, the side channel the driver uses to configure the sensor;
  • how many MIPI CSI-2 data lanes carry the pixel stream;
  • its input clock and the port/endpoint graph that links it to the receiver.

The sensor node holds exactly those values. The compatible string is what the kernel matches against the driver; everything else describes the wiring, including the CSI-2 link frequency:

minimal_imx477: minimal_imx477@1a {
	compatible = "agilityfeat,minimal-imx477";   // matches the driver
	reg = <0x1a>;                                // I2C address on the CAM bus
	clocks = <&cam0_clk>;                         // 24 MHz input clock
	clock-names = "xclk";

	port {
		endpoint {
			data-lanes = <1 2>;                   // two CSI-2 data lanes
			link-frequencies = /bits/ 64 <450000000>;   // 450 MHz, from the datasheet
			remote-endpoint = <&csi0_ep>;         // links to the CSI-2 receiver
		};
	};
};

That link frequency also has to reach the receiver from the driver side: a CSI-2 sub-device exposes two read-only controls, V4L2_CID_LINK_FREQ and V4L2_CID_PIXEL_RATE (450 MHz and 840 MHz here, from the datasheet), that the receiver reads to program its end of the link.

The second place is the register sequence: an ordered list of {register, value} writes that initializes the sensor and selects a mode.

The sequence for the IMX477 looks like this:

static const struct mi_reg imx477_mode_1332x990[] = {
	{ 0x0136, 0x18 },   // input clock: 24 MHz
	{ 0x0137, 0x00 },
	{ 0x0138, 0x01 },
	// ... ~400 more init + mode registers ...
	{ 0x0112, 0x0a },   // CSI-2 output depth: 10-bit
	{ 0x0113, 0x0a },
	{ 0x0202, 0x08 },   // exposure, { 0x0203, 0x20 }
	{ 0x0204, 0x03 },   // analogue gain, { 0x0205, 0x80 }
};

For a custom sensor, these values come from the sensor’s register documentation. Getting a mode to stream means turning them into a table like this and working out the order the sensor expects (we generated ours with scripts/gen_regs.py from the above repo, which decodes an i2c_write trace of the stock driver streaming this mode into exactly this array).

Starting the Stream from the V4L2 Sensor Driver

Most of the sub-device operations from that first snippet are small and mechanical: report the one fixed format, enumerate the single mode, hand back the bus config. The one that does real work on the hardware is enable_streams. It runs when the pipeline starts, and it is where the overlay and the register sequence come together: it powers the sensor, writes the sequence over I2C, and issues the stream-on register as the last write, only then reporting the sensor as streaming.

Writing the table is a plain loop, applied in order and stopping at the first write the sensor doesn’t acknowledge:

static int imx477_write_regs(struct i2c_client *client, const struct mi_reg *regs, size_t n)
{
	size_t i;
	int ret;

	for (i = 0; i < n; i++) {
		// IMX477 register writes are order-sensitive; apply them exactly as given
		ret = imx477_write_reg(client, regs[i].addr, regs[i].val);
		if (ret)
			return ret;   // stop at the first unacknowledged write
	}
	return 0;
}

enable_streams ties it together:

static int minimal_enable_streams(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
				  u32 pad, u64 streams_mask)
{
	struct minimal_imx477 *mi = to_minimal(sd);
	int ret;

	ret = minimal_power_on(mi);                        // rails + input clock, so I2C is answered
	if (ret)
		return ret;

	ret = imx477_write_regs(mi->client, imx477_mode_1332x990, IMX477_MODE_1332x990_LEN);
	if (ret)
		goto err_power_off;                        // a bad value here means no stream

	ret = imx477_write_reg(mi->client, IMX477_REG_MODE_SELECT, IMX477_MODE_STREAMING);
	if (ret)
		goto err_power_off;

	mi->streaming = true;
	return 0;

err_power_off:
	minimal_power_off(mi);
	return ret;
}

With the sensor bound and streaming, the CSI-2 receiver writes the incoming frames into memory buffers (the driver never touches pixel data) and you have a /dev/video* node producing RAW Bayer: the sensor’s raw output, one color sample per pixel in a repeating red/green/blue grid. That is a mosaic, not a finished picture, which is where the standard infrastructure above the driver takes over.

From RAW Bayer to the Browser with GStreamer and WebRTC

A color sensor gives you one color per pixel in a repeating grid. Turning that into a viewable image (interpolating the missing two colors at every pixel) is debayering, and it is the first thing that has to happen above the driver. On a production board you route the RAW frames through your SoC’s ISP, which debayers in hardware; the Pi has one (PiSP), as do Jetson (NVIDIA ISP) and Rockchip (RKISP). The demo debayers with a stock GStreamer element, which keeps the step as its own stage.

From there, the path to the browser is a chain of standard stages, each consuming the previous one’s output:

Diagram of the video pipeline from a V4L2 sensor driver to a browser: RAW Bayer capture, debayer, H.264 encode, RTP packetizing and WebRTC.
The path from sensor to browser. Only the V4L2 driver at the start is custom; every stage after it is standard GStreamer and WebRTC.

In our sender this chain is built as a single GStreamer WebRTC pipeline string. The driver is the only custom piece anywhere in it:

// v4l2src reads /dev/video0 — the node our driver backs — as 16-bit Bayer
"v4l2src device=/dev/video0 ! video/x-bayer,format=rggb16le,width=1332,height=990 ! \
 bayer2rgb ! videoconvert !                        // debayer: Bayer mosaic -> RGB
 videoflip method=rotate-180 !                     // the camera is mounted upside down
 videoconvert ! video/x-raw,format=I420 ! \
 x264enc tune=zerolatency bitrate=4000 !           // encode H.264 (software on the Pi 5)
 video/x-h264,profile=constrained-baseline ! \     // a profile browsers accept
 h264parse ! rtph264pay !                           // packetize as RTP
 webrtcbin name=webrtc"                             // hand the track to WebRTC

Nothing in that chain knows or cares which sensor is upstream, which is the point of the standard formats. Note that on the Pi 5, the encoder runs in software. This is because there is no hardware video encoder in it (and it’s why the demo tops out around 25.8 fps at 1332×990). On an SoC with a hardware encoder, that stage is a V4L2 encoder in the same slot, and the rest of the chain is unchanged.

WebRTC Signaling Between webrtcbin and the Browser

A pipeline that ends in webrtcbin still isn’t a connection. webrtcbin can produce and send media, but it can’t reach a browser on its own: the two peers first have to exchange an SDP offer and answer (what each side can send and receive) and trickle ICE candidates (the network routes between them), over a side channel you provide. Our sender-rs is the program that provides it, one small binary with three jobs:

  • Serve the page. It answers plain HTTP for the two static files (index.html, app.js) the browser loads.
  • Carry the signaling. When the page opens a WebSocket back, the sender relays the offer, answer, and ICE candidates between webrtcbin and the browser.
  • Run the media. It builds the pipeline above, pulls the offer out of webrtcbin, and sets the pipeline playing.

The signaling itself is a few lines of relay, forwarding what each side produces to the other:

// webrtcbin produced an offer -> set it locally and send it to the browser
webrtc.set_local_description(&offer);
ws.send(SignalMsg::Offer { sdp: offer.sdp().as_text()? });

// a message arrived from the browser over the WebSocket
match msg {
    SignalMsg::Answer { sdp } => webrtc.set_remote_description(&sdp), // the browser's reply
    SignalMsg::Ice { candidate, .. } => webrtc.add_ice_candidate(&candidate), // a route to it
}

Streaming to Multiple Viewers with an SFU

Since the camera node is single-consumer, our sender builds one pipeline and serves one browser at a time, so a second connection tears down the first pipeline (releasing /dev/video0) before starting its own. That keeps the demo simple, but this approach only allows for a single-operator view of one camera.

A product that fans a camera out to many viewers inverts this: you capture and encode once, then share that encoded stream across viewers, rather than opening the sensor per connection. In practice that means a media server such as Janus, mediasoup, LiveKit, or any other SFU (or a MoQ publisher) sitting where the browser sits in our diagram, taking one feed from this pipeline and distributing it. The capture, debayer, and encode stages are unchanged; what grows is the fan-out layer above webrtcbin.

With those pieces in place it runs end to end: the video coming from a V4L2 driver we wrote from scratch, through a pipeline of standard parts we didn’t, all the way to a browser.

Live MIPI camera feed in a browser, streamed from a custom V4L2 driver over WebRTC
The payoff: live video from our from-scratch V4L2 driver playing in a Chrome tab on the Raspberry Pi 5 bench at ~25.8 fps, delivered end to end over WebRTC.

While this plumbing is specific to Raspberry Pi camera streaming, the same structure applies on Jetson, Rockchip and other SoCs.

Scoping a Camera Bring-Up for Your Hardware

Because the V4L2 sensor driver is the only hardware-specific piece, the size of a camera bring-up comes down to how close your sensor is to something the kernel already handles. It falls on a ladder, cheapest first:

  1. Already supported. Your sensor has a mainline driver. The work is device-tree configuration for your board, with no driver code.
  2. A close variant. Your sensor shares a register model and family with a supported one. Adding a compatible string and a mode table to the existing driver can be enough.
  3. A new sensor in a known family. No existing driver fits, but a similar sensor’s driver gives you the structure to adapt: swap the register tables, mode geometry, and timing values.
  4. A genuinely novel sensor. A new interface or behavior that no reference covers; the most from-scratch work.

Establishing where your sensor sits is the first thing a bring-up does, because it sets the whole budget. From there, a production path adds what the demo leaves out: the full mode table so you can switch resolutions and frame rates, and the hardware encoder path your SoC supports so throughput isn’t capped by software encode. Color is its own line item: the ISP yields a viewable frame from any standard-Bayer sensor, but a correct one (ours skews green without it) needs per-sensor ISP and 3A tuning.

Building applications that deliver media to your users is what we do at WebRTC.ventures. If you’re building something that moves live video, audio, or arbitrary data from a device to the people watching it, let’s talk!

Further Reading:

Recent Blog Posts