rllib  1
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, int requestOnly)
61 {
62  unsigned char c1,c2;
63  unsigned char buffer[1024*1024];
64 
65  if(sock == NULL)
66  {
67  printf("rlWebcam::ERROR sock==NULL\n");
68  return NULL;
69  }
70  if(sock->isConnected() == 0)
71  {
72  sock->connect();
73  sock->printf("GET /%s%c%c%c%c",getPath(),0x0d,0x0a,0x0d,0x0a);
74  }
75  if(sock->isConnected() == 0)
76  {
77  printf("rlWebcam::ERROR sock->isConnected() == 0\n");
78  return NULL;
79  }
80 
81  // read http header
82  char line[256];
83  while(1)
84  {
85  if(sock->readStr(line,(int) sizeof(line)-1,timeout) < 1) return NULL;
86  if(debug) printf("getFrame:: Header:%s",line);
87  if(strncmp(line,"Content-Length:",15) == 0) break;
88  }
89 
90  int len = 0;
91  sscanf(line,"Content-Length: %d", &len);
92  if(debug) printf("getFrame:: len=%d\n",len);
93  if(len > (int) (sizeof(buffer) - 1)) return NULL;
94 
95  if(requestOnly) return NULL;
96  // search for startOfImage
97  while(1)
98  {
99  if(sock->read(&c1,1,timeout) < 1) return NULL;
100  if(debug) printf("%02x ", c1);
101  if(c1 == 0x0ff)
102  {
103  if(sock->read(&c2,1,timeout) < 1) return NULL;
104  if(debug) printf("%02x ", c2);
105  if(c2 == 0x0d8)
106  {
107  if(debug) printf("\nrlWebcam::Found startOfImage\n");
108  break;
109  }
110  }
111  }
112 
113  // open output file
114  if(filename.text() == NULL)
115  {
116  printf("rlWebcam::ERROR you forgot to set filename\n");
117  return NULL;
118  }
119  FILE *fout = fopen(filename.text(),"w");
120  if(fout == NULL)
121  {
122  printf("rlWebcam::ERROR could not write file %s\n", filename.text());
123  return NULL;
124  }
125  //fputc(c1, fout);
126  //fputc(c2, fout);
127 
128  int ind = 0;
129  buffer[ind++] = c1;
130  buffer[ind++] = c2;
131  len = sock->read(&buffer[2],len-2,timeout);
132  //len = recv(sock->s,&buffer[2],len-2,MSG_WAITALL);
133  if(len < 1) return NULL;
134  ind = len + 2;
135 
136  fwrite(buffer, ind, 1, fout);
137 
138  /*
139  // read until endOfImage
140  while(1)
141  {
142  //if(sock->read(&c1,1,timeout) < 1) return NULL;
143  if(recv(sock->s,&c1,1,0) < 1) return NULL;
144  fputc(c1, fout);
145  if(debug) printf("%02x ", c1);
146  if(c1 == 0x0ff)
147  {
148  //if(sock->read(&c2,1,timeout) < 1) return NULL;
149  if(recv(sock->s,&c2,1,0) < 1) return NULL;
150  fputc(c2, fout);
151  if(debug) printf("%02x ", c2);
152  if(c2 == 0x0d9)
153  {
154  if(debug) printf("\nrlWebcam::Found endOfImage\n");
155  break;
156  }
157  }
158  }
159  */
160 
161  // close output file
162  fclose(fout);
163  return filename.text();
164 }
165 
166 int rlWebcam::getFrameBuffer(unsigned char *buffer, int maxbuffer, int timeout)
167 {
168  unsigned char c1,c2;
169 
170  if(sock == NULL)
171  {
172  printf("rlWebcam::ERROR sock==NULL\n");
173  return -1;
174  }
175  if(sock->isConnected() == 0)
176  {
177  sock->connect();
178  sock->printf("GET /%s%c%c%c%c",getPath(),0x0d,0x0a,0x0d,0x0a);
179  }
180  if(sock->isConnected() == 0)
181  {
182  printf("rlWebcam::ERROR sock->isConnected() == 0\n");
183  return -2;
184  }
185 
186  // read http header
187  char line[256];
188  while(1)
189  {
190  if(sock->readStr(line,(int) sizeof(line)-1,timeout) < 1) return -1;
191  if(debug) printf("getFrameBuffer:: Header:%s",line);
192  if(strncmp(line,"Content-Length:",15) == 0) break;
193  }
194 
195  int len = 0;
196  sscanf(line,"Content-Length: %d", &len);
197  if(debug) printf("getFrameBuffer:: len=%d\n",len);
198 
199  // search for startOfImage
200  while(1)
201  {
202  if(sock->read(&c1,1,timeout) < 1) return -3;
203  if(debug) printf("%02x ", c1);
204  if(c1 == 0x0ff)
205  {
206  if(sock->read(&c2,1,timeout) < 1) return -4;
207  if(debug) printf("%02x ", c2);
208  if(c2 == 0x0d8)
209  {
210  if(debug) printf("\nrlWebcam::Found startOfImage\n");
211  break;
212  }
213  }
214  }
215 
216  int ind = 0;
217  buffer[ind++] = c1;
218  buffer[ind++] = c2;
219  len = sock->read(&buffer[2],len-2,timeout);
220  //len = recv(sock->s,&buffer[2],len-2,MSG_WAITALL);
221  if(len < 1) return -5;
222  ind = len + 2;
223 
224  /*
225  // read until endOfImage
226  while(1)
227  {
228  //if(sock->read(&c1,1,timeout) < 1) return -5;
229  if(recv(sock->s,&c1,1,0) < 1) return -5;
230  //fputc(c1, fout);
231  buffer[ind++] = c1;
232  if(ind >= maxbuffer) return -5;
233  if(debug) printf("%02x ", c1);
234  if(c1 == 0x0ff)
235  {
236  //if(sock->read(&c2,1,timeout) < 1) return -6;
237  if(recv(sock->s,&c2,1,0) < 1) return -6;
238  //fputc(c2, fout);
239  buffer[ind++] = c2;
240  if(ind >= maxbuffer) return -7;
241  if(debug) printf("%02x ", c2);
242  if(c2 == 0x0d9)
243  {
244  if(debug) printf("\nrlWebcam::Found endOfImage\n");
245  break;
246  }
247  }
248  }
249 
250  // close output file
251  // fclose(fout);
252  */
253 
254  return ind;
255 }
256 
257 const char * rlWebcam::getUrl()
258 {
259  return url.text();
260 }
261 
262 const char * rlWebcam::getHost()
263 {
264  if(url.startsWith("http://") == 0)
265  {
266  printf("rlWebcam::wrong url syntax in %s\n", url.text());
267  printf("url syntax: http://host:port/path_to_webcam_cgi_script\n");
268  return NULL;
269  }
270  char *cptr = url.text();
271  cptr += 7;
272  temp1.setText(cptr);
273  cptr = temp1.strchr('/');
274  if(cptr != NULL) *cptr = '\0';
275  cptr = temp1.strchr(':');
276  if(cptr != NULL) *cptr = '\0';
277  if(debug) printf("rlWebcam:host=%s\n", temp1.text());
278  return temp1.text();
279 }
280 
282 {
283  int port = 80;
284  if(url.startsWith("http://") == 0)
285  {
286  printf("rlWebcam::wrong url syntax in %s\n", url.text());
287  printf("url syntax: http://host:port/path_to_webcam_cgi_script\n");
288  return -1;
289  }
290  char *cptr = url.text();
291  cptr += 7;
292  temp2.setText(cptr);
293  cptr = temp2.strchr('/');
294  if(cptr != NULL) *cptr = '\0';
295  cptr = temp2.strchr(':');
296  if(cptr != NULL) sscanf(cptr,":%d", &port);
297  if(debug) printf("rlWebcam:port=%d\n", port);
298  return port;
299 }
300 
301 const char * rlWebcam::getPath()
302 {
303  if(url.startsWith("http://") == 0)
304  {
305  printf("rlWebcam::wrong url syntax in %s\n", url.text());
306  printf("url syntax: http://host:port/path_to_webcam_cgi_script\n");
307  return NULL;
308  }
309  char *cptr = url.text();
310  cptr += 7;
311  temp3.setText(cptr);
312  cptr = temp3.strchr('/');
313  if(cptr == NULL) return "";
314  cptr++;
315  if(debug) printf("rlWebcam:path=%s\n", cptr);
316  return cptr;
317 }
318 
int getPort()
Definition: rlwebcam.cpp:281
int setUrl(const char *url)
Definition: rlwebcam.cpp:29
int startsWith(const char *startstr)
Definition: rlstring.cpp:198
rlString temp2
Definition: rlwebcam.h:96
int getFrameBuffer(unsigned char *buffer, int maxbuffer, int timeout=3000)
Definition: rlwebcam.cpp:166
int readStr(char *buf, int len, int timeout=0)
Definition: rlsocket.cpp:224
rlString temp3
Definition: rlwebcam.h:96
char * text()
Definition: rlstring.cpp:126
rlWebcam()
Definition: rlwebcam.cpp:18
const char * getPath()
Definition: rlwebcam.cpp:301
int read(void *buf, int len, int timeout=0)
Definition: rlsocket.cpp:191
const char * getSnapshot(int timeout=3000)
Definition: rlwebcam.cpp:47
int debug
Definition: rlwebcam.h:91
int setText(const char *text)
Definition: rlstring.cpp:136
int disconnect()
Definition: rlwebcam.cpp:40
const char * getHost()
Definition: rlwebcam.cpp:262
const char * getFrame(int timeout=3000, int requestOnly=0)
Definition: rlwebcam.cpp:60
rlString filename
Definition: rlwebcam.h:92
const char * getUrl()
Definition: rlwebcam.cpp:257
int connect()
Definition: rlsocket.cpp:321
int disconnect()
Definition: rlsocket.cpp:545
int isConnected()
Definition: rlsocket.cpp:559
char * strchr(int c)
Definition: rlstring.cpp:223
rlString url
Definition: rlwebcam.h:96
rlSocket * sock
Definition: rlwebcam.h:93
rlString temp1
Definition: rlwebcam.h:96
virtual ~rlWebcam()
Definition: rlwebcam.cpp:24
int printf(const char *format,...)
Definition: rlsocket.cpp:586