rllib  1
rlwebcam.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                           rlwebcam.cpp  -  description
00003                              -------------------
00004     begin                : Mo Aug 24 2009
00005     copyright            : (C) 2009 by Rainer Lehrig
00006     email                : lehrig@t-online.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This library is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as        *
00013  *   published by the Free Software Foundation                             *
00014  *                                                                         *
00015  ***************************************************************************/
00016 #include "rlwebcam.h"
00017 
00018 rlWebcam::rlWebcam()
00019 {
00020   debug = 0;
00021   sock = NULL;
00022 }
00023 
00024 rlWebcam::~rlWebcam()
00025 {
00026   if(sock != NULL) delete sock;
00027 }
00028 
00029 int rlWebcam::setUrl(const char *urlstr)
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 }
00039 
00040 int rlWebcam::disconnect()
00041 {
00042   if(sock == NULL) return -1;
00043   if(sock->isConnected()) sock->disconnect();
00044   return 0;
00045 }
00046 
00047 const char * rlWebcam::getSnapshot(int timeout)
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 }
00059 
00060 const char * rlWebcam::getFrame(int timeout)
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 }
00135 
00136 const char * rlWebcam::getUrl()
00137 {
00138   return url.text();
00139 }
00140 
00141 const char * rlWebcam::getHost()
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 }
00159 
00160 int rlWebcam::getPort()
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 }
00179 
00180 const char * rlWebcam::getPath()
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 }
00197