diff --git a/src/Rtp/RtpServer.cpp b/src/Rtp/RtpServer.cpp index 3c3a0686..e8c944b0 100644 --- a/src/Rtp/RtpServer.cpp +++ b/src/Rtp/RtpServer.cpp @@ -143,27 +143,6 @@ void RtpServer::start(uint16_t local_port, const string &stream_id, TcpMode tcp_ GET_CONFIG(int, udpRecvSocketBuffer, RtpProxy::kUdpRecvSocketBuffer); SockUtil::setRecvBuf(rtp_socket->rawFD(), udpRecvSocketBuffer); - TcpServer::Ptr tcp_server; - _tcp_mode = tcp_mode; - if (tcp_mode == PASSIVE || tcp_mode == ACTIVE) { - //创建tcp服务器 - tcp_server = std::make_shared(rtp_socket->getPoller()); - (*tcp_server)[RtpSession::kStreamID] = stream_id; - (*tcp_server)[RtpSession::kSSRC] = ssrc; - (*tcp_server)[RtpSession::kOnlyTrack] = only_track; - if (tcp_mode == PASSIVE) { - weak_ptr weak_self = shared_from_this(); - tcp_server->start(local_port, local_ip, 1024, [weak_self](std::shared_ptr &session) { - if (auto strong_self = weak_self.lock()) { - session->setRtpProcess(strong_self->_rtcp_helper->getProcess()); - } - }); - } else if (stream_id.empty()) { - // tcp主动模式时只能一个端口一个流,必须指定流id; 创建TcpServer对象也仅用于传参 - throw std::runtime_error(StrPrinter << "tcp主动模式时必需指定流id"); - } - } - //创建udp服务器 UdpServer::Ptr udp_server; RtcpHelper::Ptr helper; @@ -193,13 +172,32 @@ void RtpServer::start(uint16_t local_port, const string &stream_id, TcpMode tcp_ }); } else { //单端口多线程接收多个流,根据ssrc区分流 - udp_server = std::make_shared(rtp_socket->getPoller()); + udp_server = std::make_shared(); (*udp_server)[RtpSession::kOnlyTrack] = only_track; (*udp_server)[RtpSession::kUdpRecvBuffer] = udpRecvSocketBuffer; udp_server->start(local_port, local_ip); rtp_socket = nullptr; } + TcpServer::Ptr tcp_server; + if (tcp_mode == PASSIVE || tcp_mode == ACTIVE) { + //创建tcp服务器 + tcp_server = std::make_shared(); + (*tcp_server)[RtpSession::kStreamID] = stream_id; + (*tcp_server)[RtpSession::kSSRC] = ssrc; + (*tcp_server)[RtpSession::kOnlyTrack] = only_track; + if (tcp_mode == PASSIVE) { + weak_ptr weak_self = shared_from_this(); + auto processor = helper ? helper->getProcess() : nullptr; + tcp_server->start(local_port, local_ip, 1024, [weak_self, processor](std::shared_ptr &session) { + session->setRtpProcess(processor); + }); + } else if (stream_id.empty()) { + // tcp主动模式时只能一个端口一个流,必须指定流id; 创建TcpServer对象也仅用于传参 + throw std::runtime_error(StrPrinter << "tcp主动模式时必需指定流id"); + } + } + _on_cleanup = [rtp_socket, stream_id]() { if (rtp_socket) { //去除循环引用 @@ -211,6 +209,7 @@ void RtpServer::start(uint16_t local_port, const string &stream_id, TcpMode tcp_ _udp_server = udp_server; _rtp_socket = rtp_socket; _rtcp_helper = helper; + _tcp_mode = tcp_mode; } void RtpServer::setOnDetach(RtpProcess::onDetachCB cb) { diff --git a/src/Rtp/RtpSession.cpp b/src/Rtp/RtpSession.cpp index 3c22c875..852a8796 100644 --- a/src/Rtp/RtpSession.cpp +++ b/src/Rtp/RtpSession.cpp @@ -73,7 +73,7 @@ void RtpSession::onManager() { } void RtpSession::setRtpProcess(RtpProcess::Ptr process) { - _emit_detach = true; + _emit_detach = (bool)process; _process = std::move(process); }