mod_SurveillanceRecorder.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455
  1. #include "stdafx.h"
  2. #include "mod_SurveillanceRecorder.h"
  3. #include "y2k_time.h"
  4. #include "ModuleMix.h"
  5. #include "rec_common.h"
  6. #include "fileutil.h"
  7. #include "array.h"
  8. #include <memutil.h>
  9. #include <algorithm>
  10. #include "..\mod_CameraConfigManage\CameraConfigManage_client_g.h"
  11. #include "..\mod_CameraConfigManage\CameraConfigManage_def_g.h"
  12. using namespace CameraConfigManage;
  13. #define TIMERID_CAMCHK 1
  14. #define TIMERID_RECORD 2
  15. #define TIMERID_UPLOAD 3
  16. #define CAMCHK_CYCLE 5000
  17. #define RECORD_CYCLE 60000
  18. #define UPLOAD_CYCLE 600000
  19. #define MAX_DISK_PERCENT 95
  20. #define DATE_LEN 10
  21. #define MAX_LISTSIZE 50
  22. using namespace SurveillanceRecorder;
  23. using namespace LibVideoEcho;
  24. extern DeviceTypeEnum g_eDeviceType;
  25. static ErrorCodeEnum ParseIPAddress(const char *str, CSimpleStringA &ip, int &port)
  26. {
  27. ErrorCodeEnum Error = Error_Unexpect;
  28. if (str) {
  29. char tmp1[32] = {};
  30. char tmp2[16] = {};
  31. sscanf(str, "%s %s", tmp1, tmp2);
  32. if (tmp1[0] != 0 && tmp2[0] != 0)
  33. {
  34. ip = tmp1;
  35. port = atoi(tmp2);
  36. Error = Error_Succeed;
  37. }
  38. else
  39. return Error;
  40. }
  41. return Error;
  42. }
  43. // 寻找某目录下与通配符匹配的文件 add by ly at 2017/03/25
  44. static BOOL FindMatchedFile(LPCSTR sFindPath, LPCSTR sFindFileName, std::vector<std::string> &sFiles)
  45. {
  46. char sPath[MAX_PATH];
  47. memset(sPath, 0, MAX_PATH);
  48. char sFormatFileName[MAX_PATH+2] = "*";
  49. WIN32_FIND_DATA FindFileData;
  50. HANDLE hFind;
  51. BOOL fFinished = FALSE;
  52. lstrcpy(sFormatFileName, sFindPath);
  53. if(sFindPath[strlen(sFindPath)-1] != '\\')
  54. {
  55. lstrcat(sFormatFileName, "\\*");
  56. }
  57. else
  58. {
  59. lstrcat(sFormatFileName, "*");
  60. }
  61. lstrcat(sFormatFileName, sFindFileName);
  62. lstrcat(sFormatFileName, "*");
  63. hFind = FindFirstFile(sFormatFileName, &FindFileData);
  64. if (hFind == INVALID_HANDLE_VALUE)
  65. {
  66. return FALSE;
  67. }
  68. else
  69. {
  70. while (!fFinished)
  71. {
  72. lstrcpy(sPath, sFindPath);
  73. if(sPath[strlen(sPath)-1] != '\\')
  74. {
  75. lstrcat(sPath, "\\");
  76. }
  77. lstrcat(sPath, FindFileData.cFileName);
  78. if (!(FILE_ATTRIBUTE_DIRECTORY & FindFileData.dwFileAttributes))
  79. {
  80. sFiles.push_back(FindFileData.cFileName);
  81. }
  82. if (!FindNextFile(hFind, &FindFileData))
  83. {
  84. if (GetLastError() == ERROR_NO_MORE_FILES)
  85. {
  86. fFinished = TRUE;
  87. }
  88. else
  89. {
  90. break;
  91. }
  92. }
  93. }
  94. FindClose(hFind);
  95. }
  96. return TRUE;
  97. }
  98. static BOOL CheckDiskStatus(const char *szRoot, int nPercent)
  99. {
  100. _ULARGE_INTEGER lpFreeBytesAvailableToCaller = {}, lpTotalNumberOfBytes = {}, lpTotalNumberOfFreeBytes = {};
  101. BOOL ret = GetDiskFreeSpaceEx(szRoot, &lpFreeBytesAvailableToCaller, &lpTotalNumberOfBytes, &lpTotalNumberOfFreeBytes);
  102. if (ret == 0)
  103. {
  104. Dbg("CheckDiskStatus.GetDiskFreeSpaceEx failed(%d).",GetLastError());
  105. return FALSE;
  106. }
  107. DWORD dwTotal = lpTotalNumberOfBytes.QuadPart/1048576;
  108. DWORD dwTotalFree = lpTotalNumberOfFreeBytes.QuadPart/1048576;
  109. int ratio = dwTotalFree*100/dwTotal;
  110. Dbg("lytest free disk %d MB, %d percent free.\n", dwTotalFree, ratio);
  111. //if (ratio < (100-MAX_DISK_PERCENT))
  112. if (ratio < (100-nPercent))
  113. {
  114. return FALSE;
  115. }
  116. return TRUE;
  117. }
  118. void CSurveillanceRecorderEntity::OnPreStart( CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext )
  119. {
  120. m_eDeviceType = eStand2sType;
  121. g_eDeviceType = eStand2sType;
  122. //MessageBoxA(0,0,0,0);
  123. m_bStartCamera = FALSE;
  124. m_bCamConfig = FALSE;
  125. m_bRecordPaused = FALSE;
  126. m_bStartRecord = FALSE;
  127. m_bCameraInserted = FALSE;
  128. //m_iCameraState = CAMERA_TYPE_EWS;
  129. m_bExternStartCam = FALSE;
  130. m_max_disk_percent = MAX_DISK_PERCENT;
  131. m_bNeedRestart = false;
  132. LOG_FUNCTION();
  133. ErrorCodeEnum Error;
  134. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  135. CSystemStaticInfo stStaticinfo;
  136. spFunction->GetSystemStaticInfo(stStaticinfo);
  137. if (stricmp(stStaticinfo.strMachineType,"RVC.PAD")==0)
  138. {
  139. if (stricmp(stStaticinfo.strSite,"CMB.FLB")==0)
  140. {
  141. LOG_TRACE("the type is mobile pad");
  142. m_eDeviceType = eMobilePadType;
  143. }
  144. else
  145. {
  146. LOG_TRACE("the type is pad");
  147. m_eDeviceType = ePadtype;
  148. }
  149. }
  150. else if (stricmp(stStaticinfo.strMachineType,"RVC.Desk2S")==0)
  151. {
  152. LOG_TRACE("the type is Desk2S");
  153. m_eDeviceType = eDesk2SType;
  154. }
  155. else if (stricmp(stStaticinfo.strMachineType,"RPM.Stand1S")==0)
  156. {
  157. LOG_TRACE("the type is RPM.Stand1S");
  158. m_eDeviceType = eRpm1sType;
  159. }
  160. else
  161. {
  162. LOG_TRACE("the type is standard");
  163. m_eDeviceType = eStand2sType;
  164. }
  165. g_eDeviceType = m_eDeviceType;
  166. m_terminalNo = stStaticinfo.strTerminalID;
  167. SetCameraSysVar(CAMERA_NO_ERROR);
  168. memset(&conf,0,sizeof(ews_capture_config_t));
  169. Error = (ErrorCodeEnum)ews_capture_lib_init();
  170. if (Error == Error_Succeed)
  171. {
  172. Error = LoadConfig(&conf);
  173. if (Error!=Error_Succeed)
  174. {
  175. LOG_TRACE("Load EwsCamera Config failed(0x%x)!",Error);
  176. /*if (Error == Error_Resource)
  177. {
  178. LOG_TRACE("SurveillanceRecord is not allowed!");
  179. }
  180. else if (Error == Error_EwsCamera)
  181. {
  182. Dbg("Camera State to CAMERA_EWS_ERROR");
  183. GetFunction()->SetUserDefineState(USER_SURVEILLANCE_EWSERROR);
  184. }*/
  185. if (Error == Error_EwsCamera)
  186. {
  187. Dbg("Camera State to CAMERA_EWS_ERROR");
  188. GetFunction()->SetUserDefineState(USER_SURVEILLANCE_EWSERROR);
  189. }
  190. }
  191. else
  192. {
  193. m_bCameraInserted = TRUE;
  194. }
  195. }
  196. else
  197. {
  198. LOG_TRACE("ews_capture_lib_init failed!");
  199. }
  200. int i = 0;
  201. if ((ePadtype==m_eDeviceType||eDesk2SType==m_eDeviceType)&&m_bRecordAllowed)
  202. {
  203. // 初始化中台服务器 add by ly 2017/03/23
  204. Error = LoadServerConfig();
  205. if (Error != Error_Succeed) {
  206. LOG_TRACE("load server config failed!");
  207. pTransactionContext->SendAnswer(Error);
  208. return;
  209. }
  210. Error = GetFunction()->GetPath("Temp", m_TempDir);
  211. if (Error != Error_Succeed) {
  212. LOG_TRACE("get global record temp path failed!");
  213. pTransactionContext->SendAnswer(Error);
  214. return;
  215. }
  216. if (m_TempDir.GetLength() > 0 && m_TempDir[m_TempDir.GetLength()-1] != '\\') {
  217. m_TempDir += "\\";
  218. }
  219. Error = GetFunction()->GetPath("UploadVideo", m_RecordSaveDir);
  220. if (Error != Error_Succeed) {
  221. LOG_TRACE("get global record save path failed!");
  222. pTransactionContext->SendAnswer(Error);
  223. return;
  224. }
  225. if (m_RecordSaveDir.GetLength() > 0 && m_RecordSaveDir[m_RecordSaveDir.GetLength()-1] != '\\') {
  226. m_RecordSaveDir += "\\";
  227. }
  228. /*m_hUploadAnsEvt = CreateEvent(NULL, FALSE, FALSE, 0);
  229. if (!m_hUploadAnsEvt) {
  230. LOG_TRACE("create event handle failed!");
  231. pTransactionContext->SendAnswer(Error_NotInit);
  232. return;
  233. }*/
  234. // add by ly at 2017/05/25
  235. Error = GetFunction()->RegistSysVarEvent(SYSVAR_DISKSTATEUS_FOR_VIDEO, this);
  236. if (Error != Error_Succeed)
  237. {
  238. LOG_TRACE("register sysvar %s failed!", SYSVAR_DISKSTATEUS_FOR_VIDEO);
  239. }
  240. Dbg("[dbg] bAutoRecord:%d", m_bAutoMode);
  241. if (m_bAutoMode)
  242. {
  243. m_arrListener.Init(13);
  244. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_UI_STOP_CONTINURECORD, NULL, false);
  245. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_UI_START_ECHO_CONTINURECORD, NULL, false);
  246. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_UI_STOP_ECHO_CONTINURECORD, NULL, false);
  247. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, EVENT_USB_CAMER_INPUT, NULL, false);
  248. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, EVENT_USB_CAMER_OUTPUT, NULL, false);
  249. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_START_CONTINURECORD, NULL, false);
  250. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_STOP_CONTINURECORD, NULL, false);
  251. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_High, Error_IgnoreAll, LOG_EVT_CONTINURECORD_FAILED, NULL, false);
  252. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_High, Error_IgnoreAll, LOG_EVT_RESTART_EWSCAMERA, NULL, false);
  253. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_CONTINURECORD_SECTION_FINISHED, NULL, false);
  254. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_BEGIN_CAMERA_CONFIG, NULL, false);
  255. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_END_CAMERA_CONFIG, NULL, false);
  256. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_High, Error_IgnoreAll, LOG_EVT_CONTINURECORD_ENTITY_EXCEPTION, NULL, false);
  257. }
  258. else
  259. {
  260. m_arrListener.Init(13);
  261. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_UI_START_CONTINURECORD, NULL, false);
  262. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_UI_STOP_CONTINURECORD, NULL, false);
  263. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_UI_START_ECHO_CONTINURECORD, NULL, false);
  264. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_UI_STOP_ECHO_CONTINURECORD, NULL, false);
  265. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, EVENT_USB_CAMER_INPUT, NULL, false);
  266. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, EVENT_USB_CAMER_OUTPUT, NULL, false);
  267. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_START_CONTINURECORD, NULL, false);
  268. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_STOP_CONTINURECORD, NULL, false);
  269. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_High, Error_IgnoreAll, LOG_EVT_CONTINURECORD_FAILED, NULL, false);
  270. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_CONTINURECORD_SECTION_FINISHED, NULL, false);
  271. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_BEGIN_CAMERA_CONFIG, NULL, false);
  272. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_Middle, Error_IgnoreAll, LOG_EVT_END_CAMERA_CONFIG, NULL, false);
  273. GetFunction()->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_High, Error_IgnoreAll, LOG_EVT_CONTINURECORD_ENTITY_EXCEPTION, NULL, false);
  274. }
  275. // 当前录像模式通知业务中台 add by ly 2017/04/12
  276. ContinuRecordMode evt1;
  277. evt1.isauto = GetContinuRecordMode();
  278. SpSendBroadcast(GetFunction(), SP_MSG_OF(ContinuRecordMode), SP_MSG_SIG_OF(ContinuRecordMode), evt1);
  279. Dbg("[dbg] GetContinuRecordMode broadcast sent!");
  280. // 初始状态通知到业务中台
  281. ContinuRecordInitialState evt;
  282. evt.statecod = GetContinuRecordState();
  283. SpSendBroadcast(GetFunction(), SP_MSG_OF(ContinuRecordInitialState), SP_MSG_SIG_OF(ContinuRecordInitialState), evt);
  284. Dbg("[dbg] ContinuRecordInitialState broadcast sent!");
  285. }
  286. if (conf.strAudioIn.GetLength()>1) {
  287. if (m_kept_volume_in>=0&&m_kept_volume_in<=100) {
  288. m_pKeeperIn = volume_keeper_create((LPCTSTR)conf.strAudioIn, 1, m_kept_volume_in);
  289. if (m_pKeeperIn) {
  290. Dbg("EWS in volume keeper create ok! %d", m_kept_volume_in);
  291. } else {
  292. Dbg("EWS in volume keeper create failed!");
  293. }
  294. }
  295. }
  296. pTransactionContext->SendAnswer(Error_Succeed);
  297. }
  298. void CSurveillanceRecorderEntity::OnStarted()
  299. {
  300. m_pSelfcheckClient = new SelfChekerClient(this);
  301. ErrorCodeEnum Error = m_pSelfcheckClient->Connect();
  302. if (Error != Error_Succeed)
  303. {
  304. m_pSelfcheckClient->SafeDelete();
  305. m_pSelfcheckClient = NULL;
  306. Dbg("SelfcheckClient connect fail!");
  307. }
  308. else
  309. {
  310. Dbg("SelfcheckClient connect success!");
  311. }
  312. // Auto Start Continu Record edit by ly 2017/04/12
  313. if ((ePadtype==m_eDeviceType||eDesk2SType==m_eDeviceType)&&m_bRecordAllowed)
  314. {
  315. GetFunction()->SetTimer(TIMERID_UPLOAD,this,UPLOAD_CYCLE);
  316. if (m_bAutoMode&&!m_bCamConfig)
  317. {
  318. LogEvent(Severity_Middle,LOG_EVT_START_CONTINURECORD,NULL);
  319. }
  320. }
  321. }
  322. void CSurveillanceRecorderEntity::OnPreClose( EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext )
  323. {
  324. LOG_FUNCTION();
  325. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  326. spFunction->UnregistSysVarEvent(SYSVAR_DISKSTATEUS_FOR_VIDEO);
  327. if ((ePadtype==m_eDeviceType||eDesk2SType==m_eDeviceType)&&m_bRecordAllowed)
  328. {
  329. for (int i = 0; i < m_arrListener.GetCount(); ++i)
  330. {
  331. spFunction->UnsubscribeLog(m_arrListener[i]);
  332. }
  333. }
  334. SecureClientRelease();
  335. /*SetEvent(m_hUploadAnsEvt);
  336. CloseHandle(m_hUploadAnsEvt);
  337. m_hUploadAnsEvt = NULL;*/
  338. // Auto Stop Continu Record add by ly 2017/03/20
  339. StopEchoRecord(); // 停止回显
  340. StopRecord(); // 停止全程录像
  341. StopCamera(); // 停止摄像头
  342. GetFunction()->KillTimer(TIMERID_RECORD);
  343. GetFunction()->KillTimer(TIMERID_CAMCHK);
  344. GetFunction()->KillTimer(TIMERID_UPLOAD);
  345. if (m_pRecorder)
  346. {
  347. delete m_pRecorder;
  348. m_pRecorder = NULL;
  349. }
  350. if (m_pKeeperIn) {
  351. volume_keeper_destroy(m_pKeeperIn);
  352. m_pKeeperIn = NULL;
  353. }
  354. if (m_ews_capture) {
  355. ews_capture_stop(m_ews_capture);
  356. ews_capture_destroy(m_ews_capture);
  357. m_ews_capture = NULL;
  358. }
  359. ews_capture_lib_term();
  360. pTransactionContext->SendAnswer(Error_Succeed);
  361. }
  362. void CSurveillanceRecorderEntity::OnTimeout( DWORD dwTimerID )
  363. {
  364. if (dwTimerID == TIMERID_CAMCHK)
  365. {
  366. int ews_n;
  367. DWORD nLastEwsTime;
  368. bool bEwsTimeout = false;
  369. if (!m_ews_capture)
  370. {
  371. Dbg("m_ews_capture =Null");
  372. return;
  373. }
  374. if (!m_bStartCamera)
  375. {
  376. Dbg("m_bStartCamera =false");
  377. return;
  378. }
  379. ews_capture_detect_camera_bug(m_ews_capture, &ews_n);
  380. ews_capture_get_last_frametime(m_ews_capture,&nLastEwsTime);
  381. if (ews_n == 0)
  382. {
  383. if (m_iEwsEmptyTimes < CAMERA_BUG_THRESHOLD)
  384. {
  385. m_iEwsEmptyTimes++;
  386. Dbg("get ews camera picture fail,Times=%d",m_iEwsEmptyTimes);
  387. }
  388. }
  389. else if (ews_n == -1)
  390. {
  391. m_iEwsEmptyTimes = CAMERA_BUG_THRESHOLD;
  392. }
  393. else
  394. {
  395. if (m_iEwsEmptyTimes == CAMERA_BUG_THRESHOLD)
  396. {
  397. Dbg("ews camera get right!");
  398. }
  399. m_iEwsEmptyTimes = 0;
  400. }
  401. //ews图像是否超时
  402. if(nLastEwsTime > 0)
  403. {
  404. //Dbg("now=%d,nLastEwsTime=%d",y2k_time_now(),nLastEwsTime);
  405. if(((y2k_time_now()-nLastEwsTime) > CAMERA_TIMEOUT)&&!bEwsTimeout)
  406. {
  407. bEwsTimeout = true;
  408. Dbg("bEwsTimeout");
  409. }
  410. else if (((y2k_time_now()-nLastEwsTime) < CAMERA_TIMEOUT)&&bEwsTimeout)
  411. {
  412. bEwsTimeout = false;
  413. Dbg("change ews camera from bEwsTimeout to OK");
  414. }
  415. }
  416. //ews是否30S没有图像或者图像20秒没有更新
  417. if(((m_iEwsEmptyTimes == CAMERA_BUG_THRESHOLD)||bEwsTimeout)&&(m_nCameraErrorCode!=Error_EwsCamera))
  418. {
  419. char strMessage[MAX_PATH*2] = {0};
  420. get_external_camera_exception_message(strMessage, MAX_PATH*2, m_CamFriendlyName, "Ews camera bug detected!");
  421. LogError(Severity_Middle, Error_Hardware, ERROR_MOD_SURVEILLANCERECORDER_EWSCAMERA_BUG, strMessage);
  422. if (ePadtype==m_eDeviceType||eDesk2SType==m_eDeviceType)
  423. {
  424. m_nCameraErrorCode = Error_EwsCamera;
  425. ChangeSysvar(m_nCameraErrorCode);
  426. //RealSelfCheck();
  427. }
  428. }
  429. else if((m_iEwsEmptyTimes != CAMERA_BUG_THRESHOLD)&&!bEwsTimeout&&((m_nCameraErrorCode == Error_EwsCamera)))
  430. {
  431. if (ePadtype==m_eDeviceType||eDesk2SType==m_eDeviceType)
  432. {
  433. m_nCameraErrorCode = Error_Succeed;
  434. ChangeSysvar(m_nCameraErrorCode);
  435. }
  436. }
  437. //重启摄像头
  438. if((m_nCameraErrorCode == Error_EwsCamera)&&(m_nEwsCameraRestartNum<CAMERA_RESTARTNUM))
  439. {
  440. if (ReStartEwsCamera() != Error_Succeed)
  441. {
  442. m_nEwsCameraRestartNum++;
  443. }
  444. else
  445. {
  446. m_nEwsCameraRestartNum = 0;
  447. m_nCameraErrorCode = Error_Succeed;
  448. ChangeSysvar(m_nCameraErrorCode);
  449. m_iEwsEmptyTimes = 0;
  450. bEwsTimeout = false;
  451. }
  452. }
  453. }
  454. else if (dwTimerID == TIMERID_RECORD)
  455. {
  456. Dbg("On record timer!");
  457. std::string strWmvFile;
  458. BOOL bRet = GetCurrentSectionBySerialID((LPCTSTR)m_uid.ToString(),strWmvFile);
  459. if (!bRet)
  460. {
  461. Dbg("failed to get current record file name!");
  462. }
  463. bRet = UpdateVideoRecord(strWmvFile.c_str());
  464. if (!bRet)
  465. {
  466. Dbg("failed to update video record!");
  467. }
  468. //m_last_disk_overtop = !CheckDiskStatus("C:\\",m_max_disk_percent);
  469. //if (m_last_disk_overtop)
  470. //{
  471. // //LogEvent(Severity_Middle,LOG_EVT_STOP_CONTINURECORD,NULL);
  472. // //OnWideAngleCameraStartFailed("磁盘所剩空间不足,已停止全程双录!");
  473. // LogEvent(Severity_High,LOG_EVT_CONTINURECORD_FAILED,"磁盘所剩空间不足,已停止全程双录!");
  474. // return;
  475. //}
  476. TCHAR strNowDate[DATE_LEN+1];
  477. SYSTEMTIME st;
  478. GetLocalTime(&st);
  479. sprintf_s(strNowDate, "%4d-%02d-%02d", st.wYear,st.wMonth, st.wDay);
  480. if(strncmp(strNowDate, (LPCTSTR)m_RecordBeginTime, DATE_LEN))
  481. {
  482. Dbg("T to T+1, restart continue record!");
  483. LogEvent(Severity_Middle,LOG_EVT_STOP_CONTINURECORD,NULL);
  484. }
  485. }
  486. else if (dwTimerID == TIMERID_UPLOAD)
  487. {
  488. /*UploadVideoRecord();*/
  489. Dbg("On upload timer!");
  490. UploadVideoRecordTask *task = new UploadVideoRecordTask(this);
  491. GetFunction()->PostThreadPoolTask(task);
  492. //GetFunction()->PostEntityTaskFIFO(task);
  493. //DWORD dwCode = WaitForSingleObject(m_hUploadAnsEvt, 60000);
  494. //Dbg("dwCode=0x%x while wait for upload event", dwCode);
  495. }
  496. else
  497. {
  498. Dbg("invalid timer id!");
  499. }
  500. }
  501. ErrorCodeEnum CSurveillanceRecorderEntity::SetCameraSysVar( const CSimpleStringA &newVal )
  502. {
  503. CSmartPointer<IEntityFunction> spFunction = this->GetFunction();
  504. if (!(ePadtype==m_eDeviceType||eDesk2SType==m_eDeviceType))
  505. {
  506. return spFunction->SetSysVar(SYSVAR_EWSCAMERASTATE, (LPCTSTR)newVal);
  507. }
  508. CSimpleStringA val = newVal;
  509. switch (val[0])
  510. {
  511. case 'W':
  512. val = val.Append(m_CamFriendlyName.GetLength()>1?(LPCTSTR)m_CamFriendlyName:"全程双录");
  513. break;
  514. case 'N':
  515. case 'I':
  516. default:
  517. break;
  518. }
  519. return spFunction->SetSysVar(SYSVAR_EWSCAMERASTATE, (LPCTSTR)val);
  520. }
  521. ErrorCodeEnum CSurveillanceRecorderEntity::ChangeSysvar( ErrorCodeEnum op )
  522. {
  523. switch (op)
  524. {
  525. case Error_Succeed:
  526. Dbg("Camera State to No_Error");
  527. SetCameraSysVar(CAMERA_NO_ERROR);
  528. GetFunction()->SetUserDefineState(USER_SURVEILLANCE_IDLE);
  529. break;
  530. case Error_EwsCamera:
  531. Dbg("Camera State to Ews_Error");
  532. SetCameraSysVar(CAMERA_EWS_ERROR);
  533. GetFunction()->SetUserDefineState(USER_SURVEILLANCE_EWSERROR);
  534. break;
  535. default:
  536. break;
  537. }
  538. return Error_Succeed;
  539. }
  540. bool CSurveillanceRecorderEntity::SetAutoBright( ErrorCodeEnum nCode )
  541. {
  542. return ews_capture_set_autobrightness(m_ews_capture,nCode);
  543. }
  544. int CSurveillanceRecorderEntity::GetActiveCamera()
  545. {
  546. return 0;
  547. }
  548. HINSTANCE CSurveillanceRecorderEntity::GetEntityHinstance()
  549. {
  550. return ModuleBase::GetModuleBase()->GetInstance();
  551. }
  552. void CSurveillanceRecorderEntity::OnRecordFailed( bool bRecordDevFault )
  553. {
  554. Dbg("OnRecordFailed!");
  555. if (!bRecordDevFault)
  556. {
  557. LogEvent(Severity_High,LOG_EVT_CONTINURECORD_FAILED,NULL);
  558. }
  559. }
  560. void CSurveillanceRecorderEntity::OnRecordEntityExcption()
  561. {
  562. //MessageBoxA(0,"全程双录有出现异常,录像已停止,系统正在恢复中,预计30秒!", "录像异常",0);
  563. Dbg("全程双录出现异常,录像已停止,系统正在恢复中,预计60秒!");
  564. SYSTEMTIME st;
  565. GetLocalTime(&st);
  566. CSimpleStringA strMessage = CSimpleStringA::Format("%02d:%02d:%02d %s",st.wHour,st.wMinute, st.wSecond, "全程双录出现异常,录像已停止,系统正在恢复中,预计60秒!");
  567. LogEvent(Severity_High,LOG_EVT_CONTINURECORD_ENTITY_EXCEPTION,strMessage.GetData());
  568. }
  569. void CSurveillanceRecorderEntity::OnASectionFinished( const char *pszMessage )
  570. {
  571. // filename
  572. Dbg("file %s finished.", pszMessage);
  573. GetFunction()->KillTimer(TIMERID_RECORD);
  574. LogEvent(Severity_Middle, LOG_EVT_CONTINURECORD_SECTION_FINISHED, pszMessage);
  575. }
  576. void CSurveillanceRecorderEntity::OnRecordFinished()
  577. {
  578. Dbg("OnRecordFinished.");
  579. }
  580. ErrorCodeEnum CSurveillanceRecorderEntity::RealSelfCheck()
  581. {
  582. ErrorCodeEnum Error = Error_Succeed;
  583. if (NULL == m_pSelfcheckClient){
  584. m_pSelfcheckClient = new SelfChekerClient(this);
  585. Error = m_pSelfcheckClient->Connect();
  586. if (Error != Error_Succeed)
  587. {
  588. m_pSelfcheckClient->SafeDelete();
  589. m_pSelfcheckClient = NULL;
  590. Dbg("SelfcheckClient connect fail!");
  591. }
  592. else
  593. {
  594. Dbg("SelfcheckClient connect success!");
  595. }
  596. }
  597. if (Error_Succeed == Error)
  598. {
  599. SelfCheckerService_RealCheck_Req req;
  600. req.name = GetEntityName();
  601. SelfCheckerService_RealCheck_Ans ans;
  602. DWORD Timeout = 500;
  603. Error = m_pSelfcheckClient->RealCheck(req,ans,Timeout);
  604. if (Error!=Error_Succeed)
  605. {
  606. Dbg("RealSelfcheck fail!");
  607. if (m_nCameraErrorCode != Error_Succeed)
  608. {
  609. m_nCameraErrorCode = Error_Succeed;
  610. }
  611. }
  612. else
  613. {
  614. Dbg("RealSelfcheck suc!");
  615. }
  616. }
  617. else{
  618. Dbg("m_pSelfcheckClient is NULL.");
  619. }
  620. return Error;
  621. }
  622. ContinuRecordStateCode CSurveillanceRecorderEntity::GetContinuRecordState()
  623. {
  624. ContinuRecordStateCode statecod = State_ReadyForContinuRecord;
  625. if (conf.strVideo.GetLength()<=1||conf.strAudioIn.GetLength()<=1)
  626. {
  627. // 未配置
  628. statecod = State_EwsCameraConfigNotFound;
  629. }
  630. else if (!m_bCameraInserted)
  631. {
  632. // 未插入
  633. statecod = State_EwsCameraNotInserted;
  634. }
  635. else if(!m_bStartRecord)
  636. {
  637. // 未开始录像
  638. statecod = State_ReadyForContinuRecord;
  639. }
  640. else
  641. {
  642. if (m_nCameraErrorCode != Error_Succeed)
  643. {
  644. // 摄像头故障
  645. statecod = State_EwsCameraFault;
  646. }
  647. else
  648. {
  649. // 正常录像中
  650. statecod = State_ContinuRecording;
  651. }
  652. }
  653. return statecod;
  654. }
  655. bool CSurveillanceRecorderEntity::GetContinuRecordMode() const
  656. {
  657. return m_bAutoMode;
  658. }
  659. ErrorCodeEnum CSurveillanceRecorderEntity::ExternStartCamera()
  660. {
  661. Dbg("Extern start camera");
  662. // edit by ly@2019/04/18
  663. if (!(eDesk2SType==m_eDeviceType||ePadtype==m_eDeviceType||eMobilePadType==m_eDeviceType))
  664. {
  665. Dbg("Only support Desk2S or Pad!");
  666. return Error_Cancel;
  667. }
  668. if(m_bCamConfig)
  669. {
  670. Dbg("Camera config now!");
  671. return Error_Break;
  672. }
  673. if (conf.strVideo.GetLength() <= 1 || conf.strAudioIn.GetLength() <= 1)
  674. {
  675. Dbg("EwsCamera is not configed!");
  676. return Error_Resource;
  677. }
  678. if (!m_bCameraInserted)
  679. {
  680. Dbg("EwsCamera is not inserted!");
  681. return Error_DevNotAvailable;
  682. }
  683. if (m_bStartCamera)
  684. {
  685. Dbg("EwsCamera has started!");
  686. return Error_Succeed;
  687. }
  688. CSimpleStringA strCamState;
  689. GetFunction()->GetSysVar(SYSVAR_EWSCAMERASTATE,strCamState);
  690. if (strCamState == CAMERA_START_ING)
  691. {
  692. Dbg("EwsCamera is starting!");
  693. return Error_InvalidState;
  694. }
  695. // 通报正在启动广角摄像头
  696. SetCameraSysVar(CAMERA_START_ING);
  697. // 启动广角摄像头
  698. ErrorCodeEnum Error = StartEwsCamera();
  699. if (Error!=Error_Succeed)
  700. {
  701. Dbg("[dbg] Start Ews Camera Error=0x%x", Error);
  702. if(Error == Error_EwsCamera)
  703. {
  704. m_bStartCamera = FALSE;
  705. //SetCameraSysVar(CAMERA_EWS_ERROR);
  706. Dbg("Camera State to Error_EwsCamera");
  707. m_nCameraErrorCode = Error_EwsCamera;
  708. GetFunction()->SetUserDefineState(USER_SURVEILLANCE_EWSERROR);
  709. }
  710. else
  711. {
  712. //SetCameraSysVar(Error);
  713. Dbg("Camera State to Other Error");
  714. m_nCameraErrorCode = Error;
  715. m_bStartCamera = FALSE;
  716. }
  717. if (!m_bAutoMode)
  718. {
  719. RealSelfCheck();
  720. }
  721. }
  722. else
  723. {
  724. if (m_bRecordAllowed&&!m_pRecorder)
  725. {
  726. BOOL bRet = FALSE;
  727. m_pRecorder = new Clibwmvrecord(&bRet, this, REC_COMMON_AUDIO_EWS_SHM_QUEUE, REC_COMMON_VIDEO_EWS_SHM_RTP_QUEUE, NULL);
  728. }
  729. m_bStartCamera = TRUE;
  730. Dbg("Camera State to CAMERA_NO_ERROR");
  731. m_nCameraErrorCode = Error_Succeed;
  732. GetFunction()->SetUserDefineState(USER_SURVEILLANCE_IDLE);
  733. }
  734. SetCameraSysVar(CAMERA_NO_ERROR);
  735. m_bExternStartCam = TRUE;
  736. return Error;
  737. }
  738. ErrorCodeEnum CSurveillanceRecorderEntity::ExternStopCamera()
  739. {
  740. Dbg("Extern stop camera");
  741. // edit by ly@2019/04/18
  742. if (!(eDesk2SType==m_eDeviceType||ePadtype==m_eDeviceType||eMobilePadType==m_eDeviceType))
  743. {
  744. Dbg("Only support Desk2S or Pad!");
  745. return Error_Cancel;
  746. }
  747. if (m_bCamConfig)
  748. {
  749. Dbg("Camera config now!");
  750. return Error_Break;
  751. }
  752. if (!m_bStartRecord)
  753. {
  754. StopEwsCamera();
  755. }
  756. m_bExternStartCam = FALSE;
  757. return Error_Succeed;
  758. }
  759. ErrorCodeEnum CSurveillanceRecorderEntity::LoadConfig( ews_capture_config_t *conf )
  760. {
  761. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  762. CSmartPointer<IConfigInfo> spConfig;
  763. SpIniMappingTable table;
  764. ErrorCodeEnum Error = spFunction->OpenConfig(Config_CenterSetting, spConfig);
  765. CSimpleStringA strSecondBranch("");
  766. int iAudioQuality = 3;
  767. int iAudioSampleRate = 16;
  768. int iAudioNsPolicy = 2;
  769. int iIsAudioNsOn = 0;
  770. if (Error == Error_Succeed)
  771. {
  772. table.AddEntryBoolean("InteractiveControl", "SurveillanceRecordAllowed", m_bRecordAllowed, false);
  773. table.AddEntryBoolean("SurveillanceRecorder", "AutoMode", m_bAutoMode, false);
  774. table.AddEntryInt("SurveillanceRecorder", "MaxDiskPercent", m_max_disk_percent, MAX_DISK_PERCENT);
  775. table.AddEntryInt("SurveillanceRecorder", "SecondBranchAllowed", m_secondBranchAllowed, FALSE);
  776. table.AddEntryString("SurveillanceRecorder", "SecondBranchParams", strSecondBranch, "");
  777. table.AddEntryInt("SurveillanceRecorder", "AudioQuality", iAudioQuality, 3);
  778. table.AddEntryInt("SurveillanceRecorder", "AudioSampleRate", iAudioSampleRate, 16);
  779. table.AddEntryInt("SurveillanceRecorder","AudioNsPolicy",iAudioNsPolicy, 2);
  780. table.AddEntryInt("SurveillanceRecorder","IsAudioNsOn",iIsAudioNsOn,0);
  781. table.Load(spConfig);
  782. }
  783. if (8 != iAudioSampleRate){
  784. iAudioSampleRate = 16;
  785. }
  786. conf->iaudiosamplerate = iAudioSampleRate*1000;
  787. SetAudioQuality(iAudioQuality);
  788. SetAudioNsPolicy(iAudioNsPolicy);
  789. if (0 != iIsAudioNsOn){
  790. m_bIsAudioNsOn = true;
  791. }
  792. // edit by ly@2018/11/12
  793. if (!m_bRecordAllowed)
  794. {
  795. if (m_secondBranchAllowed && strSecondBranch.GetLength() > 0)
  796. {
  797. auto a1 = strSecondBranch.Split(',');
  798. if (a1.GetCount() > 0)
  799. {
  800. m_secondBranchParams = CAutoArray<SecondBranchParam>(a1.GetCount());
  801. for (int i = 0; i < a1.GetCount(); ++i)
  802. {
  803. memset(&m_secondBranchParams[i], 0, sizeof(SecondBranchParam));
  804. auto a2 = a1[i].Split('|');
  805. m_secondBranchParams[i].szDevicePrefix = a2[0];
  806. if (a2.GetCount() > 1)
  807. {
  808. m_secondBranchParams[i].bAutoMode = (!strcmp((LPCTSTR)a2[1], "1") ? TRUE : FALSE);
  809. }
  810. Dbg("secondBranchParams[%d]:%s,%d",i,
  811. (LPCTSTR)m_secondBranchParams[i].szDevicePrefix,
  812. m_secondBranchParams[i].bAutoMode);
  813. }
  814. }
  815. for (int i = 0; i < m_secondBranchParams.GetCount(); ++i)
  816. {
  817. int len = m_secondBranchParams[i].szDevicePrefix.GetLength();
  818. if (m_terminalNo.GetLength() >= len)
  819. {
  820. if (m_terminalNo.SubString(0, len) == m_secondBranchParams[i].szDevicePrefix)
  821. {
  822. m_bRecordAllowed = true;
  823. m_bAutoMode = m_secondBranchParams[i].bAutoMode;
  824. Dbg("bRecordAllowed:%d,m_bAutoMode:%d", m_bRecordAllowed, m_bAutoMode);
  825. break;
  826. }
  827. }
  828. }
  829. }
  830. }
  831. if (m_max_disk_percent < 0 || m_max_disk_percent > 100)
  832. {
  833. m_max_disk_percent = MAX_DISK_PERCENT;
  834. }
  835. table.Clear();
  836. Error = spFunction->OpenConfig(Config_Root, spConfig);
  837. if (Error == Error_Succeed)
  838. {
  839. table.AddEntryString("audio", "ews_in_dev", conf->strAudioIn, "$");
  840. table.AddEntryBoolean("audio", "ews_in_agc", conf->audio_ews_in_agc, false);
  841. table.AddEntryBoolean("audio", "ews_in_ns", conf->audio_ews_in_ns, false);
  842. table.AddEntryBoolean("audio", "ews_aec", conf->audio_ews_aec, false);
  843. table.AddEntryInt("audio", "ews_in_volume", m_kept_volume_in, 0);
  844. table.AddEntryString("Video", "EwsCamera", conf->strVideo, "$");
  845. table.AddEntryInt("Video", "EwsRotate", conf->video_rotate, 0);
  846. table.AddEntryString("Video", "EnvCamera", m_strEnvCam, "$");
  847. table.AddEntryString("Video", "OptCamera", m_strOptCam, "$");
  848. Error = table.Load(spConfig);
  849. if (Error == Error_Succeed) {
  850. //ews配置名自纠正
  851. AutoCorrectCameraConfigName(5000);
  852. conf->video_dev = ews_capture_get_video_device_id((LPCTSTR)conf->strVideo,m_CamFriendlyName);
  853. if (conf->video_dev == -1)
  854. {
  855. Dbg("find conf->strVideo:%s(-1)",(LPCTSTR)conf->strVideo);
  856. LogError(Severity_Middle,Error_DevMedia,ERROR_MOD_SURVEILLANCERECORDER_EWSCAM_INITFAIL,"external wide scale camera start error,please check config file or device");
  857. Error = Error_EwsCamera;
  858. return Error;
  859. }
  860. int nAudioId = ews_capture_get_audio_device_id(true, (LPCTSTR)conf->strAudioIn);
  861. if (nAudioId == -1)
  862. {
  863. Dbg("conf->strAudioIn:%s(-1)",(LPCTSTR)conf->strAudioIn);
  864. Error = Error_AudioIN;
  865. return Error;
  866. }
  867. }
  868. else
  869. {
  870. LOG_TRACE("load config failed!");
  871. }
  872. }
  873. else
  874. {
  875. LOG_TRACE("open config failed!");
  876. }
  877. return Error;
  878. }
  879. ErrorCodeEnum CSurveillanceRecorderEntity::CheckConfigEWSCameraName(){
  880. ErrorCodeEnum Error = Error_Succeed;
  881. CSimpleStringA strEwsCamFriendlyName = "$";
  882. //check Ews device
  883. if (conf.strVideo.GetLength() > 1)
  884. { // is not "$"
  885. int ews_id = ews_capture_get_video_device_id((LPCTSTR)conf.strVideo, strEwsCamFriendlyName);
  886. if (ews_id == -1)
  887. {
  888. Dbg("find strEwsCam:%s(-1)",(LPCTSTR)conf.strVideo);
  889. Error = Error_EwsCamera;
  890. return Error;
  891. }
  892. }
  893. else
  894. {
  895. //广角摄像头未配置,此时也认为广角摄像头OK(无广角摄像头场景)
  896. }
  897. return Error;
  898. }
  899. void CSurveillanceRecorderEntity::AutoCorrectCameraConfigName(long wait_time) {
  900. ErrorCodeEnum Error = Error_Succeed;
  901. int one_timeout = 400;
  902. int retry = 0;
  903. //检查配置名都成功,不用自动纠正
  904. Error = CheckConfigEWSCameraName();
  905. if (Error == Error_Succeed){
  906. return;
  907. }
  908. //只针对低柜双屏做自纠正
  909. if(eDesk2SType != m_eDeviceType){
  910. Dbg("AutoCorrectCameraConfig failed, DeviceType:%d is not Desk2SType", m_eDeviceType);
  911. return;
  912. }
  913. auto pCameraConfigManageClient = new CameraConfigManageService_ClientBase(this);
  914. while ((pCameraConfigManageClient->Connect() != Error_Succeed) && retry < wait_time/one_timeout)
  915. {
  916. Dbg("连接CameraConfigManage实体失败, retry:%d", retry);
  917. Sleep(one_timeout);
  918. retry++;
  919. }
  920. //连接不上,不纠正
  921. if (retry >= wait_time/one_timeout){
  922. pCameraConfigManageClient->SafeDelete();
  923. pCameraConfigManageClient = NULL;
  924. }
  925. else {
  926. CameraConfigManageService_AutoCorrectCameraConfig_Req req;
  927. CameraConfigManageService_AutoCorrectCameraConfig_Ans ans;
  928. DWORD Timeout = 5000;
  929. Error = pCameraConfigManageClient->AutoCorrectCameraConfig(req, ans, Timeout);
  930. if (Error != Error_Succeed)
  931. {
  932. Dbg("CameraConfigManage 调用 AutoCorrectCameraConfig 失败: 0x%x", Error);
  933. }
  934. else
  935. {
  936. Dbg("AutoCorrectCameraConfig, old EnvCamera;%s, OptCamera;%s, EwsCamera;%s",
  937. (LPCSTR)m_strEnvCam, (LPCSTR)m_strOptCam, (LPCSTR)conf.strVideo);
  938. Dbg("AutoCorrectCameraConfig, new EnvCamera;%s, OptCamera;%s, EwsCamera;%s",
  939. (LPCSTR)ans.EnvCamera, (LPCSTR)ans.OptCamera, (LPCSTR)ans.EwsCamera);
  940. if ((ans.EnvCamera.GetLength() > 1) && (m_strEnvCam.GetLength() > 1)){
  941. m_strEnvCam = ans.EnvCamera;
  942. }
  943. if ((ans.OptCamera.GetLength() > 1) && (m_strOptCam.GetLength() > 1)){
  944. m_strOptCam = ans.OptCamera;
  945. }
  946. if ((ans.EwsCamera.GetLength() > 1) && (conf.strVideo.GetLength() > 1)){
  947. conf.strVideo = ans.EwsCamera;
  948. }
  949. Dbg("AutoCorrectCameraConfig, updated EnvCamera;%s, OptCamera;%s, EwsCamera;%s",
  950. (LPCSTR)m_strEnvCam, (LPCSTR)m_strOptCam, (LPCSTR)conf.strVideo);
  951. }
  952. pCameraConfigManageClient->GetFunction()->CloseSession();
  953. pCameraConfigManageClient = NULL;
  954. }
  955. }
  956. void CSurveillanceRecorderEntity::OnWideAngleCameraStartFailed( const char* pszFailedMsg )
  957. {
  958. if (!pszFailedMsg) {
  959. return;
  960. }
  961. // 通报广角摄像头启动时故障
  962. WideAngleCameraStartFailed evt;
  963. evt.failedmsg = CSimpleStringA2W(pszFailedMsg);
  964. SpSendBroadcast(GetFunction(), SP_MSG_OF(WideAngleCameraStartFailed), SP_MSG_SIG_OF(WideAngleCameraStartFailed), evt);
  965. Dbg("[dbg] WideAngleCameraStartFailed broadcast sent!");
  966. }
  967. void CSurveillanceRecorderEntity::OnSysVarEvent( const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName )
  968. {
  969. LOG_TRACE("entity:%s key:%s value:%s", pszEntityName, pszKey, pszValue);
  970. if (_stricmp(pszKey,SYSVAR_DISKSTATEUS_FOR_VIDEO) == 0)
  971. {
  972. if (pszValue)
  973. {
  974. char state = pszValue[0];
  975. if (state == 'U')
  976. {
  977. Dbg("unknown state, maybe the video path is empty.");
  978. m_last_disk_overtop = FALSE;
  979. }
  980. else if (state == 'S')
  981. {
  982. Dbg("free disk space for video is sufficient for at least 7 days!");
  983. m_last_disk_overtop = FALSE;
  984. }
  985. else if (state == 'W')
  986. {
  987. Dbg("[Warn] free disk space for video is not sufficient for 7 days!");
  988. m_last_disk_overtop = FALSE;
  989. }
  990. else if (state == 'E')
  991. {
  992. Dbg("[Warn] free disk space for video is not sufficient for 3 days!");
  993. m_last_disk_overtop = FALSE;
  994. }
  995. else if (state == 'F')
  996. {
  997. Dbg("[Warn] free disk space for video is not sufficient for 1 day!");
  998. if (m_bStartRecord)
  999. {
  1000. LogEvent(Severity_High,LOG_EVT_CONTINURECORD_FAILED,"磁盘所剩空间不足,已停止全程双录!");
  1001. }
  1002. m_last_disk_overtop = TRUE;
  1003. }
  1004. else
  1005. {
  1006. m_last_disk_overtop = FALSE;
  1007. }
  1008. if (state != 'F')
  1009. {
  1010. if (m_bAutoMode && !m_bStartRecord && !m_bCamConfig)
  1011. {
  1012. LogEvent(Severity_Middle,LOG_EVT_START_CONTINURECORD,NULL);
  1013. }
  1014. }
  1015. }
  1016. }
  1017. }
  1018. void CSurveillanceRecorderEntity::OnLog( const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel, const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID, const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage )
  1019. {
  1020. LOG_TRACE("user_code = %08x", dwUserCode);
  1021. if ((dwUserCode == LOG_EVT_UI_START_CONTINURECORD ||
  1022. dwUserCode == LOG_EVT_START_CONTINURECORD) && !m_bCamConfig)
  1023. {
  1024. ContinuRecordStateCode statecode = GetContinuRecordState();
  1025. if (statecode == State_EwsCameraConfigNotFound)
  1026. {
  1027. OnWideAngleCameraStartFailed("广角摄像头未配置!");
  1028. return;
  1029. }
  1030. if (statecode == State_EwsCameraNotInserted)
  1031. {
  1032. OnWideAngleCameraStartFailed("广角摄像头未插入!");
  1033. return;
  1034. }
  1035. if (statecode == State_EwsCameraStarting)
  1036. {
  1037. OnWideAngleCameraStartFailed("广角摄像头正在启动中!");
  1038. return;
  1039. }
  1040. if (statecode == State_EwsCameraFault)
  1041. {
  1042. OnWideAngleCameraStartFailed("广角摄像头故障中!");
  1043. if (m_bAutoMode && !m_bCamConfig) // add by ly at 2017/05/16
  1044. {
  1045. Sleep(5000);
  1046. LogEvent(Severity_High,LOG_EVT_RESTART_EWSCAMERA,NULL);
  1047. }
  1048. return;
  1049. }
  1050. //if (!CheckDiskStatus("C:\\",m_max_disk_percent))
  1051. //{
  1052. // if (m_bAutoMode) // add by ly at 2017/05/16
  1053. // {
  1054. // if (!m_last_disk_overtop)
  1055. // {
  1056. // m_last_disk_overtop = TRUE;
  1057. // OnWideAngleCameraStartFailed("磁盘所剩空间不足,无法开始全程双录!");
  1058. // }
  1059. // Sleep(5000);
  1060. // LogEvent(Severity_High,LOG_EVT_RESTART_EWSCAMERA,NULL);
  1061. // }
  1062. // else
  1063. // {
  1064. // m_last_disk_overtop = TRUE;
  1065. // OnWideAngleCameraStartFailed("磁盘所剩空间不足,无法开始全程双录!");
  1066. // }
  1067. // return;
  1068. //}
  1069. //else
  1070. //{
  1071. // m_last_disk_overtop = FALSE;
  1072. //}
  1073. CSimpleStringA strDiskStatus;
  1074. GetFunction()->GetSysVar(SYSVAR_DISKSTATEUS_FOR_VIDEO,strDiskStatus);
  1075. Dbg("Disk status for video on start record:%s.",(LPCTSTR)strDiskStatus);
  1076. if (strDiskStatus.GetLength()>0&&strDiskStatus[0]=='F')
  1077. {
  1078. if (m_bAutoMode) // add by ly at 2017/05/16
  1079. {
  1080. if (!m_last_disk_overtop)
  1081. {
  1082. m_last_disk_overtop = TRUE;
  1083. OnWideAngleCameraStartFailed("磁盘所剩空间不足,无法开始全程双录!");
  1084. }
  1085. }
  1086. else
  1087. {
  1088. m_last_disk_overtop = TRUE;
  1089. OnWideAngleCameraStartFailed("磁盘所剩空间不足,无法开始全程双录!");
  1090. }
  1091. return;
  1092. }
  1093. else
  1094. {
  1095. m_last_disk_overtop = FALSE;
  1096. }
  1097. if (!m_bStartCamera) // 若摄像头未启动,则先启动摄像头 edit by ly@20180725
  1098. {
  1099. StartCamera();
  1100. }
  1101. else
  1102. {
  1103. WideAngleCameraStateChanged evt;
  1104. evt.statecod = State_EwsCameraStartSucceeded;
  1105. evt.statemsg = CSimpleStringA2W("已启动广角摄像头!");
  1106. evt.camname = CSimpleStringA2W(m_CamFriendlyName.GetLength()>1?m_CamFriendlyName:"广角");
  1107. SpSendBroadcast(GetFunction(), SP_MSG_OF(WideAngleCameraStateChanged), SP_MSG_SIG_OF(WideAngleCameraStateChanged), evt);
  1108. Dbg("[dbg] WideAngleCameraStateChanged broadcast sent!");
  1109. }
  1110. if (m_bStartCamera)
  1111. {
  1112. // 开始全程录像
  1113. Dbg("start record!");
  1114. //StartRecord(pszMessage);
  1115. m_uid = CUUID::Create(m_uid);
  1116. m_RecordBeginTime = m_uid.GetTime().ToTimeString();
  1117. Dbg("RecordBeginTime:%s", (LPCTSTR)m_RecordBeginTime);
  1118. StopRecord(); // 如果正在录像,则先停止
  1119. if (pszMessage&&strlen(pszMessage)>0)
  1120. {
  1121. StartRecord(pszMessage);
  1122. }
  1123. else
  1124. {
  1125. CSimpleStringA videoname = CSimpleStringA::Format("G_%s",(LPCTSTR)m_uid.ToString());
  1126. StartRecord((LPCTSTR)videoname);
  1127. }
  1128. // 开始录像计时和数据记录
  1129. RecordInit();
  1130. GetFunction()->SetTimer(TIMERID_RECORD, this, RECORD_CYCLE);
  1131. }
  1132. else
  1133. {
  1134. if (m_bAutoMode && !m_bCamConfig) // edit by ly 2017/04/12
  1135. {
  1136. LogEvent(Severity_High,LOG_EVT_RESTART_EWSCAMERA,NULL);
  1137. }
  1138. }
  1139. }
  1140. else if (dwUserCode == LOG_EVT_UI_STOP_CONTINURECORD ||
  1141. dwUserCode == LOG_EVT_STOP_CONTINURECORD)
  1142. {
  1143. Dbg("stop record!");
  1144. StopEchoRecord(); // 停止回显
  1145. StopRecord(); // 停止全程录像
  1146. StopCamera(); // 停止摄像头
  1147. GetFunction()->KillTimer(TIMERID_RECORD);
  1148. std::string strWmvFile;
  1149. BOOL bRet = GetCurrentSectionBySerialID((LPCTSTR)m_uid.ToString(),strWmvFile);
  1150. if (!bRet)
  1151. {
  1152. Dbg("failed to get current record file name!");
  1153. }
  1154. bRet = UpdateVideoRecord(strWmvFile.c_str());
  1155. if (!bRet)
  1156. {
  1157. Dbg("failed to update video record!");
  1158. }
  1159. CSimpleStringA srcfn = CSimpleStringA::Format("%s%s",
  1160. (LPCTSTR)m_TempDir,strWmvFile.c_str());
  1161. CSimpleStringA dstfn = CSimpleStringA::Format("%s%s",
  1162. (LPCTSTR)m_RecordSaveDir,strWmvFile.c_str());
  1163. bRet = MoveFile((LPCSTR)srcfn, (LPCSTR)dstfn);
  1164. if(!bRet) {
  1165. Dbg("Error Code %lu while move %s ", GetLastError(), (LPCSTR)srcfn);
  1166. }
  1167. //GetFunction()->KillTimer(TIMERID_UPLOAD);
  1168. CSimpleStringA cfn = CSimpleStringA::Format("%sG_%s.txt",(LPCTSTR)m_TempDir,(LPCTSTR)m_uid.ToString());
  1169. UploadVideoRecord((LPCTSTR)cfn);
  1170. if (!stricmp(pszMessage,"Fault"))
  1171. {
  1172. if (m_bAutoMode && !m_bCamConfig) // edit by ly 2017/04/12
  1173. {
  1174. LogEvent(Severity_High,LOG_EVT_RESTART_EWSCAMERA,NULL);
  1175. }
  1176. else
  1177. {
  1178. Dbg("[dbg] camera fault, RealSelfCheck!");
  1179. RealSelfCheck();
  1180. }
  1181. }
  1182. else
  1183. {
  1184. m_nCameraErrorCode = Error_Succeed;
  1185. if ((ePadtype==m_eDeviceType||eDesk2SType==m_eDeviceType)&&m_bRecordAllowed) // 重新开始全程录像 edit by ly 2017/04/12
  1186. {
  1187. //GetFunction()->SetTimer(TIMERID_UPLOAD,this,UPLOAD_CYCLE);
  1188. if ((dwUserCode == LOG_EVT_STOP_CONTINURECORD || m_bAutoMode == true) && !m_bCamConfig)
  1189. {
  1190. LogEvent(Severity_Middle,LOG_EVT_START_CONTINURECORD,NULL);
  1191. }
  1192. }
  1193. }
  1194. }
  1195. else if (dwUserCode == LOG_EVT_UI_START_ECHO_CONTINURECORD && !m_bCamConfig)
  1196. {
  1197. // 开始回显全程录像画面
  1198. Dbg("start to echo continuous record, get pos = %s",pszMessage);
  1199. int xPos,yPos,width,height;
  1200. sscanf(pszMessage, "%d@%d@%d@%d", &xPos, &yPos, &width, &height);
  1201. Dbg("x=%d,y=%d,width=%d,height=%d",xPos,yPos,width,height);
  1202. int ScreenWidth = GetSystemMetrics(SM_CXSCREEN); //主屏幕宽度
  1203. int ScreenHeight = GetSystemMetrics(SM_CYSCREEN); //主屏幕高度
  1204. if (xPos<0||yPos<0||width<0||height<0||
  1205. xPos+width>ScreenWidth||yPos+height>ScreenHeight) //
  1206. {
  1207. // 弹出模态窗口提示回显参数不正确
  1208. MessageBoxA(NULL,(LPCSTR)("窗口参数不正确,无法回显录像!"),NULL,MB_SYSTEMMODAL);
  1209. return;
  1210. }
  1211. StartEchoRecord(xPos, yPos, width, height);
  1212. }
  1213. else if (dwUserCode == LOG_EVT_UI_STOP_ECHO_CONTINURECORD)
  1214. {
  1215. // 停止回显全程录像画面
  1216. StopEchoRecord();
  1217. }
  1218. else if (dwUserCode == LOG_EVT_CONTINURECORD_FAILED)
  1219. {
  1220. // 通知到业务中台
  1221. ContinuRecordFailed evt;
  1222. evt.failedmsg = CSimpleStringA2W(pszMessage==NULL?"全程双录失败!":pszMessage);
  1223. SpSendBroadcast(GetFunction(), SP_MSG_OF(ContinuRecordFailed), SP_MSG_SIG_OF(ContinuRecordFailed), evt);
  1224. Dbg("[dbg] ContinuRecordFailed broadcast sent!");
  1225. if (m_bEchoRecord&&pSession)
  1226. {
  1227. if (pszMessage==NULL)
  1228. {
  1229. MessageBoxA(NULL,(LPCSTR)("全程双录失败,回显停止!"),NULL,MB_SYSTEMMODAL);
  1230. }
  1231. else
  1232. {
  1233. CSimpleStringA txt = CSimpleStringA::Format("%s回显停止!",pszMessage);
  1234. MessageBoxA(NULL,(LPCSTR)txt,NULL,MB_SYSTEMMODAL);
  1235. }
  1236. }
  1237. // 录像故障,停止回显,结束录像,停止摄像头
  1238. StopEchoRecord();
  1239. StopRecord();
  1240. StopCamera();
  1241. GetFunction()->KillTimer(TIMERID_RECORD);
  1242. std::string strWmvFile;
  1243. BOOL bRet = GetCurrentSectionBySerialID((LPCTSTR)m_uid.ToString(),strWmvFile);
  1244. if (!bRet)
  1245. {
  1246. Dbg("failed to get current record file name!");
  1247. }
  1248. bRet = UpdateVideoRecord(strWmvFile.c_str());
  1249. if (!bRet)
  1250. {
  1251. Dbg("failed to update video record!");
  1252. }
  1253. CSimpleStringA srcfn = CSimpleStringA::Format("%s%s",
  1254. (LPCTSTR)m_TempDir,strWmvFile.c_str());
  1255. CSimpleStringA dstfn = CSimpleStringA::Format("%s%s",
  1256. (LPCTSTR)m_RecordSaveDir,strWmvFile.c_str());
  1257. bRet = MoveFile((LPCSTR)srcfn, (LPCSTR)dstfn);
  1258. if(!bRet) {
  1259. Dbg("Error Code %lu while move %s ", GetLastError(), (LPCSTR)srcfn);
  1260. }
  1261. //GetFunction()->KillTimer(TIMERID_UPLOAD);
  1262. CSimpleStringA cfn = CSimpleStringA::Format("%sG_%s.txt",(LPCTSTR)m_TempDir,(LPCTSTR)m_uid.ToString());
  1263. UploadVideoRecord((LPCTSTR)cfn);
  1264. m_nCameraErrorCode = Error_Succeed;
  1265. if (m_bAutoMode && !pszMessage && !m_bCamConfig) // edit by ly at 2017/05/25
  1266. {
  1267. LogEvent(Severity_High,LOG_EVT_RESTART_EWSCAMERA,NULL);
  1268. }
  1269. }
  1270. else if (dwUserCode == EVENT_USB_CAMER_INPUT)
  1271. {
  1272. Dbg("[dbg] usbcam %s inserted.", pszMessage);
  1273. ::Sleep(2000); // 延迟2s后判断
  1274. //ews配置名自纠正
  1275. AutoCorrectCameraConfigName(4000);
  1276. if ((ePadtype==m_eDeviceType||eDesk2SType==m_eDeviceType)&&m_bRecordAllowed) // edit by ly 2017/04/12
  1277. {
  1278. if (!stricmp((LPCTSTR)conf.strVideo,pszMessage))
  1279. {
  1280. m_bCameraInserted = TRUE;
  1281. // 通报广角摄像头已经插入
  1282. WideAngleCameraInserted evt;
  1283. evt.camname = CSimpleStringA2W(m_CamFriendlyName.GetLength()>1?m_CamFriendlyName:"全程双录");
  1284. SpSendBroadcast(GetFunction(), SP_MSG_OF(WideAngleCameraInserted), SP_MSG_SIG_OF(WideAngleCameraInserted), evt);
  1285. Dbg("[dbg] WideAngleCameraInserted broadcast sent!");
  1286. //GetFunction()->SetTimer(TIMERID_UPLOAD,this,UPLOAD_CYCLE);
  1287. if (m_bAutoMode && !m_bCamConfig)
  1288. {
  1289. LogEvent(Severity_Middle,LOG_EVT_START_CONTINURECORD,NULL);
  1290. }
  1291. }
  1292. }
  1293. }
  1294. else if (dwUserCode == EVENT_USB_CAMER_OUTPUT)
  1295. {
  1296. Dbg("[dbg] usbcam %s removed.", pszMessage);
  1297. //::Sleep(1000); // 延迟1s后判断
  1298. if (!stricmp((LPCTSTR)conf.strVideo,pszMessage))
  1299. {
  1300. // 及时通知正在用广角摄像头双录的业务 add by ly@2019/04/25
  1301. if (m_bExternStartCam)
  1302. {
  1303. SetCameraSysVar(CAMERA_EWS_ERROR);
  1304. }
  1305. if ((ePadtype==m_eDeviceType||eDesk2SType==m_eDeviceType)&&m_bRecordAllowed)
  1306. {
  1307. m_bCameraInserted = FALSE;
  1308. // 通报故障(正在使用时拔出广角摄像头)
  1309. WideAngleCameraRemoved evt;
  1310. evt.camname = CSimpleStringA2W(m_CamFriendlyName.GetLength()>1?m_CamFriendlyName:"全程双录");
  1311. SpSendBroadcast(GetFunction(), SP_MSG_OF(WideAngleCameraRemoved), SP_MSG_SIG_OF(WideAngleCameraRemoved), evt);
  1312. Dbg("[dbg] WideAngleCameraRemoved broadcast sent!");
  1313. if (m_bEchoRecord&&pSession)
  1314. {
  1315. MessageBoxA(NULL,(LPCSTR)("广角摄像头已移除,回显停止!"),NULL,MB_SYSTEMMODAL);
  1316. }
  1317. StopEchoRecord(); // 停止回显
  1318. StopRecord(); // 结束录像
  1319. StopCamera(); // 停止摄像头
  1320. m_nCameraErrorCode = Error_Succeed;
  1321. GetFunction()->KillTimer(TIMERID_RECORD);
  1322. std::string strWmvFile;
  1323. BOOL bRet = GetCurrentSectionBySerialID((LPCTSTR)m_uid.ToString(),strWmvFile);
  1324. if (!bRet)
  1325. {
  1326. Dbg("failed to get current record file name!");
  1327. }
  1328. bRet = UpdateVideoRecord(strWmvFile.c_str());
  1329. if (!bRet)
  1330. {
  1331. Dbg("failed to update video record!");
  1332. }
  1333. CSimpleStringA srcfn = CSimpleStringA::Format("%s%s",
  1334. (LPCTSTR)m_TempDir,strWmvFile.c_str());
  1335. CSimpleStringA dstfn = CSimpleStringA::Format("%s%s",
  1336. (LPCTSTR)m_RecordSaveDir,strWmvFile.c_str());
  1337. bRet = MoveFile((LPCSTR)srcfn, (LPCSTR)dstfn);
  1338. if(!bRet) {
  1339. Dbg("Error Code %lu while move %s ", GetLastError(), (LPCSTR)srcfn);
  1340. }
  1341. //GetFunction()->KillTimer(TIMERID_UPLOAD);
  1342. CSimpleStringA cfn = CSimpleStringA::Format("%sG_%s.txt",(LPCTSTR)m_TempDir,(LPCTSTR)m_uid.ToString());
  1343. UploadVideoRecord((LPCTSTR)cfn);
  1344. }
  1345. }
  1346. }
  1347. else if (dwUserCode == LOG_EVT_RESTART_EWSCAMERA && !m_bCamConfig)
  1348. {
  1349. //if (!CheckDiskStatus("C:\\",m_max_disk_percent))
  1350. //{
  1351. // if (!m_last_disk_overtop) // edit by ly at 2017/05/17
  1352. // {
  1353. // m_last_disk_overtop = TRUE;
  1354. // OnWideAngleCameraStartFailed("磁盘所剩空间不足,无法开始全程双录!");
  1355. // }
  1356. // Sleep(5000);
  1357. // LogEvent(Severity_High,LOG_EVT_RESTART_EWSCAMERA,NULL);
  1358. // return;
  1359. //}
  1360. //else
  1361. //{
  1362. // m_last_disk_overtop = FALSE;
  1363. //}
  1364. // 尝试重启广角摄像头(至多3次,达到3次即重启实体)
  1365. int tryFailedTimes=0;
  1366. do
  1367. {
  1368. StartCamera();
  1369. if (m_bStartCamera)
  1370. {
  1371. // 开始全程录像
  1372. Dbg("start record!");
  1373. m_uid = CUUID::Create(m_uid);
  1374. m_RecordBeginTime = m_uid.GetTime().ToTimeString();
  1375. Dbg("RecordBeginTime:%s", (LPCTSTR)m_RecordBeginTime);
  1376. StopRecord(); // 如果正在录像,则先停止
  1377. if (pszMessage&&strlen(pszMessage)>0)
  1378. {
  1379. StartRecord(pszMessage);
  1380. }
  1381. else
  1382. {
  1383. CSimpleStringA videoname = CSimpleStringA::Format("G_%s",(LPCTSTR)m_uid.ToString());
  1384. StartRecord((LPCTSTR)videoname);
  1385. }
  1386. // 开始录像计时和数据记录
  1387. RecordInit();
  1388. GetFunction()->SetTimer(TIMERID_RECORD, this, RECORD_CYCLE);
  1389. break;
  1390. }
  1391. else
  1392. {
  1393. tryFailedTimes++;
  1394. }
  1395. } while (tryFailedTimes<3);
  1396. if (tryFailedTimes>=3)
  1397. {
  1398. Dbg("restart camera failed for 3 times, restart entity now.");
  1399. RealSelfCheck();
  1400. }
  1401. }
  1402. else if (dwUserCode == LOG_EVT_CONTINURECORD_SECTION_FINISHED)
  1403. {
  1404. CSimpleStringA filename = pszMessage;
  1405. int pos = filename.IndexOf("G");
  1406. filename = filename.SubString(pos, filename.GetLength() - pos);
  1407. BOOL bRet = UpdateVideoRecord((LPCTSTR)filename);
  1408. if (!bRet)
  1409. {
  1410. Dbg("failed to update video record!");
  1411. GetFunction()->SetTimer(TIMERID_RECORD, this, RECORD_CYCLE);
  1412. return;
  1413. }
  1414. CSimpleStringA dstfn = CSimpleStringA::Format("%s%s",
  1415. (LPCTSTR)m_RecordSaveDir,(LPCTSTR)filename);
  1416. bRet = MoveFile(pszMessage, (LPCSTR)dstfn);
  1417. if(!bRet) {
  1418. Dbg("Error Code %lu while move %s ", GetLastError(), pszMessage);
  1419. }
  1420. GetFunction()->SetTimer(TIMERID_RECORD, this, RECORD_CYCLE);
  1421. }
  1422. else if (dwUserCode == LOG_EVT_BEGIN_CAMERA_CONFIG)
  1423. {
  1424. ExternBeginControl();
  1425. }
  1426. else if (dwUserCode == LOG_EVT_END_CAMERA_CONFIG)
  1427. {
  1428. ExternEndControl();
  1429. }
  1430. else if(LOG_EVT_CONTINURECORD_ENTITY_EXCEPTION == dwUserCode)
  1431. {
  1432. HandleContinurecordEntityException(pszMessage);
  1433. }
  1434. }
  1435. void CSurveillanceRecorderEntity::OnSelfTest( EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext )
  1436. {
  1437. if (Test_ShakeHand == eTestType)
  1438. {
  1439. if ((ePadtype==m_eDeviceType||eDesk2SType==m_eDeviceType)&&m_bRecordAllowed) {
  1440. if (conf.strVideo.GetLength()>1&&conf.strAudioIn.GetLength()>1&&m_bCameraInserted) {
  1441. if (m_nCameraErrorCode != Error_Succeed) {
  1442. Dbg("[dbg][OnSelfTest] m_nCameraErrorCode:0x%x", m_nCameraErrorCode);
  1443. pTransactionContext->SendAnswer(Error_Unexpect);
  1444. m_nCameraErrorCode = Error_Succeed;
  1445. }
  1446. else {
  1447. pTransactionContext->SendAnswer(Error_Succeed);
  1448. }
  1449. }
  1450. else {
  1451. pTransactionContext->SendAnswer(Error_Succeed);
  1452. }
  1453. if (m_bNeedRestart){
  1454. Dbg("[OnSelfTest] need restart entity now.");
  1455. pTransactionContext->SendAnswer(Error_Unexpect);
  1456. }
  1457. }
  1458. else {
  1459. pTransactionContext->SendAnswer(Error_Succeed);
  1460. }
  1461. }
  1462. }
  1463. ErrorCodeEnum CSurveillanceRecorderEntity::StartEwsCamera()
  1464. {
  1465. if (m_ews_capture == NULL)
  1466. {
  1467. //ews配置名自纠正
  1468. AutoCorrectCameraConfigName(4000);
  1469. ErrorCodeEnum Error = Error_Succeed;
  1470. conf.ref_ews_capture_count = &m_lCaptureEwsCount;
  1471. Error = (ErrorCodeEnum)ews_capture_create(&conf, &m_ews_capture);
  1472. if (Error == Error_Succeed)
  1473. {
  1474. Error = ews_capture_start(m_ews_capture);
  1475. Dbg("Error code=0x%x",Error);
  1476. if (Error != Error_Succeed)
  1477. {
  1478. if (Error == Error_EwsCamera)
  1479. {
  1480. ews_capture_destroy(m_ews_capture);
  1481. m_ews_capture = NULL;
  1482. return Error;
  1483. }
  1484. else // Error_AudioIN
  1485. {
  1486. return Error;
  1487. }
  1488. }
  1489. else
  1490. {
  1491. SetAutoBright(Error);
  1492. GetFunction()->SetTimer(TIMERID_CAMCHK, this, CAMCHK_CYCLE);
  1493. return Error_Succeed;
  1494. }
  1495. }
  1496. else
  1497. {
  1498. Dbg("create ews_capture failed! %d", Error);
  1499. m_ews_capture = NULL;
  1500. return Error_Unexpect;
  1501. }
  1502. return Error;
  1503. }
  1504. else
  1505. {
  1506. Dbg("m_ews_capture == NULL");
  1507. return Error_Unexpect;
  1508. }
  1509. }
  1510. void CSurveillanceRecorderEntity::StartCamera()
  1511. {
  1512. // 通报正在启动广角摄像头
  1513. SetCameraSysVar(CAMERA_START_ING);
  1514. WideAngleCameraStateChanged evt;
  1515. evt.statecod = State_EwsCameraStarting;
  1516. evt.statemsg = CSimpleStringA2W("广角摄像头启动中,请稍等……");
  1517. evt.camname = CSimpleStringA2W(m_CamFriendlyName.GetLength()>1?m_CamFriendlyName:"广角");
  1518. SpSendBroadcast(GetFunction(), SP_MSG_OF(WideAngleCameraStateChanged), SP_MSG_SIG_OF(WideAngleCameraStateChanged), evt);
  1519. Dbg("[dbg] WideAngleCameraStateChanged broadcast sent!");
  1520. // 启动广角摄像头
  1521. ErrorCodeEnum Error = StartEwsCamera();
  1522. if (Error!=Error_Succeed)
  1523. {
  1524. Dbg("[dbg] Start Ews Camera Error=0x%x", Error);
  1525. if(Error == Error_EwsCamera)
  1526. {
  1527. m_bStartCamera = FALSE;
  1528. //SetCameraSysVar(CAMERA_EWS_ERROR);
  1529. Dbg("Camera State to Error_EwsCamera");
  1530. m_nCameraErrorCode = Error_EwsCamera;
  1531. GetFunction()->SetUserDefineState(USER_SURVEILLANCE_EWSERROR);
  1532. }
  1533. else
  1534. {
  1535. //SetCameraSysVar(Error);
  1536. Dbg("Camera State to Other Error");
  1537. m_nCameraErrorCode = Error;
  1538. m_bStartCamera = FALSE;
  1539. }
  1540. OnWideAngleCameraStartFailed("广角摄像头启动失败!");
  1541. if (!m_bAutoMode)
  1542. {
  1543. RealSelfCheck();
  1544. }
  1545. }
  1546. else
  1547. {
  1548. if (m_bRecordAllowed&&!m_pRecorder)
  1549. {
  1550. BOOL bRet = FALSE;
  1551. m_pRecorder = new Clibwmvrecord(&bRet, this, REC_COMMON_AUDIO_EWS_SHM_QUEUE, REC_COMMON_VIDEO_EWS_SHM_RTP_QUEUE, NULL);
  1552. }
  1553. // 通报广角摄像头已经成功启动
  1554. m_bStartCamera = TRUE;
  1555. Dbg("Camera State to CAMERA_NO_ERROR");
  1556. m_nCameraErrorCode = Error_Succeed;
  1557. GetFunction()->SetUserDefineState(USER_SURVEILLANCE_IDLE);
  1558. WideAngleCameraStateChanged evt;
  1559. evt.statecod = State_EwsCameraStartSucceeded;
  1560. evt.statemsg = CSimpleStringA2W("已启动广角摄像头!");
  1561. evt.camname = CSimpleStringA2W(m_CamFriendlyName.GetLength()>1?m_CamFriendlyName:"广角");
  1562. SpSendBroadcast(GetFunction(), SP_MSG_OF(WideAngleCameraStateChanged), SP_MSG_SIG_OF(WideAngleCameraStateChanged), evt);
  1563. Dbg("[dbg] WideAngleCameraStateChanged broadcast sent!");
  1564. }
  1565. SetCameraSysVar(CAMERA_NO_ERROR);
  1566. }
  1567. void CSurveillanceRecorderEntity::StopEwsCamera()
  1568. {
  1569. // edit by ly@2019/04/18
  1570. if ((ePadtype==m_eDeviceType||eMobilePadType==m_eDeviceType||eDesk2SType==m_eDeviceType)&&m_bStartCamera)
  1571. {
  1572. Dbg("Stop EwsCamera.");
  1573. if (m_ews_capture)
  1574. {
  1575. GetFunction()->KillTimer(TIMERID_CAMCHK);
  1576. ews_capture_stop(m_ews_capture);
  1577. ews_capture_destroy(m_ews_capture);
  1578. m_ews_capture = NULL;
  1579. }
  1580. SetCameraSysVar(CAMERA_NO_ERROR);
  1581. m_bStartCamera = FALSE;
  1582. }
  1583. }
  1584. void CSurveillanceRecorderEntity::StopCamera()
  1585. {
  1586. // edit by ly@20180725
  1587. if (!m_bExternStartCam)
  1588. {
  1589. StopEwsCamera();
  1590. }
  1591. }
  1592. ErrorCodeEnum CSurveillanceRecorderEntity::ReStartEwsCamera()
  1593. {
  1594. if (m_ews_capture != NULL)
  1595. {
  1596. if((ews_stop_camera(m_ews_capture)==-1))
  1597. {
  1598. Dbg("ReStartCamera,stop camera fail!");
  1599. }
  1600. Sleep(2000);
  1601. //ews配置名自纠正
  1602. AutoCorrectCameraConfigName(4000);
  1603. ErrorCodeEnum Error = Error_Succeed;
  1604. Error = ews_capture_create(&conf, m_ews_capture);
  1605. if (Error == Error_Succeed)
  1606. {
  1607. Error = ews_capture_start(m_ews_capture);
  1608. if (Error != Error_Succeed)
  1609. {
  1610. Dbg("ReStartCamera,capture_start fail!");
  1611. ews_capture_destroy(m_ews_capture);
  1612. return Error;
  1613. }
  1614. else
  1615. {
  1616. Dbg("ReStartCamera success!");
  1617. return Error_Succeed;
  1618. }
  1619. }
  1620. else
  1621. {
  1622. Dbg("ReStartCamera,capture_create fail!");
  1623. ews_capture_destroy(m_ews_capture);
  1624. return Error;
  1625. }
  1626. }
  1627. else
  1628. {
  1629. return Error_Unexpect;
  1630. }
  1631. }
  1632. void CSurveillanceRecorderEntity::StartRecord( const char *wmvfilename)
  1633. {
  1634. Dbg("wmvfilename = %s", wmvfilename);
  1635. int fps = 5;
  1636. SubtitleParam subtitleParam;
  1637. ZeroMemory(&subtitleParam,sizeof(SubtitleParam));
  1638. subtitleParam.bSubtitle = TRUE;
  1639. subtitleParam.bSubtitleSection = FALSE;
  1640. Rvc_RecordAudioParam_t tAudioParams;
  1641. tAudioParams.eRecordType = eSingleSide;
  1642. tAudioParams.eOutPutType = m_eAudioOutQuality;
  1643. tAudioParams.bIsNsOn = false;
  1644. tAudioParams.iNsPolicy = 2;
  1645. tAudioParams.iAudioOutBitRate = 8;
  1646. tAudioParams.bIsTransOn = false;
  1647. if (m_pRecorder->StartWmvRecord(fps, 90, &tAudioParams, &subtitleParam, FALSE, FALSE, (LPCSTR)m_TempDir,
  1648. m_TempDir.GetLength(), wmvfilename, strlen(wmvfilename))) {
  1649. m_bStartRecord = TRUE;
  1650. }
  1651. }
  1652. void CSurveillanceRecorderEntity::StopRecord()
  1653. {
  1654. if (m_bStartRecord)
  1655. {
  1656. m_bStartRecord = FALSE;
  1657. m_pRecorder->StopWmvRecord();
  1658. }
  1659. }
  1660. void CSurveillanceRecorderEntity::StartEchoRecord( int x, int y, int w, int h )
  1661. {
  1662. videoplay_session_conf_t video_conf;
  1663. memset(&video_conf,0,sizeof(video_conf));
  1664. video_conf.local_video_view_x = x;
  1665. video_conf.local_video_view_y = y;
  1666. video_conf.local_video_view_cx = w;
  1667. video_conf.local_video_view_cy = h;
  1668. //video_conf.ref_camera_state = &m_iCameraState;
  1669. video_conf.pHostApi = this;
  1670. strcpy(video_conf.local_video_preview_queue0,REC_COMMON_VIDEO_EWS_SHM_PREVIEW_QUEUE);
  1671. int rc = Local_videoplay_session_create(&video_conf, &pSession);
  1672. if (rc != 0)
  1673. {
  1674. Dbg("create video play session failed! rc = %d", rc);
  1675. }
  1676. else
  1677. {
  1678. rc = Local_videoplay_session_start(pSession);
  1679. if (rc != 0)
  1680. {
  1681. Dbg("start video play session failed! rc = %d", rc);
  1682. Local_videoplay_session_destroy(pSession);
  1683. pSession = NULL;
  1684. }
  1685. else
  1686. {
  1687. m_bEchoRecord = TRUE;
  1688. Dbg("start video play session succeed! rc = %d", rc);
  1689. }
  1690. }
  1691. }
  1692. void CSurveillanceRecorderEntity::StopEchoRecord()
  1693. {
  1694. if (m_bEchoRecord&&pSession)
  1695. {
  1696. m_bEchoRecord = FALSE;
  1697. Local_videoplay_session_stop(pSession);
  1698. Local_videoplay_session_destroy(pSession);
  1699. pSession = NULL;
  1700. Dbg("video play session stopped!");
  1701. }
  1702. }
  1703. ErrorCodeEnum CSurveillanceRecorderEntity::LoadServerConfig()
  1704. {
  1705. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  1706. CSmartPointer<IConfigInfo> spConfig;
  1707. ErrorCodeEnum Error = spFunction->OpenConfig(Config_CenterSetting, spConfig);
  1708. if (Error == Error_Succeed) {
  1709. CSimpleStringA str;
  1710. Error = spConfig->ReadConfigValue("SurveillanceRecorder", "Server", str);
  1711. if (Error == Error_Succeed)
  1712. {
  1713. Error = ParseIPAddress(str, m_server1, m_server1_port);
  1714. }
  1715. if (Error == Error_Succeed)
  1716. {
  1717. ErrorCodeEnum ErrorCode;
  1718. ErrorCode = spConfig->ReadConfigValue("SurveillanceRecorder", "Server_Backup", str);
  1719. if (ErrorCode == Error_Succeed)
  1720. {
  1721. ErrorCode = ParseIPAddress(str, m_server2, m_server2_port);
  1722. }
  1723. if (ErrorCode != Error_Succeed)
  1724. {
  1725. m_server2 = NULL;
  1726. m_server2_port = 0;
  1727. }
  1728. }
  1729. }
  1730. return Error;
  1731. }
  1732. bool CSurveillanceRecorderEntity::SecureClientConnect()
  1733. {
  1734. if (m_pConnection!=NULL && m_pConnection->IsConnectionOK())
  1735. {
  1736. return true;
  1737. }
  1738. SecureClientRelease();
  1739. m_pConnection = new CUploadRecordConnection(this);
  1740. /*if (!m_pConnection->Connect(m_server1,m_server1_port,3))
  1741. {
  1742. SecureClientRelease();
  1743. if ((m_server2!=NULL)&&(m_server2_port!=0)&&
  1744. ((m_server2!=m_server1)||(m_server2_port!=m_server1_port)))
  1745. {
  1746. m_pConnection = new CUploadRecordConnection(this);
  1747. if (!m_pConnection->Connect(m_server2,m_server2_port,3))
  1748. {
  1749. return false;
  1750. }
  1751. }
  1752. }*/
  1753. return m_pConnection->ConnectFromCentralSetting();
  1754. }
  1755. void CSurveillanceRecorderEntity::SecureClientRelease()
  1756. {
  1757. if (m_pConnection != NULL)
  1758. {
  1759. if (m_pConnection->IsConnectionOK())
  1760. {
  1761. m_pConnection->Close();
  1762. }
  1763. m_pConnection->DecRefCount();
  1764. m_pConnection = NULL;
  1765. }
  1766. }
  1767. BOOL CSurveillanceRecorderEntity::RecordInit()
  1768. {
  1769. CSimpleStringA fn = CSimpleStringA::Format("%sG_%s.txt",
  1770. (LPCTSTR)m_TempDir,(LPCTSTR)m_uid.ToString());
  1771. HANDLE hFile = CreateFile((LPCTSTR)fn,GENERIC_WRITE,FILE_SHARE_READ,
  1772. NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  1773. if (hFile != INVALID_HANDLE_VALUE) {
  1774. CloseHandle(hFile);
  1775. hFile = NULL;
  1776. WritePrivateProfileString("VideoRecord","LastLocalTime",
  1777. (LPCTSTR)m_RecordBeginTime,(LPCTSTR)fn);
  1778. WritePrivateProfileString("VideoRecord","LastFileCount","0",(LPCTSTR)fn);
  1779. return TRUE;
  1780. }
  1781. else {
  1782. Dbg("error to create file %s(error code:%d)!",(LPCTSTR)fn,GetLastError());
  1783. return FALSE;
  1784. }
  1785. }
  1786. BOOL CSurveillanceRecorderEntity::GetCurrentSectionBySerialID( const char* videoserialid, std::string &strWmvFile )
  1787. {
  1788. if (videoserialid == NULL)
  1789. {
  1790. return FALSE;
  1791. }
  1792. char wmvfilename[MAX_PATH] = {0};
  1793. sprintf_s(wmvfilename, "G_%s*.wmv", videoserialid);
  1794. std::vector<std::string> sfiles;
  1795. BOOL bRet = FindMatchedFile((LPCSTR)m_TempDir, wmvfilename, sfiles);
  1796. Dbg("bRet = %d while find %s", bRet, wmvfilename);
  1797. if(!bRet)
  1798. {
  1799. strWmvFile = "";
  1800. return FALSE;
  1801. }
  1802. std::vector<std::string>::iterator max_iter = max_element(sfiles.begin(),sfiles.end());
  1803. strWmvFile = max_iter->c_str();
  1804. return TRUE;
  1805. }
  1806. BOOL CSurveillanceRecorderEntity::UpdateVideoRecord(const char *videofilename)
  1807. {
  1808. CSimpleStringA fn;
  1809. if (videofilename == NULL || strlen(videofilename) <= 1)
  1810. {
  1811. Dbg("videofilename is NULL or empty!");
  1812. return FALSE;
  1813. }
  1814. fn = CSimpleStringA::Format("%s%s",(LPCTSTR)m_TempDir,videofilename);
  1815. FILE *fin = fopen((LPCTSTR)fn, "rb");
  1816. if (!fin)
  1817. {
  1818. fclose(fin);
  1819. return FALSE;
  1820. }
  1821. int FileLen = 0;
  1822. fseek(fin, 0, SEEK_END);
  1823. FileLen = ftell(fin);
  1824. fseek(fin, 0, SEEK_SET);
  1825. fclose(fin);
  1826. FileLen = int(1.0*FileLen/1024+0.5);
  1827. char strtmp[20] = {0}, strtmpkey[20] = {0};
  1828. int FileSeqNo = 0, FileCount = 0;
  1829. sscanf(videofilename,"G_%[^_]_%d.wmv",strtmp,&FileSeqNo);
  1830. FileCount = FileSeqNo + 1;
  1831. CSimpleStringA cfn = CSimpleStringA::Format("%sG_%s.txt",
  1832. (LPCTSTR)m_TempDir,strtmp);
  1833. SYSTEMTIME st;
  1834. GetLocalTime(&st);
  1835. CSimpleStringA curtime = CSimpleStringA::Format("%4d-%02d-%02d %02d:%02d:%02d",
  1836. st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute, st.wSecond);
  1837. WritePrivateProfileString("VideoRecord","LastLocalTime",
  1838. (LPCTSTR)curtime,(LPCTSTR)cfn);
  1839. sprintf_s(strtmp, "%d", FileCount);
  1840. WritePrivateProfileString("VideoRecord","LastFileCount",strtmp,(LPCTSTR)cfn);
  1841. sprintf_s(strtmpkey, "%d_FileLength", FileSeqNo);
  1842. sprintf_s(strtmp, "%d", FileLen);
  1843. WritePrivateProfileString("VideoRecord",strtmpkey,strtmp,(LPCTSTR)cfn);
  1844. return TRUE;
  1845. }
  1846. void CSurveillanceRecorderEntity::UploadVideoRecord(const char *videorecordname)
  1847. {
  1848. if (videorecordname == NULL)
  1849. {
  1850. if (SecureClientConnect())
  1851. {
  1852. auto arr = fileutil_get_sub_files((LPCTSTR)m_TempDir);
  1853. if (arr != NULL)
  1854. {
  1855. char szTmp[64];
  1856. std::vector<video_record_t *> videorecordlist;
  1857. for (int i = 0; i < arr->nelts; ++i)
  1858. {
  1859. char *pszFile = ARRAY_IDX(arr, i, char*);
  1860. if (strstr(pszFile,".txt") && strchr(pszFile,'G')
  1861. && !strstr(pszFile,(LPCTSTR)m_uid.ToString()))
  1862. {
  1863. if (videorecordlist.size() >= MAX_LISTSIZE)
  1864. {
  1865. break;
  1866. }
  1867. video_record_t* r = ZALLOC_T(video_record_t);
  1868. r->TerminalNo = _strdup((LPCTSTR)m_terminalNo);
  1869. CSimpleStringA filename = pszFile;
  1870. int pos = filename.IndexOf("G");
  1871. filename = filename.SubString(pos, filename.GetLength() - pos);
  1872. sscanf((LPCTSTR)filename, "G_%[^.].txt", szTmp);
  1873. r->VideoSerialID = _strdup(szTmp);
  1874. /*std::string strWmvFile;
  1875. BOOL bRet = GetCurrentSectionBySerialID(szTmp,strWmvFile);
  1876. if (bRet)
  1877. {
  1878. bRet = UpdateVideoRecord(strWmvFile.c_str());
  1879. if (!bRet)
  1880. {
  1881. Dbg("failed to update video record!");
  1882. }
  1883. CSimpleStringA srcfn = CSimpleStringA::Format("%s%s",
  1884. (LPCTSTR)m_TempDir,strWmvFile.c_str());
  1885. CSimpleStringA dstfn = CSimpleStringA::Format("%s%s",
  1886. (LPCTSTR)m_RecordSaveDir,strWmvFile.c_str());
  1887. bRet = MoveFile((LPCSTR)srcfn, (LPCSTR)dstfn);
  1888. if(!bRet) {
  1889. Dbg("Error Code %lu while move %s ", GetLastError(), (LPCSTR)srcfn);
  1890. }
  1891. }*/
  1892. GetPrivateProfileString("VideoRecord", "LastLocalTime", "", szTmp, 64, pszFile);
  1893. r->EndTime = _strdup(szTmp);
  1894. int nFileCount = GetPrivateProfileInt("VideoRecord", "LastFileCount", 0, pszFile);
  1895. int nFileLen = 0;
  1896. for (int j = 0; j < nFileCount; ++j)
  1897. {
  1898. sprintf_s(szTmp, "%d_FileLength", j);
  1899. nFileLen += GetPrivateProfileInt("VideoRecord", szTmp, 0, pszFile);
  1900. }
  1901. r->FileLength = nFileLen;
  1902. videorecordlist.push_back(r);
  1903. }
  1904. else if (strstr(pszFile,".wmv") && strchr(pszFile,'G')
  1905. && !strstr(pszFile,(LPCTSTR)m_uid.ToString()))
  1906. {
  1907. CSimpleStringA strWmvFile = pszFile;
  1908. int pos = strWmvFile.IndexOf("G");
  1909. strWmvFile = strWmvFile.SubString(pos, strWmvFile.GetLength() - pos);
  1910. CSimpleStringA srcfn = CSimpleStringA::Format("%s%s",
  1911. (LPCTSTR)m_TempDir,(LPCTSTR)strWmvFile);
  1912. CSimpleStringA dstfn = CSimpleStringA::Format("%s%s",
  1913. (LPCTSTR)m_RecordSaveDir,(LPCTSTR)strWmvFile);
  1914. BOOL bRet = MoveFile((LPCSTR)srcfn, (LPCSTR)dstfn);
  1915. if(!bRet) {
  1916. Dbg("Error Code %lu while move %s ", GetLastError(), (LPCSTR)srcfn);
  1917. }
  1918. }
  1919. }
  1920. Dbg("%d records added to upload list.", videorecordlist.size());
  1921. toolkit_array_free2(arr);
  1922. if (m_pConnection != NULL && m_pConnection->IsConnectionOK())
  1923. {
  1924. if (videorecordlist.size() > 0)
  1925. {
  1926. m_pConnection->SendVideoRecords(videorecordlist);
  1927. }
  1928. }
  1929. for (std::vector<video_record_t *>::size_type i = 0; i < videorecordlist.size(); ++i)
  1930. {
  1931. video_record_destroy(videorecordlist[i]);
  1932. }
  1933. }
  1934. //SecureClientRelease();
  1935. }
  1936. }
  1937. else
  1938. {
  1939. if (SecureClientConnect())
  1940. {
  1941. if (m_pConnection != NULL && m_pConnection->IsConnectionOK())
  1942. {
  1943. char szTmp[64] = {0};
  1944. video_record_t* r = ZALLOC_T(video_record_t);
  1945. r->TerminalNo = _strdup((LPCTSTR)m_terminalNo);
  1946. CSimpleStringA filename = videorecordname;
  1947. int pos = filename.IndexOf("G");
  1948. filename = filename.SubString(pos, filename.GetLength() - pos);
  1949. sscanf((LPCTSTR)filename, "G_%[^.].txt", szTmp);
  1950. r->VideoSerialID = _strdup(szTmp);
  1951. GetPrivateProfileString("VideoRecord", "LastLocalTime", "", szTmp, 64, videorecordname);
  1952. r->EndTime = _strdup(szTmp);
  1953. int nFileCount = GetPrivateProfileInt("VideoRecord", "LastFileCount", 0, videorecordname);
  1954. int nFileLen = 0;
  1955. Dbg("nFileCount=%d",nFileCount);
  1956. for (int j = 0; j < nFileCount; ++j)
  1957. {
  1958. sprintf_s(szTmp, "%d_FileLength", j);
  1959. nFileLen += GetPrivateProfileInt("VideoRecord", szTmp, 0, videorecordname);
  1960. }
  1961. Dbg("nFileLen=%d",nFileLen);
  1962. r->FileLength = nFileLen;
  1963. m_pConnection->SendVideoRecord(r);
  1964. video_record_destroy(r);
  1965. }
  1966. //SecureClientRelease();
  1967. }
  1968. }
  1969. }
  1970. void CSurveillanceRecorderEntity::OnEndUploadRecord( const char *videoserialid, int result )
  1971. {
  1972. Dbg("%s upload result: %d", videoserialid, result);
  1973. if (0 == result)
  1974. {
  1975. Dbg("succeed to save video record: G_%s", videoserialid);
  1976. CSimpleStringA cfn = CSimpleStringA::Format("%sG_%s.txt", (LPCTSTR)m_TempDir, videoserialid);
  1977. DeleteFile(cfn);
  1978. }
  1979. }
  1980. ErrorCodeEnum CSurveillanceRecorderEntity::ExternBeginControl()
  1981. {
  1982. Dbg("Extern begin control camera");
  1983. m_bCamConfig = TRUE;
  1984. if (m_ews_capture)
  1985. {
  1986. if (m_bStartRecord) m_bRecordPaused = TRUE;
  1987. LogEvent(Severity_Middle,LOG_EVT_STOP_CONTINURECORD,NULL);
  1988. GetFunction()->KillTimer(TIMERID_UPLOAD);
  1989. }
  1990. return Error_Succeed;
  1991. }
  1992. ErrorCodeEnum CSurveillanceRecorderEntity::ExternEndControl()
  1993. {
  1994. Dbg("Extern end control camera");
  1995. m_bCamConfig = FALSE;
  1996. if (m_bRecordPaused)
  1997. {
  1998. GetFunction()->SetTimer(TIMERID_UPLOAD,this,UPLOAD_CYCLE);
  1999. LogEvent(Severity_Middle,LOG_EVT_START_CONTINURECORD,NULL);
  2000. m_bRecordPaused = FALSE;
  2001. }
  2002. return Error_Succeed;
  2003. }
  2004. ErrorCodeEnum CSurveillanceRecorderEntity::HandleContinurecordEntityException(const char* pszMessage)
  2005. {
  2006. // 通知到业务中台
  2007. ContinuRecordFailed evt;
  2008. evt.failedmsg = CSimpleStringA2W(pszMessage==NULL?"全程双录异常!":pszMessage);
  2009. SpSendBroadcast(GetFunction(), SP_MSG_OF(ContinuRecordFailed), SP_MSG_SIG_OF(ContinuRecordFailed), evt);
  2010. Dbg("[dbg] ContinuRecord entity exception broadcast sent!");
  2011. if (m_bEchoRecord&&pSession)
  2012. {
  2013. if (pszMessage==NULL)
  2014. {
  2015. MessageBoxA(NULL,(LPCSTR)("全程双录异常,回显停止!"),NULL,MB_SYSTEMMODAL);
  2016. }
  2017. else
  2018. {
  2019. CSimpleStringA txt = CSimpleStringA::Format("%s回显停止!",pszMessage);
  2020. MessageBoxA(NULL,(LPCSTR)txt,NULL,MB_SYSTEMMODAL);
  2021. }
  2022. // 录像异常,停止回显
  2023. StopEchoRecord();
  2024. }
  2025. //StopRecord();
  2026. StopCamera();
  2027. GetFunction()->KillTimer(TIMERID_RECORD);
  2028. CUUID last_uid = m_uid;
  2029. std::string strWmvFile;
  2030. BOOL bRet = GetCurrentSectionBySerialID((LPCTSTR)last_uid.ToString(),strWmvFile);
  2031. if (!bRet)
  2032. {
  2033. Dbg("failed to get current record file name!");
  2034. }
  2035. bRet = UpdateVideoRecord(strWmvFile.c_str());
  2036. if (!bRet)
  2037. {
  2038. Dbg("failed to update video record!");
  2039. }
  2040. CSimpleStringA srcfn = CSimpleStringA::Format("%s%s",
  2041. (LPCTSTR)m_TempDir,strWmvFile.c_str());
  2042. CSimpleStringA dstfn = CSimpleStringA::Format("%s%s",
  2043. (LPCTSTR)m_RecordSaveDir,strWmvFile.c_str());
  2044. bRet = MoveFile((LPCSTR)srcfn, (LPCSTR)dstfn);
  2045. if(!bRet) {
  2046. Dbg("Error Code %lu while move %s ", GetLastError(), (LPCSTR)srcfn);
  2047. }
  2048. else{
  2049. Dbg("Move File from %s to %s success. ", (LPCSTR)srcfn, (LPCSTR)dstfn);
  2050. }
  2051. //GetFunction()->KillTimer(TIMERID_UPLOAD);
  2052. CSimpleStringA cfn = CSimpleStringA::Format("%sG_%s.txt",(LPCTSTR)m_TempDir,(LPCTSTR)last_uid.ToString());
  2053. UploadVideoRecord((LPCTSTR)cfn);
  2054. m_bNeedRestart = true;
  2055. //重启实体
  2056. RealSelfCheck();
  2057. //重新开始全程双录
  2058. //Dbg("Restart Continue Record.");
  2059. //RestartContinueRecord();
  2060. return Error_Succeed;
  2061. }
  2062. ErrorCodeEnum CSurveillanceRecorderEntity::RestartContinueRecord()
  2063. {
  2064. ErrorCodeEnum Error = Error_Succeed;
  2065. Dbg("Start Camera!");
  2066. StartCamera();
  2067. // 重新开始全程录像
  2068. Dbg("restart surveillance record!");
  2069. m_uid = CUUID::Create(m_uid);
  2070. m_RecordBeginTime = m_uid.GetTime().ToTimeString();
  2071. Dbg("Restart Continue Record Name is %s, RecordBeginTime:%s", (LPCTSTR)m_uid.ToString(),(LPCTSTR)m_RecordBeginTime);
  2072. //StopRecord(); // 如果正在录像,则先停止
  2073. CSimpleStringA videoname = CSimpleStringA::Format("G_%s",(LPCTSTR)m_uid.ToString());
  2074. StartRecord((LPCTSTR)videoname);
  2075. // 开始录像计时和数据记录
  2076. RecordInit();
  2077. GetFunction()->SetTimer(TIMERID_RECORD, this, RECORD_CYCLE);
  2078. return Error;
  2079. }
  2080. ErrorCodeEnum CSurveillanceRecorderEntity::SetAudioQuality(int iAudioQuality)
  2081. {
  2082. ErrorCodeEnum eRet = Error_Succeed;
  2083. Dbg("iAudioQuality == %d.", iAudioQuality);
  2084. switch (iAudioQuality)
  2085. {
  2086. case 1:
  2087. m_eAudioOutQuality = eLowDefinition;
  2088. break;
  2089. case 2:
  2090. m_eAudioOutQuality = eStandardDefinition;
  2091. break;
  2092. case 3:
  2093. m_eAudioOutQuality = eHighDefinition;
  2094. break;
  2095. case 4:
  2096. m_eAudioOutQuality = eUltraHD;
  2097. break;
  2098. default:
  2099. m_eAudioOutQuality = eUltraHD;
  2100. break;
  2101. }
  2102. return eRet;
  2103. }
  2104. ErrorCodeEnum CSurveillanceRecorderEntity::SetAudioNsPolicy(int iNsPolicy)
  2105. {
  2106. ErrorCodeEnum eRet = Error_Succeed;
  2107. Dbg("iNsPolicy == %d.", iNsPolicy);
  2108. if (1 == iNsPolicy || 3 == iNsPolicy){
  2109. m_iAudioNsPolicy = iNsPolicy;
  2110. }
  2111. else{
  2112. m_iAudioNsPolicy = 2;
  2113. }
  2114. return eRet;
  2115. }
  2116. SelfChekerClient::SelfChekerClient( CSurveillanceRecorderEntity *pEntity ) : SelfCheckerService_ClientBase(pEntity)
  2117. {
  2118. }
  2119. void SurveillanceRecordServiceSession::Handle_GetContinuRecordInfo( SpReqAnsContext<SurveillanceRecorderService_GetContinuRecordInfo_Req, SurveillanceRecorderService_GetContinuRecordInfo_Ans>::Pointer ctx )
  2120. {
  2121. if (!(ePadtype==m_pEntity->m_eDeviceType||eDesk2SType==m_pEntity->m_eDeviceType)||!m_pEntity->m_bRecordAllowed)
  2122. {
  2123. ctx->Ans.Remark = CSimpleStringA2W("本设备不支持全程双录!");
  2124. ctx->Answer(Error_Succeed);
  2125. return;
  2126. }
  2127. if (m_pEntity->m_bStartRecord)
  2128. {
  2129. ctx->Ans.TerminalNo = CSimpleStringA2W(m_pEntity->m_terminalNo);
  2130. ctx->Ans.RecordStartTime = CSimpleStringA2W(m_pEntity->m_RecordBeginTime);
  2131. ctx->Ans.VideoSerialID = CSimpleStringA2W(m_pEntity->m_uid.ToString()); // 录像流水号
  2132. if (!m_pEntity->m_bAutoMode)
  2133. {
  2134. ctx->Ans.Remark = CSimpleStringA2W("");
  2135. }
  2136. else
  2137. {
  2138. ctx->Ans.Remark = CSimpleStringA2W("AutoRecord");
  2139. }
  2140. }
  2141. else
  2142. {
  2143. if (!m_pEntity->m_bAutoMode)
  2144. {
  2145. ctx->Ans.Remark = CSimpleStringA2W("录像未开始!");
  2146. }
  2147. else
  2148. {
  2149. ctx->Ans.Remark = CSimpleStringA2W("AutoRecord:录像未开始!");
  2150. }
  2151. }
  2152. ctx->Answer(Error_Succeed);
  2153. }
  2154. void SurveillanceRecordServiceSession::Handle_GetContinuRecordState( SpReqAnsContext<SurveillanceRecorderService_GetContinuRecordState_Req, SurveillanceRecorderService_GetContinuRecordState_Ans>::Pointer ctx )
  2155. {
  2156. ctx->Ans.StateCode = m_pEntity->GetContinuRecordState();
  2157. ctx->Answer(Error_Succeed);
  2158. }
  2159. void SurveillanceRecordServiceSession::Handle_GetContinuRecordMode( SpReqAnsContext<SurveillanceRecorderService_GetContinuRecordMode_Req, SurveillanceRecorderService_GetContinuRecordMode_Ans>::Pointer ctx )
  2160. {
  2161. ctx->Ans.IsAuto = m_pEntity->GetContinuRecordMode();
  2162. ctx->Answer(Error_Succeed);
  2163. }
  2164. void SurveillanceRecordServiceSession::Handle_StartEwsCamera( SpReqAnsContext<SurveillanceRecorderService_StartEwsCamera_Req, SurveillanceRecorderService_StartEwsCamera_Ans>::Pointer ctx )
  2165. {
  2166. ErrorCodeEnum error = m_pEntity->ExternStartCamera();
  2167. Dbg("ExternStartCamera:0x%x", error);
  2168. int nRetCod = 7;
  2169. CSimpleStringA strErrMsg;
  2170. switch(error)
  2171. {
  2172. case Error_Succeed:
  2173. nRetCod = 0;
  2174. strErrMsg = "启动成功";
  2175. break;
  2176. case Error_Cancel:
  2177. nRetCod = 1;
  2178. strErrMsg = "设备类型非低柜双屏或PAD";
  2179. break;
  2180. case Error_Break:
  2181. nRetCod = 2;
  2182. strErrMsg = "当前处于摄像头配置模式";
  2183. break;
  2184. case Error_EwsCamera:
  2185. nRetCod = 3;
  2186. strErrMsg = "广角摄像头启动失败";
  2187. break;
  2188. case Error_InvalidState:
  2189. nRetCod = 4;
  2190. strErrMsg = "广角摄像头正在启动";
  2191. break;
  2192. case Error_Resource:
  2193. nRetCod = 5;
  2194. strErrMsg = "广角摄像头未配置";
  2195. break;
  2196. case Error_DevNotAvailable:
  2197. nRetCod = 6;
  2198. strErrMsg = "广角摄像头未插入";
  2199. break;
  2200. default:
  2201. nRetCod = 7;
  2202. strErrMsg = "发生其他错误";
  2203. break;
  2204. }
  2205. ctx->Ans.StartErrorCode = nRetCod;
  2206. ctx->Ans.StartErrorMsg = CSimpleStringA2W(strErrMsg);
  2207. ctx->Answer(Error_Succeed);
  2208. }
  2209. void SurveillanceRecordServiceSession::Handle_StopEwsCamera( SpReqAnsContext<SurveillanceRecorderService_StopEwsCamera_Req, SurveillanceRecorderService_StopEwsCamera_Ans>::Pointer ctx )
  2210. {
  2211. ErrorCodeEnum error = m_pEntity->ExternStopCamera();
  2212. Dbg("ExternStopCamera:0x%x", error);
  2213. int nRetCod = 2;
  2214. CSimpleStringA strErrMsg;
  2215. if (error == Error_Succeed)
  2216. {
  2217. nRetCod = 0;
  2218. strErrMsg = "停止成功";
  2219. }
  2220. else if (error == Error_Cancel)
  2221. {
  2222. nRetCod = 1;
  2223. strErrMsg = "设备类型非低柜双屏或PAD";
  2224. }
  2225. else
  2226. {
  2227. nRetCod = 2;
  2228. strErrMsg = "当前处于摄像头配置模式";
  2229. }
  2230. ctx->Ans.StopErrorCode = nRetCod;
  2231. ctx->Ans.StopErrorMsg = CSimpleStringA2W(strErrMsg);
  2232. ctx->Answer(Error_Succeed);
  2233. }
  2234. SP_BEGIN_ENTITY_MAP()
  2235. SP_ENTITY(CSurveillanceRecorderEntity)
  2236. SP_END_ENTITY_MAP()