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