From 25a1434e005104ecc4b372c5be01035ac150cb2c Mon Sep 17 00:00:00 2001 From: Johnny Date: Fri, 15 Oct 2021 18:56:49 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E4=B8=80=E6=AD=A5=E6=94=B9=E5=96=84?= =?UTF-8?q?=20WebRtcTransportImp=20=E7=9A=84=E7=94=9F=E5=91=BD=E5=91=A8?= =?UTF-8?q?=E6=9C=9F=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webrtc/WebRtcSession.cpp | 9 +++--- webrtc/WebRtcTransport.cpp | 60 +++++++++++++------------------------- webrtc/WebRtcTransport.h | 20 ++++++++----- 3 files changed, 38 insertions(+), 51 deletions(-) diff --git a/webrtc/WebRtcSession.cpp b/webrtc/WebRtcSession.cpp index 6dd508e9..856684e3 100644 --- a/webrtc/WebRtcSession.cpp +++ b/webrtc/WebRtcSession.cpp @@ -44,7 +44,7 @@ EventPoller::Ptr QueryPollerByBuffer(const Buffer::Ptr &buffer) { if (user_name.empty()) { return nullptr; } - auto ret = WebRtcTransportImp::get(user_name); + auto ret = WebRtcTransportManager::instance().getItem(user_name); return ret ? ret->getPoller() : nullptr; } @@ -62,9 +62,10 @@ void WebRtcSession::onRecv_l(const Buffer::Ptr &buffer) { _find_transport = false; auto user_name = getUserName(buffer); _identifier = user_name + '-' + to_string(reinterpret_cast(this)); - _transport = WebRtcTransportImp::move(user_name); - CHECK(_transport && _transport->getPoller()->isCurrentThread()); - _transport->setSession(shared_from_this()); + auto transport = WebRtcTransportManager::instance().getItem(user_name); + CHECK(transport && transport->getPoller()->isCurrentThread()); + transport->setSession(shared_from_this()); + _transport = std::move(transport); InfoP(this); } _ticker.resetTime(); diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 2b982094..7000ea48 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -840,6 +840,7 @@ void WebRtcTransportImp::onShutdown(const SockException &ex){ void WebRtcTransportImp::setSession(Session::Ptr session) { _session = std::move(session); + unrefSelf(); } const Session::Ptr &WebRtcTransportImp::getSession() const { @@ -856,36 +857,6 @@ uint64_t WebRtcTransportImp::getDuration() const{ ///////////////////////////////////////////////////////////////////////////////////////////// -class WebRtcTransportManager { - mutable mutex _mtx; - unordered_map > _map; - WebRtcTransportManager() = default; - -public: - static WebRtcTransportManager& instance() { - static WebRtcTransportManager s_instance; - return s_instance; - } - void addItem(string key, const WebRtcTransportImp::Ptr &ptr) { - lock_guard lck(_mtx); - _map[key] = ptr; - } - WebRtcTransportImp::Ptr getItem(const string &key) { - if (key.empty()) { - return nullptr; - } - lock_guard lck(_mtx); - auto it = _map.find(key); - if (it == _map.end()) { - return nullptr; - } - return it->second.lock(); - } - void removeItem(string key) { - lock_guard lck(_mtx); - _map.erase(key); - } -}; void WebRtcTransportImp::registerSelf() { _self = static_pointer_cast(shared_from_this()); @@ -901,15 +872,26 @@ void WebRtcTransportImp::unregisterSelf() { WebRtcTransportManager::instance().removeItem(getIdentifier()); } -WebRtcTransportImp::Ptr WebRtcTransportImp::get(const string &key) { - return WebRtcTransportManager::instance().getItem(key); +WebRtcTransportManager &WebRtcTransportManager::instance() { + static WebRtcTransportManager s_instance; + return s_instance; } - -WebRtcTransportImp::Ptr WebRtcTransportImp::move(const string &key) { - auto ret = WebRtcTransportManager::instance().getItem(key); - if (ret) { - //此对象不再强引用自己,因为自己将被WebRtcSession对象持有 - ret->unrefSelf(); +void WebRtcTransportManager::addItem(string key, const WebRtcTransportImp::Ptr &ptr) { + lock_guard lck(_mtx); + _map[key] = ptr; +} +WebRtcTransportImp::Ptr WebRtcTransportManager::getItem(const string &key) { + if (key.empty()) { + return nullptr; } - return ret; + lock_guard lck(_mtx); + auto it = _map.find(key); + if (it == _map.end()) { + return nullptr; + } + return it->second.lock(); +} +void WebRtcTransportManager::removeItem(string key) { + lock_guard lck(_mtx); + _map.erase(key); } diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index 7e58b947..eb2a72cc 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -166,14 +166,6 @@ public: using Ptr = std::shared_ptr; ~WebRtcTransportImp() override; - /** - * 创建WebRTC对象 - * @param poller 改对象需要绑定的线程 - * @return 对象 - */ - static Ptr get(const string &key); // 借用 - static Ptr move(const string &key); // 所有权转移 - void setSession(Session::Ptr session); const Session::Ptr& getSession() const; uint64_t getBytesUsage() const; @@ -231,4 +223,16 @@ private: unordered_map _ssrc_to_track; //根据接收rtp的pt获取相关信息 unordered_map > _pt_to_track; +}; + +class WebRtcTransportManager { + mutable mutex _mtx; + unordered_map > _map; + WebRtcTransportManager() = default; + +public: + static WebRtcTransportManager& instance(); + void addItem(string key, const WebRtcTransportImp::Ptr &ptr); + WebRtcTransportImp::Ptr getItem(const string &key); + void removeItem(string key); }; \ No newline at end of file