mod_assistantchannel.cpp 15 KB

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