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