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(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){
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 /*set audio capabilities */
88 strncpy(md->streams[0].addr,localip,sizeof(md->streams[0].addr));
89 md->streams[0].port=linphone_core_get_audio_port(lc);
90 md->streams[0].proto=SalProtoRtpAvp;
91 md->streams[0].type=SalAudio;
92 md->streams[0].ptime=lc->net_conf.down_ptime;
93 l=make_codec_list(lc->codecs_conf.audio_codecs,only_one_codec);
94 pt=payload_type_clone(rtp_profile_get_payload_from_mime(&av_profile,"telephone-event"));
95 l=ms_list_append(l,pt);
96 md->streams[0].payloads=l;
98 if (lc->dw_audio_bw>0)
99 md->streams[0].bandwidth=lc->dw_audio_bw;
101 if (linphone_core_video_enabled (lc)){
103 md->streams[1].port=linphone_core_get_video_port(lc);
104 md->streams[1].proto=SalProtoRtpAvp;
105 md->streams[1].type=SalVideo;
106 l=make_codec_list(lc->codecs_conf.video_codecs,only_one_codec);
107 md->streams[1].payloads=l;
109 md->streams[1].bandwidth=lc->dw_video_bw;
114 static void linphone_call_init_common(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
115 call->state=LCStateInit;
116 call->start_time=time(NULL);
117 call->media_start_time=0;
118 call->log=linphone_call_log_new(call, from, to);
119 linphone_core_notify_all_friends(call->core,LINPHONE_STATUS_ONTHEPHONE);
120 if (linphone_core_get_firewall_policy(call->core)==LINPHONE_POLICY_USE_STUN)
121 linphone_core_run_stun_tests(call->core,call);
124 static void discover_mtu(LinphoneCore *lc, const char *remote){
126 if (lc->net_conf.mtu==0 ){
127 /*attempt to discover mtu*/
128 mtu=ms_discover_mtu(remote);
131 ms_message("Discovered mtu is %i, RTP payload max size is %i",
132 mtu, ms_get_payload_max_size());
137 LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to)
139 LinphoneCall *call=ms_new0(LinphoneCall,1);
140 call->dir=LinphoneCallOutgoing;
141 call->op=sal_op_new(lc->sal);
142 sal_op_set_user_pointer(call->op,call);
144 linphone_core_get_local_ip(lc,linphone_address_get_domain(to),call->localip);
145 call->localdesc=create_local_media_description (lc,call->localip,
146 linphone_address_get_username(from),FALSE);
147 linphone_call_init_common(call,from,to);
148 discover_mtu(lc,linphone_address_get_domain (to));
152 LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, SalOp *op){
153 LinphoneCall *call=ms_new0(LinphoneCall,1);
154 LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
158 call->dir=LinphoneCallIncoming;
159 sal_op_set_user_pointer(op,call);
163 if (lc->sip_conf.ping_with_options){
164 /*the following sends an option request back to the caller so that
165 we get a chance to discover our nat'd address before answering.*/
166 call->ping_op=sal_op_new(lc->sal);
167 to_str=linphone_address_as_string(to);
168 from_str=linphone_address_as_string(from);
169 sal_op_set_route(call->ping_op,sal_op_get_network_origin(call->op));
170 sal_ping(call->ping_op,to_str,from_str);
175 linphone_address_clean(from);
176 linphone_core_get_local_ip(lc,linphone_address_get_domain(from),call->localip);
177 call->localdesc=create_local_media_description (lc,call->localip,
178 linphone_address_get_username(me),lc->sip_conf.only_one_codec);
179 linphone_call_init_common(call, from, to);
180 discover_mtu(lc,linphone_address_get_domain(from));
181 linphone_address_destroy(me);
185 void linphone_call_destroy(LinphoneCall *obj)
187 linphone_core_notify_all_friends(obj->core,obj->core->prev_mode);
188 linphone_call_log_completed(obj->log,obj);
189 linphone_core_update_allocated_audio_bandwidth(obj->core);
191 sal_op_release(obj->op);
194 if (obj->resultdesc!=NULL) {
195 sal_media_description_unref(obj->resultdesc);
196 obj->resultdesc=NULL;
198 if (obj->localdesc!=NULL) {
199 sal_media_description_unref(obj->localdesc);
203 sal_op_release(obj->ping_op);
208 /*prevent a gcc bug with %c*/
209 static size_t my_strftime(char *s, size_t max, const char *fmt, const struct tm *tm){
210 #if !defined(_WIN32_WCE)
211 return strftime(s, max, fmt, tm);
215 #endif /*_WIN32_WCE*/
218 static void set_call_log_date(LinphoneCallLog *cl, const struct tm *loctime){
219 my_strftime(cl->start_date,sizeof(cl->start_date),"%c",loctime);
222 LinphoneCallLog * linphone_call_log_new(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
223 LinphoneCallLog *cl=ms_new0(LinphoneCallLog,1);
227 #if !defined(_WIN32_WCE)
228 loctime=*localtime(&call->start_time);
230 #endif /*_WIN32_WCE*/
232 localtime_r(&call->start_time,&loctime);
234 set_call_log_date(cl,&loctime);
240 static void call_logs_write_to_config_file(LinphoneCore *lc){
245 LpConfig *cfg=lc->config;
247 if (!lc->ready) return;
249 for(i=0,elem=lc->call_logs;elem!=NULL;elem=elem->next,++i){
250 LinphoneCallLog *cl=(LinphoneCallLog*)elem->data;
251 snprintf(logsection,sizeof(logsection),"call_log_%i",i);
252 lp_config_set_int(cfg,logsection,"dir",cl->dir);
253 lp_config_set_int(cfg,logsection,"status",cl->status);
254 tmp=linphone_address_as_string(cl->from);
255 lp_config_set_string(cfg,logsection,"from",tmp);
257 tmp=linphone_address_as_string(cl->to);
258 lp_config_set_string(cfg,logsection,"to",tmp);
260 lp_config_set_string(cfg,logsection,"start_date",cl->start_date);
261 lp_config_set_int(cfg,logsection,"duration",cl->duration);
262 if (cl->refkey) lp_config_set_string(cfg,logsection,"refkey",cl->refkey);
264 for(;i<lc->max_call_logs;++i){
265 snprintf(logsection,sizeof(logsection),"call_log_%i",i);
266 lp_config_clean_section(cfg,logsection);
270 static void call_logs_read_from_config_file(LinphoneCore *lc){
274 LpConfig *cfg=lc->config;
276 snprintf(logsection,sizeof(logsection),"call_log_%i",i);
277 if (lp_config_has_section(cfg,logsection)){
278 LinphoneCallLog *cl=ms_new0(LinphoneCallLog,1);
279 cl->dir=lp_config_get_int(cfg,logsection,"dir",0);
280 cl->status=lp_config_get_int(cfg,logsection,"status",0);
281 tmp=lp_config_get_string(cfg,logsection,"from",NULL);
282 if (tmp) cl->from=linphone_address_new(tmp);
283 tmp=lp_config_get_string(cfg,logsection,"to",NULL);
284 if (tmp) cl->to=linphone_address_new(tmp);
285 tmp=lp_config_get_string(cfg,logsection,"start_date",NULL);
286 if (tmp) strncpy(cl->start_date,tmp,sizeof(cl->start_date));
287 cl->duration=lp_config_get_int(cfg,logsection,"duration",0);
288 tmp=lp_config_get_string(cfg,logsection,"refkey",NULL);
289 if (tmp) cl->refkey=ms_strdup(tmp);
290 lc->call_logs=ms_list_append(lc->call_logs,cl);
296 void linphone_call_log_completed(LinphoneCallLog *calllog, LinphoneCall *call){
297 LinphoneCore *lc=call->core;
299 calllog->duration=time(NULL)-call->start_time;
302 case LCStatePreEstablishing:
303 calllog->status=LinphoneCallAborted;
306 if (calllog->dir==LinphoneCallIncoming){
308 calllog->status=LinphoneCallMissed;
310 info=ortp_strdup_printf(ngettext("You have missed %i call.",
311 "You have missed %i calls.", lc->missed_calls),
313 lc->vtable.display_status(lc,info);
316 else calllog->status=LinphoneCallAborted;
318 case LCStateAVRunning:
319 calllog->status=LinphoneCallSuccess;
322 lc->call_logs=ms_list_prepend(lc->call_logs,(void *)calllog);
323 if (ms_list_size(lc->call_logs)>lc->max_call_logs){
324 MSList *elem,*prevelem=NULL;
325 /*find the last element*/
326 for(elem=lc->call_logs;elem!=NULL;elem=elem->next){
330 linphone_call_log_destroy((LinphoneCallLog*)elem->data);
331 lc->call_logs=ms_list_remove_link(lc->call_logs,elem);
333 if (lc->vtable.call_log_updated!=NULL){
334 lc->vtable.call_log_updated(lc,calllog);
336 call_logs_write_to_config_file(lc);
340 * @addtogroup call_logs
345 * Returns a human readable string describing the call.
347 * @note: the returned char* must be freed by the application (use ms_free()).
349 char * linphone_call_log_to_str(LinphoneCallLog *cl){
352 char *from=linphone_address_as_string (cl->from);
353 char *to=linphone_address_as_string (cl->to);
355 case LinphoneCallAborted:
358 case LinphoneCallSuccess:
359 status=_("completed");
361 case LinphoneCallMissed:
367 tmp=ortp_strdup_printf(_("%s at %s\nFrom: %s\nTo: %s\nStatus: %s\nDuration: %i mn %i sec\n"),
368 (cl->dir==LinphoneCallIncoming) ? _("Incoming call") : _("Outgoing call"),
380 void linphone_call_log_set_user_pointer(LinphoneCallLog *cl, void *up){
384 void *linphone_call_log_get_user_pointer(const LinphoneCallLog *cl){
385 return cl->user_pointer;
391 * Associate a persistent reference key to the call log.
393 * The reference key can be for example an id to an external database.
394 * It is stored in the config file, thus can survive to process exits/restarts.
397 void linphone_call_log_set_ref_key(LinphoneCallLog *cl, const char *refkey){
398 if (cl->refkey!=NULL){
402 if (refkey) cl->refkey=ms_strdup(refkey);
403 call_logs_write_to_config_file(cl->lc);
407 * Get the persistent reference key associated to the call log.
409 * The reference key can be for example an id to an external database.
410 * It is stored in the config file, thus can survive to process exits/restarts.
413 const char *linphone_call_log_get_ref_key(const LinphoneCallLog *cl){
419 void linphone_call_log_destroy(LinphoneCallLog *cl){
420 if (cl->from!=NULL) linphone_address_destroy(cl->from);
421 if (cl->to!=NULL) linphone_address_destroy(cl->to);
422 if (cl->refkey!=NULL) ms_free(cl->refkey);
426 int linphone_core_get_current_call_duration(const LinphoneCore *lc){
427 LinphoneCall *call=lc->call;
428 if (call==NULL) return 0;
429 if (call->media_start_time==0) return 0;
430 return time(NULL)-call->media_start_time;
433 const LinphoneAddress *linphone_core_get_remote_uri(LinphoneCore *lc){
434 LinphoneCall *call=lc->call;
435 if (call==NULL) return 0;
436 return call->dir==LinphoneCallIncoming ? call->log->from : call->log->to;
440 * Enable logs in supplied FILE*.
444 * @param file a C FILE* where to fprintf logs. If null stdout is used.
447 void linphone_core_enable_logs(FILE *file){
448 if (file==NULL) file=stdout;
449 ortp_set_log_file(file);
450 ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
454 * Enable logs through the user's supplied log callback.
458 * @param logfunc The address of a OrtpLogFunc callback whose protoype is
459 * typedef void (*OrtpLogFunc)(OrtpLogLevel lev, const char *fmt, va_list args);
462 void linphone_core_enable_logs_with_cb(OrtpLogFunc logfunc){
463 ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
464 ortp_set_log_handler(logfunc);
468 * Entirely disable logging.
472 void linphone_core_disable_logs(){
473 ortp_set_log_level_mask(ORTP_ERROR|ORTP_FATAL);
478 net_config_read (LinphoneCore *lc)
482 LpConfig *config=lc->config;
484 tmp=lp_config_get_int(config,"net","download_bw",0);
485 linphone_core_set_download_bandwidth(lc,tmp);
486 tmp=lp_config_get_int(config,"net","upload_bw",0);
487 linphone_core_set_upload_bandwidth(lc,tmp);
488 linphone_core_set_stun_server(lc,lp_config_get_string(config,"net","stun_server",NULL));
489 tmpstr=lp_config_get_string(lc->config,"net","nat_address",NULL);
490 if (tmpstr!=NULL && (strlen(tmpstr)<1)) tmpstr=NULL;
491 linphone_core_set_nat_address(lc,tmpstr);
492 tmp=lp_config_get_int(lc->config,"net","firewall_policy",0);
493 linphone_core_set_firewall_policy(lc,tmp);
494 tmp=lp_config_get_int(lc->config,"net","nat_sdp_only",0);
495 lc->net_conf.nat_sdp_only=tmp;
496 tmp=lp_config_get_int(lc->config,"net","mtu",0);
497 linphone_core_set_mtu(lc,tmp);
498 tmp=lp_config_get_int(lc->config,"net","download_ptime",0);
499 linphone_core_set_download_ptime(lc,tmp);
503 static void build_sound_devices_table(LinphoneCore *lc){
504 const char **devices;
508 const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
509 ndev=ms_list_size(elem);
510 devices=ms_malloc((ndev+1)*sizeof(const char *));
511 for (i=0;elem!=NULL;elem=elem->next,i++){
512 devices[i]=ms_snd_card_get_string_id((MSSndCard *)elem->data);
515 old=lc->sound_conf.cards;
516 lc->sound_conf.cards=devices;
517 if (old!=NULL) ms_free(old);
520 static void sound_config_read(LinphoneCore *lc)
527 /*alsadev let the user use custom alsa device within linphone*/
528 devid=lp_config_get_string(lc->config,"sound","alsadev",NULL);
530 MSSndCard *card=ms_alsa_card_new_custom(devid,devid);
531 ms_snd_card_manager_add_card(ms_snd_card_manager_get(),card);
534 /* retrieve all sound devices */
535 build_sound_devices_table(lc);
537 devid=lp_config_get_string(lc->config,"sound","playback_dev_id",NULL);
538 linphone_core_set_playback_device(lc,devid);
540 devid=lp_config_get_string(lc->config,"sound","ringer_dev_id",NULL);
541 linphone_core_set_ringer_device(lc,devid);
543 devid=lp_config_get_string(lc->config,"sound","capture_dev_id",NULL);
544 linphone_core_set_capture_device(lc,devid);
547 tmp=lp_config_get_int(lc->config,"sound","play_lev",80);
548 linphone_core_set_play_level(lc,tmp);
549 tmp=lp_config_get_int(lc->config,"sound","ring_lev",80);
550 linphone_core_set_ring_level(lc,tmp);
551 tmp=lp_config_get_int(lc->config,"sound","rec_lev",80);
552 linphone_core_set_rec_level(lc,tmp);
553 tmpbuf=lp_config_get_string(lc->config,"sound","source","m");
554 linphone_core_set_sound_source(lc,tmpbuf[0]);
557 tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
558 tmpbuf=lp_config_get_string(lc->config,"sound","local_ring",tmpbuf);
559 if (ortp_file_exist(tmpbuf)==-1) {
560 tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
562 if (strstr(tmpbuf,".wav")==NULL){
563 /* it currently uses old sound files, so replace them */
564 tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
567 linphone_core_set_ring(lc,tmpbuf);
569 tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
570 tmpbuf=lp_config_get_string(lc->config,"sound","remote_ring",tmpbuf);
571 if (ortp_file_exist(tmpbuf)==-1){
572 tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
574 if (strstr(tmpbuf,".wav")==NULL){
575 /* it currently uses old sound files, so replace them */
576 tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
578 linphone_core_set_ringback(lc,tmpbuf);
579 check_sound_device(lc);
580 lc->sound_conf.latency=0;
582 linphone_core_enable_echo_cancellation(lc,
583 lp_config_get_int(lc->config,"sound","echocancelation",0) |
584 lp_config_get_int(lc->config,"sound","echocancellation",0)
587 linphone_core_enable_echo_limiter(lc,
588 lp_config_get_int(lc->config,"sound","echolimiter",0));
589 linphone_core_enable_agc(lc,
590 lp_config_get_int(lc->config,"sound","agc",0));
592 gain=lp_config_get_float(lc->config,"sound","soft_play_lev",0);
593 linphone_core_set_soft_play_level(lc,gain);
596 static void sip_config_read(LinphoneCore *lc)
603 port=lp_config_get_int(lc->config,"sip","use_info",0);
604 linphone_core_set_use_info_for_dtmf(lc,port);
606 port=lp_config_get_int(lc->config,"sip","use_rfc2833",0);
607 linphone_core_set_use_rfc2833_for_dtmf(lc,port);
609 ipv6=lp_config_get_int(lc->config,"sip","use_ipv6",-1);
612 if (host_has_ipv6_network()){
613 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"));
616 linphone_core_enable_ipv6(lc,ipv6);
617 port=lp_config_get_int(lc->config,"sip","sip_port",5060);
618 linphone_core_set_sip_port(lc,port);
620 tmpstr=lp_config_get_string(lc->config,"sip","contact",NULL);
621 if (tmpstr==NULL || linphone_core_set_primary_contact(lc,tmpstr)==-1) {
622 const char *hostname=NULL;
623 const char *username=NULL;
625 hostname=getenv("HOST");
626 username=getenv("USER");
627 if (hostname==NULL) hostname=getenv("HOSTNAME");
628 #endif /*HAVE_GETENV*/
630 hostname="unknown-host";
634 contact=ortp_strdup_printf("sip:%s@%s",username,hostname);
635 linphone_core_set_primary_contact(lc,contact);
639 tmp=lp_config_get_int(lc->config,"sip","guess_hostname",1);
640 linphone_core_set_guess_hostname(lc,tmp);
643 tmp=lp_config_get_int(lc->config,"sip","inc_timeout",15);
644 linphone_core_set_inc_timeout(lc,tmp);
646 /* get proxies config */
648 LinphoneProxyConfig *cfg=linphone_proxy_config_new_from_config_file(lc->config,i);
650 linphone_core_add_proxy_config(lc,cfg);
655 /* get the default proxy */
656 tmp=lp_config_get_int(lc->config,"sip","default_proxy",-1);
657 linphone_core_set_default_proxy_index(lc,tmp);
659 /* read authentication information */
661 LinphoneAuthInfo *ai=linphone_auth_info_new_from_config_file(lc->config,i);
663 linphone_core_add_auth_info(lc,ai);
669 /*for tuning or test*/
670 lc->sip_conf.sdp_200_ack=lp_config_get_int(lc->config,"sip","sdp_200_ack",0);
671 lc->sip_conf.only_one_codec=lp_config_get_int(lc->config,"sip","only_one_codec",0);
672 lc->sip_conf.register_only_when_network_is_up=
673 lp_config_get_int(lc->config,"sip","register_only_when_network_is_up",1);
674 lc->sip_conf.ping_with_options=lp_config_get_int(lc->config,"sip","ping_with_options",1);
677 static void rtp_config_read(LinphoneCore *lc)
682 port=lp_config_get_int(lc->config,"rtp","audio_rtp_port",7078);
683 linphone_core_set_audio_port(lc,port);
685 port=lp_config_get_int(lc->config,"rtp","video_rtp_port",9078);
686 if (port==0) port=9078;
687 linphone_core_set_video_port(lc,port);
689 jitt_comp=lp_config_get_int(lc->config,"rtp","audio_jitt_comp",60);
690 linphone_core_set_audio_jittcomp(lc,jitt_comp);
691 jitt_comp=lp_config_get_int(lc->config,"rtp","video_jitt_comp",60);
692 nortp_timeout=lp_config_get_int(lc->config,"rtp","nortp_timeout",30);
693 linphone_core_set_nortp_timeout(lc,nortp_timeout);
696 static PayloadType * find_payload(RtpProfile *prof, const char *mime_type, int clock_rate, const char *recv_fmtp){
697 PayloadType *candidate=NULL;
701 it=rtp_profile_get_payload(prof,i);
702 if (it!=NULL && strcasecmp(mime_type,it->mime_type)==0
703 && (clock_rate==it->clock_rate || clock_rate<=0) ){
704 if ( (recv_fmtp && it->recv_fmtp && strcasecmp(recv_fmtp,it->recv_fmtp)==0) ||
705 (recv_fmtp==NULL && it->recv_fmtp==NULL) ){
714 static bool_t get_codec(LpConfig *config, char* type, int index, PayloadType **ret){
716 const char *mime,*fmtp;
721 snprintf(codeckey,50,"%s_%i",type,index);
722 mime=lp_config_get_string(config,codeckey,"mime",NULL);
723 if (mime==NULL || strlen(mime)==0 ) return FALSE;
725 rate=lp_config_get_int(config,codeckey,"rate",8000);
726 fmtp=lp_config_get_string(config,codeckey,"recv_fmtp",NULL);
727 enabled=lp_config_get_int(config,codeckey,"enabled",1);
728 pt=find_payload(&av_profile,mime,rate,fmtp);
729 if (pt && enabled ) pt->flags|=PAYLOAD_TYPE_ENABLED;
730 //ms_message("Found codec %s/%i",pt->mime_type,pt->clock_rate);
731 if (pt==NULL) ms_warning("Ignoring codec config %s/%i with fmtp=%s because unsupported",
732 mime,rate,fmtp ? fmtp : "");
737 static const char *codec_pref_order[]={
750 static int find_codec_rank(const char *mime){
752 for(i=0;codec_pref_order[i]!=NULL;++i){
753 if (strcasecmp(codec_pref_order[i],mime)==0)
759 static int codec_compare(const PayloadType *a, const PayloadType *b){
761 ra=find_codec_rank(a->mime_type);
762 rb=find_codec_rank(b->mime_type);
764 if (ra<rb) return -1;
768 static MSList *add_missing_codecs(SalStreamType mtype, MSList *l){
771 PayloadType *pt=rtp_profile_get_payload(&av_profile,i);
773 if (mtype==SalVideo && pt->type!=PAYLOAD_VIDEO)
775 else if (mtype==SalAudio && (pt->type!=PAYLOAD_AUDIO_PACKETIZED
776 && pt->type!=PAYLOAD_AUDIO_CONTINUOUS)){
779 if (pt && ms_filter_codec_supported(pt->mime_type)){
780 if (ms_list_find(l,pt)==NULL){
781 payload_type_set_flag(pt,PAYLOAD_TYPE_ENABLED);
782 ms_message("Adding new codec %s/%i with fmtp %s",
783 pt->mime_type,pt->clock_rate,pt->recv_fmtp ? pt->recv_fmtp : "");
784 l=ms_list_insert_sorted(l,pt,(int (*)(const void *, const void *))codec_compare);
792 static void codecs_config_read(LinphoneCore *lc)
796 MSList *audio_codecs=NULL;
797 MSList *video_codecs=NULL;
798 for (i=0;get_codec(lc->config,"audio_codec",i,&pt);i++){
800 if (!ms_filter_codec_supported(pt->mime_type)){
801 ms_warning("Codec %s is not supported by mediastreamer2, removed.",pt->mime_type);
802 }else audio_codecs=ms_list_append(audio_codecs,pt);
805 audio_codecs=add_missing_codecs(SalAudio,audio_codecs);
806 for (i=0;get_codec(lc->config,"video_codec",i,&pt);i++){
808 if (!ms_filter_codec_supported(pt->mime_type)){
809 ms_warning("Codec %s is not supported by mediastreamer2, removed.",pt->mime_type);
810 }else video_codecs=ms_list_append(video_codecs,(void *)pt);
813 video_codecs=add_missing_codecs(SalVideo,video_codecs);
814 linphone_core_set_audio_codecs(lc,audio_codecs);
815 linphone_core_set_video_codecs(lc,video_codecs);
816 linphone_core_update_allocated_audio_bandwidth(lc);
819 static void video_config_read(LinphoneCore *lc){
820 int capture, display, self_view;
824 const char **devices;
828 /* retrieve all video devices */
829 elem=ms_web_cam_manager_get_list(ms_web_cam_manager_get());
830 ndev=ms_list_size(elem);
831 devices=ms_malloc((ndev+1)*sizeof(const char *));
832 for (i=0;elem!=NULL;elem=elem->next,i++){
833 devices[i]=ms_web_cam_get_string_id((MSWebCam *)elem->data);
836 lc->video_conf.cams=devices;
838 str=lp_config_get_string(lc->config,"video","device",NULL);
839 if (str && str[0]==0) str=NULL;
840 linphone_core_set_video_device(lc,str);
842 linphone_core_set_preferred_video_size_by_name(lc,
843 lp_config_get_string(lc->config,"video","size","cif"));
845 enabled=lp_config_get_int(lc->config,"video","enabled",1);
846 capture=lp_config_get_int(lc->config,"video","capture",enabled);
847 display=lp_config_get_int(lc->config,"video","display",enabled);
848 self_view=lp_config_get_int(lc->config,"video","self_view",enabled);
850 linphone_core_enable_video(lc,capture,display);
851 linphone_core_enable_self_view(lc,self_view);
855 static void ui_config_read(LinphoneCore *lc)
859 for (i=0;(lf=linphone_friend_new_from_config_file(lc,i))!=NULL;i++){
860 linphone_core_add_friend(lc,lf);
862 call_logs_read_from_config_file(lc);
866 static void autoreplier_config_init(LinphoneCore *lc)
868 autoreplier_config_t *config=&lc->autoreplier_conf;
869 config->enabled=lp_config_get_int(lc->config,"autoreplier","enabled",0);
870 config->after_seconds=lp_config_get_int(lc->config,"autoreplier","after_seconds",6);
871 config->max_users=lp_config_get_int(lc->config,"autoreplier","max_users",1);
872 config->max_rec_time=lp_config_get_int(lc->config,"autoreplier","max_rec_time",60);
873 config->max_rec_msg=lp_config_get_int(lc->config,"autoreplier","max_rec_msg",10);
874 config->message=lp_config_get_string(lc->config,"autoreplier","message",NULL);
879 * Sets maximum available download bandwidth
881 * @ingroup media_parameters
883 * This is IP bandwidth, in kbit/s.
884 * This information is used signaled to other parties during
885 * calls (within SDP messages) so that the remote end can have
886 * sufficient knowledge to properly configure its audio & video
887 * codec output bitrate to not overflow available bandwidth.
889 * @param lc the LinphoneCore object
890 * @param bw the bandwidth in kbits/s, 0 for infinite
892 void linphone_core_set_download_bandwidth(LinphoneCore *lc, int bw){
893 lc->net_conf.download_bw=bw;
894 if (bw==0){ /*infinite*/
898 lc->dw_audio_bw=MIN(lc->audio_bw,bw);
899 lc->dw_video_bw=MAX(bw-lc->dw_audio_bw-10,0);/*-10: security margin*/
904 * Sets maximum available upload bandwidth
906 * @ingroup media_parameters
908 * This is IP bandwidth, in kbit/s.
909 * This information is used by liblinphone together with remote
910 * side available bandwidth signaled in SDP messages to properly
911 * configure audio & video codec's output bitrate.
913 * @param lc the LinphoneCore object
914 * @param bw the bandwidth in kbits/s, 0 for infinite
916 void linphone_core_set_upload_bandwidth(LinphoneCore *lc, int bw){
917 lc->net_conf.upload_bw=bw;
918 if (bw==0){ /*infinite*/
922 lc->up_audio_bw=MIN(lc->audio_bw,bw);
923 lc->up_video_bw=MAX(bw-lc->up_audio_bw-10,0);/*-10: security margin*/
928 * Retrieve the maximum available download bandwidth.
930 * @ingroup media_parameters
932 * This value was set by linphone_core_set_download_bandwidth().
935 int linphone_core_get_download_bandwidth(const LinphoneCore *lc){
936 return lc->net_conf.download_bw;
940 * Retrieve the maximum available upload bandwidth.
942 * @ingroup media_parameters
944 * This value was set by linphone_core_set_upload_bandwidth().
947 int linphone_core_get_upload_bandwidth(const LinphoneCore *lc){
948 return lc->net_conf.upload_bw;
951 * set audio packetization time linphone expect to received from peer
953 void linphone_core_set_download_ptime(LinphoneCore *lc, int ptime) {
954 lc->net_conf.down_ptime=ptime;
957 int linphone_core_get_download_ptime(LinphoneCore *lc) {
958 return lc->net_conf.down_ptime;
963 * Returns liblinphone's version as a string.
968 const char * linphone_core_get_version(void){
969 return liblinphone_version;
973 static MSList *linphone_payload_types=NULL;
975 static void linphone_core_assign_payload_type(PayloadType *const_pt, int number, const char *recv_fmtp){
977 pt=payload_type_clone(const_pt);
978 payload_type_set_number(pt,number);
979 if (recv_fmtp!=NULL) payload_type_set_recv_fmtp(pt,recv_fmtp);
980 rtp_profile_set_payload(&av_profile,number,pt);
981 linphone_payload_types=ms_list_append(linphone_payload_types,pt);
984 static void linphone_core_free_payload_types(void){
985 ms_list_for_each(linphone_payload_types,(void (*)(void*))payload_type_destroy);
986 ms_list_free(linphone_payload_types);
987 linphone_payload_types=NULL;
990 static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vtable, const char *config_path,
991 const char *factory_config_path, void * userdata)
993 memset (lc, 0, sizeof (LinphoneCore));
996 memcpy(&lc->vtable,vtable,sizeof(LinphoneCoreVTable));
998 gstate_initialize(lc);
999 gstate_new_state(lc, GSTATE_POWER_STARTUP, NULL);
1002 linphone_core_assign_payload_type(&payload_type_pcmu8000,0,NULL);
1003 linphone_core_assign_payload_type(&payload_type_gsm,3,NULL);
1004 linphone_core_assign_payload_type(&payload_type_pcma8000,8,NULL);
1005 linphone_core_assign_payload_type(&payload_type_lpc1015,115,NULL);
1006 linphone_core_assign_payload_type(&payload_type_speex_nb,110,"vbr=on");
1007 linphone_core_assign_payload_type(&payload_type_speex_wb,111,"vbr=on");
1008 linphone_core_assign_payload_type(&payload_type_speex_uwb,112,"vbr=on");
1009 linphone_core_assign_payload_type(&payload_type_telephone_event,101,"0-11");
1010 linphone_core_assign_payload_type(&payload_type_ilbc,113,"mode=30");
1012 #ifdef ENABLE_NONSTANDARD_GSM
1015 pt=payload_type_clone(&payload_type_gsm);
1016 pt->clock_rate=11025;
1017 rtp_profile_set_payload(&av_profile,114,pt);
1018 linphone_payload_types=ms_list_append(linphone_payload_types,pt);
1019 pt=payload_type_clone(&payload_type_gsm);
1020 pt->clock_rate=22050;
1021 rtp_profile_set_payload(&av_profile,115,pt);
1022 linphone_payload_types=ms_list_append(linphone_payload_types,pt);
1026 #ifdef VIDEO_ENABLED
1027 linphone_core_assign_payload_type(&payload_type_h263,34,NULL);
1028 linphone_core_assign_payload_type(&payload_type_theora,97,NULL);
1029 linphone_core_assign_payload_type(&payload_type_h263_1998,98,"CIF=1;QCIF=1");
1030 linphone_core_assign_payload_type(&payload_type_mp4v,99,"profile-level-id=3");
1031 linphone_core_assign_payload_type(&payload_type_x_snow,100,NULL);
1032 linphone_core_assign_payload_type(&payload_type_h264,102,NULL);
1033 linphone_core_assign_payload_type(&payload_type_h264,103,"packetization-mode=1");
1038 lc->config=lp_config_new(config_path);
1039 if (factory_config_path)
1040 lp_config_read_file(lc->config,factory_config_path);
1043 sal_set_user_pointer(lc->sal,lc);
1044 sal_set_callbacks(lc->sal,&linphone_sal_callbacks);
1045 if (lp_config_get_int(lc->config,"sip","use_session_timers",0)==1){
1046 sal_use_session_timers(lc->sal,200);
1048 sip_setup_register_all();
1049 sound_config_read(lc);
1050 net_config_read(lc);
1051 rtp_config_read(lc);
1052 codecs_config_read(lc);
1053 sip_config_read(lc); /* this will start eXosip*/
1054 video_config_read(lc);
1055 //autoreplier_config_init(&lc->autoreplier_conf);
1056 lc->prev_mode=LINPHONE_STATUS_ONLINE;
1057 lc->presence_mode=LINPHONE_STATUS_ONLINE;
1058 lc->max_call_logs=15;
1060 lc->vtable.display_status(lc,_("Ready"));
1061 gstate_new_state(lc, GSTATE_POWER_ON, NULL);
1062 lc->auto_net_state_mon=TRUE;
1068 * Instanciates a LinphoneCore object.
1069 * @ingroup initializing
1071 * The LinphoneCore object is the primary handle for doing all phone actions.
1072 * It should be unique within your application.
1073 * @param vtable a LinphoneCoreVTable structure holding your application callbacks
1074 * @param config_path a path to a config file. If it does not exists it will be created.
1075 * The config file is used to store all user settings, call logs, friends, proxies...
1076 * @param factory_config_path a path to a read-only config file that can be used to
1077 * to store hard-coded preference such as proxy settings or internal preferences.
1078 * The settings in this factory file always override the one in the normal config file.
1079 * It is OPTIONAL, use NULL if unneeded.
1080 * @param userdata an opaque user pointer that can be retrieved at any time (for example in
1081 * callbacks) using linphone_core_get_user_data().
1084 LinphoneCore *linphone_core_new(const LinphoneCoreVTable *vtable,
1085 const char *config_path, const char *factory_config_path, void * userdata)
1087 LinphoneCore *core=ms_new(LinphoneCore,1);
1088 linphone_core_init(core,vtable,config_path, factory_config_path, userdata);
1093 * Returns the list of available audio codecs.
1095 * This list is unmodifiable. The ->data field of the MSList points a PayloadType
1096 * structure holding the codec information.
1097 * It is possible to make copy of the list with ms_list_copy() in order to modify it
1098 * (such as the order of codecs).
1100 const MSList *linphone_core_get_audio_codecs(const LinphoneCore *lc)
1102 return lc->codecs_conf.audio_codecs;
1106 * Returns the list of available video codecs.
1108 * This list is unmodifiable. The ->data field of the MSList points a PayloadType
1109 * structure holding the codec information.
1110 * It is possible to make copy of the list with ms_list_copy() in order to modify it
1111 * (such as the order of codecs).
1113 const MSList *linphone_core_get_video_codecs(const LinphoneCore *lc)
1115 return lc->codecs_conf.video_codecs;
1119 * Sets the local "from" identity.
1122 * This data is used in absence of any proxy configuration or when no
1123 * default proxy configuration is set. See LinphoneProxyConfig
1125 int linphone_core_set_primary_contact(LinphoneCore *lc, const char *contact)
1127 LinphoneAddress *ctt;
1129 if ((ctt=linphone_address_new(contact))==0) {
1130 ms_error("Bad contact url: %s",contact);
1133 if (lc->sip_conf.contact!=NULL) ms_free(lc->sip_conf.contact);
1134 lc->sip_conf.contact=ms_strdup(contact);
1135 if (lc->sip_conf.guessed_contact!=NULL){
1136 ms_free(lc->sip_conf.guessed_contact);
1137 lc->sip_conf.guessed_contact=NULL;
1139 linphone_address_destroy(ctt);
1144 /*result must be an array of chars at least LINPHONE_IPADDR_SIZE */
1145 void linphone_core_get_local_ip(LinphoneCore *lc, const char *dest, char *result){
1146 if (linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_NAT_ADDRESS){
1147 strncpy(result,linphone_core_get_nat_address(lc),LINPHONE_IPADDR_SIZE);
1150 if (dest==NULL) dest="87.98.157.38"; /*a public IP address*/
1151 if (linphone_core_get_local_ip_for(dest,result)==0)
1153 /*else fallback to SAL routine that will attempt to find the most realistic interface */
1154 sal_get_default_local_ip(lc->sal,lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,result,LINPHONE_IPADDR_SIZE);
1157 static void update_primary_contact(LinphoneCore *lc){
1159 char tmp[LINPHONE_IPADDR_SIZE];
1161 LinphoneAddress *url;
1162 if (lc->sip_conf.guessed_contact!=NULL){
1163 ms_free(lc->sip_conf.guessed_contact);
1164 lc->sip_conf.guessed_contact=NULL;
1166 url=linphone_address_new(lc->sip_conf.contact);
1168 ms_error("Could not parse identity contact !");
1169 url=linphone_address_new("sip:unknown@unkwownhost");
1171 linphone_core_get_local_ip(lc, NULL, tmp);
1172 if (strcmp(tmp,"127.0.0.1")==0 || strcmp(tmp,"::1")==0 ){
1173 ms_warning("Local loopback network only !");
1174 lc->sip_conf.loopback_only=TRUE;
1175 }else lc->sip_conf.loopback_only=FALSE;
1176 linphone_address_set_domain(url,tmp);
1177 linphone_address_set_port_int(url,lc->sip_conf.sip_port);
1178 guessed=linphone_address_as_string(url);
1179 lc->sip_conf.guessed_contact=guessed;
1180 linphone_address_destroy(url);
1184 * Returns the default identity when no proxy configuration is used.
1188 const char *linphone_core_get_primary_contact(LinphoneCore *lc){
1191 if (lc->sip_conf.guess_hostname){
1192 if (lc->sip_conf.guessed_contact==NULL || lc->sip_conf.loopback_only){
1193 update_primary_contact(lc);
1195 identity=lc->sip_conf.guessed_contact;
1197 identity=lc->sip_conf.contact;
1203 * Tells LinphoneCore to guess local hostname automatically in primary contact.
1207 void linphone_core_set_guess_hostname(LinphoneCore *lc, bool_t val){
1208 lc->sip_conf.guess_hostname=val;
1212 * Returns TRUE if hostname part of primary contact is guessed automatically.
1216 bool_t linphone_core_get_guess_hostname(LinphoneCore *lc){
1217 return lc->sip_conf.guess_hostname;
1221 * Same as linphone_core_get_primary_contact() but the result is a LinphoneAddress object
1222 * instead of const char*
1226 LinphoneAddress *linphone_core_get_primary_contact_parsed(LinphoneCore *lc){
1227 return linphone_address_new(linphone_core_get_primary_contact(lc));
1231 * Sets the list of audio codecs.
1233 * @ingroup media_parameters
1234 * The list is taken by the LinphoneCore thus the application should not free it.
1235 * This list is made of struct PayloadType describing the codec parameters.
1237 int linphone_core_set_audio_codecs(LinphoneCore *lc, MSList *codecs)
1239 if (lc->codecs_conf.audio_codecs!=NULL) ms_list_free(lc->codecs_conf.audio_codecs);
1240 lc->codecs_conf.audio_codecs=codecs;
1245 * Sets the list of video 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_video_codecs(LinphoneCore *lc, MSList *codecs)
1253 if (lc->codecs_conf.video_codecs!=NULL) ms_list_free(lc->codecs_conf.video_codecs);
1254 lc->codecs_conf.video_codecs=codecs;
1258 const MSList * linphone_core_get_friend_list(const LinphoneCore *lc)
1264 * Returns the nominal jitter buffer size in milliseconds.
1266 * @ingroup media_parameters
1268 int linphone_core_get_audio_jittcomp(LinphoneCore *lc)
1270 return lc->rtp_conf.audio_jitt_comp;
1274 * Returns the UDP port used for audio streaming.
1276 * @ingroup network_parameters
1278 int linphone_core_get_audio_port(const LinphoneCore *lc)
1280 return lc->rtp_conf.audio_rtp_port;
1284 * Returns the UDP port used for video streaming.
1286 * @ingroup network_parameters
1288 int linphone_core_get_video_port(const LinphoneCore *lc){
1289 return lc->rtp_conf.video_rtp_port;
1294 * Returns the value in seconds of the no-rtp timeout.
1296 * @ingroup media_parameters
1297 * When no RTP or RTCP packets have been received for a while
1298 * LinphoneCore will consider the call is broken (remote end crashed or
1299 * disconnected from the network), and thus will terminate the call.
1300 * The no-rtp timeout is the duration above which the call is considered broken.
1302 int linphone_core_get_nortp_timeout(const LinphoneCore *lc){
1303 return lc->rtp_conf.nortp_timeout;
1307 * Sets the nominal audio jitter buffer size in milliseconds.
1309 * @ingroup media_parameters
1311 void linphone_core_set_audio_jittcomp(LinphoneCore *lc, int value)
1313 lc->rtp_conf.audio_jitt_comp=value;
1317 * Sets the UDP port used for audio streaming.
1319 * @ingroup network_parameters
1321 void linphone_core_set_audio_port(LinphoneCore *lc, int port)
1323 lc->rtp_conf.audio_rtp_port=port;
1327 * Sets the UDP port used for video streaming.
1329 * @ingroup network_parameters
1331 void linphone_core_set_video_port(LinphoneCore *lc, int port){
1332 lc->rtp_conf.video_rtp_port=port;
1336 * Sets the no-rtp timeout value in seconds.
1338 * @ingroup media_parameters
1339 * See linphone_core_get_nortp_timeout() for details.
1341 void linphone_core_set_nortp_timeout(LinphoneCore *lc, int nortp_timeout){
1342 lc->rtp_conf.nortp_timeout=nortp_timeout;
1346 * Indicates whether SIP INFO is used for sending digits.
1348 * @ingroup media_parameters
1350 bool_t linphone_core_get_use_info_for_dtmf(LinphoneCore *lc)
1352 return lc->sip_conf.use_info;
1356 * Sets whether SIP INFO is to be used for sending digits.
1358 * @ingroup media_parameters
1360 void linphone_core_set_use_info_for_dtmf(LinphoneCore *lc,bool_t use_info)
1362 lc->sip_conf.use_info=use_info;
1366 * Indicates whether RFC2833 is used for sending digits.
1368 * @ingroup media_parameters
1370 bool_t linphone_core_get_use_rfc2833_for_dtmf(LinphoneCore *lc)
1372 return lc->sip_conf.use_rfc2833;
1376 * Sets whether RFC2833 is to be used for sending digits.
1378 * @ingroup media_parameters
1380 void linphone_core_set_use_rfc2833_for_dtmf(LinphoneCore *lc,bool_t use_rfc2833)
1382 lc->sip_conf.use_rfc2833=use_rfc2833;
1386 * Returns the UDP port used by SIP.
1388 * @ingroup network_parameters
1390 int linphone_core_get_sip_port(LinphoneCore *lc)
1392 return lc->sip_conf.sip_port;
1395 static char _ua_name[64]="Linphone";
1396 static char _ua_version[64]=LINPHONE_VERSION;
1398 #ifdef HAVE_EXOSIP_GET_VERSION
1399 extern const char *eXosip_get_version();
1402 static void apply_user_agent(LinphoneCore *lc){
1403 char ua_string[256];
1404 snprintf(ua_string,sizeof(ua_string),"%s/%s (eXosip2/%s)",_ua_name,_ua_version,
1405 #ifdef HAVE_EXOSIP_GET_VERSION
1406 eXosip_get_version()
1411 if (lc->sal) sal_set_user_agent(lc->sal,ua_string);
1415 * Sets the user agent string used in SIP messages.
1419 void linphone_core_set_user_agent(const char *name, const char *ver){
1420 strncpy(_ua_name,name,sizeof(_ua_name)-1);
1421 strncpy(_ua_version,ver,sizeof(_ua_version));
1425 * Sets the UDP port to be used by SIP.
1427 * @ingroup network_parameters
1429 void linphone_core_set_sip_port(LinphoneCore *lc,int port)
1431 const char *anyaddr;
1433 if (port==lc->sip_conf.sip_port) return;
1434 lc->sip_conf.sip_port=port;
1436 if (lc->sal==NULL) return;
1438 if (lc->sip_conf.ipv6_enabled)
1442 err=sal_listen_port (lc->sal,anyaddr,port, SalTransportDatagram,FALSE);
1444 char *msg=ortp_strdup_printf("UDP port %i seems already in use ! Cannot initialize.",port);
1446 lc->vtable.display_warning(lc,msg);
1450 apply_user_agent(lc);
1454 * Returns TRUE if IPv6 is enabled.
1456 * @ingroup network_parameters
1457 * See linphone_core_enable_ipv6() for more details on how IPv6 is supported in liblinphone.
1459 bool_t linphone_core_ipv6_enabled(LinphoneCore *lc){
1460 return lc->sip_conf.ipv6_enabled;
1464 * Turns IPv6 support on or off.
1466 * @ingroup network_parameters
1468 * @note IPv6 support is exclusive with IPv4 in liblinphone:
1469 * when IPv6 is turned on, IPv4 calls won't be possible anymore.
1470 * By default IPv6 support is off.
1472 void linphone_core_enable_ipv6(LinphoneCore *lc, bool_t val){
1473 if (lc->sip_conf.ipv6_enabled!=val){
1474 lc->sip_conf.ipv6_enabled=val;
1476 /* we need to restart eXosip */
1477 linphone_core_set_sip_port(lc, lc->sip_conf.sip_port);
1482 static void display_bandwidth(RtpSession *as, RtpSession *vs){
1483 ms_message("bandwidth usage: audio=[d=%.1f,u=%.1f] video=[d=%.1f,u=%.1f] kbit/sec",
1484 (as!=NULL) ? (rtp_session_compute_recv_bandwidth(as)*1e-3) : 0,
1485 (as!=NULL) ? (rtp_session_compute_send_bandwidth(as)*1e-3) : 0,
1486 (vs!=NULL) ? (rtp_session_compute_recv_bandwidth(vs)*1e-3) : 0,
1487 (vs!=NULL) ? (rtp_session_compute_send_bandwidth(vs)*1e-3) : 0);
1490 static void linphone_core_disconnected(LinphoneCore *lc){
1491 lc->vtable.display_warning(lc,_("Remote end seems to have disconnected, the call is going to be closed."));
1492 linphone_core_terminate_call(lc,NULL);
1495 static void monitor_network_state(LinphoneCore *lc, time_t curtime){
1496 static time_t last_check=0;
1497 static bool_t last_status=FALSE;
1498 char result[LINPHONE_IPADDR_SIZE];
1501 /* only do the network up checking every five seconds */
1502 if (last_check==0 || (curtime-last_check)>=5){
1503 sal_get_default_local_ip(lc->sal,
1504 lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,
1505 result,LINPHONE_IPADDR_SIZE);
1506 if (strcmp(result,"::1")!=0 && strcmp(result,"127.0.0.1")!=0){
1508 }else new_status=FALSE;
1510 if (new_status!=last_status) {
1511 set_network_reachable(lc,new_status);
1512 last_status=new_status;
1517 static void proxy_update(LinphoneCore *lc){
1518 ms_list_for_each(lc->sip_conf.proxies,(void (*)(void*))&linphone_proxy_config_update);
1521 static void assign_buddy_info(LinphoneCore *lc, BuddyInfo *info){
1522 LinphoneFriend *lf=linphone_core_get_friend_by_address(lc,info->sip_uri);
1525 ms_message("%s has a BuddyInfo assigned with image %p",info->sip_uri, info->image_data);
1526 if (lc->vtable.buddy_info_updated)
1527 lc->vtable.buddy_info_updated(lc,lf);
1529 ms_warning("Could not any friend with uri %s",info->sip_uri);
1533 static void analyze_buddy_lookup_results(LinphoneCore *lc, LinphoneProxyConfig *cfg){
1535 SipSetupContext *ctx=linphone_proxy_config_get_sip_setup_context(cfg);
1536 for (elem=lc->bl_reqs;elem!=NULL;elem=ms_list_next(elem)){
1537 BuddyLookupRequest *req=(BuddyLookupRequest *)elem->data;
1538 if (req->status==BuddyLookupDone || req->status==BuddyLookupFailure){
1539 if (req->results!=NULL){
1540 BuddyInfo *i=(BuddyInfo*)req->results->data;
1541 ms_list_free(req->results);
1543 assign_buddy_info(lc,i);
1545 sip_setup_context_buddy_lookup_free(ctx,req);
1549 /*purge completed requests */
1550 while((elem=ms_list_find(lc->bl_reqs,NULL))!=NULL){
1551 lc->bl_reqs=ms_list_remove_link(lc->bl_reqs,elem);
1555 static void linphone_core_grab_buddy_infos(LinphoneCore *lc, LinphoneProxyConfig *cfg){
1557 SipSetupContext *ctx=linphone_proxy_config_get_sip_setup_context(cfg);
1558 for(elem=linphone_core_get_friend_list(lc);elem!=NULL;elem=elem->next){
1559 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
1560 if (lf->info==NULL){
1561 if (linphone_core_lookup_known_proxy(lc,lf->uri)==cfg){
1562 if (linphone_address_get_username(lf->uri)!=NULL){
1563 BuddyLookupRequest *req;
1564 char *tmp=linphone_address_as_string_uri_only(lf->uri);
1565 req=sip_setup_context_create_buddy_lookup_request(ctx);
1566 buddy_lookup_request_set_key(req,tmp);
1567 buddy_lookup_request_set_max_results(req,1);
1568 sip_setup_context_buddy_lookup_submit(ctx,req);
1569 lc->bl_reqs=ms_list_append(lc->bl_reqs,req);
1577 static void linphone_core_do_plugin_tasks(LinphoneCore *lc){
1578 LinphoneProxyConfig *cfg=NULL;
1579 linphone_core_get_default_proxy(lc,&cfg);
1581 if (lc->bl_refresh){
1582 SipSetupContext *ctx=linphone_proxy_config_get_sip_setup_context(cfg);
1583 if (ctx && (sip_setup_context_get_capabilities(ctx) & SIP_SETUP_CAP_BUDDY_LOOKUP)){
1584 linphone_core_grab_buddy_infos(lc,cfg);
1585 lc->bl_refresh=FALSE;
1588 if (lc->bl_reqs) analyze_buddy_lookup_results(lc,cfg);
1593 * Main loop function. It is crucial that your application call it periodically.
1595 * @ingroup initializing
1596 * linphone_core_iterate() performs various backgrounds tasks:
1597 * - receiving of SIP messages
1598 * - handles timers and timeout
1599 * - performs registration to proxies
1600 * - authentication retries
1601 * The application MUST call this function from periodically, in its main loop.
1602 * Be careful that this function must be call from the same thread as
1603 * other liblinphone methods. In not the case make sure all liblinphone calls are
1604 * serialized with a mutex.
1606 void linphone_core_iterate(LinphoneCore *lc){
1607 int disconnect_timeout = linphone_core_get_nortp_timeout(lc);
1608 time_t curtime=time(NULL);
1610 bool_t one_second_elapsed=FALSE;
1611 bool_t disconnected=FALSE;
1613 if (curtime-lc->prevtime>=1){
1614 lc->prevtime=curtime;
1615 one_second_elapsed=TRUE;
1618 if (lc->preview_finished){
1619 lc->preview_finished=0;
1620 ring_stop(lc->ringstream);
1621 lc->ringstream=NULL;
1622 lc_callback_obj_invoke(&lc->preview_finished_cb,lc);
1625 sal_iterate(lc->sal);
1626 if (lc->auto_net_state_mon) monitor_network_state(lc,curtime);
1630 if (lc->call!=NULL){
1631 LinphoneCall *call=lc->call;
1632 if (call->state==LCStatePreEstablishing && (curtime-call->start_time>=2)){
1633 /*start the call even if the OPTIONS reply did not arrive*/
1634 linphone_core_start_invite(lc,call,NULL);
1636 if (call->dir==LinphoneCallIncoming && call->state==LCStateRinging){
1637 elapsed=curtime-call->start_time;
1638 ms_message("incoming call ringing for %i seconds",elapsed);
1639 if (elapsed>lc->sip_conf.inc_timeout){
1640 linphone_core_terminate_call(lc,NULL);
1642 }else if (call->state==LCStateAVRunning){
1643 if (one_second_elapsed){
1644 RtpSession *as=NULL,*vs=NULL;
1645 lc->prevtime=curtime;
1646 if (lc->audiostream!=NULL)
1647 as=lc->audiostream->session;
1648 if (lc->videostream!=NULL)
1649 vs=lc->videostream->session;
1650 display_bandwidth(as,vs);
1652 #ifdef VIDEO_ENABLED
1653 if (lc->videostream!=NULL)
1654 video_stream_iterate(lc->videostream);
1656 if (lc->audiostream!=NULL && disconnect_timeout>0)
1657 disconnected=!audio_stream_alive(lc->audiostream,disconnect_timeout);
1660 if (linphone_core_video_preview_enabled(lc)){
1661 if (lc->previewstream==NULL)
1662 toggle_video_preview(lc,TRUE);
1663 #ifdef VIDEO_ENABLED
1664 else video_stream_iterate(lc->previewstream);
1667 if (lc->previewstream!=NULL)
1668 toggle_video_preview(lc,FALSE);
1671 linphone_core_disconnected(lc);
1673 linphone_core_do_plugin_tasks(lc);
1675 if (one_second_elapsed && lp_config_needs_commit(lc->config)){
1676 lp_config_sync(lc->config);
1681 bool_t linphone_core_interpret_url(LinphoneCore *lc, const char *url, LinphoneAddress **real_parsed_url, char **route){
1682 enum_lookup_res_t *enumres=NULL;
1683 LinphoneAddress *parsed_url=NULL;
1684 char *enum_domain=NULL;
1685 LinphoneProxyConfig *proxy=lc->default_proxy;;
1687 const char *tmproute;
1688 LinphoneAddress *uri;
1690 if (real_parsed_url!=NULL) *real_parsed_url=NULL;
1692 tmproute=linphone_core_get_route(lc);
1694 if (is_enum(url,&enum_domain)){
1695 lc->vtable.display_status(lc,_("Looking for telephone number destination..."));
1696 if (enum_lookup(enum_domain,&enumres)<0){
1697 lc->vtable.display_status(lc,_("Could not resolve this number."));
1698 ms_free(enum_domain);
1701 ms_free(enum_domain);
1702 tmpurl=enumres->sip_address[0];
1703 if (real_parsed_url!=NULL) *real_parsed_url=linphone_address_new(tmpurl);
1704 enum_lookup_res_free(enumres);
1705 if (tmproute) *route=ms_strdup(tmproute);
1708 /* check if we have a "sip:" */
1709 if (strstr(url,"sip:")==NULL){
1710 /* this doesn't look like a true sip uri */
1711 if (strchr(url,'@')!=NULL){
1712 /* seems like sip: is missing !*/
1713 tmpurl=ms_strdup_printf("sip:%s",url);
1714 uri=linphone_address_new(tmpurl);
1717 if (real_parsed_url!=NULL) *real_parsed_url=uri;
1723 /* append the proxy domain suffix */
1724 const char *identity=linphone_proxy_config_get_identity(proxy);
1725 char normalized_username[128];
1726 uri=linphone_address_new(identity);
1730 linphone_address_set_display_name(uri,NULL);
1731 linphone_proxy_config_normalize_number(proxy,url,normalized_username,
1732 sizeof(normalized_username));
1733 linphone_address_set_username(uri,normalized_username);
1735 if (real_parsed_url!=NULL) *real_parsed_url=uri;
1736 if (tmproute) *route=ms_strdup(tmproute);
1740 parsed_url=linphone_address_new(url);
1741 if (parsed_url!=NULL){
1742 if (real_parsed_url!=NULL) *real_parsed_url=parsed_url;
1743 else linphone_address_destroy(parsed_url);
1744 if (tmproute) *route=ms_strdup(tmproute);
1748 /* else we could not do anything with url given by user, so display an error */
1749 if (lc->vtable.display_warning!=NULL){
1750 lc->vtable.display_warning(lc,_("Could not parse given sip address. A sip url usually looks like sip:user@domain"));
1756 * Returns the default identity SIP address.
1759 * This is an helper function:
1761 * If no default proxy is set, this will return the primary contact (
1762 * see linphone_core_get_primary_contact() ). If a default proxy is set
1763 * it returns the registered identity on the proxy.
1765 const char * linphone_core_get_identity(LinphoneCore *lc){
1766 LinphoneProxyConfig *proxy=NULL;
1768 linphone_core_get_default_proxy(lc,&proxy);
1770 from=linphone_proxy_config_get_identity(proxy);
1771 }else from=linphone_core_get_primary_contact(lc);
1775 const char * linphone_core_get_route(LinphoneCore *lc){
1776 LinphoneProxyConfig *proxy=NULL;
1777 const char *route=NULL;
1778 linphone_core_get_default_proxy(lc,&proxy);
1780 route=linphone_proxy_config_get_route(proxy);
1785 LinphoneProxyConfig * linphone_core_lookup_known_proxy(LinphoneCore *lc, const LinphoneAddress *uri){
1787 LinphoneProxyConfig *found_cfg=NULL;
1788 for (elem=linphone_core_get_proxy_config_list(lc);elem!=NULL;elem=elem->next){
1789 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
1790 const char *domain=linphone_proxy_config_get_domain(cfg);
1791 if (domain!=NULL && strcmp(domain,linphone_address_get_domain(uri))==0){
1799 static char *get_fixed_contact(LinphoneCore *lc, LinphoneCall *call , LinphoneProxyConfig *dest_proxy){
1800 LinphoneAddress *ctt;
1801 const char *localip=call->localip;
1803 /* first use user's supplied ip address if asked*/
1804 if (linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_NAT_ADDRESS){
1805 ctt=linphone_core_get_primary_contact_parsed(lc);
1806 return ms_strdup_printf("sip:%s@%s",linphone_address_get_username(ctt),
1807 linphone_core_get_nat_address(lc));
1810 /* if already choosed, don't change it */
1811 if (call->op && sal_op_get_contact(call->op)!=NULL){
1814 /*if using a proxy, use the contact address as guessed with the REGISTERs*/
1815 if (dest_proxy && dest_proxy->op){
1816 const char *fixed_contact=sal_op_get_contact(dest_proxy->op);
1817 if (fixed_contact) {
1818 ms_message("Contact has been fixed using proxy to %s",fixed_contact);
1819 return ms_strdup(fixed_contact);
1822 /* if the ping OPTIONS request succeeded use the contact guessed from the
1825 const char *guessed=sal_op_get_contact(call->ping_op);
1827 ms_message("Contact has been fixed using OPTIONS to %s",guessed);
1828 return ms_strdup(guessed);
1832 ctt=linphone_core_get_primary_contact_parsed(lc);
1836 /*otherwise use supllied localip*/
1837 linphone_address_set_domain(ctt,localip);
1838 linphone_address_set_port_int(ctt,linphone_core_get_sip_port(lc));
1839 ret=linphone_address_as_string_uri_only(ctt);
1840 linphone_address_destroy(ctt);
1841 ms_message("Contact has been fixed using local ip to %s",ret);
1847 int linphone_core_start_invite(LinphoneCore *lc, LinphoneCall *call, LinphoneProxyConfig *dest_proxy){
1850 char *real_url,*barmsg;
1852 /*try to be best-effort in giving real local or routable contact address */
1853 contact=get_fixed_contact(lc,call,dest_proxy);
1855 sal_op_set_contact(call->op, contact);
1858 call->state=LCStateInit;
1859 linphone_core_init_media_streams(lc,lc->call);
1860 if (!lc->sip_conf.sdp_200_ack){
1861 call->media_pending=TRUE;
1862 sal_call_set_local_media_description(call->op,call->localdesc);
1864 real_url=linphone_address_as_string(call->log->to);
1865 from=linphone_address_as_string(call->log->from);
1866 err=sal_call(call->op,from,real_url);
1868 if (lc->sip_conf.sdp_200_ack){
1869 call->media_pending=TRUE;
1870 sal_call_set_local_media_description(call->op,call->localdesc);
1872 barmsg=ortp_strdup_printf("%s %s", _("Contacting"), real_url);
1873 lc->vtable.display_status(lc,barmsg);
1877 ms_warning("Could not initiate call.");
1878 lc->vtable.display_status(lc,_("could not call"));
1879 linphone_core_stop_media_streams(lc,call);
1880 linphone_call_destroy(call);
1882 }else gstate_new_state(lc, GSTATE_CALL_OUT_INVITE, real_url);
1889 * Initiates an outgoing call
1891 * @ingroup call_control
1892 * @param lc the LinphoneCore object
1893 * @param url the destination of the call (sip address).
1895 int linphone_core_invite(LinphoneCore *lc, const char *url)
1899 const char *from=NULL;
1900 LinphoneProxyConfig *proxy=NULL;
1901 LinphoneAddress *parsed_url2=NULL;
1902 LinphoneAddress *real_parsed_url=NULL;
1903 char *real_url=NULL;
1904 LinphoneProxyConfig *dest_proxy=NULL;
1907 if (lc->call!=NULL){
1908 lc->vtable.display_warning(lc,_("Sorry, having multiple simultaneous calls is not supported yet !"));
1912 linphone_core_get_default_proxy(lc,&proxy);
1913 if (!linphone_core_interpret_url(lc,url,&real_parsed_url,&route)){
1916 real_url=linphone_address_as_string(real_parsed_url);
1917 dest_proxy=linphone_core_lookup_known_proxy(lc,real_parsed_url);
1919 if (proxy!=dest_proxy && dest_proxy!=NULL) {
1920 ms_message("Overriding default proxy setting for this call:");
1921 ms_message("The used identity will be %s",linphone_proxy_config_get_identity(dest_proxy));
1924 if (dest_proxy!=NULL)
1925 from=linphone_proxy_config_get_identity(dest_proxy);
1926 else if (proxy!=NULL)
1927 from=linphone_proxy_config_get_identity(proxy);
1929 /* if no proxy or no identity defined for this proxy, default to primary contact*/
1930 if (from==NULL) from=linphone_core_get_primary_contact(lc);
1932 parsed_url2=linphone_address_new(from);
1934 call=linphone_call_new_outgoing(lc,parsed_url2,real_parsed_url);
1935 sal_op_set_route(call->op,route);
1938 if (dest_proxy!=NULL || lc->sip_conf.ping_with_options==FALSE){
1939 err=linphone_core_start_invite(lc,call,dest_proxy);
1941 /*defer the start of the call after the OPTIONS ping*/
1942 call->state=LCStatePreEstablishing;
1943 call->ping_op=sal_op_new(lc->sal);
1944 sal_ping(call->ping_op,from,real_url);
1945 call->start_time=time(NULL);
1948 if (real_url!=NULL) ms_free(real_url);
1949 if (route!=NULL) ms_free(route);
1953 int linphone_core_refer(LinphoneCore *lc, const char *url)
1955 char *real_url=NULL;
1956 LinphoneAddress *real_parsed_url=NULL;
1959 if (!linphone_core_interpret_url(lc,url,&real_parsed_url, &route)){
1963 if (route!=NULL) ms_free(route);
1966 ms_warning("No established call to refer.");
1970 real_url=linphone_address_as_string (real_parsed_url);
1971 sal_refer(call->op,real_url);
1977 * Returns true if in incoming call is pending, ie waiting for being answered or declined.
1979 * @ingroup call_control
1981 bool_t linphone_core_inc_invite_pending(LinphoneCore*lc){
1982 if (lc->call!=NULL && lc->call->dir==LinphoneCallIncoming){
1988 void linphone_core_init_media_streams(LinphoneCore *lc, LinphoneCall *call){
1989 SalMediaDescription *md=call->localdesc;
1990 lc->audiostream=audio_stream_new(md->streams[0].port,linphone_core_ipv6_enabled(lc));
1991 if (linphone_core_echo_limiter_enabled(lc)){
1992 const char *type=lp_config_get_string(lc->config,"sound","el_type","mic");
1993 if (strcasecmp(type,"mic")==0)
1994 audio_stream_enable_echo_limiter(lc->audiostream,ELControlMic);
1995 else if (strcasecmp(type,"speaker")==0)
1996 audio_stream_enable_echo_limiter(lc->audiostream,ELControlSpeaker);
1998 audio_stream_enable_gain_control(lc->audiostream,TRUE);
1999 if (linphone_core_echo_cancellation_enabled(lc)){
2000 int len,delay,framesize;
2001 len=lp_config_get_int(lc->config,"sound","ec_tail_len",0);
2002 delay=lp_config_get_int(lc->config,"sound","ec_delay",0);
2003 framesize=lp_config_get_int(lc->config,"sound","ec_framesize",0);
2004 audio_stream_set_echo_canceller_params(lc->audiostream,len,delay,framesize);
2006 audio_stream_enable_automatic_gain_control(lc->audiostream,linphone_core_agc_enabled(lc));
2008 int enabled=lp_config_get_int(lc->config,"sound","noisegate",0);
2009 audio_stream_enable_noise_gate(lc->audiostream,enabled);
2012 rtp_session_set_transports(lc->audiostream->session,lc->a_rtp,lc->a_rtcp);
2014 #ifdef VIDEO_ENABLED
2015 if ((lc->video_conf.display || lc->video_conf.capture) && md->streams[1].port>0)
2016 lc->videostream=video_stream_new(md->streams[1].port,linphone_core_ipv6_enabled(lc));
2018 lc->videostream=NULL;
2022 static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'};
2024 static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
2025 LinphoneCore* lc = (LinphoneCore*)user_data;
2026 if (dtmf<0 || dtmf>15){
2027 ms_warning("Bad dtmf value %i",dtmf);
2030 if (lc->vtable.dtmf_received != NULL)
2031 lc->vtable.dtmf_received(lc, dtmf_tab[dtmf]);
2034 static void parametrize_equalizer(LinphoneCore *lc, AudioStream *st){
2036 MSFilter *f=st->equalizer;
2037 int enabled=lp_config_get_int(lc->config,"sound","eq_active",0);
2038 const char *gains=lp_config_get_string(lc->config,"sound","eq_gains",NULL);
2039 ms_filter_call_method(f,MS_EQUALIZER_SET_ACTIVE,&enabled);
2045 if (sscanf(gains,"%f:%f:%f %n",&g.frequency,&g.gain,&g.width,&bytes)==3){
2046 ms_message("Read equalizer gains: %f(~%f) --> %f",g.frequency,g.width,g.gain);
2047 ms_filter_call_method(f,MS_EQUALIZER_SET_GAIN,&g);
2056 static void post_configure_audio_streams(LinphoneCore *lc){
2057 AudioStream *st=lc->audiostream;
2058 float gain=lp_config_get_float(lc->config,"sound","mic_gain",-1);
2060 audio_stream_set_mic_gain(st,gain);
2061 float recv_gain = lc->sound_conf.soft_play_lev;
2062 if (recv_gain != 0) {
2063 linphone_core_set_soft_play_level(lc,recv_gain);
2065 if (linphone_core_echo_limiter_enabled(lc)){
2066 float speed=lp_config_get_float(lc->config,"sound","el_speed",-1);
2067 float thres=lp_config_get_float(lc->config,"sound","el_thres",-1);
2068 float force=lp_config_get_float(lc->config,"sound","el_force",-1);
2069 int sustain=lp_config_get_int(lc->config,"sound","el_sustain",-1);
2071 if (st->el_type==ELControlMic){
2073 if (speed==-1) speed=0.03;
2074 if (force==-1) force=10;
2076 else if (st->el_type==ELControlSpeaker){
2078 if (speed==-1) speed=0.02;
2079 if (force==-1) force=5;
2082 ms_filter_call_method(f,MS_VOLUME_SET_EA_SPEED,&speed);
2084 ms_filter_call_method(f,MS_VOLUME_SET_EA_THRESHOLD,&thres);
2086 ms_filter_call_method(f,MS_VOLUME_SET_EA_FORCE,&force);
2088 ms_filter_call_method(f,MS_VOLUME_SET_EA_SUSTAIN,&sustain);
2092 float ng_thres=lp_config_get_float(lc->config,"sound","ng_thres",0.05);
2093 float ng_floorgain=lp_config_get_float(lc->config,"sound","ng_floorgain",0);
2094 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
2095 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&ng_floorgain);
2097 parametrize_equalizer(lc,st);
2098 if (lc->vtable.dtmf_received!=NULL){
2099 /* replace by our default action*/
2100 audio_stream_play_received_dtmfs(lc->audiostream,FALSE);
2101 rtp_session_signal_connect(lc->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
2105 static RtpProfile *make_profile(LinphoneCore *lc, const SalStreamDescription *desc, int *used_pt){
2108 RtpProfile *prof=rtp_profile_new("Call profile");
2110 if (desc->type==SalAudio){
2111 bw=get_min_bandwidth(lc->up_audio_bw,desc->bandwidth);
2113 else bw=get_min_bandwidth(lc->up_video_bw,desc->bandwidth);
2114 for(elem=desc->payloads;elem!=NULL;elem=elem->next){
2115 PayloadType *pt=(PayloadType*)elem->data;
2116 if (bw>0) pt->normal_bitrate=bw*1000;
2117 else if (desc->type==SalAudio){
2118 pt->normal_bitrate=-1;
2121 *used_pt=payload_type_get_number(pt);
2126 snprintf(tmp,sizeof(tmp),"ptime=%i",desc->ptime);
2127 payload_type_append_send_fmtp(pt,tmp);
2129 rtp_profile_set_payload(prof,payload_type_get_number(pt),pt);
2134 void linphone_core_start_media_streams(LinphoneCore *lc, LinphoneCall *call){
2135 LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
2136 const char *tool="linphone-" LINPHONE_VERSION;
2139 /* adjust rtp jitter compensation. It must be at least the latency of the sound card */
2140 int jitt_comp=MAX(lc->sound_conf.latency,lc->rtp_conf.audio_jitt_comp);
2142 if (call->media_start_time==0) call->media_start_time=time(NULL);
2144 cname=linphone_address_as_string_uri_only(me);
2146 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
2147 SalProtoRtpAvp,SalAudio);
2149 call->audio_profile=make_profile(lc,stream,&used_pt);
2150 if (!lc->use_files){
2151 MSSndCard *playcard=lc->sound_conf.play_sndcard;
2152 MSSndCard *captcard=lc->sound_conf.capt_sndcard;
2153 if (playcard==NULL) {
2154 ms_warning("No card defined for playback !");
2157 if (captcard==NULL) {
2158 ms_warning("No card defined for capture !");
2161 audio_stream_start_now(
2163 call->audio_profile,
2164 stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
2171 linphone_core_echo_cancellation_enabled(lc));
2173 audio_stream_start_with_files(
2175 call->audio_profile,
2176 stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
2184 post_configure_audio_streams(lc);
2185 audio_stream_set_rtcp_information(lc->audiostream, cname, tool);
2186 }else ms_warning("No audio stream defined ?");
2188 #ifdef VIDEO_ENABLED
2190 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
2191 SalProtoRtpAvp,SalVideo);
2192 /* shutdown preview */
2193 if (lc->previewstream!=NULL) {
2194 video_preview_stop(lc->previewstream);
2195 lc->previewstream=NULL;
2197 if (stream && (lc->video_conf.display || lc->video_conf.capture)) {
2198 const char *addr=stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr;
2199 call->video_profile=make_profile(lc,stream,&used_pt);
2200 video_stream_set_sent_video_size(lc->videostream,linphone_core_get_preferred_video_size(lc));
2201 video_stream_enable_self_view(lc->videostream,lc->video_conf.selfview);
2202 if (lc->video_conf.display && lc->video_conf.capture)
2203 video_stream_start(lc->videostream,
2204 call->video_profile, addr, stream->port,
2206 used_pt, jitt_comp, lc->video_conf.device);
2207 else if (lc->video_conf.display)
2208 video_stream_recv_only_start(lc->videostream,
2209 call->video_profile, addr, stream->port,
2210 used_pt, jitt_comp);
2211 else if (lc->video_conf.capture)
2212 video_stream_send_only_start(lc->videostream,
2213 call->video_profile, addr, stream->port,
2215 used_pt, jitt_comp, lc->video_conf.device);
2216 video_stream_set_rtcp_information(lc->videostream, cname,tool);
2223 linphone_address_destroy(me);
2224 lc->call->state=LCStateAVRunning;
2227 void linphone_core_stop_media_streams(LinphoneCore *lc, LinphoneCall *call){
2228 if (lc->audiostream!=NULL) {
2229 audio_stream_stop(lc->audiostream);
2230 lc->audiostream=NULL;
2232 #ifdef VIDEO_ENABLED
2233 if (lc->videostream!=NULL){
2234 if (lc->video_conf.display && lc->video_conf.capture)
2235 video_stream_stop(lc->videostream);
2236 else if (lc->video_conf.display)
2237 video_stream_recv_only_stop(lc->videostream);
2238 else if (lc->video_conf.capture)
2239 video_stream_send_only_stop(lc->videostream);
2240 lc->videostream=NULL;
2242 if (linphone_core_video_preview_enabled(lc)){
2243 if (lc->previewstream==NULL){
2244 lc->previewstream=video_preview_start(lc->video_conf.device, lc->video_conf.vsize);
2248 if (call->audio_profile){
2249 rtp_profile_clear_all(call->audio_profile);
2250 rtp_profile_destroy(call->audio_profile);
2251 call->audio_profile=NULL;
2253 if (call->video_profile){
2254 rtp_profile_clear_all(call->video_profile);
2255 rtp_profile_destroy(call->video_profile);
2256 call->video_profile=NULL;
2261 * Accept an incoming call.
2263 * @ingroup call_control
2264 * Basically the application is notified of incoming calls within the
2265 * invite_recv callback of the #LinphoneCoreVTable structure.
2266 * The application can later respond positively to the call using
2268 * @param lc the LinphoneCore object
2269 * @param url the SIP address of the originator of the call, or NULL.
2270 * This argument is useful for managing multiple calls simulatenously,
2271 * however this feature is not supported yet.
2272 * Using NULL will accept the unique incoming call in progress.
2274 int linphone_core_accept_call(LinphoneCore *lc, const char *url)
2276 LinphoneCall *call=lc->call;
2277 const char *contact=NULL;
2283 if (call->state==LCStateAVRunning){
2284 /*call already accepted*/
2289 if (lc->ringstream!=NULL) {
2290 ms_message("stop ringing");
2291 ring_stop(lc->ringstream);
2292 ms_message("ring stopped");
2293 lc->ringstream=NULL;
2296 /*try to be best-effort in giving real local or routable contact address*/
2297 contact=get_fixed_contact(lc,call,NULL);
2299 sal_op_set_contact(call->op,contact);
2301 sal_call_accept(call->op);
2302 lc->vtable.display_status(lc,_("Connected."));
2303 gstate_new_state(lc, GSTATE_CALL_IN_CONNECTED, NULL);
2304 call->resultdesc=sal_call_get_final_media_description(call->op);
2305 if (call->resultdesc){
2306 sal_media_description_ref(call->resultdesc);
2307 linphone_core_start_media_streams(lc, call);
2308 }else call->media_pending=TRUE;
2309 ms_message("call answered.");
2314 * Terminates a call.
2316 * @ingroup call_control
2317 * @param lc The LinphoneCore
2318 * @param url the destination of the call to be terminated, use NULL if there is
2319 * only one call (which is case in this version of liblinphone).
2321 int linphone_core_terminate_call(LinphoneCore *lc, const char *url)
2323 LinphoneCall *call=lc->call;
2328 sal_call_terminate(call->op);
2331 if (lc->ringstream!=NULL) {
2332 ring_stop(lc->ringstream);
2333 lc->ringstream=NULL;
2335 linphone_core_stop_media_streams(lc,call);
2336 lc->vtable.display_status(lc,_("Call ended") );
2337 gstate_new_state(lc, GSTATE_CALL_END, NULL);
2338 linphone_call_destroy(call);
2343 * Returns TRUE if there is a call running or pending.
2345 * @ingroup call_control
2347 bool_t linphone_core_in_call(const LinphoneCore *lc){
2348 return lc->call!=NULL;
2351 int linphone_core_send_publish(LinphoneCore *lc,
2352 LinphoneOnlineStatus presence_mode)
2355 for (elem=linphone_core_get_proxy_config_list(lc);elem!=NULL;elem=ms_list_next(elem)){
2356 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
2357 if (cfg->publish) linphone_proxy_config_send_publish(cfg,presence_mode);
2363 * Set the incoming call timeout in seconds.
2365 * @ingroup call_control
2366 * If an incoming call isn't answered for this timeout period, it is
2367 * automatically declined.
2369 void linphone_core_set_inc_timeout(LinphoneCore *lc, int seconds){
2370 lc->sip_conf.inc_timeout=seconds;
2374 * Returns the incoming call timeout
2376 * @ingroup call_control
2377 * See linphone_core_set_inc_timeout() for details.
2379 int linphone_core_get_inc_timeout(LinphoneCore *lc){
2380 return lc->sip_conf.inc_timeout;
2383 void linphone_core_set_presence_info(LinphoneCore *lc,int minutes_away,
2384 const char *contact,
2385 LinphoneOnlineStatus presence_mode)
2387 if (minutes_away>0) lc->minutes_away=minutes_away;
2389 if (lc->alt_contact!=NULL) {
2390 ms_free(lc->alt_contact);
2391 lc->alt_contact=NULL;
2393 if (contact) lc->alt_contact=ms_strdup(contact);
2394 if (lc->presence_mode!=presence_mode){
2395 linphone_core_notify_all_friends(lc,presence_mode);
2397 Improve the use of all LINPHONE_STATUS available.
2398 !TODO Do not mix "presence status" with "answer status code"..
2399 Use correct parameter to follow sip_if_match/sip_etag.
2401 linphone_core_send_publish(lc,presence_mode);
2403 lc->prev_mode=lc->presence_mode;
2404 lc->presence_mode=presence_mode;
2407 LinphoneOnlineStatus linphone_core_get_presence_info(const LinphoneCore *lc){
2408 return lc->presence_mode;
2412 * Get playback sound level in 0-100 scale.
2414 * @ingroup media_parameters
2416 int linphone_core_get_play_level(LinphoneCore *lc)
2418 return lc->sound_conf.play_lev;
2422 * Get ring sound level in 0-100 scale
2424 * @ingroup media_parameters
2426 int linphone_core_get_ring_level(LinphoneCore *lc)
2428 return lc->sound_conf.ring_lev;
2432 * Get sound capture level in 0-100 scale
2434 * @ingroup media_parameters
2436 int linphone_core_get_rec_level(LinphoneCore *lc){
2437 return lc->sound_conf.rec_lev;
2441 * Set sound ring level in 0-100 scale
2443 * @ingroup media_parameters
2445 void linphone_core_set_ring_level(LinphoneCore *lc, int level){
2447 lc->sound_conf.ring_lev=level;
2448 sndcard=lc->sound_conf.ring_sndcard;
2449 if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_PLAYBACK,level);
2453 void linphone_core_set_soft_play_level(LinphoneCore *lc, float level){
2455 lc->sound_conf.soft_play_lev=level;
2456 AudioStream *st=lc->audiostream;
2457 if (!st) return; /*just return*/
2460 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_DB_GAIN,&gain);
2461 }else ms_warning("Could not apply gain: gain control wasn't activated.");
2463 float linphone_core_get_soft_play_level(LinphoneCore *lc) {
2465 AudioStream *st=lc->audiostream;
2467 ms_filter_call_method(st->volrecv,MS_VOLUME_GET,&gain);
2468 }else ms_warning("Could not get gain: gain control wasn't activated.");
2474 * Set sound playback level in 0-100 scale
2476 * @ingroup media_parameters
2478 void linphone_core_set_play_level(LinphoneCore *lc, int level){
2480 lc->sound_conf.play_lev=level;
2481 sndcard=lc->sound_conf.play_sndcard;
2482 if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_PLAYBACK,level);
2486 * Set sound capture level in 0-100 scale
2488 * @ingroup media_parameters
2490 void linphone_core_set_rec_level(LinphoneCore *lc, int level)
2493 lc->sound_conf.rec_lev=level;
2494 sndcard=lc->sound_conf.capt_sndcard;
2495 if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_CAPTURE,level);
2498 static MSSndCard *get_card_from_string_id(const char *devid, unsigned int cap){
2499 MSSndCard *sndcard=NULL;
2501 sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
2502 if (sndcard!=NULL &&
2503 (ms_snd_card_get_capabilities(sndcard) & cap)==0 ){
2504 ms_warning("%s card does not have the %s capability, ignoring.",
2506 cap==MS_SND_CARD_CAP_CAPTURE ? "capture" : "playback");
2510 if (sndcard==NULL) {
2511 /* get a card that has read+write capabilities */
2512 sndcard=ms_snd_card_manager_get_default_card(ms_snd_card_manager_get());
2513 /* otherwise refine to the first card having the right capability*/
2515 const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
2516 for(;elem!=NULL;elem=elem->next){
2517 sndcard=(MSSndCard*)elem->data;
2518 if (ms_snd_card_get_capabilities(sndcard) & cap) break;
2521 if (sndcard==NULL){/*looks like a bug! take the first one !*/
2522 const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
2523 sndcard=(MSSndCard*)elem->data;
2526 if (sndcard==NULL) ms_error("Could not find a suitable soundcard !");
2531 * Returns true if the specified sound device can capture sound.
2533 * @ingroup media_parameters
2534 * @param devid the device name as returned by linphone_core_get_sound_devices()
2536 bool_t linphone_core_sound_device_can_capture(LinphoneCore *lc, const char *devid){
2538 sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
2539 if (sndcard!=NULL && (ms_snd_card_get_capabilities(sndcard) & MS_SND_CARD_CAP_CAPTURE)) return TRUE;
2544 * Returns true if the specified sound device can play sound.
2546 * @ingroup media_parameters
2547 * @param devid the device name as returned by linphone_core_get_sound_devices()
2549 bool_t linphone_core_sound_device_can_playback(LinphoneCore *lc, const char *devid){
2551 sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
2552 if (sndcard!=NULL && (ms_snd_card_get_capabilities(sndcard) & MS_SND_CARD_CAP_PLAYBACK)) return TRUE;
2557 * Sets the sound device used for ringing.
2559 * @ingroup media_parameters
2560 * @param devid the device name as returned by linphone_core_get_sound_devices()
2562 int linphone_core_set_ringer_device(LinphoneCore *lc, const char * devid){
2563 MSSndCard *card=get_card_from_string_id(devid,MS_SND_CARD_CAP_PLAYBACK);
2564 lc->sound_conf.ring_sndcard=card;
2565 if (card && lc->ready)
2566 lp_config_set_string(lc->config,"sound","ringer_dev_id",ms_snd_card_get_string_id(card));
2571 * Sets the sound device used for playback.
2573 * @ingroup media_parameters
2574 * @param devid the device name as returned by linphone_core_get_sound_devices()
2576 int linphone_core_set_playback_device(LinphoneCore *lc, const char * devid){
2577 MSSndCard *card=get_card_from_string_id(devid,MS_SND_CARD_CAP_PLAYBACK);
2578 lc->sound_conf.play_sndcard=card;
2579 if (card && lc->ready)
2580 lp_config_set_string(lc->config,"sound","playback_dev_id",ms_snd_card_get_string_id(card));
2585 * Sets the sound device used for capture.
2587 * @ingroup media_parameters
2588 * @param devid the device name as returned by linphone_core_get_sound_devices()
2590 int linphone_core_set_capture_device(LinphoneCore *lc, const char * devid){
2591 MSSndCard *card=get_card_from_string_id(devid,MS_SND_CARD_CAP_CAPTURE);
2592 lc->sound_conf.capt_sndcard=card;
2593 if (card && lc->ready)
2594 lp_config_set_string(lc->config,"sound","capture_dev_id",ms_snd_card_get_string_id(card));
2599 * Returns the name of the currently assigned sound device for ringing.
2601 * @ingroup media_parameters
2603 const char * linphone_core_get_ringer_device(LinphoneCore *lc)
2605 if (lc->sound_conf.ring_sndcard) return ms_snd_card_get_string_id(lc->sound_conf.ring_sndcard);
2610 * Returns the name of the currently assigned sound device for playback.
2612 * @ingroup media_parameters
2614 const char * linphone_core_get_playback_device(LinphoneCore *lc)
2616 return lc->sound_conf.play_sndcard ? ms_snd_card_get_string_id(lc->sound_conf.play_sndcard) : NULL;
2620 * Returns the name of the currently assigned sound device for capture.
2622 * @ingroup media_parameters
2624 const char * linphone_core_get_capture_device(LinphoneCore *lc)
2626 return lc->sound_conf.capt_sndcard ? ms_snd_card_get_string_id(lc->sound_conf.capt_sndcard) : NULL;
2630 * Returns an unmodifiable array of available sound devices.
2632 * @ingroup media_parameters
2633 * The array is NULL terminated.
2635 const char** linphone_core_get_sound_devices(LinphoneCore *lc){
2636 build_sound_devices_table(lc);
2637 return lc->sound_conf.cards;
2641 * Returns an unmodifiable array of available video capture devices.
2643 * @ingroup media_parameters
2644 * The array is NULL terminated.
2646 const char** linphone_core_get_video_devices(const LinphoneCore *lc){
2647 return lc->video_conf.cams;
2650 char linphone_core_get_sound_source(LinphoneCore *lc)
2652 return lc->sound_conf.source;
2655 void linphone_core_set_sound_source(LinphoneCore *lc, char source)
2657 MSSndCard *sndcard=lc->sound_conf.capt_sndcard;
2658 lc->sound_conf.source=source;
2659 if (!sndcard) return;
2662 ms_snd_card_set_capture(sndcard,MS_SND_CARD_MIC);
2665 ms_snd_card_set_capture(sndcard,MS_SND_CARD_LINE);
2673 * Sets the path to a wav file used for ringing.
2675 * The file must be a wav 16bit linear.
2677 * @ingroup media_parameters
2679 void linphone_core_set_ring(LinphoneCore *lc,const char *path){
2680 if (lc->sound_conf.local_ring!=0){
2681 ms_free(lc->sound_conf.local_ring);
2683 lc->sound_conf.local_ring=ms_strdup(path);
2684 if (lc->ready && lc->sound_conf.local_ring)
2685 lp_config_set_string(lc->config,"sound","local_ring",lc->sound_conf.local_ring);
2689 * Returns the path to the wav file used for ringing.
2691 * @ingroup media_parameters
2693 const char *linphone_core_get_ring(const LinphoneCore *lc){
2694 return lc->sound_conf.local_ring;
2697 static void notify_end_of_ring(void *ud ,unsigned int event, void * arg){
2698 LinphoneCore *lc=(LinphoneCore*)ud;
2699 lc->preview_finished=1;
2702 int linphone_core_preview_ring(LinphoneCore *lc, const char *ring,LinphoneCoreCbFunc func,void * userdata)
2704 if (lc->ringstream!=0){
2705 ms_warning("Cannot start ring now,there's already a ring being played");
2708 lc_callback_obj_init(&lc->preview_finished_cb,func,userdata);
2709 lc->preview_finished=0;
2710 if (lc->sound_conf.ring_sndcard!=NULL){
2711 lc->ringstream=ring_start_with_cb(ring,2000,lc->sound_conf.ring_sndcard,notify_end_of_ring,(void *)lc);
2717 * Sets the path to a wav file used for ringing back.
2719 * Ringback means the ring that is heard when it's ringing at the remote party.
2720 * The file must be a wav 16bit linear.
2722 * @ingroup media_parameters
2724 void linphone_core_set_ringback(LinphoneCore *lc, const char *path){
2725 if (lc->sound_conf.remote_ring!=0){
2726 ms_free(lc->sound_conf.remote_ring);
2728 lc->sound_conf.remote_ring=ms_strdup(path);
2732 * Returns the path to the wav file used for ringing back.
2734 * @ingroup media_parameters
2736 const char * linphone_core_get_ringback(const LinphoneCore *lc){
2737 return lc->sound_conf.remote_ring;
2741 * Enables or disable echo cancellation.
2743 * @ingroup media_parameters
2745 void linphone_core_enable_echo_cancellation(LinphoneCore *lc, bool_t val){
2746 lc->sound_conf.ec=val;
2748 lp_config_set_int(lc->config,"sound","echocancellation",val);
2752 * Returns TRUE if echo cancellation is enabled.
2754 * @ingroup media_parameters
2756 bool_t linphone_core_echo_cancellation_enabled(LinphoneCore *lc){
2757 return lc->sound_conf.ec;
2760 void linphone_core_enable_echo_limiter(LinphoneCore *lc, bool_t val){
2761 lc->sound_conf.ea=val;
2764 bool_t linphone_core_echo_limiter_enabled(const LinphoneCore *lc){
2765 return lc->sound_conf.ea;
2769 * Mutes or unmutes the local microphone.
2771 * @ingroup media_parameters
2773 void linphone_core_mute_mic(LinphoneCore *lc, bool_t val){
2774 if (lc->audiostream!=NULL){
2775 audio_stream_set_mic_gain(lc->audiostream,
2776 (val==TRUE) ? 0 : 1.0);
2780 void linphone_core_enable_agc(LinphoneCore *lc, bool_t val){
2781 lc->sound_conf.agc=val;
2784 bool_t linphone_core_agc_enabled(const LinphoneCore *lc){
2785 return lc->sound_conf.agc;
2789 * Send the specified dtmf.
2791 * @ingroup media_parameters
2792 * This function only works during calls. The dtmf is automatically played to the user.
2793 * @param lc The LinphoneCore object
2794 * @param dtmf The dtmf name specified as a char, such as '0', '#' etc...
2797 void linphone_core_send_dtmf(LinphoneCore *lc, char dtmf)
2799 /*By default we send DTMF RFC2833 if we do not have enabled SIP_INFO but we can also send RFC2833 and SIP_INFO*/
2800 if (linphone_core_get_use_rfc2833_for_dtmf(lc)!=0 || linphone_core_get_use_info_for_dtmf(lc)==0)
2803 if (lc->audiostream!=NULL){
2804 audio_stream_send_dtmf(lc->audiostream,dtmf);
2808 ms_error("we cannot send RFC2833 dtmf when we are not in communication");
2811 if (linphone_core_get_use_info_for_dtmf(lc)!=0){
2812 /* Out of Band DTMF (use INFO method) */
2813 LinphoneCall *call=lc->call;
2817 sal_call_send_dtmf(call->op,dtmf);
2821 void linphone_core_set_stun_server(LinphoneCore *lc, const char *server){
2822 if (lc->net_conf.stun_server!=NULL)
2823 ms_free(lc->net_conf.stun_server);
2825 lc->net_conf.stun_server=ms_strdup(server);
2826 else lc->net_conf.stun_server=NULL;
2829 const char * linphone_core_get_stun_server(const LinphoneCore *lc){
2830 return lc->net_conf.stun_server;
2833 const char * linphone_core_get_relay_addr(const LinphoneCore *lc){
2834 return lc->net_conf.relay;
2837 int linphone_core_set_relay_addr(LinphoneCore *lc, const char *addr){
2838 if (lc->net_conf.relay!=NULL){
2839 ms_free(lc->net_conf.relay);
2840 lc->net_conf.relay=NULL;
2843 lc->net_conf.relay=ms_strdup(addr);
2848 void linphone_core_set_nat_address(LinphoneCore *lc, const char *addr)
2850 if (lc->net_conf.nat_address!=NULL){
2851 ms_free(lc->net_conf.nat_address);
2853 if (addr!=NULL) lc->net_conf.nat_address=ms_strdup(addr);
2854 else lc->net_conf.nat_address=NULL;
2855 if (lc->sip_conf.contact) update_primary_contact(lc);
2858 const char *linphone_core_get_nat_address(const LinphoneCore *lc)
2860 return lc->net_conf.nat_address;
2863 void linphone_core_set_firewall_policy(LinphoneCore *lc, LinphoneFirewallPolicy pol){
2864 lc->net_conf.firewall_policy=pol;
2865 if (lc->sip_conf.contact) update_primary_contact(lc);
2868 LinphoneFirewallPolicy linphone_core_get_firewall_policy(const LinphoneCore *lc){
2869 return lc->net_conf.firewall_policy;
2873 * Get the list of call logs (past calls).
2875 * @ingroup call_logs
2877 const MSList * linphone_core_get_call_logs(LinphoneCore *lc){
2879 return lc->call_logs;
2883 * Erase the call log.
2885 * @ingroup call_logs
2887 void linphone_core_clear_call_logs(LinphoneCore *lc){
2889 ms_list_for_each(lc->call_logs,(void (*)(void*))linphone_call_log_destroy);
2890 lc->call_logs=ms_list_free(lc->call_logs);
2891 call_logs_write_to_config_file(lc);
2894 static void toggle_video_preview(LinphoneCore *lc, bool_t val){
2895 #ifdef VIDEO_ENABLED
2896 if (lc->videostream==NULL){
2898 if (lc->previewstream==NULL){
2899 lc->previewstream=video_preview_start(lc->video_conf.device,
2900 lc->video_conf.vsize);
2903 if (lc->previewstream!=NULL){
2904 video_preview_stop(lc->previewstream);
2905 lc->previewstream=NULL;
2913 * Enables video globally.
2915 * @ingroup media_parameters
2916 * This function does not have any effect during calls. It just indicates LinphoneCore to
2917 * initiate future calls with video or not. The two boolean parameters indicate in which
2918 * direction video is enabled. Setting both to false disables video entirely.
2920 * @param vcap_enabled indicates whether video capture is enabled
2921 * @param display_enabled indicates whether video display should be shown
2924 void linphone_core_enable_video(LinphoneCore *lc, bool_t vcap_enabled, bool_t display_enabled){
2925 #ifndef VIDEO_ENABLED
2926 if (vcap_enabled || display_enabled)
2927 ms_warning("This version of linphone was built without video support.");
2929 lc->video_conf.capture=vcap_enabled;
2930 lc->video_conf.display=display_enabled;
2932 /* need to re-apply network bandwidth settings*/
2933 linphone_core_set_download_bandwidth(lc,
2934 linphone_core_get_download_bandwidth(lc));
2935 linphone_core_set_upload_bandwidth(lc,
2936 linphone_core_get_upload_bandwidth(lc));
2940 * Returns TRUE if video is enabled, FALSE otherwise.
2941 * @ingroup media_parameters
2943 bool_t linphone_core_video_enabled(LinphoneCore *lc){
2944 return (lc->video_conf.display || lc->video_conf.capture);
2948 * Controls video preview enablement.
2950 * @ingroup media_parameters
2951 * Video preview refers to the action of displaying the local webcam image
2952 * to the user while not in call.
2954 void linphone_core_enable_video_preview(LinphoneCore *lc, bool_t val){
2955 lc->video_conf.show_local=val;
2959 * Returns TRUE if video previewing is enabled.
2960 * @ingroup media_parameters
2962 bool_t linphone_core_video_preview_enabled(const LinphoneCore *lc){
2963 return lc->video_conf.show_local;
2967 * Enables or disable self view during calls.
2969 * @ingroup media_parameters
2970 * Self-view refers to having local webcam image inserted in corner
2971 * of the video window during calls.
2972 * This function works at any time, including during calls.
2974 void linphone_core_enable_self_view(LinphoneCore *lc, bool_t val){
2975 lc->video_conf.selfview=val;
2976 #ifdef VIDEO_ENABLED
2977 if (lc->videostream){
2978 video_stream_enable_self_view(lc->videostream,val);
2984 * Returns TRUE if self-view is enabled, FALSE otherwise.
2986 * @ingroup media_parameters
2988 * Refer to linphone_core_enable_self_view() for details.
2990 bool_t linphone_core_self_view_enabled(const LinphoneCore *lc){
2991 return lc->video_conf.selfview;
2995 * Sets the active video device.
2997 * @ingroup media_parameters
2998 * @param id the name of the video device as returned by linphone_core_get_video_devices()
3000 int linphone_core_set_video_device(LinphoneCore *lc, const char *id){
3001 MSWebCam *olddev=lc->video_conf.device;
3004 lc->video_conf.device=ms_web_cam_manager_get_cam(ms_web_cam_manager_get(),id);
3005 if (lc->video_conf.device==NULL){
3006 ms_warning("Could not found video device %s",id);
3009 if (lc->video_conf.device==NULL)
3010 lc->video_conf.device=ms_web_cam_manager_get_default_cam(ms_web_cam_manager_get());
3011 if (olddev!=NULL && olddev!=lc->video_conf.device){
3012 toggle_video_preview(lc,FALSE);/*restart the video local preview*/
3014 if (lc->ready && lc->video_conf.device){
3015 vd=ms_web_cam_get_string_id(lc->video_conf.device);
3016 if (vd && strstr(vd,"Static picture")!=NULL){
3019 lp_config_set_string(lc->config,"video","device",vd);
3025 * Returns the name of the currently active video device.
3027 * @ingroup media_parameters
3029 const char *linphone_core_get_video_device(const LinphoneCore *lc){
3030 if (lc->video_conf.device) return ms_web_cam_get_string_id(lc->video_conf.device);
3035 * Returns the native window handle of the video window, casted as an unsigned long.
3037 * @ingroup media_parameters
3039 unsigned long linphone_core_get_native_video_window_id(const LinphoneCore *lc){
3040 #ifdef VIDEO_ENABLED
3041 if (lc->videostream)
3042 return video_stream_get_native_window_id(lc->videostream);
3043 if (lc->previewstream)
3044 return video_stream_get_native_window_id(lc->previewstream);
3049 static MSVideoSizeDef supported_resolutions[]={
3050 { {MS_VIDEO_SIZE_SVGA_W,MS_VIDEO_SIZE_SVGA_H} , "svga" },
3051 { {MS_VIDEO_SIZE_4CIF_W,MS_VIDEO_SIZE_4CIF_H} , "4cif" },
3052 { {MS_VIDEO_SIZE_VGA_W,MS_VIDEO_SIZE_VGA_H} , "vga" },
3053 { {MS_VIDEO_SIZE_CIF_W,MS_VIDEO_SIZE_CIF_H} , "cif" },
3054 { {MS_VIDEO_SIZE_QVGA_W,MS_VIDEO_SIZE_QVGA_H} , "qvga" },
3055 { {MS_VIDEO_SIZE_QCIF_W,MS_VIDEO_SIZE_QCIF_H} , "qcif" },
3060 * Returns the zero terminated table of supported video resolutions.
3062 * @ingroup media_parameters
3064 const MSVideoSizeDef *linphone_core_get_supported_video_sizes(LinphoneCore *lc){
3065 return supported_resolutions;
3068 static MSVideoSize video_size_get_by_name(const char *name){
3069 MSVideoSizeDef *pdef=supported_resolutions;
3070 MSVideoSize null_vsize={0,0};
3071 for(;pdef->name!=NULL;pdef++){
3072 if (strcasecmp(name,pdef->name)==0){
3076 ms_warning("Video resolution %s is not supported in linphone.",name);
3080 static const char *video_size_get_name(MSVideoSize vsize){
3081 MSVideoSizeDef *pdef=supported_resolutions;
3082 for(;pdef->name!=NULL;pdef++){
3083 if (pdef->vsize.width==vsize.width && pdef->vsize.height==vsize.height){
3090 static bool_t video_size_supported(MSVideoSize vsize){
3091 if (video_size_get_name(vsize)) return TRUE;
3092 ms_warning("Video resolution %ix%i is not supported in linphone.",vsize.width,vsize.height);
3097 * Sets the preferred video size.
3099 * @ingroup media_parameters
3100 * This applies only to the stream that is captured and sent to the remote party,
3101 * since we accept all standard video size on the receive path.
3103 void linphone_core_set_preferred_video_size(LinphoneCore *lc, MSVideoSize vsize){
3104 if (video_size_supported(vsize)){
3105 MSVideoSize oldvsize=lc->video_conf.vsize;
3106 lc->video_conf.vsize=vsize;
3107 if (!ms_video_size_equal(oldvsize,vsize) && lc->previewstream!=NULL){
3108 toggle_video_preview(lc,FALSE);
3109 toggle_video_preview(lc,TRUE);
3112 lp_config_set_string(lc->config,"video","size",video_size_get_name(vsize));
3117 * Sets the preferred video size by its name.
3119 * @ingroup media_parameters
3120 * This is identical to linphone_core_set_preferred_video_size() except
3121 * that it takes the name of the video resolution as input.
3122 * Video resolution names are: qcif, svga, cif, vga, 4cif, svga ...
3124 void linphone_core_set_preferred_video_size_by_name(LinphoneCore *lc, const char *name){
3125 MSVideoSize vsize=video_size_get_by_name(name);
3126 MSVideoSize default_vsize={MS_VIDEO_SIZE_CIF_W,MS_VIDEO_SIZE_CIF_H};
3127 if (vsize.width!=0) linphone_core_set_preferred_video_size(lc,vsize);
3128 else linphone_core_set_preferred_video_size(lc,default_vsize);
3132 * Returns the current preferred video size for sending.
3134 * @ingroup media_parameters
3136 MSVideoSize linphone_core_get_preferred_video_size(LinphoneCore *lc){
3137 return lc->video_conf.vsize;
3140 void linphone_core_use_files(LinphoneCore *lc, bool_t yesno){
3141 lc->use_files=yesno;
3144 void linphone_core_set_play_file(LinphoneCore *lc, const char *file){
3145 if (lc->play_file!=NULL){
3146 ms_free(lc->play_file);
3150 lc->play_file=ms_strdup(file);
3151 if (lc->audiostream->ticker)
3152 audio_stream_play(lc->audiostream,file);
3156 void linphone_core_set_record_file(LinphoneCore *lc, const char *file){
3157 if (lc->rec_file!=NULL){
3158 ms_free(lc->rec_file);
3162 lc->rec_file=ms_strdup(file);
3163 if (lc->audiostream)
3164 audio_stream_record(lc->audiostream,file);
3169 * Retrieves the user pointer that was given to linphone_core_new()
3171 * @ingroup initializing
3173 void *linphone_core_get_user_data(LinphoneCore *lc){
3177 int linphone_core_get_mtu(const LinphoneCore *lc){
3178 return lc->net_conf.mtu;
3181 void linphone_core_set_mtu(LinphoneCore *lc, int mtu){
3182 lc->net_conf.mtu=mtu;
3185 ms_error("MTU too small !");
3189 ms_message("MTU is supposed to be %i, rtp payload max size will be %i",mtu, ms_get_payload_max_size());
3190 }else ms_set_mtu(0);//use mediastreamer2 default value
3193 void linphone_core_set_waiting_callback(LinphoneCore *lc, LinphoneWaitingCallback cb, void *user_context){
3195 lc->wait_ctx=user_context;
3198 void linphone_core_start_waiting(LinphoneCore *lc, const char *purpose){
3200 lc->wait_ctx=lc->wait_cb(lc,lc->wait_ctx,LinphoneWaitingStart,purpose,0);
3204 void linphone_core_update_progress(LinphoneCore *lc, const char *purpose, float progress){
3206 lc->wait_ctx=lc->wait_cb(lc,lc->wait_ctx,LinphoneWaitingProgress,purpose,progress);
3216 void linphone_core_stop_waiting(LinphoneCore *lc){
3218 lc->wait_ctx=lc->wait_cb(lc,lc->wait_ctx,LinphoneWaitingFinished,NULL,0);
3222 void linphone_core_set_audio_transports(LinphoneCore *lc, RtpTransport *rtp, RtpTransport *rtcp){
3227 void net_config_uninit(LinphoneCore *lc)
3229 net_config_t *config=&lc->net_conf;
3230 lp_config_set_int(lc->config,"net","download_bw",config->download_bw);
3231 lp_config_set_int(lc->config,"net","upload_bw",config->upload_bw);
3233 if (config->stun_server!=NULL)
3234 lp_config_set_string(lc->config,"net","stun_server",config->stun_server);
3235 if (config->nat_address!=NULL)
3236 lp_config_set_string(lc->config,"net","nat_address",config->nat_address);
3237 lp_config_set_int(lc->config,"net","firewall_policy",config->firewall_policy);
3238 lp_config_set_int(lc->config,"net","mtu",config->mtu);
3239 if (lc->net_conf.stun_server!=NULL)
3240 ms_free(lc->net_conf.stun_server);
3244 void sip_config_uninit(LinphoneCore *lc)
3248 sip_config_t *config=&lc->sip_conf;
3249 lp_config_set_int(lc->config,"sip","sip_port",config->sip_port);
3250 lp_config_set_int(lc->config,"sip","guess_hostname",config->guess_hostname);
3251 lp_config_set_string(lc->config,"sip","contact",config->contact);
3252 lp_config_set_int(lc->config,"sip","inc_timeout",config->inc_timeout);
3253 lp_config_set_int(lc->config,"sip","use_info",config->use_info);
3254 lp_config_set_int(lc->config,"sip","use_rfc2833",config->use_rfc2833);
3255 lp_config_set_int(lc->config,"sip","use_ipv6",config->ipv6_enabled);
3256 lp_config_set_int(lc->config,"sip","register_only_when_network_is_up",config->register_only_when_network_is_up);
3258 lp_config_set_int(lc->config,"sip","default_proxy",linphone_core_get_default_proxy(lc,NULL));
3260 for(elem=config->proxies,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
3261 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)(elem->data);
3262 linphone_proxy_config_write_to_config_file(lc->config,cfg,i);
3263 linphone_proxy_config_edit(cfg); /* to unregister */
3269 sal_iterate(lc->sal);
3278 ms_list_for_each(config->proxies,(void (*)(void*)) linphone_proxy_config_destroy);
3279 ms_list_free(config->proxies);
3280 config->proxies=NULL;
3282 linphone_proxy_config_write_to_config_file(lc->config,NULL,i); /*mark the end */
3284 for(elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
3285 LinphoneAuthInfo *ai=(LinphoneAuthInfo*)(elem->data);
3286 linphone_auth_info_write_config(lc->config,ai,i);
3288 linphone_auth_info_write_config(lc->config,NULL,i); /* mark the end */
3289 ms_list_for_each(lc->auth_info,(void (*)(void*))linphone_auth_info_destroy);
3290 ms_list_free(lc->auth_info);
3292 sal_uninit(lc->sal);
3296 void rtp_config_uninit(LinphoneCore *lc)
3298 rtp_config_t *config=&lc->rtp_conf;
3299 lp_config_set_int(lc->config,"rtp","audio_rtp_port",config->audio_rtp_port);
3300 lp_config_set_int(lc->config,"rtp","video_rtp_port",config->video_rtp_port);
3301 lp_config_set_int(lc->config,"rtp","audio_jitt_comp",config->audio_jitt_comp);
3302 lp_config_set_int(lc->config,"rtp","video_jitt_comp",config->video_jitt_comp);
3303 lp_config_set_int(lc->config,"rtp","nortp_timeout",config->nortp_timeout);
3306 void sound_config_uninit(LinphoneCore *lc)
3308 sound_config_t *config=&lc->sound_conf;
3309 ms_free(config->cards);
3311 lp_config_set_string(lc->config,"sound","remote_ring",config->remote_ring);
3313 if (config->local_ring) ms_free(config->local_ring);
3314 if (config->remote_ring) ms_free(config->remote_ring);
3315 ms_snd_card_manager_destroy();
3318 void video_config_uninit(LinphoneCore *lc)
3320 lp_config_set_int(lc->config,"video","enabled",linphone_core_video_enabled(lc));
3321 lp_config_set_string(lc->config,"video","size",video_size_get_name(linphone_core_get_preferred_video_size(lc)));
3322 lp_config_set_int(lc->config,"video","display",lc->video_conf.display);
3323 lp_config_set_int(lc->config,"video","capture",lc->video_conf.capture);
3324 lp_config_set_int(lc->config,"video","show_local",linphone_core_video_preview_enabled(lc));
3325 lp_config_set_int(lc->config,"video","self_view",linphone_core_self_view_enabled(lc));
3328 void codecs_config_uninit(LinphoneCore *lc)
3331 codecs_config_t *config=&lc->codecs_conf;
3336 for(node=config->audio_codecs;node!=NULL;node=ms_list_next(node)){
3337 pt=(PayloadType*)(node->data);
3338 sprintf(key,"audio_codec_%i",index);
3339 lp_config_set_string(lc->config,key,"mime",pt->mime_type);
3340 lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
3341 lp_config_set_int(lc->config,key,"enabled",linphone_core_payload_type_enabled(lc,pt));
3345 for(node=config->video_codecs;node!=NULL;node=ms_list_next(node)){
3346 pt=(PayloadType*)(node->data);
3347 sprintf(key,"video_codec_%i",index);
3348 lp_config_set_string(lc->config,key,"mime",pt->mime_type);
3349 lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
3350 lp_config_set_int(lc->config,key,"enabled",linphone_core_payload_type_enabled(lc,pt));
3351 lp_config_set_string(lc->config,key,"recv_fmtp",pt->recv_fmtp);
3356 void ui_config_uninit(LinphoneCore* lc)
3359 ms_list_for_each(lc->friends,(void (*)(void *))linphone_friend_destroy);
3360 ms_list_free(lc->friends);
3366 * Returns the LpConfig object used to manage the storage (config) file.
3369 * The application can use the LpConfig object to insert its own private
3370 * sections and pairs of key=value in the configuration file.
3373 LpConfig *linphone_core_get_config(LinphoneCore *lc){
3377 static void linphone_core_uninit(LinphoneCore *lc)
3381 linphone_core_terminate_call(lc,NULL);
3388 linphone_core_iterate(lc);
3392 ms_list_for_each(lc->friends,(void (*)(void *))linphone_friend_close_subscriptions);
3393 gstate_new_state(lc, GSTATE_POWER_SHUTDOWN, NULL);
3394 #ifdef VIDEO_ENABLED
3395 if (lc->previewstream!=NULL){
3396 video_preview_stop(lc->previewstream);
3397 lc->previewstream=NULL;
3400 /* save all config */
3401 net_config_uninit(lc);
3402 sip_config_uninit(lc);
3403 rtp_config_uninit(lc);
3404 sound_config_uninit(lc);
3405 video_config_uninit(lc);
3406 codecs_config_uninit(lc);
3407 ui_config_uninit(lc);
3408 if (lp_config_needs_commit(lc->config)) lp_config_sync(lc->config);
3409 lp_config_destroy(lc->config);
3410 lc->config = NULL; /* Mark the config as NULL to block further calls */
3411 sip_setup_unregister_all();
3413 linphone_core_free_payload_types();
3416 gstate_new_state(lc, GSTATE_POWER_OFF, NULL);
3419 static void set_network_reachable(LinphoneCore* lc,bool_t isReachable){
3420 ms_message("Network state is now [%s]",isReachable?"UP":"DOWN");
3421 // second get the list of available proxies
3422 const MSList *elem=linphone_core_get_proxy_config_list(lc);
3423 for(;elem!=NULL;elem=elem->next){
3424 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
3425 if (linphone_proxy_config_register_enabled(cfg) ) {
3433 lc->network_reachable=isReachable;
3436 void linphone_core_set_network_reachable(LinphoneCore* lc,bool_t isReachable) {
3437 //first disable automatic mode
3438 if (lc->auto_net_state_mon) {
3439 ms_message("Disabling automatic network state monitoring");
3440 lc->auto_net_state_mon=FALSE;
3442 set_network_reachable(lc,isReachable);
3445 * Destroys a LinphoneCore
3447 * @ingroup initializing
3449 void linphone_core_destroy(LinphoneCore *lc){
3450 linphone_core_uninit(lc);