📄 sipxtapi.cpp
字号:
//// Copyright (C) 2005-2006 SIPez LLC.// Licensed to SIPfoundry under a Contributor Agreement.//// Copyright (C) 2004-2006 SIPfoundry Inc.// Licensed by SIPfoundry under the LGPL license.//// Copyright (C) 2004, 2005 Pingtel Corp.// Licensed to SIPfoundry under a Contributor Agreement.// // $$//////////////////////////////////////////////////////////////////////////////// SYSTEM INCLUDES#include <assert.h>#ifdef _WIN32#include <windows.h>#include <winsock.h>#else /* _WIN32 */#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#endif /* _WIN32 */#include <math.h>// APPLICATION INCLUDES#include "os/OsDefs.h"#include "tapi/sipXtapi.h"#include "tapi/sipXtapiEvents.h"#include "tapi/sipXtapiInternal.h"#include "tapi/SipXHandleMap.h"#include "tapi/SipXMessageObserver.h"//#include "rtcp/RTCManager.h"#include "net/SipUserAgent.h"#include "net/SdpCodecFactory.h"#include "cp/CallManager.h"// Media include files#include "mi/CpMediaInterfaceFactory.h"#include "mi/CpMediaInterfaceFactoryImpl.h"#include "mi/CpMediaInterfaceFactoryFactory.h"#include "ptapi/PtProvider.h"#include "net/Url.h"#include "net/NameValueTokenizer.h"#include "os/OsConfigDb.h"#include "net/SipLineMgr.h"#include "net/SipRefreshMgr.h"#include "os/OsLock.h"#include "os/OsSysLog.h"#include "os/OsTimerTask.h"#include "os/OsStunAgentTask.h"#include "net/TapiMgr.h"#include "net/SipSrvLookup.h"#include "net/SipSubscribeServer.h"#include "net/SipSubscribeClient.h"#include "net/SipDialogMgr.h"#include "net/SipPublishContentMgr.h"#include "os/HostAdapterAddress.h"#include "utl/UtlSList.h"// DEFINES#define MP_SAMPLE_RATE 8000 // Sample rate (don't change)#define MP_SAMPLES_PER_FRAME 80 // Frames per second (don't change)// GLOBAL VARIABLES// EXTERNAL VARIABLESextern SipXHandleMap* gpCallHandleMap ; // sipXtapiInternal.cppextern SipXHandleMap* gpLineHandleMap ; // sipXtapiInternal.cppextern SipXHandleMap* gpConfHandleMap ; // sipXtapiInternal.cppextern SipXHandleMap* gpInfoHandleMap ; // sipXtapiInternal.cppextern SipXHandleMap* gpPubHandleMap ; // sipXtapiInternal.cppextern SipXHandleMap* gpSubHandleMap ; // sipXtapiInternal.cppextern UtlDList* gpSessionList ; // sipXtapiInternal.cpp// EXTERNAL FUNCTIONS#if defined(_VXWORKS)extern "C" char* strdup(const char*);extern "C" int strcasecmp(const char *s1, const char *s2);#endif// STRUCTURES/* ============================ FUNCTIONS ================================= */#if defined(_WIN32)BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ){ switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE;}#endif /* defined(_WIN32) */static void initLogger(){ OsSysLog::initialize(0, // do not cache any log messages in memory "sipXtapi"); // name for messages from this program}static bool validConnection(const SIPX_CALL_DATA* pData){ assert(pData != NULL) ; assert(pData->callId != NULL) ; assert(pData->pInst != NULL) ; assert(pData->pInst->pCallManager != NULL) ; assert(pData->pInst->pRefreshManager != NULL) ; assert(pData->pInst->pLineManager != NULL) ; assert(pData->lineURI != NULL) ; assert(pData->remoteAddress != NULL) ; return (pData && pData->callId && pData->remoteAddress && pData->lineURI && pData->pInst->pCallManager && pData->pInst->pRefreshManager && pData->pInst->pLineManager ) ;}void destroyCallData(SIPX_CALL_DATA* pData){ if (pData != NULL) { // Increment call count pData->pInst->pLock->acquire() ; pData->pInst->nCalls-- ; assert(pData->pInst->nCalls >= 0) ; pData->pInst->pLock->release() ; if (pData->callId != NULL) { delete pData->callId ; pData->callId = NULL ; } if (pData->lineURI != NULL) { delete pData->lineURI ; pData->lineURI = NULL ; } if (pData->remoteAddress != NULL) { delete pData->remoteAddress ; pData->remoteAddress = NULL ; } if (pData->pMutex != NULL) { delete pData->pMutex ; pData->pMutex = NULL ; } if (pData->ghostCallId != NULL) { delete pData->ghostCallId; pData->ghostCallId = NULL; } if (pData->sessionCallId != NULL) { delete pData->sessionCallId ; pData->sessionCallId = NULL ; } delete pData ; }}static void destroyLineData(SIPX_LINE_DATA* pData){ if (pData != NULL) { if (pData->lineURI != NULL) { delete pData->pMutex ; delete pData->lineURI ; } delete pData ; }}static SIPX_LINE_DATA* createLineData(SIPX_INSTANCE_DATA* pInst, const Url& uri){ SIPX_LINE_DATA* pData = new SIPX_LINE_DATA ; memset ((void*) pData, 0, sizeof(SIPX_LINE_DATA)); if (pData != NULL) { pData->lineURI = new Url(uri) ; pData->pInst = pInst ; pData->pMutex = new OsRWMutex(OsRWMutex::Q_FIFO) ; if ((pData->lineURI == NULL) || (pData->pMutex == NULL)) { destroyLineData(pData) ; pData = NULL ; } else { pInst->pLock->acquire() ; pInst->nLines++ ; pInst->pLock->release() ; } } return pData ;}/**************************************************************************** * Initialization ***************************************************************************/#ifdef _WIN32static void initAudioDevices(SIPX_INSTANCE_DATA* pInst){ WAVEOUTCAPS outcaps ; WAVEINCAPS incaps ; int numDevices ; int i ; numDevices = waveInGetNumDevs(); for (i=0; i<numDevices && i<MAX_AUDIO_DEVICES; i++) { waveInGetDevCaps(i, &incaps, sizeof(WAVEINCAPS)) ; pInst->inputAudioDevices[i] = strdup(incaps.szPname) ; } numDevices = waveOutGetNumDevs(); for (i=0; i<numDevices && i<MAX_AUDIO_DEVICES; i++) { waveOutGetDevCaps(i, &outcaps, sizeof(WAVEOUTCAPS)) ; pInst->outputAudioDevices[i] = strdup(outcaps.szPname) ; }}#endif /* _WIN32 */SIPXTAPI_API SIPX_RESULT sipxInitialize(SIPX_INST* phInst, const int tcpPort, const int udpPort, const int tlsPort, const int rtpPortStart, const int maxConnections, const char* szIdentity, const char* szBindToAddr, bool bUseSequentialPorts){ OsSysLog::add(FAC_SIPXTAPI, PRI_INFO, "sipxInitialize tcpPort=%d udpPort=%d tlsPort=%d rpPortStart=%d maxConnections=%d Identity=%s bindTo=%s bSequentialPorts=%d", tcpPort, udpPort, tlsPort, rtpPortStart, maxConnections, szIdentity, szBindToAddr, bUseSequentialPorts) ; char szTemp[128]; char szBindToAddr2[32]; sipxConfigGetVersion(szTemp, 128); SIPX_RESULT rc = SIPX_RESULT_INVALID_ARGS ;#ifdef SIPXTAPI_EVAL_EXPIRATION OsDateTime expireDate(EVAL_EXPIRE_YEAR, EVAL_EXPIRE_MONTH, EVAL_EXPIRE_DAY, 23, 59, 59, 0) ; OsDateTime nowDate ; OsTime expireTime ; OsTime nowTime ; OsDateTime::getCurTime(nowDate) ; expireDate.cvtToTimeSinceEpoch(expireTime) ; nowDate.cvtToTimeSinceEpoch(nowTime) ; if (nowTime > expireTime) { return SIPX_RESULT_EVAL_TIMEOUT ; }#endif // set the sipXtackLib's tapi callback function pointers TapiMgr::getInstance().setTapiCallCallback(&sipxFireCallEvent); TapiMgr::getInstance().setTapiLineCallback(&sipxFireLineEvent); TapiMgr::getInstance().setTapiCallback(&sipxFireEvent); if (!szBindToAddr || !strcmp(szBindToAddr, DEFAULT_BIND_ADDRESS)) { const HostAdapterAddress* addresses[MAX_IP_ADDRESSES]; int numAddresses = 0; memset(addresses, 0, sizeof(addresses)); getAllLocalHostIps(addresses, numAddresses); assert(numAddresses > 0); // Bind to the first address in the list. strcpy(szBindToAddr2, (char*)addresses[0]->mAddress.data()); // Now free up the list. for (int i = 0; i < numAddresses; i++) delete addresses[i]; } else { strcpy(szBindToAddr2, szBindToAddr); } unsigned long bindTo = inet_addr(szBindToAddr2); if (bindTo != INADDR_NONE) { //OsSocket::setDefaultBindAddress(bindTo) ; SIPX_INSTANCE_DATA* pInst = new SIPX_INSTANCE_DATA; memset(pInst, 0, sizeof(SIPX_INSTANCE_DATA)) ; // Create Line and Refersh Manager pInst->pLineManager = new SipLineMgr() ; pInst->pRefreshManager = new SipRefreshMgr() ; pInst->pRefreshManager->setLineMgr(pInst->pLineManager); // Init counts pInst->pLock = new OsMutex(OsMutex::Q_FIFO) ; pInst->nCalls = 0 ; pInst->nLines = 0 ; pInst->nConferences = 0 ; // Bind the SIP user agent to a port and start it up pInst->pSipUserAgent = new SipUserAgent( tcpPort, // sipTcpPort udpPort, // sipUdpPort tlsPort, // sipTlsPort NULL, // publicAddress NULL, // defaultUser szBindToAddr, // default IP Address NULL, // sipProxyServers NULL, // sipDirectoryServers NULL, // sipRegistryServers NULL, // authenticationScheme NULL, // authenicateRealm NULL, // authenticateDb NULL, // authorizeUserIds NULL, // authorizePasswords NULL, // natPingUrl 0, // natPingFrequency "PING", // natPingMethod pInst->pLineManager, // lineMgr SIP_DEFAULT_RTT, // sipFirstResendTimeout TRUE, // defaultToUaTransactions -1, // readBufferSize OsServerTask::DEF_MAX_MSGS, // queueSize bUseSequentialPorts); // bUseNextAvailablePort pInst->pSipUserAgent->allowMethod(SIP_INFO_METHOD); pInst->pSipUserAgent->start(); // Startup Line Manager Refresh Manager pInst->pLineManager->initializeRefreshMgr(pInst->pRefreshManager) ; pInst->pRefreshManager->init(pInst->pSipUserAgent, pInst->pSipUserAgent->getTcpPort(), pInst->pSipUserAgent->getUdpPort()) ; pInst->pRefreshManager->StartRefreshMgr(); // Create and start up a SIP SUBSCRIBE server pInst->pSubscribeServer = SipSubscribeServer::buildBasicServer(*pInst->pSipUserAgent); pInst->pSubscribeServer->start(); // Create and start up a SIP SUBSCRIBE client SipDialogMgr* clientDialogMgr = new SipDialogMgr; SipRefreshManager* clientRefreshManager = new SipRefreshManager(*pInst->pSipUserAgent, *clientDialogMgr); clientRefreshManager->start(); pInst->pSubscribeClient = new SipSubscribeClient(*pInst->pSipUserAgent, *clientDialogMgr, *clientRefreshManager); pInst->pSubscribeClient->start(); // Enable PCMU, PCMA, Tones/RFC2833 codecs
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -