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