[feat] use packet sender to send all rtp packets

This commit is contained in:
dijunkun
2025-03-17 18:44:29 +08:00
parent b0306d510c
commit fa667df1e0
11 changed files with 79 additions and 18 deletions

View File

@@ -8,8 +8,11 @@ AudioChannelSend::~AudioChannelSend() {}
AudioChannelSend::AudioChannelSend( AudioChannelSend::AudioChannelSend(
std::shared_ptr<IceAgent> ice_agent, std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<PacketSender> packet_sender,
std::shared_ptr<IOStatistics> ice_io_statistics) std::shared_ptr<IOStatistics> ice_io_statistics)
: ice_agent_(ice_agent), ice_io_statistics_(ice_io_statistics) {} : packet_sender_(packet_sender),
ice_agent_(ice_agent),
ice_io_statistics_(ice_io_statistics) {}
void AudioChannelSend::Initialize(rtp::PAYLOAD_TYPE payload_type) { void AudioChannelSend::Initialize(rtp::PAYLOAD_TYPE payload_type) {
rtp_audio_sender_ = std::make_unique<RtpAudioSender>(ice_io_statistics_); rtp_audio_sender_ = std::make_unique<RtpAudioSender>(ice_io_statistics_);
@@ -46,7 +49,7 @@ int AudioChannelSend::SendAudio(char *data, size_t size) {
if (rtp_audio_sender_ && rtp_packetizer_) { if (rtp_audio_sender_ && rtp_packetizer_) {
std::vector<std::unique_ptr<RtpPacket>> rtp_packets = std::vector<std::unique_ptr<RtpPacket>> rtp_packets =
rtp_packetizer_->Build((uint8_t *)data, (uint32_t)size, 0, true); rtp_packetizer_->Build((uint8_t *)data, (uint32_t)size, 0, true);
rtp_audio_sender_->Enqueue(std::move(rtp_packets)); packet_sender_->EnqueueRtpPacket(rtp_packets, 0);
} }
return 0; return 0;

View File

@@ -8,6 +8,7 @@
#define _AUDIO_CHANNEL_SEND_H_ #define _AUDIO_CHANNEL_SEND_H_
#include "ice_agent.h" #include "ice_agent.h"
#include "packet_sender.h"
#include "rtp_audio_sender.h" #include "rtp_audio_sender.h"
#include "rtp_packetizer.h" #include "rtp_packetizer.h"
@@ -15,6 +16,7 @@ class AudioChannelSend {
public: public:
AudioChannelSend(); AudioChannelSend();
AudioChannelSend(std::shared_ptr<IceAgent> ice_agent, AudioChannelSend(std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<PacketSender> packet_sender,
std::shared_ptr<IOStatistics> ice_io_statistics); std::shared_ptr<IOStatistics> ice_io_statistics);
~AudioChannelSend(); ~AudioChannelSend();
@@ -34,6 +36,7 @@ class AudioChannelSend {
void OnReceiverReport(const ReceiverReport& receiver_report) {} void OnReceiverReport(const ReceiverReport& receiver_report) {}
private: private:
std::shared_ptr<PacketSender> packet_sender_ = nullptr;
std::shared_ptr<IceAgent> ice_agent_ = nullptr; std::shared_ptr<IceAgent> ice_agent_ = nullptr;
std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr; std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr;
std::unique_ptr<RtpPacketizer> rtp_packetizer_ = nullptr; std::unique_ptr<RtpPacketizer> rtp_packetizer_ = nullptr;

View File

@@ -8,8 +8,11 @@ DataChannelSend::~DataChannelSend() {}
DataChannelSend::DataChannelSend( DataChannelSend::DataChannelSend(
std::shared_ptr<IceAgent> ice_agent, std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<PacketSender> packet_sender,
std::shared_ptr<IOStatistics> ice_io_statistics) std::shared_ptr<IOStatistics> ice_io_statistics)
: ice_agent_(ice_agent), ice_io_statistics_(ice_io_statistics) {} : packet_sender_(packet_sender),
ice_agent_(ice_agent),
ice_io_statistics_(ice_io_statistics) {}
void DataChannelSend::Initialize(rtp::PAYLOAD_TYPE payload_type) { void DataChannelSend::Initialize(rtp::PAYLOAD_TYPE payload_type) {
rtp_data_sender_ = std::make_unique<RtpDataSender>(ice_io_statistics_); rtp_data_sender_ = std::make_unique<RtpDataSender>(ice_io_statistics_);
@@ -46,7 +49,7 @@ int DataChannelSend::SendData(const char *data, size_t size) {
if (rtp_data_sender_ && rtp_packetizer_) { if (rtp_data_sender_ && rtp_packetizer_) {
std::vector<std::unique_ptr<RtpPacket>> rtp_packets = std::vector<std::unique_ptr<RtpPacket>> rtp_packets =
rtp_packetizer_->Build((uint8_t *)data, (uint32_t)size, 0, true); rtp_packetizer_->Build((uint8_t *)data, (uint32_t)size, 0, true);
rtp_data_sender_->Enqueue(std::move(rtp_packets)); packet_sender_->EnqueueRtpPacket(rtp_packets, 0);
} }
return 0; return 0;

View File

@@ -8,6 +8,7 @@
#define _DATA_CHANNEL_SEND_H_ #define _DATA_CHANNEL_SEND_H_
#include "ice_agent.h" #include "ice_agent.h"
#include "packet_sender.h"
#include "rtp_data_sender.h" #include "rtp_data_sender.h"
#include "rtp_packetizer.h" #include "rtp_packetizer.h"
@@ -15,6 +16,7 @@ class DataChannelSend {
public: public:
DataChannelSend(); DataChannelSend();
DataChannelSend(std::shared_ptr<IceAgent> ice_agent, DataChannelSend(std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<PacketSender> packet_sender,
std::shared_ptr<IOStatistics> ice_io_statistics); std::shared_ptr<IOStatistics> ice_io_statistics);
~DataChannelSend(); ~DataChannelSend();
@@ -34,6 +36,7 @@ class DataChannelSend {
void OnReceiverReport(const ReceiverReport& receiver_report) {} void OnReceiverReport(const ReceiverReport& receiver_report) {}
private: private:
std::shared_ptr<PacketSender> packet_sender_ = nullptr;
std::shared_ptr<IceAgent> ice_agent_ = nullptr; std::shared_ptr<IceAgent> ice_agent_ = nullptr;
std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr; std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr;
std::unique_ptr<RtpPacketizer> rtp_packetizer_ = nullptr; std::unique_ptr<RtpPacketizer> rtp_packetizer_ = nullptr;

View File

@@ -9,10 +9,12 @@ VideoChannelSend::~VideoChannelSend() {}
VideoChannelSend::VideoChannelSend( VideoChannelSend::VideoChannelSend(
std::shared_ptr<SystemClock> clock, std::shared_ptr<IceAgent> ice_agent, std::shared_ptr<SystemClock> clock, std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<PacketSender> packet_sender,
std::shared_ptr<IOStatistics> ice_io_statistics, std::shared_ptr<IOStatistics> ice_io_statistics,
std::function<void(const webrtc::RtpPacketToSend& packet)> std::function<void(const webrtc::RtpPacketToSend& packet)>
on_sent_packet_func) on_sent_packet_func)
: ice_agent_(ice_agent), : ice_agent_(ice_agent),
packet_sender_(packet_sender),
ice_io_statistics_(ice_io_statistics), ice_io_statistics_(ice_io_statistics),
on_sent_packet_func_(on_sent_packet_func), on_sent_packet_func_(on_sent_packet_func),
clock_(clock){}; clock_(clock){};
@@ -76,8 +78,8 @@ int VideoChannelSend::SendVideo(
rtp_packetizer_->Build((uint8_t*)encoded_frame->Buffer(), rtp_packetizer_->Build((uint8_t*)encoded_frame->Buffer(),
(uint32_t)encoded_frame->Size(), (uint32_t)encoded_frame->Size(),
encoded_frame->CaptureTimestamp(), true); encoded_frame->CaptureTimestamp(), true);
rtp_video_sender_->Enqueue(std::move(rtp_packets), packet_sender_->EnqueueRtpPacket(std::move(rtp_packets),
encoded_frame->CaptureTimestamp()); encoded_frame->CaptureTimestamp());
} }
return 0; return 0;

View File

@@ -13,6 +13,7 @@
#include "congestion_control.h" #include "congestion_control.h"
#include "congestion_control_feedback.h" #include "congestion_control_feedback.h"
#include "ice_agent.h" #include "ice_agent.h"
#include "packet_sender.h"
#include "rtp_packetizer.h" #include "rtp_packetizer.h"
#include "rtp_video_sender.h" #include "rtp_video_sender.h"
#include "transport_feedback_adapter.h" #include "transport_feedback_adapter.h"
@@ -23,6 +24,7 @@ class VideoChannelSend {
VideoChannelSend(); VideoChannelSend();
VideoChannelSend(std::shared_ptr<SystemClock> clock, VideoChannelSend(std::shared_ptr<SystemClock> clock,
std::shared_ptr<IceAgent> ice_agent, std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<PacketSender> packet_sender,
std::shared_ptr<IOStatistics> ice_io_statistics, std::shared_ptr<IOStatistics> ice_io_statistics,
std::function<void(const webrtc::RtpPacketToSend& packet)> std::function<void(const webrtc::RtpPacketToSend& packet)>
on_sent_packet_func_); on_sent_packet_func_);
@@ -71,6 +73,7 @@ class VideoChannelSend {
void UpdateCongestedState(); void UpdateCongestedState();
private: private:
std::shared_ptr<PacketSender> packet_sender_ = nullptr;
std::shared_ptr<IceAgent> ice_agent_ = nullptr; std::shared_ptr<IceAgent> ice_agent_ = nullptr;
std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr; std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr;
std::unique_ptr<RtpPacketizer> rtp_packetizer_ = nullptr; std::unique_ptr<RtpPacketizer> rtp_packetizer_ = nullptr;

View File

@@ -53,7 +53,7 @@ void IceTransportController::Create(
CreateAudioCodec(); CreateAudioCodec();
controller_ = std::make_unique<CongestionControl>(); controller_ = std::make_unique<CongestionControl>();
packet_sender_ = std::make_unique<PacketSenderImp>(ice_agent, webrtc_clock_); packet_sender_ = std::make_shared<PacketSenderImp>(ice_agent, webrtc_clock_);
packet_sender_->SetPacingRates(DataRate::BitsPerSec(300000), packet_sender_->SetPacingRates(DataRate::BitsPerSec(300000),
DataRate::Zero()); DataRate::Zero());
packet_sender_->SetOnSentPacketFunc( packet_sender_->SetOnSentPacketFunc(
@@ -67,7 +67,7 @@ void IceTransportController::Create(
resolution_adapter_ = std::make_unique<ResolutionAdapter>(); resolution_adapter_ = std::make_unique<ResolutionAdapter>();
video_channel_send_ = std::make_unique<VideoChannelSend>( video_channel_send_ = std::make_unique<VideoChannelSend>(
clock_, ice_agent, ice_io_statistics, clock_, ice_agent, packet_sender_, ice_io_statistics,
[this](const webrtc::RtpPacketToSend& packet) { [this](const webrtc::RtpPacketToSend& packet) {
OnSentRtpPacket(packet); OnSentRtpPacket(packet);
}); });
@@ -78,10 +78,10 @@ void IceTransportController::Create(
return video_channel_send_->GeneratePadding(size, capture_timestamp_ms); return video_channel_send_->GeneratePadding(size, capture_timestamp_ms);
}); });
audio_channel_send_ = audio_channel_send_ = std::make_unique<AudioChannelSend>(
std::make_unique<AudioChannelSend>(ice_agent, ice_io_statistics); ice_agent, packet_sender_, ice_io_statistics);
data_channel_send_ = data_channel_send_ = std::make_unique<DataChannelSend>(
std::make_unique<DataChannelSend>(ice_agent, ice_io_statistics); ice_agent, packet_sender_, ice_io_statistics);
video_channel_send_->Initialize(video_codec_payload_type); video_channel_send_->Initialize(video_codec_payload_type);
audio_channel_send_->Initialize(rtp::PAYLOAD_TYPE::OPUS); audio_channel_send_->Initialize(rtp::PAYLOAD_TYPE::OPUS);

View File

@@ -21,6 +21,7 @@
#include "data_channel_receive.h" #include "data_channel_receive.h"
#include "data_channel_send.h" #include "data_channel_send.h"
#include "ice_agent.h" #include "ice_agent.h"
#include "packet_sender.h"
#include "packet_sender_imp.h" #include "packet_sender_imp.h"
#include "resolution_adapter.h" #include "resolution_adapter.h"
#include "transport_feedback_adapter.h" #include "transport_feedback_adapter.h"
@@ -107,7 +108,7 @@ class IceTransportController
std::shared_ptr<IceAgent> ice_agent_ = nullptr; std::shared_ptr<IceAgent> ice_agent_ = nullptr;
std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr; std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr;
std::unique_ptr<RtpPacketizer> rtp_packetizer_ = nullptr; std::unique_ptr<RtpPacketizer> rtp_packetizer_ = nullptr;
std::unique_ptr<PacketSenderImp> packet_sender_ = nullptr; std::shared_ptr<PacketSenderImp> packet_sender_ = nullptr;
std::string remote_user_id_; std::string remote_user_id_;
void *user_data_ = nullptr; void *user_data_ = nullptr;

View File

@@ -18,8 +18,9 @@ class PacketSender {
virtual ~PacketSender() {} virtual ~PacketSender() {}
virtual int Send() = 0; virtual int Send() = 0;
virtual int InsertRtpPacket( virtual int EnqueueRtpPacket(
std::vector<std::unique_ptr<RtpPacket>> &rtp_packets) = 0; std::vector<std::unique_ptr<RtpPacket>> &rtp_packets,
int64_t capture_timestamp_ms) = 0;
}; };
#endif #endif

View File

@@ -250,4 +250,47 @@ void PacketSenderImp::UpdateStats() {
PacketSenderImp::Stats PacketSenderImp::GetStats() const { PacketSenderImp::Stats PacketSenderImp::GetStats() const {
return current_stats_; return current_stats_;
}
/*----------------------------------------------------------------------------*/
int PacketSenderImp::EnqueueRtpPacket(
std::vector<std::unique_ptr<RtpPacket>> &rtp_packets,
int64_t capture_timestamp_ms) {
std::vector<std::unique_ptr<webrtc::RtpPacketToSend>> to_send_rtp_packets;
for (auto &rtp_packet : rtp_packets) {
std::unique_ptr<webrtc::RtpPacketToSend> rtp_packet_to_send(
static_cast<webrtc::RtpPacketToSend *>(rtp_packet.release()));
rtp_packet_to_send->set_capture_time(
webrtc::Timestamp::Millis(capture_timestamp_ms));
rtp_packet_to_send->set_transport_sequence_number(transport_seq_++);
switch (rtp_packet_to_send->PayloadType()) {
case rtp::PAYLOAD_TYPE::H264:
rtp_packet_to_send->set_packet_type(webrtc::RtpPacketMediaType::kVideo);
break;
case rtp::PAYLOAD_TYPE::AV1:
rtp_packet_to_send->set_packet_type(webrtc::RtpPacketMediaType::kVideo);
break;
case rtp::PAYLOAD_TYPE::H264_FEC_SOURCE:
rtp_packet_to_send->set_packet_type(
webrtc::RtpPacketMediaType::kForwardErrorCorrection);
break;
case rtp::PAYLOAD_TYPE::H264_FEC_REPAIR:
rtp_packet_to_send->set_packet_type(
webrtc::RtpPacketMediaType::kForwardErrorCorrection);
break;
case rtp::PAYLOAD_TYPE::OPUS:
rtp_packet_to_send->set_packet_type(webrtc::RtpPacketMediaType::kAudio);
break;
default:
rtp_packet_to_send->set_packet_type(webrtc::RtpPacketMediaType::kVideo);
break;
}
to_send_rtp_packets.push_back(std::move(rtp_packet_to_send));
}
EnqueuePackets(std::move(to_send_rtp_packets));
return 0;
} }

View File

@@ -37,9 +37,8 @@ class PacketSenderImp : public PacketSender,
public: public:
int Send() { return 0; } int Send() { return 0; }
int InsertRtpPacket(std::vector<std::unique_ptr<RtpPacket>>& rtp_packets) { int EnqueueRtpPacket(std::vector<std::unique_ptr<RtpPacket>>& rtp_packets,
return 0; int64_t capture_timestamp_ms);
}
void SetOnSentPacketFunc( void SetOnSentPacketFunc(
std::function<void(const webrtc::RtpPacketToSend&)> on_sent_packet_func) { std::function<void(const webrtc::RtpPacketToSend&)> on_sent_packet_func) {