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