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