mod_recorder.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. if (0 == stat(pfilename, &statbuf)) {
  53. return statbuf.st_size;
  54. }
  55. else {
  56. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("errno info is %s.", strerror(errno));
  57. return 0;
  58. }
  59. #endif
  60. }
  61. static const char* GetFileName(const char* pfilename)
  62. {
  63. if (NULL == pfilename) {
  64. return NULL;
  65. }
  66. return strstr(pfilename, RVC_TRANSATCION_RECORD_SUFFIX);
  67. }
  68. static void LogRecordFileSize(const char* pszMessage)
  69. {
  70. unsigned long ufilesize = GetFileSize(pszMessage);
  71. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_VIDEO_SIZE, CSimpleStringA::Format("%s file size is %u byte.", pszMessage, ufilesize).GetData());
  72. }
  73. static void rvcDbg(filecrypt_loglevel elevel, const char* fmt, ...)
  74. {
  75. LOG_LEVEL_E eloglevel = LOG_LEVEL_DEBUG;
  76. if (FILECRYPT_LOG_INFO <= elevel) {
  77. eloglevel = LOG_LEVEL_INFO;
  78. }
  79. va_list arg;
  80. va_start(arg, fmt);
  81. int n = vsnprintf(NULL, 0, fmt, arg);
  82. if (n >= MAX_LOG_LEN) {
  83. char* buf = (char*)malloc((size_t)(n + 1));
  84. vsnprintf(buf, n + 1, fmt, arg);
  85. DbgWithLink(eloglevel, LOG_TYPE_SYSTEM)("%s", buf);
  86. free(buf);
  87. }
  88. else{
  89. char strlog[MAX_LOG_LEN] = {0};
  90. vsnprintf(strlog, MAX_LOG_LEN, fmt, arg);
  91. DbgWithLink(eloglevel, LOG_TYPE_SYSTEM)("%s", strlog);
  92. }
  93. va_end(arg);
  94. }
  95. static bool rvcMoveFile(const char* strSrcFile, const char* strDstFile)
  96. {
  97. bool bRet = false;
  98. if (NULL == strSrcFile || NULL == strDstFile) {
  99. return bRet;
  100. }
  101. #ifdef RVC_OS_WIN
  102. bRet = MoveFile(strSrcFile, strDstFile);
  103. #else
  104. if (0 == rename(strSrcFile, strDstFile)) {
  105. bRet = true;
  106. }
  107. #endif // RVC_OS_WIN
  108. return bRet;
  109. }
  110. static bool RvcDeleteFile(const char* strSrcFile)
  111. {
  112. bool bRet = false;
  113. if (NULL == strSrcFile) {
  114. return bRet;
  115. }
  116. #ifdef RVC_OS_WIN
  117. bRet = DeleteFile(strSrcFile);
  118. #else
  119. if (0 == remove(strSrcFile)) {
  120. bRet = true;
  121. }
  122. #endif // RVC_OS_WIN
  123. return bRet;
  124. }
  125. void RecordServiceSession::Handle_StartTransactionRecord(SpReqAnsContext<RecorderSerVice_StartTransactionRecord_Req, RecorderSerVice_StartTransactionRecord_Ans>::Pointer ctx)
  126. {
  127. DbgToBeidou(ctx->link, __FUNCTION__)();
  128. if (m_pEntity->GetStartFlag()) {
  129. m_pEntity->StopRecord();
  130. }
  131. m_pEntity->SetRecordSessionID(ctx->Req.VideoName.GetData());
  132. m_pEntity->StartRecord(CSimpleStringA::Format("%s%s", RVC_TRANSATCION_RECORD_SUFFIX, ctx->Req.VideoName.GetData()).GetData());
  133. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("start video record %s.", ctx->Req.VideoName.GetData());
  134. ctx->Answer(Error_Succeed);
  135. }
  136. void RecordServiceSession::Handle_StopTransactionRecord(SpReqAnsContext<RecorderSerVice_StopTransactionRecord_Req, RecorderSerVice_StopTransactionRecord_Ans>::Pointer ctx)
  137. {
  138. DbgToBeidou(ctx->link, __FUNCTION__)();
  139. m_pEntity->StopRecord();
  140. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("stop video record %s.", ctx->Req.VideoName.GetData());
  141. ctx->Answer(Error_Succeed);
  142. }
  143. void CRecorderEntity::OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  144. {
  145. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  146. pTransactionContext->SendAnswer(Error);
  147. }
  148. void CRecorderEntity::OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  149. {
  150. ErrorCodeEnum Error = __OnClose(Error_Succeed);
  151. pTransactionContext->SendAnswer(Error);
  152. }
  153. ErrorCodeEnum CRecorderEntity::__OnStart(ErrorCodeEnum preOperationError)
  154. {
  155. ErrorCodeEnum Error = Error_Succeed;
  156. m_eDeviceType = RvcGetDeviceType();
  157. if (preOperationError != Error_Succeed) {
  158. return preOperationError;
  159. }
  160. m_iActiveCamera = CAMERA_TYPE_ENV;
  161. m_iCameraState = 'N';
  162. InitRecorder();
  163. int i = 0;
  164. m_arrListener.Init(8);
  165. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, EVENT_MOD_PAUSE_RECORD, NULL, false);
  166. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, EVENT_MOD_CONTINUE_RECORD, NULL, false);
  167. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_RECORDER_SECTION_FINISHED, NULL, false);
  168. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_RECORDER_WHOLE_FINISHED, NULL, false);
  169. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_START_BUSINESSRECORD_FAILED, NULL, false);
  170. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_MEDIACONTROLLER_CAMERA_STARTED, NULL, false);
  171. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_MEDIACONTROLLER_CAMERA_STOPPED, NULL, false);
  172. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_UI_RETURNMENU, 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 = GetFileSize(pszMessage);
  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 = GetFileSize(pszMessage);
  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. case LOG_EVT_UI_RETURNMENU:
  490. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recv LOG_EVT_UI_RETURNMENU event");
  491. break;
  492. default:
  493. break;
  494. }
  495. }
  496. void CRecorderEntity::OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
  497. {
  498. if (_stricmp(pszKey, SYSVAR_CAMERASTATE) == 0)
  499. {
  500. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("OnSysVarEvent Key = %s, Value = %s.", pszKey, pszValue);
  501. m_iCameraState = pszValue[0];
  502. if (pszValue[0] == 'E'){
  503. m_iActiveCamera = CAMERA_TYPE_OPT;
  504. }
  505. else if (pszValue[0] == 'O'){
  506. m_iActiveCamera = CAMERA_TYPE_ENV;
  507. }
  508. else if(pszValue[0] == 'B'){
  509. m_iActiveCamera = CAMERA_TYPE_ERROR;
  510. }
  511. else if (pszValue[0] == 'N'){
  512. m_iActiveCamera = CAMERA_TYPE_ENV;
  513. }
  514. }
  515. else if (_stricmp(pszKey, SYSVAR_ACTIVETRACKINGCAMERA) == 0)
  516. {
  517. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("OnSysVarEvent Key = %s, Value = %s.", pszKey, pszValue);
  518. if (m_iCameraState == 'N'){
  519. if (pszValue[0] == 'E'){
  520. m_iActiveCamera = CAMERA_TYPE_ENV;
  521. }
  522. else if (pszValue[0] == 'O'){
  523. m_iActiveCamera = CAMERA_TYPE_OPT;
  524. }
  525. }
  526. }
  527. }
  528. void CRecorderEntity::OnSelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
  529. {
  530. if (Test_ShakeHand == eTestType){
  531. pTransactionContext->SendAnswer(Error_Succeed);
  532. }
  533. }
  534. void CRecorderEntity::StartRecord(const char *videofilename)
  535. {
  536. int fps = 5;
  537. Rvc_RecordAudioParam_t tAudioParams;
  538. tAudioParams.eRecordType = eSingleSide;
  539. tAudioParams.eOutPutType = eLowDefinition;
  540. tAudioParams.bIsNsOn = true;
  541. tAudioParams.iNsPolicy = 2;
  542. tAudioParams.iAudioOutBitRate = 8;
  543. tAudioParams.bIsTransOn = false;
  544. tAudioParams.iAudioChannels = 1;
  545. if (m_pRecorder->StartVideoRecord(fps, 75, eMP4, &tAudioParams, NULL, false, true, m_TempDir.GetData(), m_RecordSaveDir.GetLength(), videofilename, strlen(videofilename)))
  546. {
  547. m_bStarted = true;
  548. m_eBusinessStatus = eSuccess;
  549. }
  550. }
  551. void CRecorderEntity::StopRecord()
  552. {
  553. if (m_bStarted) {
  554. m_pRecorder->StopVideoRecord();
  555. m_bStarted = false;
  556. }
  557. }
  558. void CRecorderEntity::SetRecordSessionID(const char* strRecordID)
  559. {
  560. if (NULL != strRecordID) {
  561. memset(m_strRecordName, 0 , MAX_PATH);
  562. snprintf(m_strRecordName, MAX_PATH, "%s", strRecordID);
  563. }
  564. }
  565. void CRecorderEntity::GetRecordSessionID(char* strRecordID, size_t uLen)
  566. {
  567. if (NULL != strRecordID) {
  568. snprintf(strRecordID, uLen, "%s", m_strRecordName);
  569. }
  570. }
  571. DeviceTypeEnum CRecorderEntity::RvcGetDeviceType()
  572. {
  573. DeviceTypeEnum eType = eStand2sType;
  574. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  575. CSystemStaticInfo stStaticinfo;
  576. spFunction->GetSystemStaticInfo(stStaticinfo);
  577. if (_stricmp(stStaticinfo.strMachineType.GetData(), "RVC.Stand1SPlus") == 0) {
  578. eType = eStand1SPlusType;
  579. }
  580. else if (_stricmp(stStaticinfo.strMachineType.GetData(), "RVC.CardStore") == 0 || _stricmp(stStaticinfo.strMachineType.GetData(), "RVC.CardPrinter") == 0) {
  581. eType = eCardStore;
  582. }
  583. else {
  584. eType = eStand2sType;
  585. }
  586. if (eType >= 0 && eType < sizeof(Device_Type_Table) / sizeof(char*)) {
  587. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("device type is %s.", Device_Type_Table[eType]);
  588. }
  589. m_terminalNo = stStaticinfo.strTerminalID;
  590. return eType;
  591. }
  592. int CRecorderEntity::HandleFinishedVideoRecord(const char* videofilename)
  593. {
  594. int iRet = -1;
  595. if (NULL == videofilename){
  596. return iRet;
  597. }
  598. char strSession[RVC_MAX_VIDEO_NAME_LEN] = {0};
  599. int iSeriesNum = -1;
  600. char strFormat[RVC_MAX_VIDEO_NAME_LEN] = {0};
  601. if (-1 == GetRecordVideoInfo(videofilename, strSession, RVC_MAX_VIDEO_NAME_LEN, &iSeriesNum, strFormat, RVC_MAX_VIDEO_NAME_LEN)){
  602. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[%s] get record video info failed.", videofilename);
  603. return iRet;
  604. }
  605. CSimpleStringA srcfile = CSimpleStringA::Format("%s%s_%d_end.%s", m_TempDir.GetData(), strSession, iSeriesNum, strFormat);
  606. CSimpleStringA dstfile = CSimpleStringA::Format("%s%s_%d_end.%s", m_RecordSaveDir.GetData(), strSession, iSeriesNum, strFormat);
  607. bool bRet = false;
  608. if (ExistsFile(srcfile.GetData())){
  609. LogRecordFileSize(srcfile.GetData());
  610. bRet = rvcMoveFile(srcfile.GetData(), dstfile.GetData());
  611. if(!bRet) {
  612. #ifdef RVC_OS_WIN
  613. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_MOVE_FAILED, CSimpleStringA::Format("Error Code %u while move %s ", GetLastError(), srcfile.GetData()).GetData());
  614. #else
  615. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_MOVE_FAILED, CSimpleStringA::Format("%s(%d) while move %s ", strerror(errno), errno, srcfile.GetData()).GetData());
  616. #endif // RVC_OS_WIN
  617. }
  618. else {
  619. AddToVideoRecordList(dstfile.GetData());
  620. }
  621. }
  622. srcfile = CSimpleStringA::Format("%s%s_%d.%s",m_TempDir.GetData(), strSession, iSeriesNum-1, strFormat);
  623. dstfile = CSimpleStringA::Format("%s%s_%d.%s",m_RecordSaveDir.GetData(), strSession, iSeriesNum-1, strFormat);
  624. if (ExistsFile(srcfile.GetData())){
  625. bRet = rvcMoveFile(srcfile.GetData(), dstfile.GetData());
  626. if(!bRet) {
  627. #ifdef RVC_OS_WIN
  628. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_MOVE_FAILED, CSimpleStringA::Format("Error Code %u while move %s ", GetLastError(), srcfile.GetData()).GetData());
  629. #else
  630. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_MOVE_FAILED, CSimpleStringA::Format("%s(%d) while move %s ", strerror(errno), errno, srcfile.GetData()).GetData());
  631. #endif // RVC_OS_WIN
  632. }
  633. else {
  634. AddToVideoRecordList(dstfile.GetData());
  635. }
  636. }
  637. iRet = 0;
  638. return iRet;
  639. }
  640. int CRecorderEntity::HandleEncryptVideoRecord(const char* videofilename)
  641. {
  642. int iRet = -1;
  643. if (NULL == videofilename){
  644. return iRet;
  645. }
  646. if (!ExistsFile(videofilename)) {
  647. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("File %s is not exist.", videofilename);
  648. return iRet;
  649. }
  650. filecryption_callback_t cb = {0};
  651. cb.dbg = &rvcDbg;
  652. char strOutFile[MAX_PATH] = {0};
  653. int iresult = encryption_file(strOutFile, MAX_PATH, videofilename, &cb, eVerB);
  654. if (0 != iresult){
  655. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_RECORDER_ENCRYPT_FAILED, CSimpleStringA::Format("encryption file %s failed, delete out temp file %s!", videofilename, strOutFile).GetData());
  656. if (ExistsFile(strOutFile)) {
  657. if (!RvcDeleteFile(strOutFile)) {
  658. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("DeleteFile file %s failed!", strOutFile);
  659. }
  660. }
  661. return iRet;
  662. }
  663. bool bRet = RvcDeleteFile(videofilename);
  664. if(!bRet) {
  665. #ifdef RVC_OS_WIN
  666. 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());
  667. #else
  668. 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());
  669. #endif // RVC_OS_WIN
  670. if (!RvcDeleteFile(strOutFile)){
  671. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("DeleteFile file %s failed!", strOutFile);
  672. }
  673. return iRet;
  674. }
  675. else{
  676. if (rvcMoveFile(strOutFile, videofilename)){
  677. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("after encryption rename %s to %s Success!",strOutFile, videofilename);
  678. iRet = 0;
  679. }
  680. else{
  681. #ifdef RVC_OS_WIN
  682. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_RECORDER_RENAME_FAILED, CSimpleStringA::Format("Error Code %lu while rename %s.", GetLastError(), strOutFile).GetData());
  683. #else
  684. LogWarn(Severity_Middle, Error_Debug, LOG_EVT_RECORDER_RENAME_FAILED, CSimpleStringA::Format("%s(%d) while rename %s.", strerror(errno), errno, strOutFile).GetData());
  685. #endif // RVC_OS_WIN
  686. }
  687. }
  688. #if 0
  689. char strdecFile[MAX_PATH] = { 0 };
  690. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("begin decrypt file %s.", videofilename);
  691. iresult = decryption_file(strdecFile, MAX_PATH, videofilename, &cb, eVerB);
  692. if (0 == iresult) {
  693. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("decrypt file %s -> %s success!", videofilename, strdecFile);
  694. }
  695. else {
  696. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("decrypt file %s -> %s failed!", videofilename, strdecFile);
  697. }
  698. #endif
  699. return iRet;
  700. }
  701. int CRecorderEntity::HandleSaveVideoRecord(const char* videofilename)
  702. {
  703. int iRet = -1;
  704. if (NULL == videofilename){
  705. return iRet;
  706. }
  707. char strSession[RVC_MAX_VIDEO_NAME_LEN] = {0};
  708. int iSeriesNum = -1;
  709. char strFormat[RVC_MAX_VIDEO_NAME_LEN] = {0};
  710. if (-1 == GetRecordVideoInfo(videofilename, strSession, RVC_MAX_VIDEO_NAME_LEN, &iSeriesNum, strFormat, RVC_MAX_VIDEO_NAME_LEN)){
  711. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("[%s] get record video info failed.", videofilename);
  712. return iRet;
  713. }
  714. CSimpleStringA srcfile = CSimpleStringA::Format("%s%s_%d.%s", m_TempDir.GetData(), strSession, iSeriesNum, strFormat);
  715. CSimpleStringA dstfile = CSimpleStringA::Format("%s%s_%d.%s", m_RecordSaveDir.GetData(), strSession, iSeriesNum, strFormat);
  716. if (ExistsFile(srcfile.GetData())) {
  717. LogRecordFileSize(srcfile.GetData());
  718. bool bRet = rvcMoveFile(srcfile.GetData(), dstfile.GetData());
  719. if (!bRet) {
  720. #ifdef RVC_OS_WIN
  721. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_MOVE_FAILED, CSimpleStringA::Format("Error Code %u while move %s ", GetLastError(), srcfile.GetData()));
  722. #else
  723. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_MOVE_FAILED, CSimpleStringA::Format("%s(%d) while move %s ", strerror(errno), errno, srcfile.GetData()));
  724. #endif // RVC_OS_WIN
  725. }
  726. else {
  727. AddToVideoRecordList(dstfile.GetData());
  728. }
  729. }
  730. iRet = 0;
  731. return iRet;
  732. }
  733. int CRecorderEntity::GetRecordVideoInfo(const char* videofilename, char* strSession, size_t uSessionLen, int* iSeriesNum, char* strFormat, size_t uFormatLen)
  734. {
  735. int iRet = -1;
  736. char strFileName[RVC_MAX_VIDEO_NAME_LEN] = {0};
  737. size_t uLen = strlen(videofilename);
  738. if (uLen <= RVC_MAX_VIDEO_NAME_LEN){
  739. const char *pIndex = strrchr(videofilename, SPLIT_SLASH);
  740. if (pIndex){
  741. snprintf(strFileName, RVC_MAX_VIDEO_NAME_LEN, "%s", pIndex + 1);
  742. }
  743. }
  744. else{
  745. return iRet;
  746. }
  747. int ioffset = 0;
  748. if (0 == memcmp(strFileName, RVC_TRANSATCION_RECORD_SUFFIX, strlen(RVC_TRANSATCION_RECORD_SUFFIX))) {
  749. ioffset = strlen(RVC_TRANSATCION_RECORD_SUFFIX);
  750. }
  751. char* pNum = strstr(strFileName+ioffset, "_");
  752. if (pNum){
  753. *pNum = 0;
  754. strcpy(strSession, strFileName);
  755. pNum++;
  756. }
  757. else{
  758. return iRet;
  759. }
  760. char* pend = strstr(pNum, "_end");
  761. if (pend){
  762. *pend = 0;
  763. *iSeriesNum = atoi(pNum);
  764. pNum = pend + 1;
  765. }
  766. char* pFormat = strstr(pNum, ".");
  767. if (pFormat){
  768. *pFormat = 0;
  769. pFormat++;
  770. strcpy(strFormat, pFormat);
  771. }
  772. else{
  773. return iRet;
  774. }
  775. if (NULL == pend){
  776. *iSeriesNum = atoi(pNum);
  777. }
  778. iRet = 0;
  779. return iRet;
  780. }
  781. int CRecorderEntity::SaveExceptionRecordVideos()
  782. {
  783. int iRet = -1;
  784. #ifdef RVC_OS_WIN
  785. char srcFilePath[MAX_PATH]={0};
  786. WIN32_FIND_DATA FindFileData;
  787. HANDLE hFind;
  788. bool fFinished = false;
  789. char strVideoFormat[MAX_PATH] = { 0 };
  790. snprintf(strVideoFormat, MAX_PATH, "%s", RECORD_MP4_SUFFIX);
  791. snprintf(srcFilePath, MAX_PATH, "%s*.%s", m_TempDir.GetData(), strVideoFormat);
  792. hFind = FindFirstFile(srcFilePath, &FindFileData);
  793. if (INVALID_HANDLE_VALUE != hFind)
  794. {
  795. while (!fFinished){
  796. if (FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes){
  797. goto on_next;
  798. }
  799. if (0 == memcmp(FindFileData.cFileName, RVC_TRANSATCION_RECORD_SUFFIX, strlen(RVC_TRANSATCION_RECORD_SUFFIX)) ||
  800. 0 == memcmp(FindFileData.cFileName, RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX, strlen(RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX))){
  801. CSimpleStringA srcfile = CSimpleStringA::Format("%s%s",m_TempDir.GetData(), FindFileData.cFileName);
  802. unsigned long uSize = GetFileSize(srcfile.GetData());
  803. if (RVC_MIN_RECORD_FILESIZE >= uSize) {
  804. LogWarn(Severity_Low, Error_Debug, LOG_EVT_INVALID_RECORD_FILE, CSimpleStringA::Format("invalid record file %s, delelte it.", srcfile.GetData()).GetData());
  805. RvcDeleteFile(srcfile.GetData());
  806. goto on_next;
  807. }
  808. if (m_bEncFlag){
  809. filecryption_callback_t cb = {0};
  810. cb.dbg = &rvcDbg;
  811. if (false == is_file_encrypted(srcfile.GetData(), &cb)){
  812. if (is_file_completed(srcfile.GetData(), &cb)) {
  813. HandleEncryptVideoRecord(srcfile.GetData());
  814. }
  815. }
  816. else{
  817. char* pIndex = NULL;
  818. if (pIndex = strstr(FindFileData.cFileName, RVC_FILEENC_STR)){
  819. char strname[MAX_PATH] = {0};
  820. memcpy(strname, pIndex+strlen(RVC_FILEENC_STR), strlen(pIndex+strlen(RVC_FILEENC_STR)));
  821. CSimpleStringA tempsrcfile = CSimpleStringA::Format("%s%s",m_TempDir.GetData(), strname);
  822. if (!rename(srcfile.GetData(),tempsrcfile.GetData())){
  823. srcfile = tempsrcfile;
  824. memset(FindFileData.cFileName, 0, MAX_PATH);
  825. memcpy(FindFileData.cFileName, strname, strlen(strname));
  826. }
  827. else{
  828. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_RENAME_FAILED, CSimpleStringA::Format("Error Code %lu while rename %s.", GetLastError(), srcfile.GetData()).GetData());
  829. }
  830. }
  831. }
  832. }
  833. CSimpleStringA dstfile = CSimpleStringA::Format("%s%s",m_RecordSaveDir.GetData(), FindFileData.cFileName);
  834. bool bRet = rvcMoveFile(srcfile.GetData(), dstfile.GetData());
  835. if(!bRet) {
  836. 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());
  837. }
  838. else{
  839. AddToVideoRecordList(dstfile.GetData());
  840. iRet = 0;
  841. }
  842. }
  843. on_next:
  844. if (!FindNextFile(hFind, &FindFileData)){
  845. if (GetLastError() == ERROR_NO_MORE_FILES){
  846. fFinished = true;
  847. }
  848. else{
  849. break;
  850. }
  851. }
  852. }
  853. FindClose(hFind);
  854. }
  855. #else
  856. struct dirent* ptr;
  857. DIR* dir = opendir(m_TempDir.GetData());
  858. while ((ptr = readdir(dir)) != NULL)
  859. {
  860. if (ptr->d_type & DT_DIR) {
  861. continue;
  862. }
  863. if (0 == memcmp(ptr->d_name, RVC_TRANSATCION_RECORD_SUFFIX, strlen(RVC_TRANSATCION_RECORD_SUFFIX)) ||
  864. 0 == memcmp(ptr->d_name, RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX, strlen(RVC_ENCRYPT_TRANSATCION_RECORD_SUFFIX))) {
  865. CSimpleStringA srcfile = CSimpleStringA::Format("%s%s", m_TempDir.GetData(), ptr->d_name);
  866. unsigned long uSize = GetFileSize(srcfile.GetData());
  867. if (RVC_MIN_RECORD_FILESIZE >= uSize) {
  868. LogWarn(Severity_Low, Error_Debug, LOG_EVT_INVALID_RECORD_FILE, CSimpleStringA::Format("invalid record file %s, delelte it.", srcfile.GetData()).GetData());
  869. RvcDeleteFile(srcfile.GetData());
  870. continue;
  871. }
  872. if (m_bEncFlag) {
  873. filecryption_callback_t cb = { 0 };
  874. cb.dbg = &rvcDbg;
  875. if (false == is_file_encrypted(srcfile.GetData(), &cb)) {
  876. if (is_file_completed(srcfile.GetData(), &cb)) {
  877. HandleEncryptVideoRecord(srcfile.GetData());
  878. }
  879. }
  880. else {
  881. char* pIndex = NULL;
  882. if (pIndex = strstr(ptr->d_name, RVC_FILEENC_STR)) {
  883. char strname[MAX_PATH] = { 0 };
  884. memcpy(strname, pIndex + strlen(RVC_FILEENC_STR), strlen(pIndex + strlen(RVC_FILEENC_STR)));
  885. CSimpleStringA tempsrcfile = CSimpleStringA::Format("%s%s", m_TempDir.GetData(), strname);
  886. if (rvcMoveFile(srcfile.GetData(), tempsrcfile.GetData())) {
  887. srcfile = tempsrcfile;
  888. memset(ptr->d_name, 0, MAX_PATH);
  889. memcpy(ptr->d_name, strname, strlen(strname));
  890. }
  891. else {
  892. LogWarn(Severity_Low, Error_Debug, LOG_EVT_RECORDER_RENAME_FAILED, CSimpleStringA::Format("%s(%d) while rename %s.", strerror(errno), errno, srcfile.GetData()).GetData());
  893. }
  894. }
  895. }
  896. }
  897. CSimpleStringA dstfile = CSimpleStringA::Format("%s%s", m_RecordSaveDir.GetData(), ptr->d_name);
  898. bool bRet = rvcMoveFile(srcfile.GetData(), dstfile.GetData());
  899. if (!bRet) {
  900. 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());
  901. }
  902. else {
  903. AddToVideoRecordList(dstfile.GetData());
  904. iRet = 0;
  905. }
  906. }
  907. }
  908. closedir(dir);
  909. #endif // RVC_OS_WIN
  910. return iRet;
  911. }
  912. SP_BEGIN_ENTITY_MAP()
  913. SP_ENTITY(CRecorderEntity)
  914. SP_END_ENTITY_MAP()