From 3c9776e9faad1b5d6bcdbe4b7933f99c9427faee Mon Sep 17 00:00:00 2001 From: Hu Chunming <2657262686@qq.com> Date: Thu, 8 Aug 2024 09:55:18 +0800 Subject: [PATCH] 代码优化 --- src/decoder/dvpp/DvppRtpDecoder.cpp | 15 +++++++++------ src/decoder/dvpp/DvppRtpDecoder.h | 10 +++++----- src/decoder/gb28181/rtp2/RTPReceiver2.cpp | 11 +++++------ src/decoder/gb28181/rtp2/Rtp.cpp | 1 + 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/decoder/dvpp/DvppRtpDecoder.cpp b/src/decoder/dvpp/DvppRtpDecoder.cpp index 9c7f6d8..237d03d 100644 --- a/src/decoder/dvpp/DvppRtpDecoder.cpp +++ b/src/decoder/dvpp/DvppRtpDecoder.cpp @@ -1,7 +1,6 @@ #include "DvppRtpDecoder.h" #include "DvppSourceManager.h" -#include "../gb28181/rtp2/Rtp.h" #define CHECK_AND_RETURN(ret, message) \ @@ -365,9 +364,9 @@ void DvppRtpDecoder::release_ffmpeg() { } void DvppRtpDecoder::CacheBuffer(uint8_t* recvBuf, int recvBufSize) { - if ((m_bufferSize + recvBufSize - RTP_HEADER_SIZE) < MAX_RTP_BUFFER_SIZE) { - memcpy(m_buffer + m_bufferSize, recvBuf + RTP_HEADER_SIZE, recvBufSize - RTP_HEADER_SIZE); - m_bufferSize += recvBufSize - RTP_HEADER_SIZE; + if ((m_bufferSize + recvBufSize) < MAX_RTP_BUFFER_SIZE) { + memcpy(m_buffer + m_bufferSize, recvBuf, recvBufSize); + m_bufferSize += recvBufSize; } else { LOG_WARN("recvBufSize = {} over MAX_RTP_BUFFER_SIZE ", recvBufSize); } @@ -402,8 +401,8 @@ bool DvppRtpDecoder::probe() { m_buffer_waiting_time = 3000; //probe只等待30s,避免卡主任务添加 // todo: 此处可能有泄露 - unsigned char* avioBuff = (unsigned char*)av_malloc(MAX_RTP_BUFFER_SIZE); - AVIOContext *ioCtx = avio_alloc_context(avioBuff, sizeof(avioBuff), 0, this, avio_read_packet, NULL, NULL); + unsigned char* avioBuff = (unsigned char*)av_malloc(2048); // 32768 < MAX_RTP_BUFFER_SIZE - RTP_HEADER_SIZE + AVIOContext *ioCtx = avio_alloc_context(avioBuff, 2048, 0, this, avio_read_packet, NULL, NULL); do{ fmt_ctx = avformat_alloc_context(); @@ -577,6 +576,10 @@ void DvppRtpDecoder::read_thread() { continue; } + // av_packet_free(&pkt); + // pkt = nullptr; + // continue; + if (m_DvppCacheCounter.load() > m_cache_gop){ // 解码器解码不过来。实时流在此处的处理会导致花屏,这是由于解码器性能问题导致,无法避免 // 实时流在这里处理是为了避免长时间不读取数据导致数据中断 diff --git a/src/decoder/dvpp/DvppRtpDecoder.h b/src/decoder/dvpp/DvppRtpDecoder.h index 063728f..f3b3233 100644 --- a/src/decoder/dvpp/DvppRtpDecoder.h +++ b/src/decoder/dvpp/DvppRtpDecoder.h @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include "depend_headers.h" #include "dvpp_headers.h" @@ -21,6 +21,7 @@ using namespace std; #define MAX_RTP_BUFFER_SIZE 4194304 // 4M = 4 * 1024 * 1024 = 4194304 字节 + typedef void(*CallBack_DecodeFinished)(void* userdata); class DvppRtpDecoder @@ -30,10 +31,6 @@ public: ~DvppRtpDecoder(); public: - std::atomic m_buffer[MAX_RTP_BUFFER_SIZE]; - std::atomic_int m_bufferSize {0}; - -public: bool Init(FFDecConfig cfg); void Close(); bool start(); @@ -160,5 +157,8 @@ private: int m_buffer_waiting_time{3000} ; + std::atomic m_buffer[MAX_RTP_BUFFER_SIZE]; + std::atomic_int m_bufferSize {0}; + }; #endif //__DVPP_RTP_DECODER_H__ \ No newline at end of file diff --git a/src/decoder/gb28181/rtp2/RTPReceiver2.cpp b/src/decoder/gb28181/rtp2/RTPReceiver2.cpp index 591d063..7e99804 100644 --- a/src/decoder/gb28181/rtp2/RTPReceiver2.cpp +++ b/src/decoder/gb28181/rtp2/RTPReceiver2.cpp @@ -170,7 +170,7 @@ int RTPReceiver2::udp_server() { last_time = get_cur_time_second(); // buffer 抛出 - m_buffer_cbk(m_bufferParam, recvBuf, recvBufSize, 0); + m_buffer_cbk(m_bufferParam, recvBuf + RTP_HEADER_SIZE, recvBufSize - RTP_HEADER_SIZE, 0); } close(server_fd); @@ -220,12 +220,11 @@ int RTPReceiver2::tcp_server() { long long last_time = get_cur_time_second(); + socklen_t len = sizeof(sockaddr); + sockaddr_in accept_addr; while (!m_bRtpExit) { - // LOG_INFO("阻塞监听新连接..."); - // 阻塞接收请求 start - socklen_t len = sizeof(sockaddr); - sockaddr_in accept_addr; + // 非阻塞接收请求 start int clientFd = accept(listenfd, (struct sockaddr*)&accept_addr, &len); if (clientFd < 0) { if (!bFilter) { @@ -310,7 +309,7 @@ void RTPReceiver2::parseTcpData(uint8_t* recvBuf, int recvBufSize) { // mRecvCacheSize,rtpHeader.marker, rtpHeader.timestamp); // buffer 抛出 - m_buffer_cbk(m_bufferParam, mRecvRtpBuffer, mRecvRtpBufferSize, rtpHeader.timestamp); + m_buffer_cbk(m_bufferParam, mRecvRtpBuffer + RTP_HEADER_SIZE, mRecvRtpBufferSize - RTP_HEADER_SIZE, rtpHeader.timestamp); } else { //LOGI("跳出解析:cacheSize=%d,pktSize=%d", cacheSize, pktSize); diff --git a/src/decoder/gb28181/rtp2/Rtp.cpp b/src/decoder/gb28181/rtp2/Rtp.cpp index 03e2a8a..94df415 100644 --- a/src/decoder/gb28181/rtp2/Rtp.cpp +++ b/src/decoder/gb28181/rtp2/Rtp.cpp @@ -23,6 +23,7 @@ void rtpHeaderInit(struct RtpPacket* rtpPacket, uint8_t csrcLen, uint8_t extensi rtpPacket->rtpHeader.timestamp = timestamp; rtpPacket->rtpHeader.ssrc = ssrc; } + int parseRtpHeader(uint8_t* headerBuf, struct RtpHeader* rtpHeader){ memset(rtpHeader,0,sizeof(*rtpHeader)); /* byte 0 */ -- libgit2 0.21.4