rlWebcam Class Reference

#include <rlwebcam.h>

Collaboration diagram for rlWebcam:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 rlWebcam ()
virtual ~rlWebcam ()
int setUrl (const char *url)
int disconnect ()
const char * getSnapshot (int timeout=3000)
const char * getFrame (int timeout=3000)
const char * getUrl ()
const char * getHost ()
int getPort ()
const char * getPath ()

Public Attributes

int debug
rlString filename

Private Attributes

rlSocketsock
rlString url
rlString temp1
rlString temp2
rlString temp3

Detailed Description

class for handling networked webcams over http:// that use motion jpeg
If you do not know under which URL your webcam provides the M-JPEG video stream
you may use tcpdump to figure it out.
Example:
tcpdump -X -i eth0 -t -q -s 0 "host 192.168.1.200 && port 80" | grep -A 10 GET
IP myhost.46727 > 192.168.1.200.http: tcp 97
 0x0000:  4500 0089 edb6 4000 4006 c891 c0a8 010e  E.....@.@.......
 0x0010:  c0a8 01c8 b687 0050 d99f 8b7d 0003 d5b2  .......P...}....
 0x0020:  5018 16d0 2460 0000 4745 5420 2f63 6769  P...$`..GET./cgi
 0x0030:  2d62 696e 2f53 7472 6561 6d3f 5669 6465  -bin/Stream?Vide
 0x0040:  6f20 4175 7468 6f72 697a 6174 696f 6e3a  o.Authorization:
 0x0050:  2042 6173 6963 2059 5752 7461 5734 3663  .Basic.YWRtaW46c
 0x0060:  4746 7a63 3364 7663 6d51 3d3f 7765 6263  GFzc3dvcmQ=?webc
 0x0070:  616d 5057 443d 526f 6f74 436f 6f6b 6965  amPWD=RootCookie
 0x0080:  3030 3030 300d 0a0d 0a                   00000....
Usage example for pvbrowser slot functions:
include "rlwebcam.h"
typedef struct // (todo: define your data structure here)
{
  rlWebcam webcamBig;
}
DATA;
static int slotInit(PARAM *p, DATA *d)
{
  if(p == NULL || d == NULL) return -1;
  p->sleep = 20;
  p->force_null_event = 0;
  d->webcamBig.debug = 0;
  d->webcamBig.filename.printf("%swebcam.jpg", p->file_prefix);
  d->webcamBig.setUrl("http://192.168.1.200/cgi-bin/Stream?Video Authorization: Basic YWRtaW46cGFzc3dvcmQ=?webcamPWD=RootCookie00000");
  return 0;
}
static int slotNullEvent(PARAM *p, DATA *d)
{
  if(p == NULL || d == NULL) return -1;
  if(const char *fname = d->webcamBig.getFrame()) // OR if(const char *fname = d->webcamBig.getSnapshot()) 
  {
    pvDownloadFileAs(p,fname,"webcam.jpg");
    pvSetImage(p,WebcamBig,"webcam.jpg"); // WebcamBig is a pvQImage object that accepts jpeg images
  }
  return 0;
}
 

Definition at line 77 of file rlwebcam.h.


Constructor & Destructor Documentation

rlWebcam::rlWebcam (  ) 

Definition at line 18 of file rlwebcam.cpp.

00019 {
00020   debug = 0;
00021   sock = NULL;
00022 }

rlWebcam::~rlWebcam (  )  [virtual]

Definition at line 24 of file rlwebcam.cpp.

00025 {
00026   if(sock != NULL) delete sock;
00027 }


Member Function Documentation

int rlWebcam::disconnect (  ) 

Definition at line 40 of file rlwebcam.cpp.

00041 {
00042   if(sock == NULL) return -1;
00043   if(sock->isConnected()) sock->disconnect();
00044   return 0;
00045 }

const char * rlWebcam::getFrame ( int  timeout = 3000  ) 

Definition at line 60 of file rlwebcam.cpp.

00061 {
00062   unsigned char c1,c2;
00063 
00064   if(sock == NULL)
00065   {
00066     printf("rlWebcam::ERROR sock==NULL\n");
00067     return NULL;
00068   }
00069   if(sock->isConnected() == 0) 
00070   {
00071     sock->connect();
00072     sock->printf("GET /%s%c%c%c%c",getPath(),0x0d,0x0a,0x0d,0x0a);
00073   }  
00074   if(sock->isConnected() == 0)
00075   {
00076     printf("rlWebcam::ERROR sock->isConnected() == 0\n");
00077     return NULL;
00078   }
00079 
00080   // search for startOfImage
00081   while(1)
00082   {
00083     if(sock->read(&c1,1,timeout) < 1) return NULL;
00084     if(debug) printf("%02x ", c1);
00085     if(c1 == 0x0ff)
00086     {
00087       if(sock->read(&c2,1,timeout) < 1) return NULL;
00088       if(debug) printf("%02x ", c2);
00089       if(c2 == 0x0d8)
00090       {
00091         if(debug) printf("\nrlWebcam::Found startOfImage\n");
00092         break;
00093       }
00094     }
00095   }
00096 
00097   // open output file
00098   if(filename.text() == NULL)
00099   {
00100     printf("rlWebcam::ERROR you forgot to set filename\n");
00101     return NULL;
00102   }
00103   FILE *fout = fopen(filename.text(),"w");
00104   if(fout == NULL)
00105   {
00106     printf("rlWebcam::ERROR could not write file %s\n", filename.text());
00107     return NULL;
00108   }
00109   fputc(c1, fout);
00110   fputc(c2, fout);
00111 
00112   // read until endOfImage
00113   while(1)
00114   {
00115     if(sock->read(&c1,1,timeout) < 1) return NULL;
00116     fputc(c1, fout);
00117     if(debug) printf("%02x ", c1);
00118     if(c1 == 0x0ff)
00119     {
00120       if(sock->read(&c2,1,timeout) < 1) return NULL;
00121       fputc(c2, fout);
00122       if(debug) printf("%02x ", c2);
00123       if(c2 == 0x0d9)
00124       {
00125         if(debug) printf("\nrlWebcam::Found endOfImage\n");
00126         break;
00127       }
00128     }
00129   }
00130 
00131   // close output file
00132   fclose(fout);
00133   return filename.text();
00134 }

const char * rlWebcam::getHost (  ) 

Definition at line 141 of file rlwebcam.cpp.

00142 {
00143   if(url.startsWith("http://") == 0)
00144   {
00145     printf("rlWebcam::wrong url syntax in %s\n", url.text());
00146     printf("url syntax: http://host:port/path_to_webcam_cgi_script\n");
00147     return NULL;
00148   }
00149   char *cptr = url.text();
00150   cptr += 7;
00151   temp1.setText(cptr);
00152   cptr = temp1.strchr('/');
00153   if(cptr != NULL) *cptr = '\0';
00154   cptr = temp1.strchr(':');
00155   if(cptr != NULL) *cptr = '\0';
00156   if(debug) printf("rlWebcam:host=%s\n", temp1.text());
00157   return temp1.text();
00158 }

const char * rlWebcam::getPath (  ) 

Definition at line 180 of file rlwebcam.cpp.

00181 {
00182   if(url.startsWith("http://") == 0)
00183   {
00184     printf("rlWebcam::wrong url syntax in %s\n", url.text());
00185     printf("url syntax: http://host:port/path_to_webcam_cgi_script\n");
00186     return NULL;
00187   }
00188   char *cptr = url.text();
00189   cptr += 7;
00190   temp3.setText(cptr);
00191   cptr = temp3.strchr('/');
00192   if(cptr == NULL) return "";
00193   cptr++;
00194   if(debug) printf("rlWebcam:path=%s\n", cptr);
00195   return cptr;
00196 }

int rlWebcam::getPort (  ) 

Definition at line 160 of file rlwebcam.cpp.

00161 {
00162   int port = 80;
00163   if(url.startsWith("http://") == 0)
00164   {
00165     printf("rlWebcam::wrong url syntax in %s\n", url.text());
00166     printf("url syntax: http://host:port/path_to_webcam_cgi_script\n");
00167     return -1;
00168   }
00169   char *cptr = url.text();
00170   cptr += 7;
00171   temp2.setText(cptr);
00172   cptr = temp2.strchr('/');
00173   if(cptr != NULL) *cptr = '\0';
00174   cptr = temp2.strchr(':');
00175   if(cptr != NULL) sscanf(cptr,":%d", &port);
00176   if(debug) printf("rlWebcam:port=%d\n", port);
00177   return port;
00178 }

const char * rlWebcam::getSnapshot ( int  timeout = 3000  ) 

Definition at line 47 of file rlwebcam.cpp.

00048 {
00049   if(sock == NULL)
00050   {
00051     printf("rlWebcam::ERROR sock==NULL\n");
00052     return NULL;
00053   }
00054   if(sock->isConnected()) sock->disconnect();
00055   const char *cptr = getFrame(timeout);
00056   sock->disconnect();
00057   return cptr;
00058 }

const char * rlWebcam::getUrl (  ) 

Definition at line 136 of file rlwebcam.cpp.

00137 {
00138   return url.text();
00139 }

int rlWebcam::setUrl ( const char *  url  ) 

Definition at line 29 of file rlwebcam.cpp.

00030 {
00031   if(urlstr == NULL) return -1;
00032   url.setText(urlstr);
00033   if(sock != NULL) delete sock;
00034   sock = NULL;
00035   if(getHost() == NULL || getPort() < 0) return -1;
00036   sock = new rlSocket(getHost(),getPort(),1);
00037   return 0;
00038 }


Member Data Documentation

Definition at line 90 of file rlwebcam.h.

Definition at line 91 of file rlwebcam.h.

Definition at line 94 of file rlwebcam.h.

Definition at line 95 of file rlwebcam.h.

Definition at line 95 of file rlwebcam.h.

Definition at line 95 of file rlwebcam.h.

Definition at line 95 of file rlwebcam.h.


The documentation for this class was generated from the following files:
Generated on Mon Aug 30 20:16:17 2010 for rllib by  doxygen 1.6.3