[feat] enable congestion controller

This commit is contained in:
dijunkun
2025-02-07 17:42:05 +08:00
parent 316a0220a8
commit 8d7068aa32
32 changed files with 184 additions and 46 deletions

View File

@@ -2,6 +2,7 @@
#include <chrono>
#include "common.h"
#include "log.h"
#define RTCP_SR_INTERVAL 1000
@@ -9,7 +10,7 @@
RtpAudioSender::RtpAudioSender() { SetPeriod(std::chrono::milliseconds(5)); }
RtpAudioSender::RtpAudioSender(std::shared_ptr<IOStatistics> io_statistics)
: io_statistics_(io_statistics) {
: ssrc_(GenerateUniqueSsrc()), io_statistics_(io_statistics) {
SetPeriod(std::chrono::milliseconds(5));
}
@@ -17,6 +18,8 @@ RtpAudioSender::~RtpAudioSender() {
if (rtp_statistics_) {
rtp_statistics_->Stop();
}
SSRCManager::Instance().DeleteSsrc(ssrc_);
}
void RtpAudioSender::Enqueue(std::vector<RtpPacket>& rtp_packets) {

View File

@@ -25,8 +25,8 @@ class RtpAudioSender : public ThreadBase {
public:
void Enqueue(std::vector<RtpPacket> &rtp_packets);
void SetSendDataFunc(std::function<int(const char *, size_t)> data_send_func);
uint32_t GetSsrc() { return ssrc_; }
private:
private:
int SendRtpPacket(RtpPacket &rtp_packet);
int SendRtcpSR(RtcpSenderReport &rtcp_sr);
@@ -39,6 +39,9 @@ class RtpAudioSender : public ThreadBase {
private:
std::function<int(const char *, size_t)> data_send_func_ = nullptr;
RingBuffer<RtpPacket> rtp_packe_queue_;
private:
uint32_t ssrc_ = 0;
std::unique_ptr<RtpStatistics> rtp_statistics_ = nullptr;
std::shared_ptr<IOStatistics> io_statistics_ = nullptr;
uint32_t last_send_bytes_ = 0;

View File

@@ -2,6 +2,7 @@
#include <chrono>
#include "common.h"
#include "log.h"
#define RTCP_SR_INTERVAL 1000
@@ -9,7 +10,7 @@
RtpDataSender::RtpDataSender() {}
RtpDataSender::RtpDataSender(std::shared_ptr<IOStatistics> io_statistics)
: io_statistics_(io_statistics) {
: ssrc_(GenerateUniqueSsrc()), io_statistics_(io_statistics) {
SetPeriod(std::chrono::milliseconds(5));
}
@@ -17,6 +18,8 @@ RtpDataSender::~RtpDataSender() {
if (rtp_statistics_) {
rtp_statistics_->Stop();
}
SSRCManager::Instance().DeleteSsrc(ssrc_);
}
void RtpDataSender::Enqueue(std::vector<RtpPacket>& rtp_packets) {

View File

@@ -25,6 +25,7 @@ class RtpDataSender : public ThreadBase {
public:
void Enqueue(std::vector<RtpPacket> &rtp_packets);
void SetSendDataFunc(std::function<int(const char *, size_t)> data_send_func);
uint32_t GetSsrc() { return ssrc_; }
private:
private:
@@ -41,6 +42,7 @@ class RtpDataSender : public ThreadBase {
RingBuffer<RtpPacket> rtp_packe_queue_;
private:
uint32_t ssrc_ = 0;
std::unique_ptr<RtpStatistics> rtp_statistics_ = nullptr;
std::shared_ptr<IOStatistics> io_statistics_ = nullptr;
uint32_t last_send_bytes_ = 0;

View File

@@ -9,34 +9,35 @@
#define NV12_BUFFER_SIZE (1280 * 720 * 3 / 2)
#define RTCP_RR_INTERVAL 1000
RtpVideoReceiver::RtpVideoReceiver()
RtpVideoReceiver::RtpVideoReceiver(std::shared_ptr<Clock> clock)
: feedback_ssrc_(GenerateUniqueSsrc()),
active_remb_module_(nullptr),
receive_side_congestion_controller_(
clock_,
clock,
[this](std::vector<std::unique_ptr<RtcpPacket>> packets) {
SendCombinedRtcpPacket(std::move(packets));
},
[this](int64_t bitrate_bps, std::vector<uint32_t> ssrcs) {
SendRemb(bitrate_bps, ssrcs);
}),
clock_(Clock::GetRealTimeClockShared()) {
clock_(clock) {
SetPeriod(std::chrono::milliseconds(5));
// rtcp_thread_ = std::thread(&RtpVideoReceiver::RtcpThread, this);
}
RtpVideoReceiver::RtpVideoReceiver(std::shared_ptr<IOStatistics> io_statistics)
RtpVideoReceiver::RtpVideoReceiver(std::shared_ptr<Clock> clock,
std::shared_ptr<IOStatistics> io_statistics)
: io_statistics_(io_statistics),
feedback_ssrc_(GenerateUniqueSsrc()),
receive_side_congestion_controller_(
clock_,
clock,
[this](std::vector<std::unique_ptr<RtcpPacket>> packets) {
SendCombinedRtcpPacket(std::move(packets));
},
[this](int64_t bitrate_bps, std::vector<uint32_t> ssrcs) {
SendRemb(bitrate_bps, ssrcs);
}),
clock_(Clock::GetRealTimeClockShared()) {
clock_(clock) {
SetPeriod(std::chrono::milliseconds(5));
// rtcp_thread_ = std::thread(&RtpVideoReceiver::RtcpThread, this);
@@ -404,6 +405,21 @@ int RtpVideoReceiver::SendRtcpRR(RtcpReceiverReport& rtcp_rr) {
return 0;
}
TimeDelta AtoToTimeDelta(uint16_t receive_info) {
// receive_info
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |R|ECN| Arrival time offset |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
const uint16_t ato = receive_info & 0x1FFF;
if (ato == 0x1FFE) {
return TimeDelta::PlusInfinity();
}
if (ato == 0x1FFF) {
return TimeDelta::MinusInfinity();
}
return TimeDelta::Seconds(ato) / 1024;
}
void RtpVideoReceiver::SendCombinedRtcpPacket(
std::vector<std::unique_ptr<RtcpPacket>> rtcp_packets) {
if (!data_send_func_) {
@@ -414,9 +430,18 @@ void RtpVideoReceiver::SendCombinedRtcpPacket(
RTCPSender rtcp_sender(
[this](const uint8_t* buffer, size_t size) -> int {
webrtc::rtcp::CommonHeader rtcp_block;
// bool valid = true;
// if (!rtcp_block.Parse(buffer, size)) {
// valid = false;
// }
webrtc::rtcp::CongestionControlFeedback feedback;
feedback.Parse(rtcp_block);
return data_send_func_((const char*)buffer, size);
},
IP_PACKET_SIZE);
1200);
for (auto& rtcp_packet : rtcp_packets) {
rtcp_packet->SetSenderSsrc(feedback_ssrc_);

View File

@@ -23,8 +23,9 @@ using namespace webrtc;
class RtpVideoReceiver : public ThreadBase {
public:
RtpVideoReceiver();
RtpVideoReceiver(std::shared_ptr<IOStatistics> io_statistics);
RtpVideoReceiver(std::shared_ptr<Clock> clock);
RtpVideoReceiver(std::shared_ptr<Clock> clock,
std::shared_ptr<IOStatistics> io_statistics);
virtual ~RtpVideoReceiver();
public:

View File

@@ -2,6 +2,7 @@
#include <chrono>
#include "common.h"
#include "log.h"
// #define SAVE_RTP_SENT_STREAM
@@ -11,7 +12,7 @@
RtpVideoSender::RtpVideoSender() {}
RtpVideoSender::RtpVideoSender(std::shared_ptr<IOStatistics> io_statistics)
: io_statistics_(io_statistics) {
: ssrc_(GenerateUniqueSsrc()), io_statistics_(io_statistics) {
SetPeriod(std::chrono::milliseconds(5));
#ifdef SAVE_RTP_SENT_STREAM
file_rtp_sent_ = fopen("rtp_sent_stream.h264", "w+b");
@@ -26,6 +27,8 @@ RtpVideoSender::~RtpVideoSender() {
rtp_statistics_->Stop();
}
SSRCManager::Instance().DeleteSsrc(ssrc_);
#ifdef SAVE_RTP_SENT_STREAM
if (file_rtp_sent_) {
fflush(file_rtp_sent_);
@@ -51,12 +54,26 @@ void RtpVideoSender::SetSendDataFunc(
data_send_func_ = data_send_func;
}
void RtpVideoSender::SetOnSentPacketFunc(
std::function<void(const webrtc::RtpPacketToSend&)> on_sent_packet_func) {
on_sent_packet_func_ = on_sent_packet_func;
}
int RtpVideoSender::SendRtpPacket(RtpPacket& rtp_packet) {
if (!data_send_func_) {
LOG_ERROR("data_send_func_ is nullptr");
return -1;
}
if (on_sent_packet_func_) {
webrtc::RtpPacketToSend rtp_packet_to_send;
rtp_packet_to_send.SetSequenceNumber(rtp_packet.SequenceNumber());
rtp_packet_to_send.SetSsrc(rtp_packet.Ssrc());
rtp_packet_to_send.set_transport_sequence_number(transport_seq_++);
rtp_packet_to_send.set_packet_type(webrtc::RtpPacketMediaType::kVideo);
on_sent_packet_func_(rtp_packet_to_send);
}
if (0 != data_send_func_((const char*)rtp_packet.Buffer().data(),
rtp_packet.Size())) {
// LOG_ERROR("Send rtp packet failed");

View File

@@ -7,6 +7,7 @@
#include "ringbuffer.h"
#include "rtcp_sender_report.h"
#include "rtp_packet.h"
#include "rtp_packet_to_send.h"
#include "rtp_statistics.h"
#include "thread_base.h"
@@ -19,6 +20,9 @@ class RtpVideoSender : public ThreadBase {
public:
void Enqueue(std::vector<RtpPacket> &rtp_packets);
void SetSendDataFunc(std::function<int(const char *, size_t)> data_send_func);
void SetOnSentPacketFunc(
std::function<void(const webrtc::RtpPacketToSend &)> on_sent_packet_func);
uint32_t GetSsrc() { return ssrc_; }
private:
int SendRtpPacket(RtpPacket &rtp_packet);
@@ -31,9 +35,12 @@ class RtpVideoSender : public ThreadBase {
private:
std::function<int(const char *, size_t)> data_send_func_ = nullptr;
std::function<void(const webrtc::RtpPacketToSend &)> on_sent_packet_func_ =
nullptr;
RingBuffer<RtpPacket> rtp_packe_queue_;
private:
uint32_t ssrc_ = 0;
std::unique_ptr<RtpStatistics> rtp_statistics_ = nullptr;
std::shared_ptr<IOStatistics> io_statistics_ = nullptr;
uint32_t last_send_bytes_ = 0;
@@ -41,6 +48,9 @@ class RtpVideoSender : public ThreadBase {
uint32_t total_rtp_payload_sent_ = 0;
uint32_t total_rtp_packets_sent_ = 0;
private:
int64_t transport_seq_ = 0;
private:
FILE *file_rtp_sent_ = nullptr;
};