Skip to content

Commit

Permalink
fix GB28181: When camera restart, can not connect to SRS. ossrs#3944
Browse files Browse the repository at this point in the history
  • Loading branch information
yushimeng committed Feb 2, 2024
1 parent 2a2da22 commit 0d5fc78
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
29 changes: 27 additions & 2 deletions trunk/src/app/srs_app_gb28181.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ SrsLazyGbSipTcpConn::SrsLazyGbSipTcpConn(SrsLazyObjectWrapper<SrsLazyGbSipTcpCon
conn_ = NULL;
receiver_ = NULL;
sender_ = NULL;

cond_ = srs_cond_new();
trd_ = new SrsSTCoroutine("sip", this);
}

Expand Down Expand Up @@ -612,6 +612,11 @@ void SrsLazyGbSipTcpConn::enqueue_sip_message(SrsSipMessage* msg)
sender_->enqueue(msg);
}

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

void SrsLazyGbSipTcpConn::drive_state(SrsSipMessage* msg)
{
srs_error_t err = srs_success;
Expand All @@ -622,6 +627,10 @@ void SrsLazyGbSipTcpConn::drive_state(SrsSipMessage* msg)
srs_sip_state(ostate, state_).c_str()); \
}

if (state_ == SrsGbSipStateDisconnect) {
return;
}

//const char* mt = msg->type_ == HTTP_REQUEST ? "REQUEST" : "RESPONSE";
//const char* mm = msg->type_ == HTTP_REQUEST ? http_method_str(msg->method_) : "Response";
//int ms = msg->type_ == HTTP_REQUEST ? 200 : msg->status_;
Expand Down Expand Up @@ -866,13 +875,23 @@ bool SrsLazyGbSipTcpConn::is_bye()
return state_ == SrsGbSipStateBye;
}

bool SrsLazyGbSipTcpConn::is_disconnect()
{
return state_ == SrsGbSipStateDisconnect;
}

SrsGbSipState SrsLazyGbSipTcpConn::set_state(SrsGbSipState v)
{
SrsGbSipState state = state_;
state_ = v;
return state;
}

void SrsLazyGbSipTcpConn::wake_up()
{
srs_cond_signal(cond_);
}

const SrsContextId& SrsLazyGbSipTcpConn::get_id()
{
return trd_->cid();
Expand Down Expand Up @@ -952,7 +971,10 @@ srs_error_t SrsLazyGbSipTcpConn::do_cycle()
}

// TODO: Handle other messages.
srs_usleep(SRS_UTIME_NO_TIMEOUT);
int ret = srs_cond_timedwait(cond_, 10 * SRS_UTIME_SECONDS);
if (ret == -1) {
return srs_error_new(ret, "errno:%d", errno);
}
}

return err;
Expand Down Expand Up @@ -1048,6 +1070,9 @@ srs_error_t SrsLazyGbSipTcpReceiver::cycle()
// TODO: FIXME: Notify SIP transport to cleanup.
if (err != srs_success) {
srs_error("SIP: Receive err %s", srs_error_desc(err).c_str());
if (sip_) {
sip_->on_sip_disconnect();
}
}

return err;
Expand Down
6 changes: 6 additions & 0 deletions trunk/src/app/srs_app_gb28181.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ enum SrsGbSipState
SrsGbSipStateReinviting,
SrsGbSipStateStable,
SrsGbSipStateBye,
SrsGbSipStateDisconnect,
};
std::string srs_gb_sip_state(SrsGbSipState state);

Expand Down Expand Up @@ -206,6 +207,7 @@ class SrsLazyGbSipTcpConn : public SrsLazyObject, public ISrsResource, public IS
SrsLazyGbSipTcpReceiver* receiver_;
SrsLazyGbSipTcpSender* sender_;
SrsCoroutine* trd_;
srs_cond_t cond_;
private:
friend class SrsLazyObjectWrapper<SrsLazyGbSipTcpConn>;
SrsLazyGbSipTcpConn(SrsLazyObjectWrapper<SrsLazyGbSipTcpConn>* wrapper_root);
Expand All @@ -224,6 +226,8 @@ class SrsLazyGbSipTcpConn : public SrsLazyObject, public ISrsResource, public IS
public:
// When got a SIP message.
srs_error_t on_sip_message(SrsSipMessage* msg);
// When SIP connection lost.
void on_sip_disconnect();
// Enqueue a SIP message to send, which might be a request or response.
void enqueue_sip_message(SrsSipMessage* msg);
private:
Expand All @@ -247,8 +251,10 @@ class SrsLazyGbSipTcpConn : public SrsLazyObject, public ISrsResource, public IS
bool is_stable();
// Whether SIP is bye bye.
bool is_bye();
bool is_disconnect();
private:
SrsGbSipState set_state(SrsGbSipState v);
void wake_up();
// Interface ISrsResource
public:
virtual const SrsContextId& get_id();
Expand Down

0 comments on commit 0d5fc78

Please sign in to comment.