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/VideoToImageMulth/RtspCaptureElement.cpp |  117 ++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 80 insertions(+), 37 deletions(-)

diff --git a/QiaoJiaSystem/VideoToImageMulth/RtspCaptureElement.cpp b/QiaoJiaSystem/VideoToImageMulth/RtspCaptureElement.cpp
index 16aafb7..7316f37 100644
--- a/QiaoJiaSystem/VideoToImageMulth/RtspCaptureElement.cpp
+++ b/QiaoJiaSystem/VideoToImageMulth/RtspCaptureElement.cpp
@@ -2,24 +2,27 @@
 #include <basic/debug/Debug.h>
 #include <opencv2/opencv.hpp>
 #include <basic/timer_counter/Clocktimer.h>
-#include <basic/pipe_element/ffmpeg/cap_ffmpeg_impl.hpp>
 #include <thread>
 #include <basic/util/app/AppPreference.hpp>
 #include <QString>
 #include <QDateTime>
 #include "RtspAnalysManager.h"
-RtspCaptureElement::RtspCaptureElement(const std::string &path, const std::string& camId,int fps, int reopenTime, int gpuIndex,RtspAnalysManager* manager):
-    TimerElement(10),m_path(path),m_gpuIndex(gpuIndex),
-    m_reopenTime(reopenTime),m_camId(camId),m_pManager(manager){
-    m_cutPath= appPref.getStringData("user.loop.absolute.path");
+//#include <basic/pipe_element/ffmpeg/cap_ffmpeg_impl.hpp>
+#include "../../../BasicPlatForm/basic/pipe_element/ffmpeg/FfmpegElement.cpp"
+#include <basic/util/app/AppConfig.h>
+
+RtspCaptureElement::RtspCaptureElement(const std::string &path, const std::string &camId, int fps, int reopenTime,
+                                       int gpuIndex, RtspAnalysManager *manager) :
+    TimerElement(10), m_path(path), m_gpuIndex(gpuIndex),
+    m_reopenTime(reopenTime), m_camId(camId), m_pManager(manager) {
+    m_cutPath = appPref.getStringData("user.loop.absolute.path");
 
     assert(!m_cutPath.empty());
     m_capture = new CvCapture_FFMPEG(m_camId);
 }
 
 //瀹氭椂鎶撳彇鍥剧墖,閫氳繃灏嗗浘鐗囨斁鍏tspImageRedisElement鐨勯槦鍒椾腑,鏉ュ噺灏戣棰戠殑涓㈠抚
-void RtspCaptureElement::timerFunc()
-{
+void RtspCaptureElement::timerFunc() {
 
     bool ret = m_capture->grabFrame();
     if (!ret) {
@@ -36,30 +39,62 @@
         }
     }
 
-    m_picCount++;
+    u_char *data;
+    int width = 0, height = 0, step = 0, cn = 0;
+    cv::Mat copyMat;
+
+    //#publish Video
+    if (m_publishVideoRet) {
+        if (videoPublishElement == nullptr) {
+            string path = appConfig.getStringProperty("srsAddr") + "cam" + m_camId + ".flv";
+            cv::Size size_(appConfig.getIntProperty("pulish.width"), appConfig.getIntProperty("pulish.height"));
+            int gupIdx = appPref.getIntData("gpu.index");
+            videoPublishElement = new ffmpeg::PipeVideoPublishElement(path, size_, "flv", 25, gupIdx);
+            videoPublishElement->start();
+        } else {
+//            DBG("videoPublishElement->setImage()");
+            m_capture->retrieveFrame(0, &data, &step, &width, &height, &cn);
+            cv::Mat img(height, width, CV_8UC3, data, step);
+            img.copyTo(copyMat);
+
+            if (!copyMat.empty()) {
+                videoPublishElement->setImage(copyMat);
+                videoPublishElement->submit();
+            } else {
+                DBG("copyMat.empty()");
+            }
+        }
+    } else {
+        if (videoPublishElement != nullptr) {
+            DBG("videoPublishElement->stop()");
+            videoPublishElement->stop();
+            videoPublishElement->wait();
+            delete videoPublishElement;
+            videoPublishElement = nullptr;
+        }
+    }
+
     //鍑犲紶閫変竴寮犳斁鍏edis
+    m_picCount++;
     if (m_picCount % m_nPicsPickOne != 0) {
         return;
     } else {
         m_picCount.store(0);
     }
 
-    {
-        u_char *data;
-        int width = 0, height = 0, step = 0, cn = 0;
+    if (copyMat.empty()) {
+//        ERR("copyMat.empty()");
         m_capture->retrieveFrame(0, &data, &step, &width, &height, &cn);
-
         cv::Mat img(height, width, CV_8UC3, data, step);
-        cv::Mat copyMat;
         img.copyTo(copyMat);
-        std::string imageName = m_capture->GetImageName();
-        m_pManager->SaveImageToRedis(m_camId,imageName,copyMat);
     }
+    std::string imageName = m_capture->GetImageName();
+    m_pManager->SaveImageToRedis(m_camId, imageName, copyMat);
+
     fireConnectors();
 }
 
-std::string RtspCaptureElement::MakeDir(const std::string &timeStamp)
-{
+std::string RtspCaptureElement::MakeDir(const std::string &timeStamp) {
     std::string t_FilePath = m_cutPath;
 
     if (t_FilePath.back() != '/') {
@@ -68,11 +103,11 @@
     char buf[24];
     QDateTime dt = QDateTime::fromString(QString::fromStdString(timeStamp), "yyyy-MM-dd hh:mm:ss:zzz");
 
-    std::string t_strTime=dt.toString("yyyyMMddhh").toStdString();
+    std::string t_strTime = dt.toString("yyyyMMddhh").toStdString();
     // DBG("t_strTime="<<t_strTime);
-    t_FilePath.append(m_camId + "/" + t_strTime.substr(0, 6)+ "/" +t_strTime.substr(6, 2) + "/");
+    t_FilePath.append(m_camId + "/" + t_strTime.substr(0, 6) + "/" + t_strTime.substr(6, 2) + "/");
     //YYYYMMDDHH
-    t_FilePath.append(t_strTime.substr(0,10)+ "/");
+    t_FilePath.append(t_strTime.substr(0, 10) + "/");
     std::string t_cmd = "mkdir -p '";
     t_cmd.append(t_FilePath + "'");
     //#get path mkdir path
@@ -80,36 +115,44 @@
 
     return t_FilePath;
 }
-void RtspCaptureElement::SaveVideo(const std::string &strImageName)
-{
-    std::string strTimeStamp= AppUtil::getTimeUSecString();
-    std::string strPath=MakeDir(strTimeStamp);
-    m_capture->SaveVideoByImageName(strPath,strImageName);
+
+void RtspCaptureElement::SaveVideo(const std::string &strImageName) {
+    INFO("SaveVideo: " << strImageName);
+    std::string strTimeStamp = AppUtil::getTimeUSecString();
+    std::string strPath = MakeDir(strTimeStamp);
+    m_capture->SaveVideoByImageName(strPath, strImageName);
 }
 
 
-void RtspCaptureElement::openVideo()
-{
-    if(m_gpuIndex>=0){
-        setenv("CUDA_VISIBLE_DEVICES", std::to_string(m_gpuIndex).c_str(),0);
+void RtspCaptureElement::openVideo() {
+    if (m_gpuIndex >= 0) {
+        setenv("CUDA_VISIBLE_DEVICES", std::to_string(m_gpuIndex).c_str(), 0);
     }
-    m_capture->open(m_path.c_str(),m_gpuIndex>=0);
+    INFO("Open Video " << m_path << "  GPU_Index: " << m_gpuIndex);
+    m_capture->open(m_path.c_str(), m_gpuIndex >= 0);
 }
 
-void RtspCaptureElement::threadInitial()
-{
+void RtspCaptureElement::threadInitial() {
+    INFO("MYH DEBUG");
     openVideo();
 }
 
-void RtspCaptureElement::threadClosing()
-{
+void RtspCaptureElement::threadClosing() {
+    INFO("MYH DEBUG");
     m_capture->close();
     delete m_capture;
     m_capture = nullptr;
 }
 
-void RtspCaptureElement::SetVideoMinMaxSeconds(const int minSeconds, const int maxSeconds)
-{
-    m_capture->SetMinMaxVideoSeconds(minSeconds,maxSeconds);
+void RtspCaptureElement::SetVideoMinMaxSeconds(const int minSeconds, const int maxSeconds) {
+    m_capture->SetMinMaxVideoSeconds(minSeconds, maxSeconds);
+}
+
+void RtspCaptureElement::startPublishVideo() {
+    m_publishVideoRet = true;
+}
+
+void RtspCaptureElement::stopPublishVideo() {
+    m_publishVideoRet = false;
 }
 

--
Gitblit v1.8.0