From 7489c304decdbf74a9fe4acfbf53ab354bbd0bab Mon Sep 17 00:00:00 2001 From: cmhu <2657262686@qq.com> Date: Fri, 12 Apr 2024 17:07:10 +0800 Subject: [PATCH] 更新demo --- src/decoder/gb28181/main.cpp.28181 | 88 +++++++++++++++++++++++++++++++++------------------------------------------------------- 1 file changed, 33 insertions(+), 55 deletions(-) diff --git a/src/decoder/gb28181/main.cpp.28181 b/src/decoder/gb28181/main.cpp.28181 index de6ea1c..c64a263 100644 --- a/src/decoder/gb28181/main.cpp.28181 +++ b/src/decoder/gb28181/main.cpp.28181 @@ -26,8 +26,6 @@ using namespace std; -SipServer sipServer; - static void RTP_Stream_CallBack(void* userdata, int videoType, char* data, int len, int isKey, uint64_t pts, uint64_t localPts) { LOG_INFO("len: {}", len); @@ -47,23 +45,28 @@ bool RequestStream(const char* deviceId, int rtp_port); // ȡ MIN_RTP_PORT(10000)~MAX_RTP_PORT(60000)֮�������˿�(ż���������������˿ڿ���) int allocRtpPort() { - static int s_rtpPort = MIN_RTP_PORT; - // if (MIN_RTP_PORT == s_rtpPort) - // { - // srand((unsigned int)time(NULL)); - // s_rtpPort = MIN_RTP_PORT + (rand() % MIN_RTP_PORT); - // } + int s_rtpPort = MIN_RTP_PORT; + + srand((unsigned int)time(NULL)); + s_rtpPort = MIN_RTP_PORT + (rand() % MIN_RTP_PORT); + + if (s_rtpPort % 2) + ++s_rtpPort; - // if (s_rtpPort % 2) - // ++s_rtpPort; + int count = 0; while (true) { - s_rtpPort = s_rtpPort >= MAX_RTP_PORT ? MIN_RTP_PORT : s_rtpPort; + if (s_rtpPort >= MAX_RTP_PORT) { + s_rtpPort = MIN_RTP_PORT; + count ++; + if (count > 1) { + printf("10000 到 60000 没有可用的port"); + } + } int i = 0; - for (; i < 2; i++) - { + for (; i < 2; i++) { sockaddr_in sRecvAddr; int s = socket(AF_INET, SOCK_DGRAM, 0); @@ -72,15 +75,13 @@ int allocRtpPort() { sRecvAddr.sin_port = htons(s_rtpPort + i); int nResult = bind(s, (sockaddr *)&sRecvAddr, sizeof(sRecvAddr)); - if (nResult != 0) - { + if (nResult != 0) { break; } nResult = close(s); - if (nResult != 0) - { - printf("closesocket failed:%d\n", nResult); + if (nResult != 0) { + printf("closesocket failed : {}", nResult); break; } } @@ -130,15 +131,8 @@ bool start_rtp(string deviceId, int m_port) { bool RequestStream(const char* deviceId, int rtp_port) { - - std::vector< DeviceInfo > vec_device = sipServer.GetDeviceList(); - if (vec_device.size() <= 0) - { - cout << "no device" << endl; - return false; - } - - int ret = sipServer.RequestInvite_TCP_a(vec_device[0].id.c_str(), rtp_port); + SipServer* pServer = SipServer::getInstance(); + int ret = pServer->RequestInvite_UDP(deviceId, rtp_port); if (ret > 0) { return true; @@ -149,46 +143,30 @@ bool RequestStream(const char* deviceId, int rtp_port) { int main(int argc, char *argv[]) { - ServerInfo info( - "SY_Sip_Server", - "12345678", - "192.168.60.179", - 15060, - "34020000002000000002", - "3402000000", - "sy123456", - 1800, - 3600); - - - sipServer.Init(info); - - std::this_thread::sleep_for(std::chrono::seconds(5)); + if (argc <= 1) + { + printf("请输入待获取的摄像头的视频通道编码ID。"); + return -1; + } - sipServer.cacheCatalog(); + SipServer* pServer = SipServer::getInstance(); + pServer->Init(nullptr); - std::vector< DeviceInfo > vec_device; + // const char* sipid = "34020000001310000001"; + const char* sipid = argv[1]; char oper = 'g'; while (oper != 'q') { oper = getchar(); switch (oper) { case 'g': - sipServer.cacheCatalog(); break; case 'i': - vec_device = sipServer.GetDeviceList(); - if (vec_device.size() <= 0) - { - cout << "no device" << endl; - break; - } - { - int rtp_port = 30026;//allocRtpPort(); - start_rtp(vec_device[0].id, rtp_port); + int rtp_port = allocRtpPort(); + start_rtp(sipid, rtp_port); - sipServer.RequestInvite_UDP(vec_device[0].id.c_str(), rtp_port); + pServer->RequestInvite_UDP(sipid, rtp_port); } break; case 'b': -- libgit2 0.21.4