Blame view

src/decoder/gb28181/sip/SipServer.cpp 22.1 KB
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  //

  // Created bxc on 2022/11/25.

  //

  

  #include "SipServer.h"

  

  #ifndef WIN32

  // Linux系统

  #include <arpa/inet.h>

  #else

  #include <WinSock2.h>

  #pragma comment(lib, "ws2_32.lib")

  #endif // !WIN32

  

  #include <cstring>

  #include "../common_header.h"

  #include "./Utils/HTTPDigest.h"

  

  #include <sstream>

fe625b06   Hu Chunming   设备状态的检查与定时更新
20
  #include <algorithm>

ffe81045   Hu Chunming   优化目录xml解析;优化sipse...
21
  #include <fstream>

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
22
23
24
25
26
27
28
29
30
31
32
33
34
  

  using namespace std;

  

  static void event_loop_thread(void* arg) {

      SipServer* _this = (SipServer*)arg;

      if (_this != nullptr) {

          _this->event_loop();

      }

      else {

          LOG_ERROR("event_loop线程启动失败 !");

      }

  }

  

fe625b06   Hu Chunming   设备状态的检查与定时更新
35
36
37
38
39
40
41
42
43
44
  static void timing_getcatlog_thread(void* arg) {

      SipServer* _this = (SipServer*)arg;

      if (_this != nullptr) {

          _this->timing_getcatlog();

      }

      else {

          LOG_ERROR("event_loop线程启动失败 !");

      }

  }

  

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
45
46
47
48
49
50
51
52
53
  static void dt_printSipMsg(osip_message_t* msg) {

      osip_message_t* clone_event = NULL;

      size_t length = 0;

      char* message = NULL;

      osip_message_clone(msg, &clone_event);

      osip_message_to_str(clone_event, &message, &length);

      LOG_INFO("{}", message);

  }

  

fe625b06   Hu Chunming   设备状态的检查与定时更新
54
55
56
57
58
59
60
61
62
63
64
  static bool isntspace(const char &ch) {

      return !isspace(ch);

  }

  

  static std::string trim(const std::string &s) {

      std::string::const_iterator iter1 = find_if(s.begin(), s.end(), isntspace);

      std::string::const_iterator iter2 = find_if(s.rbegin(), s.rend(), isntspace).base();

  

      return iter1 < iter2 ? string(iter1, iter2) : std::string("");

  }

  

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  SipServer::SipServer():

          mQuit(false),

          mSipCtx(nullptr){

  #ifdef WIN32

      WSADATA wsaData;

      if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)

      {

          LOG_ERROR("WSAStartup Error");

          return;

      }

  #endif // WIN32

  

  }

  SipServer::~SipServer() {

      LOG_INFO("~SipServer");

  

      if (m_event_loop_thread)

      {

          mQuit = true;

          m_event_loop_thread->join();

  

          delete m_event_loop_thread;

          m_event_loop_thread = nullptr;

      }

  

      this->clearClientMap();

  #ifdef WIN32

      WSACleanup();

  #endif // WIN32

  }

  

ffe81045   Hu Chunming   优化目录xml解析;优化sipse...
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  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;

      }

  

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
119
      LOG_INFO("{}:{}", mInfo.getIp(), mInfo.getPort());

74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
120
121
122
  

      m_event_loop_thread = new std::thread(event_loop_thread, this);

  

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
123
124
125
126
127
128
129
      return true;

  }

  

  int SipServer::sip_event_handle(eXosip_event_t *evtp) {

  

      switch(evtp->type) {

          case EXOSIP_CALL_MESSAGE_NEW://14

fe625b06   Hu Chunming   设备状态的检查与定时更新
130
              // LOG_INFO("EXOSIP_CALL_MESSAGE_NEW type={}", evtp->type);

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
              this->dump_request(evtp);

              this->dump_response(evtp);

              break;

  

          case EXOSIP_CALL_CLOSED://21

              LOG_INFO("EXOSIP_CALL_CLOSED type={}",evtp->type);

              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);

  

ffe81045   Hu Chunming   优化目录xml解析;优化sipse...
146
              // this->clearClientMap();

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
147
148
              break;

          case EXOSIP_MESSAGE_NEW://23

1116447e   Hu Chunming   代码逻辑有花
149
              // LOG_INFO("EXOSIP_MESSAGE_NEW type={}",evtp->type);

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
150
151
152
153
154
155
156
157
  

              if (MSG_IS_REGISTER(evtp->request)) {

                  this->response_register(evtp);

              }

              else if (MSG_IS_MESSAGE(evtp->request)) {

                  this->response_message(evtp);

              }

              else if(MSG_IS_BYE(evtp->request)){

fe625b06   Hu Chunming   设备状态的检查与定时更新
158
                  LOG_ERROR("BYE");

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
              }

              else{

                  LOG_ERROR("unknown2");

              }

              break;

          case EXOSIP_MESSAGE_ANSWERED:

              this->dump_request(evtp);

              break;

          case EXOSIP_MESSAGE_REQUESTFAILURE:

              LOG_INFO("EXOSIP_MESSAGE_REQUESTFAILURE type={}: Receive feedback on sending failure after actively sending a message", evtp->type);

              this->dump_request(evtp);

              this->dump_response(evtp);

              break;

          case EXOSIP_CALL_INVITE:

              LOG_INFO("EXOSIP_CALL_INVITE type={}: The server receives the Invite request actively sent by the client", evtp->type);

              break;

          case EXOSIP_CALL_PROCEEDING://5

              LOG_INFO("EXOSIP_CALL_PROCEEDING type={}: When the server receives the Invite (SDP) confirmation reply from the client", evtp->type);

              this->dump_request(evtp);

              this->dump_response(evtp);

              break;

          case EXOSIP_CALL_ANSWERED:// 7

              LOG_INFO("EXOSIP_CALL_ANSWERED type={}: The server receives an invite (SDP) confirmation reply from the client", evtp->type);

              this->dump_request(evtp);

              this->dump_response(evtp);

  

              this->response_invite_ack(evtp);

              break;

          case EXOSIP_CALL_SERVERFAILURE:

              LOG_INFO("EXOSIP_CALL_SERVERFAILURE type={}", evtp->type);

              break;

          case EXOSIP_IN_SUBSCRIPTION_NEW:

              LOG_INFO("EXOSIP_IN_SUBSCRIPTION_NEW type={}", evtp->type);

              break;

          default:

              LOG_INFO("type={} unknown", evtp->type);

              break;

      }

  

      return 0;

  }

  

  int SipServer::init_sip_server() {

      mSipCtx = eXosip_malloc();

      if (!mSipCtx) {

          LOG_ERROR("eXosip_malloc error");

          return -1;

      }

      if (eXosip_init(mSipCtx)) {

          LOG_ERROR("eXosip_init error");

          return -1;

      }

      if (eXosip_listen_addr(mSipCtx, IPPROTO_UDP, nullptr, mInfo.getPort(), AF_INET, 0)) {

          LOG_ERROR("eXosip_listen_addr error");

          return -1;

      }

      eXosip_set_user_agent(mSipCtx, mInfo.getUa().c_str());

      if (eXosip_add_authentication_info(mSipCtx, mInfo.getSipId().c_str(), mInfo.getSipId().c_str(), mInfo.getSipPass().c_str(), NULL, mInfo.getSipRealm().c_str())) {

          LOG_ERROR("eXosip_add_authentication_info error");

          return -1;

      }

  

      return 0;

  }

  

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
224
225
226
227
228
  void SipServer::event_loop() {

  

      if(this->init_sip_server() !=0 ){

          return;

      }

ffe81045   Hu Chunming   优化目录xml解析;优化sipse...
229
      

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
230
231
232
233
234
235
236
237
238
239
240
241
242
      while(!mQuit) {

          eXosip_event_t *evtp = eXosip_event_wait(mSipCtx, 0, 20);

          if (!evtp){

              eXosip_automatic_action(mSipCtx);

              osip_usleep(100000);

              continue;

          }

          eXosip_automatic_action(mSipCtx);

          this->sip_event_handle(evtp);

          eXosip_event_free(evtp);

      }

  }

  

fe625b06   Hu Chunming   设备状态的检查与定时更新
243
244
245
246
247
248
249
250
251
252
253
254
  void SipServer::close() {

      mQuit = true;

  }

  

  void SipServer::timing_getcatlog() {

      while(!mQuit) {

          // 5分钟更新一次

          std::this_thread::sleep_for(std::chrono::minutes(5));

          cacheCatalog();

      }

  }

  

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  void SipServer::response_message_answer(eXosip_event_t *evtp,int code){

  

      int returnCode = 0 ;

      osip_message_t * pRegister = nullptr;

      returnCode = eXosip_message_build_answer (mSipCtx,evtp->tid,code,&pRegister);

      bool bRegister = false;

      if(pRegister){

          bRegister = true;

      }

      if (returnCode == 0 && bRegister)

      {

          eXosip_lock(mSipCtx);

          eXosip_message_send_answer (mSipCtx,evtp->tid,code,pRegister);

          eXosip_unlock(mSipCtx);

      }

      else{

          LOG_ERROR("code={},returnCode={},bRegister={}",code,returnCode,bRegister);

      }

  

  }

  void SipServer::response_register(eXosip_event_t *evtp) {

  

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
      osip_authorization_t * auth = nullptr;

      osip_message_get_authorization(evtp->request, 0, &auth);

  

      if(auth && auth->username){

  

          char *method = NULL, // REGISTER

          *algorithm = NULL, // MD5

          *username = NULL,// 340200000013200000024

          *realm = NULL, // sip服务器传给客户端,客户端携带并提交上来的sip服务域

          *nonce = NULL, //sip服务器传给客户端,客户端携带并提交上来的nonce

          *nonce_count = NULL,

          *uri = NULL; // sip:34020000002000000001@3402000000

  

          osip_contact_t *contact = nullptr;

          osip_message_get_contact (evtp->request, 0, &contact);

  

          method = evtp->request->sip_method;

          char calc_response[HASHHEXLEN];

          HASHHEX HA1, HA2 = "", Response;

  

  #define SIP_STRDUP(field) if (auth->field) (field) = osip_strdup_without_quote(auth->field)

  

          SIP_STRDUP(algorithm);

          SIP_STRDUP(username);

          SIP_STRDUP(realm);

          SIP_STRDUP(nonce);

          SIP_STRDUP(nonce_count);

          SIP_STRDUP(uri);

  

          DigestCalcHA1(algorithm, username, realm, mInfo.getSipPass().c_str(), nonce, nonce_count, HA1);

          DigestCalcResponse(HA1, nonce, nonce_count, auth->cnonce, auth->message_qop, 0, method, uri, HA2, Response);

  

          HASHHEX temp_HA1;

          HASHHEX temp_response;

          DigestCalcHA1("REGISTER", username, mInfo.getSipRealm().c_str(), mInfo.getSipPass().c_str(), mInfo.getNonce().c_str(), NULL, temp_HA1);

          DigestCalcResponse(temp_HA1, mInfo.getNonce().c_str(), NULL, NULL, NULL, 0, method, uri, NULL, temp_response);

          memcpy(calc_response, temp_response, HASHHEXLEN);

  

          

  

          if (!memcmp(calc_response, Response, HASHHEXLEN)) {

              this->response_message_answer(evtp,200);

  

              Client* client = new Client(strdup(contact->url->host),

                  atoi(contact->url->port),

                  strdup(username));

  

              LOG_INFO("Camera registration succee,ip={},port={},device={}",client->getIp(),client->getPort(),client->getDevice());

  

1116447e   Hu Chunming   代码逻辑有花
326
327
328
329
330
331
332
333
334
335
              std::lock_guard<std::mutex> l(m_client_map_mtx);

  

              auto it_client = mClientMap.find(client->getDevice());

              if (it_client != mClientMap.end()) {

                  Client* c = it_client->second;

                  delete c;

                  c = nullptr;

                  mClientMap.erase(it_client);

              }

              

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
336
337
              mClientMap.insert(std::make_pair(client->getDevice(),client));

  

5b86d771   Hu Chunming   实现GB28181 UDP 基于d...
338
339
              // NVR注册成功,立即请求设备目录

              RequestCatalog(client);

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
  

          } else {

              this->response_message_answer(evtp,401);

              LOG_INFO("Camera registration error, p={},port={},device={}", strdup(contact->url->host), atoi(contact->url->port), strdup(username));

          }

  

          osip_free(algorithm);

          osip_free(username);

          osip_free(realm);

          osip_free(nonce);

          osip_free(nonce_count);

          osip_free(uri);

      } else {

          response_register_401unauthorized(evtp);

      }

  

  }

  

  void SipServer::response_register_401unauthorized(eXosip_event_t *evtp) {

  

      char *dest = nullptr;

      osip_message_t * reg = nullptr;

      osip_www_authenticate_t * header = nullptr;

  

      osip_www_authenticate_init(&header);

      osip_www_authenticate_set_auth_type (header, osip_strdup("Digest"));

      osip_www_authenticate_set_realm(header,osip_enquote(mInfo.getSipRealm().c_str()));

      osip_www_authenticate_set_nonce(header,osip_enquote(mInfo.getNonce().c_str()));

      osip_www_authenticate_to_str(header, &dest);

      int ret = eXosip_message_build_answer (mSipCtx, evtp->tid, 401, &reg);

      if ( ret == 0 && reg != nullptr ) {

          osip_message_set_www_authenticate(reg, dest);

          osip_message_set_content_type(reg, "Application/MANSCDP+xml");

          eXosip_lock(mSipCtx);

          eXosip_message_send_answer (mSipCtx, evtp->tid,401, reg);

          eXosip_unlock(mSipCtx);

          LOG_INFO("response_register_401unauthorized success");

      }else {

          LOG_INFO("response_register_401unauthorized error");

      }

  

      osip_www_authenticate_free(header);

      osip_free(dest);

  

  }

  

0af3b245   Hu Chunming   添加多nvr和多摄像头管理
386
387
388
389
390
391
392
393
  void printDevice(std::vector<DeviceInfo>  vec_device) {

      LOG_INFO("-----start print----");

      for (size_t i = 0; i < vec_device.size(); i++) {

          LOG_INFO("{}   {}", vec_device[i].id, vec_device[i].parentid);

      }

      LOG_INFO("-----end print----");

  }

  

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
394
395
396
397
398
399
400
401
402
403
404
  void SipServer::response_message(eXosip_event_t *evtp) {

  

      osip_body_t* body = nullptr;

      char CmdType[64] = {0};

      char DeviceID[64] = {0};

      osip_message_get_body(evtp->request, 0, &body);

      if(body){

          parse_xml(body->body, "<CmdType>", false, "</CmdType>", false, CmdType);

          parse_xml(body->body, "<DeviceID>", false, "</DeviceID>", false, DeviceID);

      }

  

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
405
406
407
408
      if(!strcmp(CmdType, "Catalog")) {

          this->response_message_answer(evtp,200);

          // 需要根据对方的Catelog请求,做一些相应的应答请求

          CCatalogParser catPaser;

ffe81045   Hu Chunming   优化目录xml解析;优化sipse...
409
          std::vector<DeviceInfo>  vec_device = catPaser.DecodeCatlog(body->body);

0af3b245   Hu Chunming   添加多nvr和多摄像头管理
410
          printDevice(vec_device);

1116447e   Hu Chunming   代码逻辑有花
411
412
  

          std::lock_guard<std::mutex> l(m_device_map_mtx);

0af3b245   Hu Chunming   添加多nvr和多摄像头管理
413
414
415
416
          for (size_t i = 0; i < vec_device.size(); i++) {

              DeviceInfo info = vec_device[i];

              m_device_map[info.id] = info;

          }

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
417
418
419
      }

      else if(!strcmp(CmdType, "Keepalive")){

          this->response_message_answer(evtp,200);

1116447e   Hu Chunming   代码逻辑有花
420
421
422
423
424
425
          // LOG_INFO("CmdType={},DeviceID={}", CmdType, DeviceID);

          std::lock_guard<std::mutex> l_c(m_client_map_mtx);

          Client* client = mClientMap[DeviceID];

          if (client != nullptr) {

              client->updateHeartBeat(UtilTools::get_cur_time_ms());

          }

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
426
427
428
      }else{

          this->response_message_answer(evtp,200);

      }

fe625b06   Hu Chunming   设备状态的检查与定时更新
429
430
431
  }

  

  bool SipServer::check_device_status(string id) {

1116447e   Hu Chunming   代码逻辑有花
432
      std::lock_guard<std::mutex> l(m_device_map_mtx);

fe625b06   Hu Chunming   设备状态的检查与定时更新
433
434
435
436
      auto it_info = m_device_map.find(id);

      if (it_info == m_device_map.end()) {

          return false;

      }

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
437
  

fe625b06   Hu Chunming   设备状态的检查与定时更新
438
439
440
441
442
443
444
445
      string status = trim(it_info->second.status);

      // status = std::tolower(status.c_str());

      transform(status.begin(), status.end(), status.begin(),::tolower);

      if (status == "on"){

          return true;

      }

      

      return false;

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
446
447
  }

  

0af3b245   Hu Chunming   添加多nvr和多摄像头管理
448
  Client* SipServer::get_parent_by_id(string id) {

1116447e   Hu Chunming   代码逻辑有花
449
      std::lock_guard<std::mutex> l(m_device_map_mtx);

0af3b245   Hu Chunming   添加多nvr和多摄像头管理
450
451
452
453
454
      auto it_info = m_device_map.find(id);

      if (it_info == m_device_map.end()) {

          return nullptr;

      }

      string parent_id = it_info->second.parentid;

1116447e   Hu Chunming   代码逻辑有花
455
456
  

      std::lock_guard<std::mutex> l_c(m_client_map_mtx);

0af3b245   Hu Chunming   添加多nvr和多摄像头管理
457
458
459
460
461
462
463
464
      auto it_client = mClientMap.find(parent_id);

      if (it_client == mClientMap.end()) {

          return nullptr;

      }

      

      return mClientMap[parent_id];

  }

  

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
  void SipServer::response_invite_ack(eXosip_event_t *evtp){

  

      osip_message_t* msg = nullptr;

      int ret = eXosip_call_build_ack(mSipCtx, evtp->did, &msg);

      if (!ret && msg) {

          eXosip_call_send_ack(mSipCtx, evtp->did, msg);

      } else {

          LOG_ERROR("eXosip_call_send_ack error={}", ret);

      }

  

  }

  int SipServer::request_bye(eXosip_event_t* evtp) {

  

      eXosip_lock(mSipCtx);

      int ret = eXosip_call_terminate(mSipCtx, evtp->cid, evtp->did);

      eXosip_unlock(mSipCtx);

  

      return ret;

  }

  

74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
485
  int SipServer::RequestInvite_UDP(const char* dst_channel, int rtpPort) {

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
486
  

fe625b06   Hu Chunming   设备状态的检查与定时更新
487
488
489
      // 检查设备是否在线

      if (!check_device_status(dst_channel)) {

          return -2;

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
490
491
      }

  

0af3b245   Hu Chunming   添加多nvr和多摄像头管理
492
493
494
495
      Client* client = get_parent_by_id(dst_channel);

      if (client == nullptr) {

          return -1;

      }

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
496
497
498
499
500
501
502
503
504
505
  

      LOG_INFO("INVITE UDP");

  

      char session_exp[1024] = { 0 };

      osip_message_t* msg = nullptr;

      char from[1024] = { 0 };

      char to[1024] = { 0 };

      char sdp[2048] = { 0 };

      char head[1024] = { 0 };

  

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
506
507
      sprintf(from, "sip:%s@%s:%d", mInfo.getSipId().c_str(), mInfo.getIp().c_str(), mInfo.getPort());

      sprintf(to, "sip:%s@%s:%d", dst_channel, client->getIp().c_str(), client->getPort());

0af3b245   Hu Chunming   添加多nvr和多摄像头管理
508
      snprintf(sdp, 2048,

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
509
510
511
512
513
          "v=0\r\n"

          "o=%s 0 0 IN IP4 %s\r\n"

          "s=Play\r\n"

          "c=IN IP4 %s\r\n"

          "t=0 0\r\n"

0af3b245   Hu Chunming   添加多nvr和多摄像头管理
514
          "m=video %d RTP/AVP 96 98 97\r\n"

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
515
          "a=recvonly\r\n"

0af3b245   Hu Chunming   添加多nvr和多摄像头管理
516
          "a=rtpmap:96 PS/90000\r\n"

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
          "a=rtpmap:98 H264/90000\r\n"

          "a=rtpmap:97 MPEG4/90000\r\n"

          "a=setup:passive\r\n"

          "a=connection:new\r\n"

          "y=0100000001\r\n"

          "f=\r\n", mInfo.getSipId().c_str(), mInfo.getIp().c_str(), mInfo.getIp().c_str(), rtpPort);

  

      int ret = eXosip_call_build_initial_invite(mSipCtx, &msg, to, from, nullptr, nullptr);

      if (ret) {

          LOG_ERROR("eXosip_call_build_initial_invite error: {} {} ret:{}", from, to, ret);

          return -1;

      }

  

      osip_message_set_body(msg, sdp, strlen(sdp));

      osip_message_set_content_type(msg, "application/sdp");

      snprintf(session_exp, sizeof(session_exp) - 1, "%i;refresher=uac", mInfo.getTimeout());

      osip_message_set_header(msg, "Session-Expires", session_exp);

      osip_message_set_supported(msg, "timer");

  

      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);

      }

      else {

          LOG_ERROR("eXosip_call_send_initial_invite error: call_id={}", call_id);

      }

64a12438   Hu Chunming   修复返回值错误问题
543
      return call_id;

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
544
545
  }

  

74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
546
  int SipServer::RequestInvite_TCP_a(const char* dst_channel, int rtpPort) {

fe625b06   Hu Chunming   设备状态的检查与定时更新
547
548
549
      // 检查设备是否在线

      if (!check_device_status(dst_channel)) {

          return -2;

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
550
551
      }

  

0af3b245   Hu Chunming   添加多nvr和多摄像头管理
552
553
554
555
      Client* client = get_parent_by_id(dst_channel);

      if (client == nullptr) {

          return -1;

      }

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
556
557
558
559
560
561
562
563
564
565
  

      LOG_INFO("INVITE TCP active");

  

      char session_exp[1024] = { 0 };

      osip_message_t* msg = nullptr;

      char from[1024] = { 0 };

      char to[1024] = { 0 };

      char sdp[2048] = { 0 };

      char head[1024] = { 0 };

  

74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
566
      // const char* dst_channel = "34020000001320000001";

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
  

      sprintf(from, "sip:%s@%s:%d", mInfo.getSipId().c_str(), mInfo.getIp().c_str(), mInfo.getPort());

      sprintf(to, "sip:%s@%s:%d", dst_channel, client->getIp().c_str(), client->getPort());

      snprintf(sdp, 2048,

          "v=0\r\n"

          "o=%s 0 0 IN IP4 %s\r\n"

          "s=Play\r\n"

          "c=IN IP4 %s\r\n"

          "t=0 0\r\n"

          "m=video %d TCP/RTP/AVP 96 98 97\r\n"

          "a=recvonly\r\n"

          "a=rtpmap:96 PS/90000\r\n"

          "a=rtpmap:98 H264/90000\r\n"

          "a=rtpmap:97 MPEG4/90000\r\n"

          "a=setup:active\r\n"

          "a=connection:new\r\n"

          "y=0100000001\r\n"

          "f=\r\n", mInfo.getSipId().c_str(), mInfo.getIp().c_str(), mInfo.getIp().c_str(), rtpPort);

  

      int ret = eXosip_call_build_initial_invite(mSipCtx, &msg, to, from, nullptr, nullptr);

      if (ret) {

          LOG_ERROR("eXosip_call_build_initial_invite error: {} {} ret:{}", from, to, ret);

          return -1;

      }

  

      osip_message_set_body(msg, sdp, strlen(sdp));

      osip_message_set_content_type(msg, "application/sdp");

      snprintf(session_exp, sizeof(session_exp) - 1, "%i;refresher=uac", mInfo.getTimeout());

      osip_message_set_header(msg, "Session-Expires", session_exp);

      osip_message_set_supported(msg, "timer");

  

      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);

      }

      else {

          LOG_ERROR("eXosip_call_send_initial_invite error: call_id={}", call_id);

      }

64a12438   Hu Chunming   修复返回值错误问题
605
      return call_id;

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
606
607
608
  }

  

  void SipServer::cacheCatalog() {

1116447e   Hu Chunming   代码逻辑有花
609
610
611
  

      std::lock_guard<std::mutex> l(m_client_map_mtx);

  

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
      if (mClientMap.size() <= 0){

          cout << "no IPC" << endl;

          return ;

      }

  

      cout << "client size:" << mClientMap.size() << endl;

      for (auto it = mClientMap.begin(); it != mClientMap.end(); it++) {

          RequestCatalog(it->second);

      }

  }

  

  void SipServer::RequestCatalog(Client* client) {

  

      eXosip_lock(mSipCtx);

  

      osip_message_t* catlog_msg = NULL;

      char to[100];/*sip:主叫用户名@被叫IP地址*/

      char from[100];/*sip:被叫IP地址:被叫IP端口*/

      char xml_body[4096];

  

      memset(to, 0, 100);

      memset(from, 0, 100);

      memset(xml_body, 0, 4096);

  

      sprintf(from, "sip:%s@%s:%d", mInfo.getSipId().c_str(), mInfo.getIp().c_str(), mInfo.getPort());

      sprintf(to, "sip:%s@%s:%d", client->getDevice().c_str(), client->getIp().c_str(), client->getPort());

      eXosip_message_build_request(mSipCtx, &catlog_msg, "MESSAGE", to, from, NULL);/*构建"MESSAGE"请求*/

  

      snprintf(xml_body, 4096,

          "<?xml version=\"1.0\"?>"

          "<Query>"

          "<CmdType>Catalog</CmdType>"

          "<SN>%d</SN>"

          "<DeviceID>%s</DeviceID>"

          "</Query>", rand() % (99999 - 10000 + 1) + 10000, client->getDevice().c_str());

  

      osip_message_set_body(catlog_msg, xml_body, strlen(xml_body));

      osip_message_set_content_type(catlog_msg, "Application/MANSCDP+xml");

      eXosip_message_send_request(mSipCtx, catlog_msg);

  

      eXosip_unlock(mSipCtx);

  }

  

  int SipServer::clearClientMap(){

1116447e   Hu Chunming   代码逻辑有花
656
      std::lock_guard<std::mutex> l(m_client_map_mtx);

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
657
658
659
660
661
662
663
664
665
      for (auto iter=mClientMap.begin(); iter!=mClientMap.end(); iter++) {

          delete iter->second;

          iter->second = nullptr;

      }

      mClientMap.clear();

  

      return 0;

  }

  

0af3b245   Hu Chunming   添加多nvr和多摄像头管理
666
  void SipServer::deleteClientByDevice(string device) {

1116447e   Hu Chunming   代码逻辑有花
667
      std::lock_guard<std::mutex> l(m_client_map_mtx);

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
668
669
670
671
672
673
674
675
676
      auto it = mClientMap.find(device);

      if (it == mClientMap.end()) {

          return ;

      }

      delete it->second;

      it->second = nullptr;

      mClientMap.erase(it);

  }

  

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
  int SipServer::parse_xml(const char *data, const char *s_mark, bool with_s_make, const char *e_mark, bool with_e_make, char *dest) {

      const char* satrt = strstr( data, s_mark );

  

      if(satrt != NULL) {

          const char* end = strstr(satrt, e_mark);

  

          if(end != NULL){

              int s_pos = with_s_make ? 0 : strlen(s_mark);

              int e_pos = with_e_make ? strlen(e_mark) : 0;

  

              strncpy( dest, satrt+s_pos, (end+e_pos) - (satrt+s_pos) );

          }

          return 0;

      }

      return -1;

  

  }

  void SipServer::dump_request(eXosip_event_t *evtp) {

      char *s;

      size_t len;

      osip_message_to_str(evtp->request, &s, &len);

      LOG_INFO("\nprint request start\ntype={}\n{}\nprint request end\n",evtp->type,s);

  }

  void SipServer::dump_response(eXosip_event_t *evtp) {

      char *s;

      size_t len;

      osip_message_to_str(evtp->response, &s, &len);

      LOG_INFO("\nprint response start\ntype={}\n{}\nprint response end\n",evtp->type,s);

  }