Commit e5c14c8e32d64ff7e3a5d27b30abdafeb0a2efae

Authored by Hu Chunming
1 parent 826e3002

修复解码器异常退出时,接收器还在正常跑的问题

src/decoder/dvpp/DvppRtpDecoder.cpp
@@ -101,8 +101,6 @@ bool DvppRtpDecoder::Init(FFDecConfig cfg) { @@ -101,8 +101,6 @@ bool DvppRtpDecoder::Init(FFDecConfig cfg) {
101 101
102 m_bResize = m_cfg.resize; 102 m_bResize = m_cfg.resize;
103 103
104 - decode_finished_cbk = cfg.decode_finished_cbk;  
105 -  
106 bool bRet = init_dvpp(cfg); 104 bool bRet = init_dvpp(cfg);
107 if(!bRet){ 105 if(!bRet){
108 return false; 106 return false;
@@ -264,6 +262,12 @@ void DvppRtpDecoder::setFinishedDecArg(const void* finishedDecArg){ @@ -264,6 +262,12 @@ void DvppRtpDecoder::setFinishedDecArg(const void* finishedDecArg){
264 m_finishedDecArg = finishedDecArg; 262 m_finishedDecArg = finishedDecArg;
265 } 263 }
266 264
  265 +void DvppRtpDecoder::SetFinishedCallback(CallBack_DecodeFinished cb, void* param)
  266 +{
  267 + m_finish_cbk = cb;
  268 + m_finishParam = param;
  269 +}
  270 +
267 void DvppRtpDecoder::pause(){ 271 void DvppRtpDecoder::pause(){
268 m_bPause = true; 272 m_bPause = true;
269 } 273 }
@@ -370,27 +374,27 @@ void DvppRtpDecoder::CacheBuffer(uint8_t* recvBuf, int recvBufSize) { @@ -370,27 +374,27 @@ void DvppRtpDecoder::CacheBuffer(uint8_t* recvBuf, int recvBufSize) {
370 } 374 }
371 375
372 int DvppRtpDecoder::ReadBuffer(uint8_t* buf, int buffsize) { 376 int DvppRtpDecoder::ReadBuffer(uint8_t* buf, int buffsize) {
373 - int ret = 0; 377 +
374 int count = 0; 378 int count = 0;
375 - while(m_bRunning && (m_bufferSize < 4096 || m_bufferSize < buffsize)){ 379 + while(m_bufferSize < buffsize){
  380 + if(!m_bRunning){
  381 + return AVERROR_EXIT;
  382 + }
376 std::this_thread::sleep_for(std::chrono::milliseconds(10)); 383 std::this_thread::sleep_for(std::chrono::milliseconds(10));
377 count++; 384 count++;
378 - if (count >= 1000) {  
379 - // 只等待10s  
380 - return -1; 385 + if (count >= 3000) {
  386 + // 只等待30s
  387 + return AVERROR(EIO);
381 } 388 }
382 } 389 }
383 390
384 - if (m_bufferSize >= buffsize) {  
385 - memcpy(buf, m_buffer, buffsize);  
386 - m_bufferSize = m_bufferSize - buffsize;  
387 - memmove(m_buffer, m_buffer + buffsize, m_bufferSize);  
388 - ret = buffsize;  
389 - } 391 + memcpy(buf, m_buffer, buffsize);
  392 + m_bufferSize = m_bufferSize - buffsize;
  393 + memmove(m_buffer, m_buffer + buffsize, m_bufferSize);
390 394
391 - printf("m_bufferSize=%d buffsize=%d\n", m_bufferSize.load(), buffsize); 395 + // printf("m_bufferSize=%d buffsize=%d\n", m_bufferSize.load(), buffsize);
392 396
393 - return ret; 397 + return buffsize;
394 } 398 }
395 399
396 bool DvppRtpDecoder::probe() { 400 bool DvppRtpDecoder::probe() {
@@ -426,7 +430,7 @@ bool DvppRtpDecoder::probe() { @@ -426,7 +430,7 @@ bool DvppRtpDecoder::probe() {
426 av_dict_set( &net_options, "max_delay", "500000", 0); //设置最大时延 430 av_dict_set( &net_options, "max_delay", "500000", 0); //设置最大时延
427 431
428 //打开流 432 //打开流
429 - ret = avformat_open_input(&fmt_ctx, 0, 0, &net_options); 433 + ret = avformat_open_input(&fmt_ctx, 0, inputFmt, &net_options);
430 if (ret != 0) { 434 if (ret != 0) {
431 LOG_ERROR("avformat_open_input error: {}", ret); 435 LOG_ERROR("avformat_open_input error: {}", ret);
432 break; 436 break;
@@ -577,6 +581,8 @@ void DvppRtpDecoder::read_thread() { @@ -577,6 +581,8 @@ void DvppRtpDecoder::read_thread() {
577 if (m_DvppCacheCounter.load() > m_cache_gop){ 581 if (m_DvppCacheCounter.load() > m_cache_gop){
578 // 解码器解码不过来。实时流在此处的处理会导致花屏,这是由于解码器性能问题导致,无法避免 582 // 解码器解码不过来。实时流在此处的处理会导致花屏,这是由于解码器性能问题导致,无法避免
579 // 实时流在这里处理是为了避免长时间不读取数据导致数据中断 583 // 实时流在这里处理是为了避免长时间不读取数据导致数据中断
  584 + av_packet_free(&pkt);
  585 + pkt = nullptr;
580 std::this_thread::sleep_for(std::chrono::milliseconds(10)); 586 std::this_thread::sleep_for(std::chrono::milliseconds(10));
581 continue; 587 continue;
582 } 588 }
@@ -609,11 +615,16 @@ void DvppRtpDecoder::read_thread() { @@ -609,11 +615,16 @@ void DvppRtpDecoder::read_thread() {
609 if(nSended < 0) { 615 if(nSended < 0) {
610 // 执行出错,强行结束整个任务 616 // 执行出错,强行结束整个任务
611 m_bRunning=false; 617 m_bRunning=false;
  618 + av_packet_free(&pkt);
  619 + pkt = nullptr;
612 break; 620 break;
613 } 621 }
614 622
615 #ifdef USE_VILLAGE 623 #ifdef USE_VILLAGE
616 m_recoderManager.cache_pkt(pkt, frame_nb, m_dec_name); 624 m_recoderManager.cache_pkt(pkt, frame_nb, m_dec_name);
  625 + #else
  626 + av_packet_free(&pkt);
  627 + pkt = nullptr;
617 #endif 628 #endif
618 } else { 629 } else {
619 av_packet_free(&pkt); 630 av_packet_free(&pkt);
@@ -646,8 +657,8 @@ void DvppRtpDecoder::read_thread() { @@ -646,8 +657,8 @@ void DvppRtpDecoder::read_thread() {
646 657
647 LOG_INFO("[{}]- read thread exit.", m_dec_name); 658 LOG_INFO("[{}]- read thread exit.", m_dec_name);
648 659
649 - if(decode_finished_cbk) {  
650 - decode_finished_cbk(m_finishedDecArg); 660 + if(m_finish_cbk) {
  661 + m_finish_cbk(m_finishParam);
651 } 662 }
652 } 663 }
653 664
src/decoder/dvpp/DvppRtpDecoder.h
@@ -21,6 +21,8 @@ using namespace std; @@ -21,6 +21,8 @@ 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 +typedef void(*CallBack_DecodeFinished)(void* userdata);
  25 +
24 class DvppRtpDecoder 26 class DvppRtpDecoder
25 { 27 {
26 public: 28 public:
@@ -73,6 +75,8 @@ public: @@ -73,6 +75,8 @@ public:
73 75
74 int ReadBuffer(uint8_t* buf, int buffsize); 76 int ReadBuffer(uint8_t* buf, int buffsize);
75 77
  78 + void SetFinishedCallback(CallBack_DecodeFinished cb, void* param);
  79 +
76 public: 80 public:
77 void doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicDesc *output, void *pUserData); 81 void doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicDesc *output, void *pUserData);
78 void doProcessReport(); 82 void doProcessReport();
@@ -96,7 +100,6 @@ private: @@ -96,7 +100,6 @@ private:
96 string m_dec_name; 100 string m_dec_name;
97 101
98 const void * m_finishedDecArg {nullptr}; 102 const void * m_finishedDecArg {nullptr};
99 - DECODE_FINISHED_CALLBACK decode_finished_cbk {nullptr};  
100 103
101 bool m_bFinished{false}; 104 bool m_bFinished{false};
102 bool m_bRunning{false}; 105 bool m_bRunning{false};
@@ -152,5 +155,8 @@ private: @@ -152,5 +155,8 @@ private:
152 155
153 VpcUtils m_vpcUtils; 156 VpcUtils m_vpcUtils;
154 157
  158 + void* m_finishParam;
  159 + CallBack_DecodeFinished m_finish_cbk; // 录像流结束回调
  160 +
155 }; 161 };
156 #endif //__DVPP_RTP_DECODER_H__ 162 #endif //__DVPP_RTP_DECODER_H__
157 \ No newline at end of file 163 \ No newline at end of file
src/decoder/gb28181/rtp2/RTPReceiver2.cpp
1 #include "RTPReceiver2.h" 1 #include "RTPReceiver2.h"
2 -#include "rtppacket.h"  
3 #include <thread> 2 #include <thread>
4 3
5 #include "../common_header.h" 4 #include "../common_header.h"
@@ -68,19 +67,12 @@ bool RTPReceiver2::Open(string channel_id, bool isUdp) { @@ -68,19 +67,12 @@ bool RTPReceiver2::Open(string channel_id, bool isUdp) {
68 Close(); 67 Close();
69 return false; 68 return false;
70 } 69 }
71 -  
72 - m_bOpened = true;  
73 70
74 LOG_INFO("[{}] started.", m_SipChannelId); 71 LOG_INFO("[{}] started.", m_SipChannelId);
75 72
76 return true; 73 return true;
77 } 74 }
78 75
79 -bool RTPReceiver2::IsOpened(){  
80 - LOG_INFO("[{}] isopen:{} ", m_SipChannelId, m_bOpened);  
81 - return m_bOpened;  
82 -}  
83 -  
84 void RTPReceiver2::Close(){ 76 void RTPReceiver2::Close(){
85 m_bRtpExit = true; 77 m_bRtpExit = true;
86 78
src/decoder/gb28181/rtp2/RTPReceiver2.h
@@ -19,7 +19,6 @@ public: @@ -19,7 +19,6 @@ public:
19 virtual ~RTPReceiver2(); 19 virtual ~RTPReceiver2();
20 20
21 bool Open(string channel_id, bool isUdp); 21 bool Open(string channel_id, bool isUdp);
22 - bool IsOpened();  
23 void Close(); 22 void Close();
24 23
25 void SetVodEndCallback(CallBack_VodFileEnd cb, void* param); 24 void SetVodEndCallback(CallBack_VodFileEnd cb, void* param);
@@ -50,7 +49,6 @@ public: @@ -50,7 +49,6 @@ public:
50 string m_SipChannelId; 49 string m_SipChannelId;
51 int m_rtp_port{-1}; 50 int m_rtp_port{-1};
52 51
53 - std::atomic_bool m_bOpened;  
54 std::atomic_bool m_bAccepted; 52 std::atomic_bool m_bAccepted;
55 std::atomic_bool m_bClosing; 53 std::atomic_bool m_bClosing;
56 54