1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219 |
- #include "stdafx.h"
- #include "ConnectorFSM.h"
- #include "../include/ModuleMix.h"
- #define RELEASEING_TIMER_INTERVAL 1200
- #define RELEASEING_SIP_TIMER 20000
- #define SIP_CALL_TIMER 70000 //sip呼叫超时时间,20170314,增加超时时间解决坐席忽闪问题
- #define RELEASEING_SIP_TIMEOUT 30000
- #define SIP_CONNECT_FAIL_TIMES 10
- #define LIVEDETECT_CONNECT_INTERVAL 5000 //live detection实体检测间隔5s
- #define LIVEDETECTION_TIMEOUT 60000 //活体检测超时时间60s
- class SyncServiceClient:public SyncService_ClientBase
- {
- public:
- SyncServiceClient(CEntityBase *pEntity, ACMCallFSM* fsm) : SyncService_ClientBase(pEntity), m_fsm(fsm) {}
- ACMCallFSM *m_fsm;
- };
- class MyPhoneClient : public PhoneService_ClientBase
- {
- public:
- MyPhoneClient(CEntityBase *pEntity, ACMCallFSM* fsm) : PhoneService_ClientBase(pEntity), m_fsm(fsm) {}
- virtual void OnMessage(ErrorCodeEnum Error, PhoneService_PhoneState_Info &Msg, CSmartPointer<IReleasable> pData)
- {
- Dbg("rx sip phone state = %d, status = %s", Msg.state, (LPCSTR)Msg.status);
- //LOG_TRACE("rx sip phone state = %d", Msg.state);
- if (Error == Error_Succeed) {
- switch (Msg.state) {
- case ePhone_Init:
- case ePhone_Terminated:
- {
- FSMEvent *e = new FSMEvent(USER_EVT_SIP_STATE_IDLE);
- m_fsm->PostEventFIFO(e);
- }
- break;
- case ePhone_Ready:
- {
- Dbg("sip channel connected");
- FSMEvent *e = new FSMEvent(USER_EVT_SIP_STATE_CONNECTED);
- m_fsm->PostEventFIFO(e);
- }
- break;
- default:
- break;
- }
- }
- }
- ACMCallFSM *m_fsm;
- };
- class MyChannelClient : public ChannelService_ClientBase
- {
- public:
- MyChannelClient(CEntityBase *pEntity, ACMCallFSM* fsm) : ChannelService_ClientBase(pEntity), m_fsm(fsm) {}
- virtual void OnMessage(ErrorCodeEnum Error, ChannelService_State_Info &Msg, CSmartPointer<IReleasable> pData)
- {
- if (Error == Error_Succeed) {
- switch (Msg.state) {
- case eChannelState_Idle:
- {
- FSMEvent *e = new FSMEvent(USER_EVT_CHAN_STATE_IDLE);
- m_fsm->PostEventFIFO(e);
- }
- break;
- case eChannelState_Connected:
- {
- Dbg("CHAN_STATE_CONNECTED.param = %s", (LPCSTR)Msg.param);
- FSMEvent *e = new ChanStateConnectedEvent(Msg.param);
- m_fsm->PostEventFIFO(e);
- }
- break;
- default:
- break;
- }
- }
- }
- ACMCallFSM *m_fsm;
- };
- ACMCallFSM::ACMCallFSM( )
- : m_pPhoneClient(NULL), m_pChannelClient(NULL), m_bRing(FALSE),m_nCurSipServer(MAIN_SERVER),m_nCurChanServer(MAIN_SERVER)
- {
- }
- ACMCallFSM::~ACMCallFSM()
- {
- }
- ErrorCodeEnum ACMCallFSM::OnInit()
- {
- memset(m_strSIPProxyIP,0,sizeof(m_strSIPProxyIP));
- memset(m_strSIPCallNum,0,sizeof(m_strSIPCallNum));
- memset(m_iSIPProxyPort,0,sizeof(m_iSIPProxyPort));
- memset(m_strChanCallNum,0,sizeof(m_strChanCallNum));
- memset(m_strChanProxyIP,0,sizeof(m_strChanProxyIP));
- memset(m_iSIPProxyPort,0,sizeof(m_iSIPProxyPort));
- ErrorCodeEnum Error = LoadConfig();
- if (Error != Error_Succeed)
- goto on_error;
- Dbg("LoadConfig success");
- GetEntityBase()->GetFunction()->SetSysVar("CallState", "O"); // set to offline state
- Error = LoadTerminalId();
- if (Error != Error_Succeed)
- goto on_error;
- Dbg("sip proxy ip1:%s,num1:%s,port1:%d;ip2:%s,num2:%s,port2:%d", (LPCSTR)m_strSIPProxyIP[0],(LPCSTR)m_strSIPCallNum[0], m_iSIPProxyPort[0],(LPCSTR)m_strSIPProxyIP[1],(LPCSTR)m_strSIPCallNum[1], m_iSIPProxyPort[1]);
- Dbg("chan proxy ip1:%s,num1:%s,port1:%d;ip2:%s,num2:%s,port2:%d", (LPCSTR)m_strChanProxyIP[0],(LPCSTR)m_strChanCallNum[0], m_iChanProxyPort[0],(LPCSTR)m_strChanProxyIP[1],(LPCSTR)m_strChanCallNum[1], m_iChanProxyPort[1]);
- Dbg("TerminalId: %s", (LPCSTR)m_strTerminalId);
- m_bHangup = FALSE;
- m_bHandFree = TRUE;
- m_bAgentHandFree = TRUE;
- m_bIsAgentControl = FALSE;
- m_nSipErrorNum = 0;
- m_nLiveDetctionTime = 0;
- memset(&m_CallingParam,0,sizeof(m_CallingParam));
- m_pPhoneClient = new MyPhoneClient(m_pEntity, this);
- Error = m_pPhoneClient->Connect();
- if (Error != Error_Succeed)
- goto on_error;
- PhoneService_BeginState_Sub PhoneSub;
- Error = m_pPhoneClient->BeginState(PhoneSub);
- if (Error != Error_Succeed)
- goto on_error;
- m_pSyncServiceClient = new SyncServiceClient(m_pEntity,this);
- Error = m_pSyncServiceClient->Connect();
- if (Error != Error_Succeed)
- {
- Dbg("Connect SyncService Fail!");
- m_pSyncServiceClient->SafeDelete();
- m_pSyncServiceClient = NULL;
- //goto on_error;
- }
- m_nSysCallType = 0;
- m_pChannelClient = new MyChannelClient(m_pEntity, this);
- Error = m_pChannelClient->Connect();
- if (Error != Error_Succeed)
- goto on_error;
- ChannelService_BeginState_Sub ChannelSub;
- Error = m_pChannelClient->BeginState(ChannelSub);
- if (Error != Error_Succeed)
- goto on_error;
- Dbg("fsm init ok!");
- AddStateHooker(this);
- return Error;
- on_error:
- Dbg("on_error");
- FSMEvent *e = new FSMEvent(USER_EVT_ERROR);
- PostEventFIFO(e); // jump to error state
- return Error;
- }
- BOOL ACMCallFSM::ReConnectionAssistchan()
- {
- if (m_pChannelClient != NULL)
- {
- m_pChannelClient->GetFunction()->CloseSession();
- m_pChannelClient->SafeDelete();
- m_pChannelClient = NULL;
- Dbg("Close AssistChannel Session ");
- }
- if (m_pChannelClient == NULL)
- {
- Dbg("ReConnection AssistChannel Session");
- ErrorCodeEnum Error;
- m_pChannelClient = new MyChannelClient(m_pEntity, this);
- Error = m_pChannelClient->Connect();
- if (Error != Error_Succeed)
- {
- m_pChannelClient->SafeDelete();
- Dbg("m_channelClient connect fail!");
- return FALSE;
- }
- if (Error == Error_Succeed)
- {
- ChannelService_BeginState_Sub ChannelSub;
- Error = m_pChannelClient->BeginState(ChannelSub);
- if (Error != Error_Succeed)
- {
- Dbg("BeginState biz channel failed!");
- m_pChannelClient->GetFunction()->CloseSession();
- m_pChannelClient->SafeDelete();
- m_pChannelClient = NULL;
- return FALSE;
- }
- }
- }
- return TRUE;
- }
- BOOL ACMCallFSM::ReConnectionSipphone()
- {
- if (m_pPhoneClient != NULL)
- {
- m_pPhoneClient->GetFunction()->CloseSession();
- m_pPhoneClient->SafeDelete();
- m_pPhoneClient = NULL;
- Dbg("Close sip Session ");
- }
- if (m_pPhoneClient == NULL)
- {
- Dbg("ReConnection sip Session");
- ErrorCodeEnum Error;
- m_pPhoneClient = new MyPhoneClient(m_pEntity, this);
- Error = m_pPhoneClient->Connect();
- if (Error != Error_Succeed)
- {
- m_pPhoneClient->SafeDelete();
- Dbg("m_phoneClient connect fail!");
- return FALSE;
- }
- if (Error == Error_Succeed)
- {
- PhoneService_BeginState_Sub PhoneSub;
- Error = m_pPhoneClient->BeginState(PhoneSub);
- if (Error != Error_Succeed)
- {
- Dbg("BeginState sip failed!");
- m_pPhoneClient->GetFunction()->CloseSession();
- m_pPhoneClient->SafeDelete();
- m_pPhoneClient = NULL;
- return FALSE;
- }
- }
- }
- return TRUE;
- }
- int ACMCallFSM::GetDelayTime()
- {
- ErrorCodeEnum error= Error_Unexpect;
- SyncService_GetMachineData_Req req;
- req.key = "DelayTime";
- SyncService_GetMachineData_Ans ans;
- DWORD Timeout = 500;
- if (m_pSyncServiceClient)
- {
- error = m_pSyncServiceClient->GetMachineData(req,ans,Timeout);
- }
- if (error == Error_Succeed)
- {
- int delaytime = atoi(ans.value);
- return delaytime;
- }
- else
- {
- return -1;
- }
- }
- void ACMCallFSM::SetDelayTime()
- {
- if (m_pSyncServiceClient)
- {
- SyncService_SetMachineData_Info info;
- info.key = "DelayTime";
- info.data = "0";
- m_pSyncServiceClient->SetMachineData(info);
- Dbg("set delay time to 0");
- }
- }
- ErrorCodeEnum ACMCallFSM::OnExit()
- {
- return Error_Succeed;
- }
- void ACMCallFSM::OnStateTrans(int iSrcState, int iDstState)
- {
- Dbg("FSM state from state %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
- if (CheckBeginRing(iSrcState, iDstState)) {
- StartRing();
- } else {
- if (CheckEndRing(iSrcState, iDstState))
- StopRing();
- }
- if (iSrcState != FSM_STATE_INIT && iDstState != FSM_STATE_EXIT) {
- int st1 = TranslateState(iSrcState);
- int st2 = TranslateState(iDstState);
- if (st1 != st2) {
- PhoneState evt;
- evt.state = st2;
- evt.status = CSimpleStringA::Format("OnStateTrans from state %d to %d", st1, st2);
- //LOG_TRACE(evt.status);
- if (!((st2 == eState_Fail)&&(m_nCurChanServer!=Error_Server)&&(m_nCurSipServer!=Error_Server))||m_bHangup)
- {
- Dbg("Broadcast state from %d to %d", st1, st2);
- SpSendBroadcast(GetEntityBase()->GetFunction(), SP_MSG_OF(PhoneState), SP_MSG_SIG_OF(PhoneState), evt);
- }
- SetCallState(st2);
- GetEntityBase()->GetFunction()->SetUserDefineState(st2+USER_SIP_OFFLINE);
- }
- }
- }
- ErrorCodeEnum ACMCallFSM::SetCallState(int state)
- {
- char *sts[] = {
- "O", // Offline
- "C", // Connecting
- "H", // HandFree
- "P", // Pickup
- "B", // Broken
- "F", // Fail
- "R", // Releasing
- "L" // LiveDetect
- };
- char* strCallState = "O";
- if (0 <= state && state < sizeof(sts)/sizeof(char*)){
- strCallState = sts[state];
- }
- Dbg("set call state to [%s].", strCallState);
- return GetEntityBase()->GetFunction()->SetSysVar("CallState", strCallState);
- }
- void ACMCallFSM::s0_on_entry()
- {
- m_strHintCallNum.Clear();
- m_nCurChanServer = MAIN_SERVER;
- m_nCurSipServer = MAIN_SERVER;
- memset(&m_CallingParam,0,sizeof(m_CallingParam));
- GetEntityBase()->GetFunction()->SetSysVar("CallType", "N"); //进入初始状态时设置呼叫模式为常规呼叫
- m_nSysCallType = 0;
- }
- void ACMCallFSM::s0_on_exit()
- {
- m_bHangup = FALSE;
- }
- unsigned int ACMCallFSM::s0_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_PICKUP_CALL)
- {
- memset(&m_CallingParam,0,sizeof(m_CallingParam)); //正常呼叫
- GetEntityBase()->GetFunction()->SetSysVar(SYSVAR_CALLTYPE, CALLTYPE_NORMAL); // 设置呼叫模式为常规呼叫
- m_nSysCallType = 0;
- }
- else if (event->iEvt == USER_EVT_HANDFREE_CALL)
- {
- GetEntityBase()->GetFunction()->SetSysVar(SYSVAR_CALLTYPE,CALLTYPE_NORMAL); // 设置呼叫模式为常规呼叫
- m_nSysCallType = 0;
- memset(&m_CallingParam,0,sizeof(m_CallingParam)); // 正常呼叫
- }
- else if (event->iEvt == USER_EVT_COMMAND_CALL)
- {
- //由指令模块触发呼叫
- GetEntityBase()->GetFunction()->SetSysVar(SYSVAR_CALLTYPE, CALLTYPE_MOBILE); // 设置呼叫模式为手机呼叫
- m_nSysCallType = 1;
- Dbg("start command call!");
- }
- else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- else if (event->iEvt == USER_EVT_STARTVIDEODISPLAY)
- {
- StartVideoDisplayEvent *e = static_cast<StartVideoDisplayEvent *>(event);
- StartVideo((LPCSTR)e->m_param);
- }
- else if (event->iEvt == USER_EVT_SHOWLOACALVIDEO)
- {
- ShowLocalVideoEvent *e = static_cast<ShowLocalVideoEvent *>(event);
- SetCallingType(NORMAL_CALLTYPE);
- StartVideo((LPCSTR)e->m_param);
- }
- else if (event->iEvt == USER_EVT_SHOWLOACALREMOTEVIDEO)
- {
- ShowLocalAndRemoteVideoEvent *e = static_cast<ShowLocalAndRemoteVideoEvent *>(event);
- SetCallingType(NORMAL_CALLTYPE);
- StartVideo((LPCSTR)e->m_param);
- }
- else if (event->iEvt == USER_EVT_DOUBLE_RECORD_CALL)
- {
- m_CallingParam.nCallType = DOUBLERECORD_CALLTYPE;
- //GetEntityBase()->GetFunction()->SetSysVar(SYSVAR_CALLTYPE,CALLTYPE_RECORD); // 设置呼叫模式为双录呼叫
- m_nSysCallType = 0;
- Dbg("start double record call,call type is %d.", m_CallingParam.nCallType);
- }
- else if (event->iEvt == USER_EVT_STOPLOACALREMOTEVIDEO)
- {
- Dbg("stop show local and remote video.");
- StopVideo();
- SetCallingType(NORMAL_CALLTYPE);
- }
- return 0;
- }
- void ACMCallFSM::s7_on_entry()
- {
- }
- void ACMCallFSM::s7_on_exit()
- {
- }
- unsigned int ACMCallFSM::s7_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_STOPLOCALVIDEO)
- {
- Dbg("stop show local video");
- StopVideo();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- return 0;
- }
- void ACMCallFSM::s9_on_entry()
- {
- Dbg("start liveness detection.");
- m_nLiveDetctionTime = 0;
- ScheduleTimer(9, 2*LIVEDETECT_CONNECT_INTERVAL);
- }
- void ACMCallFSM::s9_on_exit()
- {
- CancelTimer(9);
- m_nLiveDetctionTime = 0;
- }
- unsigned int ACMCallFSM::s9_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_STOPVIDEODISPLAY)
- {
- //ScheduleTimer(9,3000);
- Dbg("stop live detection display");
- StopVideo();
- LogEvent(Severity_Middle,LOG_EVT_RELEASELIVEDETECTION,"release live detection");
- }
- else if (event->iEvt == EVT_TIMER)
- {
- m_nLiveDetctionTime += LIVEDETECT_CONNECT_INTERVAL;
- if (Error_InvalidState == GetLivenessDetectionStatus()){
- StopLivenessDetection();
- }
- else{
- if (m_nLiveDetctionTime > LIVEDETECTION_TIMEOUT){
- Dbg("liveness detection timeout.");
- StopLivenessDetection();
- }
- else{
- ScheduleTimer(9, LIVEDETECT_CONNECT_INTERVAL);
- }
- }
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- return 0;
- }
- void ACMCallFSM::s8_on_entry()
- {
- int time = GetDelayTime();
- Dbg("get Delay time = %d",time);
- if (time > 0)
- {
- ScheduleTimer(8,time*1000);
- }
- else
- {
- PostEventFIFO(new FSMEvent(EVT_TIMER));
- }
- }
- void ACMCallFSM::s8_on_exit()
- {
- CancelTimer(8);
- }
- unsigned int ACMCallFSM::s8_on_event(FSMEvent* event)
- {
- return 0;
- }
- void ACMCallFSM::s10_on_entry()
- {
- //LOG_FUNCTION();
- ErrorCodeEnum Error = Error_Succeed;
- if (m_nCurSipServer == Error_Server)
- {
- Error = Error_NetBroken;
- }
- if (Error != Error_Succeed)
- {
- PostEventFIFO(new FSMEvent(USER_EVT_JMP_FAIL));
- }
- }
- void ACMCallFSM::s10_on_exit() {}
- unsigned int ACMCallFSM::s10_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- else if (event->iEvt == USER_EVT_STOPLOACALREMOTEVIDEO)
- {
- Dbg("hang up call and stop show local and remote video.");
- PostEventFIFO(new FSMEvent(USER_EVT_HANGUP));
- m_bHangup=TRUE;
- StopVideo();
- }
- return 0;
- }
- void ACMCallFSM::s11_on_entry()
- {
- ErrorCodeEnum Error = Error_Succeed;
- if (m_nCurSipServer == Error_Server)
- {
- Error = Error_NetBroken;
- }
- if (Error == Error_Succeed)
- {
- if (m_CallingParam.nCallType != NORMAL_CALLTYPE && m_CallingParam.nCallType != DOUBLERECORD_CALLTYPE)
- {
- Dbg("Begin Make Distribute Call!");
- Error = MakeCall(m_nCurSipServer,m_CallingParam);
- if (Error!=Error_Succeed)
- {
- m_nCurSipServer?(m_nCurSipServer=Error_Server):(m_nCurSipServer=BACK_SERVER);
- }
- Dbg("make call result:0x%08x", Error);
- }
- else
- {
- if (NORMAL_CALLTYPE == m_CallingParam.nCallType){
- Dbg("Begin Make Normal Call!");
- }
- else{
- Dbg("Begin Make Record Call!");
- }
-
- Error = MakeCall(m_strHintCallNum.GetLength() > 0 ? (LPCSTR)m_strHintCallNum : (LPCSTR)m_strSIPCallNum[m_nCurSipServer],m_nCurSipServer);
- if (Error!=Error_Succeed)
- {
- m_nCurSipServer?(m_nCurSipServer=Error_Server):(m_nCurSipServer=BACK_SERVER);
- }
- Dbg("make call result:0x%08x", Error);
- }
- }
- if (Error != Error_Succeed)
- {
- PostEventFIFO(new FSMEvent(USER_EVT_JMP_FAIL));
- }
- else
- {
- ScheduleTimer(11,SIP_CALL_TIMER);
- }
- }
- void ACMCallFSM::s11_on_exit()
- {
- CancelTimer(11);
- }
- unsigned int ACMCallFSM::s11_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_HANGUP)
- {
- m_bHangup = TRUE;
- }
- else if (event->iEvt == USER_EVT_SIP_STATE_IDLE)
- {
- m_nCurSipServer?(m_nCurSipServer=Error_Server):(m_nCurSipServer=BACK_SERVER);
- m_nSipErrorNum++;
- //StopChannel();
- }
- else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- else if (event->iEvt == EVT_TIMER)
- {
- Dbg("sip call timeout,release call");
- m_nSipErrorNum++;
- }
- else if (event->iEvt == USER_EVT_JMP_FAIL)
- {
- m_nSipErrorNum++;
- }
- else if (event->iEvt == USER_EVT_STOPLOACALREMOTEVIDEO)
- {
- Dbg("hang up call and stop show local and remote video.");
- PostEventFIFO(new FSMEvent(USER_EVT_HANGUP));
- m_bHangup=TRUE;
- StopVideo();
- }
- return 0;
- }
- void ACMCallFSM::s12_on_entry()
- {
- ErrorCodeEnum Error = Error_Succeed;
- if (m_nCurChanServer == Error_Server)
- {
- Error = Error_NetBroken;
- }
- m_nSipErrorNum = 0;
- Sleep(200);
- if (Error == Error_Succeed)
- {
- Dbg("begin start channel,m_nCurChanServer=%d",m_nCurChanServer);
- if (m_CallingParam.nCallType == NORMAL_CALLTYPE && m_CallingParam.nCallType != DOUBLERECORD_CALLTYPE)
- {
- Error = StartChannel(m_nCurChanServer);
- }
- else
- {
- Error = StartChannel(m_nCurChanServer,m_CallingParam);
- if (DOUBLERECORD_CALLTYPE == m_CallingParam.nCallType){
- if (FALSE == m_bHandFree){
- StopSpeakerAudioCapture();
- Dbg("double record call type, current is pick up mode, stop speaker audio capture.");
- }
- }
- }
- Dbg("start channel result:0x%08x", Error);
- if (Error != Error_Succeed)
- {
- Dbg("start channel failed:0x%08x,start hangup", Error);
- HangupCall();
- m_nCurChanServer?(m_nCurChanServer=Error_Server):(m_nCurChanServer=BACK_SERVER);
- Dbg("hangup call result:0x%08x", Error);
- }
- }
- if (Error != Error_Succeed)
- {
- PostEventFIFO(new FSMEvent(USER_EVT_JMP_FAIL));
- }
- }
- void ACMCallFSM::s12_on_exit() {}
- unsigned int ACMCallFSM::s12_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_HANGUP)
- {
- m_bHangup = TRUE;
- }
- else if (event->iEvt == USER_EVT_SIP_STATE_IDLE)
- {
- m_nCurSipServer?(m_nCurSipServer=Error_Server):(m_nCurSipServer=BACK_SERVER);
- StopChannel();
- }
- else if (event->iEvt == USER_EVT_CHAN_STATE_IDLE)
- {
- if (!m_bHangup)
- {
- m_nCurChanServer?(m_nCurChanServer=Error_Server):(m_nCurChanServer=BACK_SERVER);
- }
- HangupCall();
- //StopVideo();
- }
- else if (event->iEvt == USER_EVT_CHAN_STATE_CONNECTED)
- {
- ChanStateConnectedEvent *e = static_cast<ChanStateConnectedEvent *>(event);
- StartVideo((LPCSTR)e->m_param);
- }
- else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- else if (event->iEvt == USER_EVT_STOPLOACALREMOTEVIDEO)
- {
- Dbg("hang up call and stop show local and remote video.");
- PostEventFIFO(new FSMEvent(USER_EVT_HANGUP));
- m_bHangup=TRUE;
- StopVideo();
- }
-
- return 0;
- }
- void ACMCallFSM::s13_on_entry()
- {
- if (m_bHandFree)
- {
- PostEventLIFO(new FSMEvent(USER_EVT_JMP_HANDFREE));
- }
- else
- {
- PostEventLIFO(new FSMEvent(USER_EVT_JMP_PICKUP));
- }
- }
- void ACMCallFSM::s13_on_exit() {}
- unsigned int ACMCallFSM::s13_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- return 0;
- }
- void ACMCallFSM::s14_on_entry()
- {
- }
- void ACMCallFSM::s14_on_exit()
- {
- }
- unsigned int ACMCallFSM::s14_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_STOPLOACALREMOTEVIDEO)
- {
- Dbg("stop show local and remote video.");
- StopVideo();
- }
- else if (event->iEvt == USER_EVT_STOP_RECORD_BROADCAST)
- {
- Dbg("stop double record broadcast.");
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- return 0;
- }
- void ACMCallFSM::s20_on_entry()
- {
- StopChannel();
- }
- void ACMCallFSM::s20_on_exit() {}
- unsigned int ACMCallFSM::s20_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_CHAN_STATE_IDLE)
- {
- StopVideo();
- }
- else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- return 0;
- }
- void ACMCallFSM::s21_on_entry()
- {
- ScheduleTimer(21,RELEASEING_SIP_TIMER);
- HangupCall();
- }
- void ACMCallFSM::s21_on_exit()
- {
- CancelTimer(21);
- CancelTimer(210);
- }
- unsigned int ACMCallFSM::s21_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- else if (event->iEvt == EVT_TIMER)
- {
- if(event->param1 == 21)
- {
- Dbg("hangupcall timeout,release call");
- ScheduleTimer(210,RELEASEING_SIP_TIMEOUT);
- ReleaseCall(0);
- }
- else if (event->param1 == 210)
- {
- Dbg("release call timeout,restart sipphone");
- PostEventFIFO(new FSMEvent(USER_EVT_JMP_FAIL));
- //重启mod_sipphone
- //RealSipErrorCheck();
- LogEvent(Severity_High,EVENT_MOD_RELEASESIP_TIMEOUT,"restart sipphone ");
- }
- }
- return 0;
- }
- void ACMCallFSM::s22_on_entry()
- {
- StopChannel();
- }
- void ACMCallFSM::s22_on_exit() {}
- unsigned int ACMCallFSM::s22_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_CHAN_STATE_IDLE)
- {
- StopVideo();
- }
- else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- return 0;
- }
- void ACMCallFSM::s23_on_entry()
- {
- if((!m_bHangup)&&(((m_nCurSipServer==BACK_SERVER)&&(m_nCurChanServer!=Error_Server))||((m_nCurChanServer==BACK_SERVER)&&(m_nCurSipServer!=Error_Server))))
- {
- PostEventLIFO(new FSMEvent(USER_EVT_RECONNECT));
- }
- else
- {
- ScheduleTimer(23, RELEASEING_TIMER_INTERVAL);
- }
- }
- void ACMCallFSM::s23_on_exit()
- {
- CancelTimer(23);
- }
- unsigned int ACMCallFSM::s23_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- else if (event->iEvt == EVT_TIMER)
- {
- if (m_nSipErrorNum >= SIP_CONNECT_FAIL_TIMES)
- {
- m_nSipErrorNum = 0;
- //RealSipErrorCheck();
- LogEvent(Severity_High,EVENT_MOD_RELEASESIP_TIMEOUT,"restart sipphone ");
- }
- }
- return 0;
- }
- void ACMCallFSM::s24_on_entry()
- {
- ScheduleTimer(24, RELEASEING_TIMER_INTERVAL);
- }
- void ACMCallFSM::s24_on_exit()
- {
- CancelTimer(24);
- }
- unsigned int ACMCallFSM::s24_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- return 0;
- }
- void ACMCallFSM::s3_on_entry() {}
- void ACMCallFSM::s3_on_exit() {}
- unsigned int ACMCallFSM::s3_on_event(FSMEvent* event)
- {
- //LOG_TRACE("ACMCallFSM::s3_on_event, id = %d", event->iEvt);
- if (event->iEvt == USER_EVT_TO_HANDFREE)
- {
- //m_bHandFree = TRUE;
- }
- else if (event->iEvt == USER_EVT_TO_PICKUP)
- {
- //m_bHandFree = FALSE;
- }
- else if (event->iEvt == USER_EVT_SIP_STATE_IDLE)
- {
- StopChannel();
- }
- else if (event->iEvt == USER_EVT_CHAN_STATE_IDLE)
- {
- HangupCall();
- StopVideo();
- }
- else if (event->iEvt == USER_EVT_AGENT_WRITABLE)
- {
- AllowAgentWrite();
- }
- else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- return 0;
- }
- void ACMCallFSM::s4_on_entry() {}
- void ACMCallFSM::s4_on_exit() {}
- unsigned int ACMCallFSM::s4_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_TO_HANDFREE)
- {
- //m_bHandFree = TRUE;
- }
- else if (event->iEvt == USER_EVT_TO_PICKUP)
- {
- //m_bHandFree = FALSE;
- }
- else if (event->iEvt == USER_EVT_SIP_STATE_IDLE) {
- StopChannel();
- } else if (event->iEvt == USER_EVT_CHAN_STATE_IDLE) {
- HangupCall();
- StopVideo();
- } else if (event->iEvt == USER_EVT_AGENT_WRITABLE) {
- AllowAgentWrite();
- }
- else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- return 0;
- }
- void ACMCallFSM::s50_on_entry() {}
- void ACMCallFSM::s50_on_exit() {}
- unsigned int ACMCallFSM::s50_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_CHAN_STATE_IDLE) {
- StopVideo();
- }
- else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- return 0;
- }
- void ACMCallFSM::s51_on_entry() {}
- void ACMCallFSM::s51_on_exit() {}
- unsigned int ACMCallFSM::s51_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_CHAN_STATE_IDLE) {
- StopVideo();
- }
- return 0;
- }
- void ACMCallFSM::s52_on_entry()
- {
- ScheduleTimer(52,RELEASEING_SIP_TIMER);
- }
- void ACMCallFSM::s52_on_exit()
- {
- CancelTimer(52);
- CancelTimer(520);
- }
- unsigned int ACMCallFSM::s52_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- else if (event->iEvt == EVT_TIMER)
- {
- if (event->param1 == 52)
- {
- ScheduleTimer(520,RELEASEING_SIP_TIMEOUT);
- Dbg("sip finished timeout,release call");
- ReleaseCall(0);
- }
- else if (event->param1 == 520)
- {
- Dbg("release call timeout,restart sipphone");
- PostEventFIFO(new FSMEvent(USER_EVT_JMP_FAIL));
- //重启mod_sipphone
- //RealSipErrorCheck();
- LogEvent(Severity_High,EVENT_MOD_RELEASESIP_TIMEOUT,"restart sipphone ");
- }
- }
- return 0;
- }
- void ACMCallFSM::s53_on_entry()
- {
- ScheduleTimer(53, RELEASEING_TIMER_INTERVAL);
- }
- void ACMCallFSM::s53_on_exit()
- {
- CancelTimer(53);
- }
- unsigned int ACMCallFSM::s53_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- else if (event->iEvt == EVT_TIMER)
- {
- if (m_nSipErrorNum >= SIP_CONNECT_FAIL_TIMES)
- {
- m_nSipErrorNum = 0;
- //RealSipErrorCheck();
- LogEvent(Severity_High,EVENT_MOD_RELEASESIP_TIMEOUT,"restart sipphone ");
- }
- }
- return 0;
- }
- void ACMCallFSM::s60_on_entry()
- {
- StopChannel();
- HangupCall();
- }
- void ACMCallFSM::s60_on_exit() {}
- unsigned int ACMCallFSM::s60_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_CHAN_STATE_IDLE)
- {
- StopVideo();
- }
- else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- return 0;
- }
- void ACMCallFSM::s61_on_entry() {}
- void ACMCallFSM::s61_on_exit() {}
- unsigned int ACMCallFSM::s61_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_CHAN_STATE_IDLE) {
- StopVideo();
- }
- else if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- return 0;
- }
- void ACMCallFSM::s62_on_entry()
- {
- ScheduleTimer(62,RELEASEING_SIP_TIMER);
- }
- void ACMCallFSM::s62_on_exit()
- {
- CancelTimer(62);
- CancelTimer(620);
- }
- unsigned int ACMCallFSM::s62_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- else if (event->iEvt == EVT_TIMER)
- {
- if (event->param1 == 62)
- {
- ScheduleTimer(620,RELEASEING_SIP_TIMEOUT);
- Dbg("sip hangup timeout,release call");
- ReleaseCall(0);
- }
- else if (event->param1 == 620)
- {
- Dbg("release call timeout,restart sipphone");
- PostEventFIFO(new FSMEvent(USER_EVT_JMP_FAIL));
- //重启mod_sipphone
- //RealSipErrorCheck();
- LogEvent(Severity_High,EVENT_MOD_RELEASESIP_TIMEOUT,"restart sipphone ");
- }
- }
- return 0;
- }
- void ACMCallFSM::s63_on_entry()
- {
- ScheduleTimer(63, RELEASEING_TIMER_INTERVAL);
- }
- void ACMCallFSM::s63_on_exit()
- {
- CancelTimer(63);
- }
- unsigned int ACMCallFSM::s63_on_event(FSMEvent* event)
- {
- if (event->iEvt == USER_EVT_ASSISTCHAN_IDEL)
- {
- ReConnectionAssistchan();
- }
- else if (event->iEvt == USER_EVT_SIPPHONE_IDEL)
- {
- ReConnectionSipphone();
- }
- else if (event->iEvt == EVT_TIMER)
- {
- if (m_nSipErrorNum >= SIP_CONNECT_FAIL_TIMES)
- {
- m_nSipErrorNum = 0;
- //RealSipErrorCheck();
- LogEvent(Severity_High,EVENT_MOD_RELEASESIP_TIMEOUT,"restart sipphone ");
- }
- }
- return 0;
- }
- int ACMCallFSM::TranslateState( int innerState )
- {
- switch (innerState) {
- case s0:
- return eState_Offline;
- case s8:
- case s10:
- case s11:
- case s12:
- case s13:
- return eState_Connecting;
- case s20:
- case s21:
- case s22:
- case s23:
- case s24:
- return eState_Fail;
- case s3:
- return eState_HandFree;
- case s4:
- return eState_Pickup;
- case s50:
- case s51:
- case s52:
- case s53:
- return eState_Broken;
- case s7:
- case s14:
- case s60:
- case s61:
- case s62:
- case s63:
- return eState_Releasing;
- case s9:
- return eState_LiveDetect;
- default:
- assert(0);
- return 0;
- }
- }
|