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