Skip to content

Commit

Permalink
Plc auto (#514)
Browse files Browse the repository at this point in the history
* auto mode for Regulator

* clang format failing somewhere, so just hit *.cpp and *.h

* duh... need to c-f on another machine, meanwhile this is the Regulatotr parser handling  auto in the CLI, for example, -q auto, -q auto15, translates to -500, -15 and does the right thing

* merge dev, clang-format -i src/Regulator.*

* fix missing calc in auto that didn't migrate from plc-countConcealments properly

* merged dev, clang-format -i src/*.cpp, clang-format -i src/*.h

* messing with CF problem

Co-authored-by: Chris Chafe <cchafe@github.com>
  • Loading branch information
cchafe and Chris Chafe committed Mar 27, 2022
1 parent 300e8d7 commit 5e61568
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
51 changes: 32 additions & 19 deletions src/Regulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
// use jack_iodelay
// use jmess -s delay.xml and jmess -c delay.xml

// tested outgoing loss impairments with
// tested outgoing loss impairments with (replace lo with relevant network interface)
// sudo tc qdisc add dev lo root netem loss 2%
// sudo tc qdisc del dev lo root netem loss 2%
// tested jitter impairments with
Expand Down Expand Up @@ -85,7 +85,12 @@ Regulator::Regulator(int sample_rate, int channels, int bit_res, int FPP, int qL
, mFPP(FPP)
, mSampleRate(sample_rate)
, mMsecTolerance((double)qLen)
, mAuto(false)
{
if (mMsecTolerance < 0.0) { // handle, for example, CLI -q auto15 or -q auto
mAuto = true;
mMsecTolerance *= -1.0;
};
switch (mAudioBitRes) { // int from JitterBuffer to AudioInterface enum
case 1:
mBitResolutionMode = AudioInterface::audioBitResolutionT::BIT8;
Expand Down Expand Up @@ -138,8 +143,8 @@ Regulator::Regulator(int sample_rate, int channels, int bit_res, int FPP, int qL
memcpy(mZeros, mXfrBuffer, mBytes);
mAssembledPacket = new int8_t[mBytes]; // for asym
memcpy(mAssembledPacket, mXfrBuffer, mBytes);
pushStat = new StdDev((int)(floor(48000.0 / (double)mFPP)), 1);
pullStat = new StdDev((int)(floor(48000.0 / (double)mFPP)), 2);
pushStat = new StdDev(&mIncomingTimer, (int)(floor(48000.0 / (double)mFPP)), 1);
pullStat = new StdDev(&mIncomingTimer, (int)(floor(48000.0 / (double)mFPP)), 2);
mLastLostCount = 0; // for stats
mIncomingTimer.start();
mLastSeqNumIn = -1;
Expand Down Expand Up @@ -191,8 +196,8 @@ void Regulator::changeGlobal_3(int x)

void Regulator::printParams()
{
qDebug() << "mMsecTolerance" << mMsecTolerance << "mNumSlots" << mNumSlots
<< "mModSeqNum" << mModSeqNum << "mLostWindow" << mLostWindow;
// qDebug() << "mMsecTolerance" << mMsecTolerance << "mNumSlots" << mNumSlots
// << "mModSeqNum" << mModSeqNum << "mLostWindow" << mLostWindow;
#ifdef GUIBS3
updateGUI((int)mMsecTolerance, mNumSlots);
#endif
Expand Down Expand Up @@ -273,7 +278,12 @@ void Regulator::pushPacket(const int8_t* buf, int seq_num)
mLastSeqNumIn = seq_num;
if (mLastSeqNumIn != -1)
memcpy(mSlots[mLastSeqNumIn % mNumSlots], buf, mBytes);
pushStat->tick();
double nowMS = pushStat->tick();
if (mAuto && (nowMS > 2000.0)) {
double tmp = pushStat->longTermStdDev + pushStat->longTermMax;
tmp += 2.0; // 2 ms -- kind of a guess
changeGlobal(tmp);
}
};

//*******************************************************************************
Expand Down Expand Up @@ -565,17 +575,18 @@ ChanData::ChanData(int i, int FPP, int hist) : ch(i)
}

//*******************************************************************************
StdDev::StdDev(int w, int id) : window(w), mId(id)
StdDev::StdDev(QElapsedTimer* timer, int w, int id) : mTimer(timer), window(w), mId(id)
{
reset();
longTermStdDev = 0.0;
longTermStdDevAcc = 0.0;
longTermCnt = 0;
lastMean = 0.0;
lastMin = 0;
lastMax = 0;
lastPlcUnderruns = 0;
mTimer.start();
lastMin = 0.0;
lastMax = 0.0;
longTermMax = 0.0;
longTermMaxAcc = 0.0;
lastTime = 0.0;
data.resize(w, 0.0);
}

Expand All @@ -592,8 +603,9 @@ void StdDev::reset()

double StdDev::tick()
{
double msElapsed = (double)mTimer.nsecsElapsed() / 1000000.0;
mTimer.start();
double now = (double)mTimer->nsecsElapsed() / 1000000.0;
double msElapsed = now - lastTime;
lastTime = now;
if (ctr != window) {
data[ctr] = msElapsed;
if (msElapsed < min)
Expand All @@ -614,6 +626,8 @@ double StdDev::tick()
if (longTermCnt) {
longTermStdDevAcc += stdDev;
longTermStdDev = longTermStdDevAcc / (double)longTermCnt;
longTermMaxAcc += max;
longTermMax = longTermMaxAcc / (double)longTermCnt;
if (gVerboseFlag)
cout << setw(10) << mean << setw(10) << lastMin << setw(10) << max
<< setw(10) << stdDev << setw(10) << longTermStdDev << " " << mId
Expand All @@ -624,14 +638,13 @@ double StdDev::tick()
"stdDev / longTermStdDev) \n";

longTermCnt++;
lastMean = mean;
lastMin = min;
lastMax = max;
lastStdDev = stdDev;
lastPlcUnderruns = plcUnderruns;
lastMean = mean;
lastMin = min;
lastMax = max;
lastStdDev = stdDev;
reset();
}
return msElapsed;
return lastTime;
}
//*******************************************************************************
bool Regulator::getStats(RingBuffer::IOStat* stat, bool reset)
Expand Down
12 changes: 8 additions & 4 deletions src/Regulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ class ChanData
class StdDev
{
public:
StdDev(int w, int id);
StdDev(QElapsedTimer* timer, int w, int id);
void reset();
double tick();
QElapsedTimer mTimer;
QElapsedTimer* mTimer;
std::vector<double> data;
double mean;
double var;
Expand All @@ -112,12 +112,15 @@ class StdDev
double lastMean;
double lastMin;
double lastMax;
int plcUnderruns;
int lastPlcUnderruns;
double lastStdDev;
double longTermStdDev;
double longTermStdDevAcc;
double longTermMax;
double longTermMaxAcc;
double lastTime;
int longTermCnt;
int plcUnderruns;
int lastPlcUnderruns;
};

#ifdef GUIBS3
Expand Down Expand Up @@ -197,6 +200,7 @@ class Regulator : public RingBuffer
int mFPPratioNumerator;
int mFPPratioDenominator;
int mPartialPacketCnt;
bool mAuto;
#ifdef GUIBS3
HerlperGUI* hg;
void updateGUI(double msTol, int nSlots, int lostWin);
Expand Down

0 comments on commit 5e61568

Please sign in to comment.