mod_snapshot.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. if (NULL != m_video_env_q) {
  171. delete m_video_env_q;
  172. m_video_env_q = NULL;
  173. }
  174. if (NULL != m_video_opt_q) {
  175. delete m_video_opt_q;
  176. m_video_opt_q = NULL;
  177. }
  178. return Error_Succeed;
  179. }
  180. void WriteStamp(video_frame *srcfrm)
  181. {
  182. #ifdef RVC_OS_WIN
  183. __try {
  184. CImageFrame frm;
  185. frm.data = srcfrm->data[0];
  186. frm.width = srcfrm->width;
  187. frm.height = srcfrm->height;
  188. frm.framesize = srcfrm->width * srcfrm->height * 3;
  189. if (!SnapShot(&frm)) {
  190. Dbg("write stamp failed!");
  191. }
  192. }
  193. __except (EXCEPTION_EXECUTE_HANDLER) {
  194. Dbg("snapshot throw windows exception!");
  195. }
  196. #else
  197. CImageFrame frm;
  198. frm.data = srcfrm->data[0];
  199. frm.width = srcfrm->width;
  200. frm.height = srcfrm->height;
  201. frm.framesize = srcfrm->width * srcfrm->height * 3;
  202. if (!SnapShot(&frm)) {
  203. Dbg("write stamp failed!");
  204. }
  205. #endif
  206. }
  207. void Capture(int rc)
  208. {
  209. video_frame *frm = NULL;
  210. if((rc == 0)||(rc==Error_TimeOut))
  211. {
  212. if (m_video_env_q && m_video_opt_q)
  213. {
  214. int combine_width = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
  215. int combine_height = REC_COMMON_VIDEO_SNAPSHOT_WIDTH + REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
  216. frm = video_frame_new(combine_width, combine_height, VIDEO_FORMAT_RGB24);
  217. video_frame_fill_black(frm);
  218. // get env snapshot
  219. {
  220. video_frame tmp_frm = {0};
  221. tmp_frm.data[0] = frm->data[0];
  222. tmp_frm.linesize[0] = frm->linesize[0];
  223. tmp_frm.format = frm->format;
  224. tmp_frm.width = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
  225. tmp_frm.height = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
  226. DWORD time = m_video_env_q->GetLastFrameTime();
  227. y2k_time_t nowtime = y2k_time_now();
  228. Dbg("%s:%d env now time is %d, get frame time is %d, time interval is %ds.", __FUNCTION__, __LINE__, nowtime, time, nowtime - time);
  229. if ((nowtime - time) < ENV_CAP_TIMEOUT)
  230. {
  231. m_video_env_q->GetVideo2(&tmp_frm, 0);
  232. }
  233. else
  234. {
  235. Dbg("the env snapshot videoqueque image timeout!");
  236. }
  237. }
  238. // get opt snapshot
  239. {
  240. video_frame tmp_frm = {0};
  241. 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;
  242. tmp_frm.linesize[0] = frm->linesize[0];
  243. tmp_frm.format = frm->format;
  244. tmp_frm.width = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
  245. tmp_frm.height = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
  246. DWORD time = m_video_opt_q->GetLastFrameTime();
  247. y2k_time_t nowtime = y2k_time_now();
  248. Dbg("%s:%d opt now time is %d, get frame time is %d, time interval is %ds.", __FUNCTION__, __LINE__, nowtime, time, nowtime - time);
  249. if ((nowtime - time) < OPT_CAP_TIMEOUT)
  250. {
  251. m_video_opt_q->GetVideo2(&tmp_frm, 0);
  252. }
  253. else
  254. {
  255. Dbg("the opt snapshot videoqueque image timeout!");
  256. }
  257. }
  258. }
  259. else if (m_video_env_q)
  260. {
  261. frm = video_frame_new(REC_COMMON_VIDEO_SNAPSHOT_WIDTH, REC_COMMON_VIDEO_SNAPSHOT_HEIGHT, VIDEO_FORMAT_RGB24);
  262. video_frame_fill_black(frm);
  263. video_frame tmp_frm = {0};
  264. tmp_frm.data[0] = frm->data[0];
  265. tmp_frm.linesize[0] = frm->linesize[0];
  266. tmp_frm.format = frm->format;
  267. tmp_frm.width = REC_COMMON_VIDEO_SNAPSHOT_WIDTH;
  268. tmp_frm.height = REC_COMMON_VIDEO_SNAPSHOT_HEIGHT;
  269. DWORD time = m_video_env_q->GetLastFrameTime();
  270. y2k_time_t nowtime = y2k_time_now();
  271. Dbg("%s:%d env now time is %d, get frame time is %d, time interval is %ds.", __FUNCTION__, __LINE__, nowtime, time, nowtime - time);
  272. if ((nowtime - time) < ENV_CAP_TIMEOUT)
  273. {
  274. m_video_env_q->GetVideo2(&tmp_frm, 0);
  275. }
  276. else
  277. {
  278. Dbg("the env snapshot videoqueque image timeout!");
  279. }
  280. }
  281. else
  282. {
  283. //assert(0); // not go here
  284. rc = -1;
  285. }
  286. #ifndef RVC_OS_WIN
  287. {
  288. //linux需翻转环境摄像头图像
  289. int size = frm->linesize[0] * frm->height;
  290. Dbg("size = %d, width= %d height= %d", size, frm->width, frm->height);
  291. RotationDown(frm->data[0], frm->width, REC_COMMON_VIDEO_SNAPSHOT_HEIGHT, 3);
  292. }
  293. #endif
  294. }
  295. jpeg2k_coded_image codec_image = {0};
  296. if((rc == 0)||(rc==Error_TimeOut))
  297. {
  298. WriteStamp(frm);
  299. jpeg2k_raw_image raw_image;
  300. raw_image.data = frm->data[0];
  301. raw_image.width = frm->width;
  302. raw_image.height = frm->height;
  303. raw_image.len = raw_image.width * raw_image.height * 3;
  304. rc = jpeg2k_encode(&raw_image, &codec_image, 10);
  305. }
  306. if ((rc == 0)||(rc==Error_TimeOut))
  307. {
  308. ChannelService_Send_Info Info;
  309. Info.compress = false;
  310. Info.encrypt = false;
  311. Info.type = ACM_TYPE_PHT;
  312. Info.id = 0;
  313. Info.sub_type = ACM_PHT_ANS | ACM_PHT_SNAPSHOT;
  314. Info.data.m_pData = (char *)codec_image.data;
  315. Info.data.m_iLength = codec_image.len;
  316. m_pChannelClient->Send(Info);
  317. jpeg2k_encode_free(&codec_image);
  318. }
  319. else
  320. {
  321. ChannelService_Send_Info Info;
  322. Info.compress = false;
  323. Info.encrypt = false;
  324. Info.type = ACM_TYPE_PHT;
  325. Info.id = 0;
  326. Info.sub_type = ACM_PHT_ANS | ACM_PHT_SNAPSHOT;
  327. Info.data.m_pData = (char *)&rc;
  328. Info.data.m_iLength = sizeof(rc);
  329. m_pChannelClient->Send(Info);
  330. }
  331. #if 0
  332. video_frame_save_bmpfile("snapshot_abc.bmp", frm);
  333. //video_frame_save_bmpfile("d:\\ab.bmp", &frm);
  334. #endif
  335. if (frm)
  336. video_frame_delete(frm);
  337. }
  338. ErrorCodeEnum StartCapture(int id)
  339. {
  340. if ((ePadtype == m_eDeviceType) || (eMobilePadType == m_eDeviceType) || (eDesk2SType == m_eDeviceType)){
  341. m_video_env_q = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
  342. }
  343. else{ // == 2
  344. m_video_env_q = new Clibvideoqueue(REC_COMMON_VIDEO_ENV_SHM_SNAPSHOT_QUEUE);
  345. m_video_opt_q = new Clibvideoqueue(REC_COMMON_VIDEO_OPT_SHM_SNAPSHOT_QUEUE);
  346. }
  347. if (!m_video_env_q && !m_video_opt_q) {
  348. Dbg("cannot start capture!"); // never go here
  349. return Error_Unexpect;
  350. }
  351. if (m_dwCapture) {
  352. LOG_TRACE("already start capture!");
  353. return Error_Duplication;
  354. }
  355. //触发对应的摄像头进行图像采集
  356. if (m_video_env_q && m_video_opt_q)
  357. {
  358. LogEvent(Severity_Middle, MOD_EVENT_SNAPSHOT_START_CAPTURE_ENVOPT, "agent start capture env and opt camera picture!");
  359. }
  360. else if (m_video_env_q)
  361. {
  362. LogEvent(Severity_Middle, MOD_EVENT_SNAPSHOT_START_CAPTURE_ENV, "agent start capture env camera picture!");
  363. }
  364. else if (m_video_opt_q)
  365. {
  366. LogEvent(Severity_Middle, MOD_EVENT_SNAPSHOT_START_CAPTURE_OPT, "agent start capture opt camera picture!");
  367. }
  368. m_dwCapture = SP::Module::Comm::RVCGetTickCount();
  369. GetFunction()->SetTimer(1, this, 2000);
  370. return Error_Succeed;
  371. }
  372. void StopCapture()
  373. {
  374. GetFunction()->KillTimer(1);
  375. m_dwCapture = 0;
  376. }
  377. virtual void OnTimeout(DWORD dwTimerID)
  378. {
  379. Capture(Error_TimeOut);
  380. Dbg("timeout!");
  381. GetFunction()->KillTimer(dwTimerID);
  382. m_dwCapture = 0;
  383. }
  384. virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  385. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  386. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage)
  387. {
  388. if (dwUserCode == MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENVOPT ||
  389. dwUserCode == MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_OPT ||
  390. dwUserCode == MOD_EVENT_MEDIACONTROLLER_FINISHED_CAPTURE_ENV)
  391. {
  392. DWORD dwNow = SP::Module::Comm::RVCGetTickCount();
  393. LOG_TRACE("capture duration:%dms", dwNow - m_dwCapture);
  394. Capture(Error_Succeed);
  395. StopCapture();
  396. }
  397. else if (dwUserCode == LOG_EVT_SELFCHECK_ASSISTANTCHANNEL_IDLE)
  398. {
  399. Dbg("recv LOG_EVT_SELFCHECK_ASSISTANTCHANNEL_IDLE");
  400. if (m_pChannelClient != NULL)
  401. {
  402. m_pChannelClient->GetFunction()->CloseSession();
  403. m_pChannelClient = NULL;
  404. Dbg("Close AssistChannel Session ");
  405. }
  406. if (m_pChannelClient == NULL)
  407. {
  408. Dbg("ReConnection AssistChannel Session");
  409. m_pChannelClient = new ChannelClient(this);
  410. ErrorCodeEnum Error = m_pChannelClient->Connect();
  411. if (Error != Error_Succeed)
  412. {
  413. m_pChannelClient->SafeDelete();
  414. m_pChannelClient = NULL;
  415. Dbg("AssistChannelClient connect fail!");
  416. }
  417. if (Error == Error_Succeed)
  418. {
  419. ChannelService_BeginRecv_Sub Sub;
  420. Sub.type = ACM_TYPE_PHT;
  421. Error = m_pChannelClient->BeginRecv(Sub);
  422. if (Error != Error_Succeed)
  423. {
  424. m_pChannelClient->GetFunction()->CloseSession();
  425. m_pChannelClient = NULL;
  426. }
  427. }
  428. }
  429. }
  430. }
  431. private:
  432. // we use root.ini Video section config to decide camera count
  433. ErrorCodeEnum DecideCameraCount(int &nCount)
  434. {
  435. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  436. CSmartPointer<IConfigInfo> spConfig;
  437. ErrorCodeEnum Error = spFunction->OpenConfig(Config_Root, spConfig);
  438. if (Error == Error_Succeed)
  439. {
  440. CSimpleStringA strVideoEnv;
  441. CSimpleStringA strVideoOpt;
  442. SpIniMappingTable table;
  443. nCount = 0;
  444. table.AddEntryString("Video", "EnvCamera", strVideoEnv, "$");
  445. table.AddEntryString("Video", "OptCamera", strVideoOpt, "$");
  446. Error = table.Load(spConfig);
  447. if (Error == Error_Succeed)
  448. {
  449. if (strVideoEnv.GetLength() > 1)
  450. nCount++;
  451. if (strVideoOpt.GetLength() > 1)
  452. nCount++;
  453. }
  454. }
  455. return Error;
  456. }
  457. private:
  458. Clibvideoqueue *m_video_env_q;
  459. Clibvideoqueue *m_video_opt_q;
  460. ChannelClient *m_pChannelClient;
  461. CUUID m_uuidCaptureTimer;
  462. DWORD m_dwCapture;
  463. DeviceTypeEnum m_eDeviceType;
  464. CAutoArray<CUUID> m_arrListener;
  465. };
  466. void ChannelClient::OnMessage( ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer<IReleasable> pData )
  467. {
  468. LOG_FUNCTION();
  469. if (Error == Error_Succeed) {
  470. CPhotoCaptureEntity *pEntity = static_cast<CPhotoCaptureEntity*>(m_pEntityBase);
  471. int cat = ACM_PHT_CAT(Msg.sub_type);
  472. if (cat == ACM_PHT_REQ)
  473. {
  474. Error = pEntity->StartCapture(Msg.id);
  475. if (Error)
  476. {
  477. pEntity->Capture(Error);
  478. }
  479. }
  480. else
  481. {
  482. _ASSERT(0);
  483. }
  484. }
  485. }
  486. ChannelClient::ChannelClient( CPhotoCaptureEntity *pEntity ) : ChannelService_ClientBase(pEntity)
  487. {
  488. }
  489. SP_BEGIN_ENTITY_MAP()
  490. SP_ENTITY(CPhotoCaptureEntity)
  491. SP_END_ENTITY_MAP()