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 static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, bool_t only_one_codec){
69 for(it=codecs;it!=NULL;it=it->next){
70 PayloadType *pt=(PayloadType*)it->data;
71 if ((pt->flags & PAYLOAD_TYPE_ENABLED) && linphone_core_check_payload_type_usability(lc,pt)){
72 l=ms_list_append(l,payload_type_clone(pt));
73 if (only_one_codec) break;
79 static SalMediaDescription *create_local_media_description(LinphoneCore *lc,
80 const char *localip, const char *username, bool_t only_one_codec){
83 SalMediaDescription *md=sal_media_description_new();
85 strncpy(md->addr,localip,sizeof(md->addr));
86 strncpy(md->username,username,sizeof(md->username));
87 md->bandwidth=linphone_core_get_download_bandwidth(lc);
88 /*set audio capabilities */
89 strncpy(md->streams[0].addr,localip,sizeof(md->streams[0].addr));
90 md->streams[0].port=linphone_core_get_audio_port(lc);
91 md->streams[0].proto=SalProtoRtpAvp;
92 md->streams[0].type=SalAudio;
93 md->streams[0].ptime=lc->net_conf.down_ptime;
94 l=make_codec_list(lc,lc->codecs_conf.audio_codecs,only_one_codec);
95 pt=payload_type_clone(rtp_profile_get_payload_from_mime(&av_profile,"telephone-event"));
96 l=ms_list_append(l,pt);
97 md->streams[0].payloads=l;
99 if (lc->dw_audio_bw>0)
100 md->streams[0].bandwidth=lc->dw_audio_bw;
102 if (linphone_core_video_enabled (lc)){
104 md->streams[1].port=linphone_core_get_video_port(lc);
105 md->streams[1].proto=SalProtoRtpAvp;
106 md->streams[1].type=SalVideo;
107 l=make_codec_list(lc,lc->codecs_conf.video_codecs,only_one_codec);
108 md->streams[1].payloads=l;
110 md->streams[1].bandwidth=lc->dw_video_bw;
115 static void linphone_call_init_common(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
116 call->state=LCStateInit;
117 call->start_time=time(NULL);
118 call->media_start_time=0;
119 call->log=linphone_call_log_new(call, from, to);
120 linphone_core_notify_all_friends(call->core,LINPHONE_STATUS_ONTHEPHONE);
121 if (linphone_core_get_firewall_policy(call->core)==LINPHONE_POLICY_USE_STUN)
122 linphone_core_run_stun_tests(call->core,call);
125 static void discover_mtu(LinphoneCore *lc, const char *remote){
127 if (lc->net_conf.mtu==0 ){
128 /*attempt to discover mtu*/
129 mtu=ms_discover_mtu(remote);
132 ms_message("Discovered mtu is %i, RTP payload max size is %i",
133 mtu, ms_get_payload_max_size());
138 LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to)
140 LinphoneCall *call=ms_new0(LinphoneCall,1);
141 call->dir=LinphoneCallOutgoing;
142 call->op=sal_op_new(lc->sal);
143 sal_op_set_user_pointer(call->op,call);
145 linphone_core_get_local_ip(lc,linphone_address_get_domain(to),call->localip);
146 call->localdesc=create_local_media_description (lc,call->localip,
147 linphone_address_get_username(from),FALSE);
148 linphone_call_init_common(call,from,to);
149 discover_mtu(lc,linphone_address_get_domain (to));
153 LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, SalOp *op){
154 LinphoneCall *call=ms_new0(LinphoneCall,1);
155 LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
159 call->dir=LinphoneCallIncoming;
160 sal_op_set_user_pointer(op,call);
164 if (lc->sip_conf.ping_with_options){
165 /*the following sends an option request back to the caller so that
166 we get a chance to discover our nat'd address before answering.*/
167 call->ping_op=sal_op_new(lc->sal);
168 to_str=linphone_address_as_string(to);
169 from_str=linphone_address_as_string(from);
170 sal_op_set_route(call->ping_op,sal_op_get_network_origin(call->op));
171 sal_ping(call->ping_op,to_str,from_str);
176 linphone_address_clean(from);
177 linphone_core_get_local_ip(lc,linphone_address_get_domain(from),call->localip);
178 call->localdesc=create_local_media_description (lc,call->localip,
179 linphone_address_get_username(me),lc->sip_conf.only_one_codec);
180 linphone_call_init_common(call, from, to);
181 discover_mtu(lc,linphone_address_get_domain(from));
182 linphone_address_destroy(me);
186 void linphone_call_destroy(LinphoneCall *obj)
188 linphone_core_notify_all_friends(obj->core,obj->core->prev_mode);
189 linphone_call_log_completed(obj->log,obj);
190 linphone_core_update_allocated_audio_bandwidth(obj->core);
192 sal_op_release(obj->op);
195 if (obj->resultdesc!=NULL) {
196 sal_media_description_unref(obj->resultdesc);
197 obj->resultdesc=NULL;
199 if (obj->localdesc!=NULL) {
200 sal_media_description_unref(obj->localdesc);
204 sal_op_release(obj->ping_op);
209 /*prevent a gcc bug with %c*/
210 static size_t my_strftime(char *s, size_t max, const char *fmt, const struct tm *tm){
211 #if !defined(_WIN32_WCE)
212 return strftime(s, max, fmt, tm);
216 #endif /*_WIN32_WCE*/
219 static void set_call_log_date(LinphoneCallLog *cl, const struct tm *loctime){
220 my_strftime(cl->start_date,sizeof(cl->start_date),"%c",loctime);
223 LinphoneCallLog * linphone_call_log_new(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
224 LinphoneCallLog *cl=ms_new0(LinphoneCallLog,1);
228 #if !defined(_WIN32_WCE)
229 loctime=*localtime(&call->start_time);
231 #endif /*_WIN32_WCE*/
233 localtime_r(&call->start_time,&loctime);
235 set_call_log_date(cl,&loctime);
241 static void call_logs_write_to_config_file(LinphoneCore *lc){
246 LpConfig *cfg=lc->config;
248 if (!lc->ready) return;
250 for(i=0,elem=lc->call_logs;elem!=NULL;elem=elem->next,++i){
251 LinphoneCallLog *cl=(LinphoneCallLog*)elem->data;
252 snprintf(logsection,sizeof(logsection),"call_log_%i",i);
253 lp_config_set_int(cfg,logsection,"dir",cl->dir);
254 lp_config_set_int(cfg,logsection,"status",cl->status);
255 tmp=linphone_address_as_string(cl->from);
256 lp_config_set_string(cfg,logsection,"from",tmp);
258 tmp=linphone_address_as_string(cl->to);
259 lp_config_set_string(cfg,logsection,"to",tmp);
261 lp_config_set_string(cfg,logsection,"start_date",cl->start_date);
262 lp_config_set_int(cfg,logsection,"duration",cl->duration);
263 if (cl->refkey) lp_config_set_string(cfg,logsection,"refkey",cl->refkey);
265 for(;i<lc->max_call_logs;++i){
266 snprintf(logsection,sizeof(logsection),"call_log_%i",i);
267 lp_config_clean_section(cfg,logsection);
271 static void call_logs_read_from_config_file(LinphoneCore *lc){
275 LpConfig *cfg=lc->config;
277 snprintf(logsection,sizeof(logsection),"call_log_%i",i);
278 if (lp_config_has_section(cfg,logsection)){
279 LinphoneCallLog *cl=ms_new0(LinphoneCallLog,1);
280 cl->dir=lp_config_get_int(cfg,logsection,"dir",0);
281 cl->status=lp_config_get_int(cfg,logsection,"status",0);
282 tmp=lp_config_get_string(cfg,logsection,"from",NULL);
283 if (tmp) cl->from=linphone_address_new(tmp);
284 tmp=lp_config_get_string(cfg,logsection,"to",NULL);
285 if (tmp) cl->to=linphone_address_new(tmp);
286 tmp=lp_config_get_string(cfg,logsection,"start_date",NULL);
287 if (tmp) strncpy(cl->start_date,tmp,sizeof(cl->start_date));
288 cl->duration=lp_config_get_int(cfg,logsection,"duration",0);
289 tmp=lp_config_get_string(cfg,logsection,"refkey",NULL);
290 if (tmp) cl->refkey=ms_strdup(tmp);
291 lc->call_logs=ms_list_append(lc->call_logs,cl);
297 void linphone_call_log_completed(LinphoneCallLog *calllog, LinphoneCall *call){
298 LinphoneCore *lc=call->core;
300 calllog->duration=time(NULL)-call->start_time;
303 case LCStatePreEstablishing:
304 calllog->status=LinphoneCallAborted;
307 if (calllog->dir==LinphoneCallIncoming){
309 calllog->status=LinphoneCallMissed;
311 info=ortp_strdup_printf(ngettext("You have missed %i call.",
312 "You have missed %i calls.", lc->missed_calls),
314 lc->vtable.display_status(lc,info);
317 else calllog->status=LinphoneCallAborted;
319 case LCStateAVRunning:
320 calllog->status=LinphoneCallSuccess;
323 lc->call_logs=ms_list_prepend(lc->call_logs,(void *)calllog);
324 if (ms_list_size(lc->call_logs)>lc->max_call_logs){
325 MSList *elem,*prevelem=NULL;
326 /*find the last element*/
327 for(elem=lc->call_logs;elem!=NULL;elem=elem->next){
331 linphone_call_log_destroy((LinphoneCallLog*)elem->data);
332 lc->call_logs=ms_list_remove_link(lc->call_logs,elem);
334 if (lc->vtable.call_log_updated!=NULL){
335 lc->vtable.call_log_updated(lc,calllog);
337 call_logs_write_to_config_file(lc);
341 * @addtogroup call_logs
346 * Returns a human readable string describing the call.
348 * @note: the returned char* must be freed by the application (use ms_free()).
350 char * linphone_call_log_to_str(LinphoneCallLog *cl){
353 char *from=linphone_address_as_string (cl->from);
354 char *to=linphone_address_as_string (cl->to);
356 case LinphoneCallAborted:
359 case LinphoneCallSuccess:
360 status=_("completed");
362 case LinphoneCallMissed:
368 tmp=ortp_strdup_printf(_("%s at %s\nFrom: %s\nTo: %s\nStatus: %s\nDuration: %i mn %i sec\n"),
369 (cl->dir==LinphoneCallIncoming) ? _("Incoming call") : _("Outgoing call"),
381 void linphone_call_log_set_user_pointer(LinphoneCallLog *cl, void *up){
385 void *linphone_call_log_get_user_pointer(const LinphoneCallLog *cl){
386 return cl->user_pointer;
392 * Associate a persistent reference key to the call log.
394 * The reference key can be for example an id to an external database.
395 * It is stored in the config file, thus can survive to process exits/restarts.
398 void linphone_call_log_set_ref_key(LinphoneCallLog *cl, const char *refkey){
399 if (cl->refkey!=NULL){
403 if (refkey) cl->refkey=ms_strdup(refkey);
404 call_logs_write_to_config_file(cl->lc);
408 * Get the persistent reference key associated to the call log.
410 * The reference key can be for example an id to an external database.
411 * It is stored in the config file, thus can survive to process exits/restarts.
414 const char *linphone_call_log_get_ref_key(const LinphoneCallLog *cl){
420 void linphone_call_log_destroy(LinphoneCallLog *cl){
421 if (cl->from!=NULL) linphone_address_destroy(cl->from);
422 if (cl->to!=NULL) linphone_address_destroy(cl->to);
423 if (cl->refkey!=NULL) ms_free(cl->refkey);
428 * Returns TRUE if the LinphoneCall asked to autoanswer
431 bool_t linphone_call_asked_to_autoanswer(LinphoneCall *call){
432 //return TRUE if the unique(for the moment) incoming call asked to be autoanswered
434 return sal_call_autoanswer_asked(call->op);
439 int linphone_core_get_current_call_duration(const LinphoneCore *lc){
440 LinphoneCall *call=lc->call;
441 if (call==NULL) return 0;
442 if (call->media_start_time==0) return 0;
443 return time(NULL)-call->media_start_time;
446 const LinphoneAddress *linphone_core_get_remote_uri(LinphoneCore *lc){
447 LinphoneCall *call=lc->call;
448 if (call==NULL) return 0;
449 return call->dir==LinphoneCallIncoming ? call->log->from : call->log->to;
453 * Enable logs in supplied FILE*.
457 * @param file a C FILE* where to fprintf logs. If null stdout is used.
460 void linphone_core_enable_logs(FILE *file){
461 if (file==NULL) file=stdout;
462 ortp_set_log_file(file);
463 ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
467 * Enable logs through the user's supplied log callback.
471 * @param logfunc The address of a OrtpLogFunc callback whose protoype is
472 * typedef void (*OrtpLogFunc)(OrtpLogLevel lev, const char *fmt, va_list args);
475 void linphone_core_enable_logs_with_cb(OrtpLogFunc logfunc){
476 ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
477 ortp_set_log_handler(logfunc);
481 * Entirely disable logging.
485 void linphone_core_disable_logs(){
486 ortp_set_log_level_mask(ORTP_ERROR|ORTP_FATAL);
491 net_config_read (LinphoneCore *lc)
495 LpConfig *config=lc->config;
497 tmp=lp_config_get_int(config,"net","download_bw",0);
498 linphone_core_set_download_bandwidth(lc,tmp);
499 tmp=lp_config_get_int(config,"net","upload_bw",0);
500 linphone_core_set_upload_bandwidth(lc,tmp);
501 linphone_core_set_stun_server(lc,lp_config_get_string(config,"net","stun_server",NULL));
502 tmpstr=lp_config_get_string(lc->config,"net","nat_address",NULL);
503 if (tmpstr!=NULL && (strlen(tmpstr)<1)) tmpstr=NULL;
504 linphone_core_set_nat_address(lc,tmpstr);
505 tmp=lp_config_get_int(lc->config,"net","firewall_policy",0);
506 linphone_core_set_firewall_policy(lc,tmp);
507 tmp=lp_config_get_int(lc->config,"net","nat_sdp_only",0);
508 lc->net_conf.nat_sdp_only=tmp;
509 tmp=lp_config_get_int(lc->config,"net","mtu",0);
510 linphone_core_set_mtu(lc,tmp);
511 tmp=lp_config_get_int(lc->config,"net","download_ptime",0);
512 linphone_core_set_download_ptime(lc,tmp);
516 static void build_sound_devices_table(LinphoneCore *lc){
517 const char **devices;
521 const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
522 ndev=ms_list_size(elem);
523 devices=ms_malloc((ndev+1)*sizeof(const char *));
524 for (i=0;elem!=NULL;elem=elem->next,i++){
525 devices[i]=ms_snd_card_get_string_id((MSSndCard *)elem->data);
528 old=lc->sound_conf.cards;
529 lc->sound_conf.cards=devices;
530 if (old!=NULL) ms_free(old);
533 static void sound_config_read(LinphoneCore *lc)
540 /*alsadev let the user use custom alsa device within linphone*/
541 devid=lp_config_get_string(lc->config,"sound","alsadev",NULL);
543 MSSndCard *card=ms_alsa_card_new_custom(devid,devid);
544 ms_snd_card_manager_add_card(ms_snd_card_manager_get(),card);
547 /* retrieve all sound devices */
548 build_sound_devices_table(lc);
550 devid=lp_config_get_string(lc->config,"sound","playback_dev_id",NULL);
551 linphone_core_set_playback_device(lc,devid);
553 devid=lp_config_get_string(lc->config,"sound","ringer_dev_id",NULL);
554 linphone_core_set_ringer_device(lc,devid);
556 devid=lp_config_get_string(lc->config,"sound","capture_dev_id",NULL);
557 linphone_core_set_capture_device(lc,devid);
560 tmp=lp_config_get_int(lc->config,"sound","play_lev",80);
561 linphone_core_set_play_level(lc,tmp);
562 tmp=lp_config_get_int(lc->config,"sound","ring_lev",80);
563 linphone_core_set_ring_level(lc,tmp);
564 tmp=lp_config_get_int(lc->config,"sound","rec_lev",80);
565 linphone_core_set_rec_level(lc,tmp);
566 tmpbuf=lp_config_get_string(lc->config,"sound","source","m");
567 linphone_core_set_sound_source(lc,tmpbuf[0]);
570 tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
571 tmpbuf=lp_config_get_string(lc->config,"sound","local_ring",tmpbuf);
572 if (ortp_file_exist(tmpbuf)==-1) {
573 tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
575 if (strstr(tmpbuf,".wav")==NULL){
576 /* it currently uses old sound files, so replace them */
577 tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
580 linphone_core_set_ring(lc,tmpbuf);
582 tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
583 tmpbuf=lp_config_get_string(lc->config,"sound","remote_ring",tmpbuf);
584 if (ortp_file_exist(tmpbuf)==-1){
585 tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
587 if (strstr(tmpbuf,".wav")==NULL){
588 /* it currently uses old sound files, so replace them */
589 tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
591 linphone_core_set_ringback(lc,tmpbuf);
592 check_sound_device(lc);
593 lc->sound_conf.latency=0;
595 linphone_core_enable_echo_cancellation(lc,
596 lp_config_get_int(lc->config,"sound","echocancelation",0) |
597 lp_config_get_int(lc->config,"sound","echocancellation",0)
600 linphone_core_enable_echo_limiter(lc,
601 lp_config_get_int(lc->config,"sound","echolimiter",0));
602 linphone_core_enable_agc(lc,
603 lp_config_get_int(lc->config,"sound","agc",0));
605 gain=lp_config_get_float(lc->config,"sound","soft_play_lev",0);
606 linphone_core_set_soft_play_level(lc,gain);
609 static void sip_config_read(LinphoneCore *lc)
616 port=lp_config_get_int(lc->config,"sip","use_info",0);
617 linphone_core_set_use_info_for_dtmf(lc,port);
619 port=lp_config_get_int(lc->config,"sip","use_rfc2833",0);
620 linphone_core_set_use_rfc2833_for_dtmf(lc,port);
622 ipv6=lp_config_get_int(lc->config,"sip","use_ipv6",-1);
625 if (host_has_ipv6_network()){
626 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"));
629 linphone_core_enable_ipv6(lc,ipv6);
630 port=lp_config_get_int(lc->config,"sip","sip_port",5060);
631 linphone_core_set_sip_port(lc,port);
633 tmpstr=lp_config_get_string(lc->config,"sip","contact",NULL);
634 if (tmpstr==NULL || linphone_core_set_primary_contact(lc,tmpstr)==-1) {
635 const char *hostname=NULL;
636 const char *username=NULL;
638 hostname=getenv("HOST");
639 username=getenv("USER");
640 if (hostname==NULL) hostname=getenv("HOSTNAME");
641 #endif /*HAVE_GETENV*/
643 hostname="unknown-host";
647 contact=ortp_strdup_printf("sip:%s@%s",username,hostname);
648 linphone_core_set_primary_contact(lc,contact);
652 tmp=lp_config_get_int(lc->config,"sip","guess_hostname",1);
653 linphone_core_set_guess_hostname(lc,tmp);
656 tmp=lp_config_get_int(lc->config,"sip","inc_timeout",15);
657 linphone_core_set_inc_timeout(lc,tmp);
659 /* get proxies config */
661 LinphoneProxyConfig *cfg=linphone_proxy_config_new_from_config_file(lc->config,i);
663 linphone_core_add_proxy_config(lc,cfg);
668 /* get the default proxy */
669 tmp=lp_config_get_int(lc->config,"sip","default_proxy",-1);
670 linphone_core_set_default_proxy_index(lc,tmp);
672 /* read authentication information */
674 LinphoneAuthInfo *ai=linphone_auth_info_new_from_config_file(lc->config,i);
676 linphone_core_add_auth_info(lc,ai);
677 linphone_auth_info_destroy(ai);
683 /*for tuning or test*/
684 lc->sip_conf.sdp_200_ack=lp_config_get_int(lc->config,"sip","sdp_200_ack",0);
685 lc->sip_conf.only_one_codec=lp_config_get_int(lc->config,"sip","only_one_codec",0);
686 lc->sip_conf.register_only_when_network_is_up=
687 lp_config_get_int(lc->config,"sip","register_only_when_network_is_up",1);
688 lc->sip_conf.ping_with_options=lp_config_get_int(lc->config,"sip","ping_with_options",1);
689 lc->sip_conf.auto_net_state_mon=lp_config_get_int(lc->config,"sip","auto_net_state_mon",1);
692 static void rtp_config_read(LinphoneCore *lc)
697 port=lp_config_get_int(lc->config,"rtp","audio_rtp_port",7078);
698 linphone_core_set_audio_port(lc,port);
700 port=lp_config_get_int(lc->config,"rtp","video_rtp_port",9078);
701 if (port==0) port=9078;
702 linphone_core_set_video_port(lc,port);
704 jitt_comp=lp_config_get_int(lc->config,"rtp","audio_jitt_comp",60);
705 linphone_core_set_audio_jittcomp(lc,jitt_comp);
706 jitt_comp=lp_config_get_int(lc->config,"rtp","video_jitt_comp",60);
707 nortp_timeout=lp_config_get_int(lc->config,"rtp","nortp_timeout",30);
708 linphone_core_set_nortp_timeout(lc,nortp_timeout);
711 static PayloadType * find_payload(RtpProfile *prof, const char *mime_type, int clock_rate, const char *recv_fmtp){
712 PayloadType *candidate=NULL;
716 it=rtp_profile_get_payload(prof,i);
717 if (it!=NULL && strcasecmp(mime_type,it->mime_type)==0
718 && (clock_rate==it->clock_rate || clock_rate<=0) ){
719 if ( (recv_fmtp && it->recv_fmtp && strcasecmp(recv_fmtp,it->recv_fmtp)==0) ||
720 (recv_fmtp==NULL && it->recv_fmtp==NULL) ){
729 static bool_t get_codec(LpConfig *config, char* type, int index, PayloadType **ret){
731 const char *mime,*fmtp;
736 snprintf(codeckey,50,"%s_%i",type,index);
737 mime=lp_config_get_string(config,codeckey,"mime",NULL);
738 if (mime==NULL || strlen(mime)==0 ) return FALSE;
740 rate=lp_config_get_int(config,codeckey,"rate",8000);
741 fmtp=lp_config_get_string(config,codeckey,"recv_fmtp",NULL);
742 enabled=lp_config_get_int(config,codeckey,"enabled",1);
743 pt=find_payload(&av_profile,mime,rate,fmtp);
744 if (pt && enabled ) pt->flags|=PAYLOAD_TYPE_ENABLED;
745 //ms_message("Found codec %s/%i",pt->mime_type,pt->clock_rate);
746 if (pt==NULL) ms_warning("Ignoring codec config %s/%i with fmtp=%s because unsupported",
747 mime,rate,fmtp ? fmtp : "");
752 static const char *codec_pref_order[]={
765 static int find_codec_rank(const char *mime){
767 for(i=0;codec_pref_order[i]!=NULL;++i){
768 if (strcasecmp(codec_pref_order[i],mime)==0)
774 static int codec_compare(const PayloadType *a, const PayloadType *b){
776 ra=find_codec_rank(a->mime_type);
777 rb=find_codec_rank(b->mime_type);
779 if (ra<rb) return -1;
783 static MSList *add_missing_codecs(SalStreamType mtype, MSList *l){
786 PayloadType *pt=rtp_profile_get_payload(&av_profile,i);
788 if (mtype==SalVideo && pt->type!=PAYLOAD_VIDEO)
790 else if (mtype==SalAudio && (pt->type!=PAYLOAD_AUDIO_PACKETIZED
791 && pt->type!=PAYLOAD_AUDIO_CONTINUOUS)){
794 if (pt && ms_filter_codec_supported(pt->mime_type)){
795 if (ms_list_find(l,pt)==NULL){
796 payload_type_set_flag(pt,PAYLOAD_TYPE_ENABLED);
797 ms_message("Adding new codec %s/%i with fmtp %s",
798 pt->mime_type,pt->clock_rate,pt->recv_fmtp ? pt->recv_fmtp : "");
799 l=ms_list_insert_sorted(l,pt,(int (*)(const void *, const void *))codec_compare);
807 static void codecs_config_read(LinphoneCore *lc)
811 MSList *audio_codecs=NULL;
812 MSList *video_codecs=NULL;
813 for (i=0;get_codec(lc->config,"audio_codec",i,&pt);i++){
815 if (!ms_filter_codec_supported(pt->mime_type)){
816 ms_warning("Codec %s is not supported by mediastreamer2, removed.",pt->mime_type);
817 }else audio_codecs=ms_list_append(audio_codecs,pt);
820 audio_codecs=add_missing_codecs(SalAudio,audio_codecs);
821 for (i=0;get_codec(lc->config,"video_codec",i,&pt);i++){
823 if (!ms_filter_codec_supported(pt->mime_type)){
824 ms_warning("Codec %s is not supported by mediastreamer2, removed.",pt->mime_type);
825 }else video_codecs=ms_list_append(video_codecs,(void *)pt);
828 video_codecs=add_missing_codecs(SalVideo,video_codecs);
829 linphone_core_set_audio_codecs(lc,audio_codecs);
830 linphone_core_set_video_codecs(lc,video_codecs);
831 linphone_core_update_allocated_audio_bandwidth(lc);
834 static void video_config_read(LinphoneCore *lc){
835 int capture, display, self_view;
839 const char **devices;
843 /* retrieve all video devices */
844 elem=ms_web_cam_manager_get_list(ms_web_cam_manager_get());
845 ndev=ms_list_size(elem);
846 devices=ms_malloc((ndev+1)*sizeof(const char *));
847 for (i=0;elem!=NULL;elem=elem->next,i++){
848 devices[i]=ms_web_cam_get_string_id((MSWebCam *)elem->data);
851 lc->video_conf.cams=devices;
853 str=lp_config_get_string(lc->config,"video","device",NULL);
854 if (str && str[0]==0) str=NULL;
855 linphone_core_set_video_device(lc,str);
857 linphone_core_set_preferred_video_size_by_name(lc,
858 lp_config_get_string(lc->config,"video","size","cif"));
860 enabled=lp_config_get_int(lc->config,"video","enabled",1);
861 capture=lp_config_get_int(lc->config,"video","capture",enabled);
862 display=lp_config_get_int(lc->config,"video","display",enabled);
863 self_view=lp_config_get_int(lc->config,"video","self_view",enabled);
865 linphone_core_enable_video(lc,capture,display);
866 linphone_core_enable_self_view(lc,self_view);
870 static void ui_config_read(LinphoneCore *lc)
874 for (i=0;(lf=linphone_friend_new_from_config_file(lc,i))!=NULL;i++){
875 linphone_core_add_friend(lc,lf);
877 call_logs_read_from_config_file(lc);
881 static void autoreplier_config_init(LinphoneCore *lc)
883 autoreplier_config_t *config=&lc->autoreplier_conf;
884 config->enabled=lp_config_get_int(lc->config,"autoreplier","enabled",0);
885 config->after_seconds=lp_config_get_int(lc->config,"autoreplier","after_seconds",6);
886 config->max_users=lp_config_get_int(lc->config,"autoreplier","max_users",1);
887 config->max_rec_time=lp_config_get_int(lc->config,"autoreplier","max_rec_time",60);
888 config->max_rec_msg=lp_config_get_int(lc->config,"autoreplier","max_rec_msg",10);
889 config->message=lp_config_get_string(lc->config,"autoreplier","message",NULL);
894 * Sets maximum available download bandwidth
896 * @ingroup media_parameters
898 * This is IP bandwidth, in kbit/s.
899 * This information is used signaled to other parties during
900 * calls (within SDP messages) so that the remote end can have
901 * sufficient knowledge to properly configure its audio & video
902 * codec output bitrate to not overflow available bandwidth.
904 * @param lc the LinphoneCore object
905 * @param bw the bandwidth in kbits/s, 0 for infinite
907 void linphone_core_set_download_bandwidth(LinphoneCore *lc, int bw){
908 lc->net_conf.download_bw=bw;
909 if (bw==0){ /*infinite*/
913 lc->dw_audio_bw=MIN(lc->audio_bw,bw);
914 lc->dw_video_bw=MAX(bw-lc->dw_audio_bw-10,0);/*-10: security margin*/
919 * Sets maximum available upload bandwidth
921 * @ingroup media_parameters
923 * This is IP bandwidth, in kbit/s.
924 * This information is used by liblinphone together with remote
925 * side available bandwidth signaled in SDP messages to properly
926 * configure audio & video codec's output bitrate.
928 * @param lc the LinphoneCore object
929 * @param bw the bandwidth in kbits/s, 0 for infinite
931 void linphone_core_set_upload_bandwidth(LinphoneCore *lc, int bw){
932 lc->net_conf.upload_bw=bw;
933 if (bw==0){ /*infinite*/
937 lc->up_audio_bw=MIN(lc->audio_bw,bw);
938 lc->up_video_bw=MAX(bw-lc->up_audio_bw-10,0);/*-10: security margin*/
943 * Retrieve the maximum available download bandwidth.
945 * @ingroup media_parameters
947 * This value was set by linphone_core_set_download_bandwidth().
950 int linphone_core_get_download_bandwidth(const LinphoneCore *lc){
951 return lc->net_conf.download_bw;
955 * Retrieve the maximum available upload bandwidth.
957 * @ingroup media_parameters
959 * This value was set by linphone_core_set_upload_bandwidth().
962 int linphone_core_get_upload_bandwidth(const LinphoneCore *lc){
963 return lc->net_conf.upload_bw;
966 * set audio packetization time linphone expect to received from peer
968 void linphone_core_set_download_ptime(LinphoneCore *lc, int ptime) {
969 lc->net_conf.down_ptime=ptime;
972 int linphone_core_get_download_ptime(LinphoneCore *lc) {
973 return lc->net_conf.down_ptime;
978 * Returns liblinphone's version as a string.
983 const char * linphone_core_get_version(void){
984 return liblinphone_version;
988 static MSList *linphone_payload_types=NULL;
990 static void linphone_core_assign_payload_type(PayloadType *const_pt, int number, const char *recv_fmtp){
992 pt=payload_type_clone(const_pt);
993 payload_type_set_number(pt,number);
994 if (recv_fmtp!=NULL) payload_type_set_recv_fmtp(pt,recv_fmtp);
995 rtp_profile_set_payload(&av_profile,number,pt);
996 linphone_payload_types=ms_list_append(linphone_payload_types,pt);
999 static void linphone_core_free_payload_types(void){
1000 ms_list_for_each(linphone_payload_types,(void (*)(void*))payload_type_destroy);
1001 ms_list_free(linphone_payload_types);
1002 linphone_payload_types=NULL;
1005 static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vtable, const char *config_path,
1006 const char *factory_config_path, void * userdata)
1008 memset (lc, 0, sizeof (LinphoneCore));
1011 memcpy(&lc->vtable,vtable,sizeof(LinphoneCoreVTable));
1013 gstate_initialize(lc);
1014 gstate_new_state(lc, GSTATE_POWER_STARTUP, NULL);
1017 linphone_core_assign_payload_type(&payload_type_pcmu8000,0,NULL);
1018 linphone_core_assign_payload_type(&payload_type_gsm,3,NULL);
1019 linphone_core_assign_payload_type(&payload_type_pcma8000,8,NULL);
1020 linphone_core_assign_payload_type(&payload_type_lpc1015,115,NULL);
1021 linphone_core_assign_payload_type(&payload_type_speex_nb,110,"vbr=on");
1022 linphone_core_assign_payload_type(&payload_type_speex_wb,111,"vbr=on");
1023 linphone_core_assign_payload_type(&payload_type_speex_uwb,112,"vbr=on");
1024 linphone_core_assign_payload_type(&payload_type_telephone_event,101,"0-11");
1025 linphone_core_assign_payload_type(&payload_type_ilbc,113,"mode=30");
1027 #ifdef ENABLE_NONSTANDARD_GSM
1030 pt=payload_type_clone(&payload_type_gsm);
1031 pt->clock_rate=11025;
1032 rtp_profile_set_payload(&av_profile,114,pt);
1033 linphone_payload_types=ms_list_append(linphone_payload_types,pt);
1034 pt=payload_type_clone(&payload_type_gsm);
1035 pt->clock_rate=22050;
1036 rtp_profile_set_payload(&av_profile,115,pt);
1037 linphone_payload_types=ms_list_append(linphone_payload_types,pt);
1041 #ifdef VIDEO_ENABLED
1042 linphone_core_assign_payload_type(&payload_type_h263,34,NULL);
1043 linphone_core_assign_payload_type(&payload_type_theora,97,NULL);
1044 linphone_core_assign_payload_type(&payload_type_h263_1998,98,"CIF=1;QCIF=1");
1045 linphone_core_assign_payload_type(&payload_type_mp4v,99,"profile-level-id=3");
1046 linphone_core_assign_payload_type(&payload_type_x_snow,100,NULL);
1047 linphone_core_assign_payload_type(&payload_type_h264,102,NULL);
1048 linphone_core_assign_payload_type(&payload_type_h264,103,"packetization-mode=1");
1053 lc->config=lp_config_new(config_path);
1054 if (factory_config_path)
1055 lp_config_read_file(lc->config,factory_config_path);
1058 sal_set_user_pointer(lc->sal,lc);
1059 sal_set_callbacks(lc->sal,&linphone_sal_callbacks);
1060 if (lp_config_get_int(lc->config,"sip","use_session_timers",0)==1){
1061 sal_use_session_timers(lc->sal,200);
1063 sip_setup_register_all();
1064 sound_config_read(lc);
1065 net_config_read(lc);
1066 rtp_config_read(lc);
1067 codecs_config_read(lc);
1068 sip_config_read(lc); /* this will start eXosip*/
1069 video_config_read(lc);
1070 //autoreplier_config_init(&lc->autoreplier_conf);
1071 lc->prev_mode=LINPHONE_STATUS_ONLINE;
1072 lc->presence_mode=LINPHONE_STATUS_ONLINE;
1073 lc->max_call_logs=15;
1075 lc->vtable.display_status(lc,_("Ready"));
1076 gstate_new_state(lc, GSTATE_POWER_ON, NULL);
1077 lc->auto_net_state_mon=lc->sip_conf.auto_net_state_mon;
1083 * Instanciates a LinphoneCore object.
1084 * @ingroup initializing
1086 * The LinphoneCore object is the primary handle for doing all phone actions.
1087 * It should be unique within your application.
1088 * @param vtable a LinphoneCoreVTable structure holding your application callbacks
1089 * @param config_path a path to a config file. If it does not exists it will be created.
1090 * The config file is used to store all user settings, call logs, friends, proxies...
1091 * @param factory_config_path a path to a read-only config file that can be used to
1092 * to store hard-coded preference such as proxy settings or internal preferences.
1093 * The settings in this factory file always override the one in the normal config file.
1094 * It is OPTIONAL, use NULL if unneeded.
1095 * @param userdata an opaque user pointer that can be retrieved at any time (for example in
1096 * callbacks) using linphone_core_get_user_data().
1099 LinphoneCore *linphone_core_new(const LinphoneCoreVTable *vtable,
1100 const char *config_path, const char *factory_config_path, void * userdata)
1102 LinphoneCore *core=ms_new(LinphoneCore,1);
1103 linphone_core_init(core,vtable,config_path, factory_config_path, userdata);
1108 * Returns the list of available audio codecs.
1110 * This list is unmodifiable. The ->data field of the MSList points a PayloadType
1111 * structure holding the codec information.
1112 * It is possible to make copy of the list with ms_list_copy() in order to modify it
1113 * (such as the order of codecs).
1115 const MSList *linphone_core_get_audio_codecs(const LinphoneCore *lc)
1117 return lc->codecs_conf.audio_codecs;
1121 * Returns the list of available video codecs.
1123 * This list is unmodifiable. The ->data field of the MSList points a PayloadType
1124 * structure holding the codec information.
1125 * It is possible to make copy of the list with ms_list_copy() in order to modify it
1126 * (such as the order of codecs).
1128 const MSList *linphone_core_get_video_codecs(const LinphoneCore *lc)
1130 return lc->codecs_conf.video_codecs;
1134 * Sets the local "from" identity.
1137 * This data is used in absence of any proxy configuration or when no
1138 * default proxy configuration is set. See LinphoneProxyConfig
1140 int linphone_core_set_primary_contact(LinphoneCore *lc, const char *contact)
1142 LinphoneAddress *ctt;
1144 if ((ctt=linphone_address_new(contact))==0) {
1145 ms_error("Bad contact url: %s",contact);
1148 if (lc->sip_conf.contact!=NULL) ms_free(lc->sip_conf.contact);
1149 lc->sip_conf.contact=ms_strdup(contact);
1150 if (lc->sip_conf.guessed_contact!=NULL){
1151 ms_free(lc->sip_conf.guessed_contact);
1152 lc->sip_conf.guessed_contact=NULL;
1154 linphone_address_destroy(ctt);
1159 /*result must be an array of chars at least LINPHONE_IPADDR_SIZE */
1160 void linphone_core_get_local_ip(LinphoneCore *lc, const char *dest, char *result){
1161 if (linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_NAT_ADDRESS){
1162 strncpy(result,linphone_core_get_nat_address(lc),LINPHONE_IPADDR_SIZE);
1165 if (linphone_core_get_local_ip_for(lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,dest,result)==0)
1167 /*else fallback to SAL routine that will attempt to find the most realistic interface */
1168 sal_get_default_local_ip(lc->sal,lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,result,LINPHONE_IPADDR_SIZE);
1171 static void update_primary_contact(LinphoneCore *lc){
1173 char tmp[LINPHONE_IPADDR_SIZE];
1175 LinphoneAddress *url;
1176 if (lc->sip_conf.guessed_contact!=NULL){
1177 ms_free(lc->sip_conf.guessed_contact);
1178 lc->sip_conf.guessed_contact=NULL;
1180 url=linphone_address_new(lc->sip_conf.contact);
1182 ms_error("Could not parse identity contact !");
1183 url=linphone_address_new("sip:unknown@unkwownhost");
1185 linphone_core_get_local_ip(lc, NULL, tmp);
1186 if (strcmp(tmp,"127.0.0.1")==0 || strcmp(tmp,"::1")==0 ){
1187 ms_warning("Local loopback network only !");
1188 lc->sip_conf.loopback_only=TRUE;
1189 }else lc->sip_conf.loopback_only=FALSE;
1190 linphone_address_set_domain(url,tmp);
1191 linphone_address_set_port_int(url,lc->sip_conf.sip_port);
1192 guessed=linphone_address_as_string(url);
1193 lc->sip_conf.guessed_contact=guessed;
1194 linphone_address_destroy(url);
1198 * Returns the default identity when no proxy configuration is used.
1202 const char *linphone_core_get_primary_contact(LinphoneCore *lc){
1205 if (lc->sip_conf.guess_hostname){
1206 if (lc->sip_conf.guessed_contact==NULL || lc->sip_conf.loopback_only){
1207 update_primary_contact(lc);
1209 identity=lc->sip_conf.guessed_contact;
1211 identity=lc->sip_conf.contact;
1217 * Tells LinphoneCore to guess local hostname automatically in primary contact.
1221 void linphone_core_set_guess_hostname(LinphoneCore *lc, bool_t val){
1222 lc->sip_conf.guess_hostname=val;
1226 * Returns TRUE if hostname part of primary contact is guessed automatically.
1230 bool_t linphone_core_get_guess_hostname(LinphoneCore *lc){
1231 return lc->sip_conf.guess_hostname;
1235 * Same as linphone_core_get_primary_contact() but the result is a LinphoneAddress object
1236 * instead of const char*
1240 LinphoneAddress *linphone_core_get_primary_contact_parsed(LinphoneCore *lc){
1241 return linphone_address_new(linphone_core_get_primary_contact(lc));
1245 * Sets the list of audio codecs.
1247 * @ingroup media_parameters
1248 * The list is taken by the LinphoneCore thus the application should not free it.
1249 * This list is made of struct PayloadType describing the codec parameters.
1251 int linphone_core_set_audio_codecs(LinphoneCore *lc, MSList *codecs)
1253 if (lc->codecs_conf.audio_codecs!=NULL) ms_list_free(lc->codecs_conf.audio_codecs);
1254 lc->codecs_conf.audio_codecs=codecs;
1259 * Sets the list of video codecs.
1261 * @ingroup media_parameters
1262 * The list is taken by the LinphoneCore thus the application should not free it.
1263 * This list is made of struct PayloadType describing the codec parameters.
1265 int linphone_core_set_video_codecs(LinphoneCore *lc, MSList *codecs)
1267 if (lc->codecs_conf.video_codecs!=NULL) ms_list_free(lc->codecs_conf.video_codecs);
1268 lc->codecs_conf.video_codecs=codecs;
1272 const MSList * linphone_core_get_friend_list(const LinphoneCore *lc)
1278 * Returns the nominal jitter buffer size in milliseconds.
1280 * @ingroup media_parameters
1282 int linphone_core_get_audio_jittcomp(LinphoneCore *lc)
1284 return lc->rtp_conf.audio_jitt_comp;
1288 * Returns the UDP port used for audio streaming.
1290 * @ingroup network_parameters
1292 int linphone_core_get_audio_port(const LinphoneCore *lc)
1294 return lc->rtp_conf.audio_rtp_port;
1298 * Returns the UDP port used for video streaming.
1300 * @ingroup network_parameters
1302 int linphone_core_get_video_port(const LinphoneCore *lc){
1303 return lc->rtp_conf.video_rtp_port;
1308 * Returns the value in seconds of the no-rtp timeout.
1310 * @ingroup media_parameters
1311 * When no RTP or RTCP packets have been received for a while
1312 * LinphoneCore will consider the call is broken (remote end crashed or
1313 * disconnected from the network), and thus will terminate the call.
1314 * The no-rtp timeout is the duration above which the call is considered broken.
1316 int linphone_core_get_nortp_timeout(const LinphoneCore *lc){
1317 return lc->rtp_conf.nortp_timeout;
1321 * Sets the nominal audio jitter buffer size in milliseconds.
1323 * @ingroup media_parameters
1325 void linphone_core_set_audio_jittcomp(LinphoneCore *lc, int value)
1327 lc->rtp_conf.audio_jitt_comp=value;
1331 * Sets the UDP port used for audio streaming.
1333 * @ingroup network_parameters
1335 void linphone_core_set_audio_port(LinphoneCore *lc, int port)
1337 lc->rtp_conf.audio_rtp_port=port;
1341 * Sets the UDP port used for video streaming.
1343 * @ingroup network_parameters
1345 void linphone_core_set_video_port(LinphoneCore *lc, int port){
1346 lc->rtp_conf.video_rtp_port=port;
1350 * Sets the no-rtp timeout value in seconds.
1352 * @ingroup media_parameters
1353 * See linphone_core_get_nortp_timeout() for details.
1355 void linphone_core_set_nortp_timeout(LinphoneCore *lc, int nortp_timeout){
1356 lc->rtp_conf.nortp_timeout=nortp_timeout;
1360 * Indicates whether SIP INFO is used for sending digits.
1362 * @ingroup media_parameters
1364 bool_t linphone_core_get_use_info_for_dtmf(LinphoneCore *lc)
1366 return lc->sip_conf.use_info;
1370 * Sets whether SIP INFO is to be used for sending digits.
1372 * @ingroup media_parameters
1374 void linphone_core_set_use_info_for_dtmf(LinphoneCore *lc,bool_t use_info)
1376 lc->sip_conf.use_info=use_info;
1380 * Indicates whether RFC2833 is used for sending digits.
1382 * @ingroup media_parameters
1384 bool_t linphone_core_get_use_rfc2833_for_dtmf(LinphoneCore *lc)
1386 return lc->sip_conf.use_rfc2833;
1390 * Sets whether RFC2833 is to be used for sending digits.
1392 * @ingroup media_parameters
1394 void linphone_core_set_use_rfc2833_for_dtmf(LinphoneCore *lc,bool_t use_rfc2833)
1396 lc->sip_conf.use_rfc2833=use_rfc2833;
1400 * Returns the UDP port used by SIP.
1402 * @ingroup network_parameters
1404 int linphone_core_get_sip_port(LinphoneCore *lc)
1406 return lc->sip_conf.sip_port;
1409 static char _ua_name[64]="Linphone";
1410 static char _ua_version[64]=LINPHONE_VERSION;
1412 #ifdef HAVE_EXOSIP_GET_VERSION
1413 extern const char *eXosip_get_version();
1416 static void apply_user_agent(LinphoneCore *lc){
1417 char ua_string[256];
1418 snprintf(ua_string,sizeof(ua_string)-1,"%s/%s (eXosip2/%s)",_ua_name,_ua_version,
1419 #ifdef HAVE_EXOSIP_GET_VERSION
1420 eXosip_get_version()
1425 if (lc->sal) sal_set_user_agent(lc->sal,ua_string);
1429 * Sets the user agent string used in SIP messages.
1433 void linphone_core_set_user_agent(const char *name, const char *ver){
1434 strncpy(_ua_name,name,sizeof(_ua_name)-1);
1435 strncpy(_ua_version,ver,sizeof(_ua_version));
1439 * Sets the UDP port to be used by SIP.
1441 * @ingroup network_parameters
1443 void linphone_core_set_sip_port(LinphoneCore *lc,int port)
1445 const char *anyaddr;
1447 if (port==lc->sip_conf.sip_port) return;
1448 lc->sip_conf.sip_port=port;
1450 if (lc->sal==NULL) return;
1452 if (lc->sip_conf.ipv6_enabled)
1456 err=sal_listen_port (lc->sal,anyaddr,port, SalTransportDatagram,FALSE);
1458 char *msg=ortp_strdup_printf("UDP port %i seems already in use ! Cannot initialize.",port);
1460 lc->vtable.display_warning(lc,msg);
1464 apply_user_agent(lc);
1468 * Returns TRUE if IPv6 is enabled.
1470 * @ingroup network_parameters
1471 * See linphone_core_enable_ipv6() for more details on how IPv6 is supported in liblinphone.
1473 bool_t linphone_core_ipv6_enabled(LinphoneCore *lc){
1474 return lc->sip_conf.ipv6_enabled;
1478 * Turns IPv6 support on or off.
1480 * @ingroup network_parameters
1482 * @note IPv6 support is exclusive with IPv4 in liblinphone:
1483 * when IPv6 is turned on, IPv4 calls won't be possible anymore.
1484 * By default IPv6 support is off.
1486 void linphone_core_enable_ipv6(LinphoneCore *lc, bool_t val){
1487 if (lc->sip_conf.ipv6_enabled!=val){
1488 lc->sip_conf.ipv6_enabled=val;
1490 /* we need to restart eXosip */
1491 linphone_core_set_sip_port(lc, lc->sip_conf.sip_port);
1496 static void display_bandwidth(RtpSession *as, RtpSession *vs){
1497 ms_message("bandwidth usage: audio=[d=%.1f,u=%.1f] video=[d=%.1f,u=%.1f] kbit/sec",
1498 (as!=NULL) ? (rtp_session_compute_recv_bandwidth(as)*1e-3) : 0,
1499 (as!=NULL) ? (rtp_session_compute_send_bandwidth(as)*1e-3) : 0,
1500 (vs!=NULL) ? (rtp_session_compute_recv_bandwidth(vs)*1e-3) : 0,
1501 (vs!=NULL) ? (rtp_session_compute_send_bandwidth(vs)*1e-3) : 0);
1504 static void linphone_core_disconnected(LinphoneCore *lc){
1505 lc->vtable.display_warning(lc,_("Remote end seems to have disconnected, the call is going to be closed."));
1506 linphone_core_terminate_call(lc,NULL);
1509 static void monitor_network_state(LinphoneCore *lc, time_t curtime){
1510 static time_t last_check=0;
1511 static bool_t last_status=FALSE;
1512 char result[LINPHONE_IPADDR_SIZE];
1513 bool_t new_status=last_status;
1515 /* only do the network up checking every five seconds */
1516 if (last_check==0 || (curtime-last_check)>=5){
1517 linphone_core_get_local_ip_for(lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,NULL,result);
1518 if (strcmp(result,"::1")!=0 && strcmp(result,"127.0.0.1")!=0){
1520 }else new_status=FALSE;
1522 if (new_status!=last_status) {
1524 ms_message("New local ip address is %s",result);
1526 set_network_reachable(lc,new_status);
1527 last_status=new_status;
1532 static void proxy_update(LinphoneCore *lc){
1533 ms_list_for_each(lc->sip_conf.proxies,(void (*)(void*))&linphone_proxy_config_update);
1536 static void assign_buddy_info(LinphoneCore *lc, BuddyInfo *info){
1537 LinphoneFriend *lf=linphone_core_get_friend_by_address(lc,info->sip_uri);
1540 ms_message("%s has a BuddyInfo assigned with image %p",info->sip_uri, info->image_data);
1541 if (lc->vtable.buddy_info_updated)
1542 lc->vtable.buddy_info_updated(lc,lf);
1544 ms_warning("Could not any friend with uri %s",info->sip_uri);
1548 static void analyze_buddy_lookup_results(LinphoneCore *lc, LinphoneProxyConfig *cfg){
1550 SipSetupContext *ctx=linphone_proxy_config_get_sip_setup_context(cfg);
1551 for (elem=lc->bl_reqs;elem!=NULL;elem=ms_list_next(elem)){
1552 BuddyLookupRequest *req=(BuddyLookupRequest *)elem->data;
1553 if (req->status==BuddyLookupDone || req->status==BuddyLookupFailure){
1554 if (req->results!=NULL){
1555 BuddyInfo *i=(BuddyInfo*)req->results->data;
1556 ms_list_free(req->results);
1558 assign_buddy_info(lc,i);
1560 sip_setup_context_buddy_lookup_free(ctx,req);
1564 /*purge completed requests */
1565 while((elem=ms_list_find(lc->bl_reqs,NULL))!=NULL){
1566 lc->bl_reqs=ms_list_remove_link(lc->bl_reqs,elem);
1570 static void linphone_core_grab_buddy_infos(LinphoneCore *lc, LinphoneProxyConfig *cfg){
1572 SipSetupContext *ctx=linphone_proxy_config_get_sip_setup_context(cfg);
1573 for(elem=linphone_core_get_friend_list(lc);elem!=NULL;elem=elem->next){
1574 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
1575 if (lf->info==NULL){
1576 if (linphone_core_lookup_known_proxy(lc,lf->uri)==cfg){
1577 if (linphone_address_get_username(lf->uri)!=NULL){
1578 BuddyLookupRequest *req;
1579 char *tmp=linphone_address_as_string_uri_only(lf->uri);
1580 req=sip_setup_context_create_buddy_lookup_request(ctx);
1581 buddy_lookup_request_set_key(req,tmp);
1582 buddy_lookup_request_set_max_results(req,1);
1583 sip_setup_context_buddy_lookup_submit(ctx,req);
1584 lc->bl_reqs=ms_list_append(lc->bl_reqs,req);
1592 static void linphone_core_do_plugin_tasks(LinphoneCore *lc){
1593 LinphoneProxyConfig *cfg=NULL;
1594 linphone_core_get_default_proxy(lc,&cfg);
1596 if (lc->bl_refresh){
1597 SipSetupContext *ctx=linphone_proxy_config_get_sip_setup_context(cfg);
1598 if (ctx && (sip_setup_context_get_capabilities(ctx) & SIP_SETUP_CAP_BUDDY_LOOKUP)){
1599 linphone_core_grab_buddy_infos(lc,cfg);
1600 lc->bl_refresh=FALSE;
1603 if (lc->bl_reqs) analyze_buddy_lookup_results(lc,cfg);
1608 * Main loop function. It is crucial that your application call it periodically.
1610 * @ingroup initializing
1611 * linphone_core_iterate() performs various backgrounds tasks:
1612 * - receiving of SIP messages
1613 * - handles timers and timeout
1614 * - performs registration to proxies
1615 * - authentication retries
1616 * The application MUST call this function from periodically, in its main loop.
1617 * Be careful that this function must be call from the same thread as
1618 * other liblinphone methods. In not the case make sure all liblinphone calls are
1619 * serialized with a mutex.
1621 void linphone_core_iterate(LinphoneCore *lc){
1622 int disconnect_timeout = linphone_core_get_nortp_timeout(lc);
1623 time_t curtime=time(NULL);
1625 bool_t one_second_elapsed=FALSE;
1626 bool_t disconnected=FALSE;
1628 if (curtime-lc->prevtime>=1){
1629 lc->prevtime=curtime;
1630 one_second_elapsed=TRUE;
1633 if (lc->preview_finished){
1634 lc->preview_finished=0;
1635 ring_stop(lc->ringstream);
1636 lc->ringstream=NULL;
1637 lc_callback_obj_invoke(&lc->preview_finished_cb,lc);
1640 sal_iterate(lc->sal);
1641 if (lc->auto_net_state_mon) monitor_network_state(lc,curtime);
1645 if (lc->call!=NULL){
1646 LinphoneCall *call=lc->call;
1647 if (call->state==LCStatePreEstablishing && (curtime-call->start_time>=2)){
1648 /*start the call even if the OPTIONS reply did not arrive*/
1649 linphone_core_start_invite(lc,call,NULL);
1651 if (call->dir==LinphoneCallIncoming && call->state==LCStateRinging){
1652 elapsed=curtime-call->start_time;
1653 ms_message("incoming call ringing for %i seconds",elapsed);
1654 if (elapsed>lc->sip_conf.inc_timeout){
1655 linphone_core_terminate_call(lc,NULL);
1657 }else if (call->state==LCStateAVRunning){
1658 if (one_second_elapsed){
1659 RtpSession *as=NULL,*vs=NULL;
1660 lc->prevtime=curtime;
1661 if (lc->audiostream!=NULL)
1662 as=lc->audiostream->session;
1663 if (lc->videostream!=NULL)
1664 vs=lc->videostream->session;
1665 display_bandwidth(as,vs);
1667 #ifdef VIDEO_ENABLED
1668 if (lc->videostream!=NULL)
1669 video_stream_iterate(lc->videostream);
1671 if (lc->audiostream!=NULL && disconnect_timeout>0)
1672 disconnected=!audio_stream_alive(lc->audiostream,disconnect_timeout);
1675 if (linphone_core_video_preview_enabled(lc)){
1676 if (lc->previewstream==NULL)
1677 toggle_video_preview(lc,TRUE);
1678 #ifdef VIDEO_ENABLED
1679 else video_stream_iterate(lc->previewstream);
1682 if (lc->previewstream!=NULL)
1683 toggle_video_preview(lc,FALSE);
1686 linphone_core_disconnected(lc);
1688 linphone_core_do_plugin_tasks(lc);
1690 if (one_second_elapsed && lp_config_needs_commit(lc->config)){
1691 lp_config_sync(lc->config);
1696 * Interpret a call destination as supplied by the user, and returns a fully qualified
1699 * A sip address should look like DisplayName <sip:username@domain:port> .
1700 * Basically this function performs the following tasks
1701 * - if a phone number is entered, prepend country prefix of the default proxy
1702 * configuration, eventually escape the '+' by 00.
1703 * - if no domain part is supplied, append the domain name of the default proxy
1704 * - if no sip: is present, prepend it
1706 * The result is a syntaxically correct SIP address.
1709 LinphoneAddress * linphone_core_interpret_url(LinphoneCore *lc, const char *url){
1710 enum_lookup_res_t *enumres=NULL;
1711 char *enum_domain=NULL;
1712 LinphoneProxyConfig *proxy=lc->default_proxy;;
1714 LinphoneAddress *uri;
1716 if (is_enum(url,&enum_domain)){
1717 lc->vtable.display_status(lc,_("Looking for telephone number destination..."));
1718 if (enum_lookup(enum_domain,&enumres)<0){
1719 lc->vtable.display_status(lc,_("Could not resolve this number."));
1720 ms_free(enum_domain);
1723 ms_free(enum_domain);
1724 tmpurl=enumres->sip_address[0];
1725 uri=linphone_address_new(tmpurl);
1726 enum_lookup_res_free(enumres);
1729 /* check if we have a "sip:" */
1730 if (strstr(url,"sip:")==NULL){
1731 /* this doesn't look like a true sip uri */
1732 if (strchr(url,'@')!=NULL){
1733 /* seems like sip: is missing !*/
1734 tmpurl=ms_strdup_printf("sip:%s",url);
1735 uri=linphone_address_new(tmpurl);
1743 /* append the proxy domain suffix */
1744 const char *identity=linphone_proxy_config_get_identity(proxy);
1745 char normalized_username[128];
1746 uri=linphone_address_new(identity);
1750 linphone_address_set_display_name(uri,NULL);
1751 linphone_proxy_config_normalize_number(proxy,url,normalized_username,
1752 sizeof(normalized_username));
1753 linphone_address_set_username(uri,normalized_username);
1757 uri=linphone_address_new(url);
1761 /* else we could not do anything with url given by user, so display an error */
1762 if (lc->vtable.display_warning!=NULL){
1763 lc->vtable.display_warning(lc,_("Could not parse given sip address. A sip url usually looks like sip:user@domain"));
1769 * Returns the default identity SIP address.
1772 * This is an helper function:
1774 * If no default proxy is set, this will return the primary contact (
1775 * see linphone_core_get_primary_contact() ). If a default proxy is set
1776 * it returns the registered identity on the proxy.
1778 const char * linphone_core_get_identity(LinphoneCore *lc){
1779 LinphoneProxyConfig *proxy=NULL;
1781 linphone_core_get_default_proxy(lc,&proxy);
1783 from=linphone_proxy_config_get_identity(proxy);
1784 }else from=linphone_core_get_primary_contact(lc);
1788 const char * linphone_core_get_route(LinphoneCore *lc){
1789 LinphoneProxyConfig *proxy=NULL;
1790 const char *route=NULL;
1791 linphone_core_get_default_proxy(lc,&proxy);
1793 route=linphone_proxy_config_get_route(proxy);
1798 bool_t linphone_core_is_in_communication_with(LinphoneCore *lc, const char *to)
1802 const LinphoneAddress *la=linphone_core_get_remote_uri(lc);
1807 tmp = linphone_address_as_string(la);
1817 LinphoneProxyConfig * linphone_core_lookup_known_proxy(LinphoneCore *lc, const LinphoneAddress *uri){
1819 LinphoneProxyConfig *found_cfg=NULL;
1820 for (elem=linphone_core_get_proxy_config_list(lc);elem!=NULL;elem=elem->next){
1821 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
1822 const char *domain=linphone_proxy_config_get_domain(cfg);
1823 if (domain!=NULL && strcmp(domain,linphone_address_get_domain(uri))==0){
1831 static char *get_fixed_contact(LinphoneCore *lc, LinphoneCall *call , LinphoneProxyConfig *dest_proxy){
1832 LinphoneAddress *ctt;
1833 const char *localip=call->localip;
1835 /* first use user's supplied ip address if asked*/
1836 if (linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_NAT_ADDRESS){
1837 ctt=linphone_core_get_primary_contact_parsed(lc);
1838 return ms_strdup_printf("sip:%s@%s",linphone_address_get_username(ctt),
1839 linphone_core_get_nat_address(lc));
1842 /* if already choosed, don't change it */
1843 if (call->op && sal_op_get_contact(call->op)!=NULL){
1846 /*if using a proxy, use the contact address as guessed with the REGISTERs*/
1847 if (dest_proxy && dest_proxy->op){
1848 const char *fixed_contact=sal_op_get_contact(dest_proxy->op);
1849 if (fixed_contact) {
1850 ms_message("Contact has been fixed using proxy to %s",fixed_contact);
1851 return ms_strdup(fixed_contact);
1854 /* if the ping OPTIONS request succeeded use the contact guessed from the
1857 const char *guessed=sal_op_get_contact(call->ping_op);
1859 ms_message("Contact has been fixed using OPTIONS to %s",guessed);
1860 return ms_strdup(guessed);
1864 ctt=linphone_core_get_primary_contact_parsed(lc);
1868 /*otherwise use supllied localip*/
1869 linphone_address_set_domain(ctt,localip);
1870 linphone_address_set_port_int(ctt,linphone_core_get_sip_port(lc));
1871 ret=linphone_address_as_string_uri_only(ctt);
1872 linphone_address_destroy(ctt);
1873 ms_message("Contact has been fixed using local ip to %s",ret);
1879 int linphone_core_start_invite(LinphoneCore *lc, LinphoneCall *call, LinphoneProxyConfig *dest_proxy){
1882 char *real_url,*barmsg;
1884 /*try to be best-effort in giving real local or routable contact address */
1885 contact=get_fixed_contact(lc,call,dest_proxy);
1887 sal_op_set_contact(call->op, contact);
1890 call->state=LCStateInit;
1891 linphone_core_init_media_streams(lc,lc->call);
1892 if (!lc->sip_conf.sdp_200_ack){
1893 call->media_pending=TRUE;
1894 sal_call_set_local_media_description(call->op,call->localdesc);
1896 real_url=linphone_address_as_string(call->log->to);
1897 from=linphone_address_as_string(call->log->from);
1898 err=sal_call(call->op,from,real_url);
1900 if (lc->sip_conf.sdp_200_ack){
1901 call->media_pending=TRUE;
1902 sal_call_set_local_media_description(call->op,call->localdesc);
1904 barmsg=ortp_strdup_printf("%s %s", _("Contacting"), real_url);
1905 lc->vtable.display_status(lc,barmsg);
1909 ms_warning("Could not initiate call.");
1910 lc->vtable.display_status(lc,_("could not call"));
1911 linphone_core_stop_media_streams(lc,call);
1912 linphone_call_destroy(call);
1914 }else gstate_new_state(lc, GSTATE_CALL_OUT_INVITE, real_url);
1921 * Initiates an outgoing call
1923 * @ingroup call_control
1924 * @param lc the LinphoneCore object
1925 * @param url the destination of the call (sip address, or phone number).
1927 int linphone_core_invite(LinphoneCore *lc, const char *url){
1928 LinphoneAddress *addr=linphone_core_interpret_url(lc,url);
1931 err=linphone_core_invite_address(lc,addr);
1932 linphone_address_destroy(addr);
1939 * Initiates an outgoing call given a destination LinphoneAddress
1941 * @ingroup call_control
1942 * @param lc the LinphoneCore object
1943 * @param url the destination of the call (sip address).
1945 int linphone_core_invite_address(LinphoneCore *lc, const LinphoneAddress *real_parsed_url)
1948 const char *route=NULL;
1949 const char *from=NULL;
1950 LinphoneProxyConfig *proxy=NULL;
1951 LinphoneAddress *parsed_url2=NULL;
1952 char *real_url=NULL;
1953 LinphoneProxyConfig *dest_proxy=NULL;
1956 if (lc->call!=NULL){
1957 lc->vtable.display_warning(lc,_("Sorry, having multiple simultaneous calls is not supported yet !"));
1961 linphone_core_get_default_proxy(lc,&proxy);
1962 route=linphone_core_get_route(lc);
1964 real_url=linphone_address_as_string(real_parsed_url);
1965 dest_proxy=linphone_core_lookup_known_proxy(lc,real_parsed_url);
1967 if (proxy!=dest_proxy && dest_proxy!=NULL) {
1968 ms_message("Overriding default proxy setting for this call:");
1969 ms_message("The used identity will be %s",linphone_proxy_config_get_identity(dest_proxy));
1972 if (dest_proxy!=NULL)
1973 from=linphone_proxy_config_get_identity(dest_proxy);
1974 else if (proxy!=NULL)
1975 from=linphone_proxy_config_get_identity(proxy);
1977 /* if no proxy or no identity defined for this proxy, default to primary contact*/
1978 if (from==NULL) from=linphone_core_get_primary_contact(lc);
1980 parsed_url2=linphone_address_new(from);
1982 call=linphone_call_new_outgoing(lc,parsed_url2,linphone_address_clone(real_parsed_url));
1983 sal_op_set_route(call->op,route);
1986 if (dest_proxy!=NULL || lc->sip_conf.ping_with_options==FALSE){
1987 err=linphone_core_start_invite(lc,call,dest_proxy);
1989 /*defer the start of the call after the OPTIONS ping*/
1990 call->state=LCStatePreEstablishing;
1991 call->ping_op=sal_op_new(lc->sal);
1992 sal_ping(call->ping_op,from,real_url);
1993 call->start_time=time(NULL);
1996 if (real_url!=NULL) ms_free(real_url);
2000 int linphone_core_refer(LinphoneCore *lc, const char *url)
2002 char *real_url=NULL;
2003 LinphoneAddress *real_parsed_url=linphone_core_interpret_url(lc,url);
2006 if (!real_parsed_url){
2012 ms_warning("No established call to refer.");
2015 //lc->call=NULL; //Do not do that you will lose the call afterward . . .
2016 real_url=linphone_address_as_string (real_parsed_url);
2017 sal_refer(call->op,real_url);
2023 * Returns true if in incoming call is pending, ie waiting for being answered or declined.
2025 * @ingroup call_control
2027 bool_t linphone_core_inc_invite_pending(LinphoneCore*lc){
2028 if (lc->call!=NULL && lc->call->dir==LinphoneCallIncoming){
2034 void linphone_core_init_media_streams(LinphoneCore *lc, LinphoneCall *call){
2035 SalMediaDescription *md=call->localdesc;
2036 lc->audiostream=audio_stream_new(md->streams[0].port,linphone_core_ipv6_enabled(lc));
2037 if (linphone_core_echo_limiter_enabled(lc)){
2038 const char *type=lp_config_get_string(lc->config,"sound","el_type","mic");
2039 if (strcasecmp(type,"mic")==0)
2040 audio_stream_enable_echo_limiter(lc->audiostream,ELControlMic);
2041 else if (strcasecmp(type,"speaker")==0)
2042 audio_stream_enable_echo_limiter(lc->audiostream,ELControlSpeaker);
2044 audio_stream_enable_gain_control(lc->audiostream,TRUE);
2045 if (linphone_core_echo_cancellation_enabled(lc)){
2046 int len,delay,framesize;
2047 len=lp_config_get_int(lc->config,"sound","ec_tail_len",0);
2048 delay=lp_config_get_int(lc->config,"sound","ec_delay",0);
2049 framesize=lp_config_get_int(lc->config,"sound","ec_framesize",0);
2050 audio_stream_set_echo_canceller_params(lc->audiostream,len,delay,framesize);
2052 audio_stream_enable_automatic_gain_control(lc->audiostream,linphone_core_agc_enabled(lc));
2054 int enabled=lp_config_get_int(lc->config,"sound","noisegate",0);
2055 audio_stream_enable_noise_gate(lc->audiostream,enabled);
2058 rtp_session_set_transports(lc->audiostream->session,lc->a_rtp,lc->a_rtcp);
2060 #ifdef VIDEO_ENABLED
2061 if ((lc->video_conf.display || lc->video_conf.capture) && md->streams[1].port>0)
2062 lc->videostream=video_stream_new(md->streams[1].port,linphone_core_ipv6_enabled(lc));
2064 lc->videostream=NULL;
2068 static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'};
2070 static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
2071 LinphoneCore* lc = (LinphoneCore*)user_data;
2072 if (dtmf<0 || dtmf>15){
2073 ms_warning("Bad dtmf value %i",dtmf);
2076 if (lc->vtable.dtmf_received != NULL)
2077 lc->vtable.dtmf_received(lc, dtmf_tab[dtmf]);
2080 static void parametrize_equalizer(LinphoneCore *lc, AudioStream *st){
2082 MSFilter *f=st->equalizer;
2083 int enabled=lp_config_get_int(lc->config,"sound","eq_active",0);
2084 const char *gains=lp_config_get_string(lc->config,"sound","eq_gains",NULL);
2085 ms_filter_call_method(f,MS_EQUALIZER_SET_ACTIVE,&enabled);
2091 if (sscanf(gains,"%f:%f:%f %n",&g.frequency,&g.gain,&g.width,&bytes)==3){
2092 ms_message("Read equalizer gains: %f(~%f) --> %f",g.frequency,g.width,g.gain);
2093 ms_filter_call_method(f,MS_EQUALIZER_SET_GAIN,&g);
2102 static void post_configure_audio_streams(LinphoneCore *lc){
2103 AudioStream *st=lc->audiostream;
2104 float gain=lp_config_get_float(lc->config,"sound","mic_gain",-1);
2106 audio_stream_set_mic_gain(st,gain);
2107 float recv_gain = lc->sound_conf.soft_play_lev;
2108 if (recv_gain != 0) {
2109 linphone_core_set_soft_play_level(lc,recv_gain);
2111 if (linphone_core_echo_limiter_enabled(lc)){
2112 float speed=lp_config_get_float(lc->config,"sound","el_speed",-1);
2113 float thres=lp_config_get_float(lc->config,"sound","el_thres",-1);
2114 float force=lp_config_get_float(lc->config,"sound","el_force",-1);
2115 int sustain=lp_config_get_int(lc->config,"sound","el_sustain",-1);
2117 if (st->el_type==ELControlMic){
2119 if (speed==-1) speed=0.03;
2120 if (force==-1) force=10;
2122 else if (st->el_type==ELControlSpeaker){
2124 if (speed==-1) speed=0.02;
2125 if (force==-1) force=5;
2128 ms_filter_call_method(f,MS_VOLUME_SET_EA_SPEED,&speed);
2130 ms_filter_call_method(f,MS_VOLUME_SET_EA_THRESHOLD,&thres);
2132 ms_filter_call_method(f,MS_VOLUME_SET_EA_FORCE,&force);
2134 ms_filter_call_method(f,MS_VOLUME_SET_EA_SUSTAIN,&sustain);
2138 float ng_thres=lp_config_get_float(lc->config,"sound","ng_thres",0.05);
2139 float ng_floorgain=lp_config_get_float(lc->config,"sound","ng_floorgain",0);
2140 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
2141 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&ng_floorgain);
2143 parametrize_equalizer(lc,st);
2144 if (lc->vtable.dtmf_received!=NULL){
2145 /* replace by our default action*/
2146 audio_stream_play_received_dtmfs(lc->audiostream,FALSE);
2147 rtp_session_signal_connect(lc->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
2151 static RtpProfile *make_profile(LinphoneCore *lc, const SalMediaDescription *md, const SalStreamDescription *desc, int *used_pt){
2154 RtpProfile *prof=rtp_profile_new("Call profile");
2158 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
2159 PayloadType *pt=(PayloadType*)elem->data;
2162 if (desc->type==SalAudio){
2163 linphone_core_update_allocated_audio_bandwidth_in_call(lc,pt);
2165 *used_pt=payload_type_get_number(pt);
2168 if (desc->bandwidth>0) remote_bw=desc->bandwidth;
2169 else if (md->bandwidth>0) {
2170 /*case where b=AS is given globally, not per stream*/
2171 remote_bw=md->bandwidth;
2172 if (desc->type==SalVideo){
2173 remote_bw-=lc->audio_bw;
2177 if (desc->type==SalAudio){
2178 bw=get_min_bandwidth(lc->up_audio_bw,remote_bw);
2179 }else bw=get_min_bandwidth(lc->up_video_bw,remote_bw);
2180 if (bw>0) pt->normal_bitrate=bw*1000;
2181 else if (desc->type==SalAudio){
2182 pt->normal_bitrate=-1;
2186 snprintf(tmp,sizeof(tmp),"ptime=%i",desc->ptime);
2187 payload_type_append_send_fmtp(pt,tmp);
2189 rtp_profile_set_payload(prof,payload_type_get_number(pt),pt);
2194 void linphone_core_start_media_streams(LinphoneCore *lc, LinphoneCall *call){
2195 LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
2196 const char *tool="linphone-" LINPHONE_VERSION;
2199 /* adjust rtp jitter compensation. It must be at least the latency of the sound card */
2200 int jitt_comp=MAX(lc->sound_conf.latency,lc->rtp_conf.audio_jitt_comp);
2202 if (call->media_start_time==0) call->media_start_time=time(NULL);
2204 cname=linphone_address_as_string_uri_only(me);
2206 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
2207 SalProtoRtpAvp,SalAudio);
2209 call->audio_profile=make_profile(lc,call->resultdesc,stream,&used_pt);
2210 if (!lc->use_files){
2211 MSSndCard *playcard=lc->sound_conf.play_sndcard;
2212 MSSndCard *captcard=lc->sound_conf.capt_sndcard;
2213 if (playcard==NULL) {
2214 ms_warning("No card defined for playback !");
2217 if (captcard==NULL) {
2218 ms_warning("No card defined for capture !");
2221 audio_stream_start_now(
2223 call->audio_profile,
2224 stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
2231 linphone_core_echo_cancellation_enabled(lc));
2233 audio_stream_start_with_files(
2235 call->audio_profile,
2236 stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
2244 post_configure_audio_streams(lc);
2245 audio_stream_set_rtcp_information(lc->audiostream, cname, tool);
2246 }else ms_warning("No audio stream defined ?");
2248 #ifdef VIDEO_ENABLED
2250 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
2251 SalProtoRtpAvp,SalVideo);
2252 /* shutdown preview */
2253 if (lc->previewstream!=NULL) {
2254 video_preview_stop(lc->previewstream);
2255 lc->previewstream=NULL;
2257 if (stream && (lc->video_conf.display || lc->video_conf.capture)) {
2258 const char *addr=stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr;
2259 call->video_profile=make_profile(lc,call->resultdesc,stream,&used_pt);
2260 video_stream_set_sent_video_size(lc->videostream,linphone_core_get_preferred_video_size(lc));
2261 video_stream_enable_self_view(lc->videostream,lc->video_conf.selfview);
2262 if (lc->video_conf.display && lc->video_conf.capture)
2263 video_stream_start(lc->videostream,
2264 call->video_profile, addr, stream->port,
2266 used_pt, jitt_comp, lc->video_conf.device);
2267 else if (lc->video_conf.display)
2268 video_stream_recv_only_start(lc->videostream,
2269 call->video_profile, addr, stream->port,
2270 used_pt, jitt_comp);
2271 else if (lc->video_conf.capture)
2272 video_stream_send_only_start(lc->videostream,
2273 call->video_profile, addr, stream->port,
2275 used_pt, jitt_comp, lc->video_conf.device);
2276 video_stream_set_rtcp_information(lc->videostream, cname,tool);
2283 linphone_address_destroy(me);
2284 lc->call->state=LCStateAVRunning;
2287 void linphone_core_stop_media_streams(LinphoneCore *lc, LinphoneCall *call){
2288 if (lc->audiostream!=NULL) {
2289 audio_stream_stop(lc->audiostream);
2290 lc->audiostream=NULL;
2292 #ifdef VIDEO_ENABLED
2293 if (lc->videostream!=NULL){
2294 if (lc->video_conf.display && lc->video_conf.capture)
2295 video_stream_stop(lc->videostream);
2296 else if (lc->video_conf.display)
2297 video_stream_recv_only_stop(lc->videostream);
2298 else if (lc->video_conf.capture)
2299 video_stream_send_only_stop(lc->videostream);
2300 lc->videostream=NULL;
2302 if (linphone_core_video_preview_enabled(lc)){
2303 if (lc->previewstream==NULL){
2304 lc->previewstream=video_preview_start(lc->video_conf.device, lc->video_conf.vsize);
2308 if (call->audio_profile){
2309 rtp_profile_clear_all(call->audio_profile);
2310 rtp_profile_destroy(call->audio_profile);
2311 call->audio_profile=NULL;
2313 if (call->video_profile){
2314 rtp_profile_clear_all(call->video_profile);
2315 rtp_profile_destroy(call->video_profile);
2316 call->video_profile=NULL;
2321 * Accept an incoming call.
2323 * @ingroup call_control
2324 * Basically the application is notified of incoming calls within the
2325 * invite_recv callback of the #LinphoneCoreVTable structure.
2326 * The application can later respond positively to the call using
2328 * @param lc the LinphoneCore object
2329 * @param url the SIP address of the originator of the call, or NULL.
2330 * This argument is useful for managing multiple calls simulatenously,
2331 * however this feature is not supported yet.
2332 * Using NULL will accept the unique incoming call in progress.
2334 int linphone_core_accept_call(LinphoneCore *lc, const char *url)
2336 LinphoneCall *call=lc->call;
2337 const char *contact=NULL;
2343 if (call->state==LCStateAVRunning){
2344 /*call already accepted*/
2349 if (lc->ringstream!=NULL) {
2350 ms_message("stop ringing");
2351 ring_stop(lc->ringstream);
2352 ms_message("ring stopped");
2353 lc->ringstream=NULL;
2356 /*try to be best-effort in giving real local or routable contact address*/
2357 contact=get_fixed_contact(lc,call,NULL);
2359 sal_op_set_contact(call->op,contact);
2361 sal_call_accept(call->op);
2362 lc->vtable.display_status(lc,_("Connected."));
2363 gstate_new_state(lc, GSTATE_CALL_IN_CONNECTED, NULL);
2364 call->resultdesc=sal_call_get_final_media_description(call->op);
2365 if (call->resultdesc){
2366 sal_media_description_ref(call->resultdesc);
2367 linphone_core_start_media_streams(lc, call);
2368 }else call->media_pending=TRUE;
2369 ms_message("call answered.");
2374 * Terminates a call.
2376 * @ingroup call_control
2377 * @param lc The LinphoneCore
2378 * @param url the destination of the call to be terminated, use NULL if there is
2379 * only one call (which is case in this version of liblinphone).
2381 int linphone_core_terminate_call(LinphoneCore *lc, const char *url)
2383 LinphoneCall *call=lc->call;
2388 sal_call_terminate(call->op);
2391 if (lc->ringstream!=NULL) {
2392 ring_stop(lc->ringstream);
2393 lc->ringstream=NULL;
2395 linphone_core_stop_media_streams(lc,call);
2396 lc->vtable.display_status(lc,_("Call ended") );
2397 gstate_new_state(lc, GSTATE_CALL_END, NULL);
2398 linphone_call_destroy(call);
2403 * Returns TRUE if there is a call running or pending.
2405 * @ingroup call_control
2407 bool_t linphone_core_in_call(const LinphoneCore *lc){
2408 return lc->call!=NULL;
2412 * Returns The _LinphoneCall struct of the current call if one is in call
2414 * @ingroup call_control
2416 struct _LinphoneCall *linphone_core_get_current_call(LinphoneCore *lc)
2418 if(linphone_core_in_call(lc))
2424 int linphone_core_send_publish(LinphoneCore *lc,
2425 LinphoneOnlineStatus presence_mode)
2428 for (elem=linphone_core_get_proxy_config_list(lc);elem!=NULL;elem=ms_list_next(elem)){
2429 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
2430 if (cfg->publish) linphone_proxy_config_send_publish(cfg,presence_mode);
2436 * Set the incoming call timeout in seconds.
2438 * @ingroup call_control
2439 * If an incoming call isn't answered for this timeout period, it is
2440 * automatically declined.
2442 void linphone_core_set_inc_timeout(LinphoneCore *lc, int seconds){
2443 lc->sip_conf.inc_timeout=seconds;
2447 * Returns the incoming call timeout
2449 * @ingroup call_control
2450 * See linphone_core_set_inc_timeout() for details.
2452 int linphone_core_get_inc_timeout(LinphoneCore *lc){
2453 return lc->sip_conf.inc_timeout;
2456 void linphone_core_set_presence_info(LinphoneCore *lc,int minutes_away,
2457 const char *contact,
2458 LinphoneOnlineStatus presence_mode)
2460 if (minutes_away>0) lc->minutes_away=minutes_away;
2462 if (lc->alt_contact!=NULL) {
2463 ms_free(lc->alt_contact);
2464 lc->alt_contact=NULL;
2466 if (contact) lc->alt_contact=ms_strdup(contact);
2467 if (lc->presence_mode!=presence_mode){
2468 linphone_core_notify_all_friends(lc,presence_mode);
2470 Improve the use of all LINPHONE_STATUS available.
2471 !TODO Do not mix "presence status" with "answer status code"..
2472 Use correct parameter to follow sip_if_match/sip_etag.
2474 linphone_core_send_publish(lc,presence_mode);
2476 lc->prev_mode=lc->presence_mode;
2477 lc->presence_mode=presence_mode;
2480 LinphoneOnlineStatus linphone_core_get_presence_info(const LinphoneCore *lc){
2481 return lc->presence_mode;
2485 * Get playback sound level in 0-100 scale.
2487 * @ingroup media_parameters
2489 int linphone_core_get_play_level(LinphoneCore *lc)
2491 return lc->sound_conf.play_lev;
2495 * Get ring sound level in 0-100 scale
2497 * @ingroup media_parameters
2499 int linphone_core_get_ring_level(LinphoneCore *lc)
2501 return lc->sound_conf.ring_lev;
2505 * Get sound capture level in 0-100 scale
2507 * @ingroup media_parameters
2509 int linphone_core_get_rec_level(LinphoneCore *lc){
2510 return lc->sound_conf.rec_lev;
2514 * Set sound ring level in 0-100 scale
2516 * @ingroup media_parameters
2518 void linphone_core_set_ring_level(LinphoneCore *lc, int level){
2520 lc->sound_conf.ring_lev=level;
2521 sndcard=lc->sound_conf.ring_sndcard;
2522 if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_PLAYBACK,level);
2526 void linphone_core_set_soft_play_level(LinphoneCore *lc, float level){
2528 lc->sound_conf.soft_play_lev=level;
2529 AudioStream *st=lc->audiostream;
2530 if (!st) return; /*just return*/
2533 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_DB_GAIN,&gain);
2534 }else ms_warning("Could not apply gain: gain control wasn't activated.");
2536 float linphone_core_get_soft_play_level(LinphoneCore *lc) {
2538 AudioStream *st=lc->audiostream;
2540 ms_filter_call_method(st->volrecv,MS_VOLUME_GET,&gain);
2541 }else ms_warning("Could not get gain: gain control wasn't activated.");
2547 * Set sound playback level in 0-100 scale
2549 * @ingroup media_parameters
2551 void linphone_core_set_play_level(LinphoneCore *lc, int level){
2553 lc->sound_conf.play_lev=level;
2554 sndcard=lc->sound_conf.play_sndcard;
2555 if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_PLAYBACK,level);
2559 * Set sound capture level in 0-100 scale
2561 * @ingroup media_parameters
2563 void linphone_core_set_rec_level(LinphoneCore *lc, int level)
2566 lc->sound_conf.rec_lev=level;
2567 sndcard=lc->sound_conf.capt_sndcard;
2568 if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_CAPTURE,level);
2571 static MSSndCard *get_card_from_string_id(const char *devid, unsigned int cap){
2572 MSSndCard *sndcard=NULL;
2574 sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
2575 if (sndcard!=NULL &&
2576 (ms_snd_card_get_capabilities(sndcard) & cap)==0 ){
2577 ms_warning("%s card does not have the %s capability, ignoring.",
2579 cap==MS_SND_CARD_CAP_CAPTURE ? "capture" : "playback");
2583 if (sndcard==NULL) {
2584 /* get a card that has read+write capabilities */
2585 sndcard=ms_snd_card_manager_get_default_card(ms_snd_card_manager_get());
2586 /* otherwise refine to the first card having the right capability*/
2588 const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
2589 for(;elem!=NULL;elem=elem->next){
2590 sndcard=(MSSndCard*)elem->data;
2591 if (ms_snd_card_get_capabilities(sndcard) & cap) break;
2594 if (sndcard==NULL){/*looks like a bug! take the first one !*/
2595 const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
2596 if (elem) sndcard=(MSSndCard*)elem->data;
2599 if (sndcard==NULL) ms_error("Could not find a suitable soundcard !");
2604 * Returns true if the specified sound device can capture sound.
2606 * @ingroup media_parameters
2607 * @param devid the device name as returned by linphone_core_get_sound_devices()
2609 bool_t linphone_core_sound_device_can_capture(LinphoneCore *lc, const char *devid){
2611 sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
2612 if (sndcard!=NULL && (ms_snd_card_get_capabilities(sndcard) & MS_SND_CARD_CAP_CAPTURE)) return TRUE;
2617 * Returns true if the specified sound device can play sound.
2619 * @ingroup media_parameters
2620 * @param devid the device name as returned by linphone_core_get_sound_devices()
2622 bool_t linphone_core_sound_device_can_playback(LinphoneCore *lc, const char *devid){
2624 sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
2625 if (sndcard!=NULL && (ms_snd_card_get_capabilities(sndcard) & MS_SND_CARD_CAP_PLAYBACK)) return TRUE;
2630 * Sets the sound device used for ringing.
2632 * @ingroup media_parameters
2633 * @param devid the device name as returned by linphone_core_get_sound_devices()
2635 int linphone_core_set_ringer_device(LinphoneCore *lc, const char * devid){
2636 MSSndCard *card=get_card_from_string_id(devid,MS_SND_CARD_CAP_PLAYBACK);
2637 lc->sound_conf.ring_sndcard=card;
2638 if (card && lc->ready)
2639 lp_config_set_string(lc->config,"sound","ringer_dev_id",ms_snd_card_get_string_id(card));
2644 * Sets the sound device used for playback.
2646 * @ingroup media_parameters
2647 * @param devid the device name as returned by linphone_core_get_sound_devices()
2649 int linphone_core_set_playback_device(LinphoneCore *lc, const char * devid){
2650 MSSndCard *card=get_card_from_string_id(devid,MS_SND_CARD_CAP_PLAYBACK);
2651 lc->sound_conf.play_sndcard=card;
2652 if (card && lc->ready)
2653 lp_config_set_string(lc->config,"sound","playback_dev_id",ms_snd_card_get_string_id(card));
2658 * Sets the sound device used for capture.
2660 * @ingroup media_parameters
2661 * @param devid the device name as returned by linphone_core_get_sound_devices()
2663 int linphone_core_set_capture_device(LinphoneCore *lc, const char * devid){
2664 MSSndCard *card=get_card_from_string_id(devid,MS_SND_CARD_CAP_CAPTURE);
2665 lc->sound_conf.capt_sndcard=card;
2666 if (card && lc->ready)
2667 lp_config_set_string(lc->config,"sound","capture_dev_id",ms_snd_card_get_string_id(card));
2672 * Returns the name of the currently assigned sound device for ringing.
2674 * @ingroup media_parameters
2676 const char * linphone_core_get_ringer_device(LinphoneCore *lc)
2678 if (lc->sound_conf.ring_sndcard) return ms_snd_card_get_string_id(lc->sound_conf.ring_sndcard);
2683 * Returns the name of the currently assigned sound device for playback.
2685 * @ingroup media_parameters
2687 const char * linphone_core_get_playback_device(LinphoneCore *lc)
2689 return lc->sound_conf.play_sndcard ? ms_snd_card_get_string_id(lc->sound_conf.play_sndcard) : NULL;
2693 * Returns the name of the currently assigned sound device for capture.
2695 * @ingroup media_parameters
2697 const char * linphone_core_get_capture_device(LinphoneCore *lc)
2699 return lc->sound_conf.capt_sndcard ? ms_snd_card_get_string_id(lc->sound_conf.capt_sndcard) : NULL;
2703 * Returns an unmodifiable array of available sound devices.
2705 * @ingroup media_parameters
2706 * The array is NULL terminated.
2708 const char** linphone_core_get_sound_devices(LinphoneCore *lc){
2709 build_sound_devices_table(lc);
2710 return lc->sound_conf.cards;
2714 * Returns an unmodifiable array of available video capture devices.
2716 * @ingroup media_parameters
2717 * The array is NULL terminated.
2719 const char** linphone_core_get_video_devices(const LinphoneCore *lc){
2720 return lc->video_conf.cams;
2723 char linphone_core_get_sound_source(LinphoneCore *lc)
2725 return lc->sound_conf.source;
2728 void linphone_core_set_sound_source(LinphoneCore *lc, char source)
2730 MSSndCard *sndcard=lc->sound_conf.capt_sndcard;
2731 lc->sound_conf.source=source;
2732 if (!sndcard) return;
2735 ms_snd_card_set_capture(sndcard,MS_SND_CARD_MIC);
2738 ms_snd_card_set_capture(sndcard,MS_SND_CARD_LINE);
2746 * Sets the path to a wav file used for ringing.
2748 * The file must be a wav 16bit linear.
2750 * @ingroup media_parameters
2752 void linphone_core_set_ring(LinphoneCore *lc,const char *path){
2753 if (lc->sound_conf.local_ring!=0){
2754 ms_free(lc->sound_conf.local_ring);
2756 lc->sound_conf.local_ring=ms_strdup(path);
2757 if (lc->ready && lc->sound_conf.local_ring)
2758 lp_config_set_string(lc->config,"sound","local_ring",lc->sound_conf.local_ring);
2762 * Returns the path to the wav file used for ringing.
2764 * @ingroup media_parameters
2766 const char *linphone_core_get_ring(const LinphoneCore *lc){
2767 return lc->sound_conf.local_ring;
2770 static void notify_end_of_ring(void *ud ,unsigned int event, void * arg){
2771 LinphoneCore *lc=(LinphoneCore*)ud;
2772 lc->preview_finished=1;
2775 int linphone_core_preview_ring(LinphoneCore *lc, const char *ring,LinphoneCoreCbFunc func,void * userdata)
2777 if (lc->ringstream!=0){
2778 ms_warning("Cannot start ring now,there's already a ring being played");
2781 lc_callback_obj_init(&lc->preview_finished_cb,func,userdata);
2782 lc->preview_finished=0;
2783 if (lc->sound_conf.ring_sndcard!=NULL){
2784 lc->ringstream=ring_start_with_cb(ring,2000,lc->sound_conf.ring_sndcard,notify_end_of_ring,(void *)lc);
2790 * Sets the path to a wav file used for ringing back.
2792 * Ringback means the ring that is heard when it's ringing at the remote party.
2793 * The file must be a wav 16bit linear.
2795 * @ingroup media_parameters
2797 void linphone_core_set_ringback(LinphoneCore *lc, const char *path){
2798 if (lc->sound_conf.remote_ring!=0){
2799 ms_free(lc->sound_conf.remote_ring);
2801 lc->sound_conf.remote_ring=ms_strdup(path);
2805 * Returns the path to the wav file used for ringing back.
2807 * @ingroup media_parameters
2809 const char * linphone_core_get_ringback(const LinphoneCore *lc){
2810 return lc->sound_conf.remote_ring;
2814 * Enables or disable echo cancellation.
2816 * @ingroup media_parameters
2818 void linphone_core_enable_echo_cancellation(LinphoneCore *lc, bool_t val){
2819 lc->sound_conf.ec=val;
2821 lp_config_set_int(lc->config,"sound","echocancellation",val);
2825 * Returns TRUE if echo cancellation is enabled.
2827 * @ingroup media_parameters
2829 bool_t linphone_core_echo_cancellation_enabled(LinphoneCore *lc){
2830 return lc->sound_conf.ec;
2833 void linphone_core_enable_echo_limiter(LinphoneCore *lc, bool_t val){
2834 lc->sound_conf.ea=val;
2837 bool_t linphone_core_echo_limiter_enabled(const LinphoneCore *lc){
2838 return lc->sound_conf.ea;
2842 * Mutes or unmutes the local microphone.
2844 * @ingroup media_parameters
2846 void linphone_core_mute_mic(LinphoneCore *lc, bool_t val){
2847 if (lc->audiostream!=NULL){
2848 audio_stream_set_mic_gain(lc->audiostream,
2849 (val==TRUE) ? 0 : 1.0);
2853 bool_t linphone_core_is_mic_muted(LinphoneCore *lc) {
2855 if (lc->audiostream && lc->audiostream->volsend){
2856 ms_filter_call_method(lc->audiostream->volsend,MS_VOLUME_GET_GAIN,&gain);
2857 }else ms_warning("Could not get gain: gain control wasn't activated. ");
2862 void linphone_core_enable_agc(LinphoneCore *lc, bool_t val){
2863 lc->sound_conf.agc=val;
2866 bool_t linphone_core_agc_enabled(const LinphoneCore *lc){
2867 return lc->sound_conf.agc;
2871 * Send the specified dtmf.
2873 * @ingroup media_parameters
2874 * This function only works during calls. The dtmf is automatically played to the user.
2875 * @param lc The LinphoneCore object
2876 * @param dtmf The dtmf name specified as a char, such as '0', '#' etc...
2879 void linphone_core_send_dtmf(LinphoneCore *lc, char dtmf)
2881 /*By default we send DTMF RFC2833 if we do not have enabled SIP_INFO but we can also send RFC2833 and SIP_INFO*/
2882 if (linphone_core_get_use_rfc2833_for_dtmf(lc)!=0 || linphone_core_get_use_info_for_dtmf(lc)==0)
2885 if (lc->audiostream!=NULL){
2886 audio_stream_send_dtmf(lc->audiostream,dtmf);
2890 ms_error("we cannot send RFC2833 dtmf when we are not in communication");
2893 if (linphone_core_get_use_info_for_dtmf(lc)!=0){
2894 /* Out of Band DTMF (use INFO method) */
2895 LinphoneCall *call=lc->call;
2899 sal_call_send_dtmf(call->op,dtmf);
2903 void linphone_core_set_stun_server(LinphoneCore *lc, const char *server){
2904 if (lc->net_conf.stun_server!=NULL)
2905 ms_free(lc->net_conf.stun_server);
2907 lc->net_conf.stun_server=ms_strdup(server);
2908 else lc->net_conf.stun_server=NULL;
2911 const char * linphone_core_get_stun_server(const LinphoneCore *lc){
2912 return lc->net_conf.stun_server;
2915 const char * linphone_core_get_relay_addr(const LinphoneCore *lc){
2916 return lc->net_conf.relay;
2919 int linphone_core_set_relay_addr(LinphoneCore *lc, const char *addr){
2920 if (lc->net_conf.relay!=NULL){
2921 ms_free(lc->net_conf.relay);
2922 lc->net_conf.relay=NULL;
2925 lc->net_conf.relay=ms_strdup(addr);
2930 void linphone_core_set_nat_address(LinphoneCore *lc, const char *addr)
2932 if (lc->net_conf.nat_address!=NULL){
2933 ms_free(lc->net_conf.nat_address);
2935 if (addr!=NULL) lc->net_conf.nat_address=ms_strdup(addr);
2936 else lc->net_conf.nat_address=NULL;
2937 if (lc->sip_conf.contact) update_primary_contact(lc);
2940 const char *linphone_core_get_nat_address(const LinphoneCore *lc)
2942 return lc->net_conf.nat_address;
2945 void linphone_core_set_firewall_policy(LinphoneCore *lc, LinphoneFirewallPolicy pol){
2946 lc->net_conf.firewall_policy=pol;
2947 if (lc->sip_conf.contact) update_primary_contact(lc);
2950 LinphoneFirewallPolicy linphone_core_get_firewall_policy(const LinphoneCore *lc){
2951 return lc->net_conf.firewall_policy;
2955 * Get the list of call logs (past calls).
2957 * @ingroup call_logs
2959 const MSList * linphone_core_get_call_logs(LinphoneCore *lc){
2961 return lc->call_logs;
2965 * Erase the call log.
2967 * @ingroup call_logs
2969 void linphone_core_clear_call_logs(LinphoneCore *lc){
2971 ms_list_for_each(lc->call_logs,(void (*)(void*))linphone_call_log_destroy);
2972 lc->call_logs=ms_list_free(lc->call_logs);
2973 call_logs_write_to_config_file(lc);
2976 static void toggle_video_preview(LinphoneCore *lc, bool_t val){
2977 #ifdef VIDEO_ENABLED
2978 if (lc->videostream==NULL){
2980 if (lc->previewstream==NULL){
2981 lc->previewstream=video_preview_start(lc->video_conf.device,
2982 lc->video_conf.vsize);
2985 if (lc->previewstream!=NULL){
2986 video_preview_stop(lc->previewstream);
2987 lc->previewstream=NULL;
2995 * Enables video globally.
2997 * @ingroup media_parameters
2998 * This function does not have any effect during calls. It just indicates LinphoneCore to
2999 * initiate future calls with video or not. The two boolean parameters indicate in which
3000 * direction video is enabled. Setting both to false disables video entirely.
3002 * @param vcap_enabled indicates whether video capture is enabled
3003 * @param display_enabled indicates whether video display should be shown
3006 void linphone_core_enable_video(LinphoneCore *lc, bool_t vcap_enabled, bool_t display_enabled){
3007 #ifndef VIDEO_ENABLED
3008 if (vcap_enabled || display_enabled)
3009 ms_warning("This version of linphone was built without video support.");
3011 lc->video_conf.capture=vcap_enabled;
3012 lc->video_conf.display=display_enabled;
3014 /* need to re-apply network bandwidth settings*/
3015 linphone_core_set_download_bandwidth(lc,
3016 linphone_core_get_download_bandwidth(lc));
3017 linphone_core_set_upload_bandwidth(lc,
3018 linphone_core_get_upload_bandwidth(lc));
3022 * Returns TRUE if video is enabled, FALSE otherwise.
3023 * @ingroup media_parameters
3025 bool_t linphone_core_video_enabled(LinphoneCore *lc){
3026 return (lc->video_conf.display || lc->video_conf.capture);
3030 * Controls video preview enablement.
3032 * @ingroup media_parameters
3033 * Video preview refers to the action of displaying the local webcam image
3034 * to the user while not in call.
3036 void linphone_core_enable_video_preview(LinphoneCore *lc, bool_t val){
3037 lc->video_conf.show_local=val;
3041 * Returns TRUE if video previewing is enabled.
3042 * @ingroup media_parameters
3044 bool_t linphone_core_video_preview_enabled(const LinphoneCore *lc){
3045 return lc->video_conf.show_local;
3049 * Enables or disable self view during calls.
3051 * @ingroup media_parameters
3052 * Self-view refers to having local webcam image inserted in corner
3053 * of the video window during calls.
3054 * This function works at any time, including during calls.
3056 void linphone_core_enable_self_view(LinphoneCore *lc, bool_t val){
3057 lc->video_conf.selfview=val;
3058 #ifdef VIDEO_ENABLED
3059 if (lc->videostream){
3060 video_stream_enable_self_view(lc->videostream,val);
3066 * Returns TRUE if self-view is enabled, FALSE otherwise.
3068 * @ingroup media_parameters
3070 * Refer to linphone_core_enable_self_view() for details.
3072 bool_t linphone_core_self_view_enabled(const LinphoneCore *lc){
3073 return lc->video_conf.selfview;
3077 * Sets the active video device.
3079 * @ingroup media_parameters
3080 * @param id the name of the video device as returned by linphone_core_get_video_devices()
3082 int linphone_core_set_video_device(LinphoneCore *lc, const char *id){
3083 MSWebCam *olddev=lc->video_conf.device;
3086 lc->video_conf.device=ms_web_cam_manager_get_cam(ms_web_cam_manager_get(),id);
3087 if (lc->video_conf.device==NULL){
3088 ms_warning("Could not found video device %s",id);
3091 if (lc->video_conf.device==NULL)
3092 lc->video_conf.device=ms_web_cam_manager_get_default_cam(ms_web_cam_manager_get());
3093 if (olddev!=NULL && olddev!=lc->video_conf.device){
3094 toggle_video_preview(lc,FALSE);/*restart the video local preview*/
3096 if (lc->ready && lc->video_conf.device){
3097 vd=ms_web_cam_get_string_id(lc->video_conf.device);
3098 if (vd && strstr(vd,"Static picture")!=NULL){
3101 lp_config_set_string(lc->config,"video","device",vd);
3107 * Returns the name of the currently active video device.
3109 * @ingroup media_parameters
3111 const char *linphone_core_get_video_device(const LinphoneCore *lc){
3112 if (lc->video_conf.device) return ms_web_cam_get_string_id(lc->video_conf.device);
3116 int linphone_core_set_static_picture(LinphoneCore *lc, const char *path) {
3117 #ifdef VIDEO_ENABLED
3118 VideoStream *vs = NULL;
3119 /* Select the video stream from the call in the first place */
3120 if (lc && lc->videostream) {
3121 vs = lc->videostream;
3123 /* If not in call, select the video stream from the preview */
3124 if (vs == NULL && lc && lc->previewstream) {
3125 vs = lc->previewstream;
3128 /* If we have a video stream (either preview, either from call), we
3129 have a source and it is using the static picture filter, then
3130 force the filter to use that picture. */
3131 if (vs && vs->source) {
3132 if (ms_filter_get_id(vs->source) == MS_STATIC_IMAGE_ID) {
3133 ms_filter_call_method(vs->source, MS_FILTER_SET_IMAGE,
3138 /* Tell the static image filter to use that image from now on so
3139 that the image will be used next time it has to be read */
3140 ms_static_image_set_default_image(path);
3142 ms_warning("Video support not compiled.");
3148 * Returns the native window handle of the video window, casted as an unsigned long.
3150 * @ingroup media_parameters
3152 unsigned long linphone_core_get_native_video_window_id(const LinphoneCore *lc){
3153 #ifdef VIDEO_ENABLED
3154 if (lc->videostream)
3155 return video_stream_get_native_window_id(lc->videostream);
3156 if (lc->previewstream)
3157 return video_stream_get_native_window_id(lc->previewstream);
3162 static MSVideoSizeDef supported_resolutions[]={
3163 { {MS_VIDEO_SIZE_SVGA_W,MS_VIDEO_SIZE_SVGA_H} , "svga" },
3164 { {MS_VIDEO_SIZE_4CIF_W,MS_VIDEO_SIZE_4CIF_H} , "4cif" },
3165 { {MS_VIDEO_SIZE_VGA_W,MS_VIDEO_SIZE_VGA_H} , "vga" },
3166 { {MS_VIDEO_SIZE_CIF_W,MS_VIDEO_SIZE_CIF_H} , "cif" },
3167 { {MS_VIDEO_SIZE_QVGA_W,MS_VIDEO_SIZE_QVGA_H} , "qvga" },
3168 { {MS_VIDEO_SIZE_QCIF_W,MS_VIDEO_SIZE_QCIF_H} , "qcif" },
3173 * Returns the zero terminated table of supported video resolutions.
3175 * @ingroup media_parameters
3177 const MSVideoSizeDef *linphone_core_get_supported_video_sizes(LinphoneCore *lc){
3178 return supported_resolutions;
3181 static MSVideoSize video_size_get_by_name(const char *name){
3182 MSVideoSizeDef *pdef=supported_resolutions;
3183 MSVideoSize null_vsize={0,0};
3184 for(;pdef->name!=NULL;pdef++){
3185 if (strcasecmp(name,pdef->name)==0){
3189 ms_warning("Video resolution %s is not supported in linphone.",name);
3193 static const char *video_size_get_name(MSVideoSize vsize){
3194 MSVideoSizeDef *pdef=supported_resolutions;
3195 for(;pdef->name!=NULL;pdef++){
3196 if (pdef->vsize.width==vsize.width && pdef->vsize.height==vsize.height){
3203 static bool_t video_size_supported(MSVideoSize vsize){
3204 if (video_size_get_name(vsize)) return TRUE;
3205 ms_warning("Video resolution %ix%i is not supported in linphone.",vsize.width,vsize.height);
3210 * Sets the preferred video size.
3212 * @ingroup media_parameters
3213 * This applies only to the stream that is captured and sent to the remote party,
3214 * since we accept all standard video size on the receive path.
3216 void linphone_core_set_preferred_video_size(LinphoneCore *lc, MSVideoSize vsize){
3217 if (video_size_supported(vsize)){
3218 MSVideoSize oldvsize=lc->video_conf.vsize;
3219 lc->video_conf.vsize=vsize;
3220 if (!ms_video_size_equal(oldvsize,vsize) && lc->previewstream!=NULL){
3221 toggle_video_preview(lc,FALSE);
3222 toggle_video_preview(lc,TRUE);
3225 lp_config_set_string(lc->config,"video","size",video_size_get_name(vsize));
3230 * Sets the preferred video size by its name.
3232 * @ingroup media_parameters
3233 * This is identical to linphone_core_set_preferred_video_size() except
3234 * that it takes the name of the video resolution as input.
3235 * Video resolution names are: qcif, svga, cif, vga, 4cif, svga ...
3237 void linphone_core_set_preferred_video_size_by_name(LinphoneCore *lc, const char *name){
3238 MSVideoSize vsize=video_size_get_by_name(name);
3239 MSVideoSize default_vsize={MS_VIDEO_SIZE_CIF_W,MS_VIDEO_SIZE_CIF_H};
3240 if (vsize.width!=0) linphone_core_set_preferred_video_size(lc,vsize);
3241 else linphone_core_set_preferred_video_size(lc,default_vsize);
3245 * Returns the current preferred video size for sending.
3247 * @ingroup media_parameters
3249 MSVideoSize linphone_core_get_preferred_video_size(LinphoneCore *lc){
3250 return lc->video_conf.vsize;
3253 void linphone_core_use_files(LinphoneCore *lc, bool_t yesno){
3254 lc->use_files=yesno;
3257 void linphone_core_set_play_file(LinphoneCore *lc, const char *file){
3258 if (lc->play_file!=NULL){
3259 ms_free(lc->play_file);
3263 lc->play_file=ms_strdup(file);
3264 if (lc->audiostream->ticker)
3265 audio_stream_play(lc->audiostream,file);
3269 void linphone_core_set_record_file(LinphoneCore *lc, const char *file){
3270 if (lc->rec_file!=NULL){
3271 ms_free(lc->rec_file);
3275 lc->rec_file=ms_strdup(file);
3276 if (lc->audiostream)
3277 audio_stream_record(lc->audiostream,file);
3282 * Retrieves the user pointer that was given to linphone_core_new()
3284 * @ingroup initializing
3286 void *linphone_core_get_user_data(LinphoneCore *lc){
3290 int linphone_core_get_mtu(const LinphoneCore *lc){
3291 return lc->net_conf.mtu;
3294 void linphone_core_set_mtu(LinphoneCore *lc, int mtu){
3295 lc->net_conf.mtu=mtu;
3298 ms_error("MTU too small !");
3302 ms_message("MTU is supposed to be %i, rtp payload max size will be %i",mtu, ms_get_payload_max_size());
3303 }else ms_set_mtu(0);//use mediastreamer2 default value
3306 void linphone_core_set_waiting_callback(LinphoneCore *lc, LinphoneWaitingCallback cb, void *user_context){
3308 lc->wait_ctx=user_context;
3311 void linphone_core_start_waiting(LinphoneCore *lc, const char *purpose){
3313 lc->wait_ctx=lc->wait_cb(lc,lc->wait_ctx,LinphoneWaitingStart,purpose,0);
3317 void linphone_core_update_progress(LinphoneCore *lc, const char *purpose, float progress){
3319 lc->wait_ctx=lc->wait_cb(lc,lc->wait_ctx,LinphoneWaitingProgress,purpose,progress);
3329 void linphone_core_stop_waiting(LinphoneCore *lc){
3331 lc->wait_ctx=lc->wait_cb(lc,lc->wait_ctx,LinphoneWaitingFinished,NULL,0);
3335 void linphone_core_set_audio_transports(LinphoneCore *lc, RtpTransport *rtp, RtpTransport *rtcp){
3340 void net_config_uninit(LinphoneCore *lc)
3342 net_config_t *config=&lc->net_conf;
3343 lp_config_set_int(lc->config,"net","download_bw",config->download_bw);
3344 lp_config_set_int(lc->config,"net","upload_bw",config->upload_bw);
3346 if (config->stun_server!=NULL){
3347 lp_config_set_string(lc->config,"net","stun_server",config->stun_server);
3348 ms_free(lc->net_conf.stun_server);
3350 if (config->nat_address!=NULL){
3351 lp_config_set_string(lc->config,"net","nat_address",config->nat_address);
3352 ms_free(lc->net_conf.nat_address);
3354 lp_config_set_int(lc->config,"net","firewall_policy",config->firewall_policy);
3355 lp_config_set_int(lc->config,"net","mtu",config->mtu);
3359 void sip_config_uninit(LinphoneCore *lc)
3363 sip_config_t *config=&lc->sip_conf;
3364 lp_config_set_int(lc->config,"sip","sip_port",config->sip_port);
3365 lp_config_set_int(lc->config,"sip","guess_hostname",config->guess_hostname);
3366 lp_config_set_string(lc->config,"sip","contact",config->contact);
3367 lp_config_set_int(lc->config,"sip","inc_timeout",config->inc_timeout);
3368 lp_config_set_int(lc->config,"sip","use_info",config->use_info);
3369 lp_config_set_int(lc->config,"sip","use_rfc2833",config->use_rfc2833);
3370 lp_config_set_int(lc->config,"sip","use_ipv6",config->ipv6_enabled);
3371 lp_config_set_int(lc->config,"sip","register_only_when_network_is_up",config->register_only_when_network_is_up);
3373 lp_config_set_int(lc->config,"sip","default_proxy",linphone_core_get_default_proxy(lc,NULL));
3375 for(elem=config->proxies,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
3376 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)(elem->data);
3377 linphone_proxy_config_write_to_config_file(lc->config,cfg,i);
3378 linphone_proxy_config_edit(cfg); /* to unregister */
3384 sal_iterate(lc->sal);
3393 ms_list_for_each(config->proxies,(void (*)(void*)) linphone_proxy_config_destroy);
3394 ms_list_free(config->proxies);
3395 config->proxies=NULL;
3397 linphone_proxy_config_write_to_config_file(lc->config,NULL,i); /*mark the end */
3399 for(elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
3400 LinphoneAuthInfo *ai=(LinphoneAuthInfo*)(elem->data);
3401 linphone_auth_info_write_config(lc->config,ai,i);
3403 linphone_auth_info_write_config(lc->config,NULL,i); /* mark the end */
3404 ms_list_for_each(lc->auth_info,(void (*)(void*))linphone_auth_info_destroy);
3405 ms_list_free(lc->auth_info);
3408 sal_uninit(lc->sal);
3411 if (lc->sip_conf.guessed_contact)
3412 ms_free(lc->sip_conf.guessed_contact);
3413 if (config->contact)
3414 ms_free(config->contact);
3418 void rtp_config_uninit(LinphoneCore *lc)
3420 rtp_config_t *config=&lc->rtp_conf;
3421 lp_config_set_int(lc->config,"rtp","audio_rtp_port",config->audio_rtp_port);
3422 lp_config_set_int(lc->config,"rtp","video_rtp_port",config->video_rtp_port);
3423 lp_config_set_int(lc->config,"rtp","audio_jitt_comp",config->audio_jitt_comp);
3424 lp_config_set_int(lc->config,"rtp","video_jitt_comp",config->video_jitt_comp);
3425 lp_config_set_int(lc->config,"rtp","nortp_timeout",config->nortp_timeout);
3428 void sound_config_uninit(LinphoneCore *lc)
3430 sound_config_t *config=&lc->sound_conf;
3431 ms_free(config->cards);
3433 lp_config_set_string(lc->config,"sound","remote_ring",config->remote_ring);
3435 if (config->local_ring) ms_free(config->local_ring);
3436 if (config->remote_ring) ms_free(config->remote_ring);
3437 ms_snd_card_manager_destroy();
3440 void video_config_uninit(LinphoneCore *lc)
3442 lp_config_set_int(lc->config,"video","enabled",linphone_core_video_enabled(lc));
3443 lp_config_set_string(lc->config,"video","size",video_size_get_name(linphone_core_get_preferred_video_size(lc)));
3444 lp_config_set_int(lc->config,"video","display",lc->video_conf.display);
3445 lp_config_set_int(lc->config,"video","capture",lc->video_conf.capture);
3446 lp_config_set_int(lc->config,"video","show_local",linphone_core_video_preview_enabled(lc));
3447 lp_config_set_int(lc->config,"video","self_view",linphone_core_self_view_enabled(lc));
3448 if (lc->video_conf.cams)
3449 ms_free(lc->video_conf.cams);
3452 void codecs_config_uninit(LinphoneCore *lc)
3455 codecs_config_t *config=&lc->codecs_conf;
3460 for(node=config->audio_codecs;node!=NULL;node=ms_list_next(node)){
3461 pt=(PayloadType*)(node->data);
3462 sprintf(key,"audio_codec_%i",index);
3463 lp_config_set_string(lc->config,key,"mime",pt->mime_type);
3464 lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
3465 lp_config_set_int(lc->config,key,"enabled",linphone_core_payload_type_enabled(lc,pt));
3469 for(node=config->video_codecs;node!=NULL;node=ms_list_next(node)){
3470 pt=(PayloadType*)(node->data);
3471 sprintf(key,"video_codec_%i",index);
3472 lp_config_set_string(lc->config,key,"mime",pt->mime_type);
3473 lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
3474 lp_config_set_int(lc->config,key,"enabled",linphone_core_payload_type_enabled(lc,pt));
3475 lp_config_set_string(lc->config,key,"recv_fmtp",pt->recv_fmtp);
3478 ms_list_free(lc->codecs_conf.audio_codecs);
3479 ms_list_free(lc->codecs_conf.video_codecs);
3482 void ui_config_uninit(LinphoneCore* lc)
3485 ms_list_for_each(lc->friends,(void (*)(void *))linphone_friend_destroy);
3486 ms_list_free(lc->friends);
3492 * Returns the LpConfig object used to manage the storage (config) file.
3495 * The application can use the LpConfig object to insert its own private
3496 * sections and pairs of key=value in the configuration file.
3499 LpConfig *linphone_core_get_config(LinphoneCore *lc){
3503 static void linphone_core_uninit(LinphoneCore *lc)
3507 linphone_core_terminate_call(lc,NULL);
3514 linphone_core_iterate(lc);
3518 ms_list_for_each(lc->friends,(void (*)(void *))linphone_friend_close_subscriptions);
3519 gstate_new_state(lc, GSTATE_POWER_SHUTDOWN, NULL);
3520 #ifdef VIDEO_ENABLED
3521 if (lc->previewstream!=NULL){
3522 video_preview_stop(lc->previewstream);
3523 lc->previewstream=NULL;
3526 /* save all config */
3527 net_config_uninit(lc);
3528 sip_config_uninit(lc);
3529 rtp_config_uninit(lc);
3530 sound_config_uninit(lc);
3531 video_config_uninit(lc);
3532 codecs_config_uninit(lc);
3533 ui_config_uninit(lc);
3534 if (lp_config_needs_commit(lc->config)) lp_config_sync(lc->config);
3535 lp_config_destroy(lc->config);
3536 lc->config = NULL; /* Mark the config as NULL to block further calls */
3537 sip_setup_unregister_all();
3539 ms_list_for_each(lc->call_logs,(void (*)(void*))linphone_call_log_destroy);
3540 lc->call_logs=ms_list_free(lc->call_logs);
3542 linphone_core_free_payload_types();
3545 gstate_new_state(lc, GSTATE_POWER_OFF, NULL);
3548 static void set_network_reachable(LinphoneCore* lc,bool_t isReachable){
3549 ms_message("Network state is now [%s]",isReachable?"UP":"DOWN");
3550 // second get the list of available proxies
3551 const MSList *elem=linphone_core_get_proxy_config_list(lc);
3552 for(;elem!=NULL;elem=elem->next){
3553 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
3554 if (linphone_proxy_config_register_enabled(cfg) ) {
3562 lc->network_reachable=isReachable;
3565 void linphone_core_set_network_reachable(LinphoneCore* lc,bool_t isReachable) {
3566 //first disable automatic mode
3567 if (lc->auto_net_state_mon) {
3568 ms_message("Disabling automatic network state monitoring");
3569 lc->auto_net_state_mon=FALSE;
3571 set_network_reachable(lc,isReachable);
3574 ortp_socket_t linphone_core_get_sip_socket(LinphoneCore *lc){
3575 return sal_get_socket(lc->sal);
3578 * Destroys a LinphoneCore
3580 * @ingroup initializing
3582 void linphone_core_destroy(LinphoneCore *lc){
3583 linphone_core_uninit(lc);