#include "NewRecordVideoElement.h"
|
#include <basic/util/app/AppPreference.hpp>
|
|
//#todo index int -> string
|
NewRecordVideoElement::NewRecordVideoElement(std::string camid) :
|
videoEncoderElement(cv::Size(1920, 1080), 8, 0), camID(camid) {
|
|
// basicPath();
|
m_cutPath = appPref.getStringData("user.loop.absolute.path");
|
}
|
|
NewRecordVideoElement::~NewRecordVideoElement() {
|
std::queue<ImgInfo> empty;
|
empty.swap(m_imgBufQue);
|
|
//m_imgBufQue.clear();
|
}
|
|
std::string NewRecordVideoElement::startRecord() {
|
|
// ImgInfo info=m_HiredisTool.getImage(camID);
|
ImgInfo info;
|
getImg(info);
|
std::string srcPath = getFileName(info.time);
|
|
// DBG("fisrt fps time"<<info.time);
|
try {
|
videoEncoderElement.threadInitial(srcPath, info.img);
|
}
|
catch (std::exception &e) {
|
ERR(e.what())
|
}
|
return srcPath;
|
}
|
|
void NewRecordVideoElement::endRecord() {
|
doRecord();
|
doRecord();
|
doRecord();
|
videoEncoderElement.threadClosing();
|
}
|
|
void NewRecordVideoElement::doRecord() {
|
|
// ImgInfo info=m_HiredisTool.getImage(camID);
|
ImgInfo info;
|
getImg(info);
|
|
// DBG(" time="<<info.time);
|
videoEncoderElement.doFunc(info.img);
|
}
|
|
std::string NewRecordVideoElement::getFileName(std::string timeStamp) {
|
std::string dirPath = makeDir();
|
|
char szDateTime[256] = {0};
|
sprintf(szDateTime, "%s%s.mp4", dirPath.c_str(), timeStamp.c_str());
|
DBG(szDateTime);
|
return szDateTime;
|
}
|
|
|
std::string NewRecordVideoElement::makeDir() {
|
|
//# ./camIndex/YYYYMM/DD/YYYYMMDDHH/
|
std::string t_FilePath = m_cutPath;
|
|
if (t_FilePath.back() != '/') {
|
t_FilePath.push_back('/');
|
}
|
char buf[24];
|
|
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/
|
t_FilePath.append(camID + "/" + t_strTime.substr(0, 6) + "/" + t_strTime.substr(6, 2) + "/");
|
//YYYYMMDDHH
|
t_FilePath.append(t_strTime.substr(0, 10) + "/");
|
std::string t_cmd = "mkdir -p '";
|
t_cmd.append(t_FilePath + "'");
|
//#get path mkdir path
|
system(t_cmd.c_str());
|
|
return t_FilePath;
|
}
|
|
void NewRecordVideoElement::pushImgBuf(const std::string &time, cv::Mat &img) {
|
ImgInfo info;
|
img.copyTo(info.img);
|
// info.img=img;
|
info.time = time;
|
m_imgBufQue.push(info);
|
// int size=m_imgBufQue.size();
|
// DBG("m_imgBufQue size="<<size);
|
}
|
|
void NewRecordVideoElement::getImg(ImgInfo &info) {
|
//todo
|
int len = 20;
|
info = m_imgBufQue.front();
|
int size = m_imgBufQue.size();
|
// DBG("m_imgBufQue size="<<size<<" time="<<info.time);
|
if (size > len) {
|
m_imgBufQue.pop();
|
}
|
|
|
}
|
|
void NewRecordVideoElement::threadFunc() {
|
Record();
|
}
|
|
void NewRecordVideoElement::threadInitial() {
|
recordInit(40, 100);
|
}
|
|
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) {
|
if (isTrigger) {
|
triggerDelay = 0;
|
sdkTrigger = true;
|
} else {
|
if (triggerDelay++ >= fileMin / 2) {
|
sdkTrigger = false;
|
} else {
|
sdkTrigger = true;
|
}
|
}
|
}
|
|
void NewRecordVideoElement::recordInit(int videoMin, int videoMax) {
|
sdkTrigger = false;
|
fileMin = videoMin;
|
fileMax = videoMax;
|
triggerDelay = fileMin / 2;
|
|
recordStatus = RECORD_STOP;
|
videoLength = 0;
|
recordDelay = 0;
|
}
|