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