-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.cpp
188 lines (154 loc) · 4.38 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include "DiscoveryRTSP_IP.h"
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <string.h>
#include "VideoPlayer.h"
#include "VideoWriterManager.h"
#define M_FOTO 1
#define M_VIDEO_IPCAM 2
#define M_VIDEO_WEBCAM 3
using namespace std;
/**
TODO's
Descobrir url automaticamente.
Testar de fora.
Testar se so salva movimento
Salvar videos diferentes a cada 2GB.
**/
std::vector<Camera *> readConfigurationFile(const char * file);
int main(int argc, char* argv[]) {
std::list<char *> urls;
std::vector<Camera *> cameras;
const char * url = NULL;
const char * dev = NULL;
char *title;
int modo=0;
// ######### ANALISA PARAMETROS ##########
if (argc == 1) {
printf("Usage:\n\t./VideoOnvif IP (will just search the onvifs IPs)\n\t./VideoOnvif RTSP_URL (udp)\n\t./VideoOnvif WEBCAM (or device)\n\t./VideoOnvif configFile.txt\n\n" );
return -1;
}
std::string argv1 = std::string(argv[1]);
int lpos = argv1.find_last_of('.');
std::string fim = argv1.substr(lpos+1);
// webcam
if (argv1.find("webcam")!=string::npos ) {
modo = M_VIDEO_WEBCAM;
// webcam
} else if (((int)argv1.find("/dev/"))>=0) {
modo = M_VIDEO_WEBCAM;
dev = argv1.c_str();
// config file.
} else if (lpos>=0 && (argv1.find(".txt")!=string::npos || argv1.find(".dat")!=string::npos )) {
modo = M_VIDEO_IPCAM;
cameras = readConfigurationFile(argv[1]);
// if rtsp
} else if ( argv1.find("rtsp://")!=string::npos ) {
modo = M_VIDEO_IPCAM;
url = argv[1];
// try IPCAM again.
} else {
modo = M_VIDEO_IPCAM;
}
//printf("argv1.find(/dev/) = %ld \n", argv1.find("/dev/") );
//printf("modo = %d %s\n", modo, dev);
if (modo == M_VIDEO_IPCAM || modo == M_VIDEO_WEBCAM)
{
if (modo == M_VIDEO_IPCAM)
{
printf("modo M_VIDEO_IPCAM\n");
if (cameras.empty()) {
if (url==NULL) {
printf("Looking for IP\n");
DiscoveryRTSP_IP disc("192.168.15.2"); // TODO dectar IP/rede auto
if (argc>2)
disc = DiscoveryRTSP_IP(argv[2]);
std::list<char *> ips = disc.discovery();
if (ips.empty()) {
std::cout << "Not possible to found the camera ip." << std::endl;
return -1;
}
printf("Just looking for the IPs. I don't know the url. Exiting.\n");
// TODO discovery the rtsp url.
return 0;
}
else {
char*url2 = new char[100];
memcpy(url2, url, strlen(url)+1);
urls.push_back(url2);
}
int i=0;
for (std::list<char*>::iterator it=urls.begin(); it!=urls.end(); ++it) {
title = new char[50];
sprintf(title, "cam%02d", i++);
bool udp = (argc>2 && strcmp(argv[2], "udp")==0);
cameras.push_back(new Camera(title, *it, udp, true));
}
}
}
else if (modo == M_VIDEO_WEBCAM) {
printf("modo M_VIDEO_WEBCAM\n");
if (dev==NULL)
dev = "/dev/video0";
char *url = new char[50];
title = new char[50];
sprintf(title, "webcam");
sprintf(url, "%s", dev);
cameras.push_back(new Camera(title, url, false, true) );
}
for (int i=0 ; i<cameras.size() ; i++) {
if (cameras[i]->isToSave())
cameras[i]->registerMovementListener(new VideoWriterManager(cameras[i]->getTitle()));
cameras[i]->start(); // start the thread
}
VideoPlayer *vp = new VideoPlayer(cameras, "Onvif PVR");
vp->run(); // not a thread
delete vp;
}
printf("Exiting...\n");
return 0;
}
std::vector<Camera *> readConfigurationFile(const char * file) {
std::ifstream stream;
stream.open(file);
std::string line;
std::string url;
bool udp;
bool save;
std::vector<Camera *> cameras;
int index = 1;
char *title;
char *url_c;
if (stream.is_open()) {
while(!stream.eof()) {
std::getline(stream, line);
if (line[0]!='#' && line[0]!='\0') // # comments the line. # must be the first character.
{
size_t pos = line.find_first_of (" \t\n\r");
std::string url = line.substr( 0, pos );
pos = line.find("udp");
udp = pos!=std::string::npos;
pos = line.find("save");
save = pos!=std::string::npos;
url_c = new char[100];
title = new char[50];
sprintf(url_c, "%s", url.c_str());
sprintf(title, "cam%02d", index++);
Camera * cam = new Camera(title, url_c, udp, save);
cameras.push_back(cam);
}
}
stream.close();
}
else {
printf("Error opening file %s\n", file);
}
return cameras;
}