c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
1
2
3
|
#include"RTPTcpReceiver.h"
#include "../common_header.h"
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
4
|
#include "../sip/SipServer.h"
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
64
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
// class TcpRTPSession : public RTPSession
// {
// public:
// void setReceiver(RTPTcpReceiver* r){
// tcpReceiver = r;
// }
// protected:
// void OnValidatedRTPPacket(RTPSourceData *srcdat, RTPPacket *rtppack, bool isonprobation, bool *ispackethandled)
// {
// // printf("SSRC %x Got packet (%d bytes) in OnValidatedRTPPacket from source 0x%04x!\n", GetLocalSSRC(),
// // (int)rtppack->GetPayloadLength(), srcdat->GetSSRC());
// LOG_DEBUG("SSRC {} Got packet ({} bytes) in OnValidatedRTPPacket from source {}}!\n", GetLocalSSRC(),
// (int)rtppack->GetPayloadLength(), srcdat->GetSSRC());
// if(nullptr != tcpReceiver){
// tcpReceiver->ParsePacket(rtppack);
// }
// DeletePacket(rtppack);
// *ispackethandled = true;
// }
// void OnRTCPSDESItem(RTPSourceData *srcdat, RTCPSDESPacket::ItemType t, const void *itemdata, size_t itemlength)
// {
// char msg[1024];
// memset(msg, 0, sizeof(msg));
// if (itemlength >= sizeof(msg))
// itemlength = sizeof(msg)-1;
// memcpy(msg, itemdata, itemlength);
// // printf("SSRC %x Received SDES item (%d): %s from SSRC %x\n", GetLocalSSRC(), (int)t, msg, srcdat->GetSSRC());
// LOG_DEBUG("SSRC {} Received SDES item ({}): {} from SSRC {}\n", GetLocalSSRC(), (int)t, msg, srcdat->GetSSRC());
// }
// private:
// RTPTcpReceiver* tcpReceiver{nullptr};
// };
class MyTCPTransmitter : public RTPTCPTransmitter
{
public:
void setReceiver(RTPTcpReceiver* r){
tcpReceiver = r;
}
public:
MyTCPTransmitter() : RTPTCPTransmitter(0){ }
void OnSendError(SocketType sock)
{
LOG_ERROR("Error sending over socket {}, removing destination", sock);
DeleteDestination(RTPTCPAddress(sock));
if(nullptr != tcpReceiver && !tcpReceiver->isClosing()){
tcpReceiver->ReConnect();
}
}
void OnReceiveError(SocketType sock)
{
LOG_ERROR("Error receiving over socket {}, removing destination", sock);
DeleteDestination(RTPTCPAddress(sock));
}
private:
RTPTcpReceiver* tcpReceiver{nullptr};
};
static int rtp_revc_thread_(void* param)
{
if (!param)
{
return -1;
}
RTPTcpReceiver* self = (RTPTcpReceiver*)param;
return self->OnRtpRecv();
}
static int listen_finish_thread_(void* param)
{
if (!param)
{
return -1;
}
RTPTcpReceiver* self = (RTPTcpReceiver*)param;
return self->ListenFinish();
}
RTPTcpReceiver::RTPTcpReceiver()
: m_bRtpExit(false)
, m_bOpened(false)
, m_idleCount(-1)
, m_noDataCount(-1)
, m_nListener(-1)
, m_bAccepted(false)
, m_bClosing(false)
{
m_rtpSessionPtr = new RTPSession();
m_pSessparams = new RTPSessionParams();
m_pTrans = new MyTCPTransmitter();
}
RTPTcpReceiver::~RTPTcpReceiver(){
if (IsOpened())
Close();
if(m_rtpSessionPtr != nullptr){
delete m_rtpSessionPtr;
m_rtpSessionPtr = nullptr;
}
if(m_pSessparams != nullptr){
delete m_pSessparams;
m_pSessparams = nullptr;
}
if(m_pTrans != nullptr){
delete m_pTrans;
m_pTrans = nullptr;
}
}
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
132
133
134
135
136
137
138
139
140
141
142
|
bool RTPTcpReceiver::Open(string channel_id){
m_SipChannelId = channel_id;
int rtpPort = allocRtpPort();
if (rtpPort < 0) {
return false;
}
m_rtp_port = rtpPort;
if(0 != initSession(m_rtp_port)){
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
143
144
145
146
147
|
return false;
}
m_bOpened = true;
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
148
|
LOG_INFO("[{}] started.", m_SipChannelId);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
149
150
151
152
153
|
return true;
}
bool RTPTcpReceiver::IsOpened(){
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
154
|
LOG_INFO("[{}] isopng:{} ", m_SipChannelId, m_bOpened);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
return m_bOpened;
}
void RTPTcpReceiver::Close(){
m_bRtpExit = true;
if(m_listenFinishThread.joinable()){
m_listenFinishThread.join();
}
}
void RTPTcpReceiver::close_task(){
m_bRtpExit = true;
m_bClosing = true;
m_bAccepted = true;
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
173
|
LOG_DEBUG("[{}] 1.", m_SipChannelId);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
174
175
176
177
178
179
180
|
// rtp接收线程退出
if (m_rtpThread.joinable())
{
m_rtpThread.join();
}
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
181
|
LOG_DEBUG("[{}] 2.", m_SipChannelId);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
182
183
184
185
186
|
ClosePsThread();
m_bOpened = false;
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
187
|
LOG_INFO("[{}] closed.", m_SipChannelId);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
}
bool RTPTcpReceiver::isClosing(){
return m_bClosing;
}
int RTPTcpReceiver::initSession(int localPort){
m_nListener = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
if (m_nListener < 0)
{
return -1;
}
sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(sockaddr_in));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(localPort);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
int nRet = bind(m_nListener, (sockaddr*)&serverAddr, sizeof(serverAddr));
if (nRet == -1)
{
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
209
|
LOG_ERROR("[{}] 绑定端口失败: {}", m_SipChannelId, localPort);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
210
211
212
213
214
|
return -1;
}
if (listen(m_nListener, 1) == -1)
{
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
215
|
LOG_ERROR("[{}] listen 失败", m_SipChannelId);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
return -1;
}
int nPackSize = 45678;
m_pSessparams->SetProbationType(RTPSources::NoProbation);
m_pSessparams->SetOwnTimestampUnit(90000.0 / 25.0);
m_pSessparams->SetMaximumPacketSize(nPackSize + 64);
int status = m_pTrans->Init(false);
status = m_pTrans->Create(65535, NULL);
m_pTrans->setReceiver(this);
status = m_rtpSessionPtr->Create(*m_pSessparams, m_pTrans);
if (status < 0)
{
// 若status = -59 ,需运行 export LOGNAME=root ,见 https://blog.csdn.net/m0_37876242/article/details/128588162
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
232
|
LOG_ERROR("[{}] create session error: {}", m_SipChannelId, status);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
233
234
235
|
return -1;
}
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
236
237
238
239
240
|
m_rtpThread = std::thread(rtp_revc_thread_, this);
m_listenFinishThread = std::thread(listen_finish_thread_, this);
InitPS();
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
241
242
243
244
245
246
|
bool bRet = RequestStream();
if (!bRet)
{
LOG_INFO("[{}] 请求流失败!", m_SipChannelId);
return -1;
}
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
247
|
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
248
|
LOG_INFO("[{}] 初始化成功, congratulations !!!", m_SipChannelId);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
249
250
251
252
253
254
255
256
257
258
|
return 0;
}
int RTPTcpReceiver::OnRtpRecv()
{
if(nullptr == m_rtpSessionPtr){
return -1;
}
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
259
|
LOG_INFO("[{}] OnRtpRecv started, m_nListener : {}", m_SipChannelId, m_nListener);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
260
261
262
263
264
|
sockaddr_in clientAddr;
int nLen = sizeof(sockaddr_in);
SocketType nServer = -1;
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
265
|
LOG_INFO("[{}] Poll started.", m_SipChannelId);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
266
267
268
269
270
271
272
273
274
|
int reconn_times = 0;
int reaccept_times = 0;
bool bReconn = false;
while(!m_bRtpExit){
while(!m_bAccepted){
if(m_bRtpExit){
goto end_flag;
}
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
while (!bReconn){
if(m_bRtpExit){
goto end_flag;
}
reconn_times++;
if(reconn_times > 10){
// 10次请求都失败,结束任务
m_bRtpExit = true;
goto end_flag;
}
LOG_DEBUG("[{}] RequestStream...", m_SipChannelId);
bReconn = RequestStream();
if (bReconn){
LOG_DEBUG("[{}] RequestStream, True", m_SipChannelId);
continue;
}
LOG_DEBUG("[{}] RequestStream, False", m_SipChannelId);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
293
|
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
294
295
|
std::this_thread::sleep_for(std::chrono::seconds(5));
}
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
296
|
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
297
|
LOG_DEBUG("[{}] accepting...", m_SipChannelId);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
298
299
300
|
nServer = accept(m_nListener, (sockaddr*)&clientAddr, (socklen_t * ) &nLen);
if (-1 == nServer){
reaccept_times++;
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
301
|
LOG_DEBUG("[{}] reaccept_times = {}", m_SipChannelId, reaccept_times);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
302
|
if(reaccept_times > 600){
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
303
|
LOG_DEBUG("[{}] reaccept_times > 600", m_SipChannelId);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
304
305
306
307
308
309
|
bReconn = false;
reaccept_times = 0;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
continue;
}
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
310
|
LOG_DEBUG("[{}] accept success", m_SipChannelId);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
311
312
313
314
315
316
|
m_rtpSessionPtr->AddDestination(RTPTCPAddress(nServer));
m_bAccepted = true;
bReconn = false;
reconn_times = 0;
reaccept_times = 0;
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
317
|
LOG_INFO("[{}] nServer={}", m_SipChannelId, nServer);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
318
319
320
321
322
323
324
325
326
327
328
329
|
break;
}
m_rtpSessionPtr->BeginDataAccess();
if (m_rtpSessionPtr->GotoFirstSourceWithData())
{
do
{
RTPPacket *pack;
while ((pack = m_rtpSessionPtr->GetNextPacket()) != NULL)
{
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
330
|
// LOG_DEBUG("[{}] time: {} ", m_SipChannelId, UtilTools::get_cur_time_ms());
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
ParsePacket(pack);
m_rtpSessionPtr->DeletePacket(pack);
}
} while (m_rtpSessionPtr->GotoNextSourceWithData());
}
m_rtpSessionPtr->EndDataAccess();
m_rtpSessionPtr->Poll();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
end_flag:
m_rtpSessionPtr->Destroy();
if(nServer > 0){
close(nServer);
}
if(m_nListener > 0){
close(m_nListener);
}
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
355
|
LOG_INFO("[{}] OnRtpRecv exited.", m_SipChannelId);
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
return 0;
}
int RTPTcpReceiver::ListenFinish(){
while(!m_bRtpExit){
std::this_thread::sleep_for(std::chrono::seconds(3));
}
close_task();
}
bool RTPTcpReceiver::ReConnect(){
m_bAccepted = false;
}
bool RTPTcpReceiver::RequestStream(){
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
373
374
375
|
SipServer* pServer = SipServer::getInstance();
int ret = -1;
if (pServer){
|
5b86d771
Hu Chunming
实现GB28181 UDP 基于d...
|
376
|
ret = pServer->RequestInvite_TCP_a(m_SipChannelId.c_str(), m_rtp_port);
|
74d1e6a8
Hu Chunming
完成gb28181大体的代码,未完...
|
377
378
379
|
}
return (ret > 0) ;
|
c8285c8d
Hu Chunming
GB28181 UDP 有重大进展...
|
380
|
}
|