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