mod_assistantchannel.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. #include "stdafx.h"
  2. #include "mod_assistantchannel.h"
  3. #include "VideoDesc.h"
  4. #include "AssistantChannel_server_g.h"
  5. #include "../../Other/rvcmediacommon/rvc_media_common.h"
  6. #include "chan_protocol.h"
  7. using namespace AssistantChannel;
  8. static const char *__states[] = {
  9. "Idle", "Connecting", "Connected", "Closing"
  10. };
  11. inline const char *state2str(int state)
  12. {
  13. return __states[state];
  14. }
  15. static __inline unsigned int hash32_buf(const void *bf, size_t len, unsigned int hash)
  16. {
  17. const unsigned char *s = (const unsigned char*)bf;
  18. while (len-- != 0) /* "nemesi": k=257, r=r*257 */
  19. hash = hash * 257 + *s++;
  20. return (hash * 257);
  21. }
  22. static int MakeDesc(DWORD eScreen,DeviceTypeEnum eDevicetype)
  23. {
  24. if (eScreen == 1)
  25. {
  26. if (eMobilePadType == eDevicetype)
  27. {
  28. return media_desc_encode(
  29. ACM_VIDEO_MODE_SQUARE, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_MOBILE,
  30. ACM_VIDEO_MODE_QVGA, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_MOBILEAGENT);
  31. }
  32. else if((ePadtype == eDevicetype)||(eDesk2SType == eDevicetype))
  33. {
  34. return media_desc_encode(
  35. ACM_VIDEO_MODE_SQUARE, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_MOBILE,
  36. ACM_VIDEO_MODE_QVGA, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_MOBILE);
  37. }
  38. else
  39. {
  40. return media_desc_encode(
  41. ACM_VIDEO_MODE_SQUARE, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_BASELINE,
  42. ACM_VIDEO_MODE_QVGA, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_BASELINE);
  43. }
  44. }
  45. else if (eScreen == 2)
  46. {
  47. return media_desc_encode(
  48. ACM_VIDEO_MODE_SQUARE, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_BASELINE,
  49. ACM_VIDEO_MODE_VGA, ACM_VIDEO_ENCODE_H264, ACM_VIDEO_FPS_BASELINE);
  50. }
  51. else
  52. {
  53. assert(0);
  54. }
  55. return 0;
  56. }
  57. ErrorCodeEnum CBizChannelEntity::__OnStart(ErrorCodeEnum preOperationError)
  58. {
  59. //MessageBoxA(0,0,0,0);
  60. bizchan_lib_init();
  61. m_eState = eChannelState_Idle;
  62. m_pChan = NULL;
  63. m_eDeviceType = eStand2sType;
  64. //is Pad Version
  65. CSmartPointer<IEntityFunction> spFunction = GetFunction();
  66. CSystemStaticInfo stStaticinfo;
  67. spFunction->GetSystemStaticInfo(stStaticinfo);
  68. if (stricmp(stStaticinfo.strMachineType,"RVC.PAD")==0)
  69. {
  70. if (stricmp(stStaticinfo.strSite,"CMB.FLB")==0)
  71. {
  72. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("the type is mobile pad");
  73. m_eDeviceType = eMobilePadType;
  74. }
  75. else
  76. {
  77. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("the type is pad");
  78. m_eDeviceType = ePadtype;
  79. }
  80. }
  81. else if (stricmp(stStaticinfo.strMachineType,"RVC.Desk2S")==0)
  82. {
  83. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("the type is Desk2S");
  84. m_eDeviceType = eDesk2SType;
  85. }
  86. else if (stricmp(stStaticinfo.strMachineType,"RPM.Stand1S")==0)
  87. {
  88. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("the type is RPM.Stand1S");
  89. m_eDeviceType = eRpm1sType;
  90. }
  91. else
  92. {
  93. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("the type is standard");
  94. m_eDeviceType = eStand2sType;
  95. }
  96. ListEntry_InitHead(&m_stateList);
  97. ListEntry_InitHead(&m_rxpktList);
  98. //Connect("99.1.100.215", 2323, "9990", NORMAL_CALLTYPE);
  99. return Error_Succeed;
  100. }
  101. ErrorCodeEnum CBizChannelEntity::__OnClose(ErrorCodeEnum preOperationError)
  102. {
  103. //.....
  104. bizchan_lib_term();
  105. return Error_Succeed;
  106. }
  107. CServerSessionBase * CBizChannelEntity::OnNewSession(const char* pszRemoteEntityName, const char * pszClass)
  108. {
  109. //LOG_FUNCTION();
  110. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s connected class = %s!", pszRemoteEntityName, pszClass);
  111. return new ChannelServiceSession(this, m_id_seq++);
  112. }
  113. ErrorCodeEnum CBizChannelEntity::Connect(const char *ip, int port, const char *callno, CallingTypeEnum eType)
  114. {
  115. ErrorCodeEnum Error = Error_Succeed;
  116. if (m_eState == eChannelState_Idle)
  117. {
  118. CSystemStaticInfo Info;
  119. bizchan_config_t config = {0};
  120. bizchan_callback_t cb = {0};
  121. GetFunction()->GetSystemStaticInfo(Info);
  122. config.proxy_server = const_cast<char*>(ip);
  123. config.proxy_server_port = port;
  124. if (MOBILETOPAD_CALLTYPE == eType || PADTOPAD_CALLTYPE == eType){
  125. config.session_id = const_cast<char*>(callno);
  126. }else{
  127. config.session_id = const_cast<char*>(((const char*)Info.strTerminalID));
  128. }
  129. if (MOBILETOPAD_CALLTYPE == eType){
  130. config.crypt_type = 1;
  131. }
  132. config.call_no = const_cast<char*>(callno);
  133. config.client_id = "";
  134. config.video.desc = MakeDesc(Info.eScreen,m_eDeviceType);
  135. config.video.rtp_port = REC_COMMON_VIDEO_PORT;
  136. cb.user_data = this;
  137. cb.on_close = &__on_close;
  138. cb.on_connect = &__on_connect;
  139. cb.on_destroy = &__on_destroy;
  140. cb.on_recv_pkt = &__on_recv_pkt;
  141. cb.dbg = &__dbg;
  142. int rc = bizchan_create(&config, &cb, &m_pChan);
  143. if (rc == 0)
  144. {
  145. rc = bizchan_start_connect(m_pChan);
  146. }
  147. if (rc != 0)
  148. Error = Error_Unexpect;
  149. else
  150. ChangeState(eChannelState_Connecting);
  151. }
  152. else
  153. {
  154. Error = Error_InvalidState;
  155. }
  156. //Dbg("Connect: %d", Error);
  157. return Error;
  158. }
  159. ErrorCodeEnum CBizChannelEntity::Close()
  160. {
  161. ErrorCodeEnum Error = Error_Succeed;
  162. if (m_eState == eChannelState_Connecting || m_eState == eChannelState_Connected) {
  163. int rc = bizchan_start_close(m_pChan);
  164. if (rc == 0) {
  165. ChangeState(eChannelState_Closing);
  166. } else {
  167. Error = Error_Unexpect;
  168. }
  169. } else {
  170. Error = Error_InvalidState;
  171. }
  172. return Error_Succeed;
  173. }
  174. ErrorCodeEnum CBizChannelEntity::RegisterState(int id, SpSubscribeContext<ChannelService_BeginState_Sub, ChannelService_State_Info>::Pointer ctx)
  175. {
  176. state_entry *pos;
  177. ListEntry_ForEach(pos, &m_stateList, state_entry, entry)
  178. {
  179. if (pos->id == id)
  180. {
  181. return Error_AlreadyExist;
  182. }
  183. }
  184. pos = new state_entry();
  185. pos->ctx = ctx;
  186. pos->id = id;
  187. ListEntry_AddTail(&m_stateList, &pos->entry);
  188. return Error_Succeed;
  189. }
  190. ErrorCodeEnum CBizChannelEntity::RegisterRxPkt(int id, SpSubscribeContext<ChannelService_BeginRecv_Sub, ChannelService_Packet_Info>::Pointer ctx)
  191. {
  192. rxpkt_entry *pos;
  193. ListEntry_ForEach(pos, &m_rxpktList, rxpkt_entry, entry)
  194. {
  195. if ((pos->id == id)&&(pos->ctx->Req.type == ctx->Req.type))
  196. {
  197. return Error_AlreadyExist;
  198. }
  199. }
  200. //Dbg("RegisterRxPkt id = %d",id);
  201. pos = new rxpkt_entry();
  202. pos->id = id;
  203. pos->ctx = ctx;
  204. ListEntry_AddTail(&m_rxpktList, &pos->entry);
  205. return Error_Succeed;
  206. }
  207. void CBizChannelEntity::UnregisterState(int id)
  208. {
  209. state_entry *pos,*n;
  210. ListEntry_ForEachSafe(pos,n, &m_stateList, state_entry, entry) {
  211. if (pos->id == id) {
  212. ListEntry_DeleteNode(&pos->entry);
  213. delete pos;
  214. }
  215. }
  216. }
  217. void CBizChannelEntity::UnregisterRxpkt(int id)
  218. {
  219. rxpkt_entry *pos,*n;
  220. //ListEntry_ForEach(pos, &m_rxpktList, rxpkt_entry, entry)
  221. ListEntry_ForEachSafe(pos, n, &m_rxpktList, rxpkt_entry, entry)
  222. {
  223. if((pos->id == id))
  224. {
  225. //Dbg("UnregisterRxpkt id = %d",pos->id);
  226. ListEntry_DeleteNode(&pos->entry);
  227. delete pos;
  228. }
  229. }
  230. }
  231. ErrorCodeEnum CBizChannelEntity::Send(int type, bool compress, bool encrypt, int sub_type, int id, CBlob &data)
  232. {
  233. if (m_eState == eChannelState_Connected) {
  234. //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));
  235. //Dbg("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));
  236. int rc = bizchan_post_pkt(m_pChan, type, compress, encrypt, sub_type, id, (const char*)data.m_pData, data.m_iLength);
  237. return rc == 0 ? Error_Succeed : Error_NetBroken;
  238. }
  239. else
  240. {
  241. return Error_NetBroken;
  242. }
  243. }
  244. void CBizChannelEntity::ChangeState(int new_state, const char *param)
  245. {
  246. if (m_eState != new_state) {
  247. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("change state from %s to %s", state2str(m_eState), state2str(new_state));
  248. m_eState = new_state;
  249. state_entry *pos;
  250. ListEntry_ForEach(pos, &m_stateList, state_entry, entry) {
  251. ChannelService_State_Info State;
  252. State.state = new_state;
  253. State.status = state2str(new_state);
  254. if (param) {
  255. State.param = param;
  256. }
  257. pos->ctx->SendMessage(State);
  258. }
  259. }
  260. }
  261. //void on_recv_pkt(int type, int sub_type, const char *pkt, int pkt_size)
  262. void CBizChannelEntity::on_recv_pkt(int type, int sub_type, int id, CBlob &data)
  263. {
  264. //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));
  265. //Dbg("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));
  266. rxpkt_entry *pos;
  267. ListEntry_ForEach(pos, &m_rxpktList, rxpkt_entry, entry)
  268. {
  269. if (pos->ctx->Req.type == type)
  270. {
  271. ChannelService_Packet_Info pkt;
  272. pkt.type = type;
  273. pkt.sub_type = sub_type;
  274. pkt.id = id;
  275. //pkt.data.m_bManaged = true;
  276. pkt.data = data;
  277. //data.m_bManaged = false;
  278. pos->ctx->SendMessage(pkt);
  279. //break;
  280. }
  281. }
  282. }
  283. void CBizChannelEntity::on_connect(int error, const char *remote_ip, int remote_video_rtp, int remote_video_desc)
  284. {
  285. if (!error) {
  286. if (m_eState == eChannelState_Connecting) {
  287. CSimpleStringA strValue;
  288. ErrorCodeEnum Error = GetFunction()->GetSysVar("VideoWindowInitializeParam", strValue);
  289. if (Error == Error_Succeed) {
  290. int local_view_x;
  291. int local_view_y;
  292. int local_view_cx;
  293. int local_view_cy;
  294. int remote_view_x;
  295. int remote_view_y;
  296. int remote_view_cx;
  297. int remote_view_cy;
  298. int remote_video_width = 0;
  299. int remote_video_height = 0;
  300. {
  301. CSystemStaticInfo Info;
  302. GetFunction()->GetSystemStaticInfo(Info);
  303. if (Info.eScreen == 1)
  304. {
  305. remote_video_width = REC_COMMON_VIDEO_SSM_AGENT_WIDTH;
  306. remote_video_height = REC_COMMON_VIDEO_SSM_AGENT_HEIGHT;
  307. }
  308. else
  309. {
  310. remote_video_width = REC_COMMON_VIDEO_DSM_AGENT_WIDTH;
  311. remote_video_height = REC_COMMON_VIDEO_DSM_AGENT_HEIGHT;
  312. }
  313. }
  314. ParseVideoViewParam((LPCSTR)strValue, local_view_x, local_view_y, local_view_cx, local_view_cy,
  315. remote_view_x, remote_view_y, remote_view_cx, remote_view_cy);
  316. //Dbg("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,
  317. // remote_view_x, remote_view_y, remote_view_cx, remote_view_cy);
  318. CSimpleStringA param;
  319. if (eMobilePadType == m_eDeviceType)
  320. {
  321. param = BuildVideoDesc(remote_ip, remote_video_rtp, remote_video_width, remote_video_height,
  322. REC_COMMON_VIDEO_FPS_MOBILE_AGENT, 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. }
  325. else if((ePadtype == m_eDeviceType)||(eDesk2SType == m_eDeviceType))
  326. {
  327. param = BuildVideoDesc(remote_ip, remote_video_rtp, remote_video_width, remote_video_height,
  328. REC_COMMON_VIDEO_FPS_MOBILE, local_view_x, local_view_y, local_view_cx, local_view_cy,
  329. remote_view_x, remote_view_y, remote_view_cx, remote_view_cy);
  330. }
  331. else
  332. {
  333. param = BuildVideoDesc(remote_ip, remote_video_rtp, remote_video_width, remote_video_height,
  334. REC_COMMON_VIDEO_FPS, local_view_x, local_view_y, local_view_cx, local_view_cy,
  335. remote_view_x, remote_view_y, remote_view_cx, remote_view_cy);
  336. }
  337. ChangeState(eChannelState_Connected, (LPCSTR)param);
  338. } else {
  339. Dbg("get VideoWindowInitializeParam failed!");
  340. bizchan_start_close(m_pChan);
  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. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("_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. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start connect, %s:%d [%s]", (LPCSTR)ctx->Req.ip, ctx->Req.port, (LPCSTR)ctx->Req.callno);
  399. ErrorCodeEnum Error = m_pEntity->Connect(ctx->Req.ip, ctx->Req.port, ctx->Req.callno, (CallingTypeEnum)ctx->Req.etype);
  400. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("connect Error = %d", Error);
  401. ctx->Answer(Error);
  402. }
  403. void ChannelServiceSession::Handle_Close( SpReqAnsContext<ChannelService_Close_Req, ChannelService_Close_Ans>::Pointer ctx )
  404. {
  405. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("connect close!");
  406. ErrorCodeEnum Error = m_pEntity->Close();
  407. ctx->Answer(Error);
  408. }
  409. void ChannelServiceSession::Handle_GetState( SpReqAnsContext<ChannelService_GetState_Req, ChannelService_GetState_Ans>::Pointer ctx )
  410. {
  411. ctx->Ans.status = state2str(m_pEntity->GetState());
  412. ctx->Answer(Error_Succeed);
  413. }
  414. void ChannelServiceSession::Handle_BeginState( SpSubscribeContext<ChannelService_BeginState_Sub, ChannelService_State_Info>::Pointer ctx )
  415. {
  416. m_pEntity->RegisterState(m_id, ctx);
  417. }
  418. void ChannelServiceSession::Handle_EndState( SpOnewayCallContext<ChannelService_EndState_Info>::Pointer ctx )
  419. {
  420. m_pEntity->UnregisterState(m_id);
  421. }
  422. void ChannelServiceSession::Handle_Send( SpOnewayCallContext<ChannelService_Send_Info>::Pointer ctx )
  423. {
  424. m_pEntity->Send(ctx->Info.type, ctx->Info.compress, ctx->Info.encrypt, ctx->Info.sub_type, ctx->Info.id, ctx->Info.data);
  425. }
  426. void ChannelServiceSession::Handle_BeginRecv( SpSubscribeContext<ChannelService_BeginRecv_Sub, ChannelService_Packet_Info>::Pointer ctx )
  427. {
  428. m_pEntity->RegisterRxPkt(m_id, ctx);
  429. }
  430. void ChannelServiceSession::Handle_EndRecv( SpOnewayCallContext<ChannelService_EndRecv_Info>::Pointer ctx )
  431. {
  432. m_pEntity->UnregisterRxpkt(m_id);
  433. }
  434. void ChannelServiceSession::OnClose( ErrorCodeEnum eErrorCode )
  435. {
  436. m_pEntity->UnregisterRxpkt(m_id);
  437. m_pEntity->UnregisterState(m_id);
  438. }
  439. SP_BEGIN_ENTITY_MAP()
  440. SP_ENTITY(CBizChannelEntity)
  441. SP_END_ENTITY_MAP()