2014-11-08 21:22:46 +01:00
|
|
|
/*
|
|
|
|
* tuner.c: SAT>IP plugin for the Video Disk Recorder
|
|
|
|
*
|
|
|
|
* See the README file for copyright information and how to reach the author.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "discover.h"
|
|
|
|
#include "param.h"
|
|
|
|
#include "tuner.h"
|
|
|
|
#include "data.h"
|
|
|
|
|
|
|
|
enum LOGLEVEL {
|
|
|
|
logFunc = 0x01,
|
|
|
|
logFuncPerf = 0x02,
|
|
|
|
logData = 0x10,
|
|
|
|
logDataDetails = 0x20,
|
|
|
|
logAll = 0xFFFF
|
|
|
|
};
|
|
|
|
|
2014-11-09 22:56:15 +01:00
|
|
|
int logLevel = logFunc /*| logFuncPerf | logData*/;
|
2014-11-08 21:22:46 +01:00
|
|
|
|
|
|
|
#define log(lvl) \
|
|
|
|
if (logLevel & lvl) \
|
|
|
|
debug("%s::%s [device %d]", __CLASS__, __FUNCTION__, deviceM->GetId())
|
|
|
|
#define log1(lvl, a) \
|
|
|
|
if (logLevel & lvl) \
|
|
|
|
debug("%s::%s" a " [device %d]", __CLASS__, __FUNCTION__, deviceM->GetId())
|
|
|
|
#define log2(lvl, a, b...) \
|
|
|
|
if (logLevel & lvl) \
|
|
|
|
debug("%s::%s " a " [device %d]", __CLASS__, __FUNCTION__, b, deviceM->GetId())
|
|
|
|
#define __CLASS__ "cSatipTunerDataThread"
|
|
|
|
|
|
|
|
cSatipTunerDataThread::cSatipTunerDataThread(cSatipDeviceIf &deviceP, cSatipTunerStatistics &statisticsP, unsigned int packetLenP)
|
2014-11-09 21:39:59 +01:00
|
|
|
: cThread(cString::sprintf("SAT>IP data %d", deviceP.GetId())),
|
2014-11-08 21:22:46 +01:00
|
|
|
deviceM(&deviceP),
|
|
|
|
statisticsM(&statisticsP),
|
|
|
|
packetBufferLenM(packetLenP),
|
|
|
|
packetBufferM(NULL),
|
|
|
|
rtpSocketM(NULL),
|
2014-11-09 22:24:22 +01:00
|
|
|
timeDataReceivedM(),
|
2014-11-08 21:22:46 +01:00
|
|
|
sleepM(),
|
|
|
|
mutexM()
|
|
|
|
{
|
|
|
|
log2(logFunc, "(...,...,%d)", packetLenP);
|
|
|
|
|
|
|
|
// Allocate packet buffer
|
|
|
|
packetBufferM = MALLOC(unsigned char, packetBufferLenM);
|
|
|
|
if (packetBufferM)
|
|
|
|
memset(packetBufferM, 0, packetBufferLenM);
|
|
|
|
else
|
|
|
|
error("MALLOC() failed for packet buffer [device %d]", deviceM->GetId());
|
|
|
|
}
|
|
|
|
|
|
|
|
cSatipTunerDataThread::~cSatipTunerDataThread()
|
|
|
|
{
|
|
|
|
log(logFunc);
|
|
|
|
cMutexLock MutexLock(&mutexM);
|
|
|
|
|
|
|
|
// Free allocated memory
|
|
|
|
free(packetBufferM);
|
|
|
|
packetBufferM = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cSatipTunerDataThread::Start(cSatipSocket *rtpSocketP)
|
|
|
|
{
|
|
|
|
log1(logFunc, "(...)");
|
|
|
|
cMutexLock MutexLock(&mutexM);
|
|
|
|
|
|
|
|
rtpSocketM = rtpSocketP;
|
|
|
|
|
|
|
|
// Start thread
|
|
|
|
cThread::Start();
|
|
|
|
}
|
|
|
|
|
2014-11-09 22:24:22 +01:00
|
|
|
int cSatipTunerDataThread::LastReceivedMs()
|
2014-11-08 21:22:46 +01:00
|
|
|
{
|
2014-11-09 22:24:22 +01:00
|
|
|
int rc = timeDataReceivedM.Elapsed();
|
|
|
|
|
2014-11-09 22:56:15 +01:00
|
|
|
log2(logFuncPerf, "returning %d", rc);
|
|
|
|
|
|
|
|
return rc;
|
2014-11-09 22:24:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void cSatipTunerDataThread::ResetLastReceivedMs()
|
|
|
|
{
|
|
|
|
log(logFunc);
|
2014-11-08 21:22:46 +01:00
|
|
|
|
2014-11-09 22:24:22 +01:00
|
|
|
timeDataReceivedM.Set();
|
2014-11-08 21:22:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void cSatipTunerDataThread::Cancel(int WaitSeconds)
|
|
|
|
{
|
|
|
|
if (Running())
|
|
|
|
cThread::Cancel(WaitSeconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cSatipTunerDataThread::Flush(void)
|
|
|
|
{
|
|
|
|
log(logFunc);
|
|
|
|
cMutexLock MutexLock(&mutexM);
|
|
|
|
|
|
|
|
if (rtpSocketM)
|
|
|
|
rtpSocketM->Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cSatipTunerDataThread::Action(void)
|
|
|
|
{
|
|
|
|
log(logFunc);
|
2014-11-09 22:56:15 +01:00
|
|
|
bool polling = SatipConfig.IsPolling();
|
2014-11-08 21:22:46 +01:00
|
|
|
|
|
|
|
// Increase priority
|
|
|
|
SetPriority(-1);
|
|
|
|
|
|
|
|
// Do the thread loop
|
|
|
|
while (Running() && packetBufferM) {
|
|
|
|
int length = -1;
|
|
|
|
unsigned int size = min(deviceM->CheckData(), packetBufferLenM);
|
|
|
|
|
|
|
|
mutexM.Lock();
|
|
|
|
|
|
|
|
// Read data
|
|
|
|
if (rtpSocketM && rtpSocketM->IsOpen()) {
|
|
|
|
length = rtpSocketM->ReadVideo(packetBufferM, size);
|
2014-11-09 22:56:15 +01:00
|
|
|
if (!polling || length > 0)
|
|
|
|
timeDataReceivedM.Set();
|
|
|
|
|
2014-11-08 21:22:46 +01:00
|
|
|
log2(logData, "received %d bytes", length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > 0) {
|
|
|
|
log2(logDataDetails, "trying to write %d bytes", length);
|
|
|
|
deviceM->WriteData(packetBufferM, length);
|
|
|
|
log2(logDataDetails, "wrote %d bytes", length);
|
|
|
|
|
|
|
|
if (statisticsM)
|
|
|
|
statisticsM->AddTunerStatistic(length);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
mutexM.Unlock();
|
|
|
|
|
|
|
|
// to avoid busy loop and reduce cpu load
|
2014-11-09 22:56:15 +01:00
|
|
|
if (polling && length <= 0)
|
2014-11-08 21:22:46 +01:00
|
|
|
sleepM.Wait(10);
|
|
|
|
}
|
|
|
|
}
|