mod_assistantchannel.cpp 15 KB

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