Skip to content

Commit

Permalink
保证rtp服务器tcp模式下线程安全
Browse files Browse the repository at this point in the history
  • Loading branch information
xia-chu committed Jun 22, 2024
1 parent 2ead272 commit 2bf16e6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion 3rdpart/ZLToolKit
Submodule ZLToolKit updated from 5144e2 to 79c10f
11 changes: 6 additions & 5 deletions src/Rtp/RtpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ class RtcpHelper: public std::enable_shared_from_this<RtcpHelper> {

void RtpServer::start(uint16_t local_port, const string &stream_id, TcpMode tcp_mode, const char *local_ip, bool re_use_port, uint32_t ssrc, int only_track, bool multiplex) {
//创建udp服务器
Socket::Ptr rtp_socket = Socket::createSocket(nullptr, true);
Socket::Ptr rtcp_socket = Socket::createSocket(nullptr, true);
auto poller = EventPollerPool::Instance().getPoller();
Socket::Ptr rtp_socket = Socket::createSocket(poller, true);
Socket::Ptr rtcp_socket = Socket::createSocket(poller, true);
if (local_port == 0) {
//随机端口,rtp端口采用偶数
auto pair = std::make_pair(rtp_socket, rtcp_socket);
Expand Down Expand Up @@ -181,14 +182,14 @@ void RtpServer::start(uint16_t local_port, const string &stream_id, TcpMode tcp_

TcpServer::Ptr tcp_server;
if (tcp_mode == PASSIVE || tcp_mode == ACTIVE) {
//创建tcp服务器
tcp_server = std::make_shared<TcpServer>();
auto processor = helper ? helper->getProcess() : nullptr;
// 如果共享同一个processor对象,那么tcp server深圳为单线程模式确保线程安全
tcp_server = std::make_shared<TcpServer>(processor ? poller : nullptr);
(*tcp_server)[RtpSession::kStreamID] = stream_id;
(*tcp_server)[RtpSession::kSSRC] = ssrc;
(*tcp_server)[RtpSession::kOnlyTrack] = only_track;
if (tcp_mode == PASSIVE) {
weak_ptr<RtpServer> weak_self = shared_from_this();
auto processor = helper ? helper->getProcess() : nullptr;
tcp_server->start<RtpSession>(local_port, local_ip, 1024, [weak_self, processor](std::shared_ptr<RtpSession> &session) {
session->setRtpProcess(processor);
});
Expand Down

0 comments on commit 2bf16e6

Please sign in to comment.