From 2b85ad1f60e27b89631a0ce0eb68795dc7d65cc1 Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 4 Feb 2021 17:27:32 +0800 Subject: [PATCH] RTC: Reuse UDP socket to receive packet. 4.0.67 --- README.md | 1 + trunk/src/app/srs_app_listener.cpp | 31 +++++++++++++++++++++------- trunk/src/app/srs_app_listener.hpp | 3 +++ trunk/src/core/srs_core_version4.hpp | 2 +- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1e778e66e..2a27804d7 100755 --- a/README.md +++ b/README.md @@ -155,6 +155,7 @@ For previous versions, please read: ## V4 changes +* v4.0, 2021-02-04, RTC: Reuse UDP socket to receive packet. 4.0.67 * v4.0, 2021-02-04, At least wait 1ms when <1ms, to avoid epoll_wait spin loop. 4.0.66 * v4.0, 2021-01-31, Enable -std=c++11 by default. 4.0.65 * v4.0, 2021-01-25, Enable --nasm and --srtp-asm by default for performance. 4.0.64 diff --git a/trunk/src/app/srs_app_listener.cpp b/trunk/src/app/srs_app_listener.cpp index d0414afb8..b93769e9d 100755 --- a/trunk/src/app/srs_app_listener.cpp +++ b/trunk/src/app/srs_app_listener.cpp @@ -313,7 +313,25 @@ int SrsUdpMuxSocket::recvfrom(srs_utime_t timeout) return 0; } - if (nread > 0) { + // Parse address from cache. + bool parsed = false; + if (from.ss_family == AF_INET) { + sockaddr_in* addr = (sockaddr_in*)&from; + + // Load from fast cache, previous ip. + std::map::iterator it = cache_.find(addr->sin_addr.s_addr); + if (it == cache_.end()) { + peer_ip = inet_ntoa(addr->sin_addr); + cache_[addr->sin_addr.s_addr] = peer_ip; + } else { + peer_ip = it->second; + } + + peer_port = ntohs(addr->sin_port); + parsed = true; + } + + if (!parsed && nread > 0) { // TODO: FIXME: Maybe we should not covert to string for each packet. char address_string[64]; char port_string[16]; @@ -514,6 +532,11 @@ srs_error_t SrsUdpMuxListener::cycle() SrsAutoFree(SrsErrorPithyPrint, pp_pkt_handler_err); set_socket_buffer(); + + // Because we have to decrypt the cipher of received packet payload, + // and the size is not determined, so we think there is at least one copy, + // and we can reuse the plaintext h264/opus with players when got plaintext. + SrsUdpMuxSocket skt(lfd); while (true) { if ((err = trd->pull()) != srs_success) { @@ -522,12 +545,6 @@ srs_error_t SrsUdpMuxListener::cycle() nn_loop++; - // TODO: FIXME: Refactor the memory cache for receiver. - // Because we have to decrypt the cipher of received packet payload, - // and the size is not determined, so we think there is at least one copy, - // and we can reuse the plaintext h264/opus with players when got plaintext. - SrsUdpMuxSocket skt(lfd); - int nread = skt.recvfrom(SRS_UTIME_NO_TIMEOUT); if (nread <= 0) { if (nread < 0) { diff --git a/trunk/src/app/srs_app_listener.hpp b/trunk/src/app/srs_app_listener.hpp index 758c09e73..0f856548c 100644 --- a/trunk/src/app/srs_app_listener.hpp +++ b/trunk/src/app/srs_app_listener.hpp @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -135,6 +136,8 @@ public: // TODO: FIXME: Rename it. Refine it for performance issue. class SrsUdpMuxSocket { +private: + std::map cache_; private: char* buf; int nb_buf; diff --git a/trunk/src/core/srs_core_version4.hpp b/trunk/src/core/srs_core_version4.hpp index 916b6e361..672284cf9 100644 --- a/trunk/src/core/srs_core_version4.hpp +++ b/trunk/src/core/srs_core_version4.hpp @@ -24,6 +24,6 @@ #ifndef SRS_CORE_VERSION4_HPP #define SRS_CORE_VERSION4_HPP -#define SRS_VERSION4_REVISION 66 +#define SRS_VERSION4_REVISION 67 #endif