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