rllib  1
Public Member Functions | Public Attributes | Private Attributes | List of all members
rlWebcam Class Reference

#include <rlwebcam.h>

Collaboration diagram for rlWebcam:
Collaboration graph
[legend]

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, int requestOnly=0)
 
int getFrameBuffer (unsigned char *buffer, int maxbuffer, int timeout=3000)
 
const char * getUrl ()
 
const char * getHost ()
 
int getPort ()
 
const char * getPath ()
 

Public Attributes

int debug
 
rlString filename
 
rlSocketsock
 

Private Attributes

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::rlWebcam ( )

Definition at line 18 of file rlwebcam.cpp.

19 {
20  debug = 0;
21  sock = NULL;
22 }
int debug
Definition: rlwebcam.h:91
rlSocket * sock
Definition: rlwebcam.h:93

◆ ~rlWebcam()

rlWebcam::~rlWebcam ( )
virtual

Definition at line 24 of file rlwebcam.cpp.

25 {
26  if(sock != NULL) delete sock;
27 }
rlSocket * sock
Definition: rlwebcam.h:93

Member Function Documentation

◆ disconnect()

int rlWebcam::disconnect ( )

Definition at line 40 of file rlwebcam.cpp.

41 {
42  if(sock == NULL) return -1;
43  if(sock->isConnected()) sock->disconnect();
44  return 0;
45 }
int disconnect()
Definition: rlsocket.cpp:545
int isConnected()
Definition: rlsocket.cpp:559
rlSocket * sock
Definition: rlwebcam.h:93

◆ getFrame()

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

Definition at line 60 of file rlwebcam.cpp.

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 }
int readStr(char *buf, int len, int timeout=0)
Definition: rlsocket.cpp:224
char * text()
Definition: rlstring.cpp:126
const char * getPath()
Definition: rlwebcam.cpp:301
int read(void *buf, int len, int timeout=0)
Definition: rlsocket.cpp:191
int debug
Definition: rlwebcam.h:91
rlString filename
Definition: rlwebcam.h:92
int connect()
Definition: rlsocket.cpp:321
int isConnected()
Definition: rlsocket.cpp:559
rlSocket * sock
Definition: rlwebcam.h:93
int printf(const char *format,...)
Definition: rlsocket.cpp:586

◆ getFrameBuffer()

int rlWebcam::getFrameBuffer ( unsigned char *  buffer,
int  maxbuffer,
int  timeout = 3000 
)

Definition at line 166 of file rlwebcam.cpp.

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 }
int readStr(char *buf, int len, int timeout=0)
Definition: rlsocket.cpp:224
const char * getPath()
Definition: rlwebcam.cpp:301
int read(void *buf, int len, int timeout=0)
Definition: rlsocket.cpp:191
int debug
Definition: rlwebcam.h:91
int connect()
Definition: rlsocket.cpp:321
int isConnected()
Definition: rlsocket.cpp:559
rlSocket * sock
Definition: rlwebcam.h:93
int printf(const char *format,...)
Definition: rlsocket.cpp:586

◆ getHost()

const char * rlWebcam::getHost ( )

Definition at line 262 of file rlwebcam.cpp.

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 }
int startsWith(const char *startstr)
Definition: rlstring.cpp:198
char * text()
Definition: rlstring.cpp:126
int debug
Definition: rlwebcam.h:91
int setText(const char *text)
Definition: rlstring.cpp:136
char * strchr(int c)
Definition: rlstring.cpp:223
rlString url
Definition: rlwebcam.h:96
rlString temp1
Definition: rlwebcam.h:96

◆ getPath()

const char * rlWebcam::getPath ( )

Definition at line 301 of file rlwebcam.cpp.

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 }
int startsWith(const char *startstr)
Definition: rlstring.cpp:198
rlString temp3
Definition: rlwebcam.h:96
char * text()
Definition: rlstring.cpp:126
int debug
Definition: rlwebcam.h:91
int setText(const char *text)
Definition: rlstring.cpp:136
char * strchr(int c)
Definition: rlstring.cpp:223
rlString url
Definition: rlwebcam.h:96

◆ getPort()

int rlWebcam::getPort ( )

Definition at line 281 of file rlwebcam.cpp.

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 }
int startsWith(const char *startstr)
Definition: rlstring.cpp:198
rlString temp2
Definition: rlwebcam.h:96
char * text()
Definition: rlstring.cpp:126
int debug
Definition: rlwebcam.h:91
int setText(const char *text)
Definition: rlstring.cpp:136
char * strchr(int c)
Definition: rlstring.cpp:223
rlString url
Definition: rlwebcam.h:96

◆ getSnapshot()

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

Definition at line 47 of file rlwebcam.cpp.

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 }
const char * getFrame(int timeout=3000, int requestOnly=0)
Definition: rlwebcam.cpp:60
int disconnect()
Definition: rlsocket.cpp:545
int isConnected()
Definition: rlsocket.cpp:559
rlSocket * sock
Definition: rlwebcam.h:93

◆ getUrl()

const char * rlWebcam::getUrl ( )

Definition at line 257 of file rlwebcam.cpp.

258 {
259  return url.text();
260 }
char * text()
Definition: rlstring.cpp:126
rlString url
Definition: rlwebcam.h:96

◆ setUrl()

int rlWebcam::setUrl ( const char *  url)

Definition at line 29 of file rlwebcam.cpp.

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 }
int getPort()
Definition: rlwebcam.cpp:281
int setText(const char *text)
Definition: rlstring.cpp:136
const char * getHost()
Definition: rlwebcam.cpp:262
rlString url
Definition: rlwebcam.h:96
rlSocket * sock
Definition: rlwebcam.h:93

Member Data Documentation

◆ debug

int rlWebcam::debug

Definition at line 91 of file rlwebcam.h.

◆ filename

rlString rlWebcam::filename

Definition at line 92 of file rlwebcam.h.

◆ sock

rlSocket* rlWebcam::sock

Definition at line 93 of file rlwebcam.h.

◆ temp1

rlString rlWebcam::temp1
private

Definition at line 96 of file rlwebcam.h.

◆ temp2

rlString rlWebcam::temp2
private

Definition at line 96 of file rlwebcam.h.

◆ temp3

rlString rlWebcam::temp3
private

Definition at line 96 of file rlwebcam.h.

◆ url

rlString rlWebcam::url
private

Definition at line 96 of file rlwebcam.h.


The documentation for this class was generated from the following files: