From c67513257f08f472d2afe710dd8beacfdfb0897d Mon Sep 17 00:00:00 2001 From: zhangjixing <775834166@qq.com> Date: 星期六, 05 一月 2019 19:47:37 +0800 Subject: [PATCH] commit add new file --- QiaoJiaSystem/build/FaceDetectResourcesTest | 0 QiaoJiaSystem/StructureApp/HiredisTool.cpp | 379 ++++++++++++++++++++++++++++++++++ .gitignore | 6 QiaoJiaSystem/build/RapidStructureApp | 0 QiaoJiaSystem/StructureApp/HiredisTool.h | 55 +++++ QiaoJiaSystem/StructureApp/NewEncodeVideo.h | 25 ++ QiaoJiaSystem/build/TestCilent | 0 QiaoJiaSystem/build/RecordVideo | 0 QiaoJiaSystem/StructureApp/NewEncodeVideoManager.cpp | 59 +++++ QiaoJiaSystem/StructureApp/NewEncodeVideoManager.h | 20 + QiaoJiaSystem/StructureApp/NewEncodeVideo.cpp | 60 +++++ QiaoJiaSystem/build/RapidStructureAppRtsp | 0 12 files changed, 604 insertions(+), 0 deletions(-) diff --git a/.gitignore b/.gitignore index 6dde67c..ccb23fd 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,9 @@ QiaoJiaSystem/\.idea/ QiaoJiaSystem/cmake-build-debug/ + +*.cmake + +build-QiaoJiaSystem-unknown-Default/ + +QiaoJiaSystem/build/glog_dump\.log diff --git a/QiaoJiaSystem/StructureApp/HiredisTool.cpp b/QiaoJiaSystem/StructureApp/HiredisTool.cpp new file mode 100644 index 0000000..2094325 --- /dev/null +++ b/QiaoJiaSystem/StructureApp/HiredisTool.cpp @@ -0,0 +1,379 @@ +#include "HiredisTool.h" +#include <vector> +#include<basic/util/opencv/CvUtil.h> +#include <jsoncpp/json/json.h> +#include <basic/debug/Debug.h> +#include <basic/util/app/AppConfig.h> +HiredisTool::HiredisTool(): +m_redis(nullptr), +m_nImgListLen(0) +{ + init(); +} +HiredisTool::~HiredisTool() +{ + if(m_redis != nullptr) + { + redisFree(m_redis);//鏋愭瀯鍑芥暟閲婃斁璧勬簮 + DBG("~RedisTool :: free redis connection ") ; + } + +} + +void HiredisTool::init() +{ + struct timeval timeout = { 1, 500000 }; // 1.5 seconds 璁剧疆杩炴帴绛夊緟鏃堕棿 + + // m_nImgListLen=appConfig.getIntProperty("redis_buf_len"); + std::string ip=appConfig.getStringProperty("redis_ip"); + + int port=6379; + m_redis = redisConnectWithTimeout(ip.c_str(), port, timeout);//寤虹珛杩炴帴 + if (m_redis->err) { + + DBG("RedisTool : Connection error: %s"<< m_redis->errstr); + } + else + { + DBG( "init redis tool success " ); + + } +} + + + + + +//std::map<std::string,int> HiredisTool::findAllCamera() +//{ +// std::map<std::string,int> camIdMap; +// if(!checkParam()) +// { +// return camIdMap; +// } + +// redisReply *reply; + +// reply = (redisReply*)redisCommand(m_redis,"hgetall %s", cam_key); + +// if(!checkResult(reply)) +// { +// return camIdMap; +// } +// if(reply->type == REDIS_REPLY_ARRAY) +// { +// for (int i= 0; i < reply->elements; i+=2) +// { +// std::string key=reply->element[i]->str; +// std::string value=reply->element[i+1]->str; +// if(key.empty()||value.empty()) continue; +// camIdMap.insert(std::pair<std::string,int>(key,atoi(value.c_str()))); +// } +// } +// freeReplyObject(reply); +// return camIdMap; +//} + + + + + + +bool HiredisTool::pushImageBuf(const std::string& file_name,const cv::Mat& img) +{ + + + std::vector<uchar> buf; + Json::Value json; + + CvUtil::cvMat2Buffer(img,buf); +// json["time"]=info.time; + json["img"]=std::string(buf.begin(),buf.end()); + // buf.clear(); + std::string strBuf=std::string(buf.begin(),buf.end()); + if(!listLpush(file_name,json.toStyledString())); + { + + return false; + } + + //DBG(json.toStyledString()); + + return true; +} + +void HiredisTool::getImage(const std::string& file_name, cv::Mat& img) +{ + + + std::string content; + cv::Mat imgTep; + + + listRpop(file_name,content); + + + Json::Reader reader; + Json::Value value; + if(!reader.parse(content,value)) + { + return ; + } + + std::string str=value["img"].asString(); + std::vector<uchar> data; + + data.resize(str.size()); + data.assign(str.begin(),str.end()); + CvUtil::buffer2CvMat(data,imgTep); + imgTep.copyTo(img); + // return img; + +} +//bool HiredisTool::setCameraState(const std::string& cam_id,const int& type) +//{ +// DBG("type="<<type); +// return hashSet(file_list,cam_id,type); +//} +bool HiredisTool::clearAllImageBuf() +{ +// auto cam_map=findAllCamera(); +// for(auto cam:cam_map) +// { +// delKey(cam.first); +// } +} +bool HiredisTool::listLpush(const std::string& key,const std::string& value) +{ + if(!checkParam()) + { + return false; + } + redisReply *reply; + + reply = (redisReply*)redisCommand(m_redis,"lpush %s %s", key.c_str(),value.c_str()); + if(!checkResult(reply)) + { + return false; + } + + freeReplyObject(reply); + + return true; +} + +bool HiredisTool::listRpop(const std::string& key,std::string& value) +{ + if(!checkParam()) + { + return false; + } + redisReply *reply; + //1.find all filename + reply = (redisReply*)redisCommand(m_redis,"rpop %s ", key.c_str()); + + if(!checkResult(reply)) + { + return false; + } + if(reply->type == REDIS_REPLY_STRING) + { + value=reply->str; + } + freeReplyObject(reply); + + return true; +} +bool HiredisTool::hashSet(const std::string& tab,const std::string& key,const int& value) +{ + if(!checkParam()) + { + return false; + } + redisReply *reply; + + + reply = (redisReply*)redisCommand(m_redis,"hset %s %s %d", tab.c_str(),key.c_str(),value); + + if(!checkResult(reply)) + { + return false; + } + freeReplyObject(reply); + + + return true; +} + +int HiredisTool::getSize(const std::string& key) +{ + int size=0; + if(!checkParam()) + { + return size; + } + redisReply *reply; + + reply = (redisReply*)redisCommand(m_redis,"llen %s", key.c_str()); + + if(!checkResult(reply)) + { + return size; + } + size=reply->integer; + freeReplyObject(reply); + return size; +} +bool HiredisTool::listLindex(const std::string& key,std::string& value) +{ + int size=0; + if(!checkParam()) + { + return size; + } + redisReply *reply; + + reply = (redisReply*)redisCommand(m_redis,"lindex %s -1", key.c_str()); + + if(!checkResult(reply)) + { + return false; + } + if(reply->type == REDIS_REPLY_STRING) + { + value=reply->str; + } + freeReplyObject(reply); +} +bool HiredisTool::delKey(const std::string& key) +{ + if(!checkParam()) + { + return false; + } + redisReply *reply; + + reply = (redisReply*)redisCommand(m_redis,"del %s", key.c_str()); + + + if(!checkResult(reply)) + { + return false; + } + freeReplyObject(reply); + return true; +} +bool HiredisTool::checkParam() +{ + if(m_redis == nullptr || m_redis->err) + { + DBG("Redis init Error !!!"); + init(); + return false; + } + return true; +} +bool HiredisTool::checkResult(redisReply *reply) +{ + if (reply==nullptr) + { + DBG("reply nullptr !!!"); + return false; + } + + if (reply->type == REDIS_REPLY_ERROR) + { + ERR("redisCommand Error: "<<reply->str); + freeReplyObject(reply); + return false; + } + return true; +} +void HiredisTool::addFileInfo(const std::string& filename,const int& file_status) +{ + + hashSet(file_list,filename,file_status); + } +std::queue<cv::Mat> HiredisTool::getImgQue(const std::string& filename) +{ + std::queue<cv::Mat> que; + if(!checkParam()) + { + return que; + } + redisReply *reply; + + reply = (redisReply*)redisCommand(m_redis,"lrange %s 0 -1", filename.c_str()); + + if(!checkResult(reply)) + { + return que; + } + if(reply->type == REDIS_REPLY_ARRAY) + { + for(int i=0;i<reply->elements;++i) + { + std::string str=reply->element[i]->str; + std::vector<uchar> data; + cv::Mat img; + data.resize(str.size()); + data.assign(str.begin(),str.end()); + CvUtil::buffer2CvMat(data,img); + que.push(img); + } + } + freeReplyObject(reply); +} +std::map<std::string,int> HiredisTool::findAllFileStatus() +{ + std::map<std::string,int> fileMap; + if(!checkParam()) + { + return fileMap; + } + + redisReply *reply; + + reply = (redisReply*)redisCommand(m_redis,"hgetall %s", file_list); + + if(!checkResult(reply)) + { + return fileMap; + } + + if(reply->type == REDIS_REPLY_ARRAY) + { + + for (int i= 0; i < reply->elements; i+=2) + { + std::string key=reply->element[i]->str; + std::string value=reply->element[i+1]->str; + + if(key.empty()||value.empty()) continue; + + fileMap.insert(std::pair<std::string,int>(key,atoi(value.c_str()))); + } + } + freeReplyObject(reply); + return fileMap; +} +bool HiredisTool::hashDel(const std::string& tab,const std::string &key) +{ + if(!checkParam()) + { + return false; + } + redisReply *reply; + + + reply = (redisReply*)redisCommand(m_redis,"hdel %s %s", tab.c_str(),key.c_str()); + + if(!checkResult(reply)) + { + return false; + } + freeReplyObject(reply); + + + return true; +} diff --git a/QiaoJiaSystem/StructureApp/HiredisTool.h b/QiaoJiaSystem/StructureApp/HiredisTool.h new file mode 100644 index 0000000..b57e0ee --- /dev/null +++ b/QiaoJiaSystem/StructureApp/HiredisTool.h @@ -0,0 +1,55 @@ +#ifndef HIREDISTOOL_H +#define HIREDISTOOL_H +#include <opencv/cv.h> +//#include <hiredis.h> +#include "../../BasicPlatForm/libs/hiredis-master/include/hiredis.h" +#include <map> +#include <queue> +struct ImgInfo +{ + std::string time; + cv::Mat img; +}; + +static const char * file_list="file_list"; +class HiredisTool +{ +public: + HiredisTool(); + ~HiredisTool(); + + //bool setCameraState(const std::string& cam_id,const int& type); + // std::map<std::string,int> findAllCamera(); + bool clearAllImageBuf(); + + bool pushImageBuf(const std::string& file_name,const cv::Mat& img); + void getImage(const std::string& file_name,cv::Mat& img); + + + void addFileInfo(const std::string& filename,const int& file_status); + std::queue<cv::Mat> getImgQue(const std::string& filename); + std::map<std::string,int> findAllFileStatus(); + + bool delKey(const std::string& key); + bool listRpop(const std::string& key,std::string& value); + int getSize(const std::string& key); + bool hashDel(const std::string& tab,const std::string &key); + bool hashSet(const std::string& tab,const std::string& key,const int& value); +private: + + + bool listLindex(const std::string& key,std::string& value); +// bool listRpop(const std::string& key,std::string& value); + bool listLpush(const std::string& key,const std::string& value); + + + bool checkParam(); + bool checkResult(redisReply *reply); + void init(); +private: + redisContext *m_redis; + int m_nImgListLen; + +}; + +#endif // HREDISTOOL_H diff --git a/QiaoJiaSystem/StructureApp/NewEncodeVideo.cpp b/QiaoJiaSystem/StructureApp/NewEncodeVideo.cpp new file mode 100644 index 0000000..067444f --- /dev/null +++ b/QiaoJiaSystem/StructureApp/NewEncodeVideo.cpp @@ -0,0 +1,60 @@ +#include "NewEncodeVideo.h" + +NewEncodeVideo::NewEncodeVideo(): +videoEncoderElement(cv::Size(1920, 1080), 8, 0) +{ + +} +NewEncodeVideo::~NewEncodeVideo() +{ + +} + +void NewEncodeVideo::threadFunc() +{ + + std::string src_path=getProperty("src_path"); + if(src_path.empty()) + { + return ; + } + int size=m_hiredisTool.getSize(src_path); + DBG("buf_size="<<size<<" file_name="<<src_path); + bool fileCreate = false; + for(int i=0;i<size;++i) + { + cv::Mat img; + m_hiredisTool.getImage(src_path,img); + if(img.empty()) + continue; + if(!fileCreate) + { + fileCreate = true; + videoEncoderElement.threadInitial(getProperty("src_path"),img); + + } + else + { + videoEncoderElement.doFunc(img); + } + usleep(2*1000); + } + // bool isOpen=false; +// while(!m_imgQue.empty()) +// { +// if(!isOpen) +// { +// videoEncoderElement.threadInitial(getProperty("src_path"),m_imgQue.back()); +// isOpen=true; +// } +// else +// { +// videoEncoderElement.doFunc(m_imgQue.back()); + +// } +// m_imgQue.pop(); +// } + videoEncoderElement.threadClosing(); + m_hiredisTool.delKey(src_path); + m_hiredisTool.hashDel(file_list,src_path); +} diff --git a/QiaoJiaSystem/StructureApp/NewEncodeVideo.h b/QiaoJiaSystem/StructureApp/NewEncodeVideo.h new file mode 100644 index 0000000..c24375f --- /dev/null +++ b/QiaoJiaSystem/StructureApp/NewEncodeVideo.h @@ -0,0 +1,25 @@ +#ifndef NEWENCODEVIDEO_H +#define NEWENCODEVIDEO_H +#include "HiredisTool.h" +#include <basic/pipe/PipeElement.h> +#include <basic/pipe_element/ffmpeg/FfmpegElement.h> +class NewEncodeVideo: public basic::PipeElement +{ +public: + NewEncodeVideo(); + ~NewEncodeVideo(); + void thisEncodeFunc(); + static void encodeFunc(void *arg); + + void setImageQue(std::queue<cv::Mat>& imgQue); + +private: + virtual void threadFunc()override; +private: + + std::queue<cv::Mat> m_imgQue; + HiredisTool m_hiredisTool; + ffmpeg::VideoEncodeElement videoEncoderElement; +}; + +#endif // NEWENCODEVIDEO_H diff --git a/QiaoJiaSystem/StructureApp/NewEncodeVideoManager.cpp b/QiaoJiaSystem/StructureApp/NewEncodeVideoManager.cpp new file mode 100644 index 0000000..a2c04ca --- /dev/null +++ b/QiaoJiaSystem/StructureApp/NewEncodeVideoManager.cpp @@ -0,0 +1,59 @@ +#include "NewEncodeVideoManager.h" +#include "NewRecordVideoElement.h" +#include <basic/util/app/AppConfig.h> +NewEncodeVideoManager::NewEncodeVideoManager(): +TimerElement(1000) +{ + // m_hiredisTool.delKey("") + int thread_num=appConfig.getIntProperty("encode_thread_num"); + for(int i=0;i<thread_num;++i) + { + NewEncodeVideo* newEncodeViedo=new NewEncodeVideo; + if(newEncodeViedo) + { + m_newEncodeVideoVec.push_back(newEncodeViedo); + newEncodeViedo->start(); + } + + } + +} +NewEncodeVideoManager::~NewEncodeVideoManager() +{ + for(int i=0;i<m_newEncodeVideoVec.size();++i) + { + if(m_newEncodeVideoVec[i]) + { + m_newEncodeVideoVec[i]->stop(); + delete m_newEncodeVideoVec[i]; + } + } + +} +void NewEncodeVideoManager::timerFunc() +{ + std::map<std::string,int> fileMap=m_hiredisTool.findAllFileStatus(); +// auto it=fileMap.begin(); + for(auto it=fileMap.begin();it!=fileMap.end();it++) + { + if(it->second == RECORD_ENDING) + { + + + + + for(int i=0;i<m_newEncodeVideoVec.size();++i) + { + if(m_newEncodeVideoVec[i] && !m_newEncodeVideoVec[i]->isBusy()) + { + m_hiredisTool.hashSet(file_list,it->first,RECORD_STOP); + m_newEncodeVideoVec[i]->setProperty("src_path",it->first); + m_newEncodeVideoVec[i]->submit(); + break; + } + } + + + } + } +} diff --git a/QiaoJiaSystem/StructureApp/NewEncodeVideoManager.h b/QiaoJiaSystem/StructureApp/NewEncodeVideoManager.h new file mode 100644 index 0000000..3d6ac72 --- /dev/null +++ b/QiaoJiaSystem/StructureApp/NewEncodeVideoManager.h @@ -0,0 +1,20 @@ +#ifndef NEWENCODEVIDEOMANAGER_H +#define NEWENCODEVIDEOMANAGER_H +#include <basic/pipe/TimerElement.h> +#include "NewEncodeVideo.h" + +class NewEncodeVideoManager: public TimerElement +{ +public: + NewEncodeVideoManager(); + ~NewEncodeVideoManager(); +private: + virtual void timerFunc(); + +private: + HiredisTool m_hiredisTool; + std::vector<NewEncodeVideo*> m_newEncodeVideoVec; + +}; + +#endif // NEWENCODEVIDEOMANAGER_H diff --git a/QiaoJiaSystem/build/FaceDetectResourcesTest b/QiaoJiaSystem/build/FaceDetectResourcesTest new file mode 100755 index 0000000..818b439 --- /dev/null +++ b/QiaoJiaSystem/build/FaceDetectResourcesTest Binary files differ diff --git a/QiaoJiaSystem/build/RapidStructureApp b/QiaoJiaSystem/build/RapidStructureApp new file mode 100755 index 0000000..a4fd3ca --- /dev/null +++ b/QiaoJiaSystem/build/RapidStructureApp Binary files differ diff --git a/QiaoJiaSystem/build/RapidStructureAppRtsp b/QiaoJiaSystem/build/RapidStructureAppRtsp new file mode 100755 index 0000000..4988e29 --- /dev/null +++ b/QiaoJiaSystem/build/RapidStructureAppRtsp Binary files differ diff --git a/QiaoJiaSystem/build/RecordVideo b/QiaoJiaSystem/build/RecordVideo new file mode 100755 index 0000000..a5fa727 --- /dev/null +++ b/QiaoJiaSystem/build/RecordVideo Binary files differ diff --git a/QiaoJiaSystem/build/TestCilent b/QiaoJiaSystem/build/TestCilent new file mode 100755 index 0000000..88b4210 --- /dev/null +++ b/QiaoJiaSystem/build/TestCilent Binary files differ -- Gitblit v1.8.0