Liblinphone 3.9.1中文--Modules--Tutorials:

本文提供了一系列基于Liblinphone的教程示例,涵盖好友状态通知、聊天室消息传递、基本通话等功能,并介绍了如何实现注册、实时文本通信等核心操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tutorials(教程)


 1.Basic buddy status notification (基本的好友状态通知)

    这个程序是一个非常简单的liblinphone用例,演示如何发起SIP订阅和接收来自sip uri标识(从命令行传递的)的通知.

    参数必须像 sip:jehan@sip.linphone.org        

    例如 budy_list sip:jehan@sip.linphone.org

    订阅在SIGINT上清除


/*
buddy_status
Copyright (C) 2010  Belledonne Communications SARL
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#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;
}
static void notify_presence_recv_updated (LinphoneCore *lc,  LinphoneFriend *friend) {
        const LinphonePresenceModel* model = linphone_friend_get_presence_model(friend);
        const LinphoneAddress* friend_address = linphone_friend_get_address(friend);
        LinphonePresenceActivity *activity = linphone_presence_model_get_activity(model);
        char *activity_str = linphone_presence_activity_to_string(activity);
        printf("New state state [%s] for user id [%s] \n"
                                ,activity_str
                                ,linphone_address_as_string (friend_address));
}
static void new_subscription_requested (LinphoneCore *lc,  LinphoneFriend *friend, const char* url) {
        const LinphoneAddress* friend_address = linphone_friend_get_address(friend);
        printf(" [%s] wants to see your status, accepting\n"
                                ,linphone_address_as_string (friend_address));
        linphone_friend_edit(friend); /* start editing friend */
        linphone_friend_set_inc_subscribe_policy(friend,LinphoneSPAccept); /* Accept incoming subscription request for this friend*/
        linphone_friend_done(friend); /*commit change*/
        linphone_core_add_friend(lc,friend); /* add this new friend to the buddy list*/
}
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]"
                                ,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};
        char* dest_friend=NULL;
        char* identity=NULL;
        char* password=NULL;
        LinphoneFriend* my_friend=NULL;
        /* takes   sip uri  identity from the command line arguments */
        if (argc>1){
                dest_friend=argv[1];
        }
        /* takes   sip uri  identity from the command line arguments */
        if (argc>2){
                identity=argv[2];
        }
        /* takes   password from the command line arguments */
        if (argc>3){
                password=argv[3];
        }
        signal(SIGINT,stop);
//#define DEBUG
#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 both notify_presence_received and new_subscription_requested callbacks
         in order to get notifications about friend status.
         */
        vtable.notify_presence_received=notify_presence_recv_updated;
        vtable.new_subscription_requested=new_subscription_requested;
        vtable.registration_state_changed=registration_state_changed; /*just in case sip proxy is used*/
        /*
         Instantiate a LinphoneCore object given the LinphoneCoreVTable
        */
        lc=linphone_core_new(&vtable,NULL,NULL,NULL);
        /*sip proxy might be requested*/
        if (identity != NULL)  {
                /*create proxy config*/
                LinphoneProxyConfig* proxy_cfg = linphone_proxy_config_new();
                /*parse identity*/
                LinphoneAddress *from = linphone_address_new(identity);
                LinphoneAuthInfo *info;
                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*/
                linphone_proxy_config_set_server_addr(proxy_cfg,linphone_address_get_domain(from)); /* we assume domain = proxy server address*/
                linphone_proxy_config_enable_register(proxy_cfg,TRUE); /*activate registration for this proxy config*/
                linphone_proxy_config_enable_publish(proxy_cfg,TRUE); /* enable presence satus publication for this proxy*/
                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*/
                /* Loop until registration status is available */
                do {
                        linphone_core_iterate(lc); /* first iterate initiates registration */
                        ms_usleep(100000);
                }
                while(  running && linphone_proxy_config_get_state(proxy_cfg) == LinphoneRegistrationProgress);
        }
        if (dest_friend) {
                my_friend = linphone_core_create_friend_with_address(lc, dest_friend); /*creates friend object from dest*/
                if (my_friend == NULL) {
                        printf("bad destination uri for friend [%s]\n",dest_friend);
                        goto end;
                }
                linphone_friend_enable_subscribes(my_friend,TRUE); /*configure this friend to emit SUBSCRIBE message after being added to LinphoneCore*/
                linphone_friend_set_inc_subscribe_policy(my_friend,LinphoneSPAccept); /* Accept incoming subscription request for this friend*/
                linphone_core_add_friend(lc,my_friend); /* add my friend to the buddy list, initiate SUBSCRIBE message*/
        }
        /*set my status to online*/
        linphone_core_set_presence_model(lc, linphone_presence_model_new_with_activity(LinphonePresenceActivityOnline, NULL));
        /* main loop for receiving notifications and doing background linphone core work: */
        while(running){
                linphone_core_iterate(lc); /* first iterate initiates subscription */
                ms_usleep(50000);
        }
        /* change my presence status to offline*/
        linphone_core_set_presence_model(lc, linphone_presence_model_new_with_activity(LinphonePresenceActivityOffline, NULL));
        linphone_core_iterate(lc); /* just to make sure new status is initiate message is issued */
        linphone_friend_edit(my_friend); /* start editing friend */
        linphone_friend_enable_subscribes(my_friend,FALSE); /*disable subscription for this friend*/
        linphone_friend_done(my_friend); /*commit changes triggering an UNSUBSCRIBE message*/
        linphone_core_iterate(lc); /* just to make sure unsubscribe message is issued */
end:
        printf("Shutting down...\n");
        linphone_core_destroy(lc);
        printf("Exited\n");
        return 0;
}



 2.Chat room and messaging (聊天室和消息传递)

    这个程序是一个liblinphone的一个非常简单的用例,演示如何发送/接收来自一个sip uri标识的SIP消息.

    参数必须像 sip:jehan@sip.linphone.org.

    例如 chatroom sip:jehan@sip.linphone.org

    

/*
linphone
Copyright (C) 2010  Belledonne Communications SARL
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#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;
}
void text_received(LinphoneCore *lc, LinphoneChatRoom *room, const LinphoneAddress *from, const char *message) {
        printf(" Message [%s] received from [%s] \n",message,linphone_address_as_string (from));
}
LinphoneCore *lc;
int main(int argc, char *argv[]){
        LinphoneCoreVTable vtable={0};
        char* dest_friend=NULL;
        LinphoneChatRoom* chat_room;
        /* takes   sip uri  identity from the command line arguments */
        if (argc>1){
                dest_friend=argv[1];
        }
        signal(SIGINT,stop);
//#define DEBUG
#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 text_received callback
         in order to get notifications about incoming message.
         */
        vtable.text_received=text_received;
        /*
         Instantiate a LinphoneCore object given the LinphoneCoreVTable
        */
        lc=linphone_core_new(&vtable,NULL,NULL,NULL);
        /*Next step is to create a chat root*/
        chat_room = linphone_core_get_chat_room_from_uri(lc,dest_friend);
        linphone_chat_room_send_message(chat_room,"Hello world"); /*sending message*/
        /* main loop for receiving incoming messages and doing background linphone core work: */
        while(running){
                linphone_core_iterate(lc);
                ms_usleep(50000);
        }
        printf("Shutting down...\n");
        linphone_core_destroy(lc);
        printf("Exited\n");
        return 0;
}

 3.Basic call(基本的通话)

   这个程序是一个非常简单的liblinphone用例.它只是将sip uri作为第一个参数并试图调用它.

/*
linphone
Copyright (C) 2010  Belledonne Communications SARL 
 (simon.morlat@linphone.org)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#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);
        }
}
int main(int argc, char *argv[]){
        LinphoneCoreVTable vtable={0};
        LinphoneCore *lc;
        LinphoneCall *call=NULL;
        const char *dest=NULL;
        /* take the destination sip uri from the command line arguments */
        if (argc>1){
                dest=argv[1];
        }
        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 call_state_changed callbacks
         in order to get notifications about the progress of the call.
         */
        vtable.call_state_changed=call_state_changed;
        /*
         Instanciate a LinphoneCore object given the LinphoneCoreVTable
        */
        lc=linphone_core_new(&vtable,NULL,NULL,NULL);
        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);
        }
        /* main loop for receiving notifications and doing background linphonecore work: */
        while(running){
                linphone_core_iterate(lc);
                ms_usleep(50000);
        }
        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);
        }
end:
        printf("Shutting down...\n");
        linphone_core_destroy(lc);
        printf("Exited\n");
        return 0;
}



4.Generic subscribe/notify example(通用的订阅/通知例子)

   这个程序是一个非常简单的liblinphone用例.它演示了如何监听一个SIP订阅.然后定期发送通知请求.第一个参数必须像 sip:jehan@sip.linphone.org,第二个参数必须是密码.

   例如 registration sip:jehan@sip.linphone.org 密码

   注册在SIGINT上清除

/*
linphone
Copyright (C) 2010  Belledonne Communications SARL
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#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;
}
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};
        LinphoneProxyConfig* proxy_cfg;
        LinphoneAddress *from;
        LinphoneAuthInfo *info;
        char* identity=NULL;
        char* password=NULL;
        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;
        /*
         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_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);
        }
        proxy_cfg = linphone_core_get_default_proxy_config(lc); /* 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;
}


5.Real Time Text Receiver(实时文本接收器)

   这个程序是可以实时地从端口5060上接收到聊天消息.使用realtimetext_sender来生成聊天信息.用法:./realtimetext_receiver

   

/*
linphone
Copyright (C) 2015  Belledonne Communications SARL
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#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;
}
int main(int argc, char *argv[]){
        LinphoneCoreVTable vtable={0};
        LinphoneCore *lc;
        LinphoneCall *call=NULL;
        LinphoneChatRoom *chat_room;
        LinphoneChatMessage *chat_message=NULL;
        const char *dest=NULL;
        LCSipTransports tp;
        tp.udp_port=LC_SIP_TRANSPORT_RANDOM;
        tp.tcp_port=LC_SIP_TRANSPORT_RANDOM;
        tp.tls_port=LC_SIP_TRANSPORT_RANDOM;
        /* take the destination sip uri from the command line arguments */
        if (argc>1){
                dest=argv[1];
        }
        signal(SIGINT,stop);
#ifdef DEBUG
        linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
#endif
        /*
         Instanciate a LinphoneCore object given the LinphoneCoreVTable
        */
        lc=linphone_core_new(&vtable,NULL,NULL,NULL);
        linphone_core_set_sip_transports(lc,&tp); /*to avoid port colliding with receiver*/
        if (dest){
                /*
                 Place an outgoing call with rtt enabled
                */
                LinphoneCallParams *cp = linphone_core_create_call_params(lc, NULL);
                linphone_call_params_enable_realtime_text(cp,TRUE); /*enable real time text*/
                call=linphone_core_invite_with_params(lc,dest,cp);
                linphone_call_params_destroy(cp);
                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);
        }
        /*wait for call to be established*/
        while   (running && (linphone_call_get_state(call) == LinphoneCallOutgoingProgress
                                                || linphone_call_get_state(call) == LinphoneCallOutgoingInit)) {
                linphone_core_iterate(lc);
                ms_usleep(50000);
        }
        /*check if call is established*/
        switch (linphone_call_get_state(call)) {
        case LinphoneCallError:
        case LinphoneCallReleased:
        case LinphoneCallEnd:
                printf("Could not place call to %s\n",dest);
                goto end;
                break;
        default:
                break;
                /*continue*/
        }
        chat_room=linphone_call_get_chat_room(call); /*create a chat room associated to this call*/
        /* main loop for sending message and doing background linphonecore work: */
        while(running){
                char character;
                /*to disable terminal buffering*/
                if (system ("/bin/stty raw") == -1){
                        ms_error("/bin/stty error");
                }
                character = getchar();
                if (system("/bin/stty cooked") == -1){
                        ms_error("/bin/stty error");
                }
                if (character==0x03) {/*CTRL C*/
                        running=0;
                        break;
                }
                if (character != EOF) {
                        /* user has typed something*/
                        if (chat_message == NULL) {
                                /*create a new message*/
                                chat_message = linphone_chat_room_create_message(chat_room,""); /*create an empty message*/
                        }
                        if (character == '\r') {
                                /*new line, committing message*/
                                linphone_chat_room_send_chat_message(chat_room,chat_message);
                                chat_message = NULL; /*reset message*/
                        } else {
                                linphone_chat_message_put_char(chat_message,character); /*send char in realtime*/
                        }
                }
                linphone_core_iterate(lc);
                ms_usleep(50000);
        }
        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);
        }
end:
        printf("Shutting down...\n");
        linphone_core_destroy(lc);
        printf("Exited\n");
        return 0;
}


6.Real Time Text Sender

    这个程序仅实时地发送聊天消息到目标URI.用realtimetext_receiver来接收消息.用法:./realtimetext_sender sip:localhost:5060

/*
linphone
Copyright (C) 2015  Belledonne Communications SARL
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#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;
}
int main(int argc, char *argv[]){
        LinphoneCoreVTable vtable={0};
        LinphoneCore *lc;
        LinphoneCall *call=NULL;
        LinphoneChatRoom *chat_room;
        LinphoneChatMessage *chat_message=NULL;
        const char *dest=NULL;
        LCSipTransports tp;
        tp.udp_port=LC_SIP_TRANSPORT_RANDOM;
        tp.tcp_port=LC_SIP_TRANSPORT_RANDOM;
        tp.tls_port=LC_SIP_TRANSPORT_RANDOM;
        /* take the destination sip uri from the command line arguments */
        if (argc>1){
                dest=argv[1];
        }
        signal(SIGINT,stop);
#ifdef DEBUG
        linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
#endif
        /*
         Instanciate a LinphoneCore object given the LinphoneCoreVTable
        */
        lc=linphone_core_new(&vtable,NULL,NULL,NULL);
        linphone_core_set_sip_transports(lc,&tp); /*to avoid port colliding with receiver*/
        if (dest){
                /*
                 Place an outgoing call with rtt enabled
                */
                LinphoneCallParams *cp = linphone_core_create_call_params(lc, NULL);
                linphone_call_params_enable_realtime_text(cp,TRUE); /*enable real time text*/
                call=linphone_core_invite_with_params(lc,dest,cp);
                linphone_call_params_destroy(cp);
                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);
        }
        /*wait for call to be established*/
        while   (running && (linphone_call_get_state(call) == LinphoneCallOutgoingProgress
                                                || linphone_call_get_state(call) == LinphoneCallOutgoingInit)) {
                linphone_core_iterate(lc);
                ms_usleep(50000);
        }
        /*check if call is established*/
        switch (linphone_call_get_state(call)) {
        case LinphoneCallError:
        case LinphoneCallReleased:
        case LinphoneCallEnd:
                printf("Could not place call to %s\n",dest);
                goto end;
                break;
        default:
                break;
                /*continue*/
        }
        chat_room=linphone_call_get_chat_room(call); /*create a chat room associated to this call*/
        /* main loop for sending message and doing background linphonecore work: */
        while(running){
                char character;
                /*to disable terminal buffering*/
                if (system ("/bin/stty raw") == -1){
                        ms_error("/bin/stty error");
                }
                character = getchar();
                if (system("/bin/stty cooked") == -1){
                        ms_error("/bin/stty error");
                }
                if (character==0x03) {/*CTRL C*/
                        running=0;
                        break;
                }
                if (character != EOF) {
                        /* user has typed something*/
                        if (chat_message == NULL) {
                                /*create a new message*/
                                chat_message = linphone_chat_room_create_message(chat_room,""); /*create an empty message*/
                        }
                        if (character == '\r') {
                                /*new line, committing message*/
                                linphone_chat_room_send_chat_message(chat_room,chat_message);
                                chat_message = NULL; /*reset message*/
                        } else {
                                linphone_chat_message_put_char(chat_message,character); /*send char in realtime*/
                        }
                }
                linphone_core_iterate(lc);
                ms_usleep(50000);
        }
        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);
        }
end:
        printf("Shutting down...\n");
        linphone_core_destroy(lc);
        printf("Exited\n");
        return 0;
}




7.Basic registration(基本的注册)

    这个程序是一个非常简单的liblinphone用例.演示如何从一个sip URI(由命令行传递的)发起一个SIP注册.第一个参数必须像 sip:jehan@sip.linphone.org, 第二个参数必须为密码.

   例如 registration sip:jehan@sip.linphone.org 密码

   注册在SIGINT上被清除.

   

/*
linphone
Copyright (C) 2010  Belledonne Communications SARL
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#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;
}
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};
        LinphoneProxyConfig* proxy_cfg;
        LinphoneAddress *from;
        LinphoneAuthInfo *info;
        char* identity=NULL;
        char* password=NULL;
        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;
        /*
         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_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);
        }
        proxy_cfg = linphone_core_get_default_proxy_config(lc); /* 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值