liblinphone实现电话拨打及接听

示范了通过调用liblinphone拨打电话的过程,原程序 

http://mirror.yongbok.net/nongnu/linphone/docs/liblinphone/group__basic__call__tutorials.html

代码中增加了接听部分

/*
 * linphone-call.c
 * liblinphone拨打示例
 * */
#include "linphone/linphonecore.h"
#include <signal.h>

static bool_t running=TRUE;
static void stop(int signum){
        running=FALSE;
}

/*
 * 定义回调函数,用于获取拨打电话过程状态通知
 */
static void call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cstate, const char *msg){
        printf("call state: %s\n",msg);
        switch(cstate){
                case LinphoneCallIncomingReceived:
                    printf("comes a new incoming call! Accept(y/n):");
                    char s = getchar();
                    if(s == 'y')
                    {
                        printf("accept\n");
                        linphone_core_accept_call(lc, call);    //接听电话
                    }
                    else {
                        printf("refuse\n");
                        linphone_core_terminate_call(lc, call);
                    }                    
                 break;
                default:
                        printf("Unhandled notification %i\n",cstate);
        }
}

/*
 * 主函数
 */
int main(int argc, char *argv[]){
        LinphoneCoreVTable vtable={0};
        LinphoneCore *lc;
        LinphoneCall *call=NULL;
        const char *dest=NULL;

        /* 命令行参数目的地址,如sip:[email protected]*/
        if (argc>1){
                dest=argv[1];
        }

        signal(SIGINT,stop);    //获取Ctrl-C信号,以结束程序 

        /* 1.用回调函数表LinphoneCoreVTable,实例化一个LinphoneCore对像。
         * LinphoneCoreVTable中所有项都是可选的,这里仅仅设置了call_state_changed,用于获取拨打时的状态通知。
         */
        vtable.call_state_changed=call_state_changed;

        lc=linphone_core_new(&vtable,NULL,NULL,NULL);


         /*
          * 2.拨打电话
          * */
        if (dest){

                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);    //增加call的引用计数,
        }
        /*
         * 3.接收通知的主循环,调用linphone_core_iterate函数完成后台处理工作
         */
        while(running){
                linphone_core_iterate(lc); 
                ms_usleep(50000);
                printf("TT");
        }
        /*
         * 4.接收到Ctrl-C,挂断所有通话
         * */
        if(linphone_core_in_call(lc))
        {
            linphone_core_terminate_all_calls(lc);
        }

#if 0
       if (call && linphone_call_get_state(call)!=LinphoneCallEnd){
                /* terminate the call */
                printf("Terminating the call...\n");
                linphone_core_terminate_call(lc,call); /*挂断*/
                /*at this stage we don't need the call object */
                linphone_call_unref(call); 
        }
#endif

end:
        printf("Shutting down...\n");

        /*
         * 5.释放资源,销毁LinphoneCore对象
         * */
        linphone_core_destroy(lc);  

        printf("Exited\n");
        return 0;
}

在mingw中编译,使用下面的命令
$ gcc -o call linphone-call.c -llinphone.dll -lmediastreamer.dll

测试 在另一个ip上开启一个linphone客户端

拨打:

$ call sip:192.168.9.81
ortp-warning-./share/sounds/linphone/rings/oldphone.wav does not exist
Unhandled notification 2
Call to sip:192.168.9.81 is in progress...ortp-warning-cannot set noise gate mod
e to [0] because no volume send
Unhandled notification 3
ortp-warning-Failed to open ./share/sounds/linphone/ringback.wav
It is now ringing remotely !

接听:

Administrator@liangguangwei /e
$ call
ortp-warning-./share/sounds/linphone/rings/oldphone.wav does not exist
ortp-warning- searching for already_a_call_with_remote_address.
ortp-warning-Failed to open ./share/sounds/linphone/rings/oldphone.wav
call state: Incoming call
comes a new incoming call! Accept(y/n):y

PS:

程序中似乎存在一个问题,第一次接听正常,挂断后就死锁了,无法再次接听,不清楚是什么原因!






你可能感兴趣的:(object,gcc,null,basic,Signal,电话)