mod_assistantchannel.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #include "stdafx.h"
  2. #include "mod_assistantchannel.h"
  3. #include "EventCode.h"
  4. static const char *__states[] = {
  5. "Idle", "Connecting", "Connected", "Closing"
  6. };
  7. inline const char *state2str(int state)
  8. {
  9. return __states[state];
  10. }
  11. static __inline unsigned int hash32_buf(const void *bf, size_t len, unsigned int hash)
  12. {
  13. const unsigned char *s = (const unsigned char*)bf;
  14. while (len-- != 0) /* "nemesi": k=257, r=r*257 */
  15. hash = hash * 257 + *s++;
  16. return (hash * 257);
  17. }
  18. static int MakeDesc(DWORD eScreen,DeviceTypeEnum eDevicetype)
  19. {
  20. if (eScreen == 1)
  21. {
  22. return media_desc_encode(
  23. ACM_VIDEO_MODE_SQUARE, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_BASELINE,
  24. ACM_VIDEO_MODE_QVGA, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_BASELINE);
  25. }
  26. else if (eScreen == 2)
  27. {
  28. return media_desc_encode(
  29. ACM_VIDEO_MODE_SQUARE, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_BASELINE,
  30. ACM_VIDEO_MODE_VGA, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_BASELINE);
  31. }
  32. else
  33. {
  34. assert(0);
  35. }
  36. return 0;
  37. }
  38. static void __on_recv_pkt(bizchan_t *chan, int type, int sub_type, int id, const char *pkt, int pkt_size, void *user_data)
  39. {
  40. CBizChannelEntity *pThis = static_cast<CBizChannelEntity *>(user_data);
  41. pThis->_on_recv_pkt(type, sub_type, id, pkt, pkt_size);
  42. }
  43. static void __on_connect(bizchan_t *chan, int error, const char *remote_ip, int remote_video_rtp, int remote_video_desc, const char *remote_client_id, void *user_data)
  44. {
  45. CBizChannelEntity *pThis = static_cast<CBizChannelEntity *>(user_data);
  46. pThis->_on_connect(error, remote_ip, remote_video_rtp, remote_video_desc);
  47. }
  48. static void __on_close(bizchan_t *chan, void *user_data)
  49. {
  50. CBizChannelEntity *pThis = static_cast<CBizChannelEntity *>(user_data);
  51. pThis->_on_close();
  52. }
  53. static void __on_destroy(bizchan_t *chan, void *user_data)
  54. {
  55. }
  56. static void __dbg(void *user_data, const char *fmt, va_list arg)
  57. {
  58. //vDbg(fmt, arg);
  59. #if defined(RVC_OS_WIN)
  60. int n = _vscprintf(fmt, arg);
  61. #else
  62. int n = _scprintf(fmt, arg);
  63. #endif //RVC_OS_WIN
  64. if (n >= MAX_PATH) {
  65. char* buf = (char*)malloc((size_t)(n + 1));
  66. vsnprintf(buf, n + 1, fmt, arg);
  67. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", buf);
  68. free(buf);
  69. }
  70. else {
  71. char strlog[MAX_PATH] = { 0 };
  72. #if defined(RVC_OS_WIN)
  73. _vsnprintf(strlog, MAX_PATH, fmt, arg);
  74. #else
  75. vsnprintf(strlog, MAX_PATH, fmt, arg);
  76. #endif //RVC_OS_WIN
  77. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s", strlog);
  78. }
  79. }
  80. void CBizChannelEntity::OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  81. {
  82. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  83. pTransactionContext->SendAnswer(Error);
  84. }
  85. void CBizChannelEntity::OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  86. {
  87. ErrorCodeEnum Error = __OnClose(Error_Succeed);
  88. pTransactionContext->SendAnswer(Error);
  89. }
  90. ErrorCodeEnum CBizChannelEntity::__OnStart(ErrorCodeEnum preOperationError)
  91. {
  92. //MessageBoxA(0,0,0,0);
  93. bizchan_lib_init();
  94. m_eState = eChannelState_Idle;
  95. m_pChan = NULL;
  96. m_eDeviceType = RvcGetDeviceType();
  97. ListEntry_InitHead(&m_stateList);
  98. ListEntry_InitHead(&m_rxpktList);
  99. return Error_Succeed;
  100. }
  101. void CBizChannelEntity::OnStarted()
  102. {
  103. LogEvent(Severity_Middle, LOG_EVT_MOD_ASSISCHAN_STARTED_SUCCESS, "assistant channel started successfully.");
  104. }
  105. DeviceTypeEnum CBizChannelEntity::RvcGetDeviceType()
  106. {
  107. DeviceTypeEnum eType = eStand2sType;
  108. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  109. CSystemStaticInfo stStaticinfo;
  110. spFunction->GetSystemStaticInfo(stStaticinfo);
  111. if (_stricmp(stStaticinfo.strMachineType, "RVC.Stand1SPlus") == 0) {
  112. eType = eStand1SPlusType;
  113. }
  114. else if (stricmp(stStaticinfo.strMachineType, "RVC.CardStore") == 0 || stricmp(stStaticinfo.strMachineType, "RVC.CardPrinter") == 0) {
  115. eType = eCardStore;
  116. }
  117. else{
  118. eType = eStand2sType;
  119. }
  120. if (eType >= 0 && eType < sizeof(Device_Type_Table)/sizeof(char*)){
  121. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("device type is %s.", Device_Type_Table[eType]);
  122. }
  123. return eType;
  124. }
  125. ErrorCodeEnum CBizChannelEntity::__OnClose(ErrorCodeEnum preOperationError)
  126. {
  127. //.....
  128. bizchan_lib_term();
  129. return Error_Succeed;
  130. }
  131. CServerSessionBase* CBizChannelEntity::OnNewSession(const char* pszRemoteEntityName, const char * pszClass)
  132. {
  133. return new ChannelServiceSession(this, m_id_seq++);
  134. }
  135. ErrorCodeEnum CBizChannelEntity::Connect(const char *ip, int port)
  136. {
  137. ErrorCodeEnum Error = Error_Succeed;
  138. if (m_eState == eChannelState_Idle)
  139. {
  140. CSystemStaticInfo Info;
  141. bizchan_config_t config = {0};
  142. bizchan_callback_t cb = {0};
  143. GetFunction()->GetSystemStaticInfo(Info);
  144. config.proxy_server = const_cast<char*>(ip);
  145. config.proxy_server_port = port;
  146. config.session_id = const_cast<char*>(((const char*)Info.strTerminalID));
  147. config.client_id = "";
  148. config.call_no = "";
  149. config.video.desc = MakeDesc(Info.eScreen,m_eDeviceType);
  150. config.video.rtp_port = REC_COMMON_VIDEO_PORT;
  151. cb.user_data = this;
  152. cb.on_close = &__on_close;
  153. cb.on_connect = &__on_connect;
  154. cb.on_destroy = &__on_destroy;
  155. cb.on_recv_pkt = &__on_recv_pkt;
  156. cb.dbg = &__dbg;
  157. int rc = bizchan_create(&config, &cb, &m_pChan);
  158. if (rc == 0) {
  159. rc = bizchan_start_connect(m_pChan);
  160. }
  161. if (rc != 0) {
  162. Error = Error_Unexpect;
  163. }
  164. else {
  165. ChangeState(eChannelState_Connecting);
  166. }
  167. }
  168. else
  169. {
  170. Error = Error_InvalidState;
  171. }
  172. return Error;
  173. }
  174. ErrorCodeEnum CBizChannelEntity::Close()
  175. {
  176. ErrorCodeEnum Error = Error_Succeed;
  177. if (m_eState == eChannelState_Connecting || m_eState == eChannelState_Connected) {
  178. int rc = bizchan_start_close(m_pChan);
  179. if (rc == 0) {
  180. ChangeState(eChannelState_Closing);
  181. } else {
  182. Error = Error_Unexpect;
  183. }
  184. } else {
  185. Error = Error_InvalidState;
  186. }
  187. return Error_Succeed;
  188. }
  189. ErrorCodeEnum CBizChannelEntity::RegisterState(int id, SpSubscribeContext<ChannelService_BeginState_Sub, ChannelService_State_Info>::Pointer ctx)
  190. {
  191. state_entry *pos;
  192. ListEntry_ForEach(pos, &m_stateList, state_entry, entry)
  193. {
  194. if (pos->id == id)
  195. {
  196. return Error_AlreadyExist;
  197. }
  198. }
  199. pos = new state_entry();
  200. pos->ctx = ctx;
  201. pos->id = id;
  202. ListEntry_AddTail(&m_stateList, &pos->entry);
  203. return Error_Succeed;
  204. }
  205. ErrorCodeEnum CBizChannelEntity::RegisterRxPkt(int id, SpSubscribeContext<ChannelService_BeginRecv_Sub, ChannelService_Packet_Info>::Pointer ctx)
  206. {
  207. rxpkt_entry *pos;
  208. ListEntry_ForEach(pos, &m_rxpktList, rxpkt_entry, entry)
  209. {
  210. if ((pos->id == id)&&(pos->ctx->Req.type == ctx->Req.type))
  211. {
  212. return Error_AlreadyExist;
  213. }
  214. }
  215. pos = new rxpkt_entry();
  216. pos->id = id;
  217. pos->ctx = ctx;
  218. ListEntry_AddTail(&m_rxpktList, &pos->entry);
  219. return Error_Succeed;
  220. }
  221. void CBizChannelEntity::UnregisterState(int id)
  222. {
  223. state_entry *pos,*n;
  224. ListEntry_ForEachSafe(pos,n, &m_stateList, state_entry, entry) {
  225. if (pos->id == id) {
  226. ListEntry_DeleteNode(&pos->entry);
  227. delete pos;
  228. }
  229. }
  230. }
  231. void CBizChannelEntity::UnregisterRxpkt(int id)
  232. {
  233. rxpkt_entry *pos,*n;
  234. //ListEntry_ForEach(pos, &m_rxpktList, rxpkt_entry, entry)
  235. ListEntry_ForEachSafe(pos, n, &m_rxpktList, rxpkt_entry, entry)
  236. {
  237. if((pos->id == id))
  238. {
  239. ListEntry_DeleteNode(&pos->entry);
  240. delete pos;
  241. }
  242. }
  243. }
  244. ErrorCodeEnum CBizChannelEntity::Send(int type, bool compress, bool encrypt, int sub_type, int id, CBlob &data)
  245. {
  246. if (m_eState == eChannelState_Connected) {
  247. //LOG_TRACE("tx pkt, %d bytes, type = %d, compress = %d, encrypt = %d, sub_type = %d, id = %d, hash=%d", data.m_iLength, type, !!compress, !!encrypt, sub_type, id, hash32_buf(data.m_pData, data.m_iLength, 0));
  248. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("tx pkt, %d bytes, type = %d, compress = %d, encrypt = %d, sub_type = %d, id = %d, hash=%d", data.m_iLength, type, !!compress, !!encrypt, sub_type, id, hash32_buf(data.m_pData, data.m_iLength, 0));
  249. int rc = bizchan_post_pkt(m_pChan, type, compress, encrypt, sub_type, id, (const char*)data.m_pData, data.m_iLength);
  250. return rc == 0 ? Error_Succeed : Error_NetBroken;
  251. }
  252. else
  253. {
  254. return Error_NetBroken;
  255. }
  256. }
  257. void CBizChannelEntity::ChangeState(int new_state, const char *param)
  258. {
  259. if (m_eState != new_state) {
  260. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("change state from %s to %s", state2str(m_eState), state2str(new_state));
  261. m_eState = new_state;
  262. state_entry *pos;
  263. ListEntry_ForEach(pos, &m_stateList, state_entry, entry) {
  264. ChannelService_State_Info State;
  265. State.state = new_state;
  266. State.status = state2str(new_state);
  267. if (param) {
  268. State.param = param;
  269. }
  270. pos->ctx->SendMessage(State);
  271. }
  272. }
  273. }
  274. //void on_recv_pkt(int type, int sub_type, const char *pkt, int pkt_size)
  275. void CBizChannelEntity::on_recv_pkt(int type, int sub_type, int id, CBlob &data)
  276. {
  277. //LOG_TRACE("rx pkt, %d bytes, type = %d, sub_type = %d, id = %d, hash = %d", data.m_iLength, type, sub_type, id, hash32_buf(data.m_pData, data.m_iLength, 0));
  278. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("rx pkt, %d bytes, type = %d, sub_type = %d, id = %d, hash = %d", data.m_iLength, type, sub_type, id, hash32_buf(data.m_pData, data.m_iLength, 0));
  279. rxpkt_entry *pos;
  280. ListEntry_ForEach(pos, &m_rxpktList, rxpkt_entry, entry)
  281. {
  282. if (pos->ctx->Req.type == type)
  283. {
  284. ChannelService_Packet_Info pkt;
  285. pkt.type = type;
  286. pkt.sub_type = sub_type;
  287. pkt.id = id;
  288. //pkt.data.m_bManaged = true;
  289. pkt.data = data;
  290. //data.m_bManaged = false;
  291. pos->ctx->SendMessage(pkt);
  292. //break;
  293. }
  294. }
  295. }
  296. void CBizChannelEntity::on_connect(int error, const char *remote_ip, int remote_video_rtp, int remote_video_desc)
  297. {
  298. if (!error) {
  299. if (m_eState == eChannelState_Connecting) {
  300. CSimpleStringA strValue;
  301. ErrorCodeEnum Error = GetFunction()->GetSysVar("VideoWindowInitializeParam", strValue);
  302. if (Error == Error_Succeed) {
  303. int local_view_x;
  304. int local_view_y;
  305. int local_view_cx;
  306. int local_view_cy;
  307. int remote_view_x;
  308. int remote_view_y;
  309. int remote_view_cx;
  310. int remote_view_cy;
  311. int remote_video_width = 0;
  312. int remote_video_height = 0;
  313. {
  314. CSystemStaticInfo Info;
  315. GetFunction()->GetSystemStaticInfo(Info);
  316. if (Info.eScreen == 1)
  317. {
  318. remote_video_width = REC_COMMON_VIDEO_SSM_AGENT_WIDTH;
  319. remote_video_height = REC_COMMON_VIDEO_SSM_AGENT_HEIGHT;
  320. }
  321. else
  322. {
  323. remote_video_width = REC_COMMON_VIDEO_DSM_AGENT_WIDTH;
  324. remote_video_height = REC_COMMON_VIDEO_DSM_AGENT_HEIGHT;
  325. }
  326. }
  327. ParseVideoViewParam((LPCSTR)strValue, local_view_x, local_view_y, local_view_cx, local_view_cy,
  328. remote_view_x, remote_view_y, remote_view_cx, remote_view_cy);
  329. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Get Video Window Initialize Param:%d,%d,%d,%d %d,%d,%d,%d",local_view_x, local_view_y, local_view_cx, local_view_cy,
  330. //remote_view_x, remote_view_y, remote_view_cx, remote_view_cy);
  331. CSimpleStringA param;
  332. param = BuildVideoDesc(remote_ip, remote_video_rtp, remote_video_width, remote_video_height,
  333. REC_COMMON_VIDEO_FPS, local_view_x, local_view_y, local_view_cx, local_view_cy,
  334. remote_view_x, remote_view_y, remote_view_cx, remote_view_cy);
  335. ChangeState(eChannelState_Connected, (LPCSTR)param);
  336. }
  337. else {
  338. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("get VideoWindowInitializeParam failed!");
  339. bizchan_start_close(m_pChan);
  340. }
  341. }
  342. }
  343. else {
  344. if (m_pChan) {
  345. bizchan_close(m_pChan);
  346. bizchan_destroy(m_pChan);
  347. m_pChan = NULL;
  348. }
  349. ChangeState(eChannelState_Idle);
  350. }
  351. }
  352. void CBizChannelEntity::on_close()
  353. {
  354. if (m_pChan) {
  355. bizchan_close(m_pChan);
  356. bizchan_destroy(m_pChan);
  357. m_pChan = NULL;
  358. }
  359. ChangeState(eChannelState_Idle);
  360. }
  361. void CBizChannelEntity::_on_recv_pkt(int type, int sub_type, int id, const char *pkt, int pkt_size)
  362. {
  363. //LOG_TRACE("_on rx pkt, %d bytes, type = %d, sub_type = %d, id = %d, hash = %d", pkt_size, type, sub_type, id, hash32_buf(pkt, pkt_size, 0));
  364. NotifyOnRecvPkt *task = new NotifyOnRecvPkt();
  365. task->m_pEntity = this;
  366. task->type = type;
  367. task->sub_type = sub_type;
  368. task->id = id;
  369. task->data.m_bManaged = true;
  370. task->data.m_iLength = pkt_size;
  371. if (pkt_size)
  372. {
  373. task->data.m_pData = new char[pkt_size];
  374. memcpy(task->data.m_pData, pkt, pkt_size);
  375. } else {
  376. task->data.m_pData = NULL;
  377. }
  378. GetFunction()->PostEntityTaskFIFO(task);
  379. }
  380. void CBizChannelEntity::_on_connect(int error, const char *remote_ip, int remote_video_rtp, int remote_video_desc)
  381. {
  382. NotifyOnConnect *task = new NotifyOnConnect();
  383. task->m_pEntity = this;
  384. task->error = error;
  385. task->remote_rtp_ip = remote_ip;
  386. task->remote_video_port = remote_video_rtp;
  387. task->remote_video_desc = remote_video_desc;
  388. GetFunction()->PostEntityTaskFIFO(task);
  389. }
  390. void CBizChannelEntity::_on_close()
  391. {
  392. NotifyOnClose *task = new NotifyOnClose();
  393. task->m_pEntity = this;
  394. GetFunction()->PostEntityTaskFIFO(task);
  395. }
  396. void ChannelServiceSession::Handle_Connect( SpReqAnsContext<ChannelService_Connect_Req, ChannelService_Connect_Ans>::Pointer ctx )
  397. {
  398. DbgToBeidou(ctx->link, __FUNCTION__)();
  399. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("start connect, %s:%d", ctx->Req.ip.GetData(), ctx->Req.port);
  400. ErrorCodeEnum Error = m_pEntity->Connect(ctx->Req.ip, ctx->Req.port);
  401. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("connect Error = %d", Error);
  402. ctx->Answer(Error);
  403. }
  404. void ChannelServiceSession::Handle_Close( SpReqAnsContext<ChannelService_Close_Req, ChannelService_Close_Ans>::Pointer ctx )
  405. {
  406. DbgToBeidou(ctx->link, __FUNCTION__)();
  407. ErrorCodeEnum Error = m_pEntity->Close();
  408. ctx->Answer(Error);
  409. }
  410. void ChannelServiceSession::Handle_GetState( SpReqAnsContext<ChannelService_GetState_Req, ChannelService_GetState_Ans>::Pointer ctx )
  411. {
  412. DbgToBeidou(ctx->link, __FUNCTION__)();
  413. ctx->Ans.status = state2str(m_pEntity->GetState());
  414. ctx->Answer(Error_Succeed);
  415. }
  416. void ChannelServiceSession::Handle_BeginState( SpSubscribeContext<ChannelService_BeginState_Sub, ChannelService_State_Info>::Pointer ctx )
  417. {
  418. m_pEntity->RegisterState(m_id, ctx);
  419. }
  420. void ChannelServiceSession::Handle_EndState( SpOnewayCallContext<ChannelService_EndState_Info>::Pointer ctx )
  421. {
  422. DbgToBeidou(ctx->link, __FUNCTION__)();
  423. m_pEntity->UnregisterState(m_id);
  424. }
  425. void ChannelServiceSession::Handle_Send( SpOnewayCallContext<ChannelService_Send_Info>::Pointer ctx )
  426. {
  427. DbgToBeidou(ctx->link, __FUNCTION__)();
  428. m_pEntity->Send(ctx->Info.type, ctx->Info.compress, ctx->Info.encrypt, ctx->Info.sub_type, ctx->Info.id, ctx->Info.data);
  429. }
  430. void ChannelServiceSession::Handle_BeginRecv( SpSubscribeContext<ChannelService_BeginRecv_Sub, ChannelService_Packet_Info>::Pointer ctx )
  431. {
  432. m_pEntity->RegisterRxPkt(m_id, ctx);
  433. }
  434. void ChannelServiceSession::Handle_EndRecv( SpOnewayCallContext<ChannelService_EndRecv_Info>::Pointer ctx )
  435. {
  436. DbgToBeidou(ctx->link, __FUNCTION__)();
  437. m_pEntity->UnregisterRxpkt(m_id);
  438. }
  439. void ChannelServiceSession::OnClose( ErrorCodeEnum eErrorCode )
  440. {
  441. m_pEntity->UnregisterRxpkt(m_id);
  442. m_pEntity->UnregisterState(m_id);
  443. }
  444. SP_BEGIN_ENTITY_MAP()
  445. SP_ENTITY(CBizChannelEntity)
  446. SP_END_ENTITY_MAP()