From 576056c465cf7f18554731c5b6e58390b141be86 Mon Sep 17 00:00:00 2001 From: Haibo Chen Date: Sat, 9 Jul 2022 21:44:57 +0800 Subject: [PATCH] get ip from env, for externIP in file 'config.ini' (#1786) --- 3rdpart/ZLToolKit | 2 +- webrtc/WebRtcTransport.cpp | 34 ++++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/3rdpart/ZLToolKit b/3rdpart/ZLToolKit index 136b6b2f..5b30d6d4 160000 --- a/3rdpart/ZLToolKit +++ b/3rdpart/ZLToolKit @@ -1 +1 @@ -Subproject commit 136b6b2f28193da218f577423db217aeb0f7aa6a +Subproject commit 5b30d6d4cb344b368d4a8272ef07e1656fc85021 diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 9a9989f2..42025238 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -48,6 +48,21 @@ static onceToken token([]() { static atomic s_key{0}; +static void translateIPFromEnv(std::vector &v) { + for (auto iter = v.begin(); iter != v.end();) { + if (start_with(*iter, "$")) { + auto ip = toolkit::getEnv(*iter); + if (ip.empty()) { + iter = v.erase(iter); + } else { + *iter++ = ip; + } + } else { + ++iter; + } + } +} + WebRtcTransport::WebRtcTransport(const EventPoller::Ptr &poller) { _poller = poller; _identifier = "zlm_" + to_string(++s_key); @@ -468,8 +483,9 @@ void WebRtcTransportImp::onStartWebRTC() { // 系统又要有rid,这里手工生成rid,并为其绑定ssrc std::string rid = "r" + std::to_string(index); track->rtp_ext_ctx->setRid(ssrc.ssrc, rid); - if(ssrc.rtx_ssrc) + if (ssrc.rtx_ssrc) { track->rtp_ext_ctx->setRid(ssrc.rtx_ssrc, rid); + } } ++index; } @@ -478,10 +494,12 @@ void WebRtcTransportImp::onStartWebRTC() { void WebRtcTransportImp::onCheckAnswer(RtcSession &sdp) { //修改answer sdp的ip、端口信息 - GET_CONFIG_FUNC(std::vector, extern_ips, RTC::kExternIP, [](string str){ + GET_CONFIG_FUNC(std::vector, extern_ips, RTC::kExternIP, [](string str) { std::vector ret; - if (str.length()) + if (str.length()) { ret = split(str, ","); + } + translateIPFromEnv(ret); return ret; }); for (auto &m : sdp.media) { @@ -556,15 +574,16 @@ void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const { //添加接收端口candidate信息 GET_CONFIG_FUNC(std::vector, extern_ips, RTC::kExternIP, [](string str){ std::vector ret; - if (str.length()) + if (str.length()) { ret = split(str, ","); + } + translateIPFromEnv(ret); return ret; }); if (extern_ips.empty()) { std::string localIp = SockUtil::get_local_ip(); configure.addCandidate(*makeIceCandidate(localIp, local_port, 120, "udp")); - } - else { + } else { const uint32_t delta = 10; uint32_t priority = 100 + delta * extern_ips.size(); for (auto ip : extern_ips) { @@ -575,7 +594,6 @@ void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const { } - /////////////////////////////////////////////////////////////////// class RtpChannel : public RtpTrackImp, public std::enable_shared_from_this { @@ -685,7 +703,7 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) { if (it != _ssrc_to_track.end()) { auto &track = it->second; auto rtp_chn = track->getRtpChannel(sr->ssrc); - if(!rtp_chn){ + if (!rtp_chn) { WarnL << "未识别的sr rtcp包:" << rtcp->dumpString(); } else { //InfoL << "接收丢包率,ssrc:" << sr->ssrc << ",loss rate(%):" << rtp_chn->getLossRate();