home   articles   archive   forum   masthead  
Published at 14.10.2000
Author: Ronny Ziegler
Translator: Andy Ziegler
Languages: de nl
Printer printer-version
Support Us!
 

Webcam without a Webcam

Webcam Webcams are as nice as unnecessary. Nearly all new cameras use the USB port and a special protocol so the Linux world is closed for these. Nevertheless if you wanted to use a webcam you should try it with a TV card.

Webcam or TV card

Just a handful of webcams are supported by Linux so you should inform yourself if a webcam worked with Linux or not.
Owner of a TV card and a video camera do not have so much trouble.
But already without a camera many nice things can be done, like streaming videos into the network or just the actual TV program.
Wouldn't it be nice if you showed your latest holiday video to any relative in a foreign country via Internet (enough bandwidth recommended).
At least in your LAN you are able to view the TV via web browser on a PC without any TV card.

The Software

The used software backend is the program bttvgrab in the version 0.15.5 (get it from http://moes.pmnet.uni-oldenburg.de/bttvgrab/).
This program has a very simple steering and the feature that it can be started from the console. So it is ideal for calls out of scripts or other programs.
After unzipping the program to the /tmp directory

  >> tar zxvf bttvgrab-0.15.5.tar.gz -C /tmp/
  
three commands are enough
  >> cd /tmp/bttvgrab-0.15.5/
  >> ./configure
  >> make
  
to compile the program. Then you copy the complete directory to /usr/local
  >> cp /tmp/bttvgrab-0.15.5/ /usr/local/
  
(A make install is not recommended because we will use bttvgrab in a script and we do not need to spread the files over the whole system.)

If you wanted to start grabbing the actual TV screen you would need a call with the format (executed in the directory /usr/local/bttvgrab-0.15.10/)

  >> bttvgrab -f webcam:/tmp/bttvgrab.jpg -s <sleeptime> -w <width> -o jpg -Q
  
SLEEPTIME sets the time interval which has to be waited until the next actual TV picture is taken. This depends on the "object" you want to record. For your fishes a value of 5 to 10 seconds should be enough.
WIDTH sets the size of the picture and should be between 32 and 768.
The remaining options are for:
-f webcam:/tmp/bttvgrab saves the recorded picture with this file name. -o jpg creates pictures in the JPEG format and -Q disables any TV sound record.

A call that makes sense could look like:

  >> bttvgrab -f webcam:/tmp/bttvgrab.jpg -s 5 -w 640 -o jpg -Q
  
The program responds with the following text:

Output: bttvgrab
  +-< bttvgrab 0.15.10 [2000-03-21] (v4l) by J. Walter/A. Kopacz >---------------+
  |									       |
  |									       |
  |------------------------------------------------------------------------------|
  |                                                                              |
  |  Image Nr.: 8                        Time: 0:0.43                            |
  |        FPS: 0.2 (0.2)         Frames lost: 11                                |
  | Image size:  640x480                                                         |
  |                                                                              |
  |     Status: Working...                                                       |
  |                                                                              |
  |------------------------------------------------------------------------------|
  |                                                                              |
  |                                                                              |
  |  Keys:  Q     - Quit                                                         |
  |         P     - Pause                                                        |
  |         S     - Sync HDDs                                                    |
  | Grabbing into: /tmp/image-new [19]                                           |
  |                                                                              |
  |                                                                              |
  |                                                                              |
  |                                                                              |
  |                                                                              |
  +-----------------------------< http://moes.pmnet.uni-oldenburg.de/bttvgrab/ >-+
  


An increasing Image Nr. shows you that everything works fine. In addition you see that the program creates the file /tmp/image-new while bttvgrab.jpg is a link to this file.

Into the Web...

Until now we have set a regular recording of the actual picture. In the next step we have to give access to the web users for this picture.

Therefore we create a new directory on the web server (we suppose that the main directory for the web server is at /usr/local/httpd - there it is in SuSE distributions).

First you create the directory webcam where all files will be placed.

  >> mkdir /usr/local/httpd/htdocs/webcam
  

The easiest way to put the actual picture of bttvgrab on the web page would be a meta tag.

   <meta http-equiv="refresh" content="5">
  
This reloads the page every 5 seconds. Every new load of the page would automatically reload the picture.

webcam.html
  <HTML><HEAD><TITLE>bttvgrab WebCam</TITLE></HEAD>
          <meta http-equiv="pragma" content="no-cache">
          <meta http-equiv="expires" content="0">
          <meta http-equiv="refresh" content="5"> <!-- Here you have to set the time interval -->
  </HEAD>
  <BODY BGCOLOR="black" TEXT="black" LINK="blue" VLINK="blue" ALINK="red">
  <CENTER>
  <P><IMG SRC="./webgrab.jpg" ALT="-bttvgrab WebCam Picture-">
  <P>
  
  </BODY></HTML>
  


This script shows the file /usr/local/httpd/htdocs/webcam/webgrab.jpg after every refresh.
Of course you have to call bttvgrab with a slight different option. In particular it is important that the refresh rate for the web page and bttvgrab are identical. Otherwise the server will be loaded too much.

  >> bttvgrab -f webcam:/usr/local/httpd/htdocs/webcam/webgrab.jpg -s 1 -w 640 -o jpg -Q
  
At http://localhost/webcam/webcam.html you can see this page.

As you can see this way of refreshing the picture is not very pleasant. For every refresh the picture has to be rebuild and you get flicker that gets on your nerves.
Much better would it be if the page remains and just the picture is refreshed regularly.

Server-Push

Also this option is supported by modern browsers and is called server push.
You have to invest more work for it but it is not too much.

The HTML page that is needed should look like:

server-push.html
  <pre>
  <HTML><HEAD><TITLE>bttvgrab WebCam</TITLE></HEAD>
  
  </HEAD>
  <BODY BGCOLOR="black" TEXT="white" LINK="blue" VLINK="blue" ALINK="red">
  <CENTER>
  Text
  <hr>
  <P><IMG SRC="./cgi-bin/nph-webgrab.cgi/0.gif" ALT="-bttvgrab WebCam Picture-">
  <P>
  
  </BODY></HTML>
  </pre>
  


and has one single duty: show a picture. The image tag <IMG SRC="..."> does not point to a picture file but a CGI script which is responsible for streaming the picture.
The script should look like the following example and should be placed in the cgi-bin directory inside of the webcam directory.

cgi-bin/nph-webgrab.cgi
   #!/usr/bin/perl
   
   print "HTTP/1.0 200 Document follows\n";
   print "Content-type: multipart/x-mixed-replace;boundary=goober\n\n";
   
   while(true){
      print "\n--goober\n";
      print "Content-type: image/gif\n\n";
  
      open(IN, "../webgrab.jpg");
      
      while (read(IN, $buffer, 4096)){
          print $buffer;
      }    
      close("../webgrab.jpg");
      #sleep(1);
   }
  
   print "\n--goober--\n";
          
   exit 0;
  


Important is that the name of the script start with the letters nph. This signals to the browser that it must not use the cache.
If the browser cached the data flow it might occur that short interrupts in the presentation of the picture can be seen and many pictures in a shorter time are showed at once.
The switched off cache of the browser forces us to handle other parts of the HTTP protocol which we usually do not need explicitly.
That's why we need

    print "HTTP/1.0 200 Document follows\n";
    print "Content-type: multipart/x-mixed-replace;boundary=goober\n\n";
  

The line

   #sleep(1);
  
sets the time interval in seconds after which the picture is refreshed. In the current setting this line is switched off and it allows a submission of pictures as fast as the network can.
But this is only recommended in your local network. If you wanted to place the picture into the Internet you should activate the line and set a number similar to the one you use for bttvgrab.

Next you have to tell the Apache webserver that there an CGI script in webcam/cgi-bin exists.
You have to add the following entry in the file /etc/httpd/srm.conf:

File /etc/httpd/srm.conf
   # ScriptAlias: This controls which directories contain server scripts. 
   # Format: ScriptAlias fakename realname 
   
   ScriptAlias /cgi-bin/ "/usr/local/httpd/cgi-bin/" 
   
   #The next line has to be added!
   ScriptAlias /webcam/cgi-bin/ "/usr/local/httpd/htdocs/webcam/cgi-bin/"
  


Then you restart the web server (with SuSE)

  >> /sbin/init.d/apache restart 
  
Now everything should be set and you can try out the web site with the server push technology.
Therefore you start bttvgrab (to demonstrate the abilities the sleep-time is switched off)
  >> bttvgrab -f webcam:/usr/local/httpd/htdocs/webcam/webgrab.jpg -w 200 -o jpg -Q
  
and then you got to http://localhost/webcam/server-push.html.

It surprised me how smooth the video runs inside the browser. On the other hand you have a very high CPU load.
To support your own webcam you should set a sleeping-time in bttvgrab and nph-webgrab.cgi that makes sense.

Outlook

How you use this technology as a webcam?
Very simple. You connect your camcorder to the TV card or to a video recorder and this one to the TV card.

This webcam replacement would only make sense if you already owned a video camera and a VCR. If not it would be too expensive to buy these as a webcam replacement.
But it is an interesting and cheap way if you do not invest any new money.

Another problem is that you do not have any program to set the tuner on the TV card and there is no way to change the channel.
A set of the channel could work with set-tv from the package XawTV by Gerd Knorr.
A little effort in programming should make it possible that you change the channels via your web site.

For the experienced user we suggest to start the webcam automatically after the login to your provider via an entry in the file /etc/ppp/ip-up.
If you owned a permanent connection to the Internet it would make sense if you automatically start the webcam after rebooting. Therefore you could create a init script.

Another way to place your camera picture on a web site could be a Java applet. Using this option it is possible to suppress the continuous loading of the picture but show it after its full load. If you were interested in such a solution you should look into the Java documentation: keyword: ImageObserver.


Links:
http://moes.pmnet.uni-oldenburg.de/bttvgrab/ Homepage of the bttvgrab program



Talkback Area




Enter Own Comment