#include "VideoRecorder.h" #include #include VideoRecorder::VideoRecorder(const std::string &fullPath,const std::string &uuid, const std::string &strRtsp, const int mseconds,LDBTool* db) : m_pInFmtCtx(nullptr), m_pInVideoStream(nullptr), m_pOutFmtCtx(nullptr), m_pDict(nullptr), m_pOutVideo_stream(nullptr), m_bStatus(true),/*m_thd(recodeFunc,this),*/ m_bStart(false), m_mseconds(mseconds), m_strfileName(""), m_uuid(uuid), m_bSave(false), m_fullPath(fullPath),db_c(db){ m_CommonFFmpeg.init(); for (int i = 0; i < 3; i++) { if (m_CommonFFmpeg.rtspInputOpen(strRtsp.c_str()) >= 0) { m_thd = new std::thread(recodeFunc, this); return; } usleep(20000); } } VideoRecorder::~VideoRecorder() { m_bSave = false; m_bStatus = false; m_thd->join(); delete m_thd; m_thd = nullptr; m_CommonFFmpeg.rtspInputClose(); } void VideoRecorder::thisRecodeFunc() { while (m_bStatus) { // std::string strDate; //#todo get FilePath FileName ///#todo shengcheng luzhi wenjian ming long lDate = getCurrentUs(); bool bFirstKeyFrame = true; long int nFirstKeyPts = 0; long int nFirstKeyDts = 0; if (m_strfileName.empty() && !m_bSave) { GetFileName(m_strfileName); DBG("srtPath" << m_strfileName << "m_uuid" << m_uuid); } else{ DBG("m_strfileName:" << m_strfileName); DBG("m_bSave:" << m_bSave); } m_CommonFFmpeg.fileOutOpen(m_strfileName.c_str(), m_CommonFFmpeg.getRtspStream()); //#todo while (m_bStatus && ((getCurrentUs() - lDate) < m_mseconds * 1000)) { void *handle = m_CommonFFmpeg.rtspCreatePkg(); if (m_CommonFFmpeg.rtspReadPkg(handle) < 0) { if(bFirstKeyFrame){ ERR("rtspReadPkg m_strfileName:" << m_strfileName.c_str() << "getRtspStream:" << m_CommonFFmpeg.getRtspStream()); usleep(20000);//20ms m_bSave = false; m_strfileName.clear(); break; } else{ continue; } } if (bFirstKeyFrame) { if (m_CommonFFmpeg.isKeyFrame(handle)) { DBG("I"); nFirstKeyPts = m_CommonFFmpeg.getPkgPts(handle); nFirstKeyDts = m_CommonFFmpeg.getPkgDts(handle); bFirstKeyFrame = false; if (!m_bSave && db_c) { if (db_c->insertRecordVedioPath(QString::fromStdString(m_uuid), QString::fromStdString(m_strfileName))) { m_bSave = true; } else { ERR("insertRecordVedioPath DB ERR m_uuid=" << m_uuid.c_str() << "m_strfileName=" << m_strfileName.c_str()); } } } else { // DBG("slice"); m_CommonFFmpeg.rtspDestroyPkg(handle); continue; DBG("srtPath" << m_strfileName << "m_uuid" << m_uuid); } } m_CommonFFmpeg.conversion(handle, nFirstKeyPts, nFirstKeyDts, m_CommonFFmpeg.getRtspStream()); m_CommonFFmpeg.fileWritePkg(handle); m_CommonFFmpeg.rtspDestroyPkg(handle); } m_CommonFFmpeg.fileOutClose(); } } long VideoRecorder::getCurrentUs() { timeval time; gettimeofday(&time, nullptr); return time.tv_sec * 1000000 + time.tv_usec; } long VideoRecorder::getCurrentUs(std::string &strDateTime) { timeval time; gettimeofday(&time, nullptr); __time_t sec = time.tv_sec; char buf[128] = {0}; strftime(buf, 64, "%Y%m%d_%H:%M:%S", localtime(&sec)); char szDateTime[128] = {0}; sprintf(szDateTime, "%s/%s:%ld.mp4", m_strfileName.c_str(), buf, time.tv_usec / 1000); strDateTime = szDateTime; return time.tv_sec * 1000000 + time.tv_usec; } void VideoRecorder::recodeFunc(void *arg) { VideoRecorder *thisPtr = (VideoRecorder *) arg; thisPtr->thisRecodeFunc(); } void VideoRecorder::GetFileName(std::string &fileName) { //std::string tem="/home/basic/work_src/test_Rec/1"; // timeval time; // gettimeofday(&time, nullptr); time_t t = time(nullptr); char buf[128] = {0}; strftime(buf, 64, "%Y-%m-%d %H:%M:%S", localtime(&t)); char szDateTime[256] = {0}; sprintf(szDateTime, "%s%s.mp4", m_fullPath.c_str(), buf); fileName = szDateTime; //std::cout << "File path " << fileName.c_str() << "FUNC " << __FUNCTION__ << " line " << __LINE__ << std::endl; }