#include "RtspCaptureElement.h" #include #include #include #include #include #include #include #include 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; int width = 0, height = 0, step = 0, cn = 0; DBG("GRABFrame "<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); } //每个Cam的队列最多M_CAM_PIC_MAX_COUNT张 { /*ClockTimer timer("RedisTime "); auto imageCount = m_redisTool.getSize(m_camId); if (imageCount > M_CAM_PIC_MAX_COUNT) { ERR("Too Much Pics In Cam " << m_camId << " QUeue"); return; }*/ 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(); /*ClockTimer SetImageValueTimer("RedisTimeSetKey "); m_redisTool.setKeyImage(imageName, copyMat); ClockTimer SetImageNameTimer("RedisTimeSetKey "); m_redisTool.listLpush(m_camId, imageName);*/ } 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="<SaveVideoByImageName(strPath,strImageName); } void RtspCaptureElement::openVideo() { if(m_gpuIndex>=0){ setenv("CUDA_VISIBLE_DEVICES", std::to_string(m_gpuIndex).c_str(),0); } m_capture->open(m_path.c_str(),m_gpuIndex>=0); } void RtspCaptureElement::threadInitial() { openVideo(); } void RtspCaptureElement::threadClosing() { m_capture->close(); delete m_capture; m_capture = nullptr; } void RtspCaptureElement::SetVideoMinMaxSeconds(const int minSeconds, const int maxSeconds) { m_capture->SetMinMaxVideoSeconds(minSeconds,maxSeconds); }