From c0c2b18b8bd22e9770ca8e46ab037745cc27189b Mon Sep 17 00:00:00 2001 From: dijunkun Date: Wed, 4 Sep 2024 17:31:08 +0800 Subject: [PATCH] [feat] add statistics module --- src/common/sequence_number_unwrapper.h | 70 ++++++++++++++++++++++++++ src/statistics/receiver_statistics.cpp | 7 +++ src/statistics/receiver_statistics.h | 21 ++++++++ src/statistics/sender_statistics.cpp | 7 +++ src/statistics/sender_statistics.h | 21 ++++++++ src/statistics/statistics_base.cpp | 5 ++ src/statistics/statistics_base.h | 19 +++++++ src/transmission/ice_transmission.h | 6 +++ xmake.lua | 8 ++- 9 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 src/common/sequence_number_unwrapper.h create mode 100644 src/statistics/receiver_statistics.cpp create mode 100644 src/statistics/receiver_statistics.h create mode 100644 src/statistics/sender_statistics.cpp create mode 100644 src/statistics/sender_statistics.h create mode 100644 src/statistics/statistics_base.cpp create mode 100644 src/statistics/statistics_base.h diff --git a/src/common/sequence_number_unwrapper.h b/src/common/sequence_number_unwrapper.h new file mode 100644 index 0000000..a63617f --- /dev/null +++ b/src/common/sequence_number_unwrapper.h @@ -0,0 +1,70 @@ +/* + * @Author: DI JUNKUN + * @Date: 2024-08-21 + * Copyright (c) 2024 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _SEQUENCE_NUMBER_UNWRAPPER_H_ +#define _SEQUENCE_NUMBER_UNWRAPPER_H_ + +#include + +#include +#include + +// A sequence number unwrapper where the first unwrapped value equals the +// first value being unwrapped. +template +class SeqNumUnwrapper { + static_assert( + std::is_unsigned::value && + std::numeric_limits::max() < std::numeric_limits::max(), + "Type unwrapped must be an unsigned integer smaller than int64_t."); + + public: + // Unwraps `value` and updates the internal state of the unwrapper. + int64_t Unwrap(T value) { + if (!last_value_) { + last_unwrapped_ = {value}; + } else { + last_unwrapped_ += Delta(*last_value_, value); + } + + last_value_ = value; + return last_unwrapped_; + } + + // Returns the `value` without updating the internal state of the unwrapper. + int64_t PeekUnwrap(T value) const { + if (!last_value_) { + return value; + } + return last_unwrapped_ + Delta(*last_value_, value); + } + + // Resets the unwrapper to its initial state. Unwrapped sequence numbers will + // being at 0 after resetting. + void Reset() { + last_unwrapped_ = 0; + last_value_.reset(); + } + + private: + static int64_t Delta(T last_value, T new_value) { + constexpr int64_t kBackwardAdjustment = + M == 0 ? int64_t{std::numeric_limits::max()} + 1 : M; + int64_t result = ForwardDiff(last_value, new_value); + if (!AheadOrAt(new_value, last_value)) { + result -= kBackwardAdjustment; + } + return result; + } + + int64_t last_unwrapped_ = 0; + std::unique_ptr last_value_; +}; + +using RtpTimestampUnwrapper = SeqNumUnwrapper; +using RtpSeqNumUnwrapper = SeqNumUnwrapper; + +#endif \ No newline at end of file diff --git a/src/statistics/receiver_statistics.cpp b/src/statistics/receiver_statistics.cpp new file mode 100644 index 0000000..c873ba9 --- /dev/null +++ b/src/statistics/receiver_statistics.cpp @@ -0,0 +1,7 @@ +#include "receiver_statistics.h" + +#include "log.h" + +ReceiverStatistics::ReceiverStatistics() {} + +ReceiverStatistics::~ReceiverStatistics() {} \ No newline at end of file diff --git a/src/statistics/receiver_statistics.h b/src/statistics/receiver_statistics.h new file mode 100644 index 0000000..590554e --- /dev/null +++ b/src/statistics/receiver_statistics.h @@ -0,0 +1,21 @@ +/* + * @Author: DI JUNKUN + * @Date: 2024-09-04 + * Copyright (c) 2024 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _RECEIVER_STATISTICS_H_ +#define _RECEIVER_STATISTICS_H_ + +#include "statistics_base.h" + +class ReceiverStatistics : public StatisticsBase { + public: + ReceiverStatistics(); + virtual ~ReceiverStatistics(); + + private: + /* data */ +}; + +#endif \ No newline at end of file diff --git a/src/statistics/sender_statistics.cpp b/src/statistics/sender_statistics.cpp new file mode 100644 index 0000000..27b53d9 --- /dev/null +++ b/src/statistics/sender_statistics.cpp @@ -0,0 +1,7 @@ +#include "sender_statistics.h" + +#include "log.h" + +SenderStatistics::SenderStatistics() {} + +SenderStatistics::~SenderStatistics() {} \ No newline at end of file diff --git a/src/statistics/sender_statistics.h b/src/statistics/sender_statistics.h new file mode 100644 index 0000000..db0bcf3 --- /dev/null +++ b/src/statistics/sender_statistics.h @@ -0,0 +1,21 @@ +/* + * @Author: DI JUNKUN + * @Date: 2024-09-04 + * Copyright (c) 2024 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _SENDER_STATISTICS_H_ +#define _SENDER_STATISTICS_H_ + +#include "statistics_base.h" + +class SenderStatistics : public StatisticsBase { + public: + SenderStatistics(); + virtual ~SenderStatistics(); + + private: + /* data */ +}; + +#endif \ No newline at end of file diff --git a/src/statistics/statistics_base.cpp b/src/statistics/statistics_base.cpp new file mode 100644 index 0000000..0f2cc30 --- /dev/null +++ b/src/statistics/statistics_base.cpp @@ -0,0 +1,5 @@ +#include "statistics_base.h" + +StatisticsBase::StatisticsBase() {} + +StatisticsBase::~StatisticsBase() {} \ No newline at end of file diff --git a/src/statistics/statistics_base.h b/src/statistics/statistics_base.h new file mode 100644 index 0000000..8a89ede --- /dev/null +++ b/src/statistics/statistics_base.h @@ -0,0 +1,19 @@ +/* + * @Author: DI JUNKUN + * @Date: 2024-09-04 + * Copyright (c) 2024 by DI JUNKUN, All Rights Reserved. + */ + +#ifndef _STATISTICS_BASE_H_ +#define _STATISTICS_BASE_H_ + +class StatisticsBase { + public: + StatisticsBase(); + virtual ~StatisticsBase(); + + private: + /* data */ +}; + +#endif \ No newline at end of file diff --git a/src/transmission/ice_transmission.h b/src/transmission/ice_transmission.h index 12c5592..ac72ecc 100644 --- a/src/transmission/ice_transmission.h +++ b/src/transmission/ice_transmission.h @@ -11,6 +11,7 @@ #include "congestion_control.h" #include "ice_agent.h" +#include "receiver_statistics.h" #include "ringbuffer.h" #include "rtp_audio_receiver.h" #include "rtp_audio_sender.h" @@ -20,6 +21,7 @@ #include "rtp_packet.h" #include "rtp_video_receiver.h" #include "rtp_video_sender.h" +#include "sender_statistics.h" #include "ws_transmission.h" class IceTransmission { @@ -150,6 +152,10 @@ class IceTransmission { bool start_send_packet_ = false; uint32_t last_complete_frame_ts_ = 0; + + private: + std::unique_ptr sender_statistics_ = nullptr; + std::unique_ptr receiver_statistics_ = nullptr; }; #endif \ No newline at end of file diff --git a/xmake.lua b/xmake.lua index bd0cb75..78133ec 100644 --- a/xmake.lua +++ b/xmake.lua @@ -170,9 +170,15 @@ target("qos") add_files("src/qos/kcp/*.c") add_includedirs("src/qos/kcp", {public = true}) +target("statistics") + set_kind("object") + add_deps("log") + add_files("src/statistics/*.cpp") + add_includedirs("src/statistics", {public = true}) + target("transmission") set_kind("object") - add_deps("log", "ws", "ice", "qos", "rtp", "rtcp") + add_deps("log", "ws", "ice", "qos", "rtp", "rtcp", "statistics") add_files("src/transmission/*.cpp") add_includedirs("src/ws", "src/ice", "src/qos", {public = true})