#include "RtspCaptureElement.h"
|
#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>
|
RtspCaptureElement::RtspCaptureElement(const std::string &path, const std::string& camId,int fps, int reopenTime, int gpuIndex):
|
TimerElement(10),m_path(path),m_gpuIndex(gpuIndex),
|
m_reopenTime(reopenTime),m_camId(camId){
|
m_cutPath= appPref.getStringData("user.loop.absolute.path");
|
|
assert(!m_cutPath.empty());
|
m_capture = new CvCapture_FFMPEG(m_camId);
|
}
|
|
//定时执行,将
|
void RtspCaptureElement::timerFunc()
|
{
|
u_char *data = nullptr;
|
int width = 0;
|
int height = 0;
|
int step = 0;
|
int cn = 0;
|
DBG("GRABFrame "<<m_camId);
|
bool ret = m_capture->grabFrame();
|
if (!ret) {
|
if (m_reopenTime < 0) {
|
stop();
|
INFO("grabFrame faild, element stopping");
|
return;
|
} else {
|
usleep(m_reopenTime * 100);
|
INFO("grabFrame faild, try reopen video: " << m_path);
|
openVideo();
|
ret = m_capture->grabFrame();
|
if (!ret)return;
|
}
|
}
|
|
m_picCount++;
|
//几张选一张放入Redis
|
if (m_picCount % m_nPicsPickOne != 0) {
|
return;
|
} else {
|
m_picCount.store(0);
|
}
|
|
{
|
ClockTimer timer("FrameToImage");
|
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();
|
}
|
fireConnectors();
|
}
|
|
std::string RtspCaptureElement::MakeDir(const std::string &timeStamp)
|
{
|
std::string t_FilePath = m_cutPath;
|
|
if (t_FilePath.back() != '/') {
|
t_FilePath.push_back('/');
|
}
|
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();
|
// DBG("t_strTime="<<t_strTime);
|
t_FilePath.append(m_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 RtspCaptureElement::SaveVideo(const std::string &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);
|
}
|
bool bResult = m_capture->open(m_path.c_str(),m_gpuIndex>=0);
|
if(bResult)
|
{
|
INFO("GPUIndex: "<<m_gpuIndex<<" VideoPath:"<<m_path<<" Succeed");
|
} else{
|
ERR("GPUIndex: "<<m_gpuIndex<<" VideoPath:"<<m_path<<" Failed");
|
}
|
}
|
|
//线程启动之前调用,
|
void RtspCaptureElement::threadInitial()
|
{
|
openVideo();
|
}
|
|
//线程结束的时候调用,关闭ffmpeg流
|
void RtspCaptureElement::threadClosing()
|
{
|
m_capture->close();
|
delete m_capture;
|
m_capture = nullptr;
|
}
|
|
//设置保存视频的最小和最大时长
|
void RtspCaptureElement::SetVideoMinMaxSeconds(const int minSeconds, const int maxSeconds)
|
{
|
INFO("VideoMinSeconds: "<<minSeconds<<" VideoMaxSeconds: "<<maxSeconds);
|
m_capture->SetMinMaxVideoSeconds(minSeconds,maxSeconds);
|
}
|