mod_recorder.cpp 32 KB

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