-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.cpp
218 lines (188 loc) · 6.25 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* ---------------------------------------------------------------------------
** This software is in the public domain, furnished "as is", without technical
** support, and with no warranty, express or implied, as to its usefulness for
** any purpose.
**
** main.cpp
**
** Simple Test Using live555helper
** -------------------------------------------------------------------------*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <signal.h>
#include "environment.h"
#include "rtspconnectionclient.h"
#include "sdpclient.h"
#include "mkvclient.h"
class RTSPCallback : public RTSPConnection::Callback
{
private:
std::map<std::string,std::ofstream> m_ofs;
std::string m_fileprefix;
public:
RTSPCallback(const std::string & output) : m_fileprefix(output) {}
virtual bool onNewSession(const char* id, const char* media, const char* codec, const char*) {
if (!m_fileprefix.empty()) {
auto it = m_ofs.find(id);
if (it == m_ofs.end()) {
std::string filename = m_fileprefix + "_" + media + "_" + codec + "_" + id;
m_ofs[id].open(filename.c_str(), std::ofstream::out | std::ofstream::trunc);
}
}
std::cout << id << " " << media << "/" << codec << std::endl;
return true;
}
virtual bool onData(const char* id, unsigned char* buffer, ssize_t size, struct timeval presentationTime) {
std::cout << id << " " << size << " ts:" << presentationTime.tv_sec << "." << presentationTime.tv_usec << std::endl;
auto it = m_ofs.find(id);
if (it != m_ofs.end()) {
it->second.write((char*)buffer, size);
}
return true;
}
virtual void onError(RTSPConnection& connection, const char* message) {
std::cout << "Error:" << message << std::endl;
connection.start(10);
}
virtual void onConnectionTimeout(RTSPConnection& connection) {
std::cout << "Connection timeout -> retry" << std::endl;
connection.start();
}
virtual void onDataTimeout(RTSPConnection& connection) {
std::cout << "Data timeout -> retry" << std::endl;
connection.start();
}
};
class SDPCallback : public SDPClient::Callback
{
private:
std::map<std::string,std::ofstream> m_ofs;
std::string m_fileprefix;
public:
SDPCallback(const std::string & output) : m_fileprefix(output) {}
virtual bool onNewSession(const char* id, const char* media, const char* codec, const char*) {
if (!m_fileprefix.empty()) {
auto it = m_ofs.find(id);
if (it == m_ofs.end()) {
std::string filename = m_fileprefix + "_" + media + "_" + codec + "_" + id;
m_ofs[id].open(filename.c_str(), std::ofstream::out | std::ofstream::trunc);
}
}
std::cout << id << " " << media << "/" << codec << std::endl;
return true;
}
virtual bool onData(const char* id, unsigned char* buffer, ssize_t size, struct timeval presentationTime) {
std::cout << id << " " << size << " ts:" << presentationTime.tv_sec << "." << presentationTime.tv_usec << std::endl;
auto it = m_ofs.find(id);
if (it != m_ofs.end()) {
it->second.write((char*)buffer, size);
}
return true;
}
virtual void onError(SDPClient& connection, const char* message) {
std::cout << "Error:" << message << std::endl;
}
};
class MKVCallback : public MKVClient::Callback
{
private:
std::map<std::string,std::ofstream> m_ofs;
std::string m_fileprefix;
public:
MKVCallback(const std::string & output) : m_fileprefix(output) {}
virtual bool onNewSession(const char* id, const char* media, const char* codec, const char*) {
if (!m_fileprefix.empty()) {
auto it = m_ofs.find(id);
if (it == m_ofs.end()) {
std::string filename = m_fileprefix + "_" + media + "_" + codec + "_" + id;
m_ofs[id].open(filename.c_str(), std::ofstream::out | std::ofstream::trunc);
}
}
std::cout << id << " " << media << "/" << codec << std::endl;
return true;
}
virtual bool onData(const char* id, unsigned char* buffer, ssize_t size, struct timeval presentationTime) {
std::cout << id << " " << size << " ts:" << presentationTime.tv_sec << "." << presentationTime.tv_usec << std::endl;
auto it = m_ofs.find(id);
if (it != m_ofs.end()) {
it->second.write((char*)buffer, size);
}
return true;
}
};
char stop = 0;
void sig_handler(int signo)
{
if (signo == SIGINT) {
printf("received SIGINT\n");
stop = 1;
}
}
void usage(const char* app) {
std::cout << "Usage: " << app << " url" << std::endl;
}
int main(int argc, char *argv[])
{
// default value
int timeout = 10;
std::string rtptransport = "udp";
int logLevel = 255;
std::string output;
std::map<std::string,std::string> opts;
// decode args
int c = 0;
while ((c = getopt (argc, argv, "hv:" "t:o:" "MTH")) != -1)
{
switch (c)
{
case 'v': logLevel = atoi(optarg); break;
case 'h': usage(argv[0]); return 0;
case 't': timeout= atoi(optarg); break;
case 'o': output = optarg; break;
case 'M': rtptransport = "multicast"; break;
case 'T': rtptransport = "tcp" ; break;
case 'H': rtptransport = "http" ; break;
}
}
opts["timeout"] = std::to_string(timeout);
opts["rtptransport"] = rtptransport;
if (optind<argc)
{
Environment env(stop);
std::string url = argv[optind];
if ( (url.find("rtsp://") == 0) || (url.find("rtsps://") == 0) ) {
RTSPCallback cb(output);
RTSPConnection rtspClient(env, &cb, url.c_str(), opts, logLevel);
signal(SIGINT, sig_handler);
std::cout << "Start mainloop" << std::endl;
env.mainloop();
} else if (url.find("file://") == 0) {
MKVCallback cb(output);
MKVClient mkvClient(env, &cb, url.c_str(), std::map<std::string,std::string>());
signal(SIGINT, sig_handler);
std::cout << "Start mainloop" << std::endl;
env.mainloop();
} else {
std::string sdp;
if (url.find("rtp://") == 0) {
sdp.assign(SDPClient::getSdpFromRtpUrl(url));
} else {
std::ifstream is(url);
sdp.assign((std::istreambuf_iterator<char>(is)), std::istreambuf_iterator<char>());
}
std::cout << "SDP:\n" << sdp << std::endl;
SDPCallback cb(output);
SDPClient rtpClient(env, &cb, sdp.c_str(), std::map<std::string,std::string>());
signal(SIGINT, sig_handler);
std::cout << "Start mainloop" << std::endl;
env.mainloop();
}
}
else
{
usage(argv[0]);
}
return 0;
}