mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 19:00:01 +08:00
openRtpServer接口新增re_use_port参数
This commit is contained in:
parent
6073175e75
commit
a6e82799f5
@ -1289,6 +1289,11 @@
|
|||||||
"key": "stream_id",
|
"key": "stream_id",
|
||||||
"value": "test",
|
"value": "test",
|
||||||
"description": "该端口绑定的流id\n"
|
"description": "该端口绑定的流id\n"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"key": "re_use_port",
|
||||||
|
"value": "0",
|
||||||
|
"description": "是否重用端口,默认为0,非必选参数"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -1056,7 +1056,7 @@ void installWebApi() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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>(), "0.0.0.0", false);
|
server->start(allArgs["port"], stream_id, allArgs["enable_tcp"].as<bool>(), "0.0.0.0", allArgs["re_use_port"].as<bool>());
|
||||||
server->setOnDetach([stream_id]() {
|
server->setOnDetach([stream_id]() {
|
||||||
//设置rtp超时移除事件
|
//设置rtp超时移除事件
|
||||||
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);
|
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);
|
||||||
|
@ -88,21 +88,21 @@ 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, bool enable_reuse) {
|
void RtpServer::start(uint16_t local_port, const string &stream_id, bool enable_tcp, const char *local_ip, bool re_use_port) {
|
||||||
//创建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);
|
||||||
if (local_port == 0) {
|
if (local_port == 0) {
|
||||||
//随机端口,rtp端口采用偶数
|
//随机端口,rtp端口采用偶数
|
||||||
auto pair = std::make_pair(rtp_socket, rtcp_socket);
|
auto pair = std::make_pair(rtp_socket, rtcp_socket);
|
||||||
makeSockPair(pair, local_ip);
|
makeSockPair(pair, local_ip, re_use_port);
|
||||||
//取偶数端口
|
//取偶数端口
|
||||||
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, enable_reuse)) {
|
} else if (!rtp_socket->bindUdpSock(local_port, local_ip, re_use_port)) {
|
||||||
//用户指定端口
|
//用户指定端口
|
||||||
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, enable_reuse)) {
|
} else if (!rtcp_socket->bindUdpSock(rtp_socket->get_local_port() + 1, local_ip, re_use_port)) {
|
||||||
// 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));
|
||||||
}
|
}
|
||||||
|
@ -37,8 +37,9 @@ public:
|
|||||||
* @param stream_id 流id,置空则使用ssrc
|
* @param stream_id 流id,置空则使用ssrc
|
||||||
* @param enable_tcp 是否启用tcp服务器
|
* @param enable_tcp 是否启用tcp服务器
|
||||||
* @param local_ip 绑定的本地网卡ip
|
* @param local_ip 绑定的本地网卡ip
|
||||||
|
* @param re_use_port 是否设置socket为re_use属性
|
||||||
*/
|
*/
|
||||||
void start(uint16_t local_port, const std::string &stream_id = "", bool enable_tcp = true, const char *local_ip = "0.0.0.0",bool enable_reuse = true);
|
void start(uint16_t local_port, const std::string &stream_id = "", bool enable_tcp = true, const char *local_ip = "0.0.0.0", bool re_use_port = true);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取绑定的本地端口
|
* 获取绑定的本地端口
|
||||||
|
@ -384,7 +384,7 @@ public:
|
|||||||
return *instance;
|
return *instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
void bindUdpSock(std::pair<Socket::Ptr, Socket::Ptr> &pair, const string &local_ip) {
|
void bindUdpSock(std::pair<Socket::Ptr, Socket::Ptr> &pair, const string &local_ip, bool re_use_port) {
|
||||||
auto &sock0 = pair.first;
|
auto &sock0 = pair.first;
|
||||||
auto &sock1 = pair.second;
|
auto &sock1 = pair.second;
|
||||||
auto sock_pair = getPortPair();
|
auto sock_pair = getPortPair();
|
||||||
@ -392,12 +392,12 @@ public:
|
|||||||
throw runtime_error("none reserved udp port in pool");
|
throw runtime_error("none reserved udp port in pool");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sock0->bindUdpSock(2 * *sock_pair, local_ip.data(), false)) {
|
if (!sock0->bindUdpSock(2 * *sock_pair, local_ip.data(), re_use_port)) {
|
||||||
//分配端口失败
|
//分配端口失败
|
||||||
throw runtime_error("open udp socket[0] failed");
|
throw runtime_error("open udp socket[0] failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sock1->bindUdpSock(2 * *sock_pair + 1, local_ip.data(), false)) {
|
if (!sock1->bindUdpSock(2 * *sock_pair + 1, local_ip.data(), re_use_port)) {
|
||||||
//分配端口失败
|
//分配端口失败
|
||||||
throw runtime_error("open udp socket[1] failed");
|
throw runtime_error("open udp socket[1] failed");
|
||||||
}
|
}
|
||||||
@ -445,18 +445,14 @@ private:
|
|||||||
deque<uint16_t> _port_pair_pool;
|
deque<uint16_t> _port_pair_pool;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void makeSockPair_l(std::pair<Socket::Ptr, Socket::Ptr> &pair, const string &local_ip) {
|
void makeSockPair(std::pair<Socket::Ptr, Socket::Ptr> &pair, const string &local_ip, bool re_use_port) {
|
||||||
PortManager::Instance().bindUdpSock(pair, local_ip);
|
|
||||||
}
|
|
||||||
|
|
||||||
void makeSockPair(std::pair<Socket::Ptr, Socket::Ptr> &pair, const string &local_ip) {
|
|
||||||
//全局互斥锁保护,防止端口重复分配
|
//全局互斥锁保护,防止端口重复分配
|
||||||
static recursive_mutex s_mtx;
|
static recursive_mutex s_mtx;
|
||||||
lock_guard<recursive_mutex> lck(s_mtx);
|
lock_guard<recursive_mutex> lck(s_mtx);
|
||||||
int try_count = 0;
|
int try_count = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
makeSockPair_l(pair, local_ip);
|
PortManager::Instance().bindUdpSock(pair, local_ip, re_use_port);
|
||||||
break;
|
break;
|
||||||
} catch (exception &ex) {
|
} catch (exception &ex) {
|
||||||
if (++try_count == 3) {
|
if (++try_count == 3) {
|
||||||
|
@ -378,7 +378,7 @@ private:
|
|||||||
//创建rtp over tcp4个字节的头
|
//创建rtp over tcp4个字节的头
|
||||||
toolkit::Buffer::Ptr makeRtpOverTcpPrefix(uint16_t size, uint8_t interleaved);
|
toolkit::Buffer::Ptr makeRtpOverTcpPrefix(uint16_t size, uint8_t interleaved);
|
||||||
//创建rtp-rtcp端口对
|
//创建rtp-rtcp端口对
|
||||||
void makeSockPair(std::pair<toolkit::Socket::Ptr, toolkit::Socket::Ptr> &pair, const std::string &local_ip);
|
void makeSockPair(std::pair<toolkit::Socket::Ptr, toolkit::Socket::Ptr> &pair, const std::string &local_ip, bool re_use_port = false);
|
||||||
//十六进制方式打印ssrc
|
//十六进制方式打印ssrc
|
||||||
std::string printSSRC(uint32_t ui32Ssrc);
|
std::string printSSRC(uint32_t ui32Ssrc);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user