From 02f00642e9a2173393fe3cef440073ebdb5bb8ab Mon Sep 17 00:00:00 2001 From: dijunkun Date: Wed, 5 Mar 2025 17:48:33 +0800 Subject: [PATCH] [feat] clean rtp packet buffer queue when ice destroyed --- .../meida_channel/audio_channel_receive.cpp | 30 +++++++++---------- .../meida_channel/audio_channel_send.cpp | 5 +--- .../meida_channel/data_channel_receive.cpp | 30 +++++++++---------- .../meida_channel/data_channel_send.cpp | 5 +--- .../meida_channel/video_channel_receive.cpp | 30 +++++++++---------- .../meida_channel/video_channel_send.cpp | 5 +--- src/channel/rtp_channel/rtp_audio_sender.cpp | 7 +++-- src/channel/rtp_channel/rtp_data_sender.cpp | 7 +++-- src/channel/rtp_channel/rtp_video_sender.cpp | 13 ++++---- src/ice/ice_agent.cpp | 15 ++++------ src/ice/ice_agent.h | 17 +++++++++-- 11 files changed, 82 insertions(+), 82 deletions(-) diff --git a/src/channel/meida_channel/audio_channel_receive.cpp b/src/channel/meida_channel/audio_channel_receive.cpp index bc2080d..d9b7de2 100644 --- a/src/channel/meida_channel/audio_channel_receive.cpp +++ b/src/channel/meida_channel/audio_channel_receive.cpp @@ -23,25 +23,23 @@ void AudioChannelReceive::Initialize(rtp::PAYLOAD_TYPE payload_type) { } }); - rtp_audio_receiver_->SetSendDataFunc( - [this](const char *data, size_t size) -> int { - if (!ice_agent_) { - LOG_ERROR("ice_agent_ is nullptr"); - return -1; - } + rtp_audio_receiver_->SetSendDataFunc([this](const char *data, + size_t size) -> int { + if (!ice_agent_) { + LOG_ERROR("ice_agent_ is nullptr"); + return -1; + } - auto ice_state = ice_agent_->GetIceState(); + auto ice_state = ice_agent_->GetIceState(); - if (ice_state != NICE_COMPONENT_STATE_CONNECTED && - ice_state != NICE_COMPONENT_STATE_READY) { - LOG_ERROR("Ice is not connected, state = [{}]", - nice_component_state_to_string(ice_state)); - return -2; - } + if (ICE_STATE_NULLPTR == ice_state || ICE_STATE_DESTROYED == ice_state) { + LOG_ERROR("Ice is not connected, state = [{}]", (int)ice_state); + return -2; + } - ice_io_statistics_->UpdateAudioOutboundBytes((uint32_t)size); - return ice_agent_->Send(data, size); - }); + ice_io_statistics_->UpdateAudioOutboundBytes((uint32_t)size); + return ice_agent_->Send(data, size); + }); } void AudioChannelReceive::Destroy() {} diff --git a/src/channel/meida_channel/audio_channel_send.cpp b/src/channel/meida_channel/audio_channel_send.cpp index 9ddc24a..1136feb 100644 --- a/src/channel/meida_channel/audio_channel_send.cpp +++ b/src/channel/meida_channel/audio_channel_send.cpp @@ -25,10 +25,7 @@ void AudioChannelSend::Initialize(rtp::PAYLOAD_TYPE payload_type) { auto ice_state = ice_agent_->GetIceState(); - if (ice_state != NICE_COMPONENT_STATE_CONNECTED && - ice_state != NICE_COMPONENT_STATE_READY) { - LOG_ERROR("Ice is not connected, state = [{}]", - nice_component_state_to_string(ice_state)); + if (ICE_STATE_DESTROYED == ice_state) { return -2; } diff --git a/src/channel/meida_channel/data_channel_receive.cpp b/src/channel/meida_channel/data_channel_receive.cpp index 4cb1fb1..d865ffd 100644 --- a/src/channel/meida_channel/data_channel_receive.cpp +++ b/src/channel/meida_channel/data_channel_receive.cpp @@ -23,25 +23,23 @@ void DataChannelReceive::Initialize(rtp::PAYLOAD_TYPE payload_type) { } }); - rtp_data_receiver_->SetSendDataFunc( - [this](const char *data, size_t size) -> int { - if (!ice_agent_) { - LOG_ERROR("ice_agent_ is nullptr"); - return -1; - } + rtp_data_receiver_->SetSendDataFunc([this](const char *data, + size_t size) -> int { + if (!ice_agent_) { + LOG_ERROR("ice_agent_ is nullptr"); + return -1; + } - auto ice_state = ice_agent_->GetIceState(); + auto ice_state = ice_agent_->GetIceState(); - if (ice_state != NICE_COMPONENT_STATE_CONNECTED && - ice_state != NICE_COMPONENT_STATE_READY) { - LOG_ERROR("Ice is not connected, state = [{}]", - nice_component_state_to_string(ice_state)); - return -2; - } + if (ICE_STATE_NULLPTR == ice_state || ICE_STATE_DESTROYED == ice_state) { + LOG_ERROR("Ice is not connected, state = [{}]", (int)ice_state); + return -2; + } - ice_io_statistics_->UpdateDataOutboundBytes((uint32_t)size); - return ice_agent_->Send(data, size); - }); + ice_io_statistics_->UpdateDataOutboundBytes((uint32_t)size); + return ice_agent_->Send(data, size); + }); } void DataChannelReceive::Destroy() {} diff --git a/src/channel/meida_channel/data_channel_send.cpp b/src/channel/meida_channel/data_channel_send.cpp index ea6d89d..581307a 100644 --- a/src/channel/meida_channel/data_channel_send.cpp +++ b/src/channel/meida_channel/data_channel_send.cpp @@ -25,10 +25,7 @@ void DataChannelSend::Initialize(rtp::PAYLOAD_TYPE payload_type) { auto ice_state = ice_agent_->GetIceState(); - if (ice_state != NICE_COMPONENT_STATE_CONNECTED && - ice_state != NICE_COMPONENT_STATE_READY) { - LOG_ERROR("Ice is not connected, state = [{}]", - nice_component_state_to_string(ice_state)); + if (ICE_STATE_DESTROYED == ice_state) { return -2; } diff --git a/src/channel/meida_channel/video_channel_receive.cpp b/src/channel/meida_channel/video_channel_receive.cpp index d7b1f47..4cdca32 100644 --- a/src/channel/meida_channel/video_channel_receive.cpp +++ b/src/channel/meida_channel/video_channel_receive.cpp @@ -23,25 +23,23 @@ void VideoChannelReceive::Initialize(rtp::PAYLOAD_TYPE payload_type) { on_receive_complete_frame_(video_frame); }); - rtp_video_receiver_->SetSendDataFunc( - [this](const char *data, size_t size) -> int { - if (!ice_agent_) { - LOG_ERROR("ice_agent_ is nullptr"); - return -1; - } + rtp_video_receiver_->SetSendDataFunc([this](const char *data, + size_t size) -> int { + if (!ice_agent_) { + LOG_ERROR("ice_agent_ is nullptr"); + return -1; + } - auto ice_state = ice_agent_->GetIceState(); + auto ice_state = ice_agent_->GetIceState(); - if (ice_state != NICE_COMPONENT_STATE_CONNECTED && - ice_state != NICE_COMPONENT_STATE_READY) { - LOG_ERROR("Ice is not connected, state = [{}]", - nice_component_state_to_string(ice_state)); - return -2; - } + if (ICE_STATE_NULLPTR == ice_state || ICE_STATE_DESTROYED == ice_state) { + LOG_ERROR("Ice is not connected, state = [{}]", (int)ice_state); + return -2; + } - ice_io_statistics_->UpdateVideoOutboundBytes((uint32_t)size); - return ice_agent_->Send(data, size); - }); + ice_io_statistics_->UpdateVideoOutboundBytes((uint32_t)size); + return ice_agent_->Send(data, size); + }); rtp_video_receiver_->Start(); } diff --git a/src/channel/meida_channel/video_channel_send.cpp b/src/channel/meida_channel/video_channel_send.cpp index b804585..7e9b094 100644 --- a/src/channel/meida_channel/video_channel_send.cpp +++ b/src/channel/meida_channel/video_channel_send.cpp @@ -31,10 +31,7 @@ void VideoChannelSend::Initialize(rtp::PAYLOAD_TYPE payload_type) { auto ice_state = ice_agent_->GetIceState(); - if (ice_state != NICE_COMPONENT_STATE_CONNECTED && - ice_state != NICE_COMPONENT_STATE_READY) { - // LOG_ERROR("Ice is not connected, state = [{}]", - // nice_component_state_to_string(ice_state)); + if (ICE_STATE_DESTROYED == ice_state) { return -2; } diff --git a/src/channel/rtp_channel/rtp_audio_sender.cpp b/src/channel/rtp_channel/rtp_audio_sender.cpp index 03e3f90..8e22a67 100644 --- a/src/channel/rtp_channel/rtp_audio_sender.cpp +++ b/src/channel/rtp_channel/rtp_audio_sender.cpp @@ -45,9 +45,10 @@ int RtpAudioSender::SendRtpPacket(std::shared_ptr rtp_packet) { return -1; } - if (0 != data_send_func_((const char*)rtp_packet->Buffer().data(), - rtp_packet->Size())) { - LOG_ERROR("Send rtp packet failed"); + int ret = data_send_func_((const char*)rtp_packet->Buffer().data(), + rtp_packet->Size()); + if (-2 == ret) { + rtp_packet_queue_.clear(); return -1; } diff --git a/src/channel/rtp_channel/rtp_data_sender.cpp b/src/channel/rtp_channel/rtp_data_sender.cpp index 9bf9f0c..a5c48e4 100644 --- a/src/channel/rtp_channel/rtp_data_sender.cpp +++ b/src/channel/rtp_channel/rtp_data_sender.cpp @@ -45,9 +45,10 @@ int RtpDataSender::SendRtpPacket(std::shared_ptr rtp_packet) { return -1; } - if (0 != data_send_func_((const char*)rtp_packet->Buffer().data(), - rtp_packet->Size())) { - LOG_ERROR("Send rtp packet failed"); + int ret = data_send_func_((const char*)rtp_packet->Buffer().data(), + rtp_packet->Size()); + if (-2 == ret) { + rtp_packet_queue_.clear(); return -1; } diff --git a/src/channel/rtp_channel/rtp_video_sender.cpp b/src/channel/rtp_channel/rtp_video_sender.cpp index f5e5961..67c9604 100644 --- a/src/channel/rtp_channel/rtp_video_sender.cpp +++ b/src/channel/rtp_channel/rtp_video_sender.cpp @@ -85,9 +85,11 @@ int RtpVideoSender::SendRtpPacket( } last_rtp_timestamp_ = rtp_packet_to_send->capture_time().ms(); - if (0 != data_send_func_((const char*)rtp_packet_to_send->Buffer().data(), - rtp_packet_to_send->Size())) { - // LOG_ERROR("Send rtp packet failed"); + + int ret = data_send_func_((const char*)rtp_packet_to_send->Buffer().data(), + rtp_packet_to_send->Size()); + if (-2 == ret) { + rtp_packet_queue_.clear(); return -1; } @@ -131,8 +133,9 @@ int RtpVideoSender::SendRtcpSR(SenderReport& rtcp_sr) { return -1; } - if (data_send_func_((const char*)rtcp_sr.Buffer(), rtcp_sr.Size())) { - LOG_ERROR("Send SR failed"); + int ret = data_send_func_((const char*)rtcp_sr.Buffer(), rtcp_sr.Size()); + if (ret != 0) { + LOG_ERROR("Send rtcp sr failed"); return -1; } diff --git a/src/ice/ice_agent.cpp b/src/ice/ice_agent.cpp index ab43388..71bf22e 100644 --- a/src/ice/ice_agent.cpp +++ b/src/ice/ice_agent.cpp @@ -335,23 +335,20 @@ int IceAgent::GatherCandidates() { return 0; } -NiceComponentState IceAgent::GetIceState() { +ICE_STATE IceAgent::GetIceState() { if (!nice_inited_) { - LOG_ERROR("Nice agent has not been initialized"); - return NiceComponentState::NICE_COMPONENT_STATE_LAST; + return ICE_STATE_NOT_INITIALIZED; } if (nullptr == agent_) { - LOG_ERROR("Nice agent is nullptr"); - return NiceComponentState::NICE_COMPONENT_STATE_LAST; + return ICE_STATE_NULLPTR; } if (destroyed_) { - LOG_ERROR("Nice agent is destroyed"); - return NiceComponentState::NICE_COMPONENT_STATE_LAST; + return ICE_STATE_DESTROYED; } - state_ = nice_agent_get_component_state(agent_, stream_id_, 1); + state_ = (ICE_STATE)nice_agent_get_component_state(agent_, stream_id_, 1); return state_; } @@ -377,7 +374,7 @@ int IceAgent::Send(const char *data, size_t size) { return -1; } - // if (NiceComponentState::NICE_COMPONENT_STATE_READY != + // if (ICE_STATE_READY != // nice_agent_get_component_state(agent_, stream_id_, 1)) { // LOG_ERROR("Nice agent not ready"); // return -1; diff --git a/src/ice/ice_agent.h b/src/ice/ice_agent.h index 061adc8..a2d6ad9 100644 --- a/src/ice/ice_agent.h +++ b/src/ice/ice_agent.h @@ -9,6 +9,19 @@ #include "glib.h" #include "nice/agent.h" +typedef enum { + ICE_STATE_DISCONNECTED, + ICE_STATE_GATHERING, + ICE_STATE_CONNECTING, + ICE_STATE_CONNECTED, + ICE_STATE_READY, + ICE_STATE_FAILED, + ICE_STATE_NOT_INITIALIZED, + ICE_STATE_DESTROYED, + ICE_STATE_NULLPTR, + ICE_STATE_LAST +} ICE_STATE; + typedef void (*nice_cb_state_changed_t)(NiceAgent* agent, guint stream_id, guint component_id, NiceComponentState state, @@ -56,7 +69,7 @@ class IceAgent { int GatherCandidates(); - NiceComponentState GetIceState(); + ICE_STATE GetIceState(); int SetRemoteGatheringDone(); @@ -99,7 +112,7 @@ class IceAgent { uint32_t n_components_ = 1; // char* local_sdp_ = nullptr; std::string local_sdp_ = ""; - NiceComponentState state_ = NiceComponentState::NICE_COMPONENT_STATE_LAST; + ICE_STATE state_ = ICE_STATE_LAST; bool destroyed_ = false; gboolean agent_closed_ = false;