RTC: srtp发送减少一次内存拷贝,提高webrtc发送性能

This commit is contained in:
xiongziliang 2021-11-27 21:30:46 +08:00
parent 5daf52cafd
commit 90315ebce5
2 changed files with 31 additions and 29 deletions

View File

@ -46,6 +46,7 @@ static onceToken token([]() {
WebRtcTransport::WebRtcTransport(const EventPoller::Ptr &poller) { WebRtcTransport::WebRtcTransport(const EventPoller::Ptr &poller) {
_poller = poller; _poller = poller;
_identifier = to_string(reinterpret_cast<uint64_t>(this)); _identifier = to_string(reinterpret_cast<uint64_t>(this));
_packet_pool.setSize(64);
} }
void WebRtcTransport::onCreate(){ 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) { 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) { 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) { 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) { 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){ void WebRtcTransport::sendSockData(const char *buf, size_t len, RTC::TransportTuple *tuple){
auto tuple = _ice_server->GetSelectedTuple(); auto pkt = _packet_pool.obtain();
assert(tuple); pkt->assign(buf, len);
onSendSockData(buf, len, (struct sockaddr_in *) tuple, flush); onSendSockData(std::move(pkt), true, tuple ? tuple : _ice_server->GetSelectedTuple());
} }
RTC::TransportTuple* WebRtcTransport::getSelectedTuple() const{ 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) { void WebRtcTransport::sendRtpPacket(const char *buf, int len, bool flush, void *ctx) {
if (_srtp_session_send) { if (_srtp_session_send) {
auto pkt = _packet_pool.obtain();
//预留rtx加入的两个字节 //预留rtx加入的两个字节
CHECK((size_t)len + SRTP_MAX_TRAILER_LEN + 2 <= sizeof(_srtp_buf)); pkt->setCapacity((size_t) len + SRTP_MAX_TRAILER_LEN + 2);
memcpy(_srtp_buf, buf, len); pkt->assign(buf, len);
onBeforeEncryptRtp((char *) _srtp_buf, len, ctx); onBeforeEncryptRtp(pkt->data(), len, ctx);
if (_srtp_session_send->EncryptRtp(_srtp_buf, &len)) { if (_srtp_session_send->EncryptRtp(reinterpret_cast<uint8_t *>(pkt->data()), &len)) {
onSendSockData((char *) _srtp_buf, len, flush); 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) { if (_srtp_session_send) {
CHECK((size_t)len + SRTP_MAX_TRAILER_LEN <= sizeof(_srtp_buf)); auto pkt = _packet_pool.obtain();
memcpy(_srtp_buf, buf, len); //预留rtx加入的两个字节
onBeforeEncryptRtcp((char *) _srtp_buf, len, ctx); pkt->setCapacity((size_t) len + SRTP_MAX_TRAILER_LEN + 2);
if (_srtp_session_send->EncryptRtcp(_srtp_buf, &len)) { pkt->assign(buf, len);
onSendSockData((char *) _srtp_buf, len, flush); onBeforeEncryptRtcp(pkt->data(), len, ctx);
if (_srtp_session_send->EncryptRtcp(reinterpret_cast<uint8_t *>(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) { WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller) : WebRtcTransport(poller) {
InfoL << getIdentifier(); InfoL << getIdentifier();
_packet_pool.setSize(64);
} }
WebRtcTransportImp::~WebRtcTransportImp() { WebRtcTransportImp::~WebRtcTransportImp() {
@ -325,16 +330,14 @@ void WebRtcTransportImp::onDestory() {
unregisterSelf(); 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) { if (!_session) {
WarnL << "send data failed:" << len; WarnL << "send data failed:" << buf->size();
return; return;
} }
auto ptr = _packet_pool.obtain();
ptr->assign(buf, len);
//一次性发送一帧的rtp数据提高网络io性能 //一次性发送一帧的rtp数据提高网络io性能
_session->setSendFlushFlag(flush); _session->setSendFlushFlag(flush);
_session->send(std::move(ptr)); _session->send(std::move(buf));
} }
/////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////

View File

@ -135,7 +135,7 @@ protected:
virtual void onStartWebRTC() = 0; virtual void onStartWebRTC() = 0;
virtual void onRtcConfigure(RtcConfigure &configure) const; virtual void onRtcConfigure(RtcConfigure &configure) const;
virtual void onCheckSdp(SdpType type, RtcSession &sdp) = 0; 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 onRtp(const char *buf, size_t len, uint64_t stamp_ms) = 0;
virtual void onRtcp(const char *buf, size_t len) = 0; virtual void onRtcp(const char *buf, size_t len) = 0;
@ -149,7 +149,7 @@ protected:
void sendRtcpPli(uint32_t ssrc); void sendRtcpPli(uint32_t ssrc);
private: 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); void setRemoteDtlsFingerprint(const RtcSession &remote);
protected: protected:
@ -157,7 +157,6 @@ protected:
RtcSession::Ptr _answer_sdp; RtcSession::Ptr _answer_sdp;
private: private:
uint8_t _srtp_buf[2000];
string _identifier; string _identifier;
EventPoller::Ptr _poller; EventPoller::Ptr _poller;
std::shared_ptr<RTC::IceServer> _ice_server; std::shared_ptr<RTC::IceServer> _ice_server;
@ -165,6 +164,8 @@ private:
std::shared_ptr<RTC::SrtpSession> _srtp_session_send; std::shared_ptr<RTC::SrtpSession> _srtp_session_send;
std::shared_ptr<RTC::SrtpSession> _srtp_session_recv; std::shared_ptr<RTC::SrtpSession> _srtp_session_recv;
Ticker _ticker; Ticker _ticker;
//循环池
ResourcePool<BufferRaw> _packet_pool;
}; };
class RtpChannel; class RtpChannel;
@ -232,7 +233,7 @@ public:
protected: protected:
WebRtcTransportImp(const EventPoller::Ptr &poller); WebRtcTransportImp(const EventPoller::Ptr &poller);
void onStartWebRTC() override; 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 onCheckSdp(SdpType type, RtcSession &sdp) override;
void onRtcConfigure(RtcConfigure &configure) const override; void onRtcConfigure(RtcConfigure &configure) const override;
@ -279,8 +280,6 @@ private:
unordered_map<uint32_t/*ssrc*/, MediaTrack::Ptr> _ssrc_to_track; unordered_map<uint32_t/*ssrc*/, MediaTrack::Ptr> _ssrc_to_track;
//根据接收rtp的pt获取相关信息 //根据接收rtp的pt获取相关信息
unordered_map<uint8_t/*pt*/, std::unique_ptr<WrappedMediaTrack>> _pt_to_track; unordered_map<uint8_t/*pt*/, std::unique_ptr<WrappedMediaTrack>> _pt_to_track;
//循环池
ResourcePool<BufferRaw> _packet_pool;
}; };
class WebRtcTransportManager { class WebRtcTransportManager {