mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 10:40:05 +08:00
Auto get icecand ip address from http request (#3251)
Get ip address from http `Host` header, and set it to icecand ip for webrtc
This commit is contained in:
parent
9977b550e0
commit
fd1ebb1a51
@ -20,6 +20,7 @@
|
||||
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include <regex>
|
||||
#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<WebRtcArgsImp>(allArgs, sender.getIdentifier());
|
||||
WebRtcPluginManager::Instance().getAnswerSdp(static_cast<Session&>(sender), type, *args,
|
||||
[invoker, val, offer, headerOut](const WebRtcInterface &exchanger) mutable {
|
||||
WebRtcPluginManager::Instance().getAnswerSdp(static_cast<Session&>(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";
|
||||
|
@ -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<SdpAttrCandidate> cands) {
|
||||
_cands = std::move(cands);
|
||||
}
|
||||
|
||||
void WebRtcTransportImp::setLocalIp(const std::string &localIp) {
|
||||
_localIp = localIp;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
class RtpChannel : public RtpTrackImp, public std::enable_shared_from_this<RtpChannel> {
|
||||
@ -1239,6 +1243,10 @@ std::string exchangeSdp(const WebRtcInterface &exchanger, const std::string& off
|
||||
return const_cast<WebRtcInterface &>(exchanger).getAnswerSdp(offer);
|
||||
}
|
||||
|
||||
void setLocalIp(const WebRtcInterface& exchanger, const std::string& localIp) {
|
||||
return const_cast<WebRtcInterface &>(exchanger).setLocalIp(localIp);
|
||||
}
|
||||
|
||||
void WebRtcPluginManager::setListener(Listener cb) {
|
||||
lock_guard<mutex> lck(_mtx_creator);
|
||||
_listener = std::move(cb);
|
||||
|
@ -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<SdpAttrCandidate> 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<uint8_t/*pt*/, std::unique_ptr<WrappedMediaTrack>> _pt_to_track;
|
||||
std::vector<SdpAttrCandidate> _cands;
|
||||
//源访问的hostip
|
||||
std::string _localIp;
|
||||
};
|
||||
|
||||
class WebRtcTransportManager {
|
||||
|
Loading…
Reference in New Issue
Block a user