#include "RtspCaptureElement.h" #include #include #include #include #include #include #include #include "RtspAnalysManager.h" //#include #include "../../../BasicPlatForm/basic/pipe_element/ffmpeg/FfmpegElement.cpp" RtspCaptureElement::RtspCaptureElement(const std::string &path, const std::string &camId, int fps, int reopenTime, int gpuIndex, RtspAnalysManager *manager) : TimerElement(10), m_path(path), m_gpuIndex(gpuIndex), m_reopenTime(reopenTime), m_camId(camId), m_pManager(manager) { m_cutPath = appPref.getStringData("user.loop.absolute.path"); assert(!m_cutPath.empty()); m_capture = new CvCapture_FFMPEG(m_camId); } //定时抓取图片,通过将图片放入RtspImageRedisElement的队列中,来减少视频的丢帧 void RtspCaptureElement::timerFunc() { 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); } { u_char *data; int width = 0, height = 0, step = 0, cn = 0; 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(); m_pManager->SaveImageToRedis(m_camId, imageName, copyMat); } 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); } INFO("Open Video " << m_path << " GPU_Index: " << m_gpuIndex); m_capture->open(m_path.c_str(), m_gpuIndex >= 0); } void RtspCaptureElement::threadInitial() { INFO("MYH DEBUG"); openVideo(); } void RtspCaptureElement::threadClosing() { INFO("MYH DEBUG"); m_capture->close(); delete m_capture; m_capture = nullptr; } void RtspCaptureElement::SetVideoMinMaxSeconds(const int minSeconds, const int maxSeconds) { m_capture->SetMinMaxVideoSeconds(minSeconds, maxSeconds); }