mod_recorder.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. #include "mod_recorder.h"
  2. #ifdef RVC_OS_WIN
  3. #include "stdafx.h"
  4. #else
  5. #include<sys/stat.h>
  6. #include<sys/types.h>
  7. #include<dirent.h>
  8. #include<unistd.h>
  9. #endif // RVC_OS_WIN
  10. #include "Event.h"
  11. #include "y2k_time.h"
  12. #include <string.h>
  13. #include "filecryption.h"
  14. #include "mod_facetracking/sysvar.h"
  15. #include "mod_interactivecontrol/Event.h"
  16. #include "mod_mediacontroller/Event.h"
  17. using namespace Recorder;
  18. #ifndef RVC_MAX_VIDEO_NAME_LEN
  19. #define RVC_MAX_VIDEO_NAME_LEN 256
  20. #endif
  21. #ifndef RVC_FILEENC_STR
  22. #define RVC_FILEENC_STR "enc_"
  23. #endif
  24. #ifndef MAX_LOG_LEN
  25. #define MAX_LOG_LEN 512
  26. #endif
  27. #ifndef RVC_TRANSATCION_RECORD_SUFFIX
  28. #define RVC_TRANSATCION_RECORD_SUFFIX "B_"
  29. #endif // !RVC_TRANSATCION_RECORD_SUFFIX
  30. #ifndef RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX
  31. #define RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX "enc_B_"
  32. #endif // !RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX
  33. #ifndef RVC_MIN_RECORD_FILESIZE
  34. #define RVC_MIN_RECORD_FILESIZE 1024
  35. #endif
  36. static unsigned long GetFileSize(const char* pfilename)
  37. {
  38. #ifdef RVC_OS_WIN
  39. unsigned long usize = 0;
  40. if (NULL == pfilename) {
  41. return usize;
  42. }
  43. FILE* pFile = fopen(pfilename, "rb");
  44. if (pFile) {
  45. fseek(pFile, 0, SEEK_END);
  46. usize = ftell(pFile);
  47. fclose(pFile);
  48. }
  49. return usize;
  50. #else
  51. struct stat statbuf;
  52. stat(pfilename, &statbuf);
  53. return statbuf.st_size;
  54. #endif
  55. }
  56. static const char* GetFileName(const char* pfilename)
  57. {
  58. if (NULL == pfilename) {
  59. return NULL;
  60. }
  61. return strstr(pfilename, RVC_TRANSATCION_RECORD_SUFFIX);
  62. }
  63. static unsigned long GetRecordFileSize(const char* pszMessage, bool bencrypted)
  64. {
  65. unsigned long ufilesize = GetFileSize(pszMessage);
  66. if (false == bencrypted) {
  67. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_VIDEO_SIZE, CSimpleStringA::Format("%s file size is %u byte.", pszMessage, ufilesize).GetData());
  68. }
  69. else {
  70. LogWarn(Severity_Low, Error_Debug, LOG_EVT_ENCRYPTED_RECORDER_VIDEO_SIZE, CSimpleStringA::Format("encrypted file %s size is %u byte.", pszMessage, ufilesize).GetData());
  71. }
  72. return ufilesize;
  73. }
  74. static void rvcDbg(filecrypt_loglevel elevel, const char* fmt, ...)
  75. {
  76. LOG_LEVEL_E eloglevel = LOG_LEVEL_DEBUG;
  77. if (FILECRYPT_LOG_INFO <= elevel) {
  78. eloglevel = LOG_LEVEL_INFO;
  79. }
  80. va_list arg;
  81. va_start(arg, fmt);
  82. int n = vsnprintf(NULL, 0, fmt, arg);
  83. if (n >= MAX_LOG_LEN) {
  84. char* buf = (char*)malloc((size_t)(n + 1));
  85. vsnprintf(buf, n + 1, fmt, arg);
  86. DbgWithLink(eloglevel, LOG_TYPE_SYSTEM)("%s", buf);
  87. free(buf);
  88. }
  89. else{
  90. char strlog[MAX_LOG_LEN] = {0};
  91. vsnprintf(strlog, MAX_LOG_LEN, fmt, arg);
  92. DbgWithLink(eloglevel, LOG_TYPE_SYSTEM)("%s", strlog);
  93. }
  94. va_end(arg);
  95. }
  96. static bool rvcMoveFile(const char* strSrcFile, const char* strDstFile)
  97. {
  98. bool bRet = false;
  99. if (NULL == strSrcFile || NULL == strDstFile) {
  100. return bRet;
  101. }
  102. #ifdef RVC_OS_WIN
  103. bRet = MoveFile(strSrcFile, strDstFile);
  104. #else
  105. if (0 == rename(strSrcFile, strDstFile)) {
  106. bRet = true;
  107. }
  108. #endif // RVC_OS_WIN
  109. return bRet;
  110. }
  111. static bool RvcDeleteFile(const char* strSrcFile)
  112. {
  113. bool bRet = false;
  114. if (NULL == strSrcFile) {
  115. return bRet;
  116. }
  117. #ifdef RVC_OS_WIN
  118. bRet = DeleteFile(strSrcFile);
  119. #else
  120. if (0 == remove(strSrcFile)) {
  121. bRet = true;
  122. }
  123. #endif // RVC_OS_WIN
  124. return bRet;
  125. }
  126. void RecordServiceSession::Handle_StartTransactionRecord(SpReqAnsContext<RecorderSerVice_StartTransactionRecord_Req, RecorderSerVice_StartTransactionRecord_Ans>::Pointer ctx)
  127. {
  128. DbgToBeidou(ctx->link, __FUNCTION__)();
  129. if (m_pEntity->GetStartFlag()) {
  130. m_pEntity->StopRecord();
  131. }
  132. m_pEntity->SetRecordSessionID(ctx->Req.VideoName.GetData());
  133. m_pEntity->StartRecord(CSimpleStringA::Format("%s%s", RVC_TRANSATCION_RECORD_SUFFIX, ctx->Req.VideoName.GetData()).GetData());
  134. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("start video record %s.", ctx->Req.VideoName.GetData());
  135. ctx->Answer(Error_Succeed);
  136. }
  137. void RecordServiceSession::Handle_StopTransactionRecord(SpReqAnsContext<RecorderSerVice_StopTransactionRecord_Req, RecorderSerVice_StopTransactionRecord_Ans>::Pointer ctx)
  138. {
  139. DbgToBeidou(ctx->link, __FUNCTION__)();
  140. m_pEntity->StopRecord();
  141. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("stop video record %s.", ctx->Req.VideoName.GetData());
  142. ctx->Answer(Error_Succeed);
  143. }
  144. void CRecorderEntity::OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  145. {
  146. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  147. pTransactionContext->SendAnswer(Error);
  148. }
  149. void CRecorderEntity::OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  150. {
  151. ErrorCodeEnum Error = __OnClose(Error_Succeed);
  152. pTransactionContext->SendAnswer(Error);
  153. }
  154. ErrorCodeEnum CRecorderEntity::__OnStart(ErrorCodeEnum preOperationError)
  155. {
  156. ErrorCodeEnum Error = Error_Succeed;
  157. m_eDeviceType = RvcGetDeviceType();
  158. if (preOperationError != Error_Succeed) {
  159. return preOperationError;
  160. }
  161. m_iActiveCamera = CAMERA_TYPE_ENV;
  162. m_iCameraState = 'N';
  163. InitRecorder();
  164. int i = 0;
  165. m_arrListener.Init(7);
  166. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, EVENT_MOD_PAUSE_RECORD, NULL, false);
  167. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, EVENT_MOD_CONTINUE_RECORD, NULL, false);
  168. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_RECORDER_SECTION_FINISHED, NULL, false);
  169. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_RECORDER_WHOLE_FINISHED, NULL, false);
  170. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_START_BUSINESSRECORD_FAILED, NULL, false);
  171. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_MEDIACONTROLLER_CAMERA_STARTED, NULL, false);
  172. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_MEDIACONTROLLER_CAMERA_STOPPED, NULL, false);
  173. GetFunction()->RegistSysVarEvent(SYSVAR_ACTIVETRACKINGCAMERA,this);
  174. GetFunction()->RegistSysVarEvent(SYSVAR_CAMERASTATE,this);
  175. CSimpleStringA strValue;
  176. GetFunction()->GetSysVar(SYSVAR_CAMERASTATE, strValue);
  177. m_iCameraState = strValue[0];
  178. if (strValue[0] == 'E'){
  179. m_iActiveCamera = CAMERA_TYPE_OPT;
  180. if (eStand1SPlusType == m_eDeviceType){
  181. m_iActiveCamera = CAMERA_TYPE_ERROR;
  182. }
  183. }
  184. else if (strValue[0] == 'O'){
  185. m_iActiveCamera = CAMERA_TYPE_ENV;
  186. }
  187. else if(strValue[0] == 'B'){
  188. m_iActiveCamera = CAMERA_TYPE_ERROR;
  189. }
  190. else if (strValue[0] == 'N'){
  191. m_iActiveCamera = CAMERA_TYPE_ENV;
  192. }
  193. Error = GetFunction()->GetPath("Temp", m_TempDir);
  194. if (Error != Error_Succeed) {
  195. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get global record temp path failed!");
  196. }
  197. if (m_TempDir.GetLength() > 0 && m_TempDir[m_TempDir.GetLength()-1] != SPLIT_SLASH) {
  198. m_TempDir += SPLIT_SLASH_STR;
  199. }
  200. Error = GetFunction()->GetPath("UploadVideo", m_RecordSaveDir);
  201. if (Error != Error_Succeed) {
  202. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get global record save path failed!");
  203. }
  204. if (m_RecordSaveDir.GetLength() > 0 && m_RecordSaveDir[m_RecordSaveDir.GetLength()-1] != SPLIT_SLASH) {
  205. m_RecordSaveDir += SPLIT_SLASH_STR;
  206. }
  207. return Error;
  208. }
  209. void CRecorderEntity::OnStarted()
  210. {
  211. CSystemStaticInfo si;
  212. ErrorCodeEnum Error = GetFunction()->GetSystemStaticInfo(si);
  213. if (Error == Error_Succeed) {
  214. m_strAppVersion = si.InstallVersion.ToString();
  215. m_strTerminalId = si.strTerminalID;
  216. }
  217. LoadEntityConfig();
  218. //DeleteExceptionLogFiles();
  219. SaveExceptionRecordVideos();
  220. if (m_vRecordList.size() > 0) {
  221. HandleExceptionRecordVideos();
  222. PostVideoRecordInfos();
  223. }
  224. }
  225. bool CRecorderEntity::InitRecorder()
  226. {
  227. bool bRet = false;
  228. if (eStand1SPlusType == m_eDeviceType) {
  229. m_pRecorder = new Clibvideorecord(&bRet, this, REC_COMMON_AUDIO_SHM_QUEUE,
  230. REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE, NULL);
  231. }
  232. else {
  233. m_pRecorder = new Clibvideorecord(&bRet, this, REC_COMMON_AUDIO_SHM_QUEUE,
  234. REC_COMMON_VIDEO_ENV_SHM_RTP_QUEUE, REC_COMMON_VIDEO_OPT_SHM_RTP_QUEUE);
  235. }
  236. return bRet;
  237. }
  238. bool CRecorderEntity::ReleaseRecorder()
  239. {
  240. if (m_pRecorder) {
  241. delete m_pRecorder;
  242. m_pRecorder = NULL;
  243. }
  244. return true;
  245. }
  246. ErrorCodeEnum CRecorderEntity::__OnClose(ErrorCodeEnum preOperationError)
  247. {
  248. if (preOperationError != Error_Succeed) {
  249. return preOperationError;
  250. }
  251. for (int i = 0; i < m_arrListener.GetCount(); ++i) {
  252. GetFunction()->UnsubscribeLog(m_arrListener[i]);
  253. }
  254. GetFunction()->UnregistSysVarEvent(SYSVAR_ACTIVETRACKINGCAMERA);
  255. GetFunction()->UnregistSysVarEvent(SYSVAR_CAMERASTATE);
  256. StopRecord();
  257. return Error_Succeed;
  258. }
  259. CServerSessionBase* CRecorderEntity::OnNewSession(const char* pszRemoteEntityName, const char* pszClass)
  260. {
  261. return new RecordServiceSession(this);
  262. }
  263. void CRecorderEntity::Debug(record_loglevel elevel, const char *fmt, ...)
  264. {
  265. if (RECORD_LOG_INFO <= elevel) {
  266. va_list arg;
  267. va_start(arg, fmt);
  268. int n = vsnprintf(NULL, 0, fmt, arg);
  269. if (n >= MAX_LOG_LEN) {
  270. char* buf = (char*)malloc((size_t)(n + 1));
  271. vsnprintf(buf, n + 1, fmt, arg);
  272. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  273. free(buf);
  274. }
  275. else{
  276. char strlog[MAX_LOG_LEN] = {0};
  277. vsnprintf(strlog, MAX_LOG_LEN, fmt, arg);
  278. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  279. }
  280. va_end(arg);
  281. }
  282. }
  283. void CRecorderEntity::vDebug(record_loglevel elevel, const char* str, va_list list)
  284. {
  285. if (RECORD_LOG_INFO <= elevel) {
  286. int n = vsnprintf(NULL, 0, str, list);
  287. if (n >= MAX_LOG_LEN) {
  288. char* buf = (char*)malloc((size_t)(n + 1));
  289. vsnprintf(buf, n + 1, str, list);
  290. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  291. free(buf);
  292. }
  293. else {
  294. char strlog[MAX_LOG_LEN] = { 0 };
  295. vsnprintf(strlog, MAX_LOG_LEN, str, list);
  296. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  297. }
  298. }
  299. }
  300. int CRecorderEntity::GetActiveCamera()
  301. {
  302. return m_iActiveCamera;
  303. }
  304. ErrorCodeEnum CRecorderEntity::LoadEntityConfig()
  305. {
  306. ErrorCodeEnum Error = Error_Succeed;
  307. int iTimeOut = RVC_HTTPTIMEOUT;
  308. int iStopEncflag = 0;
  309. CSimpleStringA strHttpServerAddr("");
  310. CSmartPointer<IConfigInfo> spConfig;
  311. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  312. if (spFunction->OpenConfig(Config_CenterSetting, spConfig) == Error_Succeed) {
  313. spConfig->ReadConfigValue("Recorder", "http_video_record_addr", strHttpServerAddr);
  314. spConfig->ReadConfigValueInt("Recorder", "http_timeout", iTimeOut);
  315. spConfig->ReadConfigValueInt("Recorder", "stopencflag", iStopEncflag);
  316. }
  317. else {
  318. Error = Error_Failed;
  319. }
  320. if (strHttpServerAddr.GetLength() > 0) {
  321. m_strHttpServerAddr = strHttpServerAddr;
  322. }
  323. if (iTimeOut > 0 && iTimeOut < 20 * RVC_HTTPTIMEOUT) {
  324. m_iHttpTimeOut = iTimeOut;
  325. }
  326. if (1 == iStopEncflag) {
  327. m_bEncFlag = false;
  328. }
  329. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("m_bEncFlag is %s.", m_bEncFlag ? "true":"false");
  330. return Error;
  331. }
  332. ErrorCodeEnum CRecorderEntity::PostVideoRecordInfos()
  333. {
  334. ErrorCodeEnum Error = Error_Failed;
  335. char strtimenow[MAX_PATH] = { 0 };
  336. y2k_time_t nowtime = y2k_time_now();
  337. y2k_to_string(nowtime, strtimenow, MAX_PATH);
  338. video_record_info_t video_params;
  339. video_params.strServerURL = m_strHttpServerAddr;
  340. video_params.strAPI = m_strHttpServerAPI;
  341. video_params.strAppVersion = m_strAppVersion;
  342. video_params.strRecordEndTime = strtimenow;
  343. video_params.strTerminalNo = m_strTerminalId;
  344. video_params.iBusinessStatus = (int)m_eBusinessStatus;
  345. if (eFailed == m_eBusinessStatus) {
  346. if (m_vRecordList.size() > 0) {
  347. video_params.iBusinessStatus = eInterrupt;
  348. }
  349. }
  350. video_params.strRecordID = m_strRecordName;
  351. for (vector<record_item_t>::iterator it = m_vRecordList.begin(); it < m_vRecordList.end(); ++it) {
  352. video_params.vRecordList.push_back(*it);
  353. }
  354. unsigned int uposttime = 0;
  355. CSimpleStringA strErrorMsg("");
  356. if (0 == post_video_recordinfo_list(uposttime, strErrorMsg, &video_params, m_iHttpTimeOut, false)) {
  357. LogWarn(Severity_Low, Error_Debug, LOG_EVT_POST_RECORDINFO_COST_TIME, CSimpleStringA::Format("post video record infos cost time is %ums.", uposttime).GetData());
  358. Error = Error_Succeed;
  359. }
  360. else {
  361. LogWarn(Severity_Middle, Error_Exception, LOG_EVT_POST_RECORDINFO_FAILED, strErrorMsg.GetData());
  362. }
  363. m_vRecordList.clear();
  364. return Error;
  365. }
  366. ErrorCodeEnum CRecorderEntity::HandleExceptionRecordVideos()
  367. {
  368. ErrorCodeEnum Error = Error_Failed;
  369. const char* videofilename = m_vRecordList[0].file_path.c_str();
  370. if (NULL == videofilename) {
  371. return Error;
  372. }
  373. char strSession[RVC_MAX_VIDEO_NAME_LEN] = { 0 };
  374. int iSeriesNum = -1;
  375. char strFormat[RVC_MAX_VIDEO_NAME_LEN] = { 0 };
  376. if (-1 == GetRecordVideoInfo(videofilename, strSession, RVC_MAX_VIDEO_NAME_LEN, &iSeriesNum, strFormat, RVC_MAX_VIDEO_NAME_LEN)) {
  377. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[%s] get record video info failed.", videofilename);
  378. return Error;
  379. }
  380. SetRecordSessionID(strSession + strlen(RVC_TRANSATCION_RECORD_SUFFIX));
  381. Error = Error_Succeed;
  382. return Error;
  383. }
  384. ErrorCodeEnum CRecorderEntity::AddToVideoRecordList(const char* videofilename)
  385. {
  386. ErrorCodeEnum Error = Error_Failed;
  387. if (NULL == videofilename) {
  388. return Error;
  389. }
  390. record_item_t* item = new record_item_t();
  391. item->file_path = videofilename;
  392. item->file_length = (int)GetFileSize(videofilename);
  393. const char* strfilename = GetFileName(videofilename);
  394. if (strfilename) {
  395. item->file_name = strfilename;
  396. }
  397. m_vRecordList.push_back(*item);
  398. Error = Error_Succeed;
  399. return Error;
  400. }
  401. void CRecorderEntity::OnRecordFailed(eRvcRecordFailedCase eCase, const char *pszMessage, bool bRecordDevFault)
  402. {
  403. m_eBusinessStatus = eFailed;
  404. if (!bRecordDevFault){
  405. LogEvent(Severity_Middle,LOG_EVT_RECORDFAILED,"0");
  406. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_RECORDING_FAILED, CSimpleStringA::Format("%s 本地录音录像失败,已停止!", pszMessage ? pszMessage : " ").GetData());
  407. }
  408. else{
  409. LogEvent(Severity_Middle,LOG_EVT_RECORDFAILED,"1");
  410. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_RECORDING_FAILED, CSimpleStringA::Format("%s 本地录音录像设备故障,尝试恢复中(预计10s内),请稍等", pszMessage ? pszMessage : " ").GetData());
  411. }
  412. }
  413. void CRecorderEntity::OnRecordEntityExcption()
  414. {
  415. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORD_ENTITY_EXCEPTION, "OnRecordEntityExcption!");
  416. }
  417. void CRecorderEntity::OnRecordFinished()
  418. {
  419. }
  420. int CRecorderEntity:: GetCameraState()
  421. {
  422. return m_iCameraState;
  423. }
  424. void CRecorderEntity::OnASectionFinished(const char *pszMessage, int iSerialNum, bool bfinished)
  425. {
  426. if (false == bfinished){
  427. LogEvent(Severity_Middle, LOG_EVT_RECORDER_SECTION_FINISHED, pszMessage);
  428. }
  429. else{
  430. LogEvent(Severity_Middle, LOG_EVT_RECORDER_WHOLE_FINISHED, pszMessage);
  431. }
  432. }
  433. void CRecorderEntity::OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  434. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  435. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage, const linkContext& pLinkInfo)
  436. {
  437. switch (dwUserCode)
  438. {
  439. case EVENT_MOD_PAUSE_RECORD:
  440. m_pRecorder->PauseRecord();
  441. if (m_bStarted) {
  442. m_pRecorder->CloseVideoFile();
  443. }
  444. break;
  445. case EVENT_MOD_CONTINUE_RECORD:
  446. m_pRecorder->ContinueRecord();
  447. break;
  448. case LOG_EVT_RECORDER_SECTION_FINISHED:
  449. {
  450. unsigned long uSize = GetRecordFileSize(pszMessage, false);
  451. if (RVC_MIN_RECORD_FILESIZE < uSize) {
  452. if (m_bEncFlag) {
  453. HandleEncryptVideoRecord(pszMessage);
  454. }
  455. HandleSaveVideoRecord(pszMessage);
  456. }
  457. else {
  458. LogWarn(Severity_Low, Error_Debug, LOG_EVT_INVALID_RECORD_FILE, CSimpleStringA::Format("invalid record file %s, delelte it.", pszMessage).GetData());
  459. RvcDeleteFile(pszMessage);
  460. }
  461. }
  462. break;
  463. case LOG_EVT_RECORDER_WHOLE_FINISHED:
  464. {
  465. unsigned long uSize = GetRecordFileSize(pszMessage, false);
  466. if (RVC_MIN_RECORD_FILESIZE < uSize) {
  467. if (m_bEncFlag) {
  468. HandleEncryptVideoRecord(pszMessage);
  469. }
  470. HandleFinishedVideoRecord(pszMessage);
  471. PostVideoRecordInfos();
  472. }
  473. else {
  474. LogWarn(Severity_Low, Error_Debug, LOG_EVT_INVALID_RECORD_FILE, CSimpleStringA::Format("invalid record file %s, delelte it.", pszMessage).GetData());
  475. RvcDeleteFile(pszMessage);
  476. }
  477. }
  478. break;
  479. case LOG_EVT_START_BUSINESSRECORD_FAILED:
  480. m_eBusinessStatus = eFailed;
  481. PostVideoRecordInfos();
  482. break;
  483. case LOG_EVT_MEDIACONTROLLER_CAMERA_STARTED:
  484. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recv LOG_EVT_MEDIACONTROLLER_CAMERA_STARTED event");
  485. break;
  486. case LOG_EVT_MEDIACONTROLLER_CAMERA_STOPPED:
  487. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recv LOG_EVT_MEDIACONTROLLER_CAMERA_STOPPED event");
  488. break;
  489. default:
  490. break;
  491. }
  492. }
  493. void CRecorderEntity::OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
  494. {
  495. if (_stricmp(pszKey, SYSVAR_CAMERASTATE) == 0)
  496. {
  497. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("OnSysVarEvent Key = %s, Value = %s.", pszKey, pszValue);
  498. m_iCameraState = pszValue[0];
  499. if (pszValue[0] == 'E'){
  500. m_iActiveCamera = CAMERA_TYPE_OPT;
  501. }
  502. else if (pszValue[0] == 'O'){
  503. m_iActiveCamera = CAMERA_TYPE_ENV;
  504. }
  505. else if(pszValue[0] == 'B'){
  506. m_iActiveCamera = CAMERA_TYPE_ERROR;
  507. }
  508. else if (pszValue[0] == 'N'){
  509. m_iActiveCamera = CAMERA_TYPE_ENV;
  510. }
  511. }
  512. else if (_stricmp(pszKey, SYSVAR_ACTIVETRACKINGCAMERA) == 0)
  513. {
  514. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("OnSysVarEvent Key = %s, Value = %s.", pszKey, pszValue);
  515. if (m_iCameraState == 'N'){
  516. if (pszValue[0] == 'E'){
  517. m_iActiveCamera = CAMERA_TYPE_ENV;
  518. }
  519. else if (pszValue[0] == 'O'){
  520. m_iActiveCamera = CAMERA_TYPE_OPT;
  521. }
  522. }
  523. }
  524. }
  525. void CRecorderEntity::OnSelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
  526. {
  527. if (Test_ShakeHand == eTestType){
  528. pTransactionContext->SendAnswer(Error_Succeed);
  529. }
  530. }
  531. void CRecorderEntity::StartRecord(const char *videofilename)
  532. {
  533. int fps = 5;
  534. Rvc_RecordAudioParam_t tAudioParams;
  535. tAudioParams.eRecordType = eSingleSide;
  536. tAudioParams.eOutPutType = eLowDefinition;
  537. tAudioParams.bIsNsOn = true;
  538. tAudioParams.iNsPolicy = 2;
  539. tAudioParams.iAudioOutBitRate = 8;
  540. tAudioParams.bIsTransOn = false;
  541. tAudioParams.iAudioChannels = 1;
  542. if (m_pRecorder->StartVideoRecord(fps, 75, eMP4, &tAudioParams, NULL, false, true, m_TempDir.GetData(), m_RecordSaveDir.GetLength(), videofilename, strlen(videofilename)))
  543. {
  544. m_bStarted = true;
  545. m_eBusinessStatus = eSuccess;
  546. }
  547. }
  548. void CRecorderEntity::StopRecord()
  549. {
  550. if (m_bStarted) {
  551. m_pRecorder->StopVideoRecord();
  552. m_bStarted = false;
  553. }
  554. }
  555. void CRecorderEntity::SetRecordSessionID(const char* strRecordID)
  556. {
  557. if (NULL != strRecordID) {
  558. memset(m_strRecordName, 0 , MAX_PATH);
  559. snprintf(m_strRecordName, MAX_PATH, "%s", strRecordID);
  560. }
  561. }
  562. void CRecorderEntity::GetRecordSessionID(char* strRecordID, size_t uLen)
  563. {
  564. if (NULL != strRecordID) {
  565. snprintf(strRecordID, uLen, "%s", m_strRecordName);
  566. }
  567. }
  568. DeviceTypeEnum CRecorderEntity::RvcGetDeviceType()
  569. {
  570. DeviceTypeEnum eType = eStand2sType;
  571. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  572. CSystemStaticInfo stStaticinfo;
  573. spFunction->GetSystemStaticInfo(stStaticinfo);
  574. if (_stricmp(stStaticinfo.strMachineType.GetData(), "RVC.Stand1SPlus") == 0) {
  575. eType = eStand1SPlusType;
  576. }
  577. else if (_stricmp(stStaticinfo.strMachineType.GetData(), "RVC.CardStore") == 0 || _stricmp(stStaticinfo.strMachineType.GetData(), "RVC.CardPrinter") == 0) {
  578. eType = eCardStore;
  579. }
  580. else {
  581. eType = eStand2sType;
  582. }
  583. if (eType >= 0 && eType < sizeof(Device_Type_Table) / sizeof(char*)) {
  584. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("device type is %s.", Device_Type_Table[eType]);
  585. }
  586. m_terminalNo = stStaticinfo.strTerminalID;
  587. return eType;
  588. }
  589. int CRecorderEntity::HandleFinishedVideoRecord(const char* videofilename)
  590. {
  591. int iRet = -1;
  592. if (NULL == videofilename){
  593. return iRet;
  594. }
  595. char strSession[RVC_MAX_VIDEO_NAME_LEN] = {0};
  596. int iSeriesNum = -1;
  597. char strFormat[RVC_MAX_VIDEO_NAME_LEN] = {0};
  598. if (-1 == GetRecordVideoInfo(videofilename, strSession, RVC_MAX_VIDEO_NAME_LEN, &iSeriesNum, strFormat, RVC_MAX_VIDEO_NAME_LEN)){
  599. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[%s] get record video info failed.", videofilename);
  600. return iRet;
  601. }
  602. CSimpleStringA srcfile = CSimpleStringA::Format("%s%s_%d_end.%s", m_TempDir.GetData(), strSession, iSeriesNum, strFormat);
  603. CSimpleStringA dstfile = CSimpleStringA::Format("%s%s_%d_end.%s", m_RecordSaveDir.GetData(), strSession, iSeriesNum, strFormat);
  604. bool bRet = false;
  605. if (ExistsFile(srcfile.GetData())){
  606. bRet = rvcMoveFile(srcfile.GetData(), dstfile.GetData());
  607. if(!bRet) {
  608. #ifdef RVC_OS_WIN
  609. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_MOVE_FAILED, CSimpleStringA::Format("Error Code %u while move %s ", GetLastError(), srcfile.GetData()).GetData());
  610. #else
  611. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_MOVE_FAILED, CSimpleStringA::Format("%s(%d) while move %s ", strerror(errno), errno, srcfile.GetData()).GetData());
  612. #endif // RVC_OS_WIN
  613. }
  614. else {
  615. AddToVideoRecordList(dstfile.GetData());
  616. }
  617. }
  618. srcfile = CSimpleStringA::Format("%s%s_%d.%s",m_TempDir.GetData(), strSession, iSeriesNum-1, strFormat);
  619. dstfile = CSimpleStringA::Format("%s%s_%d.%s",m_RecordSaveDir.GetData(), strSession, iSeriesNum-1, strFormat);
  620. if (ExistsFile(srcfile.GetData())){
  621. bRet = rvcMoveFile(srcfile.GetData(), dstfile.GetData());
  622. if(!bRet) {
  623. #ifdef RVC_OS_WIN
  624. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_MOVE_FAILED, CSimpleStringA::Format("Error Code %u while move %s ", GetLastError(), srcfile.GetData()).GetData());
  625. #else
  626. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_MOVE_FAILED, CSimpleStringA::Format("%s(%d) while move %s ", strerror(errno), errno, srcfile.GetData()).GetData());
  627. #endif // RVC_OS_WIN
  628. }
  629. else {
  630. AddToVideoRecordList(dstfile.GetData());
  631. }
  632. }
  633. iRet = 0;
  634. return iRet;
  635. }
  636. int CRecorderEntity::HandleEncryptVideoRecord(const char* videofilename)
  637. {
  638. int iRet = -1;
  639. if (NULL == videofilename){
  640. return iRet;
  641. }
  642. if (!ExistsFile(videofilename)) {
  643. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("File %s is not exist.", videofilename);
  644. return iRet;
  645. }
  646. filecryption_callback_t cb = {0};
  647. cb.dbg = &rvcDbg;
  648. char strOutFile[MAX_PATH] = {0};
  649. int iresult = encryption_file(strOutFile, MAX_PATH, videofilename, &cb, eVerB);
  650. if (0 != iresult){
  651. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_RECORDER_ENCRYPT_FAILED, CSimpleStringA::Format("encryption file %s failed, delete out temp file %s!", videofilename, strOutFile).GetData());
  652. if (ExistsFile(strOutFile)) {
  653. if (!RvcDeleteFile(strOutFile)) {
  654. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("DeleteFile file %s failed!", strOutFile);
  655. }
  656. }
  657. return iRet;
  658. }
  659. else {
  660. GetRecordFileSize(strOutFile, true);
  661. }
  662. bool bRet = RvcDeleteFile(videofilename);
  663. if(!bRet) {
  664. #ifdef RVC_OS_WIN
  665. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_RECORDER_DELETE_FAILED, CSimpleStringA::Format("Error Code %lu while delete %s, delete out temp file[%s]!", GetLastError(), videofilename, strOutFile).GetData());
  666. #else
  667. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_RECORDER_DELETE_FAILED, CSimpleStringA::Format("%s(%d) while delete %s, delete out temp file[%s]!", strerror(errno), errno, videofilename, strOutFile).GetData());
  668. #endif // RVC_OS_WIN
  669. if (!RvcDeleteFile(strOutFile)){
  670. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("DeleteFile file %s failed!", strOutFile);
  671. }
  672. return iRet;
  673. }
  674. else{
  675. if (rvcMoveFile(strOutFile, videofilename)){
  676. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("after encryption rename %s to %s Success!",strOutFile, videofilename);
  677. iRet = 0;
  678. }
  679. else{
  680. #ifdef RVC_OS_WIN
  681. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_RECORDER_RENAME_FAILED, CSimpleStringA::Format("Error Code %lu while rename %s.", GetLastError(), strOutFile).GetData());
  682. #else
  683. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_RECORDER_RENAME_FAILED, CSimpleStringA::Format("%s(%d) while rename %s.", strerror(errno), errno, strOutFile).GetData());
  684. #endif // RVC_OS_WIN
  685. }
  686. }
  687. #if 0
  688. char strdecFile[MAX_PATH] = { 0 };
  689. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("begin decrypt file %s.", videofilename);
  690. iresult = decryption_file(strdecFile, MAX_PATH, videofilename, &cb, eVerB);
  691. if (0 == iresult) {
  692. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("decrypt file %s -> %s success!", videofilename, strdecFile);
  693. }
  694. else {
  695. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("decrypt file %s -> %s failed!", videofilename, strdecFile);
  696. }
  697. #endif
  698. return iRet;
  699. }
  700. int CRecorderEntity::HandleSaveVideoRecord(const char* videofilename)
  701. {
  702. int iRet = -1;
  703. if (NULL == videofilename){
  704. return iRet;
  705. }
  706. char strSession[RVC_MAX_VIDEO_NAME_LEN] = {0};
  707. int iSeriesNum = -1;
  708. char strFormat[RVC_MAX_VIDEO_NAME_LEN] = {0};
  709. if (-1 == GetRecordVideoInfo(videofilename, strSession, RVC_MAX_VIDEO_NAME_LEN, &iSeriesNum, strFormat, RVC_MAX_VIDEO_NAME_LEN)){
  710. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[%s] get record video info failed.", videofilename);
  711. return iRet;
  712. }
  713. CSimpleStringA srcfile = CSimpleStringA::Format("%s%s_%d.%s", m_TempDir.GetData(), strSession, iSeriesNum, strFormat);
  714. CSimpleStringA dstfile = CSimpleStringA::Format("%s%s_%d.%s", m_RecordSaveDir.GetData(), strSession, iSeriesNum, strFormat);
  715. if (ExistsFile(srcfile.GetData())) {
  716. bool bRet = rvcMoveFile(srcfile.GetData(), dstfile.GetData());
  717. if (!bRet) {
  718. #ifdef RVC_OS_WIN
  719. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_MOVE_FAILED, CSimpleStringA::Format("Error Code %u while move %s ", GetLastError(), srcfile.GetData()));
  720. #else
  721. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_MOVE_FAILED, CSimpleStringA::Format("%s(%d) while move %s ", strerror(errno), errno, srcfile.GetData()));
  722. #endif // RVC_OS_WIN
  723. }
  724. else {
  725. AddToVideoRecordList(dstfile.GetData());
  726. }
  727. }
  728. iRet = 0;
  729. return iRet;
  730. }
  731. int CRecorderEntity::GetRecordVideoInfo(const char* videofilename, char* strSession, size_t uSessionLen, int* iSeriesNum, char* strFormat, size_t uFormatLen)
  732. {
  733. int iRet = -1;
  734. char strFileName[RVC_MAX_VIDEO_NAME_LEN] = {0};
  735. size_t uLen = strlen(videofilename);
  736. if (uLen <= RVC_MAX_VIDEO_NAME_LEN){
  737. const char *pIndex = strrchr(videofilename, SPLIT_SLASH);
  738. if (pIndex){
  739. snprintf(strFileName, RVC_MAX_VIDEO_NAME_LEN, "%s", pIndex + 1);
  740. }
  741. }
  742. else{
  743. return iRet;
  744. }
  745. int ioffset = 0;
  746. if (0 == memcmp(strFileName, RVC_TRANSATCION_RECORD_SUFFIX, strlen(RVC_TRANSATCION_RECORD_SUFFIX))) {
  747. ioffset = strlen(RVC_TRANSATCION_RECORD_SUFFIX);
  748. }
  749. char* pNum = strstr(strFileName+ioffset, "_");
  750. if (pNum){
  751. *pNum = 0;
  752. strcpy(strSession, strFileName);
  753. pNum++;
  754. }
  755. else{
  756. return iRet;
  757. }
  758. char* pend = strstr(pNum, "_end");
  759. if (pend){
  760. *pend = 0;
  761. *iSeriesNum = atoi(pNum);
  762. pNum = pend + 1;
  763. }
  764. char* pFormat = strstr(pNum, ".");
  765. if (pFormat){
  766. *pFormat = 0;
  767. pFormat++;
  768. strcpy(strFormat, pFormat);
  769. }
  770. else{
  771. return iRet;
  772. }
  773. if (NULL == pend){
  774. *iSeriesNum = atoi(pNum);
  775. }
  776. iRet = 0;
  777. return iRet;
  778. }
  779. int CRecorderEntity::SaveExceptionRecordVideos()
  780. {
  781. int iRet = -1;
  782. #ifdef RVC_OS_WIN
  783. char srcFilePath[MAX_PATH]={0};
  784. WIN32_FIND_DATA FindFileData;
  785. HANDLE hFind;
  786. bool fFinished = false;
  787. char strVideoFormat[MAX_PATH] = { 0 };
  788. snprintf(strVideoFormat, MAX_PATH, "%s", RECORD_MP4_SUFFIX);
  789. snprintf(srcFilePath, MAX_PATH, "%s*.%s", m_TempDir.GetData(), strVideoFormat);
  790. hFind = FindFirstFile(srcFilePath, &FindFileData);
  791. if (INVALID_HANDLE_VALUE != hFind)
  792. {
  793. while (!fFinished){
  794. if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes){
  795. goto on_next;
  796. }
  797. if (0 == memcmp(FindFileData.cFileName, RVC_TRANSATCION_RECORD_SUFFIX, strlen(RVC_TRANSATCION_RECORD_SUFFIX)) ||
  798. 0 == memcmp(FindFileData.cFileName, RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX, strlen(RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX))){
  799. CSimpleStringA srcfile = CSimpleStringA::Format("%s%s",m_TempDir.GetData(), FindFileData.cFileName);
  800. unsigned long uSize = GetRecordFileSize(srcfile.GetData(), false);
  801. if (RVC_MIN_RECORD_FILESIZE >= uSize) {
  802. LogWarn(Severity_Low, Error_Debug, LOG_EVT_INVALID_RECORD_FILE, CSimpleStringA::Format("invalid record file %s, delelte it.", srcfile.GetData()).GetData());
  803. RvcDeleteFile(srcfile.GetData());
  804. goto on_next;
  805. }
  806. if (m_bEncFlag){
  807. filecryption_callback_t cb = {0};
  808. cb.dbg = &rvcDbg;
  809. if (false == is_file_encrypted(srcfile.GetData(), &cb)){
  810. if (is_file_completed(srcfile.GetData(), &cb)) {
  811. HandleEncryptVideoRecord(srcfile.GetData());
  812. }
  813. }
  814. else{
  815. char* pIndex = NULL;
  816. if (pIndex = strstr(FindFileData.cFileName, RVC_FILEENC_STR)){
  817. char strname[MAX_PATH] = {0};
  818. memcpy(strname, pIndex+strlen(RVC_FILEENC_STR), strlen(pIndex+strlen(RVC_FILEENC_STR)));
  819. CSimpleStringA tempsrcfile = CSimpleStringA::Format("%s%s",m_TempDir.GetData(), strname);
  820. if (!rename(srcfile.GetData(),tempsrcfile.GetData())){
  821. srcfile = tempsrcfile;
  822. memset(FindFileData.cFileName, 0, MAX_PATH);
  823. memcpy(FindFileData.cFileName, strname, strlen(strname));
  824. }
  825. else{
  826. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_RENAME_FAILED, CSimpleStringA::Format("Error Code %lu while rename %s.", GetLastError(), srcfile.GetData()).GetData());
  827. }
  828. }
  829. }
  830. }
  831. CSimpleStringA dstfile = CSimpleStringA::Format("%s%s",m_RecordSaveDir.GetData(), FindFileData.cFileName);
  832. bool bRet = rvcMoveFile(srcfile.GetData(), dstfile.GetData());
  833. if(!bRet) {
  834. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_MOVE_FAILED, CSimpleStringA::Format("Error Code %u while move %s -> %s", GetLastError(), srcfile.GetData(), dstfile.GetData()).GetData());
  835. }
  836. else{
  837. AddToVideoRecordList(dstfile.GetData());
  838. iRet = 0;
  839. }
  840. }
  841. on_next:
  842. if (!FindNextFile(hFind, &FindFileData)){
  843. if (GetLastError() == ERROR_NO_MORE_FILES){
  844. fFinished = true;
  845. }
  846. else{
  847. break;
  848. }
  849. }
  850. }
  851. FindClose(hFind);
  852. }
  853. #else
  854. struct dirent* ptr;
  855. DIR* dir = opendir(m_TempDir.GetData());
  856. while ((ptr = readdir(dir)) != NULL)
  857. {
  858. if (ptr->d_type & DT_DIR) {
  859. continue;
  860. }
  861. if (0 == memcmp(ptr->d_name, RVC_TRANSATCION_RECORD_SUFFIX, strlen(RVC_TRANSATCION_RECORD_SUFFIX)) ||
  862. 0 == memcmp(ptr->d_name, RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX, strlen(RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX))) {
  863. CSimpleStringA srcfile = CSimpleStringA::Format("%s%s", m_TempDir.GetData(), ptr->d_name);
  864. unsigned long uSize = GetRecordFileSize(srcfile.GetData(), false);
  865. if (RVC_MIN_RECORD_FILESIZE >= uSize) {
  866. LogWarn(Severity_Low, Error_Debug, LOG_EVT_INVALID_RECORD_FILE, CSimpleStringA::Format("invalid record file %s, delelte it.", pszMessage).GetData());
  867. RvcDeleteFile(pszMessage);
  868. continue;
  869. }
  870. if (m_bEncFlag) {
  871. filecryption_callback_t cb = { 0 };
  872. cb.dbg = &rvcDbg;
  873. if (false == is_file_encrypted(srcfile.GetData(), &cb)) {
  874. if (is_file_completed(srcfile.GetData(), &cb)) {
  875. HandleEncryptVideoRecord(srcfile.GetData());
  876. }
  877. }
  878. else {
  879. char* pIndex = NULL;
  880. if (pIndex = strstr(ptr->d_name, RVC_FILEENC_STR)) {
  881. char strname[MAX_PATH] = { 0 };
  882. memcpy(strname, pIndex + strlen(RVC_FILEENC_STR), strlen(pIndex + strlen(RVC_FILEENC_STR)));
  883. CSimpleStringA tempsrcfile = CSimpleStringA::Format("%s%s", m_TempDir.GetData(), strname);
  884. if (rvcMoveFile(srcfile.GetData(), tempsrcfile.GetData())) {
  885. srcfile = tempsrcfile;
  886. memset(ptr->d_name, 0, MAX_PATH);
  887. memcpy(ptr->d_name, strname, strlen(strname));
  888. }
  889. else {
  890. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_RENAME_FAILED, CSimpleStringA::Format("%s(%d) while rename %s.", strerror(errno), errno, srcfile.GetData()).GetData());
  891. }
  892. }
  893. }
  894. }
  895. CSimpleStringA dstfile = CSimpleStringA::Format("%s%s", m_RecordSaveDir.GetData(), ptr->d_name);
  896. bool bRet = rvcMoveFile(srcfile.GetData(), dstfile.GetData());
  897. if (!bRet) {
  898. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_MOVE_FAILED, CSimpleStringA::Format("Error Code %u while move %s -> %s", GetLastError(), srcfile.GetData(), dstfile.GetData()).GetData());
  899. }
  900. else {
  901. AddToVideoRecordList(dstfile.GetData());
  902. iRet = 0;
  903. }
  904. }
  905. }
  906. closedir(dir);
  907. #endif // RVC_OS_WIN
  908. return iRet;
  909. }
  910. SP_BEGIN_ENTITY_MAP()
  911. SP_ENTITY(CRecorderEntity)
  912. SP_END_ENTITY_MAP()