|
rllib
1
|
#include <rlwebcam.h>

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 | |
| rlSocket * | sock |
| rlString | url |
| rlString | temp1 |
| rlString | temp2 |
| rlString | temp3 |
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.
| rlWebcam::rlWebcam | ( | ) |
Definition at line 18 of file rlwebcam.cpp.
| rlWebcam::~rlWebcam | ( | ) | [virtual] |
Definition at line 24 of file rlwebcam.cpp.
| int rlWebcam::disconnect | ( | ) |
Definition at line 40 of file rlwebcam.cpp.
{
if(sock == NULL) return -1;
if(sock->isConnected()) sock->disconnect();
return 0;
}
| const char * rlWebcam::getFrame | ( | int | timeout = 3000 | ) |
Definition at line 60 of file rlwebcam.cpp.
{
unsigned char c1,c2;
if(sock == NULL)
{
printf("rlWebcam::ERROR sock==NULL\n");
return NULL;
}
if(sock->isConnected() == 0)
{
sock->connect();
sock->printf("GET /%s%c%c%c%c",getPath(),0x0d,0x0a,0x0d,0x0a);
}
if(sock->isConnected() == 0)
{
printf("rlWebcam::ERROR sock->isConnected() == 0\n");
return NULL;
}
// search for startOfImage
while(1)
{
if(sock->read(&c1,1,timeout) < 1) return NULL;
if(debug) printf("%02x ", c1);
if(c1 == 0x0ff)
{
if(sock->read(&c2,1,timeout) < 1) return NULL;
if(debug) printf("%02x ", c2);
if(c2 == 0x0d8)
{
if(debug) printf("\nrlWebcam::Found startOfImage\n");
break;
}
}
}
// open output file
if(filename.text() == NULL)
{
printf("rlWebcam::ERROR you forgot to set filename\n");
return NULL;
}
FILE *fout = fopen(filename.text(),"w");
if(fout == NULL)
{
printf("rlWebcam::ERROR could not write file %s\n", filename.text());
return NULL;
}
fputc(c1, fout);
fputc(c2, fout);
// read until endOfImage
while(1)
{
if(sock->read(&c1,1,timeout) < 1) return NULL;
fputc(c1, fout);
if(debug) printf("%02x ", c1);
if(c1 == 0x0ff)
{
if(sock->read(&c2,1,timeout) < 1) return NULL;
fputc(c2, fout);
if(debug) printf("%02x ", c2);
if(c2 == 0x0d9)
{
if(debug) printf("\nrlWebcam::Found endOfImage\n");
break;
}
}
}
// close output file
fclose(fout);
return filename.text();
}
| const char * rlWebcam::getHost | ( | ) |
Definition at line 141 of file rlwebcam.cpp.
{
if(url.startsWith("http://") == 0)
{
printf("rlWebcam::wrong url syntax in %s\n", url.text());
printf("url syntax: http://host:port/path_to_webcam_cgi_script\n");
return NULL;
}
char *cptr = url.text();
cptr += 7;
temp1.setText(cptr);
cptr = temp1.strchr('/');
if(cptr != NULL) *cptr = '\0';
cptr = temp1.strchr(':');
if(cptr != NULL) *cptr = '\0';
if(debug) printf("rlWebcam:host=%s\n", temp1.text());
return temp1.text();
}
| const char * rlWebcam::getPath | ( | ) |
Definition at line 180 of file rlwebcam.cpp.
{
if(url.startsWith("http://") == 0)
{
printf("rlWebcam::wrong url syntax in %s\n", url.text());
printf("url syntax: http://host:port/path_to_webcam_cgi_script\n");
return NULL;
}
char *cptr = url.text();
cptr += 7;
temp3.setText(cptr);
cptr = temp3.strchr('/');
if(cptr == NULL) return "";
cptr++;
if(debug) printf("rlWebcam:path=%s\n", cptr);
return cptr;
}
| int rlWebcam::getPort | ( | ) |
Definition at line 160 of file rlwebcam.cpp.
{
int port = 80;
if(url.startsWith("http://") == 0)
{
printf("rlWebcam::wrong url syntax in %s\n", url.text());
printf("url syntax: http://host:port/path_to_webcam_cgi_script\n");
return -1;
}
char *cptr = url.text();
cptr += 7;
temp2.setText(cptr);
cptr = temp2.strchr('/');
if(cptr != NULL) *cptr = '\0';
cptr = temp2.strchr(':');
if(cptr != NULL) sscanf(cptr,":%d", &port);
if(debug) printf("rlWebcam:port=%d\n", port);
return port;
}
| const char * rlWebcam::getSnapshot | ( | int | timeout = 3000 | ) |
Definition at line 47 of file rlwebcam.cpp.
{
if(sock == NULL)
{
printf("rlWebcam::ERROR sock==NULL\n");
return NULL;
}
if(sock->isConnected()) sock->disconnect();
const char *cptr = getFrame(timeout);
sock->disconnect();
return cptr;
}
| const char * rlWebcam::getUrl | ( | ) |
Definition at line 136 of file rlwebcam.cpp.
| int rlWebcam::setUrl | ( | const char * | url | ) |
| int rlWebcam::debug |
Definition at line 90 of file rlwebcam.h.
Definition at line 91 of file rlwebcam.h.
rlSocket* rlWebcam::sock [private] |
Definition at line 94 of file rlwebcam.h.
rlString rlWebcam::temp1 [private] |
Definition at line 95 of file rlwebcam.h.
rlString rlWebcam::temp2 [private] |
Definition at line 95 of file rlwebcam.h.
rlString rlWebcam::temp3 [private] |
Definition at line 95 of file rlwebcam.h.
rlString rlWebcam::url [private] |
Definition at line 95 of file rlwebcam.h.
1.7.5.1