Live Webcam with Linux and Free Software
Abstract: How to make a live webcam feed using freely available software. The clients do not need anything except a browser which is reasonably standards compliant.
Why?
We first begin with the motivation for this project. Why would someone spend time and effort in putting a live webcam feed of his house? There are several motivations, and you might have your own.
- Security. You might want to monitor some place, like your garage, or front door, or a room with some super valuable things.
- Safety. You might have a really tiny baby: the less than one year olds that look like little rats, and spend nearly all their time sleeping. In some cases, such babies should not be woken up when they're asleep, but you'd still like to look and make sure that they are comfortable and well. Same for pets. Especially if you have cats and want to see them play in their room without having to drive home, and look about.
- Monitoring. You might be running a physical experiment that takes a few days and needs to be monitored. You might be running a chemical experiment that you want to monitor without harm to yourself.
- Personal contact. Keeping in touch with family and friends when you are living far away from them. This is why I use the webcam: so my family and girlfriend can see me from time to time.
- Motion detection. This is far cooler, and I'll write another article about this.
What do I need
The basic list of requirements is quite small.
- Network: If you plan to have a web feed, then clearly you need a fast connection to the Internet. Anything that gives 56kbps should be good in theory, but try not to get dialups. It is better to have an always-connected interface. A connection to the Internet is required only if you want the feed to be available on the Internet. If you have a huge home network, and a huge home, you can do this so you can see other parts of the house. If you plan to have an ethernet network, either to the Internet or for home, pick up a supported ethernet card. Most ethernet cards are supported under Linux.
- Computer: Any computer capable of doing USB should be good. However, webcams generate a lot of bus traffic, so if you have a slow machine, don't plan to use it for anything else. Creation of a jpeg image is also CPU intensive. On my 700Mhz Celeron, the entire setup takes about 20% of the CPU all the time. Plan to lose a serious chunk of your machine if it is a slow one. If it is a fast machine, then plan to have one process running on it at all times. Requirements on the CPU can be reduced. Continue reading.
- USB webcam: These are made by a lot of companies like Logitech, Creative, Labtec and the like. Before purchasing make sure that the particular camera is supported under Linux. Logitech, Philips and other manufacturers have a history of not releasing specs unless soul selling NDAs are signed first. This might be the hardest part of the whole process: getting a supported camera. I use a Logitech QuickCam Messenger, which is supported with the source available. Look through the Quickcam Express site, this old HOWTO, and through the net with Google. I'll accept testimonials from people if you include your webcam name, model, USB ID string (do an "lsusb"), and location of the driver (source only, please). If you have a camera which is kinda-sortof-notreally supported, mail the company from whom you bought it, and tell them you're never going to buy their product again. If you pay for the hardware, you are entitled to run it on software of your choice.
Alright, so that's all the hardware you need. If you have a webcam which is not supported, and you have bought it from the store, you might be able to return it for a full refund.
Setup
The setup follows many steps, with a checkpoint at the end of a step. If you cannot complete a checkpoint, stop, and rectify the error before proceeding.
- Install Linux with editors (emacs/vi), compilers (gcc), and the X-windows environment on your computer. The choice of desktop does not matter. Verify this setup by logging in as root, running emacs, running gcc, and running an xterm inside X-Windows.
- Setup your computer for networking. This means the following:
- If you plan to do networking through a telephone line, setup your modem with wvdial, kppp, or whatever you plan to use.
- If you plan to do networking through ethernet: for broadband, LAN, or what have you, setup your network card to connect to the Internet.
- Download the kernel sources for the version that you plan. I would suggest the latest kernel version, unless you have your reasons. You might need them, you might not. Verify this part by running 'make menuconfig' in the kernel directory, and building a kernel.
- Still with me? Great. Now is when the fun begins. Now attach the USB camera, and find out if it is supported. If it is supported, then you are done. A supported camera usually gives happy messages in /var/log/messages and /var/log/syslog about how it was detected, and how some driver is very happy with it, and will provide support for it. If this has happened, you are good. Go on to the next step. Chances are that this doesn't happen right off. In this case, you have to use your Internet connection to download the appropriate kernel modules which will support your camera. Remember I told you that you need to make sure that your camera is supported and there is a driver available with source. Now is the time to go get that driver, untar unzip it, and follow its instructions. Every driver has its own set of instructions, so I cannot possibly cover them all here. If you have some trouble, look through the README files, and ask questions on the developer mailing lists. When asking questions, be polite, since the developers don't owe you anything. Provide as much documentation as possible, which includes linux kernel version number, gcc version number, device ID, webcam driver version number. Verify this setup by running "camstream", and looking at your oh-so-wonderful mugshot in the screen.
- Now install apache on your machine, or some webserver which can serve static files. Tiny http servers are great, if you have a dinky computer. We will only be serving static content, so if you don't need it for other things, you can turn off fancy features like mod-php, mod-perl, and CGI. Don't need it. Verify this setup by changing the main page of your website to your sweetheart's name, and asking the sweetheart in question to access it remotely. In case your sweetheart doesn't have opposable thumbs: a turtle, a cow, a rabbit, go to some other computer on the Internet: a cybercafe, a University, a Friend's house, and verify that the page is visible on the open Internet.
- Install the fine program called motion. It is available at Motion's fine website but you might be able to get it from your Linux distributor's CD, website, or other places. If required, download sources, compile, and install. Verify this setup by running 'motion' on the commandline as root. Some messages should flash by, and you should see longer messages when you move in front of the camera.
- Now use my motion.conf file. This goes in /etc/motion/, but look through your motion setup to verify that the location of the file is correct. This makes motion take a single picture every 5 seconds, and put it in a directory where your webserver should be able to serve it on the Internet. 'motion' should be run as root, either as a daemon '-d' or you can pipe the output to /dev/null and run it in the background. Verify this setup by running motion, and using another computer to see the static image in a browser by giving the right path. Note: If you want to reduce the requirements on your CPU, you could make it take pictures less often, or you could go through the motion manual page to find out how to take pictures as png files and not jpg files.
- Optional If you do this setup, motion will write a jpeg
file to your disk every five seconds. On old disks, this kind of
hammering can push them over the edge. Also, you probably want to
avoid this disk traffic. One trick is to make a ramdisk, and ask
motion to write the picture on the ramdisk. The pictures I get from
motion are around 19k, so a ramdisk of 1M should suffice. Do the
following steps, all as root:
# insmod rd # <- In case you need to put the module in the kernel # dd if=/dev/zero of=/dev/ram0 bs=1M count=1 # <- Make the ramdisk of 1M # mkfs.ext2 /dev/ram0 # <- Make an ext2 filesystem on it # mount /dev/ram0 /var/www/YOUR/DIRECTORY # <- attach to filesystem
Now when you run motion, it will write only to the RAM, thinking that it is a disk. This will make the write quicker, and will save your disk from too much traffic. - Web page: Since we don't want to make any claims on the clients, it would be best if they were just plain browser boxes. Download my web-page framework, and modify just the basic stuff to reflect your settings. What I really like about this setup is that the client needs only a javascript enabled browser, the page doesn't reload everytime: only the image. And as a bonus, the client can set his refresh frequency, in case their connection is too slow, too fast... Verify this setup by asking your friends to look through the website, and by waving at the camera. You could also mail me about your webcam, and I'll verify that I can see you. :)
Credits
This work builds on the work of a huge community of happy hackers. I just put this page together. The real work was done by all these people:
- CITA staff at UIUC, and Cowbow at cowbowscripts.org on whose open source Javascript stuff I based my javascript page.
- The fine, fine folks at Apache, who are still whipping IIS's soft tushy.
- Mathias Kuester and Christian Magnusson for making the excellent Quickcam messenger driver, which I'm using.
- Georg Acher, Jean-Frederic Clere, for making the first Video 4 Linux drivers, upon whose work so much driver development is based.
- Kenneth Lavrsen and Jeroen Vreeken for making motion, a fine tool.
- Linus Torvalds, and Richard Stallman for starting the Linux kernel and the GNU toolset without whom this community wouldn't exist.
This page was made by a noisy rabbit. You can reach me at "vikram-web at mayin dot org". This page was last updated at: Thu Oct 13 09:41:11 PDT 2005