From f946a62d3921e86b44ff8e2973138304b9cd53cd Mon Sep 17 00:00:00 2001 From: chenshijun <csj_sky@126.com> Date: 星期二, 16 四月 2019 16:36:32 +0800 Subject: [PATCH] 解决修改参数和推流画面跳跃问题 --- QiaoJiaSystem/GB28181DecoderModel/VideoCaptureElementWithRtp.cpp | 154 ++++++++++++++++++++++++++++++++++----------------- 1 files changed, 103 insertions(+), 51 deletions(-) diff --git a/QiaoJiaSystem/GB28181DecoderModel/VideoCaptureElementWithRtp.cpp b/QiaoJiaSystem/GB28181DecoderModel/VideoCaptureElementWithRtp.cpp index b2acfc4..fb01c4d 100644 --- a/QiaoJiaSystem/GB28181DecoderModel/VideoCaptureElementWithRtp.cpp +++ b/QiaoJiaSystem/GB28181DecoderModel/VideoCaptureElementWithRtp.cpp @@ -5,6 +5,7 @@ #include <opencv2/imgproc.hpp> #include <qt5/QtCore/QDateTime> #include <basic/util/app/AppPreference.hpp> +#include <basic/util/app/AppConfig.h> #include "VideoCaptureElementWithRtp.h" #include <VideoToImageMulth/RtspAnalysManager.h> @@ -35,44 +36,80 @@ bool BASICGB28181::VideoCaptureElementWithRtp::startRtpStream(int streamTransType) { - //绛夊緟涓嬪眰ffmpeg灏唕tp鍖呰В鐮佹垚涓哄浘鐗囧悗瑙﹀彂淇″彿,鐒跺悗瑙﹀彂褰撳墠绫荤殑submit - std::thread waitSignalAndEmit([&](BASICGB28181::VideoCaptureElementWithRtp *p_this) { - p_this->m_waitSignal = true; - //寰幆锛岀敱鎴愬憳鍙橀噺鏉ョ淮鎶よ繖涓嚎绋嬬殑杩愯鐘舵�� - while (p_this->m_waitSignal) { + if (!m_waitSignalrunning) { + DBG("std::thread waitSignalAndEmit create New!!"); + //绛夊緟涓嬪眰ffmpeg灏唕tp鍖呰В鐮佹垚涓哄浘鐗囧悗瑙﹀彂淇″彿,鐒跺悗瑙﹀彂褰撳墠绫荤殑submit + std::thread waitSignalAndEmit([&](BASICGB28181::VideoCaptureElementWithRtp *p_this) { + p_this->m_waitSignal = true; + p_this->m_waitSignalrunning = true; + //寰幆锛岀敱鎴愬憳鍙橀噺鏉ョ淮鎶よ繖涓嚎绋嬬殑杩愯鐘舵�� + while (p_this->m_waitSignal) { //#TODO wait test #ifdef TestCode - DBG("waitSignal(\"DecoderImageOK\") begin"); + DBG("waitSignal(\"DecoderImageOK\") begin"); #endif - //绛夊緟淇″彿瑙﹀彂 - gSignalLock.waitSignal(p_this->m_chanPubID + "DecoderImageOK"); + //绛夊緟淇″彿瑙﹀彂 + gSignalLock.waitSignal(p_this->m_chanPubID + "DecoderImageOK"); #ifdef TestCode - DBG("waitSignal(\"DecoderImageOK\") after"); + DBG("waitSignal(\"DecoderImageOK\") after"); #endif - /****褰曞儚妯″潡浠g爜*****/ - p_this->m_picCount++; - //鍑犲紶閫変竴寮犳斁鍏edis - if (p_this->m_picCount % m_nPicsPickOne != 0) { - continue; - } else { - p_this->m_picCount.store(0); - } + // 浠巉fmpeg瑙g爜绫讳腑鑾峰彇鍥剧墖 + p_this->m_fFmpegDecoderJPG.getImage().copyTo(p_this->m_image); -// 浠巉fmpeg瑙g爜绫讳腑鑾峰彇鍥剧墖 - p_this->m_fFmpegDecoderJPG.getImage().copyTo(p_this->m_image); - { - cv::Mat copyMat; - std::string imageName = p_this->m_fFmpegDecoderJPG.GetImageName(); - p_this->m_image.copyTo(copyMat); - m_pManager->SaveImageToRedis(p_this->m_chanPubID, imageName, copyMat); - } - /*********/ + { + if (p_this->m_publishVideoRet) { + if (p_this->videoPublishElement == nullptr) { + string path = appConfig.getStringProperty("srsAddr") + "cam" + p_this->m_chanPubID + ".flv"; + cv::Size size_(appConfig.getIntProperty("pulish.width"), appConfig.getIntProperty("pulish.height")); + int gupIdx = appPref.getIntData("gpu.index"); + DBG("videoPublishpath: " << p_this->m_chanPubID << path << " h:" << size_.height); + p_this->videoPublishElement = new ffmpeg::PipeVideoPublishElement(path, size_, "flv", 25, gupIdx); + p_this->videoPublishElement->start(); + } else { + if (!p_this->m_image.empty()) { +// DBG("videoPublishElement->setImage() : " << p_this->m_chanPubID); + p_this->videoPublishElement->setImage(p_this->m_image); + p_this->videoPublishElement->submit(); + } else { + ERR("m_image.empty()"); + } + } + } else { + if (p_this->videoPublishElement != nullptr) { + DBG("videoPublishElement->stop() :" << p_this->m_chanPubID); + p_this->videoPublishElement->stop(); + p_this->videoPublishElement->wait(); + delete p_this->videoPublishElement; + p_this->videoPublishElement = nullptr; + }else{ +// DBG("videoPublishElement null :" << p_this->m_chanPubID); + } + } + } - p_this->submit(); - } - INFO("waitSignalAndEmit is exit..."); - }, this); - waitSignalAndEmit.detach(); + /****褰曞儚妯″潡浠g爜*****/ + p_this->m_picCount++; + //鍑犲紶閫変竴寮犳斁鍏edis + if (p_this->m_picCount % m_nPicsPickOne != 0) { + continue; + } else { + p_this->m_picCount.store(0); + } + + { + cv::Mat copyMat; + std::string imageName = p_this->m_fFmpegDecoderJPG.GetImageName(); + p_this->m_image.copyTo(copyMat); + p_this->m_pManager->SaveImageToRedis(p_this->m_chanPubID, imageName, copyMat); + } + + p_this->submit(); + } + p_this->m_waitSignalrunning = false; + INFO("waitSignalAndEmit is exit..."); + }, this); + waitSignalAndEmit.detach(); + } TryCath( //--------------鍥芥爣璁惧鎴栧垯鍥芥爣涓嬬骇骞冲彴蹇呴』鏀寔GB28181-2016---------------------------------------------- @@ -111,14 +148,31 @@ p_this->m_running = true; //鍚姩ffmpeg瑙g爜妯″潡 p_this->m_fFmpegDecoderJPG.startThd(p_this->m_chanPubID, p_this->m_fps, p_this->m_gpuIdx); + usleep(1000000); //闃诲绾跨▼锛岀瓑寰呭閮ㄨЕ鍙戝叧闂偣鎾� while (p_this->m_running) { -// if(p_this->m_fFmpegDecoderJPG.getRunning()) { - usleep(300000); -// }else{ -// p_this->m_running = false; -// break; -// } + if (p_this->m_fFmpegDecoderJPG.getRunning()) { + usleep(300000); + } else { + + // 鏍规嵁reopenTime鍒ゆ柇鏄惁闇�瑕侀噸鍚� + if (p_this->reopenTime < 0) { + p_this->m_running = false; + stop(); + INFO("grabFrame faild, element stopping"); + } else { + //todo 涓氬姟姝婚攣 + usleep((6 - p_this->reopenTime--) * 1000000); + INFO("grabFrame faild, try reopen video, reopenTime:" << p_this->reopenTime); + + //鍏抽棴ffmpeg瑙g爜妯″潡 + p_this->m_fFmpegDecoderJPG.stopThd(); + //鍚姩ffmpeg瑙g爜妯″潡 + DBG("m_chanPubID:"<<p_this->m_chanPubID << " m_fps:" << p_this->m_fps); + p_this->m_fFmpegDecoderJPG.startThd(p_this->m_chanPubID, p_this->m_fps, p_this->m_gpuIdx); + continue; + } + } } DBG("videoCaptureElementThd stop ..."); //鍋滄鐐规挱 @@ -130,9 +184,12 @@ //鐐规挱澶辫触 p_this->m_waitSignal = false; p_this->m_running = false; + //鍏抽棴ffmpeg瑙g爜妯″潡 p_this->m_fFmpegDecoderJPG.stopThd(); ERR(p_this->m_chanPubID << " C_RealVideoStart is error lrealhandle is " << lrealhandle); + + p_this->startRtpStream(p_this->m_streamTransType); } }, this, streamTransType); @@ -180,19 +237,6 @@ void BASICGB28181::VideoCaptureElementWithRtp::threadFunc() { - if ((!m_running) || (!m_waitSignal)) { -// 鏍规嵁reopenTime鍒ゆ柇鏄惁闇�瑕侀噸鍚� - if (reopenTime < 0) { - stop(); - INFO("grabFrame faild, element stopping"); - return; - } else { - usleep(reopenTime * 1000); - INFO("grabFrame faild, try reopen video: "); - startRtpStream(m_streamTransType); - return; - } - } fireConnectors(); } @@ -217,7 +261,7 @@ void BASICGB28181::VideoCaptureElementWithRtp::SaveVideo(const std::string &strImageName) { - INFO("SaveVideo: " << strImageName); +// INFO("SaveVideo: " << strImageName); std::string strTimeStamp = AppUtil::getTimeUSecString(); std::string strPath = MakeDir(strTimeStamp); m_fFmpegDecoderJPG.SaveVideoByImageName(strPath, strImageName); @@ -245,3 +289,11 @@ return t_FilePath; } + +void BASICGB28181::VideoCaptureElementWithRtp::startPublishVideo() { + m_publishVideoRet = true; +} + +void BASICGB28181::VideoCaptureElementWithRtp::stopPublishVideo() { + m_publishVideoRet = false; +} -- Gitblit v1.8.0