2007-09-24 15:03:38 +02:00
|
|
|
/*
|
|
|
|
* sectionfilter.c: IPTV plugin for the Video Disk Recorder
|
|
|
|
*
|
|
|
|
* See the README file for copyright information and how to reach the author.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sectionfilter.h"
|
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
cIptvSectionFilter::cIptvSectionFilter(int DeviceIndex, int Index,
|
|
|
|
u_short Pid, u_char Tid, u_char Mask)
|
2007-09-24 19:20:58 +02:00
|
|
|
: pusi_seen(0),
|
|
|
|
feedcc(0),
|
|
|
|
doneq(0),
|
|
|
|
secbuf(NULL),
|
|
|
|
secbufp(0),
|
|
|
|
seclen(0),
|
|
|
|
tsfeedp(0),
|
|
|
|
pid(Pid),
|
2009-02-27 15:05:19 +01:00
|
|
|
devid(DeviceIndex),
|
2008-02-17 20:18:47 +01:00
|
|
|
id(Index),
|
|
|
|
pipeName("")
|
2007-09-24 15:03:38 +02:00
|
|
|
{
|
2009-02-27 15:05:19 +01:00
|
|
|
//debug("cIptvSectionFilter::cIptvSectionFilter(%d, %d)\n", devid, id);
|
|
|
|
int i;
|
|
|
|
|
2007-09-24 15:03:38 +02:00
|
|
|
memset(secbuf_base, '\0', sizeof(secbuf_base));
|
|
|
|
memset(filter_value, '\0', sizeof(filter_value));
|
|
|
|
memset(filter_mask, '\0', sizeof(filter_mask));
|
|
|
|
memset(filter_mode, '\0', sizeof(filter_mode));
|
|
|
|
memset(maskandmode, '\0', sizeof(maskandmode));
|
|
|
|
memset(maskandnotmode, '\0', sizeof(maskandnotmode));
|
|
|
|
|
2007-09-27 13:48:28 +02:00
|
|
|
filter_value[0] = Tid;
|
2007-09-24 15:03:38 +02:00
|
|
|
filter_mask[0] = Mask;
|
|
|
|
|
2007-09-27 13:48:28 +02:00
|
|
|
// Invert the filter
|
2009-02-27 15:05:19 +01:00
|
|
|
for (i = 0; i < DMX_MAX_FILTER_SIZE; ++i)
|
2007-09-27 13:48:28 +02:00
|
|
|
filter_value[i] ^= 0xff;
|
|
|
|
|
2007-09-24 15:03:38 +02:00
|
|
|
uint8_t mask, mode, local_doneq = 0;
|
2009-02-27 15:05:19 +01:00
|
|
|
for (i = 0; i < DMX_MAX_FILTER_SIZE; ++i) {
|
2008-01-30 22:57:33 +01:00
|
|
|
mode = filter_mode[i];
|
|
|
|
mask = filter_mask[i];
|
|
|
|
maskandmode[i] = mask & mode;
|
|
|
|
local_doneq |= maskandnotmode[i] = mask & ~mode;
|
|
|
|
}
|
2007-09-24 15:03:38 +02:00
|
|
|
doneq = local_doneq ? 1 : 0;
|
|
|
|
|
|
|
|
struct stat sb;
|
2009-02-27 15:05:19 +01:00
|
|
|
pipeName = cString::sprintf(IPTV_FILTER_FILENAME, devid, id);
|
2007-09-24 15:03:38 +02:00
|
|
|
stat(pipeName, &sb);
|
|
|
|
if (S_ISFIFO(sb.st_mode))
|
2007-09-27 00:07:45 +02:00
|
|
|
unlink(pipeName);
|
2009-02-27 15:05:19 +01:00
|
|
|
i = mknod(pipeName, 0644 | S_IFIFO, 0);
|
|
|
|
ERROR_IF_RET(i < 0, "mknod()", return);
|
2007-09-24 15:03:38 +02:00
|
|
|
|
|
|
|
// Create descriptors
|
|
|
|
fifoDescriptor = open(pipeName, O_RDWR | O_NONBLOCK);
|
|
|
|
readDescriptor = open(pipeName, O_RDONLY | O_NONBLOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
cIptvSectionFilter::~cIptvSectionFilter()
|
|
|
|
{
|
2009-02-27 15:05:19 +01:00
|
|
|
//debug("cIptvSectionFilter::~cIptvSectionfilter(%d, %d)\n", devid, id);
|
2007-09-24 15:03:38 +02:00
|
|
|
close(fifoDescriptor);
|
|
|
|
close(readDescriptor);
|
|
|
|
unlink(pipeName);
|
|
|
|
fifoDescriptor = -1;
|
|
|
|
readDescriptor = -1;
|
2009-03-20 16:43:31 +01:00
|
|
|
secbuf = NULL;
|
2007-09-24 15:03:38 +02:00
|
|
|
}
|
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
int cIptvSectionFilter::GetReadDesc(void)
|
2007-09-24 15:03:38 +02:00
|
|
|
{
|
|
|
|
return readDescriptor;
|
|
|
|
}
|
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
inline uint16_t cIptvSectionFilter::GetLength(const uint8_t *Data)
|
2007-09-24 15:03:38 +02:00
|
|
|
{
|
2009-02-27 15:05:19 +01:00
|
|
|
return 3 + ((Data[1] & 0x0f) << 8) + Data[2];
|
2007-09-24 15:03:38 +02:00
|
|
|
}
|
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
void cIptvSectionFilter::New(void)
|
2007-09-24 15:03:38 +02:00
|
|
|
{
|
|
|
|
tsfeedp = secbufp = seclen = 0;
|
|
|
|
secbuf = secbuf_base;
|
|
|
|
}
|
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
int cIptvSectionFilter::Filter(void)
|
2007-09-24 15:03:38 +02:00
|
|
|
{
|
|
|
|
uint8_t neq = 0;
|
|
|
|
int i;
|
|
|
|
|
2009-03-20 16:43:31 +01:00
|
|
|
if (secbuf) {
|
|
|
|
for (i = 0; i < DMX_MAX_FILTER_SIZE; ++i) {
|
|
|
|
uint8_t local_xor = filter_value[i] ^ secbuf[i];
|
|
|
|
if (maskandmode[i] & local_xor)
|
|
|
|
return 0;
|
|
|
|
neq |= maskandnotmode[i] & local_xor;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doneq && !neq)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// There is no data in the fifo, more can be written
|
|
|
|
if (!select_single_desc(fifoDescriptor, 0, false)) {
|
|
|
|
i = write(fifoDescriptor, secbuf, seclen);
|
|
|
|
ERROR_IF(i < 0, "write()");
|
|
|
|
// Update statistics
|
|
|
|
AddSectionStatistic(i, 1);
|
|
|
|
}
|
2007-09-24 19:20:58 +02:00
|
|
|
}
|
2009-02-27 15:05:19 +01:00
|
|
|
return 0;
|
2007-09-24 15:03:38 +02:00
|
|
|
}
|
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
inline int cIptvSectionFilter::Feed(void)
|
2007-09-24 15:03:38 +02:00
|
|
|
{
|
2009-02-27 15:05:19 +01:00
|
|
|
if (Filter() < 0)
|
2007-09-24 19:20:58 +02:00
|
|
|
return -1;
|
2007-09-24 15:03:38 +02:00
|
|
|
seclen = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
int cIptvSectionFilter::CopyDump(const uint8_t *buf, uint8_t len)
|
2007-09-24 15:03:38 +02:00
|
|
|
{
|
|
|
|
uint16_t limit, seclen_local, n;
|
|
|
|
|
2007-09-24 19:20:58 +02:00
|
|
|
if (tsfeedp >= DMX_MAX_SECFEED_SIZE)
|
|
|
|
return 0;
|
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
if (tsfeedp + len > DMX_MAX_SECFEED_SIZE)
|
2007-09-24 19:20:58 +02:00
|
|
|
len = DMX_MAX_SECFEED_SIZE - tsfeedp;
|
2007-09-24 15:03:38 +02:00
|
|
|
|
|
|
|
if (len <= 0)
|
2007-09-24 19:20:58 +02:00
|
|
|
return 0;
|
2007-09-24 15:03:38 +02:00
|
|
|
|
|
|
|
memcpy(secbuf_base + tsfeedp, buf, len);
|
|
|
|
tsfeedp += len;
|
|
|
|
|
|
|
|
limit = tsfeedp;
|
2007-09-24 19:20:58 +02:00
|
|
|
if (limit > DMX_MAX_SECFEED_SIZE)
|
2009-03-20 17:00:17 +01:00
|
|
|
return -1; // internal error should never happen
|
2007-09-24 15:03:38 +02:00
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
// Always set secbuf
|
2007-09-24 15:03:38 +02:00
|
|
|
secbuf = secbuf_base + secbufp;
|
|
|
|
|
2008-01-30 22:57:33 +01:00
|
|
|
for (n = 0; secbufp + 2 < limit; ++n) {
|
2009-02-27 15:05:19 +01:00
|
|
|
seclen_local = GetLength(secbuf);
|
2007-09-24 19:20:58 +02:00
|
|
|
if (seclen_local <= 0 || seclen_local > DMX_MAX_SECTION_SIZE ||
|
|
|
|
seclen_local + secbufp > limit)
|
|
|
|
return 0;
|
|
|
|
seclen = seclen_local;
|
|
|
|
if (pusi_seen)
|
2009-02-27 15:05:19 +01:00
|
|
|
Feed();
|
|
|
|
secbufp += seclen_local;
|
|
|
|
secbuf += seclen_local;
|
2007-09-24 19:20:58 +02:00
|
|
|
}
|
2007-09-24 15:03:38 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
void cIptvSectionFilter::Process(const uint8_t* Data)
|
2007-09-24 15:03:38 +02:00
|
|
|
{
|
2009-02-27 15:05:19 +01:00
|
|
|
if (Data[0] != TS_SYNC_BYTE)
|
2007-09-24 19:20:58 +02:00
|
|
|
return;
|
2009-02-24 12:40:16 +01:00
|
|
|
|
2007-09-24 15:03:38 +02:00
|
|
|
// Stop if not the PID this filter is looking for
|
2009-02-27 15:05:19 +01:00
|
|
|
if (ts_pid(Data) != pid)
|
2007-09-24 19:20:58 +02:00
|
|
|
return;
|
2007-09-24 15:03:38 +02:00
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
uint8_t count = payload(Data);
|
2007-09-24 15:03:38 +02:00
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
// Check if no payload or out of range
|
|
|
|
if (count == 0)
|
2007-09-24 19:20:58 +02:00
|
|
|
return;
|
2007-09-24 15:03:38 +02:00
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
// Payload start
|
|
|
|
uint8_t p = TS_SIZE - count;
|
2007-09-24 15:03:38 +02:00
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
uint8_t cc = Data[3] & 0x0f;
|
2007-09-24 15:03:38 +02:00
|
|
|
int ccok = ((feedcc + 1) & 0x0f) == cc;
|
2009-02-24 12:40:16 +01:00
|
|
|
feedcc = cc;
|
2007-09-24 15:03:38 +02:00
|
|
|
|
|
|
|
int dc_i = 0;
|
2009-02-27 15:05:19 +01:00
|
|
|
if (Data[3] & 0x20) {
|
|
|
|
// Adaption field present, check for discontinuity_indicator
|
|
|
|
if ((Data[4] > 0) && (Data[5] & 0x80))
|
2007-09-24 19:20:58 +02:00
|
|
|
dc_i = 1;
|
|
|
|
}
|
2007-09-24 15:03:38 +02:00
|
|
|
|
|
|
|
if (!ccok || dc_i) {
|
2009-02-27 15:05:19 +01:00
|
|
|
// Discontinuity detected. Reset pusi_seen = 0 to
|
|
|
|
// stop feeding of suspicious data until next PUSI=1 arrives
|
2007-09-24 19:20:58 +02:00
|
|
|
pusi_seen = 0;
|
2009-02-27 15:05:19 +01:00
|
|
|
New();
|
2007-09-24 19:20:58 +02:00
|
|
|
}
|
2007-09-24 15:03:38 +02:00
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
if (Data[1] & 0x40) {
|
|
|
|
// PUSI=1 (is set), section boundary is here
|
|
|
|
if (count > 1 && Data[p] < count) {
|
|
|
|
const uint8_t *before = &Data[p + 1];
|
|
|
|
uint8_t before_len = Data[p];
|
2007-09-24 19:20:58 +02:00
|
|
|
const uint8_t *after = &before[before_len];
|
2008-01-05 00:36:37 +01:00
|
|
|
uint8_t after_len = (count - 1) - before_len;
|
2009-02-27 15:05:19 +01:00
|
|
|
CopyDump(before, before_len);
|
2007-09-24 19:20:58 +02:00
|
|
|
|
2009-02-27 15:05:19 +01:00
|
|
|
// Before start of new section, set pusi_seen = 1
|
2007-09-24 19:20:58 +02:00
|
|
|
pusi_seen = 1;
|
2009-02-27 15:05:19 +01:00
|
|
|
New();
|
|
|
|
CopyDump(after, after_len);
|
2007-09-24 19:20:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2009-02-27 15:05:19 +01:00
|
|
|
// PUSI=0 (is not set), no section boundary
|
|
|
|
CopyDump(&Data[p], count);
|
2007-09-24 19:20:58 +02:00
|
|
|
}
|
2007-09-24 15:03:38 +02:00
|
|
|
}
|