http api新建的rtp服务器可以自动超时移除

This commit is contained in:
xiongziliang 2020-07-08 10:25:30 +08:00
parent e58a63c528
commit b2ff53037b
6 changed files with 47 additions and 2 deletions

View File

@ -761,11 +761,20 @@ void installWebApi() {
RtpServer::Ptr server = std::make_shared<RtpServer>();
server->start(allArgs["port"], allArgs["stream_id"], allArgs["enable_tcp"].as<bool>());
val["port"] = server->getPort();
auto port = server->getPort();
server->setOnDetach([port]() {
//设置rtp超时移除事件
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);
s_rtpServerMap.erase(port);
});
//保存对象
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);
s_rtpServerMap.emplace(server->getPort(), server);
s_rtpServerMap.emplace(port, server);
//回复json
val["port"] = port;
});
api_regist1("/index/api/closeRtpServer",[](API_ARGS1){

View File

@ -177,6 +177,16 @@ bool RtpProcess::alive() {
return false;
}
void RtpProcess::onDetach(){
if(_on_detach){
_on_detach();
}
}
void RtpProcess::setOnDetach(const function<void()> &cb) {
_on_detach = cb;
}
string RtpProcess::get_peer_ip() {
if(_addr){
return SockUtil::inet_ntoa(((struct sockaddr_in *) _addr)->sin_addr);

View File

@ -44,6 +44,16 @@ public:
*/
bool alive();
/**
* RtpSelector移除时触发
*/
void onDetach();
/**
* onDetach事件回调
*/
void setOnDetach(const function<void()> &cb);
/// SockInfo override
string get_local_ip() override;
uint16_t get_local_port() override;
@ -79,6 +89,7 @@ private:
MediaInfo _media_info;
uint64_t _total_bytes = 0;
Socket::Ptr _sock;
function<void()> _on_detach;
};
}//namespace mediakit

View File

@ -91,7 +91,9 @@ void RtpSelector::onManager() {
continue;
}
WarnL << "RtpProcess timeout:" << it->first;
auto process = it->second->getProcess();
it = _map_rtp_process.erase(it);
process->onDetach();
}
}

View File

@ -69,6 +69,13 @@ void RtpServer::start(uint16_t local_port, const string &stream_id, bool enable
_tcp_server = tcp_server;
_udp_server = udp_server;
_rtp_process = process;
}
void RtpServer::setOnDetach(const function<void()> &cb){
if(_rtp_process){
_rtp_process->setOnDetach(cb);
}
}
EventPoller::Ptr RtpServer::getPoller() {

View File

@ -52,9 +52,15 @@ public:
*/
EventPoller::Ptr getPoller();
/**
* RtpProcess onDetach事件回调
*/
void setOnDetach(const function<void()> &cb);
protected:
Socket::Ptr _udp_server;
TcpServer::Ptr _tcp_server;
RtpProcess::Ptr _rtp_process;
function<void()> _on_clearup;
};