Blame view

src/decoder/gb28181/rtp/RTPReceiver.cpp 8.79 KB
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
1
2
3
4
5
  #include "RTPReceiver.h"
  #include "rtppacket.h"
  #include <thread>
  
  #include "../common_header.h"
83bf6c73   Hu Chunming   端口范围可配置
6
  #include "../sip/SipServer.h"
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
7
  
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
8
9
10
11
  #ifdef __linux__
  #include "arpa/inet.h"
  #endif
  
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
12
  #define BUFFERSIZE_1024     1024
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
13
14
15
  
  const int kVideoFrameSize = BUFFERSIZE_1024*BUFFERSIZE_1024*5*2;
  
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
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
  // PS解包器回调
  static int ReceivePESFunction(unsigned char streamid, void * data, int size, uint64_t pts, uint64_t localPts, bool key, void* param)
  {
      if (NULL != data && size > 0)
      {
          ((RTPReceiver*)param)->OnPsDemux(streamid, (BYTE*)data, size, key, (uint64_t)pts, (uint64_t)localPts);
      }
      return 0;
  }
  
  static int ps_demuxer_thread_(void* param)
  {
  	if (!param)
  	{
  		return -1;
  	}
  
  	RTPReceiver* self = (RTPReceiver*)param;
  	return self->OnPsProcess();
  }
  
  RTPReceiver::RTPReceiver()
  :m_LastPTS(-1)
  , m_LastIsKeyFrame(0)
  , m_SliceBuf(1024*1024)
  , m_h264DataFunc(NULL)
  , m_hVodEndFunc(NULL)
  , m_usrParam(NULL)
  , m_bPsExit(false)
  , m_psThreadPtr(nullptr)
  {
      m_LastStreamType = 0;
      recvTmpBuf = new BYTE[kVideoFrameSize];
  }
  
  RTPReceiver::~RTPReceiver(){
      if(recvTmpBuf != nullptr){
          delete[] recvTmpBuf;
      }
  }
  
  void RTPReceiver::SetOutputCallback(CallBack_Stream cb, void* param)
  {
  	m_h264DataFunc = cb;
  	m_usrParam = param;
  }
  
  void RTPReceiver::SetVodEndCallback(CallBack_VodFileEnd cb, void* param)
  {
      m_hVodEndFunc = cb;
  	m_usrParam = param;
  }
  
  void RTPReceiver::SetRequestStreamCallback(CallBack_Request_Stream cb){
      m_callback_request_stream = cb;
  }
  
  int RTPReceiver::InitPS(){
      
  	m_psParser.SetReceiveFunction(ReceivePESFunction, this);
  
      m_psThreadPtr = new std::thread(ps_demuxer_thread_, this);
      if(nullptr == m_psThreadPtr){
          return -1;
      }
  
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
82
      LOG_INFO("[{}] InitPS finished", m_SipChannelId);
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
83
84
85
86
87
  
      return 0;
  }
  
  void RTPReceiver::ClosePsThread(){
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
88
      LOG_INFO("[{}] 3.", m_SipChannelId);
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
89
90
91
92
93
94
95
96
97
      m_bPsExit = true;
      // PS解包线程退出
      if (m_psThreadPtr->joinable())
      {
          m_psThreadPtr->join();
          delete m_psThreadPtr;
          m_psThreadPtr = nullptr;
      }
      
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
98
      LOG_INFO("[{}] ps demux thread quit", m_SipChannelId);
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
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
132
133
  }
  
  // 处理去除了PS头的数据
  // 下级厂商发过来的流有可能是MPEG-4/H264/SVAC中的任意一种
  // 国标标准规定, 编码方(下级)可以选择实现H264MPEG4或者SVAC, 但解码方(上级)
  // 必须同时实现对H264MPEG4SVAC的支持
  void RTPReceiver::OnPsDemux(unsigned char streamId, BYTE *data, int len, bool key, uint64_t pts, uint64_t localPts)
  {
  	if (!m_h264DataFunc)
  	{
  		return;
  	}
  
  	if (-1 == m_LastPTS)
  	{
  		if (!key)
  		{
  			return;
  		}
  		m_LastPTS = pts;
  		m_LastIsKeyFrame = key;
  		m_LastStreamType = streamId;
  	}
  
  	// 音频数据不处理
  	if (0xC0 == streamId)
  	{
  		return;
  	}
  	
  	////add by mds 20190424
  	//if (m_notToDecodCount > 50000)//针对大华相机,可能会很久不调用解码
  	//{
  	//	byte_buffer bb(64);
  	//	bb << ERROR_REALSTREAM_INTERRUPT << "This session have a long time no decoding";
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
134
   //   	LOG_INFO("[{}] Long time no decoding!!!m_notToDecodCount=[{}]", m_SipChannelId, m_notToDecodCount);
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
135
136
137
138
139
140
141
142
143
  	//		
  	//	if (m_usrParam)
  	//	{
   //   		if (((VideoSession *)GetUsrParam())->msgChan()->is_valid())
   //       		((VideoSession *)GetUsrParam())->msgChan()->send_msg(bb.data_ptr(), bb.data_size());
  
  	//		//通知网关关闭句柄
  	//		if(!((VideoSession *)GetUsrParam())->streamHandle().empty())
  	//		{
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
144
   //   			LOG_INFO("[{}] ---->Notify hisense gateway release handle = {} !<----", m_SipChannelId, ((VideoSession *)GetUsrParam())->streamHandle());
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  
   //   			if (((VideoSession *)GetUsrParam())->video_type() == EREAL)
   //       			real_stream_stop(((VideoSession *)GetUsrParam())->streamHandle());
   //         
   //   			if (((VideoSession *)GetUsrParam())->video_type() == ERECORD)
   //       			record_stream_stop(((VideoSession *)GetUsrParam())->streamHandle());
   //                                                 
   //   			((VideoSession *)GetUsrParam())->streamHandle().clear();
  	//		}
   //       
  	//		m_hVodEndFunc(m_usrParam);
  	//	}
   //     
  	//	bb.bset(0);
  	//	return;
  	//}
  	
  	if (m_LastPTS != pts)
  	{
5b86d771   Hu Chunming   实现GB28181 UDP 基于d...
164
165
166
167
168
169
170
171
172
173
  		int stream_type = 0;
  		if (VIDEO_TYPE_H264 == streamId) {
  			stream_type = 0;
  		} else if(VIDEO_TYPE_H265 == streamId) {
  			stream_type = 1;
  		} else {
  			LOG_ERROR("[{}] - video type not support!", m_SipChannelId);
  		}
  		
  
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
174
  		m_notToDecodCount = 0;
5b86d771   Hu Chunming   实现GB28181 UDP 基于d...
175
  		m_h264DataFunc(m_usrParam, stream_type, (char *)m_SliceBuf.head(), m_SliceBuf.len(), m_LastIsKeyFrame, m_LastPTS, localPts);
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  
  		m_SliceBuf.reset();
  
  		m_LastPTS = pts;
  		m_LastIsKeyFrame = key;
  		m_LastStreamType = streamId;
  	}
  
  	m_notToDecodCount++;
  	m_SliceBuf.add((char*)data, len);
  }
  
  // PS包线程
  int RTPReceiver::OnPsProcess()
  {
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
191
      LOG_INFO("[{}] started.", m_SipChannelId);
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
192
193
      while (!m_bPsExit) {
  		m_psFrameMutex.lock();
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
194
          // LOG_DEBUG("[{}] PS frame size : {}", m_SipChannelId, m_psVideoFrames.size());
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
195
196
197
198
199
200
201
202
203
204
205
206
207
  		if (m_psVideoFrames.size() <= 0){
  			m_psFrameMutex.unlock();
  			std::this_thread::sleep_for(std::chrono::milliseconds(10));
  			continue;
  		}
          Frame* frame = m_psVideoFrames.front();
  		m_psVideoFrames.pop();
  		m_psFrameMutex.unlock();
  		if (frame != nullptr)
  		{
  			int nRet = m_psParser.AddData(frame->buf_, frame->len_);
  			if (nRet == -1)
  			{
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
208
  				LOG_INFO("m_psParser return -1--{}", m_SipChannelId);
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
209
210
211
  			}
  			else if (nRet == -2)
  			{
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
212
  				LOG_INFO("m_psParser return -2--{}", m_SipChannelId);
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
213
214
215
  			}
  			else if (nRet == -3)
  			{
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
216
  				LOG_INFO("m_psParser return -3--{}", m_SipChannelId);
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
217
218
219
220
221
222
223
224
225
226
227
  			}
  
  			delete frame;
  			frame = nullptr;
  		}
      }
  
  	ClearPsVideoFrameList();
  
      m_hVodEndFunc(m_usrParam);
  
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
228
  	LOG_INFO("[{}] exited.", m_SipChannelId);
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
229
230
231
232
  
  	return 0;
  }
  
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  int RTPReceiver::GetPsFrameListSize()
  {
  	std::lock_guard<std::mutex> l(m_psFrameMutex);
  	return m_psVideoFrames.size();
  }
  
  void RTPReceiver::ClearPsVideoFrameList()
  {
  	std::lock_guard<std::mutex> l(m_psFrameMutex);
  	while (!m_psVideoFrames.empty()) {
  		Frame* f = m_psVideoFrames.front();
  		delete f;
  		m_psVideoFrames.pop();
  	}
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
247
      LOG_INFO("[{}] cleared ps video frame list!", m_SipChannelId);
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  }
  
  int RTPReceiver::ParsePacket(RTPPacket* packet){
      do {
  
          if (0 == packet->GetPayloadType()) 
          {
                  // 音频数据, 暂不处理
              break; 
          }
  
          // 判断是否收到完整的帧(有些厂商打的marker标记不准, 所以只能看时间戳来判断)
          uint32_t curPts = packet->GetTimestamp();
          if (lastPts != 0 && curPts != lastPts) {
              mark = 1;
          }
          lastPts = curPts;
  
          int payloadLen = packet->GetPayloadLength();
          if (offset + payloadLen > kVideoFrameSize)
          {
              offset = 0, mark = 0;
              break;
          }
  
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
273
          // LOG_DEBUG("[{}] ParsePacket GetPayloadLength", m_SipChannelId);
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
274
275
276
277
278
279
280
281
282
283
284
285
286
  
          if (mark)
          {
              BYTE* frameBuf = (BYTE*)malloc(sizeof(BYTE) * offset);
              if (!frameBuf) {
                  offset = 0, mark = 0;
                  break; 
              }
              memcpy(frameBuf, recvTmpBuf, offset);
              if (!m_bPsExit){
                  std::lock_guard<std::mutex> l(m_psFrameMutex);
                  if (m_psVideoFrames.size() < 100)
                  {
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
287
                      // LOG_DEBUG("[{}]ParsePacket push", m_SipChannelId);
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
288
289
290
291
292
293
294
295
296
                      m_psVideoFrames.push(new Frame(frameBuf, offset, false));
                  }
                  else {
                      free(frameBuf);
                  }
              }
              else{
                  //若此时解码线程已经退出,不再往m_psVideoFrames推送帧,且退出当前线程
                  free(frameBuf);
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
297
                  LOG_INFO("ParsePacket quit, device_id:{}", m_SipChannelId);
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
298
299
300
301
302
303
304
305
306
307
308
                  return 1;
              }
              offset = 0;
              mark = 0;
          }
  
          memcpy(recvTmpBuf + offset, packet->GetPayloadData(), payloadLen);
          offset += payloadLen;
      } while (0);
  
      return 0;
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
309
310
311
312
  }
  
  int RTPReceiver::allocRtpPort() {
  
83bf6c73   Hu Chunming   端口范围可配置
313
314
315
316
  	SipServer* pServer = SipServer::getInstance();
  	int MIN_RTP_PORT = pServer->GetMinRtpPort() ;
  	int MAX_RTP_PORT = pServer->GetMaxRtpPort();
  
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
317
318
319
320
321
322
323
324
  	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;
  
5b86d771   Hu Chunming   实现GB28181 UDP 基于d...
325
326
  	int count = 0;
  
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
327
328
  	while (true)
  	{
5b86d771   Hu Chunming   实现GB28181 UDP 基于d...
329
330
331
332
333
334
335
  		if (s_rtpPort >= MAX_RTP_PORT) {
  			s_rtpPort = MIN_RTP_PORT;
  			count ++;
  			if (count > 1) {
  				LOG_ERROR("[{}] - 10000 到 60000 没有可用的port", m_SipChannelId);
  			}
  		}
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
336
  
5b86d771   Hu Chunming   实现GB28181 UDP 基于d...
337
338
  		int i = 0;
  		for (; i < 2; i++) {
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  			sockaddr_in sRecvAddr;
  			int s = socket(AF_INET, SOCK_DGRAM, 0);
  
  			sRecvAddr.sin_family = AF_INET;        
  			sRecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);    
  			sRecvAddr.sin_port = htons(s_rtpPort + i); 
  
  			int nResult = bind(s, (sockaddr *)&sRecvAddr, sizeof(sRecvAddr));
  			if (nResult != 0) {
  				break;
  			}
  
  			nResult = close(s);
  			if (nResult != 0) {
  				LOG_ERROR("[{}] - closesocket failed : {}", m_SipChannelId, nResult);
5b86d771   Hu Chunming   实现GB28181 UDP 基于d...
354
  				break;
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
355
356
357
  			}
  		}
  
5b86d771   Hu Chunming   实现GB28181 UDP 基于d...
358
359
360
  		if (i == 2)
  			break;
  
74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
361
362
363
364
  		s_rtpPort += 2;
  	}
  
  	return s_rtpPort;
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
365
  }