中间件底层,websocket

FsProxy.cpp 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. #include "StdAfx.h"
  2. #include "FsProxy.h"
  3. #include "ChanExten.h"
  4. #include "ChanTrunk.h"
  5. #include "Session.h"
  6. #include "OperationReactor.h"
  7. #include "../IVR/IvrSysInc.h"
  8. SINGLETON_IMPLEMENT(CFsProxy)
  9. CFsProxy::CFsProxy(void) : m_Gateway(this), m_pEventHandler(NULL)
  10. {
  11. }
  12. CFsProxy::~CFsProxy(void)
  13. {
  14. }
  15. /*****************************************************************
  16. **【函数名称】 __transLogicState2CtiState
  17. **【函数功能】 将逻辑状态转换为CTI识别的状态
  18. **【参数】
  19. **【返回值】
  20. ****************************************************************/
  21. UINT CFsProxy::__transLogicState2CtiState(DEV_RES_TYPE ChanType, CHAN_LOGIC_STATE State)
  22. {
  23. if (ChanType == DEV_RES_TYPE_EXT)
  24. {
  25. UINT HoldMask = State & HELD_STATE_IND_MASK;
  26. switch (State & HELD_STATE_FILTER_MASK)
  27. {
  28. case CHAN_LOGIC_STATE_DISABLED: return INNER_STATE_DISABLED; // 不可用
  29. case CHAN_LOGIC_STATE_FREE: return INNER_STATE_FREE | HoldMask; // 空闲
  30. case CHAN_LOGIC_STATE_INIT: return INNER_STATE_INIT | HoldMask; // 摘机等待拨号
  31. case CHAN_LOGIC_STATE_DIALING: return INNER_STATE_DIALING | HoldMask; // 拨号
  32. case CHAN_LOGIC_STATE_RING_BACK: return INNER_STATE_RING_BACK | HoldMask; // 呼出振铃
  33. case CHAN_LOGIC_STATE_ALERTING: return INNER_STATE_ALERTING | HoldMask; // 来电振铃
  34. case CHAN_LOGIC_STATE_TALKING: return INNER_STATE_TALKING | HoldMask; // 通话中
  35. default:
  36. ASSERT(FALSE);
  37. return INNER_STATE_DISABLED;
  38. }
  39. }
  40. else
  41. {
  42. switch (State)
  43. {
  44. case CHAN_LOGIC_STATE_DISABLED: return TRUNK_STATE_DISABLED; // 不可用
  45. case CHAN_LOGIC_STATE_FREE: return TRUNK_STATE_FREE; // 空闲
  46. case CHAN_LOGIC_STATE_DIALING: return TRUNK_STATE_DIALING; // 拨号
  47. case CHAN_LOGIC_STATE_RING_BACK: return TRUNK_STATE_RING_BACK; // 呼出振铃
  48. case CHAN_LOGIC_STATE_ALERTING: return TRUNK_STATE_ALERTING; // 来电振铃
  49. case CHAN_LOGIC_STATE_STANDBY:
  50. case CHAN_LOGIC_STATE_TALKING: return TRUNK_STATE_TALKING; // 通话中
  51. default:
  52. ASSERT(FALSE);
  53. return TRUNK_STATE_UNKNOWN;
  54. }
  55. }
  56. }
  57. /*****************************************************************
  58. **【函数名称】 __addExten
  59. **【函数功能】 添加分机
  60. **【参数】
  61. **【返回值】
  62. ****************************************************************/
  63. void CFsProxy::__addExten(UINT ExtenNo)
  64. {
  65. std::lock_guard<std::mutex> lock(m_LockMapChanExt);
  66. CChanExten* pExten = new(std::nothrow) CChanExten(this, ExtenNo);
  67. if (pExten == NULL)return;
  68. m_MapChanExt.SetAt(ExtenNo, pExten);
  69. pExten->regist();
  70. }
  71. /*****************************************************************
  72. **【函数名称】 delExten
  73. **【函数功能】 删除分机
  74. **【参数】
  75. **【返回值】
  76. ****************************************************************/
  77. void CFsProxy::__delExten(UINT ExtenNo)
  78. {
  79. std::lock_guard<std::mutex> lock(m_LockMapChanExt);
  80. CChanExten* pExten = NULL;
  81. if (m_MapChanExt.Lookup(ExtenNo, pExten))
  82. {
  83. if (pExten->isFree())
  84. {
  85. m_MapChanExt.RemoveKey(ExtenNo);
  86. FS_LINK_DELETE(pExten);
  87. }
  88. else pExten->discard(true);
  89. }
  90. }
  91. /*****************************************************************
  92. **【函数名称】 __freeExten
  93. **【函数功能】 清空分机
  94. **【参数】
  95. **【返回值】
  96. ****************************************************************/
  97. void CFsProxy::__freeExten(void)
  98. {
  99. std::lock_guard<std::mutex> lock(m_LockMapChanExt);
  100. UINT ExtenNo = 0;
  101. CChanExten* pExten = NULL;
  102. POSITION Pos = m_MapChanExt.GetStartPosition();
  103. while (Pos != NULL)
  104. {
  105. m_MapChanExt.GetNextAssoc(Pos, ExtenNo, pExten);
  106. FS_LINK_DELETE(pExten);
  107. }
  108. m_MapChanExt.RemoveAll();
  109. }
  110. /*****************************************************************
  111. **【函数名称】 __initTrunkChan
  112. **【函数功能】 初始化中继通道
  113. **【参数】
  114. **【返回值】
  115. ****************************************************************/
  116. void CFsProxy::__initTrunkChan(void)
  117. {
  118. std::lock_guard<std::mutex> lock(m_LockArrayTrunk);
  119. for (int i = 1; i <= CConfig::trunkCount(); ++i)
  120. {
  121. CChanTrunk* pTrunk = new(std::nothrow) CChanTrunk(this, i);
  122. if (pTrunk) {
  123. m_ArrayTrunk.Add(pTrunk);
  124. pTrunk->regist();
  125. }
  126. }
  127. }
  128. /*****************************************************************
  129. **【函数名称】 __freeTrunkChan
  130. **【函数功能】 清空中继通道
  131. **【参数】
  132. **【返回值】
  133. ****************************************************************/
  134. void CFsProxy::__freeTrunkChan(void)
  135. {
  136. std::lock_guard<std::mutex> lock(m_LockArrayTrunk);
  137. CChanTrunk* pTrunk = NULL;
  138. for (int i = 0; i < m_ArrayTrunk.GetCount(); ++i)
  139. {
  140. pTrunk = m_ArrayTrunk[i];
  141. FS_LINK_DELETE(pTrunk);
  142. }
  143. m_ArrayTrunk.RemoveAll();
  144. }
  145. /*****************************************************************
  146. **【函数名称】 __getSession
  147. **【函数功能】 查找会话
  148. **【参数】
  149. **【返回值】
  150. ****************************************************************/
  151. CSession* CFsProxy::__getSession(PCHAN_EVENT_NOTIFY pNotify, bool NewWhenNull /*= false*/)
  152. {
  153. CSession* pSession = __getSession(pNotify->CallId);
  154. if (pSession == NULL && NewWhenNull)
  155. {
  156. pSession = new(std::nothrow) CSession(this, pNotify->CallId);
  157. if (pSession) {
  158. pSession->prepare(pNotify);
  159. std::lock_guard<std::mutex> lock(m_LockMapSesssoin);
  160. m_MapSession.SetAt(pNotify->CallId, pSession);
  161. LOGGER(LOG_LEVEL_NORMAL, _T("{CFsProxy}:Add Session[%s],ChanId[%s],EventId[%d],HangupCause[%s|%s]"), pSession->id(), pNotify->ChanId, pNotify->EventId, pNotify->HangupCause, pNotify->HangupDisposition);
  162. }
  163. }
  164. return pSession;
  165. }
  166. /*****************************************************************
  167. **【函数名称】 __getSession
  168. **【函数功能】 查找会话
  169. **【参数】
  170. **【返回值】
  171. ****************************************************************/
  172. CSession* CFsProxy::__getSession(LPCTSTR SessionId)
  173. {
  174. std::lock_guard<std::mutex> lock(m_LockMapSesssoin);
  175. CSession* pSession = NULL;
  176. m_MapSession.Lookup(SessionId, pSession);
  177. return pSession;
  178. }
  179. /*****************************************************************
  180. **【函数名称】 __delSession
  181. **【函数功能】 删除会话
  182. **【参数】
  183. **【返回值】
  184. ****************************************************************/
  185. void CFsProxy::__delSession(LPCTSTR SessionId)
  186. {
  187. if (SessionId == NULL)
  188. return;
  189. CSession* pSession = NULL;
  190. std::lock_guard<std::mutex> lock(m_LockMapSesssoin);
  191. if (m_MapSession.Lookup(SessionId, pSession))
  192. {
  193. m_MapSession.RemoveKey(SessionId);
  194. LOGGER(LOG_LEVEL_NORMAL, _T("{FsProxy}: Delete Session[%s]"), pSession->id());
  195. FS_LINK_DELETE(pSession);
  196. }
  197. }
  198. /*****************************************************************
  199. **【函数名称】 __freeSession
  200. **【函数功能】 清空会话
  201. **【参数】
  202. **【返回值】
  203. ****************************************************************/
  204. void CFsProxy::__freeSession(void)
  205. {
  206. CString SessionId = 0;
  207. CSession* pSession = NULL;
  208. std::lock_guard<std::mutex> lock(m_LockMapSesssoin);
  209. POSITION Pos = m_MapSession.GetStartPosition();
  210. while (Pos != NULL)
  211. {
  212. m_MapSession.GetNextAssoc(Pos, SessionId, pSession);
  213. FS_LINK_DELETE(pSession);
  214. }
  215. m_MapSession.RemoveAll();
  216. }
  217. /*****************************************************************
  218. **【函数名称】 __kill
  219. **【函数功能】 挂断通道
  220. **【参数】
  221. **【返回值】
  222. ****************************************************************/
  223. bool CFsProxy::__kill(LONG JobId, LPCTSTR ChanId)
  224. {
  225. CString EslCmd;
  226. if (JobId == FS_LINK_JOBID_INVALID)
  227. EslCmd.Format(_T("bgapi uuid_kill %s"), ChanId);
  228. else
  229. EslCmd.Format(_T("bgapi uuid_kill %s\r\n%s: %ld"), ChanId, ESL_HEADER_JOB_UUID, JobId);
  230. return m_Gateway.sendCmd(EslCmd);
  231. }
  232. /*****************************************************************
  233. **【函数名称】 init
  234. **【函数功能】 初始化
  235. **【参数】
  236. **【返回值】
  237. ****************************************************************/
  238. bool CFsProxy::init(IEslEventHandler* pEventHandler)
  239. {
  240. ASSERT(pEventHandler != NULL);
  241. m_pEventHandler = pEventHandler;
  242. __initTrunkChan();
  243. if (m_Gateway.start())
  244. {
  245. m_Gateway.hangupAll();
  246. if (m_Gateway.scanExten())
  247. {
  248. LOGGER(LOG_LEVEL_NORMAL, _T("{FsProxy}: FreeSWITCH代理初始化成功"));
  249. return true;
  250. }
  251. else
  252. {
  253. LOGGER(LOG_LEVEL_ERROR, _T("{FsProxy}: FreeSWITCH代理初始化失败, 扫描分机资源失败"));
  254. return false;
  255. }
  256. }
  257. else
  258. {
  259. LOGGER(LOG_LEVEL_ERROR, _T("{FsProxy}: FreeSWITCH代理初始化失败, ESL网关启动失败"));
  260. m_pEventHandler = NULL;
  261. return false;
  262. }
  263. }
  264. /*****************************************************************
  265. **【函数名称】 release
  266. **【函数功能】 释放资源
  267. **【参数】
  268. **【返回值】
  269. ****************************************************************/
  270. void CFsProxy::release(void)
  271. {
  272. esl_disconnect(&m_EslHandle);
  273. m_Gateway.stop();
  274. __freeSession();
  275. __freeExten();
  276. __freeTrunkChan();
  277. }
  278. /*****************************************************************
  279. **【函数名称】 onChanRegist
  280. **【函数功能】 通道注册的处理函数
  281. **【参数】
  282. **【返回值】
  283. ****************************************************************/
  284. void CFsProxy::onChanRegist(DEV_RES_TYPE ChanType, UINT ChanNo, CHAN_LOGIC_STATE ChanState)
  285. {
  286. CDevControl::GetInstance().onEventResDetail(ChanType, ChanNo);
  287. CDevControl::GetInstance().onEventResState(ChanType, ChanNo, __transLogicState2CtiState(ChanType, ChanState));
  288. }
  289. /*****************************************************************
  290. **【函数名称】 onExtenDestroy
  291. **【函数功能】 分机销毁的处理函数
  292. **【参数】
  293. **【返回值】
  294. ****************************************************************/
  295. void CFsProxy::onExtenDestroy(UINT ExtenNo)
  296. {
  297. CDevControl::GetInstance().onEventResState(DEV_RES_TYPE_EXT, ExtenNo, INNER_STATE_REMOVE);
  298. }
  299. /*****************************************************************
  300. **【函数名称】 onExtenDirectOp
  301. **【函数功能】 分机直接操作启动
  302. **【参数】 OpType 启动的操作类型
  303. pHostChan 触发事件的通道
  304. pNotify 触发操作事件内容
  305. **【返回值】
  306. ****************************************************************/
  307. void CFsProxy::onExtenDirectOp(DEV_OP OpType, CVirtualChan* pHostChan, PCHAN_EVENT_NOTIFY pNotify)
  308. {
  309. switch (OpType)
  310. {
  311. case DEV_OP_CALL_OUT:
  312. {
  313. if (!COperationReactor::GetInstance().onExtenCallFromDev(pHostChan, pNotify))
  314. __kill(FS_LINK_JOBID_INVALID, pHostChan->chanId());
  315. }
  316. break;
  317. } // end switch
  318. }
  319. /*****************************************************************
  320. **【函数名称】 onChanStateUpdate
  321. **【函数功能】 通道状态更新处理
  322. **【参数】
  323. **【返回值】
  324. *****************************************************************/
  325. void CFsProxy::onChanStateUpdate(LONG OpInstance, CVirtualChan* pChan)
  326. {
  327. if (OpInstance != FS_LINK_INSTANCE_INVALID)
  328. m_pEventHandler->onEslEvtChanState(OpInstance, pChan);
  329. CDevControl::GetInstance().onEventResState(pChan->type(), pChan->no(), __transLogicState2CtiState(pChan->type(), pChan->state()), pChan->callerNum(), pChan->calleeNum(),pChan->ActiveHanguper(),pChan->TmpChanID());
  330. }
  331. /*****************************************************************
  332. **【函数名称】 onChanPoor
  333. **【函数功能】 空闲通道枯竭处理
  334. **【参数】
  335. **【返回值】
  336. *****************************************************************/
  337. void CFsProxy::onChanPoor(CSession* pSession, PCHAN_EVENT_NOTIFY pNotify)
  338. {
  339. __kill(FS_LINK_JOBID_INVALID, pNotify->ChanId);
  340. }
  341. /*****************************************************************
  342. **【函数名称】 onTrunkCallIn
  343. **【函数功能】 中继呼入
  344. **【参数】
  345. **【返回值】
  346. *****************************************************************/
  347. void CFsProxy::onTrunkCallIn(CChanTrunk* pTrunk)
  348. {
  349. CDevControl::GetInstance().onEventDevOperation(pTrunk->no(), DEV_OP_CALL_IN, pTrunk->callerNum(), pTrunk->calleeNum());
  350. }
  351. /*****************************************************************
  352. **【函数名称】 onEslDisconnect
  353. **【函数功能】 ESL连接中断处理
  354. **【参数】
  355. **【返回值】
  356. *****************************************************************/
  357. void CFsProxy::onEslDisconnect(void)
  358. {
  359. CDevControl::GetInstance().onEventDevDown();
  360. }
  361. /*****************************************************************
  362. **【函数名称】 onEslSipReg
  363. **【函数功能】 分机注册的处理函数
  364. **【参数】
  365. **【返回值】
  366. ****************************************************************/
  367. void CFsProxy::onEslExtenReg(UINT ExtenNo)
  368. {
  369. CChanExten* pExten = getExten(ExtenNo);
  370. if (pExten == NULL)
  371. {
  372. __addExten(ExtenNo);
  373. }
  374. else
  375. {
  376. if (pExten->isVoid()) // 若被丢弃,则改变丢弃状态
  377. pExten->discard(false);
  378. }
  379. #ifdef _DEBUG
  380. LOGGER(LOG_LEVEL_NORMAL, _T("{FsProxy}: 分机[%u]注册"), ExtenNo);
  381. #endif
  382. }
  383. /*****************************************************************
  384. **【函数名称】 onEslExtenUnreg
  385. **【函数功能】 分机注销的处理函数
  386. **【参数】
  387. **【返回值】
  388. ****************************************************************/
  389. void CFsProxy::onEslExtenUnreg(UINT ExtenNo)
  390. {
  391. __delExten(ExtenNo);
  392. #ifdef _DEBUG
  393. LOGGER(LOG_LEVEL_NORMAL, _T("{FsProxy}: 分机[%u]注销"), ExtenNo);
  394. #endif
  395. }
  396. /*****************************************************************
  397. **【函数名称】 onEslGwExten
  398. **【函数功能】 网关分机的处理函数
  399. **【参数】
  400. **【返回值】
  401. ****************************************************************/
  402. void CFsProxy::onEslGwExtenReg(LPCTSTR ExtenNo, LPCTSTR Ip)
  403. {
  404. m_MapAddrGwExt.SetAt(ExtenNo, Ip);
  405. return;
  406. }
  407. void CFsProxy::onEslGwExtenUnreg(LPCTSTR ExtenNo)
  408. {
  409. CString t_ip;
  410. if (m_MapAddrGwExt.Lookup(ExtenNo, t_ip))
  411. {
  412. t_ip.Empty();
  413. m_MapAddrGwExt.RemoveKey(ExtenNo);
  414. }
  415. }
  416. /*****************************************************************
  417. **【函数名称】 onEslEvtBgJobDone
  418. **【函数功能】 后台任务执行结束事件处理
  419. **【参数】
  420. **【返回值】
  421. *****************************************************************/
  422. void CFsProxy::onEslEvtBgJobDone(PBG_JOB_NOTIFY pNotify)
  423. {
  424. ASSERT(m_pEventHandler != NULL);
  425. m_pEventHandler->onEslEvtBgJobDone(pNotify);
  426. }
  427. /*****************************************************************
  428. **【函数名称】 onEslEvtChannel
  429. **【函数功能】 通道事件处理
  430. **【参数】
  431. **【返回值】
  432. *****************************************************************/
  433. void CFsProxy::onEslEvtChannel(PCHAN_EVENT_NOTIFY pNotify)
  434. {
  435. ASSERT(pNotify != NULL);
  436. if (pNotify == NULL)
  437. return;
  438. CSession* pSession = __getSession(pNotify, true);
  439. ASSERT(pSession != NULL);
  440. if (pSession == NULL)
  441. {
  442. LOGGER(LOG_LEVEL_WARNING, _T("{FsProxy}: 通道事件,没有找到会话,EslEventId=%d,ChanId[%s],CallId[%s],Caller[%s],Callee[%s]"), pNotify->EventId, pNotify->ChanId, pNotify->CallId, pNotify->Caller, pNotify->Callee);
  443. return;
  444. }
  445. __try {
  446. pSession->onChanEvent(pNotify);
  447. if (pSession->isVoid())
  448. __delSession(pSession->id());
  449. }
  450. __except (EXCEPTION_EXECUTE_HANDLER)
  451. {
  452. LOGGER(LOG_LEVEL_WARNING, _T("{FsProxy}: 处理通道事件时异常[%u],EslEventId=%d,ChanId[%s],CallId[%s],Caller[%s],Callee[%s]"), GetExceptionCode(), pNotify->EventId, pNotify->ChanId, pNotify->CallId, pNotify->Caller, pNotify->Callee);
  453. __delSession(pSession->id());
  454. }
  455. }
  456. /*****************************************************************
  457. **【函数名称】 onEslEvtDtmf
  458. **【函数功能】 DTMF事件处理
  459. **【参数】
  460. **【返回值】
  461. *****************************************************************/
  462. void CFsProxy::onEslEvtDtmf(PDTMF_NOTIFY pNotify)
  463. {
  464. ASSERT(pNotify != NULL);
  465. CSession* pSession = __getSession(pNotify->CallId);
  466. ASSERT(pSession != NULL);
  467. if (pSession != NULL)
  468. pSession->onChanDtmf(pNotify);
  469. }
  470. /*****************************************************************
  471. **【函数名称】 onEslEvtHold
  472. **【函数功能】 保持事件处理
  473. **【参数】
  474. **【返回值】
  475. *****************************************************************/
  476. void CFsProxy::onEslEvtHold(PHOLD_NOTIFY pNotify)
  477. {
  478. ASSERT(pNotify != NULL);
  479. CSession* pSession = __getSession(pNotify->CallId);
  480. ASSERT(pSession != NULL);
  481. if (pSession != NULL)
  482. pSession->onChanHold(pNotify);
  483. }
  484. /*****************************************************************
  485. **【函数名称】 getExten
  486. **【函数功能】 查找分机
  487. **【参数】
  488. **【返回值】
  489. ****************************************************************/
  490. CChanExten* CFsProxy::getExten(UINT ExtenNo)
  491. {
  492. std::lock_guard<std::mutex> lock(m_LockMapChanExt);
  493. CChanExten* pExten = NULL;
  494. m_MapChanExt.Lookup(ExtenNo, pExten);
  495. return pExten;
  496. }
  497. /*****************************************************************
  498. **【函数名称】 getTrunk
  499. **【函数功能】 查找中继
  500. **【参数】
  501. **【返回值】
  502. ****************************************************************/
  503. CChanTrunk* CFsProxy::getTrunk(UINT TrunkNo)
  504. {
  505. std::lock_guard<std::mutex> lock(m_LockArrayTrunk);
  506. if (TrunkNo <= 0 || TrunkNo > (UINT)m_ArrayTrunk.GetCount())
  507. return NULL;
  508. --TrunkNo;
  509. return m_ArrayTrunk[TrunkNo];
  510. }
  511. /*****************************************************************
  512. **【函数名称】 delChan
  513. **【函数功能】 删除通道
  514. **【参数】
  515. **【返回值】
  516. ****************************************************************/
  517. void CFsProxy::delChan(CVirtualChan* pChan)
  518. {
  519. if (pChan->type() == DEV_RES_TYPE_EXT)
  520. __delExten(pChan->no());
  521. }
  522. /*****************************************************************
  523. **【函数名称】 getFreeTrunk
  524. **【函数功能】 查找空闲中继
  525. **【参数】
  526. **【返回值】
  527. ****************************************************************/
  528. CChanTrunk* CFsProxy::getFreeTrunk(void)
  529. {
  530. // 当前轮循到的索引
  531. static int PosStatic = 0;
  532. int CurPos = PosStatic;
  533. int BusyTruckCount = 0;
  534. std::lock_guard<std::mutex> lock(m_LockArrayTrunk);
  535. // 保证遍历一轮
  536. for (int i = 0; i < m_ArrayTrunk.GetCount(); ++i)
  537. {
  538. // 保证POS是有效的
  539. if (PosStatic >= m_ArrayTrunk.GetCount())
  540. PosStatic = 0;
  541. CChanTrunk* pTrunk = m_ArrayTrunk[PosStatic++];
  542. ASSERT(pTrunk != NULL);
  543. // 当前外线是否空闲
  544. if (pTrunk->isFree())
  545. {
  546. return pTrunk;
  547. }
  548. else
  549. {
  550. BusyTruckCount++;
  551. }
  552. }
  553. LOGGER(LOG_LEVEL_WARNING, _T("{FsProxy}: 没有空闲中继通道,起始索引=%d,当前索引=%d,中继总数量=%d,忙通道数量=%d"), CurPos, PosStatic, m_ArrayTrunk.GetCount(), BusyTruckCount);
  554. //2019.3.7
  555. PosStatic = CurPos;
  556. for (int i = 0; i < m_ArrayTrunk.GetCount(); ++i)
  557. {
  558. // 保证POS是有效的
  559. if (PosStatic >= m_ArrayTrunk.GetCount())
  560. PosStatic = 0;
  561. CChanTrunk* pTrunk = m_ArrayTrunk[PosStatic++];
  562. ASSERT(pTrunk != NULL);
  563. // 当前外线是否空闲
  564. if (pTrunk->state() == CHAN_LOGIC_STATE_FREE)
  565. {
  566. if (pTrunk->currOp() != NULL) {
  567. COperationReactor::GetInstance().releaseOpResult(pTrunk->currOp());
  568. pTrunk->releaseOp(pTrunk->currOp());
  569. LOGGER(LOG_LEVEL_WARNING, _T("{FsProxy}:没有空闲中继通道,通道[%u],释放操作."), pTrunk->no());
  570. }
  571. return pTrunk;
  572. }
  573. }
  574. LOGGER(LOG_LEVEL_WARNING, _T("{FsProxy}: 没有空闲中继通道,起始索引=%d,当前索引=%d"), CurPos, PosStatic);
  575. return NULL;
  576. }
  577. /*****************************************************************
  578. **【函数名称】 getAssoChanInSession
  579. **【函数功能】 获取会话中关联通道
  580. **【参数】
  581. **【返回值】
  582. ****************************************************************/
  583. CVirtualChan* CFsProxy::getAssoChanInSession(CVirtualChan* pChan)
  584. {
  585. CSession* pSession = __getSession(pChan->sessionId());
  586. if (pSession != NULL)
  587. return pSession->getAssoChan(pChan);
  588. else
  589. return NULL;
  590. }
  591. /*****************************************************************
  592. **【函数名称】 getBusyChan
  593. **【函数功能】 获取忙通道
  594. **【参数】
  595. **【返回值】
  596. *****************************************************************/
  597. CVirtualChan* CFsProxy::getBusyChan(LPCTSTR ChanId)
  598. {
  599. std::lock_guard<std::mutex> lock(m_LockMapBusyChan);
  600. CVirtualChan* pBusyChan = NULL;
  601. m_MapBusyChan.Lookup(ChanId, pBusyChan);
  602. return pBusyChan;
  603. }
  604. /*****************************************************************
  605. **【函数名称】 regBusyChan
  606. **【函数功能】 登记忙通道
  607. **【参数】
  608. **【返回值】
  609. *****************************************************************/
  610. void CFsProxy::regBusyChan(CVirtualChan* pChan)
  611. {
  612. std::lock_guard<std::mutex> lock(m_LockMapBusyChan);
  613. m_MapBusyChan.SetAt(pChan->chanId(), pChan);
  614. }
  615. /*****************************************************************
  616. **【函数名称】 unregBusyChan
  617. **【函数功能】 取消登记忙通道
  618. **【参数】
  619. **【返回值】
  620. *****************************************************************/
  621. void CFsProxy::unregBusyChan(CVirtualChan* pChan)
  622. {
  623. std::lock_guard<std::mutex> lock(m_LockMapBusyChan);
  624. m_MapBusyChan.RemoveKey(pChan->chanId());
  625. }
  626. /*****************************************************************
  627. **【函数名称】 ExtenCall
  628. **【函数功能】 分机呼叫
  629. **【参数】
  630. **【返回值】
  631. ****************************************************************/
  632. bool CFsProxy::ExtenCall(LONG JobId, CVirtualChan* pChan, LPCTSTR CallerNum, LPCTSTR CalleeNum)
  633. {
  634. CString EslCmd;
  635. /*EslCmd.Format(_T("bgapi originate {origination_caller_id_number=%lu,absolute_codec_string='PCMA,PCMU'}user/%lu %s XML %s\r\n%s: %ld"),
  636. pChan->no(), pChan->no(), CalleeNum, CConfig::extContext(), ESL_HEADER_JOB_UUID, JobId);*/
  637. CString CallString;
  638. CString Caller, Called;
  639. Called = CalleeNum;
  640. if (!m_CallStringMaker.makeCallStringWithDefault(Caller, Called, CallString))
  641. return false;
  642. EslCmd.Format(_T("bgapi originate {origination_caller_id_number=%s,%s=%lu,absolute_codec_string='PCMA,PCMU'}user/%lu %s XML %s\r\n%s: %ld"),
  643. Called, ESL_VAR_OP_CALLER, pChan->no(), pChan->no(), CalleeNum, CConfig::extContext(), ESL_HEADER_JOB_UUID, JobId);
  644. return m_Gateway.sendCmd(EslCmd);
  645. }
  646. /*****************************************************************
  647. **【函数名称】 PredictionCall
  648. **【函数功能】 预测外呼
  649. **【参数】
  650. **【返回值】
  651. ****************************************************************/
  652. bool CFsProxy::PredictionCall(LONG JobId, CString CallerNum, CString CalleeNum)
  653. {
  654. CString CallString;
  655. if (!m_CallStringMaker.makeCallStringWithDefault(CallerNum, CalleeNum, CallString))
  656. return false;
  657. CString EslCmd;
  658. EslCmd.Format(_T("bgapi originate {origination_caller_id_number=%s,%s=%ld}%s %s XML %s\r\n%s: %ld"),
  659. CallerNum, ESL_VAR_OP_INSTANCE, JobId, CallString, CalleeNum, CConfig::pCallContext(), ESL_HEADER_JOB_UUID, JobId);
  660. return m_Gateway.sendCmd(EslCmd);
  661. }
  662. /*****************************************************************
  663. **【函数名称】 answer
  664. **【函数功能】 应答通道
  665. **【参数】
  666. **【返回值】
  667. ****************************************************************/
  668. bool CFsProxy::answer(LONG JobId, CVirtualChan* pChan)
  669. {
  670. return false;
  671. }
  672. /*****************************************************************
  673. **【函数名称】 kill
  674. **【函数功能】 挂断通道
  675. **【参数】
  676. **【返回值】
  677. ****************************************************************/
  678. bool CFsProxy::kill(LONG JobId, CVirtualChan* pChan)
  679. {
  680. return __kill(JobId, pChan->chanId());
  681. }
  682. /*****************************************************************
  683. **【函数名称】 consult
  684. **【函数功能】 协商呼叫
  685. **【参数】
  686. **【返回值】
  687. ****************************************************************/
  688. bool CFsProxy::consult(LONG JobId, CVirtualChan* pChan, CString DestNum)
  689. {
  690. CString CallerNum;
  691. CString CallString;
  692. if (!m_CallStringMaker.makeCallString(CallerNum, DestNum, CallString))
  693. return false;
  694. CString EslCmd;
  695. EslCmd.Format(_T("bgapi uuid_broadcast %s att_xfer::%s\r\n%s: %ld"), pChan->chanId(), CallString, ESL_HEADER_JOB_UUID, JobId);
  696. return m_Gateway.sendCmd(EslCmd);
  697. }
  698. /*****************************************************************
  699. **【函数名称】 insert
  700. **【函数功能】 强插
  701. **【参数】
  702. **【返回值】
  703. ****************************************************************/
  704. bool CFsProxy::insert(LONG JobId, CVirtualChan* pChan, LPCTSTR DestSessionId)
  705. {
  706. CString EslCmd;
  707. EslCmd.Format(_T("bgapi originate {origination_caller_id_number=%lu}user/%lu &three_way(%s)\r\n%s: %ld"),
  708. pChan->no(), pChan->no(), DestSessionId, ESL_HEADER_JOB_UUID, JobId);
  709. return m_Gateway.sendCmd(EslCmd);
  710. }
  711. /*****************************************************************
  712. **【函数名称】 intercept
  713. **【函数功能】 强截
  714. **【参数】
  715. **【返回值】
  716. ****************************************************************/
  717. bool CFsProxy::intercept(LONG JobId, CVirtualChan* pChan, LPCTSTR DestChanId)
  718. {
  719. CString EslCmd;
  720. EslCmd.Format(_T("bgapi originate {origination_caller_id_number=%lu}user/%lu &intercept(%s)\r\n%s: %ld"),
  721. pChan->no(), pChan->no(), DestChanId, ESL_HEADER_JOB_UUID, JobId);
  722. return m_Gateway.sendCmd(EslCmd);
  723. }
  724. /*****************************************************************
  725. **【函数名称】 listen
  726. **【函数功能】 监听
  727. **【参数】
  728. **【返回值】
  729. ****************************************************************/
  730. bool CFsProxy::listen(LONG JobId, CVirtualChan* pChan, LPCTSTR DestChanId)
  731. {
  732. CString EslCmd;
  733. EslCmd.Format(_T("bgapi originate {origination_caller_id_number=%lu}user/%lu &eavesdrop(%s)\r\n%s: %ld"),
  734. pChan->no(), pChan->no(), DestChanId, ESL_HEADER_JOB_UUID, JobId);
  735. return m_Gateway.sendCmd(EslCmd);
  736. }
  737. // ych
  738. std::string CFsProxy::EslCommand(std::string strFsIp, int FsPort, std::string strName, std::string strPsWd, std::string strCommand)
  739. {
  740. if (!m_EslHandle.connected)
  741. {
  742. esl_connect(&m_EslHandle, strFsIp.c_str(), FsPort, strName.c_str(), strPsWd.c_str());
  743. }
  744. if (m_EslHandle.connected)
  745. {
  746. esl_send_recv_timed(&m_EslHandle, strCommand.c_str(), 2000);
  747. if (m_EslHandle.last_sr_event && m_EslHandle.last_sr_event->body)
  748. {
  749. std::string strRet = m_EslHandle.last_sr_event->body;
  750. return strRet;
  751. }
  752. }
  753. return "";
  754. }
  755. /*****************************************************************
  756. **【函数名称】 meeting
  757. **【函数功能】 会议
  758. **【参数】
  759. **【返回值】
  760. ****************************************************************/
  761. bool CFsProxy::meeting(LONG JobId, CString CallerNum, CString DestNum, LPCTSTR MeetingId)
  762. {
  763. bool bMatchPrefix = m_CallStringMaker.isMatchPrefix(DestNum);
  764. TRUNK_MATCH* pMatch = NULL;
  765. CString CallString;
  766. if (!m_CallStringMaker.makeCallString(CallerNum, DestNum, CallString, pMatch))
  767. return false;
  768. if (pMatch != NULL)
  769. {
  770. SIP_ACCOUNT* pAccount = CConfig::sipAccount().getAccount(pMatch->TrunkItemId);
  771. if (bMatchPrefix && pAccount != NULL && pAccount->IsDynamicGw) // 外线且动态网关
  772. {
  773. std::string strSipAccount = pAccount->Account;
  774. std::string strGwIp = EslCommand(CConfig::fsAddr(), CConfig::fsPort(), "", CConfig::fsPwd(), "api sofia_contact " + strSipAccount);
  775. if (!strGwIp.empty())
  776. {
  777. size_t Index = strGwIp.find_first_of('@');
  778. if (Index != std::string::npos)
  779. {
  780. strGwIp = strGwIp.substr(Index);
  781. CallString = "sofia/internal/";
  782. CallString += DestNum;
  783. CallString += strGwIp.c_str();
  784. }
  785. }
  786. }
  787. }
  788. LOGGER(LOG_LEVEL_NORMAL, _T("{FsProxy}: 三方会议呼叫字符串: %s"), CallString);
  789. CString EslCmd;
  790. EslCmd.Format(_T("bgapi originate {origination_caller_id_name=%s,origination_caller_id_number=%s,%s=%ld,absolute_codec_string='PCMA,PCMU'}%s %s XML %s\r\n%s: %ld"),
  791. CallerNum,CallerNum, ESL_VAR_OP_INSTANCE, JobId, CallString, MeetingId, CConfig::meetingContext(), ESL_HEADER_JOB_UUID, JobId);
  792. LOGGER(LOG_LEVEL_NORMAL, _T("{FsProxy}: 三方会议呼叫字符串: %s"), EslCmd);
  793. return m_Gateway.sendCmd(EslCmd);
  794. }
  795. /*****************************************************************
  796. **【函数名称】 muteOn
  797. **【函数功能】 静音
  798. **【参数】
  799. **【返回值】
  800. ****************************************************************/
  801. bool CFsProxy::muteOn(LONG JobId, CVirtualChan* pChan)
  802. {
  803. CString EslCmd;
  804. EslCmd.Format(_T("bgapi uuid_audio %s start write mute -4\r\n%s: %ld"), pChan->chanId(), ESL_HEADER_JOB_UUID, JobId);
  805. return m_Gateway.sendCmd(EslCmd);
  806. }
  807. /*****************************************************************
  808. **【函数名称】 muteOff
  809. **【函数功能】 取消静音
  810. **【参数】
  811. **【返回值】
  812. ****************************************************************/
  813. bool CFsProxy::muteOff(LONG JobId, CVirtualChan* pChan)
  814. {
  815. CString EslCmd;
  816. EslCmd.Format(_T("bgapi uuid_audio %s stop write mute -4\r\n%s: %ld"), pChan->chanId(), ESL_HEADER_JOB_UUID, JobId);
  817. return m_Gateway.sendCmd(EslCmd);
  818. }
  819. /*****************************************************************
  820. **【函数名称】 holdon
  821. **【函数功能】 保持
  822. **【参数】
  823. **【返回值】
  824. ****************************************************************/
  825. bool CFsProxy::holdon(LONG JobId, CVirtualChan* pChan)
  826. {
  827. CString EslCmd;
  828. EslCmd.Format(_T("bgapi uuid_hold %s\r\n%s: %ld"), pChan->chanId(), ESL_HEADER_JOB_UUID, JobId);
  829. return m_Gateway.sendCmd(EslCmd);
  830. }
  831. /*****************************************************************
  832. **【函数名称】 takeBack
  833. **【函数功能】 接回
  834. **【参数】
  835. **【返回值】
  836. ****************************************************************/
  837. bool CFsProxy::takeBack(LONG JobId, CVirtualChan* pChan)
  838. {
  839. CString EslCmd;
  840. EslCmd.Format(_T("bgapi uuid_hold off %s\r\n%s: %ld"), pChan->chanId(), ESL_HEADER_JOB_UUID, JobId);
  841. return m_Gateway.sendCmd(EslCmd);
  842. }
  843. /*****************************************************************
  844. **【函数名称】 record
  845. **【函数功能】 录音
  846. **【参数】
  847. **【返回值】
  848. ****************************************************************/
  849. bool CFsProxy::record(LONG JobId, CVirtualChan* pChan, LPCTSTR RcdFile)
  850. {
  851. CString EslCmd;
  852. EslCmd.Format(_T("bgapi uuid_record %s start %s\r\n%s: %ld"), pChan->chanId(), RcdFile, ESL_HEADER_JOB_UUID, JobId);
  853. return m_Gateway.sendCmd(EslCmd);
  854. }
  855. /*****************************************************************
  856. **【函数名称】 transfer
  857. **【函数功能】 呼叫转移
  858. **【参数】
  859. **【返回值】
  860. ****************************************************************/
  861. bool CFsProxy::transfer(LONG JobId, CVirtualChan* pChan, LPCTSTR DestNum)
  862. {
  863. CString Param;
  864. Param.Format(_T("ringback=${us-ring}"));
  865. //Param.Format(_T("ringback=$${sound_prefix}\\ringback.mp3"));
  866. m_Gateway.Execte2(ESL_APP_SET, Param, pChan->chanId());
  867. CString EslCmd;
  868. EslCmd.Format(_T("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld"), pChan->chanId(), DestNum, CConfig::extContext(), ESL_HEADER_JOB_UUID, JobId);
  869. return m_Gateway.sendCmd(EslCmd);
  870. }
  871. /*****************************************************************
  872. **【函数名称】 transfer2Context
  873. **【函数功能】 转移通道至其它Context
  874. **【参数】
  875. **【返回值】
  876. ****************************************************************/
  877. bool CFsProxy::transfer2Context(LONG JobId, LPCTSTR DestChanId, LPCTSTR Exten, LPCTSTR Context, bool BothSide /*= false*/)
  878. {
  879. CString EslCmd;
  880. if (BothSide)
  881. EslCmd.Format(_T("bgapi uuid_transfer %s -both %s xml %s\r\n%s: %ld"), DestChanId, Exten, Context, ESL_HEADER_JOB_UUID, JobId);
  882. else
  883. EslCmd.Format(_T("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld"), DestChanId, Exten, Context, ESL_HEADER_JOB_UUID, JobId);
  884. return m_Gateway.sendCmd(EslCmd);
  885. }
  886. bool CFsProxy::transfer2Context(LONG JobId, LPCTSTR DestChanId, LPCTSTR Exten, LPCTSTR Context, LPCTSTR CallId, bool BothSide)
  887. {
  888. CString Param;
  889. Param.Format(_T("call_id=%s"), CallId);
  890. m_Gateway.Execte2(ESL_APP_SET, Param, DestChanId);
  891. CString EslCmd;
  892. if (BothSide)
  893. EslCmd.Format(_T("bgapi uuid_transfer %s -both %s xml %s\r\n%s: %ld"), DestChanId, Exten, Context, ESL_HEADER_JOB_UUID, JobId);
  894. else
  895. EslCmd.Format(_T("bgapi uuid_transfer %s %s xml %s\r\n%s: %ld"), DestChanId, Exten, Context, ESL_HEADER_JOB_UUID, JobId);
  896. return m_Gateway.sendCmd(EslCmd);
  897. }
  898. bool CFsProxy::setChannelVariable(LONG JobId, LPCTSTR DestChanId, LPCTSTR VarName, LPCTSTR Content)
  899. {
  900. CString EslCmd;
  901. EslCmd.Format(_T("bgapi uuid_setvar %s %s %s\r\n%s: %ld"), DestChanId, VarName, Content, ESL_HEADER_JOB_UUID, JobId);
  902. bool ret = m_Gateway.sendCmd(EslCmd);
  903. return ret;
  904. }
  905. bool CFsProxy::ExecteApp(LPCTSTR App, LPCTSTR Param, LPCTSTR ChanId)
  906. {
  907. return m_Gateway.Execte2(App, Param, ChanId);
  908. }
  909. bool CFsProxy::sendDtmf(LONG JobId, LPCTSTR DestChanId, LPCTSTR Content)
  910. {
  911. CString EslCmd;
  912. EslCmd.Format(_T("bgapi uuid_send_dtmf %s %s\r\n%s: %ld"), DestChanId, Content, ESL_HEADER_JOB_UUID, JobId);
  913. bool ret = m_Gateway.sendCmd(EslCmd);
  914. return ret;
  915. }
  916. bool CFsProxy::setConferencePlay(LONG JobId, LPCTSTR MeetingId, LPCTSTR MemberId, bool isPlay)
  917. {
  918. CString EslCmd;
  919. if (isPlay)
  920. //EslCmd.Format(_T("bgapi conference %s play local_stream://moh %s\r\n%s: %ld"), MeetingId, MemberId, ESL_HEADER_JOB_UUID, JobId);
  921. EslCmd.Format(_T("bgapi conference %s play local_stream://moh %s"), MeetingId, MemberId);
  922. else
  923. EslCmd.Format(_T("bgapi conference %s stop all %s\r\n%s: %ld"), MeetingId, MemberId, ESL_HEADER_JOB_UUID, JobId);
  924. LOGGER(LOG_LEVEL_NORMAL, _T("{CFsProxy}: 执行会议放音命令[%s]"), EslCmd);
  925. bool ret = m_Gateway.sendCmd(EslCmd);
  926. return ret;
  927. }
  928. bool CFsProxy::getConferenceMemberId(LPCTSTR MeetingId, LPCTSTR ChanId, CString &MemberId)
  929. {
  930. return m_Gateway.getConferenceMemberId(MeetingId, ChanId, MemberId);
  931. }
  932. bool CFsProxy::setConferenceExitSound(LPCTSTR MeetingId, bool isPlay)
  933. {
  934. CString EslCmd;
  935. if (isPlay)
  936. EslCmd.Format(_T("bgapi conference %s exit_sound on"), MeetingId);
  937. else
  938. EslCmd.Format(_T("bgapi conference %s exit_sound off"), MeetingId);
  939. LOGGER(LOG_LEVEL_NORMAL, _T("{CFsProxy}: 设置会议人员离开是否播放声音[%s]"), EslCmd);
  940. bool ret = m_Gateway.sendCmd(EslCmd);
  941. return ret;
  942. }
  943. bool CFsProxy::setConferenceIsMute(LONG JobId, LPCTSTR MeetingId, LPCTSTR MemberId, bool isMute)
  944. {
  945. CString EslCmd;
  946. if (isMute)
  947. {
  948. EslCmd.Format(_T("bgapi conference %s deaf %s"), MeetingId, MemberId);
  949. m_Gateway.sendCmd(EslCmd);
  950. EslCmd.Format(_T("bgapi conference %s mute %s\r\n%s: %ld"), MeetingId, MemberId, ESL_HEADER_JOB_UUID, JobId);
  951. }
  952. else
  953. {
  954. EslCmd.Format(_T("bgapi conference %s undeaf %s"), MeetingId, MemberId);
  955. m_Gateway.sendCmd(EslCmd);
  956. EslCmd.Format(_T("bgapi conference %s stop all %s"), MeetingId, MemberId);
  957. m_Gateway.sendCmd(EslCmd);
  958. EslCmd.Format(_T("bgapi conference %s unmute %s\r\n%s: %ld"), MeetingId, MemberId, ESL_HEADER_JOB_UUID, JobId);
  959. }
  960. LOGGER(LOG_LEVEL_NORMAL, _T("{CFsProxy}: 设置会议人员是否静音[%s]"), EslCmd);
  961. bool ret = m_Gateway.sendCmd(EslCmd);
  962. return ret;
  963. }
  964. /*****************************************************************
  965. **【函数名称】 cancel
  966. **【函数功能】 取消当前执行的应用
  967. **【参数】
  968. **【返回值】
  969. ****************************************************************/
  970. bool CFsProxy::cancel(CChanTrunk* pChan)
  971. {
  972. ASSERT(pChan != NULL);
  973. return m_Gateway.Execte2(ESL_APP_BREAK, NULL, pChan->chanId());
  974. }
  975. /*****************************************************************
  976. **【函数名称】 hangup
  977. **【函数功能】 挂机
  978. **【参数】
  979. **【返回值】
  980. ****************************************************************/
  981. bool CFsProxy::hangup(CChanTrunk* pChan)
  982. {
  983. ASSERT(pChan != NULL);
  984. return m_Gateway.execute(pChan->m_pEslHandle, ESL_APP_HANGUP, NULL);
  985. }
  986. /*****************************************************************
  987. **【函数名称】 leaveWord
  988. **【函数功能】 留言
  989. **【参数】
  990. **【返回值】
  991. ****************************************************************/
  992. bool CFsProxy::leaveWord(CChanTrunk* pChan, LPCTSTR FileName, UINT LimitTime, TCHAR FinishKey)
  993. {
  994. ASSERT(pChan != NULL);
  995. CString Param;
  996. Param.Format(_T("playback_terminators=%c"), FinishKey);
  997. m_Gateway.Execte2(ESL_APP_SET, Param, pChan->chanId());
  998. Param.Format(_T("%s %lu"), FileName, LimitTime);
  999. return m_Gateway.Execte2(ESL_APP_LEAVEWORD, Param, pChan->chanId());
  1000. }
  1001. /*****************************************************************
  1002. **【函数名称】 playAndDtmf
  1003. **【函数功能】 放音收按键
  1004. **【参数】
  1005. **【返回值】
  1006. ****************************************************************/
  1007. bool CFsProxy::playAndDtmf(CChanTrunk* pChan, PlayVoiceContent* pPlayContent)
  1008. {
  1009. ASSERT(pChan != NULL);
  1010. ASSERT(pPlayContent != NULL);
  1011. if (pChan == NULL || pPlayContent == NULL) return false;
  1012. TCHAR AudioFile[MAX_PATH] = { 0 };
  1013. // 如果使用了TTS,进行TTS转换
  1014. switch (pPlayContent->nTts)
  1015. {
  1016. default:
  1017. case PLAY_CONTENT_AUDIO: // 不使用TTS
  1018. lstrcpy(AudioFile, pPlayContent->szFileName);
  1019. break;
  1020. case PLAY_CONTENT_TTS_STR: // TTS文本
  1021. {
  1022. ITtsInterface::getInstance().setTTSParam(pPlayContent->nTtsDigitMode, pPlayContent->nTtsSpeed, pPlayContent->nTtsVolume);
  1023. if (!ITtsInterface::getInstance().string2Audio(pPlayContent->szFileName, AudioFile, MAX_PATH))
  1024. {
  1025. LOGGER(LOG_LEVEL_WARNING, _T("{FsProxy}: 中继通道[%lu]放音时TTS文本转换失败"), pChan->no());
  1026. return false;
  1027. }
  1028. }
  1029. break;
  1030. case PLAY_CONTENT_TTS_FILE: // TTS文件
  1031. {
  1032. ITtsInterface::getInstance().setTTSParam(pPlayContent->nTtsDigitMode, pPlayContent->nTtsSpeed, pPlayContent->nTtsVolume);
  1033. if (!ITtsInterface::getInstance().file2Audio(pPlayContent->szFileName, AudioFile, MAX_PATH))
  1034. {
  1035. LOGGER(LOG_LEVEL_WARNING, _T("{FsProxy}: 中继通道[%lu]放音时TTS文件转换失败, File = %s"), pChan->no(), pPlayContent->szFileName);
  1036. return false;
  1037. }
  1038. }
  1039. break;
  1040. }
  1041. CString Param;
  1042. if (pPlayContent->nModel == PVM_PLAY_ONLY && pPlayContent->cDtmfEnd == 0)
  1043. {
  1044. Param.Format(_T("playback_terminators=none"));
  1045. m_Gateway.Execte2(ESL_APP_SET, Param, pChan->chanId());
  1046. //m_Gateway.execute(pChan->m_pEslHandle,ESL_APP_SET,Param);
  1047. Param.Format(_T("%s"), AudioFile);
  1048. return m_Gateway.Execte2(ESL_APP_PLAY, Param, pChan->chanId());
  1049. //return m_Gateway.execute(pChan->m_pEslHandle,ESL_APP_PLAY,Param);
  1050. }
  1051. else
  1052. {
  1053. Param.Format(_T("%lu %lu 1 %lu %c %s silence_stream://250 %s"),1 /*pPlayContent->nDtmfCount*/, pPlayContent->nDtmfCount,
  1054. pPlayContent->nDtmfCount * pPlayContent->nDtmfPeriod * 1000, pPlayContent->cDtmfEnd, AudioFile, ESL_VAR_DTMF_KEY);
  1055. return m_Gateway.Execte2(ESL_APP_PLAY_DTMF, Param, pChan->chanId());
  1056. //return m_Gateway.execute(pChan->m_pEslHandle,ESL_APP_PLAY_DTMF,Param);
  1057. }
  1058. }
  1059. /*****************************************************************
  1060. **【函数名称】 bridge
  1061. **【函数功能】 桥接通道
  1062. **【参数】
  1063. **【返回值】
  1064. ****************************************************************/
  1065. bool CFsProxy::bridge(CChanTrunk* pChan, CString CallerNum, CString CalleeNum)
  1066. {
  1067. CString CallString;
  1068. if (!m_CallStringMaker.makeCallString(CallerNum, CalleeNum, CallString))
  1069. return false;
  1070. CString Param;
  1071. Param.Format(_T("ringback=${us-ring}"));
  1072. m_Gateway.Execte2(ESL_APP_SET, Param, pChan->chanId());
  1073. return m_Gateway.Execte2(ESL_APP_BRIDGE, CallString, pChan->chanId());
  1074. }
  1075. /*****************************************************************
  1076. **【函数名称】 bridge
  1077. **【函数功能】 桥接通道 动态网关方法
  1078. **【参数】
  1079. **【返回值】
  1080. ****************************************************************/
  1081. bool CFsProxy::bridge(CChanTrunk* pChan, CString CalleeNum)
  1082. {
  1083. CString EslCmd;
  1084. EslCmd.Format(_T("bgapi uuid_transfer %s %s xml %s"), pChan->chanId(), CalleeNum, CConfig::extContext());
  1085. return m_Gateway.sendCmd(EslCmd);
  1086. }