In the sequel «Get the most out of your Ubuntu home server» and «How to do cool tech stuff rather inexpensively», this topic is about using webcams, and starting and stopping them remotely.
You can get a webcam literally anywhere. It’s fairly affordable, and they will most certainly connect through USB. And fortunately, that makes them compatible with any 10 year old hardware.
To use a webcam with, say, a Ubuntu computer, you would have to make sure that the webcam is UVC compliant (USB video device class). Quite often this feature isn’t even marked as a feature on the packaging, so you need to actually plug it in to know that it works. Actually, most «cheap» webcams actually support UVC. There are lists online of webcams supporting UVC. Or you can just bring your handy laptop to the store, and try for yourself. Running this command while connecting should give you sufficient indications if the webcam is supported:
tail -f /var/log/syslog
The above command works in Ubuntu 11.04. Before this, «messages» was used, not «syslog«. Anyways – what you’re looking for is something like this:
kernel: [ 3102.392028] usb 1-1: new full speed USB device using uhci_hcd and address 18 kernel: [ 3102.677343] uvcvideo: Found UVC 1.00 device USB2.0_Camera (093a:2700) kernel: [ 3102.686505] input: USB2.0_Camera as /devices/pci0000:00/0000:00:1f.2/usb1/1-1/1-1:1.0/input/input3
You can check that the cam actually works, by starting «cheese«.
Next, we want to stream the video – that is, to be able to have a URL that we can connect and view the video. For that we need a broadcast server, and we also need to decide upon a video format. For the sake of cross platform compatibility, I would choose Theora as video format, and Icecast as stream server. These two components come from the same people – that is xiph.org, and therefore you can trust that these things work pretty well together. And best of all – Theora is supported natively on most browsers like Firefox, Opera and Google Chrome, meaning that you can playback the video literally anywhere, without extra plugins. Even on Android phones, which may use the Firefox Mobile application, which similar to its mother software also supports Theora.
Icecast is installed from Ubuntu repositories as usual. You will have to edit the config-file at /etc/icecast2/icecast.xml, to add passwords and your desired listening ports and so on.
To get the video from your webcam to Icecast, we need to do quite a trick. First we use ffmpeg2theora to encode the raw video from the webcam. Then we use oggfwd (ogg forward ;)) to push this to our Icecast server. A typical command line would look something like this:
ffmpeg2theora /dev/video0 -f video4linux2 --no-skeleton --inputfps 15 -v 5.5 -x 320 -y 240 --noaudio -o - | oggfwd -d "<your description>" -n "<stream_name>" localhost <icecast-port-number> <icecast-relay-password> <desired-url>
Remember that the video source (/dev/video0) must be readable (!), which it is not by default. Chmod usually takes care of this. You may tune ffmpeg2theora parameters, i.e the resolution and encoding quality. The above command currently works with a Clas Ohlson webcam.
You should make sure that you have some way to toggle the streaming video. This can easily be done with a php script, launching a shell script like this:
exec('nohup sh stream1.sh > /dev/null 2>&1 &');
You may also add a detector to see if the script is actually running, by this command:
$p = exec('ps -ef | grep oggfwd',$out);
$is_it_really_running = (strpos($out[0],'<part-of-stream-name>') > 0);
That’s it! Universal video surveillance made affordable and robust.
(You may also look into udev scripts, to make sure that the video source /etc/video0 is readable at every reboot.)