diff --git a/src/rtp/rtp_video_receive_statistics.cpp b/src/rtp/rtp_video_receive_statistics.cpp new file mode 100644 index 0000000..03ebb80 --- /dev/null +++ b/src/rtp/rtp_video_receive_statistics.cpp @@ -0,0 +1,18 @@ +#include "rtp_video_receive_statistics.h" + +#include "log.h" + +RtpVideoReceiveStatistics::RtpVideoReceiveStatistics() {} + +RtpVideoReceiveStatistics::~RtpVideoReceiveStatistics() {} + +void RtpVideoReceiveStatistics::UpdateReceiveBytes(uint32_t received_bytes) { + received_bytes_ += received_bytes; +} + +bool RtpVideoReceiveStatistics::Process() { + LOG_INFO("rtp statistics: Receive [{} bps]", received_bytes_); + received_bytes_ = 0; + std::this_thread::sleep_for(std::chrono::seconds(1)); + return true; +} \ No newline at end of file diff --git a/src/rtp/rtp_video_receive_statistics.h b/src/rtp/rtp_video_receive_statistics.h new file mode 100644 index 0000000..78ce9e6 --- /dev/null +++ b/src/rtp/rtp_video_receive_statistics.h @@ -0,0 +1,21 @@ +#ifndef _RTP_VIDEO_RECEIVE_STATISTICS_H_ +#define _RTP_VIDEO_RECEIVE_STATISTICS_H_ + +#include "thread_base.h" + +class RtpVideoReceiveStatistics : public ThreadBase { + public: + RtpVideoReceiveStatistics(); + ~RtpVideoReceiveStatistics(); + + public: + void UpdateReceiveBytes(uint32_t received_bytes); + + private: + bool Process(); + + private: + uint32_t received_bytes_ = 0; +}; + +#endif \ No newline at end of file diff --git a/src/rtp/rtp_video_receiver.cpp b/src/rtp/rtp_video_receiver.cpp index 0f74ad3..5c37886 100644 --- a/src/rtp/rtp_video_receiver.cpp +++ b/src/rtp/rtp_video_receiver.cpp @@ -9,6 +9,16 @@ RtpVideoReceiver::RtpVideoReceiver() {} RtpVideoReceiver::~RtpVideoReceiver() {} void RtpVideoReceiver::InsertRtpPacket(RtpPacket& rtp_packet) { + if (!rtp_video_receive_statistics_) { + rtp_video_receive_statistics_ = + std::make_unique(); + rtp_video_receive_statistics_->Start(); + } + + if (rtp_video_receive_statistics_) { + rtp_video_receive_statistics_->UpdateReceiveBytes(rtp_packet.Size()); + } + if (NAL_UNIT_TYPE::NALU == rtp_packet.NalUnitType()) { compelete_video_frame_queue_.push( VideoFrame(rtp_packet.Payload(), rtp_packet.Size())); @@ -93,12 +103,13 @@ bool RtpVideoReceiver::Process() { VideoFrame video_frame; compelete_video_frame_queue_.pop(video_frame); if (on_receive_complete_frame_) { - auto now_complete_frame_ts = - std::chrono::high_resolution_clock::now().time_since_epoch().count() / - 1000000; - uint32_t duration = now_complete_frame_ts - last_complete_frame_ts_; - LOG_ERROR("Duration {}", 1000 / duration); - last_complete_frame_ts_ = now_complete_frame_ts; + // auto now_complete_frame_ts = + // std::chrono::high_resolution_clock::now().time_since_epoch().count() + // / 1000000; + // uint32_t duration = now_complete_frame_ts - last_complete_frame_ts_; + // LOG_ERROR("Duration {}", 1000 / duration); + // last_complete_frame_ts_ = now_complete_frame_ts; + on_receive_complete_frame_(video_frame); } } diff --git a/src/rtp/rtp_video_receiver.h b/src/rtp/rtp_video_receiver.h index c80b62e..67a42fc 100644 --- a/src/rtp/rtp_video_receiver.h +++ b/src/rtp/rtp_video_receiver.h @@ -8,6 +8,7 @@ #include "frame.h" #include "ringbuffer.h" #include "rtp_codec.h" +#include "rtp_video_receive_statistics.h" #include "thread_base.h" class RtpVideoReceiver : public ThreadBase { @@ -35,8 +36,11 @@ class RtpVideoReceiver : public ThreadBase { uint8_t* nv12_data_ = nullptr; std::function on_receive_complete_frame_ = nullptr; uint32_t last_complete_frame_ts_ = 0; - RingBuffer compelete_video_frame_queue_; + + private: + std::unique_ptr rtp_video_receive_statistics_ = + nullptr; }; #endif diff --git a/src/rtp/rtp_video_send_statistics.cpp b/src/rtp/rtp_video_send_statistics.cpp new file mode 100644 index 0000000..c5b4f9f --- /dev/null +++ b/src/rtp/rtp_video_send_statistics.cpp @@ -0,0 +1,18 @@ +#include "rtp_video_send_statistics.h" + +#include "log.h" + +RtpVideoSendStatistics::RtpVideoSendStatistics() {} + +RtpVideoSendStatistics::~RtpVideoSendStatistics() {} + +void RtpVideoSendStatistics::UpdateSentBytes(uint32_t sent_bytes) { + sent_bytes_ += sent_bytes; +} + +bool RtpVideoSendStatistics::Process() { + LOG_INFO("rtp statistics: Send [{} bps]", sent_bytes_); + sent_bytes_ = 0; + std::this_thread::sleep_for(std::chrono::seconds(1)); + return true; +} \ No newline at end of file diff --git a/src/rtp/rtp_video_send_statistics.h b/src/rtp/rtp_video_send_statistics.h new file mode 100644 index 0000000..811764d --- /dev/null +++ b/src/rtp/rtp_video_send_statistics.h @@ -0,0 +1,21 @@ +#ifndef _RTP_VIDEO_SEND_STATISTICS_H_ +#define _RTP_VIDEO_SEND_STATISTICS_H_ + +#include "thread_base.h" + +class RtpVideoSendStatistics : public ThreadBase { + public: + RtpVideoSendStatistics(); + ~RtpVideoSendStatistics(); + + public: + void UpdateSentBytes(uint32_t sent_bytes); + + private: + bool Process(); + + private: + uint32_t sent_bytes_ = 0; +}; + +#endif \ No newline at end of file diff --git a/src/rtp/rtp_video_sender.cpp b/src/rtp/rtp_video_sender.cpp index ebffcee..762f07e 100644 --- a/src/rtp/rtp_video_sender.cpp +++ b/src/rtp/rtp_video_sender.cpp @@ -6,23 +6,36 @@ RtpVideoSender::RtpVideoSender() {} -RtpVideoSender::~RtpVideoSender() {} +RtpVideoSender::~RtpVideoSender() { rtp_video_send_statistics_->Stop(); } void RtpVideoSender::Enqueue(std::vector& rtp_packets) { + if (!rtp_video_send_statistics_) { + rtp_video_send_statistics_ = std::make_unique(); + rtp_video_send_statistics_->Start(); + } + for (auto& rtp_packet : rtp_packets) { rtp_packe_queue_.push(rtp_packet); } } bool RtpVideoSender::Process() { - for (size_t i = 0; i < 50; i++) + last_send_bytes_ = 0; + + for (size_t i = 0; i < 10; i++) if (!rtp_packe_queue_.isEmpty()) { RtpPacket rtp_packet; rtp_packe_queue_.pop(rtp_packet); if (rtp_packet_send_func_) { rtp_packet_send_func_(rtp_packet); + last_send_bytes_ += rtp_packet.Size(); } } + if (rtp_video_send_statistics_) { + rtp_video_send_statistics_->UpdateSentBytes(last_send_bytes_); + } + + std::this_thread::sleep_for(std::chrono::milliseconds(5)); return true; } \ No newline at end of file diff --git a/src/rtp/rtp_video_sender.h b/src/rtp/rtp_video_sender.h index ffe347f..fb44784 100644 --- a/src/rtp/rtp_video_sender.h +++ b/src/rtp/rtp_video_sender.h @@ -5,6 +5,7 @@ #include "ringbuffer.h" #include "rtp_packet.h" +#include "rtp_video_send_statistics.h" #include "thread_base.h" class RtpVideoSender : public ThreadBase { @@ -27,6 +28,10 @@ class RtpVideoSender : public ThreadBase { private: std::function rtp_packet_send_func_ = nullptr; RingBuffer rtp_packe_queue_; + + private: + std::unique_ptr rtp_video_send_statistics_ = nullptr; + uint32_t last_send_bytes_ = 0; }; #endif \ No newline at end of file