使用C API进行Linphone注册和拨打电话(Windows)

时间:2015-11-16 16:15:05

标签: c api linphone

你能帮我解决下一个问题。 我正在尝试通过C API(Windows)进行Linphone注册和拨打电话。 我可以使用本教程单独成功注册本地SIP服务器 Basic registration,但我不知道在使用本教程Basic call注册后如何拨打电话。 问题是Basic registration代码包含在应用程序运行时执行的永久循环:

while (running){
        linphone_core_iterate(lc); /* first iterate initiates registration */
        ms_usleep(50000);
    }

Basic call代码还包含在应用程序运行时执行的永久循环:

while(running){
                linphone_core_iterate(lc);
                ms_usleep(50000);
        }

如果要将Basic call代码放入第一个循环,程序将尝试反复拨打电话。如何进行注册,然后拨打电话?你能帮帮我吗?感谢。

我的工作代码:

#ifdef IN_LINPHONE
#include "linphonecore.h"
#else
#include "linphone/linphonecore.h"
#endif
#include <signal.h>
static bool_t running = TRUE;
static void stop(int signum){
    running = FALSE;
}
/*
* Call state notification callback
*/
static void call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cstate, const char *msg){
    switch (cstate){
    case LinphoneCallOutgoingRinging:
        printf("It is now ringing remotely !\n");
        break;
    case LinphoneCallOutgoingEarlyMedia:
        printf("Receiving some early media\n");
        break;
    case LinphoneCallConnected:
        printf("We are connected !\n");
        break;
    case LinphoneCallStreamsRunning:
        printf("Media streams established !\n");
        break;
    case LinphoneCallEnd:
        printf("Call is terminated.\n");
        break;
    case LinphoneCallError:
        printf("Call failure !");
        break;
    default:
        printf("Unhandled notification %i\n", cstate);
    }
}
static void registration_state_changed(struct _LinphoneCore *lc, LinphoneProxyConfig *cfg, LinphoneRegistrationState cstate, const char *message){
    printf("New registration state %s for user id [%s] at proxy [%s]\n"
        , linphone_registration_state_to_string(cstate)
        , linphone_proxy_config_get_identity(cfg)
        , linphone_proxy_config_get_addr(cfg));
}

LinphoneCore *lc;
int main(int argc, char *argv[]){
    LinphoneCoreVTable vtable = { 0 };
    LinphoneCore *lc;
    LinphoneCall *call = NULL;
    const char *dest = "sip:sip2@officesip.local";
    LinphoneProxyConfig* proxy_cfg;
    LinphoneAddress *from;
    LinphoneAuthInfo *info;
    char* identity = "sip:sip@officesip.local";
    char* password = "sip";
    const char* server_addr;
    /* takes   sip uri  identity from the command line arguments */
    /*if (argc>1){
        identity = argv[1];
    }
    /* takes   password from the command line arguments */
    /*if (argc>2){
        password = argv[2];
    }
    */
    //signal(SIGINT, stop);

#ifdef DEBUG
    linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
#endif
    /*
    Fill the LinphoneCoreVTable with application callbacks.
    All are optional. Here we only use the registration_state_changed callbacks
    in order to get notifications about the progress of the registration.
    */
    vtable.registration_state_changed = registration_state_changed;
    vtable.call_state_changed = call_state_changed;
    /*
    Instanciate a LinphoneCore object given the LinphoneCoreVTable
    */
    lc = linphone_core_new(&vtable, NULL, NULL, NULL);
    /*create proxy config*/
    proxy_cfg = linphone_proxy_config_new();
    /*parse identity*/
    from = linphone_address_new(identity);
    if (from == NULL){
        printf("%s not a valid sip uri, must be like sip:toto@sip.linphone.org \n", identity);
        goto end;
    }
    if (password != NULL){
        info = linphone_auth_info_new(linphone_address_get_username(from), NULL, password, NULL, NULL, NULL); /*create authentication structure from identity*/
        linphone_core_add_auth_info(lc, info); /*add authentication info to LinphoneCore*/
    }
    // configure proxy entries
    linphone_proxy_config_set_identity(proxy_cfg, identity); /*set identity with user name and domain*/
    server_addr = linphone_address_get_domain(from); /*extract domain address from identity*/
    //linphone_proxy_config_set_server_addr(proxy_cfg, server_addr); /* we assume domain = proxy server address*/
    linphone_proxy_config_set_server_addr(proxy_cfg, "localhost");
    linphone_proxy_config_enable_register(proxy_cfg, TRUE); /*activate registration for this proxy config*/
    linphone_address_destroy(from); /*release resource*/
    linphone_core_add_proxy_config(lc, proxy_cfg); /*add proxy config to linphone core*/
    linphone_core_set_default_proxy(lc, proxy_cfg); /*set to default proxy*/
    /* main loop for receiving notifications and doing background linphonecore work: */
    while (running){
        linphone_core_iterate(lc); /* first iterate initiates registration */
        ms_usleep(50000);
        //------------------------------------------------------------------------------------------------------
        //HERE I'M TRYING TO PLACE A CALL INSIDE LOOP
        //------------------------------------------------------------------------------------------------------
        if (dest){
            /*
            Place an outgoing call
            */
            call = linphone_core_invite(lc, dest);
            if (call == NULL){
                printf("Could not place call to %s\n", dest);
                goto end;
            }
            else printf("Call to %s is in progress...", dest);
            linphone_call_ref(call);
        }
        //------------------------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------------------------
    }

    linphone_core_get_default_proxy(lc, &proxy_cfg); /* get default proxy config*/
    linphone_proxy_config_edit(proxy_cfg); /*start editing proxy configuration*/
    linphone_proxy_config_enable_register(proxy_cfg, FALSE); /*de-activate registration for this proxy config*/
    linphone_proxy_config_done(proxy_cfg); /*initiate REGISTER with expire = 0*/
    while (linphone_proxy_config_get_state(proxy_cfg) != LinphoneRegistrationCleared){
        linphone_core_iterate(lc); /*to make sure we receive call backs before shutting down*/
        ms_usleep(50000);
    }

end:
    printf("Shutting down...\n");
    linphone_core_destroy(lc);
    printf("Exited\n");
    return 0;
}

1 个答案:

答案 0 :(得分:1)

以下是this example的解决方案,使用:

/* Loop until registration is OK */
    do {
        linphone_core_iterate(lc); /* first iterate initiates registration */
        ms_usleep(100000);
    } while (running && linphone_proxy_config_get_state(proxy_cfg) == LinphoneRegistrationProgress);

而不是:

while (running){
        linphone_core_iterate(lc); /* first iterate initiates registration */
        ms_usleep(50000);
    }

之后,我可以拨打电话并发送\接收短信。