Monday, March 16, 2009

Streaming Webcam Tinkering

Last Friday a friend showed me his chicken-cam. He got a wireless IP webcam and mounted it looking down into his chicken coop (currently just baby chicks in an indoor cage). That got me thinking (and something to do over the weekend). I had an unused PC sitting around that already had Ubuntu installed on it, and an old but decent webcam that I hardly use. I figured it wouldn't be too hard to put them both together and get some streaming video up and running. We've got some cockatiels sitting on some eggs right now. I could mount the camera there.

I had to make a trip into Portland on Saturday for a coin show (for David's coin collecting merit badge) and I was right next to Free Geek so I made a stop there to visit the thrift store. I picked up a couple USB webcams ($3 each) so I could use those instead of the better one I already have. These will be going into or on a bird cage and I'm not sure how well I'll be able to protect them...if you know what I mean. (I also picked up a 250GB drive for my Windows server for just $35, but that's not related.)

Stage 1: Getting the camera working in Ubuntu

Piece of cake. I plugged it in to the USB port. Done. Well, I actually did install some software just to make sure it worked.
   sudo apt-get install camorama

Camorama is just a simple webcam viewer. That's really all it does.

Stage 2: Install a webserver
   sudo apt-get install apache2 php5

I don't really have need of the PHP right now. I'm just slapping together some HTML pages to get things going. So, webserver...done.

Stage 3: Install Motion

Motion is a webcam application for linux that turns a simple webcam into a motion activated security camera. When it detects motion it will save still images and video to the hard drive for later viewing. It can be setup to take intermittent still shots. It will do timelapse videos. It even has a built in webserver for streaming video. Installing this was also a piece of cake in Ubuntu. I guess it's popular enough that it's available in the Ubuntu repositories.
   sudo apt-get install motion

Some minor modifications to the configuration files was needed. (Config files located in /etc/motion) The following changes were made, everything else was left as default. Explanations of each setting are giving in the config file. I set it up to run with 2 webcams.

   daemon on
framerate 2
max_mpeg_time 600
output_normal best
output_motion off
ffmpeg_cap_new on
ffmpeg_timelapse 60
ffmpeg_video_codec msmpeg4
snapshot_interval 60
target_dir /var/www/motion
webcam_port 8081
webcam_localhost off
control_port 8080
thread /etc/motion/thread1.conf
thread /etc/motion/thread2.conf

   videodevice /dev/video0
text_left CAMERA 1
target_dir /var/www/motion/cam1
webcam_port 8081

   videodevice /dev/video1
text_left CAMERA 2
target_dir /var/www/motion/cam2
webcam_port 8082

Running motion:

sudo motion -n

The -n option forces it into non-daemon mode (where it doesn't release the console so I can turn if off easier). I'll set it up to run automatically in the background once I get it setup exactly where I want it.

At this point I just started goofing around with it and having fun playing with the motion capture portion. Basically acting like a 13-year-old with some "spy" toys.

Once everybody was sick of me and I had had my fun it was time to move on to...

Stage 4: Get it streaming

The harder part.

It took some scouring of the web and looking at the code behind some example web pages to get this going. But with a little trial and error things worked out with less effort than I thought it was going to take.

I actually did get it streaming right away after installing Motion. But it didn't work quite right on Firefox (my browser of choice) and it wasn't embedded into a webpage the way I wanted it to. There was also no way it was going to be accessible outside my network without poking a bunch of holes in my firewall. No thanks. So I had to find another way. Other people were doing it. I figured it couldn't be all that hard.

Stage 4a: Installing a Java applet to make a clean and embedded stream.

The creator of Motion recommended Cambozola. I was thrown off by the install. It just involved a simple "move this directory to where you want it." Not what I was expecting, but it worked. I moved the whole thing to my /var/www directory.

The following code gets a nice looking stream onto a web page.

<applet code=com.charliemouse.cambozola.Viewer
width=325 height=245
style="border-width:1 border-color:gray; border-style:solid;">
<param name="url" value="">

Stage 4b: Get it working outside the network

The problem with the above code is that end value "". Any computer that is not the computer running Motion won't find anything at that address. I could change the address to the IP of the computer running Motion but then it only works inside my network. If I want to see the stream from anywhere outside my network I had to find another way.

Fortunately, the creator of Motion also made a nice little app that proxies the Motion webserver. MjpegProxyGrab. This install was very easy. Just follow the instructions on that page.

One mistake I made was not knowing where my cgi-bin was. I thought I could just make a cgi-bin in the /var/www directory. But when I tried viewing the web page I just got an error that the application couldn't be found. I dug through the server error log (/var/log/apache2/error.log) and found that it was looking in the wrong place. Instead of trying to redirect everything to the cgi-bin that I put in the wrong place I just moved the apps to the right bin in /usr/lib/cgi-bin. After that things were working perfectly. Use the new code to embed in a web page.

<applet code=com.charliemouse.cambozola.Viewer
width=325 height=245
style="border-width:1 border-color:gray; border-style:solid;">
<param name="url" value="/cgi-bin/nph-mjprox?1">

I do still need to figure out how to get the digital certificate warning to go away though.

Stage 5: Locate cameras and begin streaming

Actually, this stage is yet to come. I don't want to make too many changes to the nest right now while the birds are still tending the eggs. We don't want them to panic and abandon them. So I'm going to do this part later. Maybe after the eggs hatch. Meanwhile, I've got to find some other use for this new toy. Maybe we'll also do a butterfly-cam soon.

I also verified that the cameras can see infrared (I just pointed a TV remote control at the camera and could see the light emitted by the IR LEDs), so I plan to make a ring of IR LEDs to go around the lenses of the cameras for night viewing inside the bird nest. I just need to verify that cockatiels don't see infrared so they won't be disturbed by it.


  1. I recently came accross your blog and have been reading along. I thought I would leave my first comment. I dont know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.


  2. Thanks Joannah. Good to see another Linux tech-head making a visit. I think that makes you reader #2. ;)

    I'll have to go check out Linux Memory. Looks interesting.

  3. I enjoyed the read as well. I'm trying to do something similar but for different reasons. If you ever get a chance I'd like to hear how you finished up the project.