📄 uacallcontrol.cxx
字号:
}voidUaCallControl::handleGuiEvents(Sptr<GuiEvent> gEvent){ cpLog(LOG_DEBUG, "Handling GUI event Key (%s), value:(%s)", gEvent->getKey().c_str(), gEvent->getValue().c_str()); switch (gEvent->getType()) { case G_ACCEPT: { Sptr<CallAgent> cAgent = getActiveCall(); if(cAgent != 0) { cpLog(LOG_DEBUG, "Accept Call:"); cAgent->acceptCall(); } } break; case G_STOP: { Sptr<CallAgent> cAgent = getActiveCall(); if(cAgent != 0) { cpLog(LOG_DEBUG, "Stopping Call:"); //BYE or CANCEL cAgent->stopCall(); } else { cpLog(LOG_ERR, "No active calls found to stop."); } } break; case G_INVITE: { //UaFacade::instance().postMsg("TRYING "); //Initiate an Invite to the remote party string value = gEvent->getValue(); initiateInvite(value); } break; case G_HOLD: { Sptr<CallAgent> cAgent = getActiveCall(); if(cAgent != 0) { cpLog(LOG_DEBUG, "Stopping Call:"); //BYE or CANCEL cAgent->processHold(); } else { cpLog(LOG_ERR, "No active calls found to stop."); } } break; case G_RESUME: { Sptr<CallAgent> cAgent = getActiveCall(); if(cAgent != 0) { cpLog(LOG_DEBUG, "Resuming Call:"); //BYE or CANCEL cAgent->processResume(); } else { cpLog(LOG_ERR, "No active calls found to stop."); } } break; case G_DOSUBSCRIBE: { UaConfiguration::instance().parseConfig(); if(UaFacade::instance().getRegistrationManager() != 0) { UaFacade::instance().getRegistrationManager()->addRegistration(1); UaFacade::instance().getRegistrationManager()->startRegistration(); } } case G_REGISTRATIONEXPIRED: { //Preferences changed, do the regsitration //Parse the configuration data UaConfiguration::instance().parseConfig(); if(UaFacade::instance().getRegistrationManager() != 0) { UaFacade::instance().getRegistrationManager()->addRegistration(1); UaFacade::instance().getRegistrationManager()->startRegistration(); } } break; case G_PREF: { //Preferences changed, do the regsitration //Parse the configuration data //Save the OLD value of SIP port, since SIP port can not //be modified from the config file, however parseConfig //will try to set it to the default. So set the port back to //saved port int sPort = atoi(UaConfiguration::instance().getValue(LocalSipPortTag).c_str()); UaConfiguration::instance().parseConfig(); UaConfiguration::instance().setValue(LocalSipPortTag, Data(sPort).c_str()); if(UaFacade::instance().getRegistrationManager() != 0) { UaFacade::instance().getRegistrationManager()->addRegistration(); UaFacade::instance().getRegistrationManager()->startRegistration(); } } break; case G_SHUTDOWN: { UaFacade::instance().shutdown(); } break; default: cpLog(LOG_DEBUG, "TODO..."); break; }}UaCallControl& UaCallControl::instance(){ if(myInstance == 0) { myInstance = new UaCallControl(); } return *myInstance;}voidUaCallControl::destroy(void){ cpLog(LOG_DEBUG, "UaCallControl::destroy"); delete UaCallControl::myInstance; UaCallControl::myInstance = 0;}voidUaCallControl::initiateInvite(const string& to){ //Parse the to string //1000-> sip:1000@proxy.com;user=phone //xvz-> sip:xyz@proxy.com; //xyz@domain.com -> sip:xyz@<domain SIP server IP> //xyz@fqdn -> sip:xyz@fqdn //Case 1: sip:user@... string::size_type pos = to.find_first_of(":@"); Sptr<BaseUrl> toUrl; if(pos != string::npos) { toUrl = parseUrl(to); if(toUrl == 0) { //Case 2 //user@xyz.com //Assume it to be a SIP url string mTo("sip:"); mTo += to; toUrl = parseUrl(mTo); if(toUrl == 0) { //Give-up, url is not the right type UaFacade::instance().postMsg("ERROR Invalid URL"); return; } } } else { //Only user part toUrl = new SipUrl(); Sptr<SipUrl> sUrl; sUrl.dynamicCast(toUrl); sUrl->setUserValue(to); sUrl->setHost(UaConfiguration::instance().getValue(ProxyServerTag)); } string sipPort = UaConfiguration::instance().getValue(LocalSipPortTag); string rtpPort = UaConfiguration::instance().getValue(MinRtpPortTag); Sptr<InviteMsg> msg = new InviteMsg( toUrl, atoi( sipPort.c_str()) ,atoi(rtpPort.c_str()) ); SipFrom from; from.setHost(Data(theSystem.gethostAddress())); from.setUser( UaConfiguration::instance().getValue(UserNameTag)); from.setDisplayName(UaConfiguration::instance().getValue(DisplayNameTag)); if(sipPort != "5060") { from.setPort(sipPort.c_str()); } CryptoRandom random; unsigned char tempTag[NUM_TAG_RANDOMNESS]; int len = random.getRandom(tempTag, NUM_TAG_RANDOMNESS); Data fromTag; if (len > 0) { fromTag = convertToHex(tempTag, NUM_TAG_RANDOMNESS); } from.setTag(fromTag); msg->setFrom( from ); // Set transport in Via: SipVia via = msg->getVia(); msg->removeVia(); via.setTransport( UaConfiguration::instance().getValue(SipTransportTag) ); Data viaBranch("z9hG4bK"); viaBranch += msg->computeBranch2(); via.setBranch(viaBranch); msg->setVia( via ); SipUserAgent uAgent("Vovia-SIP-SoftPhone/0.1 (www.vovida.org)"); msg->setUserAgent(uAgent); // Set Contact header Sptr< SipUrl > myUrl = new SipUrl; myUrl->setUserValue( UaConfiguration::instance().getValue(UserNameTag) ); SipContact myContact; if( UaConfiguration::instance().getValue(NATAddressIPTag).length()) { myUrl->setHost( Data(UaConfiguration::instance().getValue(NATAddressIPTag))); } else { myUrl->setHost( Data( theSystem.gethostAddress() ) ); } myUrl->setPort( UaConfiguration::instance().getValue(LocalSipPortTag) ); myContact.setUrl( myUrl ); msg->setNumContact( 0 ); // Clear old contact msg->setContact( myContact ); SipRequestLine& reqLine = msg->getMutableRequestLine(); reqLine.setTransportParam(UaConfiguration::instance().getValue(SipTransportTag)); Sptr<SipSdp> sipSdp; sipSdp.dynamicCast ( msg->getContentData( 0 ) ); assert(sipSdp != 0); Data NAT_HOST = UaConfiguration::instance().getValue(NATAddressIPTag);#ifdef USE_MPEGLIB int video = atoi(UaConfiguration::instance().getValue(VideoTag).c_str()); //Even if video mode is on, when running in command line mode force the //non-video option if(video && (!UaCommandLine::instance()->getIntOpt("cmdline"))) { Data sdpData; Sptr<VideoDevice> vDevice; vDevice.dynamicCast(UaFacade::instance().getMediaDevice()); assert(vDevice != 0); SdpSession& localSdp = sipSdp->getSdpDescriptor(); localSdp.flushMediaList(); vDevice->generateSdpData(localSdp); } else { SdpSession localSdp = MediaController::instance().createSession(); if(NAT_HOST.length()) { setHost(localSdp, NAT_HOST); } sipSdp->setSdpDescriptor(localSdp); }#else SdpSession localSdp = MediaController::instance().createSession(); if(NAT_HOST.length()) { setHost(localSdp, NAT_HOST); } sipSdp->setSdpDescriptor(localSdp);#endif //Create call-agent to handle the call from now on Sptr<CallAgent> cAgent = new CallAgent(msg, Vocal::UA::A_CLIENT); //Persist the agent for the duration of the call myMutex.lock(); myCallMap[cAgent->getId()] = cAgent; myMutex.unlock(); //UaClient state-machine will take the call from here}boolUaCallControl::busy(Sptr<SipCommand> sipMsg){ Sptr<CallAgent> cAgent = getActiveCall(sipMsg); if(cAgent != 0) { cpLog(LOG_DEBUG, "A call is already active"); Sptr<StatusMsg> statusMsg = new StatusMsg(*sipMsg, 486); UaFacade::instance().getSipTransceiver()->sendReply(statusMsg); return true; } return false;}Sptr<CallAgent>UaCallControl::getActiveCall(Sptr<SipMsg> sipMsg){ cpLog(LOG_DEBUG, "UaCallControl::getActiveCall"); myMutex.lock(); if(myCallMap.size()) { cpLog(LOG_DEBUG, "UaCallControl::getActiveCall"); //Get the call from the call-map for(CallMap::iterator itr = myCallMap.begin(); itr != myCallMap.end(); itr++) { Sptr<CallAgent> cAgent=0; cAgent.dynamicCast((*itr).second); if(cAgent->isActive()) { CallControlMode cMode = UaFacade::instance().getMode(); if((sipMsg != 0) && (( cMode == CALL_MODE_ANNON) || ( cMode == CALL_MODE_VMAIL))) { //Get the callId SipTransactionId id(*sipMsg); SipTransactionId id2(*(cAgent->getInvokee()->getRequest())); if(id == id2) { //Same request coming again //do Nothing myMutex.unlock(); return 0; } else { cAgent = 0; continue; } } myMutex.unlock(); return cAgent; } } } myMutex.unlock(); return 0;}Sptr<BaseUrl>UaCallControl::parseUrl(const string& to){ cpLog(LOG_DEBUG, "Parsing URL:%s", to.c_str()); string::size_type pos = to.find_first_of(":"); Sptr<BaseUrl> toUrl; try { //First try to parse Url //if it is not a valid URL it will throw an //exception SipParserMode::instance().setStrictMode(true); string left = to.substr(0, pos); if(left == "sip") { cpLog(LOG_DEBUG, "SIP_URL:%s", to.c_str()); toUrl = new SipUrl(to); } else if(left == "tel") { cpLog(LOG_DEBUG, "TEL_URL:%s", to.c_str()); toUrl = new TelUrl(to); } else { cpLog(LOG_DEBUG, "UNKNOWN URL:%s", to.c_str()); SipParserMode::instance().setStrictMode(false); return 0; } SipParserMode::instance().setStrictMode(false); } catch (VException& e) { cpLog(LOG_DEBUG, "UNKNOWN URL:%s", to.c_str()); return 0; } return toUrl;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -