WebCam-server-tutorial

This tutorial explains how to setup a webcam server using the webcam-server package. I have written a daemon script since webcam-server does not provide one. I will provide it to you. This tutorial comes from my blog. I will provide the link to the original post (http://hacktivision.com/index.php/2009/06/16/setting-up-an-ubuntu-webcam-server?blog=2), but note, I am simply elaborating on my original writing.

I used Ubuntu 9.04 and a Logitech QuickCam Communicate MP (http://hacktivision.com/index.php/2009/06/11/ubuntu-webcam?blog=2) to create this tutorial. It is a good idea to see if your webcam is working by using “Cheese Webcam Booth” or “Camorama Webcam Viewer”. Both can be installed using “Add/Remove…” in the main menu in the task bar.

So let’s get started!

1. Installing the webcam-server package

The webcam-server package can be installed by searching with synaptic if you prefer to use a GUI. The command line is

sudo apt-get install webcam-server

Enter your password if prompted. Answer yes with a “y” if prompted.

2. Create the startup script.

Use your favorite text editor to create and edit the file /etc/init.d/webcam-server. You will need to be root or use sudo. This command will create and open the file in nano:

sudo nano /etc/init.d/webcam-server

Once open, paste the following text into the file:

#!/bin/sh

SERVER_BIN=webcam-server
LOCK_FILE=/var/lock/$SERVER_BIN
RTRN=0
OPTIONS=’-v -g 320×240 -p 8888 -c hacktivision.com’

start() {

[ -f $LOCK_FILE ] && echo “$SERVER_BIN already started”
[ -f $LOCK_FILE ] && return

echo -n “Starting $SERVER_BIN: “
export LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so
nohup $SERVER_BIN $OPTIONS > /dev/null 2>/dev/null &
RTRN=$?
[ $RTRN -eq 0 ] && echo Started! || echo FAIL
[ $RTRN -eq 0 ] && touch $LOCK_FILE
}

stop() {
[ -f $LOCK_FILE ] || echo “$SERVER_BIN is not running”
[ -f $LOCK_FILE ] || return
echo -n “Stopping $SERVER_BIN: “
pkill -f “$SERVER_BIN $OPTIONS”
RTRN=$?
rm -f $LOCK_FILE
[ $RTRN -eq 0 ] && echo Stopped! || echo FAIL
}

case “$1” in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo “Usage: $0 {start|stop|restart}”
RTRN=1
esac

exit $RTRN

Please note that the line

export LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so

is required for some webcams, but you may be able to remove it if your webcam does not require it. The workaround was found at https://bugs.launchpad.net/ubuntu/+source/amsn/+bug/314805.

3. Set permissions and link the startup script

You will need to set permissions and create a link in the /etc/rc3.d directory that points to the script. Here are the commands to do so:

sudo chmod +x /etc/init.d/webcam-server
sudo update-rc.d webcam-server defaults

Again, enter your password if prompted.

4. Test your webcam and startup script

It’s a good idea to test if your startup script and webcam are working at this point. You can do this by running

sudo /etc/init.d/webcam-server start

and then using your browser to view http://localhost:8888/. You should see a snapshot from your webcam. Do a full refresh to see an updated picture. If you do not see a picture, or get an error, consider adding or removing the “export LD_PRELOAD=…” line from your script. If that does not work, please reply with your findings and I will try to help.

5. Install Apache HTTP Server

Up to this point, you can view snapshots from your webcam locally, but you probably want to share your webcam with the world (or at least some friends). To do this, you will need to install Apache (or another web server if you choose).

To install apache run

sudo apt-get install apache2

Accept all the default options, or if you are an advanced user, configure Apache as you so choose. Apache will be started when installation is complete. Also, your webroot, or the directory that Apache will share.

*You may need to forward port 80 on your router to your computers IP address.

6. Copy the webcam-server html and applet to your webroot

The webcam-server package comes with html and java files that will allow visitors to see a stream of images from your webcam. The webpage uses an applet to do this.

To copy the files to your webroot run

sudo cp /usr/share/doc/webcam-server/applet/* /var/www/

To test if you copied the files correctly, and if Apache is working, goto http://localhost/webcam.html.

You should see a stream from your webcam. If you do not, make sure several files have been copied to your webroot, including webcam.html and some jar and gz files.

Conclusion and tips

You are now setup to stream your webcam on a webpage, but there is more to do to share it with the world.

The default webcam.html file is setup to only run at 1 frame per second and only be server to localhost, meaning only on your computer. It can be edited to server at a domain or from an IP address though. You can also add your own HTML to customize the page that is streaming your webcam.

Here is an example of what the webcam.html file would look like on my website, hacktivision.com (http://hacktivision.com/) at 60 frames per second:

<html>
<head>
<title>WebCam</title>
</head>
<p align=”center”>
<a href=”http://hacktivision.com” title=”hacktivision.com – Ubuntu webcam server”>Hacktivision</a>
</p>
<div align=”center”>
<APPLET CODE = “WebCamApplet.class” archive=”applet.jar” WIDTH = “320” HEIGHT = “240”>
<param name=URL value=”http://hacktivision.com:8888″>
<param name=FPS value=”60″>
<param name=width value=”320″>
<param name=height value=”240″>
</APPLET>
</div>
</body>
</html>

Credits

I wrote the original article which can be found at http://hacktivision.com/index.php/2009/06/16/setting-up-an-ubuntu-webcam-server?blog=2.

I found the workaround used in the startup script at https://bugs.launchpad.net/ubuntu/+source/amsn/+bug/314805.

And of course, much of my Linux knowledge was attained right here at Ubuntu Forums.