[feat] move rtp packet sender out of channel module

This commit is contained in:
dijunkun
2025-03-13 21:11:20 +08:00
parent 23df1f3b60
commit d2b45b91e7
32 changed files with 681 additions and 266 deletions

View File

@@ -44,9 +44,9 @@ void AudioChannelSend::Destroy() {
int AudioChannelSend::SendAudio(char *data, size_t size) {
if (rtp_audio_sender_ && rtp_packetizer_) {
std::vector<std::shared_ptr<RtpPacket>> rtp_packets =
std::vector<std::unique_ptr<RtpPacket>> rtp_packets =
rtp_packetizer_->Build((uint8_t *)data, (uint32_t)size, 0, true);
rtp_audio_sender_->Enqueue(rtp_packets);
rtp_audio_sender_->Enqueue(std::move(rtp_packets));
}
return 0;

View File

@@ -44,9 +44,9 @@ void DataChannelSend::Destroy() {
int DataChannelSend::SendData(const char *data, size_t size) {
if (rtp_data_sender_ && rtp_packetizer_) {
std::vector<std::shared_ptr<RtpPacket>> rtp_packets =
std::vector<std::unique_ptr<RtpPacket>> rtp_packets =
rtp_packetizer_->Build((uint8_t *)data, (uint32_t)size, 0, true);
rtp_data_sender_->Enqueue(rtp_packets);
rtp_data_sender_->Enqueue(std::move(rtp_packets));
}
return 0;

View File

@@ -48,6 +48,21 @@ void VideoChannelSend::Initialize(rtp::PAYLOAD_TYPE payload_type) {
rtp_video_sender_->Start();
}
void VideoChannelSend::SetEnqueuePacketsFunc(
std::function<void(std::vector<std::unique_ptr<webrtc::RtpPacketToSend>>&)>
enqueue_packets_func) {
rtp_video_sender_->SetEnqueuePacketsFunc(enqueue_packets_func);
}
std::vector<std::unique_ptr<RtpPacket>> VideoChannelSend::GeneratePadding(
uint32_t payload_size, int64_t capture_timestamp_ms) {
if (rtp_packetizer_) {
return rtp_packetizer_->BuildPadding(payload_size, capture_timestamp_ms,
true);
}
return std::vector<std::unique_ptr<RtpPacket>>{};
}
void VideoChannelSend::Destroy() {
if (rtp_video_sender_) {
rtp_video_sender_->Stop();
@@ -57,11 +72,12 @@ void VideoChannelSend::Destroy() {
int VideoChannelSend::SendVideo(
std::shared_ptr<VideoFrameWrapper> encoded_frame) {
if (rtp_video_sender_ && rtp_packetizer_) {
std::vector<std::shared_ptr<RtpPacket>> rtp_packets =
std::vector<std::unique_ptr<RtpPacket>> rtp_packets =
rtp_packetizer_->Build((uint8_t*)encoded_frame->Buffer(),
(uint32_t)encoded_frame->Size(),
encoded_frame->CaptureTimestamp(), true);
rtp_video_sender_->Enqueue(rtp_packets, encoded_frame->CaptureTimestamp());
rtp_video_sender_->Enqueue(std::move(rtp_packets),
encoded_frame->CaptureTimestamp());
}
return 0;

View File

@@ -28,6 +28,14 @@ class VideoChannelSend {
on_sent_packet_func_);
~VideoChannelSend();
void SetEnqueuePacketsFunc(
std::function<
void(std::vector<std::unique_ptr<webrtc::RtpPacketToSend>>&)>
enqueue_packets_func);
std::vector<std::unique_ptr<RtpPacket>> GeneratePadding(
uint32_t payload_size, int64_t capture_timestamp_ms);
public:
void Initialize(rtp::PAYLOAD_TYPE payload_type);
void Destroy();

View File

@@ -23,14 +23,14 @@ RtpAudioSender::~RtpAudioSender() {
}
void RtpAudioSender::Enqueue(
std::vector<std::shared_ptr<RtpPacket>> rtp_packets) {
std::vector<std::unique_ptr<RtpPacket>> rtp_packets) {
if (!rtp_statistics_) {
rtp_statistics_ = std::make_unique<RtpStatistics>();
rtp_statistics_->Start();
}
for (auto& rtp_packet : rtp_packets) {
rtp_packet_queue_.push(rtp_packet);
rtp_packet_queue_.push(std::move(rtp_packet));
}
}
@@ -39,7 +39,7 @@ void RtpAudioSender::SetSendDataFunc(
data_send_func_ = data_send_func;
}
int RtpAudioSender::SendRtpPacket(std::shared_ptr<RtpPacket> rtp_packet) {
int RtpAudioSender::SendRtpPacket(std::unique_ptr<RtpPacket> rtp_packet) {
if (!data_send_func_) {
LOG_ERROR("data_send_func_ is nullptr");
return -1;
@@ -141,9 +141,11 @@ bool RtpAudioSender::Process() {
for (size_t i = 0; i < 10; i++)
if (!rtp_packet_queue_.isEmpty()) {
std::shared_ptr<RtpPacket> rtp_packet;
rtp_packet_queue_.pop(rtp_packet);
SendRtpPacket(rtp_packet);
std::optional<std::unique_ptr<RtpPacket>> rtp_packet =
rtp_packet_queue_.pop();
if (rtp_packet) {
SendRtpPacket(std::move(*rtp_packet));
}
}
if (rtp_statistics_) {

View File

@@ -24,13 +24,13 @@ class RtpAudioSender : public ThreadBase {
virtual ~RtpAudioSender();
public:
void Enqueue(std::vector<std::shared_ptr<RtpPacket>> rtp_packets);
void Enqueue(std::vector<std::unique_ptr<RtpPacket>> rtp_packets);
void SetSendDataFunc(std::function<int(const char *, size_t)> data_send_func);
uint32_t GetSsrc() { return ssrc_; }
void OnReceiverReport(const ReceiverReport &receiver_report) {}
private:
int SendRtpPacket(std::shared_ptr<RtpPacket> rtp_packet);
int SendRtpPacket(std::unique_ptr<RtpPacket> rtp_packet);
int SendRtcpSR(SenderReport &rtcp_sr);
bool CheckIsTimeSendSR();
@@ -40,7 +40,7 @@ class RtpAudioSender : public ThreadBase {
private:
std::function<int(const char *, size_t)> data_send_func_ = nullptr;
RingBuffer<std::shared_ptr<RtpPacket>> rtp_packet_queue_;
RingBuffer<std::unique_ptr<RtpPacket>> rtp_packet_queue_;
private:
uint32_t ssrc_ = 0;

View File

@@ -23,14 +23,14 @@ RtpDataSender::~RtpDataSender() {
}
void RtpDataSender::Enqueue(
std::vector<std::shared_ptr<RtpPacket>> rtp_packets) {
std::vector<std::unique_ptr<RtpPacket>> rtp_packets) {
if (!rtp_statistics_) {
rtp_statistics_ = std::make_unique<RtpStatistics>();
rtp_statistics_->Start();
}
for (auto& rtp_packet : rtp_packets) {
rtp_packet_queue_.push(rtp_packet);
rtp_packet_queue_.push(std::move(rtp_packet));
}
}
@@ -39,7 +39,7 @@ void RtpDataSender::SetSendDataFunc(
data_send_func_ = data_send_func;
}
int RtpDataSender::SendRtpPacket(std::shared_ptr<RtpPacket> rtp_packet) {
int RtpDataSender::SendRtpPacket(std::unique_ptr<RtpPacket> rtp_packet) {
if (!data_send_func_) {
LOG_ERROR("data_send_func_ is nullptr");
return -1;
@@ -141,9 +141,11 @@ bool RtpDataSender::Process() {
for (size_t i = 0; i < 10; i++)
if (!rtp_packet_queue_.isEmpty()) {
std::shared_ptr<RtpPacket> rtp_packet;
rtp_packet_queue_.pop(rtp_packet);
SendRtpPacket(rtp_packet);
std::optional<std::unique_ptr<RtpPacket>> rtp_packet =
rtp_packet_queue_.pop();
if (rtp_packet) {
SendRtpPacket(std::move(*rtp_packet));
}
}
if (rtp_statistics_) {

View File

@@ -24,14 +24,14 @@ class RtpDataSender : public ThreadBase {
virtual ~RtpDataSender();
public:
void Enqueue(std::vector<std::shared_ptr<RtpPacket>> rtp_packets);
void Enqueue(std::vector<std::unique_ptr<RtpPacket>> rtp_packets);
void SetSendDataFunc(std::function<int(const char *, size_t)> data_send_func);
uint32_t GetSsrc() { return ssrc_; }
void OnReceiverReport(const ReceiverReport &receiver_report) {}
private:
private:
int SendRtpPacket(std::shared_ptr<RtpPacket> rtp_packet);
int SendRtpPacket(std::unique_ptr<RtpPacket> rtp_packet);
int SendRtcpSR(SenderReport &rtcp_sr);
bool CheckIsTimeSendSR();
@@ -41,7 +41,7 @@ class RtpDataSender : public ThreadBase {
private:
std::function<int(const char *, size_t)> data_send_func_ = nullptr;
RingBuffer<std::shared_ptr<RtpPacket>> rtp_packet_queue_;
RingBuffer<std::unique_ptr<RtpPacket>> rtp_packet_queue_;
private:
uint32_t ssrc_ = 0;

View File

@@ -532,9 +532,8 @@ bool RtpVideoReceiver::CheckIsTimeSendRR() {
bool RtpVideoReceiver::Process() {
if (!compelete_video_frame_queue_.isEmpty()) {
VideoFrame video_frame;
compelete_video_frame_queue_.pop(video_frame);
if (on_receive_complete_frame_) {
std::optional<VideoFrame> video_frame = compelete_video_frame_queue_.pop();
if (on_receive_complete_frame_ && video_frame) {
// auto now_complete_frame_ts =
// std::chrono::duration_cast<std::chrono::milliseconds>(
// std::chrono::system_clock::now().time_since_epoch())
@@ -543,7 +542,7 @@ bool RtpVideoReceiver::Process() {
// LOG_ERROR("Duration {}", duration);
// last_complete_frame_ts_ = now_complete_frame_ts;
on_receive_complete_frame_(video_frame);
on_receive_complete_frame_(*video_frame);
// #ifdef SAVE_RTP_RECV_STREAM
// fwrite((unsigned char*)video_frame.Buffer(), 1,
// video_frame.Size(),

View File

@@ -44,22 +44,26 @@ RtpVideoSender::~RtpVideoSender() {
}
void RtpVideoSender::Enqueue(
std::vector<std::shared_ptr<RtpPacket>>& rtp_packets,
std::vector<std::unique_ptr<RtpPacket>>& rtp_packets,
int64_t capture_timestamp_ms) {
if (!rtp_statistics_) {
rtp_statistics_ = std::make_unique<RtpStatistics>();
rtp_statistics_->Start();
}
std::vector<std::unique_ptr<webrtc::RtpPacketToSend>> to_send_rtp_packets;
for (auto& rtp_packet : rtp_packets) {
std::shared_ptr<webrtc::RtpPacketToSend> rtp_packet_to_send =
std::dynamic_pointer_cast<webrtc::RtpPacketToSend>(rtp_packet);
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_++);
rtp_packet_to_send->set_packet_type(webrtc::RtpPacketMediaType::kVideo);
rtp_packet_queue_.push(std::move(rtp_packet_to_send));
// rtp_packet_queue_.push(std::move(rtp_packet_to_send));
to_send_rtp_packets.push_back(std::move(rtp_packet_to_send));
}
enqueue_packets_func_(std::move(to_send_rtp_packets));
}
void RtpVideoSender::SetSendDataFunc(
@@ -72,18 +76,19 @@ void RtpVideoSender::SetOnSentPacketFunc(
on_sent_packet_func_ = on_sent_packet_func;
}
void RtpVideoSender::SetEnqueuePacketsFunc(
std::function<void(std::vector<std::unique_ptr<webrtc::RtpPacketToSend>>&)>
enqueue_packets_func) {
enqueue_packets_func_ = enqueue_packets_func;
}
int RtpVideoSender::SendRtpPacket(
std::shared_ptr<webrtc::RtpPacketToSend> rtp_packet_to_send) {
std::unique_ptr<webrtc::RtpPacketToSend> rtp_packet_to_send) {
if (!data_send_func_) {
LOG_ERROR("data_send_func_ is nullptr");
return -1;
}
if (on_sent_packet_func_) {
on_sent_packet_func_(*rtp_packet_to_send);
rtp_packet_history_->AddPacket(rtp_packet_to_send, clock_->CurrentTime());
}
last_rtp_timestamp_ = rtp_packet_to_send->capture_time().ms();
int ret = data_send_func_((const char*)rtp_packet_to_send->Buffer().data(),
@@ -124,6 +129,12 @@ int RtpVideoSender::SendRtpPacket(
SendRtcpSR(rtcp_sr);
}
if (on_sent_packet_func_) {
on_sent_packet_func_(*rtp_packet_to_send);
rtp_packet_history_->AddPacket(std::move(rtp_packet_to_send),
clock_->CurrentTime());
}
return 0;
}
@@ -164,10 +175,10 @@ bool RtpVideoSender::Process() {
for (size_t i = 0; i < 10; i++)
if (!rtp_packet_queue_.isEmpty()) {
std::shared_ptr<webrtc::RtpPacketToSend> rtp_packet_to_send;
pop_success = rtp_packet_queue_.pop(rtp_packet_to_send);
if (pop_success) {
SendRtpPacket(rtp_packet_to_send);
std::optional<std::unique_ptr<webrtc::RtpPacketToSend>>
rtp_packet_to_send = rtp_packet_queue_.pop();
if (rtp_packet_to_send) {
SendRtpPacket(std::move(*rtp_packet_to_send));
}
}

View File

@@ -23,17 +23,21 @@ class RtpVideoSender : public ThreadBase {
virtual ~RtpVideoSender();
public:
void Enqueue(std::vector<std::shared_ptr<RtpPacket>> &rtp_packets,
void Enqueue(std::vector<std::unique_ptr<RtpPacket>> &rtp_packets,
int64_t capture_timestamp_ms);
void SetSendDataFunc(std::function<int(const char *, size_t)> data_send_func);
void SetOnSentPacketFunc(
std::function<void(const webrtc::RtpPacketToSend &)> on_sent_packet_func);
void SetEnqueuePacketsFunc(
std::function<
void(std::vector<std::unique_ptr<webrtc::RtpPacketToSend>> &)>
enqueue_packets_func);
uint32_t GetSsrc() { return ssrc_; }
void OnReceiverReport(const ReceiverReport &receiver_report);
private:
int SendRtpPacket(
std::shared_ptr<webrtc::RtpPacketToSend> rtp_packet_to_send);
std::unique_ptr<webrtc::RtpPacketToSend> rtp_packet_to_send);
int SendRtcpSR(SenderReport &rtcp_sr);
bool CheckIsTimeSendSR();
@@ -45,7 +49,9 @@ class RtpVideoSender : public ThreadBase {
std::function<int(const char *, size_t)> data_send_func_ = nullptr;
std::function<void(const webrtc::RtpPacketToSend &)> on_sent_packet_func_ =
nullptr;
RingBuffer<std::shared_ptr<webrtc::RtpPacketToSend>> rtp_packet_queue_;
std::function<void(std::vector<std::unique_ptr<webrtc::RtpPacketToSend>> &)>
enqueue_packets_func_ = nullptr;
RingBuffer<std::unique_ptr<webrtc::RtpPacketToSend>> rtp_packet_queue_;
private:
uint32_t ssrc_ = 0;