Blame view

sip/SipServer.h 5.07 KB
c887a0f0   Hu Chunming   提交初成版代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #ifndef __SIPSERVER_H__

  #define __SIPSERVER_H__

  

  extern "C" {

  #include <osip2/osip_mt.h>

  #include <eXosip2/eXosip.h>

  }

  

  #include <map>

  #include <string>

  #include <thread>

  #include <chrono>

  #include <mutex>

  #include <tuple>

  

756d2ef6   Hu Chunming   修改缓存设备状态的更新方式
16
  #include "./Message/GBXmlParser.h"

c887a0f0   Hu Chunming   提交初成版代码
17
18
19
20
  #include "sip_header.h"

  

  using namespace std;

  

756d2ef6   Hu Chunming   修改缓存设备状态的更新方式
21
22
23
24
25
26
27
  enum DeviceType {

      DEVICE_TYPE_UNKNOWN,

      DEVICE_TYPE_IPC,

      DEVICE_TYPE_NVR,

      DEVICE_TYPE_PLATFORM

  };

  

c887a0f0   Hu Chunming   提交初成版代码
28
29
30
  class Client {

  public:

      Client(string ip, int port, string device) :

e918c2a7   Hu Chunming   call release 自动重新...
31
32
33
34
              mIp(ip), mPort(port),  mDevice(device) 

      {

          mRtpPort = 0;

          mIsReg = false;

756d2ef6   Hu Chunming   修改缓存设备状态的更新方式
35
          mType = DEVICE_TYPE_UNKNOWN;

c887a0f0   Hu Chunming   提交初成版代码
36
      }

e918c2a7   Hu Chunming   call release 自动重新...
37
  

c887a0f0   Hu Chunming   提交初成版代码
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
      ~Client() = default;

  public:

  

      void setRtpPort(int rtpPort) {

          mRtpPort = rtpPort;

      }

  

      void setReg(bool isReg) {

          mIsReg = isReg;

      }

      string  getDevice() const{

          return mDevice;

      }

      string  getIp() const{

          return mIp;

      }

      int getPort() const{

          return mPort;

      }

  

      unsigned long getHeartBeat() {

          return mHeartBeatTime;

      }

  

      void updateHeartBeat(unsigned long ts) {

          mHeartBeatTime = ts;

3a5bc7be   Hu Chunming   添加无鉴权;支持摄像头直连
64
65
66
67
68
69
70
71
72
73
74
75
          mHeartBeatCount++;

          if (mHeartBeatFirst <= 0){

              mHeartBeatFirst = ts;

          }

      }

  

      unsigned long getHeartBeatGap() {

          if (mHeartBeatCount >= 2)

          {

             return (mHeartBeatTime - mHeartBeatFirst)/mHeartBeatCount ;

          }

          return 0;

c887a0f0   Hu Chunming   提交初成版代码
76
77
      }

  

10821ac3   Hu Chunming   1.定时刷新设备目录;
78
79
80
81
82
83
84
85
      long getExpiry() {

          return mExipry;

      }

  

      void setExpiry(long expiry) {

          mExipry = expiry;

      }

  

756d2ef6   Hu Chunming   修改缓存设备状态的更新方式
86
87
88
89
90
91
92
93
      DeviceType getDeviceType() {

          return mType;

      }

  

      void setDeviceType(DeviceType type) {

          mType = type;

      }

  

c887a0f0   Hu Chunming   提交初成版代码
94
95
96
97
98
99
100
  private:

      string mIp; // client ip

      int mPort; // client port

      string mDevice;// 340200000013200000024

      bool mIsReg;

      int mRtpPort;

      unsigned long mHeartBeatTime{0};

3a5bc7be   Hu Chunming   添加无鉴权;支持摄像头直连
101
102
103
      unsigned long mHeartBeatFirst{0};

      unsigned long mHeartBeatGap{1};

      unsigned long mHeartBeatCount{0};

10821ac3   Hu Chunming   1.定时刷新设备目录;
104
      long mExipry{0};

756d2ef6   Hu Chunming   修改缓存设备状态的更新方式
105
      DeviceType mType;

c887a0f0   Hu Chunming   提交初成版代码
106
107
108
  };

  

  

e918c2a7   Hu Chunming   call release 自动重新...
109
110
  class WebSocketServer;

  

c887a0f0   Hu Chunming   提交初成版代码
111
112
113
114
115
  class SipServer {

  public:

      SipServer();

      ~SipServer();

  

e918c2a7   Hu Chunming   call release 自动重新...
116
      bool Init(ServerInfo* pInfo, WebSocketServer* pServer);

c887a0f0   Hu Chunming   提交初成版代码
117
  

2a17578f   Hu Chunming   添加tcp_p支持;
118
119
120
121
122
123
124
      /*

      GB28181流传输几种模式

          UDP:服务端监听UDP端口,通过INVITE信令告知设备端口,设备主动向服务端发起流传输

          TCP被动:服务端监听TCP端口,通过INVITE信令告知设备端口,设备向服务端发起流传输

          TCP主动:设备端告知服务端监听的TCP端口情况,服务端主动向设备拉流,此种场景较少,且设备所在网络可以被服务所在网络访问(如下级设备与上级GB28181服务在同一个局域网,或者都在公网上能相互访问)。

       */

  

c887a0f0   Hu Chunming   提交初成版代码
125
126
      int RequestInvite_UDP(const char* dst_channel, const char* rtpIp, int rtpPort);

  

2a17578f   Hu Chunming   添加tcp_p支持;
127
      int RequestInvite_TCP_p(const char* dst_channel, const char* rtpIp, int rtpPort);

c887a0f0   Hu Chunming   提交初成版代码
128
129
130
131
132
  

      int ByeInvite(std::string channel_id, string ip, int rtpPort);

  

      void Close();

  

2a17578f   Hu Chunming   添加tcp_p支持;
133
134
      void RemoveInviteTask(tuple<std::string, std::string, int>);

  

c887a0f0   Hu Chunming   提交初成版代码
135
136
  public:

      void event_loop();

c887a0f0   Hu Chunming   提交初成版代码
137
138
139
140
141
142
  

  private:

      int init_sip_server();

      int sip_event_handle(eXosip_event_t *evtp);

  

      void RequestCatalog(Client* client);

c887a0f0   Hu Chunming   提交初成版代码
143
  

3a5bc7be   Hu Chunming   添加无鉴权;支持摄像头直连
144
145
      void RequestDeviceInfo(Client* client);

  

10821ac3   Hu Chunming   1.定时刷新设备目录;
146
      void cacheClient(osip_uri_t *url, int expiry);

73ef4ff3   Hu Chunming   提交三方库
147
  

c887a0f0   Hu Chunming   提交初成版代码
148
149
      void response_message_answer(eXosip_event_t *evtp,int code);

      void response_register(eXosip_event_t *evtp);

3a5bc7be   Hu Chunming   添加无鉴权;支持摄像头直连
150
      void response_register_noauth(eXosip_event_t *evtp);

c887a0f0   Hu Chunming   提交初成版代码
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
      void response_register_401unauthorized(eXosip_event_t *evt);

      void response_message(eXosip_event_t *evtp);

      void response_invite_ack(eXosip_event_t *evtp);

      int request_bye(eXosip_event_t* evtp);// 通知相机停止推流

      int parse_xml(const char* data, const char* s_mark, bool with_s_make, const char* e_mark, bool with_e_make, char* dest);

      void dump_request(eXosip_event_t *evtp);

      void dump_response(eXosip_event_t *evtp);

  

      int clearClientMap();

      void deleteClientByDevice(string device);

  

      Client* get_parent_by_id(string id);

      bool check_device_status(string id);

  

      void cache_invite_callinfo(eXosip_event_t *evtp);

  

e918c2a7   Hu Chunming   call release 自动重新...
167
168
      int reInvite(int cid);

  

3a5bc7be   Hu Chunming   添加无鉴权;支持摄像头直连
169
      int check_device_type(string sip_id);

e918c2a7   Hu Chunming   call release 自动重新...
170
  

2a17578f   Hu Chunming   添加tcp_p支持;
171
172
173
174
      int invite_UDP_nolock(const char* dst_channel, const char* rtpIp, int rtpPort);

  

      int invite_TCP_p_nolock(const char* dst_channel, const char* rtpIp, int rtpPort);

  

c887a0f0   Hu Chunming   提交初成版代码
175
176
177
178
179
180
181
182
183
184
185
186
  private:

      bool mQuit{ false };

      eXosip_t *mSipCtx;

      ServerInfo mInfo;

  

      std::map<std::string, Client *> mClientMap;// <DeviceID,SipClient>

      mutex m_client_map_mtx;

      std::map<std::string, DeviceInfo> m_device_map;

      mutex m_device_map_mtx;

  

      thread* m_event_loop_thread{nullptr};

  

e918c2a7   Hu Chunming   call release 自动重新...
187
      mutex m_invite_callinfo_map_mtx;

c887a0f0   Hu Chunming   提交初成版代码
188
      std::map< tuple<std::string, std::string, int>, CallInfo> m_invite_callinfo_map;

e918c2a7   Hu Chunming   call release 自动重新...
189
190
191
192
      mutex m_invite_cmd_map_mtx;

      std::map< tuple<std::string, std::string, int>, string> m_invite_cmd_map;

  

      WebSocketServer* m_pWsServer;

c887a0f0   Hu Chunming   提交初成版代码
193
194
195
196
  };

  

  

  #endif //__SIPSERVER_H__