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