For #1638, #307, switch to RTC session context cid for reusing UDP ports

pull/1658/head
winlin 5 years ago
parent 29b9203428
commit 25fec76ea9

@ -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; server = svr;
rtc_server = rtc_svr; rtc_server = rtc_svr;
@ -730,6 +730,8 @@ SrsRtcSession::SrsRtcSession(SrsServer* svr, SrsRtcServer* rtc_svr, const SrsReq
request = req; request = req;
source = NULL; source = NULL;
cid = context_id;
} }
SrsRtcSession::~SrsRtcSession() SrsRtcSession::~SrsRtcSession()
@ -742,6 +744,11 @@ SrsRtcSession::~SrsRtcSession()
srs_freep(strd); 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 SrsRtcSession::on_stun(SrsUdpMuxSocket* udp_mux_skt, SrsStunPacket* stun_req)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
@ -1215,7 +1222,8 @@ SrsRtcSession* SrsRtcServer::create_rtc_session(const SrsRequest& req, const Srs
break; 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)); map_username_session.insert(make_pair(username, session));
local_sdp.set_ice_ufrag(local_ufrag); 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()); 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); 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()); 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); 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()); 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<const uint8_t*>(udp_mux_skt->data()), udp_mux_skt->size())) { if (is_rtcp(reinterpret_cast<const uint8_t*>(udp_mux_skt->data()), udp_mux_skt->size())) {
err = rtc_session->on_rtcp(udp_mux_skt); err = rtc_session->on_rtcp(udp_mux_skt);
} else { } else {

@ -194,11 +194,14 @@ private:
std::string username; std::string username;
std::string peer_id; std::string peer_id;
srs_utime_t last_stun_time; srs_utime_t last_stun_time;
private:
// For each RTC session, we use a specified cid for debugging logs.
int cid;
public: public:
SrsRequest request; SrsRequest request;
SrsSource* source; SrsSource* source;
public: 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(); virtual ~SrsRtcSession();
public: public:
SrsSdp* get_local_sdp() { return &local_sdp; } SrsSdp* get_local_sdp() { return &local_sdp; }
@ -214,6 +217,8 @@ public:
std::string get_peer_id() const { return peer_id; } std::string get_peer_id() const { return peer_id; }
void set_peer_id(const std::string& id) { peer_id = id; } void set_peer_id(const std::string& id) { peer_id = id; }
void switch_to_context();
public: public:
srs_error_t on_stun(SrsUdpMuxSocket* udp_mux_skt, SrsStunPacket* stun_req); srs_error_t on_stun(SrsUdpMuxSocket* udp_mux_skt, SrsStunPacket* stun_req);
srs_error_t on_dtls(SrsUdpMuxSocket* udp_mux_skt); srs_error_t on_dtls(SrsUdpMuxSocket* udp_mux_skt);

Loading…
Cancel
Save