3 Copyright (C) 2000 Simon MORLAT (simon.morlat@linphone.org)
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "linphonecore.h"
24 #include "mediastreamer2/mediastream.h"
25 #include "mediastreamer2/msvolume.h"
26 #include "mediastreamer2/msequalizer.h"
28 #include <ortp/telephonyevents.h>
37 /*#define UNSTANDART_GSM_11K 1*/
39 static const char *liblinphone_version=LIBLINPHONE_VERSION;
40 static void set_network_reachable(LinphoneCore* lc,bool_t isReachable);
44 void linphone_core_get_local_ip(LinphoneCore *lc, const char *dest, char *result);
45 static void toggle_video_preview(LinphoneCore *lc, bool_t val);
47 /* relative path where is stored local ring*/
48 #define LOCAL_RING "rings/oldphone.wav"
49 /* same for remote ring (ringback)*/
50 #define REMOTE_RING "ringback.wav"
52 extern SalCallbacks linphone_sal_callbacks;
54 void lc_callback_obj_init(LCCallbackObj *obj,LinphoneCoreCbFunc func,void* ud)
60 int lc_callback_obj_invoke(LCCallbackObj *obj, LinphoneCore *lc){
61 if (obj->_func!=NULL) obj->_func(lc,obj->_user_data);
66 /*prevent a gcc bug with %c*/
67 static size_t my_strftime(char *s, size_t max, const char *fmt, const struct tm *tm){
68 #if !defined(_WIN32_WCE)
69 return strftime(s, max, fmt, tm);
76 static void set_call_log_date(LinphoneCallLog *cl, const struct tm *loctime){
77 my_strftime(cl->start_date,sizeof(cl->start_date),"%c",loctime);
80 LinphoneCallLog * linphone_call_log_new(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
81 LinphoneCallLog *cl=ms_new0(LinphoneCallLog,1);
85 #if !defined(_WIN32_WCE)
86 loctime=*localtime(&call->start_time);
90 localtime_r(&call->start_time,&loctime);
92 set_call_log_date(cl,&loctime);
98 static void call_logs_write_to_config_file(LinphoneCore *lc){
103 LpConfig *cfg=lc->config;
105 if (!lc->ready) return;
107 for(i=0,elem=lc->call_logs;elem!=NULL;elem=elem->next,++i){
108 LinphoneCallLog *cl=(LinphoneCallLog*)elem->data;
109 snprintf(logsection,sizeof(logsection),"call_log_%i",i);
110 lp_config_set_int(cfg,logsection,"dir",cl->dir);
111 lp_config_set_int(cfg,logsection,"status",cl->status);
112 tmp=linphone_address_as_string(cl->from);
113 lp_config_set_string(cfg,logsection,"from",tmp);
115 tmp=linphone_address_as_string(cl->to);
116 lp_config_set_string(cfg,logsection,"to",tmp);
118 lp_config_set_string(cfg,logsection,"start_date",cl->start_date);
119 lp_config_set_int(cfg,logsection,"duration",cl->duration);
120 if (cl->refkey) lp_config_set_string(cfg,logsection,"refkey",cl->refkey);
122 for(;i<lc->max_call_logs;++i){
123 snprintf(logsection,sizeof(logsection),"call_log_%i",i);
124 lp_config_clean_section(cfg,logsection);
128 static void call_logs_read_from_config_file(LinphoneCore *lc){
132 LpConfig *cfg=lc->config;
134 snprintf(logsection,sizeof(logsection),"call_log_%i",i);
135 if (lp_config_has_section(cfg,logsection)){
136 LinphoneCallLog *cl=ms_new0(LinphoneCallLog,1);
137 cl->dir=lp_config_get_int(cfg,logsection,"dir",0);
138 cl->status=lp_config_get_int(cfg,logsection,"status",0);
139 tmp=lp_config_get_string(cfg,logsection,"from",NULL);
140 if (tmp) cl->from=linphone_address_new(tmp);
141 tmp=lp_config_get_string(cfg,logsection,"to",NULL);
142 if (tmp) cl->to=linphone_address_new(tmp);
143 tmp=lp_config_get_string(cfg,logsection,"start_date",NULL);
144 if (tmp) strncpy(cl->start_date,tmp,sizeof(cl->start_date));
145 cl->duration=lp_config_get_int(cfg,logsection,"duration",0);
146 tmp=lp_config_get_string(cfg,logsection,"refkey",NULL);
147 if (tmp) cl->refkey=ms_strdup(tmp);
148 lc->call_logs=ms_list_append(lc->call_logs,cl);
154 void linphone_call_log_completed(LinphoneCallLog *calllog, LinphoneCall *call, LinphoneCallStatus status){
155 LinphoneCore *lc=call->core;
157 calllog->duration=time(NULL)-call->start_time;
159 if (status==LinphoneCallMissed){
162 info=ortp_strdup_printf(ngettext("You have missed %i call.",
163 "You have missed %i calls.", lc->missed_calls),
165 lc->vtable.display_status(lc,info);
167 }else calllog->status=status;
168 lc->call_logs=ms_list_prepend(lc->call_logs,(void *)calllog);
169 if (ms_list_size(lc->call_logs)>lc->max_call_logs){
170 MSList *elem,*prevelem=NULL;
171 /*find the last element*/
172 for(elem=lc->call_logs;elem!=NULL;elem=elem->next){
176 linphone_call_log_destroy((LinphoneCallLog*)elem->data);
177 lc->call_logs=ms_list_remove_link(lc->call_logs,elem);
179 if (lc->vtable.call_log_updated!=NULL){
180 lc->vtable.call_log_updated(lc,calllog);
182 call_logs_write_to_config_file(lc);
186 * @addtogroup call_logs
191 * Returns a human readable string describing the call.
193 * @note: the returned char* must be freed by the application (use ms_free()).
195 char * linphone_call_log_to_str(LinphoneCallLog *cl){
198 char *from=linphone_address_as_string (cl->from);
199 char *to=linphone_address_as_string (cl->to);
201 case LinphoneCallAborted:
204 case LinphoneCallSuccess:
205 status=_("completed");
207 case LinphoneCallMissed:
213 tmp=ortp_strdup_printf(_("%s at %s\nFrom: %s\nTo: %s\nStatus: %s\nDuration: %i mn %i sec\n"),
214 (cl->dir==LinphoneCallIncoming) ? _("Incoming call") : _("Outgoing call"),
226 void linphone_call_log_set_user_pointer(LinphoneCallLog *cl, void *up){
230 void *linphone_call_log_get_user_pointer(const LinphoneCallLog *cl){
231 return cl->user_pointer;
237 * Associate a persistent reference key to the call log.
239 * The reference key can be for example an id to an external database.
240 * It is stored in the config file, thus can survive to process exits/restarts.
243 void linphone_call_log_set_ref_key(LinphoneCallLog *cl, const char *refkey){
244 if (cl->refkey!=NULL){
248 if (refkey) cl->refkey=ms_strdup(refkey);
249 call_logs_write_to_config_file(cl->lc);
253 * Get the persistent reference key associated to the call log.
255 * The reference key can be for example an id to an external database.
256 * It is stored in the config file, thus can survive to process exits/restarts.
259 const char *linphone_call_log_get_ref_key(const LinphoneCallLog *cl){
265 void linphone_call_log_destroy(LinphoneCallLog *cl){
266 if (cl->from!=NULL) linphone_address_destroy(cl->from);
267 if (cl->to!=NULL) linphone_address_destroy(cl->to);
268 if (cl->refkey!=NULL) ms_free(cl->refkey);
273 * Returns TRUE if the LinphoneCall asked to autoanswer
276 bool_t linphone_call_asked_to_autoanswer(LinphoneCall *call){
277 //return TRUE if the unique(for the moment) incoming call asked to be autoanswered
279 return sal_call_autoanswer_asked(call->op);
284 int linphone_core_get_call_duration(LinphoneCall *call){
285 if (call==NULL) return 0;
286 if (call->media_start_time==0) return 0;
287 return time(NULL)-call->media_start_time;
290 int linphone_core_get_current_call_duration(const LinphoneCore *lc){
291 LinphoneCall *call=linphone_core_get_current_call((LinphoneCore *)lc);
292 return linphone_core_get_call_duration(call);
295 const LinphoneAddress *linphone_core_get_current_call_remote_address(struct _LinphoneCore *lc){
296 LinphoneCall *call=linphone_core_get_current_call(lc);
297 if (call==NULL) return 0;
298 return linphone_call_get_remote_address(call);
302 * Enable logs in supplied FILE*.
306 * @param file a C FILE* where to fprintf logs. If null stdout is used.
309 void linphone_core_enable_logs(FILE *file){
310 if (file==NULL) file=stdout;
311 ortp_set_log_file(file);
312 ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
316 * Enable logs through the user's supplied log callback.
320 * @param logfunc The address of a OrtpLogFunc callback whose protoype is
321 * typedef void (*OrtpLogFunc)(OrtpLogLevel lev, const char *fmt, va_list args);
324 void linphone_core_enable_logs_with_cb(OrtpLogFunc logfunc){
325 ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
326 ortp_set_log_handler(logfunc);
330 * Entirely disable logging.
334 void linphone_core_disable_logs(){
335 ortp_set_log_level_mask(ORTP_ERROR|ORTP_FATAL);
340 net_config_read (LinphoneCore *lc)
344 LpConfig *config=lc->config;
346 tmp=lp_config_get_int(config,"net","download_bw",0);
347 linphone_core_set_download_bandwidth(lc,tmp);
348 tmp=lp_config_get_int(config,"net","upload_bw",0);
349 linphone_core_set_upload_bandwidth(lc,tmp);
350 linphone_core_set_stun_server(lc,lp_config_get_string(config,"net","stun_server",NULL));
351 tmpstr=lp_config_get_string(lc->config,"net","nat_address",NULL);
352 if (tmpstr!=NULL && (strlen(tmpstr)<1)) tmpstr=NULL;
353 linphone_core_set_nat_address(lc,tmpstr);
354 tmp=lp_config_get_int(lc->config,"net","firewall_policy",0);
355 linphone_core_set_firewall_policy(lc,tmp);
356 tmp=lp_config_get_int(lc->config,"net","nat_sdp_only",0);
357 lc->net_conf.nat_sdp_only=tmp;
358 tmp=lp_config_get_int(lc->config,"net","mtu",0);
359 linphone_core_set_mtu(lc,tmp);
360 tmp=lp_config_get_int(lc->config,"net","download_ptime",0);
361 linphone_core_set_download_ptime(lc,tmp);
365 static void build_sound_devices_table(LinphoneCore *lc){
366 const char **devices;
370 const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
371 ndev=ms_list_size(elem);
372 devices=ms_malloc((ndev+1)*sizeof(const char *));
373 for (i=0;elem!=NULL;elem=elem->next,i++){
374 devices[i]=ms_snd_card_get_string_id((MSSndCard *)elem->data);
377 old=lc->sound_conf.cards;
378 lc->sound_conf.cards=devices;
379 if (old!=NULL) ms_free(old);
382 static void sound_config_read(LinphoneCore *lc)
389 /*alsadev let the user use custom alsa device within linphone*/
390 devid=lp_config_get_string(lc->config,"sound","alsadev",NULL);
392 MSSndCard *card=ms_alsa_card_new_custom(devid,devid);
393 ms_snd_card_manager_add_card(ms_snd_card_manager_get(),card);
396 /* retrieve all sound devices */
397 build_sound_devices_table(lc);
399 devid=lp_config_get_string(lc->config,"sound","playback_dev_id",NULL);
400 linphone_core_set_playback_device(lc,devid);
402 devid=lp_config_get_string(lc->config,"sound","ringer_dev_id",NULL);
403 linphone_core_set_ringer_device(lc,devid);
405 devid=lp_config_get_string(lc->config,"sound","capture_dev_id",NULL);
406 linphone_core_set_capture_device(lc,devid);
409 tmp=lp_config_get_int(lc->config,"sound","play_lev",80);
410 linphone_core_set_play_level(lc,tmp);
411 tmp=lp_config_get_int(lc->config,"sound","ring_lev",80);
412 linphone_core_set_ring_level(lc,tmp);
413 tmp=lp_config_get_int(lc->config,"sound","rec_lev",80);
414 linphone_core_set_rec_level(lc,tmp);
415 tmpbuf=lp_config_get_string(lc->config,"sound","source","m");
416 linphone_core_set_sound_source(lc,tmpbuf[0]);
419 tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
420 tmpbuf=lp_config_get_string(lc->config,"sound","local_ring",tmpbuf);
421 if (ortp_file_exist(tmpbuf)==-1) {
422 tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
424 if (strstr(tmpbuf,".wav")==NULL){
425 /* it currently uses old sound files, so replace them */
426 tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
429 linphone_core_set_ring(lc,tmpbuf);
431 tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
432 tmpbuf=lp_config_get_string(lc->config,"sound","remote_ring",tmpbuf);
433 if (ortp_file_exist(tmpbuf)==-1){
434 tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
436 if (strstr(tmpbuf,".wav")==NULL){
437 /* it currently uses old sound files, so replace them */
438 tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
440 linphone_core_set_ringback(lc,tmpbuf);
441 check_sound_device(lc);
442 lc->sound_conf.latency=0;
444 linphone_core_enable_echo_cancellation(lc,
445 lp_config_get_int(lc->config,"sound","echocancelation",0) |
446 lp_config_get_int(lc->config,"sound","echocancellation",0)
449 linphone_core_enable_echo_limiter(lc,
450 lp_config_get_int(lc->config,"sound","echolimiter",0));
451 linphone_core_enable_agc(lc,
452 lp_config_get_int(lc->config,"sound","agc",0));
454 gain=lp_config_get_float(lc->config,"sound","soft_play_lev",0);
455 linphone_core_set_soft_play_level(lc,gain);
458 static void sip_config_read(LinphoneCore *lc)
465 port=lp_config_get_int(lc->config,"sip","use_info",0);
466 linphone_core_set_use_info_for_dtmf(lc,port);
468 port=lp_config_get_int(lc->config,"sip","use_rfc2833",0);
469 linphone_core_set_use_rfc2833_for_dtmf(lc,port);
471 ipv6=lp_config_get_int(lc->config,"sip","use_ipv6",-1);
474 if (host_has_ipv6_network()){
475 lc->vtable.display_message(lc,_("Your machine appears to be connected to an IPv6 network. By default linphone always uses IPv4. Please update your configuration if you want to use IPv6"));
478 linphone_core_enable_ipv6(lc,ipv6);
479 port=lp_config_get_int(lc->config,"sip","sip_port",5060);
480 linphone_core_set_sip_port(lc,port);
482 tmpstr=lp_config_get_string(lc->config,"sip","contact",NULL);
483 if (tmpstr==NULL || linphone_core_set_primary_contact(lc,tmpstr)==-1) {
484 const char *hostname=NULL;
485 const char *username=NULL;
487 hostname=getenv("HOST");
488 username=getenv("USER");
489 if (hostname==NULL) hostname=getenv("HOSTNAME");
490 #endif /*HAVE_GETENV*/
492 hostname="unknown-host";
496 contact=ortp_strdup_printf("sip:%s@%s",username,hostname);
497 linphone_core_set_primary_contact(lc,contact);
501 tmp=lp_config_get_int(lc->config,"sip","guess_hostname",1);
502 linphone_core_set_guess_hostname(lc,tmp);
505 tmp=lp_config_get_int(lc->config,"sip","inc_timeout",15);
506 linphone_core_set_inc_timeout(lc,tmp);
508 /* get proxies config */
510 LinphoneProxyConfig *cfg=linphone_proxy_config_new_from_config_file(lc->config,i);
512 linphone_core_add_proxy_config(lc,cfg);
517 /* get the default proxy */
518 tmp=lp_config_get_int(lc->config,"sip","default_proxy",-1);
519 linphone_core_set_default_proxy_index(lc,tmp);
521 /* read authentication information */
523 LinphoneAuthInfo *ai=linphone_auth_info_new_from_config_file(lc->config,i);
525 linphone_core_add_auth_info(lc,ai);
526 linphone_auth_info_destroy(ai);
532 /*for tuning or test*/
533 lc->sip_conf.sdp_200_ack=lp_config_get_int(lc->config,"sip","sdp_200_ack",0);
534 lc->sip_conf.only_one_codec=lp_config_get_int(lc->config,"sip","only_one_codec",0);
535 lc->sip_conf.register_only_when_network_is_up=
536 lp_config_get_int(lc->config,"sip","register_only_when_network_is_up",1);
537 lc->sip_conf.ping_with_options=lp_config_get_int(lc->config,"sip","ping_with_options",1);
538 lc->sip_conf.auto_net_state_mon=lp_config_get_int(lc->config,"sip","auto_net_state_mon",1);
541 static void rtp_config_read(LinphoneCore *lc)
546 port=lp_config_get_int(lc->config,"rtp","audio_rtp_port",7078);
547 linphone_core_set_audio_port(lc,port);
549 port=lp_config_get_int(lc->config,"rtp","video_rtp_port",9078);
550 if (port==0) port=9078;
551 linphone_core_set_video_port(lc,port);
553 jitt_comp=lp_config_get_int(lc->config,"rtp","audio_jitt_comp",60);
554 linphone_core_set_audio_jittcomp(lc,jitt_comp);
555 jitt_comp=lp_config_get_int(lc->config,"rtp","video_jitt_comp",60);
556 nortp_timeout=lp_config_get_int(lc->config,"rtp","nortp_timeout",30);
557 linphone_core_set_nortp_timeout(lc,nortp_timeout);
560 static PayloadType * find_payload(RtpProfile *prof, const char *mime_type, int clock_rate, const char *recv_fmtp){
561 PayloadType *candidate=NULL;
565 it=rtp_profile_get_payload(prof,i);
566 if (it!=NULL && strcasecmp(mime_type,it->mime_type)==0
567 && (clock_rate==it->clock_rate || clock_rate<=0) ){
568 if ( (recv_fmtp && it->recv_fmtp && strstr(recv_fmtp,it->recv_fmtp)!=NULL) ||
569 (recv_fmtp==NULL && it->recv_fmtp==NULL) ){
571 if (recv_fmtp) payload_type_set_recv_fmtp(it,recv_fmtp);
575 if (it->recv_fmtp==NULL) candidate=it;
580 if (candidate && recv_fmtp){
581 payload_type_set_recv_fmtp(candidate,recv_fmtp);
586 static bool_t get_codec(LpConfig *config, char* type, int index, PayloadType **ret){
588 const char *mime,*fmtp;
593 snprintf(codeckey,50,"%s_%i",type,index);
594 mime=lp_config_get_string(config,codeckey,"mime",NULL);
595 if (mime==NULL || strlen(mime)==0 ) return FALSE;
597 rate=lp_config_get_int(config,codeckey,"rate",8000);
598 fmtp=lp_config_get_string(config,codeckey,"recv_fmtp",NULL);
599 enabled=lp_config_get_int(config,codeckey,"enabled",1);
600 pt=find_payload(&av_profile,mime,rate,fmtp);
601 if (pt && enabled ) pt->flags|=PAYLOAD_TYPE_ENABLED;
602 //ms_message("Found codec %s/%i",pt->mime_type,pt->clock_rate);
603 if (pt==NULL) ms_warning("Ignoring codec config %s/%i with fmtp=%s because unsupported",
604 mime,rate,fmtp ? fmtp : "");
609 static const char *codec_pref_order[]={
622 static int find_codec_rank(const char *mime){
624 for(i=0;codec_pref_order[i]!=NULL;++i){
625 if (strcasecmp(codec_pref_order[i],mime)==0)
631 static int codec_compare(const PayloadType *a, const PayloadType *b){
633 ra=find_codec_rank(a->mime_type);
634 rb=find_codec_rank(b->mime_type);
636 if (ra<rb) return -1;
640 static MSList *add_missing_codecs(SalStreamType mtype, MSList *l){
643 PayloadType *pt=rtp_profile_get_payload(&av_profile,i);
645 if (mtype==SalVideo && pt->type!=PAYLOAD_VIDEO)
647 else if (mtype==SalAudio && (pt->type!=PAYLOAD_AUDIO_PACKETIZED
648 && pt->type!=PAYLOAD_AUDIO_CONTINUOUS)){
651 if (pt && ms_filter_codec_supported(pt->mime_type)){
652 if (ms_list_find(l,pt)==NULL){
653 payload_type_set_flag(pt,PAYLOAD_TYPE_ENABLED);
654 ms_message("Adding new codec %s/%i with fmtp %s",
655 pt->mime_type,pt->clock_rate,pt->recv_fmtp ? pt->recv_fmtp : "");
656 l=ms_list_insert_sorted(l,pt,(int (*)(const void *, const void *))codec_compare);
664 static void codecs_config_read(LinphoneCore *lc)
668 MSList *audio_codecs=NULL;
669 MSList *video_codecs=NULL;
670 for (i=0;get_codec(lc->config,"audio_codec",i,&pt);i++){
672 if (!ms_filter_codec_supported(pt->mime_type)){
673 ms_warning("Codec %s is not supported by mediastreamer2, removed.",pt->mime_type);
674 }else audio_codecs=ms_list_append(audio_codecs,pt);
677 audio_codecs=add_missing_codecs(SalAudio,audio_codecs);
678 for (i=0;get_codec(lc->config,"video_codec",i,&pt);i++){
680 if (!ms_filter_codec_supported(pt->mime_type)){
681 ms_warning("Codec %s is not supported by mediastreamer2, removed.",pt->mime_type);
682 }else video_codecs=ms_list_append(video_codecs,(void *)pt);
685 video_codecs=add_missing_codecs(SalVideo,video_codecs);
686 linphone_core_set_audio_codecs(lc,audio_codecs);
687 linphone_core_set_video_codecs(lc,video_codecs);
688 linphone_core_update_allocated_audio_bandwidth(lc);
691 static void video_config_read(LinphoneCore *lc){
692 int capture, display, self_view;
696 const char **devices;
700 /* retrieve all video devices */
701 elem=ms_web_cam_manager_get_list(ms_web_cam_manager_get());
702 ndev=ms_list_size(elem);
703 devices=ms_malloc((ndev+1)*sizeof(const char *));
704 for (i=0;elem!=NULL;elem=elem->next,i++){
705 devices[i]=ms_web_cam_get_string_id((MSWebCam *)elem->data);
708 lc->video_conf.cams=devices;
710 str=lp_config_get_string(lc->config,"video","device",NULL);
711 if (str && str[0]==0) str=NULL;
712 linphone_core_set_video_device(lc,str);
714 linphone_core_set_preferred_video_size_by_name(lc,
715 lp_config_get_string(lc->config,"video","size","cif"));
717 enabled=lp_config_get_int(lc->config,"video","enabled",1);
718 capture=lp_config_get_int(lc->config,"video","capture",enabled);
719 display=lp_config_get_int(lc->config,"video","display",enabled);
720 self_view=lp_config_get_int(lc->config,"video","self_view",enabled);
722 linphone_core_enable_video(lc,capture,display);
723 linphone_core_enable_self_view(lc,self_view);
727 static void ui_config_read(LinphoneCore *lc)
731 for (i=0;(lf=linphone_friend_new_from_config_file(lc,i))!=NULL;i++){
732 linphone_core_add_friend(lc,lf);
734 call_logs_read_from_config_file(lc);
738 static void autoreplier_config_init(LinphoneCore *lc)
740 autoreplier_config_t *config=&lc->autoreplier_conf;
741 config->enabled=lp_config_get_int(lc->config,"autoreplier","enabled",0);
742 config->after_seconds=lp_config_get_int(lc->config,"autoreplier","after_seconds",6);
743 config->max_users=lp_config_get_int(lc->config,"autoreplier","max_users",1);
744 config->max_rec_time=lp_config_get_int(lc->config,"autoreplier","max_rec_time",60);
745 config->max_rec_msg=lp_config_get_int(lc->config,"autoreplier","max_rec_msg",10);
746 config->message=lp_config_get_string(lc->config,"autoreplier","message",NULL);
751 * Sets maximum available download bandwidth
753 * @ingroup media_parameters
755 * This is IP bandwidth, in kbit/s.
756 * This information is used signaled to other parties during
757 * calls (within SDP messages) so that the remote end can have
758 * sufficient knowledge to properly configure its audio & video
759 * codec output bitrate to not overflow available bandwidth.
761 * @param lc the LinphoneCore object
762 * @param bw the bandwidth in kbits/s, 0 for infinite
764 void linphone_core_set_download_bandwidth(LinphoneCore *lc, int bw){
765 lc->net_conf.download_bw=bw;
766 if (bw==0){ /*infinite*/
770 lc->dw_audio_bw=MIN(lc->audio_bw,bw);
771 lc->dw_video_bw=MAX(bw-lc->dw_audio_bw-10,0);/*-10: security margin*/
776 * Sets maximum available upload bandwidth
778 * @ingroup media_parameters
780 * This is IP bandwidth, in kbit/s.
781 * This information is used by liblinphone together with remote
782 * side available bandwidth signaled in SDP messages to properly
783 * configure audio & video codec's output bitrate.
785 * @param lc the LinphoneCore object
786 * @param bw the bandwidth in kbits/s, 0 for infinite
788 void linphone_core_set_upload_bandwidth(LinphoneCore *lc, int bw){
789 lc->net_conf.upload_bw=bw;
790 if (bw==0){ /*infinite*/
794 lc->up_audio_bw=MIN(lc->audio_bw,bw);
795 lc->up_video_bw=MAX(bw-lc->up_audio_bw-10,0);/*-10: security margin*/
800 * Retrieve the maximum available download bandwidth.
802 * @ingroup media_parameters
804 * This value was set by linphone_core_set_download_bandwidth().
807 int linphone_core_get_download_bandwidth(const LinphoneCore *lc){
808 return lc->net_conf.download_bw;
812 * Retrieve the maximum available upload bandwidth.
814 * @ingroup media_parameters
816 * This value was set by linphone_core_set_upload_bandwidth().
819 int linphone_core_get_upload_bandwidth(const LinphoneCore *lc){
820 return lc->net_conf.upload_bw;
823 * set audio packetization time linphone expect to received from peer
825 void linphone_core_set_download_ptime(LinphoneCore *lc, int ptime) {
826 lc->net_conf.down_ptime=ptime;
829 int linphone_core_get_download_ptime(LinphoneCore *lc) {
830 return lc->net_conf.down_ptime;
835 * Returns liblinphone's version as a string.
840 const char * linphone_core_get_version(void){
841 return liblinphone_version;
845 static MSList *linphone_payload_types=NULL;
847 static void linphone_core_assign_payload_type(PayloadType *const_pt, int number, const char *recv_fmtp){
849 pt=payload_type_clone(const_pt);
850 payload_type_set_number(pt,number);
851 if (recv_fmtp!=NULL) payload_type_set_recv_fmtp(pt,recv_fmtp);
852 rtp_profile_set_payload(&av_profile,number,pt);
853 linphone_payload_types=ms_list_append(linphone_payload_types,pt);
856 static void linphone_core_free_payload_types(void){
857 ms_list_for_each(linphone_payload_types,(void (*)(void*))payload_type_destroy);
858 ms_list_free(linphone_payload_types);
859 linphone_payload_types=NULL;
862 static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vtable, const char *config_path,
863 const char *factory_config_path, void * userdata)
865 memset (lc, 0, sizeof (LinphoneCore));
868 memcpy(&lc->vtable,vtable,sizeof(LinphoneCoreVTable));
870 gstate_initialize(lc);
871 gstate_new_state(lc, GSTATE_POWER_STARTUP, NULL);
874 linphone_core_assign_payload_type(&payload_type_pcmu8000,0,NULL);
875 linphone_core_assign_payload_type(&payload_type_gsm,3,NULL);
876 linphone_core_assign_payload_type(&payload_type_pcma8000,8,NULL);
877 linphone_core_assign_payload_type(&payload_type_lpc1015,115,NULL);
878 linphone_core_assign_payload_type(&payload_type_speex_nb,110,"vbr=on");
879 linphone_core_assign_payload_type(&payload_type_speex_wb,111,"vbr=on");
880 linphone_core_assign_payload_type(&payload_type_speex_uwb,112,"vbr=on");
881 linphone_core_assign_payload_type(&payload_type_telephone_event,101,"0-11");
882 linphone_core_assign_payload_type(&payload_type_ilbc,113,"mode=30");
883 linphone_core_assign_payload_type(&payload_type_amr,114,"octet-align=1");
885 #ifdef ENABLE_NONSTANDARD_GSM
888 pt=payload_type_clone(&payload_type_gsm);
889 pt->clock_rate=11025;
890 rtp_profile_set_payload(&av_profile,114,pt);
891 linphone_payload_types=ms_list_append(linphone_payload_types,pt);
892 pt=payload_type_clone(&payload_type_gsm);
893 pt->clock_rate=22050;
894 rtp_profile_set_payload(&av_profile,115,pt);
895 linphone_payload_types=ms_list_append(linphone_payload_types,pt);
900 linphone_core_assign_payload_type(&payload_type_h263,34,NULL);
901 linphone_core_assign_payload_type(&payload_type_theora,97,NULL);
902 linphone_core_assign_payload_type(&payload_type_h263_1998,98,"CIF=1;QCIF=1");
903 linphone_core_assign_payload_type(&payload_type_mp4v,99,"profile-level-id=3");
904 linphone_core_assign_payload_type(&payload_type_x_snow,100,NULL);
905 linphone_core_assign_payload_type(&payload_type_h264,102,NULL);
906 linphone_core_assign_payload_type(&payload_type_h264,103,"packetization-mode=1");
911 lc->config=lp_config_new(config_path);
912 if (factory_config_path)
913 lp_config_read_file(lc->config,factory_config_path);
916 sal_set_user_pointer(lc->sal,lc);
917 sal_set_callbacks(lc->sal,&linphone_sal_callbacks);
918 if (lp_config_get_int(lc->config,"sip","use_session_timers",0)==1){
919 sal_use_session_timers(lc->sal,200);
921 sip_setup_register_all();
922 sound_config_read(lc);
925 codecs_config_read(lc);
926 sip_config_read(lc); /* this will start eXosip*/
927 video_config_read(lc);
928 //autoreplier_config_init(&lc->autoreplier_conf);
929 lc->prev_mode=LINPHONE_STATUS_ONLINE;
930 lc->presence_mode=LINPHONE_STATUS_ONLINE;
931 lc->max_call_logs=15;
933 lc->vtable.display_status(lc,_("Ready"));
934 gstate_new_state(lc, GSTATE_POWER_ON, NULL);
935 lc->auto_net_state_mon=lc->sip_conf.auto_net_state_mon;
941 * Instanciates a LinphoneCore object.
942 * @ingroup initializing
944 * The LinphoneCore object is the primary handle for doing all phone actions.
945 * It should be unique within your application.
946 * @param vtable a LinphoneCoreVTable structure holding your application callbacks
947 * @param config_path a path to a config file. If it does not exists it will be created.
948 * The config file is used to store all user settings, call logs, friends, proxies...
949 * @param factory_config_path a path to a read-only config file that can be used to
950 * to store hard-coded preference such as proxy settings or internal preferences.
951 * The settings in this factory file always override the one in the normal config file.
952 * It is OPTIONAL, use NULL if unneeded.
953 * @param userdata an opaque user pointer that can be retrieved at any time (for example in
954 * callbacks) using linphone_core_get_user_data().
957 LinphoneCore *linphone_core_new(const LinphoneCoreVTable *vtable,
958 const char *config_path, const char *factory_config_path, void * userdata)
960 LinphoneCore *core=ms_new(LinphoneCore,1);
961 linphone_core_init(core,vtable,config_path, factory_config_path, userdata);
966 * Returns the list of available audio codecs.
968 * This list is unmodifiable. The ->data field of the MSList points a PayloadType
969 * structure holding the codec information.
970 * It is possible to make copy of the list with ms_list_copy() in order to modify it
971 * (such as the order of codecs).
973 const MSList *linphone_core_get_audio_codecs(const LinphoneCore *lc)
975 return lc->codecs_conf.audio_codecs;
979 * Returns the list of available video codecs.
981 * This list is unmodifiable. The ->data field of the MSList points a PayloadType
982 * structure holding the codec information.
983 * It is possible to make copy of the list with ms_list_copy() in order to modify it
984 * (such as the order of codecs).
986 const MSList *linphone_core_get_video_codecs(const LinphoneCore *lc)
988 return lc->codecs_conf.video_codecs;
992 * Sets the local "from" identity.
995 * This data is used in absence of any proxy configuration or when no
996 * default proxy configuration is set. See LinphoneProxyConfig
998 int linphone_core_set_primary_contact(LinphoneCore *lc, const char *contact)
1000 LinphoneAddress *ctt;
1002 if ((ctt=linphone_address_new(contact))==0) {
1003 ms_error("Bad contact url: %s",contact);
1006 if (lc->sip_conf.contact!=NULL) ms_free(lc->sip_conf.contact);
1007 lc->sip_conf.contact=ms_strdup(contact);
1008 if (lc->sip_conf.guessed_contact!=NULL){
1009 ms_free(lc->sip_conf.guessed_contact);
1010 lc->sip_conf.guessed_contact=NULL;
1012 linphone_address_destroy(ctt);
1017 /*result must be an array of chars at least LINPHONE_IPADDR_SIZE */
1018 void linphone_core_get_local_ip(LinphoneCore *lc, const char *dest, char *result){
1019 if (linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_NAT_ADDRESS){
1020 strncpy(result,linphone_core_get_nat_address(lc),LINPHONE_IPADDR_SIZE);
1023 if (linphone_core_get_local_ip_for(lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,dest,result)==0)
1025 /*else fallback to SAL routine that will attempt to find the most realistic interface */
1026 sal_get_default_local_ip(lc->sal,lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,result,LINPHONE_IPADDR_SIZE);
1029 static void update_primary_contact(LinphoneCore *lc){
1031 char tmp[LINPHONE_IPADDR_SIZE];
1033 LinphoneAddress *url;
1034 if (lc->sip_conf.guessed_contact!=NULL){
1035 ms_free(lc->sip_conf.guessed_contact);
1036 lc->sip_conf.guessed_contact=NULL;
1038 url=linphone_address_new(lc->sip_conf.contact);
1040 ms_error("Could not parse identity contact !");
1041 url=linphone_address_new("sip:unknown@unkwownhost");
1043 linphone_core_get_local_ip(lc, NULL, tmp);
1044 if (strcmp(tmp,"127.0.0.1")==0 || strcmp(tmp,"::1")==0 ){
1045 ms_warning("Local loopback network only !");
1046 lc->sip_conf.loopback_only=TRUE;
1047 }else lc->sip_conf.loopback_only=FALSE;
1048 linphone_address_set_domain(url,tmp);
1049 linphone_address_set_port_int(url,lc->sip_conf.sip_port);
1050 guessed=linphone_address_as_string(url);
1051 lc->sip_conf.guessed_contact=guessed;
1052 linphone_address_destroy(url);
1056 * Returns the default identity when no proxy configuration is used.
1060 const char *linphone_core_get_primary_contact(LinphoneCore *lc){
1063 if (lc->sip_conf.guess_hostname){
1064 if (lc->sip_conf.guessed_contact==NULL || lc->sip_conf.loopback_only){
1065 update_primary_contact(lc);
1067 identity=lc->sip_conf.guessed_contact;
1069 identity=lc->sip_conf.contact;
1075 * Tells LinphoneCore to guess local hostname automatically in primary contact.
1079 void linphone_core_set_guess_hostname(LinphoneCore *lc, bool_t val){
1080 lc->sip_conf.guess_hostname=val;
1084 * Returns TRUE if hostname part of primary contact is guessed automatically.
1088 bool_t linphone_core_get_guess_hostname(LinphoneCore *lc){
1089 return lc->sip_conf.guess_hostname;
1093 * Same as linphone_core_get_primary_contact() but the result is a LinphoneAddress object
1094 * instead of const char*
1098 LinphoneAddress *linphone_core_get_primary_contact_parsed(LinphoneCore *lc){
1099 return linphone_address_new(linphone_core_get_primary_contact(lc));
1103 * Sets the list of audio codecs.
1105 * @ingroup media_parameters
1106 * The list is taken by the LinphoneCore thus the application should not free it.
1107 * This list is made of struct PayloadType describing the codec parameters.
1109 int linphone_core_set_audio_codecs(LinphoneCore *lc, MSList *codecs)
1111 if (lc->codecs_conf.audio_codecs!=NULL) ms_list_free(lc->codecs_conf.audio_codecs);
1112 lc->codecs_conf.audio_codecs=codecs;
1117 * Sets the list of video codecs.
1119 * @ingroup media_parameters
1120 * The list is taken by the LinphoneCore thus the application should not free it.
1121 * This list is made of struct PayloadType describing the codec parameters.
1123 int linphone_core_set_video_codecs(LinphoneCore *lc, MSList *codecs)
1125 if (lc->codecs_conf.video_codecs!=NULL) ms_list_free(lc->codecs_conf.video_codecs);
1126 lc->codecs_conf.video_codecs=codecs;
1130 const MSList * linphone_core_get_friend_list(const LinphoneCore *lc)
1136 * Returns the nominal jitter buffer size in milliseconds.
1138 * @ingroup media_parameters
1140 int linphone_core_get_audio_jittcomp(LinphoneCore *lc)
1142 return lc->rtp_conf.audio_jitt_comp;
1146 * Returns the UDP port used for audio streaming.
1148 * @ingroup network_parameters
1150 int linphone_core_get_audio_port(const LinphoneCore *lc)
1152 return lc->rtp_conf.audio_rtp_port;
1156 * Returns the UDP port used for video streaming.
1158 * @ingroup network_parameters
1160 int linphone_core_get_video_port(const LinphoneCore *lc){
1161 return lc->rtp_conf.video_rtp_port;
1166 * Returns the value in seconds of the no-rtp timeout.
1168 * @ingroup media_parameters
1169 * When no RTP or RTCP packets have been received for a while
1170 * LinphoneCore will consider the call is broken (remote end crashed or
1171 * disconnected from the network), and thus will terminate the call.
1172 * The no-rtp timeout is the duration above which the call is considered broken.
1174 int linphone_core_get_nortp_timeout(const LinphoneCore *lc){
1175 return lc->rtp_conf.nortp_timeout;
1179 * Sets the nominal audio jitter buffer size in milliseconds.
1181 * @ingroup media_parameters
1183 void linphone_core_set_audio_jittcomp(LinphoneCore *lc, int value)
1185 lc->rtp_conf.audio_jitt_comp=value;
1189 * Sets the UDP port used for audio streaming.
1191 * @ingroup network_parameters
1193 void linphone_core_set_audio_port(LinphoneCore *lc, int port)
1195 lc->rtp_conf.audio_rtp_port=port;
1199 * Sets the UDP port used for video streaming.
1201 * @ingroup network_parameters
1203 void linphone_core_set_video_port(LinphoneCore *lc, int port){
1204 lc->rtp_conf.video_rtp_port=port;
1208 * Sets the no-rtp timeout value in seconds.
1210 * @ingroup media_parameters
1211 * See linphone_core_get_nortp_timeout() for details.
1213 void linphone_core_set_nortp_timeout(LinphoneCore *lc, int nortp_timeout){
1214 lc->rtp_conf.nortp_timeout=nortp_timeout;
1218 * Indicates whether SIP INFO is used for sending digits.
1220 * @ingroup media_parameters
1222 bool_t linphone_core_get_use_info_for_dtmf(LinphoneCore *lc)
1224 return lc->sip_conf.use_info;
1228 * Sets whether SIP INFO is to be used for sending digits.
1230 * @ingroup media_parameters
1232 void linphone_core_set_use_info_for_dtmf(LinphoneCore *lc,bool_t use_info)
1234 lc->sip_conf.use_info=use_info;
1238 * Indicates whether RFC2833 is used for sending digits.
1240 * @ingroup media_parameters
1242 bool_t linphone_core_get_use_rfc2833_for_dtmf(LinphoneCore *lc)
1244 return lc->sip_conf.use_rfc2833;
1248 * Sets whether RFC2833 is to be used for sending digits.
1250 * @ingroup media_parameters
1252 void linphone_core_set_use_rfc2833_for_dtmf(LinphoneCore *lc,bool_t use_rfc2833)
1254 lc->sip_conf.use_rfc2833=use_rfc2833;
1258 * Returns the UDP port used by SIP.
1260 * @ingroup network_parameters
1262 int linphone_core_get_sip_port(LinphoneCore *lc)
1264 return lc->sip_conf.sip_port;
1267 static char _ua_name[64]="Linphone";
1268 static char _ua_version[64]=LINPHONE_VERSION;
1270 #ifdef HAVE_EXOSIP_GET_VERSION
1271 extern const char *eXosip_get_version();
1274 static void apply_user_agent(LinphoneCore *lc){
1275 char ua_string[256];
1276 snprintf(ua_string,sizeof(ua_string)-1,"%s/%s (eXosip2/%s)",_ua_name,_ua_version,
1277 #ifdef HAVE_EXOSIP_GET_VERSION
1278 eXosip_get_version()
1283 if (lc->sal) sal_set_user_agent(lc->sal,ua_string);
1287 * Sets the user agent string used in SIP messages.
1291 void linphone_core_set_user_agent(const char *name, const char *ver){
1292 strncpy(_ua_name,name,sizeof(_ua_name)-1);
1293 strncpy(_ua_version,ver,sizeof(_ua_version));
1297 * Sets the UDP port to be used by SIP.
1299 * @ingroup network_parameters
1301 void linphone_core_set_sip_port(LinphoneCore *lc,int port)
1303 const char *anyaddr;
1305 if (port==lc->sip_conf.sip_port) return;
1306 lc->sip_conf.sip_port=port;
1308 if (lc->sal==NULL) return;
1310 if (lc->sip_conf.ipv6_enabled)
1314 err=sal_listen_port (lc->sal,anyaddr,port, SalTransportDatagram,FALSE);
1316 char *msg=ortp_strdup_printf("UDP port %i seems already in use ! Cannot initialize.",port);
1318 lc->vtable.display_warning(lc,msg);
1322 apply_user_agent(lc);
1326 * Returns TRUE if IPv6 is enabled.
1328 * @ingroup network_parameters
1329 * See linphone_core_enable_ipv6() for more details on how IPv6 is supported in liblinphone.
1331 bool_t linphone_core_ipv6_enabled(LinphoneCore *lc){
1332 return lc->sip_conf.ipv6_enabled;
1336 * Turns IPv6 support on or off.
1338 * @ingroup network_parameters
1340 * @note IPv6 support is exclusive with IPv4 in liblinphone:
1341 * when IPv6 is turned on, IPv4 calls won't be possible anymore.
1342 * By default IPv6 support is off.
1344 void linphone_core_enable_ipv6(LinphoneCore *lc, bool_t val){
1345 if (lc->sip_conf.ipv6_enabled!=val){
1346 lc->sip_conf.ipv6_enabled=val;
1348 /* we need to restart eXosip */
1349 linphone_core_set_sip_port(lc, lc->sip_conf.sip_port);
1354 static void display_bandwidth(RtpSession *as, RtpSession *vs){
1355 ms_message("bandwidth usage: audio=[d=%.1f,u=%.1f] video=[d=%.1f,u=%.1f] kbit/sec",
1356 (as!=NULL) ? (rtp_session_compute_recv_bandwidth(as)*1e-3) : 0,
1357 (as!=NULL) ? (rtp_session_compute_send_bandwidth(as)*1e-3) : 0,
1358 (vs!=NULL) ? (rtp_session_compute_recv_bandwidth(vs)*1e-3) : 0,
1359 (vs!=NULL) ? (rtp_session_compute_send_bandwidth(vs)*1e-3) : 0);
1362 static void linphone_core_disconnected(LinphoneCore *lc){
1363 lc->vtable.display_warning(lc,_("Remote end seems to have disconnected, the call is going to be closed."));
1364 linphone_core_terminate_call(lc,NULL);
1367 static void monitor_network_state(LinphoneCore *lc, time_t curtime){
1368 static time_t last_check=0;
1369 static bool_t last_status=FALSE;
1370 char result[LINPHONE_IPADDR_SIZE];
1371 bool_t new_status=last_status;
1373 /* only do the network up checking every five seconds */
1374 if (last_check==0 || (curtime-last_check)>=5){
1375 linphone_core_get_local_ip_for(lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,NULL,result);
1376 if (strcmp(result,"::1")!=0 && strcmp(result,"127.0.0.1")!=0){
1378 }else new_status=FALSE;
1380 if (new_status!=last_status) {
1382 ms_message("New local ip address is %s",result);
1384 set_network_reachable(lc,new_status);
1385 last_status=new_status;
1390 static void proxy_update(LinphoneCore *lc){
1391 ms_list_for_each(lc->sip_conf.proxies,(void (*)(void*))&linphone_proxy_config_update);
1394 static void assign_buddy_info(LinphoneCore *lc, BuddyInfo *info){
1395 LinphoneFriend *lf=linphone_core_get_friend_by_address(lc,info->sip_uri);
1398 ms_message("%s has a BuddyInfo assigned with image %p",info->sip_uri, info->image_data);
1399 if (lc->vtable.buddy_info_updated)
1400 lc->vtable.buddy_info_updated(lc,lf);
1402 ms_warning("Could not any friend with uri %s",info->sip_uri);
1406 static void analyze_buddy_lookup_results(LinphoneCore *lc, LinphoneProxyConfig *cfg){
1408 SipSetupContext *ctx=linphone_proxy_config_get_sip_setup_context(cfg);
1409 for (elem=lc->bl_reqs;elem!=NULL;elem=ms_list_next(elem)){
1410 BuddyLookupRequest *req=(BuddyLookupRequest *)elem->data;
1411 if (req->status==BuddyLookupDone || req->status==BuddyLookupFailure){
1412 if (req->results!=NULL){
1413 BuddyInfo *i=(BuddyInfo*)req->results->data;
1414 ms_list_free(req->results);
1416 assign_buddy_info(lc,i);
1418 sip_setup_context_buddy_lookup_free(ctx,req);
1422 /*purge completed requests */
1423 while((elem=ms_list_find(lc->bl_reqs,NULL))!=NULL){
1424 lc->bl_reqs=ms_list_remove_link(lc->bl_reqs,elem);
1428 static void linphone_core_grab_buddy_infos(LinphoneCore *lc, LinphoneProxyConfig *cfg){
1430 SipSetupContext *ctx=linphone_proxy_config_get_sip_setup_context(cfg);
1431 for(elem=linphone_core_get_friend_list(lc);elem!=NULL;elem=elem->next){
1432 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
1433 if (lf->info==NULL){
1434 if (linphone_core_lookup_known_proxy(lc,lf->uri)==cfg){
1435 if (linphone_address_get_username(lf->uri)!=NULL){
1436 BuddyLookupRequest *req;
1437 char *tmp=linphone_address_as_string_uri_only(lf->uri);
1438 req=sip_setup_context_create_buddy_lookup_request(ctx);
1439 buddy_lookup_request_set_key(req,tmp);
1440 buddy_lookup_request_set_max_results(req,1);
1441 sip_setup_context_buddy_lookup_submit(ctx,req);
1442 lc->bl_reqs=ms_list_append(lc->bl_reqs,req);
1450 static void linphone_core_do_plugin_tasks(LinphoneCore *lc){
1451 LinphoneProxyConfig *cfg=NULL;
1452 linphone_core_get_default_proxy(lc,&cfg);
1454 if (lc->bl_refresh){
1455 SipSetupContext *ctx=linphone_proxy_config_get_sip_setup_context(cfg);
1456 if (ctx && (sip_setup_context_get_capabilities(ctx) & SIP_SETUP_CAP_BUDDY_LOOKUP)){
1457 linphone_core_grab_buddy_infos(lc,cfg);
1458 lc->bl_refresh=FALSE;
1461 if (lc->bl_reqs) analyze_buddy_lookup_results(lc,cfg);
1466 * Main loop function. It is crucial that your application call it periodically.
1468 * @ingroup initializing
1469 * linphone_core_iterate() performs various backgrounds tasks:
1470 * - receiving of SIP messages
1471 * - handles timers and timeout
1472 * - performs registration to proxies
1473 * - authentication retries
1474 * The application MUST call this function from periodically, in its main loop.
1475 * Be careful that this function must be call from the same thread as
1476 * other liblinphone methods. In not the case make sure all liblinphone calls are
1477 * serialized with a mutex.
1479 void linphone_core_iterate(LinphoneCore *lc){
1480 int disconnect_timeout = linphone_core_get_nortp_timeout(lc);
1481 time_t curtime=time(NULL);
1483 bool_t one_second_elapsed=FALSE;
1484 bool_t disconnected=FALSE;
1486 if (curtime-lc->prevtime>=1){
1487 lc->prevtime=curtime;
1488 one_second_elapsed=TRUE;
1491 if (lc->preview_finished){
1492 lc->preview_finished=0;
1493 ring_stop(lc->ringstream);
1494 lc->ringstream=NULL;
1495 lc_callback_obj_invoke(&lc->preview_finished_cb,lc);
1498 sal_iterate(lc->sal);
1499 if (lc->auto_net_state_mon) monitor_network_state(lc,curtime);
1502 LinphoneCall *call = linphone_core_get_current_call(lc);
1504 if (call->state==LinphoneCallPreEstablishing && (curtime-call->start_time>=2)){
1505 /*start the call even if the OPTIONS reply did not arrive*/
1506 linphone_core_start_invite(lc,call,NULL);
1508 if (call->dir==LinphoneCallIncoming && call->state==LinphoneCallRinging){
1509 elapsed=curtime-call->start_time;
1510 ms_message("incoming call ringing for %i seconds",elapsed);
1511 if (elapsed>lc->sip_conf.inc_timeout){
1512 call->log->status=LinphoneCallMissed;
1513 linphone_core_terminate_call(lc,NULL);
1515 }else if (call->state==LinphoneCallAVRunning){
1516 if (one_second_elapsed){
1517 RtpSession *as=NULL,*vs=NULL;
1518 lc->prevtime=curtime;
1519 if (lc->audiostream!=NULL)
1520 as=lc->audiostream->session;
1521 if (lc->videostream!=NULL)
1522 vs=lc->videostream->session;
1523 display_bandwidth(as,vs);
1525 #ifdef VIDEO_ENABLED
1526 if (lc->videostream!=NULL)
1527 video_stream_iterate(lc->videostream);
1529 if (lc->audiostream!=NULL && disconnect_timeout>0)
1530 disconnected=!audio_stream_alive(lc->audiostream,disconnect_timeout);
1533 if (linphone_core_video_preview_enabled(lc)){
1534 if (lc->previewstream==NULL)
1535 toggle_video_preview(lc,TRUE);
1536 #ifdef VIDEO_ENABLED
1537 else video_stream_iterate(lc->previewstream);
1540 if (lc->previewstream!=NULL)
1541 toggle_video_preview(lc,FALSE);
1544 linphone_core_disconnected(lc);
1546 linphone_core_do_plugin_tasks(lc);
1548 if (one_second_elapsed && lp_config_needs_commit(lc->config)){
1549 lp_config_sync(lc->config);
1554 * Interpret a call destination as supplied by the user, and returns a fully qualified
1557 * A sip address should look like DisplayName <sip:username@domain:port> .
1558 * Basically this function performs the following tasks
1559 * - if a phone number is entered, prepend country prefix of the default proxy
1560 * configuration, eventually escape the '+' by 00.
1561 * - if no domain part is supplied, append the domain name of the default proxy
1562 * - if no sip: is present, prepend it
1564 * The result is a syntaxically correct SIP address.
1567 LinphoneAddress * linphone_core_interpret_url(LinphoneCore *lc, const char *url){
1568 enum_lookup_res_t *enumres=NULL;
1569 char *enum_domain=NULL;
1570 LinphoneProxyConfig *proxy=lc->default_proxy;;
1572 LinphoneAddress *uri;
1574 if (is_enum(url,&enum_domain)){
1575 lc->vtable.display_status(lc,_("Looking for telephone number destination..."));
1576 if (enum_lookup(enum_domain,&enumres)<0){
1577 lc->vtable.display_status(lc,_("Could not resolve this number."));
1578 if(lc->vtable.failure_recv)
1579 lc->vtable.failure_recv(lc,NULL,400);
1580 ms_free(enum_domain);
1583 ms_free(enum_domain);
1584 tmpurl=enumres->sip_address[0];
1585 uri=linphone_address_new(tmpurl);
1586 enum_lookup_res_free(enumres);
1589 /* check if we have a "sip:" */
1590 if (strstr(url,"sip:")==NULL){
1591 /* this doesn't look like a true sip uri */
1592 if (strchr(url,'@')!=NULL){
1593 /* seems like sip: is missing !*/
1594 tmpurl=ms_strdup_printf("sip:%s",url);
1595 uri=linphone_address_new(tmpurl);
1603 /* append the proxy domain suffix */
1604 const char *identity=linphone_proxy_config_get_identity(proxy);
1605 char normalized_username[128];
1606 uri=linphone_address_new(identity);
1610 linphone_address_set_display_name(uri,NULL);
1611 linphone_proxy_config_normalize_number(proxy,url,normalized_username,
1612 sizeof(normalized_username));
1613 linphone_address_set_username(uri,normalized_username);
1617 uri=linphone_address_new(url);
1621 /* else we could not do anything with url given by user, so display an error */
1622 if (lc->vtable.display_warning!=NULL){
1623 lc->vtable.display_warning(lc,_("Could not parse given sip address. A sip url usually looks like sip:user@domain"));
1629 * Returns the default identity SIP address.
1632 * This is an helper function:
1634 * If no default proxy is set, this will return the primary contact (
1635 * see linphone_core_get_primary_contact() ). If a default proxy is set
1636 * it returns the registered identity on the proxy.
1638 const char * linphone_core_get_identity(LinphoneCore *lc){
1639 LinphoneProxyConfig *proxy=NULL;
1641 linphone_core_get_default_proxy(lc,&proxy);
1643 from=linphone_proxy_config_get_identity(proxy);
1644 }else from=linphone_core_get_primary_contact(lc);
1648 const char * linphone_core_get_route(LinphoneCore *lc){
1649 LinphoneProxyConfig *proxy=NULL;
1650 const char *route=NULL;
1651 linphone_core_get_default_proxy(lc,&proxy);
1653 route=linphone_proxy_config_get_route(proxy);
1658 bool_t linphone_core_is_in_communication_with(LinphoneCore *lc, const char *to)
1662 const LinphoneAddress *la=linphone_core_get_current_call_remote_address(lc);
1667 tmp = linphone_address_as_string(la);
1677 LinphoneProxyConfig * linphone_core_lookup_known_proxy(LinphoneCore *lc, const LinphoneAddress *uri){
1679 LinphoneProxyConfig *found_cfg=NULL;
1680 for (elem=linphone_core_get_proxy_config_list(lc);elem!=NULL;elem=elem->next){
1681 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
1682 const char *domain=linphone_proxy_config_get_domain(cfg);
1683 if (domain!=NULL && strcmp(domain,linphone_address_get_domain(uri))==0){
1691 static char *get_fixed_contact(LinphoneCore *lc, LinphoneCall *call , LinphoneProxyConfig *dest_proxy){
1692 LinphoneAddress *ctt;
1693 const char *localip=call->localip;
1695 /* first use user's supplied ip address if asked*/
1696 if (linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_NAT_ADDRESS){
1697 ctt=linphone_core_get_primary_contact_parsed(lc);
1698 return ms_strdup_printf("sip:%s@%s",linphone_address_get_username(ctt),
1699 linphone_core_get_nat_address(lc));
1702 /* if already choosed, don't change it */
1703 if (call->op && sal_op_get_contact(call->op)!=NULL){
1706 /* if the ping OPTIONS request succeeded use the contact guessed from the
1709 const char *guessed=sal_op_get_contact(call->ping_op);
1711 ms_message("Contact has been fixed using OPTIONS to %s",guessed);
1712 return ms_strdup(guessed);
1716 /*if using a proxy, use the contact address as guessed with the REGISTERs*/
1717 if (dest_proxy && dest_proxy->op){
1718 const char *fixed_contact=sal_op_get_contact(dest_proxy->op);
1719 if (fixed_contact) {
1720 ms_message("Contact has been fixed using proxy to %s",fixed_contact);
1721 return ms_strdup(fixed_contact);
1725 ctt=linphone_core_get_primary_contact_parsed(lc);
1729 /*otherwise use supllied localip*/
1730 linphone_address_set_domain(ctt,localip);
1731 linphone_address_set_port_int(ctt,linphone_core_get_sip_port(lc));
1732 ret=linphone_address_as_string_uri_only(ctt);
1733 linphone_address_destroy(ctt);
1734 ms_message("Contact has been fixed using local ip to %s",ret);
1740 int linphone_core_start_invite(LinphoneCore *lc, LinphoneCall *call, LinphoneProxyConfig *dest_proxy){
1743 char *real_url,*barmsg;
1745 /*try to be best-effort in giving real local or routable contact address */
1746 contact=get_fixed_contact(lc,call,dest_proxy);
1748 sal_op_set_contact(call->op, contact);
1751 call->state=LinphoneCallInit;
1752 linphone_core_init_media_streams(lc,call);
1753 if (!lc->sip_conf.sdp_200_ack){
1754 call->media_pending=TRUE;
1755 sal_call_set_local_media_description(call->op,call->localdesc);
1757 real_url=linphone_address_as_string(call->log->to);
1758 from=linphone_address_as_string(call->log->from);
1759 err=sal_call(call->op,from,real_url);
1761 if (lc->sip_conf.sdp_200_ack){
1762 call->media_pending=TRUE;
1763 sal_call_set_local_media_description(call->op,call->localdesc);
1765 barmsg=ortp_strdup_printf("%s %s", _("Contacting"), real_url);
1766 lc->vtable.display_status(lc,barmsg);
1770 ms_warning("Could not initiate call.");
1771 lc->vtable.display_status(lc,_("could not call"));
1772 if(call == linphone_core_get_current_call(lc))
1773 linphone_core_stop_media_streams(lc,call);
1774 linphone_call_unref(call);
1775 }else gstate_new_state(lc, GSTATE_CALL_OUT_INVITE, real_url);
1782 * Initiates an outgoing call
1784 * @ingroup call_control
1785 * @param lc the LinphoneCore object
1786 * @param url the destination of the call (sip address, or phone number).
1788 LinphoneCall * linphone_core_invite(LinphoneCore *lc, const char *url){
1789 LinphoneAddress *addr=linphone_core_interpret_url(lc,url);
1792 call=linphone_core_invite_address(lc,addr);
1793 linphone_address_destroy(addr);
1800 * Initiates an outgoing call given a destination LinphoneAddress
1802 * @ingroup call_control
1803 * @param lc the LinphoneCore object
1804 * @param url the destination of the call (sip address).
1806 LinphoneCall * linphone_core_invite_address(LinphoneCore *lc, const LinphoneAddress *real_parsed_url)
1809 const char *route=NULL;
1810 const char *from=NULL;
1811 LinphoneProxyConfig *proxy=NULL;
1812 LinphoneAddress *parsed_url2=NULL;
1813 char *real_url=NULL;
1814 LinphoneProxyConfig *dest_proxy=NULL;
1817 if (linphone_core_in_call(lc)){
1818 lc->vtable.display_warning(lc,_("Sorry, you have to pause or stop the current call first !"));
1821 if(!linphone_core_can_we_add_call(lc)){
1822 lc->vtable.display_warning(lc,_("Sorry, we have reached the maximum number of simultaneous calls"));
1825 linphone_core_get_default_proxy(lc,&proxy);
1826 route=linphone_core_get_route(lc);
1828 real_url=linphone_address_as_string(real_parsed_url);
1829 dest_proxy=linphone_core_lookup_known_proxy(lc,real_parsed_url);
1831 if (proxy!=dest_proxy && dest_proxy!=NULL) {
1832 ms_message("Overriding default proxy setting for this call:");
1833 ms_message("The used identity will be %s",linphone_proxy_config_get_identity(dest_proxy));
1836 if (dest_proxy!=NULL)
1837 from=linphone_proxy_config_get_identity(dest_proxy);
1838 else if (proxy!=NULL)
1839 from=linphone_proxy_config_get_identity(proxy);
1841 /* if no proxy or no identity defined for this proxy, default to primary contact*/
1842 if (from==NULL) from=linphone_core_get_primary_contact(lc);
1844 parsed_url2=linphone_address_new(from);
1846 call=linphone_call_new_outgoing(lc,parsed_url2,linphone_address_clone(real_parsed_url));
1847 sal_op_set_route(call->op,route);
1849 if(linphone_core_add_call(lc,call)!= 0)
1851 ms_warning("we had a problem in adding the call into the invite ... weird\n");
1852 linphone_call_unref(call);
1855 linphone_core_set_as_current_call(lc,call);
1856 if (dest_proxy!=NULL || lc->sip_conf.ping_with_options==FALSE){
1857 err=linphone_core_start_invite(lc,call,dest_proxy);
1859 /*defer the start of the call after the OPTIONS ping*/
1860 call->state=LinphoneCallPreEstablishing;
1861 call->ping_op=sal_op_new(lc->sal);
1862 sal_ping(call->ping_op,from,real_url);
1863 sal_op_set_user_pointer(call->ping_op,call);
1864 call->start_time=time(NULL);
1867 if (real_url!=NULL) ms_free(real_url);
1871 int linphone_core_refer(LinphoneCore *lc, LinphoneCall *call, const char *url)
1873 char *real_url=NULL;
1874 LinphoneAddress *real_parsed_url=linphone_core_interpret_url(lc,url);
1876 if (!real_parsed_url){
1881 ms_warning("No established call to refer.");
1884 //lc->call=NULL; //Do not do that you will lose the call afterward . . .
1885 real_url=linphone_address_as_string (real_parsed_url);
1886 sal_refer(call->op,real_url);
1892 * Returns true if an incoming call is pending, ie waiting for being answered or declined.
1894 * @ingroup call_control
1896 bool_t linphone_core_inc_invite_pending(LinphoneCore*lc){
1897 LinphoneCall *call = linphone_core_get_current_call(lc);
1900 if(call->dir==LinphoneCallIncoming)
1906 void linphone_core_init_media_streams(LinphoneCore *lc, LinphoneCall *call){
1907 SalMediaDescription *md=call->localdesc;
1908 lc->audiostream=audio_stream_new(md->streams[0].port,linphone_core_ipv6_enabled(lc));
1909 if (linphone_core_echo_limiter_enabled(lc)){
1910 const char *type=lp_config_get_string(lc->config,"sound","el_type","mic");
1911 if (strcasecmp(type,"mic")==0)
1912 audio_stream_enable_echo_limiter(lc->audiostream,ELControlMic);
1913 else if (strcasecmp(type,"speaker")==0)
1914 audio_stream_enable_echo_limiter(lc->audiostream,ELControlSpeaker);
1916 audio_stream_enable_gain_control(lc->audiostream,TRUE);
1917 if (linphone_core_echo_cancellation_enabled(lc)){
1918 int len,delay,framesize;
1919 len=lp_config_get_int(lc->config,"sound","ec_tail_len",0);
1920 delay=lp_config_get_int(lc->config,"sound","ec_delay",0);
1921 framesize=lp_config_get_int(lc->config,"sound","ec_framesize",0);
1922 audio_stream_set_echo_canceller_params(lc->audiostream,len,delay,framesize);
1924 audio_stream_enable_automatic_gain_control(lc->audiostream,linphone_core_agc_enabled(lc));
1926 int enabled=lp_config_get_int(lc->config,"sound","noisegate",0);
1927 audio_stream_enable_noise_gate(lc->audiostream,enabled);
1930 rtp_session_set_transports(lc->audiostream->session,lc->a_rtp,lc->a_rtcp);
1932 #ifdef VIDEO_ENABLED
1933 if ((lc->video_conf.display || lc->video_conf.capture) && md->streams[1].port>0)
1934 lc->videostream=video_stream_new(md->streams[1].port,linphone_core_ipv6_enabled(lc));
1936 lc->videostream=NULL;
1940 static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'};
1942 static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
1943 LinphoneCore* lc = (LinphoneCore*)user_data;
1944 if (dtmf<0 || dtmf>15){
1945 ms_warning("Bad dtmf value %i",dtmf);
1948 if (lc->vtable.dtmf_received != NULL)
1949 lc->vtable.dtmf_received(lc, dtmf_tab[dtmf]);
1952 static void parametrize_equalizer(LinphoneCore *lc, AudioStream *st){
1954 MSFilter *f=st->equalizer;
1955 int enabled=lp_config_get_int(lc->config,"sound","eq_active",0);
1956 const char *gains=lp_config_get_string(lc->config,"sound","eq_gains",NULL);
1957 ms_filter_call_method(f,MS_EQUALIZER_SET_ACTIVE,&enabled);
1963 if (sscanf(gains,"%f:%f:%f %n",&g.frequency,&g.gain,&g.width,&bytes)==3){
1964 ms_message("Read equalizer gains: %f(~%f) --> %f",g.frequency,g.width,g.gain);
1965 ms_filter_call_method(f,MS_EQUALIZER_SET_GAIN,&g);
1974 static void post_configure_audio_streams(LinphoneCore *lc){
1975 AudioStream *st=lc->audiostream;
1976 float gain=lp_config_get_float(lc->config,"sound","mic_gain",-1);
1978 audio_stream_set_mic_gain(st,gain);
1979 float recv_gain = lc->sound_conf.soft_play_lev;
1980 if (recv_gain != 0) {
1981 linphone_core_set_soft_play_level(lc,recv_gain);
1983 if (linphone_core_echo_limiter_enabled(lc)){
1984 float speed=lp_config_get_float(lc->config,"sound","el_speed",-1);
1985 float thres=lp_config_get_float(lc->config,"sound","el_thres",-1);
1986 float force=lp_config_get_float(lc->config,"sound","el_force",-1);
1987 int sustain=lp_config_get_int(lc->config,"sound","el_sustain",-1);
1989 if (st->el_type==ELControlMic){
1991 if (speed==-1) speed=0.03;
1992 if (force==-1) force=10;
1994 else if (st->el_type==ELControlSpeaker){
1996 if (speed==-1) speed=0.02;
1997 if (force==-1) force=5;
2000 ms_filter_call_method(f,MS_VOLUME_SET_EA_SPEED,&speed);
2002 ms_filter_call_method(f,MS_VOLUME_SET_EA_THRESHOLD,&thres);
2004 ms_filter_call_method(f,MS_VOLUME_SET_EA_FORCE,&force);
2006 ms_filter_call_method(f,MS_VOLUME_SET_EA_SUSTAIN,&sustain);
2010 float ng_thres=lp_config_get_float(lc->config,"sound","ng_thres",0.05);
2011 float ng_floorgain=lp_config_get_float(lc->config,"sound","ng_floorgain",0);
2012 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
2013 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&ng_floorgain);
2015 parametrize_equalizer(lc,st);
2016 if (lc->vtable.dtmf_received!=NULL){
2017 /* replace by our default action*/
2018 audio_stream_play_received_dtmfs(lc->audiostream,FALSE);
2019 rtp_session_signal_connect(lc->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
2023 static RtpProfile *make_profile(LinphoneCore *lc, const SalMediaDescription *md, const SalStreamDescription *desc, int *used_pt){
2026 RtpProfile *prof=rtp_profile_new("Call profile");
2030 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
2031 PayloadType *pt=(PayloadType*)elem->data;
2034 if (desc->type==SalAudio){
2035 linphone_core_update_allocated_audio_bandwidth_in_call(lc,pt);
2037 *used_pt=payload_type_get_number(pt);
2040 if (desc->bandwidth>0) remote_bw=desc->bandwidth;
2041 else if (md->bandwidth>0) {
2042 /*case where b=AS is given globally, not per stream*/
2043 remote_bw=md->bandwidth;
2044 if (desc->type==SalVideo){
2045 remote_bw-=lc->audio_bw;
2049 if (desc->type==SalAudio){
2050 bw=get_min_bandwidth(lc->up_audio_bw,remote_bw);
2051 }else bw=get_min_bandwidth(lc->up_video_bw,remote_bw);
2052 if (bw>0) pt->normal_bitrate=bw*1000;
2053 else if (desc->type==SalAudio){
2054 pt->normal_bitrate=-1;
2058 snprintf(tmp,sizeof(tmp),"ptime=%i",desc->ptime);
2059 payload_type_append_send_fmtp(pt,tmp);
2061 rtp_profile_set_payload(prof,payload_type_get_number(pt),pt);
2066 void linphone_core_start_media_streams(LinphoneCore *lc, LinphoneCall *call){
2067 LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
2068 const char *tool="linphone-" LINPHONE_VERSION;
2071 /* adjust rtp jitter compensation. It must be at least the latency of the sound card */
2072 int jitt_comp=MAX(lc->sound_conf.latency,lc->rtp_conf.audio_jitt_comp);
2074 if (call->media_start_time==0) call->media_start_time=time(NULL);
2076 cname=linphone_address_as_string_uri_only(me);
2078 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
2079 SalProtoRtpAvp,SalAudio);
2081 call->audio_profile=make_profile(lc,call->resultdesc,stream,&used_pt);
2082 if (!lc->use_files){
2083 MSSndCard *playcard=lc->sound_conf.play_sndcard;
2084 MSSndCard *captcard=lc->sound_conf.capt_sndcard;
2085 if (playcard==NULL) {
2086 ms_warning("No card defined for playback !");
2089 if (captcard==NULL) {
2090 ms_warning("No card defined for capture !");
2093 audio_stream_start_now(
2095 call->audio_profile,
2096 stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
2103 linphone_core_echo_cancellation_enabled(lc));
2105 audio_stream_start_with_files(
2107 call->audio_profile,
2108 stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
2116 post_configure_audio_streams(lc);
2117 audio_stream_set_rtcp_information(lc->audiostream, cname, tool);
2118 }else ms_warning("No audio stream defined ?");
2120 #ifdef VIDEO_ENABLED
2122 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
2123 SalProtoRtpAvp,SalVideo);
2124 /* shutdown preview */
2125 if (lc->previewstream!=NULL) {
2126 video_preview_stop(lc->previewstream);
2127 lc->previewstream=NULL;
2129 if (stream && (lc->video_conf.display || lc->video_conf.capture)) {
2130 const char *addr=stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr;
2131 call->video_profile=make_profile(lc,call->resultdesc,stream,&used_pt);
2132 video_stream_set_sent_video_size(lc->videostream,linphone_core_get_preferred_video_size(lc));
2133 video_stream_enable_self_view(lc->videostream,lc->video_conf.selfview);
2134 if (lc->video_conf.display && lc->video_conf.capture)
2135 video_stream_start(lc->videostream,
2136 call->video_profile, addr, stream->port,
2138 used_pt, jitt_comp, lc->video_conf.device);
2139 else if (lc->video_conf.display)
2140 video_stream_recv_only_start(lc->videostream,
2141 call->video_profile, addr, stream->port,
2142 used_pt, jitt_comp);
2143 else if (lc->video_conf.capture)
2144 video_stream_send_only_start(lc->videostream,
2145 call->video_profile, addr, stream->port,
2147 used_pt, jitt_comp, lc->video_conf.device);
2148 video_stream_set_rtcp_information(lc->videostream, cname,tool);
2155 linphone_address_destroy(me);
2156 call->state=LinphoneCallAVRunning;
2159 void linphone_core_stop_media_streams(LinphoneCore *lc, LinphoneCall *call){
2160 if (lc->audiostream!=NULL) {
2161 audio_stream_stop(lc->audiostream);
2162 lc->audiostream=NULL;
2164 #ifdef VIDEO_ENABLED
2165 if (lc->videostream!=NULL){
2166 if (lc->video_conf.display && lc->video_conf.capture)
2167 video_stream_stop(lc->videostream);
2168 else if (lc->video_conf.display)
2169 video_stream_recv_only_stop(lc->videostream);
2170 else if (lc->video_conf.capture)
2171 video_stream_send_only_stop(lc->videostream);
2172 lc->videostream=NULL;
2174 if (linphone_core_video_preview_enabled(lc)){
2175 if (lc->previewstream==NULL){
2176 lc->previewstream=video_preview_start(lc->video_conf.device, lc->video_conf.vsize);
2180 if (call->audio_profile){
2181 rtp_profile_clear_all(call->audio_profile);
2182 rtp_profile_destroy(call->audio_profile);
2183 call->audio_profile=NULL;
2185 if (call->video_profile){
2186 rtp_profile_clear_all(call->video_profile);
2187 rtp_profile_destroy(call->video_profile);
2188 call->video_profile=NULL;
2193 * Accept an incoming call.
2195 * @ingroup call_control
2196 * Basically the application is notified of incoming calls within the
2197 * invite_recv callback of the #LinphoneCoreVTable structure.
2198 * The application can later respond positively to the call using
2200 * @param lc the LinphoneCore object
2201 * @param url the SIP address of the originator of the call, or NULL.
2202 * This argument is useful for managing multiple calls simulatenously,
2203 * however this feature is not supported yet.
2204 * Using NULL will accept the unique incoming call in progress.
2206 int linphone_core_accept_call(LinphoneCore *lc, LinphoneCall *call)
2208 LinphoneProxyConfig *cfg=NULL;
2209 const char *contact=NULL;
2212 //if just one call is present answer the only one ...
2213 if(ms_list_size(linphone_core_get_calls(lc)) != 1)
2216 call = linphone_core_get_calls(lc)->data;
2219 if (call->state==LinphoneCallAVRunning){
2220 /*call already accepted*/
2225 if (lc->ringstream!=NULL) {
2226 ms_message("stop ringing");
2227 ring_stop(lc->ringstream);
2228 ms_message("ring stopped");
2229 lc->ringstream=NULL;
2231 if(linphone_core_set_as_current_call(lc,call)!=0)
2233 ms_message("another call is already in process\n");
2236 linphone_core_get_default_proxy(lc,&cfg);
2237 /*try to be best-effort in giving real local or routable contact address*/
2238 contact=get_fixed_contact(lc,call,cfg);
2240 sal_op_set_contact(call->op,contact);
2242 sal_call_accept(call->op);
2243 lc->vtable.display_status(lc,_("Connected."));
2244 gstate_new_state(lc, GSTATE_CALL_IN_CONNECTED, NULL);
2245 call->resultdesc=sal_call_get_final_media_description(call->op);
2246 if (call->resultdesc){
2247 sal_media_description_ref(call->resultdesc);
2248 if(call == linphone_core_get_current_call(lc))
2249 linphone_core_start_media_streams(lc, call);
2250 }else call->media_pending=TRUE;
2251 ms_message("call answered.");
2256 * Terminates a call.
2258 * @ingroup call_control
2259 * @param lc The LinphoneCore
2260 * @param url the destination of the call to be terminated, use NULL if there is
2261 * only one call (which is case in this version of liblinphone).
2263 int linphone_core_terminate_call(LinphoneCore *lc, LinphoneCall *the_call)
2266 if (the_call == NULL){
2267 call = linphone_core_get_current_call(lc);
2277 sal_call_terminate(call->op);
2280 if (lc->ringstream!=NULL) {
2281 ring_stop(lc->ringstream);
2282 lc->ringstream=NULL;
2284 if(call == linphone_core_get_current_call(lc))
2285 linphone_core_stop_media_streams(lc,call);
2286 lc->vtable.display_status(lc,_("Call ended") );
2287 gstate_new_state(lc, GSTATE_CALL_END, NULL);
2288 linphone_call_set_terminated(call);
2289 linphone_call_unref(call);
2294 * Terminates all the calls.
2296 * @ingroup call_control
2297 * @param lc The LinphoneCore
2299 int linphone_core_terminate_all_calls(LinphoneCore *lc){
2303 while(calls->next != NULL)
2305 linphone_core_terminate_call(lc,(LinphoneCall *)calls->data);
2306 calls = calls->next;
2308 ms_list_free(lc->calls);
2313 * Returns the calls MSList
2315 * @ingroup call_control
2317 MSList *linphone_core_get_calls(LinphoneCore *lc)
2319 return ms_list_copy(lc->calls);
2323 * Returns TRUE if there is a call running or pending.
2325 * @ingroup call_control
2327 bool_t linphone_core_in_call(const LinphoneCore *lc){
2328 return linphone_core_get_current_call((LinphoneCore *)lc)!=NULL;
2332 * Returns The _LinphoneCall struct of the current call if one is in call
2334 * @ingroup call_control
2336 LinphoneCall *linphone_core_get_current_call(LinphoneCore *lc)
2338 if(lc->current_call != NULL)
2339 return lc->current_call;
2344 * Permits to pause the call
2346 * @ingroup call_control
2348 int linphone_core_pause_call(LinphoneCore *lc, LinphoneCall *the_call)
2350 LinphoneCall *call = the_call;
2353 ms_error("LinphoneCore was null\n");
2358 if(linphone_core_in_call(lc))
2360 call = linphone_core_get_current_call(lc);
2364 ms_error("LinphoneCall was null\n");
2368 if(linphone_core_get_current_call(lc) != call)
2370 ms_error("The call asked to be paused was not the current on\n");
2373 sal_call_hold(call->op,TRUE);
2374 call->state = LinphoneCallPaused;
2375 linphone_core_unset_the_current_call(lc);
2376 linphone_core_stop_media_streams(lc,call);
2377 lc->vtable.display_status(lc,_("Pause the current call"));
2382 * Permits to resume the call
2384 * @ingroup call_control
2386 int linphone_core_resume_call(LinphoneCore *lc, LinphoneCall *the_call)
2389 LinphoneCall *call = the_call;
2392 ms_error("LinphoneCore was null\n");
2397 MSList *calls = linphone_core_get_calls(lc);
2398 if(ms_list_size(calls) == 1)
2400 call = ((LinphoneCall *)calls->data);
2401 ms_list_free(calls);
2405 ms_error("LinphoneCall was null\n");
2406 ms_list_free(calls);
2410 if(linphone_core_get_current_call(lc) != NULL)
2412 ms_error("There is already a call in process pause or stop it first\n");
2415 linphone_core_init_media_streams(lc,call);
2416 sal_call_hold(call->op,FALSE);
2417 call->state = LinphoneCallAVRunning;
2418 linphone_core_set_as_current_call(lc,call);
2419 linphone_core_start_media_streams(lc,call);
2420 snprintf(temp,sizeof(temp),"Resume the call with %s",linphone_call_get_remote_address_as_string(call));
2421 lc->vtable.display_status(lc,temp);
2427 * Compare the remote address with the one in call
2430 * @param b the remote address to compare with
2431 * @return 0 if it's the good call else 1
2433 static int linphone_call_remote_address_compare(const void * a, const void * b)
2435 if(b == NULL || a ==NULL)
2437 char *the_remote_address = ((char *)b);
2438 LinphoneCall *call = ((LinphoneCall *)a);
2440 ms_message("the remote address:%s\n",the_remote_address);
2441 ms_message("the call:%p => %s\n",call,linphone_call_get_remote_address_as_string(call));
2443 if(!strcmp(linphone_call_get_remote_address_as_string(call),the_remote_address))
2451 * Get the call with the remote_address specified
2453 * @param remote_address
2454 * @return the LinphoneCall of the call if found
2456 LinphoneCall *linphone_core_get_call_by_remote_address(LinphoneCore *lc, const char *remote_address){
2458 MSList *the_call = ms_list_find_custom(lc->calls,linphone_call_remote_address_compare,(void *)remote_address);
2459 if(the_call != NULL)
2461 return ((LinphoneCall *)the_call->data);
2466 int linphone_core_send_publish(LinphoneCore *lc,
2467 LinphoneOnlineStatus presence_mode)
2470 for (elem=linphone_core_get_proxy_config_list(lc);elem!=NULL;elem=ms_list_next(elem)){
2471 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
2472 if (cfg->publish) linphone_proxy_config_send_publish(cfg,presence_mode);
2478 * Set the incoming call timeout in seconds.
2480 * @ingroup call_control
2481 * If an incoming call isn't answered for this timeout period, it is
2482 * automatically declined.
2484 void linphone_core_set_inc_timeout(LinphoneCore *lc, int seconds){
2485 lc->sip_conf.inc_timeout=seconds;
2489 * Returns the incoming call timeout
2491 * @ingroup call_control
2492 * See linphone_core_set_inc_timeout() for details.
2494 int linphone_core_get_inc_timeout(LinphoneCore *lc){
2495 return lc->sip_conf.inc_timeout;
2498 void linphone_core_set_presence_info(LinphoneCore *lc,int minutes_away,
2499 const char *contact,
2500 LinphoneOnlineStatus presence_mode)
2502 if (minutes_away>0) lc->minutes_away=minutes_away;
2504 if (lc->alt_contact!=NULL) {
2505 ms_free(lc->alt_contact);
2506 lc->alt_contact=NULL;
2508 if (contact) lc->alt_contact=ms_strdup(contact);
2509 if (lc->presence_mode!=presence_mode){
2510 linphone_core_notify_all_friends(lc,presence_mode);
2512 Improve the use of all LINPHONE_STATUS available.
2513 !TODO Do not mix "presence status" with "answer status code"..
2514 Use correct parameter to follow sip_if_match/sip_etag.
2516 linphone_core_send_publish(lc,presence_mode);
2518 lc->prev_mode=lc->presence_mode;
2519 lc->presence_mode=presence_mode;
2522 LinphoneOnlineStatus linphone_core_get_presence_info(const LinphoneCore *lc){
2523 return lc->presence_mode;
2527 * Get playback sound level in 0-100 scale.
2529 * @ingroup media_parameters
2531 int linphone_core_get_play_level(LinphoneCore *lc)
2533 return lc->sound_conf.play_lev;
2537 * Get ring sound level in 0-100 scale
2539 * @ingroup media_parameters
2541 int linphone_core_get_ring_level(LinphoneCore *lc)
2543 return lc->sound_conf.ring_lev;
2547 * Get sound capture level in 0-100 scale
2549 * @ingroup media_parameters
2551 int linphone_core_get_rec_level(LinphoneCore *lc){
2552 return lc->sound_conf.rec_lev;
2556 * Set sound ring level in 0-100 scale
2558 * @ingroup media_parameters
2560 void linphone_core_set_ring_level(LinphoneCore *lc, int level){
2562 lc->sound_conf.ring_lev=level;
2563 sndcard=lc->sound_conf.ring_sndcard;
2564 if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_PLAYBACK,level);
2568 void linphone_core_set_soft_play_level(LinphoneCore *lc, float level){
2570 lc->sound_conf.soft_play_lev=level;
2571 AudioStream *st=lc->audiostream;
2572 if (!st) return; /*just return*/
2575 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_DB_GAIN,&gain);
2576 }else ms_warning("Could not apply gain: gain control wasn't activated.");
2578 float linphone_core_get_soft_play_level(LinphoneCore *lc) {
2580 AudioStream *st=lc->audiostream;
2582 ms_filter_call_method(st->volrecv,MS_VOLUME_GET,&gain);
2583 }else ms_warning("Could not get gain: gain control wasn't activated.");
2589 * Set sound playback level in 0-100 scale
2591 * @ingroup media_parameters
2593 void linphone_core_set_play_level(LinphoneCore *lc, int level){
2595 lc->sound_conf.play_lev=level;
2596 sndcard=lc->sound_conf.play_sndcard;
2597 if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_PLAYBACK,level);
2601 * Set sound capture level in 0-100 scale
2603 * @ingroup media_parameters
2605 void linphone_core_set_rec_level(LinphoneCore *lc, int level)
2608 lc->sound_conf.rec_lev=level;
2609 sndcard=lc->sound_conf.capt_sndcard;
2610 if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_CAPTURE,level);
2613 static MSSndCard *get_card_from_string_id(const char *devid, unsigned int cap){
2614 MSSndCard *sndcard=NULL;
2616 sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
2617 if (sndcard!=NULL &&
2618 (ms_snd_card_get_capabilities(sndcard) & cap)==0 ){
2619 ms_warning("%s card does not have the %s capability, ignoring.",
2621 cap==MS_SND_CARD_CAP_CAPTURE ? "capture" : "playback");
2625 if (sndcard==NULL) {
2626 /* get a card that has read+write capabilities */
2627 sndcard=ms_snd_card_manager_get_default_card(ms_snd_card_manager_get());
2628 /* otherwise refine to the first card having the right capability*/
2630 const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
2631 for(;elem!=NULL;elem=elem->next){
2632 sndcard=(MSSndCard*)elem->data;
2633 if (ms_snd_card_get_capabilities(sndcard) & cap) break;
2636 if (sndcard==NULL){/*looks like a bug! take the first one !*/
2637 const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
2638 if (elem) sndcard=(MSSndCard*)elem->data;
2641 if (sndcard==NULL) ms_error("Could not find a suitable soundcard !");
2646 * Returns true if the specified sound device can capture sound.
2648 * @ingroup media_parameters
2649 * @param devid the device name as returned by linphone_core_get_sound_devices()
2651 bool_t linphone_core_sound_device_can_capture(LinphoneCore *lc, const char *devid){
2653 sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
2654 if (sndcard!=NULL && (ms_snd_card_get_capabilities(sndcard) & MS_SND_CARD_CAP_CAPTURE)) return TRUE;
2659 * Returns true if the specified sound device can play sound.
2661 * @ingroup media_parameters
2662 * @param devid the device name as returned by linphone_core_get_sound_devices()
2664 bool_t linphone_core_sound_device_can_playback(LinphoneCore *lc, const char *devid){
2666 sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
2667 if (sndcard!=NULL && (ms_snd_card_get_capabilities(sndcard) & MS_SND_CARD_CAP_PLAYBACK)) return TRUE;
2672 * Sets the sound device used for ringing.
2674 * @ingroup media_parameters
2675 * @param devid the device name as returned by linphone_core_get_sound_devices()
2677 int linphone_core_set_ringer_device(LinphoneCore *lc, const char * devid){
2678 MSSndCard *card=get_card_from_string_id(devid,MS_SND_CARD_CAP_PLAYBACK);
2679 lc->sound_conf.ring_sndcard=card;
2680 if (card && lc->ready)
2681 lp_config_set_string(lc->config,"sound","ringer_dev_id",ms_snd_card_get_string_id(card));
2686 * Sets the sound device used for playback.
2688 * @ingroup media_parameters
2689 * @param devid the device name as returned by linphone_core_get_sound_devices()
2691 int linphone_core_set_playback_device(LinphoneCore *lc, const char * devid){
2692 MSSndCard *card=get_card_from_string_id(devid,MS_SND_CARD_CAP_PLAYBACK);
2693 lc->sound_conf.play_sndcard=card;
2694 if (card && lc->ready)
2695 lp_config_set_string(lc->config,"sound","playback_dev_id",ms_snd_card_get_string_id(card));
2700 * Sets the sound device used for capture.
2702 * @ingroup media_parameters
2703 * @param devid the device name as returned by linphone_core_get_sound_devices()
2705 int linphone_core_set_capture_device(LinphoneCore *lc, const char * devid){
2706 MSSndCard *card=get_card_from_string_id(devid,MS_SND_CARD_CAP_CAPTURE);
2707 lc->sound_conf.capt_sndcard=card;
2708 if (card && lc->ready)
2709 lp_config_set_string(lc->config,"sound","capture_dev_id",ms_snd_card_get_string_id(card));
2714 * Returns the name of the currently assigned sound device for ringing.
2716 * @ingroup media_parameters
2718 const char * linphone_core_get_ringer_device(LinphoneCore *lc)
2720 if (lc->sound_conf.ring_sndcard) return ms_snd_card_get_string_id(lc->sound_conf.ring_sndcard);
2725 * Returns the name of the currently assigned sound device for playback.
2727 * @ingroup media_parameters
2729 const char * linphone_core_get_playback_device(LinphoneCore *lc)
2731 return lc->sound_conf.play_sndcard ? ms_snd_card_get_string_id(lc->sound_conf.play_sndcard) : NULL;
2735 * Returns the name of the currently assigned sound device for capture.
2737 * @ingroup media_parameters
2739 const char * linphone_core_get_capture_device(LinphoneCore *lc)
2741 return lc->sound_conf.capt_sndcard ? ms_snd_card_get_string_id(lc->sound_conf.capt_sndcard) : NULL;
2745 * Returns an unmodifiable array of available sound devices.
2747 * @ingroup media_parameters
2748 * The array is NULL terminated.
2750 const char** linphone_core_get_sound_devices(LinphoneCore *lc){
2751 build_sound_devices_table(lc);
2752 return lc->sound_conf.cards;
2756 * Returns an unmodifiable array of available video capture devices.
2758 * @ingroup media_parameters
2759 * The array is NULL terminated.
2761 const char** linphone_core_get_video_devices(const LinphoneCore *lc){
2762 return lc->video_conf.cams;
2765 char linphone_core_get_sound_source(LinphoneCore *lc)
2767 return lc->sound_conf.source;
2770 void linphone_core_set_sound_source(LinphoneCore *lc, char source)
2772 MSSndCard *sndcard=lc->sound_conf.capt_sndcard;
2773 lc->sound_conf.source=source;
2774 if (!sndcard) return;
2777 ms_snd_card_set_capture(sndcard,MS_SND_CARD_MIC);
2780 ms_snd_card_set_capture(sndcard,MS_SND_CARD_LINE);
2788 * Sets the path to a wav file used for ringing.
2790 * The file must be a wav 16bit linear.
2792 * @ingroup media_parameters
2794 void linphone_core_set_ring(LinphoneCore *lc,const char *path){
2795 if (lc->sound_conf.local_ring!=0){
2796 ms_free(lc->sound_conf.local_ring);
2798 lc->sound_conf.local_ring=ms_strdup(path);
2799 if (lc->ready && lc->sound_conf.local_ring)
2800 lp_config_set_string(lc->config,"sound","local_ring",lc->sound_conf.local_ring);
2804 * Returns the path to the wav file used for ringing.
2806 * @ingroup media_parameters
2808 const char *linphone_core_get_ring(const LinphoneCore *lc){
2809 return lc->sound_conf.local_ring;
2812 static void notify_end_of_ring(void *ud ,unsigned int event, void * arg){
2813 LinphoneCore *lc=(LinphoneCore*)ud;
2814 lc->preview_finished=1;
2817 int linphone_core_preview_ring(LinphoneCore *lc, const char *ring,LinphoneCoreCbFunc func,void * userdata)
2819 if (lc->ringstream!=0){
2820 ms_warning("Cannot start ring now,there's already a ring being played");
2823 lc_callback_obj_init(&lc->preview_finished_cb,func,userdata);
2824 lc->preview_finished=0;
2825 if (lc->sound_conf.ring_sndcard!=NULL){
2826 lc->ringstream=ring_start_with_cb(ring,2000,lc->sound_conf.ring_sndcard,notify_end_of_ring,(void *)lc);
2832 * Sets the path to a wav file used for ringing back.
2834 * Ringback means the ring that is heard when it's ringing at the remote party.
2835 * The file must be a wav 16bit linear.
2837 * @ingroup media_parameters
2839 void linphone_core_set_ringback(LinphoneCore *lc, const char *path){
2840 if (lc->sound_conf.remote_ring!=0){
2841 ms_free(lc->sound_conf.remote_ring);
2843 lc->sound_conf.remote_ring=ms_strdup(path);
2847 * Returns the path to the wav file used for ringing back.
2849 * @ingroup media_parameters
2851 const char * linphone_core_get_ringback(const LinphoneCore *lc){
2852 return lc->sound_conf.remote_ring;
2856 * Enables or disable echo cancellation.
2858 * @ingroup media_parameters
2860 void linphone_core_enable_echo_cancellation(LinphoneCore *lc, bool_t val){
2861 lc->sound_conf.ec=val;
2863 lp_config_set_int(lc->config,"sound","echocancellation",val);
2867 * Returns TRUE if echo cancellation is enabled.
2869 * @ingroup media_parameters
2871 bool_t linphone_core_echo_cancellation_enabled(LinphoneCore *lc){
2872 return lc->sound_conf.ec;
2875 void linphone_core_enable_echo_limiter(LinphoneCore *lc, bool_t val){
2876 lc->sound_conf.ea=val;
2879 bool_t linphone_core_echo_limiter_enabled(const LinphoneCore *lc){
2880 return lc->sound_conf.ea;
2884 * Mutes or unmutes the local microphone.
2886 * @ingroup media_parameters
2888 void linphone_core_mute_mic(LinphoneCore *lc, bool_t val){
2889 if (lc->audiostream!=NULL){
2890 audio_stream_set_mic_gain(lc->audiostream,
2891 (val==TRUE) ? 0 : 1.0);
2895 bool_t linphone_core_is_mic_muted(LinphoneCore *lc) {
2897 if (lc->audiostream && lc->audiostream->volsend){
2898 ms_filter_call_method(lc->audiostream->volsend,MS_VOLUME_GET_GAIN,&gain);
2899 }else ms_warning("Could not get gain: gain control wasn't activated. ");
2904 void linphone_core_enable_agc(LinphoneCore *lc, bool_t val){
2905 lc->sound_conf.agc=val;
2908 bool_t linphone_core_agc_enabled(const LinphoneCore *lc){
2909 return lc->sound_conf.agc;
2913 * Send the specified dtmf.
2915 * @ingroup media_parameters
2916 * This function only works during calls. The dtmf is automatically played to the user.
2917 * @param lc The LinphoneCore object
2918 * @param dtmf The dtmf name specified as a char, such as '0', '#' etc...
2921 void linphone_core_send_dtmf(LinphoneCore *lc, char dtmf)
2923 /*By default we send DTMF RFC2833 if we do not have enabled SIP_INFO but we can also send RFC2833 and SIP_INFO*/
2924 if (linphone_core_get_use_rfc2833_for_dtmf(lc)!=0 || linphone_core_get_use_info_for_dtmf(lc)==0)
2927 if (lc->audiostream!=NULL){
2928 audio_stream_send_dtmf(lc->audiostream,dtmf);
2932 ms_error("we cannot send RFC2833 dtmf when we are not in communication");
2935 if (linphone_core_get_use_info_for_dtmf(lc)!=0){
2936 /* Out of Band DTMF (use INFO method) */
2937 LinphoneCall *call=linphone_core_get_current_call(lc);
2941 sal_call_send_dtmf(call->op,dtmf);
2945 void linphone_core_set_stun_server(LinphoneCore *lc, const char *server){
2946 if (lc->net_conf.stun_server!=NULL)
2947 ms_free(lc->net_conf.stun_server);
2949 lc->net_conf.stun_server=ms_strdup(server);
2950 else lc->net_conf.stun_server=NULL;
2953 const char * linphone_core_get_stun_server(const LinphoneCore *lc){
2954 return lc->net_conf.stun_server;
2957 const char * linphone_core_get_relay_addr(const LinphoneCore *lc){
2958 return lc->net_conf.relay;
2961 int linphone_core_set_relay_addr(LinphoneCore *lc, const char *addr){
2962 if (lc->net_conf.relay!=NULL){
2963 ms_free(lc->net_conf.relay);
2964 lc->net_conf.relay=NULL;
2967 lc->net_conf.relay=ms_strdup(addr);
2972 void linphone_core_set_nat_address(LinphoneCore *lc, const char *addr)
2974 if (lc->net_conf.nat_address!=NULL){
2975 ms_free(lc->net_conf.nat_address);
2977 if (addr!=NULL) lc->net_conf.nat_address=ms_strdup(addr);
2978 else lc->net_conf.nat_address=NULL;
2979 if (lc->sip_conf.contact) update_primary_contact(lc);
2982 const char *linphone_core_get_nat_address(const LinphoneCore *lc)
2984 return lc->net_conf.nat_address;
2987 void linphone_core_set_firewall_policy(LinphoneCore *lc, LinphoneFirewallPolicy pol){
2988 lc->net_conf.firewall_policy=pol;
2989 if (lc->sip_conf.contact) update_primary_contact(lc);
2992 LinphoneFirewallPolicy linphone_core_get_firewall_policy(const LinphoneCore *lc){
2993 return lc->net_conf.firewall_policy;
2997 * Get the list of call logs (past calls).
2999 * @ingroup call_logs
3001 const MSList * linphone_core_get_call_logs(LinphoneCore *lc){
3003 return lc->call_logs;
3007 * Erase the call log.
3009 * @ingroup call_logs
3011 void linphone_core_clear_call_logs(LinphoneCore *lc){
3013 ms_list_for_each(lc->call_logs,(void (*)(void*))linphone_call_log_destroy);
3014 lc->call_logs=ms_list_free(lc->call_logs);
3015 call_logs_write_to_config_file(lc);
3018 static void toggle_video_preview(LinphoneCore *lc, bool_t val){
3019 #ifdef VIDEO_ENABLED
3020 if (lc->videostream==NULL){
3022 if (lc->previewstream==NULL){
3023 lc->previewstream=video_preview_start(lc->video_conf.device,
3024 lc->video_conf.vsize);
3027 if (lc->previewstream!=NULL){
3028 video_preview_stop(lc->previewstream);
3029 lc->previewstream=NULL;
3037 * Enables video globally.
3039 * @ingroup media_parameters
3040 * This function does not have any effect during calls. It just indicates LinphoneCore to
3041 * initiate future calls with video or not. The two boolean parameters indicate in which
3042 * direction video is enabled. Setting both to false disables video entirely.
3044 * @param vcap_enabled indicates whether video capture is enabled
3045 * @param display_enabled indicates whether video display should be shown
3048 void linphone_core_enable_video(LinphoneCore *lc, bool_t vcap_enabled, bool_t display_enabled){
3049 #ifndef VIDEO_ENABLED
3050 if (vcap_enabled || display_enabled)
3051 ms_warning("This version of linphone was built without video support.");
3053 lc->video_conf.capture=vcap_enabled;
3054 lc->video_conf.display=display_enabled;
3056 /* need to re-apply network bandwidth settings*/
3057 linphone_core_set_download_bandwidth(lc,
3058 linphone_core_get_download_bandwidth(lc));
3059 linphone_core_set_upload_bandwidth(lc,
3060 linphone_core_get_upload_bandwidth(lc));
3064 * Returns TRUE if video is enabled, FALSE otherwise.
3065 * @ingroup media_parameters
3067 bool_t linphone_core_video_enabled(LinphoneCore *lc){
3068 return (lc->video_conf.display || lc->video_conf.capture);
3072 * Controls video preview enablement.
3074 * @ingroup media_parameters
3075 * Video preview refers to the action of displaying the local webcam image
3076 * to the user while not in call.
3078 void linphone_core_enable_video_preview(LinphoneCore *lc, bool_t val){
3079 lc->video_conf.show_local=val;
3083 * Returns TRUE if video previewing is enabled.
3084 * @ingroup media_parameters
3086 bool_t linphone_core_video_preview_enabled(const LinphoneCore *lc){
3087 return lc->video_conf.show_local;
3091 * Enables or disable self view during calls.
3093 * @ingroup media_parameters
3094 * Self-view refers to having local webcam image inserted in corner
3095 * of the video window during calls.
3096 * This function works at any time, including during calls.
3098 void linphone_core_enable_self_view(LinphoneCore *lc, bool_t val){
3099 lc->video_conf.selfview=val;
3100 #ifdef VIDEO_ENABLED
3101 if (lc->videostream){
3102 video_stream_enable_self_view(lc->videostream,val);
3108 * Returns TRUE if self-view is enabled, FALSE otherwise.
3110 * @ingroup media_parameters
3112 * Refer to linphone_core_enable_self_view() for details.
3114 bool_t linphone_core_self_view_enabled(const LinphoneCore *lc){
3115 return lc->video_conf.selfview;
3119 * Sets the active video device.
3121 * @ingroup media_parameters
3122 * @param id the name of the video device as returned by linphone_core_get_video_devices()
3124 int linphone_core_set_video_device(LinphoneCore *lc, const char *id){
3125 MSWebCam *olddev=lc->video_conf.device;
3128 lc->video_conf.device=ms_web_cam_manager_get_cam(ms_web_cam_manager_get(),id);
3129 if (lc->video_conf.device==NULL){
3130 ms_warning("Could not found video device %s",id);
3133 if (lc->video_conf.device==NULL)
3134 lc->video_conf.device=ms_web_cam_manager_get_default_cam(ms_web_cam_manager_get());
3135 if (olddev!=NULL && olddev!=lc->video_conf.device){
3136 toggle_video_preview(lc,FALSE);/*restart the video local preview*/
3138 if (lc->ready && lc->video_conf.device){
3139 vd=ms_web_cam_get_string_id(lc->video_conf.device);
3140 if (vd && strstr(vd,"Static picture")!=NULL){
3143 lp_config_set_string(lc->config,"video","device",vd);
3149 * Returns the name of the currently active video device.
3151 * @ingroup media_parameters
3153 const char *linphone_core_get_video_device(const LinphoneCore *lc){
3154 if (lc->video_conf.device) return ms_web_cam_get_string_id(lc->video_conf.device);
3158 int linphone_core_set_static_picture(LinphoneCore *lc, const char *path) {
3159 #ifdef VIDEO_ENABLED
3160 VideoStream *vs = NULL;
3161 /* Select the video stream from the call in the first place */
3162 if (lc && lc->videostream) {
3163 vs = lc->videostream;
3165 /* If not in call, select the video stream from the preview */
3166 if (vs == NULL && lc && lc->previewstream) {
3167 vs = lc->previewstream;
3170 /* If we have a video stream (either preview, either from call), we
3171 have a source and it is using the static picture filter, then
3172 force the filter to use that picture. */
3173 if (vs && vs->source) {
3174 if (ms_filter_get_id(vs->source) == MS_STATIC_IMAGE_ID) {
3175 ms_filter_call_method(vs->source, MS_FILTER_SET_IMAGE,
3180 /* Tell the static image filter to use that image from now on so
3181 that the image will be used next time it has to be read */
3182 ms_static_image_set_default_image(path);
3184 ms_warning("Video support not compiled.");
3190 * Returns the native window handle of the video window, casted as an unsigned long.
3192 * @ingroup media_parameters
3194 unsigned long linphone_core_get_native_video_window_id(const LinphoneCore *lc){
3195 #ifdef VIDEO_ENABLED
3196 if (lc->videostream)
3197 return video_stream_get_native_window_id(lc->videostream);
3198 if (lc->previewstream)
3199 return video_stream_get_native_window_id(lc->previewstream);
3204 static MSVideoSizeDef supported_resolutions[]={
3205 { {MS_VIDEO_SIZE_SVGA_W,MS_VIDEO_SIZE_SVGA_H} , "svga" },
3206 { {MS_VIDEO_SIZE_4CIF_W,MS_VIDEO_SIZE_4CIF_H} , "4cif" },
3207 { {MS_VIDEO_SIZE_VGA_W,MS_VIDEO_SIZE_VGA_H} , "vga" },
3208 { {MS_VIDEO_SIZE_CIF_W,MS_VIDEO_SIZE_CIF_H} , "cif" },
3209 { {MS_VIDEO_SIZE_QVGA_W,MS_VIDEO_SIZE_QVGA_H} , "qvga" },
3210 { {MS_VIDEO_SIZE_QCIF_W,MS_VIDEO_SIZE_QCIF_H} , "qcif" },
3215 * Returns the zero terminated table of supported video resolutions.
3217 * @ingroup media_parameters
3219 const MSVideoSizeDef *linphone_core_get_supported_video_sizes(LinphoneCore *lc){
3220 return supported_resolutions;
3223 static MSVideoSize video_size_get_by_name(const char *name){
3224 MSVideoSizeDef *pdef=supported_resolutions;
3225 MSVideoSize null_vsize={0,0};
3226 for(;pdef->name!=NULL;pdef++){
3227 if (strcasecmp(name,pdef->name)==0){
3231 ms_warning("Video resolution %s is not supported in linphone.",name);
3235 static const char *video_size_get_name(MSVideoSize vsize){
3236 MSVideoSizeDef *pdef=supported_resolutions;
3237 for(;pdef->name!=NULL;pdef++){
3238 if (pdef->vsize.width==vsize.width && pdef->vsize.height==vsize.height){
3245 static bool_t video_size_supported(MSVideoSize vsize){
3246 if (video_size_get_name(vsize)) return TRUE;
3247 ms_warning("Video resolution %ix%i is not supported in linphone.",vsize.width,vsize.height);
3252 * Sets the preferred video size.
3254 * @ingroup media_parameters
3255 * This applies only to the stream that is captured and sent to the remote party,
3256 * since we accept all standard video size on the receive path.
3258 void linphone_core_set_preferred_video_size(LinphoneCore *lc, MSVideoSize vsize){
3259 if (video_size_supported(vsize)){
3260 MSVideoSize oldvsize=lc->video_conf.vsize;
3261 lc->video_conf.vsize=vsize;
3262 if (!ms_video_size_equal(oldvsize,vsize) && lc->previewstream!=NULL){
3263 toggle_video_preview(lc,FALSE);
3264 toggle_video_preview(lc,TRUE);
3267 lp_config_set_string(lc->config,"video","size",video_size_get_name(vsize));
3272 * Sets the preferred video size by its name.
3274 * @ingroup media_parameters
3275 * This is identical to linphone_core_set_preferred_video_size() except
3276 * that it takes the name of the video resolution as input.
3277 * Video resolution names are: qcif, svga, cif, vga, 4cif, svga ...
3279 void linphone_core_set_preferred_video_size_by_name(LinphoneCore *lc, const char *name){
3280 MSVideoSize vsize=video_size_get_by_name(name);
3281 MSVideoSize default_vsize={MS_VIDEO_SIZE_CIF_W,MS_VIDEO_SIZE_CIF_H};
3282 if (vsize.width!=0) linphone_core_set_preferred_video_size(lc,vsize);
3283 else linphone_core_set_preferred_video_size(lc,default_vsize);
3287 * Returns the current preferred video size for sending.
3289 * @ingroup media_parameters
3291 MSVideoSize linphone_core_get_preferred_video_size(LinphoneCore *lc){
3292 return lc->video_conf.vsize;
3295 void linphone_core_use_files(LinphoneCore *lc, bool_t yesno){
3296 lc->use_files=yesno;
3299 void linphone_core_set_play_file(LinphoneCore *lc, const char *file){
3300 if (lc->play_file!=NULL){
3301 ms_free(lc->play_file);
3305 lc->play_file=ms_strdup(file);
3306 if (lc->audiostream->ticker)
3307 audio_stream_play(lc->audiostream,file);
3311 void linphone_core_set_record_file(LinphoneCore *lc, const char *file){
3312 if (lc->rec_file!=NULL){
3313 ms_free(lc->rec_file);
3317 lc->rec_file=ms_strdup(file);
3318 if (lc->audiostream)
3319 audio_stream_record(lc->audiostream,file);
3324 * Retrieves the user pointer that was given to linphone_core_new()
3326 * @ingroup initializing
3328 void *linphone_core_get_user_data(LinphoneCore *lc){
3332 int linphone_core_get_mtu(const LinphoneCore *lc){
3333 return lc->net_conf.mtu;
3336 void linphone_core_set_mtu(LinphoneCore *lc, int mtu){
3337 lc->net_conf.mtu=mtu;
3340 ms_error("MTU too small !");
3344 ms_message("MTU is supposed to be %i, rtp payload max size will be %i",mtu, ms_get_payload_max_size());
3345 }else ms_set_mtu(0);//use mediastreamer2 default value
3348 void linphone_core_set_waiting_callback(LinphoneCore *lc, LinphoneWaitingCallback cb, void *user_context){
3350 lc->wait_ctx=user_context;
3353 void linphone_core_start_waiting(LinphoneCore *lc, const char *purpose){
3355 lc->wait_ctx=lc->wait_cb(lc,lc->wait_ctx,LinphoneWaitingStart,purpose,0);
3359 void linphone_core_update_progress(LinphoneCore *lc, const char *purpose, float progress){
3361 lc->wait_ctx=lc->wait_cb(lc,lc->wait_ctx,LinphoneWaitingProgress,purpose,progress);
3371 void linphone_core_stop_waiting(LinphoneCore *lc){
3373 lc->wait_ctx=lc->wait_cb(lc,lc->wait_ctx,LinphoneWaitingFinished,NULL,0);
3377 void linphone_core_set_audio_transports(LinphoneCore *lc, RtpTransport *rtp, RtpTransport *rtcp){
3382 void net_config_uninit(LinphoneCore *lc)
3384 net_config_t *config=&lc->net_conf;
3385 lp_config_set_int(lc->config,"net","download_bw",config->download_bw);
3386 lp_config_set_int(lc->config,"net","upload_bw",config->upload_bw);
3388 if (config->stun_server!=NULL){
3389 lp_config_set_string(lc->config,"net","stun_server",config->stun_server);
3390 ms_free(lc->net_conf.stun_server);
3392 if (config->nat_address!=NULL){
3393 lp_config_set_string(lc->config,"net","nat_address",config->nat_address);
3394 ms_free(lc->net_conf.nat_address);
3396 lp_config_set_int(lc->config,"net","firewall_policy",config->firewall_policy);
3397 lp_config_set_int(lc->config,"net","mtu",config->mtu);
3401 void sip_config_uninit(LinphoneCore *lc)
3405 sip_config_t *config=&lc->sip_conf;
3406 lp_config_set_int(lc->config,"sip","sip_port",config->sip_port);
3407 lp_config_set_int(lc->config,"sip","guess_hostname",config->guess_hostname);
3408 lp_config_set_string(lc->config,"sip","contact",config->contact);
3409 lp_config_set_int(lc->config,"sip","inc_timeout",config->inc_timeout);
3410 lp_config_set_int(lc->config,"sip","use_info",config->use_info);
3411 lp_config_set_int(lc->config,"sip","use_rfc2833",config->use_rfc2833);
3412 lp_config_set_int(lc->config,"sip","use_ipv6",config->ipv6_enabled);
3413 lp_config_set_int(lc->config,"sip","register_only_when_network_is_up",config->register_only_when_network_is_up);
3415 lp_config_set_int(lc->config,"sip","default_proxy",linphone_core_get_default_proxy(lc,NULL));
3417 for(elem=config->proxies,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
3418 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)(elem->data);
3419 linphone_proxy_config_write_to_config_file(lc->config,cfg,i);
3420 linphone_proxy_config_edit(cfg); /* to unregister */
3426 sal_iterate(lc->sal);
3435 ms_list_for_each(config->proxies,(void (*)(void*)) linphone_proxy_config_destroy);
3436 ms_list_free(config->proxies);
3437 config->proxies=NULL;
3439 linphone_proxy_config_write_to_config_file(lc->config,NULL,i); /*mark the end */
3441 for(elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
3442 LinphoneAuthInfo *ai=(LinphoneAuthInfo*)(elem->data);
3443 linphone_auth_info_write_config(lc->config,ai,i);
3445 linphone_auth_info_write_config(lc->config,NULL,i); /* mark the end */
3446 ms_list_for_each(lc->auth_info,(void (*)(void*))linphone_auth_info_destroy);
3447 ms_list_free(lc->auth_info);
3450 sal_uninit(lc->sal);
3453 if (lc->sip_conf.guessed_contact)
3454 ms_free(lc->sip_conf.guessed_contact);
3455 if (config->contact)
3456 ms_free(config->contact);
3460 void rtp_config_uninit(LinphoneCore *lc)
3462 rtp_config_t *config=&lc->rtp_conf;
3463 lp_config_set_int(lc->config,"rtp","audio_rtp_port",config->audio_rtp_port);
3464 lp_config_set_int(lc->config,"rtp","video_rtp_port",config->video_rtp_port);
3465 lp_config_set_int(lc->config,"rtp","audio_jitt_comp",config->audio_jitt_comp);
3466 lp_config_set_int(lc->config,"rtp","video_jitt_comp",config->video_jitt_comp);
3467 lp_config_set_int(lc->config,"rtp","nortp_timeout",config->nortp_timeout);
3470 void sound_config_uninit(LinphoneCore *lc)
3472 sound_config_t *config=&lc->sound_conf;
3473 ms_free(config->cards);
3475 lp_config_set_string(lc->config,"sound","remote_ring",config->remote_ring);
3477 if (config->local_ring) ms_free(config->local_ring);
3478 if (config->remote_ring) ms_free(config->remote_ring);
3479 ms_snd_card_manager_destroy();
3482 void video_config_uninit(LinphoneCore *lc)
3484 lp_config_set_int(lc->config,"video","enabled",linphone_core_video_enabled(lc));
3485 lp_config_set_string(lc->config,"video","size",video_size_get_name(linphone_core_get_preferred_video_size(lc)));
3486 lp_config_set_int(lc->config,"video","display",lc->video_conf.display);
3487 lp_config_set_int(lc->config,"video","capture",lc->video_conf.capture);
3488 lp_config_set_int(lc->config,"video","show_local",linphone_core_video_preview_enabled(lc));
3489 lp_config_set_int(lc->config,"video","self_view",linphone_core_self_view_enabled(lc));
3490 if (lc->video_conf.cams)
3491 ms_free(lc->video_conf.cams);
3494 void codecs_config_uninit(LinphoneCore *lc)
3497 codecs_config_t *config=&lc->codecs_conf;
3502 for(node=config->audio_codecs;node!=NULL;node=ms_list_next(node)){
3503 pt=(PayloadType*)(node->data);
3504 sprintf(key,"audio_codec_%i",index);
3505 lp_config_set_string(lc->config,key,"mime",pt->mime_type);
3506 lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
3507 lp_config_set_int(lc->config,key,"enabled",linphone_core_payload_type_enabled(lc,pt));
3511 for(node=config->video_codecs;node!=NULL;node=ms_list_next(node)){
3512 pt=(PayloadType*)(node->data);
3513 sprintf(key,"video_codec_%i",index);
3514 lp_config_set_string(lc->config,key,"mime",pt->mime_type);
3515 lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
3516 lp_config_set_int(lc->config,key,"enabled",linphone_core_payload_type_enabled(lc,pt));
3517 lp_config_set_string(lc->config,key,"recv_fmtp",pt->recv_fmtp);
3520 ms_list_free(lc->codecs_conf.audio_codecs);
3521 ms_list_free(lc->codecs_conf.video_codecs);
3524 void ui_config_uninit(LinphoneCore* lc)
3527 ms_list_for_each(lc->friends,(void (*)(void *))linphone_friend_destroy);
3528 ms_list_free(lc->friends);
3534 * Returns the LpConfig object used to manage the storage (config) file.
3537 * The application can use the LpConfig object to insert its own private
3538 * sections and pairs of key=value in the configuration file.
3541 LpConfig *linphone_core_get_config(LinphoneCore *lc){
3545 static void linphone_core_uninit(LinphoneCore *lc)
3547 if(linphone_core_get_calls_nb(lc)){
3549 linphone_core_terminate_all_calls(lc);
3556 linphone_core_iterate(lc);
3560 ms_list_for_each(lc->friends,(void (*)(void *))linphone_friend_close_subscriptions);
3561 gstate_new_state(lc, GSTATE_POWER_SHUTDOWN, NULL);
3562 #ifdef VIDEO_ENABLED
3563 if (lc->previewstream!=NULL){
3564 video_preview_stop(lc->previewstream);
3565 lc->previewstream=NULL;
3568 /* save all config */
3569 net_config_uninit(lc);
3570 sip_config_uninit(lc);
3571 rtp_config_uninit(lc);
3572 sound_config_uninit(lc);
3573 video_config_uninit(lc);
3574 codecs_config_uninit(lc);
3575 ui_config_uninit(lc);
3576 if (lp_config_needs_commit(lc->config)) lp_config_sync(lc->config);
3577 lp_config_destroy(lc->config);
3578 lc->config = NULL; /* Mark the config as NULL to block further calls */
3579 sip_setup_unregister_all();
3581 ms_list_for_each(lc->call_logs,(void (*)(void*))linphone_call_log_destroy);
3582 lc->call_logs=ms_list_free(lc->call_logs);
3584 linphone_core_free_payload_types();
3587 gstate_new_state(lc, GSTATE_POWER_OFF, NULL);
3590 static void set_network_reachable(LinphoneCore* lc,bool_t isReachable){
3591 ms_message("Network state is now [%s]",isReachable?"UP":"DOWN");
3592 // second get the list of available proxies
3593 const MSList *elem=linphone_core_get_proxy_config_list(lc);
3594 for(;elem!=NULL;elem=elem->next){
3595 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
3596 if (linphone_proxy_config_register_enabled(cfg) ) {
3604 lc->network_reachable=isReachable;
3607 void linphone_core_set_network_reachable(LinphoneCore* lc,bool_t isReachable) {
3608 //first disable automatic mode
3609 if (lc->auto_net_state_mon) {
3610 ms_message("Disabling automatic network state monitoring");
3611 lc->auto_net_state_mon=FALSE;
3613 set_network_reachable(lc,isReachable);
3616 bool_t linphone_core_is_network_reachabled(LinphoneCore* lc) {
3617 return lc->network_reachable;
3619 ortp_socket_t linphone_core_get_sip_socket(LinphoneCore *lc){
3620 return sal_get_socket(lc->sal);
3623 * Destroys a LinphoneCore
3625 * @ingroup initializing
3627 void linphone_core_destroy(LinphoneCore *lc){
3628 linphone_core_uninit(lc);
3632 * Get the number of Call
3634 * @ingroup call_control
3636 int linphone_core_get_calls_nb(const LinphoneCore *lc)
3639 if(lc->calls == NULL)
3645 returned = ms_list_size(lc->calls);
3651 * Check if we do not have exceed the number of simultaneous call
3653 * @ingroup call_control
3655 bool_t linphone_core_can_we_add_call(LinphoneCore *lc)
3657 if(linphone_core_get_calls_nb(lc) < NB_MAX_CALLS)
3663 * Unset the current call
3665 * @ingroup call_control
3667 int linphone_core_unset_the_current_call(LinphoneCore *lc)
3669 if(lc->current_call == NULL)
3671 lc->current_call = NULL;
3672 ms_message("Current call unset\n");
3677 * Set the call in parameter as the new current call
3679 * @ingroup call_control
3681 int linphone_core_set_as_current_call(LinphoneCore *lc, LinphoneCall *call)
3683 if(lc->current_call != NULL)
3685 lc->current_call = call;
3690 * Add the call in the LinphoneCall list
3692 * @ingroup call_control
3694 int linphone_core_add_call( LinphoneCore *lc, LinphoneCall *call)
3696 if(linphone_core_can_we_add_call(lc))
3698 MSList *the_calls = lc->calls;
3699 the_calls = ms_list_append(the_calls,(void *)call);
3700 lc->calls = the_calls;
3707 * Add the call in the LinphoneCall list
3709 * @ingroup call_control
3711 int linphone_core_del_call( LinphoneCore *lc, LinphoneCall *call)
3715 MSList *the_calls = lc->calls;
3716 if(call == linphone_core_get_current_call(lc))
3718 linphone_core_unset_the_current_call(lc);
3720 it=ms_list_find(the_calls,call);
3723 the_calls = ms_list_remove_link(the_calls,it);
3727 ms_warning("could not find the call into the list\n");
3730 lc->calls = the_calls;