支持绑定到指定网卡 (#3760)

This commit is contained in:
u7ko4 2024-08-01 11:03:26 +08:00 committed by GitHub
parent 81aef25583
commit e3cad7f8fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 28 additions and 19 deletions

View File

@ -18,13 +18,15 @@ using namespace mediakit;
API_EXPORT mk_rtp_server API_CALL mk_rtp_server_create(uint16_t port, int tcp_mode, const char *stream_id) {
RtpServer::Ptr *server = new RtpServer::Ptr(new RtpServer);
(*server)->start(port, MediaTuple { DEFAULT_VHOST, kRtpAppName, stream_id, "" }, (RtpServer::TcpMode)tcp_mode);
GET_CONFIG(std::string, local_ip, General::kListenIP)
(*server)->start(port, local_ip.c_str(), MediaTuple { DEFAULT_VHOST, kRtpAppName, stream_id, "" }, (RtpServer::TcpMode)tcp_mode);
return (mk_rtp_server)server;
}
API_EXPORT mk_rtp_server API_CALL mk_rtp_server_create2(uint16_t port, int tcp_mode, const char *vhost, const char *app, const char *stream_id) {
RtpServer::Ptr *server = new RtpServer::Ptr(new RtpServer);
(*server)->start(port, MediaTuple { vhost, app, stream_id, "" }, (RtpServer::TcpMode)tcp_mode);
GET_CONFIG(std::string, local_ip, General::kListenIP)
(*server)->start(port, local_ip.c_str(), MediaTuple { vhost, app, stream_id, "" }, (RtpServer::TcpMode)tcp_mode);
return (mk_rtp_server)server;
}

View File

@ -128,6 +128,8 @@ wait_add_track_ms=3000
unready_frame_cache=100
#是否启用观看人数变化事件广播置1则启用置0则关闭
broadcast_player_count_changed=0
#绑定的本地网卡ip
listen_ip=::
[hls]
#hls写文件的buf大小调整参数可以提高文件io性能

View File

@ -482,7 +482,7 @@ uint16_t openRtpServer(uint16_t local_port, const mediakit::MediaTuple &tuple, i
}
auto server = s_rtp_server.makeWithAction(key, [&](RtpServer::Ptr server) {
server->start(local_port, tuple, (RtpServer::TcpMode)tcp_mode, local_ip.c_str(), re_use_port, ssrc, only_track, multiplex);
server->start(local_port, local_ip.c_str(), tuple, (RtpServer::TcpMode)tcp_mode, re_use_port, ssrc, only_track, multiplex);
});
server->setOnDetach([key](const SockException &ex) {
//设置rtp超时移除事件
@ -1242,7 +1242,7 @@ void installWebApi() {
// 兼容老版本请求新版本去除only_audio参数并新增only_track参数
only_track = 1;
}
std::string local_ip = "::";
GET_CONFIG(std::string, local_ip, General::kListenIP)
if (!allArgs["local_ip"].empty()) {
local_ip = allArgs["local_ip"];
}

View File

@ -281,6 +281,7 @@ int start_main(int argc,char *argv[]) {
});
}
std::string listen_ip = mINI::Instance()[General::kListenIP];
uint16_t shellPort = mINI::Instance()[Shell::kPort];
uint16_t rtspPort = mINI::Instance()[Rtsp::kPort];
uint16_t rtspsPort = mINI::Instance()[Rtsp::kSSLPort];
@ -362,39 +363,39 @@ int start_main(int argc,char *argv[]) {
try {
//rtsp服务器端口默认554
if (rtspPort) { rtspSrv->start<RtspSession>(rtspPort); }
if (rtspPort) { rtspSrv->start<RtspSession>(rtspPort, listen_ip); }
//rtsps服务器端口默认322
if (rtspsPort) { rtspSSLSrv->start<RtspSessionWithSSL>(rtspsPort); }
if (rtspsPort) { rtspSSLSrv->start<RtspSessionWithSSL>(rtspsPort, listen_ip); }
//rtmp服务器端口默认1935
if (rtmpPort) { rtmpSrv->start<RtmpSession>(rtmpPort); }
if (rtmpPort) { rtmpSrv->start<RtmpSession>(rtmpPort, listen_ip); }
//rtmps服务器端口默认19350
if (rtmpsPort) { rtmpsSrv->start<RtmpSessionWithSSL>(rtmpsPort); }
if (rtmpsPort) { rtmpsSrv->start<RtmpSessionWithSSL>(rtmpsPort, listen_ip); }
//http服务器端口默认80
if (httpPort) { httpSrv->start<HttpSession>(httpPort); }
if (httpPort) { httpSrv->start<HttpSession>(httpPort, listen_ip); }
//https服务器端口默认443
if (httpsPort) { httpsSrv->start<HttpsSession>(httpsPort); }
if (httpsPort) { httpsSrv->start<HttpsSession>(httpsPort, listen_ip); }
//telnet远程调试服务器
if (shellPort) { shellSrv->start<ShellSession>(shellPort); }
if (shellPort) { shellSrv->start<ShellSession>(shellPort, listen_ip); }
#if defined(ENABLE_RTPPROXY)
//创建rtp服务器
if (rtpPort) { rtpServer->start(rtpPort); }
if (rtpPort) { rtpServer->start(rtpPort, listen_ip.c_str()); }
#endif//defined(ENABLE_RTPPROXY)
#if defined(ENABLE_WEBRTC)
//webrtc udp服务器
if (rtcPort) { rtcSrv_udp->start<WebRtcSession>(rtcPort);}
if (rtcPort) { rtcSrv_udp->start<WebRtcSession>(rtcPort, listen_ip);}
if (rtcTcpPort) { rtcSrv_tcp->start<WebRtcSession>(rtcTcpPort);}
if (rtcTcpPort) { rtcSrv_tcp->start<WebRtcSession>(rtcTcpPort, listen_ip);}
#endif//defined(ENABLE_WEBRTC)
#if defined(ENABLE_SRT)
// srt udp服务器
if (srtPort) { srtSrv->start<SRT::SrtSession>(srtPort); }
if (srtPort) { srtSrv->start<SRT::SrtSession>(srtPort, listen_ip); }
#endif//defined(ENABLE_SRT)
} catch (std::exception &ex) {

View File

@ -84,6 +84,7 @@ const string kWaitTrackReadyMS = GENERAL_FIELD "wait_track_ready_ms";
const string kWaitAddTrackMS = GENERAL_FIELD "wait_add_track_ms";
const string kUnreadyFrameCache = GENERAL_FIELD "unready_frame_cache";
const string kBroadcastPlayerCountChanged = GENERAL_FIELD "broadcast_player_count_changed";
const string kListenIP = GENERAL_FIELD "listen_ip";
static onceToken token([]() {
mINI::Instance()[kFlowThreshold] = 1024;
@ -99,6 +100,7 @@ static onceToken token([]() {
mINI::Instance()[kWaitAddTrackMS] = 3000;
mINI::Instance()[kUnreadyFrameCache] = 100;
mINI::Instance()[kBroadcastPlayerCountChanged] = 0;
mINI::Instance()[kListenIP] = "::";
});
} // namespace General

View File

@ -202,6 +202,8 @@ extern const std::string kWaitAddTrackMS;
extern const std::string kUnreadyFrameCache;
// 是否启用观看人数变化事件广播置1则启用置0则关闭
extern const std::string kBroadcastPlayerCountChanged;
// 绑定的本地网卡ip
extern const std::string kListenIP;
} // namespace General
namespace Protocol {

View File

@ -122,7 +122,7 @@ private:
std::shared_ptr<struct sockaddr_storage> _rtcp_addr;
};
void RtpServer::start(uint16_t local_port, const MediaTuple &tuple, TcpMode tcp_mode, const char *local_ip, bool re_use_port, uint32_t ssrc, int only_track, bool multiplex) {
void RtpServer::start(uint16_t local_port, const char *local_ip, const MediaTuple &tuple, TcpMode tcp_mode, bool re_use_port, uint32_t ssrc, int only_track, bool multiplex) {
//创建udp服务器
auto poller = EventPollerPool::Instance().getPoller();
Socket::Ptr rtp_socket = Socket::createSocket(poller, true);

View File

@ -36,15 +36,15 @@ public:
/**
*
* @param local_port 0
* @param local_ip ip
* @param stream_id id使ssrc
* @param tcp_mode tcp服务模式
* @param local_ip ip
* @param re_use_port socket为re_use属性
* @param ssrc ssrc
* @param multiplex
*/
void start(uint16_t local_port, const MediaTuple &tuple = MediaTuple{DEFAULT_VHOST, kRtpAppName, "", ""}, TcpMode tcp_mode = PASSIVE,
const char *local_ip = "::", bool re_use_port = true, uint32_t ssrc = 0, int only_track = 0, bool multiplex = false);
void start(uint16_t local_port, const char *local_ip = "::", const MediaTuple &tuple = MediaTuple{DEFAULT_VHOST, kRtpAppName, "", ""}, TcpMode tcp_mode = PASSIVE,
bool re_use_port = true, uint32_t ssrc = 0, int only_track = 0, bool multiplex = false);
/**
* tcp服务(tcp主动模式)