-
Notifications
You must be signed in to change notification settings - Fork 6
/
device.c
462 lines (420 loc) · 13.4 KB
/
device.c
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* device.c: IPTV plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
#include "config.h"
#include "source.h"
#include "device.h"
#define IPTV_MAX_DEVICES MAXDEVICES
static cIptvDevice * IptvDevicesS[IPTV_MAX_DEVICES] = { NULL };
cIptvDevice::cIptvDevice(unsigned int indexP)
: deviceIndexM(indexP),
dvrFdM(-1),
isPacketDeliveredM(false),
isOpenDvrM(false),
sidScanEnabledM(false),
pidScanEnabledM(false),
channelM()
{
unsigned int bufsize = (unsigned int)IPTV_BUFFER_SIZE;
bufsize -= (bufsize % TS_SIZE);
info("Creating IPTV device %d (CardIndex=%d)", deviceIndexM, CardIndex());
tsBufferM = new cRingBufferLinear(bufsize + 1, TS_SIZE, false,
*cString::sprintf("IPTV TS %d", deviceIndexM));
if (tsBufferM) {
tsBufferM->SetTimeouts(100, 100);
tsBufferM->SetIoThrottle();
pIptvStreamerM = new cIptvStreamer(*this, tsBufferM->Free());
}
pUdpProtocolM = new cIptvProtocolUdp();
pCurlProtocolM = new cIptvProtocolCurl();
pHttpProtocolM = new cIptvProtocolHttp();
pFileProtocolM = new cIptvProtocolFile();
pExtProtocolM = new cIptvProtocolExt();
pPidScannerM = new cPidScanner();
// Start section handler for iptv device
pIptvSectionM = new cIptvSectionFilterHandler(deviceIndexM, bufsize + 1);
StartSectionHandler();
// Sid scanner must be created after the section handler
AttachFilter(pSidScannerM = new cSidScanner());
// Check if dvr fifo exists
struct stat sb;
cString filename = cString::sprintf(IPTV_DVR_FILENAME, deviceIndexM);
stat(filename, &sb);
if (S_ISFIFO(sb.st_mode)) {
dvrFdM = open(filename, O_RDWR | O_NONBLOCK);
if (dvrFdM >= 0)
info("IPTV device %d redirecting input stream to '%s'", deviceIndexM, *filename);
}
}
cIptvDevice::~cIptvDevice()
{
debug1("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
// Stop section handler of iptv device
StopSectionHandler();
DELETE_POINTER(pIptvSectionM);
DELETE_POINTER(pSidScannerM);
DELETE_POINTER(pPidScannerM);
DELETE_POINTER(pIptvStreamerM);
DELETE_POINTER(pExtProtocolM);
DELETE_POINTER(pFileProtocolM);
DELETE_POINTER(pHttpProtocolM);
DELETE_POINTER(pCurlProtocolM);
DELETE_POINTER(pUdpProtocolM);
DELETE_POINTER(tsBufferM);
// Close dvr fifo
if (dvrFdM >= 0) {
int fd = dvrFdM;
dvrFdM = -1;
close(fd);
}
}
bool cIptvDevice::Initialize(unsigned int deviceCountP)
{
debug1("%s (%u)", __PRETTY_FUNCTION__, deviceCountP);
new cIptvSourceParam(IPTV_SOURCE_CHARACTER, "IPTV");
if (deviceCountP > IPTV_MAX_DEVICES)
deviceCountP = IPTV_MAX_DEVICES;
for (unsigned int i = 0; i < deviceCountP; ++i)
IptvDevicesS[i] = new cIptvDevice(i);
for (unsigned int i = deviceCountP; i < IPTV_MAX_DEVICES; ++i)
IptvDevicesS[i] = NULL;
return true;
}
void cIptvDevice::Shutdown(void)
{
debug1("%s", __PRETTY_FUNCTION__);
for (int i = 0; i < IPTV_MAX_DEVICES; ++i) {
if (IptvDevicesS[i])
IptvDevicesS[i]->CloseDvr();
}
}
unsigned int cIptvDevice::Count(void)
{
unsigned int count = 0;
debug1("%s", __PRETTY_FUNCTION__);
for (unsigned int i = 0; i < IPTV_MAX_DEVICES; ++i) {
if (IptvDevicesS[i] != NULL)
count++;
}
return count;
}
cIptvDevice *cIptvDevice::GetIptvDevice(int cardIndexP)
{
debug16("%s (%d)", __PRETTY_FUNCTION__, cardIndexP);
for (unsigned int i = 0; i < IPTV_MAX_DEVICES; ++i) {
if (IptvDevicesS[i] && (IptvDevicesS[i]->CardIndex() == cardIndexP)) {
debug16("%s (%d) Found", __PRETTY_FUNCTION__, cardIndexP);
return IptvDevicesS[i];
}
}
return NULL;
}
cString cIptvDevice::GetGeneralInformation(void)
{
debug16("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
LOCK_CHANNELS_READ;
return cString::sprintf("IPTV device: %d\nCardIndex: %d\nStream: %s\nStream bitrate: %s\n%sChannel: %s",
deviceIndexM, CardIndex(),
pIptvStreamerM ? *pIptvStreamerM->GetInformation() : "",
pIptvStreamerM ? *pIptvStreamerM->GetStreamerStatistic() : "",
*GetBufferStatistic(),
*Channels->GetByNumber(cDevice::CurrentChannel())->ToText());
}
cString cIptvDevice::GetPidsInformation(void)
{
debug16("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
return GetPidStatistic();
}
cString cIptvDevice::GetFiltersInformation(void)
{
debug16("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
return cString::sprintf("Active section filters:\n%s", pIptvSectionM ? *pIptvSectionM->GetInformation() : "");
}
cString cIptvDevice::GetInformation(unsigned int pageP)
{
// generate information string
cString s;
switch (pageP) {
case IPTV_DEVICE_INFO_GENERAL:
s = GetGeneralInformation();
break;
case IPTV_DEVICE_INFO_PIDS:
s = GetPidsInformation();
break;
case IPTV_DEVICE_INFO_FILTERS:
s = GetFiltersInformation();
break;
case IPTV_DEVICE_INFO_PROTOCOL:
s = pIptvStreamerM ? *pIptvStreamerM->GetInformation() : "";
break;
case IPTV_DEVICE_INFO_BITRATE:
s = pIptvStreamerM ? *pIptvStreamerM->GetStreamerStatistic() : "";
break;
default:
s = cString::sprintf("%s%s%s",
*GetGeneralInformation(),
*GetPidsInformation(),
*GetFiltersInformation());
break;
}
return s;
}
cString cIptvDevice::DeviceType(void) const
{
debug16("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
return "IPTV";
}
cString cIptvDevice::DeviceName(void) const
{
debug1("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
return cString::sprintf("IPTV %d", deviceIndexM);
}
int cIptvDevice::SignalStrength(void) const
{
debug1("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
return (100);
}
int cIptvDevice::SignalQuality(void) const
{
debug1("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
return (100);
}
bool cIptvDevice::ProvidesSource(int sourceP) const
{
debug1("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
return (cSource::IsType(sourceP, IPTV_SOURCE_CHARACTER));
}
bool cIptvDevice::ProvidesTransponder(const cChannel *channelP) const
{
debug1("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
return (ProvidesSource(channelP->Source()));
}
bool cIptvDevice::ProvidesChannel(const cChannel *channelP, int priorityP, bool *needsDetachReceiversP) const
{
bool result = false;
bool hasPriority = (priorityP == IDLEPRIORITY) || (priorityP > this->Priority());
bool needsDetachReceivers = false;
debug1("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
if (channelP && ProvidesTransponder(channelP)) {
result = hasPriority;
if (Receiving()) {
if (channelP->GetChannelID() == channelM.GetChannelID())
result = true;
else
needsDetachReceivers = Receiving();
}
}
if (needsDetachReceiversP)
*needsDetachReceiversP = needsDetachReceivers;
return result;
}
bool cIptvDevice::ProvidesEIT(void) const
{
return false;
}
int cIptvDevice::NumProvidedSystems(void) const
{
return 1;
}
const cChannel *cIptvDevice::GetCurrentlyTunedTransponder(void) const
{
return &channelM;
}
bool cIptvDevice::IsTunedToTransponder(const cChannel *channelP) const
{
return channelP ? (channelP->GetChannelID() == channelM.GetChannelID()) : false;
}
bool cIptvDevice::MaySwitchTransponder(const cChannel *channelP) const
{
return cDevice::MaySwitchTransponder(channelP);
}
bool cIptvDevice::SetChannelDevice(const cChannel *channelP, bool liveViewP)
{
cIptvProtocolIf *protocol;
cIptvTransponderParameters itp(channelP->Parameters());
debug1("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
if (isempty(itp.Address())) {
error("Unrecognized IPTV address: %s", channelP->Parameters());
return false;
}
switch (itp.Protocol()) {
case cIptvTransponderParameters::eProtocolUDP:
protocol = pUdpProtocolM;
break;
case cIptvTransponderParameters::eProtocolCURL:
protocol = pCurlProtocolM;
break;
case cIptvTransponderParameters::eProtocolHTTP:
protocol = pHttpProtocolM;
break;
case cIptvTransponderParameters::eProtocolFILE:
protocol = pFileProtocolM;
break;
case cIptvTransponderParameters::eProtocolEXT:
protocol = pExtProtocolM;
break;
default:
error("Unrecognized IPTV protocol: %s", channelP->Parameters());
return false;
break;
}
sidScanEnabledM = itp.SidScan() ? true : false;
pidScanEnabledM = itp.PidScan() ? true : false;
if (pIptvStreamerM && pIptvStreamerM->SetSource(itp.Address(), itp.Parameter(), deviceIndexM, protocol)) {
channelM = *channelP;
if (sidScanEnabledM && pSidScannerM && IptvConfig.GetSectionFiltering())
pSidScannerM->SetChannel(channelM.GetChannelID());
if (pidScanEnabledM && pPidScannerM)
pPidScannerM->SetChannel(channelM.GetChannelID());
}
return true;
}
bool cIptvDevice::SetPid(cPidHandle *handleP, int typeP, bool onP)
{
debug1("%s (%d, %d, %d) [device %d]", __PRETTY_FUNCTION__, handleP ? handleP->pid : -1, typeP, onP, deviceIndexM);
if (pIptvStreamerM && handleP)
return pIptvStreamerM->SetPid(handleP->pid, typeP, onP);
return true;
}
int cIptvDevice::OpenFilter(u_short pidP, u_char tidP, u_char maskP)
{
debug16("%s (%d, %d, %d) [device %d]", __PRETTY_FUNCTION__, pidP, tidP, maskP, deviceIndexM);
if (pIptvSectionM && IptvConfig.GetSectionFiltering()) {
if (pIptvStreamerM)
pIptvStreamerM->SetPid(pidP, ptOther, true);
return pIptvSectionM->Open(pidP, tidP, maskP);
}
return -1;
}
void cIptvDevice::CloseFilter(int handleP)
{
debug16("%s (%d) [device %d]", __PRETTY_FUNCTION__, handleP, deviceIndexM);
if (pIptvSectionM) {
if (pIptvStreamerM)
pIptvStreamerM->SetPid(pIptvSectionM->GetPid(handleP), ptOther, false);
pIptvSectionM->Close(handleP);
}
}
bool cIptvDevice::OpenDvr(void)
{
debug1("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
isPacketDeliveredM = false;
tsBufferM->Clear();
if (pIptvStreamerM)
pIptvStreamerM->Open();
if (sidScanEnabledM && pSidScannerM && IptvConfig.GetSectionFiltering())
pSidScannerM->Open();
if (pidScanEnabledM && pPidScannerM)
pPidScannerM->Open();
isOpenDvrM = true;
return true;
}
void cIptvDevice::CloseDvr(void)
{
debug1("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
if (pidScanEnabledM && pPidScannerM)
pPidScannerM->Close();
if (sidScanEnabledM && pSidScannerM)
pSidScannerM->Close();
if (pIptvStreamerM)
pIptvStreamerM->Close();
isOpenDvrM = false;
}
bool cIptvDevice::HasLock(int timeoutMsP) const
{
debug16("%s (%d) [device %d]", __PRETTY_FUNCTION__, timeoutMsP, deviceIndexM);
return (pIptvStreamerM && pIptvStreamerM->Active());
}
bool cIptvDevice::HasInternalCam(void)
{
debug16("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
return false;
}
void cIptvDevice::WriteData(uchar *bufferP, int lengthP)
{
debug16("%s (, %d) [device %d]", __PRETTY_FUNCTION__, lengthP, deviceIndexM);
int len;
// Send data to dvr fifo
if (dvrFdM >= 0)
len = write(dvrFdM, bufferP, lengthP);
// Fill up TS buffer
if (tsBufferM) {
len = tsBufferM->Put(bufferP, lengthP);
if (len != lengthP)
tsBufferM->ReportOverflow(lengthP - len);
}
// Filter the sections
if (pIptvSectionM && IptvConfig.GetSectionFiltering())
pIptvSectionM->Write(bufferP, lengthP);
}
unsigned int cIptvDevice::CheckData(void)
{
debug16("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
if (tsBufferM)
return (unsigned int)tsBufferM->Free();
return 0;
}
uchar *cIptvDevice::GetData(int *availableP)
{
debug16("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
if (isOpenDvrM && tsBufferM) {
int count = 0;
if (isPacketDeliveredM)
SkipData(TS_SIZE);
uchar *p = tsBufferM->Get(count);
if (p && count >= TS_SIZE) {
if (*p != TS_SYNC_BYTE) {
for (int i = 1; i < count; i++) {
if (p[i] == TS_SYNC_BYTE) {
count = i;
break;
}
}
tsBufferM->Del(count);
info("Skipped %d bytes to sync on TS packet", count);
return NULL;
}
isPacketDeliveredM = true;
if (availableP)
*availableP = count;
// Update pid statistics
AddPidStatistic(ts_pid(p), payload(p));
return p;
}
}
return NULL;
}
void cIptvDevice::SkipData(int countP)
{
debug16("%s (%d) [device %d]]", __PRETTY_FUNCTION__, countP, deviceIndexM);
tsBufferM->Del(countP);
isPacketDeliveredM = false;
// Update buffer statistics
AddBufferStatistic(countP, tsBufferM->Available());
}
bool cIptvDevice::GetTSPacket(uchar *&dataP)
{
debug16("%s [device %d]", __PRETTY_FUNCTION__, deviceIndexM);
if (tsBufferM) {
if (cCamSlot *cs = CamSlot()) {
if (cs->WantsTsData()) {
int available;
dataP = GetData(&available);
if (dataP) {
dataP = cs->Decrypt(dataP, available);
SkipData(available);
}
return true;
}
}
dataP = GetData();
return true;
}
// Reduce cpu load by preventing busylooping
cCondWait::SleepMs(10);
dataP = NULL;
return true;
}