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

Factored tuner state machine a bit more robust.

This commit is contained in:
Rolf Ahrenberg 2014-11-17 22:59:01 +02:00
parent 54f8b4f0a6
commit 8ec972d4ee
2 changed files with 61 additions and 33 deletions

83
tuner.c
View File

@ -29,7 +29,8 @@ cSatipTuner::cSatipTuner(cSatipDeviceIf &deviceP, unsigned int packetLenP)
statusUpdateM(), statusUpdateM(),
pidUpdateCacheM(), pidUpdateCacheM(),
sessionM(""), sessionM(""),
tunerStatusM(tsIdle), currentStateM(tsIdle),
nextStateM(tsIdle),
timeoutM(eMinKeepAliveIntervalMs), timeoutM(eMinKeepAliveIntervalMs),
hasLockM(false), hasLockM(false),
signalStrengthM(-1), signalStrengthM(-1),
@ -64,7 +65,8 @@ cSatipTuner::~cSatipTuner()
{ {
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM); debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
tunerStatusM = tsIdle; currentStateM = tsIdle;
nextStateM = tsIdle;
// Stop thread // Stop thread
sleepM.Signal(); sleepM.Signal();
@ -85,26 +87,30 @@ void cSatipTuner::Action(void)
reConnectM.Set(eConnectTimeoutMs); reConnectM.Set(eConnectTimeoutMs);
// Do the thread loop // Do the thread loop
while (Running()) { while (Running()) {
switch (tunerStatusM) { mutexM.Lock();
if (currentStateM != nextStateM)
currentStateM = nextStateM;
mutexM.Unlock();
switch (currentStateM) {
case tsIdle: case tsIdle:
//debug("cSatipTuner::%s(): tsIdle [device %d]", __FUNCTION__, deviceIdM); //debug("cSatipTuner::%s(): tsIdle [device %d]", __FUNCTION__, deviceIdM);
break; break;
case tsRelease: case tsRelease:
//debug("cSatipTuner::%s(): tsRelease [device %d]", __FUNCTION__, deviceIdM); //debug("cSatipTuner::%s(): tsRelease [device %d]", __FUNCTION__, deviceIdM);
Disconnect(); Disconnect();
tunerStatusM = tsIdle; nextStateM = tsIdle;
break; break;
case tsSet: case tsSet:
//debug("cSatipTuner::%s(): tsSet [device %d]", __FUNCTION__, deviceIdM); //debug("cSatipTuner::%s(): tsSet [device %d]", __FUNCTION__, deviceIdM);
reConnectM.Set(eConnectTimeoutMs); reConnectM.Set(eConnectTimeoutMs);
Disconnect(); Disconnect();
if (Connect()) { if (Connect()) {
tunerStatusM = tsTuned; nextStateM = tsTuned;
UpdatePids(true); UpdatePids(true);
} }
else { else {
error("Tuning failed - re-tuning [device %d]", deviceIdM); error("Tuning failed - re-tuning [device %d]", deviceIdM);
tunerStatusM = tsIdle; nextStateM = tsIdle;
} }
break; break;
case tsTuned: case tsTuned:
@ -119,33 +125,32 @@ void cSatipTuner::Action(void)
signalQualityM = eDefaultSignalQuality; signalQualityM = eDefaultSignalQuality;
} }
if (hasLockM) if (hasLockM)
tunerStatusM = tsLocked; nextStateM = tsLocked;
} }
break; break;
case tsLocked: case tsLocked:
//debug("cSatipTuner::%s(): tsLocked [device %d]", __FUNCTION__, deviceIdM); //debug("cSatipTuner::%s(): tsLocked [device %d]", __FUNCTION__, deviceIdM);
tunerStatusM = tsLocked;
if (!UpdatePids()) { if (!UpdatePids()) {
error("Pid update failed - re-tuning [device %d]", deviceIdM); error("Pid update failed - re-tuning [device %d]", deviceIdM);
tunerStatusM = tsSet; RequestState(tsSet);
break; break;
} }
if (!KeepAlive()) { if (!KeepAlive()) {
error("Keep-alive failed - re-tuning [device %d]", deviceIdM); error("Keep-alive failed - re-tuning [device %d]", deviceIdM);
tunerStatusM = tsSet; RequestState(tsSet);
break; break;
} }
if (reConnectM.TimedOut()) { if (reConnectM.TimedOut()) {
error("Connection timeout - re-tuning [device %d]", deviceIdM); error("Connection timeout - re-tuning [device %d]", deviceIdM);
tunerStatusM = tsSet; RequestState(tsSet);
break; break;
} }
break; break;
default: default:
error("Unknown tuner status %d [device %d]", tunerStatusM, deviceIdM); error("Unknown tuner status %d [device %d]", currentStateM, deviceIdM);
break; break;
} }
sleepM.Wait(100); // to avoid busy loop and reduce cpu load sleepM.Wait(eSleepTimeoutMs); // to avoid busy loop and reduce cpu load
} }
debug("cSatipTuner::%s(): exiting [device %d]", __FUNCTION__, deviceIdM); debug("cSatipTuner::%s(): exiting [device %d]", __FUNCTION__, deviceIdM);
} }
@ -154,7 +159,10 @@ bool cSatipTuner::Open(void)
{ {
cMutexLock MutexLock(&mutexM); cMutexLock MutexLock(&mutexM);
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM); debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
tunerStatusM = tsSet;
RequestState(tsSet);
// return always true
return true; return true;
} }
@ -162,7 +170,10 @@ bool cSatipTuner::Close(void)
{ {
cMutexLock MutexLock(&mutexM); cMutexLock MutexLock(&mutexM);
debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM); debug("cSatipTuner::%s() [device %d]", __FUNCTION__, deviceIdM);
tunerStatusM = tsRelease;
RequestState(tsRelease);
// return always true
return true; return true;
} }
@ -175,7 +186,7 @@ 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 ((tunerStatusM >= tsTuned) && (streamIdM >= 0)) { if (streamIdM >= 0) {
debug("cSatipTuner::%s(): retune [device %d]", __FUNCTION__, deviceIdM); debug("cSatipTuner::%s(): retune [device %d]", __FUNCTION__, deviceIdM);
KeepAlive(true); KeepAlive(true);
// Flush any old content // Flush any old content
@ -206,11 +217,12 @@ 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 ((tunerStatusM >= tsTuned) && !isempty(*streamAddrM) && (streamIdM >= 0)) { if (!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; currentStateM = tsIdle;
nextStateM = tsIdle;
// Reset signal parameters // Reset signal parameters
hasLockM = false; hasLockM = false;
@ -323,12 +335,12 @@ 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);
tunerStatusM = tsSet; RequestState(tsSet);
} }
} }
else else
tunerStatusM = tsRelease; RequestState(tsRelease);
sleepM.Signal();
return true; return true;
} }
@ -348,14 +360,15 @@ bool cSatipTuner::SetPid(int pidP, int typeP, bool onP)
} }
pidUpdateCacheM.Set(ePidUpdateIntervalMs); pidUpdateCacheM.Set(ePidUpdateIntervalMs);
sleepM.Signal(); sleepM.Signal();
return true; return true;
} }
bool cSatipTuner::UpdatePids(bool forceP) bool cSatipTuner::UpdatePids(bool forceP)
{ {
//debug("cSatipTuner::%s(%d) tunerStatus=%s [device %d]", __FUNCTION__, forceP, TunerStatusString(tunerStatusM), deviceIdM); //debug("cSatipTuner::%s(%d) tunerStatus=%s [device %d]", __FUNCTION__, forceP, TunerStateString(currentStateM), deviceIdM);
if (((forceP && pidsM.Size()) || (pidUpdateCacheM.TimedOut() && (addPidsM.Size() || delPidsM.Size()))) && if (((forceP && pidsM.Size()) || (pidUpdateCacheM.TimedOut() && (addPidsM.Size() || delPidsM.Size()))) &&
(tunerStatusM >= tsTuned) && !isempty(*streamAddrM) && (streamIdM > 0)) { !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) {
@ -390,7 +403,7 @@ bool cSatipTuner::UpdatePids(bool forceP)
bool cSatipTuner::KeepAlive(bool forceP) bool cSatipTuner::KeepAlive(bool forceP)
{ {
//debug("cSatipTuner::%s(%d) tunerStatus=%s [device %d]", __FUNCTION__, forceP, TunerStatusString(tunerStatusM), deviceIdM); //debug("cSatipTuner::%s(%d) tunerStatus=%s [device %d]", __FUNCTION__, forceP, TunerStateString(currentStateM), deviceIdM);
cMutexLock MutexLock(&mutexM); cMutexLock MutexLock(&mutexM);
if (keepAliveM.TimedOut()) { if (keepAliveM.TimedOut()) {
keepAliveM.Set(timeoutM); keepAliveM.Set(timeoutM);
@ -407,7 +420,7 @@ bool cSatipTuner::KeepAlive(bool forceP)
bool cSatipTuner::ReadReceptionStatus(bool forceP) bool cSatipTuner::ReadReceptionStatus(bool forceP)
{ {
//debug("cSatipTuner::%s(%d) tunerStatus=%s [device %d]", __FUNCTION__, forceP, TunerStatusString(tunerStatusM), deviceIdM); //debug("cSatipTuner::%s(%d) tunerStatus=%s [device %d]", __FUNCTION__, forceP, TunerStateString(currentStateM), deviceIdM);
cMutexLock MutexLock(&mutexM); cMutexLock MutexLock(&mutexM);
if (statusUpdateM.TimedOut()) { if (statusUpdateM.TimedOut()) {
statusUpdateM.Set(eStatusUpdateTimeoutMs); statusUpdateM.Set(eStatusUpdateTimeoutMs);
@ -422,9 +435,20 @@ bool cSatipTuner::ReadReceptionStatus(bool forceP)
return false; return false;
} }
const char *cSatipTuner::TunerStatusString(eTunerStatus statusP) bool cSatipTuner::RequestState(eTunerState stateP)
{ {
switch (statusP) { cMutexLock MutexLock(&mutexM);
debug("cSatipTuner::%s(%d) [device %d]", __FUNCTION__, TunerStateString(stateP), deviceIdM);
nextStateM = stateP;
// return always true
return true;
}
const char *cSatipTuner::TunerStateString(eTunerState stateP)
{
switch (stateP) {
case tsIdle: case tsIdle:
return "tsIdle"; return "tsIdle";
case tsRelease: case tsRelease:
@ -438,6 +462,7 @@ const char *cSatipTuner::TunerStatusString(eTunerStatus statusP)
default: default:
break; break;
} }
return "---"; return "---";
} }
@ -456,7 +481,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 (tunerStatusM >= tsTuned) && hasLockM; return (currentStateM >= tsTuned) && hasLockM;
} }
cString cSatipTuner::GetSignalStatus(void) cString cSatipTuner::GetSignalStatus(void)
@ -468,5 +493,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 (tunerStatusM >= tsTuned) ? cString::sprintf("rtsp://%s/?%s [stream=%d]", *streamAddrM, *streamParamM, streamIdM) : "connection failed"; return (currentStateM >= tsTuned) ? cString::sprintf("rtsp://%s/?%s [stream=%d]", *streamAddrM, *streamParamM, streamIdM) : "connection failed";
} }

11
tuner.h
View File

@ -52,12 +52,13 @@ private:
eDefaultSignalStrength = 15, eDefaultSignalStrength = 15,
eDefaultSignalQuality = 224, eDefaultSignalQuality = 224,
eApplicationMaxSizeB = 1500, eApplicationMaxSizeB = 1500,
eSleepTimeoutMs = 500, // in milliseconds
eStatusUpdateTimeoutMs = 1000, // in milliseconds eStatusUpdateTimeoutMs = 1000, // in milliseconds
ePidUpdateIntervalMs = 250, // in milliseconds ePidUpdateIntervalMs = 250, // in milliseconds
eConnectTimeoutMs = 5000, // in milliseconds eConnectTimeoutMs = 5000, // in milliseconds
eMinKeepAliveIntervalMs = 30000 // in milliseconds eMinKeepAliveIntervalMs = 30000 // in milliseconds
}; };
enum eTunerStatus { tsIdle, tsRelease, tsSet, tsTuned, tsLocked }; enum eTunerState { tsIdle, tsRelease, tsSet, tsTuned, tsLocked };
cCondWait sleepM; cCondWait sleepM;
cSatipDeviceIf* deviceM; cSatipDeviceIf* deviceM;
@ -75,7 +76,8 @@ private:
cTimeMs statusUpdateM; cTimeMs statusUpdateM;
cTimeMs pidUpdateCacheM; cTimeMs pidUpdateCacheM;
cString sessionM; cString sessionM;
eTunerStatus tunerStatusM; eTunerState currentStateM;
eTunerState nextStateM;
int timeoutM; int timeoutM;
bool hasLockM; bool hasLockM;
int signalStrengthM; int signalStrengthM;
@ -90,7 +92,8 @@ private:
bool KeepAlive(bool forceP = false); bool KeepAlive(bool forceP = false);
bool ReadReceptionStatus(bool forceP = false); bool ReadReceptionStatus(bool forceP = false);
bool UpdatePids(bool forceP = false); bool UpdatePids(bool forceP = false);
const char *TunerStatusString(eTunerStatus statusP); bool RequestState(eTunerState stateP);
const char *TunerStateString(eTunerState stateP);
protected: protected:
virtual void Action(void); virtual void Action(void);
@ -98,7 +101,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 tunerStatusM > tsIdle; } bool IsTuned(void) const { return currentStateM > 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);