- use std::chrono types for timelength values

This commit is contained in:
2026-02-13 08:38:27 +03:00
parent bb1f92fa31
commit 8f826e321c
4 changed files with 54 additions and 42 deletions

View File

@@ -31,8 +31,8 @@
using namespace MT; using namespace MT;
// ----------------- RtpBuffer::Packet -------------- // ----------------- RtpBuffer::Packet --------------
RtpBuffer::Packet::Packet(const std::shared_ptr<RTPPacket>& packet, int timelength, int rate) RtpBuffer::Packet::Packet(const std::shared_ptr<RTPPacket>& packet, std::chrono::milliseconds timelength, int samplerate)
:mRtp(packet), mTimelength(timelength), mRate(rate) :mRtp(packet), mTimelength(timelength), mSamplerate(samplerate)
{ {
} }
@@ -41,14 +41,14 @@ std::shared_ptr<RTPPacket> RtpBuffer::Packet::rtp() const
return mRtp; return mRtp;
} }
int RtpBuffer::Packet::timelength() const std::chrono::milliseconds RtpBuffer::Packet::timelength() const
{ {
return mTimelength; return mTimelength;
} }
int RtpBuffer::Packet::rate() const int RtpBuffer::Packet::samplerate() const
{ {
return mRate; return mSamplerate;
} }
const std::vector<short>& RtpBuffer::Packet::pcm() const const std::vector<short>& RtpBuffer::Packet::pcm() const
@@ -74,32 +74,32 @@ RtpBuffer::~RtpBuffer()
ICELogDebug(<< "Number of add packets: " << mAddCounter << ", number of retrieved packets " << mReturnedCounter); ICELogDebug(<< "Number of add packets: " << mAddCounter << ", number of retrieved packets " << mReturnedCounter);
} }
void RtpBuffer::setHigh(int milliseconds) void RtpBuffer::setHigh(std::chrono::milliseconds t)
{ {
mHigh = milliseconds; mHigh = t;
} }
int RtpBuffer::high() const std::chrono::milliseconds RtpBuffer::high() const
{ {
return mHigh; return mHigh;
} }
void RtpBuffer::setLow(int milliseconds) void RtpBuffer::setLow(std::chrono::milliseconds t)
{ {
mLow = milliseconds; mLow = t;
} }
int RtpBuffer::low() const std::chrono::milliseconds RtpBuffer::low() const
{ {
return mLow; return mLow;
} }
void RtpBuffer::setPrebuffer(int milliseconds) void RtpBuffer::setPrebuffer(std::chrono::milliseconds t)
{ {
mPrebuffer = milliseconds; mPrebuffer = t;
} }
int RtpBuffer::prebuffer() const std::chrono::milliseconds RtpBuffer::prebuffer() const
{ {
return mPrebuffer; return mPrebuffer;
} }
@@ -115,7 +115,7 @@ bool SequenceSort(const std::shared_ptr<RtpBuffer::Packet>& p1, const std::share
return p1->rtp()->GetExtendedSequenceNumber() < p2->rtp()->GetExtendedSequenceNumber(); return p1->rtp()->GetExtendedSequenceNumber() < p2->rtp()->GetExtendedSequenceNumber();
} }
std::shared_ptr<RtpBuffer::Packet> RtpBuffer::add(std::shared_ptr<jrtplib::RTPPacket> packet, int timelength, int rate) std::shared_ptr<RtpBuffer::Packet> RtpBuffer::add(std::shared_ptr<jrtplib::RTPPacket> packet, std::chrono::milliseconds timelength, int rate)
{ {
if (!packet) if (!packet)
return std::shared_ptr<Packet>(); return std::shared_ptr<Packet>();
@@ -161,7 +161,7 @@ std::shared_ptr<RtpBuffer::Packet> RtpBuffer::add(std::shared_ptr<jrtplib::RTPPa
} }
// Get amount of available audio (in milliseconds) in jitter buffer // Get amount of available audio (in milliseconds) in jitter buffer
int available = findTimelength(); auto available = findTimelength();
if (newSeqno > minno || (available < mHigh)) if (newSeqno > minno || (available < mHigh))
{ {
@@ -199,9 +199,9 @@ RtpBuffer::FetchResult RtpBuffer::fetch(ResultList& rl)
rl.clear(); rl.clear();
// See if there is enough information in buffer // See if there is enough information in buffer
int total = findTimelength(); auto total = findTimelength();
while (total > mHigh && mPacketList.size() && 0 != mHigh) while (total > mHigh && mPacketList.size() && 0ms != mHigh)
{ {
ICELogMedia( << "Dropping RTP packets from jitter buffer"); ICELogMedia( << "Dropping RTP packets from jitter buffer");
total -= mPacketList.front()->timelength(); total -= mPacketList.front()->timelength();
@@ -303,12 +303,12 @@ RtpBuffer::FetchResult RtpBuffer::fetch(ResultList& rl)
return result; return result;
} }
int RtpBuffer::findTimelength() std::chrono::milliseconds RtpBuffer::findTimelength()
{ {
int available = 0; std::chrono::milliseconds r = 0ms;
for (unsigned i = 0; i < mPacketList.size(); i++) for (const auto& p: mPacketList)
available += mPacketList[i]->timelength(); r += p->timelength();
return available; return r;
} }
int RtpBuffer::getNumberOfReturnedPackets() const int RtpBuffer::getNumberOfReturnedPackets() const
@@ -424,6 +424,7 @@ bool AudioReceiver::add(const std::shared_ptr<jrtplib::RTPPacket>& p, Codec** de
ptype = p->GetPayloadType(); ptype = p->GetPayloadType();
ICELogMedia(<< "Adding packet No " << p->GetSequenceNumber()); ICELogMedia(<< "Adding packet No " << p->GetSequenceNumber());
// Increase codec counter // Increase codec counter
mStat.mCodecCount[ptype]++; mStat.mCodecCount[ptype]++;
@@ -432,7 +433,7 @@ bool AudioReceiver::add(const std::shared_ptr<jrtplib::RTPPacket>& p, Codec** de
auto codecIter = mCodecMap.find(ptype); auto codecIter = mCodecMap.find(ptype);
if (codecIter == mCodecMap.end()) if (codecIter == mCodecMap.end())
{ {
time_length = 10; // Well, there is no information about the codec; skip this packet
} }
else else
{ {
@@ -467,6 +468,9 @@ bool AudioReceiver::add(const std::shared_ptr<jrtplib::RTPPacket>& p, Codec** de
mJitterStats.process(p.get(), samplerate); mJitterStats.process(p.get(), samplerate);
mStat.mJitter = static_cast<float>(mJitterStats.get()); mStat.mJitter = static_cast<float>(mJitterStats.get());
if (!codec)
return false; // There is no sense to add this packet into jitter buffer - we can't decode this
// Check if packet is CNG // Check if packet is CNG
if (payloadLength >= 1 && payloadLength <= 6 && (ptype == 0 || ptype == 8)) if (payloadLength >= 1 && payloadLength <= 6 && (ptype == 0 || ptype == 8))
time_length = mLastPacketTimeLength ? mLastPacketTimeLength : 20; time_length = mLastPacketTimeLength ? mLastPacketTimeLength : 20;
@@ -476,12 +480,12 @@ bool AudioReceiver::add(const std::shared_ptr<jrtplib::RTPPacket>& p, Codec** de
{ {
// It will cause statistics to report about bad RTP packet // It will cause statistics to report about bad RTP packet
// I have to replay last packet payload here to avoid report about lost packet // I have to replay last packet payload here to avoid report about lost packet
mBuffer.add(p, time_length, samplerate); mBuffer.add(p, std::chrono::milliseconds(time_length), samplerate);
return false; return false;
} }
// Queue packet to buffer // Queue packet to buffer
auto packet = mBuffer.add(p, time_length, samplerate).get(); auto packet = mBuffer.add(p, std::chrono::milliseconds(time_length), samplerate).get();
return packet; return packet;
} }

View File

@@ -39,11 +39,11 @@ public:
class Packet class Packet
{ {
public: public:
Packet(const std::shared_ptr<RTPPacket>& packet, int timelen, int rate); Packet(const std::shared_ptr<RTPPacket>& packet, std::chrono::milliseconds timelen, int samplerate);
std::shared_ptr<RTPPacket> rtp() const; std::shared_ptr<RTPPacket> rtp() const;
int timelength() const; std::chrono::milliseconds timelength() const;
int rate() const; int samplerate() const;
const std::vector<short>& pcm() const; const std::vector<short>& pcm() const;
std::vector<short>& pcm(); std::vector<short>& pcm();
@@ -53,8 +53,8 @@ public:
protected: protected:
std::shared_ptr<RTPPacket> mRtp; std::shared_ptr<RTPPacket> mRtp;
int mTimelength = 0, std::chrono::milliseconds mTimelength = 0ms;
mRate = 0; int mSamplerate = 0;
std::vector<short> mPcm; std::vector<short> mPcm;
std::chrono::microseconds mTimestamp = 0us; std::chrono::microseconds mTimestamp = 0us;
}; };
@@ -65,23 +65,23 @@ public:
unsigned ssrc() const; unsigned ssrc() const;
void setSsrc(unsigned ssrc); void setSsrc(unsigned ssrc);
void setHigh(int milliseconds); void setHigh(std::chrono::milliseconds t);
int high() const; std::chrono::milliseconds high() const;
void setLow(int milliseconds); void setLow(std::chrono::milliseconds t);
int low() const; std::chrono::milliseconds low() const;
void setPrebuffer(int milliseconds); void setPrebuffer(std::chrono::milliseconds t);
int prebuffer() const; std::chrono::milliseconds prebuffer() const;
int getNumberOfReturnedPackets() const; int getNumberOfReturnedPackets() const;
int getNumberOfAddPackets() const; int getNumberOfAddPackets() const;
int findTimelength(); std::chrono::milliseconds findTimelength();
int getCount() const; int getCount() const;
// Returns false if packet was not add - maybe too old or too new or duplicate // Returns false if packet was not add - maybe too old or too new or duplicate
std::shared_ptr<Packet> add(std::shared_ptr<RTPPacket> packet, int timelength, int rate); std::shared_ptr<Packet> add(std::shared_ptr<RTPPacket> packet, std::chrono::milliseconds timelength, int rate);
typedef std::vector<std::shared_ptr<Packet>> ResultList; typedef std::vector<std::shared_ptr<Packet>> ResultList;
typedef std::shared_ptr<ResultList> PResultList; typedef std::shared_ptr<ResultList> PResultList;
@@ -90,9 +90,9 @@ public:
protected: protected:
unsigned mSsrc = 0; unsigned mSsrc = 0;
int mHigh = RTP_BUFFER_HIGH, std::chrono::milliseconds mHigh = std::chrono::milliseconds(RTP_BUFFER_HIGH),
mLow = RTP_BUFFER_LOW, mLow = std::chrono::milliseconds(RTP_BUFFER_LOW),
mPrebuffer = RTP_BUFFER_PREBUFFER; mPrebuffer = std::chrono::milliseconds(RTP_BUFFER_PREBUFFER);
int mReturnedCounter = 0, int mReturnedCounter = 0,
mAddCounter = 0; mAddCounter = 0;

View File

@@ -466,3 +466,10 @@ Logger::operator << (const std::filesystem::path& p)
*mStream << p; *mStream << p;
return *this; return *this;
} }
Logger&
Logger::operator << (const std::chrono::milliseconds t)
{
*mStream << t.count() << "ms";
return *this;
}

View File

@@ -130,6 +130,7 @@ public:
Logger& operator << (const unsigned int data); Logger& operator << (const unsigned int data);
Logger& operator << (const uint64_t data); Logger& operator << (const uint64_t data);
Logger& operator << (const std::filesystem::path& p); Logger& operator << (const std::filesystem::path& p);
Logger& operator << (const std::chrono::milliseconds t);
protected: protected:
LogGuard mGuard; LogGuard mGuard;