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