From 25fec76ea9b154c9e7269531641c32ec611f497a Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 17 Mar 2020 12:33:08 +0800 Subject: [PATCH] For #1638, #307, switch to RTC session context cid for reusing UDP ports --- trunk/src/app/srs_app_rtc_conn.cpp | 24 ++++++++++++++++++++++-- trunk/src/app/srs_app_rtc_conn.hpp | 7 ++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/trunk/src/app/srs_app_rtc_conn.cpp b/trunk/src/app/srs_app_rtc_conn.cpp index 2361b721b..c3629db20 100644 --- a/trunk/src/app/srs_app_rtc_conn.cpp +++ b/trunk/src/app/srs_app_rtc_conn.cpp @@ -716,7 +716,7 @@ void SrsRtcSenderThread::send_and_free_messages(SrsSharedPtrMessage** msgs, int } } -SrsRtcSession::SrsRtcSession(SrsServer* svr, SrsRtcServer* rtc_svr, const SrsRequest& req, const std::string& un) +SrsRtcSession::SrsRtcSession(SrsServer* svr, SrsRtcServer* rtc_svr, const SrsRequest& req, const std::string& un, int context_id) { server = svr; rtc_server = rtc_svr; @@ -730,6 +730,8 @@ SrsRtcSession::SrsRtcSession(SrsServer* svr, SrsRtcServer* rtc_svr, const SrsReq request = req; source = NULL; + + cid = context_id; } SrsRtcSession::~SrsRtcSession() @@ -742,6 +744,11 @@ SrsRtcSession::~SrsRtcSession() srs_freep(strd); } +void SrsRtcSession::switch_to_context() +{ + _srs_context->set_id(cid); +} + srs_error_t SrsRtcSession::on_stun(SrsUdpMuxSocket* udp_mux_skt, SrsStunPacket* stun_req) { srs_error_t err = srs_success; @@ -1215,7 +1222,8 @@ SrsRtcSession* SrsRtcServer::create_rtc_session(const SrsRequest& req, const Srs break; } - SrsRtcSession* session = new SrsRtcSession(server, this, req, username); + int cid = _srs_context->get_id(); + SrsRtcSession* session = new SrsRtcSession(server, this, req, username, cid); map_username_session.insert(make_pair(username, session)); local_sdp.set_ice_ufrag(local_ufrag); @@ -1256,6 +1264,10 @@ srs_error_t SrsRtcServer::on_stun(SrsUdpMuxSocket* udp_mux_skt) return srs_error_new(ERROR_RTC_STUN, "can not find rtc_session, stun username=%s", username.c_str()); } + // Now, we got the RTC session to handle the packet, switch to its context + // to make all logs write to the "correct" pid+cid. + rtc_session->switch_to_context(); + return rtc_session->on_stun(udp_mux_skt, &stun_req); } @@ -1267,6 +1279,10 @@ srs_error_t SrsRtcServer::on_dtls(SrsUdpMuxSocket* udp_mux_skt) return srs_error_new(ERROR_RTC_DTLS, "can not find rtc session by peer_id=%s", udp_mux_skt->get_peer_id().c_str()); } + // Now, we got the RTC session to handle the packet, switch to its context + // to make all logs write to the "correct" pid+cid. + rtc_session->switch_to_context(); + return rtc_session->on_dtls(udp_mux_skt); } @@ -1280,6 +1296,10 @@ srs_error_t SrsRtcServer::on_rtp_or_rtcp(SrsUdpMuxSocket* udp_mux_skt) return srs_error_new(ERROR_RTC_RTP, "can not find rtc session by peer_id=%s", udp_mux_skt->get_peer_id().c_str()); } + // Now, we got the RTC session to handle the packet, switch to its context + // to make all logs write to the "correct" pid+cid. + rtc_session->switch_to_context(); + if (is_rtcp(reinterpret_cast(udp_mux_skt->data()), udp_mux_skt->size())) { err = rtc_session->on_rtcp(udp_mux_skt); } else { diff --git a/trunk/src/app/srs_app_rtc_conn.hpp b/trunk/src/app/srs_app_rtc_conn.hpp index 422e6ca9d..57914dd7d 100644 --- a/trunk/src/app/srs_app_rtc_conn.hpp +++ b/trunk/src/app/srs_app_rtc_conn.hpp @@ -194,11 +194,14 @@ private: std::string username; std::string peer_id; srs_utime_t last_stun_time; +private: + // For each RTC session, we use a specified cid for debugging logs. + int cid; public: SrsRequest request; SrsSource* source; public: - SrsRtcSession(SrsServer* svr, SrsRtcServer* rtc_svr, const SrsRequest& req, const std::string& un); + SrsRtcSession(SrsServer* svr, SrsRtcServer* rtc_svr, const SrsRequest& req, const std::string& un, int context_id); virtual ~SrsRtcSession(); public: SrsSdp* get_local_sdp() { return &local_sdp; } @@ -214,6 +217,8 @@ public: std::string get_peer_id() const { return peer_id; } void set_peer_id(const std::string& id) { peer_id = id; } + + void switch_to_context(); public: srs_error_t on_stun(SrsUdpMuxSocket* udp_mux_skt, SrsStunPacket* stun_req); srs_error_t on_dtls(SrsUdpMuxSocket* udp_mux_skt);