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

443 lines
13 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"
2010-03-04 16:34:21 +01:00
#include "source.h"
2007-09-12 19:28:59 +02:00
#include "device.h"
#define IPTV_MAX_DEVICES MAXDEVICES
2007-09-12 19:28:59 +02:00
2013-02-23 14:31:11 +01:00
static cIptvDevice * IptvDevicesS[IPTV_MAX_DEVICES] = { NULL };
2007-09-12 19:28:59 +02:00
2013-02-23 14:31:11 +01:00
cIptvDevice::cIptvDevice(unsigned int indexP)
: deviceIndexM(indexP),
dvrFdM(-1),
isPacketDeliveredM(false),
isOpenDvrM(false),
sidScanEnabledM(false),
pidScanEnabledM(false),
channelM()
2007-09-12 19:28:59 +02:00
{
2010-03-04 16:34:21 +01:00
unsigned int bufsize = (unsigned int)MEGABYTE(IptvConfig.GetTsBufferSize());
2009-02-26 15:04:12 +01:00
bufsize -= (bufsize % TS_SIZE);
2013-02-23 14:31:11 +01:00
isyslog("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());
}
2007-09-21 23:50:52 +02:00
ResetBuffering();
2013-02-23 14:31:11 +01:00
pUdpProtocolM = new cIptvProtocolUdp();
pCurlProtocolM = new cIptvProtocolCurl();
pHttpProtocolM = new cIptvProtocolHttp();
pFileProtocolM = new cIptvProtocolFile();
pExtProtocolM = new cIptvProtocolExt();
pPidScannerM = new cPidScanner();
2007-10-01 20:14:57 +02:00
// Start section handler for iptv device
pIptvSectionM = new cIptvSectionFilterHandler(deviceIndexM, bufsize + 1);
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
2013-02-25 23:34:45 +01:00
AttachFilter(pSidScannerM = new cSidScanner());
// Check if dvr fifo exists
struct stat sb;
2013-02-23 14:31:11 +01:00
cString filename = cString::sprintf(IPTV_DVR_FILENAME, deviceIndexM);
stat(filename, &sb);
if (S_ISFIFO(sb.st_mode)) {
2013-02-23 14:31:11 +01:00
dvrFdM = open(filename, O_RDWR | O_NONBLOCK);
if (dvrFdM >= 0)
dsyslog("IPTV device %d redirecting input stream to '%s'", deviceIndexM, *filename);
}
2007-09-12 19:28:59 +02:00
}
cIptvDevice::~cIptvDevice()
{
2013-02-23 16:12:46 +01:00
debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
2013-02-25 23:34:45 +01:00
// 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
2013-02-23 14:31:11 +01:00
if (dvrFdM >= 0) {
int fd = dvrFdM;
dvrFdM = -1;
close(fd);
}
2007-09-12 19:28:59 +02:00
}
2013-02-23 14:31:11 +01:00
bool cIptvDevice::Initialize(unsigned int deviceCountP)
2007-09-12 19:28:59 +02:00
{
2013-02-23 16:12:46 +01:00
debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceCountP);
2010-03-04 16:34:21 +01:00
new cIptvSourceParam(IPTV_SOURCE_CHARACTER, "IPTV");
2013-02-23 14:31:11 +01:00
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;
2007-09-12 19:28:59 +02:00
return true;
}
void cIptvDevice::Shutdown(void)
{
debug("cIptvDevice::%s()", __FUNCTION__);
for (int i = 0; i < IPTV_MAX_DEVICES; ++i) {
if (IptvDevicesS[i])
IptvDevicesS[i]->CloseDvr();
}
}
2007-09-12 19:28:59 +02:00
unsigned int cIptvDevice::Count(void)
{
unsigned int count = 0;
2013-02-23 16:12:46 +01:00
debug("cIptvDevice::%s()", __FUNCTION__);
2007-09-12 19:28:59 +02:00
for (unsigned int i = 0; i < IPTV_MAX_DEVICES; ++i) {
2013-02-23 14:31:11 +01:00
if (IptvDevicesS[i] != NULL)
2007-09-12 19:28:59 +02:00
count++;
}
return count;
}
2013-02-23 14:31:11 +01:00
cIptvDevice *cIptvDevice::GetIptvDevice(int cardIndexP)
2007-09-12 19:28:59 +02:00
{
2013-02-23 16:12:46 +01:00
//debug("cIptvDevice::%s(%d)", __FUNCTION__, cardIndexP);
for (unsigned int i = 0; i < IPTV_MAX_DEVICES; ++i) {
if (IptvDevicesS[i] && (IptvDevicesS[i]->CardIndex() == cardIndexP)) {
2013-02-23 16:12:46 +01:00
//debug("cIptvDevice::%s(%d): found!", __FUNCTION__, cardIndexP);
2013-02-23 14:31:11 +01:00
return IptvDevicesS[i];
}
}
2007-09-12 19:28:59 +02:00
return NULL;
}
cString cIptvDevice::GetGeneralInformation(void)
{
2013-02-23 16:12:46 +01:00
//debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
2012-04-01 21:46:08 +02:00
return cString::sprintf("IPTV device: %d\nCardIndex: %d\nStream: %s\nStream bitrate: %s\n%sChannel: %s",
2013-02-23 14:31:11 +01:00
deviceIndexM, CardIndex(),
pIptvStreamerM ? *pIptvStreamerM->GetInformation() : "",
pIptvStreamerM ? *pIptvStreamerM->GetStreamerStatistic() : "",
*GetBufferStatistic(),
*Channels.GetByNumber(cDevice::CurrentChannel())->ToText());
}
cString cIptvDevice::GetPidsInformation(void)
{
2013-02-23 16:12:46 +01:00
//debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
return GetPidStatistic();
}
cString cIptvDevice::GetFiltersInformation(void)
{
2013-02-23 16:12:46 +01:00
//debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
return cString::sprintf("Active section filters:\n%s", pIptvSectionM ? *pIptvSectionM->GetInformation() : "");
}
2013-02-23 14:31:11 +01:00
cString cIptvDevice::GetInformation(unsigned int pageP)
{
2007-10-08 00:54:09 +02:00
// generate information string
cString s;
2013-02-23 14:31:11 +01:00
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;
2012-04-01 21:46:08 +02:00
case IPTV_DEVICE_INFO_PROTOCOL:
2013-02-23 14:31:11 +01:00
s = pIptvStreamerM ? *pIptvStreamerM->GetInformation() : "";
2012-04-01 21:46:08 +02:00
break;
case IPTV_DEVICE_INFO_BITRATE:
2013-02-23 14:31:11 +01:00
s = pIptvStreamerM ? *pIptvStreamerM->GetStreamerStatistic() : "";
2012-04-01 21:46:08 +02:00
break;
default:
s = cString::sprintf("%s%s%s",
*GetGeneralInformation(),
*GetPidsInformation(),
*GetFiltersInformation());
break;
}
return s;
}
cString cIptvDevice::DeviceType(void) const
{
//debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
return "IPTV";
}
2012-03-25 15:42:06 +02:00
cString cIptvDevice::DeviceName(void) const
{
2013-02-23 16:12:46 +01:00
debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
2013-02-23 14:31:11 +01:00
return cString::sprintf("IPTV %d", deviceIndexM);
2012-03-25 15:42:06 +02:00
}
2011-06-19 21:07:01 +02:00
int cIptvDevice::SignalStrength(void) const
{
2013-02-23 16:12:46 +01:00
debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
2011-06-19 21:07:01 +02:00
return (100);
}
int cIptvDevice::SignalQuality(void) const
{
2013-02-23 16:12:46 +01:00
debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
2011-06-19 21:07:01 +02:00
return (100);
}
2013-02-23 14:31:11 +01:00
bool cIptvDevice::ProvidesSource(int sourceP) const
2007-09-12 19:28:59 +02:00
{
2013-02-23 16:12:46 +01:00
debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
2013-02-23 14:31:11 +01:00
return (cSource::IsType(sourceP, IPTV_SOURCE_CHARACTER));
2007-09-12 19:28:59 +02:00
}
2013-02-23 14:31:11 +01:00
bool cIptvDevice::ProvidesTransponder(const cChannel *channelP) const
2007-09-12 19:28:59 +02:00
{
2013-02-23 16:12:46 +01:00
debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
2013-02-23 14:31:11 +01:00
return (ProvidesSource(channelP->Source()));
2007-09-12 19:28:59 +02:00
}
2013-02-23 14:31:11 +01:00
bool cIptvDevice::ProvidesChannel(const cChannel *channelP, int priorityP, bool *needsDetachReceiversP) const
2007-09-12 19:28:59 +02:00
{
bool result = false;
2013-02-23 14:31:11 +01:00
bool hasPriority = (priorityP == IDLEPRIORITY) || (priorityP > this->Priority());
2012-06-02 14:37:05 +02:00
bool needsDetachReceivers = false;
2007-09-12 19:28:59 +02:00
2013-02-23 16:12:46 +01:00
debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
2012-06-02 14:37:05 +02:00
2013-02-23 14:31:11 +01:00
if (channelP && ProvidesTransponder(channelP)) {
2012-06-02 14:37:05 +02:00
result = hasPriority;
if (Receiving()) {
if (channelP->GetChannelID() == channelM.GetChannelID())
2012-06-02 14:37:05 +02:00
result = true;
else
needsDetachReceivers = Receiving();
}
}
2013-02-23 14:31:11 +01:00
if (needsDetachReceiversP)
*needsDetachReceiversP = needsDetachReceivers;
2007-09-12 19:28:59 +02:00
return result;
}
2011-09-04 16:58:18 +02:00
bool cIptvDevice::ProvidesEIT(void) const
{
return false;
}
2008-01-28 22:36:32 +01:00
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;
}
2013-02-23 14:31:11 +01:00
bool cIptvDevice::SetChannelDevice(const cChannel *channelP, bool liveViewP)
2007-09-12 19:28:59 +02:00
{
2007-09-14 17:44:25 +02:00
cIptvProtocolIf *protocol;
2013-02-23 14:31:11 +01:00
cIptvTransponderParameters itp(channelP->Parameters());
2007-09-12 19:28:59 +02:00
2013-02-23 16:12:46 +01:00
debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
2011-06-15 16:49:15 +02:00
2010-03-04 16:34:21 +01:00
if (isempty(itp.Address())) {
2013-02-23 14:31:11 +01:00
error("Unrecognized IPTV address: %s", channelP->Parameters());
return false;
}
2010-03-04 16:34:21 +01:00
switch (itp.Protocol()) {
case cIptvTransponderParameters::eProtocolUDP:
2013-02-23 14:31:11 +01:00
protocol = pUdpProtocolM;
2010-03-04 16:34:21 +01:00
break;
case cIptvTransponderParameters::eProtocolCURL:
2013-02-23 14:31:11 +01:00
protocol = pCurlProtocolM;
break;
2010-03-04 16:34:21 +01:00
case cIptvTransponderParameters::eProtocolHTTP:
2013-02-23 14:31:11 +01:00
protocol = pHttpProtocolM;
2010-03-04 16:34:21 +01:00
break;
case cIptvTransponderParameters::eProtocolFILE:
2013-02-23 14:31:11 +01:00
protocol = pFileProtocolM;
2010-03-04 16:34:21 +01:00
break;
case cIptvTransponderParameters::eProtocolEXT:
2013-02-23 14:31:11 +01:00
protocol = pExtProtocolM;
2010-03-04 16:34:21 +01:00
break;
default:
2013-02-23 14:31:11 +01:00
error("Unrecognized IPTV protocol: %s", channelP->Parameters());
2010-03-04 16:34:21 +01:00
return false;
break;
}
2013-02-23 14:31:11 +01:00
sidScanEnabledM = itp.SidScan() ? true : false;
pidScanEnabledM = itp.PidScan() ? true : false;
if (pIptvStreamerM->Set(itp.Address(), itp.Parameter(), deviceIndexM, protocol)) {
channelM = *channelP;
2013-02-23 14:31:11 +01:00
if (sidScanEnabledM && pSidScannerM && IptvConfig.GetSectionFiltering())
pSidScannerM->SetChannel(channelM.GetChannelID());
2013-02-23 14:31:11 +01:00
if (pidScanEnabledM && pPidScannerM)
pPidScannerM->SetChannel(channelM.GetChannelID());
}
2007-09-12 19:28:59 +02:00
return true;
}
2013-02-23 14:31:11 +01:00
bool cIptvDevice::SetPid(cPidHandle *handleP, int typeP, bool onP)
2007-09-12 19:28:59 +02:00
{
2013-02-23 16:12:46 +01:00
debug("cIptvDevice::%s(%d): pid=%d type=%d on=%d", __FUNCTION__, deviceIndexM, handleP->pid, typeP, onP);
2007-09-12 19:28:59 +02:00
return true;
}
2013-02-23 14:31:11 +01:00
int cIptvDevice::OpenFilter(u_short pidP, u_char tidP, u_char maskP)
{
2013-04-01 21:36:41 +02:00
//debug("cIptvDevice::%s(%d): pid=%d tid=%d mask=%d", __FUNCTION__, deviceIndexM, pidP, tidP, maskP);
if (pIptvSectionM && IptvConfig.GetSectionFiltering())
return pIptvSectionM->Open(pidP, tidP, maskP);
return -1;
}
2013-02-23 14:31:11 +01:00
void cIptvDevice::CloseFilter(int handleP)
{
2013-04-01 21:36:41 +02:00
//debug("cIptvDevice::%s(%d): handle=%d", __FUNCTION__, deviceIndexM, handleP);
if (pIptvSectionM)
pIptvSectionM->Close(handleP);
}
2007-09-12 19:28:59 +02:00
bool cIptvDevice::OpenDvr(void)
{
2013-02-23 16:12:46 +01:00
debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
2013-02-23 14:31:11 +01:00
isPacketDeliveredM = false;
tsBufferM->Clear();
2007-09-21 23:50:52 +02:00
ResetBuffering();
2013-02-23 14:31:11 +01:00
if (pIptvStreamerM)
pIptvStreamerM->Open();
if (sidScanEnabledM && pSidScannerM && IptvConfig.GetSectionFiltering())
pSidScannerM->Open();
if (pidScanEnabledM && pPidScannerM)
pPidScannerM->Open();
2013-02-23 14:31:11 +01:00
isOpenDvrM = true;
2007-09-12 19:28:59 +02:00
return true;
}
void cIptvDevice::CloseDvr(void)
{
2013-02-23 16:12:46 +01:00
debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
if (pidScanEnabledM && pPidScannerM)
pPidScannerM->Close();
2014-01-07 12:17:30 +01:00
if (sidScanEnabledM && pSidScannerM)
2013-02-23 14:31:11 +01:00
pSidScannerM->Close();
if (pIptvStreamerM)
pIptvStreamerM->Close();
isOpenDvrM = false;
2007-09-12 19:28:59 +02:00
}
2013-02-23 14:31:11 +01:00
bool cIptvDevice::HasLock(int timeoutMsP) const
{
2013-02-23 16:12:46 +01:00
//debug("cIptvDevice::%s(%d): timeoutMs=%d", __FUNCTION__, deviceIndexM, timeoutMsP);
2013-02-23 14:31:11 +01:00
return (!IsBuffering());
2007-09-21 23:50:52 +02:00
}
bool cIptvDevice::HasInternalCam(void)
{
2013-02-23 16:12:46 +01:00
//debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
return true;
}
2007-09-21 23:50:52 +02:00
void cIptvDevice::ResetBuffering(void)
{
2013-02-23 16:12:46 +01:00
debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
// Pad prefill to multiple of TS_SIZE
2013-02-23 14:31:11 +01:00
tsBufferPrefillM = (unsigned int)MEGABYTE(IptvConfig.GetTsBufferSize()) *
2007-09-21 23:50:52 +02:00
IptvConfig.GetTsBufferPrefillRatio() / 100;
2013-02-23 14:31:11 +01:00
tsBufferPrefillM -= (tsBufferPrefillM % TS_SIZE);
2007-09-21 23:50:52 +02:00
}
2013-02-23 14:31:11 +01:00
bool cIptvDevice::IsBuffering(void) const
2007-09-21 23:50:52 +02:00
{
2013-02-23 16:12:46 +01:00
//debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
2013-02-23 14:31:11 +01:00
if (tsBufferPrefillM && tsBufferM && tsBufferM->Available() < tsBufferPrefillM)
return true;
else
tsBufferPrefillM = 0;
return false;
}
void cIptvDevice::WriteData(uchar *bufferP, int lengthP)
{
//debug("cIptvDevice::%s(%d)", __FUNCTION__, 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)
{
//debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
if (tsBufferM)
return (unsigned int)tsBufferM->Free();
return 0;
}
2007-09-12 19:28:59 +02:00
bool cIptvDevice::GetTSPacket(uchar *&Data)
{
2013-02-23 16:12:46 +01:00
//debug("cIptvDevice::%s(%d)", __FUNCTION__, deviceIndexM);
if (isOpenDvrM && tsBufferM && !IsBuffering()) {
2013-02-23 14:31:11 +01:00
if (isPacketDeliveredM) {
tsBufferM->Del(TS_SIZE);
isPacketDeliveredM = false;
2007-10-10 00:12:17 +02:00
// Update buffer statistics
2013-02-23 14:31:11 +01:00
AddBufferStatistic(TS_SIZE, tsBufferM->Available());
2007-09-12 19:28:59 +02:00
}
2011-06-15 16:49:15 +02:00
int Count = 0;
2013-02-23 14:31:11 +01:00
uchar *p = tsBufferM->Get(Count);
2007-09-21 23:50:52 +02:00
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
}
2013-02-23 14:31:11 +01:00
tsBufferM->Del(Count);
error("Skipped %d bytes to sync on TS packet", Count);
2007-09-21 23:50:52 +02:00
return false;
}
2013-02-23 14:31:11 +01:00
isPacketDeliveredM = true;
2007-09-21 23:50:52 +02:00
Data = p;
// Update pid statistics
AddPidStatistic(ts_pid(p), payload(p));
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
2009-02-26 15:04:12 +01:00
cCondWait::SleepMs(10);
2007-09-12 19:28:59 +02:00
Data = NULL;
return true;
}