Split out jitter timer to be one per metadata, clear out jitter data when clearing as well

This commit is contained in:
Thulinma 2023-07-20 10:04:43 +02:00
parent 7d55a3f1c4
commit 0ec2800894
2 changed files with 71 additions and 66 deletions

View file

@ -2278,84 +2278,74 @@ namespace DTSC{
pack.getFlag("keyframe"), pack.getDataLen()); pack.getFlag("keyframe"), pack.getDataLen());
} }
/// Helper class that calculates inter-packet jitter jitterTimer::jitterTimer(){
class jitterTimer{ for (int i = 0; i < 8; ++i){
public: trueTime[i] = 0;
uint64_t trueTime[8]; // Array of bootMS-based measurement points packTime[i] = 0;
uint64_t packTime[8]; // Array of corresponding packet times }
uint64_t curJitter; // Maximum jitter measurement in past 10 seconds maxJitter = 200;
unsigned int x; // Current indice within above two arrays lastTime = 0;
uint64_t maxJitter; // Highest jitter ever observed by this jitterTimer x = 0;
uint64_t lastTime; // Last packet used for a measurement point }
jitterTimer(){
uint64_t jitterTimer::addPack(uint64_t t){
if (veryUglyJitterOverride){return veryUglyJitterOverride;}
uint64_t curMs = Util::bootMS();
if (!x){
// First call, set the whole array to this packet
for (int i = 0; i < 8; ++i){ for (int i = 0; i < 8; ++i){
trueTime[i] = 0; trueTime[i] = curMs;
packTime[i] = 0; packTime[i] = t;
} }
maxJitter = 200; ++x;
lastTime = 0; trueTime[x % 8] = curMs;
x = 0; packTime[x % 8] = t;
lastTime = t;
curJitter = 0;
} }
uint64_t addPack(uint64_t t){ if (t > lastTime + 2500){
if (veryUglyJitterOverride){return veryUglyJitterOverride;} if ((x % 4) == 0){
uint64_t curMs = Util::bootMS(); if (maxJitter > 50 && curJitter < maxJitter - 50){
if (!x){ MEDIUM_MSG("Jitter lowered from %" PRIu64 " to %" PRIu64 " ms", maxJitter, curJitter);
// First call, set the whole array to this packet maxJitter = curJitter;
for (int i = 0; i < 8; ++i){
trueTime[i] = curMs;
packTime[i] = t;
} }
++x; curJitter = maxJitter*0.90;
trueTime[x % 8] = curMs;
packTime[x % 8] = t;
lastTime = t;
curJitter = 0;
} }
if (t > lastTime + 2500){ ++x;
if ((x % 4) == 0){ trueTime[x % 8] = curMs;
if (maxJitter > 50 && curJitter < maxJitter - 50){ packTime[x % 8] = t;
MEDIUM_MSG("Jitter lowered from %" PRIu64 " to %" PRIu64 " ms", maxJitter, curJitter); lastTime = t;
maxJitter = curJitter;
}
curJitter = maxJitter*0.90;
}
++x;
trueTime[x % 8] = curMs;
packTime[x % 8] = t;
lastTime = t;
}
uint64_t realTime = (curMs - trueTime[(x + 1) % 8]);
uint64_t arriTime = (t - packTime[(x + 1) % 8]);
int64_t jitter = (realTime - arriTime);
if (jitter < 0){
// Negative jitter = packets arriving too soon.
// This is... ehh... not a bad thing? I guess..?
// if (jitter < -1000){
// INFO_MSG("Jitter = %" PRId64 " ms (max: %" PRIu64 ")", jitter, maxJitter);
//}
}else{
// Positive jitter = packets arriving too late.
// We need to delay playback at least by this amount to account for it.
if ((uint64_t)jitter > maxJitter){
if (jitter - maxJitter > 420){
INFO_MSG("Jitter increased from %" PRIu64 " to %" PRId64 " ms", maxJitter, jitter);
}else{
HIGH_MSG("Jitter increased from %" PRIu64 " to %" PRId64 " ms", maxJitter, jitter);
}
maxJitter = (uint64_t)jitter;
}
if (curJitter < (uint64_t)jitter){curJitter = (uint64_t)jitter;}
}
return maxJitter;
} }
}; uint64_t realTime = (curMs - trueTime[(x + 1) % 8]);
uint64_t arriTime = (t - packTime[(x + 1) % 8]);
int64_t jitter = (realTime - arriTime);
if (jitter < 0){
// Negative jitter = packets arriving too soon.
// This is... ehh... not a bad thing? I guess..?
// if (jitter < -1000){
// INFO_MSG("Jitter = %" PRId64 " ms (max: %" PRIu64 ")", jitter, maxJitter);
//}
}else{
// Positive jitter = packets arriving too late.
// We need to delay playback at least by this amount to account for it.
if ((uint64_t)jitter > maxJitter){
if (jitter - maxJitter > 420){
INFO_MSG("Jitter increased from %" PRIu64 " to %" PRId64 " ms", maxJitter, jitter);
}else{
HIGH_MSG("Jitter increased from %" PRIu64 " to %" PRId64 " ms", maxJitter, jitter);
}
maxJitter = (uint64_t)jitter;
}
if (curJitter < (uint64_t)jitter){curJitter = (uint64_t)jitter;}
}
return maxJitter;
}
/// Updates the metadata given the packet's properties. /// Updates the metadata given the packet's properties.
void Meta::update(uint64_t packTime, int64_t packOffset, uint32_t packTrack, uint64_t packDataSize, void Meta::update(uint64_t packTime, int64_t packOffset, uint32_t packTrack, uint64_t packDataSize,
uint64_t packBytePos, bool isKeyframe, uint64_t packSendSize){ uint64_t packBytePos, bool isKeyframe, uint64_t packSendSize){
///\todo warning Re-Implement Ivec ///\todo warning Re-Implement Ivec
if (getLive()){ if (getLive()){
static std::map<size_t, jitterTimer> theJitters;
setMinKeepAway(packTrack, theJitters[packTrack].addPack(packTime)); setMinKeepAway(packTrack, theJitters[packTrack].addPack(packTime));
} }
@ -2548,6 +2538,7 @@ namespace DTSC{
/// Wipes internal structures, also marking as outdated and deleting memory structures if in /// Wipes internal structures, also marking as outdated and deleting memory structures if in
/// master mode. /// master mode.
void Meta::clear(){ void Meta::clear(){
theJitters.clear();
if (isMemBuf){ if (isMemBuf){
isMemBuf = false; isMemBuf = false;
free(streamMemBuf); free(streamMemBuf);

View file

@ -268,6 +268,19 @@ namespace DTSC{
Util::RelAccXFieldData fragmentSizeField; Util::RelAccXFieldData fragmentSizeField;
}; };
class jitterTimer{
public:
uint64_t trueTime[8]; // Array of bootMS-based measurement points
uint64_t packTime[8]; // Array of corresponding packet times
uint64_t curJitter; // Maximum jitter measurement in past 10 seconds
unsigned int x; // Current indice within above two arrays
uint64_t maxJitter; // Highest jitter ever observed by this jitterTimer
uint64_t lastTime; // Last packet used for a measurement point
jitterTimer();
uint64_t addPack(uint64_t t);
};
class Meta{ class Meta{
public: public:
Meta(const std::string &_streamName, const DTSC::Scan &src); Meta(const std::string &_streamName, const DTSC::Scan &src);
@ -499,6 +512,7 @@ namespace DTSC{
std::map<size_t, size_t> sizeMemBuf; std::map<size_t, size_t> sizeMemBuf;
private: private:
std::map<size_t, jitterTimer> theJitters;
// Internal buffers so we don't always need to search for everything // Internal buffers so we don't always need to search for everything
Util::RelAccXFieldData streamVodField; Util::RelAccXFieldData streamVodField;
Util::RelAccXFieldData streamLiveField; Util::RelAccXFieldData streamLiveField;