rllib  1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
rlwebcam.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlwebcam.cpp - description
3  -------------------
4  begin : Mo Aug 24 2009
5  copyright : (C) 2009 by R. Lehrig
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include "rlwebcam.h"
17 
19 {
20  debug = 0;
21  sock = NULL;
22 }
23 
25 {
26  if(sock != NULL) delete sock;
27 }
28 
29 int rlWebcam::setUrl(const char *urlstr)
30 {
31  if(urlstr == NULL) return -1;
32  url.setText(urlstr);
33  if(sock != NULL) delete sock;
34  sock = NULL;
35  if(getHost() == NULL || getPort() < 0) return -1;
36  sock = new rlSocket(getHost(),getPort(),1);
37  return 0;
38 }
39 
41 {
42  if(sock == NULL) return -1;
43  if(sock->isConnected()) sock->disconnect();
44  return 0;
45 }
46 
47 const char * rlWebcam::getSnapshot(int timeout)
48 {
49  if(sock == NULL)
50  {
51  printf("rlWebcam::ERROR sock==NULL\n");
52  return NULL;
53  }
54  if(sock->isConnected()) sock->disconnect();
55  const char *cptr = getFrame(timeout);
56  sock->disconnect();
57  return cptr;
58 }
59 
60 const char * rlWebcam::getFrame(int timeout)
61 {
62  unsigned char c1,c2;
63 
64  if(sock == NULL)
65  {
66  printf("rlWebcam::ERROR sock==NULL\n");
67  return NULL;
68  }
69  if(sock->isConnected() == 0)
70  {
71  sock->connect();
72  sock->printf("GET /%s%c%c%c%c",getPath(),0x0d,0x0a,0x0d,0x0a);
73  }
74  if(sock->isConnected() == 0)
75  {
76  printf("rlWebcam::ERROR sock->isConnected() == 0\n");
77  return NULL;
78  }
79 
80  // search for startOfImage
81  while(1)
82  {
83  if(sock->read(&c1,1,timeout) < 1) return NULL;
84  if(debug) printf("%02x ", c1);
85  if(c1 == 0x0ff)
86  {
87  if(sock->read(&c2,1,timeout) < 1) return NULL;
88  if(debug) printf("%02x ", c2);
89  if(c2 == 0x0d8)
90  {
91  if(debug) printf("\nrlWebcam::Found startOfImage\n");
92  break;
93  }
94  }
95  }
96 
97  // open output file
98  if(filename.text() == NULL)
99  {
100  printf("rlWebcam::ERROR you forgot to set filename\n");
101  return NULL;
102  }
103  FILE *fout = fopen(filename.text(),"w");
104  if(fout == NULL)
105  {
106  printf("rlWebcam::ERROR could not write file %s\n", filename.text());
107  return NULL;
108  }
109  fputc(c1, fout);
110  fputc(c2, fout);
111 
112  // read until endOfImage
113  while(1)
114  {
115  if(sock->read(&c1,1,timeout) < 1) return NULL;
116  fputc(c1, fout);
117  if(debug) printf("%02x ", c1);
118  if(c1 == 0x0ff)
119  {
120  if(sock->read(&c2,1,timeout) < 1) return NULL;
121  fputc(c2, fout);
122  if(debug) printf("%02x ", c2);
123  if(c2 == 0x0d9)
124  {
125  if(debug) printf("\nrlWebcam::Found endOfImage\n");
126  break;
127  }
128  }
129  }
130 
131  // close output file
132  fclose(fout);
133  return filename.text();
134 }
135 
136 const char * rlWebcam::getUrl()
137 {
138  return url.text();
139 }
140 
141 const char * rlWebcam::getHost()
142 {
143  if(url.startsWith("http://") == 0)
144  {
145  printf("rlWebcam::wrong url syntax in %s\n", url.text());
146  printf("url syntax: http://host:port/path_to_webcam_cgi_script\n");
147  return NULL;
148  }
149  char *cptr = url.text();
150  cptr += 7;
151  temp1.setText(cptr);
152  cptr = temp1.strchr('/');
153  if(cptr != NULL) *cptr = '\0';
154  cptr = temp1.strchr(':');
155  if(cptr != NULL) *cptr = '\0';
156  if(debug) printf("rlWebcam:host=%s\n", temp1.text());
157  return temp1.text();
158 }
159 
161 {
162  int port = 80;
163  if(url.startsWith("http://") == 0)
164  {
165  printf("rlWebcam::wrong url syntax in %s\n", url.text());
166  printf("url syntax: http://host:port/path_to_webcam_cgi_script\n");
167  return -1;
168  }
169  char *cptr = url.text();
170  cptr += 7;
171  temp2.setText(cptr);
172  cptr = temp2.strchr('/');
173  if(cptr != NULL) *cptr = '\0';
174  cptr = temp2.strchr(':');
175  if(cptr != NULL) sscanf(cptr,":%d", &port);
176  if(debug) printf("rlWebcam:port=%d\n", port);
177  return port;
178 }
179 
180 const char * rlWebcam::getPath()
181 {
182  if(url.startsWith("http://") == 0)
183  {
184  printf("rlWebcam::wrong url syntax in %s\n", url.text());
185  printf("url syntax: http://host:port/path_to_webcam_cgi_script\n");
186  return NULL;
187  }
188  char *cptr = url.text();
189  cptr += 7;
190  temp3.setText(cptr);
191  cptr = temp3.strchr('/');
192  if(cptr == NULL) return "";
193  cptr++;
194  if(debug) printf("rlWebcam:path=%s\n", cptr);
195  return cptr;
196 }
197