2014-03-08 12:07:47 +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"
|
2014-03-13 17:23:55 +01:00
|
|
|
#include "discover.h"
|
2014-03-08 12:07:47 +01:00
|
|
|
#include "tuner.h"
|
|
|
|
|
|
|
|
cSatipTuner::cSatipTuner(cSatipDeviceIf &deviceP, unsigned int packetLenP)
|
|
|
|
: cThread("SAT>IP tuner"),
|
2014-11-08 21:22:46 +01:00
|
|
|
dataThreadM(deviceP, *this, packetLenP),
|
2014-03-08 12:07:47 +01:00
|
|
|
sleepM(),
|
|
|
|
deviceM(&deviceP),
|
|
|
|
rtpSocketM(new cSatipSocket()),
|
|
|
|
rtcpSocketM(new cSatipSocket()),
|
|
|
|
streamAddrM(""),
|
|
|
|
streamParamM(""),
|
2014-03-13 17:23:55 +01:00
|
|
|
currentServerM(NULL),
|
|
|
|
nextServerM(NULL),
|
2014-03-08 12:07:47 +01:00
|
|
|
mutexM(),
|
|
|
|
handleM(NULL),
|
|
|
|
keepAliveM(),
|
2014-11-05 20:55:40 +01:00
|
|
|
statusUpdateM(),
|
2014-03-08 12:07:47 +01:00
|
|
|
pidUpdateCacheM(),
|
2014-05-14 21:14:59 +02:00
|
|
|
sessionM(""),
|
2014-03-31 21:07:15 +02:00
|
|
|
timeoutM(eMinKeepAliveIntervalMs),
|
2014-11-08 21:22:46 +01:00
|
|
|
reconnectM(false),
|
2014-03-08 12:07:47 +01:00
|
|
|
tunedM(false),
|
|
|
|
hasLockM(false),
|
|
|
|
signalStrengthM(-1),
|
|
|
|
signalQualityM(-1),
|
|
|
|
streamIdM(-1),
|
2014-04-19 16:27:47 +02:00
|
|
|
addPidsM(),
|
|
|
|
delPidsM(),
|
2014-03-08 12:07:47 +01:00
|
|
|
pidsM()
|
|
|
|
{
|
2014-11-08 21:22:46 +01:00
|
|
|
debug("cSatipTuner::%s(%d) [device %d]", __FUNCTION__, packetLenP, deviceM->GetId());
|
|
|
|
|
|
|
|
RtspInitialize();
|
|
|
|
|
|
|
|
// Open sockets
|
|
|
|
for (int i = 100; i > 0; --i) {
|
|
|
|
if (rtpSocketM->Open() && rtcpSocketM->Open(rtpSocketM->Port() + 1))
|
|
|
|
break;
|
|
|
|
rtpSocketM->Close();
|
|
|
|
rtcpSocketM->Close();
|
|
|
|
}
|
|
|
|
if ((rtpSocketM->Port() <= 0) || (rtcpSocketM->Port() <= 0)) {
|
|
|
|
error("Cannot open required RTP/RTCP ports [device %d]", deviceM->GetId());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
info("Using RTP/RTCP ports %d-%d [device %d]", rtpSocketM->Port(), rtcpSocketM->Port(), deviceM->GetId());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start threads
|
|
|
|
dataThreadM.Start(rtpSocketM);
|
2014-03-08 12:07:47 +01:00
|
|
|
Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
cSatipTuner::~cSatipTuner()
|
|
|
|
{
|
2014-04-12 15:57:28 +02:00
|
|
|
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
2014-11-08 21:22:46 +01:00
|
|
|
// Stop threads
|
|
|
|
dataThreadM.Cancel(3);
|
2014-03-08 12:07:47 +01:00
|
|
|
sleepM.Signal();
|
|
|
|
if (Running())
|
|
|
|
Cancel(3);
|
|
|
|
Close();
|
2014-11-08 21:22:46 +01:00
|
|
|
|
|
|
|
// Terminate curl session
|
|
|
|
RtspTerminate();
|
|
|
|
|
|
|
|
// Close the listening sockets
|
|
|
|
if (rtpSocketM)
|
|
|
|
rtpSocketM->Close();
|
|
|
|
if (rtcpSocketM)
|
|
|
|
rtcpSocketM->Close();
|
|
|
|
|
2014-03-08 12:07:47 +01:00
|
|
|
// Free allocated memory
|
|
|
|
DELETENULL(rtcpSocketM);
|
|
|
|
DELETENULL(rtpSocketM);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t cSatipTuner::HeaderCallback(void *ptrP, size_t sizeP, size_t nmembP, void *dataP)
|
|
|
|
{
|
|
|
|
cSatipTuner *obj = reinterpret_cast<cSatipTuner *>(dataP);
|
2014-11-08 21:22:46 +01:00
|
|
|
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, obj->deviceM->GetId());
|
|
|
|
|
2014-03-08 12:07:47 +01:00
|
|
|
size_t len = sizeP * nmembP;
|
|
|
|
|
|
|
|
char *s, *p = (char *)ptrP;
|
|
|
|
char *r = strtok_r(p, "\r\n", &s);
|
2014-03-16 18:58:09 +01:00
|
|
|
while (obj && r) {
|
2014-03-08 12:07:47 +01:00
|
|
|
//debug("cSatipTuner::%s(%zu): %s", __FUNCTION__, len, r);
|
2014-03-13 17:23:55 +01:00
|
|
|
r = skipspace(r);
|
2014-03-08 12:07:47 +01:00
|
|
|
if (strstr(r, "com.ses.streamID")) {
|
2014-03-16 18:58:09 +01:00
|
|
|
int streamid = -1;
|
|
|
|
if (sscanf(r, "com.ses.streamID:%11d", &streamid) == 1)
|
|
|
|
obj->SetStreamId(streamid);
|
2014-03-08 12:07:47 +01:00
|
|
|
}
|
|
|
|
else if (strstr(r, "Session:")) {
|
2014-03-16 18:58:09 +01:00
|
|
|
int timeout = -1;
|
|
|
|
char *session = NULL;
|
|
|
|
if (sscanf(r, "Session:%m[^;];timeout=%11d", &session, &timeout) == 2)
|
2014-04-02 20:06:37 +02:00
|
|
|
obj->SetSessionTimeout(skipspace(session), timeout * 1000);
|
2014-03-16 18:58:09 +01:00
|
|
|
else if (sscanf(r, "Session:%m[^;]", &session) == 1)
|
2014-03-31 21:07:15 +02:00
|
|
|
obj->SetSessionTimeout(skipspace(session));
|
2014-03-16 18:58:09 +01:00
|
|
|
FREE_POINTER(session);
|
2014-03-08 12:07:47 +01:00
|
|
|
}
|
|
|
|
r = strtok_r(NULL, "\r\n", &s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2014-11-05 20:55:40 +01:00
|
|
|
size_t cSatipTuner::DataCallback(void *ptrP, size_t sizeP, size_t nmembP, void *dataP)
|
|
|
|
{
|
|
|
|
cSatipTuner *obj = reinterpret_cast<cSatipTuner *>(dataP);
|
|
|
|
size_t len = sizeP * nmembP;
|
|
|
|
//debug("cSatipTuner::%s(%zu)", __FUNCTION__, len);
|
|
|
|
|
|
|
|
if (obj && (len > 0)) {
|
|
|
|
char *data = strndup((char*)ptrP, len);
|
|
|
|
obj->ParseReceptionParameters(data);
|
|
|
|
FREE_POINTER(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2014-11-08 21:22:46 +01:00
|
|
|
void cSatipTuner::DataTimeoutCallback(void *objP)
|
|
|
|
{
|
|
|
|
cSatipTuner *obj = reinterpret_cast<cSatipTuner *>(objP);
|
|
|
|
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, obj->deviceM->GetId());
|
|
|
|
|
|
|
|
if (obj)
|
|
|
|
obj->reconnectM = true;
|
|
|
|
}
|
|
|
|
|
2014-11-09 01:25:47 +01:00
|
|
|
int cSatipTuner::DebugCallback(CURL *handleP, curl_infotype typeP, char *dataP, size_t sizeP, void *userPtrP)
|
2014-11-08 21:22:46 +01:00
|
|
|
{
|
2014-11-09 01:25:47 +01:00
|
|
|
cSatipTuner *obj = reinterpret_cast<cSatipTuner *>(userPtrP);
|
|
|
|
|
|
|
|
if (obj) {
|
|
|
|
switch (typeP) {
|
|
|
|
case CURLINFO_TEXT:
|
|
|
|
debug("cSatipTuner::%s(%d): RTSP INFO %.*s", __FUNCTION__, obj->deviceM->GetId(), (int)sizeP, dataP);
|
|
|
|
break;
|
|
|
|
case CURLINFO_HEADER_IN:
|
|
|
|
debug("cSatipTuner::%s(%d): RTSP HEAD <<< %.*s", __FUNCTION__, obj->deviceM->GetId(), (int)sizeP, dataP);
|
|
|
|
break;
|
|
|
|
case CURLINFO_HEADER_OUT:
|
|
|
|
debug("cSatipTuner::%s(%d): RTSP HEAD >>> %.*s", __FUNCTION__, obj->deviceM->GetId(), (int)sizeP, dataP);
|
|
|
|
break;
|
|
|
|
case CURLINFO_DATA_IN:
|
|
|
|
debug("cSatipTuner::%s(%d): RTSP DATA <<< %.*s", __FUNCTION__, obj->deviceM->GetId(), (int)sizeP, dataP);
|
|
|
|
break;
|
|
|
|
case CURLINFO_DATA_OUT:
|
|
|
|
debug("cSatipTuner::%s(%d): RTSP DATA >>> %.*s", __FUNCTION__, obj->deviceM->GetId(), (int)sizeP, dataP);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-08 21:22:46 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-08 12:07:47 +01:00
|
|
|
void cSatipTuner::Action(void)
|
|
|
|
{
|
2014-04-12 15:57:28 +02:00
|
|
|
debug("cSatipTuner::%s(): entering [device %d]", __FUNCTION__, deviceM->GetId());
|
2014-11-08 21:22:46 +01:00
|
|
|
cTimeMs rtcpTimeout(eReConnectTimeoutMs);
|
2014-03-08 12:07:47 +01:00
|
|
|
// Increase priority
|
|
|
|
SetPriority(-1);
|
|
|
|
// Do the thread loop
|
2014-11-08 21:22:46 +01:00
|
|
|
while (Running()) {
|
|
|
|
if (reconnectM) {
|
|
|
|
info("SAT>IP Device %d timed out. Reconnecting.", deviceM->GetId());
|
|
|
|
cMutexLock MutexLock(&mutexM);
|
|
|
|
if (tunedM)
|
|
|
|
Disconnect();
|
|
|
|
Connect();
|
|
|
|
reconnectM = false;
|
|
|
|
}
|
|
|
|
if (tunedM) {
|
2014-03-08 12:07:47 +01:00
|
|
|
// Update pids
|
|
|
|
UpdatePids();
|
|
|
|
// Remember the heart beat
|
|
|
|
KeepAlive();
|
2014-11-08 21:22:46 +01:00
|
|
|
// Read reception statistics via DESCRIBE
|
|
|
|
if (!pidsM.Size() && statusUpdateM.TimedOut() ) {
|
|
|
|
cMutexLock MutexLock(&mutexM);
|
|
|
|
if (RtspDescribe())
|
|
|
|
statusUpdateM.Set(eStatusUpdateTimeoutMs);
|
|
|
|
}
|
|
|
|
// Read reception statistics via RTCP
|
2014-03-08 12:07:47 +01:00
|
|
|
if (rtcpSocketM && rtcpSocketM->IsOpen()) {
|
|
|
|
unsigned char buf[1450];
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
if (rtcpSocketM->ReadApplication(buf, sizeof(buf)) > 0) {
|
|
|
|
ParseReceptionParameters((const char *)buf);
|
2014-11-08 21:22:46 +01:00
|
|
|
rtcpTimeout.Set(eReConnectTimeoutMs);
|
2014-03-08 12:07:47 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-03 20:54:19 +01:00
|
|
|
// Quirk for devices without valid reception data
|
|
|
|
if (currentServerM && currentServerM->Quirk(cSatipServer::eSatipQuirkForceLock)) {
|
|
|
|
hasLockM = true;
|
|
|
|
signalStrengthM = eDefaultSignalStrength;
|
|
|
|
signalQualityM = eDefaultSignalQuality;
|
|
|
|
}
|
2014-11-08 21:22:46 +01:00
|
|
|
if (rtcpTimeout.TimedOut())
|
|
|
|
reconnectM = true;
|
2014-03-08 12:07:47 +01:00
|
|
|
}
|
|
|
|
sleepM.Wait(10); // to avoid busy loop and reduce cpu load
|
|
|
|
}
|
2014-04-12 15:57:28 +02:00
|
|
|
debug("cSatipTuner::%s(): exiting [device %d]", __FUNCTION__, deviceM->GetId());
|
2014-03-08 12:07:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipTuner::Open(void)
|
|
|
|
{
|
2014-04-12 15:57:28 +02:00
|
|
|
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
2014-11-05 20:55:40 +01:00
|
|
|
return Connect();
|
2014-03-08 12:07:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipTuner::Close(void)
|
|
|
|
{
|
2014-04-12 15:57:28 +02:00
|
|
|
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
2014-11-05 20:55:40 +01:00
|
|
|
return Disconnect();
|
2014-03-08 12:07:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipTuner::Connect(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutexM);
|
2014-04-12 15:57:28 +02:00
|
|
|
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
2014-03-08 12:07:47 +01:00
|
|
|
|
2014-11-08 21:22:46 +01:00
|
|
|
if (isempty(*streamAddrM)) {
|
|
|
|
if (tunedM)
|
|
|
|
Disconnect();
|
|
|
|
tunedM = false;
|
|
|
|
return tunedM;
|
|
|
|
}
|
2014-03-08 12:07:47 +01:00
|
|
|
|
2014-11-08 21:22:46 +01:00
|
|
|
// Setup stream
|
|
|
|
tunedM = RtspSetup(streamParamM, rtpSocketM->Port(), rtcpSocketM->Port());
|
|
|
|
if (!tunedM)
|
|
|
|
return tunedM;
|
2014-03-08 12:07:47 +01:00
|
|
|
|
2014-11-08 21:22:46 +01:00
|
|
|
dataThreadM.Flush();
|
|
|
|
keepAliveM.Set(timeoutM);
|
2014-03-08 12:07:47 +01:00
|
|
|
|
2014-11-08 21:22:46 +01:00
|
|
|
// Start playing
|
|
|
|
UpdatePids(true);
|
|
|
|
if (nextServerM) {
|
|
|
|
cSatipDiscover::GetInstance()->UseServer(nextServerM, true);
|
|
|
|
currentServerM = nextServerM;
|
|
|
|
nextServerM = NULL;
|
2014-03-08 12:07:47 +01:00
|
|
|
}
|
|
|
|
|
2014-11-08 21:22:46 +01:00
|
|
|
return tunedM;
|
2014-03-08 12:07:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipTuner::Disconnect(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutexM);
|
2014-04-12 15:57:28 +02:00
|
|
|
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
2014-03-08 12:07:47 +01:00
|
|
|
|
2014-11-08 21:22:46 +01:00
|
|
|
// Teardown rtsp session
|
|
|
|
if (tunedM) {
|
|
|
|
RtspTeardown();
|
|
|
|
streamIdM = -1;
|
|
|
|
tunedM = false;
|
2014-03-08 12:07:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset signal parameters
|
|
|
|
hasLockM = false;
|
|
|
|
signalStrengthM = -1;
|
|
|
|
signalQualityM = -1;
|
|
|
|
|
2014-03-13 17:23:55 +01:00
|
|
|
if (currentServerM)
|
|
|
|
cSatipDiscover::GetInstance()->UseServer(currentServerM, false);
|
2014-11-05 20:55:40 +01:00
|
|
|
statusUpdateM.Set(0);
|
2014-03-31 21:07:15 +02:00
|
|
|
timeoutM = eMinKeepAliveIntervalMs;
|
2014-04-19 16:27:47 +02:00
|
|
|
addPidsM.Clear();
|
|
|
|
delPidsM.Clear();
|
2014-03-08 12:07:47 +01:00
|
|
|
|
2014-11-05 20:55:40 +01:00
|
|
|
// return always true
|
2014-03-08 12:07:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipTuner::ValidateLatestResponse(void)
|
|
|
|
{
|
2014-04-12 15:57:28 +02:00
|
|
|
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
2014-03-08 12:07:47 +01:00
|
|
|
if (handleM) {
|
|
|
|
long rc = 0;
|
|
|
|
CURLcode res = CURLE_OK;
|
|
|
|
SATIP_CURL_EASY_GETINFO(handleM, CURLINFO_RESPONSE_CODE, &rc);
|
|
|
|
if (rc == 200)
|
|
|
|
return true;
|
|
|
|
else if (rc != 0)
|
2014-04-12 15:57:28 +02:00
|
|
|
error("Tuner detected invalid status code %ld [device %d]", rc, deviceM->GetId());
|
2014-03-08 12:07:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cSatipTuner::ParseReceptionParameters(const char *paramP)
|
|
|
|
{
|
2014-04-12 15:57:28 +02:00
|
|
|
//debug("cSatipTuner::%s(%s) [device %d]", __FUNCTION__, paramP, deviceM->GetId());
|
2014-03-08 12:07:47 +01:00
|
|
|
// DVB-S2:
|
|
|
|
// ver=<major>.<minor>;src=<srcID>;tuner=<feID>,<level>,<lock>,<quality>,<frequency>,<polarisation>,<system>,<type>,<pilots>,<roll_off>,<symbol_rate>,<fec_inner>;pids=<pid0>,...,<pidn>
|
|
|
|
// DVB-T2:
|
|
|
|
// ver=1.1;tuner=<feID>,<level>,<lock>,<quality>,<freq>,<bw>,<msys>,<tmode>,<mtype>,<gi>,<fec>,<plp>,<t2id>,<sm>;pids=<pid0>,...,<pidn>
|
|
|
|
if (!isempty(paramP)) {
|
|
|
|
char *s = strdup(paramP);
|
|
|
|
char *c = strstr(s, ";tuner=");
|
|
|
|
if (c) {
|
|
|
|
int value;
|
|
|
|
|
|
|
|
// level:
|
|
|
|
// Numerical value between 0 and 255
|
|
|
|
// An incoming L-band satellite signal of
|
|
|
|
// -25dBm corresponds to 224
|
|
|
|
// -65dBm corresponds to 32
|
|
|
|
// No signal corresponds to 0
|
|
|
|
c = strstr(c, ",");
|
|
|
|
value = atoi(++c);
|
|
|
|
// Scale value to 0-100
|
|
|
|
signalStrengthM = (value >= 0) ? (value * 100 / 255) : -1;
|
|
|
|
|
|
|
|
// lock:
|
|
|
|
// lock Set to one of the following values:
|
|
|
|
// "0" the frontend is not locked
|
|
|
|
// "1" the frontend is locked
|
|
|
|
c = strstr(c, ",");
|
2014-11-08 21:22:46 +01:00
|
|
|
value = !!atoi(++c);
|
|
|
|
if (value != hasLockM)
|
|
|
|
info("Device %d %s lock", deviceM->GetId(), hasLockM ? "gained" : "lost");
|
|
|
|
hasLockM = value;
|
2014-03-08 12:07:47 +01:00
|
|
|
|
|
|
|
// quality:
|
|
|
|
// Numerical value between 0 and 15
|
|
|
|
// Lowest value corresponds to highest error rate
|
|
|
|
// The value 15 shall correspond to
|
|
|
|
// -a BER lower than 2x10-4 after Viterbi for DVB-S
|
|
|
|
// -a PER lower than 10-7 for DVB-S2
|
|
|
|
c = strstr(c, ",");
|
|
|
|
value = atoi(++c);
|
|
|
|
// Scale value to 0-100
|
|
|
|
signalQualityM = (hasLockM && (value >= 0)) ? (value * 100 / 15) : 0;
|
|
|
|
}
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-16 18:58:09 +01:00
|
|
|
void cSatipTuner::SetStreamId(int streamIdP)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutexM);
|
2014-04-12 15:57:28 +02:00
|
|
|
debug("cSatipTuner::%s(%d) [device %d]", __FUNCTION__, streamIdP, deviceM->GetId());
|
2014-03-16 18:58:09 +01:00
|
|
|
streamIdM = streamIdP;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cSatipTuner::SetSessionTimeout(const char *sessionP, int timeoutP)
|
2014-03-08 12:07:47 +01:00
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutexM);
|
2014-04-12 15:57:28 +02:00
|
|
|
debug("cSatipTuner::%s(%s, %d) [device %d]", __FUNCTION__, sessionP, timeoutP, deviceM->GetId());
|
2014-03-16 18:58:09 +01:00
|
|
|
sessionM = sessionP;
|
2014-03-31 21:07:15 +02:00
|
|
|
timeoutM = (timeoutP > eMinKeepAliveIntervalMs) ? timeoutP : eMinKeepAliveIntervalMs;
|
2014-03-08 12:07:47 +01:00
|
|
|
}
|
|
|
|
|
2014-03-13 17:23:55 +01:00
|
|
|
bool cSatipTuner::SetSource(cSatipServer *serverP, const char *parameterP, const int indexP)
|
2014-03-08 12:07:47 +01:00
|
|
|
{
|
2014-04-12 15:57:28 +02:00
|
|
|
debug("cSatipTuner::%s(%s, %d) [device %d]", __FUNCTION__, parameterP, indexP, deviceM->GetId());
|
2014-03-13 17:23:55 +01:00
|
|
|
cMutexLock MutexLock(&mutexM);
|
2014-11-08 21:22:46 +01:00
|
|
|
|
2014-11-03 20:55:54 +01:00
|
|
|
if (serverP) {
|
|
|
|
nextServerM = cSatipDiscover::GetInstance()->GetServer(serverP);
|
|
|
|
if (nextServerM && !isempty(nextServerM->Address()) && !isempty(parameterP)) {
|
|
|
|
// Update stream address and parameter
|
|
|
|
streamAddrM = nextServerM->Address();
|
|
|
|
streamParamM = parameterP;
|
|
|
|
// Reconnect
|
|
|
|
Connect();
|
|
|
|
}
|
2014-03-08 12:07:47 +01:00
|
|
|
}
|
2014-11-03 20:55:54 +01:00
|
|
|
else
|
|
|
|
Disconnect();
|
2014-11-08 21:22:46 +01:00
|
|
|
|
2014-03-08 12:07:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipTuner::SetPid(int pidP, int typeP, bool onP)
|
|
|
|
{
|
2014-11-08 21:22:46 +01:00
|
|
|
debug("cSatipTuner::%s(%d, %d, %d) [device %d]", __FUNCTION__, pidP, typeP, onP, deviceM->GetId());
|
2014-03-08 12:07:47 +01:00
|
|
|
cMutexLock MutexLock(&mutexM);
|
2014-11-08 21:22:46 +01:00
|
|
|
|
2014-04-19 16:27:47 +02:00
|
|
|
if (onP) {
|
2014-11-09 01:32:50 +01:00
|
|
|
pidsM.AddPid(pidP);
|
|
|
|
addPidsM.AddPid(pidP);
|
|
|
|
delPidsM.RemovePid(pidP);
|
2014-04-19 16:27:47 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-11-09 01:32:50 +01:00
|
|
|
pidsM.RemovePid(pidP);
|
|
|
|
delPidsM.AddPid(pidP);
|
|
|
|
addPidsM.RemovePid(pidP);
|
2014-04-19 16:27:47 +02:00
|
|
|
}
|
2014-11-08 21:22:46 +01:00
|
|
|
|
2014-03-08 12:07:47 +01:00
|
|
|
pidUpdateCacheM.Set(ePidUpdateIntervalMs);
|
2014-11-08 21:22:46 +01:00
|
|
|
|
2014-03-08 12:07:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-08 21:22:46 +01:00
|
|
|
cString cSatipTuner::GeneratePidParameter(bool allPidsP)
|
2014-03-08 12:07:47 +01:00
|
|
|
{
|
2014-11-08 21:22:46 +01:00
|
|
|
debug("cSatipTuner::%s(%d) [device %d]", __FUNCTION__, (int)allPidsP, deviceM->GetId());
|
2014-03-08 12:07:47 +01:00
|
|
|
cMutexLock MutexLock(&mutexM);
|
2014-11-08 21:22:46 +01:00
|
|
|
|
|
|
|
bool usedummy = !!(currentServerM && currentServerM->Quirk(cSatipServer::eSatipQuirkPlayPids));
|
|
|
|
cString param = "";
|
|
|
|
|
|
|
|
if (allPidsP || usedummy) {
|
|
|
|
if (usedummy && (pidsM.Size() == 1) && (pidsM[0] < 0x20))
|
|
|
|
param = cString::sprintf("%s,%d", *param, eDummyPid);
|
|
|
|
else if (pidsM.Size()) {
|
|
|
|
param = "pids=";
|
|
|
|
for (int i = 0; i < pidsM.Size(); ++i)
|
|
|
|
param = cString::sprintf("%s%s%d", *param, (i == 0 ? "" : ","), pidsM[i]);
|
2014-04-19 16:27:47 +02:00
|
|
|
}
|
2014-11-08 21:22:46 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (addPidsM.Size()) {
|
|
|
|
param = "addpids=";
|
|
|
|
for (int i = 0; i < addPidsM.Size(); ++i)
|
|
|
|
param = cString::sprintf("%s%s%d", *param, (i == 0 ? "" : ","), addPidsM[i]);
|
2014-04-19 16:27:47 +02:00
|
|
|
addPidsM.Clear();
|
2014-11-08 21:22:46 +01:00
|
|
|
}
|
|
|
|
if (delPidsM.Size()) {
|
|
|
|
param = cString::sprintf("%s%sdelpids=", *param, (isempty(*param) ? "" : "&"));
|
|
|
|
for (int i = 0; i < delPidsM.Size(); ++i)
|
|
|
|
param = cString::sprintf("%s%s%d", *param, (i == 0 ? "" : ","), delPidsM[i]);
|
2014-04-19 16:27:47 +02:00
|
|
|
delPidsM.Clear();
|
|
|
|
}
|
2014-11-08 21:22:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return param;
|
|
|
|
}
|
2014-03-08 12:07:47 +01:00
|
|
|
|
2014-11-08 21:22:46 +01:00
|
|
|
bool cSatipTuner::UpdatePids(bool forceP)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutexM);
|
|
|
|
//debug("cSatipTuner::%s(%d) [device %d]", __FUNCTION__, (int)forceP, deviceM->GetId());
|
|
|
|
|
|
|
|
if (((forceP && pidsM.Size()) ||
|
|
|
|
(pidUpdateCacheM.TimedOut() && (addPidsM.Size() || delPidsM.Size())) ) &&
|
|
|
|
tunedM && handleM && !isempty(*streamAddrM)) {
|
|
|
|
// Disable RTP Timeout while sending PLAY Command
|
|
|
|
dataThreadM.SetTimeout(-1, &DataTimeoutCallback, this);
|
|
|
|
if (RtspPlay(*GeneratePidParameter(forceP))) {
|
|
|
|
addPidsM.Clear();
|
|
|
|
delPidsM.Clear();
|
|
|
|
dataThreadM.SetTimeout(eReConnectTimeoutMs, &DataTimeoutCallback, this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Disconnect();
|
2014-03-08 12:07:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipTuner::KeepAlive(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutexM);
|
|
|
|
if (tunedM && handleM && keepAliveM.TimedOut()) {
|
2014-04-12 15:57:28 +02:00
|
|
|
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
2014-11-08 21:22:46 +01:00
|
|
|
if (RtspOptions()) {
|
|
|
|
keepAliveM.Set(timeoutM);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipTuner::RtspInitialize()
|
|
|
|
{
|
|
|
|
// Initialize the curl session
|
|
|
|
if (!handleM) {
|
|
|
|
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
|
|
|
|
|
|
|
handleM = curl_easy_init();
|
|
|
|
CURLcode res = CURLE_OK;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Verbose output
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_DEBUGFUNCTION, RtspDebugCallback);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_VERBOSE, 1L);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_DEBUGDATA, this);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// No progress meter and no signaling
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_NOPROGRESS, 1L);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_NOSIGNAL, 1L);
|
|
|
|
|
|
|
|
// Set timeouts
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_TIMEOUT_MS, (long)eConnectTimeoutMs);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_CONNECTTIMEOUT_MS, (long)eConnectTimeoutMs);
|
|
|
|
|
|
|
|
// Set user-agent
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_USERAGENT, *cString::sprintf("vdr-%s/%s (device %d)", PLUGIN_NAME_I18N, VERSION, deviceM->GetId()));
|
|
|
|
|
|
|
|
return !!handleM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipTuner::RtspTerminate()
|
|
|
|
{
|
|
|
|
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
|
|
|
if (handleM) {
|
|
|
|
curl_easy_cleanup(handleM);
|
|
|
|
handleM = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipTuner::RtspSetup(const char *paramP, int rtpPortP, int rtcpPortP)
|
|
|
|
{
|
|
|
|
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
|
|
|
if (handleM) {
|
|
|
|
cString uri;
|
|
|
|
CURLcode res = CURLE_OK;
|
|
|
|
|
|
|
|
// set URL, this will not change
|
|
|
|
// Note that we are unescaping the adress here and do NOT store it for future use.
|
|
|
|
char *p = curl_easy_unescape(handleM, *streamAddrM, 0, NULL);
|
|
|
|
cString url = cString::sprintf("rtsp://%s/", p);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_URL, *url);
|
|
|
|
curl_free(p);
|
|
|
|
|
|
|
|
if (streamIdM >= 0)
|
|
|
|
uri = cString::sprintf("rtsp://%s/stream=%d?%s", *streamAddrM, streamIdM, paramP);
|
|
|
|
else
|
|
|
|
uri = cString::sprintf("rtsp://%s/?%s", *streamAddrM, paramP);
|
|
|
|
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, *uri);
|
|
|
|
cString transport = cString::sprintf("RTP/AVP;unicast;client_port=%d-%d", rtpPortP, rtcpPortP);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_TRANSPORT, *transport);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_SETUP);
|
|
|
|
// Set header callback for catching the session and timeout
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_HEADERFUNCTION, cSatipTuner::HeaderCallback);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEHEADER, this);
|
|
|
|
SATIP_CURL_EASY_PERFORM(handleM);
|
|
|
|
// Session id is now known - disable header parsing
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_HEADERFUNCTION, NULL);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEHEADER, NULL);
|
|
|
|
|
|
|
|
// Validate session id
|
|
|
|
if (streamIdM < 0) {
|
|
|
|
error("Internal Error: No session id received [device %d]", deviceM->GetId());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// For some SATIP boxes e.g. GSSBOX and Triax TSS 400 we need to strip the
|
|
|
|
// leading '0' of the sessionID.
|
|
|
|
if (nextServerM && nextServerM->Quirk(cSatipServer::eSatipQuirkSessionId) && !isempty(*sessionM) && startswith(*sessionM, "0")) {
|
|
|
|
debug("cSatipTuner::%s(): session id quirk [device %d]", __FUNCTION__, deviceM->GetId());
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_SESSION_ID, SkipZeroes(*sessionM));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValidateLatestResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipTuner::RtspTeardown(void)
|
|
|
|
{
|
|
|
|
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
|
|
|
if (handleM) {
|
2014-03-08 12:07:47 +01:00
|
|
|
CURLcode res = CURLE_OK;
|
|
|
|
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
|
|
|
|
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, *uri);
|
2014-11-08 21:22:46 +01:00
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_TEARDOWN);
|
2014-03-08 12:07:47 +01:00
|
|
|
SATIP_CURL_EASY_PERFORM(handleM);
|
2014-11-08 21:22:46 +01:00
|
|
|
|
|
|
|
// Reset data we have about the session
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_SESSION_ID, NULL);
|
|
|
|
streamIdM = -1;
|
|
|
|
sessionM = "";
|
|
|
|
timeoutM = eMinKeepAliveIntervalMs;
|
|
|
|
|
|
|
|
return ValidateLatestResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipTuner::RtspPlay(const char *paramP)
|
|
|
|
{
|
|
|
|
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
|
|
|
if (handleM) {
|
|
|
|
cString uri;
|
|
|
|
CURLcode res = CURLE_OK;
|
|
|
|
|
|
|
|
if (paramP)
|
|
|
|
uri = cString::sprintf("rtsp://%s/stream=%d?%s", *streamAddrM, streamIdM, paramP);
|
2014-03-08 12:07:47 +01:00
|
|
|
else
|
2014-11-08 21:22:46 +01:00
|
|
|
uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
|
|
|
|
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, *uri);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_PLAY);
|
|
|
|
SATIP_CURL_EASY_PERFORM(handleM);
|
2014-03-08 12:07:47 +01:00
|
|
|
|
2014-11-08 21:22:46 +01:00
|
|
|
return ValidateLatestResponse();
|
2014-03-08 12:07:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-08 21:22:46 +01:00
|
|
|
bool cSatipTuner::RtspOptions(void)
|
2014-11-05 20:55:40 +01:00
|
|
|
{
|
2014-11-08 21:22:46 +01:00
|
|
|
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
|
|
|
if (handleM) {
|
|
|
|
CURLcode res = CURLE_OK;
|
|
|
|
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
|
|
|
|
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, *uri);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_OPTIONS);
|
|
|
|
SATIP_CURL_EASY_PERFORM(handleM);
|
|
|
|
|
|
|
|
return ValidateLatestResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipTuner::RtspDescribe(void)
|
|
|
|
{
|
|
|
|
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
|
|
|
if (handleM) {
|
2014-11-05 20:55:40 +01:00
|
|
|
CURLcode res = CURLE_OK;
|
|
|
|
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
|
|
|
|
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_STREAM_URI, *uri);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_RTSP_REQUEST, (long)CURL_RTSPREQ_DESCRIBE);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEFUNCTION, cSatipTuner::DataCallback);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEDATA, this);
|
|
|
|
SATIP_CURL_EASY_PERFORM(handleM);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEFUNCTION, NULL);
|
|
|
|
SATIP_CURL_EASY_SETOPT(handleM, CURLOPT_WRITEDATA, NULL);
|
2014-11-08 21:22:46 +01:00
|
|
|
|
2014-11-05 20:55:40 +01:00
|
|
|
if (ValidateLatestResponse())
|
2014-11-08 21:22:46 +01:00
|
|
|
return true;
|
2014-11-05 20:55:40 +01:00
|
|
|
|
2014-11-08 21:22:46 +01:00
|
|
|
Disconnect();
|
2014-11-05 20:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-08 12:07:47 +01:00
|
|
|
int cSatipTuner::SignalStrength(void)
|
|
|
|
{
|
2014-04-12 15:57:28 +02:00
|
|
|
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
2014-03-08 12:07:47 +01:00
|
|
|
return signalStrengthM;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cSatipTuner::SignalQuality(void)
|
|
|
|
{
|
2014-04-12 15:57:28 +02:00
|
|
|
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
2014-03-08 12:07:47 +01:00
|
|
|
return signalQualityM;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cSatipTuner::HasLock(void)
|
|
|
|
{
|
2014-04-12 15:57:28 +02:00
|
|
|
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
2014-03-08 12:07:47 +01:00
|
|
|
return tunedM && hasLockM;
|
|
|
|
}
|
|
|
|
|
|
|
|
cString cSatipTuner::GetSignalStatus(void)
|
|
|
|
{
|
2014-04-12 15:57:28 +02:00
|
|
|
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
2014-03-08 12:07:47 +01:00
|
|
|
return cString::sprintf("lock=%d strength=%d quality=%d", HasLock(), SignalStrength(), SignalQuality());
|
|
|
|
}
|
|
|
|
|
|
|
|
cString cSatipTuner::GetInformation(void)
|
|
|
|
{
|
2014-04-12 15:57:28 +02:00
|
|
|
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceM->GetId());
|
2014-03-08 12:07:47 +01:00
|
|
|
return tunedM ? cString::sprintf("rtsp://%s/?%s [stream=%d]", *streamAddrM, *streamParamM, streamIdM) : "connection failed";
|
|
|
|
}
|