vdr-plugin-iptv/device.c

208 lines
5.3 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-14 20:02:22 +02:00
* $Id: device.c,v 1.10 2007/09/14 18:02:22 ajhseppa Exp $
2007-09-12 19:28:59 +02:00
*/
#include "common.h"
#include "device.h"
#define IPTV_MAX_DEVICES 8
cIptvDevice * IptvDevices[IPTV_MAX_DEVICES];
unsigned int cIptvDevice::deviceCount = 0;
cIptvDevice::cIptvDevice(unsigned int Index)
: deviceIndex(Index),
isPacketDelivered(false),
isOpenDvr(false),
2007-09-14 17:44:25 +02:00
mutex()
2007-09-12 19:28:59 +02:00
{
debug("cIptvDevice::cIptvDevice(%d)\n", deviceIndex);
2007-09-12 19:28:59 +02:00
tsBuffer = new cRingBufferLinear(MEGABYTE(8), TS_SIZE, false, "IPTV");
tsBuffer->SetTimeouts(100, 100);
2007-09-14 17:44:25 +02:00
pUdpProtocol = new cIptvProtocolUdp();
//pRtspProtocol = new cIptvProtocolRtsp();
//pHttpProtocol = new cIptvProtocolHttp();
2007-09-12 19:28:59 +02:00
pIptvStreamer = new cIptvStreamer(tsBuffer, &mutex);
2007-09-14 17:44:25 +02:00
StartSectionHandler();
2007-09-12 19:28:59 +02:00
}
cIptvDevice::~cIptvDevice()
{
debug("cIptvDevice::~cIptvDevice(%d)\n", deviceIndex);
2007-09-14 17:44:25 +02:00
delete pIptvStreamer;
delete pUdpProtocol;
2007-09-12 19:28:59 +02:00
delete tsBuffer;
}
bool cIptvDevice::Initialize(unsigned int DeviceCount)
{
debug("cIptvDevice::Initialize()\n");
if (DeviceCount > IPTV_MAX_DEVICES)
DeviceCount = IPTV_MAX_DEVICES;
for (unsigned int i = 0; i < DeviceCount; ++i)
IptvDevices[i] = new cIptvDevice(i);
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])
count++;
}
return count;
}
cIptvDevice *cIptvDevice::Get(unsigned int DeviceIndex)
{
debug("cIptvDevice::Get()\n");
if ((DeviceIndex > 0) && (DeviceIndex <= IPTV_MAX_DEVICES))
return IptvDevices[DeviceIndex - 1];
return NULL;
}
2007-09-14 17:44:25 +02:00
cString cIptvDevice::GetChannelSettings(const char *Param, int *IpPort, cIptvProtocolIf* *Protocol)
2007-09-12 19:28:59 +02:00
{
unsigned int a, b, c, d;
debug("cIptvDevice::GetChannelSettings(%d)\n", deviceIndex);
2007-09-12 19:28:59 +02:00
if (sscanf(Param, "IPTV-UDP-%u.%u.%u.%u-%u", &a, &b, &c, &d, IpPort) == 5) {
2007-09-14 17:44:25 +02:00
*Protocol = pUdpProtocol;
2007-09-12 19:28:59 +02:00
return cString::sprintf("%u.%u.%u.%u", a, b, c, d);
}
else if (sscanf(Param, "IPTV-RTSP-%u.%u.%u.%u-%u", &a, &b, &c, &d, IpPort) == 5) {
2007-09-14 17:44:25 +02:00
*Protocol = NULL; // pRtspProtocol;
2007-09-12 19:28:59 +02:00
return cString::sprintf("%u.%u.%u.%u", a, b, c, d);
}
else if (sscanf(Param, "IPTV-HTTP-%u.%u.%u.%u-%u", &a, &b, &c, &d, IpPort) == 5) {
2007-09-14 17:44:25 +02:00
*Protocol = NULL; // pHttpProtocol;
2007-09-12 19:28:59 +02:00
return cString::sprintf("%u.%u.%u.%u", a, b, c, d);
}
return NULL;
}
bool cIptvDevice::ProvidesIptv(const char *Param) const
{
debug("cIptvDevice::ProvidesIptv(%d)\n", deviceIndex);
2007-09-12 19:28:59 +02:00
return (strncmp(Param, "IPTV", 4) == 0);
}
bool cIptvDevice::ProvidesSource(int Source) const
{
debug("cIptvDevice::ProvidesSource(%d)\n", deviceIndex);
2007-09-12 19:28:59 +02:00
return ((Source & cSource::st_Mask) == cSource::stPlug);
}
bool cIptvDevice::ProvidesTransponder(const cChannel *Channel) const
{
debug("cIptvDevice::ProvidesTransponder(%d)\n", deviceIndex);
2007-09-12 19:28:59 +02:00
return (ProvidesSource(Channel->Source()) && ProvidesIptv(Channel->Param()));
}
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;
}
bool cIptvDevice::SetChannelDevice(const cChannel *Channel, bool LiveView)
{
2007-09-14 17:44:25 +02:00
int port;
2007-09-13 20:14:41 +02:00
cString addr;
2007-09-14 17:44:25 +02:00
cIptvProtocolIf *protocol;
2007-09-12 19:28:59 +02:00
debug("cIptvDevice::SetChannelDevice(%d)\n", deviceIndex);
2007-09-12 19:28:59 +02:00
addr = GetChannelSettings(Channel->Param(), &port, &protocol);
if (addr)
2007-09-14 17:44:25 +02:00
pIptvStreamer->Set(addr, port, protocol);
2007-09-12 19:28:59 +02:00
return true;
}
bool cIptvDevice::SetPid(cPidHandle *Handle, int Type, bool On)
{
debug("cIptvDevice::SetPid(%d)\n", deviceIndex);
2007-09-12 19:28:59 +02:00
return true;
}
int cIptvDevice::OpenFilter(u_short Pid, u_char Tid, u_char Mask)
{
2007-09-14 20:02:22 +02:00
int pipeDesc[2];
debug("cIptvDevice::OpenFilter(): Pid=%d Tid=%02X Mask=%02X\n", Pid, Tid, Mask);
2007-09-14 20:02:22 +02:00
// Create a pipe
if (pipe(pipeDesc) < 0) {
char tmp[64];
error("ERROR: pipe(): %s", strerror_r(errno, tmp, sizeof(tmp)));
}
// Write pipe is used by the pid filtering class
// cReceiver Filter(pipeDesc[1], Pid, Tid, Mask)
// Give the read pipe to vdr
return pipeDesc[0];
}
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-14 17:44:25 +02:00
pIptvStreamer->Open();
2007-09-12 19:28:59 +02:00
isOpenDvr = true;
return true;
}
void cIptvDevice::CloseDvr(void)
{
debug("cIptvDevice::CloseDvr(%d)\n", deviceIndex);
2007-09-14 17:44:25 +02:00
pIptvStreamer->Close();
2007-09-12 19:28:59 +02:00
isOpenDvr = false;
}
bool cIptvDevice::GetTSPacket(uchar *&Data)
{
int Count = 0;
//debug("cIptvDevice::GetTSPacket(%d)\n", deviceIndex);
2007-09-12 19:28:59 +02:00
if (isPacketDelivered) {
tsBuffer->Del(TS_SIZE);
isPacketDelivered = false;
}
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;
}
}
tsBuffer->Del(Count);
error("ERROR: skipped %d bytes to sync on TS packet\n", Count);
return false;
}
isPacketDelivered = true;
Data = p;
return true;
}
Data = NULL;
return true;
}