diff --git a/sip/SipServer.cpp b/sip/SipServer.cpp index c68e651..357f523 100644 --- a/sip/SipServer.cpp +++ b/sip/SipServer.cpp @@ -22,6 +22,8 @@ #include #include +#include "WebSocketServer.h" + using namespace std; @@ -88,14 +90,16 @@ SipServer::~SipServer() { #endif // WIN32 } -bool SipServer::Init(ServerInfo* pInfo) { +bool SipServer::Init(ServerInfo* pInfo, WebSocketServer* pServer) { - if (pInfo == nullptr) { + if (pInfo == nullptr || pServer == nullptr) { return false; } mInfo = *pInfo; + m_pWsServer = pServer; + m_event_loop_thread = new std::thread(event_loop_thread, this); return true; @@ -112,14 +116,15 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) { case EXOSIP_CALL_CLOSED://21 LOG_INFO("EXOSIP_CALL_CLOSED type={}",evtp->type); - this->dump_request(evtp); - this->dump_response(evtp); + // this->dump_request(evtp); + // this->dump_response(evtp); break; case EXOSIP_CALL_RELEASED://22 LOG_INFO("EXOSIP_CALL_RELEASED type={}", evtp->type); - this->dump_request(evtp); - this->dump_response(evtp); + reInvite(evtp->cid); + // this->dump_request(evtp); + // this->dump_response(evtp); // this->clearClientMap(); break; @@ -164,10 +169,13 @@ int SipServer::sip_event_handle(eXosip_event_t *evtp) { cache_invite_callinfo(evtp); break; case EXOSIP_CALL_SERVERFAILURE: - LOG_INFO("EXOSIP_CALL_SERVERFAILURE type={}", evtp->type); + LOG_INFO("EXOSIP_CALL_SERVERFAILURE cid={}", evtp->cid); break; case EXOSIP_IN_SUBSCRIPTION_NEW: - LOG_INFO("EXOSIP_IN_SUBSCRIPTION_NEW type={}", evtp->type); + LOG_INFO("EXOSIP_IN_SUBSCRIPTION_NEW cid={}", evtp->cid); + break; + case EXOSIP_CALL_MESSAGE_ANSWERED: + LOG_INFO("EXOSIP_CALL_MESSAGE_ANSWERED cid={}", evtp->cid); break; default: LOG_INFO("type={} unknown", evtp->type); @@ -212,11 +220,13 @@ void SipServer::cache_invite_callinfo(eXosip_event_t *evtp) { string ip = contract_ip_from_message(s); string port = contract_port_from_message(s); - std::cout << "ip:" << ip << " port:" << port << std::endl; + LOG_INFO("ip:{} port:{}", ip, port); string channel_id = evtp->response->to->url->username; auto key = std::make_tuple(channel_id, ip, atoi(port.c_str())); + + std::lock_guard l(m_invite_callinfo_map_mtx); m_invite_callinfo_map[key] = call_info; } catch(const std::exception& e) @@ -548,6 +558,8 @@ int SipServer::request_bye(eXosip_event_t* evtp) { int SipServer::ByeInvite(std::string channel_id, string ip, int rtpPort) { + std::lock_guard l(m_invite_callinfo_map_mtx); + auto key = std::make_tuple(channel_id, ip, rtpPort); auto it = m_invite_callinfo_map.find(key); @@ -558,14 +570,64 @@ int SipServer::ByeInvite(std::string channel_id, string ip, int rtpPort) { CallInfo info = it->second; eXosip_lock(mSipCtx); - int ret = eXosip_call_terminate(mSipCtx, info.cid, info.did); + eXosip_call_terminate(mSipCtx, info.cid, info.did); eXosip_unlock(mSipCtx); m_invite_callinfo_map.erase(it); + std::lock_guard l_cmd(m_invite_cmd_map_mtx); + m_invite_cmd_map.erase(it->first); + return 0; } +int SipServer::reInvite(int cid) { + + std::lock_guard l(m_invite_callinfo_map_mtx); + + bool bFound = false; + auto it = m_invite_callinfo_map.begin(); + for (; it != m_invite_callinfo_map.end(); it++) { + CallInfo info = it->second; + if (info.cid == cid) { + bFound = true; + break; + } + } + + if (bFound) + { + auto key = it->first; + + std::lock_guard l_cmd(m_invite_cmd_map_mtx); + auto it_cmd = m_invite_cmd_map.find(key); + if(it_cmd != m_invite_cmd_map.end()) { + string strChannelId = std::get<0>(key); + string strIp = std::get<1>(key); + int iPort = std::get<2>(key); + + int ret = -1; + string cmd = it_cmd->second; + if (cmd == "udp") { + ret = RequestInvite_UDP(strChannelId.c_str(), strIp.c_str(), iPort); + } else if (cmd == "tcp_a") { + ret = RequestInvite_TCP_a(strChannelId.c_str(), strIp.c_str(), iPort); + } + + if (ret <= 0) { + LOG_ERROR("reInvite failed!"); + m_pWsServer->response_client(strChannelId, iPort, cmd, ret); + } + } + } + + return -1; +} + +int SipServer::inviteFailedResponse(int cid) { + +} + int SipServer::RequestInvite_UDP(const char* dst_channel, const char* rtpIp, int rtpPort) { // 检查设备是否在线 @@ -621,14 +683,17 @@ int SipServer::RequestInvite_UDP(const char* dst_channel, const char* rtpIp, int int call_id = eXosip_call_send_initial_invite(mSipCtx, msg); if (call_id > 0) { LOG_INFO("eXosip_call_send_initial_invite success: call_id={}", call_id); + auto key = std::make_tuple(string(dst_channel), string(rtpIp), rtpPort); + m_invite_cmd_map[key] = "udp"; } else { LOG_ERROR("eXosip_call_send_initial_invite error: call_id={}", call_id); } + return call_id; } -int SipServer::RequestInvite_TCP_a(const char* dst_channel, int rtpPort) { +int SipServer::RequestInvite_TCP_a(const char* dst_channel, const char* rtpIp, int rtpPort) { // 检查设备是否在线 if (!check_device_status(dst_channel)) { return -2; @@ -682,6 +747,8 @@ int SipServer::RequestInvite_TCP_a(const char* dst_channel, int rtpPort) { int call_id = eXosip_call_send_initial_invite(mSipCtx, msg); if (call_id > 0) { LOG_INFO("eXosip_call_send_initial_invite success: call_id={}", call_id); + auto key = std::make_tuple(string(dst_channel), string(rtpIp), rtpPort); + m_invite_cmd_map[key] = "tcp_a"; } else { LOG_ERROR("eXosip_call_send_initial_invite error: call_id={}", call_id); @@ -694,11 +761,11 @@ void SipServer::cacheCatalog() { std::lock_guard l(m_client_map_mtx); if (mClientMap.size() <= 0){ - cout << "no IPC" << endl; + LOG_WARN("NO IPC"); return ; } - cout << "client size:" << mClientMap.size() << endl; + LOG_INFO("client size:{}", mClientMap.size()); for (auto it = mClientMap.begin(); it != mClientMap.end(); it++) { RequestCatalog(it->second); } @@ -791,7 +858,7 @@ void SipServer::dump_request(eXosip_event_t *evtp) { size_t len; osip_message_to_str(evtp->request, &s, &len); if (s) { - LOG_INFO("\nprint request start\ntype={}\n{}\nprint request end\n",evtp->type,s); + // LOG_INFO("\nprint request start\ntype={}\n{}\nprint request end\n",evtp->type,s); } } @@ -801,6 +868,6 @@ void SipServer::dump_response(eXosip_event_t *evtp) { osip_message_to_str(evtp->response, &s, &len); if (s) { - LOG_INFO("\nprint response start\ntype={}\n{}\nprint response end\n",evtp->type,s); + // LOG_INFO("\nprint response start\ntype={}\n{}\nprint response end\n",evtp->type,s); } } \ No newline at end of file diff --git a/sip/SipServer.h b/sip/SipServer.h index 09dfcab..c7b712f 100644 --- a/sip/SipServer.h +++ b/sip/SipServer.h @@ -21,12 +21,12 @@ using namespace std; class Client { public: Client(string ip, int port, string device) : - mIp(ip), - mPort(port), - mRtpPort(0), - mDevice(device), - mIsReg(false){ + mIp(ip), mPort(port), mDevice(device) + { + mRtpPort = 0; + mIsReg = false; } + ~Client() = default; public: @@ -74,16 +74,18 @@ private: }; +class WebSocketServer; + class SipServer { public: SipServer(); ~SipServer(); - bool Init(ServerInfo* pInfo); + bool Init(ServerInfo* pInfo, WebSocketServer* pServer); int RequestInvite_UDP(const char* dst_channel, const char* rtpIp, int rtpPort); - int RequestInvite_TCP_a(const char* dst_channel, int rtpPort); + int RequestInvite_TCP_a(const char* dst_channel, const char* rtpIp, int rtpPort); int ByeInvite(std::string channel_id, string ip, int rtpPort); @@ -120,6 +122,10 @@ private: void cache_invite_callinfo(eXosip_event_t *evtp); + int reInvite(int cid); + + int inviteFailedResponse(int cid); + private: bool mQuit{ false }; eXosip_t *mSipCtx; @@ -132,7 +138,12 @@ private: thread* m_event_loop_thread{nullptr}; + mutex m_invite_callinfo_map_mtx; std::map< tuple, CallInfo> m_invite_callinfo_map; + mutex m_invite_cmd_map_mtx; + std::map< tuple, string> m_invite_cmd_map; + + WebSocketServer* m_pWsServer; };