Commit 3c9776e9faad1b5d6bcdbe4b7933f99c9427faee

Authored by Hu Chunming
1 parent 579b2613

代码优化

src/decoder/dvpp/DvppRtpDecoder.cpp
1 #include "DvppRtpDecoder.h" 1 #include "DvppRtpDecoder.h"
2 2
3 #include "DvppSourceManager.h" 3 #include "DvppSourceManager.h"
4 -#include "../gb28181/rtp2/Rtp.h"  
5 4
6 5
7 #define CHECK_AND_RETURN(ret, message) \ 6 #define CHECK_AND_RETURN(ret, message) \
@@ -365,9 +364,9 @@ void DvppRtpDecoder::release_ffmpeg() { @@ -365,9 +364,9 @@ void DvppRtpDecoder::release_ffmpeg() {
365 } 364 }
366 365
367 void DvppRtpDecoder::CacheBuffer(uint8_t* recvBuf, int recvBufSize) { 366 void DvppRtpDecoder::CacheBuffer(uint8_t* recvBuf, int recvBufSize) {
368 - if ((m_bufferSize + recvBufSize - RTP_HEADER_SIZE) < MAX_RTP_BUFFER_SIZE) {  
369 - memcpy(m_buffer + m_bufferSize, recvBuf + RTP_HEADER_SIZE, recvBufSize - RTP_HEADER_SIZE);  
370 - m_bufferSize += recvBufSize - RTP_HEADER_SIZE; 367 + if ((m_bufferSize + recvBufSize) < MAX_RTP_BUFFER_SIZE) {
  368 + memcpy(m_buffer + m_bufferSize, recvBuf, recvBufSize);
  369 + m_bufferSize += recvBufSize;
371 } else { 370 } else {
372 LOG_WARN("recvBufSize = {} over MAX_RTP_BUFFER_SIZE ", recvBufSize); 371 LOG_WARN("recvBufSize = {} over MAX_RTP_BUFFER_SIZE ", recvBufSize);
373 } 372 }
@@ -402,8 +401,8 @@ bool DvppRtpDecoder::probe() { @@ -402,8 +401,8 @@ bool DvppRtpDecoder::probe() {
402 m_buffer_waiting_time = 3000; //probe只等待30s,避免卡主任务添加 401 m_buffer_waiting_time = 3000; //probe只等待30s,避免卡主任务添加
403 402
404 // todo: 此处可能有泄露 403 // todo: 此处可能有泄露
405 - unsigned char* avioBuff = (unsigned char*)av_malloc(MAX_RTP_BUFFER_SIZE);  
406 - AVIOContext *ioCtx = avio_alloc_context(avioBuff, sizeof(avioBuff), 0, this, avio_read_packet, NULL, NULL); 404 + unsigned char* avioBuff = (unsigned char*)av_malloc(2048); // 32768 < MAX_RTP_BUFFER_SIZE - RTP_HEADER_SIZE
  405 + AVIOContext *ioCtx = avio_alloc_context(avioBuff, 2048, 0, this, avio_read_packet, NULL, NULL);
407 406
408 do{ 407 do{
409 fmt_ctx = avformat_alloc_context(); 408 fmt_ctx = avformat_alloc_context();
@@ -577,6 +576,10 @@ void DvppRtpDecoder::read_thread() { @@ -577,6 +576,10 @@ void DvppRtpDecoder::read_thread() {
577 continue; 576 continue;
578 } 577 }
579 578
  579 + // av_packet_free(&pkt);
  580 + // pkt = nullptr;
  581 + // continue;
  582 +
580 if (m_DvppCacheCounter.load() > m_cache_gop){ 583 if (m_DvppCacheCounter.load() > m_cache_gop){
581 // 解码器解码不过来。实时流在此处的处理会导致花屏,这是由于解码器性能问题导致,无法避免 584 // 解码器解码不过来。实时流在此处的处理会导致花屏,这是由于解码器性能问题导致,无法避免
582 // 实时流在这里处理是为了避免长时间不读取数据导致数据中断 585 // 实时流在这里处理是为了避免长时间不读取数据导致数据中断
src/decoder/dvpp/DvppRtpDecoder.h
@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 #include <queue> 7 #include <queue>
8 #include <mutex> 8 #include <mutex>
9 #include <chrono> 9 #include <chrono>
10 -#include<string> 10 +#include <string>
11 11
12 #include "depend_headers.h" 12 #include "depend_headers.h"
13 #include "dvpp_headers.h" 13 #include "dvpp_headers.h"
@@ -21,6 +21,7 @@ using namespace std; @@ -21,6 +21,7 @@ using namespace std;
21 21
22 #define MAX_RTP_BUFFER_SIZE 4194304 // 4M = 4 * 1024 * 1024 = 4194304 字节 22 #define MAX_RTP_BUFFER_SIZE 4194304 // 4M = 4 * 1024 * 1024 = 4194304 字节
23 23
  24 +
24 typedef void(*CallBack_DecodeFinished)(void* userdata); 25 typedef void(*CallBack_DecodeFinished)(void* userdata);
25 26
26 class DvppRtpDecoder 27 class DvppRtpDecoder
@@ -30,10 +31,6 @@ public: @@ -30,10 +31,6 @@ public:
30 ~DvppRtpDecoder(); 31 ~DvppRtpDecoder();
31 32
32 public: 33 public:
33 - std::atomic<char> m_buffer[MAX_RTP_BUFFER_SIZE];  
34 - std::atomic_int m_bufferSize {0};  
35 -  
36 -public:  
37 bool Init(FFDecConfig cfg); 34 bool Init(FFDecConfig cfg);
38 void Close(); 35 void Close();
39 bool start(); 36 bool start();
@@ -160,5 +157,8 @@ private: @@ -160,5 +157,8 @@ private:
160 157
161 int m_buffer_waiting_time{3000} ; 158 int m_buffer_waiting_time{3000} ;
162 159
  160 + std::atomic<char> m_buffer[MAX_RTP_BUFFER_SIZE];
  161 + std::atomic_int m_bufferSize {0};
  162 +
163 }; 163 };
164 #endif //__DVPP_RTP_DECODER_H__ 164 #endif //__DVPP_RTP_DECODER_H__
165 \ No newline at end of file 165 \ No newline at end of file
src/decoder/gb28181/rtp2/RTPReceiver2.cpp
@@ -170,7 +170,7 @@ int RTPReceiver2::udp_server() { @@ -170,7 +170,7 @@ int RTPReceiver2::udp_server() {
170 last_time = get_cur_time_second(); 170 last_time = get_cur_time_second();
171 171
172 // buffer 抛出 172 // buffer 抛出
173 - m_buffer_cbk(m_bufferParam, recvBuf, recvBufSize, 0); 173 + m_buffer_cbk(m_bufferParam, recvBuf + RTP_HEADER_SIZE, recvBufSize - RTP_HEADER_SIZE, 0);
174 } 174 }
175 175
176 close(server_fd); 176 close(server_fd);
@@ -220,12 +220,11 @@ int RTPReceiver2::tcp_server() { @@ -220,12 +220,11 @@ int RTPReceiver2::tcp_server() {
220 220
221 long long last_time = get_cur_time_second(); 221 long long last_time = get_cur_time_second();
222 222
  223 + socklen_t len = sizeof(sockaddr);
  224 + sockaddr_in accept_addr;
223 while (!m_bRtpExit) 225 while (!m_bRtpExit)
224 { 226 {
225 - // LOG_INFO("阻塞监听新连接...");  
226 - // 阻塞接收请求 start  
227 - socklen_t len = sizeof(sockaddr);  
228 - sockaddr_in accept_addr; 227 + // 非阻塞接收请求 start
229 int clientFd = accept(listenfd, (struct sockaddr*)&accept_addr, &len); 228 int clientFd = accept(listenfd, (struct sockaddr*)&accept_addr, &len);
230 if (clientFd < 0) { 229 if (clientFd < 0) {
231 if (!bFilter) { 230 if (!bFilter) {
@@ -310,7 +309,7 @@ void RTPReceiver2::parseTcpData(uint8_t* recvBuf, int recvBufSize) { @@ -310,7 +309,7 @@ void RTPReceiver2::parseTcpData(uint8_t* recvBuf, int recvBufSize) {
310 // mRecvCacheSize,rtpHeader.marker, rtpHeader.timestamp); 309 // mRecvCacheSize,rtpHeader.marker, rtpHeader.timestamp);
311 310
312 // buffer 抛出 311 // buffer 抛出
313 - m_buffer_cbk(m_bufferParam, mRecvRtpBuffer, mRecvRtpBufferSize, rtpHeader.timestamp); 312 + m_buffer_cbk(m_bufferParam, mRecvRtpBuffer + RTP_HEADER_SIZE, mRecvRtpBufferSize - RTP_HEADER_SIZE, rtpHeader.timestamp);
314 313
315 } else { 314 } else {
316 //LOGI("跳出解析:cacheSize=%d,pktSize=%d", cacheSize, pktSize); 315 //LOGI("跳出解析:cacheSize=%d,pktSize=%d", cacheSize, pktSize);
src/decoder/gb28181/rtp2/Rtp.cpp
@@ -23,6 +23,7 @@ void rtpHeaderInit(struct RtpPacket* rtpPacket, uint8_t csrcLen, uint8_t extensi @@ -23,6 +23,7 @@ void rtpHeaderInit(struct RtpPacket* rtpPacket, uint8_t csrcLen, uint8_t extensi
23 rtpPacket->rtpHeader.timestamp = timestamp; 23 rtpPacket->rtpHeader.timestamp = timestamp;
24 rtpPacket->rtpHeader.ssrc = ssrc; 24 rtpPacket->rtpHeader.ssrc = ssrc;
25 } 25 }
  26 +
26 int parseRtpHeader(uint8_t* headerBuf, struct RtpHeader* rtpHeader){ 27 int parseRtpHeader(uint8_t* headerBuf, struct RtpHeader* rtpHeader){
27 memset(rtpHeader,0,sizeof(*rtpHeader)); 28 memset(rtpHeader,0,sizeof(*rtpHeader));
28 /* byte 0 */ 29 /* byte 0 */