[feat] use the io statistics module to collect network information

This commit is contained in:
dijunkun
2024-11-29 17:53:30 +08:00
parent 11d84068a4
commit daef6f19dc
34 changed files with 405 additions and 102 deletions

View File

@@ -4,6 +4,9 @@
RtpAudioReceiver::RtpAudioReceiver() {}
RtpAudioReceiver::RtpAudioReceiver(std::shared_ptr<IOStatistics> io_statistics)
: io_statistics_(io_statistics) {}
RtpAudioReceiver::~RtpAudioReceiver() {
if (rtp_statistics_) {
rtp_statistics_->Stop();
@@ -16,8 +19,17 @@ void RtpAudioReceiver::InsertRtpPacket(RtpPacket& rtp_packet) {
rtp_statistics_->Start();
}
last_recv_bytes_ = (uint32_t)rtp_packet.Size();
total_rtp_payload_recv_ += (uint32_t)rtp_packet.PayloadSize();
total_rtp_packets_recv_++;
if (rtp_statistics_) {
rtp_statistics_->UpdateReceiveBytes((uint32_t)rtp_packet.Size());
rtp_statistics_->UpdateReceiveBytes(last_recv_bytes_);
}
if (io_statistics_) {
io_statistics_->UpdateAudioInboundBytes(last_recv_bytes_);
io_statistics_->IncrementAudioInboundRtpPacketCount();
}
if (CheckIsTimeSendRR()) {

View File

@@ -9,6 +9,7 @@
#include <functional>
#include "io_statistics.h"
#include "rtcp_receiver_report.h"
#include "rtp_codec.h"
#include "rtp_statistics.h"
@@ -16,6 +17,7 @@
class RtpAudioReceiver {
public:
RtpAudioReceiver();
RtpAudioReceiver(std::shared_ptr<IOStatistics> io_statistics);
~RtpAudioReceiver();
public:
@@ -38,6 +40,10 @@ class RtpAudioReceiver {
private:
std::unique_ptr<RtpStatistics> rtp_statistics_ = nullptr;
std::shared_ptr<IOStatistics> io_statistics_ = nullptr;
uint32_t last_recv_bytes_ = 0;
uint32_t total_rtp_payload_recv_ = 0;
uint32_t total_rtp_packets_recv_ = 0;
uint32_t last_send_rtcp_rr_packet_ts_ = 0;
std::function<int(const char*, size_t)> data_send_func_ = nullptr;
};

View File

@@ -8,6 +8,9 @@
RtpAudioSender::RtpAudioSender() {}
RtpAudioSender::RtpAudioSender(std::shared_ptr<IOStatistics> io_statistics)
: io_statistics_(io_statistics) {}
RtpAudioSender::~RtpAudioSender() {
if (rtp_statistics_) {
rtp_statistics_->Stop();
@@ -43,8 +46,13 @@ int RtpAudioSender::SendRtpPacket(RtpPacket& rtp_packet) {
}
last_send_bytes_ += (uint32_t)rtp_packet.Size();
total_rtp_packets_sent_++;
total_rtp_payload_sent_ += (uint32_t)rtp_packet.PayloadSize();
total_rtp_packets_sent_++;
if (io_statistics_) {
io_statistics_->UpdateAudioOutboundBytes(last_send_bytes_);
io_statistics_->IncrementAudioOutboundRtpPacketCount();
}
if (CheckIsTimeSendSR()) {
RtcpSenderReport rtcp_sr;

View File

@@ -9,6 +9,7 @@
#include <functional>
#include "io_statistics.h"
#include "ringbuffer.h"
#include "rtcp_sender_report.h"
#include "rtp_packet.h"
@@ -18,6 +19,7 @@
class RtpAudioSender : public ThreadBase {
public:
RtpAudioSender();
RtpAudioSender(std::shared_ptr<IOStatistics> io_statistics);
virtual ~RtpAudioSender();
public:
@@ -38,10 +40,11 @@ class RtpAudioSender : public ThreadBase {
std::function<int(const char *, size_t)> data_send_func_ = nullptr;
RingBuffer<RtpPacket> rtp_packe_queue_;
std::unique_ptr<RtpStatistics> rtp_statistics_ = nullptr;
std::shared_ptr<IOStatistics> io_statistics_ = nullptr;
uint32_t last_send_bytes_ = 0;
uint32_t last_send_rtcp_sr_packet_ts_ = 0;
uint32_t total_rtp_packets_sent_ = 0;
uint32_t total_rtp_payload_sent_ = 0;
uint32_t total_rtp_packets_sent_ = 0;
uint32_t last_send_rtcp_sr_packet_ts_ = 0;
};
#endif

View File

@@ -4,6 +4,9 @@
RtpDataReceiver::RtpDataReceiver() {}
RtpDataReceiver::RtpDataReceiver(std::shared_ptr<IOStatistics> io_statistics)
: io_statistics_(io_statistics) {}
RtpDataReceiver::~RtpDataReceiver() {
if (rtp_statistics_) {
rtp_statistics_->Stop();
@@ -16,8 +19,17 @@ void RtpDataReceiver::InsertRtpPacket(RtpPacket& rtp_packet) {
rtp_statistics_->Start();
}
last_recv_bytes_ = (uint32_t)rtp_packet.Size();
total_rtp_payload_recv_ += (uint32_t)rtp_packet.PayloadSize();
total_rtp_packets_recv_++;
if (rtp_statistics_) {
rtp_statistics_->UpdateReceiveBytes((uint32_t)rtp_packet.Size());
rtp_statistics_->UpdateReceiveBytes(last_recv_bytes_);
}
if (io_statistics_) {
io_statistics_->UpdateDataInboundBytes(last_recv_bytes_);
io_statistics_->IncrementDataInboundRtpPacketCount();
}
if (CheckIsTimeSendRR()) {

View File

@@ -3,6 +3,7 @@
#include <functional>
#include "io_statistics.h"
#include "rtcp_receiver_report.h"
#include "rtp_codec.h"
#include "rtp_statistics.h"
@@ -10,6 +11,7 @@
class RtpDataReceiver {
public:
RtpDataReceiver();
RtpDataReceiver(std::shared_ptr<IOStatistics> io_statistics);
~RtpDataReceiver();
public:
@@ -32,6 +34,11 @@ class RtpDataReceiver {
private:
std::unique_ptr<RtpStatistics> rtp_statistics_ = nullptr;
std::shared_ptr<IOStatistics> io_statistics_ = nullptr;
uint32_t last_recv_bytes_ = 0;
uint32_t total_rtp_payload_recv_ = 0;
uint32_t total_rtp_packets_recv_ = 0;
uint32_t last_send_rtcp_rr_packet_ts_ = 0;
std::function<int(const char*, size_t)> data_send_func_ = nullptr;
};

View File

@@ -8,6 +8,9 @@
RtpDataSender::RtpDataSender() {}
RtpDataSender::RtpDataSender(std::shared_ptr<IOStatistics> io_statistics)
: io_statistics_(io_statistics) {}
RtpDataSender::~RtpDataSender() {
if (rtp_statistics_) {
rtp_statistics_->Stop();
@@ -43,8 +46,13 @@ int RtpDataSender::SendRtpPacket(RtpPacket& rtp_packet) {
}
last_send_bytes_ += (uint32_t)rtp_packet.Size();
total_rtp_packets_sent_++;
total_rtp_payload_sent_ += (uint32_t)rtp_packet.PayloadSize();
total_rtp_packets_sent_++;
if (io_statistics_) {
io_statistics_->UpdateDataOutboundBytes(last_send_bytes_);
io_statistics_->IncrementDataOutboundRtpPacketCount();
}
if (CheckIsTimeSendSR()) {
RtcpSenderReport rtcp_sr;

View File

@@ -9,6 +9,7 @@
#include <functional>
#include "io_statistics.h"
#include "ringbuffer.h"
#include "rtcp_sender_report.h"
#include "rtp_packet.h"
@@ -18,6 +19,7 @@
class RtpDataSender : public ThreadBase {
public:
RtpDataSender();
RtpDataSender(std::shared_ptr<IOStatistics> io_statistics);
virtual ~RtpDataSender();
public:
@@ -37,11 +39,14 @@ class RtpDataSender : public ThreadBase {
private:
std::function<int(const char *, size_t)> data_send_func_ = nullptr;
RingBuffer<RtpPacket> rtp_packe_queue_;
private:
std::unique_ptr<RtpStatistics> rtp_statistics_ = nullptr;
std::shared_ptr<IOStatistics> io_statistics_ = nullptr;
uint32_t last_send_bytes_ = 0;
uint32_t last_send_rtcp_sr_packet_ts_ = 0;
uint32_t total_rtp_packets_sent_ = 0;
uint32_t total_rtp_payload_sent_ = 0;
uint32_t total_rtp_packets_sent_ = 0;
uint32_t last_send_rtcp_sr_packet_ts_ = 0;
};
#endif

View File

@@ -7,6 +7,9 @@
RtpVideoReceiver::RtpVideoReceiver() {}
RtpVideoReceiver::RtpVideoReceiver(std::shared_ptr<IOStatistics> io_statistics)
: io_statistics_(io_statistics) {}
RtpVideoReceiver::~RtpVideoReceiver() {
if (rtp_statistics_) {
rtp_statistics_->Stop();
@@ -19,8 +22,17 @@ void RtpVideoReceiver::InsertRtpPacket(RtpPacket& rtp_packet) {
rtp_statistics_->Start();
}
last_recv_bytes_ = (uint32_t)rtp_packet.PayloadSize();
total_rtp_payload_recv_ += (uint32_t)rtp_packet.PayloadSize();
total_rtp_packets_recv_++;
if (rtp_statistics_) {
rtp_statistics_->UpdateReceiveBytes((uint32_t)rtp_packet.Size());
rtp_statistics_->UpdateReceiveBytes(last_recv_bytes_);
}
if (io_statistics_) {
io_statistics_->UpdateVideoInboundBytes(last_recv_bytes_);
io_statistics_->IncrementVideoInboundRtpPacketCount();
}
if (CheckIsTimeSendRR()) {

View File

@@ -7,6 +7,7 @@
#include <set>
#include "fec_decoder.h"
#include "io_statistics.h"
#include "ringbuffer.h"
#include "rtcp_receiver_report.h"
#include "rtp_codec.h"
@@ -17,6 +18,7 @@
class RtpVideoReceiver : public ThreadBase {
public:
RtpVideoReceiver();
RtpVideoReceiver(std::shared_ptr<IOStatistics> io_statistics);
virtual ~RtpVideoReceiver();
public:
@@ -53,6 +55,11 @@ class RtpVideoReceiver : public ThreadBase {
private:
std::unique_ptr<RtpStatistics> rtp_statistics_ = nullptr;
std::shared_ptr<IOStatistics> io_statistics_ = nullptr;
uint32_t last_recv_bytes_ = 0;
uint32_t total_rtp_packets_recv_ = 0;
uint32_t total_rtp_payload_recv_ = 0;
uint32_t last_send_rtcp_rr_packet_ts_ = 0;
std::function<int(const char*, size_t)> data_send_func_ = nullptr;

View File

@@ -8,6 +8,9 @@
RtpVideoSender::RtpVideoSender() {}
RtpVideoSender::RtpVideoSender(std::shared_ptr<IOStatistics> io_statistics)
: io_statistics_(io_statistics) {}
RtpVideoSender::~RtpVideoSender() {
if (rtp_statistics_) {
rtp_statistics_->Stop();
@@ -43,8 +46,13 @@ int RtpVideoSender::SendRtpPacket(RtpPacket& rtp_packet) {
}
last_send_bytes_ += (uint32_t)rtp_packet.Size();
total_rtp_packets_sent_++;
total_rtp_payload_sent_ += (uint32_t)rtp_packet.PayloadSize();
total_rtp_packets_sent_++;
if (io_statistics_) {
io_statistics_->UpdateVideoOutboundBytes(last_send_bytes_);
io_statistics_->IncrementVideoOutboundRtpPacketCount();
}
if (CheckIsTimeSendSR()) {
RtcpSenderReport rtcp_sr;

View File

@@ -3,6 +3,7 @@
#include <functional>
#include "io_statistics.h"
#include "ringbuffer.h"
#include "rtcp_sender_report.h"
#include "rtp_packet.h"
@@ -12,6 +13,7 @@
class RtpVideoSender : public ThreadBase {
public:
RtpVideoSender();
RtpVideoSender(std::shared_ptr<IOStatistics> io_statistics);
virtual ~RtpVideoSender();
public:
@@ -33,10 +35,11 @@ class RtpVideoSender : public ThreadBase {
private:
std::unique_ptr<RtpStatistics> rtp_statistics_ = nullptr;
std::shared_ptr<IOStatistics> io_statistics_ = nullptr;
uint32_t last_send_bytes_ = 0;
uint32_t last_send_rtcp_sr_packet_ts_ = 0;
uint32_t total_rtp_packets_sent_ = 0;
uint32_t total_rtp_payload_sent_ = 0;
uint32_t total_rtp_packets_sent_ = 0;
};
#endif

View File

@@ -14,6 +14,22 @@ void RtpStatistics::UpdateReceiveBytes(uint32_t received_bytes) {
received_bytes_ += received_bytes;
}
void RtpStatistics::UpdatePacketLossRate(uint16_t seq_num) {
if (last_received_seq_num_ != 0) {
if (last_received_seq_num_ < seq_num) {
// seq wrap
if (seq_num - last_received_seq_num_ > 0x8000) {
lost_packets_num_ += 0xffff - last_received_seq_num_ + seq_num + 1;
} else {
lost_packets_num_ += seq_num - last_received_seq_num_ - 1;
}
} else if (last_received_seq_num_ > seq_num) {
lost_packets_num_ += 0xffff - last_received_seq_num_ + seq_num + 1;
}
}
last_received_seq_num_ = seq_num;
}
bool RtpStatistics::Process() {
if (!sent_bytes_) {
// LOG_INFO("rtp statistics: Send [{} bps]", sent_bytes_);

View File

@@ -9,8 +9,12 @@ class RtpStatistics : public ThreadBase {
virtual ~RtpStatistics();
public:
// send side
void UpdateSentBytes(uint32_t sent_bytes);
// receive side
void UpdateReceiveBytes(uint32_t received_bytes);
void UpdatePacketLossRate(uint16_t seq_num);
private:
bool Process();
@@ -18,6 +22,8 @@ class RtpStatistics : public ThreadBase {
private:
uint32_t sent_bytes_ = 0;
uint32_t received_bytes_ = 0;
uint16_t last_received_seq_num_ = 0;
uint32_t lost_packets_num_ = 0;
};
#endif

View File

@@ -1,5 +0,0 @@
#include "rtp_transceiver.h"
RtpTransceiver::RtpTransceiver() {}
RtpTransceiver::~RtpTransceiver() {}

View File

@@ -1,19 +0,0 @@
#ifndef _RTP_TRANSCEIVER_H_
#define _RTP_TRANSCEIVER_H_
#include <cstddef>
#include <functional>
class RtpTransceiver {
public:
RtpTransceiver();
~RtpTransceiver();
public:
virtual void SetSendDataFunc(
std::function<int(const char *, size_t)> data_send_func) = 0;
virtual void OnReceiveData(const char *data, size_t size) = 0;
};
#endif