2008-01-30 23:41:58 +01:00
|
|
|
/*
|
|
|
|
* pidscanner.c: IPTV plugin for the Video Disk Recorder
|
|
|
|
*
|
|
|
|
* See the README file for copyright information and how to reach the author.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "pidscanner.h"
|
|
|
|
|
2008-02-02 22:06:14 +01:00
|
|
|
#define PIDSCANNER_TIMEOUT_IN_MS 15000 /* 15s timeout for detection */
|
2008-08-06 10:05:52 +02:00
|
|
|
#define PIDSCANNER_APID_COUNT 5 /* minimum count of audio pid samples for pid detection */
|
|
|
|
#define PIDSCANNER_VPID_COUNT 5 /* minimum count of video pid samples for pid detection */
|
|
|
|
#define PIDSCANNER_PID_DELTA_COUNT 100 /* minimum count of pid samples for audio/video only pid detection */
|
2008-02-01 22:54:24 +01:00
|
|
|
|
2008-01-30 23:41:58 +01:00
|
|
|
cPidScanner::cPidScanner(void)
|
2008-02-01 22:54:24 +01:00
|
|
|
: timeout(0),
|
|
|
|
process(true),
|
2008-01-30 23:41:58 +01:00
|
|
|
Vpid(0xFFFF),
|
|
|
|
Apid(0xFFFF),
|
|
|
|
numVpids(0),
|
|
|
|
numApids(0)
|
|
|
|
{
|
|
|
|
debug("cPidScanner::cPidScanner()\n");
|
|
|
|
channel = cChannel();
|
|
|
|
}
|
|
|
|
|
|
|
|
cPidScanner::~cPidScanner()
|
|
|
|
{
|
|
|
|
debug("cPidScanner::~cPidScanner()\n");
|
|
|
|
}
|
|
|
|
|
2008-02-02 21:51:47 +01:00
|
|
|
void cPidScanner::SetChannel(const cChannel *Channel)
|
|
|
|
{
|
|
|
|
if (Channel) {
|
2010-03-04 16:34:21 +01:00
|
|
|
debug("cPidScanner::SetChannel(): %s\n", Channel->Parameters());
|
2008-02-02 21:51:47 +01:00
|
|
|
channel = *Channel;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
debug("cPidScanner::SetChannel()\n");
|
|
|
|
channel = cChannel();
|
|
|
|
}
|
|
|
|
Vpid = 0xFFFF;
|
|
|
|
numVpids = 0;
|
|
|
|
Apid = 0xFFFF;
|
|
|
|
numApids = 0;
|
|
|
|
process = true;
|
|
|
|
timeout.Set(PIDSCANNER_TIMEOUT_IN_MS);
|
|
|
|
}
|
|
|
|
|
2008-01-30 23:41:58 +01:00
|
|
|
void cPidScanner::Process(const uint8_t* buf)
|
|
|
|
{
|
|
|
|
//debug("cPidScanner::Process()\n");
|
|
|
|
if (!process)
|
|
|
|
return;
|
|
|
|
|
2008-02-01 22:54:24 +01:00
|
|
|
// Stop scanning after defined timeout
|
2008-02-02 21:23:44 +01:00
|
|
|
if (timeout.TimedOut()) {
|
|
|
|
debug("cPidScanner::Process: Timed out determining pids\n");
|
2008-02-01 22:54:24 +01:00
|
|
|
process = false;
|
2008-02-02 21:23:44 +01:00
|
|
|
}
|
2008-02-01 22:54:24 +01:00
|
|
|
|
2008-02-02 21:51:47 +01:00
|
|
|
// Verify TS packet
|
2008-01-30 23:41:58 +01:00
|
|
|
if (buf[0] != 0x47) {
|
|
|
|
error("Not TS packet: 0x%X\n", buf[0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found TS packet
|
|
|
|
int pid = ts_pid(buf);
|
|
|
|
int xpid = (buf[1] << 8 | buf[2]);
|
|
|
|
|
2008-02-02 21:51:47 +01:00
|
|
|
// Check if payload available
|
2008-01-30 23:41:58 +01:00
|
|
|
uint8_t count = payload(buf);
|
|
|
|
if (count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (xpid & 0x4000) {
|
|
|
|
// Stream start (Payload Unit Start Indicator)
|
|
|
|
uchar *d = (uint8_t*)buf;
|
|
|
|
d += 4;
|
|
|
|
// pointer to payload
|
|
|
|
if (buf[3] & 0x20)
|
|
|
|
d += d[0] + 1;
|
|
|
|
// Skip adaption field
|
|
|
|
if (buf[3] & 0x10) {
|
|
|
|
// Payload present
|
|
|
|
if ((d[0] == 0) && (d[1] == 0) && (d[2] == 1)) {
|
|
|
|
// PES packet start
|
|
|
|
int sid = d[3];
|
|
|
|
// Stream ID
|
|
|
|
if ((sid >= 0xC0) && (sid <= 0xDF)) {
|
|
|
|
if (pid < Apid) {
|
2008-04-03 00:55:04 +02:00
|
|
|
debug("cPidScanner::Process: Found lower Apid: 0x%X instead of 0x%X\n", pid, Apid);
|
2008-01-30 23:41:58 +01:00
|
|
|
Apid = pid;
|
|
|
|
numApids = 1;
|
|
|
|
}
|
2008-02-02 21:23:44 +01:00
|
|
|
else if (pid == Apid) {
|
2008-01-30 23:41:58 +01:00
|
|
|
++numApids;
|
2008-04-03 00:55:04 +02:00
|
|
|
debug("cPidScanner::Process: Incrementing Apids, now at %d\n", numApids);
|
2008-02-02 21:23:44 +01:00
|
|
|
}
|
2008-01-30 23:41:58 +01:00
|
|
|
}
|
|
|
|
else if ((sid >= 0xE0) && (sid <= 0xEF)) {
|
|
|
|
if (pid < Vpid) {
|
2008-04-03 00:55:04 +02:00
|
|
|
debug("cPidScanner::Process: Found lower Vpid: 0x%X instead of 0x%X\n", pid, Vpid);
|
2008-01-30 23:41:58 +01:00
|
|
|
Vpid = pid;
|
|
|
|
numVpids = 1;
|
|
|
|
}
|
2008-02-02 21:23:44 +01:00
|
|
|
else if (pid == Vpid) {
|
2008-01-30 23:41:58 +01:00
|
|
|
++numVpids;
|
2008-04-03 00:55:04 +02:00
|
|
|
debug("cPidScanner::Process: Incrementing Vpids, now at %d\n", numVpids);
|
2008-02-02 21:23:44 +01:00
|
|
|
}
|
2008-01-30 23:41:58 +01:00
|
|
|
}
|
|
|
|
}
|
2008-04-03 00:55:04 +02:00
|
|
|
if (((numVpids >= PIDSCANNER_VPID_COUNT) && (numApids >= PIDSCANNER_APID_COUNT)) ||
|
|
|
|
(abs(numApids - numVpids) >= PIDSCANNER_PID_DELTA_COUNT)) {
|
2008-02-02 21:51:47 +01:00
|
|
|
// Lock channels for pid updates
|
2008-02-01 22:58:22 +01:00
|
|
|
if (!Channels.Lock(true, 10)) {
|
|
|
|
timeout.Set(PIDSCANNER_TIMEOUT_IN_MS);
|
2008-01-30 23:41:58 +01:00
|
|
|
return;
|
2008-02-01 22:58:22 +01:00
|
|
|
}
|
2008-01-30 23:41:58 +01:00
|
|
|
cChannel *IptvChannel = Channels.GetByChannelID(channel.GetChannelID());
|
2009-02-25 21:52:25 +01:00
|
|
|
if (IptvChannel) {
|
|
|
|
int Apids[MAXAPIDS + 1] = { 0 }; // these lists are zero-terminated
|
2010-06-06 19:36:58 +02:00
|
|
|
int Atypes[MAXAPIDS + 1] = { 0 };
|
2009-02-25 21:52:25 +01:00
|
|
|
int Dpids[MAXDPIDS + 1] = { 0 };
|
2010-06-06 19:36:58 +02:00
|
|
|
int Dtypes[MAXDPIDS + 1] = { 0 };
|
2009-02-25 21:52:25 +01:00
|
|
|
int Spids[MAXSPIDS + 1] = { 0 };
|
|
|
|
char ALangs[MAXAPIDS][MAXLANGCODE2] = { "" };
|
|
|
|
char DLangs[MAXDPIDS][MAXLANGCODE2] = { "" };
|
|
|
|
char SLangs[MAXSPIDS][MAXLANGCODE2] = { "" };
|
2010-07-10 22:27:07 +02:00
|
|
|
int Vtype = IptvChannel->Vtype();
|
2009-02-25 21:52:25 +01:00
|
|
|
int Ppid = IptvChannel->Ppid();
|
|
|
|
int Tpid = IptvChannel->Tpid();
|
|
|
|
bool foundApid = false;
|
|
|
|
if (numVpids < PIDSCANNER_VPID_COUNT)
|
|
|
|
Vpid = 0; // No detected video pid
|
|
|
|
else if (numApids < PIDSCANNER_APID_COUNT)
|
|
|
|
Apid = 0; // No detected audio pid
|
2010-07-10 22:27:07 +02:00
|
|
|
for (unsigned int i = 0; i < MAXAPIDS; ++i) {
|
2009-02-25 21:52:25 +01:00
|
|
|
Apids[i] = IptvChannel->Apid(i);
|
2010-06-06 19:36:58 +02:00
|
|
|
Atypes[i] = IptvChannel->Atype(i);
|
2009-02-25 21:52:25 +01:00
|
|
|
if (Apids[i] && (Apids[i] == Apid))
|
|
|
|
foundApid = true;
|
|
|
|
}
|
2010-06-06 19:36:58 +02:00
|
|
|
if (!foundApid) {
|
2009-02-25 21:52:25 +01:00
|
|
|
Apids[0] = Apid;
|
2010-06-06 19:36:58 +02:00
|
|
|
Atypes[0] = 4;
|
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i < MAXDPIDS; ++i) {
|
2009-02-25 21:52:25 +01:00
|
|
|
Dpids[i] = IptvChannel->Dpid(i);
|
2010-06-06 19:36:58 +02:00
|
|
|
Dtypes[i] = IptvChannel->Dtype(i);
|
|
|
|
}
|
2009-02-25 21:52:25 +01:00
|
|
|
for (unsigned int i = 0; i < MAXSPIDS; ++i)
|
|
|
|
Spids[i] = IptvChannel->Spid(i);
|
|
|
|
debug("cPidScanner::Process(): Vpid=0x%04X, Apid=0x%04X\n", Vpid, Apid);
|
2010-06-06 19:36:58 +02:00
|
|
|
IptvChannel->SetPids(Vpid, Ppid, Vtype, Apids, Atypes, ALangs, Dpids, Dtypes, DLangs, Spids, SLangs, Tpid);
|
2009-02-25 21:52:25 +01:00
|
|
|
}
|
2008-01-30 23:41:58 +01:00
|
|
|
Channels.Unlock();
|
2008-02-02 21:23:44 +01:00
|
|
|
process = false;
|
2008-01-30 23:41:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|