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