- 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;
// ----------------- RtpBuffer::Packet --------------
RtpBuffer::Packet::Packet(const std::shared_ptr<RTPPacket>& packet, int timelength, int rate)
:mRtp(packet), mTimelength(timelength), mRate(rate)
RtpBuffer::Packet::Packet(const std::shared_ptr<RTPPacket>& packet, std::chrono::milliseconds timelength, int samplerate)
:mRtp(packet), mTimelength(timelength), mSamplerate(samplerate)
{
}
@@ -41,14 +41,14 @@ std::shared_ptr<RTPPacket> RtpBuffer::Packet::rtp() const
return mRtp;
}
int RtpBuffer::Packet::timelength() const
std::chrono::milliseconds RtpBuffer::Packet::timelength() const
{
return mTimelength;
}
int RtpBuffer::Packet::rate() const
int RtpBuffer::Packet::samplerate() const
{
return mRate;
return mSamplerate;
}
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);
}
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;
}
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;
}
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;
}
@@ -115,7 +115,7 @@ bool SequenceSort(const std::shared_ptr<RtpBuffer::Packet>& p1, const std::share
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)
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
int available = findTimelength();
auto available = findTimelength();
if (newSeqno > minno || (available < mHigh))
{
@@ -199,9 +199,9 @@ RtpBuffer::FetchResult RtpBuffer::fetch(ResultList& rl)
rl.clear();
// 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");
total -= mPacketList.front()->timelength();
@@ -303,12 +303,12 @@ RtpBuffer::FetchResult RtpBuffer::fetch(ResultList& rl)
return result;
}
int RtpBuffer::findTimelength()
std::chrono::milliseconds RtpBuffer::findTimelength()
{
int available = 0;
for (unsigned i = 0; i < mPacketList.size(); i++)
available += mPacketList[i]->timelength();
return available;
std::chrono::milliseconds r = 0ms;
for (const auto& p: mPacketList)
r += p->timelength();
return r;
}
int RtpBuffer::getNumberOfReturnedPackets() const
@@ -424,6 +424,7 @@ bool AudioReceiver::add(const std::shared_ptr<jrtplib::RTPPacket>& p, Codec** de
ptype = p->GetPayloadType();
ICELogMedia(<< "Adding packet No " << p->GetSequenceNumber());
// Increase codec counter
mStat.mCodecCount[ptype]++;
@@ -432,7 +433,7 @@ bool AudioReceiver::add(const std::shared_ptr<jrtplib::RTPPacket>& p, Codec** de
auto codecIter = mCodecMap.find(ptype);
if (codecIter == mCodecMap.end())
{
time_length = 10;
// Well, there is no information about the codec; skip this packet
}
else
{
@@ -467,6 +468,9 @@ bool AudioReceiver::add(const std::shared_ptr<jrtplib::RTPPacket>& p, Codec** de
mJitterStats.process(p.get(), samplerate);
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
if (payloadLength >= 1 && payloadLength <= 6 && (ptype == 0 || ptype == 8))
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
// 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;
}
// 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;
}

View File

@@ -39,11 +39,11 @@ public:
class Packet
{
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;
int timelength() const;
int rate() const;
std::chrono::milliseconds timelength() const;
int samplerate() const;
const std::vector<short>& pcm() const;
std::vector<short>& pcm();
@@ -53,8 +53,8 @@ public:
protected:
std::shared_ptr<RTPPacket> mRtp;
int mTimelength = 0,
mRate = 0;
std::chrono::milliseconds mTimelength = 0ms;
int mSamplerate = 0;
std::vector<short> mPcm;
std::chrono::microseconds mTimestamp = 0us;
};
@@ -65,23 +65,23 @@ public:
unsigned ssrc() const;
void setSsrc(unsigned ssrc);
void setHigh(int milliseconds);
int high() const;
void setHigh(std::chrono::milliseconds t);
std::chrono::milliseconds high() const;
void setLow(int milliseconds);
int low() const;
void setLow(std::chrono::milliseconds t);
std::chrono::milliseconds low() const;
void setPrebuffer(int milliseconds);
int prebuffer() const;
void setPrebuffer(std::chrono::milliseconds t);
std::chrono::milliseconds prebuffer() const;
int getNumberOfReturnedPackets() const;
int getNumberOfAddPackets() const;
int findTimelength();
std::chrono::milliseconds findTimelength();
int getCount() const;
// 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::shared_ptr<ResultList> PResultList;
@@ -90,9 +90,9 @@ public:
protected:
unsigned mSsrc = 0;
int mHigh = RTP_BUFFER_HIGH,
mLow = RTP_BUFFER_LOW,
mPrebuffer = RTP_BUFFER_PREBUFFER;
std::chrono::milliseconds mHigh = std::chrono::milliseconds(RTP_BUFFER_HIGH),
mLow = std::chrono::milliseconds(RTP_BUFFER_LOW),
mPrebuffer = std::chrono::milliseconds(RTP_BUFFER_PREBUFFER);
int mReturnedCounter = 0,
mAddCounter = 0;

View File

@@ -466,3 +466,10 @@ Logger::operator << (const std::filesystem::path& p)
*mStream << p;
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 uint64_t data);
Logger& operator << (const std::filesystem::path& p);
Logger& operator << (const std::chrono::milliseconds t);
protected:
LogGuard mGuard;