Commit 7489c304decdbf74a9fe4acfbf53ab354bbd0bab

Authored by Hu Chunming
1 parent 83bf6c73

更新demo

src/decoder/gb28181/main.cpp.28181
... ... @@ -26,8 +26,6 @@
26 26 using namespace std;
27 27  
28 28  
29   -SipServer sipServer;
30   -
31 29 static void RTP_Stream_CallBack(void* userdata, int videoType, char* data, int len, int isKey, uint64_t pts, uint64_t localPts)
32 30 {
33 31 LOG_INFO("len: {}", len);
... ... @@ -47,23 +45,28 @@ bool RequestStream(const char* deviceId, int rtp_port);
47 45 // ȡ MIN_RTP_PORT(10000)~MAX_RTP_PORT(60000)֮�������˿�(ż���������������˿ڿ���)
48 46 int allocRtpPort() {
49 47  
50   - static int s_rtpPort = MIN_RTP_PORT;
51   - // if (MIN_RTP_PORT == s_rtpPort)
52   - // {
53   - // srand((unsigned int)time(NULL));
54   - // s_rtpPort = MIN_RTP_PORT + (rand() % MIN_RTP_PORT);
55   - // }
  48 + int s_rtpPort = MIN_RTP_PORT;
  49 +
  50 + srand((unsigned int)time(NULL));
  51 + s_rtpPort = MIN_RTP_PORT + (rand() % MIN_RTP_PORT);
  52 +
  53 + if (s_rtpPort % 2)
  54 + ++s_rtpPort;
56 55  
57   - // if (s_rtpPort % 2)
58   - // ++s_rtpPort;
  56 + int count = 0;
59 57  
60 58 while (true)
61 59 {
62   - s_rtpPort = s_rtpPort >= MAX_RTP_PORT ? MIN_RTP_PORT : s_rtpPort;
  60 + if (s_rtpPort >= MAX_RTP_PORT) {
  61 + s_rtpPort = MIN_RTP_PORT;
  62 + count ++;
  63 + if (count > 1) {
  64 + printf("10000 到 60000 没有可用的port");
  65 + }
  66 + }
63 67  
64 68 int i = 0;
65   - for (; i < 2; i++)
66   - {
  69 + for (; i < 2; i++) {
67 70 sockaddr_in sRecvAddr;
68 71 int s = socket(AF_INET, SOCK_DGRAM, 0);
69 72  
... ... @@ -72,15 +75,13 @@ int allocRtpPort() {
72 75 sRecvAddr.sin_port = htons(s_rtpPort + i);
73 76  
74 77 int nResult = bind(s, (sockaddr *)&sRecvAddr, sizeof(sRecvAddr));
75   - if (nResult != 0)
76   - {
  78 + if (nResult != 0) {
77 79 break;
78 80 }
79 81  
80 82 nResult = close(s);
81   - if (nResult != 0)
82   - {
83   - printf("closesocket failed:%d\n", nResult);
  83 + if (nResult != 0) {
  84 + printf("closesocket failed : {}", nResult);
84 85 break;
85 86 }
86 87 }
... ... @@ -130,15 +131,8 @@ bool start_rtp(string deviceId, int m_port) {
130 131  
131 132  
132 133 bool RequestStream(const char* deviceId, int rtp_port) {
133   -
134   - std::vector< DeviceInfo > vec_device = sipServer.GetDeviceList();
135   - if (vec_device.size() <= 0)
136   - {
137   - cout << "no device" << endl;
138   - return false;
139   - }
140   -
141   - int ret = sipServer.RequestInvite_TCP_a(vec_device[0].id.c_str(), rtp_port);
  134 + SipServer* pServer = SipServer::getInstance();
  135 + int ret = pServer->RequestInvite_UDP(deviceId, rtp_port);
142 136 if (ret > 0)
143 137 {
144 138 return true;
... ... @@ -149,46 +143,30 @@ bool RequestStream(const char* deviceId, int rtp_port) {
149 143  
150 144 int main(int argc, char *argv[]) {
151 145  
152   - ServerInfo info(
153   - "SY_Sip_Server",
154   - "12345678",
155   - "192.168.60.179",
156   - 15060,
157   - "34020000002000000002",
158   - "3402000000",
159   - "sy123456",
160   - 1800,
161   - 3600);
162   -
163   -
164   - sipServer.Init(info);
165   -
166   - std::this_thread::sleep_for(std::chrono::seconds(5));
  146 + if (argc <= 1)
  147 + {
  148 + printf("请输入待获取的摄像头的视频通道编码ID。");
  149 + return -1;
  150 + }
167 151  
168   - sipServer.cacheCatalog();
  152 + SipServer* pServer = SipServer::getInstance();
  153 + pServer->Init(nullptr);
169 154  
170   - std::vector< DeviceInfo > vec_device;
  155 + // const char* sipid = "34020000001310000001";
  156 + const char* sipid = argv[1];
171 157 char oper = 'g';
172 158 while (oper != 'q') {
173 159 oper = getchar();
174 160 switch (oper)
175 161 {
176 162 case 'g':
177   - sipServer.cacheCatalog();
178 163 break;
179 164 case 'i':
180   - vec_device = sipServer.GetDeviceList();
181   - if (vec_device.size() <= 0)
182   - {
183   - cout << "no device" << endl;
184   - break;
185   - }
186   -
187 165 {
188   - int rtp_port = 30026;//allocRtpPort();
189   - start_rtp(vec_device[0].id, rtp_port);
  166 + int rtp_port = allocRtpPort();
  167 + start_rtp(sipid, rtp_port);
190 168  
191   - sipServer.RequestInvite_UDP(vec_device[0].id.c_str(), rtp_port);
  169 + pServer->RequestInvite_UDP(sipid, rtp_port);
192 170 }
193 171 break;
194 172 case 'b':
... ...