zhangmeng
2019-06-10 439a23896bda9c4e954092816354914e645579b6
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,7 +36,7 @@
bool BASICGB28181::VideoCaptureElementWithRtp::startRtpStream(int streamTransType) {
    if(!m_waitSignalrunning) {
    if (!m_waitSignalrunning) {
        DBG("std::thread waitSignalAndEmit create New!!");
        //等待下层ffmpeg将rtp包解码成为图片后触发信号,然后触发当前类的submit
        std::thread waitSignalAndEmit([&](BASICGB28181::VideoCaptureElementWithRtp *p_this) {
@@ -52,6 +53,39 @@
#ifdef TestCode
                DBG("waitSignal(\"DecoderImageOK\") after");
#endif
                //            从ffmpeg解码类中获取图片
                p_this->m_fFmpegDecoderJPG.getImage().copyTo(p_this->m_image);
                {
                    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()) {
                                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->m_picCount++;
                //几张选一张放入Redis
@@ -61,15 +95,12 @@
                    p_this->m_picCount.store(0);
                }
//            从ffmpeg解码类中获取图片
                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);
                    p_this->m_pManager->SaveImageToRedis(p_this->m_chanPubID, imageName, copyMat);
                }
                /*********/
                p_this->submit();
            }
@@ -119,23 +150,24 @@
                usleep(1000000);
                //阻塞线程,等待外部触发关闭点播
                while (p_this->m_running) {
                    if(p_this->m_fFmpegDecoderJPG.getRunning()) {
                    if (p_this->m_fFmpegDecoderJPG.getRunning()) {
                        usleep(300000);
                    } else {
                        // 根据reopenTime判断是否需要重启
                        if (reopenTime < 0) {
                        if (p_this->reopenTime < 0) {
                            p_this->m_running = false;
                            stop();
                            INFO("grabFrame faild, element stopping");
                        } else {
                            //todo 业务死锁
                            usleep((6 - reopenTime--) * 1000000);
                            INFO("grabFrame faild, try reopen video: ");
                            usleep((6 - p_this->reopenTime--) * 1000000);
                            INFO("grabFrame faild, try reopen video, reopenTime:" << p_this->reopenTime);
                            //关闭ffmpeg解码模块
                            p_this->m_fFmpegDecoderJPG.stopThd();
                            //启动ffmpeg解码模块
                            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;
                        }
@@ -204,20 +236,6 @@
void BASICGB28181::VideoCaptureElementWithRtp::threadFunc() {
//    if ((!m_running) || (!m_waitSignal)) {
////        根据reopenTime判断是否需要重启
//        if (reopenTime < 0) {
//            stop();
//            INFO("grabFrame faild, element stopping");
//            return;
//        } else {
//            //todo 业务死锁
//            usleep(reopenTime * 1000);
//            INFO("grabFrame faild, try reopen video: ");
//            startRtpStream(m_streamTransType);
//            return;
//        }
//    }
    fireConnectors();
}
@@ -242,7 +260,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);
@@ -270,3 +288,11 @@
    return t_FilePath;
}
void BASICGB28181::VideoCaptureElementWithRtp::startPublishVideo() {
    m_publishVideoRet = true;
}
void BASICGB28181::VideoCaptureElementWithRtp::stopPublishVideo() {
    m_publishVideoRet = false;
}