Friday, March 17, 2017

Streaming video from an MJPEG network camera to applications in Linux

I recently got a D-Link DCS-930L network camera. It outputs a motion JPEG (MJPEG) stream at http://address/video.cgi. Although there's no kernel driver for receiving that, it is possible to send it to applications from user space via v4l2loopback.

First you need to install the module. Debian and Ubuntu have it in the v4l2loopback-dkms. If your distribution doesn't have it, then you'll need to build it and install it yourself. Then, load the module. If you want Chrome to see it, load it with the exclusive_caps=1 parameter, like sudo modprobe v4l2loopback exclusive_caps=1 .

Then, run a program to read the video and send it to the loopback device. It's possible with ffmpeg, using something like this:

ffmpeg -f mjpeg -i http://user:pass@192.168.1.30/video.cgi -s 640x480 -vf format=pix_fmts=yuv420p -f v4l2 /dev/video0

Note user:pass, which would be the credentials needed to log in to the camera. If you put yours there, note that it's insecure, as ps shows command lines. Also note the resolution and video device, which you may need to change.

A similar method could be used if you wanted to read from one video device, apply effects, and then present the video with effects to an application.