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