1
0
mirror of https://github.com/rofafor/vdr-plugin-satip.git synced 2023-10-10 13:37:42 +02:00

Implemented a preliminary state machine for tuner.

This commit is contained in:
Rolf Ahrenberg 2014-11-15 20:15:00 +02:00
parent e51650cd0a
commit 0431806f24
4 changed files with 144 additions and 89 deletions

View File

@ -82,3 +82,4 @@ VDR Plugin 'satip' Revision History
- Fixed EIT scan (Thanks to Stefan Schallenberg). - Fixed EIT scan (Thanks to Stefan Schallenberg).
- Refactored input thread to increase performance. - Refactored input thread to increase performance.
- Added new STAT command into the SVDRP interface. - Added new STAT command into the SVDRP interface.
- Refactored tuner implementation.

7
rtsp.c
View File

@ -253,16 +253,17 @@ bool cSatipRtsp::Teardown(const char *uriP)
bool cSatipRtsp::ValidateLatestResponse(void) bool cSatipRtsp::ValidateLatestResponse(void)
{ {
debug("cSatipRtsp::%s(%d)", __FUNCTION__, tunerIdM); bool result = false;
if (handleM) { if (handleM) {
long rc = 0; long rc = 0;
CURLcode res = CURLE_OK; CURLcode res = CURLE_OK;
SATIP_CURL_EASY_GETINFO(handleM, CURLINFO_RESPONSE_CODE, &rc); SATIP_CURL_EASY_GETINFO(handleM, CURLINFO_RESPONSE_CODE, &rc);
if (rc == 200) if (rc == 200)
return true; result = true;
else if (rc != 0) else if (rc != 0)
error("Tuner detected invalid status code %ld [device %d]", rc, tunerIdM); error("Tuner detected invalid status code %ld [device %d]", rc, tunerIdM);
} }
debug("cSatipRtsp::%s(%d): %s", __FUNCTION__, tunerIdM, result ? "ok" : "failed");
return false; return result;
} }

209
tuner.c
View File

@ -30,10 +30,9 @@ cSatipTuner::cSatipTuner(cSatipDeviceIf &deviceP, unsigned int packetLenP)
statusUpdateM(), statusUpdateM(),
pidUpdateCacheM(), pidUpdateCacheM(),
sessionM(""), sessionM(""),
tunerStatusM(tsIdle),
fdM(epoll_create(eMaxFileDescriptors)), fdM(epoll_create(eMaxFileDescriptors)),
timeoutM(eMinKeepAliveIntervalMs), timeoutM(eMinKeepAliveIntervalMs),
openedM(false),
tunedM(false),
hasLockM(false), hasLockM(false),
signalStrengthM(-1), signalStrengthM(-1),
signalQualityM(-1), signalQualityM(-1),
@ -89,6 +88,9 @@ cSatipTuner::cSatipTuner(cSatipDeviceIf &deviceP, unsigned int packetLenP)
cSatipTuner::~cSatipTuner() cSatipTuner::~cSatipTuner()
{ {
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM); debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
tunerStatusM = tsIdle;
// Stop thread // Stop thread
sleepM.Signal(); sleepM.Signal();
if (Running()) if (Running())
@ -120,7 +122,7 @@ cSatipTuner::~cSatipTuner()
void cSatipTuner::Action(void) void cSatipTuner::Action(void)
{ {
debug("cSatipTuner::%s(): entering [device %d]", __FUNCTION__, deviceIdM); debug("cSatipTuner::%s(): entering [device %d]", __FUNCTION__, deviceIdM);
cTimeMs timeout(eReConnectTimeoutMs); cTimeMs reconnection(eConnectTimeoutMs);
// Increase priority // Increase priority
SetPriority(-1); SetPriority(-1);
// Do the thread loop // Do the thread loop
@ -129,8 +131,8 @@ void cSatipTuner::Action(void)
int nfds = epoll_wait(fdM, events, eMaxFileDescriptors, eReadTimeoutMs); int nfds = epoll_wait(fdM, events, eMaxFileDescriptors, eReadTimeoutMs);
switch (nfds) { switch (nfds) {
default: default:
reconnection.Set(eConnectTimeoutMs);
for (int i = 0; i < nfds; ++i) { for (int i = 0; i < nfds; ++i) {
timeout.Set(eReConnectTimeoutMs);
if (events[i].data.fd == rtpSocketM->Fd()) { if (events[i].data.fd == rtpSocketM->Fd()) {
// Read data // Read data
int length = rtpSocketM->ReadVideo(packetBufferM, min(deviceM->CheckData(), packetBufferLenM)); int length = rtpSocketM->ReadVideo(packetBufferM, min(deviceM->CheckData(), packetBufferLenM));
@ -146,27 +148,66 @@ void cSatipTuner::Action(void)
ParseReceptionParameters((const char *)buf); ParseReceptionParameters((const char *)buf);
} }
} }
// fall through! // fall through into epoll timeout
case 0: case 0:
// Update pids switch (tunerStatusM) {
UpdatePids(); case tsIdle:
// Remember the heart beat //debug("cSatipTuner::%s(): tsIdle [device %d]", __FUNCTION__, deviceIdM);
KeepAlive(); break;
// Read reception statistics via DESCRIBE and RTCP case tsRelease:
if (ReadReceptionStatus()) { //debug("cSatipTuner::%s(): tsRelease [device %d]", __FUNCTION__, deviceIdM);
// Quirk for devices without valid reception data Disconnect();
if (currentServerM && currentServerM->Quirk(cSatipServer::eSatipQuirkForceLock)) { tunerStatusM = tsIdle;
hasLockM = true; break;
signalStrengthM = eDefaultSignalStrength; case tsSet:
signalQualityM = eDefaultSignalQuality; //debug("cSatipTuner::%s(): tsSet [device %d]", __FUNCTION__, deviceIdM);
} reconnection.Set(eConnectTimeoutMs);
} Disconnect();
// Reconnect if necessary if (Connect()) {
if (openedM && timeout.TimedOut()) { tunerStatusM = tsTuned;
Disconnect(); UpdatePids(true);
Connect(); }
timeout.Set(eReConnectTimeoutMs); else
} tunerStatusM = tsIdle;
break;
case tsTuned:
//debug("cSatipTuner::%s(): tsTuned [device %d]", __FUNCTION__, deviceIdM);
reconnection.Set(eConnectTimeoutMs);
// Read reception statistics via DESCRIBE and RTCP
if (hasLockM || ReadReceptionStatus()) {
// Quirk for devices without valid reception data
if (currentServerM && currentServerM->Quirk(cSatipServer::eSatipQuirkForceLock)) {
hasLockM = true;
signalStrengthM = eDefaultSignalStrength;
signalQualityM = eDefaultSignalQuality;
}
if (hasLockM)
tunerStatusM = tsLocked;
}
break;
case tsLocked:
//debug("cSatipTuner::%s(): tsLocked [device %d]", __FUNCTION__, deviceIdM);
tunerStatusM = tsLocked;
if (!UpdatePids()) {
debug("cSatipTuner::%s(): pid update failed - re-tuning [device %d]", __FUNCTION__, deviceIdM);
tunerStatusM = tsSet;
break;
}
if (!KeepAlive()) {
debug("cSatipTuner::%s(): keep-alive failed - re-tuning [device %d]", __FUNCTION__, deviceIdM);
tunerStatusM = tsSet;
break;
}
if (reconnection.TimedOut()) {
debug("cSatipTuner::%s(): connection timeout - re-tuning [device %d]", __FUNCTION__, deviceIdM);
tunerStatusM = tsSet;
break;
}
break;
default:
error("Unknown tuner status %d", tunerStatusM);
break;
}
break; break;
case -1: case -1:
error("epoll_wait() failed"); error("epoll_wait() failed");
@ -178,14 +219,18 @@ void cSatipTuner::Action(void)
bool cSatipTuner::Open(void) bool cSatipTuner::Open(void)
{ {
cMutexLock MutexLock(&mutexM);
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM); debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
return Connect(); tunerStatusM = tsSet;
return true;
} }
bool cSatipTuner::Close(void) bool cSatipTuner::Close(void)
{ {
cMutexLock MutexLock(&mutexM);
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM); debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
return Disconnect(); tunerStatusM = tsRelease;
return true;
} }
bool cSatipTuner::Connect(void) bool cSatipTuner::Connect(void)
@ -197,39 +242,29 @@ bool cSatipTuner::Connect(void)
cString connectionUri = cString::sprintf("rtsp://%s", *streamAddrM); cString connectionUri = cString::sprintf("rtsp://%s", *streamAddrM);
cString uri = cString::sprintf("%s/?%s", *connectionUri, *streamParamM); cString uri = cString::sprintf("%s/?%s", *connectionUri, *streamParamM);
// Just retune // Just retune
if (tunedM && (streamIdM >= 0)) { if ((tunerStatusM >= tsTuned) && (streamIdM >= 0) && rtpSocketM && rtcpSocketM) {
debug("cSatipTuner::%s(): retune [device %d]", __FUNCTION__, deviceIdM); debug("cSatipTuner::%s(): retune [device %d]", __FUNCTION__, deviceIdM);
keepAliveM.Set(0); KeepAlive(true);
KeepAlive();
// Flush any old content // Flush any old content
if (rtpSocketM) rtpSocketM->Flush();
rtpSocketM->Flush(); return rtspM->Setup(*uri, rtpSocketM->Port(), rtcpSocketM->Port());
openedM = rtspM->Setup(*uri, rtpSocketM->Port(), rtcpSocketM->Port());
return openedM;
} }
keepAliveM.Set(timeoutM); keepAliveM.Set(timeoutM);
openedM = rtspM->Options(*connectionUri); if (rtspM->Options(*connectionUri)) {
if (openedM) {
if (nextServerM && nextServerM->Quirk(cSatipServer::eSatipQuirkSessionId)) if (nextServerM && nextServerM->Quirk(cSatipServer::eSatipQuirkSessionId))
rtspM->SetSession(SkipZeroes(*sessionM)); rtspM->SetSession(SkipZeroes(*sessionM));
if (rtspM->Setup(*uri, rtpSocketM->Port(), rtcpSocketM->Port())) { if (rtspM->Setup(*uri, rtpSocketM->Port(), rtcpSocketM->Port())) {
tunedM = true;
UpdatePids(true);
if (nextServerM) { if (nextServerM) {
cSatipDiscover::GetInstance()->UseServer(nextServerM, true); cSatipDiscover::GetInstance()->UseServer(nextServerM, true);
currentServerM = nextServerM; currentServerM = nextServerM;
nextServerM = NULL; nextServerM = NULL;
} }
} }
else return true;
openedM = false;
} }
return openedM;
} }
openedM = false; return false;
return openedM;
} }
bool cSatipTuner::Disconnect(void) bool cSatipTuner::Disconnect(void)
@ -237,10 +272,11 @@ bool cSatipTuner::Disconnect(void)
cMutexLock MutexLock(&mutexM); cMutexLock MutexLock(&mutexM);
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM); debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
if (openedM && !isempty(*streamAddrM)) { if ((tunerStatusM != tsIdle) && !isempty(*streamAddrM) && (streamIdM >= 0)) {
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM); cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
rtspM->Teardown(*uri); rtspM->Teardown(*uri);
} }
tunerStatusM = tsIdle;
// Reset signal parameters // Reset signal parameters
hasLockM = false; hasLockM = false;
@ -249,8 +285,6 @@ bool cSatipTuner::Disconnect(void)
if (currentServerM) if (currentServerM)
cSatipDiscover::GetInstance()->UseServer(currentServerM, false); cSatipDiscover::GetInstance()->UseServer(currentServerM, false);
openedM = false;
tunedM = false;
statusUpdateM.Set(0); statusUpdateM.Set(0);
timeoutM = eMinKeepAliveIntervalMs; timeoutM = eMinKeepAliveIntervalMs;
addPidsM.Clear(); addPidsM.Clear();
@ -337,12 +371,11 @@ bool cSatipTuner::SetSource(cSatipServer *serverP, const char *parameterP, const
// Update stream address and parameter // Update stream address and parameter
streamAddrM = rtspM->RtspUnescapeString(nextServerM->Address()); streamAddrM = rtspM->RtspUnescapeString(nextServerM->Address());
streamParamM = rtspM->RtspUnescapeString(parameterP); streamParamM = rtspM->RtspUnescapeString(parameterP);
// Reconnect tunerStatusM = tsSet;
Connect();
} }
} }
else else
Disconnect(); tunerStatusM = tsRelease;
return true; return true;
} }
@ -366,9 +399,9 @@ bool cSatipTuner::SetPid(int pidP, int typeP, bool onP)
bool cSatipTuner::UpdatePids(bool forceP) bool cSatipTuner::UpdatePids(bool forceP)
{ {
cMutexLock MutexLock(&mutexM); //debug("cSatipTuner::%s(%d) tunerStatus=%s [device %d]", __FUNCTION__, forceP, TunerStatusString(tunerStatusM), deviceIdM);
if (((forceP && pidsM.Size()) || (pidUpdateCacheM.TimedOut() && (addPidsM.Size() || delPidsM.Size()))) && if (((forceP && pidsM.Size()) || (pidUpdateCacheM.TimedOut() && (addPidsM.Size() || delPidsM.Size()))) &&
tunedM && !isempty(*streamAddrM) && (streamIdM > 0)) { (tunerStatusM >= tsTuned) && !isempty(*streamAddrM) && (streamIdM > 0)) {
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM); cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
bool usedummy = !!(currentServerM && currentServerM->Quirk(cSatipServer::eSatipQuirkPlayPids)); bool usedummy = !!(currentServerM && currentServerM->Quirk(cSatipServer::eSatipQuirkPlayPids));
if (forceP || usedummy) { if (forceP || usedummy) {
@ -392,44 +425,66 @@ bool cSatipTuner::UpdatePids(bool forceP)
uri = cString::sprintf("%s%d%s", *uri, delPidsM[i], (i == (delPidsM.Size() - 1)) ? "" : ","); uri = cString::sprintf("%s%d%s", *uri, delPidsM[i], (i == (delPidsM.Size() - 1)) ? "" : ",");
} }
} }
if (rtspM->Play(*uri)) { if (!rtspM->Play(*uri))
addPidsM.Clear(); return false;
delPidsM.Clear(); addPidsM.Clear();
return true; delPidsM.Clear();
}
Disconnect();
} }
return false; return true;
} }
bool cSatipTuner::KeepAlive(void) bool cSatipTuner::KeepAlive(bool forceP)
{ {
//debug("cSatipTuner::%s(%d) tunerStatus=%s [device %d]", __FUNCTION__, forceP, TunerStatusString(tunerStatusM), deviceIdM);
cMutexLock MutexLock(&mutexM); cMutexLock MutexLock(&mutexM);
if (tunedM && keepAliveM.TimedOut()) { if (keepAliveM.TimedOut()) {
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
keepAliveM.Set(timeoutM); keepAliveM.Set(timeoutM);
if (rtspM->Options(*uri)) forceP = true;
}
if (forceP && !isempty(*streamAddrM) && (streamIdM > 0)) {
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
if (!rtspM->Options(*uri))
return false;
}
return true;
}
bool cSatipTuner::ReadReceptionStatus(bool forceP)
{
//debug("cSatipTuner::%s(%d) tunerStatus=%s [device %d]", __FUNCTION__, forceP, TunerStatusString(tunerStatusM), deviceIdM);
cMutexLock MutexLock(&mutexM);
if (statusUpdateM.TimedOut()) {
statusUpdateM.Set(eStatusUpdateTimeoutMs);
forceP = true;
}
if (forceP && !isempty(*streamAddrM) && (streamIdM > 0)) {
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM);
if (rtspM->Describe(*uri))
return true; return true;
Disconnect();
} }
return false; return false;
} }
bool cSatipTuner::ReadReceptionStatus(void) const char *cSatipTuner::TunerStatusString(eTunerStatus statusP)
{ {
cMutexLock MutexLock(&mutexM); switch (statusP) {
if (tunedM && !pidsM.Size() && statusUpdateM.TimedOut() ) { case tsIdle:
cString uri = cString::sprintf("rtsp://%s/stream=%d", *streamAddrM, streamIdM); return "tsIdle";
if (rtspM->Describe(*uri)) { case tsRelease:
statusUpdateM.Set(eStatusUpdateTimeoutMs); return "tsRelease";
return true; case tsSet:
} return "tsSet";
Disconnect(); case tsLocked:
} return "tsLocked";
case tsTuned:
return false; return "tsTuned";
default:
break;
}
return "---";
} }
int cSatipTuner::SignalStrength(void) int cSatipTuner::SignalStrength(void)
@ -447,7 +502,7 @@ int cSatipTuner::SignalQuality(void)
bool cSatipTuner::HasLock(void) bool cSatipTuner::HasLock(void)
{ {
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM); //debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
return tunedM && hasLockM; return (tunerStatusM >= tsTuned) && hasLockM;
} }
cString cSatipTuner::GetSignalStatus(void) cString cSatipTuner::GetSignalStatus(void)
@ -459,5 +514,5 @@ cString cSatipTuner::GetSignalStatus(void)
cString cSatipTuner::GetInformation(void) cString cSatipTuner::GetInformation(void)
{ {
//debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM); //debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
return tunedM ? cString::sprintf("rtsp://%s/?%s [stream=%d]", *streamAddrM, *streamParamM, streamIdM) : "connection failed"; return (tunerStatusM >= tsTuned) ? cString::sprintf("rtsp://%s/?%s [stream=%d]", *streamAddrM, *streamParamM, streamIdM) : "connection failed";
} }

16
tuner.h
View File

@ -52,11 +52,11 @@ private:
eDefaultSignalQuality = 224, eDefaultSignalQuality = 224,
eReadTimeoutMs = 500, // in milliseconds eReadTimeoutMs = 500, // in milliseconds
eStatusUpdateTimeoutMs = 1000, // in milliseconds eStatusUpdateTimeoutMs = 1000, // in milliseconds
eConnectTimeoutMs = 1500, // in milliseconds
ePidUpdateIntervalMs = 250, // in milliseconds ePidUpdateIntervalMs = 250, // in milliseconds
eReConnectTimeoutMs = 5000, // in milliseconds eConnectTimeoutMs = 5000, // in milliseconds
eMinKeepAliveIntervalMs = 30000 // in milliseconds eMinKeepAliveIntervalMs = 30000 // in milliseconds
}; };
enum eTunerStatus { tsIdle, tsRelease, tsSet, tsTuned, tsLocked };
cCondWait sleepM; cCondWait sleepM;
cSatipDeviceIf* deviceM; cSatipDeviceIf* deviceM;
@ -73,13 +73,11 @@ private:
cMutex mutexM; cMutex mutexM;
cTimeMs keepAliveM; cTimeMs keepAliveM;
cTimeMs statusUpdateM; cTimeMs statusUpdateM;
cTimeMs signalInfoCacheM;
cTimeMs pidUpdateCacheM; cTimeMs pidUpdateCacheM;
cString sessionM; cString sessionM;
eTunerStatus tunerStatusM;
int fdM; int fdM;
int timeoutM; int timeoutM;
bool openedM;
bool tunedM;
bool hasLockM; bool hasLockM;
int signalStrengthM; int signalStrengthM;
int signalQualityM; int signalQualityM;
@ -90,10 +88,10 @@ private:
bool Connect(void); bool Connect(void);
bool Disconnect(void); bool Disconnect(void);
bool KeepAlive(void); bool KeepAlive(bool forceP = false);
bool ReadReceptionStatus(void); bool ReadReceptionStatus(bool forceP = false);
bool UpdateSignalInfoCache(void);
bool UpdatePids(bool forceP = false); bool UpdatePids(bool forceP = false);
const char *TunerStatusString(eTunerStatus statusP);
protected: protected:
virtual void Action(void); virtual void Action(void);
@ -101,7 +99,7 @@ protected:
public: public:
cSatipTuner(cSatipDeviceIf &deviceP, unsigned int packetLenP); cSatipTuner(cSatipDeviceIf &deviceP, unsigned int packetLenP);
virtual ~cSatipTuner(); virtual ~cSatipTuner();
bool IsTuned(void) const { return tunedM; } bool IsTuned(void) const { return tunerStatusM > tsIdle; }
bool SetSource(cSatipServer *serverP, const char *parameterP, const int indexP); bool SetSource(cSatipServer *serverP, const char *parameterP, const int indexP);
bool SetPid(int pidP, int typeP, bool onP); bool SetPid(int pidP, int typeP, bool onP);
bool Open(void); bool Open(void);