mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 19:00:01 +08:00
进一步改善 WebRtcTransportImp 的生命周期逻辑。
This commit is contained in:
parent
722097be05
commit
25a1434e00
@ -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<uint64_t>(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();
|
||||
|
@ -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<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());
|
||||
@ -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<mutex> lck(_mtx);
|
||||
_map[key] = ptr;
|
||||
}
|
||||
WebRtcTransportImp::Ptr WebRtcTransportManager::getItem(const string &key) {
|
||||
if (key.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
return ret;
|
||||
lock_guard<mutex> lck(_mtx);
|
||||
auto it = _map.find(key);
|
||||
if (it == _map.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return it->second.lock();
|
||||
}
|
||||
void WebRtcTransportManager::removeItem(string key) {
|
||||
lock_guard<mutex> lck(_mtx);
|
||||
_map.erase(key);
|
||||
}
|
||||
|
@ -166,14 +166,6 @@ public:
|
||||
using Ptr = std::shared_ptr<WebRtcTransportImp>;
|
||||
~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;
|
||||
@ -232,3 +224,15 @@ private:
|
||||
//根据接收rtp的pt获取相关信息
|
||||
unordered_map<uint8_t/*pt*/, std::pair<bool/*is rtx*/,MediaTrack::Ptr> > _pt_to_track;
|
||||
};
|
||||
|
||||
class WebRtcTransportManager {
|
||||
mutable mutex _mtx;
|
||||
unordered_map<string, weak_ptr<WebRtcTransportImp> > _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);
|
||||
};
|
Loading…
Reference in New Issue
Block a user