ConnectorFSM.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. #pragma once
  2. #include "SpHelper.h"
  3. #include "SpFSM.h"
  4. #include "SpIni.h"
  5. #include "IHttpFunc.h"
  6. #include "Event.h"
  7. #include "../include/EventCode.h"
  8. #include "y2k_time.h"
  9. #include "CommEntityUtil.hpp"
  10. #ifdef _WIN32
  11. #include "resource.h"
  12. #else
  13. #include "uuid/uuid.h"
  14. #endif
  15. #include "http_callrouter.h"
  16. #include "../mod_assistantchannel/VideoDesc.h"
  17. #include "../mod_sipphone/SIPPhone_client_g.h"
  18. using namespace SIPPhone;
  19. #include "../mod_assistantchannel/AssistantChannel_client_g.h"
  20. using namespace AssistantChannel;
  21. #include "../mod_mediacontroller/MediaController_client_g.h"
  22. using namespace MediaController;
  23. #include "../mod_assistantchannel/chan_protocol.h"
  24. #include "CallType.h"
  25. #include "CounterConnector_def_g.h"
  26. #include "CounterConnector_msg_g.h"
  27. using namespace CounterConnector;
  28. #include "../mod_interactioncontext/InteractionContext_client_g.h"
  29. #include "../mod_interactioncontext/InteractionContext_def_g.h"
  30. using namespace InteractionContext;
  31. #include <vector>
  32. #include <string>
  33. #ifndef RVC_DEFAULT_HTTPTIMEOUT
  34. #define RVC_DEFAULT_HTTPTIMEOUT 10
  35. #endif // !RVC_DEFAULT_HTTPTIMEOUT
  36. #define USER_EVT_PICKUP_CALL EVT_USER+1
  37. #define USER_EVT_HANDFREE_CALL EVT_USER+2
  38. #define USER_EVT_HANGUP EVT_USER+3
  39. #define USER_EVT_DOUBLE_RECORD_CALL EVT_USER+5
  40. #define USER_EVT_JMP_FAIL EVT_USER+11
  41. #define USER_EVT_JMP_HANDFREE EVT_USER+12
  42. #define USER_EVT_JMP_PICKUP EVT_USER+13
  43. #define USER_EVT_TO_HANDFREE EVT_USER+14
  44. #define USER_EVT_TO_PICKUP EVT_USER+15
  45. #define USER_EVT_SIP_STATE_CONNECTED EVT_USER+21
  46. #define USER_EVT_SIP_STATE_IDLE EVT_USER+22
  47. #define USER_EVT_CHAN_STATE_CONNECTED EVT_USER+31
  48. #define USER_EVT_CHAN_STATE_IDLE EVT_USER+33
  49. #define USER_EVT_RECONNECT EVT_USER+40
  50. #define USER_EVT_AGENT_WRITABLE EVT_USER+41
  51. #define USER_EVT_ERROR EVT_USER+10
  52. #define USER_EVT_EXIT EVT_USER+50
  53. #define USER_EVT_STOP_RECORD_BROADCAST EVT_USER+55
  54. #define USER_EVT_ASSISTCHAN_IDEL EVT_USER+60
  55. #define USER_EVT_SIPPHONE_IDEL EVT_USER+61
  56. #define USER_EVT_STARTVIDEODISPLAY EVT_USER+70
  57. #define USER_EVT_STOPVIDEODISPLAY EVT_USER+71
  58. #define USER_EVT_SHOWLOACALVIDEO EVT_USER+72
  59. #define USER_EVT_STOPLOCALVIDEO EVT_USER+73
  60. #define USER_EVT_SHOWLOACALREMOTEVIDEO EVT_USER+74
  61. #define USER_EVT_STOPLOACALREMOTEVIDEO EVT_USER+75
  62. enum CurServerNum
  63. {
  64. Error_Server = -1,
  65. MAIN_SERVER,
  66. BACK_SERVER
  67. };
  68. #ifndef MAX_VOIP_SERVER_NUM
  69. #define MAX_VOIP_SERVER_NUM 10
  70. #endif
  71. struct ChanStateConnectedEvent : public FSMEvent
  72. {
  73. ChanStateConnectedEvent(const char *param) : FSMEvent(USER_EVT_CHAN_STATE_CONNECTED), m_param(param) {}
  74. CSimpleStringA m_param;
  75. };
  76. struct StartVideoDisplayEvent : public FSMEvent
  77. {
  78. StartVideoDisplayEvent(const char *param) : FSMEvent(USER_EVT_STARTVIDEODISPLAY), m_param(param) {}
  79. CSimpleStringA m_param;
  80. };
  81. struct ShowLocalVideoEvent : public FSMEvent
  82. {
  83. ShowLocalVideoEvent(const char *param) : FSMEvent(USER_EVT_SHOWLOACALVIDEO), m_param(param) {}
  84. CSimpleStringA m_param;
  85. };
  86. struct ShowLocalAndRemoteVideoEvent : public FSMEvent
  87. {
  88. ShowLocalAndRemoteVideoEvent(const char *param) : FSMEvent(USER_EVT_SHOWLOACALREMOTEVIDEO), m_param(param) {}
  89. CSimpleStringA m_param;
  90. };
  91. struct CallingParam
  92. {
  93. //呼叫模式,0:可视柜台正常呼叫,1:双录连线
  94. CallingTypeEnum nCallType;
  95. CSimpleStringA connect_session;
  96. CSimpleStringA connect_ip;
  97. int connect_port;
  98. CSimpleStringA subid;
  99. int assistant_port;
  100. };
  101. #ifndef MAX_LENGTH_OF_BRANCHNO
  102. #define MAX_LENGTH_OF_BRANCHNO 4
  103. #endif
  104. #ifndef MAX_LENGTH_OF_SESSIONID
  105. #define MAX_LENGTH_OF_SESSIONID 16
  106. #endif
  107. #ifndef MAX_LENGTH_OF_CONNECTIP
  108. #define MAX_LENGTH_OF_CONNECTIP 16
  109. #endif
  110. class ACMCallFSM : public FSMImpl<ACMCallFSM>, public IFSMStateHooker
  111. {
  112. public:
  113. enum {s0,s7,s8,s10,s11,s12,s13,s14,s20,s21,s22,s23,s24,s3,s4,s50,s51,s52,s53,s60,s61,s62,s63};
  114. BEGIN_FSM_STATE(ACMCallFSM)
  115. FSM_STATE_ENTRY(s0,"Offline",s0_on_entry,s0_on_exit,s0_on_event)
  116. FSM_STATE_ENTRY(s7,"ShowLocalVideo",s7_on_entry,s7_on_exit,s7_on_event)
  117. FSM_STATE_ENTRY(s8,"Delay",s8_on_entry,s8_on_exit,s8_on_event)
  118. FSM_STATE_ENTRY(s10,"Calling_Start",s10_on_entry,s10_on_exit,s10_on_event)
  119. FSM_STATE_ENTRY(s11,"Calling_SipConnect",s11_on_entry,s11_on_exit,s11_on_event)
  120. FSM_STATE_ENTRY(s12,"Calling_ChanConnect",s12_on_entry,s12_on_exit,s12_on_event)
  121. FSM_STATE_ENTRY(s13,"Calling_AllReady",s13_on_entry,s13_on_exit,s13_on_event)
  122. FSM_STATE_ENTRY(s14,"ShowLocalRemoteVideo",s14_on_entry,s14_on_exit,s14_on_event)
  123. FSM_STATE_ENTRY(s20, "ExitCall_Start",s20_on_entry,s20_on_exit,s20_on_event)
  124. FSM_STATE_ENTRY(s21, "ExitChan_Finished",s21_on_entry,s21_on_exit,s21_on_event)
  125. FSM_STATE_ENTRY(s22, "ExitSip_Finished",s22_on_entry,s22_on_exit,s22_on_event)
  126. FSM_STATE_ENTRY(s23, "ExitCall_AllFinished",s23_on_entry,s23_on_exit,s23_on_event)
  127. FSM_STATE_ENTRY(s24, "ExitCall_Delay",s24_on_entry,s24_on_exit,s24_on_event)
  128. FSM_STATE_ENTRY(s3, "HandFree",s3_on_entry,s3_on_exit,s3_on_event)
  129. FSM_STATE_ENTRY(s4, "Pickup",s4_on_entry,s4_on_exit,s4_on_event)
  130. FSM_STATE_ENTRY(s50, "Broken0",s50_on_entry,s50_on_exit,s50_on_event)
  131. FSM_STATE_ENTRY(s51, "Broken_SipFinished",s51_on_entry,s51_on_exit,s51_on_event)
  132. FSM_STATE_ENTRY(s52, "Broken_ChanFinished",s52_on_entry,s52_on_exit,s52_on_event)
  133. FSM_STATE_ENTRY(s53, "Broken_AllFinished",s53_on_entry,s53_on_exit,s53_on_event)
  134. FSM_STATE_ENTRY(s60, "Hangup_Start",s60_on_entry,s60_on_exit,s60_on_event)
  135. FSM_STATE_ENTRY(s61, "Hangup_SipFinished",s61_on_entry,s61_on_exit,s61_on_event)
  136. FSM_STATE_ENTRY(s62, "Hangup_ChanFinished",s62_on_entry,s62_on_exit,s62_on_event)
  137. FSM_STATE_ENTRY(s63, "Hangup_AllFinished",s63_on_entry,s63_on_exit,s63_on_event)
  138. END_FSM_STATE()
  139. BEGIN_FSM_RULE(ACMCallFSM,s0)
  140. FSM_RULE_ENTRY_ANY(s0, s8, USER_EVT_PICKUP_CALL)
  141. FSM_RULE_ENTRY_ANY(s0, s7, USER_EVT_SHOWLOACALVIDEO)
  142. FSM_RULE_ENTRY_ANY(s0, s14, USER_EVT_SHOWLOACALREMOTEVIDEO)
  143. FSM_RULE_ENTRY_ANY(s7, s0, USER_EVT_STOPLOCALVIDEO)
  144. FSM_RULE_ENTRY_ANY(s14, s0, USER_EVT_STOPLOACALREMOTEVIDEO)
  145. FSM_RULE_ENTRY_ANY(s0, FSM_STATE_EXIT, USER_EVT_EXIT)
  146. FSM_RULE_ENTRY_ANY(s0, s8, USER_EVT_HANDFREE_CALL)
  147. FSM_RULE_ENTRY_ANY(s0, s8, USER_EVT_DOUBLE_RECORD_CALL)
  148. FSM_RULE_ENTRY_ANY(s14, s0, USER_EVT_STOP_RECORD_BROADCAST)
  149. FSM_RULE_ENTRY_ANY(s8, s11, EVT_TIMER)
  150. FSM_RULE_ENTRY_ANY(s11, s12, USER_EVT_SIP_STATE_CONNECTED)
  151. FSM_RULE_ENTRY_ANY(s11, s23, USER_EVT_JMP_FAIL)
  152. FSM_RULE_ENTRY_ANY(s11, s21, EVT_TIMER)
  153. FSM_RULE_ENTRY_ANY(s11, s23, USER_EVT_SIP_STATE_IDLE)
  154. FSM_RULE_ENTRY_ANY(s11, s21, USER_EVT_HANGUP)
  155. FSM_RULE_ENTRY_ANY(s21, s23, USER_EVT_SIP_STATE_IDLE)
  156. FSM_RULE_ENTRY_ANY(s21, s23, USER_EVT_JMP_FAIL)
  157. FSM_RULE_ENTRY_ANY(s12, s13, USER_EVT_CHAN_STATE_CONNECTED)
  158. FSM_RULE_ENTRY_ANY(s12, s21, USER_EVT_CHAN_STATE_IDLE)
  159. FSM_RULE_ENTRY_ANY(s12, s22, USER_EVT_SIP_STATE_IDLE)
  160. FSM_RULE_ENTRY_ANY(s12, s21, USER_EVT_JMP_FAIL)
  161. FSM_RULE_ENTRY_ANY(s22, s23, USER_EVT_CHAN_STATE_IDLE)
  162. FSM_RULE_ENTRY_ANY(s12, s20, USER_EVT_HANGUP)
  163. FSM_RULE_ENTRY_ANY(s20, s21, USER_EVT_CHAN_STATE_IDLE)
  164. FSM_RULE_ENTRY_ANY(s20, s22, USER_EVT_SIP_STATE_IDLE)
  165. FSM_RULE_ENTRY_ANY(s13, s3, USER_EVT_JMP_HANDFREE)
  166. FSM_RULE_ENTRY_ANY(s13, s4, USER_EVT_JMP_PICKUP)
  167. FSM_RULE_ENTRY_ANY(s23, s0, EVT_TIMER)
  168. FSM_RULE_ENTRY_ANY(s23, s24, USER_EVT_RECONNECT)
  169. FSM_RULE_ENTRY_ANY(s24, s11, EVT_TIMER)
  170. FSM_RULE_ENTRY_ANY(s3, s4, USER_EVT_TO_PICKUP)
  171. FSM_RULE_ENTRY_ANY(s3, s51, USER_EVT_SIP_STATE_IDLE)
  172. FSM_RULE_ENTRY_ANY(s3, s52, USER_EVT_CHAN_STATE_IDLE)
  173. FSM_RULE_ENTRY_ANY(s3, s60, USER_EVT_HANGUP)
  174. FSM_RULE_ENTRY_ANY(s4, s3, USER_EVT_TO_HANDFREE)
  175. FSM_RULE_ENTRY_ANY(s4, s51, USER_EVT_SIP_STATE_IDLE)
  176. FSM_RULE_ENTRY_ANY(s4, s52, USER_EVT_CHAN_STATE_IDLE)
  177. FSM_RULE_ENTRY_ANY(s4, s60, USER_EVT_HANGUP)
  178. FSM_RULE_ENTRY_ANY(s50, s51, USER_EVT_SIP_STATE_IDLE)
  179. FSM_RULE_ENTRY_ANY(s50, s52, USER_EVT_CHAN_STATE_IDLE)
  180. FSM_RULE_ENTRY_ANY(s51, s53, USER_EVT_CHAN_STATE_IDLE)
  181. FSM_RULE_ENTRY_ANY(s52, s53, USER_EVT_SIP_STATE_IDLE)
  182. FSM_RULE_ENTRY_ANY(s52, s53, USER_EVT_JMP_FAIL)
  183. FSM_RULE_ENTRY_ANY(s53, s0, EVT_TIMER)
  184. FSM_RULE_ENTRY_ANY(s60, s61, USER_EVT_SIP_STATE_IDLE)
  185. FSM_RULE_ENTRY_ANY(s60, s62, USER_EVT_CHAN_STATE_IDLE)
  186. FSM_RULE_ENTRY_ANY(s61, s63, USER_EVT_CHAN_STATE_IDLE)
  187. FSM_RULE_ENTRY_ANY(s62, s63, USER_EVT_SIP_STATE_IDLE)
  188. FSM_RULE_ENTRY_ANY(s62, s63, USER_EVT_JMP_FAIL)
  189. FSM_RULE_ENTRY_ANY(s63, s0, EVT_TIMER)
  190. //assitchannel重启后,状态跳转到Offline
  191. FSM_RULE_ENTRY_ANY(s3, s0, USER_EVT_ASSISTCHAN_IDEL)
  192. FSM_RULE_ENTRY_ANY(s4, s0, USER_EVT_ASSISTCHAN_IDEL)
  193. FSM_RULE_ENTRY_ANY(s50, s0, USER_EVT_ASSISTCHAN_IDEL)
  194. FSM_RULE_ENTRY_ANY(s51, s0, USER_EVT_ASSISTCHAN_IDEL)
  195. FSM_RULE_ENTRY_ANY(s52, s0, USER_EVT_ASSISTCHAN_IDEL)
  196. FSM_RULE_ENTRY_ANY(s53, s0, USER_EVT_ASSISTCHAN_IDEL)
  197. FSM_RULE_ENTRY_ANY(s60, s0, USER_EVT_ASSISTCHAN_IDEL)
  198. FSM_RULE_ENTRY_ANY(s61, s0, USER_EVT_ASSISTCHAN_IDEL)
  199. FSM_RULE_ENTRY_ANY(s62, s0, USER_EVT_ASSISTCHAN_IDEL)
  200. FSM_RULE_ENTRY_ANY(s63, s0, USER_EVT_ASSISTCHAN_IDEL)
  201. //sipphone重启后,所有状态跳转到Offline
  202. FSM_RULE_ENTRY_ANY(s8, s0, USER_EVT_SIPPHONE_IDEL)
  203. FSM_RULE_ENTRY_ANY(s10, s0, USER_EVT_SIPPHONE_IDEL)
  204. FSM_RULE_ENTRY_ANY(s11, s0, USER_EVT_SIPPHONE_IDEL)
  205. FSM_RULE_ENTRY_ANY(s12, s0, USER_EVT_SIPPHONE_IDEL)
  206. FSM_RULE_ENTRY_ANY(s13, s0, USER_EVT_SIPPHONE_IDEL)
  207. FSM_RULE_ENTRY_ANY(s20, s0, USER_EVT_SIPPHONE_IDEL)
  208. FSM_RULE_ENTRY_ANY(s21, s0, USER_EVT_SIPPHONE_IDEL)
  209. FSM_RULE_ENTRY_ANY(s22, s0, USER_EVT_SIPPHONE_IDEL)
  210. FSM_RULE_ENTRY_ANY(s23, s0, USER_EVT_SIPPHONE_IDEL)
  211. FSM_RULE_ENTRY_ANY(s24, s0, USER_EVT_SIPPHONE_IDEL)
  212. FSM_RULE_ENTRY_ANY(s3, s0, USER_EVT_SIPPHONE_IDEL)
  213. FSM_RULE_ENTRY_ANY(s4, s0, USER_EVT_SIPPHONE_IDEL)
  214. FSM_RULE_ENTRY_ANY(s50, s0, USER_EVT_SIPPHONE_IDEL)
  215. FSM_RULE_ENTRY_ANY(s51, s0, USER_EVT_SIPPHONE_IDEL)
  216. FSM_RULE_ENTRY_ANY(s52, s0, USER_EVT_SIPPHONE_IDEL)
  217. FSM_RULE_ENTRY_ANY(s53, s0, USER_EVT_SIPPHONE_IDEL)
  218. FSM_RULE_ENTRY_ANY(s60, s0, USER_EVT_SIPPHONE_IDEL)
  219. FSM_RULE_ENTRY_ANY(s61, s0, USER_EVT_SIPPHONE_IDEL)
  220. FSM_RULE_ENTRY_ANY(s62, s0, USER_EVT_SIPPHONE_IDEL)
  221. FSM_RULE_ENTRY_ANY(s63, s0, USER_EVT_SIPPHONE_IDEL)
  222. //sipphone重启后,所有状态跳转到Offline
  223. //分布式呼叫场景,拒接主动挂断
  224. FSM_RULE_ENTRY_ANY(s0, s60, USER_EVT_HANGUP)
  225. //分布式呼叫场景,拒接主动挂断
  226. END_FSM_RULE()
  227. ACMCallFSM();
  228. ~ACMCallFSM();
  229. virtual void OnStateTrans(int iSrcState, int iDstState);
  230. virtual ErrorCodeEnum OnInit();
  231. virtual ErrorCodeEnum OnExit();
  232. void s0_on_entry();
  233. void s0_on_exit();
  234. unsigned int s0_on_event(FSMEvent* event);
  235. void s7_on_entry();
  236. void s7_on_exit();
  237. unsigned int s7_on_event(FSMEvent* event);
  238. void s8_on_entry();
  239. void s8_on_exit();
  240. unsigned int s8_on_event(FSMEvent* event);
  241. void s10_on_entry();
  242. void s10_on_exit();
  243. unsigned int s10_on_event(FSMEvent* event);
  244. void s11_on_entry();
  245. void s11_on_exit();
  246. unsigned int s11_on_event(FSMEvent* event);
  247. void s12_on_entry();
  248. void s12_on_exit();
  249. unsigned int s12_on_event(FSMEvent* event);
  250. void s13_on_entry();
  251. void s13_on_exit();
  252. unsigned int s13_on_event(FSMEvent* event);
  253. void s14_on_entry();
  254. void s14_on_exit();
  255. unsigned int s14_on_event(FSMEvent* event);
  256. void s20_on_entry();
  257. void s20_on_exit();
  258. unsigned int s20_on_event(FSMEvent* event);
  259. void s21_on_entry();
  260. void s21_on_exit();
  261. unsigned int s21_on_event(FSMEvent* event);
  262. void s22_on_entry();
  263. void s22_on_exit();
  264. unsigned int s22_on_event(FSMEvent* event);
  265. void s23_on_entry();
  266. void s23_on_exit();
  267. unsigned int s23_on_event(FSMEvent* event);
  268. void s24_on_entry();
  269. void s24_on_exit();
  270. unsigned int s24_on_event(FSMEvent* event);
  271. void s3_on_entry();
  272. void s3_on_exit();
  273. unsigned int s3_on_event(FSMEvent* event);
  274. void s4_on_entry();
  275. void s4_on_exit();
  276. unsigned int s4_on_event(FSMEvent* event);
  277. void s50_on_entry();
  278. void s50_on_exit();
  279. unsigned int s50_on_event(FSMEvent* event);
  280. void s51_on_entry();
  281. void s51_on_exit();
  282. unsigned int s51_on_event(FSMEvent* event);
  283. void s52_on_entry();
  284. void s52_on_exit();
  285. unsigned int s52_on_event(FSMEvent* event);
  286. void s53_on_entry();
  287. void s53_on_exit();
  288. unsigned int s53_on_event(FSMEvent* event);
  289. void s60_on_entry();
  290. void s60_on_exit();
  291. unsigned int s60_on_event(FSMEvent* event);
  292. void s61_on_entry();
  293. void s61_on_exit();
  294. unsigned int s61_on_event(FSMEvent* event);
  295. void s62_on_entry();
  296. void s62_on_exit();
  297. unsigned int s62_on_event(FSMEvent* event);
  298. void s63_on_entry();
  299. void s63_on_exit();
  300. unsigned int s63_on_event(FSMEvent* event);
  301. bool m_bHangup;
  302. //sip呼叫失败次数,连续10次SIP呼叫失败触发自检重启SIPPHONE
  303. unsigned int m_nSipErrorNum;
  304. unsigned int m_uConnectTime;
  305. bool m_bConnected;
  306. long m_lConnectCostTime;
  307. bool ReConnectionAssistchan();
  308. bool ReConnectionSipphone(bool bLog = true);
  309. bool ReConnectionSyncService();
  310. int GetCallRouteList();
  311. ErrorCodeEnum StartVideo(const char *param)
  312. {
  313. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Start Video");
  314. if (!m_pPhoneClient)
  315. return Error_NetBroken;
  316. PhoneService_StartVideo_Info Info;
  317. ErrorCodeEnum Error = ParseVideoDesc(param, Info.remote_ip, Info.remote_port,
  318. Info.remote_width, Info.remote_height, Info.remote_fps,
  319. Info.local_view_x, Info.local_view_y, Info.local_view_cx, Info.local_view_cy,
  320. Info.remote_view_x, Info.remote_view_y, Info.remote_view_cx, Info.remote_view_cy);
  321. Info.local_hwd_move = 0;
  322. Info.remote_hwd_move = 1;
  323. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("start video, remote width:%d, remote height:%d, local(%d,%d,%d,%d),remote(%d,%d,%d,%d), remote_fps:%d",
  324. // Info.remote_width, Info.remote_height,
  325. // Info.local_view_x, Info.local_view_y, Info.local_view_cx, Info.local_view_cy,
  326. // Info.remote_view_x, Info.remote_view_y, Info.remote_view_cx, Info.remote_view_cy, Info.remote_fps);
  327. if (Error == Error_Succeed) {
  328. Error = (*m_pPhoneClient)(EntityResource::getLink().upgradeLink())->StartVideo(Info);
  329. }
  330. else {
  331. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("parse video desc failed!");
  332. }
  333. return Error;
  334. }
  335. ErrorCodeEnum StopVideo()
  336. {
  337. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Stop Video");
  338. if (!m_pPhoneClient)
  339. return Error_NetBroken;
  340. return (*m_pPhoneClient)(EntityResource::getLink().upgradeLink())->StopVideo();
  341. }
  342. ErrorCodeEnum StartVideoRender(const char* param)
  343. {
  344. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Start Record Video Render, and param is %s.", param);
  345. if (!m_pPhoneClient)
  346. return Error_NetBroken;
  347. PhoneService_StartVideoRender_Info Info;
  348. ErrorCodeEnum Error = ParseVideoRenderDesc(param,
  349. Info.local_view_x, Info.local_view_y, Info.local_view_cx, Info.local_view_cy,
  350. Info.remote_view_x, Info.remote_view_y, Info.remote_view_cx, Info.remote_view_cy);
  351. Info.local_hwd_move = 0;
  352. Info.remote_hwd_move = 1;
  353. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("start record video render,local(%d,%d,%d,%d),remote(%d,%d,%d,%d)",
  354. //Info.local_view_x, Info.local_view_y, Info.local_view_cx, Info.local_view_cy,
  355. //Info.remote_view_x, Info.remote_view_y, Info.remote_view_cx, Info.remote_view_cy);
  356. if (Error == Error_Succeed) {
  357. Error = (*m_pPhoneClient)(EntityResource::getLink().upgradeLink())->StartVideoRender(Info);
  358. }
  359. else {
  360. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("parse video desc failed!");
  361. }
  362. return Error;
  363. }
  364. ErrorCodeEnum StopVideoRender()
  365. {
  366. /*DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Stop Record Video Render");*/
  367. if (!m_pPhoneClient)
  368. return Error_NetBroken;
  369. return (*m_pPhoneClient)(EntityResource::getLink().upgradeLink())->StopVideoRender();
  370. }
  371. ErrorCodeEnum RealSipErrorCheck()
  372. {
  373. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Sip connect Fail 6 times,RealSipErrorCheck!");
  374. if (!m_pPhoneClient)
  375. return Error_NetBroken;
  376. return (*m_pPhoneClient)(EntityResource::getLink().upgradeLink())->RealErrorCheck();
  377. }
  378. ErrorCodeEnum SetCallingType(CallingTypeEnum eType)
  379. {
  380. if (!m_pPhoneClient) {
  381. return Error_NetBroken;
  382. }
  383. PhoneService_SetCallingParam_Info info;
  384. info.CallType = eType;
  385. return (*m_pPhoneClient)(EntityResource::getLink().upgradeLink())->SetCallingParam(info);
  386. }
  387. //ErrorCodeEnum StopSpeakerAudioCapture()
  388. //{
  389. // ErrorCodeEnum rc = Error_NotImpl;
  390. // MediaService_ClientBase* pMSClient = new MediaService_ClientBase(m_pEntity);
  391. // if(pMSClient->Connect() != Error_Succeed)
  392. // {
  393. // pMSClient->SafeDelete();
  394. // pMSClient = NULL;
  395. // }
  396. // else
  397. // {
  398. // rc = (*pMSClient)(EntityResource::getLink().upgradeLink())->StopSpeakerRender();
  399. // if(rc != Error_Succeed){
  400. // DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Stop Speaker Render failed return 0x%08x", rc);
  401. // }
  402. // pMSClient->GetFunction()->CloseSession();
  403. // pMSClient = NULL;
  404. // }
  405. // return rc;
  406. //}
  407. private:
  408. int TranslateState(int innerState);
  409. ErrorCodeEnum SetCallState(int state);
  410. void get_format_uuid(char* strbuffer, size_t ulen)
  411. {
  412. #ifdef RVC_OS_WIN
  413. UUID uuid;
  414. size_t uuidlen = 0;
  415. RPC_CSTR buf;
  416. UuidCreate((UUID*)&uuid);
  417. UuidToString((UUID*)&uuid, &buf);
  418. uuidlen = strlen((const char*)buf);
  419. if (uuidlen < ulen) {
  420. memcpy(strbuffer, (const char*)buf, uuidlen);
  421. }
  422. RpcStringFree(&buf);
  423. #else
  424. uuid_t uuid;
  425. uuid_generate(uuid);
  426. uuid_unparse(uuid, strbuffer);
  427. #endif
  428. }
  429. ErrorCodeEnum GetLocalIP(char *buff, size_t ulen)
  430. {
  431. #if defined(RVC_OS_WIN)
  432. char tmp[MAX_PATH] = { 0 };
  433. gethostname(tmp, sizeof(tmp));
  434. hostent* ent = gethostbyname(tmp);
  435. if (ent) {
  436. int icount = 0;
  437. for (; ent->h_addr_list[icount]; ) {
  438. ++icount;
  439. }
  440. m_iNetAdapterNum = icount;
  441. for (int i = 0; ent->h_addr_list[i]; ++i) {
  442. if (ent->h_addrtype == AF_INET) {
  443. struct in_addr* in = (struct in_addr*)ent->h_addr_list[i];
  444. char* p = inet_ntoa(*in);
  445. if (p[0] != '0') {
  446. if (strstr(p, "198.168.") == NULL && 0 != strncmp(p, "2.0.0.1", strlen("2.0.0.1"))) {
  447. strcpy(buff, p);
  448. return Error_Succeed;
  449. }
  450. }
  451. }
  452. }
  453. }
  454. return Error_Unexpect;
  455. #else
  456. ErrorCodeEnum error = Error_Unexpect;
  457. int sockfd = -1;
  458. struct ifconf ifconf;
  459. struct ifreq* ifreq = NULL;
  460. char strbuf[256] = { 0 };
  461. ifconf.ifc_len = 256;
  462. ifconf.ifc_buf = strbuf;
  463. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  464. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("socket error");
  465. return error;
  466. }
  467. ioctl(sockfd, SIOCGIFCONF, &ifconf); //get all socket info
  468. ifreq = (struct ifreq*)ifconf.ifc_buf;
  469. for (int i = (ifconf.ifc_len / sizeof(struct ifreq)); i > 0; i--) {
  470. if (ifreq->ifr_flags == AF_INET) { //for ipv4
  471. char* strIP = inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr);
  472. ifreq++;
  473. if (NULL != strIP) {
  474. if (NULL == strstr(strIP, "198.168.") && NULL == strstr(strIP, "127.0.0.1")) {
  475. strcpy(buff, strIP);
  476. error = Error_Succeed;
  477. break;
  478. }
  479. }
  480. }
  481. }
  482. close(sockfd);
  483. return error;
  484. #endif //RVC_OS_WIN
  485. }
  486. ErrorCodeEnum MakeCall(const char *hint_callnum,int nSipServer)
  487. {
  488. if (!m_pPhoneClient) {
  489. return Error_NetBroken;
  490. }
  491. if((m_strSIPCallNum[nSipServer]=="")&&(m_strSIPProxyIP[nSipServer]=="")&&(m_iSIPProxyPort[nSipServer]==0)){
  492. return Error_Param;
  493. }
  494. CSimpleStringA strCode;
  495. PhoneService_MakeCall_Req Req;
  496. PhoneService_MakeCall_Ans Ans;
  497. Req.to_uri = MakeCallUri(hint_callnum,nSipServer);
  498. PhoneService_SetCallingParam_Info info;
  499. info.CallType = NORMAL_CALLTYPE;
  500. if (DOUBLERECORD_CALLTYPE == m_CallingParam.nCallType){
  501. info.CallType = DOUBLERECORD_CALLTYPE;
  502. }
  503. (*m_pPhoneClient)(EntityResource::getLink().upgradeLink())->SetCallingParam(info);
  504. ErrorCodeEnum Error = (*m_pPhoneClient)(EntityResource::getLink().upgradeLink())->MakeCall(Req, Ans, 10000);
  505. return Error;
  506. }
  507. ErrorCodeEnum MakeCall(const char *to_uri, const char *from_uri, const char *call_id, CallingParam callingparam)
  508. {
  509. if (!m_pPhoneClient){
  510. return Error_NetBroken;
  511. }
  512. CSimpleStringA strCode;
  513. PhoneService_MakeCall_Req Req;
  514. PhoneService_MakeCall_Ans Ans;
  515. Req.to_uri = to_uri;
  516. Req.from_uri = from_uri;
  517. Req.call_id = call_id;
  518. //设置呼叫参数
  519. PhoneService_SetCallingParam_Info info;
  520. info.CallType = callingparam.nCallType;
  521. info.connect_session = callingparam.connect_session;
  522. info.connect_ip = callingparam.connect_ip;
  523. info.connect_port = callingparam.connect_port;
  524. (*m_pPhoneClient)(EntityResource::getLink().upgradeLink())->SetCallingParam(info);
  525. ErrorCodeEnum Error = (*m_pPhoneClient)(EntityResource::getLink().upgradeLink())->MakeCall(Req, Ans, 10000);
  526. return Error;
  527. }
  528. ErrorCodeEnum HangupCall()
  529. {
  530. if (!m_pPhoneClient) {
  531. return Error_NetBroken;
  532. }
  533. PhoneService_HangupCall_Req Req;
  534. PhoneService_HangupCall_Ans Ans;
  535. return (*m_pPhoneClient)(EntityResource::getLink().upgradeLink())->HangupCall(Req, Ans, 10000);
  536. }
  537. ErrorCodeEnum ReleaseCall(int type)
  538. {
  539. if (!m_pPhoneClient)
  540. return Error_NetBroken;
  541. PhoneService_ReleaseCall_Req Req;
  542. Req.type = type;
  543. PhoneService_ReleaseCall_Ans Ans;
  544. return (*m_pPhoneClient)(EntityResource::getLink().upgradeLink())->ReleaseCall(Req, Ans, 10000);
  545. }
  546. ErrorCodeEnum StartChannel(int nChanServer)
  547. {
  548. if (!m_pChannelClient) {
  549. return Error_NetBroken;
  550. }
  551. if ((m_strChanProxyIP[nChanServer]=="")&&(m_strChanCallNum[nChanServer]=="")&&(m_iChanProxyPort[nChanServer]==0)){
  552. return Error_Param;
  553. }
  554. ChannelService_Connect_Req Req;
  555. ChannelService_Connect_Ans Ans;
  556. Req.callno = m_strChanCallNum[nChanServer];
  557. Req.ip = m_strChanProxyIP[nChanServer];
  558. Req.port = m_iChanProxyPort[nChanServer];
  559. return m_pChannelClient->Connect(Req, Ans, 10000);
  560. }
  561. ErrorCodeEnum StartChannel(int nChanServer,CallingParam callparam)
  562. {
  563. if (!m_pChannelClient) {
  564. return Error_NetBroken;
  565. }
  566. ChannelService_Connect_Req Req;
  567. ChannelService_Connect_Ans Ans;
  568. if ((m_strChanProxyIP[nChanServer]=="")&&(m_strChanCallNum[nChanServer]=="")&&(m_iChanProxyPort[nChanServer]==0)){
  569. return Error_Param;
  570. }
  571. else{
  572. Req.callno = m_strChanCallNum[nChanServer];
  573. Req.ip = m_strChanProxyIP[nChanServer];
  574. Req.port = m_iChanProxyPort[nChanServer];
  575. Req.etype = NORMAL_CALLTYPE;
  576. return m_pChannelClient->Connect(Req, Ans, 10000);
  577. }
  578. }
  579. ErrorCodeEnum StopChannel()
  580. {
  581. if (!m_pPhoneClient) {
  582. return Error_NetBroken;
  583. }
  584. ChannelService_Close_Req Req;
  585. ChannelService_Close_Ans Ans;
  586. return m_pChannelClient->Close(Req, Ans, 10000);
  587. }
  588. CSimpleStringA MakeCallUri(const char *callnum,const char *serverip,int serverport)
  589. {
  590. return CSimpleStringA::Format("sip:%s@%s:%d;transport=UDP",
  591. callnum, serverip, serverport);
  592. }
  593. CSimpleStringA MakeCallUri(const char *hint_callnum,int nServer)
  594. {
  595. if (m_iSIPProxyPort[nServer] == 5060)
  596. {
  597. return CSimpleStringA::Format("sip:%s@%s;transport=UDP",
  598. hint_callnum, m_strSIPProxyIP[nServer].GetData());
  599. }
  600. else
  601. {
  602. return CSimpleStringA::Format("sip:%s@%s:%d;transport=UDP",
  603. hint_callnum, m_strSIPProxyIP[nServer].GetData(), m_iSIPProxyPort[nServer]);
  604. }
  605. }
  606. ErrorCodeEnum LoadConfig()
  607. {
  608. int iTimeOut = RVC_DEFAULT_HTTPTIMEOUT;
  609. int iPrintDbg = 0;
  610. SpIniMappingTable table;
  611. table.AddEntryString("CounterConnector", "sip_proxy_ip", m_strSIPProxyIP[0], NULL);
  612. table.AddEntryInt("CounterConnector", "sip_proxy_port", m_iSIPProxyPort[0], 0);
  613. table.AddEntryString("CounterConnector", "sip_default_call_no", m_strSIPCallNum[0], NULL);
  614. table.AddEntryString("CounterConnector", "chan_proxy_ip", m_strChanProxyIP[0], NULL);
  615. table.AddEntryInt("CounterConnector", "chan_proxy_port", m_iChanProxyPort[0], 0);
  616. table.AddEntryString("CounterConnector", "chan_default_call_no", m_strChanCallNum[0], NULL);
  617. table.AddEntryString("CounterConnector", "sip_proxy_ip_backup", m_strSIPProxyIP[1], NULL);
  618. table.AddEntryInt("CounterConnector", "sip_proxy_port_backup", m_iSIPProxyPort[1], 0);
  619. table.AddEntryString("CounterConnector", "sip_default_call_no_backup", m_strSIPCallNum[1], NULL);
  620. table.AddEntryString("CounterConnector", "chan_proxy_ip_backup", m_strChanProxyIP[1], NULL);
  621. table.AddEntryInt("CounterConnector", "chan_proxy_port_backup", m_iChanProxyPort[1], 0);
  622. table.AddEntryString("CounterConnector", "chan_default_call_no_backup", m_strChanCallNum[1], NULL);
  623. table.AddEntryString("CounterConnector", "default_voip_server", m_strDefaultServer, NULL);
  624. table.AddEntryString("CounterConnector", "http_call_route_addr", m_strHttpCallRouteAddr, NULL);
  625. table.AddEntryInt("CounterConnector", "http_timeout", iTimeOut, 0);
  626. table.AddEntryInt("CounterConnector", "http_printdbg", iPrintDbg, 0);
  627. CSmartPointer<IConfigInfo> spConfig;
  628. ErrorCodeEnum Error = m_pEntity->GetFunction()->OpenConfig(Config_CenterSetting, spConfig);
  629. if (Error == Error_Succeed) {
  630. Error = table.Load(spConfig);
  631. if (Error_Succeed == Error) {
  632. if (iTimeOut > 0 && iTimeOut < 20 * RVC_DEFAULT_HTTPTIMEOUT) {
  633. m_iHttpTimeOut = iTimeOut;
  634. }
  635. if (iPrintDbg) {
  636. m_bHttpPrinttDbg = true;
  637. }
  638. }
  639. }
  640. return Error;
  641. }
  642. ErrorCodeEnum LoadTerminalId()
  643. {
  644. CSystemStaticInfo Info;
  645. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetSystemStaticInfo(Info);
  646. if (Error == Error_Succeed) {
  647. m_strTerminalId = Info.strTerminalID;
  648. }
  649. return Error;
  650. }
  651. ErrorCodeEnum AllowAgentWrite()
  652. {
  653. if (!m_pChannelClient)
  654. return Error_NetBroken;
  655. ChannelService_Send_Info Info;
  656. Info.compress = false;
  657. Info.encrypt = false;
  658. Info.id = 0;
  659. Info.sub_type = 0;
  660. Info.type = ACM_TYPE_MODE;
  661. return (*m_pChannelClient)(EntityResource::getLink().upgradeLink())->Send(Info);
  662. }
  663. ErrorCodeEnum StartRing()
  664. {
  665. #ifdef _WIN32
  666. bool bRet = ::PlaySoundA(MAKEINTRESOURCEA(IDR_RINGOUT), ModuleBase::GetModuleBase()->GetInstance(), SND_ASYNC|SND_RESOURCE|SND_LOOP);
  667. if (bRet) {
  668. m_bRing = true;
  669. return Error_Succeed;
  670. }
  671. else {
  672. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("play sound failed!%d", GetLastError());
  673. return Error_Unexpect;
  674. }
  675. #else
  676. m_bRing = TRUE;
  677. return Error_Succeed;
  678. #endif
  679. }
  680. void StopRing()
  681. {
  682. #ifdef _WIN32
  683. if (m_bRing) {
  684. bool bRet = false;
  685. int tries = 0;
  686. do {
  687. bRet = ::PlaySoundA(NULL, NULL, SND_ASYNC|SND_LOOP);
  688. } while (bRet == false && (Sleep(100), tries < 3));
  689. m_bRing = false;
  690. }
  691. #else
  692. if (m_bRing) {
  693. m_bRing = false;
  694. }
  695. #endif
  696. }
  697. bool CheckBeginRing(int iSrcState, int iDstState)
  698. {
  699. if (iSrcState == ACMCallFSM::s0 && iDstState == ACMCallFSM::s10)
  700. return true;
  701. return false;
  702. }
  703. bool CheckEndRing(int iSrcState, int iDstState)
  704. {
  705. if (m_bRing) {
  706. if (iSrcState == ACMCallFSM::s10 || iSrcState == ACMCallFSM::s12) {
  707. if (iDstState != ACMCallFSM::s10 && iDstState != ACMCallFSM::s12)
  708. return true;
  709. }
  710. }
  711. return false;
  712. }
  713. int GetDelayTime();
  714. void SetDelayTime();
  715. int ParseDefaultServer(const char* strServer);
  716. int GetFailedErrorCode(int iSrcState);
  717. int LogFailedWarns(int iFailedCode, const char* strmsg);
  718. public:
  719. //话机的物理状态
  720. bool m_bHandFree;
  721. //坐席控制话筒的当前状态
  722. bool m_bAgentHandFree;
  723. //是否处于接通坐席状态
  724. bool m_bIsAgentControl;
  725. //流程拨号,带技能号
  726. CSimpleStringA m_strHintCallNum;
  727. bool m_bAssistchanIdel;
  728. CurServerNum m_nCurSipServer;
  729. CurServerNum m_nCurChanServer;
  730. //呼叫类型,用于区分是分布式呼叫还是常规呼叫,由业务中台设置或者指令模块设置
  731. CallingParam m_CallingParam;
  732. int m_iFailedLastState;
  733. bool m_bConAssist;
  734. bool m_bConSipphone;
  735. int m_iCallRouteType;
  736. CSimpleStringA m_strQueueName;
  737. CSimpleStringA m_strAddClientLevel;
  738. bool m_bNeedQueueName;
  739. private:
  740. unsigned int m_nStarttime;
  741. PhoneService_ClientBase *m_pPhoneClient;
  742. ChannelService_ClientBase *m_pChannelClient;
  743. SyncService_ClientBase*m_pSyncServiceClient;
  744. CSimpleStringA m_strTerminalId;
  745. CSimpleStringA m_strSIPProxyIP[2];
  746. int m_iSIPProxyPort[2];
  747. CSimpleStringA m_strSIPCallNum[2];
  748. CSimpleStringA m_strChanProxyIP[2];
  749. int m_iChanProxyPort[2];
  750. CSimpleStringA m_strChanCallNum[2];
  751. //去分行化
  752. node_list_head_t *m_pCallRouteList;
  753. CSimpleStringA m_strDefaultServer;
  754. std::vector<std::string> m_voipserver;
  755. ErrorCodeEnum m_LastSipError;
  756. ErrorCodeEnum m_LastAssistError;
  757. int m_iNetAdapterNum;
  758. CSimpleStringA m_strHttpCallRouteAddr;
  759. CSimpleStringA m_strHttpServerAPI;
  760. int m_iHttpTimeOut;
  761. bool m_bHttpPrinttDbg;
  762. bool m_bRing;
  763. };