- cleanups

This commit is contained in:
Dmytro Bogovych 2025-09-26 12:55:19 +03:00
parent 4e00f659e3
commit eec5aabc42
6 changed files with 87 additions and 156 deletions

View File

@ -481,41 +481,6 @@ void AgentImpl::processWaitForEvent(JsonCpp::Value &request, JsonCpp::Value &ans
answer["status"] = Status_Ok;
}
#if defined(USE_PVQA_LIBRARY)
/*static JsonCpp::Value CsvReportToJson(const std::string& report)
{
JsonCpp::Value detectorValues;
std::istringstream iss(report);
CsvReader reader(iss);
std::vector<std::string> cells;
if (reader.readLine(cells))
{
JsonCpp::Value detectorNames;
for (size_t nameIndex = 0; nameIndex < cells.size(); nameIndex++)
detectorNames[static_cast<int>(nameIndex)] = strx::trim(cells[nameIndex]);
// Put first line name of columns
detectorValues[0] = detectorNames;
int rowIndex = 1;
while (reader.readLine(cells))
{
// Skip last column for now
JsonCpp::Value row;
for (size_t valueIndex = 0; valueIndex < cells.size(); valueIndex++)
{
bool isFloat = true;
float v = strx::toFloat(cells[valueIndex], 0.0, &isFloat);
if (isFloat)
row[static_cast<int>(valueIndex)] = static_cast<double>(v);
else
row[static_cast<int>(valueIndex)] = cells[valueIndex];
}
detectorValues[rowIndex++] = row;
}
}
return detectorValues;
}*/
#endif
void AgentImpl::processGetMediaStats(JsonCpp::Value& request, JsonCpp::Value& answer)
{

View File

@ -445,7 +445,6 @@ void Session::getSessionInfo(Session::InfoOptions options, VariantMap& info)
MT::Statistics stat;
// Iterate all session providers
stat.reset();
Stream* media = nullptr;
for (Stream& stream: mStreamList)
{
@ -469,7 +468,7 @@ void Session::getSessionInfo(Session::InfoOptions options, VariantMap& info)
info[SessionInfo_SentRtp] = static_cast<int>(stat.mSentRtp);
info[SessionInfo_SentRtcp] = static_cast<int>(stat.mSentRtcp);
if (stat.mFirstRtpTime)
info[SessionInfo_Duration] = static_cast<int>(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - *(stat.mFirstRtpTime)).count());
info[SessionInfo_Duration] = static_cast<int>(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - *(stat.mFirstRtpTime)).count());
else
info[SessionInfo_Duration] = 0;

View File

@ -157,4 +157,8 @@ private:
std::unordered_map<V, K, HashV, EqV> reverse_;
};
#include <chrono>
typedef std::chrono::steady_clock::time_point timepoint_t;
typedef std::chrono::steady_clock monoclock_t;
#endif

View File

@ -347,8 +347,8 @@ void AudioStream::dataArrived(PDatagramSocket s, const void* buffer, int length,
mStat.mReceived += length;
if (RtpHelper::isRtp(mReceiveBuffer, receiveLength))
{
if (!mStat.mFirstRtpTime.is_initialized())
mStat.mFirstRtpTime = std::chrono::system_clock::now();
if (!mStat.mFirstRtpTime)
mStat.mFirstRtpTime = std::chrono::steady_clock::now();
mStat.mReceivedRtp++;
}
else

View File

@ -1,9 +1,7 @@
#include <math.h>
#include <cmath>
#include <iostream>
#include "MT_Statistics.h"
#include "audio/Audio_Interface.h"
#include "helper/HL_Log.h"
#define LOG_SUBSYSTEM "Statistics"
using namespace MT;
@ -14,7 +12,7 @@ void JitterStatistics::process(jrtplib::RTPPacket* packet, int rate)
uint32_t timestamp = packet->GetTimestamp();
jrtplib::RTPTime receiveTime = packet->GetReceiveTime();
if (!mLastJitter.is_initialized())
if (!mLastJitter)
{
// First packet
mReceiveTime = receiveTime;
@ -69,40 +67,10 @@ void JitterStatistics::process(jrtplib::RTPPacket* packet, int rate)
Statistics::Statistics()
:mReceived(0), mSent(0), mReceivedRtp(0), mSentRtp(0),
mReceivedRtcp(0), mSentRtcp(0), mDuplicatedRtp(0), mOldRtp(0), mIllegalRtp(0),
mPacketLoss(0), mJitter(0.0), mAudioTime(0), mDecodedSize(0), mSsrc(0), mPacketDropped(0)
{
mBitrateSwitchCounter = 0;
memset(mLoss, 0, sizeof mLoss);
// It is to keep track of statistics instance via grep | wc -l
//ICELogDebug(<< "Create statistics instance.");
}
{}
Statistics::~Statistics()
{
}
void Statistics::reset()
{
mReceived = 0;
mSent = 0;
mReceivedRtp = 0;
mSentRtp = 0;
mReceivedRtcp = 0;
mSentRtcp = 0;
mDuplicatedRtp = 0;
mOldRtp = 0;
mPacketLoss = 0;
mIllegalRtp = 0;
mJitter = 0.0;
mAudioTime = 0;
mPacketDropped = 0;
mDecodedSize = 0;
memset(mLoss, 0, sizeof mLoss);
}
{}
void Statistics::calculateBurstr(double* burstr, double* lossr) const
{
@ -131,7 +99,7 @@ void Statistics::calculateBurstr(double* burstr, double* lossr) const
}
else
*burstr = 0;
//printf("total loss: %d\n", lost);
if (mReceivedRtp > 0)
*lossr = (double)((double)lost / (double)mReceivedRtp);
else
@ -173,17 +141,17 @@ double Statistics::calculateMos(double maximalMos) const
Statistics& Statistics::operator += (const Statistics& src)
{
mReceived += src.mReceived;
mSent += src.mSent;
mReceivedRtp += src.mReceivedRtp;
mSentRtp += src.mSentRtp;
mReceivedRtcp += src.mReceivedRtcp;
mSentRtcp += src.mSentRtcp;
mDuplicatedRtp += src.mDuplicatedRtp;
mOldRtp += src.mOldRtp;
mPacketLoss += src.mPacketLoss;
mPacketDropped += src.mPacketDropped;
mAudioTime += src.mAudioTime;
mReceived += src.mReceived;
mSent += src.mSent;
mReceivedRtp += src.mReceivedRtp;
mSentRtp += src.mSentRtp;
mReceivedRtcp += src.mReceivedRtcp;
mSentRtcp += src.mSentRtcp;
mDuplicatedRtp += src.mDuplicatedRtp;
mOldRtp += src.mOldRtp;
mPacketLoss += src.mPacketLoss;
mPacketDropped += src.mPacketDropped;
mAudioTime += src.mAudioTime;
for (auto codecStat: src.mCodecCount)
@ -194,49 +162,49 @@ Statistics& Statistics::operator += (const Statistics& src)
mCodecCount[codecStat.first] += codecStat.second;
}
mJitter = src.mJitter;
mRttDelay = src.mRttDelay;
mDecodingInterval = src.mDecodingInterval;
mDecodeRequested = src.mDecodeRequested;
mJitter = src.mJitter;
mRttDelay = src.mRttDelay;
mDecodingInterval = src.mDecodingInterval;
mDecodeRequested = src.mDecodeRequested;
if (!src.mCodecName.empty())
mCodecName = src.mCodecName;
// Find minimal
if (mFirstRtpTime.is_initialized())
if (mFirstRtpTime)
{
if (src.mFirstRtpTime.is_initialized())
if (src.mFirstRtpTime)
{
if (mFirstRtpTime.value() > src.mFirstRtpTime.value())
mFirstRtpTime = src.mFirstRtpTime;
}
}
else
if (src.mFirstRtpTime.is_initialized())
mFirstRtpTime = src.mFirstRtpTime;
if (src.mFirstRtpTime)
mFirstRtpTime = src.mFirstRtpTime;
mBitrateSwitchCounter += src.mBitrateSwitchCounter;
mRemotePeer = src.mRemotePeer;
mSsrc = src.mSsrc;
mBitrateSwitchCounter += src.mBitrateSwitchCounter;
mRemotePeer = src.mRemotePeer;
mSsrc = src.mSsrc;
return *this;
}
Statistics& Statistics::operator -= (const Statistics& src)
{
mReceived -= src.mReceived;
mSent -= src.mSent;
mReceivedRtp -= src.mReceivedRtp;
mIllegalRtp -= src.mIllegalRtp;
mSentRtp -= src.mSentRtp;
mReceivedRtcp -= src.mReceivedRtcp;
mSentRtcp -= src.mSentRtcp;
mDuplicatedRtp -= src.mDuplicatedRtp;
mOldRtp -= src.mOldRtp;
mPacketLoss -= src.mPacketLoss;
mPacketDropped -= src.mPacketDropped;
mReceived -= src.mReceived;
mSent -= src.mSent;
mReceivedRtp -= src.mReceivedRtp;
mIllegalRtp -= src.mIllegalRtp;
mSentRtp -= src.mSentRtp;
mReceivedRtcp -= src.mReceivedRtcp;
mSentRtcp -= src.mSentRtcp;
mDuplicatedRtp -= src.mDuplicatedRtp;
mOldRtp -= src.mOldRtp;
mPacketLoss -= src.mPacketLoss;
mPacketDropped -= src.mPacketDropped;
mAudioTime -= src.mAudioTime;
mAudioTime -= src.mAudioTime;
for (auto codecStat: src.mCodecCount)
{
if (mCodecCount.find(codecStat.first) != mCodecCount.end())
@ -250,13 +218,13 @@ Statistics& Statistics::operator -= (const Statistics& src)
std::string Statistics::toString() const
{
std::ostringstream oss;
oss << "Received: " << mReceivedRtp
<< ", lost: " << mPacketLoss
<< ", dropped: " << mPacketDropped
<< ", sent: " << mSentRtp
<< ", decoding interval: " << mDecodingInterval.average()
<< ", decode requested: " << mDecodeRequested.average()
<< ", packet interval: " << mPacketInterval.average();
oss << "Received: " << mReceivedRtp
<< ", lost: " << mPacketLoss
<< ", dropped: " << mPacketDropped
<< ", sent: " << mSentRtp
<< ", decoding interval: " << mDecodingInterval.average()
<< ", decode requested: " << mDecodeRequested.average()
<< ", packet interval: " << mPacketInterval.average();
return oss.str();
}

View File

@ -3,15 +3,17 @@
#include <chrono>
#include <map>
#include <optional>
#include <array>
#include "audio/Audio_DataWindow.h"
#include "helper/HL_Optional.hpp"
#include "helper/HL_Statistics.h"
#include "helper/HL_Types.h"
#include "jrtplib/src/rtptimeutilities.h"
#include "jrtplib/src/rtppacket.h"
using std::experimental::optional;
namespace MT
{
@ -33,59 +35,56 @@ public:
protected:
// Jitter calculation
jrtplib::RTPTime mReceiveTime = jrtplib::RTPTime(0,0);
jrtplib::RTPTime mReceiveTime = jrtplib::RTPTime(0,0);
// Last timestamp from packet in units
uint32_t mReceiveTimestamp = 0;
uint32_t mReceiveTimestamp = 0;
// It is classic jitter value in units
optional<float> mLastJitter;
std::optional<float> mLastJitter;
// Some statistics for jitter value in seconds
TestResult<float> mJitter;
TestResult<float> mJitter;
// Maximal delta in seconds
float mMaxDelta = 0.0f;
float mMaxDelta = 0.0f;
};
class Statistics
{
public:
size_t mReceived = 0, // Received traffic in bytes
mSent = 0, // Sent traffic in bytes
mReceivedRtp = 0, // Number of received rtp packets
mSentRtp = 0, // Number of sent rtp packets
mReceivedRtcp = 0, // Number of received rtcp packets
mSentRtcp = 0, // Number of sent rtcp packets
mDuplicatedRtp = 0, // Number of received duplicated rtp packets
mOldRtp = 0, // Number of late rtp packets
mPacketLoss = 0, // Number of lost packets
mPacketDropped = 0, // Number of dropped packets (due to time unsync when playing)б
mIllegalRtp = 0; // Number of rtp packets with bad payload type
size_t mReceived = 0, // Received traffic in bytes
mSent = 0, // Sent traffic in bytes
mReceivedRtp = 0, // Number of received rtp packets
mSentRtp = 0, // Number of sent rtp packets
mReceivedRtcp = 0, // Number of received rtcp packets
mSentRtcp = 0, // Number of sent rtcp packets
mDuplicatedRtp = 0, // Number of received duplicated rtp packets
mOldRtp = 0, // Number of late rtp packets
mPacketLoss = 0, // Number of lost packets
mPacketDropped = 0, // Number of dropped packets (due to time unsync when playing)б
mIllegalRtp = 0; // Number of rtp packets with bad payload type
TestResult<float> mDecodingInterval, // Average interval on call to packet decode
mDecodeRequested, // Average amount of requested audio frames to play
mPacketInterval; // Average interval between packet adding to jitter buffer
TestResult<float> mDecodingInterval, // Average interval on call to packet decode
mDecodeRequested, // Average amount of requested audio frames to play
mPacketInterval; // Average interval between packet adding to jitter buffer
int mLoss[128] = {0}; // Every item is number of loss of corresping length
size_t mAudioTime = 0; // Decoded/found time in milliseconds
size_t mDecodedSize = 0; // Number of decoded bytes
uint16_t mSsrc = 0; // Last known SSRC ID in a RTP stream
ice::NetworkAddress mRemotePeer; // Last known remote RTP address
std::array<float, 128> mLoss = {0}; // Every item is number of loss of corresping length
size_t mAudioTime = 0; // Decoded/found time in milliseconds
size_t mDecodedSize = 0; // Number of decoded bytes
uint16_t mSsrc = 0; // Last known SSRC ID in a RTP stream
ice::NetworkAddress mRemotePeer; // Last known remote RTP address
// AMR codec bitrate switch counter
int mBitrateSwitchCounter = 0;
std::string mCodecName;
float mJitter = 0.0f; // Jitter
TestResult<float> mRttDelay; // RTT delay
int mBitrateSwitchCounter = 0;
std::string mCodecName;
float mJitter = 0.0f; // Jitter
TestResult<float> mRttDelay; // RTT delay
// Timestamp when first RTP packet has arrived
optional<std::chrono::system_clock::time_point> mFirstRtpTime;
std::optional<timepoint_t> mFirstRtpTime;
std::map<int, int> mCodecCount; // Stats on used codecs
std::map<int, int> mCodecCount; // Stats on used codecs
// It is to calculate network MOS
void calculateBurstr(double* burstr, double* loss) const;
@ -98,11 +97,7 @@ public:
Statistics& operator += (const Statistics& src);
Statistics& operator -= (const Statistics& src);
float mNetworkMos = 0.0;
#if defined(USE_PVQA_LIBRARY) && !defined(PVQA_SERVER)
float mPvqaMos = 0.0;
std::string mPvqaReport;
#endif
float mNetworkMos = 0.0f;
std::string toString() const;
};