forked from berolinaro/dvbv5-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.cpp
289 lines (274 loc) · 8.04 KB
/
stream.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "DVBInterface.h"
#include "ChannelList.h"
#include "ProgramAssociationTable.h"
#include "ProgramMapTable.h"
#include "Util.h"
#include <iostream>
#include <fstream>
#include <cstring>
#include <thread>
#include <set>
#include <mutex>
extern "C" {
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <csignal>
#include <poll.h>
#include <unistd.h>
}
std::mutex newClientsMutex;
std::map<int,std::string> newClients;
Transponder const *currentTransponder = nullptr;
std::map<int,std::set<int>> watchers;
int threadNotify[2];
void httpThread(int fd) {
pollfd p;
p.fd = fd;
p.events = POLLIN|POLLHUP;
bool isGet = false, isHead = false, isBad = false, complete = false;
std::string path;
while(!complete) {
char buf[1024];
if(poll(&p, 1, 10000)>0) {
if(p.revents & POLLHUP) {
close(fd);
return;
}
ssize_t count = recv(fd, buf, 1024, 0);
buf[count]=0;
for(int i=0; i<count; i++) {
if(buf[i]=='\r') // Get rid of DOS-isms
memmove(buf+i, buf+i+1, (count--)-i);
}
Util::hexdump(reinterpret_cast<unsigned char*>(buf), count);
char *tmp;
complete = ((count>=2) && buf[count-2]=='\n' && buf[count-1]=='\n') || ((count == 1) && buf[0] == '\n');
char *line = strtok_r(buf, "\n", &tmp);
while(line) {
bool curHead = !strncasecmp(line, "HEAD ", 5);
bool curGet = !strncasecmp(line, "GET ", 4);
if(curHead || curGet) {
isHead = curHead;
isGet = curGet;
char *tmp1;
char *s = strtok_r(line, " ", &tmp1);
// Skip over "HEAD /" or "GET /" -- we've already seen that
s = strtok_r(nullptr, " ", &tmp1);
for(int i=1; i<strlen(s); i++) {
if(i<strlen(s)-2 && s[i] == '%' &&
((s[i+1] >= '0' && s[i+1] <= '9') || (s[i+1] >= 'a' && s[i+1] <= 'f') || (s[i+1] >= 'A' && s[i+1] <= 'F')) &&
((s[i+2] >= '0' && s[i+2] <= '9') || (s[i+2] >= 'a' && s[i+2] <= 'f') || (s[i+2] >= 'A' && s[i+2] <= 'F'))) {
char code[3] = {s[i+1], s[i+2], 0};
path.push_back(strtoul(code, nullptr, 16));
i+=2;
} else if(s[i] == '+')
path.push_back(' ');
else
path.push_back(s[i]);
}
s=strtok_r(nullptr, "", &tmp1);
if(!s || !strncmp(s, "HTTP/0", 6)) { // HTTP before 1.0 -- no extras expected
complete = true;
break;
}
} else if(!isHead && !isGet) {
isBad = true; complete = true;
break;
}
line = strtok_r(nullptr, "\n", &tmp);
}
} else {
close(fd);
break;
}
}
if(complete) {
std::cerr << "Got request for " << path << std::endl;
if(isGet || isHead) {
send(fd, "HTTP/1.1 200 OK\r\n", 17, 0);
send(fd, "Cache-Control: no-store, no-cache\r\n", 35, 0);
send(fd, "Content-Type: video/MP2T\r\n", 26, 0);
send(fd, "\r\n", 2, 0);
if(isGet) {
// Start sending the stream...
{
std::lock_guard<std::mutex> guard(newClientsMutex);
newClients.insert_or_assign(fd, path);
}
write(threadNotify[1], "x", 1);
std::cerr << "Client added" << std::endl;
} else {
close(fd);
}
} else {
send(fd, "HTTP/1.1 400 Bad Request\r\n", 26, 0);
send(fd, "Content-Type: text/plain\r\n", 26, 0);
send(fd, "\r\n", 2, 0);
send(fd, "Send HEAD or GET\r\n", 18, 0);
close(fd);
}
}
}
int main(int argc, char **argv) {
bool useIPv6 = argc>1 && !strcmp(argv[1], "-6");
signal(SIGPIPE, SIG_IGN);
int socksize;
int s;
union {
sockaddr addr;
sockaddr_in addr_in;
sockaddr_in6 addr_in6;
};
union {
sockaddr dest;
sockaddr_in dest_in;
sockaddr_in6 dest_in6;
};
memset(&addr, 0, sizeof(addr_in));
if(useIPv6) {
addr_in6.sin6_family = AF_INET6;
addr_in6.sin6_addr = in6addr_any;
addr_in6.sin6_port = htons(8080);
socksize = sizeof(addr_in6);
s = socket(AF_INET6, SOCK_STREAM, 0);
} else {
addr_in.sin_family = AF_INET;
addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
addr_in.sin_port = htons(8080);
socksize = sizeof(addr_in);
s = socket(AF_INET, SOCK_STREAM, 0);
}
if(s<0)
std::cerr << "socket: " << strerror(errno) << std::endl;
int b = bind(s, &addr, socksize);
while(b<0 && errno == EADDRINUSE) {
std::cerr << "Please kill any other process using the port" << std::endl;
sleep(1);
b = bind(s, &addr, socksize);
}
if(b<0)
std::cerr << "bind: " << strerror(errno) << std::endl;
int l = listen(s, SOMAXCONN);
if(l<0)
std::cerr << "listen: " << strerror(errno) << std::endl;
DVBInterfaces cards = DVBInterfaces::all();
if(!cards.size()) {
std::cerr << "No DVB interfaces found" << std::endl;
return 1;
}
ChannelList channels("channels.dvb");
if(!channels.size()) {
std::cerr << "No channel list - run \"scan >channels.dvb\"" << std::endl;
return 2;
}
int dvbFd = open("/dev/dvb/adapter0/dvr0", O_RDONLY|O_NONBLOCK);
char dvbbuf[188];
pipe(threadNotify);
pollfd p[128];
p[0].fd = s;
p[0].events = POLLIN;
p[1].fd = dvbFd;
p[1].events = POLLIN;
p[2].fd = threadNotify[0];
p[2].events = POLLIN;
int nfds = 3;
ProgramAssociationTables *pats;
while(true) {
if(poll(p, nfds, 10000) > 0) {
// Close connections to disconnected clients
for(int i=3; i<nfds; i++) {
if(p[i].revents & POLLHUP) {
close(p[i].fd);
for(auto w: watchers) {
auto entry = w.second.find(p[i].fd);
if(entry != w.second.end())
w.second.erase(entry);
}
memmove(&p[i], &p[i+1], sizeof(pollfd)*(nfds-i));
nfds--; i--;
std::cerr << "Client disconnected" << std::endl;
}
}
if(p[0].revents & POLLIN) {
socklen_t ss;
int conn = accept(s, &dest, &ss);
std::thread *http = new std::thread(&httpThread, conn);
}
if(p[1].revents & POLLIN) {
size_t const bytes = read(dvbFd, dvbbuf, 188);
if(bytes>0)
{
if(dvbbuf[0] != 0x47)
std::cerr << "Invalid sync byte" << std::endl;
uint16_t const pid = ((dvbbuf[1]&0b00011111)<<8)|dvbbuf[2];
auto w = watchers.find(pid);
if(w != watchers.end())
for(int fd: w->second)
write(fd, dvbbuf, bytes);
}
}
}
// Check for new connections
{
std::lock_guard<std::mutex> guard(newClientsMutex);
for(auto c: newClients) {
std::cerr << "Looking at new requests" << std::endl;
int const newFd = c.first;
auto newChannel = channels.find(c.second);
if(newChannel.first)
std::cerr << "Valid channel " << c.second << std::endl;
else {
std::cerr << "Invalid channel " << c.second << std::endl;
close(newFd);
continue;
}
if(!currentTransponder || (*currentTransponder != *newChannel.first)) {
std::cerr << "Switching transponder" << std::endl;
// FIXME no way to switch transponders gracefully... Probably shouldn't
// allow this to everyone!
for(int i=3; i<nfds; i++)
close(p[i].fd);
nfds = 3;
cards[0].tune(newChannel.first);
currentTransponder = newChannel.first;
int patDmx = cards[0].open("demux0", O_RDWR|O_NONBLOCK);
pats = DVBTables<ProgramAssociationTable>::read<ProgramAssociationTables>(patDmx);
close(patDmx);
} else
std::cerr << "Staying on current transponder" << std::endl;
if(pats) {
std::map<uint16_t,uint16_t> PMTPids = pats->pids();
if(PMTPids.find(newChannel.second->serviceId()) == PMTPids.end())
std::cerr << "new channel invalid" << std::endl;
else {
int pmtDmx=cards[0].open("demux0", O_RDWR|O_NONBLOCK);
ProgramMapTables *pmts = DVBTables<ProgramMapTable>::read<ProgramMapTables>(pmtDmx, PMTPids[newChannel.second->serviceId()]);
close(pmtDmx);
if(!pmts)
std::cerr << "No PMT for " << newChannel.second->serviceId() << std::endl;
else {
Program p(*pmts);
for(Stream s: p.streams()) {
if(watchers.find(s.pid()) == watchers.end()) {
if(s.pid() == 1)
continue;
if(cards[0].addPES(s.type(), s.pid(), true)) {
std::set<int> w;
w.insert(w.end(), newFd);
watchers.insert_or_assign(s.pid(), w);
}
} else
watchers[s.pid()].insert(watchers[s.pid()].end(), newFd);
}
}
}
} else
std::cerr << "No PAT" << std::endl;
p[nfds].fd = newFd;
p[nfds++].events = POLLOUT|POLLHUP;
}
newClients.clear();
}
}
}