-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDVBInterface.cpp
More file actions
342 lines (310 loc) · 9.62 KB
/
Copy pathDVBInterface.cpp
File metadata and controls
342 lines (310 loc) · 9.62 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#define DEBUG_TUNING 1
#include "DVBInterface.h"
#include "DVBTable.h"
#include "ProgramAssociationTable.h"
#include "ProgramMapTable.h"
#include "ServiceDescriptionTable.h"
#include "NetworkInformationTable.h"
#include "Util.h"
#include <cstring>
extern "C" {
#include <sys/ioctl.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <unistd.h>
#include <linux/dvb/dmx.h>
}
DVBInterface::DVBInterface(int num, std::string const &devPath):_frontendFd(-1),_currentTransponder(nullptr),_lnb(nullptr) {
_devPath = devPath;
if(_devPath[_devPath.length()-1] != '/')
_devPath += '/';
_devPath += "adapter" + std::to_string(num) + "/";
for(int i=0; i<DMX_PES_OTHER+1; i++)
_pesFd[i] = -1;
memset(&_feInfo, 0, sizeof(dvb_frontend_info));
int fd = open("frontend0", O_RDWR);
if(fd >= 0) {
ioctl(fd, FE_GET_INFO, &_feInfo);
::close(fd);
}
if(canQPSK())
_lnb = &Lnb::Universal;
}
std::string DVBInterface::FEC() const {
std::string ret;
if(canFEC1_2())
ret += " 1/2";
if(canFEC2_3())
ret += " 2/3";
if(canFEC3_4())
ret += " 3/4";
if(canFEC4_5())
ret += " 4/5";
if(canFEC5_6())
ret += " 5/6";
if(canFEC6_7())
ret += " 6/7";
if(canFEC7_8())
ret += " 7/8";
if(canFEC8_9())
ret += " 8/9";
if(canFECAuto())
ret += " Auto";
if(canTurboFEC())
ret += " TurboFEC";
return ret.empty() ? ret : ret.substr(1);
}
std::string DVBInterface::QAM() const {
std::string ret;
if(canQAM16())
ret += " 16-QAM";
if(canQAM32())
ret += " 32-QAM";
if(canQAM64())
ret += " 64-QAM";
if(canQAM128())
ret += " 128-QAM";
if(canQAM256())
ret += " 256-QAM";
if(canQAMAuto())
ret += " Auto";
return ret.size() ? ret.substr(1) : ret;
}
std::bitset<6> DVBInterface::status() const {
fe_status s;
int fd = (_frontendFd >= 0) ? _frontendFd : open("frontend0", O_RDONLY);
if(fd >= 0)
ioctl(fd, FE_READ_STATUS, &s);
return s;
}
int DVBInterface::open(std::string const &dev, int mode) const {
return ::open((_devPath + dev).c_str(), mode);
}
bool DVBInterface::resetDiseqcOverload() const {
return !ioctl(frontendFd(), FE_DISEQC_RESET_OVERLOAD);
}
bool DVBInterface::tune(Transponder const &t, uint32_t timeout) {
if(t.tune(this, timeout)) {
_currentTransponder = &t;
return true;
}
return false;
}
void DVBInterface::close() {
if(_frontendFd >= 0) {
::close(_frontendFd);
_frontendFd = -1;
}
for(int i=0; i<DMX_PES_OTHER+1; i++) {
if(_pesFd[i] >= 0) {
::close(_pesFd[i]);
_pesFd[i] = -1;
}
}
}
std::vector<Service> DVBInterface::scanTransponder() {
int dmx = open("demux0", O_RDWR|O_NONBLOCK);
if(dmx < 0)
return std::vector<Service>();
#if 0
// Code kept here for reference for now
// We don't need to scan the PAT and PMT before
// knowing what channel we want.
// May want to fall back to PAT/PMT scanning if there's
// no proper SDT or if we find any service IDs not mentioned
// in the SDT.
ProgramAssociationTables *pats = DVBTables<ProgramAssociationTable>::read<ProgramAssociationTables>(dmx);
if(!pats) // Bogus transponder didn't even send a PAT on time
return std::vector<Service>();
cerr << "Transport stream ID " << (*pats->begin())->number() << std::endl;
std::map<uint16_t,uint16_t> PMTPids = pats->pids();
delete pats;
std::vector<Program> programs;
for(auto const &p: PMTPids) {
if(p.first == 0) continue;
ProgramMapTables *pmts = DVBTables<ProgramMapTable>::read<ProgramMapTables>(dmx, p.second);
if(!pmts) // Dropped w/ timeout or other error
continue;
programs.push_back(Program(*pmts));
delete pmts;
}
for(auto const &p: programs)
p.dump(std::cerr);
#endif
ServiceDescriptionTables *sdts = DVBTables<ServiceDescriptionTable>::read<ServiceDescriptionTables>(dmx);
::close(dmx);
if(!sdts)
return std::vector<Service>();
std::vector<Service> services=sdts->services();
delete sdts;
return services;
}
void DVBInterface::scan() {
int dmx = open("demux0", O_RDWR|O_NONBLOCK);
if(dmx < 0)
return;
NetworkInformationTables *nits = DVBTables<NetworkInformationTable>::read<NetworkInformationTables>(dmx, nitPID());
::close(dmx);
if(!nits)
return;
nits->dump();
std::vector<Transponder*> t=nits->transponders();
for(auto const &tp: t) {
if(tune(tp, 5000000))
std::cerr << "Found transponder at " << tp->frequency() << std::endl;
else
std::cerr << "Transponder at " << tp->frequency() << " is listed in NIT, but doesn't seem to exist" << std::endl;
}
}
bool DVBInterface::setup(Service const &s) {
int dmx = open("demux0", O_RDWR|O_NONBLOCK);
ProgramAssociationTables *pats = DVBTables<ProgramAssociationTable>::read<ProgramAssociationTables>(dmx);
::close(dmx);
if(!pats)
return false;
std::map<uint16_t,uint16_t> PMTPids = pats->pids();
auto pmtpid = PMTPids.find(s.serviceId());
if(pmtpid == PMTPids.end()) {
std::cerr << "Service ID not found in PAT" << std::endl;
return false;
}
dmx = open("demux0", O_RDWR|O_NONBLOCK);
ProgramMapTables *pmts = DVBTables<ProgramMapTable>::read<ProgramMapTables>(dmx, (*pmtpid).second);
::close(dmx);
if(!pmts) {
delete pats;
std::cerr << "No PMT" << std::endl;
return false;
}
Program p(*pmts);
p.dump();
addPES(Stream::PCR, p.pcrPid());
for(auto const &s: p.streams())
addPES(s.type(), s.pid());
return true;
}
bool DVBInterface::addPES(Stream::StreamType t, uint16_t pid, bool fallbackToAny) {
// The code below assumes that dmx_pes_type_t is a list of
// audio, video, teletext, subtitle, pcr, audio, video, ...
// If the layout of dmx_pes_type_t ever changes in the kernel,
// we need to make some adjustments here.
// In the mean time, this calculation is better than going from
// hardcoded DMX_PES_AUDIO0 to DMX_PES_AUDIO1 to DMX_PES_AUDIO2
// etc. -- our way automatically supports DMX_PES_AUDIO4 etc.
// if the API is ever extended to handle more than 4 concurrent
// streams of each type.
dmx_pes_filter_params f;
f.pid = pid;
f.input = DMX_IN_FRONTEND;
f.output = DMX_OUT_TS_TAP;
f.pes_type = DMX_PES_PCR0;
f.flags = DMX_CHECK_CRC|DMX_IMMEDIATE_START;
switch(t) {
case Stream::Audio:
f.pes_type = DMX_PES_AUDIO0;
while(f.pes_type<=DMX_PES_OTHER && _pesFd[f.pes_type]>=0)
f.pes_type = static_cast<dmx_pes_type_t>(f.pes_type+DMX_PES_AUDIO1-DMX_PES_AUDIO0);
break;
case Stream::Video:
f.pes_type = DMX_PES_VIDEO0;
while(f.pes_type<=DMX_PES_OTHER && _pesFd[f.pes_type]>=0)
f.pes_type = static_cast<dmx_pes_type_t>(f.pes_type+DMX_PES_VIDEO1-DMX_PES_VIDEO0);
break;
case Stream::Teletext:
f.pes_type = DMX_PES_TELETEXT0;
while(f.pes_type<=DMX_PES_OTHER && _pesFd[f.pes_type]>=0)
f.pes_type = static_cast<dmx_pes_type_t>(f.pes_type+DMX_PES_TELETEXT1-DMX_PES_TELETEXT0);
break;
case Stream::Subtitle:
f.pes_type = DMX_PES_SUBTITLE0;
while(f.pes_type<=DMX_PES_OTHER && _pesFd[f.pes_type]>=0)
f.pes_type = static_cast<dmx_pes_type_t>(f.pes_type+DMX_PES_SUBTITLE1-DMX_PES_SUBTITLE0);
break;
case Stream::PCR:
f.pes_type = DMX_PES_PCR0;
while(f.pes_type<=DMX_PES_OTHER && _pesFd[f.pes_type]>=0)
f.pes_type = static_cast<dmx_pes_type_t>(f.pes_type+DMX_PES_PCR1-DMX_PES_PCR0);
break;
case Stream::Other:
if(_pesFd[DMX_PES_OTHER]>=0)
f.pes_type = static_cast<dmx_pes_type_t>(DMX_PES_OTHER+1);
else
f.pes_type = DMX_PES_OTHER;
break;
case Stream::Any:
break;
}
if(t == Stream::Any ||
(fallbackToAny &&
(f.pes_type > DMX_PES_OTHER ||
_pesFd[f.pes_type]>=0))) {
f.pes_type = static_cast<dmx_pes_type_t>(DMX_PES_OTHER+1);
for(int i=0; i<=DMX_PES_OTHER; i++)
if(_pesFd[i]<0)
f.pes_type = static_cast<dmx_pes_type_t>(i);
}
if(f.pes_type > DMX_PES_OTHER)
return false;
openPES(f.pes_type);
ioctl(_pesFd[f.pes_type], DMX_SET_PES_FILTER, &f);
return true;
}
int DVBInterface::openPES(dmx_pes_type_t pes) {
if(_pesFd[pes]>=0) {
ioctl(_pesFd[pes], DMX_STOP);
::close(_pesFd[pes]);
}
return _pesFd[pes] = open("demux0", O_RDWR);
}
uint16_t DVBInterface::nitPID() const {
int dmx = open("demux0", O_RDWR|O_NONBLOCK);
ProgramAssociationTables *pats = DVBTables<ProgramAssociationTable>::read<ProgramAssociationTables>(dmx);
::close(dmx);
if(!pats)
return NIT;
std::map<uint16_t,uint16_t> PMTPids = pats->pids();
if(PMTPids.count(0))
return PMTPids[0];
return NIT;
}
std::vector<Transponder*> DVBInterface::scanTransponders() {
int dmx = open("demux0", O_RDWR|O_NONBLOCK);
std::cerr << "Reading NIT" << std::endl;
auto nit = DVBTables<NetworkInformationTable>::read<NetworkInformationTables>(dmx, nitPID());
::close(dmx);
if(!nit) {
std::cerr << "No NIT" << std::endl;
return std::vector<Transponder*>();
}
std::cerr << "Looking at transponders" << std::endl;
return nit->transponders();
}
/* More Diseqc bits not yet supported (no satellite dish to test with...):
* https://linuxtv.org/downloads/v4l-dvb-apis-new/uapi/dvb/fe-diseqc-send-master-cmd.html
* https://linuxtv.org/downloads/v4l-dvb-apis-new/uapi/dvb/fe-diseqc-recv-slave-reply.html
* https://linuxtv.org/downloads/v4l-dvb-apis-new/uapi/dvb/fe-diseqc-send-burst.html
* https://linuxtv.org/downloads/v4l-dvb-apis-new/uapi/dvb/fe-set-tone.html
* https://linuxtv.org/downloads/v4l-dvb-apis-new/uapi/dvb/fe-set-voltage.html
* https://linuxtv.org/downloads/v4l-dvb-apis-new/uapi/dvb/fe-enable-high-lnb-voltage.html
* https://linuxtv.org/downloads/v4l-dvb-apis-new/uapi/dvb/fe-set-frontend-tune-mode.html
*
* Obscure features not yet supported:
* https://linuxtv.org/downloads/v4l-dvb-apis-new/uapi/dvb/fe-set-frontend-tune-mode.html
*/
/*
* ioctl(fd, FE_GET_PROPERTY, struct dtv_properties *arg);
* ioctl(fd, FE_SET_PROPERTY, struct dtv_properties *arg);
* dtv_properties:
* uint32_t num
* struct dtv_property *props
*/
DVBInterfaces DVBInterfaces::all() {
DVBInterfaces cards;
for(int i=0; ; i++) {
DVBInterface c = DVBInterface(i);
if(!c.exists())
break;
cards.push_back(c);
}
return cards;
}