mod_snapshot.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "SpIni.h"
  4. #include "Event.h"
  5. #include "mod_mediacontroller/Event.h"
  6. #include "AssistantChannel_client_g.h"
  7. using namespace AssistantChannel;
  8. #include "chan_protocol.h"
  9. #include "rvc_media_common.h"
  10. #include "libvideoqueue.h"
  11. #include "libfacecapture.h"
  12. #include "jpeg2k.h"
  13. #include "videoutil.h"
  14. #include "y2k_time.h"
  15. #include "EventCode.h"
  16. #include "CommEntityUtil.hpp"
  17. #include "cv.h"
  18. #include "highgui.h"
  19. #ifdef RVC_OS_WIN
  20. #define ENV_CAP_TIMEOUT 15
  21. #define OPT_CAP_TIMEOUT 10
  22. #else
  23. #define ENV_CAP_TIMEOUT 150
  24. #define OPT_CAP_TIMEOUT 100
  25. #endif
  26. #ifndef RVC_SAFE_DELETE
  27. #define RVC_SAFE_DELETE(p) { if(p){ (p)->SafeDelete(); (p)=NULL;} }
  28. #endif //RVC_SAFE_DELETE
  29. class CPhotoCaptureEntity;
  30. class ChannelClient : public ChannelService_ClientBase
  31. {
  32. public:
  33. ChannelClient(CPhotoCaptureEntity *pEntity);
  34. virtual void OnMessage(ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer<IReleasable> pData);
  35. };
  36. int RotationDown(unsigned char* src, int srcW, int srcH, int channel)
  37. {
  38. unsigned char* tempSrc = NULL;
  39. int mSize = srcW * srcH * sizeof(char) * channel;
  40. int i = 0;
  41. int j = 0;
  42. int k = 0;
  43. int desW = 0;
  44. int desH = 0;
  45. desW = srcW;
  46. desH = srcH;
  47. tempSrc = (unsigned char*)malloc(sizeof(char) * srcW * srcH * channel);
  48. memcpy(tempSrc, src, mSize);
  49. for (i = 0; i < desH; i++)
  50. {
  51. for (j = 0; j < desW; j++)
  52. {
  53. for (k = 0; k < channel; k++)
  54. {
  55. //src[(i * desW + j) * channel + k] = tempSrc[((srcH - 1 - i) * srcW + srcW - 1 - j) * channel + k];
  56. src[(i * desW + j) * channel + k] = tempSrc[((srcH - 1 - i) * srcW + j) * channel + k];
  57. }
  58. }
  59. }
  60. free(tempSrc);
  61. return 0;
  62. }
  63. // id: 0x905
  64. class CPhotoCaptureEntity : public CEntityBase, public ITimerListener, public ILogListener
  65. {
  66. public:
  67. CPhotoCaptureEntity() : m_video_env_q(NULL), m_video_opt_q(NULL), m_dwCapture(0), m_bConnectAssist(FALSE) {m_pChannelClient = NULL;}
  68. virtual ~CPhotoCaptureEntity() {}
  69. virtual const char *GetEntityName() const { return "Snapshot"; }
  70. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  71. {
  72. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  73. pTransactionContext->SendAnswer(Error);
  74. }
  75. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  76. {
  77. ErrorCodeEnum Error = __OnClose(Error_Succeed);
  78. pTransactionContext->SendAnswer(Error);
  79. }
  80. ErrorCodeEnum __OnStart(ErrorCodeEnum preOperationError)
  81. {
  82. if (preOperationError != Error_Succeed) {
  83. return preOperationError;
  84. }
  85. //is Pad Version
  86. m_eDeviceType = RvcGetDeviceType();
  87. if (eStand1SPlusType == m_eDeviceType)
  88. {
  89. m_video_env_q = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
  90. }
  91. else
  92. { // == 2
  93. m_video_env_q = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
  94. m_video_opt_q = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_SNAPSHOT_QUEUE);
  95. }
  96. CSmartPointer<IEntityFunction> spFunc = GetFunction();
  97. int i = 0;
  98. m_arrListener.Init(4);
  99. spFunc->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENV);
  100. spFunc->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_OPT);
  101. spFunc->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENVOPT);
  102. spFunc->SubscribeLog(m_arrListener[i++], this, Log_Event, Severity_None, Error_IgnoreAll, LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS,NULL,false);
  103. return Error_Succeed;
  104. }
  105. void OnStarted()
  106. {
  107. m_pChannelClient = new ChannelClient(this);
  108. if (Error_Succeed == ConnectAssistChannel()) {
  109. m_bConnectAssist = TRUE;
  110. }
  111. else {
  112. GetFunction()->SetTimer(2, this, 3370);
  113. }
  114. }
  115. ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError)
  116. {
  117. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  118. for (int i = 0; i < m_arrListener.GetCount(); ++i)
  119. {
  120. spFunction->UnsubscribeLog(m_arrListener[i]);
  121. }
  122. if (preOperationError != Error_Succeed)
  123. return preOperationError;
  124. if (m_pChannelClient) {
  125. m_pChannelClient->GetFunction()->CloseSession();
  126. m_pChannelClient = NULL;
  127. }
  128. if (NULL != m_video_env_q) {
  129. delete m_video_env_q;
  130. m_video_env_q = NULL;
  131. }
  132. if (NULL != m_video_opt_q) {
  133. delete m_video_opt_q;
  134. m_video_opt_q = NULL;
  135. }
  136. return Error_Succeed;
  137. }
  138. void WriteStamp(video_frame *srcfrm)
  139. {
  140. CImageFrame frm;
  141. frm.data = srcfrm->data[0];
  142. frm.width = srcfrm->width;
  143. frm.height = srcfrm->height;
  144. frm.framesize = srcfrm->width * srcfrm->height * 3;
  145. if (!SnapShot(&frm)) {
  146. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("write stamp failed!");
  147. }
  148. }
  149. void Capture(int rc)
  150. {
  151. video_frame *frm = NULL;
  152. if((rc == 0)||(rc==Error_TimeOut))
  153. {
  154. if (m_video_env_q && m_video_opt_q)
  155. {
  156. int combine_width = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
  157. int combine_height = REC_COMMON_VIDEO_SNAPSHOT_WIDTH + REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
  158. frm = video_frame_new(combine_width, combine_height, VIDEO_FORMAT_RGB24);
  159. video_frame_fill_black(frm);
  160. // get env snapshot
  161. {
  162. video_frame tmp_frm = {0};
  163. tmp_frm.data[0] = frm->data[0];
  164. tmp_frm.linesize[0] = frm->linesize[0];
  165. tmp_frm.format = frm->format;
  166. tmp_frm.width = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
  167. tmp_frm.height = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
  168. DWORD time = m_video_env_q->GetLastFrameTime();
  169. if ((y2k_time_now() - time) < ENV_CAP_TIMEOUT)
  170. {
  171. m_video_env_q->GetVideo2(&tmp_frm, 0);
  172. }
  173. else
  174. {
  175. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("the env snapshot videoqueque image timeout!");
  176. }
  177. }
  178. // get opt snapshot
  179. {
  180. video_frame tmp_frm = {0};
  181. tmp_frm.data[0] = frm->data[0] + frm->linesize[0] * REC_COMMON_VIDEO_SNAPSHOT_HEIGHT+(REC_COMMON_VIDEO_SNAPSHOT_WIDTH-REC_COMMON_VIDEO_SNAPSHOT_HEIGHT)/2*3;
  182. tmp_frm.linesize[0] = frm->linesize[0];
  183. tmp_frm.format = frm->format;
  184. tmp_frm.width = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
  185. tmp_frm.height = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
  186. DWORD time = m_video_opt_q->GetLastFrameTime();
  187. if ((y2k_time_now() - time) < OPT_CAP_TIMEOUT)
  188. {
  189. m_video_opt_q->GetVideo2(&tmp_frm, 0);
  190. }
  191. else
  192. {
  193. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("the opt snapshot video queque image timeout!");
  194. }
  195. }
  196. }
  197. else if (m_video_env_q)
  198. {
  199. frm = video_frame_new(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_HEIGHT, VIDEO_FORMAT_RGB24);
  200. video_frame_fill_black(frm);
  201. video_frame tmp_frm = {0};
  202. tmp_frm.data[0] = frm->data[0];
  203. tmp_frm.linesize[0] = frm->linesize[0];
  204. tmp_frm.format = frm->format;
  205. tmp_frm.width = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
  206. tmp_frm.height = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
  207. DWORD time = m_video_env_q->GetLastFrameTime();
  208. if ((y2k_time_now() - time) < ENV_CAP_TIMEOUT)
  209. {
  210. m_video_env_q->GetVideo2(&tmp_frm, 0);
  211. }
  212. else
  213. {
  214. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("the env snapshot video queque image timeout!");
  215. }
  216. }
  217. else
  218. {
  219. //assert(0); // not go here
  220. rc = -1;
  221. }
  222. #ifndef RVC_OS_WIN
  223. {
  224. int size = frm->linesize[0] * frm->height;
  225. RotationDown(frm->data[0], frm->width, REC_COMMON_VIDEO_SNAPSHOT_HEIGHT, 3);
  226. }
  227. #endif
  228. }
  229. jpeg2k_coded_image codec_image = {0};
  230. if((rc == 0)||(rc==Error_TimeOut))
  231. {
  232. WriteStamp(frm);
  233. jpeg2k_raw_image raw_image;
  234. raw_image.data = frm->data[0];
  235. raw_image.width = frm->width;
  236. raw_image.height = frm->height;
  237. raw_image.len = raw_image.width * raw_image.height * 3;
  238. rc = jpeg2k_encode(&raw_image, &codec_image, 10);
  239. }
  240. if ((rc == 0)||(rc==Error_TimeOut))
  241. {
  242. ChannelService_Send_Info Info;
  243. Info.compress = false;
  244. Info.encrypt = false;
  245. Info.type = ACM_TYPE_PHT;
  246. Info.id = 0;
  247. Info.sub_type = ACM_PHT_ANS | ACM_PHT_SNAPSHOT;
  248. Info.data.m_pData = (char*)codec_image.data;
  249. Info.data.m_iLength = codec_image.len;
  250. (*m_pChannelClient)(EntityResource::getLink().upgradeLink())->Send(Info);
  251. jpeg2k_encode_free(&codec_image);
  252. }
  253. else
  254. {
  255. ChannelService_Send_Info Info;
  256. Info.compress = false;
  257. Info.encrypt = false;
  258. Info.type = ACM_TYPE_PHT;
  259. Info.id = 0;
  260. Info.sub_type = ACM_PHT_ANS | ACM_PHT_SNAPSHOT;
  261. Info.data.m_pData = (char *)&rc;
  262. Info.data.m_iLength = sizeof(rc);
  263. (*m_pChannelClient)(EntityResource::getLink().upgradeLink())->Send(Info);
  264. }
  265. if (frm)
  266. video_frame_delete(frm);
  267. }
  268. ErrorCodeEnum StartCapture(int id)
  269. {
  270. #if defined(RVC_OS_LINUX)
  271. if (eStand1SPlusType == m_eDeviceType) {
  272. m_video_env_q = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
  273. } else { // == 2
  274. m_video_env_q = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
  275. m_video_opt_q = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_SNAPSHOT_QUEUE);
  276. }
  277. #endif //RVC_OS_LINUX
  278. if (!m_video_env_q && !m_video_opt_q) {
  279. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("cannot start capture!"); // never go here
  280. return Error_Unexpect;
  281. }
  282. if (m_dwCapture) {
  283. return Error_Duplication;
  284. }
  285. //触发对应的摄像头进行图像采集
  286. if (m_video_env_q && m_video_opt_q)
  287. {
  288. LogEvent(Severity_Middle, MOD_EVENT_SNAPSHOT_START_CAPTURE_ENVOPT, "agent start capture env and opt camera picture!");
  289. }
  290. else if (m_video_env_q)
  291. {
  292. LogEvent(Severity_Middle, MOD_EVENT_SNAPSHOT_START_CAPTURE_ENV, "agent start capture env camera picture!");
  293. }
  294. else if (m_video_opt_q)
  295. {
  296. LogEvent(Severity_Middle, MOD_EVENT_SNAPSHOT_START_CAPTURE_OPT, "agent start capture opt camera picture!");
  297. }
  298. m_dwCapture = SP::Module::Comm::RVCGetTickCount();
  299. GetFunction()->SetTimer(1, this, 2000);
  300. return Error_Succeed;
  301. }
  302. void StopCapture()
  303. {
  304. GetFunction()->KillTimer(1);
  305. m_dwCapture = 0;
  306. }
  307. DeviceTypeEnum RvcGetDeviceType()
  308. {
  309. DeviceTypeEnum eType = eStand2sType;
  310. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  311. CSystemStaticInfo stStaticinfo;
  312. spFunction->GetSystemStaticInfo(stStaticinfo);
  313. if (_stricmp(stStaticinfo.strMachineType, "RVC.Stand1SPlus") == 0) {
  314. eType = eStand1SPlusType;
  315. }
  316. else if (stricmp(stStaticinfo.strMachineType, "RVC.CardStore") == 0 || stricmp(stStaticinfo.strMachineType, "RVC.CardPrinter") == 0) {
  317. eType = eCardStore;
  318. }
  319. else {
  320. eType = eStand2sType;
  321. }
  322. if (eType >= 0 && eType < sizeof(Device_Type_Table) / sizeof(char*)) {
  323. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("device type is %s.", Device_Type_Table[eType]);
  324. }
  325. return eType;
  326. }
  327. virtual void OnTimeout(DWORD dwTimerID)
  328. {
  329. if (1 == dwTimerID) {
  330. Capture(Error_TimeOut);
  331. GetFunction()->KillTimer(dwTimerID);
  332. m_dwCapture = 0;
  333. }
  334. else if (2 == dwTimerID) {
  335. if (FALSE == m_bConnectAssist) {
  336. if (Error_Succeed == ConnectAssistChannel()) {
  337. m_bConnectAssist = TRUE;
  338. }
  339. }
  340. if (TRUE == m_bConnectAssist) {
  341. GetFunction()->KillTimer(2);
  342. }
  343. }
  344. }
  345. virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  346. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  347. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage, const linkContext& pLinkInfo)
  348. {
  349. if (dwUserCode == MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENVOPT ||
  350. dwUserCode == MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_OPT ||
  351. dwUserCode == MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENV)
  352. {
  353. DWORD dwNow = SP::Module::Comm::RVCGetTickCount();
  354. Capture(Error_Succeed);
  355. StopCapture();
  356. }
  357. else if (LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS == dwUserCode)
  358. {
  359. Sleep(975);
  360. if (m_pChannelClient != NULL){
  361. m_pChannelClient->GetFunction()->CloseSession();
  362. m_pChannelClient = NULL;
  363. m_bConnectAssist = FALSE;
  364. }
  365. if (Error_Succeed == ConnectAssistChannel()) {
  366. m_bConnectAssist = TRUE;
  367. }
  368. else {
  369. GetFunction()->SetTimer(2, this, 3370);
  370. }
  371. }
  372. }
  373. private:
  374. // we use root.ini Video section config to decide camera count
  375. ErrorCodeEnum DecideCameraCount(int &nCount)
  376. {
  377. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  378. CSmartPointer<IConfigInfo> spConfig;
  379. ErrorCodeEnum Error = spFunction->OpenConfig(Config_Root, spConfig);
  380. if (Error == Error_Succeed)
  381. {
  382. CSimpleStringA strVideoEnv;
  383. CSimpleStringA strVideoOpt;
  384. SpIniMappingTable table;
  385. nCount = 0;
  386. table.AddEntryString("Video", "EnvCamera", strVideoEnv, "$");
  387. table.AddEntryString("Video", "OptCamera", strVideoOpt, "$");
  388. Error = table.Load(spConfig);
  389. if (Error == Error_Succeed)
  390. {
  391. if (strVideoEnv.GetLength() > 1)
  392. nCount++;
  393. if (strVideoOpt.GetLength() > 1)
  394. nCount++;
  395. }
  396. }
  397. return Error;
  398. }
  399. ErrorCodeEnum ConnectAssistChannel()
  400. {
  401. if (m_pChannelClient == NULL){
  402. m_pChannelClient = new ChannelClient(this);
  403. }
  404. ErrorCodeEnum Error = m_pChannelClient->Connect();
  405. if (Error_Succeed != Error){
  406. m_pChannelClient = NULL;
  407. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("AssistChannelClient connect fail!");
  408. return Error;
  409. }
  410. if (Error_Succeed == Error){
  411. ChannelService_BeginRecv_Sub Sub;
  412. Sub.type = ACM_TYPE_PHT;
  413. Error = (*m_pChannelClient)(EntityResource::getLink().upgradeLink())->BeginRecv(Sub);
  414. if (Error_Succeed != Error){
  415. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("BeginRecv ACM_TYPE_PHT fail!");
  416. m_pChannelClient->GetFunction()->CloseSession();
  417. m_pChannelClient = NULL;
  418. return Error;
  419. }
  420. }
  421. return Error;
  422. }
  423. private:
  424. Clibvideoqueue *m_video_env_q;
  425. Clibvideoqueue *m_video_opt_q;
  426. ChannelClient *m_pChannelClient;
  427. CUUID m_uuidCaptureTimer;
  428. DWORD m_dwCapture;
  429. DeviceTypeEnum m_eDeviceType;
  430. CAutoArray<CUUID> m_arrListener;
  431. BOOL m_bConnectAssist;
  432. };
  433. void ChannelClient::OnMessage( ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer<IReleasable> pData )
  434. {
  435. if (Error == Error_Succeed) {
  436. CPhotoCaptureEntity *pEntity = static_cast<CPhotoCaptureEntity*>(m_pEntityBase);
  437. int cat = ACM_PHT_CAT(Msg.sub_type);
  438. if (cat == ACM_PHT_REQ)
  439. {
  440. Error = pEntity->StartCapture(Msg.id);
  441. if (Error)
  442. {
  443. pEntity->Capture(Error);
  444. }
  445. }
  446. else
  447. {
  448. _ASSERT(0);
  449. }
  450. }
  451. }
  452. ChannelClient::ChannelClient( CPhotoCaptureEntity *pEntity ) : ChannelService_ClientBase(pEntity)
  453. {
  454. }
  455. SP_BEGIN_ENTITY_MAP()
  456. SP_ENTITY(CPhotoCaptureEntity)
  457. SP_END_ENTITY_MAP()