]> sjero.net Git - linphone/blob - coreapi/linphonecore.c
Merge branch 'master' into dev_multicall
[linphone] / coreapi / linphonecore.c
1 /*
2 linphone
3 Copyright (C) 2000  Simon MORLAT (simon.morlat@linphone.org)
4
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.
9
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.
14
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.
18 */
19
20 #include "linphonecore.h"
21 #include "sipsetup.h"
22 #include "lpconfig.h"
23 #include "private.h"
24 #include "mediastreamer2/mediastream.h"
25 #include "mediastreamer2/msvolume.h"
26 #include "mediastreamer2/msequalizer.h"
27
28 #include <ortp/telephonyevents.h>
29
30
31 #ifdef INET6
32 #ifndef WIN32
33 #include <netdb.h>
34 #endif
35 #endif
36
37 /*#define UNSTANDART_GSM_11K 1*/
38
39 static const char *liblinphone_version=LIBLINPHONE_VERSION;
40 static void set_network_reachable(LinphoneCore* lc,bool_t isReachable);
41
42 #include "enum.h"
43
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);
46
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"
51
52 extern SalCallbacks linphone_sal_callbacks;
53
54 void lc_callback_obj_init(LCCallbackObj *obj,LinphoneCoreCbFunc func,void* ud)
55 {
56   obj->_func=func;
57   obj->_user_data=ud;
58 }
59
60 int lc_callback_obj_invoke(LCCallbackObj *obj, LinphoneCore *lc){
61         if (obj->_func!=NULL) obj->_func(lc,obj->_user_data);
62         return 0;
63 }
64
65                 
66 /*prevent a gcc bug with %c*/
67 static size_t my_strftime(char *s, size_t max, const char  *fmt,  const struct tm *tm){
68 #if !defined(_WIN32_WCE)
69         return strftime(s, max, fmt, tm);
70 #else
71         return 0;
72         /*FIXME*/
73 #endif /*_WIN32_WCE*/
74 }
75
76 static void set_call_log_date(LinphoneCallLog *cl, const struct tm *loctime){
77         my_strftime(cl->start_date,sizeof(cl->start_date),"%c",loctime);
78 }
79
80 LinphoneCallLog * linphone_call_log_new(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
81         LinphoneCallLog *cl=ms_new0(LinphoneCallLog,1);
82         struct tm loctime;
83         cl->dir=call->dir;
84 #ifdef WIN32
85 #if !defined(_WIN32_WCE)
86         loctime=*localtime(&call->start_time);
87         /*FIXME*/
88 #endif /*_WIN32_WCE*/
89 #else
90         localtime_r(&call->start_time,&loctime);
91 #endif
92         set_call_log_date(cl,&loctime);
93         cl->from=from;
94         cl->to=to;
95         return cl;
96 }
97
98 static void call_logs_write_to_config_file(LinphoneCore *lc){
99         MSList *elem;
100         char logsection[32];
101         int i;
102         char *tmp;
103         LpConfig *cfg=lc->config;
104
105         if (!lc->ready) return;
106         
107         for(i=0,elem=lc->call_logs;elem!=NULL;elem=elem->next,++i){
108                 LinphoneCallLog *cl=(LinphoneCallLog*)elem->data;
109                 snprintf(logsection,sizeof(logsection),"call_log_%i",i);
110                 lp_config_set_int(cfg,logsection,"dir",cl->dir);
111                 lp_config_set_int(cfg,logsection,"status",cl->status);
112                 tmp=linphone_address_as_string(cl->from);
113                 lp_config_set_string(cfg,logsection,"from",tmp);
114                 ms_free(tmp);
115                 tmp=linphone_address_as_string(cl->to);
116                 lp_config_set_string(cfg,logsection,"to",tmp);
117                 ms_free(tmp);
118                 lp_config_set_string(cfg,logsection,"start_date",cl->start_date);
119                 lp_config_set_int(cfg,logsection,"duration",cl->duration);
120                 if (cl->refkey) lp_config_set_string(cfg,logsection,"refkey",cl->refkey);
121         }
122         for(;i<lc->max_call_logs;++i){
123                 snprintf(logsection,sizeof(logsection),"call_log_%i",i);
124                 lp_config_clean_section(cfg,logsection);
125         }
126 }
127
128 static void call_logs_read_from_config_file(LinphoneCore *lc){
129         char logsection[32];
130         int i;
131         const char *tmp;
132         LpConfig *cfg=lc->config;
133         for(i=0;;++i){
134                 snprintf(logsection,sizeof(logsection),"call_log_%i",i);
135                 if (lp_config_has_section(cfg,logsection)){
136                         LinphoneCallLog *cl=ms_new0(LinphoneCallLog,1);
137                         cl->dir=lp_config_get_int(cfg,logsection,"dir",0);
138                         cl->status=lp_config_get_int(cfg,logsection,"status",0);
139                         tmp=lp_config_get_string(cfg,logsection,"from",NULL);
140                         if (tmp) cl->from=linphone_address_new(tmp);
141                         tmp=lp_config_get_string(cfg,logsection,"to",NULL);
142                         if (tmp) cl->to=linphone_address_new(tmp);
143                         tmp=lp_config_get_string(cfg,logsection,"start_date",NULL);
144                         if (tmp) strncpy(cl->start_date,tmp,sizeof(cl->start_date));
145                         cl->duration=lp_config_get_int(cfg,logsection,"duration",0);
146                         tmp=lp_config_get_string(cfg,logsection,"refkey",NULL);
147                         if (tmp) cl->refkey=ms_strdup(tmp);
148                         lc->call_logs=ms_list_append(lc->call_logs,cl);
149                 }else break;    
150         }
151 }
152
153
154 void linphone_call_log_completed(LinphoneCallLog *calllog, LinphoneCall *call, LinphoneCallStatus status){
155         LinphoneCore *lc=call->core;
156         
157         calllog->duration=time(NULL)-call->start_time;
158         
159         if (status==LinphoneCallMissed){
160                 char *info;
161                 lc->missed_calls++;
162                 info=ortp_strdup_printf(ngettext("You have missed %i call.",
163                     "You have missed %i calls.", lc->missed_calls),
164                 lc->missed_calls);
165                 lc->vtable.display_status(lc,info);
166                 ms_free(info);
167         }else calllog->status=status;
168         lc->call_logs=ms_list_prepend(lc->call_logs,(void *)calllog);
169         if (ms_list_size(lc->call_logs)>lc->max_call_logs){
170                 MSList *elem,*prevelem=NULL;
171                 /*find the last element*/
172                 for(elem=lc->call_logs;elem!=NULL;elem=elem->next){
173                         prevelem=elem;
174                 }
175                 elem=prevelem;
176                 linphone_call_log_destroy((LinphoneCallLog*)elem->data);
177                 lc->call_logs=ms_list_remove_link(lc->call_logs,elem);
178         }
179         if (lc->vtable.call_log_updated!=NULL){
180                 lc->vtable.call_log_updated(lc,calllog);
181         }
182         call_logs_write_to_config_file(lc);
183 }
184
185 /**
186  * @addtogroup call_logs
187  * @{
188 **/
189
190 /**
191  * Returns a human readable string describing the call.
192  * 
193  * @note: the returned char* must be freed by the application (use ms_free()).
194 **/
195 char * linphone_call_log_to_str(LinphoneCallLog *cl){
196         char *status;
197         char *tmp;
198         char *from=linphone_address_as_string (cl->from);
199         char *to=linphone_address_as_string (cl->to);
200         switch(cl->status){
201                 case LinphoneCallAborted:
202                         status=_("aborted");
203                         break;
204                 case LinphoneCallSuccess:
205                         status=_("completed");
206                         break;
207                 case LinphoneCallMissed:
208                         status=_("missed");
209                         break;
210                 default:
211                         status="unknown";
212         }
213         tmp=ortp_strdup_printf(_("%s at %s\nFrom: %s\nTo: %s\nStatus: %s\nDuration: %i mn %i sec\n"),
214                         (cl->dir==LinphoneCallIncoming) ? _("Incoming call") : _("Outgoing call"),
215                         cl->start_date,
216                         from,
217                         to,
218                         status,
219                         cl->duration/60,
220                         cl->duration%60);
221         ms_free(from);
222         ms_free(to);
223         return tmp;
224 }
225
226 void linphone_call_log_set_user_pointer(LinphoneCallLog *cl, void *up){
227         cl->user_pointer=up;
228 }
229
230 void *linphone_call_log_get_user_pointer(const LinphoneCallLog *cl){
231         return cl->user_pointer;
232 }
233
234
235
236 /**
237  * Associate a persistent reference key to the call log.
238  *
239  * The reference key can be for example an id to an external database.
240  * It is stored in the config file, thus can survive to process exits/restarts.
241  *
242 **/
243 void linphone_call_log_set_ref_key(LinphoneCallLog *cl, const char *refkey){
244         if (cl->refkey!=NULL){
245                 ms_free(cl->refkey);
246                 cl->refkey=NULL;
247         }
248         if (refkey) cl->refkey=ms_strdup(refkey);
249         call_logs_write_to_config_file(cl->lc);
250 }
251
252 /**
253  * Get the persistent reference key associated to the call log.
254  *
255  * The reference key can be for example an id to an external database.
256  * It is stored in the config file, thus can survive to process exits/restarts.
257  *
258 **/
259 const char *linphone_call_log_get_ref_key(const LinphoneCallLog *cl){
260         return cl->refkey;
261 }
262
263 /** @} */
264
265 void linphone_call_log_destroy(LinphoneCallLog *cl){
266         if (cl->from!=NULL) linphone_address_destroy(cl->from);
267         if (cl->to!=NULL) linphone_address_destroy(cl->to);
268         if (cl->refkey!=NULL) ms_free(cl->refkey);
269         ms_free(cl);
270 }
271
272 /**
273  * Returns TRUE if the LinphoneCall asked to autoanswer
274  *
275 **/
276 bool_t linphone_call_asked_to_autoanswer(LinphoneCall *call){
277         //return TRUE if the unique(for the moment) incoming call asked to be autoanswered
278         if(call)
279                 return sal_call_autoanswer_asked(call->op);
280         else
281                 return FALSE;
282 }
283
284 int linphone_core_get_call_duration(LinphoneCall *call){
285         if (call==NULL) return 0;
286         if (call->media_start_time==0) return 0;
287         return time(NULL)-call->media_start_time;
288 }
289
290 int linphone_core_get_current_call_duration(const LinphoneCore *lc){
291         LinphoneCall *call=linphone_core_get_current_call((LinphoneCore *)lc);
292         return linphone_core_get_call_duration(call);
293 }
294
295 const LinphoneAddress *linphone_core_get_current_call_remote_address(struct _LinphoneCore *lc){
296         LinphoneCall *call=linphone_core_get_current_call(lc);
297         if (call==NULL) return 0;
298         return linphone_call_get_remote_address(call);
299 }
300
301 /**
302  * Enable logs in supplied FILE*.
303  *
304  * @ingroup misc
305  *
306  * @param file a C FILE* where to fprintf logs. If null stdout is used.
307  * 
308 **/
309 void linphone_core_enable_logs(FILE *file){
310         if (file==NULL) file=stdout;
311         ortp_set_log_file(file);
312         ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
313 }
314
315 /**
316  * Enable logs through the user's supplied log callback.
317  *
318  * @ingroup misc
319  *
320  * @param logfunc The address of a OrtpLogFunc callback whose protoype is
321  *                typedef void (*OrtpLogFunc)(OrtpLogLevel lev, const char *fmt, va_list args);
322  * 
323 **/
324 void linphone_core_enable_logs_with_cb(OrtpLogFunc logfunc){
325         ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
326         ortp_set_log_handler(logfunc);
327 }
328
329 /**
330  * Entirely disable logging.
331  *
332  * @ingroup misc
333 **/
334 void linphone_core_disable_logs(){
335         ortp_set_log_level_mask(ORTP_ERROR|ORTP_FATAL);
336 }
337
338
339 static void
340 net_config_read (LinphoneCore *lc)
341 {
342         int tmp;
343         const char *tmpstr;
344         LpConfig *config=lc->config;
345
346         tmp=lp_config_get_int(config,"net","download_bw",0);
347         linphone_core_set_download_bandwidth(lc,tmp);
348         tmp=lp_config_get_int(config,"net","upload_bw",0);
349         linphone_core_set_upload_bandwidth(lc,tmp);
350         linphone_core_set_stun_server(lc,lp_config_get_string(config,"net","stun_server",NULL));
351         tmpstr=lp_config_get_string(lc->config,"net","nat_address",NULL);
352         if (tmpstr!=NULL && (strlen(tmpstr)<1)) tmpstr=NULL;
353         linphone_core_set_nat_address(lc,tmpstr);
354         tmp=lp_config_get_int(lc->config,"net","firewall_policy",0);
355         linphone_core_set_firewall_policy(lc,tmp);
356         tmp=lp_config_get_int(lc->config,"net","nat_sdp_only",0);
357         lc->net_conf.nat_sdp_only=tmp;
358         tmp=lp_config_get_int(lc->config,"net","mtu",0);
359         linphone_core_set_mtu(lc,tmp);
360         tmp=lp_config_get_int(lc->config,"net","download_ptime",0);
361         linphone_core_set_download_ptime(lc,tmp);
362
363 }
364
365 static void build_sound_devices_table(LinphoneCore *lc){
366         const char **devices;
367         const char **old;
368         int ndev;
369         int i;
370         const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
371         ndev=ms_list_size(elem);
372         devices=ms_malloc((ndev+1)*sizeof(const char *));
373         for (i=0;elem!=NULL;elem=elem->next,i++){
374                 devices[i]=ms_snd_card_get_string_id((MSSndCard *)elem->data);
375         }
376         devices[ndev]=NULL;
377         old=lc->sound_conf.cards;
378         lc->sound_conf.cards=devices;
379         if (old!=NULL) ms_free(old);
380 }
381
382 static void sound_config_read(LinphoneCore *lc)
383 {
384         /*int tmp;*/
385         const char *tmpbuf;
386         const char *devid;
387         float gain=0;
388 #ifdef __linux
389         /*alsadev let the user use custom alsa device within linphone*/
390         devid=lp_config_get_string(lc->config,"sound","alsadev",NULL);
391         if (devid){
392                 MSSndCard *card=ms_alsa_card_new_custom(devid,devid);
393                 ms_snd_card_manager_add_card(ms_snd_card_manager_get(),card);
394         }
395 #endif
396         /* retrieve all sound devices */
397         build_sound_devices_table(lc);
398
399         devid=lp_config_get_string(lc->config,"sound","playback_dev_id",NULL);
400         linphone_core_set_playback_device(lc,devid);
401
402         devid=lp_config_get_string(lc->config,"sound","ringer_dev_id",NULL);
403         linphone_core_set_ringer_device(lc,devid);
404
405         devid=lp_config_get_string(lc->config,"sound","capture_dev_id",NULL);
406         linphone_core_set_capture_device(lc,devid);
407
408 /*
409         tmp=lp_config_get_int(lc->config,"sound","play_lev",80);
410         linphone_core_set_play_level(lc,tmp);
411         tmp=lp_config_get_int(lc->config,"sound","ring_lev",80);
412         linphone_core_set_ring_level(lc,tmp);
413         tmp=lp_config_get_int(lc->config,"sound","rec_lev",80);
414         linphone_core_set_rec_level(lc,tmp);
415         tmpbuf=lp_config_get_string(lc->config,"sound","source","m");
416         linphone_core_set_sound_source(lc,tmpbuf[0]);
417 */
418
419         tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
420         tmpbuf=lp_config_get_string(lc->config,"sound","local_ring",tmpbuf);
421         if (ortp_file_exist(tmpbuf)==-1) {
422                 tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
423         }
424         if (strstr(tmpbuf,".wav")==NULL){
425                 /* it currently uses old sound files, so replace them */
426                 tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
427         }
428
429         linphone_core_set_ring(lc,tmpbuf);
430
431         tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
432         tmpbuf=lp_config_get_string(lc->config,"sound","remote_ring",tmpbuf);
433         if (ortp_file_exist(tmpbuf)==-1){
434                 tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
435         }
436         if (strstr(tmpbuf,".wav")==NULL){
437                 /* it currently uses old sound files, so replace them */
438                 tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
439         }
440         linphone_core_set_ringback(lc,tmpbuf);
441         check_sound_device(lc);
442         lc->sound_conf.latency=0;
443
444         linphone_core_enable_echo_cancellation(lc,
445             lp_config_get_int(lc->config,"sound","echocancelation",0) |
446             lp_config_get_int(lc->config,"sound","echocancellation",0)
447                 );
448
449         linphone_core_enable_echo_limiter(lc,
450                 lp_config_get_int(lc->config,"sound","echolimiter",0));
451         linphone_core_enable_agc(lc,
452                 lp_config_get_int(lc->config,"sound","agc",0));
453
454         gain=lp_config_get_float(lc->config,"sound","soft_play_lev",0);
455                 linphone_core_set_soft_play_level(lc,gain);
456 }
457
458 static void sip_config_read(LinphoneCore *lc)
459 {
460         char *contact;
461         const char *tmpstr;
462         int port;
463         int i,tmp;
464         int ipv6;
465         port=lp_config_get_int(lc->config,"sip","use_info",0);
466         linphone_core_set_use_info_for_dtmf(lc,port);
467
468         port=lp_config_get_int(lc->config,"sip","use_rfc2833",0);
469         linphone_core_set_use_rfc2833_for_dtmf(lc,port);
470
471         ipv6=lp_config_get_int(lc->config,"sip","use_ipv6",-1);
472         if (ipv6==-1){
473                 ipv6=0;
474                 if (host_has_ipv6_network()){
475                         lc->vtable.display_message(lc,_("Your machine appears to be connected to an IPv6 network. By default linphone always uses IPv4. Please update your configuration if you want to use IPv6"));
476                 }
477         }
478         linphone_core_enable_ipv6(lc,ipv6);
479         port=lp_config_get_int(lc->config,"sip","sip_port",5060);
480         linphone_core_set_sip_port(lc,port);
481
482         tmpstr=lp_config_get_string(lc->config,"sip","contact",NULL);
483         if (tmpstr==NULL || linphone_core_set_primary_contact(lc,tmpstr)==-1) {
484                 const char *hostname=NULL;
485                 const char *username=NULL;
486 #ifdef HAVE_GETENV
487                 hostname=getenv("HOST");
488                 username=getenv("USER");
489                 if (hostname==NULL) hostname=getenv("HOSTNAME");
490 #endif /*HAVE_GETENV*/
491                 if (hostname==NULL)
492                         hostname="unknown-host";
493                 if (username==NULL){
494                         username="toto";
495                 }
496                 contact=ortp_strdup_printf("sip:%s@%s",username,hostname);
497                 linphone_core_set_primary_contact(lc,contact);
498                 ms_free(contact);
499         }
500
501         tmp=lp_config_get_int(lc->config,"sip","guess_hostname",1);
502         linphone_core_set_guess_hostname(lc,tmp);
503
504
505         tmp=lp_config_get_int(lc->config,"sip","inc_timeout",15);
506         linphone_core_set_inc_timeout(lc,tmp);
507
508         /* get proxies config */
509         for(i=0;; i++){
510                 LinphoneProxyConfig *cfg=linphone_proxy_config_new_from_config_file(lc->config,i);
511                 if (cfg!=NULL){
512                         linphone_core_add_proxy_config(lc,cfg);
513                 }else{
514                         break;
515                 }
516         }
517         /* get the default proxy */
518         tmp=lp_config_get_int(lc->config,"sip","default_proxy",-1);
519         linphone_core_set_default_proxy_index(lc,tmp);
520
521         /* read authentication information */
522         for(i=0;; i++){
523                 LinphoneAuthInfo *ai=linphone_auth_info_new_from_config_file(lc->config,i);
524                 if (ai!=NULL){
525                         linphone_core_add_auth_info(lc,ai);
526                         linphone_auth_info_destroy(ai);
527                 }else{
528                         break;
529                 }
530         }
531         
532         /*for tuning or test*/
533         lc->sip_conf.sdp_200_ack=lp_config_get_int(lc->config,"sip","sdp_200_ack",0);
534         lc->sip_conf.only_one_codec=lp_config_get_int(lc->config,"sip","only_one_codec",0);
535         lc->sip_conf.register_only_when_network_is_up=
536                 lp_config_get_int(lc->config,"sip","register_only_when_network_is_up",1);
537         lc->sip_conf.ping_with_options=lp_config_get_int(lc->config,"sip","ping_with_options",1);
538         lc->sip_conf.auto_net_state_mon=lp_config_get_int(lc->config,"sip","auto_net_state_mon",1);
539 }
540
541 static void rtp_config_read(LinphoneCore *lc)
542 {
543         int port;
544         int jitt_comp;
545         int nortp_timeout;
546         port=lp_config_get_int(lc->config,"rtp","audio_rtp_port",7078);
547         linphone_core_set_audio_port(lc,port);
548
549         port=lp_config_get_int(lc->config,"rtp","video_rtp_port",9078);
550         if (port==0) port=9078;
551         linphone_core_set_video_port(lc,port);
552
553         jitt_comp=lp_config_get_int(lc->config,"rtp","audio_jitt_comp",60);
554         linphone_core_set_audio_jittcomp(lc,jitt_comp);
555         jitt_comp=lp_config_get_int(lc->config,"rtp","video_jitt_comp",60);
556         nortp_timeout=lp_config_get_int(lc->config,"rtp","nortp_timeout",30);
557         linphone_core_set_nortp_timeout(lc,nortp_timeout);
558 }
559
560 static PayloadType * find_payload(RtpProfile *prof, const char *mime_type, int clock_rate, const char *recv_fmtp){
561         PayloadType *candidate=NULL;
562         int i;
563         PayloadType *it;
564         for(i=0;i<127;++i){
565                 it=rtp_profile_get_payload(prof,i);
566                 if (it!=NULL && strcasecmp(mime_type,it->mime_type)==0
567                         && (clock_rate==it->clock_rate || clock_rate<=0) ){
568                         if ( (recv_fmtp && it->recv_fmtp && strstr(recv_fmtp,it->recv_fmtp)!=NULL) ||
569                                 (recv_fmtp==NULL && it->recv_fmtp==NULL) ){
570                                 /*exact match*/
571                                 if (recv_fmtp) payload_type_set_recv_fmtp(it,recv_fmtp);
572                                 return it;
573                         }else {
574                                 if (candidate){
575                                         if (it->recv_fmtp==NULL) candidate=it;
576                                 }else candidate=it;
577                         }
578                 }
579         }
580         if (candidate && recv_fmtp){
581                 payload_type_set_recv_fmtp(candidate,recv_fmtp);
582         }
583         return candidate;
584 }
585
586 static bool_t get_codec(LpConfig *config, char* type, int index, PayloadType **ret){
587         char codeckey[50];
588         const char *mime,*fmtp;
589         int rate,enabled;
590         PayloadType *pt;
591
592         *ret=NULL;
593         snprintf(codeckey,50,"%s_%i",type,index);
594         mime=lp_config_get_string(config,codeckey,"mime",NULL);
595         if (mime==NULL || strlen(mime)==0 ) return FALSE;
596
597         rate=lp_config_get_int(config,codeckey,"rate",8000);
598         fmtp=lp_config_get_string(config,codeckey,"recv_fmtp",NULL);
599         enabled=lp_config_get_int(config,codeckey,"enabled",1);
600         pt=find_payload(&av_profile,mime,rate,fmtp);
601         if (pt && enabled ) pt->flags|=PAYLOAD_TYPE_ENABLED;
602         //ms_message("Found codec %s/%i",pt->mime_type,pt->clock_rate);
603         if (pt==NULL) ms_warning("Ignoring codec config %s/%i with fmtp=%s because unsupported",
604                         mime,rate,fmtp ? fmtp : "");
605         *ret=pt;
606         return TRUE;
607 }
608
609 static const char *codec_pref_order[]={
610         "speex",
611         "gsm",
612         "pcmu",
613         "pcma",
614         "H264",
615         "MP4V-ES",
616         "theora",
617         "H263-1998",
618         "H263",
619         NULL,
620 };
621
622 static int find_codec_rank(const char *mime){
623         int i;
624         for(i=0;codec_pref_order[i]!=NULL;++i){
625                 if (strcasecmp(codec_pref_order[i],mime)==0)
626                         break;
627         }
628         return i;
629 }
630
631 static int codec_compare(const PayloadType *a, const PayloadType *b){
632         int ra,rb;
633         ra=find_codec_rank(a->mime_type);
634         rb=find_codec_rank(b->mime_type);
635         if (ra>rb) return 1;
636         if (ra<rb) return -1;
637         return 0;
638 }
639
640 static MSList *add_missing_codecs(SalStreamType mtype, MSList *l){
641         int i;
642         for(i=0;i<127;++i){
643                 PayloadType *pt=rtp_profile_get_payload(&av_profile,i);
644                 if (pt){
645                         if (mtype==SalVideo && pt->type!=PAYLOAD_VIDEO)
646                                 pt=NULL;
647                         else if (mtype==SalAudio && (pt->type!=PAYLOAD_AUDIO_PACKETIZED 
648                             && pt->type!=PAYLOAD_AUDIO_CONTINUOUS)){
649                                 pt=NULL;
650                         }
651                         if (pt && ms_filter_codec_supported(pt->mime_type)){
652                                 if (ms_list_find(l,pt)==NULL){
653                                         payload_type_set_flag(pt,PAYLOAD_TYPE_ENABLED);
654                                         ms_message("Adding new codec %s/%i with fmtp %s",
655                                             pt->mime_type,pt->clock_rate,pt->recv_fmtp ? pt->recv_fmtp : "");
656                                         l=ms_list_insert_sorted(l,pt,(int (*)(const void *, const void *))codec_compare);
657                                 }
658                         }
659                 }
660         }
661         return l;
662 }
663
664 static void codecs_config_read(LinphoneCore *lc)
665 {
666         int i;
667         PayloadType *pt;
668         MSList *audio_codecs=NULL;
669         MSList *video_codecs=NULL;
670         for (i=0;get_codec(lc->config,"audio_codec",i,&pt);i++){
671                 if (pt){
672                         if (!ms_filter_codec_supported(pt->mime_type)){
673                                 ms_warning("Codec %s is not supported by mediastreamer2, removed.",pt->mime_type);
674                         }else audio_codecs=ms_list_append(audio_codecs,pt);
675                 }
676         }
677         audio_codecs=add_missing_codecs(SalAudio,audio_codecs);
678         for (i=0;get_codec(lc->config,"video_codec",i,&pt);i++){
679                 if (pt){
680                         if (!ms_filter_codec_supported(pt->mime_type)){
681                                 ms_warning("Codec %s is not supported by mediastreamer2, removed.",pt->mime_type);
682                         }else video_codecs=ms_list_append(video_codecs,(void *)pt);
683                 }
684         }
685         video_codecs=add_missing_codecs(SalVideo,video_codecs);
686         linphone_core_set_audio_codecs(lc,audio_codecs);
687         linphone_core_set_video_codecs(lc,video_codecs);
688         linphone_core_update_allocated_audio_bandwidth(lc);
689 }
690
691 static void video_config_read(LinphoneCore *lc){
692         int capture, display, self_view;
693         int enabled;
694         const char *str;
695         int ndev;
696         const char **devices;
697         const MSList *elem;
698         int i;
699
700         /* retrieve all video devices */
701         elem=ms_web_cam_manager_get_list(ms_web_cam_manager_get());
702         ndev=ms_list_size(elem);
703         devices=ms_malloc((ndev+1)*sizeof(const char *));
704         for (i=0;elem!=NULL;elem=elem->next,i++){
705                 devices[i]=ms_web_cam_get_string_id((MSWebCam *)elem->data);
706         }
707         devices[ndev]=NULL;
708         lc->video_conf.cams=devices;
709
710         str=lp_config_get_string(lc->config,"video","device",NULL);
711         if (str && str[0]==0) str=NULL;
712         linphone_core_set_video_device(lc,str);
713
714         linphone_core_set_preferred_video_size_by_name(lc,
715                 lp_config_get_string(lc->config,"video","size","cif"));
716
717         enabled=lp_config_get_int(lc->config,"video","enabled",1);
718         capture=lp_config_get_int(lc->config,"video","capture",enabled);
719         display=lp_config_get_int(lc->config,"video","display",enabled);
720         self_view=lp_config_get_int(lc->config,"video","self_view",enabled);
721 #ifdef VIDEO_ENABLED
722         linphone_core_enable_video(lc,capture,display);
723         linphone_core_enable_self_view(lc,self_view);
724 #endif
725 }
726
727 static void ui_config_read(LinphoneCore *lc)
728 {
729         LinphoneFriend *lf;
730         int i;
731         for (i=0;(lf=linphone_friend_new_from_config_file(lc,i))!=NULL;i++){
732                 linphone_core_add_friend(lc,lf);
733         }
734         call_logs_read_from_config_file(lc);
735 }
736
737 /*
738 static void autoreplier_config_init(LinphoneCore *lc)
739 {
740         autoreplier_config_t *config=&lc->autoreplier_conf;
741         config->enabled=lp_config_get_int(lc->config,"autoreplier","enabled",0);
742         config->after_seconds=lp_config_get_int(lc->config,"autoreplier","after_seconds",6);
743         config->max_users=lp_config_get_int(lc->config,"autoreplier","max_users",1);
744         config->max_rec_time=lp_config_get_int(lc->config,"autoreplier","max_rec_time",60);
745         config->max_rec_msg=lp_config_get_int(lc->config,"autoreplier","max_rec_msg",10);
746         config->message=lp_config_get_string(lc->config,"autoreplier","message",NULL);
747 }
748 */
749
750 /**
751  * Sets maximum available download bandwidth
752  *
753  * @ingroup media_parameters
754  *
755  * This is IP bandwidth, in kbit/s.
756  * This information is used signaled to other parties during
757  * calls (within SDP messages) so that the remote end can have
758  * sufficient knowledge to properly configure its audio & video
759  * codec output bitrate to not overflow available bandwidth.
760  *
761  * @param lc the LinphoneCore object
762  * @param bw the bandwidth in kbits/s, 0 for infinite
763  */
764 void linphone_core_set_download_bandwidth(LinphoneCore *lc, int bw){
765         lc->net_conf.download_bw=bw;
766         if (bw==0){ /*infinite*/
767                 lc->dw_audio_bw=-1;
768                 lc->dw_video_bw=-1;
769         }else {
770                 lc->dw_audio_bw=MIN(lc->audio_bw,bw);
771                 lc->dw_video_bw=MAX(bw-lc->dw_audio_bw-10,0);/*-10: security margin*/
772         }
773 }
774
775 /**
776  * Sets maximum available upload bandwidth
777  *
778  * @ingroup media_parameters
779  *
780  * This is IP bandwidth, in kbit/s.
781  * This information is used by liblinphone together with remote
782  * side available bandwidth signaled in SDP messages to properly
783  * configure audio & video codec's output bitrate.
784  *
785  * @param lc the LinphoneCore object
786  * @param bw the bandwidth in kbits/s, 0 for infinite
787  */
788 void linphone_core_set_upload_bandwidth(LinphoneCore *lc, int bw){
789         lc->net_conf.upload_bw=bw;
790         if (bw==0){ /*infinite*/
791                 lc->up_audio_bw=-1;
792                 lc->up_video_bw=-1;
793         }else{
794                 lc->up_audio_bw=MIN(lc->audio_bw,bw);
795                 lc->up_video_bw=MAX(bw-lc->up_audio_bw-10,0);/*-10: security margin*/
796         }
797 }
798
799 /**
800  * Retrieve the maximum available download bandwidth.
801  *
802  * @ingroup media_parameters
803  *
804  * This value was set by linphone_core_set_download_bandwidth().
805  *
806 **/
807 int linphone_core_get_download_bandwidth(const LinphoneCore *lc){
808         return lc->net_conf.download_bw;
809 }
810
811 /**
812  * Retrieve the maximum available upload bandwidth.
813  *
814  * @ingroup media_parameters
815  *
816  * This value was set by linphone_core_set_upload_bandwidth().
817  *
818 **/
819 int linphone_core_get_upload_bandwidth(const LinphoneCore *lc){
820         return lc->net_conf.upload_bw;
821 }
822 /**
823  * set audio packetization time linphone expect to received from peer
824  */
825 void linphone_core_set_download_ptime(LinphoneCore *lc, int ptime) {
826         lc->net_conf.down_ptime=ptime;
827 }
828
829 int  linphone_core_get_download_ptime(LinphoneCore *lc) {
830         return lc->net_conf.down_ptime;
831 }
832
833
834 /**
835  * Returns liblinphone's version as a string.
836  *
837  * @ingroup misc
838  *
839 **/
840 const char * linphone_core_get_version(void){
841         return liblinphone_version;
842 }
843
844
845 static MSList *linphone_payload_types=NULL;
846
847 static void linphone_core_assign_payload_type(PayloadType *const_pt, int number, const char *recv_fmtp){
848         PayloadType *pt;
849         pt=payload_type_clone(const_pt);
850         payload_type_set_number(pt,number);
851         if (recv_fmtp!=NULL) payload_type_set_recv_fmtp(pt,recv_fmtp);
852         rtp_profile_set_payload(&av_profile,number,pt);
853         linphone_payload_types=ms_list_append(linphone_payload_types,pt);
854 }
855
856 static void linphone_core_free_payload_types(void){
857         ms_list_for_each(linphone_payload_types,(void (*)(void*))payload_type_destroy);
858         ms_list_free(linphone_payload_types);
859         linphone_payload_types=NULL;
860 }
861
862 static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vtable, const char *config_path, 
863     const char *factory_config_path, void * userdata)
864 {
865         memset (lc, 0, sizeof (LinphoneCore));
866         lc->data=userdata;
867
868         memcpy(&lc->vtable,vtable,sizeof(LinphoneCoreVTable));
869
870         gstate_initialize(lc);
871         gstate_new_state(lc, GSTATE_POWER_STARTUP, NULL);
872
873         ortp_init();
874         linphone_core_assign_payload_type(&payload_type_pcmu8000,0,NULL);
875         linphone_core_assign_payload_type(&payload_type_gsm,3,NULL);
876         linphone_core_assign_payload_type(&payload_type_pcma8000,8,NULL);
877         linphone_core_assign_payload_type(&payload_type_lpc1015,115,NULL);
878         linphone_core_assign_payload_type(&payload_type_speex_nb,110,"vbr=on");
879         linphone_core_assign_payload_type(&payload_type_speex_wb,111,"vbr=on");
880         linphone_core_assign_payload_type(&payload_type_speex_uwb,112,"vbr=on");
881         linphone_core_assign_payload_type(&payload_type_telephone_event,101,"0-11");
882         linphone_core_assign_payload_type(&payload_type_ilbc,113,"mode=30");
883         linphone_core_assign_payload_type(&payload_type_amr,114,"octet-align=1");
884
885 #ifdef ENABLE_NONSTANDARD_GSM
886         {
887                 PayloadType *pt;
888                 pt=payload_type_clone(&payload_type_gsm);
889                 pt->clock_rate=11025;
890                 rtp_profile_set_payload(&av_profile,114,pt);
891                 linphone_payload_types=ms_list_append(linphone_payload_types,pt);
892                 pt=payload_type_clone(&payload_type_gsm);
893                 pt->clock_rate=22050;
894                 rtp_profile_set_payload(&av_profile,115,pt);
895                 linphone_payload_types=ms_list_append(linphone_payload_types,pt);
896         }
897 #endif
898
899 #ifdef VIDEO_ENABLED
900         linphone_core_assign_payload_type(&payload_type_h263,34,NULL);
901         linphone_core_assign_payload_type(&payload_type_theora,97,NULL);
902         linphone_core_assign_payload_type(&payload_type_h263_1998,98,"CIF=1;QCIF=1");
903         linphone_core_assign_payload_type(&payload_type_mp4v,99,"profile-level-id=3");
904         linphone_core_assign_payload_type(&payload_type_x_snow,100,NULL);
905         linphone_core_assign_payload_type(&payload_type_h264,102,NULL);
906         linphone_core_assign_payload_type(&payload_type_h264,103,"packetization-mode=1");
907 #endif
908
909         ms_init();
910
911         lc->config=lp_config_new(config_path);
912         if (factory_config_path)
913                 lp_config_read_file(lc->config,factory_config_path);
914
915         lc->sal=sal_init();
916         sal_set_user_pointer(lc->sal,lc);
917         sal_set_callbacks(lc->sal,&linphone_sal_callbacks);
918         if (lp_config_get_int(lc->config,"sip","use_session_timers",0)==1){
919                 sal_use_session_timers(lc->sal,200);
920         }
921         sip_setup_register_all();
922         sound_config_read(lc);
923         net_config_read(lc);
924         rtp_config_read(lc);
925         codecs_config_read(lc);
926         sip_config_read(lc); /* this will start eXosip*/
927         video_config_read(lc);
928         //autoreplier_config_init(&lc->autoreplier_conf);
929         lc->prev_mode=LINPHONE_STATUS_ONLINE;
930         lc->presence_mode=LINPHONE_STATUS_ONLINE;
931         lc->max_call_logs=15;
932         ui_config_read(lc);
933         lc->vtable.display_status(lc,_("Ready"));
934         gstate_new_state(lc, GSTATE_POWER_ON, NULL);
935         lc->auto_net_state_mon=lc->sip_conf.auto_net_state_mon;
936
937     lc->ready=TRUE;
938 }
939
940 /**
941  * Instanciates a LinphoneCore object.
942  * @ingroup initializing
943  * 
944  * The LinphoneCore object is the primary handle for doing all phone actions.
945  * It should be unique within your application.
946  * @param vtable a LinphoneCoreVTable structure holding your application callbacks
947  * @param config_path a path to a config file. If it does not exists it will be created.
948  *        The config file is used to store all user settings, call logs, friends, proxies...
949  * @param factory_config_path a path to a read-only config file that can be used to 
950  *        to store hard-coded preference such as proxy settings or internal preferences.
951  *        The settings in this factory file always override the one in the normal config file.
952  *        It is OPTIONAL, use NULL if unneeded.
953  * @param userdata an opaque user pointer that can be retrieved at any time (for example in
954  *        callbacks) using linphone_core_get_user_data().
955  * 
956 **/
957 LinphoneCore *linphone_core_new(const LinphoneCoreVTable *vtable,
958                                                 const char *config_path, const char *factory_config_path, void * userdata)
959 {
960         LinphoneCore *core=ms_new(LinphoneCore,1);
961         linphone_core_init(core,vtable,config_path, factory_config_path, userdata);
962         return core;
963 }
964
965 /**
966  * Returns the list of available audio codecs.
967  *
968  * This list is unmodifiable. The ->data field of the MSList points a PayloadType
969  * structure holding the codec information.
970  * It is possible to make copy of the list with ms_list_copy() in order to modify it
971  * (such as the order of codecs).
972 **/
973 const MSList *linphone_core_get_audio_codecs(const LinphoneCore *lc)
974 {
975         return lc->codecs_conf.audio_codecs;
976 }
977
978 /**
979  * Returns the list of available video codecs.
980  *
981  * This list is unmodifiable. The ->data field of the MSList points a PayloadType
982  * structure holding the codec information.
983  * It is possible to make copy of the list with ms_list_copy() in order to modify it
984  * (such as the order of codecs).
985 **/
986 const MSList *linphone_core_get_video_codecs(const LinphoneCore *lc)
987 {
988         return lc->codecs_conf.video_codecs;
989 }
990
991 /**
992  * Sets the local "from" identity.
993  *
994  * @ingroup proxies
995  * This data is used in absence of any proxy configuration or when no
996  * default proxy configuration is set. See LinphoneProxyConfig
997 **/
998 int linphone_core_set_primary_contact(LinphoneCore *lc, const char *contact)
999 {
1000         LinphoneAddress *ctt;
1001
1002         if ((ctt=linphone_address_new(contact))==0) {
1003                 ms_error("Bad contact url: %s",contact);
1004                 return -1;
1005         }
1006         if (lc->sip_conf.contact!=NULL) ms_free(lc->sip_conf.contact);
1007         lc->sip_conf.contact=ms_strdup(contact);
1008         if (lc->sip_conf.guessed_contact!=NULL){
1009                 ms_free(lc->sip_conf.guessed_contact);
1010                 lc->sip_conf.guessed_contact=NULL;
1011         }
1012         linphone_address_destroy(ctt);
1013         return 0;
1014 }
1015
1016
1017 /*result must be an array of chars at least LINPHONE_IPADDR_SIZE */
1018 void linphone_core_get_local_ip(LinphoneCore *lc, const char *dest, char *result){
1019         if (linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_NAT_ADDRESS){
1020                 strncpy(result,linphone_core_get_nat_address(lc),LINPHONE_IPADDR_SIZE);
1021                 return;
1022         }
1023         if (linphone_core_get_local_ip_for(lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,dest,result)==0)
1024                 return;
1025         /*else fallback to SAL routine that will attempt to find the most realistic interface */
1026         sal_get_default_local_ip(lc->sal,lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,result,LINPHONE_IPADDR_SIZE);
1027 }
1028
1029 static void update_primary_contact(LinphoneCore *lc){
1030         char *guessed=NULL;
1031         char tmp[LINPHONE_IPADDR_SIZE];
1032
1033         LinphoneAddress *url;
1034         if (lc->sip_conf.guessed_contact!=NULL){
1035                 ms_free(lc->sip_conf.guessed_contact);
1036                 lc->sip_conf.guessed_contact=NULL;
1037         }
1038         url=linphone_address_new(lc->sip_conf.contact);
1039         if (!url){
1040                 ms_error("Could not parse identity contact !");
1041                 url=linphone_address_new("sip:unknown@unkwownhost");
1042         }
1043         linphone_core_get_local_ip(lc, NULL, tmp);
1044         if (strcmp(tmp,"127.0.0.1")==0 || strcmp(tmp,"::1")==0 ){
1045                 ms_warning("Local loopback network only !");
1046                 lc->sip_conf.loopback_only=TRUE;
1047         }else lc->sip_conf.loopback_only=FALSE;
1048         linphone_address_set_domain(url,tmp);
1049         linphone_address_set_port_int(url,lc->sip_conf.sip_port);
1050         guessed=linphone_address_as_string(url);
1051         lc->sip_conf.guessed_contact=guessed;
1052         linphone_address_destroy(url);
1053 }
1054
1055 /**
1056  * Returns the default identity when no proxy configuration is used.
1057  *
1058  * @ingroup proxies
1059 **/
1060 const char *linphone_core_get_primary_contact(LinphoneCore *lc){
1061         char *identity;
1062         
1063         if (lc->sip_conf.guess_hostname){
1064                 if (lc->sip_conf.guessed_contact==NULL || lc->sip_conf.loopback_only){
1065                         update_primary_contact(lc);
1066                 }
1067                 identity=lc->sip_conf.guessed_contact;
1068         }else{
1069                 identity=lc->sip_conf.contact;
1070         }
1071         return identity;
1072 }
1073
1074 /**
1075  * Tells LinphoneCore to guess local hostname automatically in primary contact.
1076  *
1077  * @ingroup proxies
1078 **/
1079 void linphone_core_set_guess_hostname(LinphoneCore *lc, bool_t val){
1080         lc->sip_conf.guess_hostname=val;
1081 }
1082
1083 /**
1084  * Returns TRUE if hostname part of primary contact is guessed automatically.
1085  *
1086  * @ingroup proxies
1087 **/
1088 bool_t linphone_core_get_guess_hostname(LinphoneCore *lc){
1089         return lc->sip_conf.guess_hostname;
1090 }
1091
1092 /**
1093  * Same as linphone_core_get_primary_contact() but the result is a LinphoneAddress object
1094  * instead of const char*
1095  *
1096  * @ingroup proxies
1097 **/
1098 LinphoneAddress *linphone_core_get_primary_contact_parsed(LinphoneCore *lc){
1099         return linphone_address_new(linphone_core_get_primary_contact(lc));
1100 }
1101
1102 /**
1103  * Sets the list of audio codecs.
1104  *
1105  * @ingroup media_parameters
1106  * The list is taken by the LinphoneCore thus the application should not free it.
1107  * This list is made of struct PayloadType describing the codec parameters.
1108 **/
1109 int linphone_core_set_audio_codecs(LinphoneCore *lc, MSList *codecs)
1110 {
1111         if (lc->codecs_conf.audio_codecs!=NULL) ms_list_free(lc->codecs_conf.audio_codecs);
1112         lc->codecs_conf.audio_codecs=codecs;
1113         return 0;
1114 }
1115
1116 /**
1117  * Sets the list of video codecs.
1118  *
1119  * @ingroup media_parameters
1120  * The list is taken by the LinphoneCore thus the application should not free it.
1121  * This list is made of struct PayloadType describing the codec parameters.
1122 **/
1123 int linphone_core_set_video_codecs(LinphoneCore *lc, MSList *codecs)
1124 {
1125         if (lc->codecs_conf.video_codecs!=NULL) ms_list_free(lc->codecs_conf.video_codecs);
1126         lc->codecs_conf.video_codecs=codecs;
1127         return 0;
1128 }
1129
1130 const MSList * linphone_core_get_friend_list(const LinphoneCore *lc)
1131 {
1132         return lc->friends;
1133 }
1134
1135 /**
1136  * Returns the nominal jitter buffer size in milliseconds.
1137  *
1138  * @ingroup media_parameters
1139 **/
1140 int linphone_core_get_audio_jittcomp(LinphoneCore *lc)
1141 {
1142         return lc->rtp_conf.audio_jitt_comp;
1143 }
1144
1145 /**
1146  * Returns the UDP port used for audio streaming.
1147  *
1148  * @ingroup network_parameters
1149 **/
1150 int linphone_core_get_audio_port(const LinphoneCore *lc)
1151 {
1152         return lc->rtp_conf.audio_rtp_port;
1153 }
1154
1155 /**
1156  * Returns the UDP port used for video streaming.
1157  *
1158  * @ingroup network_parameters
1159 **/
1160 int linphone_core_get_video_port(const LinphoneCore *lc){
1161         return lc->rtp_conf.video_rtp_port;
1162 }
1163
1164
1165 /**
1166  * Returns the value in seconds of the no-rtp timeout.
1167  *
1168  * @ingroup media_parameters
1169  * When no RTP or RTCP packets have been received for a while
1170  * LinphoneCore will consider the call is broken (remote end crashed or
1171  * disconnected from the network), and thus will terminate the call.
1172  * The no-rtp timeout is the duration above which the call is considered broken.
1173 **/
1174 int linphone_core_get_nortp_timeout(const LinphoneCore *lc){
1175         return lc->rtp_conf.nortp_timeout;
1176 }
1177
1178 /**
1179  * Sets the nominal audio jitter buffer size in milliseconds.
1180  *
1181  * @ingroup media_parameters
1182 **/
1183 void linphone_core_set_audio_jittcomp(LinphoneCore *lc, int value)
1184 {
1185         lc->rtp_conf.audio_jitt_comp=value;
1186 }
1187
1188 /**
1189  * Sets the UDP port used for audio streaming.
1190  *
1191  * @ingroup network_parameters
1192 **/
1193 void linphone_core_set_audio_port(LinphoneCore *lc, int port)
1194 {
1195         lc->rtp_conf.audio_rtp_port=port;
1196 }
1197
1198 /**
1199  * Sets the UDP port used for video streaming.
1200  *
1201  * @ingroup network_parameters
1202 **/
1203 void linphone_core_set_video_port(LinphoneCore *lc, int port){
1204         lc->rtp_conf.video_rtp_port=port;
1205 }
1206
1207 /**
1208  * Sets the no-rtp timeout value in seconds.
1209  * 
1210  * @ingroup media_parameters
1211  * See linphone_core_get_nortp_timeout() for details.
1212 **/
1213 void linphone_core_set_nortp_timeout(LinphoneCore *lc, int nortp_timeout){
1214         lc->rtp_conf.nortp_timeout=nortp_timeout;
1215 }
1216
1217 /**
1218  * Indicates whether SIP INFO is used for sending digits.
1219  *
1220  * @ingroup media_parameters
1221 **/
1222 bool_t linphone_core_get_use_info_for_dtmf(LinphoneCore *lc)
1223 {
1224         return lc->sip_conf.use_info;
1225 }
1226
1227 /**
1228  * Sets whether SIP INFO is to be used for sending digits.
1229  *
1230  * @ingroup media_parameters
1231 **/
1232 void linphone_core_set_use_info_for_dtmf(LinphoneCore *lc,bool_t use_info)
1233 {
1234         lc->sip_conf.use_info=use_info;
1235 }
1236
1237 /**
1238  * Indicates whether RFC2833 is used for sending digits.
1239  *
1240  * @ingroup media_parameters
1241 **/
1242 bool_t linphone_core_get_use_rfc2833_for_dtmf(LinphoneCore *lc)
1243 {
1244         return lc->sip_conf.use_rfc2833;
1245 }
1246
1247 /**
1248  * Sets whether RFC2833 is to be used for sending digits.
1249  *
1250  * @ingroup media_parameters
1251 **/
1252 void linphone_core_set_use_rfc2833_for_dtmf(LinphoneCore *lc,bool_t use_rfc2833)
1253 {
1254         lc->sip_conf.use_rfc2833=use_rfc2833;
1255 }
1256
1257 /**
1258  * Returns the UDP port used by SIP.
1259  *
1260  * @ingroup network_parameters
1261 **/
1262 int linphone_core_get_sip_port(LinphoneCore *lc)
1263 {
1264         return lc->sip_conf.sip_port;
1265 }
1266
1267 static char _ua_name[64]="Linphone";
1268 static char _ua_version[64]=LINPHONE_VERSION;
1269
1270 #ifdef HAVE_EXOSIP_GET_VERSION
1271 extern const char *eXosip_get_version();
1272 #endif
1273
1274 static void apply_user_agent(LinphoneCore *lc){
1275         char ua_string[256];
1276         snprintf(ua_string,sizeof(ua_string)-1,"%s/%s (eXosip2/%s)",_ua_name,_ua_version,
1277 #ifdef HAVE_EXOSIP_GET_VERSION
1278                  eXosip_get_version()
1279 #else
1280                  "unknown"
1281 #endif
1282         );
1283         if (lc->sal) sal_set_user_agent(lc->sal,ua_string);
1284 }
1285
1286 /**
1287  * Sets the user agent string used in SIP messages.
1288  *
1289  * @ingroup misc
1290 **/
1291 void linphone_core_set_user_agent(const char *name, const char *ver){
1292         strncpy(_ua_name,name,sizeof(_ua_name)-1);
1293         strncpy(_ua_version,ver,sizeof(_ua_version));
1294 }
1295
1296 /**
1297  * Sets the UDP port to be used by SIP.
1298  *
1299  * @ingroup network_parameters
1300 **/
1301 void linphone_core_set_sip_port(LinphoneCore *lc,int port)
1302 {
1303         const char *anyaddr;
1304         int err=0;
1305         if (port==lc->sip_conf.sip_port) return;
1306         lc->sip_conf.sip_port=port;
1307
1308         if (lc->sal==NULL) return;
1309         
1310         if (lc->sip_conf.ipv6_enabled)
1311                 anyaddr="::0";
1312         else
1313                 anyaddr="0.0.0.0";
1314         err=sal_listen_port (lc->sal,anyaddr,port, SalTransportDatagram,FALSE);
1315         if (err<0){
1316                 char *msg=ortp_strdup_printf("UDP port %i seems already in use ! Cannot initialize.",port);
1317                 ms_warning(msg);
1318                 lc->vtable.display_warning(lc,msg);
1319                 ms_free(msg);
1320                 return;
1321         }
1322         apply_user_agent(lc);
1323 }
1324
1325 /**
1326  * Returns TRUE if IPv6 is enabled.
1327  *
1328  * @ingroup network_parameters
1329  * See linphone_core_enable_ipv6() for more details on how IPv6 is supported in liblinphone.
1330 **/
1331 bool_t linphone_core_ipv6_enabled(LinphoneCore *lc){
1332         return lc->sip_conf.ipv6_enabled;
1333 }
1334
1335 /**
1336  * Turns IPv6 support on or off.
1337  *
1338  * @ingroup network_parameters
1339  *
1340  * @note IPv6 support is exclusive with IPv4 in liblinphone:
1341  * when IPv6 is turned on, IPv4 calls won't be possible anymore.
1342  * By default IPv6 support is off.
1343 **/
1344 void linphone_core_enable_ipv6(LinphoneCore *lc, bool_t val){
1345         if (lc->sip_conf.ipv6_enabled!=val){
1346                 lc->sip_conf.ipv6_enabled=val;
1347                 if (lc->sal){
1348                         /* we need to restart eXosip */
1349                         linphone_core_set_sip_port(lc, lc->sip_conf.sip_port);
1350                 }
1351         }
1352 }
1353
1354 static void display_bandwidth(RtpSession *as, RtpSession *vs){
1355         ms_message("bandwidth usage: audio=[d=%.1f,u=%.1f] video=[d=%.1f,u=%.1f] kbit/sec",
1356         (as!=NULL) ? (rtp_session_compute_recv_bandwidth(as)*1e-3) : 0,
1357         (as!=NULL) ? (rtp_session_compute_send_bandwidth(as)*1e-3) : 0,
1358         (vs!=NULL) ? (rtp_session_compute_recv_bandwidth(vs)*1e-3) : 0,
1359         (vs!=NULL) ? (rtp_session_compute_send_bandwidth(vs)*1e-3) : 0);
1360 }
1361
1362 static void linphone_core_disconnected(LinphoneCore *lc){
1363         lc->vtable.display_warning(lc,_("Remote end seems to have disconnected, the call is going to be closed."));
1364         linphone_core_terminate_call(lc,NULL);
1365 }
1366
1367 static void monitor_network_state(LinphoneCore *lc, time_t curtime){
1368         static time_t last_check=0;
1369         static bool_t last_status=FALSE;
1370         char result[LINPHONE_IPADDR_SIZE];
1371         bool_t new_status=last_status;
1372
1373         /* only do the network up checking every five seconds */
1374         if (last_check==0 || (curtime-last_check)>=5){
1375                 linphone_core_get_local_ip_for(lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,NULL,result);
1376                 if (strcmp(result,"::1")!=0 && strcmp(result,"127.0.0.1")!=0){
1377                         new_status=TRUE;
1378                 }else new_status=FALSE;
1379                 last_check=curtime;
1380                 if (new_status!=last_status) {
1381                         if (new_status){
1382                                 ms_message("New local ip address is %s",result);
1383                         }
1384                         set_network_reachable(lc,new_status);
1385                         last_status=new_status;
1386                 }
1387         }
1388 }
1389
1390 static void proxy_update(LinphoneCore *lc){
1391         ms_list_for_each(lc->sip_conf.proxies,(void (*)(void*))&linphone_proxy_config_update);
1392 }
1393
1394 static void assign_buddy_info(LinphoneCore *lc, BuddyInfo *info){
1395         LinphoneFriend *lf=linphone_core_get_friend_by_address(lc,info->sip_uri);
1396         if (lf!=NULL){
1397                 lf->info=info;
1398                 ms_message("%s has a BuddyInfo assigned with image %p",info->sip_uri, info->image_data);
1399                 if (lc->vtable.buddy_info_updated)
1400                         lc->vtable.buddy_info_updated(lc,lf);
1401         }else{
1402                 ms_warning("Could not any friend with uri %s",info->sip_uri);
1403         }
1404 }
1405
1406 static void analyze_buddy_lookup_results(LinphoneCore *lc, LinphoneProxyConfig *cfg){
1407         MSList *elem;
1408         SipSetupContext *ctx=linphone_proxy_config_get_sip_setup_context(cfg);
1409         for (elem=lc->bl_reqs;elem!=NULL;elem=ms_list_next(elem)){
1410                 BuddyLookupRequest *req=(BuddyLookupRequest *)elem->data;
1411                 if (req->status==BuddyLookupDone || req->status==BuddyLookupFailure){
1412                         if (req->results!=NULL){
1413                                 BuddyInfo *i=(BuddyInfo*)req->results->data;
1414                                 ms_list_free(req->results);
1415                                 req->results=NULL;
1416                                 assign_buddy_info(lc,i);
1417                         }
1418                         sip_setup_context_buddy_lookup_free(ctx,req);
1419                         elem->data=NULL;
1420                 }
1421         }
1422         /*purge completed requests */
1423         while((elem=ms_list_find(lc->bl_reqs,NULL))!=NULL){
1424                 lc->bl_reqs=ms_list_remove_link(lc->bl_reqs,elem);
1425         }
1426 }
1427
1428 static void linphone_core_grab_buddy_infos(LinphoneCore *lc, LinphoneProxyConfig *cfg){
1429         const MSList *elem;
1430         SipSetupContext *ctx=linphone_proxy_config_get_sip_setup_context(cfg);
1431         for(elem=linphone_core_get_friend_list(lc);elem!=NULL;elem=elem->next){
1432                 LinphoneFriend *lf=(LinphoneFriend*)elem->data;
1433                 if (lf->info==NULL){
1434                         if (linphone_core_lookup_known_proxy(lc,lf->uri)==cfg){
1435                                 if (linphone_address_get_username(lf->uri)!=NULL){
1436                                         BuddyLookupRequest *req;
1437                                         char *tmp=linphone_address_as_string_uri_only(lf->uri);
1438                                         req=sip_setup_context_create_buddy_lookup_request(ctx);
1439                                         buddy_lookup_request_set_key(req,tmp);
1440                                         buddy_lookup_request_set_max_results(req,1);
1441                                         sip_setup_context_buddy_lookup_submit(ctx,req);
1442                                         lc->bl_reqs=ms_list_append(lc->bl_reqs,req);
1443                                         ms_free(tmp);
1444                                 }
1445                         }
1446                 }
1447         }
1448 }
1449
1450 static void linphone_core_do_plugin_tasks(LinphoneCore *lc){
1451         LinphoneProxyConfig *cfg=NULL;
1452         linphone_core_get_default_proxy(lc,&cfg);
1453         if (cfg){
1454                 if (lc->bl_refresh){
1455                         SipSetupContext *ctx=linphone_proxy_config_get_sip_setup_context(cfg);
1456                         if (ctx && (sip_setup_context_get_capabilities(ctx) & SIP_SETUP_CAP_BUDDY_LOOKUP)){
1457                                 linphone_core_grab_buddy_infos(lc,cfg);
1458                                 lc->bl_refresh=FALSE;
1459                         }
1460                 }
1461                 if (lc->bl_reqs) analyze_buddy_lookup_results(lc,cfg);
1462         }
1463 }
1464
1465 /**
1466  * Main loop function. It is crucial that your application call it periodically.
1467  *
1468  * @ingroup initializing
1469  * linphone_core_iterate() performs various backgrounds tasks:
1470  * - receiving of SIP messages
1471  * - handles timers and timeout
1472  * - performs registration to proxies
1473  * - authentication retries
1474  * The application MUST call this function from periodically, in its main loop.
1475  * Be careful that this function must be call from the same thread as
1476  * other liblinphone methods. In not the case make sure all liblinphone calls are 
1477  * serialized with a mutex.
1478 **/
1479 void linphone_core_iterate(LinphoneCore *lc){
1480         int disconnect_timeout = linphone_core_get_nortp_timeout(lc);
1481         time_t curtime=time(NULL);
1482         int elapsed;
1483         bool_t one_second_elapsed=FALSE;
1484         bool_t disconnected=FALSE;
1485
1486         if (curtime-lc->prevtime>=1){
1487                 lc->prevtime=curtime;
1488                 one_second_elapsed=TRUE;
1489         }
1490
1491         if (lc->preview_finished){
1492                 lc->preview_finished=0;
1493                 ring_stop(lc->ringstream);
1494                 lc->ringstream=NULL;
1495                 lc_callback_obj_invoke(&lc->preview_finished_cb,lc);
1496         }
1497
1498         sal_iterate(lc->sal);
1499         if (lc->auto_net_state_mon) monitor_network_state(lc,curtime);
1500
1501         proxy_update(lc);
1502         LinphoneCall *call = linphone_core_get_current_call(lc);
1503         if(call){
1504                 if (call->state==LinphoneCallPreEstablishing && (curtime-call->start_time>=2)){
1505                         /*start the call even if the OPTIONS reply did not arrive*/
1506                         linphone_core_start_invite(lc,call,NULL);
1507                 }
1508                 if (call->dir==LinphoneCallIncoming && call->state==LinphoneCallRinging){
1509                         elapsed=curtime-call->start_time;
1510                         ms_message("incoming call ringing for %i seconds",elapsed);
1511                         if (elapsed>lc->sip_conf.inc_timeout){
1512                                 call->log->status=LinphoneCallMissed;
1513                                 linphone_core_terminate_call(lc,NULL);
1514                         }
1515                 }else if (call->state==LinphoneCallAVRunning){
1516                         if (one_second_elapsed){
1517                                 RtpSession *as=NULL,*vs=NULL;
1518                                 lc->prevtime=curtime;
1519                                 if (lc->audiostream!=NULL)
1520                                         as=lc->audiostream->session;
1521                                 if (lc->videostream!=NULL)
1522                                         vs=lc->videostream->session;
1523                                 display_bandwidth(as,vs);
1524                         }
1525 #ifdef VIDEO_ENABLED
1526                         if (lc->videostream!=NULL)
1527                                 video_stream_iterate(lc->videostream);
1528 #endif
1529                         if (lc->audiostream!=NULL && disconnect_timeout>0)
1530                                 disconnected=!audio_stream_alive(lc->audiostream,disconnect_timeout);
1531                 }
1532         }
1533         if (linphone_core_video_preview_enabled(lc)){
1534                 if (lc->previewstream==NULL)
1535                         toggle_video_preview(lc,TRUE);
1536 #ifdef VIDEO_ENABLED
1537                 else video_stream_iterate(lc->previewstream);
1538 #endif
1539         }else{
1540                 if (lc->previewstream!=NULL)
1541                         toggle_video_preview(lc,FALSE);
1542         }
1543         if (disconnected)
1544                 linphone_core_disconnected(lc);
1545
1546         linphone_core_do_plugin_tasks(lc);
1547
1548         if (one_second_elapsed && lp_config_needs_commit(lc->config)){
1549                 lp_config_sync(lc->config);
1550         }
1551 }
1552
1553 /**
1554  * Interpret a call destination as supplied by the user, and returns a fully qualified
1555  * LinphoneAddress.
1556  *
1557  * A sip address should look like DisplayName <sip:username@domain:port> .
1558  * Basically this function performs the following tasks
1559  * - if a phone number is entered, prepend country prefix of the default proxy
1560  *   configuration, eventually escape the '+' by 00.
1561  * - if no domain part is supplied, append the domain name of the default proxy
1562  * - if no sip: is present, prepend it
1563  * 
1564  * The result is a syntaxically correct SIP address.
1565 **/
1566
1567 LinphoneAddress * linphone_core_interpret_url(LinphoneCore *lc, const char *url){
1568         enum_lookup_res_t *enumres=NULL;
1569         char *enum_domain=NULL;
1570         LinphoneProxyConfig *proxy=lc->default_proxy;;
1571         char *tmpurl;
1572         LinphoneAddress *uri;
1573         
1574         if (is_enum(url,&enum_domain)){
1575                 lc->vtable.display_status(lc,_("Looking for telephone number destination..."));
1576                 if (enum_lookup(enum_domain,&enumres)<0){
1577                         lc->vtable.display_status(lc,_("Could not resolve this number."));
1578                         if(lc->vtable.failure_recv)
1579                                 lc->vtable.failure_recv(lc,NULL,400);
1580                         ms_free(enum_domain);
1581                         return NULL;
1582                 }
1583                 ms_free(enum_domain);
1584                 tmpurl=enumres->sip_address[0];
1585                 uri=linphone_address_new(tmpurl);
1586                 enum_lookup_res_free(enumres);
1587                 return uri;
1588         }
1589         /* check if we have a "sip:" */
1590         if (strstr(url,"sip:")==NULL){
1591                 /* this doesn't look like a true sip uri */
1592                 if (strchr(url,'@')!=NULL){
1593                         /* seems like sip: is missing !*/
1594                         tmpurl=ms_strdup_printf("sip:%s",url);
1595                         uri=linphone_address_new(tmpurl);
1596                         ms_free(tmpurl);
1597                         if (uri){
1598                                 return uri;
1599                         }
1600                 }
1601                 
1602                 if (proxy!=NULL){
1603                         /* append the proxy domain suffix */
1604                         const char *identity=linphone_proxy_config_get_identity(proxy);
1605                         char normalized_username[128];
1606                         uri=linphone_address_new(identity);
1607                         if (uri==NULL){
1608                                 return NULL;
1609                         }
1610                         linphone_address_set_display_name(uri,NULL);
1611                         linphone_proxy_config_normalize_number(proxy,url,normalized_username,
1612                                                                 sizeof(normalized_username));
1613                         linphone_address_set_username(uri,normalized_username);
1614                         return uri;
1615                 }else return NULL;
1616         }
1617         uri=linphone_address_new(url);
1618         if (uri!=NULL){
1619                 return uri;
1620         }
1621         /* else we could not do anything with url given by user, so display an error */
1622         if (lc->vtable.display_warning!=NULL){
1623                 lc->vtable.display_warning(lc,_("Could not parse given sip address. A sip url usually looks like sip:user@domain"));
1624         }
1625         return NULL;
1626 }
1627
1628 /**
1629  * Returns the default identity SIP address.
1630  *
1631  * @ingroup proxiesb
1632  * This is an helper function:
1633  *
1634  * If no default proxy is set, this will return the primary contact (
1635  * see linphone_core_get_primary_contact() ). If a default proxy is set
1636  * it returns the registered identity on the proxy.
1637 **/
1638 const char * linphone_core_get_identity(LinphoneCore *lc){
1639         LinphoneProxyConfig *proxy=NULL;
1640         const char *from;
1641         linphone_core_get_default_proxy(lc,&proxy);
1642         if (proxy!=NULL) {
1643                 from=linphone_proxy_config_get_identity(proxy);
1644         }else from=linphone_core_get_primary_contact(lc);
1645         return from;
1646 }
1647
1648 const char * linphone_core_get_route(LinphoneCore *lc){
1649         LinphoneProxyConfig *proxy=NULL;
1650         const char *route=NULL;
1651         linphone_core_get_default_proxy(lc,&proxy);
1652         if (proxy!=NULL) {
1653                 route=linphone_proxy_config_get_route(proxy);
1654         }
1655         return route;
1656 }
1657
1658 bool_t linphone_core_is_in_communication_with(LinphoneCore *lc, const char *to)
1659 {
1660         char *tmp;
1661         bool_t returned;
1662         const LinphoneAddress *la=linphone_core_get_current_call_remote_address(lc);
1663         if(la == NULL)
1664         {
1665                 return FALSE;
1666         }
1667         tmp = linphone_address_as_string(la);
1668         if(!strcmp(tmp,to))
1669                 returned = TRUE;
1670         else
1671                 returned = FALSE;
1672         if(tmp)
1673                 ms_free(tmp);
1674         return returned;
1675 }
1676
1677 LinphoneProxyConfig * linphone_core_lookup_known_proxy(LinphoneCore *lc, const LinphoneAddress *uri){
1678         const MSList *elem;
1679         LinphoneProxyConfig *found_cfg=NULL;
1680         for (elem=linphone_core_get_proxy_config_list(lc);elem!=NULL;elem=elem->next){
1681                 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
1682                 const char *domain=linphone_proxy_config_get_domain(cfg);
1683                 if (domain!=NULL && strcmp(domain,linphone_address_get_domain(uri))==0){
1684                         found_cfg=cfg;
1685                         break;
1686                 }
1687         }
1688         return found_cfg;
1689 }
1690
1691 static char *get_fixed_contact(LinphoneCore *lc, LinphoneCall *call , LinphoneProxyConfig *dest_proxy){
1692         LinphoneAddress *ctt;
1693         const char *localip=call->localip;
1694
1695         /* first use user's supplied ip address if asked*/
1696         if (linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_NAT_ADDRESS){
1697                 ctt=linphone_core_get_primary_contact_parsed(lc);
1698                 return ms_strdup_printf("sip:%s@%s",linphone_address_get_username(ctt),
1699                         linphone_core_get_nat_address(lc));
1700         }
1701
1702         /* if already choosed, don't change it */
1703         if (call->op && sal_op_get_contact(call->op)!=NULL){
1704                 return NULL;
1705         }
1706         /* if the ping OPTIONS request succeeded use the contact guessed from the
1707          received, rport*/
1708         if (call->ping_op){
1709                 const char *guessed=sal_op_get_contact(call->ping_op);
1710                 if (guessed){
1711                         ms_message("Contact has been fixed using OPTIONS to %s",guessed);
1712                         return ms_strdup(guessed);
1713                 }
1714         }
1715
1716         /*if using a proxy, use the contact address as guessed with the REGISTERs*/
1717         if (dest_proxy && dest_proxy->op){
1718                 const char *fixed_contact=sal_op_get_contact(dest_proxy->op);
1719                 if (fixed_contact) {
1720                         ms_message("Contact has been fixed using proxy to %s",fixed_contact);
1721                         return ms_strdup(fixed_contact);
1722                 }
1723         }
1724         
1725         ctt=linphone_core_get_primary_contact_parsed(lc);
1726         
1727         if (ctt!=NULL){
1728                 char *ret;
1729                 /*otherwise use supllied localip*/
1730                 linphone_address_set_domain(ctt,localip);
1731                 linphone_address_set_port_int(ctt,linphone_core_get_sip_port(lc));
1732                 ret=linphone_address_as_string_uri_only(ctt);
1733                 linphone_address_destroy(ctt);
1734                 ms_message("Contact has been fixed using local ip to %s",ret);
1735                 return ret;
1736         }
1737         return NULL;
1738 }
1739
1740 int linphone_core_start_invite(LinphoneCore *lc, LinphoneCall *call, LinphoneProxyConfig *dest_proxy){
1741         int err;
1742         char *contact;
1743         char *real_url,*barmsg;
1744         char *from;
1745         /*try to be best-effort in giving real local or routable contact address */
1746         contact=get_fixed_contact(lc,call,dest_proxy);
1747         if (contact){
1748                 sal_op_set_contact(call->op, contact);
1749                 ms_free(contact);
1750         }
1751         call->state=LinphoneCallInit;
1752         linphone_core_init_media_streams(lc,call);
1753         if (!lc->sip_conf.sdp_200_ack){ 
1754                 call->media_pending=TRUE;
1755                 sal_call_set_local_media_description(call->op,call->localdesc);
1756         }
1757         real_url=linphone_address_as_string(call->log->to);
1758         from=linphone_address_as_string(call->log->from);
1759         err=sal_call(call->op,from,real_url);
1760
1761         if (lc->sip_conf.sdp_200_ack){
1762                 call->media_pending=TRUE;
1763                 sal_call_set_local_media_description(call->op,call->localdesc);
1764         }
1765         barmsg=ortp_strdup_printf("%s %s", _("Contacting"), real_url);
1766         lc->vtable.display_status(lc,barmsg);
1767         ms_free(barmsg);
1768         
1769         if (err<0){
1770                 ms_warning("Could not initiate call.");
1771                 lc->vtable.display_status(lc,_("could not call"));
1772                 if(call == linphone_core_get_current_call(lc))
1773                         linphone_core_stop_media_streams(lc,call);
1774                 linphone_call_unref(call);
1775         }else gstate_new_state(lc, GSTATE_CALL_OUT_INVITE, real_url);
1776         ms_free(real_url);
1777         ms_free(from);
1778         return err;
1779 }
1780
1781 /**
1782  * Initiates an outgoing call
1783  *
1784  * @ingroup call_control
1785  * @param lc the LinphoneCore object
1786  * @param url the destination of the call (sip address, or phone number).
1787 **/
1788 LinphoneCall * linphone_core_invite(LinphoneCore *lc, const char *url){
1789         LinphoneAddress *addr=linphone_core_interpret_url(lc,url);
1790         if (addr){
1791                 LinphoneCall *call;
1792                 call=linphone_core_invite_address(lc,addr);
1793                 linphone_address_destroy(addr);
1794                 return call;
1795         }
1796         return NULL;
1797 }
1798
1799 /**
1800  * Initiates an outgoing call given a destination LinphoneAddress
1801  *
1802  * @ingroup call_control
1803  * @param lc the LinphoneCore object
1804  * @param url the destination of the call (sip address).
1805 **/
1806 LinphoneCall * linphone_core_invite_address(LinphoneCore *lc, const LinphoneAddress *real_parsed_url)
1807 {
1808         int err=0;
1809         const char *route=NULL;
1810         const char *from=NULL;
1811         LinphoneProxyConfig *proxy=NULL;
1812         LinphoneAddress *parsed_url2=NULL;
1813         char *real_url=NULL;
1814         LinphoneProxyConfig *dest_proxy=NULL;
1815         LinphoneCall *call;
1816         
1817         if (linphone_core_in_call(lc)){
1818                 lc->vtable.display_warning(lc,_("Sorry, you have to pause or stop the current call first !"));
1819                 return NULL;
1820         }
1821         if(!linphone_core_can_we_add_call(lc)){
1822                 lc->vtable.display_warning(lc,_("Sorry, we have reached the maximum number of simultaneous calls"));
1823                 return NULL;
1824         }
1825         linphone_core_get_default_proxy(lc,&proxy);
1826         route=linphone_core_get_route(lc);
1827         
1828         real_url=linphone_address_as_string(real_parsed_url);
1829         dest_proxy=linphone_core_lookup_known_proxy(lc,real_parsed_url);
1830
1831         if (proxy!=dest_proxy && dest_proxy!=NULL) {
1832                 ms_message("Overriding default proxy setting for this call:");
1833                 ms_message("The used identity will be %s",linphone_proxy_config_get_identity(dest_proxy));
1834         }
1835
1836         if (dest_proxy!=NULL)
1837                 from=linphone_proxy_config_get_identity(dest_proxy);
1838         else if (proxy!=NULL)
1839                 from=linphone_proxy_config_get_identity(proxy);
1840
1841         /* if no proxy or no identity defined for this proxy, default to primary contact*/
1842         if (from==NULL) from=linphone_core_get_primary_contact(lc);
1843
1844         parsed_url2=linphone_address_new(from);
1845
1846         call=linphone_call_new_outgoing(lc,parsed_url2,linphone_address_clone(real_parsed_url));
1847         sal_op_set_route(call->op,route);
1848         
1849         if(linphone_core_add_call(lc,call)!= 0)
1850         {
1851                 ms_warning("we had a problem in adding the call into the invite ... weird\n");
1852                 linphone_call_unref(call);
1853                 return NULL;
1854         }
1855         linphone_core_set_as_current_call(lc,call);
1856         if (dest_proxy!=NULL || lc->sip_conf.ping_with_options==FALSE){
1857                 err=linphone_core_start_invite(lc,call,dest_proxy);
1858         }else{
1859                 /*defer the start of the call after the OPTIONS ping*/
1860                 call->state=LinphoneCallPreEstablishing;
1861                 call->ping_op=sal_op_new(lc->sal);
1862                 sal_ping(call->ping_op,from,real_url);
1863                 sal_op_set_user_pointer(call->ping_op,call);
1864                 call->start_time=time(NULL);
1865         }
1866         
1867         if (real_url!=NULL) ms_free(real_url);
1868         return call;
1869 }
1870
1871 int linphone_core_refer(LinphoneCore *lc, LinphoneCall *call, const char *url)
1872 {
1873         char *real_url=NULL;
1874         LinphoneAddress *real_parsed_url=linphone_core_interpret_url(lc,url);
1875
1876         if (!real_parsed_url){
1877                 /* bad url */
1878                 return -1;
1879         }
1880         if (call==NULL){
1881                 ms_warning("No established call to refer.");
1882                 return -1;
1883         }
1884         //lc->call=NULL; //Do not do that you will lose the call afterward . . .
1885         real_url=linphone_address_as_string (real_parsed_url);
1886         sal_refer(call->op,real_url);
1887         ms_free(real_url);
1888         return 0;
1889 }
1890
1891 /**
1892  * Returns true if an incoming call is pending, ie waiting for being answered or declined.
1893  *
1894  * @ingroup call_control
1895 **/
1896 bool_t linphone_core_inc_invite_pending(LinphoneCore*lc){
1897         LinphoneCall *call = linphone_core_get_current_call(lc);
1898         if(call != NULL)
1899         {
1900                 if(call->dir==LinphoneCallIncoming)
1901                         return TRUE;
1902         }
1903         return FALSE;
1904 }
1905
1906 void linphone_core_init_media_streams(LinphoneCore *lc, LinphoneCall *call){
1907         SalMediaDescription *md=call->localdesc;
1908         lc->audiostream=audio_stream_new(md->streams[0].port,linphone_core_ipv6_enabled(lc));
1909         if (linphone_core_echo_limiter_enabled(lc)){
1910                 const char *type=lp_config_get_string(lc->config,"sound","el_type","mic");
1911                 if (strcasecmp(type,"mic")==0)
1912                         audio_stream_enable_echo_limiter(lc->audiostream,ELControlMic);
1913                 else if (strcasecmp(type,"speaker")==0)
1914                         audio_stream_enable_echo_limiter(lc->audiostream,ELControlSpeaker);
1915         }
1916         audio_stream_enable_gain_control(lc->audiostream,TRUE);
1917         if (linphone_core_echo_cancellation_enabled(lc)){
1918                 int len,delay,framesize;
1919                 len=lp_config_get_int(lc->config,"sound","ec_tail_len",0);
1920                 delay=lp_config_get_int(lc->config,"sound","ec_delay",0);
1921                 framesize=lp_config_get_int(lc->config,"sound","ec_framesize",0);
1922                 audio_stream_set_echo_canceller_params(lc->audiostream,len,delay,framesize);
1923         }
1924         audio_stream_enable_automatic_gain_control(lc->audiostream,linphone_core_agc_enabled(lc));
1925         {
1926                 int enabled=lp_config_get_int(lc->config,"sound","noisegate",0);
1927                 audio_stream_enable_noise_gate(lc->audiostream,enabled);
1928         }
1929         if (lc->a_rtp)
1930                 rtp_session_set_transports(lc->audiostream->session,lc->a_rtp,lc->a_rtcp);
1931
1932 #ifdef VIDEO_ENABLED
1933         if ((lc->video_conf.display || lc->video_conf.capture) && md->streams[1].port>0)
1934                 lc->videostream=video_stream_new(md->streams[1].port,linphone_core_ipv6_enabled(lc));
1935 #else
1936         lc->videostream=NULL;
1937 #endif
1938 }
1939
1940 static int dtmf_tab[16]={'0','1','2','3','4','5','6','7','8','9','*','#','A','B','C','D'};
1941
1942 static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
1943         LinphoneCore* lc = (LinphoneCore*)user_data;
1944         if (dtmf<0 || dtmf>15){
1945                 ms_warning("Bad dtmf value %i",dtmf);
1946                 return;
1947         }
1948         if (lc->vtable.dtmf_received != NULL)
1949                 lc->vtable.dtmf_received(lc, dtmf_tab[dtmf]);
1950 }
1951
1952 static void parametrize_equalizer(LinphoneCore *lc, AudioStream *st){
1953         if (st->equalizer){
1954                 MSFilter *f=st->equalizer;
1955                 int enabled=lp_config_get_int(lc->config,"sound","eq_active",0);
1956                 const char *gains=lp_config_get_string(lc->config,"sound","eq_gains",NULL);
1957                 ms_filter_call_method(f,MS_EQUALIZER_SET_ACTIVE,&enabled);
1958                 if (enabled){
1959                         if (gains){
1960                                 do{
1961                                         int bytes;
1962                                         MSEqualizerGain g;
1963                                         if (sscanf(gains,"%f:%f:%f %n",&g.frequency,&g.gain,&g.width,&bytes)==3){
1964                                                 ms_message("Read equalizer gains: %f(~%f) --> %f",g.frequency,g.width,g.gain);
1965                                                 ms_filter_call_method(f,MS_EQUALIZER_SET_GAIN,&g);
1966                                                 gains+=bytes;
1967                                         }else break;
1968                                 }while(1);
1969                         }
1970                 }
1971         }
1972 }
1973
1974 static void post_configure_audio_streams(LinphoneCore *lc){
1975         AudioStream *st=lc->audiostream;
1976         float gain=lp_config_get_float(lc->config,"sound","mic_gain",-1);
1977         if (gain!=-1)
1978                 audio_stream_set_mic_gain(st,gain);
1979         float recv_gain = lc->sound_conf.soft_play_lev;
1980         if (recv_gain != 0) {
1981                 linphone_core_set_soft_play_level(lc,recv_gain);
1982         }
1983         if (linphone_core_echo_limiter_enabled(lc)){
1984                 float speed=lp_config_get_float(lc->config,"sound","el_speed",-1);
1985                 float thres=lp_config_get_float(lc->config,"sound","el_thres",-1);
1986                 float force=lp_config_get_float(lc->config,"sound","el_force",-1);
1987                 int sustain=lp_config_get_int(lc->config,"sound","el_sustain",-1);
1988                 MSFilter *f=NULL;
1989                 if (st->el_type==ELControlMic){
1990                         f=st->volsend;
1991                         if (speed==-1) speed=0.03;
1992                         if (force==-1) force=10;
1993                 }
1994                 else if (st->el_type==ELControlSpeaker){
1995                         f=st->volrecv;
1996                         if (speed==-1) speed=0.02;
1997                         if (force==-1) force=5;
1998                 }
1999                 if (speed!=-1)
2000                         ms_filter_call_method(f,MS_VOLUME_SET_EA_SPEED,&speed);
2001                 if (thres!=-1)
2002                         ms_filter_call_method(f,MS_VOLUME_SET_EA_THRESHOLD,&thres);
2003                 if (force!=-1)
2004                         ms_filter_call_method(f,MS_VOLUME_SET_EA_FORCE,&force);
2005                 if (sustain!=-1)
2006                         ms_filter_call_method(f,MS_VOLUME_SET_EA_SUSTAIN,&sustain);
2007
2008         }
2009         if (st->volsend){
2010                 float ng_thres=lp_config_get_float(lc->config,"sound","ng_thres",0.05);
2011                 float ng_floorgain=lp_config_get_float(lc->config,"sound","ng_floorgain",0);
2012                 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_THRESHOLD,&ng_thres);
2013                 ms_filter_call_method(st->volsend,MS_VOLUME_SET_NOISE_GATE_FLOORGAIN,&ng_floorgain);
2014         }
2015         parametrize_equalizer(lc,st);
2016         if (lc->vtable.dtmf_received!=NULL){
2017                 /* replace by our default action*/
2018                 audio_stream_play_received_dtmfs(lc->audiostream,FALSE);
2019                 rtp_session_signal_connect(lc->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
2020         }
2021 }
2022
2023 static RtpProfile *make_profile(LinphoneCore *lc, const SalMediaDescription *md, const SalStreamDescription *desc, int *used_pt){
2024         int bw;
2025         const MSList *elem;
2026         RtpProfile *prof=rtp_profile_new("Call profile");
2027         bool_t first=TRUE;
2028         int remote_bw=0;
2029         
2030         for(elem=desc->payloads;elem!=NULL;elem=elem->next){
2031                 PayloadType *pt=(PayloadType*)elem->data;
2032         
2033                 if (first) {
2034                         if (desc->type==SalAudio){
2035                                 linphone_core_update_allocated_audio_bandwidth_in_call(lc,pt);
2036                         }
2037                         *used_pt=payload_type_get_number(pt);
2038                         first=FALSE;
2039                 }
2040                 if (desc->bandwidth>0) remote_bw=desc->bandwidth;
2041                 else if (md->bandwidth>0) {
2042                         /*case where b=AS is given globally, not per stream*/
2043                         remote_bw=md->bandwidth;
2044                         if (desc->type==SalVideo){
2045                                 remote_bw-=lc->audio_bw;
2046                         }
2047                 }
2048                 
2049                 if (desc->type==SalAudio){                      
2050                                 bw=get_min_bandwidth(lc->up_audio_bw,remote_bw);
2051                 }else bw=get_min_bandwidth(lc->up_video_bw,remote_bw);
2052                 if (bw>0) pt->normal_bitrate=bw*1000;
2053                 else if (desc->type==SalAudio){
2054                         pt->normal_bitrate=-1;
2055                 }
2056                 if (desc->ptime>0){
2057                         char tmp[40];
2058                         snprintf(tmp,sizeof(tmp),"ptime=%i",desc->ptime);
2059                         payload_type_append_send_fmtp(pt,tmp);
2060                 }
2061                 rtp_profile_set_payload(prof,payload_type_get_number(pt),pt);
2062         }
2063         return prof;
2064 }
2065
2066 void linphone_core_start_media_streams(LinphoneCore *lc, LinphoneCall *call){
2067         LinphoneAddress *me=linphone_core_get_primary_contact_parsed(lc);
2068         const char *tool="linphone-" LINPHONE_VERSION;
2069         char *cname;
2070         int used_pt=-1;
2071         /* adjust rtp jitter compensation. It must be at least the latency of the sound card */
2072         int jitt_comp=MAX(lc->sound_conf.latency,lc->rtp_conf.audio_jitt_comp);
2073
2074         if (call->media_start_time==0) call->media_start_time=time(NULL);
2075
2076         cname=linphone_address_as_string_uri_only(me);
2077         {
2078                 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
2079                                                         SalProtoRtpAvp,SalAudio);
2080                 if (stream){
2081                         call->audio_profile=make_profile(lc,call->resultdesc,stream,&used_pt);
2082                         if (!lc->use_files){
2083                                 MSSndCard *playcard=lc->sound_conf.play_sndcard;
2084                                 MSSndCard *captcard=lc->sound_conf.capt_sndcard;
2085                                 if (playcard==NULL) {
2086                                         ms_warning("No card defined for playback !");
2087                                         goto end;
2088                                 }
2089                                 if (captcard==NULL) {
2090                                         ms_warning("No card defined for capture !");
2091                                         goto end;
2092                                 }
2093                                 audio_stream_start_now(
2094                                         lc->audiostream,
2095                                         call->audio_profile,
2096                                         stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
2097                                         stream->port,
2098                                         stream->port+1,
2099                                         used_pt,
2100                                         jitt_comp,
2101                                         playcard,
2102                                         captcard,
2103                                         linphone_core_echo_cancellation_enabled(lc));
2104                         }else{
2105                                 audio_stream_start_with_files(
2106                                         lc->audiostream,
2107                                         call->audio_profile,
2108                                         stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr,
2109                                         stream->port,
2110                                         stream->port+1,
2111                                         used_pt,
2112                                         100,
2113                                         lc->play_file,
2114                                         lc->rec_file);
2115                         }
2116                         post_configure_audio_streams(lc);
2117                         audio_stream_set_rtcp_information(lc->audiostream, cname, tool);
2118                 }else ms_warning("No audio stream defined ?");
2119         }
2120 #ifdef VIDEO_ENABLED
2121         {
2122                 const SalStreamDescription *stream=sal_media_description_find_stream(call->resultdesc,
2123                                                         SalProtoRtpAvp,SalVideo);
2124                 /* shutdown preview */
2125                 if (lc->previewstream!=NULL) {
2126                         video_preview_stop(lc->previewstream);
2127                         lc->previewstream=NULL;
2128                 }
2129                 if (stream && (lc->video_conf.display || lc->video_conf.capture)) {
2130                         const char *addr=stream->addr[0]!='\0' ? stream->addr : call->resultdesc->addr;
2131                         call->video_profile=make_profile(lc,call->resultdesc,stream,&used_pt);
2132                         video_stream_set_sent_video_size(lc->videostream,linphone_core_get_preferred_video_size(lc));
2133                         video_stream_enable_self_view(lc->videostream,lc->video_conf.selfview);
2134                         if (lc->video_conf.display && lc->video_conf.capture)
2135                                 video_stream_start(lc->videostream,
2136                                 call->video_profile, addr, stream->port,
2137                                 stream->port+1,
2138                                 used_pt, jitt_comp, lc->video_conf.device);
2139                         else if (lc->video_conf.display)
2140                                 video_stream_recv_only_start(lc->videostream,
2141                                 call->video_profile, addr, stream->port,
2142                                 used_pt, jitt_comp);
2143                         else if (lc->video_conf.capture)
2144                                 video_stream_send_only_start(lc->videostream,
2145                                 call->video_profile, addr, stream->port,
2146                                 stream->port+1,
2147                                 used_pt, jitt_comp, lc->video_conf.device);
2148                         video_stream_set_rtcp_information(lc->videostream, cname,tool);
2149                 }
2150         }
2151 #endif
2152         goto end;
2153         end:
2154                 ms_free(cname);
2155                 linphone_address_destroy(me);
2156                 call->state=LinphoneCallAVRunning;
2157 }
2158
2159 void linphone_core_stop_media_streams(LinphoneCore *lc, LinphoneCall *call){
2160         if (lc->audiostream!=NULL) {
2161                 audio_stream_stop(lc->audiostream);
2162                 lc->audiostream=NULL;
2163         }
2164 #ifdef VIDEO_ENABLED
2165         if (lc->videostream!=NULL){
2166                 if (lc->video_conf.display && lc->video_conf.capture)
2167                         video_stream_stop(lc->videostream);
2168                 else if (lc->video_conf.display)
2169                         video_stream_recv_only_stop(lc->videostream);
2170                 else if (lc->video_conf.capture)
2171                         video_stream_send_only_stop(lc->videostream);
2172                 lc->videostream=NULL;
2173         }
2174         if (linphone_core_video_preview_enabled(lc)){
2175                 if (lc->previewstream==NULL){
2176                         lc->previewstream=video_preview_start(lc->video_conf.device, lc->video_conf.vsize);
2177                 }
2178         }
2179 #endif
2180         if (call->audio_profile){
2181                 rtp_profile_clear_all(call->audio_profile);
2182                 rtp_profile_destroy(call->audio_profile);
2183                 call->audio_profile=NULL;
2184         }
2185         if (call->video_profile){
2186                 rtp_profile_clear_all(call->video_profile);
2187                 rtp_profile_destroy(call->video_profile);
2188                 call->video_profile=NULL;
2189         }
2190 }
2191
2192 /**
2193  * Accept an incoming call.
2194  *
2195  * @ingroup call_control
2196  * Basically the application is notified of incoming calls within the
2197  * invite_recv callback of the #LinphoneCoreVTable structure.
2198  * The application can later respond positively to the call using
2199  * this method.
2200  * @param lc the LinphoneCore object
2201  * @param url the SIP address of the originator of the call, or NULL.
2202  *            This argument is useful for managing multiple calls simulatenously,
2203  *            however this feature is not supported yet.
2204  *            Using NULL will accept the unique incoming call in progress.
2205 **/
2206 int linphone_core_accept_call(LinphoneCore *lc, LinphoneCall *call)
2207 {
2208         LinphoneProxyConfig *cfg=NULL;
2209         const char *contact=NULL;
2210         
2211         if (call==NULL){
2212                 //if just one call is present answer the only one ...
2213                 if(ms_list_size(linphone_core_get_calls(lc)) != 1)
2214                         return -1;
2215                 else
2216                         call = linphone_core_get_calls(lc)->data;
2217         }
2218
2219         if (call->state==LinphoneCallAVRunning){
2220                 /*call already accepted*/
2221                 return -1;
2222         }
2223
2224         /*stop ringing */
2225         if (lc->ringstream!=NULL) {
2226                 ms_message("stop ringing");
2227                 ring_stop(lc->ringstream);
2228                 ms_message("ring stopped");
2229                 lc->ringstream=NULL;
2230         }
2231         if(linphone_core_set_as_current_call(lc,call)!=0)
2232         {
2233                 ms_message("another call is already in process\n");
2234         }
2235         
2236         linphone_core_get_default_proxy(lc,&cfg);
2237         /*try to be best-effort in giving real local or routable contact address*/
2238         contact=get_fixed_contact(lc,call,cfg);
2239         if (contact)
2240                 sal_op_set_contact(call->op,contact);
2241         
2242         sal_call_accept(call->op);
2243         lc->vtable.display_status(lc,_("Connected."));
2244         gstate_new_state(lc, GSTATE_CALL_IN_CONNECTED, NULL);
2245         call->resultdesc=sal_call_get_final_media_description(call->op);
2246         if (call->resultdesc){
2247                 sal_media_description_ref(call->resultdesc);
2248                 if(call == linphone_core_get_current_call(lc))
2249                         linphone_core_start_media_streams(lc, call);
2250         }else call->media_pending=TRUE;
2251         ms_message("call answered.");
2252         return 0;
2253 }
2254
2255 /**
2256  * Terminates a call.
2257  *
2258  * @ingroup call_control
2259  * @param lc The LinphoneCore
2260  * @param url the destination of the call to be terminated, use NULL if there is
2261  *            only one call (which is case in this version of liblinphone).
2262 **/
2263 int linphone_core_terminate_call(LinphoneCore *lc, LinphoneCall *the_call)
2264 {
2265         LinphoneCall *call;
2266         if (the_call == NULL){
2267                 call = linphone_core_get_current_call(lc);
2268                 if(call == NULL)
2269                 {
2270                         return -1;
2271                 }
2272         }
2273         else
2274         {
2275                 call = the_call;
2276         }
2277         sal_call_terminate(call->op);
2278
2279         /*stop ringing*/
2280         if (lc->ringstream!=NULL) {
2281                 ring_stop(lc->ringstream);
2282                 lc->ringstream=NULL;
2283         }
2284         if(call == linphone_core_get_current_call(lc))
2285                 linphone_core_stop_media_streams(lc,call);
2286         lc->vtable.display_status(lc,_("Call ended") );
2287         gstate_new_state(lc, GSTATE_CALL_END, NULL);
2288         linphone_call_set_terminated(call);
2289         linphone_call_unref(call);
2290         return 0;
2291 }
2292
2293 /**
2294  * Terminates all the calls.
2295  *
2296  * @ingroup call_control
2297  * @param lc The LinphoneCore
2298 **/
2299 int linphone_core_terminate_all_calls(LinphoneCore *lc){
2300         MSList *calls;
2301
2302         calls = lc->calls;
2303         while(calls->next != NULL)
2304         {
2305                 linphone_core_terminate_call(lc,(LinphoneCall *)calls->data);
2306                 calls = calls->next;
2307         }
2308         ms_list_free(lc->calls);
2309         return -1;
2310 }
2311
2312 /**
2313  * Returns the calls MSList
2314  *
2315  * @ingroup call_control
2316 **/
2317 MSList *linphone_core_get_calls(LinphoneCore *lc)
2318 {
2319         return ms_list_copy(lc->calls);
2320 }
2321
2322 /**
2323  * Returns TRUE if there is a call running or pending.
2324  *
2325  * @ingroup call_control
2326 **/
2327 bool_t linphone_core_in_call(const LinphoneCore *lc){
2328         return linphone_core_get_current_call((LinphoneCore *)lc)!=NULL;
2329 }
2330
2331 /**
2332  * Returns The _LinphoneCall struct of the current call if one is in call
2333  *
2334  * @ingroup call_control
2335 **/
2336 LinphoneCall *linphone_core_get_current_call(LinphoneCore *lc)
2337 {
2338         if(lc->current_call != NULL)
2339                 return lc->current_call;
2340         return NULL;
2341 }
2342
2343 /**
2344  * Permits to pause the call
2345  *
2346  * @ingroup call_control
2347 **/
2348 int linphone_core_pause_call(LinphoneCore *lc, LinphoneCall *the_call)
2349 {
2350         LinphoneCall *call = the_call;
2351         if(lc == NULL)
2352         {
2353                 ms_error("LinphoneCore was null\n");
2354                 return -1;
2355         }
2356         if(call == NULL)
2357         {
2358                 if(linphone_core_in_call(lc))
2359                 {
2360                         call = linphone_core_get_current_call(lc);
2361                 }
2362                 else
2363                 {
2364                         ms_error("LinphoneCall was null\n");
2365                         return -2;
2366                 }
2367         }
2368         if(linphone_core_get_current_call(lc) != call)
2369         {
2370                 ms_error("The call asked to be paused was not the current on\n");
2371                 return -3;
2372         }
2373         sal_call_hold(call->op,TRUE);
2374         call->state = LinphoneCallPaused;
2375         linphone_core_unset_the_current_call(lc);
2376         linphone_core_stop_media_streams(lc,call);
2377         lc->vtable.display_status(lc,_("Pause the current call"));
2378         return 0;
2379 }
2380
2381 /**
2382  * Permits to resume the call
2383  *
2384  * @ingroup call_control
2385 **/
2386 int linphone_core_resume_call(LinphoneCore *lc, LinphoneCall *the_call)
2387 {
2388         char temp[255];
2389         LinphoneCall *call = the_call;
2390         if(lc == NULL)
2391         {
2392                 ms_error("LinphoneCore was null\n");
2393                 return -1;
2394         }
2395         if(call == NULL)
2396         {
2397                 MSList *calls = linphone_core_get_calls(lc);
2398                 if(ms_list_size(calls) == 1)
2399                 {
2400                         call = ((LinphoneCall *)calls->data);
2401                         ms_list_free(calls);
2402                 }
2403                 else
2404                 {
2405                         ms_error("LinphoneCall was null\n");
2406                         ms_list_free(calls);
2407                         return -2;
2408                 }
2409         }
2410         if(linphone_core_get_current_call(lc) != NULL)
2411         {
2412                 ms_error("There is already a call in process pause or stop it first\n");
2413                 return -3;
2414         }
2415         linphone_core_init_media_streams(lc,call);
2416         sal_call_hold(call->op,FALSE);
2417         call->state = LinphoneCallAVRunning;
2418         linphone_core_set_as_current_call(lc,call);
2419         linphone_core_start_media_streams(lc,call);
2420         snprintf(temp,sizeof(temp),"Resume the call with %s",linphone_call_get_remote_address_as_string(call));
2421         lc->vtable.display_status(lc,temp);
2422         
2423         return 0;
2424 }
2425
2426 /**
2427  * Compare the remote address with the one in call
2428  * 
2429  * @param a the call
2430  * @param b the remote address to compare with
2431  * @return 0 if it's the good call else 1
2432  */
2433 static int linphone_call_remote_address_compare(const void * a, const void * b)
2434 {
2435         if(b == NULL || a ==NULL)
2436                 return 1;
2437         char *the_remote_address = ((char *)b);
2438         LinphoneCall *call = ((LinphoneCall *)a);
2439 #ifdef DEBUG 
2440         ms_message("the remote address:%s\n",the_remote_address);
2441         ms_message("the call:%p => %s\n",call,linphone_call_get_remote_address_as_string(call));
2442 #endif
2443         if(!strcmp(linphone_call_get_remote_address_as_string(call),the_remote_address))
2444         {
2445                 return 0;
2446         }
2447         return 1;
2448 }
2449
2450 /**
2451  * Get the call with the remote_address specified
2452  * @param lc
2453  * @param remote_address
2454  * @return the LinphoneCall of the call if found
2455  */
2456 LinphoneCall *linphone_core_get_call_by_remote_address(LinphoneCore *lc, const char *remote_address){
2457
2458         MSList *the_call = ms_list_find_custom(lc->calls,linphone_call_remote_address_compare,(void *)remote_address);
2459         if(the_call != NULL)
2460         {
2461                 return ((LinphoneCall *)the_call->data);
2462         }
2463         return NULL;
2464 }
2465
2466 int linphone_core_send_publish(LinphoneCore *lc,
2467                                LinphoneOnlineStatus presence_mode)
2468 {
2469         const MSList *elem;
2470         for (elem=linphone_core_get_proxy_config_list(lc);elem!=NULL;elem=ms_list_next(elem)){
2471                 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
2472                 if (cfg->publish) linphone_proxy_config_send_publish(cfg,presence_mode);
2473         }
2474         return 0;
2475 }
2476
2477 /**
2478  * Set the incoming call timeout in seconds.
2479  *
2480  * @ingroup call_control
2481  * If an incoming call isn't answered for this timeout period, it is 
2482  * automatically declined.
2483 **/
2484 void linphone_core_set_inc_timeout(LinphoneCore *lc, int seconds){
2485         lc->sip_conf.inc_timeout=seconds;
2486 }
2487
2488 /**
2489  * Returns the incoming call timeout
2490  *
2491  * @ingroup call_control
2492  * See linphone_core_set_inc_timeout() for details.
2493 **/
2494 int linphone_core_get_inc_timeout(LinphoneCore *lc){
2495         return lc->sip_conf.inc_timeout;
2496 }
2497
2498 void linphone_core_set_presence_info(LinphoneCore *lc,int minutes_away,
2499                                                                                                         const char *contact,
2500                                                                                                         LinphoneOnlineStatus presence_mode)
2501 {
2502         if (minutes_away>0) lc->minutes_away=minutes_away;
2503         
2504         if (lc->alt_contact!=NULL) {
2505                 ms_free(lc->alt_contact);
2506                 lc->alt_contact=NULL;
2507         }
2508         if (contact) lc->alt_contact=ms_strdup(contact);
2509         if (lc->presence_mode!=presence_mode){
2510                 linphone_core_notify_all_friends(lc,presence_mode);
2511                 /*
2512                    Improve the use of all LINPHONE_STATUS available.
2513                    !TODO Do not mix "presence status" with "answer status code"..
2514                    Use correct parameter to follow sip_if_match/sip_etag.
2515                  */
2516                 linphone_core_send_publish(lc,presence_mode);
2517         }
2518         lc->prev_mode=lc->presence_mode;
2519         lc->presence_mode=presence_mode;
2520 }
2521
2522 LinphoneOnlineStatus linphone_core_get_presence_info(const LinphoneCore *lc){
2523         return lc->presence_mode;
2524 }
2525
2526 /**
2527  * Get playback sound level in 0-100 scale.
2528  *
2529  * @ingroup media_parameters
2530 **/
2531 int linphone_core_get_play_level(LinphoneCore *lc)
2532 {
2533         return lc->sound_conf.play_lev;
2534 }
2535
2536 /**
2537  * Get ring sound level in 0-100 scale
2538  *
2539  * @ingroup media_parameters
2540 **/
2541 int linphone_core_get_ring_level(LinphoneCore *lc)
2542 {
2543         return lc->sound_conf.ring_lev;
2544 }
2545
2546 /**
2547  * Get sound capture level in 0-100 scale
2548  *
2549  * @ingroup media_parameters
2550 **/
2551 int linphone_core_get_rec_level(LinphoneCore *lc){
2552         return lc->sound_conf.rec_lev;
2553 }
2554
2555 /**
2556  * Set sound ring level in 0-100 scale
2557  *
2558  * @ingroup media_parameters
2559 **/
2560 void linphone_core_set_ring_level(LinphoneCore *lc, int level){
2561         MSSndCard *sndcard;
2562         lc->sound_conf.ring_lev=level;
2563         sndcard=lc->sound_conf.ring_sndcard;
2564         if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_PLAYBACK,level);
2565 }
2566
2567
2568 void linphone_core_set_soft_play_level(LinphoneCore *lc, float level){
2569         float gain=level;
2570         lc->sound_conf.soft_play_lev=level;
2571         AudioStream *st=lc->audiostream;
2572         if (!st) return; /*just return*/
2573
2574         if (st->volrecv){
2575                 ms_filter_call_method(st->volrecv,MS_VOLUME_SET_DB_GAIN,&gain);
2576         }else ms_warning("Could not apply gain: gain control wasn't activated.");
2577 }
2578 float linphone_core_get_soft_play_level(LinphoneCore *lc) {
2579         float gain=0;
2580         AudioStream *st=lc->audiostream;
2581         if (st->volrecv){
2582                 ms_filter_call_method(st->volrecv,MS_VOLUME_GET,&gain);
2583         }else ms_warning("Could not get gain: gain control wasn't activated.");
2584
2585         return gain;
2586 }
2587
2588 /**
2589  * Set sound playback level in 0-100 scale
2590  *
2591  * @ingroup media_parameters
2592 **/
2593 void linphone_core_set_play_level(LinphoneCore *lc, int level){
2594         MSSndCard *sndcard;
2595         lc->sound_conf.play_lev=level;
2596         sndcard=lc->sound_conf.play_sndcard;
2597         if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_PLAYBACK,level);
2598 }
2599
2600 /**
2601  * Set sound capture level in 0-100 scale
2602  *
2603  * @ingroup media_parameters
2604 **/
2605 void linphone_core_set_rec_level(LinphoneCore *lc, int level)
2606 {
2607         MSSndCard *sndcard;
2608         lc->sound_conf.rec_lev=level;
2609         sndcard=lc->sound_conf.capt_sndcard;
2610         if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_CAPTURE,level);
2611 }
2612
2613 static MSSndCard *get_card_from_string_id(const char *devid, unsigned int cap){
2614         MSSndCard *sndcard=NULL;
2615         if (devid!=NULL){
2616                 sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
2617                 if (sndcard!=NULL &&
2618                         (ms_snd_card_get_capabilities(sndcard) & cap)==0 ){
2619                         ms_warning("%s card does not have the %s capability, ignoring.",
2620                                 devid,
2621                                 cap==MS_SND_CARD_CAP_CAPTURE ? "capture" : "playback");
2622                         sndcard=NULL;
2623                 }
2624         }
2625         if (sndcard==NULL) {
2626                 /* get a card that has read+write capabilities */
2627                 sndcard=ms_snd_card_manager_get_default_card(ms_snd_card_manager_get());
2628                 /* otherwise refine to the first card having the right capability*/
2629                 if (sndcard==NULL){
2630                         const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
2631                         for(;elem!=NULL;elem=elem->next){
2632                                 sndcard=(MSSndCard*)elem->data;
2633                                 if (ms_snd_card_get_capabilities(sndcard) & cap) break;
2634                         }
2635                 }
2636                 if (sndcard==NULL){/*looks like a bug! take the first one !*/
2637                         const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
2638                         if (elem) sndcard=(MSSndCard*)elem->data;
2639         }
2640         }
2641         if (sndcard==NULL) ms_error("Could not find a suitable soundcard !");
2642         return sndcard;
2643 }
2644
2645 /**
2646  * Returns true if the specified sound device can capture sound.
2647  *
2648  * @ingroup media_parameters
2649  * @param devid the device name as returned by linphone_core_get_sound_devices()
2650 **/
2651 bool_t linphone_core_sound_device_can_capture(LinphoneCore *lc, const char *devid){
2652         MSSndCard *sndcard;
2653         sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
2654         if (sndcard!=NULL && (ms_snd_card_get_capabilities(sndcard) & MS_SND_CARD_CAP_CAPTURE)) return TRUE;
2655         return FALSE;
2656 }
2657
2658 /**
2659  * Returns true if the specified sound device can play sound.
2660  *
2661  * @ingroup media_parameters
2662  * @param devid the device name as returned by linphone_core_get_sound_devices()
2663 **/
2664 bool_t linphone_core_sound_device_can_playback(LinphoneCore *lc, const char *devid){
2665         MSSndCard *sndcard;
2666         sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
2667         if (sndcard!=NULL && (ms_snd_card_get_capabilities(sndcard) & MS_SND_CARD_CAP_PLAYBACK)) return TRUE;
2668         return FALSE;
2669 }
2670
2671 /**
2672  * Sets the sound device used for ringing.
2673  *
2674  * @ingroup media_parameters
2675  * @param devid the device name as returned by linphone_core_get_sound_devices()
2676 **/
2677 int linphone_core_set_ringer_device(LinphoneCore *lc, const char * devid){
2678         MSSndCard *card=get_card_from_string_id(devid,MS_SND_CARD_CAP_PLAYBACK);
2679         lc->sound_conf.ring_sndcard=card;
2680         if (card && lc->ready)
2681                 lp_config_set_string(lc->config,"sound","ringer_dev_id",ms_snd_card_get_string_id(card));
2682         return 0;
2683 }
2684
2685 /**
2686  * Sets the sound device used for playback.
2687  *
2688  * @ingroup media_parameters
2689  * @param devid the device name as returned by linphone_core_get_sound_devices()
2690 **/
2691 int linphone_core_set_playback_device(LinphoneCore *lc, const char * devid){
2692         MSSndCard *card=get_card_from_string_id(devid,MS_SND_CARD_CAP_PLAYBACK);
2693         lc->sound_conf.play_sndcard=card;
2694         if (card && lc->ready)
2695                 lp_config_set_string(lc->config,"sound","playback_dev_id",ms_snd_card_get_string_id(card));
2696         return 0;
2697 }
2698
2699 /**
2700  * Sets the sound device used for capture.
2701  *
2702  * @ingroup media_parameters
2703  * @param devid the device name as returned by linphone_core_get_sound_devices()
2704 **/
2705 int linphone_core_set_capture_device(LinphoneCore *lc, const char * devid){
2706         MSSndCard *card=get_card_from_string_id(devid,MS_SND_CARD_CAP_CAPTURE);
2707         lc->sound_conf.capt_sndcard=card;
2708         if (card && lc->ready)
2709                 lp_config_set_string(lc->config,"sound","capture_dev_id",ms_snd_card_get_string_id(card));
2710         return 0;
2711 }
2712
2713 /**
2714  * Returns the name of the currently assigned sound device for ringing.
2715  *
2716  * @ingroup media_parameters
2717 **/
2718 const char * linphone_core_get_ringer_device(LinphoneCore *lc)
2719 {
2720         if (lc->sound_conf.ring_sndcard) return ms_snd_card_get_string_id(lc->sound_conf.ring_sndcard);
2721         return NULL;
2722 }
2723
2724 /**
2725  * Returns the name of the currently assigned sound device for playback.
2726  *
2727  * @ingroup media_parameters
2728 **/
2729 const char * linphone_core_get_playback_device(LinphoneCore *lc)
2730 {
2731         return lc->sound_conf.play_sndcard ? ms_snd_card_get_string_id(lc->sound_conf.play_sndcard) : NULL;
2732 }
2733
2734 /**
2735  * Returns the name of the currently assigned sound device for capture.
2736  *
2737  * @ingroup media_parameters
2738 **/
2739 const char * linphone_core_get_capture_device(LinphoneCore *lc)
2740 {
2741         return lc->sound_conf.capt_sndcard ? ms_snd_card_get_string_id(lc->sound_conf.capt_sndcard) : NULL;
2742 }
2743
2744 /**
2745  * Returns an unmodifiable array of available sound devices.
2746  *
2747  * @ingroup media_parameters
2748  * The array is NULL terminated.
2749 **/
2750 const char**  linphone_core_get_sound_devices(LinphoneCore *lc){
2751         build_sound_devices_table(lc);
2752         return lc->sound_conf.cards;
2753 }
2754
2755 /**
2756  * Returns an unmodifiable array of available video capture devices.
2757  *
2758  * @ingroup media_parameters
2759  * The array is NULL terminated.
2760 **/
2761 const char**  linphone_core_get_video_devices(const LinphoneCore *lc){
2762         return lc->video_conf.cams;
2763 }
2764
2765 char linphone_core_get_sound_source(LinphoneCore *lc)
2766 {
2767         return lc->sound_conf.source;
2768 }
2769
2770 void linphone_core_set_sound_source(LinphoneCore *lc, char source)
2771 {
2772         MSSndCard *sndcard=lc->sound_conf.capt_sndcard;
2773         lc->sound_conf.source=source;
2774         if (!sndcard) return;
2775         switch(source){
2776                 case 'm':
2777                         ms_snd_card_set_capture(sndcard,MS_SND_CARD_MIC);
2778                         break;
2779                 case 'l':
2780                         ms_snd_card_set_capture(sndcard,MS_SND_CARD_LINE);
2781                         break;
2782         }
2783
2784 }
2785
2786
2787 /**
2788  * Sets the path to a wav file used for ringing.
2789  *
2790  * The file must be a wav 16bit linear.
2791  *
2792  * @ingroup media_parameters
2793 **/
2794 void linphone_core_set_ring(LinphoneCore *lc,const char *path){
2795         if (lc->sound_conf.local_ring!=0){
2796                 ms_free(lc->sound_conf.local_ring);
2797         }
2798         lc->sound_conf.local_ring=ms_strdup(path);
2799         if (lc->ready && lc->sound_conf.local_ring)
2800                 lp_config_set_string(lc->config,"sound","local_ring",lc->sound_conf.local_ring);
2801 }
2802
2803 /**
2804  * Returns the path to the wav file used for ringing.
2805  *
2806  * @ingroup media_parameters
2807 **/
2808 const char *linphone_core_get_ring(const LinphoneCore *lc){
2809         return lc->sound_conf.local_ring;
2810 }
2811
2812 static void notify_end_of_ring(void *ud ,unsigned int event, void * arg){
2813         LinphoneCore *lc=(LinphoneCore*)ud;
2814         lc->preview_finished=1;
2815 }
2816
2817 int linphone_core_preview_ring(LinphoneCore *lc, const char *ring,LinphoneCoreCbFunc func,void * userdata)
2818 {
2819         if (lc->ringstream!=0){
2820                 ms_warning("Cannot start ring now,there's already a ring being played");
2821                 return -1;
2822         }
2823         lc_callback_obj_init(&lc->preview_finished_cb,func,userdata);
2824         lc->preview_finished=0;
2825         if (lc->sound_conf.ring_sndcard!=NULL){
2826                 lc->ringstream=ring_start_with_cb(ring,2000,lc->sound_conf.ring_sndcard,notify_end_of_ring,(void *)lc);
2827         }
2828         return 0;
2829 }
2830
2831 /**
2832  * Sets the path to a wav file used for ringing back.
2833  *
2834  * Ringback means the ring that is heard when it's ringing at the remote party.
2835  * The file must be a wav 16bit linear.
2836  *
2837  * @ingroup media_parameters
2838 **/
2839 void linphone_core_set_ringback(LinphoneCore *lc, const char *path){
2840         if (lc->sound_conf.remote_ring!=0){
2841                 ms_free(lc->sound_conf.remote_ring);
2842         }
2843         lc->sound_conf.remote_ring=ms_strdup(path);
2844 }
2845
2846 /**
2847  * Returns the path to the wav file used for ringing back.
2848  *
2849  * @ingroup media_parameters
2850 **/
2851 const char * linphone_core_get_ringback(const LinphoneCore *lc){
2852         return lc->sound_conf.remote_ring;
2853 }
2854
2855 /**
2856  * Enables or disable echo cancellation.
2857  *
2858  * @ingroup media_parameters
2859 **/
2860 void linphone_core_enable_echo_cancellation(LinphoneCore *lc, bool_t val){
2861         lc->sound_conf.ec=val;
2862         if (lc->ready)
2863                 lp_config_set_int(lc->config,"sound","echocancellation",val);
2864 }
2865
2866 /**
2867  * Returns TRUE if echo cancellation is enabled.
2868  *
2869  * @ingroup media_parameters
2870 **/
2871 bool_t linphone_core_echo_cancellation_enabled(LinphoneCore *lc){
2872         return lc->sound_conf.ec;
2873 }
2874
2875 void linphone_core_enable_echo_limiter(LinphoneCore *lc, bool_t val){
2876         lc->sound_conf.ea=val;
2877 }
2878
2879 bool_t linphone_core_echo_limiter_enabled(const LinphoneCore *lc){
2880         return lc->sound_conf.ea;
2881 }
2882
2883 /**
2884  * Mutes or unmutes the local microphone.
2885  *
2886  * @ingroup media_parameters
2887 **/
2888 void linphone_core_mute_mic(LinphoneCore *lc, bool_t val){
2889         if (lc->audiostream!=NULL){
2890                  audio_stream_set_mic_gain(lc->audiostream,
2891                         (val==TRUE) ? 0 : 1.0);
2892         }
2893 }
2894
2895 bool_t linphone_core_is_mic_muted(LinphoneCore *lc) {
2896         float gain=1.0;
2897         if (lc->audiostream && lc->audiostream->volsend){
2898                         ms_filter_call_method(lc->audiostream->volsend,MS_VOLUME_GET_GAIN,&gain);
2899         }else ms_warning("Could not get gain: gain control wasn't activated. ");
2900
2901         return gain==0;
2902 }
2903
2904 void linphone_core_enable_agc(LinphoneCore *lc, bool_t val){
2905         lc->sound_conf.agc=val;
2906 }
2907
2908 bool_t linphone_core_agc_enabled(const LinphoneCore *lc){
2909         return lc->sound_conf.agc;
2910 }
2911
2912 /**
2913  * Send the specified dtmf.
2914  *
2915  * @ingroup media_parameters
2916  * This function only works during calls. The dtmf is automatically played to the user.
2917  * @param lc The LinphoneCore object
2918  * @param dtmf The dtmf name specified as a char, such as '0', '#' etc...
2919  *
2920 **/
2921 void linphone_core_send_dtmf(LinphoneCore *lc, char dtmf)
2922 {
2923         /*By default we send DTMF RFC2833 if we do not have enabled SIP_INFO but we can also send RFC2833 and SIP_INFO*/
2924         if (linphone_core_get_use_rfc2833_for_dtmf(lc)!=0 || linphone_core_get_use_info_for_dtmf(lc)==0)
2925         {
2926                 /* In Band DTMF */
2927                 if (lc->audiostream!=NULL){
2928                         audio_stream_send_dtmf(lc->audiostream,dtmf);
2929                 }
2930                 else
2931                 {
2932                         ms_error("we cannot send RFC2833 dtmf when we are not in communication");
2933                 }
2934         }
2935         if (linphone_core_get_use_info_for_dtmf(lc)!=0){
2936                 /* Out of Band DTMF (use INFO method) */
2937                 LinphoneCall *call=linphone_core_get_current_call(lc);
2938                 if (call==NULL){
2939                         return;
2940                 }
2941                 sal_call_send_dtmf(call->op,dtmf);
2942         }
2943 }
2944
2945 void linphone_core_set_stun_server(LinphoneCore *lc, const char *server){
2946         if (lc->net_conf.stun_server!=NULL)
2947                 ms_free(lc->net_conf.stun_server);
2948         if (server)
2949                 lc->net_conf.stun_server=ms_strdup(server);
2950         else lc->net_conf.stun_server=NULL;
2951 }
2952
2953 const char * linphone_core_get_stun_server(const LinphoneCore *lc){
2954         return lc->net_conf.stun_server;
2955 }
2956
2957 const char * linphone_core_get_relay_addr(const LinphoneCore *lc){
2958         return lc->net_conf.relay;
2959 }
2960
2961 int linphone_core_set_relay_addr(LinphoneCore *lc, const char *addr){
2962         if (lc->net_conf.relay!=NULL){
2963                 ms_free(lc->net_conf.relay);
2964                 lc->net_conf.relay=NULL;
2965         }
2966         if (addr){
2967                 lc->net_conf.relay=ms_strdup(addr);
2968         }
2969         return 0;
2970 }
2971
2972 void linphone_core_set_nat_address(LinphoneCore *lc, const char *addr)
2973 {
2974         if (lc->net_conf.nat_address!=NULL){
2975                 ms_free(lc->net_conf.nat_address);
2976         }
2977         if (addr!=NULL) lc->net_conf.nat_address=ms_strdup(addr);
2978         else lc->net_conf.nat_address=NULL;
2979         if (lc->sip_conf.contact) update_primary_contact(lc);
2980 }
2981
2982 const char *linphone_core_get_nat_address(const LinphoneCore *lc)
2983 {
2984         return lc->net_conf.nat_address;
2985 }
2986
2987 void linphone_core_set_firewall_policy(LinphoneCore *lc, LinphoneFirewallPolicy pol){
2988         lc->net_conf.firewall_policy=pol;
2989         if (lc->sip_conf.contact) update_primary_contact(lc);
2990 }
2991
2992 LinphoneFirewallPolicy linphone_core_get_firewall_policy(const LinphoneCore *lc){
2993         return lc->net_conf.firewall_policy;
2994 }
2995
2996 /**
2997  * Get the list of call logs (past calls).
2998  *
2999  * @ingroup call_logs
3000 **/
3001 const MSList * linphone_core_get_call_logs(LinphoneCore *lc){
3002         lc->missed_calls=0;
3003         return lc->call_logs;
3004 }
3005
3006 /**
3007  * Erase the call log.
3008  *
3009  * @ingroup call_logs
3010 **/
3011 void linphone_core_clear_call_logs(LinphoneCore *lc){
3012         lc->missed_calls=0;
3013         ms_list_for_each(lc->call_logs,(void (*)(void*))linphone_call_log_destroy);
3014         lc->call_logs=ms_list_free(lc->call_logs);
3015         call_logs_write_to_config_file(lc);
3016 }
3017
3018 static void toggle_video_preview(LinphoneCore *lc, bool_t val){
3019 #ifdef VIDEO_ENABLED
3020         if (lc->videostream==NULL){
3021                 if (val){
3022                         if (lc->previewstream==NULL){
3023                                 lc->previewstream=video_preview_start(lc->video_conf.device,
3024                                                         lc->video_conf.vsize);
3025                         }
3026                 }else{
3027                         if (lc->previewstream!=NULL){
3028                                 video_preview_stop(lc->previewstream);
3029                                 lc->previewstream=NULL;
3030                         }
3031                 }
3032         }
3033 #endif
3034 }
3035
3036 /**
3037  * Enables video globally.
3038  *
3039  * @ingroup media_parameters
3040  * This function does not have any effect during calls. It just indicates LinphoneCore to
3041  * initiate future calls with video or not. The two boolean parameters indicate in which
3042  * direction video is enabled. Setting both to false disables video entirely.
3043  *
3044  * @param vcap_enabled indicates whether video capture is enabled
3045  * @param display_enabled indicates whether video display should be shown
3046  *
3047 **/
3048 void linphone_core_enable_video(LinphoneCore *lc, bool_t vcap_enabled, bool_t display_enabled){
3049 #ifndef VIDEO_ENABLED
3050         if (vcap_enabled || display_enabled)
3051                 ms_warning("This version of linphone was built without video support.");
3052 #endif
3053         lc->video_conf.capture=vcap_enabled;
3054         lc->video_conf.display=display_enabled;
3055
3056         /* need to re-apply network bandwidth settings*/
3057         linphone_core_set_download_bandwidth(lc,
3058                 linphone_core_get_download_bandwidth(lc));
3059         linphone_core_set_upload_bandwidth(lc,
3060                 linphone_core_get_upload_bandwidth(lc));
3061 }
3062
3063 /**
3064  * Returns TRUE if video is enabled, FALSE otherwise.
3065  * @ingroup media_parameters
3066 **/
3067 bool_t linphone_core_video_enabled(LinphoneCore *lc){
3068         return (lc->video_conf.display || lc->video_conf.capture);
3069 }
3070
3071 /**
3072  * Controls video preview enablement.
3073  *
3074  * @ingroup media_parameters
3075  * Video preview refers to the action of displaying the local webcam image
3076  * to the user while not in call.
3077 **/
3078 void linphone_core_enable_video_preview(LinphoneCore *lc, bool_t val){
3079         lc->video_conf.show_local=val;
3080 }
3081
3082 /**
3083  * Returns TRUE if video previewing is enabled.
3084  * @ingroup media_parameters
3085 **/
3086 bool_t linphone_core_video_preview_enabled(const LinphoneCore *lc){
3087         return lc->video_conf.show_local;
3088 }
3089
3090 /**
3091  * Enables or disable self view during calls.
3092  *
3093  * @ingroup media_parameters
3094  * Self-view refers to having local webcam image inserted in corner
3095  * of the video window during calls.
3096  * This function works at any time, including during calls.
3097 **/
3098 void linphone_core_enable_self_view(LinphoneCore *lc, bool_t val){
3099         lc->video_conf.selfview=val;
3100 #ifdef VIDEO_ENABLED
3101         if (lc->videostream){
3102                 video_stream_enable_self_view(lc->videostream,val);
3103         }
3104 #endif
3105 }
3106
3107 /**
3108  * Returns TRUE if self-view is enabled, FALSE otherwise.
3109  *
3110  * @ingroup media_parameters
3111  *
3112  * Refer to linphone_core_enable_self_view() for details.
3113 **/
3114 bool_t linphone_core_self_view_enabled(const LinphoneCore *lc){
3115         return lc->video_conf.selfview;
3116 }
3117
3118 /**
3119  * Sets the active video device.
3120  *
3121  * @ingroup media_parameters
3122  * @param id the name of the video device as returned by linphone_core_get_video_devices()
3123 **/
3124 int linphone_core_set_video_device(LinphoneCore *lc, const char *id){
3125         MSWebCam *olddev=lc->video_conf.device;
3126         const char *vd;
3127         if (id!=NULL){
3128                 lc->video_conf.device=ms_web_cam_manager_get_cam(ms_web_cam_manager_get(),id);
3129                 if (lc->video_conf.device==NULL){
3130                         ms_warning("Could not found video device %s",id);
3131                 }
3132         }
3133         if (lc->video_conf.device==NULL)
3134                 lc->video_conf.device=ms_web_cam_manager_get_default_cam(ms_web_cam_manager_get());
3135         if (olddev!=NULL && olddev!=lc->video_conf.device){
3136                 toggle_video_preview(lc,FALSE);/*restart the video local preview*/
3137         }
3138         if (lc->ready && lc->video_conf.device){
3139                 vd=ms_web_cam_get_string_id(lc->video_conf.device);
3140                 if (vd && strstr(vd,"Static picture")!=NULL){
3141                         vd=NULL;
3142                 }
3143                 lp_config_set_string(lc->config,"video","device",vd);
3144         }
3145         return 0;
3146 }
3147
3148 /**
3149  * Returns the name of the currently active video device.
3150  *
3151  * @ingroup media_parameters
3152 **/
3153 const char *linphone_core_get_video_device(const LinphoneCore *lc){
3154         if (lc->video_conf.device) return ms_web_cam_get_string_id(lc->video_conf.device);
3155         return NULL;
3156 }
3157
3158 int linphone_core_set_static_picture(LinphoneCore *lc, const char *path) {
3159 #ifdef VIDEO_ENABLED
3160         VideoStream *vs = NULL;
3161         /* Select the video stream from the call in the first place */
3162         if (lc && lc->videostream) {
3163                 vs = lc->videostream;
3164         }
3165         /* If not in call, select the video stream from the preview */
3166         if (vs == NULL && lc && lc->previewstream) {
3167                 vs = lc->previewstream;
3168         }
3169         
3170         /* If we have a video stream (either preview, either from call), we
3171                  have a source and it is using the static picture filter, then
3172                  force the filter to use that picture. */
3173         if (vs && vs->source) {
3174                 if (ms_filter_get_id(vs->source) == MS_STATIC_IMAGE_ID) {
3175                         ms_filter_call_method(vs->source, MS_FILTER_SET_IMAGE,
3176                                                                                                                 (void *)path);
3177                 }
3178         }
3179
3180         /* Tell the static image filter to use that image from now on so
3181                  that the image will be used next time it has to be read */
3182         ms_static_image_set_default_image(path);
3183 #else
3184         ms_warning("Video support not compiled.");
3185 #endif
3186         return 0;
3187 }
3188
3189 /**
3190  * Returns the native window handle of the video window, casted as an unsigned long.
3191  *
3192  * @ingroup media_parameters
3193 **/
3194 unsigned long linphone_core_get_native_video_window_id(const LinphoneCore *lc){
3195 #ifdef VIDEO_ENABLED
3196         if (lc->videostream)
3197                 return video_stream_get_native_window_id(lc->videostream);
3198         if (lc->previewstream)
3199                 return video_stream_get_native_window_id(lc->previewstream);
3200 #endif
3201         return 0;
3202 }
3203
3204 static MSVideoSizeDef supported_resolutions[]={
3205         {       {MS_VIDEO_SIZE_SVGA_W,MS_VIDEO_SIZE_SVGA_H}     ,       "svga"  },
3206         {       {MS_VIDEO_SIZE_4CIF_W,MS_VIDEO_SIZE_4CIF_H}     ,       "4cif"  },
3207         {       {MS_VIDEO_SIZE_VGA_W,MS_VIDEO_SIZE_VGA_H}       ,       "vga"   },
3208         {       {MS_VIDEO_SIZE_CIF_W,MS_VIDEO_SIZE_CIF_H}       ,       "cif"   },
3209         {       {MS_VIDEO_SIZE_QVGA_W,MS_VIDEO_SIZE_QVGA_H}     ,       "qvga"  },
3210         {       {MS_VIDEO_SIZE_QCIF_W,MS_VIDEO_SIZE_QCIF_H}     ,       "qcif"  },
3211         {       {0,0}                   ,       NULL    }
3212 };
3213
3214 /**
3215  * Returns the zero terminated table of supported video resolutions.
3216  *
3217  * @ingroup media_parameters
3218 **/
3219 const MSVideoSizeDef *linphone_core_get_supported_video_sizes(LinphoneCore *lc){
3220         return supported_resolutions;
3221 }
3222
3223 static MSVideoSize video_size_get_by_name(const char *name){
3224         MSVideoSizeDef *pdef=supported_resolutions;
3225         MSVideoSize null_vsize={0,0};
3226         for(;pdef->name!=NULL;pdef++){
3227                 if (strcasecmp(name,pdef->name)==0){
3228                         return pdef->vsize;
3229                 }
3230         }
3231         ms_warning("Video resolution %s is not supported in linphone.",name);
3232         return null_vsize;
3233 }
3234
3235 static const char *video_size_get_name(MSVideoSize vsize){
3236         MSVideoSizeDef *pdef=supported_resolutions;
3237         for(;pdef->name!=NULL;pdef++){
3238                 if (pdef->vsize.width==vsize.width && pdef->vsize.height==vsize.height){
3239                         return pdef->name;
3240                 }
3241         }
3242         return NULL;
3243 }
3244
3245 static bool_t video_size_supported(MSVideoSize vsize){
3246         if (video_size_get_name(vsize)) return TRUE;
3247         ms_warning("Video resolution %ix%i is not supported in linphone.",vsize.width,vsize.height);
3248         return FALSE;
3249 }
3250
3251 /**
3252  * Sets the preferred video size.
3253  *
3254  * @ingroup media_parameters
3255  * This applies only to the stream that is captured and sent to the remote party,
3256  * since we accept all standard video size on the receive path.
3257 **/
3258 void linphone_core_set_preferred_video_size(LinphoneCore *lc, MSVideoSize vsize){
3259         if (video_size_supported(vsize)){
3260                 MSVideoSize oldvsize=lc->video_conf.vsize;
3261                 lc->video_conf.vsize=vsize;
3262                 if (!ms_video_size_equal(oldvsize,vsize) && lc->previewstream!=NULL){
3263                         toggle_video_preview(lc,FALSE);
3264                         toggle_video_preview(lc,TRUE);
3265                 }
3266                 if (lc->ready)
3267                         lp_config_set_string(lc->config,"video","size",video_size_get_name(vsize));
3268         }
3269 }
3270
3271 /**
3272  * Sets the preferred video size by its name.
3273  *
3274  * @ingroup media_parameters
3275  * This is identical to linphone_core_set_preferred_video_size() except
3276  * that it takes the name of the video resolution as input.
3277  * Video resolution names are: qcif, svga, cif, vga, 4cif, svga ...
3278 **/
3279 void linphone_core_set_preferred_video_size_by_name(LinphoneCore *lc, const char *name){
3280         MSVideoSize vsize=video_size_get_by_name(name);
3281         MSVideoSize default_vsize={MS_VIDEO_SIZE_CIF_W,MS_VIDEO_SIZE_CIF_H};
3282         if (vsize.width!=0)     linphone_core_set_preferred_video_size(lc,vsize);
3283         else linphone_core_set_preferred_video_size(lc,default_vsize);
3284 }
3285
3286 /**
3287  * Returns the current preferred video size for sending.
3288  *
3289  * @ingroup media_parameters
3290 **/
3291 MSVideoSize linphone_core_get_preferred_video_size(LinphoneCore *lc){
3292         return lc->video_conf.vsize;
3293 }
3294
3295 void linphone_core_use_files(LinphoneCore *lc, bool_t yesno){
3296         lc->use_files=yesno;
3297 }
3298
3299 void linphone_core_set_play_file(LinphoneCore *lc, const char *file){
3300         if (lc->play_file!=NULL){
3301                 ms_free(lc->play_file);
3302                 lc->play_file=NULL;
3303         }
3304         if (file!=NULL) {
3305                 lc->play_file=ms_strdup(file);
3306                 if (lc->audiostream->ticker)
3307                         audio_stream_play(lc->audiostream,file);
3308         }
3309 }
3310
3311 void linphone_core_set_record_file(LinphoneCore *lc, const char *file){
3312         if (lc->rec_file!=NULL){
3313                 ms_free(lc->rec_file);
3314                 lc->rec_file=NULL;
3315         }
3316         if (file!=NULL) {
3317                 lc->rec_file=ms_strdup(file);
3318                 if (lc->audiostream)
3319                         audio_stream_record(lc->audiostream,file);
3320         }
3321 }
3322
3323 /**
3324  * Retrieves the user pointer that was given to linphone_core_new()
3325  *
3326  * @ingroup initializing
3327 **/
3328 void *linphone_core_get_user_data(LinphoneCore *lc){
3329         return lc->data;
3330 }
3331
3332 int linphone_core_get_mtu(const LinphoneCore *lc){
3333         return lc->net_conf.mtu;
3334 }
3335
3336 void linphone_core_set_mtu(LinphoneCore *lc, int mtu){
3337         lc->net_conf.mtu=mtu;
3338         if (mtu>0){
3339                 if (mtu<500){
3340                         ms_error("MTU too small !");
3341                         mtu=500;
3342                 }
3343                 ms_set_mtu(mtu);
3344                 ms_message("MTU is supposed to be %i, rtp payload max size will be %i",mtu, ms_get_payload_max_size());
3345         }else ms_set_mtu(0);//use mediastreamer2 default value
3346 }
3347
3348 void linphone_core_set_waiting_callback(LinphoneCore *lc, LinphoneWaitingCallback cb, void *user_context){
3349         lc->wait_cb=cb;
3350         lc->wait_ctx=user_context;
3351 }
3352
3353 void linphone_core_start_waiting(LinphoneCore *lc, const char *purpose){
3354         if (lc->wait_cb){
3355                 lc->wait_ctx=lc->wait_cb(lc,lc->wait_ctx,LinphoneWaitingStart,purpose,0);
3356         }
3357 }
3358
3359 void linphone_core_update_progress(LinphoneCore *lc, const char *purpose, float progress){
3360         if (lc->wait_cb){
3361                 lc->wait_ctx=lc->wait_cb(lc,lc->wait_ctx,LinphoneWaitingProgress,purpose,progress);
3362         }else{
3363 #ifdef WIN32
3364                 Sleep(50000);
3365 #else
3366                 usleep(50000);
3367 #endif
3368         }
3369 }
3370
3371 void linphone_core_stop_waiting(LinphoneCore *lc){
3372         if (lc->wait_cb){
3373                 lc->wait_ctx=lc->wait_cb(lc,lc->wait_ctx,LinphoneWaitingFinished,NULL,0);
3374         }
3375 }
3376
3377 void linphone_core_set_audio_transports(LinphoneCore *lc, RtpTransport *rtp, RtpTransport *rtcp){
3378         lc->a_rtp=rtp;
3379         lc->a_rtcp=rtcp;
3380 }
3381
3382 void net_config_uninit(LinphoneCore *lc)
3383 {
3384         net_config_t *config=&lc->net_conf;
3385         lp_config_set_int(lc->config,"net","download_bw",config->download_bw);
3386         lp_config_set_int(lc->config,"net","upload_bw",config->upload_bw);
3387
3388         if (config->stun_server!=NULL){
3389                 lp_config_set_string(lc->config,"net","stun_server",config->stun_server);
3390                 ms_free(lc->net_conf.stun_server);
3391         }
3392         if (config->nat_address!=NULL){
3393                 lp_config_set_string(lc->config,"net","nat_address",config->nat_address);
3394                 ms_free(lc->net_conf.nat_address);
3395         }
3396         lp_config_set_int(lc->config,"net","firewall_policy",config->firewall_policy);
3397         lp_config_set_int(lc->config,"net","mtu",config->mtu);  
3398 }
3399
3400
3401 void sip_config_uninit(LinphoneCore *lc)
3402 {
3403         MSList *elem;
3404         int i;
3405         sip_config_t *config=&lc->sip_conf;
3406         lp_config_set_int(lc->config,"sip","sip_port",config->sip_port);
3407         lp_config_set_int(lc->config,"sip","guess_hostname",config->guess_hostname);
3408         lp_config_set_string(lc->config,"sip","contact",config->contact);
3409         lp_config_set_int(lc->config,"sip","inc_timeout",config->inc_timeout);
3410         lp_config_set_int(lc->config,"sip","use_info",config->use_info);
3411         lp_config_set_int(lc->config,"sip","use_rfc2833",config->use_rfc2833);
3412         lp_config_set_int(lc->config,"sip","use_ipv6",config->ipv6_enabled);
3413         lp_config_set_int(lc->config,"sip","register_only_when_network_is_up",config->register_only_when_network_is_up);
3414
3415         lp_config_set_int(lc->config,"sip","default_proxy",linphone_core_get_default_proxy(lc,NULL));
3416         
3417         for(elem=config->proxies,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
3418                 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)(elem->data);
3419                 linphone_proxy_config_write_to_config_file(lc->config,cfg,i);
3420                 linphone_proxy_config_edit(cfg);        /* to unregister */
3421         }
3422
3423         if (lc->sal){
3424             int i;
3425                 for (i=0;i<20;i++){
3426                         sal_iterate(lc->sal);
3427 #ifndef WIN32
3428                         usleep(100000);
3429 #else
3430                         Sleep(100);
3431 #endif
3432                 }
3433         }
3434
3435         ms_list_for_each(config->proxies,(void (*)(void*)) linphone_proxy_config_destroy);
3436         ms_list_free(config->proxies);
3437         config->proxies=NULL;
3438         
3439         linphone_proxy_config_write_to_config_file(lc->config,NULL,i);  /*mark the end */
3440
3441         for(elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
3442                 LinphoneAuthInfo *ai=(LinphoneAuthInfo*)(elem->data);
3443                 linphone_auth_info_write_config(lc->config,ai,i);
3444         }
3445         linphone_auth_info_write_config(lc->config,NULL,i); /* mark the end */
3446         ms_list_for_each(lc->auth_info,(void (*)(void*))linphone_auth_info_destroy);
3447         ms_list_free(lc->auth_info);
3448         lc->auth_info=NULL;
3449         
3450         sal_uninit(lc->sal);
3451         lc->sal=NULL;
3452
3453         if (lc->sip_conf.guessed_contact)
3454                 ms_free(lc->sip_conf.guessed_contact);
3455         if (config->contact)
3456                 ms_free(config->contact);
3457         
3458 }
3459
3460 void rtp_config_uninit(LinphoneCore *lc)
3461 {
3462         rtp_config_t *config=&lc->rtp_conf;
3463         lp_config_set_int(lc->config,"rtp","audio_rtp_port",config->audio_rtp_port);
3464         lp_config_set_int(lc->config,"rtp","video_rtp_port",config->video_rtp_port);
3465         lp_config_set_int(lc->config,"rtp","audio_jitt_comp",config->audio_jitt_comp);
3466         lp_config_set_int(lc->config,"rtp","video_jitt_comp",config->video_jitt_comp);
3467         lp_config_set_int(lc->config,"rtp","nortp_timeout",config->nortp_timeout);
3468 }
3469
3470 void sound_config_uninit(LinphoneCore *lc)
3471 {
3472         sound_config_t *config=&lc->sound_conf;
3473         ms_free(config->cards);
3474
3475         lp_config_set_string(lc->config,"sound","remote_ring",config->remote_ring);
3476
3477         if (config->local_ring) ms_free(config->local_ring);
3478         if (config->remote_ring) ms_free(config->remote_ring);
3479         ms_snd_card_manager_destroy();
3480 }
3481
3482 void video_config_uninit(LinphoneCore *lc)
3483 {
3484         lp_config_set_int(lc->config,"video","enabled",linphone_core_video_enabled(lc));
3485         lp_config_set_string(lc->config,"video","size",video_size_get_name(linphone_core_get_preferred_video_size(lc)));
3486         lp_config_set_int(lc->config,"video","display",lc->video_conf.display);
3487         lp_config_set_int(lc->config,"video","capture",lc->video_conf.capture);
3488         lp_config_set_int(lc->config,"video","show_local",linphone_core_video_preview_enabled(lc));
3489         lp_config_set_int(lc->config,"video","self_view",linphone_core_self_view_enabled(lc));
3490         if (lc->video_conf.cams)
3491                 ms_free(lc->video_conf.cams);
3492 }
3493
3494 void codecs_config_uninit(LinphoneCore *lc)
3495 {
3496         PayloadType *pt;
3497         codecs_config_t *config=&lc->codecs_conf;
3498         MSList *node;
3499         char key[50];
3500         int index;
3501         index=0;
3502         for(node=config->audio_codecs;node!=NULL;node=ms_list_next(node)){
3503                 pt=(PayloadType*)(node->data);
3504                 sprintf(key,"audio_codec_%i",index);
3505                 lp_config_set_string(lc->config,key,"mime",pt->mime_type);
3506                 lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
3507                 lp_config_set_int(lc->config,key,"enabled",linphone_core_payload_type_enabled(lc,pt));
3508                 index++;
3509         }
3510         index=0;
3511         for(node=config->video_codecs;node!=NULL;node=ms_list_next(node)){
3512                 pt=(PayloadType*)(node->data);
3513                 sprintf(key,"video_codec_%i",index);
3514                 lp_config_set_string(lc->config,key,"mime",pt->mime_type);
3515                 lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
3516                 lp_config_set_int(lc->config,key,"enabled",linphone_core_payload_type_enabled(lc,pt));
3517                 lp_config_set_string(lc->config,key,"recv_fmtp",pt->recv_fmtp);
3518                 index++;
3519         }
3520         ms_list_free(lc->codecs_conf.audio_codecs);
3521         ms_list_free(lc->codecs_conf.video_codecs);
3522 }
3523
3524 void ui_config_uninit(LinphoneCore* lc)
3525 {
3526         if (lc->friends){
3527                 ms_list_for_each(lc->friends,(void (*)(void *))linphone_friend_destroy);
3528                 ms_list_free(lc->friends);
3529                 lc->friends=NULL;
3530         }
3531 }
3532
3533 /**
3534  * Returns the LpConfig object used to manage the storage (config) file.
3535  *
3536  * @ingroup misc
3537  * The application can use the LpConfig object to insert its own private 
3538  * sections and pairs of key=value in the configuration file.
3539  * 
3540 **/
3541 LpConfig *linphone_core_get_config(LinphoneCore *lc){
3542         return lc->config;
3543 }
3544
3545 static void linphone_core_uninit(LinphoneCore *lc)
3546 {
3547         if(linphone_core_get_calls_nb(lc)){
3548                 int i;
3549                 linphone_core_terminate_all_calls(lc);
3550                 for(i=0;i<10;++i){
3551 #ifndef WIN32
3552                         usleep(50000);
3553 #else
3554                         Sleep(50);
3555 #endif
3556                         linphone_core_iterate(lc);
3557                 }
3558         }
3559         if (lc->friends)
3560                 ms_list_for_each(lc->friends,(void (*)(void *))linphone_friend_close_subscriptions);
3561         gstate_new_state(lc, GSTATE_POWER_SHUTDOWN, NULL);
3562 #ifdef VIDEO_ENABLED
3563         if (lc->previewstream!=NULL){
3564                 video_preview_stop(lc->previewstream);
3565                 lc->previewstream=NULL;
3566         }
3567 #endif
3568         /* save all config */
3569         net_config_uninit(lc);
3570         sip_config_uninit(lc);
3571         rtp_config_uninit(lc);
3572         sound_config_uninit(lc);
3573         video_config_uninit(lc);
3574         codecs_config_uninit(lc);
3575         ui_config_uninit(lc);
3576         if (lp_config_needs_commit(lc->config)) lp_config_sync(lc->config);
3577         lp_config_destroy(lc->config);
3578         lc->config = NULL; /* Mark the config as NULL to block further calls */
3579         sip_setup_unregister_all();
3580
3581         ms_list_for_each(lc->call_logs,(void (*)(void*))linphone_call_log_destroy);
3582         lc->call_logs=ms_list_free(lc->call_logs);
3583
3584         linphone_core_free_payload_types();
3585
3586         ortp_exit();
3587         gstate_new_state(lc, GSTATE_POWER_OFF, NULL);
3588 }
3589
3590 static void set_network_reachable(LinphoneCore* lc,bool_t isReachable){
3591         ms_message("Network state is now [%s]",isReachable?"UP":"DOWN");
3592         // second get the list of available proxies
3593         const MSList *elem=linphone_core_get_proxy_config_list(lc);
3594         for(;elem!=NULL;elem=elem->next){
3595                 LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
3596                 if (linphone_proxy_config_register_enabled(cfg) ) {
3597                         if (!isReachable) {
3598                                 cfg->registered=0;
3599                         }else{
3600                                 cfg->commit=TRUE;
3601                         }
3602                 }
3603         }
3604         lc->network_reachable=isReachable;
3605 }
3606
3607 void linphone_core_set_network_reachable(LinphoneCore* lc,bool_t isReachable) {
3608         //first disable automatic mode
3609         if (lc->auto_net_state_mon) {
3610                 ms_message("Disabling automatic network state monitoring");
3611                 lc->auto_net_state_mon=FALSE;
3612         }
3613         set_network_reachable(lc,isReachable);
3614 }
3615
3616 bool_t linphone_core_is_network_reachabled(LinphoneCore* lc) {
3617         return lc->network_reachable;
3618 }
3619 ortp_socket_t linphone_core_get_sip_socket(LinphoneCore *lc){
3620         return sal_get_socket(lc->sal);
3621 }
3622 /**
3623  * Destroys a LinphoneCore
3624  *
3625  * @ingroup initializing
3626 **/
3627 void linphone_core_destroy(LinphoneCore *lc){
3628         linphone_core_uninit(lc);
3629         ms_free(lc);
3630 }
3631 /**
3632  * Get the number of Call
3633  *
3634  * @ingroup call_control
3635 **/
3636 int linphone_core_get_calls_nb(const LinphoneCore *lc)
3637 {
3638         int returned;
3639         if(lc->calls == NULL)
3640         {
3641                 returned = 0;
3642         }
3643         else
3644         {
3645                 returned = ms_list_size(lc->calls);
3646         }
3647         return returned;
3648 }
3649
3650 /**
3651  * Check if we do not have exceed the number of simultaneous call
3652  *
3653  * @ingroup call_control
3654 **/
3655 bool_t linphone_core_can_we_add_call(LinphoneCore *lc)
3656 {
3657         if(linphone_core_get_calls_nb(lc) < NB_MAX_CALLS)
3658                 return TRUE;
3659         return FALSE;
3660 }
3661
3662 /**
3663  * Unset the current call
3664  *
3665  * @ingroup call_control
3666 **/
3667 int linphone_core_unset_the_current_call(LinphoneCore *lc)
3668 {
3669         if(lc->current_call == NULL)
3670                 return -1;
3671         lc->current_call = NULL;
3672         ms_message("Current call unset\n");
3673         return 0;
3674 }
3675
3676 /**
3677  * Set the call in parameter as the new current call
3678  *
3679  * @ingroup call_control
3680 **/
3681 int linphone_core_set_as_current_call(LinphoneCore *lc, LinphoneCall *call)
3682 {
3683         if(lc->current_call != NULL)
3684                 return -1;
3685         lc->current_call = call;
3686         return 0;
3687 }
3688
3689 /**
3690  * Add the call in the LinphoneCall list
3691  *
3692  * @ingroup call_control
3693 **/
3694 int linphone_core_add_call( LinphoneCore *lc, LinphoneCall *call)
3695 {
3696         if(linphone_core_can_we_add_call(lc))
3697         {
3698                 MSList *the_calls = lc->calls;
3699                 the_calls = ms_list_append(the_calls,(void *)call);
3700                 lc->calls = the_calls;
3701                 return 0;
3702         }
3703         return -1;
3704 }
3705
3706 /**
3707  * Add the call in the LinphoneCall list
3708  *
3709  * @ingroup call_control
3710 **/
3711 int linphone_core_del_call( LinphoneCore *lc, LinphoneCall *call)
3712 {
3713         MSList *it;
3714
3715         MSList *the_calls = lc->calls;
3716         if(call == linphone_core_get_current_call(lc))
3717         {
3718                 linphone_core_unset_the_current_call(lc);
3719         }       
3720         it=ms_list_find(the_calls,call);
3721         if (it) 
3722         {
3723                 the_calls = ms_list_remove_link(the_calls,it);
3724         }
3725         else
3726         {
3727                 ms_warning("could not find the call into the list\n");
3728                 return -1;
3729         }
3730         lc->calls = the_calls;
3731         return 0;
3732 }