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