mod_snapshot.cpp 15 KB

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