Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GB28181: Fix when camera restart, can not connect to SRS. #3944 #3947

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
delete session while sip disconnect and no new request bind session
yushimeng committed Feb 5, 2024
commit abff34e70de8c2385bab5af3c5a5244b623a8719
87 changes: 47 additions & 40 deletions trunk/src/app/srs_app_conn.cpp
Original file line number Diff line number Diff line change
@@ -239,6 +239,52 @@ void SrsResourceManager::remove(ISrsResource* c)
removing_ = false;
}

void SrsResourceManager::erase(ISrsResource* c)
{
for (map<string, ISrsResource*>::iterator it = conns_name_.begin(); it != conns_name_.end();) {
if (c != it->second) {
++it;
} else {
// Use C++98 style: https://stackoverflow.com/a/4636230
conns_name_.erase(it++);
}
}

for (map<string, ISrsResource*>::iterator it = conns_id_.begin(); it != conns_id_.end();) {
if (c != it->second) {
++it;
} else {
// Use C++98 style: https://stackoverflow.com/a/4636230
conns_id_.erase(it++);
}
}

for (map<uint64_t, ISrsResource*>::iterator it = conns_fast_id_.begin(); it != conns_fast_id_.end();) {
if (c != it->second) {
++it;
} else {
// Update the level-0 cache for fast-id.
uint64_t id = it->first;
SrsResourceFastIdItem* item = &conns_level0_cache_[(id | id>>32) % nn_level0_cache_];
item->nn_collisions--;
if (!item->nn_collisions) {
item->fast_id = 0;
item->available = false;
}

// Use C++98 style: https://stackoverflow.com/a/4636230
conns_fast_id_.erase(it++);
}
}

vector<ISrsResource*>::iterator it = std::find(conns_.begin(), conns_.end(), c);
if (it != conns_.end()) {
it = conns_.erase(it);
}
return;
}


void SrsResourceManager::do_remove(ISrsResource* c)
{
bool in_zombie = false;
@@ -354,46 +400,7 @@ void SrsResourceManager::do_clear()

void SrsResourceManager::dispose(ISrsResource* c)
{
for (map<string, ISrsResource*>::iterator it = conns_name_.begin(); it != conns_name_.end();) {
if (c != it->second) {
++it;
} else {
// Use C++98 style: https://stackoverflow.com/a/4636230
conns_name_.erase(it++);
}
}

for (map<string, ISrsResource*>::iterator it = conns_id_.begin(); it != conns_id_.end();) {
if (c != it->second) {
++it;
} else {
// Use C++98 style: https://stackoverflow.com/a/4636230
conns_id_.erase(it++);
}
}

for (map<uint64_t, ISrsResource*>::iterator it = conns_fast_id_.begin(); it != conns_fast_id_.end();) {
if (c != it->second) {
++it;
} else {
// Update the level-0 cache for fast-id.
uint64_t id = it->first;
SrsResourceFastIdItem* item = &conns_level0_cache_[(id | id>>32) % nn_level0_cache_];
item->nn_collisions--;
if (!item->nn_collisions) {
item->fast_id = 0;
item->available = false;
}

// Use C++98 style: https://stackoverflow.com/a/4636230
conns_fast_id_.erase(it++);
}
}

vector<ISrsResource*>::iterator it = std::find(conns_.begin(), conns_.end(), c);
if (it != conns_.end()) {
it = conns_.erase(it);
}
erase(c);

// We should copy all handlers, because it may change during callback.
vector<ISrsDisposingHandler*> handlers = handlers_;
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_conn.hpp
Original file line number Diff line number Diff line change
@@ -117,6 +117,7 @@ class SrsResourceManager : public ISrsCoroutineHandler, public ISrsResourceManag
// Interface ISrsResourceManager
public:
virtual void remove(ISrsResource* c);
virtual void erase(ISrsResource* c);
private:
void do_remove(ISrsResource* c);
void check_remove(ISrsResource* c, bool& in_zombie, bool& in_disposing);
31 changes: 28 additions & 3 deletions trunk/src/app/srs_app_gb28181.cpp
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ SrsLazyGbSession::SrsLazyGbSession(SrsLazyObjectWrapper<SrsLazyGbSession>* wrapp
sip_ = new SrsLazyObjectWrapper<SrsLazyGbSipTcpConn>();
media_ = new SrsLazyObjectWrapper<SrsLazyGbMediaTcpConn>();
muxer_ = new SrsGbMuxer(this);
state_ = SrsGbSessionStateInit;
reset();

connecting_starttime_ = 0;
connecting_timeout_ = 0;
@@ -400,6 +400,23 @@ std::string SrsLazyGbSession::desc()
return "GBS";
}

void SrsLazyGbSession::set_to_delete()
{
to_delete_ = true;
return;
}

bool SrsLazyGbSession::get_to_delete()
{
return to_delete_;
}

void SrsLazyGbSession::reset()
{
state_ = SrsGbSessionStateInit;
to_delete_ = false;
}

SrsGbListener::SrsGbListener()
{
conf_ = NULL;
@@ -521,6 +538,7 @@ SrsLazyGbSipTcpConn::~SrsLazyGbSipTcpConn()
srs_freep(register_);
srs_freep(invite_ok_);
srs_freep(conf_);
srs_cond_destroy(cond_);
}

void SrsLazyGbSipTcpConn::setup(SrsConfDirective* conf, SrsTcpListener* sip, SrsTcpListener* media, srs_netfd_t stfd)
@@ -613,7 +631,7 @@ void SrsLazyGbSipTcpConn::enqueue_sip_message(SrsSipMessage* msg)
}

void SrsLazyGbSipTcpConn::on_sip_disconnect() {
// state_ = SrsGbSipStateDisconnect;
session_->resource()->set_to_delete();
this->wake_up();
}

@@ -928,6 +946,8 @@ srs_error_t SrsLazyGbSipTcpConn::cycle()
// Interrupt the receiver and sender coroutine.
receiver_->interrupt();
sender_->interrupt();
// avoid bind session before resource destruction.
_srs_gb_manager->erase(session_);

// Note that we added wrapper to manager, so we must free the wrapper, not this connection.
SrsLazyObjectWrapper<SrsLazyGbSipTcpConn>* wrapper = wrapper_root_;
@@ -972,7 +992,8 @@ srs_error_t SrsLazyGbSipTcpConn::do_cycle()

// TODO: Handle other messages.
int ret = srs_cond_timedwait(cond_, 10 * SRS_UTIME_SECONDS);
if (ret == -1) {
// waitup by signal and no new connection bind session.
if (ret == -1 && session_->resource()->get_to_delete()) {
return srs_error_new(ret, "errno:%d", errno);
}
}
@@ -996,6 +1017,10 @@ srs_error_t SrsLazyGbSipTcpConn::bind_session(SrsSipMessage* msg, SrsLazyObjectW

// Find exists session for register, might be created by another object and still alive.
SrsLazyObjectWrapper<SrsLazyGbSession>* session = dynamic_cast<SrsLazyObjectWrapper<SrsLazyGbSession>*>(_srs_gb_manager->find_by_id(device));
// if session ready to delete but not insert in zombies, reuse it.
if (session && session->resource()->get_to_delete()) {
session->resource()->reset();
}
if (!session) {
// Create new GB session.
session = new SrsLazyObjectWrapper<SrsLazyGbSession>();
6 changes: 6 additions & 0 deletions trunk/src/app/srs_app_gb28181.hpp
Original file line number Diff line number Diff line change
@@ -117,6 +117,8 @@ class SrsLazyGbSession : public SrsLazyObject, public ISrsResource, public ISrsS
srs_utime_t reinvite_wait_;
// The number of timeout, dispose session if exceed.
uint32_t nn_timeout_;
// The flag to delete
bool to_delete_;
private:
SrsAlonePithyPrint* ppp_;
srs_utime_t startime_;
@@ -165,6 +167,10 @@ class SrsLazyGbSession : public SrsLazyObject, public ISrsResource, public ISrsS
public:
virtual const SrsContextId& get_id();
virtual std::string desc();
void set_to_delete();
bool get_to_delete();
// reset sip state, if device reconnected before insert zombies
void reset();
};

// The SIP and Media listener for GB.