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