Basic call¶
This program is a very simple usage example of liblinphone. It just takes a sip-uri as first argument and attempts to call it
/*
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* @defgroup basic_call_tutorials Basic call
* @ingroup tutorials
This program is a _very_ simple usage example of liblinphone.
It just takes a sip-uri as first argument and attempts to call it
@include helloworld.c
*/
#include "linphone/core.h"
#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_LOGS
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;
}
Basic registration¶
This program is a very simple usage example of liblinphone, desmonstrating how to initiate a SIP registration from a sip uri identity passed from the command line. First argument must be like sip:jehan@sip.linphone.org , second must be password . Registration is cleared on SIGINT.
Example: registration sip:jehan@sip.linphone.org secret
/*
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* @defgroup registration_tutorials Basic registration
* @ingroup tutorials
*This program is a _very_ simple usage example of liblinphone.
*Desmonstrating how to initiate a SIP registration from a sip uri identity passed from the command line.
*first argument must be like sip:jehan@sip.linphone.org , second must be password .
*<br>
*ex registration sip:jehan@sip.linphone.org secret
*<br>Registration is cleared on SIGINT
*<br>
*@include registration.c
*
*/
#include "linphone/core.h"
#include <signal.h>
static bool_t running=TRUE;
static void stop(int signum){
running=FALSE;
}
/**
* Registration state notification callback
*/
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_LOGS
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_unref(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;
}
Generic subscribe/notify example¶
This program is a very simple usage example of liblinphone. It demonstrates how to listen to a SIP subscription. It then sends notify requests back periodically. First argument must be like sip:jehan@sip.linphone.org , second must be password. Registration is cleared on SIGINT.
Example: registration sip:jehan@sip.linphone.org secret
/*
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* @defgroup registration_tutorials Basic registration
* @ingroup tutorials
*This program is a _very_ simple usage example of liblinphone.
*Desmonstrating how to initiate a SIP registration from a sip uri identity passed from the command line.
*first argument must be like sip:jehan@sip.linphone.org , second must be password .
*<br>
*ex registration sip:jehan@sip.linphone.org secret
*<br>Registration is cleared on SIGINT
*<br>
*@include registration.c
*
*/
#include "linphone/core.h"
#include <signal.h>
static bool_t running=TRUE;
static void stop(int signum){
running=FALSE;
}
/**
* Registration state notification callback
*/
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_LOGS
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_unref(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;
}
Basic buddy status notification¶
This program is a very simple usage example of liblinphone, demonstrating how to initiate SIP subscriptions and receive notifications from a sip uri identity passed from the command line. Argument must be like sip:jehan@sip.linphone.org . Subscription is cleared on SIGINT signal.
Example: budy_list sip:jehan@sip.linphone.org
/*
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* @defgroup buddy_tutorials Basic buddy status notification
* @ingroup tutorials
*This program is a _very_ simple usage example of liblinphone,
*demonstrating how to initiate SIP subscriptions and receive notifications from a sip uri identity passed from the command line.
*<br>Argument must be like sip:jehan@sip.linphone.org .
*<br>
*ex budy_list sip:jehan@sip.linphone.org
*<br>Subscription is cleared on SIGINT
*<br>
*@include buddy_status.c
*
*/
#include "linphone/core.h"
#include <signal.h>
static bool_t running=TRUE;
static void stop(int signum){
running=FALSE;
}
/**
* presence state change notification callback
*/
static void notify_presence_recv_updated (LinphoneCore *lc, LinphoneFriend *friend) {
const LinphoneAddress* friend_address = linphone_friend_get_address(friend);
if (friend_address != NULL) {
const LinphonePresenceModel* model = linphone_friend_get_presence_model(friend);
LinphonePresenceActivity *activity = linphone_presence_model_get_activity(model);
char *activity_str = linphone_presence_activity_to_string(activity);
char *str = linphone_address_as_string (friend_address);
printf("New state state [%s] for user id [%s] \n"
,activity_str
,str);
ms_free(str);
}
}
static void new_subscription_requested (LinphoneCore *lc, LinphoneFriend *friend, const char* url) {
const LinphoneAddress* friend_address = linphone_friend_get_address(friend);
if (friend_address != NULL) {
char *str = linphone_address_as_string (friend_address);
printf(" [%s] wants to see your status, accepting\n", str);
ms_free(str);
}
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*/
}
/**
* Registration state notification callback
*/
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;
LinphonePresenceModel *model;
/* 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_LOGS
#ifdef DEBUG_LOGS
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_unref(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*/
model = linphone_presence_model_new();
linphone_presence_model_set_basic_status(model, LinphonePresenceBasicStatusOpen);
linphone_core_set_presence_model(lc, model);
linphone_presence_model_unref(model);
/* 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*/
model = linphone_presence_model_new();
linphone_presence_model_set_basic_status(model, LinphonePresenceBasicStatusClosed);
linphone_core_set_presence_model(lc, model);
linphone_presence_model_unref(model);
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;
}
Chat room and messaging¶
This program is a very simple usage example of liblinphone, desmonstrating how to send/receive SIP MESSAGE from a sip uri identity passed from the command line. Argument must be like sip:jehan@sip.linphone.org .
Example: 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* @defgroup chatroom_tuto Chat room and messaging
* @ingroup tutorials
*This program is a _very_ simple usage example of liblinphone,
*desmonstrating how to send/receive SIP MESSAGE from a sip uri identity passed from the command line.
*<br>Argument must be like sip:jehan@sip.linphone.org .
*<br>
*ex chatroom sip:jehan@sip.linphone.org
*<br>
*@include chatroom.c
*
*/
#include "linphone/core.h"
#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_LOGS
#ifdef DEBUG_LOGS
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;
}
File transfer¶
/*
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* @defgroup chatroom_tuto Chat room and messaging
* @ingroup tutorials
*This program is a _very_ simple usage example of liblinphone,
*desmonstrating how to send/receive SIP MESSAGE from a sip uri identity passed from the command line.
*<br>Argument must be like sip:jehan@sip.linphone.org .
*<br>
*ex chatroom sip:jehan@sip.linphone.org
*<br>
*@include chatroom.c
*
*/
#include "linphone/core.h"
#include <signal.h>
static bool_t running=TRUE;
static void stop(int signum){
running=FALSE;
}
/**
* function invoked to report file transfer progress.
* */
static void file_transfer_progress_indication(LinphoneChatMessage *message, const LinphoneContent* content, size_t offset, size_t total) {
const LinphoneAddress* from_address = linphone_chat_message_get_from_address(message);
const LinphoneAddress* to_address = linphone_chat_message_get_to_address(message);
char *address = linphone_chat_message_is_outgoing(message)?linphone_address_as_string(to_address):linphone_address_as_string(from_address);
printf(" File transfer [%d%%] %s of type [%s/%s] %s [%s] \n", (int)((offset *100)/total)
,(linphone_chat_message_is_outgoing(message)?"sent":"received")
, linphone_content_get_type(content)
, linphone_content_get_subtype(content)
,(linphone_chat_message_is_outgoing(message)?"to":"from")
, address);
free(address);
}
/**
* function invoked when a file transfer is received.
**/
static void file_transfer_received(LinphoneChatMessage *message, const LinphoneContent* content, const LinphoneBuffer *buffer){
FILE* file=NULL;
if (!linphone_chat_message_get_user_data(message)) {
/*first chunk, creating file*/
file = fopen("receive_file.dump","wb");
linphone_chat_message_set_user_data(message,(void*)file); /*store fd for next chunks*/
}
file = (FILE*)linphone_chat_message_get_user_data(message);
if (linphone_buffer_is_empty(buffer)) {
printf("File transfert completed\n");
linphone_chat_message_unref(message);
fclose(file);
running=FALSE;
} else { /* store content on a file*/
if (fwrite(linphone_buffer_get_content(buffer),linphone_buffer_get_size(buffer),1,file)==0){
ms_warning("file_transfer_received() write failed: %s",strerror(errno));
}
}
}
char big_file [128000];
/*
* function called when the file transfer is initiated. file content should be feed into object LinphoneContent
* */
static LinphoneBuffer * file_transfer_send(LinphoneChatMessage *message, const LinphoneContent* content, size_t offset, size_t size){
size_t size_to_send = MIN(size, sizeof(big_file) - offset);
if (size == 0) return linphone_buffer_new(); /*end of file*/
return linphone_buffer_new_from_data((uint8_t *)big_file + offset, size_to_send);
}
/*
* Call back to get delivery status of a message
* */
static void linphone_file_transfer_state_changed(LinphoneChatMessage* msg,LinphoneChatMessageState state) {
const LinphoneAddress* to_address = linphone_chat_message_get_to_address(msg);
char *to = linphone_address_as_string(to_address);
printf("File transfer sent to [%s] delivery status is [%s] \n" , to
, linphone_chat_message_state_to_string(state));
free(to);
}
/*
* Call back called when a message is received
*/
static void message_received(LinphoneCore *lc, LinphoneChatRoom *cr, LinphoneChatMessage *msg) {
const LinphoneContent *file_transfer_info = linphone_chat_message_get_file_transfer_information(msg);
printf ("Do you really want to download %s (size %ld)?[Y/n]\nOk, let's go\n", linphone_content_get_name(file_transfer_info), (long int)linphone_content_get_size(file_transfer_info));
linphone_chat_message_download_file(msg);
}
LinphoneCore *lc;
int main(int argc, char *argv[]){
LinphoneCoreVTable vtable={0};
const char* dest_friend=NULL;
size_t i;
const char* big_file_content="big file";
LinphoneChatRoom* chat_room;
LinphoneContent* content;
LinphoneChatMessage* chat_message;
LinphoneChatMessageCbs *cbs;
/*seting dummy file content to something*/
for (i=0;i<sizeof(big_file);i+=strlen(big_file_content))
memcpy(big_file+i, big_file_content, strlen(big_file_content));
big_file[0]=*"S";
big_file[sizeof(big_file)-1]=*"E";
signal(SIGINT,stop);
//#define DEBUG_LOGS
#ifdef DEBUG_LOGS
linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
#endif
vtable.message_received=message_received;
/*
Instantiate a LinphoneCore object given the LinphoneCoreVTable
*/
lc=linphone_core_new(&vtable,NULL,NULL,NULL);
dest_friend = linphone_core_get_primary_contact(lc);
printf("Send message to me : %s\n", dest_friend);
/**
* Globally configure an http file transfer server.
*/
//linphone_core_set_file_transfer_server(lc,"http://npasc.al/lft.php");
linphone_core_set_file_transfer_server(lc,"https://www.linphone.org:444/lft.php");
/*Next step is to create a chat room*/
chat_room = linphone_core_get_chat_room_from_uri(lc,dest_friend);
content = linphone_core_create_content(lc);
linphone_content_set_type(content,"text");
linphone_content_set_subtype(content,"plain");
linphone_content_set_size(content,sizeof(big_file)); /*total size to be transfered*/
linphone_content_set_name(content,"bigfile.txt");
/*now create a chat message with custom content*/
chat_message = linphone_chat_room_create_file_transfer_message(chat_room,content);
if (chat_message == NULL) {
printf("returned message is null\n");
}
/**
* Fill the application callbacks. The file_transfer_received callback is used in order to get notifications
* about incoming file reception, file_transfer_send to feed file to be transfered and
* file_transfer_progress_indication to print progress.
*/
cbs = linphone_chat_message_get_callbacks(chat_message);
linphone_chat_message_cbs_set_file_transfer_recv(cbs, file_transfer_received);
linphone_chat_message_cbs_set_file_transfer_send(cbs, file_transfer_send);
linphone_chat_message_cbs_set_file_transfer_progress_indication(cbs, file_transfer_progress_indication);
linphone_chat_message_cbs_set_msg_state_changed(cbs, linphone_file_transfer_state_changed);
/*initiating file transfer*/
linphone_chat_room_send_chat_message(chat_room, chat_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_content_unref(content);
linphone_core_destroy(lc);
printf("Exited\n");
return 0;
}
Real Time Text Receiver¶
This program is able to receive chat message in real time on port 5060. Use realtimetext_sender to generate chat message
Example: ./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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* @defgroup real_time_text_sender Real Time Text Sender
* @ingroup tutorials
This program just send chat message in real time to dest uri. Use realtimetext_receiver to receive message.
usage: ./realtimetext_sender sip:localhost:5060
@include realtimetext_sender.c
*/
#include "linphone/core.h"
#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_LOGS
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_unref(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){
int 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;
}
Real Time Text Sender¶
This program just send chat message in real time to dest uri. Use realtimetext_receiver to receive message.
Example: ./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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* @defgroup real_time_text_sender Real Time Text Sender
* @ingroup tutorials
This program just send chat message in real time to dest uri. Use realtimetext_receiver to receive message.
usage: ./realtimetext_sender sip:localhost:5060
@include realtimetext_sender.c
*/
#include "linphone/core.h"
#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_LOGS
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_unref(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){
int 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;
}