mod_recorder.cpp 33 KB

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