diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 14b654c3..2490d39a 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -20,6 +20,7 @@ #include #include +#include #include "Util/MD5.h" #include "Util/util.h" #include "Util/File.h" @@ -1746,16 +1747,22 @@ void installWebApi() { auto type = allArgs["type"]; auto offer = allArgs.getArgs(); CHECK(!offer.empty(), "http body(webrtc offer sdp) is empty"); + std::string host = allArgs.getParser()["Host"]; + std::string localIp = host.substr(0, host.find(':')); + std::regex ipv4Regex("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"); + if (!std::regex_match(localIp, ipv4Regex)) { + localIp = ""; + } auto args = std::make_shared(allArgs, sender.getIdentifier()); - WebRtcPluginManager::Instance().getAnswerSdp(static_cast(sender), type, *args, - [invoker, val, offer, headerOut](const WebRtcInterface &exchanger) mutable { + WebRtcPluginManager::Instance().getAnswerSdp(static_cast(sender), type, *args, [invoker, val, offer, headerOut, localIp](const WebRtcInterface &exchanger) mutable { //设置返回类型 headerOut["Content-Type"] = HttpFileManager::getContentType(".json"); //设置跨域 headerOut["Access-Control-Allow-Origin"] = "*"; try { + setLocalIp(exchanger,localIp); val["sdp"] = exchangeSdp(exchanger, offer); val["id"] = exchanger.getIdentifier(); val["type"] = "answer"; diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index d338a9d7..7a39b8da 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -635,7 +635,7 @@ void WebRtcTransportImp::onCheckAnswer(RtcSession &sdp) { }); for (auto &m : sdp.media) { m.addr.reset(); - m.addr.address = extern_ips.empty() ? SockUtil::get_local_ip() : extern_ips[0]; + m.addr.address = extern_ips.empty() ? _localIp.empty() ? SockUtil::get_local_ip() : _localIp : extern_ips[0]; m.rtcp_addr.reset(); m.rtcp_addr.address = m.addr.address; @@ -730,7 +730,7 @@ void WebRtcTransportImp::onRtcConfigure(RtcConfigure &configure) const { return ret; }); if (extern_ips.empty()) { - std::string local_ip = SockUtil::get_local_ip(); + std::string local_ip = _localIp.empty() ? SockUtil::get_local_ip() : _localIp; if (local_udp_port) { configure.addCandidate(*makeIceCandidate(local_ip, local_udp_port, 120, "udp")); } if (local_tcp_port) { configure.addCandidate(*makeIceCandidate(local_ip, local_tcp_port, _preferred_tcp ? 125 : 115, "tcp")); } } else { @@ -748,6 +748,10 @@ void WebRtcTransportImp::setIceCandidate(vector cands) { _cands = std::move(cands); } +void WebRtcTransportImp::setLocalIp(const std::string &localIp) { + _localIp = localIp; +} + /////////////////////////////////////////////////////////////////// class RtpChannel : public RtpTrackImp, public std::enable_shared_from_this { @@ -1239,6 +1243,10 @@ std::string exchangeSdp(const WebRtcInterface &exchanger, const std::string& off return const_cast(exchanger).getAnswerSdp(offer); } +void setLocalIp(const WebRtcInterface& exchanger, const std::string& localIp) { + return const_cast(exchanger).setLocalIp(localIp); +} + void WebRtcPluginManager::setListener(Listener cb) { lock_guard lck(_mtx_creator); _listener = std::move(cb); diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index d0e1d67c..8e8d97fc 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -42,10 +42,13 @@ public: virtual const std::string& getIdentifier() const = 0; virtual const std::string& deleteRandStr() const { static std::string s_null; return s_null; } virtual void setIceCandidate(std::vector cands) {} + virtual void setLocalIp(const std::string &localIp) {} }; std::string exchangeSdp(const WebRtcInterface &exchanger, const std::string& offer); +void setLocalIp(const WebRtcInterface &exchanger, const std::string &localIp); + class WebRtcException : public WebRtcInterface { public: WebRtcException(const SockException &ex) : _ex(ex) {}; @@ -253,6 +256,7 @@ public: void removeTuple(RTC::TransportTuple* tuple); void safeShutdown(const SockException &ex); + void setLocalIp(const std::string &localIp) override; protected: void OnIceServerSelectedTuple(const RTC::IceServer *iceServer, RTC::TransportTuple *tuple) override; WebRtcTransportImp(const EventPoller::Ptr &poller,bool preferred_tcp = false); @@ -306,6 +310,8 @@ private: //根据接收rtp的pt获取相关信息 std::unordered_map> _pt_to_track; std::vector _cands; + //源访问的hostip + std::string _localIp; }; class WebRtcTransportManager {