mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-25 20:27:34 +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()) {
|
if (user_name.empty()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
auto ret = WebRtcTransportImp::get(user_name);
|
auto ret = WebRtcTransportManager::instance().getItem(user_name);
|
||||||
return ret ? ret->getPoller() : nullptr;
|
return ret ? ret->getPoller() : nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,9 +62,10 @@ void WebRtcSession::onRecv_l(const Buffer::Ptr &buffer) {
|
|||||||
_find_transport = false;
|
_find_transport = false;
|
||||||
auto user_name = getUserName(buffer);
|
auto user_name = getUserName(buffer);
|
||||||
_identifier = user_name + '-' + to_string(reinterpret_cast<uint64_t>(this));
|
_identifier = user_name + '-' + to_string(reinterpret_cast<uint64_t>(this));
|
||||||
_transport = WebRtcTransportImp::move(user_name);
|
auto transport = WebRtcTransportManager::instance().getItem(user_name);
|
||||||
CHECK(_transport && _transport->getPoller()->isCurrentThread());
|
CHECK(transport && transport->getPoller()->isCurrentThread());
|
||||||
_transport->setSession(shared_from_this());
|
transport->setSession(shared_from_this());
|
||||||
|
_transport = std::move(transport);
|
||||||
InfoP(this);
|
InfoP(this);
|
||||||
}
|
}
|
||||||
_ticker.resetTime();
|
_ticker.resetTime();
|
||||||
|
@ -840,6 +840,7 @@ void WebRtcTransportImp::onShutdown(const SockException &ex){
|
|||||||
|
|
||||||
void WebRtcTransportImp::setSession(Session::Ptr session) {
|
void WebRtcTransportImp::setSession(Session::Ptr session) {
|
||||||
_session = std::move(session);
|
_session = std::move(session);
|
||||||
|
unrefSelf();
|
||||||
}
|
}
|
||||||
|
|
||||||
const Session::Ptr &WebRtcTransportImp::getSession() const {
|
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() {
|
void WebRtcTransportImp::registerSelf() {
|
||||||
_self = static_pointer_cast<WebRtcTransportImp>(shared_from_this());
|
_self = static_pointer_cast<WebRtcTransportImp>(shared_from_this());
|
||||||
@ -901,15 +872,26 @@ void WebRtcTransportImp::unregisterSelf() {
|
|||||||
WebRtcTransportManager::instance().removeItem(getIdentifier());
|
WebRtcTransportManager::instance().removeItem(getIdentifier());
|
||||||
}
|
}
|
||||||
|
|
||||||
WebRtcTransportImp::Ptr WebRtcTransportImp::get(const string &key) {
|
WebRtcTransportManager &WebRtcTransportManager::instance() {
|
||||||
return WebRtcTransportManager::instance().getItem(key);
|
static WebRtcTransportManager s_instance;
|
||||||
|
return s_instance;
|
||||||
}
|
}
|
||||||
|
void WebRtcTransportManager::addItem(string key, const WebRtcTransportImp::Ptr &ptr) {
|
||||||
WebRtcTransportImp::Ptr WebRtcTransportImp::move(const string &key) {
|
lock_guard<mutex> lck(_mtx);
|
||||||
auto ret = WebRtcTransportManager::instance().getItem(key);
|
_map[key] = ptr;
|
||||||
if (ret) {
|
}
|
||||||
//此对象不再强引用自己,因为自己将被WebRtcSession对象持有
|
WebRtcTransportImp::Ptr WebRtcTransportManager::getItem(const string &key) {
|
||||||
ret->unrefSelf();
|
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>;
|
using Ptr = std::shared_ptr<WebRtcTransportImp>;
|
||||||
~WebRtcTransportImp() override;
|
~WebRtcTransportImp() override;
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建WebRTC对象
|
|
||||||
* @param poller 改对象需要绑定的线程
|
|
||||||
* @return 对象
|
|
||||||
*/
|
|
||||||
static Ptr get(const string &key); // 借用
|
|
||||||
static Ptr move(const string &key); // 所有权转移
|
|
||||||
|
|
||||||
void setSession(Session::Ptr session);
|
void setSession(Session::Ptr session);
|
||||||
const Session::Ptr& getSession() const;
|
const Session::Ptr& getSession() const;
|
||||||
uint64_t getBytesUsage() const;
|
uint64_t getBytesUsage() const;
|
||||||
@ -231,4 +223,16 @@ private:
|
|||||||
unordered_map<uint32_t/*ssrc*/, MediaTrack::Ptr> _ssrc_to_track;
|
unordered_map<uint32_t/*ssrc*/, MediaTrack::Ptr> _ssrc_to_track;
|
||||||
//根据接收rtp的pt获取相关信息
|
//根据接收rtp的pt获取相关信息
|
||||||
unordered_map<uint8_t/*pt*/, std::pair<bool/*is rtx*/,MediaTrack::Ptr> > _pt_to_track;
|
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