mod_recorder.cpp 34 KB

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