提交 0b0ade4c authored 作者: José Luis Millán's avatar José Luis Millán

Rtp: RtpDataCounter and RateCalculator

上级 d3593be8
#ifndef MS_RTC_RTP_DATA_COUNTER_H
#define MS_RTC_RTP_DATA_COUNTER_H
#include "common.h"
#include "DepLibUV.h"
#include "RTC/RtpPacket.h"
namespace RTC
{
// It is considered that the time source increases monotonically.
// ie: the current timestamp can never be minor than a timestamp in the past.
class RateCalculator
{
public:
static constexpr float kBpsScale = 8000.0f;
static constexpr float kbpsScale = 1000.0f;
static constexpr size_t defaultWindowSize = 1000;
public:
RateCalculator(size_t windowSize = defaultWindowSize, float scale = kBpsScale);
void Update(size_t count, uint64_t now);
uint32_t GetRate(uint64_t now);
private:
void RemoveOldData(uint64_t now);
void Reset(uint64_t oldestTime);
private:
struct BufferItem
{
size_t count = 0;
};
private:
std::unique_ptr<BufferItem[]> buffer;
// Time (in milliseconds) for oldest item in the time window.
uint64_t oldestTime = 0;
// Index for the oldest item in the time window.
uint32_t oldestIndex = 0;
// Total count in the time window.
size_t totalCount = 0;
// Window Size (in milliseconds).
size_t windowSize = defaultWindowSize;
// Scale in which the rate is represented.
const float scale = kBpsScale;
};
class RtpDataCounter
{
public:
void Update(RTC::RtpPacket* packet);
uint32_t GetRate(uint64_t now);
size_t GetPacketCount();
size_t GetBytes();
private:
RateCalculator rate;
size_t packets = 0;
size_t bytes = 0;
};
/* Inline instance methods. */
inline
RateCalculator::RateCalculator(size_t windowSize, float scale) :
windowSize(windowSize),
scale(scale)
{
uint64_t now = DepLibUV::GetTime();
this->Reset(now - windowSize);
}
inline
void RateCalculator::Reset(uint64_t oldestTime)
{
this->buffer.reset(new BufferItem[windowSize]);
this->totalCount = 0;
this->oldestIndex = 0;
this->oldestTime = oldestTime;
}
inline
size_t RtpDataCounter::GetPacketCount()
{
return this->packets;
}
inline
uint32_t RtpDataCounter::GetRate(uint64_t now)
{
return this->rate.GetRate(now);
}
inline
size_t RtpDataCounter::GetBytes()
{
return this->bytes;
}
}
#endif
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
'src/RTC/RtpStream.cpp', 'src/RTC/RtpStream.cpp',
'src/RTC/RtpStreamRecv.cpp', 'src/RTC/RtpStreamRecv.cpp',
'src/RTC/RtpStreamSend.cpp', 'src/RTC/RtpStreamSend.cpp',
'src/RTC/RtpDataCounter.cpp',
'src/RTC/SrtpSession.cpp', 'src/RTC/SrtpSession.cpp',
'src/RTC/StunMessage.cpp', 'src/RTC/StunMessage.cpp',
'src/RTC/TcpConnection.cpp', 'src/RTC/TcpConnection.cpp',
...@@ -114,6 +115,7 @@ ...@@ -114,6 +115,7 @@
'include/RTC/RtpStream.h', 'include/RTC/RtpStream.h',
'include/RTC/RtpStreamRecv.h', 'include/RTC/RtpStreamRecv.h',
'include/RTC/RtpStreamSend.h', 'include/RTC/RtpStreamSend.h',
'include/RTC/RtpDataCounter.h',
'include/RTC/SrtpSession.h', 'include/RTC/SrtpSession.h',
'include/RTC/StunMessage.h', 'include/RTC/StunMessage.h',
'include/RTC/TcpConnection.h', 'include/RTC/TcpConnection.h',
...@@ -238,6 +240,7 @@ ...@@ -238,6 +240,7 @@
'test/test-nack.cpp', 'test/test-nack.cpp',
'test/test-rtp.cpp', 'test/test-rtp.cpp',
'test/test-rtcp.cpp', 'test/test-rtcp.cpp',
'test/test-bitrate.cpp',
# C++ include files # C++ include files
'test/catch.hpp', 'test/catch.hpp',
'test/helpers.h' 'test/helpers.h'
......
#define MS_CLASS "RTC::RtpDataCounter"
// #define MS_LOG_DEV
#include "RTC/RtpDataCounter.h"
#include "Logger.h"
namespace RTC
{
void RateCalculator::Update(size_t size, uint64_t now)
{
MS_TRACE();
// Ignore too old data. Should never happen.
if (now < this->oldestTime)
return;
this->RemoveOldData(now);
// Set data in the index before the oldest index.
uint32_t offset = this->windowSize - 1;
uint32_t index = this->oldestIndex + offset;
if (index >= this->windowSize)
index -= this->windowSize;
this->buffer[index].count += size;
this->totalCount += size;
}
uint32_t RateCalculator::GetRate(uint64_t now)
{
MS_TRACE();
this->RemoveOldData(now);
int64_t nominalWindowSize = now - this->oldestTime;
float scale = this->scale / nominalWindowSize;
return static_cast<uint32_t>(this->totalCount * scale + 0.5f);
}
void RateCalculator::RemoveOldData(uint64_t now)
{
MS_TRACE();
uint64_t newOldestTime = now - this->windowSize;
// Should never happen.
if (newOldestTime < this->oldestTime)
{
return;
}
// We are in the same time unit (ms) as the last entry.
if (newOldestTime == this->oldestTime)
{
return;
}
// A whole window size time has elapsed since last entry. Reset the buffer.
if (newOldestTime > this->oldestTime + this->windowSize)
{
this->Reset(newOldestTime);
return;
}
while (this->oldestTime < newOldestTime)
{
const BufferItem& oldestItem = buffer[this->oldestIndex];
this->totalCount -= oldestItem.count;
this->buffer[this->oldestIndex] = BufferItem();
if (++this->oldestIndex >= this->windowSize)
this->oldestIndex = 0;
++this->oldestTime;
}
this->oldestTime = newOldestTime;
}
void RtpDataCounter::Update(RTC::RtpPacket* packet)
{
uint64_t now = DepLibUV::GetTime();
this->packets++;
this->bytes += packet->GetSize();
this->rate.Update(packet->GetSize(), now);
}
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论