diff --git a/src/ai_platform/MultiSourceProcess.cpp b/src/ai_platform/MultiSourceProcess.cpp index b354cce..e3c2520 100755 --- a/src/ai_platform/MultiSourceProcess.cpp +++ b/src/ai_platform/MultiSourceProcess.cpp @@ -228,19 +228,8 @@ int CMultiSourceProcess::InitAlgorthim(tsl_aiplatform_param vptParam){ pVpcUtil->init(m_devId); // 初始化SIP服务器 - ServerInfo info( - "SY_Sip_Server", - "12345678", - "192.168.60.179", - 15060, - "34020000002000000002", - "3402000000", - "sy123456", - 1800, - 3600); - SipServer* pSipServer = SipServer::getInstance(); - pSipServer->Init(info); + pSipServer->Init(nullptr); m_pAlgorthimThread = new thread([](void* arg) { CMultiSourceProcess* process = (CMultiSourceProcess*)arg ; diff --git a/src/decoder/gb28181/sip/Message/CatalogParser.cpp b/src/decoder/gb28181/sip/Message/CatalogParser.cpp index d3129f8..e401c4f 100644 --- a/src/decoder/gb28181/sip/Message/CatalogParser.cpp +++ b/src/decoder/gb28181/sip/Message/CatalogParser.cpp @@ -557,7 +557,7 @@ std::vector< DeviceInfo > CCatalogParser::Decode( const std::vector< tinyxml2::X return m_devices; } -std::vector< DeviceInfo > CCatalogParser::Decode_Catlog(const char* body) +std::vector< DeviceInfo > CCatalogParser::DecodeCatlog(const char* body) { std::vector< DeviceInfo > cat_list; if (body == NULL) @@ -604,14 +604,12 @@ std::vector< DeviceInfo > CCatalogParser::Decode_Catlog(const char* body) } std::string sn = pSN->Value(); - if (sn.empty()) - { + if (sn.empty()) { return cat_list; } std::string deviceid = pDeviceID->Value(); - if (deviceid.empty()) - { + if (deviceid.empty()) { return cat_list; } @@ -622,9 +620,95 @@ std::vector< DeviceInfo > CCatalogParser::Decode_Catlog(const char* body) cat_list = Decode(nodes); if (cat_list.empty()) { - LOG_ERROR("消息体解析错误!"); + LOG_WARN("消息体未解析出设备!"); + } else { + // 设置 + for (size_t i = 0; i < cat_list.size(); i++) { + cat_list[i].parentid = deviceid; + } } } return cat_list; +} + +string CCatalogParser::getItemValue(tinyxml2::XMLNode *pItem) { + + tinyxml2::XMLNode *pValue = pItem->FirstChild(); + if( pValue == NULL ) { + LOG_DEBUG( "参数值没有设置" ); + return ""; + } + + const char *value = pValue->Value(); + if( value == NULL ) { + LOG_DEBUG( "参数值为空" ); + return ""; + } + + return value; +} + +ServerInfo CCatalogParser::DecodeServerConfig(const char* body) +{ + ServerInfo info; + if (body == NULL) { + return info; + } + + tinyxml2::XMLDocument doc; + doc.Parse(body); + tinyxml2::XMLElement* pRoot = doc.RootElement(); + if (pRoot == NULL) { + return info; + } + + for (tinyxml2::XMLNode* pNode = pRoot->FirstChild(); pNode != 0; pNode = pNode->NextSibling()) + { + std::string type = pNode->Value(); + if (CGBMessage::CompareNoCase( type, "Ua" )) + { + info.setUa(getItemValue(pNode)); + } + else if (CGBMessage::CompareNoCase( type, "Nonce" )) + { + info.setNonce(getItemValue(pNode)); + } + else if (CGBMessage::CompareNoCase( type, "Ip" )) + { + info.setIp(getItemValue(pNode)); + } + else if (CGBMessage::CompareNoCase( type, "Port" )) + { + string str = getItemValue(pNode); + int i = atoi(str.c_str()); + info.setPort(i); + } + else if (CGBMessage::CompareNoCase( type, "SipId" )) + { + info.setSipId(getItemValue(pNode)); + } + else if (CGBMessage::CompareNoCase( type, "SipRealm" )) + { + info.setSipRealm(getItemValue(pNode)); + } + else if (CGBMessage::CompareNoCase( type, "Password" )) + { + info.setSipPass(getItemValue(pNode)); + } + else if (CGBMessage::CompareNoCase( type, "Timeout" )) + { + string str = getItemValue(pNode); + int i = atoi(str.c_str()); + info.setTimeout(i); + } + else if (CGBMessage::CompareNoCase( type, "Expiry" )) + { + string str = getItemValue(pNode); + int i = atoi(str.c_str()); + info.setExpiry(i); + } + } + + return info; } \ No newline at end of file diff --git a/src/decoder/gb28181/sip/Message/CatalogParser.h b/src/decoder/gb28181/sip/Message/CatalogParser.h index 96050da..d419c01 100644 --- a/src/decoder/gb28181/sip/Message/CatalogParser.h +++ b/src/decoder/gb28181/sip/Message/CatalogParser.h @@ -4,56 +4,8 @@ #include "GBMessage.h" #include -enum EEventType -{ - EVENT_ON = 0, // - EVENT_OFF, // - EVENT_VLOST, // Ƶʧ - EVENT_DEFECT, // - EVENT_ADD, // - EVENT_DEL, // ɾ - EVENT_UPDATE, // - - EVENT_UNKNOW, -}; - -struct DeviceInfo -{ - EEventType event; - std::string id; - std::string name; - std::string manufacturer; - std::string model; - std::string owner; - std::string civil; - std::string block; - std::string address; - std::string safetyway; - std::string registerway; - std::string certnum; - std::string certifiable; - std::string errcode; - std::string secrecy; - std::string parental; - std::string parentid; - std::string endtime; - std::string ip; - std::string port; - std::string password; - std::string status; - std::string longitude; - std::string latitude; - std::string ptz; - std::string position; - std::string room; - std::string use; - std::string supplylight; - std::string direction; - std::string resolution; - std::string businessgroup; -}; +#include "../sip_header.h" -////////////////////////////////////////////////////////////////////////// class CCatalogParser : public CGBMessage { public: @@ -64,7 +16,9 @@ public: public: bool Encode( std::string &message ); - std::vector< DeviceInfo > Decode_Catlog(const char* body); + std::vector DecodeCatlog(const char* body); + + ServerInfo DecodeServerConfig(const char* body); static std::string GetStrName(EEventType eType) { @@ -226,6 +180,7 @@ public: private: std::vector< DeviceInfo > Decode(const std::vector< tinyxml2::XMLNode* >& nodes); + string getItemValue(tinyxml2::XMLNode *pItem); private: std::string m_sn; diff --git a/src/decoder/gb28181/sip/SipServer.cpp b/src/decoder/gb28181/sip/SipServer.cpp index 007e8d3..1a1d995 100644 --- a/src/decoder/gb28181/sip/SipServer.cpp +++ b/src/decoder/gb28181/sip/SipServer.cpp @@ -18,6 +18,7 @@ #include #include +#include using namespace std; @@ -92,8 +93,29 @@ SipServer::~SipServer() { #endif // WIN32 } -bool SipServer::Init(ServerInfo info) { - mInfo = info; +bool SipServer::Init(ServerInfo* pInfo) { + + if (pInfo == nullptr) { + std::ifstream cfgFile("./sip_server_cfg.xml"); + if(cfgFile.is_open()) { + string strConfig; + string str; + int char_count = 0; + while(cfgFile >> str) + { + strConfig += str; + } + CCatalogParser catPaser; + mInfo = catPaser.DecodeServerConfig(strConfig.c_str()); + } else { + LOG_ERROR("read sip server config file failed!"); + return false; + } + cfgFile.close(); + } else { + mInfo = *pInfo; + } + LOG_INFO("{}:{}", mInfo.getIp(), mInfo.getPort()); m_event_loop_thread = new std::thread(event_loop_thread, this); @@ -121,7 +143,7 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) { this->dump_request(evtp); this->dump_response(evtp); - this->clearClientMap(); + // this->clearClientMap(); break; case EXOSIP_MESSAGE_NEW://23 // LOG_INFO("EXOSIP_MESSAGE_NEW type={}",evtp->type); @@ -204,6 +226,7 @@ void SipServer::event_loop() { if(this->init_sip_server() !=0 ){ return; } + while(!mQuit) { eXosip_event_t *evtp = eXosip_event_wait(mSipCtx, 0, 20); if (!evtp){ @@ -383,7 +406,7 @@ void SipServer::response_message(eXosip_event_t *evtp) { this->response_message_answer(evtp,200); // 需要根据对方的Catelog请求,做一些相应的应答请求 CCatalogParser catPaser; - std::vector vec_device = catPaser.Decode_Catlog(body->body); + std::vector vec_device = catPaser.DecodeCatlog(body->body); printDevice(vec_device); std::lock_guard l(m_device_map_mtx); diff --git a/src/decoder/gb28181/sip/SipServer.h b/src/decoder/gb28181/sip/SipServer.h index 1332fb5..d1f2cc4 100644 --- a/src/decoder/gb28181/sip/SipServer.h +++ b/src/decoder/gb28181/sip/SipServer.h @@ -1,9 +1,5 @@ -// -// Created bxc on 2022/11/25. -// - -#ifndef BXC_SIPSERVER_SIPSERVER_H -#define BXC_SIPSERVER_SIPSERVER_H +#ifndef __SIPSERVER_H__ +#define __SIPSERVER_H__ extern "C" { #include @@ -17,62 +13,10 @@ extern "C" { #include #include "./Message/CatalogParser.h" +#include "sip_header.h" using namespace std; -class ServerInfo { -public: - ServerInfo() {} - - ServerInfo(string ua,string nonce, string ip, int port, - string sipId, string sipRealm, string sipPass, int sipTimeout, int sipExpiry): - mUa(ua), - mNonce(nonce),mIp(ip),mPort(port),mSipId(sipId), - mSipRealm(sipRealm),mSipPass(sipPass),mSipTimeout(sipTimeout), - mSipExpiry(sipExpiry){} - - ~ServerInfo() = default; -public: - string getUa() const{ - return mUa; - } - string getNonce() const{ - return mNonce; - } - string getIp() const{ - return mIp; - } - int getPort() const { - return mPort; - } - string getSipId() const{ - return mSipId; - } - string getSipRealm() const{ - return mSipRealm; - } - string getSipPass() const{ - return mSipPass; - } - int getTimeout() const { - return mSipTimeout; - } - int getExpiry() const { - return mSipExpiry; - } - -private: - string mUa; - string mNonce;//SIP服务随机数值 - string mIp;//SIP服务IP - int mPort;//SIP服务端口 - string mSipId; //SIP服务器ID - string mSipRealm;//SIP服务器域 - string mSipPass;//SIP password - int mSipTimeout; //SIP timeout - int mSipExpiry;// SIP到期 -}; - class Client { public: Client(string ip, int port, string device) : @@ -135,7 +79,7 @@ public: SipServer(); ~SipServer(); - bool Init(ServerInfo info); + bool Init(ServerInfo* pInfo); int RequestInvite_UDP(const char* dst_channel, int rtpPort); @@ -184,4 +128,4 @@ private: }; -#endif //BXC_SIPSERVER_SIPSERVER_H +#endif //__SIPSERVER_H__ diff --git a/src/decoder/gb28181/sip/sip_header.h b/src/decoder/gb28181/sip/sip_header.h new file mode 100644 index 0000000..8cc80a8 --- /dev/null +++ b/src/decoder/gb28181/sip/sip_header.h @@ -0,0 +1,139 @@ +#ifndef __SIP_HEADER_H__ +#define __SIP_HEADER_H__ + +#include + +using namespace std; + +enum EEventType +{ + EVENT_ON = 0, // ���� + EVENT_OFF, // ���� + EVENT_VLOST, // ��Ƶ��ʧ + EVENT_DEFECT, // ���� + EVENT_ADD, // ���� + EVENT_DEL, // ɾ�� + EVENT_UPDATE, // ���� + + EVENT_UNKNOW, +}; + +struct DeviceInfo +{ + EEventType event; + std::string id; + std::string name; + std::string manufacturer; + std::string model; + std::string owner; + std::string civil; + std::string block; + std::string address; + std::string safetyway; + std::string registerway; + std::string certnum; + std::string certifiable; + std::string errcode; + std::string secrecy; + std::string parental; + std::string parentid; + std::string endtime; + std::string ip; + std::string port; + std::string password; + std::string status; + std::string longitude; + std::string latitude; + std::string ptz; + std::string position; + std::string room; + std::string use; + std::string supplylight; + std::string direction; + std::string resolution; + std::string businessgroup; +}; + +class ServerInfo { +public: + ServerInfo() {} + + ServerInfo(string ua,string nonce, string ip, int port, + string sipId, string sipRealm, string sipPass, int sipTimeout, int sipExpiry): + mUa(ua), + mNonce(nonce),mIp(ip),mPort(port),mSipId(sipId), + mSipRealm(sipRealm),mSipPass(sipPass),mSipTimeout(sipTimeout), + mSipExpiry(sipExpiry){} + + ~ServerInfo() = default; +public: + string getUa() const{ + return mUa; + } + void setUa(string ua) { + mUa = ua; + } + string getNonce() const{ + return mNonce; + } + void setNonce(string nonce) { + mNonce = nonce; + } + string getIp() const{ + return mIp; + } + void setIp(string s) { + mIp = s; + } + int getPort() const { + return mPort; + } + void setPort(int i) { + mPort = i; + } + + string getSipId() const{ + return mSipId; + } + void setSipId(string s) { + mSipId = s; + } + string getSipRealm() const{ + return mSipRealm; + } + void setSipRealm(string s) { + mSipRealm = s; + } + string getSipPass() const{ + return mSipPass; + } + void setSipPass(string s) { + mSipPass = s; + } + int getTimeout() const { + return mSipTimeout; + } + void setTimeout(int i) { + mSipTimeout = i; + } + int getExpiry() const { + return mSipExpiry; + } + void setExpiry(int i) { + mSipExpiry = i; + } + +private: + string mUa; + string mNonce;//SIP服务随机数值 + string mIp;//SIP服务IP + int mPort{0};//SIP服务端口 + string mSipId; //SIP服务器ID + string mSipRealm;//SIP服务器域 + string mSipPass;//SIP password + int mSipTimeout; //SIP timeout + int mSipExpiry;// SIP到期 +}; + + +#endif //__SIP_HEADER_H__ \ No newline at end of file