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.
|
|
|
|
*
|
2007-10-21 01:25:14 +02:00
|
|
|
* $Id: sectionfilter.c,v 1.15 2007/10/20 23:25:14 ajhseppa Exp $
|
2007-09-24 15:03:38 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sectionfilter.h"
|
2007-10-05 21:00:44 +02:00
|
|
|
#include "statistics.h"
|
2007-09-24 15:03:38 +02:00
|
|
|
|
|
|
|
#define IPTV_FILTER_FILENAME "/tmp/vdr-iptv%d.filter%d"
|
|
|
|
|
|
|
|
cIptvSectionFilter::cIptvSectionFilter(int Index, int devInd,
|
|
|
|
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),
|
|
|
|
id(Index)
|
2007-09-24 15:03:38 +02:00
|
|
|
{
|
2007-09-24 23:25:53 +02:00
|
|
|
//debug("cIptvSectionFilter::cIptvSectionFilter(%d)\n", Index);
|
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));
|
|
|
|
memset(pipeName, '\0', sizeof(pipeName));
|
|
|
|
|
|
|
|
SetPipeName(devInd);
|
|
|
|
|
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
|
2007-10-05 23:52:16 +02:00
|
|
|
for (int 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;
|
2007-10-05 23:52:16 +02:00
|
|
|
for (int i = 0; i < DMX_MAX_FILTER_SIZE; i++) {
|
2007-09-24 15:03:38 +02:00
|
|
|
mode = filter_mode[i];
|
|
|
|
mask = filter_mask[i];
|
|
|
|
maskandmode[i] = mask & mode;
|
|
|
|
local_doneq |= maskandnotmode[i] = mask & ~mode;
|
|
|
|
}
|
|
|
|
doneq = local_doneq ? 1 : 0;
|
|
|
|
|
|
|
|
struct stat sb;
|
|
|
|
stat(pipeName, &sb);
|
|
|
|
if (S_ISFIFO(sb.st_mode))
|
2007-09-27 00:07:45 +02:00
|
|
|
unlink(pipeName);
|
2007-09-24 15:03:38 +02:00
|
|
|
int err = mknod(pipeName, 0644 | S_IFIFO, 0);
|
2007-10-21 01:25:14 +02:00
|
|
|
ERROR_IF_RET(err < 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()
|
|
|
|
{
|
2007-09-24 23:25:53 +02:00
|
|
|
//debug("cIptvSectionFilter::~cIptvSectionfilter(%d)\n", id);
|
2007-09-24 15:03:38 +02:00
|
|
|
close(fifoDescriptor);
|
|
|
|
close(readDescriptor);
|
|
|
|
unlink(pipeName);
|
|
|
|
memset(pipeName, '\0', sizeof(pipeName));
|
|
|
|
fifoDescriptor = -1;
|
|
|
|
readDescriptor = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cIptvSectionFilter::SetPipeName(int deviceIndex)
|
|
|
|
{
|
|
|
|
snprintf(pipeName, sizeof(pipeName), IPTV_FILTER_FILENAME, deviceIndex, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
int cIptvSectionFilter::GetReadDesc()
|
|
|
|
{
|
|
|
|
return readDescriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint16_t cIptvSectionFilter::section_length(const uint8_t *buf)
|
|
|
|
{
|
|
|
|
return 3 + ((buf[1] & 0x0f) << 8) + buf[2];
|
|
|
|
}
|
|
|
|
|
2007-10-05 23:52:16 +02:00
|
|
|
int cIptvSectionFilter::dmxdev_section_callback(const uint8_t *buffer1, size_t buffer1_len,
|
|
|
|
const uint8_t *buffer2, size_t buffer2_len,
|
|
|
|
enum dmx_success success)
|
2007-09-24 15:03:38 +02:00
|
|
|
{
|
2007-10-20 22:27:58 +02:00
|
|
|
// See if there is data in the fifo
|
2007-10-20 22:43:22 +02:00
|
|
|
int retval = select_single_desc(fifoDescriptor, 0, false);
|
2007-09-24 15:03:38 +02:00
|
|
|
// There is no data in the fifo, more can be written
|
2007-10-20 22:27:58 +02:00
|
|
|
if (!retval) {
|
2007-09-24 15:03:38 +02:00
|
|
|
#ifdef DEBUG_PRINTF
|
2007-09-24 19:20:58 +02:00
|
|
|
printf("id = %d, pid %d would now write %d data to buffer\n", id, pid, buffer1_len);
|
|
|
|
for (unsigned int i = 0; i < buffer1_len; ++i)
|
|
|
|
printf("0x%X ", buffer1[i]);
|
|
|
|
printf("\n");
|
2007-09-24 15:03:38 +02:00
|
|
|
#endif
|
2007-09-24 19:20:58 +02:00
|
|
|
retval = write(fifoDescriptor, buffer1, buffer1_len);
|
2007-10-21 01:25:14 +02:00
|
|
|
ERROR_IF(retval < 0, "write()");
|
2007-10-08 18:24:48 +02:00
|
|
|
// Update statistics
|
|
|
|
AddStatistic(retval, 1);
|
2007-09-24 19:20:58 +02:00
|
|
|
}
|
2007-09-24 15:03:38 +02:00
|
|
|
#ifdef DEBUG_PRINTF
|
2007-09-24 19:20:58 +02:00
|
|
|
else if (retval)
|
|
|
|
printf("id %d pid %d data is already present\n", id, pid);
|
2007-09-24 15:03:38 +02:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-10-05 23:52:16 +02:00
|
|
|
void cIptvSectionFilter::demux_swfilter_section_new()
|
2007-09-24 15:03:38 +02:00
|
|
|
{
|
2007-10-05 23:52:16 +02:00
|
|
|
#ifdef DEMUX_SECTION_LOSS_LOG
|
2007-09-24 15:03:38 +02:00
|
|
|
if (secbufp < tsfeedp) {
|
2007-09-24 19:20:58 +02:00
|
|
|
int i, n = tsfeedp - secbufp;
|
|
|
|
/*
|
|
|
|
* Section padding is done with 0xff bytes entirely.
|
|
|
|
* Due to speed reasons, we won't check all of them
|
|
|
|
* but just first and last.
|
|
|
|
*/
|
|
|
|
if (secbuf[0] != 0xff || secbuf[n - 1] != 0xff) {
|
2007-10-05 23:52:16 +02:00
|
|
|
printf("sectionfilter.c section ts padding loss: %d/%d\n",
|
2007-09-24 19:20:58 +02:00
|
|
|
n, tsfeedp);
|
2007-10-05 23:52:16 +02:00
|
|
|
printf("sectionfilter.c pad data:");
|
2007-09-24 19:20:58 +02:00
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
printf(" %02x", secbuf[i]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
2007-09-24 15:03:38 +02:00
|
|
|
#endif
|
|
|
|
tsfeedp = secbufp = seclen = 0;
|
|
|
|
secbuf = secbuf_base;
|
|
|
|
}
|
|
|
|
|
2007-10-05 23:52:16 +02:00
|
|
|
int cIptvSectionFilter::demux_swfilter_sectionfilter()
|
2007-09-24 15:03:38 +02:00
|
|
|
{
|
|
|
|
uint8_t neq = 0;
|
|
|
|
int i;
|
|
|
|
|
2007-10-05 23:52:16 +02:00
|
|
|
for (i = 0; i < DMX_MAX_FILTER_SIZE; i++) {
|
2007-09-24 19:20:58 +02:00
|
|
|
uint8_t local_xor = filter_value[i] ^ secbuf[i];
|
|
|
|
if (maskandmode[i] & local_xor) {
|
2007-09-24 15:03:38 +02:00
|
|
|
#ifdef DEBUG_PRINTF
|
2007-09-24 19:20:58 +02:00
|
|
|
printf("maskandmode discard\n");
|
2007-09-24 15:03:38 +02:00
|
|
|
#endif
|
2007-09-24 19:20:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
neq |= maskandnotmode[i] & local_xor;
|
|
|
|
}
|
2007-09-24 15:03:38 +02:00
|
|
|
|
|
|
|
if (doneq && !neq) {
|
|
|
|
#ifdef DEBUG_PRINTF
|
2007-09-24 19:20:58 +02:00
|
|
|
printf("doneq discard, doneq = 0x%X, neq = 0x%X\n", doneq, !neq);
|
2007-09-24 15:03:38 +02:00
|
|
|
#endif
|
2007-09-24 19:20:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2007-10-05 23:52:16 +02:00
|
|
|
return dmxdev_section_callback(secbuf, seclen, NULL, 0, DMX_OK);
|
2007-09-24 15:03:38 +02:00
|
|
|
}
|
|
|
|
|
2007-10-05 23:52:16 +02:00
|
|
|
inline int cIptvSectionFilter::demux_swfilter_section_feed()
|
2007-09-24 15:03:38 +02:00
|
|
|
{
|
2007-10-05 23:52:16 +02:00
|
|
|
if (demux_swfilter_sectionfilter() < 0)
|
2007-09-24 19:20:58 +02:00
|
|
|
return -1;
|
2007-09-24 15:03:38 +02:00
|
|
|
seclen = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-05 23:52:16 +02:00
|
|
|
int cIptvSectionFilter::demux_swfilter_section_copy_dump(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;
|
|
|
|
|
2007-09-24 15:03:38 +02:00
|
|
|
if (tsfeedp + len > DMX_MAX_SECFEED_SIZE) {
|
2007-10-05 23:52:16 +02:00
|
|
|
#ifdef DEMUX_SECTION_LOSS_LOG
|
|
|
|
printf("sectionfilter.c section buffer full loss: %d/%d\n",
|
2007-09-24 19:20:58 +02:00
|
|
|
tsfeedp + len - DMX_MAX_SECFEED_SIZE,
|
|
|
|
DMX_MAX_SECFEED_SIZE);
|
2007-09-24 15:03:38 +02:00
|
|
|
#endif
|
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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dump all the sections we can find in the data (Emard)
|
|
|
|
*/
|
|
|
|
limit = tsfeedp;
|
2007-09-24 19:20:58 +02:00
|
|
|
if (limit > DMX_MAX_SECFEED_SIZE)
|
|
|
|
return -1; /* internal error should never happen */
|
2007-09-24 15:03:38 +02:00
|
|
|
|
|
|
|
/* to be sure always set secbuf */
|
|
|
|
secbuf = secbuf_base + secbufp;
|
|
|
|
|
|
|
|
for (n = 0; secbufp + 2 < limit; n++) {
|
2007-09-24 19:20:58 +02:00
|
|
|
seclen_local = section_length(secbuf);
|
|
|
|
if (seclen_local <= 0 || seclen_local > DMX_MAX_SECTION_SIZE ||
|
|
|
|
seclen_local + secbufp > limit)
|
|
|
|
return 0;
|
2007-09-24 15:03:38 +02:00
|
|
|
#ifdef DEBUG_PRINTF
|
2007-09-24 19:20:58 +02:00
|
|
|
printf("Non-mismatching seclen! %d, limit = %d\n", seclen_local, limit);
|
2007-09-24 15:03:38 +02:00
|
|
|
#endif
|
2007-09-24 19:20:58 +02:00
|
|
|
seclen = seclen_local;
|
|
|
|
/* dump [secbuf .. secbuf+seclen) */
|
|
|
|
if (pusi_seen)
|
2007-10-05 23:52:16 +02:00
|
|
|
demux_swfilter_section_feed();
|
|
|
|
#ifdef DEMUX_SECTION_LOSS_LOG
|
2007-09-24 19:20:58 +02:00
|
|
|
else
|
2007-10-05 23:52:16 +02:00
|
|
|
printf("sectionfilter.c pusi not seen, discarding section data\n");
|
2007-09-24 15:03:38 +02:00
|
|
|
#endif
|
2007-09-24 19:20:58 +02:00
|
|
|
secbufp += seclen_local; /* secbufp and secbuf moving together is */
|
|
|
|
secbuf += seclen_local; /* redundant but saves pointer arithmetic */
|
|
|
|
}
|
2007-09-24 15:03:38 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cIptvSectionFilter::ProcessData(const uint8_t* buf)
|
|
|
|
{
|
|
|
|
if (buf[0] != 0x47) {
|
2007-09-24 19:20:58 +02:00
|
|
|
error("Not TS packet: 0x%X\n", buf[0]);
|
|
|
|
return;
|
|
|
|
}
|
2007-09-24 15:03:38 +02:00
|
|
|
|
|
|
|
// Stop if not the PID this filter is looking for
|
|
|
|
if (ts_pid(buf) != pid)
|
2007-09-24 19:20:58 +02:00
|
|
|
return;
|
2007-09-24 15:03:38 +02:00
|
|
|
|
|
|
|
uint8_t count = payload(buf);
|
|
|
|
|
|
|
|
if (count == 0) /* count == 0 if no payload or out of range */
|
2007-09-24 19:20:58 +02:00
|
|
|
return;
|
2007-09-24 15:03:38 +02:00
|
|
|
|
|
|
|
uint8_t p = 188 - count; /* payload start */
|
|
|
|
|
|
|
|
uint8_t cc = buf[3] & 0x0f;
|
|
|
|
int ccok = ((feedcc + 1) & 0x0f) == cc;
|
|
|
|
feedcc = cc;
|
|
|
|
|
|
|
|
int dc_i = 0;
|
|
|
|
if (buf[3] & 0x20) {
|
2007-09-24 19:20:58 +02:00
|
|
|
/* adaption field present, check for discontinuity_indicator */
|
|
|
|
if ((buf[4] > 0) && (buf[5] & 0x80))
|
|
|
|
dc_i = 1;
|
|
|
|
}
|
2007-09-24 15:03:38 +02:00
|
|
|
|
|
|
|
if (!ccok || dc_i) {
|
2007-10-05 23:52:16 +02:00
|
|
|
#ifdef DEMUX_SECTION_LOSS_LOG
|
|
|
|
printf("sectionfilter.c discontinuity detected %d bytes lost\n",
|
2007-09-24 19:20:58 +02:00
|
|
|
count);
|
|
|
|
/*
|
|
|
|
* those bytes under sume circumstances will again be reported
|
2007-10-05 23:52:16 +02:00
|
|
|
* in the following demux_swfilter_section_new
|
2007-09-24 19:20:58 +02:00
|
|
|
*/
|
2007-09-24 15:03:38 +02:00
|
|
|
#endif
|
2007-09-24 19:20:58 +02:00
|
|
|
/*
|
|
|
|
* Discontinuity detected. Reset pusi_seen = 0 to
|
|
|
|
* stop feeding of suspicious data until next PUSI=1 arrives
|
|
|
|
*/
|
|
|
|
pusi_seen = 0;
|
2007-10-05 23:52:16 +02:00
|
|
|
demux_swfilter_section_new();
|
2007-09-24 19:20:58 +02:00
|
|
|
}
|
2007-09-24 15:03:38 +02:00
|
|
|
|
|
|
|
if (buf[1] & 0x40) {
|
2007-09-24 19:20:58 +02:00
|
|
|
/* PUSI=1 (is set), section boundary is here */
|
|
|
|
if (count > 1 && buf[p] < count) {
|
2007-09-24 15:03:38 +02:00
|
|
|
#ifdef DEBUG_PRINTF
|
2007-09-24 19:20:58 +02:00
|
|
|
printf("Valid section\n");
|
2007-09-24 15:03:38 +02:00
|
|
|
#endif
|
2007-09-24 19:20:58 +02:00
|
|
|
const uint8_t *before = &buf[p + 1];
|
|
|
|
uint8_t before_len = buf[p];
|
|
|
|
const uint8_t *after = &before[before_len];
|
|
|
|
uint8_t after_len = count - 1 - before_len;
|
|
|
|
|
2007-10-05 23:52:16 +02:00
|
|
|
demux_swfilter_section_copy_dump(before, before_len);
|
2007-09-24 19:20:58 +02:00
|
|
|
/* before start of new section, set pusi_seen = 1 */
|
|
|
|
pusi_seen = 1;
|
2007-10-05 23:52:16 +02:00
|
|
|
demux_swfilter_section_new();
|
|
|
|
demux_swfilter_section_copy_dump(after, after_len);
|
2007-09-24 19:20:58 +02:00
|
|
|
}
|
2007-10-05 23:52:16 +02:00
|
|
|
#ifdef DEMUX_SECTION_LOSS_LOG
|
2007-09-24 19:20:58 +02:00
|
|
|
else if (count > 0)
|
2007-10-05 23:52:16 +02:00
|
|
|
printf("sectionfilter.c PUSI=1 but %d bytes lost\n", count);
|
2007-09-24 15:03:38 +02:00
|
|
|
#endif
|
2007-09-24 19:20:58 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* PUSI=0 (is not set), no section boundary */
|
2007-10-05 23:52:16 +02:00
|
|
|
demux_swfilter_section_copy_dump(&buf[p], count);
|
2007-09-24 19:20:58 +02:00
|
|
|
}
|
2007-09-24 15:03:38 +02:00
|
|
|
}
|