2003-12-22 13:29:24 +01:00
|
|
|
/*
|
|
|
|
* sections.c: Section data handling
|
|
|
|
*
|
|
|
|
* See the main source file 'vdr.c' for copyright information and
|
|
|
|
* how to reach the author.
|
|
|
|
*
|
2021-06-08 15:10:51 +02:00
|
|
|
* $Id: sections.c 5.1 2021/06/08 15:10:51 kls Exp $
|
2003-12-22 13:29:24 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sections.h"
|
|
|
|
#include <unistd.h>
|
2004-01-11 15:54:37 +01:00
|
|
|
#include "channels.h"
|
2003-12-22 13:29:24 +01:00
|
|
|
#include "device.h"
|
2004-10-24 11:12:05 +02:00
|
|
|
#include "thread.h"
|
2003-12-22 13:29:24 +01:00
|
|
|
|
|
|
|
// --- cFilterHandle----------------------------------------------------------
|
|
|
|
|
|
|
|
class cFilterHandle : public cListObject {
|
|
|
|
public:
|
|
|
|
cFilterData filterData;
|
|
|
|
int handle;
|
|
|
|
int used;
|
|
|
|
cFilterHandle(const cFilterData &FilterData);
|
|
|
|
};
|
|
|
|
|
|
|
|
cFilterHandle::cFilterHandle(const cFilterData &FilterData)
|
|
|
|
{
|
|
|
|
filterData = FilterData;
|
|
|
|
handle = -1;
|
|
|
|
used = 0;
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:54:37 +01:00
|
|
|
// --- cSectionHandlerPrivate ------------------------------------------------
|
|
|
|
|
|
|
|
class cSectionHandlerPrivate {
|
|
|
|
public:
|
|
|
|
cChannel channel;
|
|
|
|
};
|
|
|
|
|
2003-12-22 13:29:24 +01:00
|
|
|
// --- cSectionHandler -------------------------------------------------------
|
|
|
|
|
|
|
|
cSectionHandler::cSectionHandler(cDevice *Device)
|
2015-01-14 11:39:55 +01:00
|
|
|
:cThread(NULL, true)
|
2003-12-22 13:29:24 +01:00
|
|
|
{
|
2004-01-11 15:54:37 +01:00
|
|
|
shp = new cSectionHandlerPrivate;
|
2003-12-22 13:29:24 +01:00
|
|
|
device = Device;
|
2019-05-28 15:54:22 +02:00
|
|
|
SetDescription("device %d section handler", device->DeviceNumber() + 1);
|
2003-12-22 13:29:24 +01:00
|
|
|
statusCount = 0;
|
|
|
|
on = false;
|
2004-08-08 14:12:43 +02:00
|
|
|
waitForLock = false;
|
2021-06-08 15:10:51 +02:00
|
|
|
flush = false;
|
|
|
|
startFilters = false;
|
2003-12-22 13:29:24 +01:00
|
|
|
Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
cSectionHandler::~cSectionHandler()
|
|
|
|
{
|
|
|
|
Cancel(3);
|
|
|
|
cFilter *fi;
|
|
|
|
while ((fi = filters.First()) != NULL)
|
|
|
|
Detach(fi);
|
2004-01-11 15:54:37 +01:00
|
|
|
delete shp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cSectionHandler::Source(void)
|
|
|
|
{
|
|
|
|
return shp->channel.Source();
|
|
|
|
}
|
|
|
|
|
|
|
|
int cSectionHandler::Transponder(void)
|
|
|
|
{
|
|
|
|
return shp->channel.Transponder();
|
|
|
|
}
|
|
|
|
|
|
|
|
const cChannel *cSectionHandler::Channel(void)
|
|
|
|
{
|
|
|
|
return &shp->channel;
|
2003-12-22 13:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void cSectionHandler::Add(const cFilterData *FilterData)
|
|
|
|
{
|
|
|
|
Lock();
|
|
|
|
statusCount++;
|
|
|
|
cFilterHandle *fh;
|
|
|
|
for (fh = filterHandles.First(); fh; fh = filterHandles.Next(fh)) {
|
|
|
|
if (fh->filterData.Is(FilterData->pid, FilterData->tid, FilterData->mask))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!fh) {
|
2004-07-17 14:22:42 +02:00
|
|
|
int handle = device->OpenFilter(FilterData->pid, FilterData->tid, FilterData->mask);
|
|
|
|
if (handle >= 0) {
|
|
|
|
fh = new cFilterHandle(*FilterData);
|
|
|
|
fh->handle = handle;
|
|
|
|
filterHandles.Add(fh);
|
|
|
|
}
|
2003-12-22 13:29:24 +01:00
|
|
|
}
|
2004-07-17 14:22:42 +02:00
|
|
|
if (fh)
|
|
|
|
fh->used++;
|
2003-12-22 13:29:24 +01:00
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cSectionHandler::Del(const cFilterData *FilterData)
|
|
|
|
{
|
|
|
|
Lock();
|
|
|
|
statusCount++;
|
|
|
|
cFilterHandle *fh;
|
|
|
|
for (fh = filterHandles.First(); fh; fh = filterHandles.Next(fh)) {
|
|
|
|
if (fh->filterData.Is(FilterData->pid, FilterData->tid, FilterData->mask)) {
|
|
|
|
if (--fh->used <= 0) {
|
2007-10-14 13:11:23 +02:00
|
|
|
device->CloseFilter(fh->handle);
|
2003-12-22 13:29:24 +01:00
|
|
|
filterHandles.Del(fh);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cSectionHandler::Attach(cFilter *Filter)
|
|
|
|
{
|
|
|
|
Lock();
|
|
|
|
statusCount++;
|
|
|
|
filters.Add(Filter);
|
|
|
|
Filter->sectionHandler = this;
|
2004-10-16 13:47:37 +02:00
|
|
|
if (on)
|
|
|
|
Filter->SetStatus(true);
|
2003-12-22 13:29:24 +01:00
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cSectionHandler::Detach(cFilter *Filter)
|
|
|
|
{
|
|
|
|
Lock();
|
|
|
|
statusCount++;
|
|
|
|
Filter->SetStatus(false);
|
|
|
|
Filter->sectionHandler = NULL;
|
|
|
|
filters.Del(Filter, false);
|
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:54:37 +01:00
|
|
|
void cSectionHandler::SetChannel(const cChannel *Channel)
|
2003-12-22 13:29:24 +01:00
|
|
|
{
|
2004-01-04 12:30:00 +01:00
|
|
|
Lock();
|
2004-02-07 15:51:57 +01:00
|
|
|
shp->channel = Channel ? *Channel : cChannel();
|
2004-01-04 12:30:00 +01:00
|
|
|
Unlock();
|
2003-12-22 13:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void cSectionHandler::SetStatus(bool On)
|
|
|
|
{
|
2004-01-04 12:30:00 +01:00
|
|
|
Lock();
|
2003-12-22 13:29:24 +01:00
|
|
|
if (on != On) {
|
2021-06-08 15:10:51 +02:00
|
|
|
if (!On || (device->HasLock() && startFilters)) {
|
2004-08-08 14:12:43 +02:00
|
|
|
statusCount++;
|
|
|
|
for (cFilter *fi = filters.First(); fi; fi = filters.Next(fi)) {
|
|
|
|
fi->SetStatus(false);
|
|
|
|
if (On)
|
|
|
|
fi->SetStatus(true);
|
|
|
|
}
|
2021-06-08 15:10:51 +02:00
|
|
|
if (flush = On)
|
|
|
|
flushTimer.Set();
|
2004-08-08 14:12:43 +02:00
|
|
|
on = On;
|
|
|
|
waitForLock = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
waitForLock = On;
|
2003-12-22 13:29:24 +01:00
|
|
|
}
|
2004-01-04 12:30:00 +01:00
|
|
|
Unlock();
|
2003-12-22 13:29:24 +01:00
|
|
|
}
|
|
|
|
|
2021-06-08 15:10:51 +02:00
|
|
|
#define FLUSH_TIME 100 // ms
|
|
|
|
|
2003-12-22 13:29:24 +01:00
|
|
|
void cSectionHandler::Action(void)
|
|
|
|
{
|
2005-08-14 11:24:57 +02:00
|
|
|
while (Running()) {
|
2003-12-22 13:29:24 +01:00
|
|
|
|
|
|
|
Lock();
|
2021-06-08 15:10:51 +02:00
|
|
|
if (waitForLock) {
|
|
|
|
startFilters = true;
|
2004-08-08 14:12:43 +02:00
|
|
|
SetStatus(true);
|
2021-06-08 15:10:51 +02:00
|
|
|
startFilters = false;
|
|
|
|
}
|
2003-12-22 13:29:24 +01:00
|
|
|
int NumFilters = filterHandles.Count();
|
|
|
|
pollfd pfd[NumFilters];
|
|
|
|
for (cFilterHandle *fh = filterHandles.First(); fh; fh = filterHandles.Next(fh)) {
|
|
|
|
int i = fh->Index();
|
|
|
|
pfd[i].fd = fh->handle;
|
|
|
|
pfd[i].events = POLLIN;
|
2004-10-16 13:47:37 +02:00
|
|
|
pfd[i].revents = 0;
|
2003-12-22 13:29:24 +01:00
|
|
|
}
|
|
|
|
int oldStatusCount = statusCount;
|
|
|
|
Unlock();
|
|
|
|
|
2021-06-08 15:10:51 +02:00
|
|
|
if (poll(pfd, NumFilters, waitForLock ? 100 : 1000) > 0) {
|
2003-12-22 13:29:24 +01:00
|
|
|
for (int i = 0; i < NumFilters; i++) {
|
|
|
|
if (pfd[i].revents & POLLIN) {
|
|
|
|
cFilterHandle *fh = NULL;
|
|
|
|
LOCK_THREAD;
|
|
|
|
if (statusCount != oldStatusCount)
|
|
|
|
break;
|
|
|
|
for (fh = filterHandles.First(); fh; fh = filterHandles.Next(fh)) {
|
|
|
|
if (pfd[i].fd == fh->handle)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fh) {
|
|
|
|
// Read section data:
|
|
|
|
unsigned char buf[4096]; // max. allowed size for any EIT section
|
2012-08-26 13:09:01 +02:00
|
|
|
int r = device->ReadFilter(fh->handle, buf, sizeof(buf));
|
2021-06-08 15:10:51 +02:00
|
|
|
if (flush)
|
2004-01-04 12:30:00 +01:00
|
|
|
continue; // we do the read anyway, to flush any data that might have come from a different transponder
|
2003-12-22 13:29:24 +01:00
|
|
|
if (r > 3) { // minimum number of bytes necessary to get section length
|
|
|
|
int len = (((buf[1] & 0x0F) << 8) | (buf[2] & 0xFF)) + 3;
|
|
|
|
if (len == r) {
|
|
|
|
// Distribute data to all attached filters:
|
|
|
|
int pid = fh->filterData.pid;
|
|
|
|
int tid = buf[0];
|
|
|
|
for (cFilter *fi = filters.First(); fi; fi = filters.Next(fi)) {
|
|
|
|
if (fi->Matches(pid, tid))
|
|
|
|
fi->Process(pid, tid, buf, len);
|
|
|
|
}
|
|
|
|
}
|
2020-11-24 21:19:49 +01:00
|
|
|
else
|
|
|
|
dsyslog("tp %d (%d/%02X) read incomplete section - len = %d, r = %d", Transponder(), fh->filterData.pid, buf[0], len, r);
|
2003-12-22 13:29:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-08 15:10:51 +02:00
|
|
|
if (flush)
|
|
|
|
flush = flushTimer.Elapsed() <= FLUSH_TIME;
|
2003-12-22 13:29:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|