Commit e918c2a70c7197e1b995c1771d68752aad5ea5c7

Authored by Hu Chunming
1 parent 346fe72a

call release 自动重新invite

sip/SipServer.cpp
@@ -22,6 +22,8 @@ @@ -22,6 +22,8 @@
22 #include <sstream> 22 #include <sstream>
23 #include <algorithm> 23 #include <algorithm>
24 24
  25 +#include "WebSocketServer.h"
  26 +
25 27
26 using namespace std; 28 using namespace std;
27 29
@@ -88,14 +90,16 @@ SipServer::~SipServer() { @@ -88,14 +90,16 @@ SipServer::~SipServer() {
88 #endif // WIN32 90 #endif // WIN32
89 } 91 }
90 92
91 -bool SipServer::Init(ServerInfo* pInfo) { 93 +bool SipServer::Init(ServerInfo* pInfo, WebSocketServer* pServer) {
92 94
93 - if (pInfo == nullptr) { 95 + if (pInfo == nullptr || pServer == nullptr) {
94 return false; 96 return false;
95 } 97 }
96 98
97 mInfo = *pInfo; 99 mInfo = *pInfo;
98 100
  101 + m_pWsServer = pServer;
  102 +
99 m_event_loop_thread = new std::thread(event_loop_thread, this); 103 m_event_loop_thread = new std::thread(event_loop_thread, this);
100 104
101 return true; 105 return true;
@@ -112,14 +116,15 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) { @@ -112,14 +116,15 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) {
112 116
113 case EXOSIP_CALL_CLOSED://21 117 case EXOSIP_CALL_CLOSED://21
114 LOG_INFO("EXOSIP_CALL_CLOSED type={}",evtp->type); 118 LOG_INFO("EXOSIP_CALL_CLOSED type={}",evtp->type);
115 - this->dump_request(evtp);  
116 - this->dump_response(evtp); 119 + // this->dump_request(evtp);
  120 + // this->dump_response(evtp);
117 break; 121 break;
118 122
119 case EXOSIP_CALL_RELEASED://22 123 case EXOSIP_CALL_RELEASED://22
120 LOG_INFO("EXOSIP_CALL_RELEASED type={}", evtp->type); 124 LOG_INFO("EXOSIP_CALL_RELEASED type={}", evtp->type);
121 - this->dump_request(evtp);  
122 - this->dump_response(evtp); 125 + reInvite(evtp->cid);
  126 + // this->dump_request(evtp);
  127 + // this->dump_response(evtp);
123 128
124 // this->clearClientMap(); 129 // this->clearClientMap();
125 break; 130 break;
@@ -164,10 +169,13 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) { @@ -164,10 +169,13 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) {
164 cache_invite_callinfo(evtp); 169 cache_invite_callinfo(evtp);
165 break; 170 break;
166 case EXOSIP_CALL_SERVERFAILURE: 171 case EXOSIP_CALL_SERVERFAILURE:
167 - LOG_INFO("EXOSIP_CALL_SERVERFAILURE type={}", evtp->type); 172 + LOG_INFO("EXOSIP_CALL_SERVERFAILURE cid={}", evtp->cid);
168 break; 173 break;
169 case EXOSIP_IN_SUBSCRIPTION_NEW: 174 case EXOSIP_IN_SUBSCRIPTION_NEW:
170 - LOG_INFO("EXOSIP_IN_SUBSCRIPTION_NEW type={}", evtp->type); 175 + LOG_INFO("EXOSIP_IN_SUBSCRIPTION_NEW cid={}", evtp->cid);
  176 + break;
  177 + case EXOSIP_CALL_MESSAGE_ANSWERED:
  178 + LOG_INFO("EXOSIP_CALL_MESSAGE_ANSWERED cid={}", evtp->cid);
171 break; 179 break;
172 default: 180 default:
173 LOG_INFO("type={} unknown", evtp->type); 181 LOG_INFO("type={} unknown", evtp->type);
@@ -212,11 +220,13 @@ void SipServer::cache_invite_callinfo(eXosip_event_t *evtp) { @@ -212,11 +220,13 @@ void SipServer::cache_invite_callinfo(eXosip_event_t *evtp) {
212 string ip = contract_ip_from_message(s); 220 string ip = contract_ip_from_message(s);
213 string port = contract_port_from_message(s); 221 string port = contract_port_from_message(s);
214 222
215 - std::cout << "ip:" << ip << " port:" << port << std::endl; 223 + LOG_INFO("ip:{} port:{}", ip, port);
216 224
217 string channel_id = evtp->response->to->url->username; 225 string channel_id = evtp->response->to->url->username;
218 226
219 auto key = std::make_tuple(channel_id, ip, atoi(port.c_str())); 227 auto key = std::make_tuple(channel_id, ip, atoi(port.c_str()));
  228 +
  229 + std::lock_guard<std::mutex> l(m_invite_callinfo_map_mtx);
220 m_invite_callinfo_map[key] = call_info; 230 m_invite_callinfo_map[key] = call_info;
221 } 231 }
222 catch(const std::exception& e) 232 catch(const std::exception& e)
@@ -548,6 +558,8 @@ int SipServer::request_bye(eXosip_event_t* evtp) { @@ -548,6 +558,8 @@ int SipServer::request_bye(eXosip_event_t* evtp) {
548 558
549 int SipServer::ByeInvite(std::string channel_id, string ip, int rtpPort) { 559 int SipServer::ByeInvite(std::string channel_id, string ip, int rtpPort) {
550 560
  561 + std::lock_guard<std::mutex> l(m_invite_callinfo_map_mtx);
  562 +
551 auto key = std::make_tuple(channel_id, ip, rtpPort); 563 auto key = std::make_tuple(channel_id, ip, rtpPort);
552 564
553 auto it = m_invite_callinfo_map.find(key); 565 auto it = m_invite_callinfo_map.find(key);
@@ -558,14 +570,64 @@ int SipServer::ByeInvite(std::string channel_id, string ip, int rtpPort) { @@ -558,14 +570,64 @@ int SipServer::ByeInvite(std::string channel_id, string ip, int rtpPort) {
558 CallInfo info = it->second; 570 CallInfo info = it->second;
559 571
560 eXosip_lock(mSipCtx); 572 eXosip_lock(mSipCtx);
561 - int ret = eXosip_call_terminate(mSipCtx, info.cid, info.did); 573 + eXosip_call_terminate(mSipCtx, info.cid, info.did);
562 eXosip_unlock(mSipCtx); 574 eXosip_unlock(mSipCtx);
563 575
564 m_invite_callinfo_map.erase(it); 576 m_invite_callinfo_map.erase(it);
565 577
  578 + std::lock_guard<std::mutex> l_cmd(m_invite_cmd_map_mtx);
  579 + m_invite_cmd_map.erase(it->first);
  580 +
566 return 0; 581 return 0;
567 } 582 }
568 583
  584 +int SipServer::reInvite(int cid) {
  585 +
  586 + std::lock_guard<std::mutex> l(m_invite_callinfo_map_mtx);
  587 +
  588 + bool bFound = false;
  589 + auto it = m_invite_callinfo_map.begin();
  590 + for (; it != m_invite_callinfo_map.end(); it++) {
  591 + CallInfo info = it->second;
  592 + if (info.cid == cid) {
  593 + bFound = true;
  594 + break;
  595 + }
  596 + }
  597 +
  598 + if (bFound)
  599 + {
  600 + auto key = it->first;
  601 +
  602 + std::lock_guard<std::mutex> l_cmd(m_invite_cmd_map_mtx);
  603 + auto it_cmd = m_invite_cmd_map.find(key);
  604 + if(it_cmd != m_invite_cmd_map.end()) {
  605 + string strChannelId = std::get<0>(key);
  606 + string strIp = std::get<1>(key);
  607 + int iPort = std::get<2>(key);
  608 +
  609 + int ret = -1;
  610 + string cmd = it_cmd->second;
  611 + if (cmd == "udp") {
  612 + ret = RequestInvite_UDP(strChannelId.c_str(), strIp.c_str(), iPort);
  613 + } else if (cmd == "tcp_a") {
  614 + ret = RequestInvite_TCP_a(strChannelId.c_str(), strIp.c_str(), iPort);
  615 + }
  616 +
  617 + if (ret <= 0) {
  618 + LOG_ERROR("reInvite failed!");
  619 + m_pWsServer->response_client(strChannelId, iPort, cmd, ret);
  620 + }
  621 + }
  622 + }
  623 +
  624 + return -1;
  625 +}
  626 +
  627 +int SipServer::inviteFailedResponse(int cid) {
  628 +
  629 +}
  630 +
569 int SipServer::RequestInvite_UDP(const char* dst_channel, const char* rtpIp, int rtpPort) { 631 int SipServer::RequestInvite_UDP(const char* dst_channel, const char* rtpIp, int rtpPort) {
570 632
571 // 检查设备是否在线 633 // 检查设备是否在线
@@ -621,14 +683,17 @@ int SipServer::RequestInvite_UDP(const char* dst_channel, const char* rtpIp, int @@ -621,14 +683,17 @@ int SipServer::RequestInvite_UDP(const char* dst_channel, const char* rtpIp, int
621 int call_id = eXosip_call_send_initial_invite(mSipCtx, msg); 683 int call_id = eXosip_call_send_initial_invite(mSipCtx, msg);
622 if (call_id > 0) { 684 if (call_id > 0) {
623 LOG_INFO("eXosip_call_send_initial_invite success: call_id={}", call_id); 685 LOG_INFO("eXosip_call_send_initial_invite success: call_id={}", call_id);
  686 + auto key = std::make_tuple(string(dst_channel), string(rtpIp), rtpPort);
  687 + m_invite_cmd_map[key] = "udp";
624 } 688 }
625 else { 689 else {
626 LOG_ERROR("eXosip_call_send_initial_invite error: call_id={}", call_id); 690 LOG_ERROR("eXosip_call_send_initial_invite error: call_id={}", call_id);
627 } 691 }
  692 +
628 return call_id; 693 return call_id;
629 } 694 }
630 695
631 -int SipServer::RequestInvite_TCP_a(const char* dst_channel, int rtpPort) { 696 +int SipServer::RequestInvite_TCP_a(const char* dst_channel, const char* rtpIp, int rtpPort) {
632 // 检查设备是否在线 697 // 检查设备是否在线
633 if (!check_device_status(dst_channel)) { 698 if (!check_device_status(dst_channel)) {
634 return -2; 699 return -2;
@@ -682,6 +747,8 @@ int SipServer::RequestInvite_TCP_a(const char* dst_channel, int rtpPort) { @@ -682,6 +747,8 @@ int SipServer::RequestInvite_TCP_a(const char* dst_channel, int rtpPort) {
682 int call_id = eXosip_call_send_initial_invite(mSipCtx, msg); 747 int call_id = eXosip_call_send_initial_invite(mSipCtx, msg);
683 if (call_id > 0) { 748 if (call_id > 0) {
684 LOG_INFO("eXosip_call_send_initial_invite success: call_id={}", call_id); 749 LOG_INFO("eXosip_call_send_initial_invite success: call_id={}", call_id);
  750 + auto key = std::make_tuple(string(dst_channel), string(rtpIp), rtpPort);
  751 + m_invite_cmd_map[key] = "tcp_a";
685 } 752 }
686 else { 753 else {
687 LOG_ERROR("eXosip_call_send_initial_invite error: call_id={}", call_id); 754 LOG_ERROR("eXosip_call_send_initial_invite error: call_id={}", call_id);
@@ -694,11 +761,11 @@ void SipServer::cacheCatalog() { @@ -694,11 +761,11 @@ void SipServer::cacheCatalog() {
694 std::lock_guard<std::mutex> l(m_client_map_mtx); 761 std::lock_guard<std::mutex> l(m_client_map_mtx);
695 762
696 if (mClientMap.size() <= 0){ 763 if (mClientMap.size() <= 0){
697 - cout << "no IPC" << endl; 764 + LOG_WARN("NO IPC");
698 return ; 765 return ;
699 } 766 }
700 767
701 - cout << "client size:" << mClientMap.size() << endl; 768 + LOG_INFO("client size:{}", mClientMap.size());
702 for (auto it = mClientMap.begin(); it != mClientMap.end(); it++) { 769 for (auto it = mClientMap.begin(); it != mClientMap.end(); it++) {
703 RequestCatalog(it->second); 770 RequestCatalog(it->second);
704 } 771 }
@@ -791,7 +858,7 @@ void SipServer::dump_request(eXosip_event_t *evtp) { @@ -791,7 +858,7 @@ void SipServer::dump_request(eXosip_event_t *evtp) {
791 size_t len; 858 size_t len;
792 osip_message_to_str(evtp->request, &s, &len); 859 osip_message_to_str(evtp->request, &s, &len);
793 if (s) { 860 if (s) {
794 - LOG_INFO("\nprint request start\ntype={}\n{}\nprint request end\n",evtp->type,s); 861 + // LOG_INFO("\nprint request start\ntype={}\n{}\nprint request end\n",evtp->type,s);
795 } 862 }
796 } 863 }
797 864
@@ -801,6 +868,6 @@ void SipServer::dump_response(eXosip_event_t *evtp) { @@ -801,6 +868,6 @@ void SipServer::dump_response(eXosip_event_t *evtp) {
801 osip_message_to_str(evtp->response, &s, &len); 868 osip_message_to_str(evtp->response, &s, &len);
802 if (s) 869 if (s)
803 { 870 {
804 - LOG_INFO("\nprint response start\ntype={}\n{}\nprint response end\n",evtp->type,s); 871 + // LOG_INFO("\nprint response start\ntype={}\n{}\nprint response end\n",evtp->type,s);
805 } 872 }
806 } 873 }
807 \ No newline at end of file 874 \ No newline at end of file
sip/SipServer.h
@@ -21,12 +21,12 @@ using namespace std; @@ -21,12 +21,12 @@ using namespace std;
21 class Client { 21 class Client {
22 public: 22 public:
23 Client(string ip, int port, string device) : 23 Client(string ip, int port, string device) :
24 - mIp(ip),  
25 - mPort(port),  
26 - mRtpPort(0),  
27 - mDevice(device),  
28 - mIsReg(false){ 24 + mIp(ip), mPort(port), mDevice(device)
  25 + {
  26 + mRtpPort = 0;
  27 + mIsReg = false;
29 } 28 }
  29 +
30 ~Client() = default; 30 ~Client() = default;
31 public: 31 public:
32 32
@@ -74,16 +74,18 @@ private: @@ -74,16 +74,18 @@ private:
74 }; 74 };
75 75
76 76
  77 +class WebSocketServer;
  78 +
77 class SipServer { 79 class SipServer {
78 public: 80 public:
79 SipServer(); 81 SipServer();
80 ~SipServer(); 82 ~SipServer();
81 83
82 - bool Init(ServerInfo* pInfo); 84 + bool Init(ServerInfo* pInfo, WebSocketServer* pServer);
83 85
84 int RequestInvite_UDP(const char* dst_channel, const char* rtpIp, int rtpPort); 86 int RequestInvite_UDP(const char* dst_channel, const char* rtpIp, int rtpPort);
85 87
86 - int RequestInvite_TCP_a(const char* dst_channel, int rtpPort); 88 + int RequestInvite_TCP_a(const char* dst_channel, const char* rtpIp, int rtpPort);
87 89
88 int ByeInvite(std::string channel_id, string ip, int rtpPort); 90 int ByeInvite(std::string channel_id, string ip, int rtpPort);
89 91
@@ -120,6 +122,10 @@ private: @@ -120,6 +122,10 @@ private:
120 122
121 void cache_invite_callinfo(eXosip_event_t *evtp); 123 void cache_invite_callinfo(eXosip_event_t *evtp);
122 124
  125 + int reInvite(int cid);
  126 +
  127 + int inviteFailedResponse(int cid);
  128 +
123 private: 129 private:
124 bool mQuit{ false }; 130 bool mQuit{ false };
125 eXosip_t *mSipCtx; 131 eXosip_t *mSipCtx;
@@ -132,7 +138,12 @@ private: @@ -132,7 +138,12 @@ private:
132 138
133 thread* m_event_loop_thread{nullptr}; 139 thread* m_event_loop_thread{nullptr};
134 140
  141 + mutex m_invite_callinfo_map_mtx;
135 std::map< tuple<std::string, std::string, int>, CallInfo> m_invite_callinfo_map; 142 std::map< tuple<std::string, std::string, int>, CallInfo> m_invite_callinfo_map;
  143 + mutex m_invite_cmd_map_mtx;
  144 + std::map< tuple<std::string, std::string, int>, string> m_invite_cmd_map;
  145 +
  146 + WebSocketServer* m_pWsServer;
136 }; 147 };
137 148
138 149