⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 placecall.cpp

📁 基于sipfoundy 公司开发的sipx协议API
💻 CPP
📖 第 1 页 / 共 2 页
字号:
void ClearSipXEvents(){    for (int i=0;i<MAX_RECORD_EVENTS; i++)    {        g_eRecordEvents[i] = CALLSTATE_UNKNOWN ;    }    g_iNextEvent = 0 ;}void dumpLocalContacts(SIPX_CALL hCall){    SIPX_CONTACT_ADDRESS contacts[10] ;    size_t nContacts;    SIPX_RESULT status = sipxConfigGetLocalContacts(g_hInst, contacts, 10, nContacts) ;    if (status == SIPX_RESULT_SUCCESS)    {        for (size_t i = 0; i<nContacts; i++)        {            const char* szType = "UNKNOWN" ;            switch (contacts[i].eContactType)            {                case CONTACT_LOCAL:                    szType = "LOCAL" ;                    break ;                case CONTACT_NAT_MAPPED:                    szType = "NAT_MAPPED" ;                    break ;                case CONTACT_RELAY:                    szType = "RELAY" ;                    break ;                case CONTACT_CONFIG:                    szType = "CONFIG" ;                    break ;            }            printf("<-> Type %s, Interface: %s, Ip %s, Port %d\n",                    szType, contacts[i].cInterface, contacts[i].cIpAddress,                    contacts[i].iPort) ;        }    }    else    {        printf("<-> Unable to query local contact addresses\n") ;    }}// Place a call to szSipUrl as szFromIdentitybool placeCall(char* szSipUrl, char* szFromIdentity, char* szUsername, char* szPassword, char *szRealm){    bool bRC = false ;    if ((szFromIdentity == NULL) || strlen(szFromIdentity) == 0)    {        szFromIdentity = "\"PlaceCall Demo\" <sip:placecalldemo@localhost>" ;    }    printf("<-> Placing call to \"%s\" as \"%s\"\n", szSipUrl, szFromIdentity) ;    printf("<-> Username: %s, passwd: %s, realm: %s (all required for auth)\n", szUsername, szPassword, szRealm) ;    sipxLineAdd(g_hInst, szFromIdentity, &g_hLine) ;    if (szUsername && szPassword && szRealm)    {        sipxLineAddCredential(g_hLine, szUsername, szPassword, szRealm) ;    }    sipxCallCreate(g_hInst, g_hLine, &g_hCall) ;    dumpLocalContacts(g_hCall) ;    sipxCallConnect(g_hCall, szSipUrl) ;    bRC = WaitForSipXEvent(CONNECTED, 30) ;    return bRC ;}// Drop call, clean up resourcesbool shutdownCall(){    printf("<-> Shutting down Call\n") ;    ClearSipXEvents() ;    sipxCallDestroy(g_hCall) ;    sipxLineRemove(g_hLine) ;    WaitForSipXEvent(DESTROYED, 5) ;    return true ;}// Play a series of tonesbool playTones(char* szPlayTones){    bool bRC = true ;    while (*szPlayTones)    {        int toneId = *szPlayTones++ ;        if (    (toneId >= '0' && toneId <= '9') ||                (toneId == '#') || (toneId == '*') || toneId == ',' || toneId == '!')        {            if (toneId == ',')            {                printf("<-> Playtone: Sleeping for 2 seconds\n") ;                SLEEP(2000) ;            }            else            {                printf("<-> Playtone: %c\n", toneId) ;                SLEEP(250) ;                if (sipxCallStartTone(g_hCall, (TONE_ID) toneId, true, true) != SIPX_RESULT_SUCCESS)                {                    printf("Playtone returned error\n");                }                SLEEP(500) ;                sipxCallStopTone(g_hCall) ;            }        }        else        {            bRC = false ;            break ;        }    }    return bRC ;}// Play a file (8000 samples/sec, 16 bit unsigned, mono PCM)bool playFile(char* szFile){    bool bRC = false ;    sipxCallPlayFile(g_hCall, szFile, true, true) ;    return true ;}// Display the list of input & output devicesvoid dumpInputOutputDevices(){    size_t numDevices ;    if (sipxAudioGetNumInputDevices(g_hInst, numDevices) == SIPX_RESULT_SUCCESS)    {        printf("Input Devices: %d\n", numDevices) ;        for (size_t i=0; i<numDevices; i++)        {            const char* szDevice ;            sipxAudioGetInputDevice(g_hInst, i, szDevice) ;            printf("\t#%d: %s\n", i, szDevice) ;        }    }    if (sipxAudioGetNumOutputDevices(g_hInst, numDevices) == SIPX_RESULT_SUCCESS)    {        printf("Output Devices: %d\n", numDevices) ;        for (size_t i=0; i<numDevices; i++)        {            const char* szDevice ;            sipxAudioGetOutputDevice(g_hInst, i, szDevice) ;            printf("\t#%d: %s\n", i, szDevice) ;        }    }    // sipxAudioSetCallOutputDevice(g_hInst, "NONE") ;    // sipxAudioSetCallInputDevice(g_hInst, "SigmaTel Audio") ;}int main(int argc, char* argv[]){    bool bError = false ;    int iDuration, iSipPort, iRtpPort, iRepeatCount ;    char* szPlayTones;    char* szSipUrl;    char* szFile;    char* szUsername;    char* szPassword;    char* szRealm;    char* szFromIdentity;    char* szStunServer;    char* szProxy;    char* szOutDevice;    char* szInDevice;    char* szCodec;    bool bUseRport ;    bool bCList;    // Parse Arguments    if (parseArgs(argc, argv, &iDuration, &iSipPort, &iRtpPort, &szPlayTones, &szFile, &szSipUrl,            &bUseRport, &szUsername, &szPassword, &szRealm, &szFromIdentity, &szStunServer, &szProxy,             &iRepeatCount, &szInDevice, &szOutDevice, &szCodec, &bCList)             && (iDuration > 0) && (portIsValid(iSipPort)) && (portIsValid(iRtpPort)))    {        // initialize sipx TAPI-like API        sipxConfigSetLogLevel(LOG_LEVEL_DEBUG) ;        sipxConfigSetLogFile("PlaceCall.log");        sipxInitialize(&g_hInst, iSipPort, iSipPort, PORT_NONE, iRtpPort);        sipxConfigEnableRport(g_hInst, bUseRport) ;        dumpInputOutputDevices() ;        sipxEventListenerAdd(g_hInst, EventCallBack, NULL) ;        if (bCList)        {            int numAudioCodecs;            int numVideoCodecs;            int index;            SIPX_AUDIO_CODEC audioCodec;            SIPX_VIDEO_CODEC videoCodec;            printf("Audio codecs:\n");            if (sipxConfigGetNumAudioCodecs(g_hInst, &numAudioCodecs) == SIPX_RESULT_SUCCESS)            {                for (index=0; index<numAudioCodecs; ++index)                {                    if (sipxConfigGetAudioCodec(g_hInst, index, &audioCodec) == SIPX_RESULT_SUCCESS)                    {                        printf("  audio %02d : %s\n", index, audioCodec.cName);                    }                    else                    {                        printf("Error in retrieving audio codec #%d\n");                    }                }            }            else            {                printf("Error in retrieving number of audio codecs\n");            }            printf("Video codecs:\n");            if (sipxConfigGetNumVideoCodecs(g_hInst, &numVideoCodecs) == SIPX_RESULT_SUCCESS)            {                for (index=0; index<numVideoCodecs; ++index)                {                    if (sipxConfigGetVideoCodec(g_hInst, index, &videoCodec) == SIPX_RESULT_SUCCESS)                    {                        printf("  video %02d : %s\n", index, videoCodec.cName);                    }                    else                    {                        printf("Error in retrieving video codec #%d\n");                    }                }            }            else            {                printf("Error in retrieving number of video codecs\n");            }            exit(0);        }        if (szProxy)        {            sipxConfigSetOutboundProxy(g_hInst, szProxy);        }        if (szStunServer)        {            sipxConfigEnableStun(g_hInst, szStunServer, 28) ;        }        if (szOutDevice)        {            if (sipxAudioSetCallOutputDevice(g_hInst, szOutDevice) != SIPX_RESULT_SUCCESS)            {                printf("!! Setting output device %s failed !!\n", szOutDevice);            }        }        if (szInDevice)        {            if (sipxAudioSetCallInputDevice(g_hInst, szInDevice) != SIPX_RESULT_SUCCESS)            {                 printf("!! Setting input device %s failed !!\n", szOutDevice);            }        }        if (szCodec)        {            if (sipxConfigSetAudioCodecByName(g_hInst, szCodec) == SIPX_RESULT_FAILURE)            {                printf("!! Setting audio codec to %s failed !!\n", szCodec);            };        }        // Wait for a STUN response (should actually look for the STUN event status        // (config event) ;        SLEEP(1500) ;        for (int i=0; i<iRepeatCount; i++)        {            ClearSipXEvents() ;            printf("<-> Attempt %d of %d\n", i+1, iRepeatCount) ;            // Place a call to designed URL            if (placeCall(szSipUrl, szFromIdentity, szUsername, szPassword, szRealm))            {                bError = false ;                // Play tones if provided                if (szPlayTones)                {                    if (!playTones(szPlayTones))                    {                        printf("%s: Failed to play tones: %s\n", argv[0], szPlayTones) ;                    }                    else                    {                        bError = true ;                    }                }                // Play file if provided                if (szFile)                {                    if (!playFile(szFile))                    {                        printf("%s: Failed to play file: %s\n", argv[0], szFile) ;                    }                    else                    {                        bError = true ;                    }                }                // Leave the call up for specified time period (or wait for hangup)                WaitForSipXEvent(DISCONNECTED, iDuration) ;                // Shutdown / cleanup                if (!shutdownCall())                {                    printf("%s: Failed to shutdown call\n", argv[0]) ;                    bError = true ;                }            }            else            {                printf("%s: Unable to complete call\n", argv[0]) ;                shutdownCall() ;                bError = true ;            }                        if (bError)            {                break ;            }        }                sipxEventListenerRemove(g_hInst, EventCallBack, NULL) ;    }    else    {        usage(argv[0]) ;    }    sipxUnInitialize(g_hInst);    return (int) bError ;}#if !defined(_WIN32)// Dummy definition of JNI_LightButton() to prevent the reference in// sipXcallLib from producing an error.void JNI_LightButton(long){}#endif /* !defined(_WIN32) */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -