Commit caef8aa259a1687ae5f5c717cb34b6012ae12214

Authored by Hu Chunming
1 parent 52d214c5

修复aclrtSetDevice和视频截存的内存泄露

src/decoder/dvpp/DvppDecoder.cpp
@@ -332,13 +332,7 @@ int DvppDecoder::getVdecType(int videoType, int profile) @@ -332,13 +332,7 @@ int DvppDecoder::getVdecType(int videoType, int profile)
332 post_decoded_cbk = cfg.post_decoded_cbk; 332 post_decoded_cbk = cfg.post_decoded_cbk;
333 333
334 do{ 334 do{
335 - aclError ret = aclrtSetDevice(m_dvpp_deviceId);  
336 - if(ret != ACL_ERROR_NONE){  
337 - LOG_ERROR("[{}]-aclrtSetDevice failed !", m_dec_name);  
338 - break;  
339 - }  
340 -  
341 - ret = aclrtCreateContext(&m_context, m_dvpp_deviceId); 335 + aclError ret = aclrtCreateContext(&m_context, m_dvpp_deviceId);
342 if (ret != ACL_ERROR_NONE) { 336 if (ret != ACL_ERROR_NONE) {
343 LOG_ERROR("[{}]-aclrtCreateContext failed !", m_dec_name); 337 LOG_ERROR("[{}]-aclrtCreateContext failed !", m_dec_name);
344 break; 338 break;
@@ -515,7 +509,7 @@ void DvppDecoder::read_thread() { @@ -515,7 +509,7 @@ void DvppDecoder::read_thread() {
515 } 509 }
516 510
517 m_bExitDisplayThd = false; 511 m_bExitDisplayThd = false;
518 - std::thread display_thread = std::thread( 512 + std::thread display_thread(
519 [](void* arg) 513 [](void* arg)
520 { 514 {
521 DvppDecoder* a=(DvppDecoder*)arg; 515 DvppDecoder* a=(DvppDecoder*)arg;
@@ -525,11 +519,9 @@ void DvppDecoder::read_thread() { @@ -525,11 +519,9 @@ void DvppDecoder::read_thread() {
525 this 519 this
526 ); 520 );
527 521
528 - aclrtContext ctx = nullptr;  
529 aclvdecChannelDesc *vdecChannelDesc = nullptr; 522 aclvdecChannelDesc *vdecChannelDesc = nullptr;
530 523
531 do { 524 do {
532 - CHECK_AND_BREAK(aclrtSetDevice(m_dvpp_deviceId), "aclrtSetDevice failed");  
533 int ret = aclrtSetCurrentContext(m_context); 525 int ret = aclrtSetCurrentContext(m_context);
534 if(ret != ACL_ERROR_NONE){ 526 if(ret != ACL_ERROR_NONE){
535 LOG_ERROR("[{}]- aclrtSetCurrentContext failed", m_dec_name); 527 LOG_ERROR("[{}]- aclrtSetCurrentContext failed", m_dec_name);
@@ -552,6 +544,8 @@ void DvppDecoder::read_thread() { @@ -552,6 +544,8 @@ void DvppDecoder::read_thread() {
552 CHECK_AND_BREAK(aclvdecCreateChannel(vdecChannelDesc), "aclvdecCreateChannel failed"); 544 CHECK_AND_BREAK(aclvdecCreateChannel(vdecChannelDesc), "aclvdecCreateChannel failed");
553 545
554 unsigned long long frame_nb = 0; 546 unsigned long long frame_nb = 0;
  547 + AVPacket* pkt = av_packet_alloc();
  548 + av_init_packet( pkt );
555 while (m_bRunning){ 549 while (m_bRunning){
556 550
557 if (!m_bReal){ 551 if (!m_bReal){
@@ -578,12 +572,9 @@ void DvppDecoder::read_thread() { @@ -578,12 +572,9 @@ void DvppDecoder::read_thread() {
578 m_last_read_ts = get_cur_time_ms(); 572 m_last_read_ts = get_cur_time_ms();
579 } 573 }
580 574
581 - AVPacket* pkt = av_packet_alloc();  
582 - av_init_packet( pkt );  
583 int result = av_read_frame(fmt_ctx, pkt); 575 int result = av_read_frame(fmt_ctx, pkt);
584 if (result == AVERROR_EOF || result < 0){ 576 if (result == AVERROR_EOF || result < 0){
585 - av_packet_free(&pkt);  
586 - pkt = nullptr; 577 + av_packet_unref(pkt);
587 LOG_WARN("[{}]- Failed to read frame!", m_dec_name); 578 LOG_WARN("[{}]- Failed to read frame!", m_dec_name);
588 break; 579 break;
589 } 580 }
@@ -591,15 +582,13 @@ void DvppDecoder::read_thread() { @@ -591,15 +582,13 @@ void DvppDecoder::read_thread() {
591 if (m_bReal && m_DvppCacheCounter.load() > m_cache_gop){ 582 if (m_bReal && m_DvppCacheCounter.load() > m_cache_gop){
592 // 解码器解码不过来。实时流在此处的处理会导致花屏,这是由于解码器性能问题导致,无法避免 583 // 解码器解码不过来。实时流在此处的处理会导致花屏,这是由于解码器性能问题导致,无法避免
593 // 实时流在这里处理是为了避免长时间不读取数据导致数据中断 584 // 实时流在这里处理是为了避免长时间不读取数据导致数据中断
594 - av_packet_free(&pkt);  
595 - pkt = nullptr; 585 + av_packet_unref(pkt);
596 std::this_thread::sleep_for(std::chrono::milliseconds(10)); 586 std::this_thread::sleep_for(std::chrono::milliseconds(10));
597 continue; 587 continue;
598 } 588 }
599 589
600 if (m_dec_keyframe && !(pkt->flags & AV_PKT_FLAG_KEY)) { 590 if (m_dec_keyframe && !(pkt->flags & AV_PKT_FLAG_KEY)) {
601 - av_packet_free(&pkt);  
602 - pkt = nullptr; 591 + av_packet_unref(pkt);
603 continue; 592 continue;
604 } 593 }
605 594
@@ -608,8 +597,7 @@ void DvppDecoder::read_thread() { @@ -608,8 +597,7 @@ void DvppDecoder::read_thread() {
608 ret = av_bsf_send_packet(h264bsfc, pkt); 597 ret = av_bsf_send_packet(h264bsfc, pkt);
609 if(ret < 0) { 598 if(ret < 0) {
610 LOG_ERROR("[{}]- av_bsf_send_packet error!", m_dec_name); 599 LOG_ERROR("[{}]- av_bsf_send_packet error!", m_dec_name);
611 - av_packet_free(&pkt);  
612 - pkt = nullptr; 600 + av_packet_unref(pkt);
613 continue; 601 continue;
614 } 602 }
615 603
@@ -631,12 +619,14 @@ void DvppDecoder::read_thread() { @@ -631,12 +619,14 @@ void DvppDecoder::read_thread() {
631 #ifdef USE_VILLAGE 619 #ifdef USE_VILLAGE
632 m_recoderManager.cache_pkt(pkt, frame_nb, m_dec_name); 620 m_recoderManager.cache_pkt(pkt, frame_nb, m_dec_name);
633 #endif 621 #endif
634 - } else {  
635 - av_packet_free(&pkt);  
636 - pkt = nullptr;  
637 - } 622 + }
  623 +
  624 + av_packet_unref(pkt);
638 } 625 }
639 626
  627 + av_packet_free(&pkt);
  628 + pkt = nullptr;
  629 +
640 if (vdecChannelDesc) { 630 if (vdecChannelDesc) {
641 sendVdecEos(vdecChannelDesc); 631 sendVdecEos(vdecChannelDesc);
642 } 632 }
@@ -764,24 +754,15 @@ void DvppDecoder::doProcessReport(){ @@ -764,24 +754,15 @@ void DvppDecoder::doProcessReport(){
764 754
765 aclError ret = aclrtSetDevice(m_dvpp_deviceId); 755 aclError ret = aclrtSetDevice(m_dvpp_deviceId);
766 if(ret != ACL_ERROR_NONE){ 756 if(ret != ACL_ERROR_NONE){
767 - // cout << "aclrtSetDevice failed" << endl;  
768 - LOG_ERROR("aclrtSetDevice failed !");  
769 - return ;  
770 - }  
771 -  
772 - aclrtContext ctx;  
773 - ret = aclrtCreateContext(&ctx, m_dvpp_deviceId);  
774 - if (ret != ACL_ERROR_NONE) {  
775 - // cout << "aclrtCreateContext failed " << endl;  
776 - LOG_ERROR("aclrtCreateContext failed !");  
777 - return ; 757 + LOG_ERROR("[{}]-aclrtSetDevice failed !", m_dec_name);
  758 + return;
778 } 759 }
779 760
780 while (!m_bExitReportThd) { 761 while (!m_bExitReportThd) {
781 aclrtProcessReport(1000); 762 aclrtProcessReport(1000);
782 } 763 }
783 764
784 - ret = aclrtDestroyContext(ctx); 765 + ret = aclrtResetDevice(m_dvpp_deviceId);
785 if(ret != ACL_ERROR_NONE){ 766 if(ret != ACL_ERROR_NONE){
786 LOG_ERROR("aclrtDestroyContext failed !"); 767 LOG_ERROR("aclrtDestroyContext failed !");
787 } 768 }
src/decoder/dvpp/DvppStreamDecoder.cpp
@@ -487,7 +487,7 @@ int DvppStreamDecoder::SendData(int videoType, char* data, int len, int isKey, u @@ -487,7 +487,7 @@ int DvppStreamDecoder::SendData(int videoType, char* data, int len, int isKey, u
487 #ifdef USE_VILLAGE 487 #ifdef USE_VILLAGE
488 //直接返回,不释放pkt,pkt在recoderManager释放 488 //直接返回,不释放pkt,pkt在recoderManager释放
489 return 0; 489 return 0;
490 -#elif 490 +#else
491 ret = 0; 491 ret = 0;
492 #endif 492 #endif
493 } 493 }