Merge branch 'master' of github.com:ZLMediaKit/ZLMediaKit

This commit is contained in:
ziyue 2022-01-17 10:36:00 +08:00
commit 0c4410c523
5 changed files with 25 additions and 8 deletions

@ -1 +1 @@
Subproject commit dd101bcc31290e3d1ec1ae939d937f2712719a0d Subproject commit a71747c7d0811263b12ef9112ec13fdffe2ab171

View File

@ -1025,17 +1025,20 @@ void installWebApi() {
api_regist("/index/api/openRtpServer",[](API_ARGS_MAP){ api_regist("/index/api/openRtpServer",[](API_ARGS_MAP){
CHECK_SECRET(); CHECK_SECRET();
CHECK_ARGS("port", "enable_tcp", "stream_id"); CHECK_ARGS("port", "enable_tcp", "stream_id");
bool enable_reuse = true;
if (!allArgs["enable_reuse"].empty()) {
enable_reuse = allArgs["enable_reuse"].as<bool>();
}
auto stream_id = allArgs["stream_id"]; auto stream_id = allArgs["stream_id"];
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx); lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);
if(s_rtpServerMap.find(stream_id) != s_rtpServerMap.end()) { if (s_rtpServerMap.find(stream_id) != s_rtpServerMap.end()) {
//为了防止RtpProcess所有权限混乱的问题不允许重复添加相同的stream_id //为了防止RtpProcess所有权限混乱的问题不允许重复添加相同的stream_id
throw InvalidArgsException("该stream_id已存在"); throw InvalidArgsException("该stream_id已存在");
} }
RtpServer::Ptr server = std::make_shared<RtpServer>(); RtpServer::Ptr server = std::make_shared<RtpServer>();
server->start(allArgs["port"], stream_id, allArgs["enable_tcp"].as<bool>()); server->start(allArgs["port"], stream_id, allArgs["enable_tcp"].as<bool>(), "0.0.0.0", enable_reuse);
server->setOnDetach([stream_id]() { server->setOnDetach([stream_id]() {
//设置rtp超时移除事件 //设置rtp超时移除事件
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx); lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);

View File

@ -84,7 +84,7 @@ private:
std::shared_ptr<struct sockaddr> _rtcp_addr; std::shared_ptr<struct sockaddr> _rtcp_addr;
}; };
void RtpServer::start(uint16_t local_port, const string &stream_id, bool enable_tcp, const char *local_ip) { void RtpServer::start(uint16_t local_port, const string &stream_id, bool enable_tcp, const char *local_ip,bool enable_reuse) {
//创建udp服务器 //创建udp服务器
Socket::Ptr rtp_socket = Socket::createSocket(nullptr, true); Socket::Ptr rtp_socket = Socket::createSocket(nullptr, true);
Socket::Ptr rtcp_socket = Socket::createSocket(nullptr, true); Socket::Ptr rtcp_socket = Socket::createSocket(nullptr, true);
@ -95,10 +95,10 @@ void RtpServer::start(uint16_t local_port, const string &stream_id, bool enable
//取偶数端口 //取偶数端口
rtp_socket = pair.first; rtp_socket = pair.first;
rtcp_socket = pair.second; rtcp_socket = pair.second;
} else if (!rtp_socket->bindUdpSock(local_port, local_ip)) { } else if (!rtp_socket->bindUdpSock(local_port, local_ip,enable_reuse)) {
//用户指定端口 //用户指定端口
throw std::runtime_error(StrPrinter << "创建rtp端口 " << local_ip << ":" << local_port << " 失败:" << get_uv_errmsg(true)); throw std::runtime_error(StrPrinter << "创建rtp端口 " << local_ip << ":" << local_port << " 失败:" << get_uv_errmsg(true));
} else if(!rtcp_socket->bindUdpSock(rtp_socket->get_local_port() + 1, local_ip)) { } else if(!rtcp_socket->bindUdpSock(rtp_socket->get_local_port() + 1, local_ip,enable_reuse)) {
// rtcp端口 // rtcp端口
throw std::runtime_error(StrPrinter << "创建rtcp端口 " << local_ip << ":" << local_port << " 失败:" << get_uv_errmsg(true)); throw std::runtime_error(StrPrinter << "创建rtcp端口 " << local_ip << ":" << local_port << " 失败:" << get_uv_errmsg(true));
} }

View File

@ -41,7 +41,7 @@ public:
* @param enable_tcp tcp服务器 * @param enable_tcp tcp服务器
* @param local_ip ip * @param local_ip ip
*/ */
void start(uint16_t local_port, const string &stream_id = "", bool enable_tcp = true, const char *local_ip = "0.0.0.0"); void start(uint16_t local_port, const string &stream_id = "", bool enable_tcp = true, const char *local_ip = "0.0.0.0",bool enable_reuse = true);
/** /**
* *

View File

@ -300,6 +300,20 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int track_idx) {
if (-1 == SockUtil::joinMultiAddrFilter(fd, multiAddr.data(), get_peer_ip().data(),get_local_ip().data())) { if (-1 == SockUtil::joinMultiAddrFilter(fd, multiAddr.data(), get_peer_ip().data(),get_local_ip().data())) {
SockUtil::joinMultiAddr(fd, multiAddr.data(),get_local_ip().data()); SockUtil::joinMultiAddr(fd, multiAddr.data(),get_local_ip().data());
} }
//设置rtcp发送端口
pRtcpSockRef = createSocket();
if (!pRtcpSockRef->bindUdpSock(0, "0.0.0.0")) {
//分配端口失败
throw runtime_error("open udp socket failed");
}
//设置发送地址和发送端口
struct sockaddr_in rtpto;
rtpto.sin_port = ntohs(rtcp_port);
rtpto.sin_family = AF_INET;
rtpto.sin_addr.s_addr = inet_addr(get_peer_ip().data());
pRtcpSockRef->bindPeerAddr((struct sockaddr *)&(rtpto));
} else { } else {
createUdpSockIfNecessary(track_idx); createUdpSockIfNecessary(track_idx);
//udp单播 //udp单播