1
0
mirror of https://github.com/rofafor/vdr-plugin-iptv.git synced 2023-10-10 13:37:03 +02:00
vdr-plugin-iptv/device.c

453 lines
14 KiB
C
Raw Normal View History

2007-09-12 19:28:59 +02:00
/*
* device.c: IPTV plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
2007-09-15 17:38:38 +02:00
#include "config.h"
2007-09-12 19:28:59 +02:00
#include "device.h"
#define IPTV_MAX_DEVICES 8
cIptvDevice * IptvDevices[IPTV_MAX_DEVICES] = { NULL };
2007-09-12 19:28:59 +02:00
unsigned int cIptvDevice::deviceCount = 0;
cIptvDevice::cIptvDevice(unsigned int Index)
: deviceIndex(Index),
dvrFd(-1),
2007-09-12 19:28:59 +02:00
isPacketDelivered(false),
isOpenDvr(false),
sidScanEnabled(false),
pidScanEnabled(false),
2007-09-14 17:44:25 +02:00
mutex()
2007-09-12 19:28:59 +02:00
{
//debug("cIptvDevice::cIptvDevice(%d)\n", deviceIndex);
isyslog("creating IPTV device %d (CardIndex=%d)", deviceIndex, CardIndex());
2007-09-16 15:38:20 +02:00
tsBuffer = new cRingBufferLinear(MEGABYTE(IptvConfig.GetTsBufferSize()),
(TS_SIZE * IptvConfig.GetReadBufferTsCount()),
2007-09-22 12:36:30 +02:00
false, "IPTV");
2007-09-15 19:15:37 +02:00
tsBuffer->SetTimeouts(100, 100);
2007-09-21 23:50:52 +02:00
ResetBuffering();
2007-09-14 17:44:25 +02:00
pUdpProtocol = new cIptvProtocolUdp();
2007-09-16 11:38:00 +02:00
pHttpProtocol = new cIptvProtocolHttp();
2007-09-16 14:18:15 +02:00
pFileProtocol = new cIptvProtocolFile();
pExtProtocol = new cIptvProtocolExt();
2007-09-12 19:28:59 +02:00
pIptvStreamer = new cIptvStreamer(tsBuffer, &mutex);
pPidScanner = new cPidScanner;
// Initialize filter pointers
2008-01-05 00:36:37 +01:00
memset(secfilters, '\0', sizeof(secfilters));
2007-10-01 20:14:57 +02:00
// Start section handler for iptv device
2007-09-20 23:15:08 +02:00
StartSectionHandler();
2007-10-01 20:14:57 +02:00
// Sid scanner must be created after the section handler
pSidScanner = new cSidScanner;
if (pSidScanner)
AttachFilter(pSidScanner);
// Check if dvr fifo exists
struct stat sb;
cString filename = cString::sprintf(IPTV_DVR_FILENAME, deviceIndex);
stat(filename, &sb);
if (S_ISFIFO(sb.st_mode)) {
dvrFd = open(filename, O_RDWR | O_NONBLOCK);
if (dvrFd >= 0)
dsyslog("IPTV device %d redirecting input stream to '%s'", deviceIndex, *filename);
}
2007-09-12 19:28:59 +02:00
}
cIptvDevice::~cIptvDevice()
{
debug("cIptvDevice::~cIptvDevice(%d)\n", deviceIndex);
// Stop section handler of iptv device
StopSectionHandler();
DELETE_POINTER(pIptvStreamer);
DELETE_POINTER(pUdpProtocol);
DELETE_POINTER(pHttpProtocol);
DELETE_POINTER(pFileProtocol);
DELETE_POINTER(pExtProtocol);
DELETE_POINTER(tsBuffer);
DELETE_POINTER(pPidScanner);
2007-09-29 01:23:12 +02:00
// Detach and destroy sid filter
2007-10-01 20:14:57 +02:00
if (pSidScanner) {
Detach(pSidScanner);
DELETE_POINTER(pSidScanner);
2007-09-29 01:23:12 +02:00
}
// Destroy all filters
2007-09-24 19:20:58 +02:00
for (int i = 0; i < eMaxSecFilterCount; ++i)
DeleteFilter(i);
// Close dvr fifo
if (dvrFd >= 0) {
int fd = dvrFd;
dvrFd = -1;
close(fd);
}
2007-09-12 19:28:59 +02:00
}
bool cIptvDevice::Initialize(unsigned int DeviceCount)
{
debug("cIptvDevice::Initialize(): DeviceCount=%d\n", DeviceCount);
2007-09-12 19:28:59 +02:00
if (DeviceCount > IPTV_MAX_DEVICES)
DeviceCount = IPTV_MAX_DEVICES;
for (unsigned int i = 0; i < DeviceCount; ++i)
IptvDevices[i] = new cIptvDevice(i);
for (unsigned int i = DeviceCount; i < IPTV_MAX_DEVICES; ++i)
IptvDevices[i] = NULL;
2007-09-12 19:28:59 +02:00
return true;
}
unsigned int cIptvDevice::Count(void)
{
unsigned int count = 0;
debug("cIptvDevice::Count()\n");
for (unsigned int i = 0; i < IPTV_MAX_DEVICES; ++i) {
if (IptvDevices[i] != NULL)
2007-09-12 19:28:59 +02:00
count++;
}
return count;
}
cIptvDevice *cIptvDevice::GetIptvDevice(int CardIndex)
2007-09-12 19:28:59 +02:00
{
//debug("cIptvDevice::GetIptvDevice(%d)\n", CardIndex);
for (unsigned int i = 0; i < IPTV_MAX_DEVICES; ++i) {
if ((IptvDevices[i] != NULL) && (IptvDevices[i]->CardIndex() == CardIndex)) {
//debug("cIptvDevice::GetIptvDevice(%d): FOUND!\n", CardIndex);
return IptvDevices[i];
}
}
2007-09-12 19:28:59 +02:00
return NULL;
}
cString cIptvDevice::GetGeneralInformation(void)
{
//debug("cIptvDevice::GetGeneralInformation(%d)\n", deviceIndex);
return cString::sprintf("IPTV device: %d\nCardIndex: %d\n%s%s%sChannel: %s",
deviceIndex, CardIndex(),
pIptvStreamer ? *pIptvStreamer->GetInformation() : "",
pIptvStreamer ? *pIptvStreamer->GetStreamerStatistic() : "",
*GetBufferStatistic(),
*Channels.GetByNumber(cDevice::CurrentChannel())->ToText());
}
cString cIptvDevice::GetPidsInformation(void)
{
//debug("cIptvDevice::GetPidsInformation(%d)\n", deviceIndex);
return GetPidStatistic();
}
cString cIptvDevice::GetFiltersInformation(void)
{
//debug("cIptvDevice::GetFiltersInformation(%d)\n", deviceIndex);
unsigned int count = 0;
cString info("Active section filters:\n");
2007-10-08 00:54:09 +02:00
// loop through active section filters
for (unsigned int i = 0; i < eMaxSecFilterCount; ++i) {
if (secfilters[i]) {
info = cString::sprintf("%sFilter %d: %s Pid=0x%02X (%s)\n", *info, i,
*secfilters[i]->GetSectionStatistic(), secfilters[i]->GetPid(),
id_pid(secfilters[i]->GetPid()));
if (++count > IPTV_STATS_ACTIVE_FILTERS_COUNT)
break;
}
2007-10-08 00:54:09 +02:00
}
return info;
}
cString cIptvDevice::GetInformation(unsigned int Page)
{
2007-10-08 00:54:09 +02:00
// generate information string
cString info;
switch (Page) {
case IPTV_DEVICE_INFO_GENERAL:
info = GetGeneralInformation();
break;
case IPTV_DEVICE_INFO_PIDS:
info = GetPidsInformation();
break;
case IPTV_DEVICE_INFO_FILTERS:
info = GetFiltersInformation();
break;
default:
2007-10-09 18:37:16 +02:00
info = cString::sprintf("%s%s%s",
*GetGeneralInformation(),
*GetPidsInformation(),
2007-10-09 18:37:16 +02:00
*GetFiltersInformation());
break;
}
return info;
}
cString cIptvDevice::GetChannelSettings(const char *IptvParam, int *Parameter, int *SidScan, int *PidScan, cIptvProtocolIf* *Protocol)
2007-09-12 19:28:59 +02:00
{
debug("cIptvDevice::GetChannelSettings(%d)\n", deviceIndex);
char *tag = NULL;
char *proto = NULL;
2007-09-16 15:11:19 +02:00
char *loc = NULL;
if (sscanf(IptvParam, "%a[^|]|S%dP%d|%a[^|]|%a[^|]|%u", &tag, SidScan, PidScan, &proto, &loc, Parameter) == 6) {
cString tagstr(tag, true);
cString protostr(proto, true);
cString locstr(loc, true);
// check if IPTV tag
if (strncasecmp(*tagstr, "IPTV", 4) == 0) {
// check if protocol is supported and update the pointer
if (strncasecmp(*protostr, "UDP", 3) == 0)
*Protocol = pUdpProtocol;
else if (strncasecmp(*protostr, "HTTP", 4) == 0)
*Protocol = pHttpProtocol;
else if (strncasecmp(*protostr, "FILE", 4) == 0)
*Protocol = pFileProtocol;
else if (strncasecmp(*protostr, "EXT", 3) == 0)
*Protocol = pExtProtocol;
else
return NULL;
// return location
return locstr;
}
}
else if (sscanf(IptvParam, "%a[^|]|P%dS%d|%a[^|]|%a[^|]|%u", &tag, PidScan, SidScan, &proto, &loc, Parameter) == 6) {
cString tagstr(tag, true);
cString protostr(proto, true);
cString locstr(loc, true);
// check if IPTV tag
if (strncasecmp(*tagstr, "IPTV", 4) == 0) {
// check if protocol is supported and update the pointer
if (strncasecmp(*protostr, "UDP", 3) == 0)
*Protocol = pUdpProtocol;
else if (strncasecmp(*protostr, "HTTP", 4) == 0)
*Protocol = pHttpProtocol;
else if (strncasecmp(*protostr, "FILE", 4) == 0)
*Protocol = pFileProtocol;
else if (strncasecmp(*protostr, "EXT", 3) == 0)
*Protocol = pExtProtocol;
else
return NULL;
// return location
return locstr;
}
}
2007-09-12 19:28:59 +02:00
return NULL;
}
bool cIptvDevice::ProvidesIptv(const char *Param) const
{
debug("cIptvDevice::ProvidesIptv(%d)\n", deviceIndex);
return (strncasecmp(Param, "IPTV", 4) == 0);
2007-09-12 19:28:59 +02:00
}
bool cIptvDevice::ProvidesSource(int Source) const
{
debug("cIptvDevice::ProvidesSource(%d)\n", deviceIndex);
return (cSource::IsPlug(Source));
2007-09-12 19:28:59 +02:00
}
bool cIptvDevice::ProvidesTransponder(const cChannel *Channel) const
{
debug("cIptvDevice::ProvidesTransponder(%d)\n", deviceIndex);
return (ProvidesSource(Channel->Source()) && ProvidesIptv(Channel->PluginParam()));
2007-09-12 19:28:59 +02:00
}
bool cIptvDevice::ProvidesChannel(const cChannel *Channel, int Priority, bool *NeedsDetachReceivers) const
{
bool result = false;
bool needsDetachReceivers = false;
debug("cIptvDevice::ProvidesChannel(%d)\n", deviceIndex);
2007-09-12 19:28:59 +02:00
if (ProvidesTransponder(Channel))
result = true;
if (NeedsDetachReceivers)
*NeedsDetachReceivers = needsDetachReceivers;
return result;
}
2008-01-28 22:36:32 +01:00
int cIptvDevice::NumProvidedSystems(void) const
{
return 1;
}
2007-09-12 19:28:59 +02:00
bool cIptvDevice::SetChannelDevice(const cChannel *Channel, bool LiveView)
{
int parameter, sidscan, pidscan;
2007-10-19 23:36:27 +02:00
cString location;
2007-09-14 17:44:25 +02:00
cIptvProtocolIf *protocol;
2007-09-12 19:28:59 +02:00
debug("cIptvDevice::SetChannelDevice(%d)\n", deviceIndex);
location = GetChannelSettings(Channel->PluginParam(), &parameter, &sidscan, &pidscan, &protocol);
2007-10-19 23:36:27 +02:00
if (isempty(location)) {
error("ERROR: Unrecognized IPTV channel settings: %s", Channel->PluginParam());
return false;
}
sidScanEnabled = sidscan ? true : false;
pidScanEnabled = pidscan ? true : false;
if (pIptvStreamer->Set(location, parameter, deviceIndex, protocol)) {
if (sidScanEnabled && pSidScanner && IptvConfig.GetSectionFiltering())
pSidScanner->SetChannel(Channel);
if (pidScanEnabled && pPidScanner)
pPidScanner->SetChannel(Channel);
}
2007-09-12 19:28:59 +02:00
return true;
}
bool cIptvDevice::SetPid(cPidHandle *Handle, int Type, bool On)
{
2007-09-19 20:02:38 +02:00
debug("cIptvDevice::SetPid(%d) Pid=%d Type=%d On=%d\n", deviceIndex, Handle->pid, Type, On);
2007-09-12 19:28:59 +02:00
return true;
}
bool cIptvDevice::DeleteFilter(unsigned int Index)
{
2007-09-24 19:20:58 +02:00
if ((Index < eMaxSecFilterCount) && secfilters[Index]) {
//debug("cIptvDevice::DeleteFilter(%d) Index=%d\n", deviceIndex, Index);
cIptvSectionFilter *tmp = secfilters[Index];
secfilters[Index] = NULL;
delete tmp;
return true;
}
return false;
}
bool cIptvDevice::IsBlackListed(u_short Pid, u_char Tid, u_char Mask)
{
//debug("cIptvDevice::IsBlackListed(%d) Pid=%d Tid=%02X Mask=%02X\n", deviceIndex, Pid, Tid, Mask);
// loop through section filter table
for (int i = 0; i < SECTION_FILTER_TABLE_SIZE; ++i) {
int index = IptvConfig.GetDisabledFilters(i);
// check if matches
if ((index >= 0) && (index < SECTION_FILTER_TABLE_SIZE) &&
2007-10-07 00:25:49 +02:00
(section_filter_table[index].pid == Pid) && (section_filter_table[index].tid == Tid) &&
(section_filter_table[index].mask == Mask)) {
//debug("cIptvDevice::IsBlackListed(%d) Found=%s\n", deviceIndex, section_filter_table[index].description);
return true;
}
}
return false;
}
int cIptvDevice::OpenFilter(u_short Pid, u_char Tid, u_char Mask)
{
// Check if disabled by user
if (!IptvConfig.GetSectionFiltering())
return -1;
// Blacklist check, refuse certain filters
if (IsBlackListed(Pid, Tid, Mask))
return -1;
// Search the next free filter slot
2007-09-24 19:20:58 +02:00
for (unsigned int i = 0; i < eMaxSecFilterCount; ++i) {
if (!secfilters[i]) {
//debug("cIptvDevice::OpenFilter(%d): Pid=%d Tid=%02X Mask=%02X Index=%d\n", deviceIndex, Pid, Tid, Mask, i);
2007-09-24 19:20:58 +02:00
secfilters[i] = new cIptvSectionFilter(i, deviceIndex, Pid, Tid, Mask);
return secfilters[i]->GetReadDesc();
2007-09-20 23:15:08 +02:00
}
}
// No free filter slot found
return -1;
}
2007-10-14 20:45:34 +02:00
void cIptvDevice::CloseFilter(int Handle)
{
2007-09-24 19:20:58 +02:00
for (unsigned int i = 0; i < eMaxSecFilterCount; ++i) {
if (secfilters[i] && (Handle == secfilters[i]->GetReadDesc())) {
//debug("cIptvDevice::CloseFilter(%d): %d\n", deviceIndex, Handle);
2007-10-14 20:45:34 +02:00
DeleteFilter(i);
break;
}
}
}
2007-09-12 19:28:59 +02:00
bool cIptvDevice::OpenDvr(void)
{
debug("cIptvDevice::OpenDvr(%d)\n", deviceIndex);
2007-09-12 19:28:59 +02:00
mutex.Lock();
isPacketDelivered = false;
tsBuffer->Clear();
mutex.Unlock();
2007-09-21 23:50:52 +02:00
ResetBuffering();
2007-09-14 17:44:25 +02:00
pIptvStreamer->Open();
if (sidScanEnabled && pSidScanner && IptvConfig.GetSectionFiltering())
2007-10-01 20:14:57 +02:00
pSidScanner->SetStatus(true);
2007-09-12 19:28:59 +02:00
isOpenDvr = true;
return true;
}
void cIptvDevice::CloseDvr(void)
{
debug("cIptvDevice::CloseDvr(%d)\n", deviceIndex);
if (sidScanEnabled && pSidScanner && IptvConfig.GetSectionFiltering())
2007-10-01 20:14:57 +02:00
pSidScanner->SetStatus(false);
2008-01-05 00:36:37 +01:00
if (pIptvStreamer)
2008-01-30 22:57:33 +01:00
pIptvStreamer->Close();
2007-09-12 19:28:59 +02:00
isOpenDvr = false;
}
bool cIptvDevice::HasLock(int TimeoutMs)
{
2007-09-21 23:50:52 +02:00
//debug("cIptvDevice::HasLock(%d): %d\n", deviceIndex, TimeoutMs);
return (!IsBuffering());
}
void cIptvDevice::ResetBuffering(void)
{
debug("cIptvDevice::ResetBuffering(%d)\n", deviceIndex);
// pad prefill to multiple of TS_SIZE
tsBufferPrefill = MEGABYTE(IptvConfig.GetTsBufferSize()) *
IptvConfig.GetTsBufferPrefillRatio() / 100;
tsBufferPrefill -= (tsBufferPrefill % TS_SIZE);
}
bool cIptvDevice::IsBuffering(void)
{
//debug("cIptvDevice::IsBuffering(%d): %d\n", deviceIndex);
if (tsBufferPrefill && tsBuffer->Available() < tsBufferPrefill)
2007-09-21 23:50:52 +02:00
return true;
else
tsBufferPrefill = 0;
return false;
}
2007-09-12 19:28:59 +02:00
bool cIptvDevice::GetTSPacket(uchar *&Data)
{
int Count = 0;
//debug("cIptvDevice::GetTSPacket(%d)\n", deviceIndex);
2007-09-21 23:50:52 +02:00
if (!IsBuffering()) {
if (isPacketDelivered) {
tsBuffer->Del(TS_SIZE);
isPacketDelivered = false;
2007-10-10 00:12:17 +02:00
// Update buffer statistics
AddBufferStatistic(TS_SIZE, tsBuffer->Available());
2007-09-12 19:28:59 +02:00
}
2007-09-21 23:50:52 +02:00
uchar *p = tsBuffer->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;
2007-09-19 20:02:38 +02:00
}
2007-09-21 23:50:52 +02:00
}
tsBuffer->Del(Count);
error("ERROR: skipped %d bytes to sync on TS packet\n", Count);
return false;
}
isPacketDelivered = true;
Data = p;
// Update pid statistics
AddPidStatistic(ts_pid(p), payload(p));
// Send data also to dvr fifo
if ((dvrFd >= 0) && (write(dvrFd, p, TS_SIZE) != TS_SIZE))
error("ERROR: write failed to FIFO\n");
// Analyze incomplete streams with built-in pid analyzer
if (pidScanEnabled && pPidScanner)
pPidScanner->Process(p);
// Run the data through all filters
2007-09-24 19:20:58 +02:00
for (unsigned int i = 0; i < eMaxSecFilterCount; ++i) {
if (secfilters[i])
secfilters[i]->ProcessData(p);
2007-09-19 20:02:38 +02:00
}
2007-09-21 23:50:52 +02:00
return true;
}
2007-09-12 19:28:59 +02:00
}
2007-10-27 01:48:30 +02:00
// Reduce cpu load by preventing busylooping
cCondWait::SleepMs(100);
2007-09-12 19:28:59 +02:00
Data = NULL;
return true;
}