派生自 development/c++

pansen
2019-03-07 d3b7bbe7102cd089680a828f5d8f6402c8cf6342
QiaoJiaSystem/StructureApp/NewRecordVideoElement.cpp
@@ -1,56 +1,75 @@
#include "NewRecordVideoElement.h"
#include <basic/util/app/AppPreference.hpp>
#include <QString>
#include <QDateTime>
//#todo index int -> string
NewRecordVideoElement::NewRecordVideoElement(std::string camid) :
videoEncoderElement(cv::Size(1920, 1080), 9, 0),
camID(camid)
//videoEncoderElement(cv::Size(1920, 1080), 5, 0),
camID(camid),
m_bSaveWH(false)
{
   // basicPath();
    //isRecord=false;
    m_cutPath= appPref.getStringData("user.loop.absolute.path");
     recordInit(appPref.getIntData("n_cut_min_duration"),appPref.getIntData("n_cut_max_duration"));
}
NewRecordVideoElement::~NewRecordVideoElement() {
m_imgBufQue.clear();
    std::queue<ImgInfo> empty;
    empty.swap(m_imgBufQue);
}
std::string NewRecordVideoElement::startRecord() {
//    ImgInfo info=m_HiredisTool.getImage(camID);
     ImgInfo info;
     getImg(info);
    std::string srcPath= getFileName(info.time);
    try {
        videoEncoderElement.threadInitial(srcPath, info.img);
    m_filename=srcPath;
   // DBG("m_filename: " << m_filename);
    if(!m_bSaveWH)
    {
       m_bSaveWH=m_hiredisTool.hashSet(camID,"width",info.img.cols) && m_hiredisTool.hashSet(camID,"height",info.img.rows);
       DBG("m_bSaveWH="<<m_bSaveWH);
    }
    catch (std::exception &e) {
        ERR(e.what())
    }
    m_hiredisTool.pushImageBuf(m_filename,info.img);
     m_hiredisTool.addFileInfo(m_filename,RECORD_DOING);
    return srcPath;
}
void NewRecordVideoElement::endRecord() {
    ImgInfo info;
    getImg(info);
    videoEncoderElement.threadClosing();
//    DBG("m_filename: " << m_filename);
//    DBG("recordDelay:" << recordDelay);
//    DBG("videoLength:" << videoLength);
//    DBG("sdkTrigger:" << sdkTrigger);
    m_hiredisTool.pushImageBuf(m_filename,info.img);
    m_hiredisTool.addFileInfo(m_filename,RECORD_ENDING);
}
void NewRecordVideoElement::doRecord() {
//    ImgInfo info=m_HiredisTool.getImage(camID);
  //  ImgInfo info=m_HiredisTool.getImage(camID);
     ImgInfo info;
     getImg(info);
     DBG(" time="<<info.time);
    videoEncoderElement.doFunc(info.img);
     m_hiredisTool.pushImageBuf(m_filename,info.img);
     m_hiredisTool.addFileInfo(m_filename,RECORD_DOING);
}
 std::string NewRecordVideoElement::getFileName(std::string timeStamp)
 std::string NewRecordVideoElement::getFileName(const std::string& timeStamp)
{
    std::string dirPath=makeDir();
    std::string dirPath=makeDir(timeStamp);
    char szDateTime[256] = {0};
    sprintf(szDateTime, "%s%s.mp4", dirPath.c_str(), timeStamp.c_str());
@@ -59,7 +78,7 @@
}
std::string NewRecordVideoElement::makeDir() {
std::string NewRecordVideoElement::makeDir(const std::string& timeStamp) {
    //# ./camIndex/YYYYMM/DD/YYYYMMDDHH/
    std::string t_FilePath = m_cutPath;
@@ -68,15 +87,13 @@
        t_FilePath.push_back('/');
    }
    char buf[24];
    QDateTime dt = QDateTime::fromString(QString::fromStdString(timeStamp), "yyyy-MM-dd hh:mm:ss:zzz");
     time_t t=time(nullptr);
     // 20180901113048 2018-09-01 11:30:48
     strftime(buf,24,"%Y%m%d%H",localtime(&t));
      std::string t_strTime(buf);
       //# ./camIndex/YYYYMM/DD/
     std::string t_strTime=dt.toString("yyyyMMddhh").toStdString();
    // DBG("t_strTime="<<t_strTime);
     t_FilePath.append(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
@@ -84,29 +101,142 @@
    return t_FilePath;
}
void NewRecordVideoElement::pushImgBuf(const std::string& time,cv::Mat img)
void NewRecordVideoElement::pushImgBuf(const std::string& time,cv::Mat& img)
{
//    std::string tmp="camId="+camID+"pushImgBuf";
//    ClockTimer cl(tmp);
     ImgInfo info;
     info.img=img;
     img.copyTo(info.img);
//     info.img=img;
     info.time=time;
     m_imgBufQue.push_front(info);
     m_imgBufQue.push(info);
    // if(isRecord)
    // DBG("camId="<<camID<<"pushtime="<<info.time);
//     int size=m_imgBufQue.size();
//     DBG("m_imgBufQue size="<<size);
}
 void NewRecordVideoElement::getImg(ImgInfo& info)
 {
        //todo
        int len=10;
         info=m_imgBufQue.back();
//     std::string tmp="camId="+camID+"getImg";
//     ClockTimer cl(tmp);
        info=m_imgBufQue.front();
        int size=m_imgBufQue.size();
        //DBG("m_imgBufQue size="<<size<<" time="<<info.time);
        if(size>len)
//        DBG("m_imgBufQue size="<<size<<" camId="<<camID<<" fileMin="<<fileMin/2);
        if(size>fileMin/2)
        {
             m_imgBufQue.pop_back();
             m_imgBufQue.pop();
        }
//        if(isRecord)
//        DBG("camId="<<camID<<"gettime="<<info.time);
 }
// void NewRecordVideoElement::threadFunc()
// {
//       Record();
// }
// void NewRecordVideoElement::threadInitial()
// {
// }
 void NewRecordVideoElement::Record() {
     switch(recordStatus)
     {
         case RECORD_STOP:
 //        DBG("recordDelay:" << recordDelay);
 //        DBG("videoLength:" << videoLength);
 //        DBG("sdkTrigger:" << sdkTrigger);
             videoLength = 0;
             recordDelay = 0;
             if(sdkTrigger){
                 recordStatus = RECORD_DOING;
                 startRecord();
             }
             else
             {
                 ImgInfo info;
                 getImg(info);
             }
             break;
         case RECORD_DOING:
             videoLength++;
             if(sdkTrigger){
                 if(videoLength < fileMax){
                     doRecord();
                 }
                 else
                 {
                     recordStatus = RECORD_STOP;
                     endRecord();
                 }
             }
             else
             {
                 recordStatus = RECORD_ENDING;
                 doRecord();
             }
             break;
         case RECORD_ENDING:
//             DBG("recordDelay:" << recordDelay);
//             DBG("videoLength:" << videoLength);
//             DBG("sdkTrigger:" << sdkTrigger);
             recordDelay++;
             videoLength++;
             if(sdkTrigger){
                 if( (recordDelay < fileMin/4) &&
                     (videoLength < fileMax)){
                     doRecord();
                 }else{
                     recordStatus = RECORD_STOP;
                     endRecord();
                 }
             }else{
                 if( (recordDelay < fileMin/2) &&
                     (videoLength < fileMax)){
                    doRecord();
                 }else{
                     recordStatus = RECORD_STOP;
                     endRecord();
                 }
             }
             break;
         default:
             break;
     }
 }
 void NewRecordVideoElement::setSdkTrigger(bool isTrigger) {
//     std::string tmp="camId="+camID+"setSdkTrigger";
//     ClockTimer cl(tmp);
     if(isTrigger)
     {
//         DBG("setSdkTrigger time=" << AppUtil::getTimeUSecString());
         triggerDelay = 0;
         sdkTrigger = true;
     }
     else
     {
         if(triggerDelay++ >= fileMin/2) {
             sdkTrigger = false;
         }
         else{
             sdkTrigger = true;
         }
     }
     Record();
 }
 void NewRecordVideoElement::recordInit(int videoMin, int videoMax) {
     sdkTrigger = false;
     fileMin = videoMin*8;//fps=8
     fileMax = videoMax*8;
     triggerDelay = fileMin/2;
     recordStatus = RECORD_STOP;
     videoLength = 0;
     recordDelay = 0;
 }