mod_recorder.cpp 34 KB

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