From 90315ebce5e49855423e1108da81c7a7a8cfbf46 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 27 Nov 2021 21:30:46 +0800 Subject: [PATCH] =?UTF-8?q?RTC:=20srtp=E5=8F=91=E9=80=81=E5=87=8F=E5=B0=91?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E5=86=85=E5=AD=98=E6=8B=B7=E8=B4=9D=EF=BC=8C?= =?UTF-8?q?=E6=8F=90=E9=AB=98webrtc=E5=8F=91=E9=80=81=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webrtc/WebRtcTransport.cpp | 49 ++++++++++++++++++++------------------ webrtc/WebRtcTransport.h | 11 ++++----- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 2f396374..738cd53b 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -46,6 +46,7 @@ static onceToken token([]() { WebRtcTransport::WebRtcTransport(const EventPoller::Ptr &poller) { _poller = poller; _identifier = to_string(reinterpret_cast(this)); + _packet_pool.setSize(64); } void WebRtcTransport::onCreate(){ @@ -69,7 +70,7 @@ const string &WebRtcTransport::getIdentifier() const { ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void WebRtcTransport::OnIceServerSendStunPacket(const RTC::IceServer *iceServer, const RTC::StunPacket *packet, RTC::TransportTuple *tuple) { - onSendSockData((char *) packet->GetData(), packet->GetSize(), (struct sockaddr_in *) tuple); + sendSockData((char *) packet->GetData(), packet->GetSize(), tuple); } void WebRtcTransport::OnIceServerSelectedTuple(const RTC::IceServer *iceServer, RTC::TransportTuple *tuple) { @@ -110,7 +111,7 @@ void WebRtcTransport::OnDtlsTransportConnected( } void WebRtcTransport::OnDtlsTransportSendData(const RTC::DtlsTransport *dtlsTransport, const uint8_t *data, size_t len) { - onSendSockData((char *)data, len); + sendSockData((char *)data, len, nullptr); } void WebRtcTransport::OnDtlsTransportConnecting(const RTC::DtlsTransport *dtlsTransport) { @@ -132,10 +133,10 @@ void WebRtcTransport::OnDtlsTransportApplicationDataReceived(const RTC::DtlsTran } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -void WebRtcTransport::onSendSockData(const char *buf, size_t len, bool flush){ - auto tuple = _ice_server->GetSelectedTuple(); - assert(tuple); - onSendSockData(buf, len, (struct sockaddr_in *) tuple, flush); +void WebRtcTransport::sendSockData(const char *buf, size_t len, RTC::TransportTuple *tuple){ + auto pkt = _packet_pool.obtain(); + pkt->assign(buf, len); + onSendSockData(std::move(pkt), true, tuple ? tuple : _ice_server->GetSelectedTuple()); } RTC::TransportTuple* WebRtcTransport::getSelectedTuple() const{ @@ -266,23 +267,28 @@ void WebRtcTransport::inputSockData(char *buf, int len, RTC::TransportTuple *tup void WebRtcTransport::sendRtpPacket(const char *buf, int len, bool flush, void *ctx) { if (_srtp_session_send) { + auto pkt = _packet_pool.obtain(); //预留rtx加入的两个字节 - CHECK((size_t)len + SRTP_MAX_TRAILER_LEN + 2 <= sizeof(_srtp_buf)); - memcpy(_srtp_buf, buf, len); - onBeforeEncryptRtp((char *) _srtp_buf, len, ctx); - if (_srtp_session_send->EncryptRtp(_srtp_buf, &len)) { - onSendSockData((char *) _srtp_buf, len, flush); + pkt->setCapacity((size_t) len + SRTP_MAX_TRAILER_LEN + 2); + pkt->assign(buf, len); + onBeforeEncryptRtp(pkt->data(), len, ctx); + if (_srtp_session_send->EncryptRtp(reinterpret_cast(pkt->data()), &len)) { + pkt->setSize(len); + onSendSockData(std::move(pkt), flush); } } } -void WebRtcTransport::sendRtcpPacket(const char *buf, int len, bool flush, void *ctx){ +void WebRtcTransport::sendRtcpPacket(const char *buf, int len, bool flush, void *ctx) { if (_srtp_session_send) { - CHECK((size_t)len + SRTP_MAX_TRAILER_LEN <= sizeof(_srtp_buf)); - memcpy(_srtp_buf, buf, len); - onBeforeEncryptRtcp((char *) _srtp_buf, len, ctx); - if (_srtp_session_send->EncryptRtcp(_srtp_buf, &len)) { - onSendSockData((char *) _srtp_buf, len, flush); + auto pkt = _packet_pool.obtain(); + //预留rtx加入的两个字节 + pkt->setCapacity((size_t) len + SRTP_MAX_TRAILER_LEN + 2); + pkt->assign(buf, len); + onBeforeEncryptRtcp(pkt->data(), len, ctx); + if (_srtp_session_send->EncryptRtcp(reinterpret_cast(pkt->data()), &len)) { + pkt->setSize(len); + onSendSockData(std::move(pkt), flush); } } } @@ -313,7 +319,6 @@ void WebRtcTransportImp::onCreate(){ WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller) : WebRtcTransport(poller) { InfoL << getIdentifier(); - _packet_pool.setSize(64); } WebRtcTransportImp::~WebRtcTransportImp() { @@ -325,16 +330,14 @@ void WebRtcTransportImp::onDestory() { unregisterSelf(); } -void WebRtcTransportImp::onSendSockData(const char *buf, size_t len, struct sockaddr_in *dst, bool flush) { +void WebRtcTransportImp::onSendSockData(Buffer::Ptr buf, bool flush, RTC::TransportTuple *tuple) { if (!_session) { - WarnL << "send data failed:" << len; + WarnL << "send data failed:" << buf->size(); return; } - auto ptr = _packet_pool.obtain(); - ptr->assign(buf, len); //一次性发送一帧的rtp数据,提高网络io性能 _session->setSendFlushFlag(flush); - _session->send(std::move(ptr)); + _session->send(std::move(buf)); } /////////////////////////////////////////////////////////////////// diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index 2879944a..d557b710 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -135,7 +135,7 @@ protected: virtual void onStartWebRTC() = 0; virtual void onRtcConfigure(RtcConfigure &configure) const; virtual void onCheckSdp(SdpType type, RtcSession &sdp) = 0; - virtual void onSendSockData(const char *buf, size_t len, struct sockaddr_in *dst, bool flush = true) = 0; + virtual void onSendSockData(Buffer::Ptr buf, bool flush = true, RTC::TransportTuple *tuple = nullptr) = 0; virtual void onRtp(const char *buf, size_t len, uint64_t stamp_ms) = 0; virtual void onRtcp(const char *buf, size_t len) = 0; @@ -149,7 +149,7 @@ protected: void sendRtcpPli(uint32_t ssrc); private: - void onSendSockData(const char *buf, size_t len, bool flush = true); + void sendSockData(const char *buf, size_t len, RTC::TransportTuple *tuple); void setRemoteDtlsFingerprint(const RtcSession &remote); protected: @@ -157,7 +157,6 @@ protected: RtcSession::Ptr _answer_sdp; private: - uint8_t _srtp_buf[2000]; string _identifier; EventPoller::Ptr _poller; std::shared_ptr _ice_server; @@ -165,6 +164,8 @@ private: std::shared_ptr _srtp_session_send; std::shared_ptr _srtp_session_recv; Ticker _ticker; + //循环池 + ResourcePool _packet_pool; }; class RtpChannel; @@ -232,7 +233,7 @@ public: protected: WebRtcTransportImp(const EventPoller::Ptr &poller); void onStartWebRTC() override; - void onSendSockData(const char *buf, size_t len, struct sockaddr_in *dst, bool flush = true) override; + void onSendSockData(Buffer::Ptr buf, bool flush = true, RTC::TransportTuple *tuple = nullptr) override; void onCheckSdp(SdpType type, RtcSession &sdp) override; void onRtcConfigure(RtcConfigure &configure) const override; @@ -279,8 +280,6 @@ private: unordered_map _ssrc_to_track; //根据接收rtp的pt获取相关信息 unordered_map> _pt_to_track; - //循环池 - ResourcePool _packet_pool; }; class WebRtcTransportManager {