#include "recorder.h" namespace MIVA { std::shared_ptr m_recorder = nullptr; /** * @description: 创建 * @param {*} * @return {*} */ std::shared_ptr recorder::CreateNew() { if(m_recorder == nullptr) m_recorder = std::make_shared(); return m_recorder; } /** * @description: 消费数据 * @param {int} sourceId * @param {Mat} frame * @return {*} */ void recorder::ConsumerData(std::string Pid,cv::Mat& frame) { //if(!enable) return; auto iter = this->videoWriters.find(Pid); if(iter != this->videoWriters.end()){ if( !outType || iter->second.fileName == ""){ time_t rawtime; char ctime[80]; struct tm *info; time(&rawtime); info = localtime(&rawtime); strftime(ctime, 80, "%Y-%m-%d_%H:%M:%S", info); string fileName = this->outDir; string time1 = ctime; if(outType){ fileName += time1 + "_" + Pid + ".mp4"; iter->second.videoWriter = std::make_shared("appsrc ! autovideoconvert ! omxh264enc ! matroskamux ! filesink location=" + fileName + " sync=false", 0, (double)2, cv::Size(1920, 1080), true); }else{ fileName += time1 + "_" + Pid + ".jpg"; cv::imwrite(fileName, frame); iter->second.videoWriter = nullptr; } iter->second.fileName = fileName; } } else{ Writer writer; time_t rawtime; char ctime[80]; struct tm *info; time(&rawtime); info = localtime(&rawtime); strftime(ctime, 80, "%Y-%m-%d_%H:%M:%S", info); string fileName = this->outDir; string time1 = ctime; if(outType){ fileName += time1 + "_" + Pid + ".mp4"; writer.videoWriter = std::make_shared("appsrc ! autovideoconvert ! omxh264enc ! matroskamux ! filesink location=" + fileName + " sync=false", 0, (double)2, cv::Size(1920, 1080), true); }else{ fileName += time1 + "_" + Pid + ".jpg"; cv::imwrite(fileName, frame); writer.videoWriter = nullptr; } writer.fileName = fileName; this->videoWriters[Pid] = writer; } if(outType) this->videoWriters[Pid].videoWriter->write(frame); } /** * @description: 启动 * @param {*} * @return {*} */ void recorder::Start() { if(!this->RecordEnable) return; time_t rawtime; char ctime[80]; struct tm *info; time(&rawtime); info = localtime(&rawtime); strftime(ctime, 80, "%Y-%m-%d_%H:%M:%S", info); this->outDir = ctime; this->outDir = this->Dir + this->outDir.substr(0, 7); if(opendir(this ->outDir.c_str()) == NULL){ mkdir((this->outDir).c_str(),S_IRWXU|S_IRWXG|S_IRWXO); } this->outDir = this->outDir + "/" + ctime + "/"; if(opendir(this ->outDir.c_str()) == NULL){ mkdir((this->outDir).c_str(),S_IRWXU|S_IRWXG|S_IRWXO); } m_recorder->enable = true; } /** * @description: 结束数据 * @param {*} * @return {*} */ void recorder::Finish() { if(!outType) return; for(auto videowriter = this->videoWriters.begin(); videowriter != this->videoWriters.end(); videowriter++){ if(videowriter->second.videoWriter != nullptr){ videowriter->second.videoWriter->release(); videowriter->second.videoWriter = nullptr; } videowriter->second.fileName = ""; } m_recorder->enable = false; } /** * @description: 获取文件名 * @param {int} sourceId * @return {*} */ std::string recorder::GetFileName(std::string Pid) { std::string fileName = ""; auto iter = this->videoWriters.find(Pid); if(iter != this->videoWriters.end()){ fileName = iter->second.fileName; } return fileName; } };