mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 10:40:05 +08:00
parent
acbe99cb43
commit
2ec3b0ccea
@ -45,7 +45,7 @@ EventPoller::Ptr WebRtcSession::getPoller(const Buffer::Ptr &buffer) {
|
||||
if (user_name.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
auto ret = WebRtcTransportImp::getRtcTransport(user_name, false);
|
||||
auto ret = WebRtcTransportImp::get(user_name);
|
||||
return ret ? ret->getPoller() : nullptr;
|
||||
}
|
||||
|
||||
@ -61,7 +61,7 @@ void WebRtcSession::onRecv_l(const Buffer::Ptr &buffer) {
|
||||
if (_find_transport) {
|
||||
//只允许寻找一次transport
|
||||
_find_transport = false;
|
||||
_transport = WebRtcTransportImp::getRtcTransport(getUserName(buffer), true);
|
||||
_transport = WebRtcTransportImp::move(getUserName(buffer));
|
||||
CHECK(_transport && _transport->getPoller()->isCurrentThread());
|
||||
_transport->setSession(shared_from_this());
|
||||
}
|
||||
|
@ -1010,13 +1010,40 @@ void WebRtcTransportImp::setSession(Session::Ptr session) {
|
||||
_session = std::move(session);
|
||||
}
|
||||
|
||||
static mutex s_rtc_mtx;
|
||||
static unordered_map<string, weak_ptr<WebRtcTransportImp> > s_rtc_map;
|
||||
class WebRtcTransportManager {
|
||||
mutable mutex _mtx;
|
||||
unordered_map<string, weak_ptr<WebRtcTransportImp> > _map;
|
||||
WebRtcTransportManager() = default;
|
||||
|
||||
public:
|
||||
static WebRtcTransportManager& instance() {
|
||||
static WebRtcTransportManager s_instance;
|
||||
return s_instance;
|
||||
}
|
||||
void addItem(string key, const WebRtcTransportImp::Ptr &ptr) {
|
||||
lock_guard<mutex> lck(_mtx);
|
||||
_map[key] = ptr;
|
||||
}
|
||||
WebRtcTransportImp::Ptr getItem(const string &key) {
|
||||
if (key.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
lock_guard<mutex> lck(_mtx);
|
||||
auto it = _map.find(key);
|
||||
if (it == _map.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return it->second.lock();
|
||||
}
|
||||
void removeItem(string key) {
|
||||
lock_guard<mutex> lck(_mtx);
|
||||
_map.erase(key);
|
||||
}
|
||||
};
|
||||
|
||||
void WebRtcTransportImp::registerSelf() {
|
||||
_self = static_pointer_cast<WebRtcTransportImp>(shared_from_this());
|
||||
lock_guard<mutex> lck(s_rtc_mtx);
|
||||
s_rtc_map[getKey()] = static_pointer_cast<WebRtcTransportImp>(_self);
|
||||
WebRtcTransportManager::instance().addItem(getKey(), _self);
|
||||
}
|
||||
|
||||
void WebRtcTransportImp::unrefSelf() {
|
||||
@ -1025,23 +1052,18 @@ void WebRtcTransportImp::unrefSelf() {
|
||||
|
||||
void WebRtcTransportImp::unregisterSelf() {
|
||||
unrefSelf();
|
||||
lock_guard<mutex> lck(s_rtc_mtx);
|
||||
s_rtc_map.erase(getKey());
|
||||
WebRtcTransportManager::instance().removeItem(getKey());
|
||||
}
|
||||
|
||||
WebRtcTransportImp::Ptr WebRtcTransportImp::getRtcTransport(const string &key, bool unref_self) {
|
||||
if (key.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
lock_guard<mutex> lck(s_rtc_mtx);
|
||||
auto it = s_rtc_map.find(key);
|
||||
if (it == s_rtc_map.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
auto ret = it->second.lock();
|
||||
if (unref_self) {
|
||||
WebRtcTransportImp::Ptr WebRtcTransportImp::get(const string &key) {
|
||||
return WebRtcTransportManager::instance().getItem(key);
|
||||
}
|
||||
|
||||
WebRtcTransportImp::Ptr WebRtcTransportImp::move(const string &key) {
|
||||
auto ret = WebRtcTransportManager::instance().getItem(key);
|
||||
if (ret) {
|
||||
//此对象不再强引用自己,因为自己将被WebRtcSession对象持有
|
||||
ret->unrefSelf();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
@ -170,7 +170,8 @@ public:
|
||||
* @return 对象
|
||||
*/
|
||||
static Ptr create(const EventPoller::Ptr &poller);
|
||||
static Ptr getRtcTransport(const string &key, bool unref_self);
|
||||
static Ptr get(const string &key); // 借用
|
||||
static Ptr move(const string &key); // 所有权转移
|
||||
|
||||
void setSession(Session::Ptr session);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user