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/RtspAnalysManager.cpp |  187 +++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 165 insertions(+), 22 deletions(-)

diff --git a/QiaoJiaSystem/VideoToImageMulth/RtspAnalysManager.cpp b/QiaoJiaSystem/VideoToImageMulth/RtspAnalysManager.cpp
index 961c0c5..295ad0b 100644
--- a/QiaoJiaSystem/VideoToImageMulth/RtspAnalysManager.cpp
+++ b/QiaoJiaSystem/VideoToImageMulth/RtspAnalysManager.cpp
@@ -9,14 +9,6 @@
 using std::string;
 void *gRtspAnalysManagerCamera = nullptr;
 
-RtspAnalysManager::RtspAnalysManager(LDBTool *_dbTool) : m_lDBTool(nullptr), m_maxCount(50), m_currentCount(0) {
-//    INFO("MYH DEBUG HERE");
-    m_lDBTool = _dbTool;
-    init();
-
-}
-
-
 static std::string rtspAddrBuild(std::string ip, const int port, std::string username,
                                  std::string passwd, std::string brand) {
     std::string rtsp_url;
@@ -38,6 +30,137 @@
     return rtsp_url;
 }
 
+RtspAnalysManager::RtspAnalysManager(LDBTool *_dbTool) : m_lDBTool(nullptr), m_maxCount(50), m_currentCount(0) {
+//    INFO("MYH DEBUG HERE");
+    m_lDBTool = _dbTool;
+    init();
+
+    IMPORT_APP_ARGS;
+    //nsq   set callback func
+    DBG("NsqMsgConsumer Init");
+    DBG("argv[0]:" << argv[0]);
+    if(argv[0][0] == '.' && argv[0][1] == '/'){
+        string pName(argv[0]+2, argv[0]+strlen(argv[0]));
+        DBG("pName:" << pName);
+        nsqMsgConsumer = new BasicMsg::Nsq::NsqMsgConsumer("127.0.0.1", "4150", pName,
+                                                           to_string(appPref.getIntData("RpcVTIMPort")));
+    }else{
+        string pName(argv[0]);
+        DBG("pName:" << pName);
+        nsqMsgConsumer = new BasicMsg::Nsq::NsqMsgConsumer("127.0.0.1", "4150", pName,
+                                                           to_string(appPref.getIntData("RpcVTIMPort")));
+    }
+
+    nsqMsgConsumer->setMessageCallback(std::bind(&OnMsgFunc, std::placeholders::_1, this));
+    nsqMsgConsumer->init();
+    DBG("NsqMsgConsumer Init END!!");
+
+}
+
+void RtspAnalysManager::OnMsgFunc(void *msgPtr, void *pThisPtr) {
+    evnsq::Message *msg = (evnsq::Message *) msgPtr;
+    DBG("Received a message, id=" << msg->id << " message=[" << msg->body.ToString() << "]");
+    RtspAnalysManager *pthis = (RtspAnalysManager *) pThisPtr;
+    Json::Reader reader;
+    Json::Value value;
+    if (reader.parse(msg->body.ToString(), value)) {
+        auto itor_json_end = value.end();
+        Json::Value cfg_val;
+        for (auto itor_json = value.begin(); itor_json != itor_json_end; ++itor_json) {
+            //#todo 浣跨敤绛栫暐妯″紡?鍙戦�佺澶氫釜鎸囦护绱Н鍚庡彂閫�?
+            if (itor_json->isString() && reader.parse(itor_json->asString(), cfg_val)) {
+                DBG("itor_json.name():" << itor_json.name());
+                if (itor_json.name() == "cut_dura_edit") {
+                    int cut_max_duration = cfg_val["cut_max_duration"].asInt();
+                    int cut_min_duration = cfg_val["cut_min_duration"].asInt();
+                    DBG("cut_max_duration: " << cut_max_duration << "   cut_min_duration: " << cut_min_duration);
+                    appPref.setIntData("n_cut_max_duration", cut_max_duration);
+                    appPref.setIntData("n_cut_min_duration", cut_min_duration);
+
+                    DBG("cut_max_duration: " << cut_max_duration << "   cut_min_duration: " << cut_min_duration);
+//        m_GB28181_Enable = appPref.getIntData("GB28181_Enable");
+                    //#todo GB28181
+                    if (pthis->m_GB28181_Enable) {
+                        for (auto &item : pthis->m_controllers_videoCapElem) {
+                            item.second->SetVideoMinMaxSeconds(cut_min_duration, cut_max_duration);
+                        }
+                    } else {
+                        for (auto &item : pthis->m_controllers) {
+                            item.second->SetVideoMinMaxSeconds(cut_min_duration, cut_max_duration);
+                        }
+                    }
+                } else if (itor_json.name() == "cam_edit") {
+                    string cam_idx = "";
+                    if(pthis->m_GB28181_Enable){
+                        cam_idx = cfg_val["equipCode"].asString();
+                    }else{
+                        cam_idx = cfg_val["str_cam_dev_id"].asString();
+                    }
+
+                    if (pthis->m_GB28181_Enable) {
+                        if (pthis->m_controllers_videoCapElem.find(cam_idx) !=
+                            pthis->m_controllers_videoCapElem.end()) {
+                            INFO("cam add is " << cam_idx);
+                            pthis->addCamera(cam_idx, "GB28181");
+                        }
+                    } else {
+                        if (pthis->m_controllers.find(cam_idx) != pthis->m_controllers.end()) {
+                            std::string rtsp_url = rtspAddrBuild(cfg_val["str_ip"].asString(), 554,
+                                                                 cfg_val["str_username"].asString(),
+                                                                 cfg_val["str_password"].asString(),
+                                                                 cfg_val["str_brand"].asString());
+                            INFO("cam add is " << cfg_val["str_addr"].asString());
+                            pthis->addCamera(cam_idx, rtsp_url);
+                        }
+                    }
+                } else if (itor_json.name() == "cam_del") {
+                    string cam_idx = "";
+                    if(pthis->m_GB28181_Enable){
+                        cam_idx = cfg_val["equipCode"].asString();
+                    }else{
+                        cam_idx = cfg_val["str_cam_dev_id"].asString();
+                    }
+                    pthis->removeCamera(cam_idx);
+                } else if (itor_json.name() == "cam_startPublish") {
+                    string cam_idx = cfg_val["id"].asString();
+                    if (pthis->m_GB28181_Enable) {
+                        if (pthis->m_controllers_videoCapElem.find(cam_idx) !=
+                            pthis->m_controllers_videoCapElem.end()) {
+                            INFO("cam add is " << cam_idx);
+                            pthis->m_controllers_videoCapElem[cam_idx]->startPublishVideo();
+                        }
+                    } else {
+                        if (pthis->m_controllers.find(cam_idx) != pthis->m_controllers.end()) {
+                            INFO("cam add is " << cam_idx);
+                            pthis->m_controllers[cam_idx]->startPublishVideo();
+                        }
+                    }
+                } else if (itor_json.name() == "cam_stopPublish") {
+                    string cam_idx = cfg_val["id"].asString();
+                    if (pthis->m_GB28181_Enable) {
+                        if (pthis->m_controllers_videoCapElem.find(cam_idx) !=
+                            pthis->m_controllers_videoCapElem.end()) {
+                            INFO("cam add is " << cfg_val["str_addr"].asString());
+                            pthis->m_controllers_videoCapElem[cam_idx]->stopPublishVideo();
+                        }
+                    } else {
+                        if (pthis->m_controllers.find(cam_idx) != pthis->m_controllers.end()) {
+                            INFO("cam add is " << cfg_val["str_addr"].asString());
+                            pthis->m_controllers[cam_idx]->stopPublishVideo();
+                        }
+                    }
+                }
+            } else {
+                DBG("itor value is not string ");
+                continue;
+            }
+        }
+    } else {
+        ERR("Error Message!!");
+    }
+}
+
+
 //鍒濆鍖栧嚱鏁�
 void RtspAnalysManager::init() {
 //    INFO("MYH DEBUG HERE")
@@ -55,14 +178,33 @@
         appPref.setIntData("n_cut_max_duration", lst_dev.n_cut_max_duration);
         appPref.setIntData("n_cut_min_duration", lst_dev.n_cut_min_duration);
         if (lst.size() > 0) {
-            for (auto item : lst) {
-                std::string t_camIdex = item.str_cam_dev_id.toStdString();
+            int startCamNO = appPref.getIntData("CamStartNO");
+            int CamCount = appPref.getIntData("CamEndNO") - appPref.getIntData("CamStartNO") + 1;
+            INFO("StartCamNO: " << startCamNO << " CamCount: " << CamCount);
+            auto itor = lst.begin();
+
+            if (startCamNO >= lst.size()) {
+                ERR("startCamNO > lst.size()");
+                return;
+            }
+
+            for (int i = 0; i < startCamNO; i++) {
+                itor++;
+            }
+
+            for (int i = 0; i < CamCount; i++) {
+                if (itor == lst.end()) {
+                    ERR("itor == lst.end()");
+                    return;
+                }
+                std::string t_camIdex = itor->str_cam_dev_id.toStdString();
                 std::string rtsp_url = "GB28181";
-//            rtspAddrBuild(item.str_ip.toStdString(), 554, item.str_username.toStdString(),
-//                                                 item.str_password.toStdString(), item.str_brand.toStdString());
-                INFO("cam add is " << item.str_addr.toStdString());
+
+                INFO("cam add is " << itor->str_addr.toStdString());
+
                 addCamera(t_camIdex, rtsp_url);
                 sleep(1);
+                itor++;
             }
         } else {
             ERR("searchCamDevTableByType size is 0");
@@ -175,7 +317,8 @@
 //                25, 0, 0, this);
 
             m_controllers_videoCapElem[index] = new PushStreamAppPipeController(const_cast<string &>(index),
-                                                                                25, 0, 0, this);
+                                                                                25, 0, appPref.getIntData("gpu.index"),
+                                                                                this);
             m_controllers_videoCapElem[index]->SetVideoMinMaxSeconds(lst_dev.n_cut_min_duration,
                                                                      lst_dev.n_cut_max_duration);
             m_controllers_videoCapElem[index]->start();
@@ -193,7 +336,7 @@
         //    auto lst = m_lDBTool->searchCamDevTableAll();
         Record_Config lst_dev = m_lDBTool->searchConfigTableWithinServerInfo();
         if (m_controllers.find(index) == m_controllers.end()) {
-            INFO("MYH DEBUG HERE");
+//            INFO("MYH DEBUG HERE");
             if (m_currentCount >= m_maxCount) {
                 ERR("addCamera faild, camera's num is full!")
                 return -1;
@@ -202,7 +345,7 @@
             m_imgRedisControllers[index] = new RtspImageRedisElement(index);
             m_imgRedisControllers[index]->start();
 
-            m_controllers[index] = new RtspCaptureElement(rtsp, index, 25, 3000, 0, this);
+            m_controllers[index] = new RtspCaptureElement(rtsp, index, 25, 3000, appPref.getIntData("gpu.index"), this);
             m_controllers[index]->SetVideoMinMaxSeconds(lst_dev.n_cut_min_duration, lst_dev.n_cut_max_duration);
             m_controllers[index]->start();
             m_currentCount++;
@@ -224,7 +367,7 @@
  * @return 鎬绘槸0
  */
 int RtspAnalysManager::removeCamera(const std::string &index) {
-    INFO("MYH DEBUG HERE");
+//    INFO("MYH DEBUG HERE");
     if (m_GB28181_Enable) {
         if (m_controllers_videoCapElem.find(index) == m_controllers_videoCapElem.end())return -1;
         auto controller = m_controllers_videoCapElem[index];
@@ -250,7 +393,7 @@
     delete imgRedis;
     m_imgRedisControllers.erase(index);
     m_imgRedisCRwLock.unlock();
-    INFO("MYH DEBUG HERE");
+//    INFO("MYH DEBUG HERE");
     return 0;
 }
 
@@ -311,7 +454,7 @@
 
 //褰曞彇瑙嗛鐨凴PC鐨勬帴鍙e嚱鏁�
 ::std::string RtspAnalysManager::recordVideo(const ::std::string &name, const ::Ice::Current &) {
-    INFO("Record Video For: " << name);
+//    INFO("Record Video For: " << name);
     ImageName_s_t nameSt = ImageName_s_t::fromString(name);
     if (nameSt.Valid()) {
         if (m_GB28181_Enable) {
@@ -338,15 +481,15 @@
 
 //淇濆瓨瑙嗛鍒癛tspImageRedis鐨勯槦鍒�,鐢盧tspCaptureElement璋冪敤
 bool RtspAnalysManager::SaveImageToRedis(const std::string &camId, const std::string &imageName, const cv::Mat &img) {
-    INFO("MYH DEBUG HERE  " << camId);
+//    INFO("MYH DEBUG HERE  " << camId);
 
 //    m_imgRedisCRwLock.rdlock();
     auto item = m_imgRedisControllers.find(camId);
     if (item != m_imgRedisControllers.end()) {
-        INFO("Save Succeed Cam: " << camId << " ImageKey: " << imageName);
+//        INFO("Save Succeed Cam: " << camId << " ImageKey: " << imageName);
         item->second->SaveImage(imageName, img);
     } else {
-        ERR("Save Failed Cam: " << camId << " ImageKey: " << imageName);
+//        ERR("Save Failed Cam: " << camId << " ImageKey: " << imageName);
     }
 //    m_imgRedisCRwLock.unlock();
 //    INFO("MYH DEBUG HERE");

--
Gitblit v1.8.0