vdr-plugin-satip/tuner.c

401 lines
12 KiB
C
Raw Normal View History

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"
#include "discover.h"
#include "poller.h"
2014-03-08 12:07:47 +01:00
#include "tuner.h"
cSatipTuner::cSatipTuner(cSatipDeviceIf &deviceP, unsigned int packetLenP)
2014-11-10 22:31:30 +01:00
: cThread(cString::sprintf("SAT>IP %d tuner", deviceP.GetId())),
2014-03-08 12:07:47 +01:00
sleepM(),
deviceM(&deviceP),
2014-11-10 22:31:30 +01:00
deviceIdM(deviceP.GetId()),
2014-11-09 11:51:52 +01:00
rtspM(new cSatipRtsp(*this)),
rtpM(new cSatipRtp(deviceP, packetLenP)),
rtcpM(new cSatipRtcp(*this, 1500)),
2014-03-08 12:07:47 +01:00
streamAddrM(""),
streamParamM(""),
currentServerM(NULL),
nextServerM(NULL),
2014-03-08 12:07:47 +01:00
mutexM(),
keepAliveM(),
statusUpdateM(),
2014-03-08 12:07:47 +01:00
pidUpdateCacheM(),
sessionM(""),
2014-03-31 21:07:15 +02:00
timeoutM(eMinKeepAliveIntervalMs),
2014-03-08 12:07:47 +01:00
openedM(false),
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()
{
debug("cSatipTuner::%s(%d) [device %d]", __FUNCTION__, packetLenP, deviceIdM);
2014-11-09 11:51:52 +01:00
// Open sockets
int i = 100;
while (i-- > 0) {
if (rtpM->Open(0) && rtcpM->Open(rtpM->Port() + 1))
2014-11-09 11:51:52 +01:00
break;
rtpM->Close();
rtcpM->Close();
2014-11-09 11:51:52 +01:00
}
if ((rtpM->Port() <= 0) || (rtcpM->Port() <= 0)) {
2014-11-09 11:51:52 +01:00
error("Cannot open required RTP/RTCP ports [device %d]", deviceIdM);
}
// Must be done after socket initialization!
cSatipPoller::GetInstance()->Register(*rtpM);
cSatipPoller::GetInstance()->Register(*rtcpM);
2014-11-09 19:32:08 +01:00
2014-03-08 12:07:47 +01:00
// Start thread
Start();
}
cSatipTuner::~cSatipTuner()
{
2014-11-09 11:51:52 +01:00
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
2014-03-08 12:07:47 +01:00
// Stop thread
sleepM.Signal();
if (Running())
Cancel(3);
Close();
2014-11-09 11:51:52 +01:00
// Close the listening sockets
cSatipPoller::GetInstance()->Unregister(*rtcpM);
cSatipPoller::GetInstance()->Unregister(*rtpM);
rtcpM->Close();
rtpM->Close();
2014-11-09 11:51:52 +01:00
2014-03-08 12:07:47 +01:00
// Free allocated memory
DELETENULL(rtpM);
DELETENULL(rtcpM);
2014-11-10 22:31:30 +01:00
DELETENULL(rtspM);
2014-03-08 12:07:47 +01:00
}
void cSatipTuner::Action(void)
{
2014-11-09 11:51:52 +01:00
debug("cSatipTuner::%s(): entering [device %d]", __FUNCTION__, deviceIdM);
2014-03-25 22:50:30 +01:00
cTimeMs timeout(eReConnectTimeoutMs);
2014-03-08 12:07:47 +01:00
// Do the thread loop
while (Running()) {
// Update pids
UpdatePids();
// Remember the heart beat
KeepAlive();
// Read reception statistics via DESCRIBE and RTCP
if (ReadReceptionStatus()) {
// Quirk for devices without valid reception data
if (currentServerM && currentServerM->Quirk(cSatipServer::eSatipQuirkForceLock)) {
hasLockM = true;
signalStrengthM = eDefaultSignalStrength;
signalQualityM = eDefaultSignalQuality;
}
}
// Reconnect if necessary
if (openedM && timeout.TimedOut()) {
Disconnect();
Connect();
timeout.Set(eReConnectTimeoutMs);
2014-03-08 12:07:47 +01:00
}
sleepM.Wait(100); // to avoid busy loop and reduce cpu load
2014-03-08 12:07:47 +01:00
}
2014-11-09 11:51:52 +01:00
debug("cSatipTuner::%s(): exiting [device %d]", __FUNCTION__, deviceIdM);
2014-03-08 12:07:47 +01:00
}
bool cSatipTuner::Open(void)
{
2014-11-09 11:51:52 +01:00
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
return Connect();
2014-03-08 12:07:47 +01:00
}
bool cSatipTuner::Close(void)
{
2014-11-09 11:51:52 +01:00
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
return Disconnect();
2014-03-08 12:07:47 +01:00
}
bool cSatipTuner::Connect(void)
{
cMutexLock MutexLock(&mutexM);
2014-11-09 11:51:52 +01:00
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
2014-03-08 12:07:47 +01:00
2014-11-09 11:51:52 +01:00
if (!isempty(*streamAddrM)) {
cString connectionUri = cString::sprintf("rtsp://%s", *streamAddrM);
cString uri = cString::sprintf("%s/?%s", *connectionUri, *streamParamM);
2014-03-08 12:07:47 +01:00
// Just retune
if (tunedM && (streamIdM >= 0)) {
2014-11-09 11:51:52 +01:00
debug("cSatipTuner::%s(): retune [device %d]", __FUNCTION__, deviceIdM);
2014-04-19 16:27:47 +02:00
keepAliveM.Set(0);
KeepAlive();
2014-03-08 12:07:47 +01:00
// Flush any old content
rtpM->Flush();
openedM = rtspM->Setup(*uri, rtpM->Port(), rtcpM->Port());
return openedM;
2014-03-08 12:07:47 +01:00
}
2014-03-27 22:27:38 +01:00
keepAliveM.Set(timeoutM);
2014-11-09 19:32:08 +01:00
openedM = rtspM->Options(*connectionUri);
2014-11-09 11:51:52 +01:00
if (openedM) {
if (nextServerM && nextServerM->Quirk(cSatipServer::eSatipQuirkSessionId))
rtspM->SetSession(SkipZeroes(*sessionM));
if (rtspM->Setup(*uri, rtpM->Port(), rtcpM->Port())) {
2014-11-09 19:32:08 +01:00
tunedM = true;
UpdatePids(true);
if (nextServerM) {
cSatipDiscover::GetInstance()->UseServer(nextServerM, true);
currentServerM = nextServerM;
nextServerM = NULL;
}
2014-11-09 11:51:52 +01:00
}
2014-11-09 19:32:08 +01:00
else
openedM = false;
}
2014-03-08 12:07:47 +01:00
return openedM;
2014-03-08 12:07:47 +01:00
}
openedM = false;
return openedM;
2014-03-08 12:07:47 +01:00
}
bool cSatipTuner::Disconnect(void)
{
cMutexLock MutexLock(&mutexM);
2014-11-09 11:51:52 +01:00
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
2014-03-08 12:07:47 +01:00
2014-11-10 22:31:30 +01:00
if (openedM && !isempty(*streamAddrM)) {
2014-11-09 11:51:52 +01:00
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
rtspM->Teardown(*uri);
2014-03-08 12:07:47 +01:00
}
// Reset signal parameters
hasLockM = false;
signalStrengthM = -1;
signalQualityM = -1;
if (currentServerM)
cSatipDiscover::GetInstance()->UseServer(currentServerM, false);
2014-11-10 22:31:30 +01:00
openedM = false;
2014-03-08 12:07:47 +01:00
tunedM = false;
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
// return always true
2014-03-08 12:07:47 +01:00
return true;
}
void cSatipTuner::ParseReceptionParameters(u_char *bufferP, int lengthP)
2014-03-08 12:07:47 +01:00
{
//debug("cSatipTuner::%s(%s, %d) [device %d]", __FUNCTION__, bufferP, lengthP, deviceIdM);
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 (lengthP > 0) {
char *s = strdup((char *)bufferP);
2014-03-08 12:07:47 +01:00
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-10 22:31:30 +01:00
hasLockM = !!atoi(++c);
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);
}
}
void cSatipTuner::SetStreamId(int streamIdP)
{
cMutexLock MutexLock(&mutexM);
2014-11-09 11:51:52 +01:00
debug("cSatipTuner::%s(%d) [device %d]", __FUNCTION__, streamIdP, deviceIdM);
streamIdM = streamIdP;
}
void cSatipTuner::SetSessionTimeout(const char *sessionP, int timeoutP)
2014-03-08 12:07:47 +01:00
{
cMutexLock MutexLock(&mutexM);
2014-11-09 11:51:52 +01:00
debug("cSatipTuner::%s(%s, %d) [device %d]", __FUNCTION__, sessionP, timeoutP, deviceIdM);
sessionM = sessionP;
2014-03-31 21:07:15 +02:00
timeoutM = (timeoutP > eMinKeepAliveIntervalMs) ? timeoutP : eMinKeepAliveIntervalMs;
2014-03-08 12:07:47 +01:00
}
2014-11-09 11:51:52 +01:00
int cSatipTuner::GetId(void)
{
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
return deviceIdM;
}
bool cSatipTuner::SetSource(cSatipServer *serverP, const char *parameterP, const int indexP)
2014-03-08 12:07:47 +01:00
{
2014-11-09 11:51:52 +01:00
debug("cSatipTuner::%s(%s, %d) [device %d]", __FUNCTION__, parameterP, indexP, deviceIdM);
cMutexLock MutexLock(&mutexM);
if (serverP) {
nextServerM = cSatipDiscover::GetInstance()->GetServer(serverP);
2014-11-09 19:32:08 +01:00
if (rtspM && nextServerM && !isempty(nextServerM->Address()) && !isempty(parameterP)) {
// Update stream address and parameter
2014-11-09 11:51:52 +01:00
streamAddrM = rtspM->RtspUnescapeString(nextServerM->Address());
2014-11-09 19:32:08 +01:00
streamParamM = rtspM->RtspUnescapeString(parameterP);
// Reconnect
Connect();
}
2014-03-08 12:07:47 +01:00
}
else
Disconnect();
sleepM.Signal();
2014-03-08 12:07:47 +01:00
return true;
}
bool cSatipTuner::SetPid(int pidP, int typeP, bool onP)
{
2014-11-09 11:51:52 +01:00
//debug("cSatipTuner::%s(%d, %d, %d) [device %d]", __FUNCTION__, pidP, typeP, onP, deviceIdM);
2014-03-08 12:07:47 +01:00
cMutexLock MutexLock(&mutexM);
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-03-08 12:07:47 +01:00
pidUpdateCacheM.Set(ePidUpdateIntervalMs);
sleepM.Signal();
2014-03-08 12:07:47 +01:00
return true;
}
2014-04-19 16:27:47 +02:00
bool cSatipTuner::UpdatePids(bool forceP)
2014-03-08 12:07:47 +01:00
{
cMutexLock MutexLock(&mutexM);
2014-04-19 16:27:47 +02:00
if (((forceP && pidsM.Size()) || (pidUpdateCacheM.TimedOut() && (addPidsM.Size() || delPidsM.Size()))) &&
2014-11-09 11:51:52 +01:00
tunedM && !isempty(*streamAddrM) && (streamIdM > 0)) {
2014-04-19 16:27:47 +02:00
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
bool usedummy = !!(currentServerM && currentServerM->Quirk(cSatipServer::eSatipQuirkPlayPids));
if (forceP || usedummy) {
2014-04-19 16:27:47 +02:00
if (pidsM.Size()) {
uri = cString::sprintf("%s?pids=", *uri);
for (int i = 0; i < pidsM.Size(); ++i)
uri = cString::sprintf("%s%d%s", *uri, pidsM[i], (i == (pidsM.Size() - 1)) ? "" : ",");
}
if (usedummy && (pidsM.Size() == 1) && (pidsM[0] < 0x20))
uri = cString::sprintf("%s,%d", *uri, eDummyPid);
2014-04-19 16:27:47 +02:00
}
else {
if (addPidsM.Size()) {
uri = cString::sprintf("%s?addpids=", *uri);
for (int i = 0; i < addPidsM.Size(); ++i)
uri = cString::sprintf("%s%d%s", *uri, addPidsM[i], (i == (addPidsM.Size() - 1)) ? "" : ",");
}
if (delPidsM.Size()) {
uri = cString::sprintf("%s%sdelpids=", *uri, addPidsM.Size() ? "&" : "?");
for (int i = 0; i < delPidsM.Size(); ++i)
uri = cString::sprintf("%s%d%s", *uri, delPidsM[i], (i == (delPidsM.Size() - 1)) ? "" : ",");
}
}
2014-11-09 11:51:52 +01:00
if (rtspM->Play(*uri)) {
2014-04-19 16:27:47 +02:00
addPidsM.Clear();
delPidsM.Clear();
2014-11-09 11:51:52 +01:00
return true;
2014-04-19 16:27:47 +02:00
}
2014-11-09 11:51:52 +01:00
Disconnect();
2014-03-08 12:07:47 +01:00
}
return false;
}
bool cSatipTuner::KeepAlive(void)
{
cMutexLock MutexLock(&mutexM);
2014-11-09 11:51:52 +01:00
if (tunedM && keepAliveM.TimedOut()) {
2014-03-08 12:07:47 +01:00
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
2014-11-09 19:32:08 +01:00
keepAliveM.Set(timeoutM);
if (rtspM->Options(*uri))
2014-11-09 11:51:52 +01:00
return true;
Disconnect();
2014-03-08 12:07:47 +01:00
}
return false;
}
bool cSatipTuner::ReadReceptionStatus(void)
{
cMutexLock MutexLock(&mutexM);
2014-11-09 11:51:52 +01:00
if (tunedM && !pidsM.Size() && statusUpdateM.TimedOut() ) {
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
2014-11-09 11:51:52 +01:00
if (rtspM->Describe(*uri)) {
statusUpdateM.Set(eStatusUpdateTimeoutMs);
2014-11-09 11:51:52 +01:00
return true;
}
Disconnect();
}
return false;
}
2014-03-08 12:07:47 +01:00
int cSatipTuner::SignalStrength(void)
{
2014-11-09 11:51:52 +01:00
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
2014-03-08 12:07:47 +01:00
return signalStrengthM;
}
int cSatipTuner::SignalQuality(void)
{
2014-11-09 11:51:52 +01:00
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
2014-03-08 12:07:47 +01:00
return signalQualityM;
}
bool cSatipTuner::HasLock(void)
{
2014-11-09 11:51:52 +01:00
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
2014-03-08 12:07:47 +01:00
return tunedM && hasLockM;
}
cString cSatipTuner::GetSignalStatus(void)
{
2014-11-09 11:51:52 +01:00
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
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-11-09 11:51:52 +01:00
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
2014-03-08 12:07:47 +01:00
return tunedM ? cString::sprintf("rtsp://%s/?%s [stream=%d]", *streamAddrM, *streamParamM, streamIdM) : "connection failed";
}