Commit e918c2a70c7197e1b995c1771d68752aad5ea5c7

Authored by Hu Chunming
1 parent 346fe72a

call release 自动重新invite

sip/SipServer.cpp
... ... @@ -22,6 +22,8 @@
22 22 #include <sstream>
23 23 #include <algorithm>
24 24  
  25 +#include "WebSocketServer.h"
  26 +
25 27  
26 28 using namespace std;
27 29  
... ... @@ -88,14 +90,16 @@ SipServer::~SipServer() {
88 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 96 return false;
95 97 }
96 98  
97 99 mInfo = *pInfo;
98 100  
  101 + m_pWsServer = pServer;
  102 +
99 103 m_event_loop_thread = new std::thread(event_loop_thread, this);
100 104  
101 105 return true;
... ... @@ -112,14 +116,15 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) {
112 116  
113 117 case EXOSIP_CALL_CLOSED://21
114 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 121 break;
118 122  
119 123 case EXOSIP_CALL_RELEASED://22
120 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 129 // this->clearClientMap();
125 130 break;
... ... @@ -164,10 +169,13 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) {
164 169 cache_invite_callinfo(evtp);
165 170 break;
166 171 case EXOSIP_CALL_SERVERFAILURE:
167   - LOG_INFO("EXOSIP_CALL_SERVERFAILURE type={}", evtp->type);
  172 + LOG_INFO("EXOSIP_CALL_SERVERFAILURE cid={}", evtp->cid);
168 173 break;
169 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 179 break;
172 180 default:
173 181 LOG_INFO("type={} unknown", evtp->type);
... ... @@ -212,11 +220,13 @@ void SipServer::cache_invite_callinfo(eXosip_event_t *evtp) {
212 220 string ip = contract_ip_from_message(s);
213 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 225 string channel_id = evtp->response->to->url->username;
218 226  
219 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 230 m_invite_callinfo_map[key] = call_info;
221 231 }
222 232 catch(const std::exception& e)
... ... @@ -548,6 +558,8 @@ int SipServer::request_bye(eXosip_event_t* evtp) {
548 558  
549 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 563 auto key = std::make_tuple(channel_id, ip, rtpPort);
552 564  
553 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 570 CallInfo info = it->second;
559 571  
560 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 574 eXosip_unlock(mSipCtx);
563 575  
564 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 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 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 683 int call_id = eXosip_call_send_initial_invite(mSipCtx, msg);
622 684 if (call_id > 0) {
623 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 689 else {
626 690 LOG_ERROR("eXosip_call_send_initial_invite error: call_id={}", call_id);
627 691 }
  692 +
628 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 698 if (!check_device_status(dst_channel)) {
634 699 return -2;
... ... @@ -682,6 +747,8 @@ int SipServer::RequestInvite_TCP_a(const char* dst_channel, int rtpPort) {
682 747 int call_id = eXosip_call_send_initial_invite(mSipCtx, msg);
683 748 if (call_id > 0) {
684 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 753 else {
687 754 LOG_ERROR("eXosip_call_send_initial_invite error: call_id={}", call_id);
... ... @@ -694,11 +761,11 @@ void SipServer::cacheCatalog() {
694 761 std::lock_guard<std::mutex> l(m_client_map_mtx);
695 762  
696 763 if (mClientMap.size() <= 0){
697   - cout << "no IPC" << endl;
  764 + LOG_WARN("NO IPC");
698 765 return ;
699 766 }
700 767  
701   - cout << "client size:" << mClientMap.size() << endl;
  768 + LOG_INFO("client size:{}", mClientMap.size());
702 769 for (auto it = mClientMap.begin(); it != mClientMap.end(); it++) {
703 770 RequestCatalog(it->second);
704 771 }
... ... @@ -791,7 +858,7 @@ void SipServer::dump_request(eXosip_event_t *evtp) {
791 858 size_t len;
792 859 osip_message_to_str(evtp->request, &s, &len);
793 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 868 osip_message_to_str(evtp->response, &s, &len);
802 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 874 \ No newline at end of file
... ...
sip/SipServer.h
... ... @@ -21,12 +21,12 @@ using namespace std;
21 21 class Client {
22 22 public:
23 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 30 ~Client() = default;
31 31 public:
32 32  
... ... @@ -74,16 +74,18 @@ private:
74 74 };
75 75  
76 76  
  77 +class WebSocketServer;
  78 +
77 79 class SipServer {
78 80 public:
79 81 SipServer();
80 82 ~SipServer();
81 83  
82   - bool Init(ServerInfo* pInfo);
  84 + bool Init(ServerInfo* pInfo, WebSocketServer* pServer);
83 85  
84 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 90 int ByeInvite(std::string channel_id, string ip, int rtpPort);
89 91  
... ... @@ -120,6 +122,10 @@ private:
120 122  
121 123 void cache_invite_callinfo(eXosip_event_t *evtp);
122 124  
  125 + int reInvite(int cid);
  126 +
  127 + int inviteFailedResponse(int cid);
  128 +
123 129 private:
124 130 bool mQuit{ false };
125 131 eXosip_t *mSipCtx;
... ... @@ -132,7 +138,12 @@ private:
132 138  
133 139 thread* m_event_loop_thread{nullptr};
134 140  
  141 + mutex m_invite_callinfo_map_mtx;
135 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  
... ...