mod_SurveillanceRecorder.cpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456
  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->SafeDelete();
  954. pCameraConfigManageClient = NULL;
  955. }
  956. }
  957. void CSurveillanceRecorderEntity::OnWideAngleCameraStartFailed( const char* pszFailedMsg )
  958. {
  959. if (!pszFailedMsg) {
  960. return;
  961. }
  962. // 通报广角摄像头启动时故障
  963. WideAngleCameraStartFailed evt;
  964. evt.failedmsg = CSimpleStringA2W(pszFailedMsg);
  965. SpSendBroadcast(GetFunction(), SP_MSG_OF(WideAngleCameraStartFailed), SP_MSG_SIG_OF(WideAngleCameraStartFailed), evt);
  966. Dbg("[dbg] WideAngleCameraStartFailed broadcast sent!");
  967. }
  968. void CSurveillanceRecorderEntity::OnSysVarEvent( const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName )
  969. {
  970. LOG_TRACE("entity:%s key:%s value:%s", pszEntityName, pszKey, pszValue);
  971. if (_stricmp(pszKey,SYSVAR_DISKSTATEUS_FOR_VIDEO) == 0)
  972. {
  973. if (pszValue)
  974. {
  975. char state = pszValue[0];
  976. if (state == 'U')
  977. {
  978. Dbg("unknown state, maybe the video path is empty.");
  979. m_last_disk_overtop = FALSE;
  980. }
  981. else if (state == 'S')
  982. {
  983. Dbg("free disk space for video is sufficient for at least 7 days!");
  984. m_last_disk_overtop = FALSE;
  985. }
  986. else if (state == 'W')
  987. {
  988. Dbg("[Warn] free disk space for video is not sufficient for 7 days!");
  989. m_last_disk_overtop = FALSE;
  990. }
  991. else if (state == 'E')
  992. {
  993. Dbg("[Warn] free disk space for video is not sufficient for 3 days!");
  994. m_last_disk_overtop = FALSE;
  995. }
  996. else if (state == 'F')
  997. {
  998. Dbg("[Warn] free disk space for video is not sufficient for 1 day!");
  999. if (m_bStartRecord)
  1000. {
  1001. LogEvent(Severity_High,LOG_EVT_CONTINURECORD_FAILED,"磁盘所剩空间不足,已停止全程双录!");
  1002. }
  1003. m_last_disk_overtop = TRUE;
  1004. }
  1005. else
  1006. {
  1007. m_last_disk_overtop = FALSE;
  1008. }
  1009. if (state != 'F')
  1010. {
  1011. if (m_bAutoMode && !m_bStartRecord && !m_bCamConfig)
  1012. {
  1013. LogEvent(Severity_Middle,LOG_EVT_START_CONTINURECORD,NULL);
  1014. }
  1015. }
  1016. }
  1017. }
  1018. }
  1019. 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 )
  1020. {
  1021. LOG_TRACE("user_code = %08x", dwUserCode);
  1022. if ((dwUserCode == LOG_EVT_UI_START_CONTINURECORD ||
  1023. dwUserCode == LOG_EVT_START_CONTINURECORD) && !m_bCamConfig)
  1024. {
  1025. ContinuRecordStateCode statecode = GetContinuRecordState();
  1026. if (statecode == State_EwsCameraConfigNotFound)
  1027. {
  1028. OnWideAngleCameraStartFailed("广角摄像头未配置!");
  1029. return;
  1030. }
  1031. if (statecode == State_EwsCameraNotInserted)
  1032. {
  1033. OnWideAngleCameraStartFailed("广角摄像头未插入!");
  1034. return;
  1035. }
  1036. if (statecode == State_EwsCameraStarting)
  1037. {
  1038. OnWideAngleCameraStartFailed("广角摄像头正在启动中!");
  1039. return;
  1040. }
  1041. if (statecode == State_EwsCameraFault)
  1042. {
  1043. OnWideAngleCameraStartFailed("广角摄像头故障中!");
  1044. if (m_bAutoMode && !m_bCamConfig) // add by ly at 2017/05/16
  1045. {
  1046. Sleep(5000);
  1047. LogEvent(Severity_High,LOG_EVT_RESTART_EWSCAMERA,NULL);
  1048. }
  1049. return;
  1050. }
  1051. //if (!CheckDiskStatus("C:\\",m_max_disk_percent))
  1052. //{
  1053. // if (m_bAutoMode) // add by ly at 2017/05/16
  1054. // {
  1055. // if (!m_last_disk_overtop)
  1056. // {
  1057. // m_last_disk_overtop = TRUE;
  1058. // OnWideAngleCameraStartFailed("磁盘所剩空间不足,无法开始全程双录!");
  1059. // }
  1060. // Sleep(5000);
  1061. // LogEvent(Severity_High,LOG_EVT_RESTART_EWSCAMERA,NULL);
  1062. // }
  1063. // else
  1064. // {
  1065. // m_last_disk_overtop = TRUE;
  1066. // OnWideAngleCameraStartFailed("磁盘所剩空间不足,无法开始全程双录!");
  1067. // }
  1068. // return;
  1069. //}
  1070. //else
  1071. //{
  1072. // m_last_disk_overtop = FALSE;
  1073. //}
  1074. CSimpleStringA strDiskStatus;
  1075. GetFunction()->GetSysVar(SYSVAR_DISKSTATEUS_FOR_VIDEO,strDiskStatus);
  1076. Dbg("Disk status for video on start record:%s.",(LPCTSTR)strDiskStatus);
  1077. if (strDiskStatus.GetLength()>0&&strDiskStatus[0]=='F')
  1078. {
  1079. if (m_bAutoMode) // add by ly at 2017/05/16
  1080. {
  1081. if (!m_last_disk_overtop)
  1082. {
  1083. m_last_disk_overtop = TRUE;
  1084. OnWideAngleCameraStartFailed("磁盘所剩空间不足,无法开始全程双录!");
  1085. }
  1086. }
  1087. else
  1088. {
  1089. m_last_disk_overtop = TRUE;
  1090. OnWideAngleCameraStartFailed("磁盘所剩空间不足,无法开始全程双录!");
  1091. }
  1092. return;
  1093. }
  1094. else
  1095. {
  1096. m_last_disk_overtop = FALSE;
  1097. }
  1098. if (!m_bStartCamera) // 若摄像头未启动,则先启动摄像头 edit by ly@20180725
  1099. {
  1100. StartCamera();
  1101. }
  1102. else
  1103. {
  1104. WideAngleCameraStateChanged evt;
  1105. evt.statecod = State_EwsCameraStartSucceeded;
  1106. evt.statemsg = CSimpleStringA2W("已启动广角摄像头!");
  1107. evt.camname = CSimpleStringA2W(m_CamFriendlyName.GetLength()>1?m_CamFriendlyName:"广角");
  1108. SpSendBroadcast(GetFunction(), SP_MSG_OF(WideAngleCameraStateChanged), SP_MSG_SIG_OF(WideAngleCameraStateChanged), evt);
  1109. Dbg("[dbg] WideAngleCameraStateChanged broadcast sent!");
  1110. }
  1111. if (m_bStartCamera)
  1112. {
  1113. // 开始全程录像
  1114. Dbg("start record!");
  1115. //StartRecord(pszMessage);
  1116. m_uid = CUUID::Create(m_uid);
  1117. m_RecordBeginTime = m_uid.GetTime().ToTimeString();
  1118. Dbg("RecordBeginTime:%s", (LPCTSTR)m_RecordBeginTime);
  1119. StopRecord(); // 如果正在录像,则先停止
  1120. if (pszMessage&&strlen(pszMessage)>0)
  1121. {
  1122. StartRecord(pszMessage);
  1123. }
  1124. else
  1125. {
  1126. CSimpleStringA videoname = CSimpleStringA::Format("G_%s",(LPCTSTR)m_uid.ToString());
  1127. StartRecord((LPCTSTR)videoname);
  1128. }
  1129. // 开始录像计时和数据记录
  1130. RecordInit();
  1131. GetFunction()->SetTimer(TIMERID_RECORD, this, RECORD_CYCLE);
  1132. }
  1133. else
  1134. {
  1135. if (m_bAutoMode && !m_bCamConfig) // edit by ly 2017/04/12
  1136. {
  1137. LogEvent(Severity_High,LOG_EVT_RESTART_EWSCAMERA,NULL);
  1138. }
  1139. }
  1140. }
  1141. else if (dwUserCode == LOG_EVT_UI_STOP_CONTINURECORD ||
  1142. dwUserCode == LOG_EVT_STOP_CONTINURECORD)
  1143. {
  1144. Dbg("stop record!");
  1145. StopEchoRecord(); // 停止回显
  1146. StopRecord(); // 停止全程录像
  1147. StopCamera(); // 停止摄像头
  1148. GetFunction()->KillTimer(TIMERID_RECORD);
  1149. std::string strWmvFile;
  1150. BOOL bRet = GetCurrentSectionBySerialID((LPCTSTR)m_uid.ToString(),strWmvFile);
  1151. if (!bRet)
  1152. {
  1153. Dbg("failed to get current record file name!");
  1154. }
  1155. bRet = UpdateVideoRecord(strWmvFile.c_str());
  1156. if (!bRet)
  1157. {
  1158. Dbg("failed to update video record!");
  1159. }
  1160. CSimpleStringA srcfn = CSimpleStringA::Format("%s%s",
  1161. (LPCTSTR)m_TempDir,strWmvFile.c_str());
  1162. CSimpleStringA dstfn = CSimpleStringA::Format("%s%s",
  1163. (LPCTSTR)m_RecordSaveDir,strWmvFile.c_str());
  1164. bRet = MoveFile((LPCSTR)srcfn, (LPCSTR)dstfn);
  1165. if(!bRet) {
  1166. Dbg("Error Code %lu while move %s ", GetLastError(), (LPCSTR)srcfn);
  1167. }
  1168. //GetFunction()->KillTimer(TIMERID_UPLOAD);
  1169. CSimpleStringA cfn = CSimpleStringA::Format("%sG_%s.txt",(LPCTSTR)m_TempDir,(LPCTSTR)m_uid.ToString());
  1170. UploadVideoRecord((LPCTSTR)cfn);
  1171. if (!stricmp(pszMessage,"Fault"))
  1172. {
  1173. if (m_bAutoMode && !m_bCamConfig) // edit by ly 2017/04/12
  1174. {
  1175. LogEvent(Severity_High,LOG_EVT_RESTART_EWSCAMERA,NULL);
  1176. }
  1177. else
  1178. {
  1179. Dbg("[dbg] camera fault, RealSelfCheck!");
  1180. RealSelfCheck();
  1181. }
  1182. }
  1183. else
  1184. {
  1185. m_nCameraErrorCode = Error_Succeed;
  1186. if ((ePadtype==m_eDeviceType||eDesk2SType==m_eDeviceType)&&m_bRecordAllowed) // 重新开始全程录像 edit by ly 2017/04/12
  1187. {
  1188. //GetFunction()->SetTimer(TIMERID_UPLOAD,this,UPLOAD_CYCLE);
  1189. if ((dwUserCode == LOG_EVT_STOP_CONTINURECORD || m_bAutoMode == true) && !m_bCamConfig)
  1190. {
  1191. LogEvent(Severity_Middle,LOG_EVT_START_CONTINURECORD,NULL);
  1192. }
  1193. }
  1194. }
  1195. }
  1196. else if (dwUserCode == LOG_EVT_UI_START_ECHO_CONTINURECORD && !m_bCamConfig)
  1197. {
  1198. // 开始回显全程录像画面
  1199. Dbg("start to echo continuous record, get pos = %s",pszMessage);
  1200. int xPos,yPos,width,height;
  1201. sscanf(pszMessage, "%d@%d@%d@%d", &xPos, &yPos, &width, &height);
  1202. Dbg("x=%d,y=%d,width=%d,height=%d",xPos,yPos,width,height);
  1203. int ScreenWidth = GetSystemMetrics(SM_CXSCREEN); //主屏幕宽度
  1204. int ScreenHeight = GetSystemMetrics(SM_CYSCREEN); //主屏幕高度
  1205. if (xPos<0||yPos<0||width<0||height<0||
  1206. xPos+width>ScreenWidth||yPos+height>ScreenHeight) //
  1207. {
  1208. // 弹出模态窗口提示回显参数不正确
  1209. MessageBoxA(NULL,(LPCSTR)("窗口参数不正确,无法回显录像!"),NULL,MB_SYSTEMMODAL);
  1210. return;
  1211. }
  1212. StartEchoRecord(xPos, yPos, width, height);
  1213. }
  1214. else if (dwUserCode == LOG_EVT_UI_STOP_ECHO_CONTINURECORD)
  1215. {
  1216. // 停止回显全程录像画面
  1217. StopEchoRecord();
  1218. }
  1219. else if (dwUserCode == LOG_EVT_CONTINURECORD_FAILED)
  1220. {
  1221. // 通知到业务中台
  1222. ContinuRecordFailed evt;
  1223. evt.failedmsg = CSimpleStringA2W(pszMessage==NULL?"全程双录失败!":pszMessage);
  1224. SpSendBroadcast(GetFunction(), SP_MSG_OF(ContinuRecordFailed), SP_MSG_SIG_OF(ContinuRecordFailed), evt);
  1225. Dbg("[dbg] ContinuRecordFailed broadcast sent!");
  1226. if (m_bEchoRecord&&pSession)
  1227. {
  1228. if (pszMessage==NULL)
  1229. {
  1230. MessageBoxA(NULL,(LPCSTR)("全程双录失败,回显停止!"),NULL,MB_SYSTEMMODAL);
  1231. }
  1232. else
  1233. {
  1234. CSimpleStringA txt = CSimpleStringA::Format("%s回显停止!",pszMessage);
  1235. MessageBoxA(NULL,(LPCSTR)txt,NULL,MB_SYSTEMMODAL);
  1236. }
  1237. }
  1238. // 录像故障,停止回显,结束录像,停止摄像头
  1239. StopEchoRecord();
  1240. StopRecord();
  1241. StopCamera();
  1242. GetFunction()->KillTimer(TIMERID_RECORD);
  1243. std::string strWmvFile;
  1244. BOOL bRet = GetCurrentSectionBySerialID((LPCTSTR)m_uid.ToString(),strWmvFile);
  1245. if (!bRet)
  1246. {
  1247. Dbg("failed to get current record file name!");
  1248. }
  1249. bRet = UpdateVideoRecord(strWmvFile.c_str());
  1250. if (!bRet)
  1251. {
  1252. Dbg("failed to update video record!");
  1253. }
  1254. CSimpleStringA srcfn = CSimpleStringA::Format("%s%s",
  1255. (LPCTSTR)m_TempDir,strWmvFile.c_str());
  1256. CSimpleStringA dstfn = CSimpleStringA::Format("%s%s",
  1257. (LPCTSTR)m_RecordSaveDir,strWmvFile.c_str());
  1258. bRet = MoveFile((LPCSTR)srcfn, (LPCSTR)dstfn);
  1259. if(!bRet) {
  1260. Dbg("Error Code %lu while move %s ", GetLastError(), (LPCSTR)srcfn);
  1261. }
  1262. //GetFunction()->KillTimer(TIMERID_UPLOAD);
  1263. CSimpleStringA cfn = CSimpleStringA::Format("%sG_%s.txt",(LPCTSTR)m_TempDir,(LPCTSTR)m_uid.ToString());
  1264. UploadVideoRecord((LPCTSTR)cfn);
  1265. m_nCameraErrorCode = Error_Succeed;
  1266. if (m_bAutoMode && !pszMessage && !m_bCamConfig) // edit by ly at 2017/05/25
  1267. {
  1268. LogEvent(Severity_High,LOG_EVT_RESTART_EWSCAMERA,NULL);
  1269. }
  1270. }
  1271. else if (dwUserCode == EVENT_USB_CAMER_INPUT)
  1272. {
  1273. Dbg("[dbg] usbcam %s inserted.", pszMessage);
  1274. ::Sleep(2000); // 延迟2s后判断
  1275. //ews配置名自纠正
  1276. AutoCorrectCameraConfigName(4000);
  1277. if ((ePadtype==m_eDeviceType||eDesk2SType==m_eDeviceType)&&m_bRecordAllowed) // edit by ly 2017/04/12
  1278. {
  1279. if (!stricmp((LPCTSTR)conf.strVideo,pszMessage))
  1280. {
  1281. m_bCameraInserted = TRUE;
  1282. // 通报广角摄像头已经插入
  1283. WideAngleCameraInserted evt;
  1284. evt.camname = CSimpleStringA2W(m_CamFriendlyName.GetLength()>1?m_CamFriendlyName:"全程双录");
  1285. SpSendBroadcast(GetFunction(), SP_MSG_OF(WideAngleCameraInserted), SP_MSG_SIG_OF(WideAngleCameraInserted), evt);
  1286. Dbg("[dbg] WideAngleCameraInserted broadcast sent!");
  1287. //GetFunction()->SetTimer(TIMERID_UPLOAD,this,UPLOAD_CYCLE);
  1288. if (m_bAutoMode && !m_bCamConfig)
  1289. {
  1290. LogEvent(Severity_Middle,LOG_EVT_START_CONTINURECORD,NULL);
  1291. }
  1292. }
  1293. }
  1294. }
  1295. else if (dwUserCode == EVENT_USB_CAMER_OUTPUT)
  1296. {
  1297. Dbg("[dbg] usbcam %s removed.", pszMessage);
  1298. //::Sleep(1000); // 延迟1s后判断
  1299. if (!stricmp((LPCTSTR)conf.strVideo,pszMessage))
  1300. {
  1301. // 及时通知正在用广角摄像头双录的业务 add by ly@2019/04/25
  1302. if (m_bExternStartCam)
  1303. {
  1304. SetCameraSysVar(CAMERA_EWS_ERROR);
  1305. }
  1306. if ((ePadtype==m_eDeviceType||eDesk2SType==m_eDeviceType)&&m_bRecordAllowed)
  1307. {
  1308. m_bCameraInserted = FALSE;
  1309. // 通报故障(正在使用时拔出广角摄像头)
  1310. WideAngleCameraRemoved evt;
  1311. evt.camname = CSimpleStringA2W(m_CamFriendlyName.GetLength()>1?m_CamFriendlyName:"全程双录");
  1312. SpSendBroadcast(GetFunction(), SP_MSG_OF(WideAngleCameraRemoved), SP_MSG_SIG_OF(WideAngleCameraRemoved), evt);
  1313. Dbg("[dbg] WideAngleCameraRemoved broadcast sent!");
  1314. if (m_bEchoRecord&&pSession)
  1315. {
  1316. MessageBoxA(NULL,(LPCSTR)("广角摄像头已移除,回显停止!"),NULL,MB_SYSTEMMODAL);
  1317. }
  1318. StopEchoRecord(); // 停止回显
  1319. StopRecord(); // 结束录像
  1320. StopCamera(); // 停止摄像头
  1321. m_nCameraErrorCode = Error_Succeed;
  1322. GetFunction()->KillTimer(TIMERID_RECORD);
  1323. std::string strWmvFile;
  1324. BOOL bRet = GetCurrentSectionBySerialID((LPCTSTR)m_uid.ToString(),strWmvFile);
  1325. if (!bRet)
  1326. {
  1327. Dbg("failed to get current record file name!");
  1328. }
  1329. bRet = UpdateVideoRecord(strWmvFile.c_str());
  1330. if (!bRet)
  1331. {
  1332. Dbg("failed to update video record!");
  1333. }
  1334. CSimpleStringA srcfn = CSimpleStringA::Format("%s%s",
  1335. (LPCTSTR)m_TempDir,strWmvFile.c_str());
  1336. CSimpleStringA dstfn = CSimpleStringA::Format("%s%s",
  1337. (LPCTSTR)m_RecordSaveDir,strWmvFile.c_str());
  1338. bRet = MoveFile((LPCSTR)srcfn, (LPCSTR)dstfn);
  1339. if(!bRet) {
  1340. Dbg("Error Code %lu while move %s ", GetLastError(), (LPCSTR)srcfn);
  1341. }
  1342. //GetFunction()->KillTimer(TIMERID_UPLOAD);
  1343. CSimpleStringA cfn = CSimpleStringA::Format("%sG_%s.txt",(LPCTSTR)m_TempDir,(LPCTSTR)m_uid.ToString());
  1344. UploadVideoRecord((LPCTSTR)cfn);
  1345. }
  1346. }
  1347. }
  1348. else if (dwUserCode == LOG_EVT_RESTART_EWSCAMERA && !m_bCamConfig)
  1349. {
  1350. //if (!CheckDiskStatus("C:\\",m_max_disk_percent))
  1351. //{
  1352. // if (!m_last_disk_overtop) // edit by ly at 2017/05/17
  1353. // {
  1354. // m_last_disk_overtop = TRUE;
  1355. // OnWideAngleCameraStartFailed("磁盘所剩空间不足,无法开始全程双录!");
  1356. // }
  1357. // Sleep(5000);
  1358. // LogEvent(Severity_High,LOG_EVT_RESTART_EWSCAMERA,NULL);
  1359. // return;
  1360. //}
  1361. //else
  1362. //{
  1363. // m_last_disk_overtop = FALSE;
  1364. //}
  1365. // 尝试重启广角摄像头(至多3次,达到3次即重启实体)
  1366. int tryFailedTimes=0;
  1367. do
  1368. {
  1369. StartCamera();
  1370. if (m_bStartCamera)
  1371. {
  1372. // 开始全程录像
  1373. Dbg("start record!");
  1374. m_uid = CUUID::Create(m_uid);
  1375. m_RecordBeginTime = m_uid.GetTime().ToTimeString();
  1376. Dbg("RecordBeginTime:%s", (LPCTSTR)m_RecordBeginTime);
  1377. StopRecord(); // 如果正在录像,则先停止
  1378. if (pszMessage&&strlen(pszMessage)>0)
  1379. {
  1380. StartRecord(pszMessage);
  1381. }
  1382. else
  1383. {
  1384. CSimpleStringA videoname = CSimpleStringA::Format("G_%s",(LPCTSTR)m_uid.ToString());
  1385. StartRecord((LPCTSTR)videoname);
  1386. }
  1387. // 开始录像计时和数据记录
  1388. RecordInit();
  1389. GetFunction()->SetTimer(TIMERID_RECORD, this, RECORD_CYCLE);
  1390. break;
  1391. }
  1392. else
  1393. {
  1394. tryFailedTimes++;
  1395. }
  1396. } while (tryFailedTimes<3);
  1397. if (tryFailedTimes>=3)
  1398. {
  1399. Dbg("restart camera failed for 3 times, restart entity now.");
  1400. RealSelfCheck();
  1401. }
  1402. }
  1403. else if (dwUserCode == LOG_EVT_CONTINURECORD_SECTION_FINISHED)
  1404. {
  1405. CSimpleStringA filename = pszMessage;
  1406. int pos = filename.IndexOf("G");
  1407. filename = filename.SubString(pos, filename.GetLength() - pos);
  1408. BOOL bRet = UpdateVideoRecord((LPCTSTR)filename);
  1409. if (!bRet)
  1410. {
  1411. Dbg("failed to update video record!");
  1412. GetFunction()->SetTimer(TIMERID_RECORD, this, RECORD_CYCLE);
  1413. return;
  1414. }
  1415. CSimpleStringA dstfn = CSimpleStringA::Format("%s%s",
  1416. (LPCTSTR)m_RecordSaveDir,(LPCTSTR)filename);
  1417. bRet = MoveFile(pszMessage, (LPCSTR)dstfn);
  1418. if(!bRet) {
  1419. Dbg("Error Code %lu while move %s ", GetLastError(), pszMessage);
  1420. }
  1421. GetFunction()->SetTimer(TIMERID_RECORD, this, RECORD_CYCLE);
  1422. }
  1423. else if (dwUserCode == LOG_EVT_BEGIN_CAMERA_CONFIG)
  1424. {
  1425. ExternBeginControl();
  1426. }
  1427. else if (dwUserCode == LOG_EVT_END_CAMERA_CONFIG)
  1428. {
  1429. ExternEndControl();
  1430. }
  1431. else if(LOG_EVT_CONTINURECORD_ENTITY_EXCEPTION == dwUserCode)
  1432. {
  1433. HandleContinurecordEntityException(pszMessage);
  1434. }
  1435. }
  1436. void CSurveillanceRecorderEntity::OnSelfTest( EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext )
  1437. {
  1438. if (Test_ShakeHand == eTestType)
  1439. {
  1440. if ((ePadtype==m_eDeviceType||eDesk2SType==m_eDeviceType)&&m_bRecordAllowed) {
  1441. if (conf.strVideo.GetLength()>1&&conf.strAudioIn.GetLength()>1&&m_bCameraInserted) {
  1442. if (m_nCameraErrorCode != Error_Succeed) {
  1443. Dbg("[dbg][OnSelfTest] m_nCameraErrorCode:0x%x", m_nCameraErrorCode);
  1444. pTransactionContext->SendAnswer(Error_Unexpect);
  1445. m_nCameraErrorCode = Error_Succeed;
  1446. }
  1447. else {
  1448. pTransactionContext->SendAnswer(Error_Succeed);
  1449. }
  1450. }
  1451. else {
  1452. pTransactionContext->SendAnswer(Error_Succeed);
  1453. }
  1454. if (m_bNeedRestart){
  1455. Dbg("[OnSelfTest] need restart entity now.");
  1456. pTransactionContext->SendAnswer(Error_Unexpect);
  1457. }
  1458. }
  1459. else {
  1460. pTransactionContext->SendAnswer(Error_Succeed);
  1461. }
  1462. }
  1463. }
  1464. ErrorCodeEnum CSurveillanceRecorderEntity::StartEwsCamera()
  1465. {
  1466. if (m_ews_capture == NULL)
  1467. {
  1468. //ews配置名自纠正
  1469. AutoCorrectCameraConfigName(4000);
  1470. ErrorCodeEnum Error = Error_Succeed;
  1471. conf.ref_ews_capture_count = &m_lCaptureEwsCount;
  1472. Error = (ErrorCodeEnum)ews_capture_create(&conf, &m_ews_capture);
  1473. if (Error == Error_Succeed)
  1474. {
  1475. Error = ews_capture_start(m_ews_capture);
  1476. Dbg("Error code=0x%x",Error);
  1477. if (Error != Error_Succeed)
  1478. {
  1479. if (Error == Error_EwsCamera)
  1480. {
  1481. ews_capture_destroy(m_ews_capture);
  1482. m_ews_capture = NULL;
  1483. return Error;
  1484. }
  1485. else // Error_AudioIN
  1486. {
  1487. return Error;
  1488. }
  1489. }
  1490. else
  1491. {
  1492. SetAutoBright(Error);
  1493. GetFunction()->SetTimer(TIMERID_CAMCHK, this, CAMCHK_CYCLE);
  1494. return Error_Succeed;
  1495. }
  1496. }
  1497. else
  1498. {
  1499. Dbg("create ews_capture failed! %d", Error);
  1500. m_ews_capture = NULL;
  1501. return Error_Unexpect;
  1502. }
  1503. return Error;
  1504. }
  1505. else
  1506. {
  1507. Dbg("m_ews_capture == NULL");
  1508. return Error_Unexpect;
  1509. }
  1510. }
  1511. void CSurveillanceRecorderEntity::StartCamera()
  1512. {
  1513. // 通报正在启动广角摄像头
  1514. SetCameraSysVar(CAMERA_START_ING);
  1515. WideAngleCameraStateChanged evt;
  1516. evt.statecod = State_EwsCameraStarting;
  1517. evt.statemsg = CSimpleStringA2W("广角摄像头启动中,请稍等……");
  1518. evt.camname = CSimpleStringA2W(m_CamFriendlyName.GetLength()>1?m_CamFriendlyName:"广角");
  1519. SpSendBroadcast(GetFunction(), SP_MSG_OF(WideAngleCameraStateChanged), SP_MSG_SIG_OF(WideAngleCameraStateChanged), evt);
  1520. Dbg("[dbg] WideAngleCameraStateChanged broadcast sent!");
  1521. // 启动广角摄像头
  1522. ErrorCodeEnum Error = StartEwsCamera();
  1523. if (Error!=Error_Succeed)
  1524. {
  1525. Dbg("[dbg] Start Ews Camera Error=0x%x", Error);
  1526. if(Error == Error_EwsCamera)
  1527. {
  1528. m_bStartCamera = FALSE;
  1529. //SetCameraSysVar(CAMERA_EWS_ERROR);
  1530. Dbg("Camera State to Error_EwsCamera");
  1531. m_nCameraErrorCode = Error_EwsCamera;
  1532. GetFunction()->SetUserDefineState(USER_SURVEILLANCE_EWSERROR);
  1533. }
  1534. else
  1535. {
  1536. //SetCameraSysVar(Error);
  1537. Dbg("Camera State to Other Error");
  1538. m_nCameraErrorCode = Error;
  1539. m_bStartCamera = FALSE;
  1540. }
  1541. OnWideAngleCameraStartFailed("广角摄像头启动失败!");
  1542. if (!m_bAutoMode)
  1543. {
  1544. RealSelfCheck();
  1545. }
  1546. }
  1547. else
  1548. {
  1549. if (m_bRecordAllowed&&!m_pRecorder)
  1550. {
  1551. BOOL bRet = FALSE;
  1552. m_pRecorder = new Clibwmvrecord(&bRet, this, REC_COMMON_AUDIO_EWS_SHM_QUEUE, REC_COMMON_VIDEO_EWS_SHM_RTP_QUEUE, NULL);
  1553. }
  1554. // 通报广角摄像头已经成功启动
  1555. m_bStartCamera = TRUE;
  1556. Dbg("Camera State to CAMERA_NO_ERROR");
  1557. m_nCameraErrorCode = Error_Succeed;
  1558. GetFunction()->SetUserDefineState(USER_SURVEILLANCE_IDLE);
  1559. WideAngleCameraStateChanged evt;
  1560. evt.statecod = State_EwsCameraStartSucceeded;
  1561. evt.statemsg = CSimpleStringA2W("已启动广角摄像头!");
  1562. evt.camname = CSimpleStringA2W(m_CamFriendlyName.GetLength()>1?m_CamFriendlyName:"广角");
  1563. SpSendBroadcast(GetFunction(), SP_MSG_OF(WideAngleCameraStateChanged), SP_MSG_SIG_OF(WideAngleCameraStateChanged), evt);
  1564. Dbg("[dbg] WideAngleCameraStateChanged broadcast sent!");
  1565. }
  1566. SetCameraSysVar(CAMERA_NO_ERROR);
  1567. }
  1568. void CSurveillanceRecorderEntity::StopEwsCamera()
  1569. {
  1570. // edit by ly@2019/04/18
  1571. if ((ePadtype==m_eDeviceType||eMobilePadType==m_eDeviceType||eDesk2SType==m_eDeviceType)&&m_bStartCamera)
  1572. {
  1573. Dbg("Stop EwsCamera.");
  1574. if (m_ews_capture)
  1575. {
  1576. GetFunction()->KillTimer(TIMERID_CAMCHK);
  1577. ews_capture_stop(m_ews_capture);
  1578. ews_capture_destroy(m_ews_capture);
  1579. m_ews_capture = NULL;
  1580. }
  1581. SetCameraSysVar(CAMERA_NO_ERROR);
  1582. m_bStartCamera = FALSE;
  1583. }
  1584. }
  1585. void CSurveillanceRecorderEntity::StopCamera()
  1586. {
  1587. // edit by ly@20180725
  1588. if (!m_bExternStartCam)
  1589. {
  1590. StopEwsCamera();
  1591. }
  1592. }
  1593. ErrorCodeEnum CSurveillanceRecorderEntity::ReStartEwsCamera()
  1594. {
  1595. if (m_ews_capture != NULL)
  1596. {
  1597. if((ews_stop_camera(m_ews_capture)==-1))
  1598. {
  1599. Dbg("ReStartCamera,stop camera fail!");
  1600. }
  1601. Sleep(2000);
  1602. //ews配置名自纠正
  1603. AutoCorrectCameraConfigName(4000);
  1604. ErrorCodeEnum Error = Error_Succeed;
  1605. Error = ews_capture_create(&conf, m_ews_capture);
  1606. if (Error == Error_Succeed)
  1607. {
  1608. Error = ews_capture_start(m_ews_capture);
  1609. if (Error != Error_Succeed)
  1610. {
  1611. Dbg("ReStartCamera,capture_start fail!");
  1612. ews_capture_destroy(m_ews_capture);
  1613. return Error;
  1614. }
  1615. else
  1616. {
  1617. Dbg("ReStartCamera success!");
  1618. return Error_Succeed;
  1619. }
  1620. }
  1621. else
  1622. {
  1623. Dbg("ReStartCamera,capture_create fail!");
  1624. ews_capture_destroy(m_ews_capture);
  1625. return Error;
  1626. }
  1627. }
  1628. else
  1629. {
  1630. return Error_Unexpect;
  1631. }
  1632. }
  1633. void CSurveillanceRecorderEntity::StartRecord( const char *wmvfilename)
  1634. {
  1635. Dbg("wmvfilename = %s", wmvfilename);
  1636. int fps = 5;
  1637. SubtitleParam subtitleParam;
  1638. ZeroMemory(&subtitleParam,sizeof(SubtitleParam));
  1639. subtitleParam.bSubtitle = TRUE;
  1640. subtitleParam.bSubtitleSection = FALSE;
  1641. Rvc_RecordAudioParam_t tAudioParams;
  1642. tAudioParams.eRecordType = eSingleSide;
  1643. tAudioParams.eOutPutType = m_eAudioOutQuality;
  1644. tAudioParams.bIsNsOn = false;
  1645. tAudioParams.iNsPolicy = 2;
  1646. tAudioParams.iAudioOutBitRate = 8;
  1647. tAudioParams.bIsTransOn = false;
  1648. if (m_pRecorder->StartWmvRecord(fps, 90, &tAudioParams, &subtitleParam, FALSE, FALSE, (LPCSTR)m_TempDir,
  1649. m_TempDir.GetLength(), wmvfilename, strlen(wmvfilename))) {
  1650. m_bStartRecord = TRUE;
  1651. }
  1652. }
  1653. void CSurveillanceRecorderEntity::StopRecord()
  1654. {
  1655. if (m_bStartRecord)
  1656. {
  1657. m_bStartRecord = FALSE;
  1658. m_pRecorder->StopWmvRecord();
  1659. }
  1660. }
  1661. void CSurveillanceRecorderEntity::StartEchoRecord( int x, int y, int w, int h )
  1662. {
  1663. videoplay_session_conf_t video_conf;
  1664. memset(&video_conf,0,sizeof(video_conf));
  1665. video_conf.local_video_view_x = x;
  1666. video_conf.local_video_view_y = y;
  1667. video_conf.local_video_view_cx = w;
  1668. video_conf.local_video_view_cy = h;
  1669. //video_conf.ref_camera_state = &m_iCameraState;
  1670. video_conf.pHostApi = this;
  1671. strcpy(video_conf.local_video_preview_queue0,REC_COMMON_VIDEO_EWS_SHM_PREVIEW_QUEUE);
  1672. int rc = Local_videoplay_session_create(&video_conf, &pSession);
  1673. if (rc != 0)
  1674. {
  1675. Dbg("create video play session failed! rc = %d", rc);
  1676. }
  1677. else
  1678. {
  1679. rc = Local_videoplay_session_start(pSession);
  1680. if (rc != 0)
  1681. {
  1682. Dbg("start video play session failed! rc = %d", rc);
  1683. Local_videoplay_session_destroy(pSession);
  1684. pSession = NULL;
  1685. }
  1686. else
  1687. {
  1688. m_bEchoRecord = TRUE;
  1689. Dbg("start video play session succeed! rc = %d", rc);
  1690. }
  1691. }
  1692. }
  1693. void CSurveillanceRecorderEntity::StopEchoRecord()
  1694. {
  1695. if (m_bEchoRecord&&pSession)
  1696. {
  1697. m_bEchoRecord = FALSE;
  1698. Local_videoplay_session_stop(pSession);
  1699. Local_videoplay_session_destroy(pSession);
  1700. pSession = NULL;
  1701. Dbg("video play session stopped!");
  1702. }
  1703. }
  1704. ErrorCodeEnum CSurveillanceRecorderEntity::LoadServerConfig()
  1705. {
  1706. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  1707. CSmartPointer<IConfigInfo> spConfig;
  1708. ErrorCodeEnum Error = spFunction->OpenConfig(Config_CenterSetting, spConfig);
  1709. if (Error == Error_Succeed) {
  1710. CSimpleStringA str;
  1711. Error = spConfig->ReadConfigValue("SurveillanceRecorder", "Server", str);
  1712. if (Error == Error_Succeed)
  1713. {
  1714. Error = ParseIPAddress(str, m_server1, m_server1_port);
  1715. }
  1716. if (Error == Error_Succeed)
  1717. {
  1718. ErrorCodeEnum ErrorCode;
  1719. ErrorCode = spConfig->ReadConfigValue("SurveillanceRecorder", "Server_Backup", str);
  1720. if (ErrorCode == Error_Succeed)
  1721. {
  1722. ErrorCode = ParseIPAddress(str, m_server2, m_server2_port);
  1723. }
  1724. if (ErrorCode != Error_Succeed)
  1725. {
  1726. m_server2 = NULL;
  1727. m_server2_port = 0;
  1728. }
  1729. }
  1730. }
  1731. return Error;
  1732. }
  1733. bool CSurveillanceRecorderEntity::SecureClientConnect()
  1734. {
  1735. if (m_pConnection!=NULL && m_pConnection->IsConnectionOK())
  1736. {
  1737. return true;
  1738. }
  1739. SecureClientRelease();
  1740. m_pConnection = new CUploadRecordConnection(this);
  1741. /*if (!m_pConnection->Connect(m_server1,m_server1_port,3))
  1742. {
  1743. SecureClientRelease();
  1744. if ((m_server2!=NULL)&&(m_server2_port!=0)&&
  1745. ((m_server2!=m_server1)||(m_server2_port!=m_server1_port)))
  1746. {
  1747. m_pConnection = new CUploadRecordConnection(this);
  1748. if (!m_pConnection->Connect(m_server2,m_server2_port,3))
  1749. {
  1750. return false;
  1751. }
  1752. }
  1753. }*/
  1754. return m_pConnection->ConnectFromCentralSetting();
  1755. }
  1756. void CSurveillanceRecorderEntity::SecureClientRelease()
  1757. {
  1758. if (m_pConnection != NULL)
  1759. {
  1760. if (m_pConnection->IsConnectionOK())
  1761. {
  1762. m_pConnection->Close();
  1763. }
  1764. m_pConnection->DecRefCount();
  1765. m_pConnection = NULL;
  1766. }
  1767. }
  1768. BOOL CSurveillanceRecorderEntity::RecordInit()
  1769. {
  1770. CSimpleStringA fn = CSimpleStringA::Format("%sG_%s.txt",
  1771. (LPCTSTR)m_TempDir,(LPCTSTR)m_uid.ToString());
  1772. HANDLE hFile = CreateFile((LPCTSTR)fn,GENERIC_WRITE,FILE_SHARE_READ,
  1773. NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  1774. if (hFile != INVALID_HANDLE_VALUE) {
  1775. CloseHandle(hFile);
  1776. hFile = NULL;
  1777. WritePrivateProfileString("VideoRecord","LastLocalTime",
  1778. (LPCTSTR)m_RecordBeginTime,(LPCTSTR)fn);
  1779. WritePrivateProfileString("VideoRecord","LastFileCount","0",(LPCTSTR)fn);
  1780. return TRUE;
  1781. }
  1782. else {
  1783. Dbg("error to create file %s(error code:%d)!",(LPCTSTR)fn,GetLastError());
  1784. return FALSE;
  1785. }
  1786. }
  1787. BOOL CSurveillanceRecorderEntity::GetCurrentSectionBySerialID( const char* videoserialid, std::string &strWmvFile )
  1788. {
  1789. if (videoserialid == NULL)
  1790. {
  1791. return FALSE;
  1792. }
  1793. char wmvfilename[MAX_PATH] = {0};
  1794. sprintf_s(wmvfilename, "G_%s*.wmv", videoserialid);
  1795. std::vector<std::string> sfiles;
  1796. BOOL bRet = FindMatchedFile((LPCSTR)m_TempDir, wmvfilename, sfiles);
  1797. Dbg("bRet = %d while find %s", bRet, wmvfilename);
  1798. if(!bRet)
  1799. {
  1800. strWmvFile = "";
  1801. return FALSE;
  1802. }
  1803. std::vector<std::string>::iterator max_iter = max_element(sfiles.begin(),sfiles.end());
  1804. strWmvFile = max_iter->c_str();
  1805. return TRUE;
  1806. }
  1807. BOOL CSurveillanceRecorderEntity::UpdateVideoRecord(const char *videofilename)
  1808. {
  1809. CSimpleStringA fn;
  1810. if (videofilename == NULL || strlen(videofilename) <= 1)
  1811. {
  1812. Dbg("videofilename is NULL or empty!");
  1813. return FALSE;
  1814. }
  1815. fn = CSimpleStringA::Format("%s%s",(LPCTSTR)m_TempDir,videofilename);
  1816. FILE *fin = fopen((LPCTSTR)fn, "rb");
  1817. if (!fin)
  1818. {
  1819. fclose(fin);
  1820. return FALSE;
  1821. }
  1822. int FileLen = 0;
  1823. fseek(fin, 0, SEEK_END);
  1824. FileLen = ftell(fin);
  1825. fseek(fin, 0, SEEK_SET);
  1826. fclose(fin);
  1827. FileLen = int(1.0*FileLen/1024+0.5);
  1828. char strtmp[20] = {0}, strtmpkey[20] = {0};
  1829. int FileSeqNo = 0, FileCount = 0;
  1830. sscanf(videofilename,"G_%[^_]_%d.wmv",strtmp,&FileSeqNo);
  1831. FileCount = FileSeqNo + 1;
  1832. CSimpleStringA cfn = CSimpleStringA::Format("%sG_%s.txt",
  1833. (LPCTSTR)m_TempDir,strtmp);
  1834. SYSTEMTIME st;
  1835. GetLocalTime(&st);
  1836. CSimpleStringA curtime = CSimpleStringA::Format("%4d-%02d-%02d %02d:%02d:%02d",
  1837. st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute, st.wSecond);
  1838. WritePrivateProfileString("VideoRecord","LastLocalTime",
  1839. (LPCTSTR)curtime,(LPCTSTR)cfn);
  1840. sprintf_s(strtmp, "%d", FileCount);
  1841. WritePrivateProfileString("VideoRecord","LastFileCount",strtmp,(LPCTSTR)cfn);
  1842. sprintf_s(strtmpkey, "%d_FileLength", FileSeqNo);
  1843. sprintf_s(strtmp, "%d", FileLen);
  1844. WritePrivateProfileString("VideoRecord",strtmpkey,strtmp,(LPCTSTR)cfn);
  1845. return TRUE;
  1846. }
  1847. void CSurveillanceRecorderEntity::UploadVideoRecord(const char *videorecordname)
  1848. {
  1849. if (videorecordname == NULL)
  1850. {
  1851. if (SecureClientConnect())
  1852. {
  1853. auto arr = fileutil_get_sub_files((LPCTSTR)m_TempDir);
  1854. if (arr != NULL)
  1855. {
  1856. char szTmp[64];
  1857. std::vector<video_record_t *> videorecordlist;
  1858. for (int i = 0; i < arr->nelts; ++i)
  1859. {
  1860. char *pszFile = ARRAY_IDX(arr, i, char*);
  1861. if (strstr(pszFile,".txt") && strchr(pszFile,'G')
  1862. && !strstr(pszFile,(LPCTSTR)m_uid.ToString()))
  1863. {
  1864. if (videorecordlist.size() >= MAX_LISTSIZE)
  1865. {
  1866. break;
  1867. }
  1868. video_record_t* r = ZALLOC_T(video_record_t);
  1869. r->TerminalNo = _strdup((LPCTSTR)m_terminalNo);
  1870. CSimpleStringA filename = pszFile;
  1871. int pos = filename.IndexOf("G");
  1872. filename = filename.SubString(pos, filename.GetLength() - pos);
  1873. sscanf((LPCTSTR)filename, "G_%[^.].txt", szTmp);
  1874. r->VideoSerialID = _strdup(szTmp);
  1875. /*std::string strWmvFile;
  1876. BOOL bRet = GetCurrentSectionBySerialID(szTmp,strWmvFile);
  1877. if (bRet)
  1878. {
  1879. bRet = UpdateVideoRecord(strWmvFile.c_str());
  1880. if (!bRet)
  1881. {
  1882. Dbg("failed to update video record!");
  1883. }
  1884. CSimpleStringA srcfn = CSimpleStringA::Format("%s%s",
  1885. (LPCTSTR)m_TempDir,strWmvFile.c_str());
  1886. CSimpleStringA dstfn = CSimpleStringA::Format("%s%s",
  1887. (LPCTSTR)m_RecordSaveDir,strWmvFile.c_str());
  1888. bRet = MoveFile((LPCSTR)srcfn, (LPCSTR)dstfn);
  1889. if(!bRet) {
  1890. Dbg("Error Code %lu while move %s ", GetLastError(), (LPCSTR)srcfn);
  1891. }
  1892. }*/
  1893. GetPrivateProfileString("VideoRecord", "LastLocalTime", "", szTmp, 64, pszFile);
  1894. r->EndTime = _strdup(szTmp);
  1895. int nFileCount = GetPrivateProfileInt("VideoRecord", "LastFileCount", 0, pszFile);
  1896. int nFileLen = 0;
  1897. for (int j = 0; j < nFileCount; ++j)
  1898. {
  1899. sprintf_s(szTmp, "%d_FileLength", j);
  1900. nFileLen += GetPrivateProfileInt("VideoRecord", szTmp, 0, pszFile);
  1901. }
  1902. r->FileLength = nFileLen;
  1903. videorecordlist.push_back(r);
  1904. }
  1905. else if (strstr(pszFile,".wmv") && strchr(pszFile,'G')
  1906. && !strstr(pszFile,(LPCTSTR)m_uid.ToString()))
  1907. {
  1908. CSimpleStringA strWmvFile = pszFile;
  1909. int pos = strWmvFile.IndexOf("G");
  1910. strWmvFile = strWmvFile.SubString(pos, strWmvFile.GetLength() - pos);
  1911. CSimpleStringA srcfn = CSimpleStringA::Format("%s%s",
  1912. (LPCTSTR)m_TempDir,(LPCTSTR)strWmvFile);
  1913. CSimpleStringA dstfn = CSimpleStringA::Format("%s%s",
  1914. (LPCTSTR)m_RecordSaveDir,(LPCTSTR)strWmvFile);
  1915. BOOL bRet = MoveFile((LPCSTR)srcfn, (LPCSTR)dstfn);
  1916. if(!bRet) {
  1917. Dbg("Error Code %lu while move %s ", GetLastError(), (LPCSTR)srcfn);
  1918. }
  1919. }
  1920. }
  1921. Dbg("%d records added to upload list.", videorecordlist.size());
  1922. toolkit_array_free2(arr);
  1923. if (m_pConnection != NULL && m_pConnection->IsConnectionOK())
  1924. {
  1925. if (videorecordlist.size() > 0)
  1926. {
  1927. m_pConnection->SendVideoRecords(videorecordlist);
  1928. }
  1929. }
  1930. for (std::vector<video_record_t *>::size_type i = 0; i < videorecordlist.size(); ++i)
  1931. {
  1932. video_record_destroy(videorecordlist[i]);
  1933. }
  1934. }
  1935. //SecureClientRelease();
  1936. }
  1937. }
  1938. else
  1939. {
  1940. if (SecureClientConnect())
  1941. {
  1942. if (m_pConnection != NULL && m_pConnection->IsConnectionOK())
  1943. {
  1944. char szTmp[64] = {0};
  1945. video_record_t* r = ZALLOC_T(video_record_t);
  1946. r->TerminalNo = _strdup((LPCTSTR)m_terminalNo);
  1947. CSimpleStringA filename = videorecordname;
  1948. int pos = filename.IndexOf("G");
  1949. filename = filename.SubString(pos, filename.GetLength() - pos);
  1950. sscanf((LPCTSTR)filename, "G_%[^.].txt", szTmp);
  1951. r->VideoSerialID = _strdup(szTmp);
  1952. GetPrivateProfileString("VideoRecord", "LastLocalTime", "", szTmp, 64, videorecordname);
  1953. r->EndTime = _strdup(szTmp);
  1954. int nFileCount = GetPrivateProfileInt("VideoRecord", "LastFileCount", 0, videorecordname);
  1955. int nFileLen = 0;
  1956. Dbg("nFileCount=%d",nFileCount);
  1957. for (int j = 0; j < nFileCount; ++j)
  1958. {
  1959. sprintf_s(szTmp, "%d_FileLength", j);
  1960. nFileLen += GetPrivateProfileInt("VideoRecord", szTmp, 0, videorecordname);
  1961. }
  1962. Dbg("nFileLen=%d",nFileLen);
  1963. r->FileLength = nFileLen;
  1964. m_pConnection->SendVideoRecord(r);
  1965. video_record_destroy(r);
  1966. }
  1967. //SecureClientRelease();
  1968. }
  1969. }
  1970. }
  1971. void CSurveillanceRecorderEntity::OnEndUploadRecord( const char *videoserialid, int result )
  1972. {
  1973. Dbg("%s upload result: %d", videoserialid, result);
  1974. if (0 == result)
  1975. {
  1976. Dbg("succeed to save video record: G_%s", videoserialid);
  1977. CSimpleStringA cfn = CSimpleStringA::Format("%sG_%s.txt", (LPCTSTR)m_TempDir, videoserialid);
  1978. DeleteFile(cfn);
  1979. }
  1980. }
  1981. ErrorCodeEnum CSurveillanceRecorderEntity::ExternBeginControl()
  1982. {
  1983. Dbg("Extern begin control camera");
  1984. m_bCamConfig = TRUE;
  1985. if (m_ews_capture)
  1986. {
  1987. if (m_bStartRecord) m_bRecordPaused = TRUE;
  1988. LogEvent(Severity_Middle,LOG_EVT_STOP_CONTINURECORD,NULL);
  1989. GetFunction()->KillTimer(TIMERID_UPLOAD);
  1990. }
  1991. return Error_Succeed;
  1992. }
  1993. ErrorCodeEnum CSurveillanceRecorderEntity::ExternEndControl()
  1994. {
  1995. Dbg("Extern end control camera");
  1996. m_bCamConfig = FALSE;
  1997. if (m_bRecordPaused)
  1998. {
  1999. GetFunction()->SetTimer(TIMERID_UPLOAD,this,UPLOAD_CYCLE);
  2000. LogEvent(Severity_Middle,LOG_EVT_START_CONTINURECORD,NULL);
  2001. m_bRecordPaused = FALSE;
  2002. }
  2003. return Error_Succeed;
  2004. }
  2005. ErrorCodeEnum CSurveillanceRecorderEntity::HandleContinurecordEntityException(const char* pszMessage)
  2006. {
  2007. // 通知到业务中台
  2008. ContinuRecordFailed evt;
  2009. evt.failedmsg = CSimpleStringA2W(pszMessage==NULL?"全程双录异常!":pszMessage);
  2010. SpSendBroadcast(GetFunction(), SP_MSG_OF(ContinuRecordFailed), SP_MSG_SIG_OF(ContinuRecordFailed), evt);
  2011. Dbg("[dbg] ContinuRecord entity exception broadcast sent!");
  2012. if (m_bEchoRecord&&pSession)
  2013. {
  2014. if (pszMessage==NULL)
  2015. {
  2016. MessageBoxA(NULL,(LPCSTR)("全程双录异常,回显停止!"),NULL,MB_SYSTEMMODAL);
  2017. }
  2018. else
  2019. {
  2020. CSimpleStringA txt = CSimpleStringA::Format("%s回显停止!",pszMessage);
  2021. MessageBoxA(NULL,(LPCSTR)txt,NULL,MB_SYSTEMMODAL);
  2022. }
  2023. // 录像异常,停止回显
  2024. StopEchoRecord();
  2025. }
  2026. //StopRecord();
  2027. StopCamera();
  2028. GetFunction()->KillTimer(TIMERID_RECORD);
  2029. CUUID last_uid = m_uid;
  2030. std::string strWmvFile;
  2031. BOOL bRet = GetCurrentSectionBySerialID((LPCTSTR)last_uid.ToString(),strWmvFile);
  2032. if (!bRet)
  2033. {
  2034. Dbg("failed to get current record file name!");
  2035. }
  2036. bRet = UpdateVideoRecord(strWmvFile.c_str());
  2037. if (!bRet)
  2038. {
  2039. Dbg("failed to update video record!");
  2040. }
  2041. CSimpleStringA srcfn = CSimpleStringA::Format("%s%s",
  2042. (LPCTSTR)m_TempDir,strWmvFile.c_str());
  2043. CSimpleStringA dstfn = CSimpleStringA::Format("%s%s",
  2044. (LPCTSTR)m_RecordSaveDir,strWmvFile.c_str());
  2045. bRet = MoveFile((LPCSTR)srcfn, (LPCSTR)dstfn);
  2046. if(!bRet) {
  2047. Dbg("Error Code %lu while move %s ", GetLastError(), (LPCSTR)srcfn);
  2048. }
  2049. else{
  2050. Dbg("Move File from %s to %s success. ", (LPCSTR)srcfn, (LPCSTR)dstfn);
  2051. }
  2052. //GetFunction()->KillTimer(TIMERID_UPLOAD);
  2053. CSimpleStringA cfn = CSimpleStringA::Format("%sG_%s.txt",(LPCTSTR)m_TempDir,(LPCTSTR)last_uid.ToString());
  2054. UploadVideoRecord((LPCTSTR)cfn);
  2055. m_bNeedRestart = true;
  2056. //重启实体
  2057. RealSelfCheck();
  2058. //重新开始全程双录
  2059. //Dbg("Restart Continue Record.");
  2060. //RestartContinueRecord();
  2061. return Error_Succeed;
  2062. }
  2063. ErrorCodeEnum CSurveillanceRecorderEntity::RestartContinueRecord()
  2064. {
  2065. ErrorCodeEnum Error = Error_Succeed;
  2066. Dbg("Start Camera!");
  2067. StartCamera();
  2068. // 重新开始全程录像
  2069. Dbg("restart surveillance record!");
  2070. m_uid = CUUID::Create(m_uid);
  2071. m_RecordBeginTime = m_uid.GetTime().ToTimeString();
  2072. Dbg("Restart Continue Record Name is %s, RecordBeginTime:%s", (LPCTSTR)m_uid.ToString(),(LPCTSTR)m_RecordBeginTime);
  2073. //StopRecord(); // 如果正在录像,则先停止
  2074. CSimpleStringA videoname = CSimpleStringA::Format("G_%s",(LPCTSTR)m_uid.ToString());
  2075. StartRecord((LPCTSTR)videoname);
  2076. // 开始录像计时和数据记录
  2077. RecordInit();
  2078. GetFunction()->SetTimer(TIMERID_RECORD, this, RECORD_CYCLE);
  2079. return Error;
  2080. }
  2081. ErrorCodeEnum CSurveillanceRecorderEntity::SetAudioQuality(int iAudioQuality)
  2082. {
  2083. ErrorCodeEnum eRet = Error_Succeed;
  2084. Dbg("iAudioQuality == %d.", iAudioQuality);
  2085. switch (iAudioQuality)
  2086. {
  2087. case 1:
  2088. m_eAudioOutQuality = eLowDefinition;
  2089. break;
  2090. case 2:
  2091. m_eAudioOutQuality = eStandardDefinition;
  2092. break;
  2093. case 3:
  2094. m_eAudioOutQuality = eHighDefinition;
  2095. break;
  2096. case 4:
  2097. m_eAudioOutQuality = eUltraHD;
  2098. break;
  2099. default:
  2100. m_eAudioOutQuality = eUltraHD;
  2101. break;
  2102. }
  2103. return eRet;
  2104. }
  2105. ErrorCodeEnum CSurveillanceRecorderEntity::SetAudioNsPolicy(int iNsPolicy)
  2106. {
  2107. ErrorCodeEnum eRet = Error_Succeed;
  2108. Dbg("iNsPolicy == %d.", iNsPolicy);
  2109. if (1 == iNsPolicy || 3 == iNsPolicy){
  2110. m_iAudioNsPolicy = iNsPolicy;
  2111. }
  2112. else{
  2113. m_iAudioNsPolicy = 2;
  2114. }
  2115. return eRet;
  2116. }
  2117. SelfChekerClient::SelfChekerClient( CSurveillanceRecorderEntity *pEntity ) : SelfCheckerService_ClientBase(pEntity)
  2118. {
  2119. }
  2120. void SurveillanceRecordServiceSession::Handle_GetContinuRecordInfo( SpReqAnsContext<SurveillanceRecorderService_GetContinuRecordInfo_Req, SurveillanceRecorderService_GetContinuRecordInfo_Ans>::Pointer ctx )
  2121. {
  2122. if (!(ePadtype==m_pEntity->m_eDeviceType||eDesk2SType==m_pEntity->m_eDeviceType)||!m_pEntity->m_bRecordAllowed)
  2123. {
  2124. ctx->Ans.Remark = CSimpleStringA2W("本设备不支持全程双录!");
  2125. ctx->Answer(Error_Succeed);
  2126. return;
  2127. }
  2128. if (m_pEntity->m_bStartRecord)
  2129. {
  2130. ctx->Ans.TerminalNo = CSimpleStringA2W(m_pEntity->m_terminalNo);
  2131. ctx->Ans.RecordStartTime = CSimpleStringA2W(m_pEntity->m_RecordBeginTime);
  2132. ctx->Ans.VideoSerialID = CSimpleStringA2W(m_pEntity->m_uid.ToString()); // 录像流水号
  2133. if (!m_pEntity->m_bAutoMode)
  2134. {
  2135. ctx->Ans.Remark = CSimpleStringA2W("");
  2136. }
  2137. else
  2138. {
  2139. ctx->Ans.Remark = CSimpleStringA2W("AutoRecord");
  2140. }
  2141. }
  2142. else
  2143. {
  2144. if (!m_pEntity->m_bAutoMode)
  2145. {
  2146. ctx->Ans.Remark = CSimpleStringA2W("录像未开始!");
  2147. }
  2148. else
  2149. {
  2150. ctx->Ans.Remark = CSimpleStringA2W("AutoRecord:录像未开始!");
  2151. }
  2152. }
  2153. ctx->Answer(Error_Succeed);
  2154. }
  2155. void SurveillanceRecordServiceSession::Handle_GetContinuRecordState( SpReqAnsContext<SurveillanceRecorderService_GetContinuRecordState_Req, SurveillanceRecorderService_GetContinuRecordState_Ans>::Pointer ctx )
  2156. {
  2157. ctx->Ans.StateCode = m_pEntity->GetContinuRecordState();
  2158. ctx->Answer(Error_Succeed);
  2159. }
  2160. void SurveillanceRecordServiceSession::Handle_GetContinuRecordMode( SpReqAnsContext<SurveillanceRecorderService_GetContinuRecordMode_Req, SurveillanceRecorderService_GetContinuRecordMode_Ans>::Pointer ctx )
  2161. {
  2162. ctx->Ans.IsAuto = m_pEntity->GetContinuRecordMode();
  2163. ctx->Answer(Error_Succeed);
  2164. }
  2165. void SurveillanceRecordServiceSession::Handle_StartEwsCamera( SpReqAnsContext<SurveillanceRecorderService_StartEwsCamera_Req, SurveillanceRecorderService_StartEwsCamera_Ans>::Pointer ctx )
  2166. {
  2167. ErrorCodeEnum error = m_pEntity->ExternStartCamera();
  2168. Dbg("ExternStartCamera:0x%x", error);
  2169. int nRetCod = 7;
  2170. CSimpleStringA strErrMsg;
  2171. switch(error)
  2172. {
  2173. case Error_Succeed:
  2174. nRetCod = 0;
  2175. strErrMsg = "启动成功";
  2176. break;
  2177. case Error_Cancel:
  2178. nRetCod = 1;
  2179. strErrMsg = "设备类型非低柜双屏或PAD";
  2180. break;
  2181. case Error_Break:
  2182. nRetCod = 2;
  2183. strErrMsg = "当前处于摄像头配置模式";
  2184. break;
  2185. case Error_EwsCamera:
  2186. nRetCod = 3;
  2187. strErrMsg = "广角摄像头启动失败";
  2188. break;
  2189. case Error_InvalidState:
  2190. nRetCod = 4;
  2191. strErrMsg = "广角摄像头正在启动";
  2192. break;
  2193. case Error_Resource:
  2194. nRetCod = 5;
  2195. strErrMsg = "广角摄像头未配置";
  2196. break;
  2197. case Error_DevNotAvailable:
  2198. nRetCod = 6;
  2199. strErrMsg = "广角摄像头未插入";
  2200. break;
  2201. default:
  2202. nRetCod = 7;
  2203. strErrMsg = "发生其他错误";
  2204. break;
  2205. }
  2206. ctx->Ans.StartErrorCode = nRetCod;
  2207. ctx->Ans.StartErrorMsg = CSimpleStringA2W(strErrMsg);
  2208. ctx->Answer(Error_Succeed);
  2209. }
  2210. void SurveillanceRecordServiceSession::Handle_StopEwsCamera( SpReqAnsContext<SurveillanceRecorderService_StopEwsCamera_Req, SurveillanceRecorderService_StopEwsCamera_Ans>::Pointer ctx )
  2211. {
  2212. ErrorCodeEnum error = m_pEntity->ExternStopCamera();
  2213. Dbg("ExternStopCamera:0x%x", error);
  2214. int nRetCod = 2;
  2215. CSimpleStringA strErrMsg;
  2216. if (error == Error_Succeed)
  2217. {
  2218. nRetCod = 0;
  2219. strErrMsg = "停止成功";
  2220. }
  2221. else if (error == Error_Cancel)
  2222. {
  2223. nRetCod = 1;
  2224. strErrMsg = "设备类型非低柜双屏或PAD";
  2225. }
  2226. else
  2227. {
  2228. nRetCod = 2;
  2229. strErrMsg = "当前处于摄像头配置模式";
  2230. }
  2231. ctx->Ans.StopErrorCode = nRetCod;
  2232. ctx->Ans.StopErrorMsg = CSimpleStringA2W(strErrMsg);
  2233. ctx->Answer(Error_Succeed);
  2234. }
  2235. SP_BEGIN_ENTITY_MAP()
  2236. SP_ENTITY(CSurveillanceRecorderEntity)
  2237. SP_END_ENTITY_MAP()