source: mediastreamer2/linphone/coreapi/linphonecore.c @ 178:3d0c37cee462

Last change on this file since 178:3d0c37cee462 was 178:3d0c37cee462, checked in by smorlat <smorlat@…>, 4 years ago

fix several memory leaks.

git-svn-id: svn+ssh://svn.savannah.nongnu.org/linphone/trunk@181 3f6dc0c8-ddfe-455d-9043-3cd528dc4637

File size: 67.3 KB
Line 
1/*
2linphone
3Copyright (C) 2000  Simon MORLAT (simon.morlat@linphone.org)
4
5This program is free software; you can redistribute it and/or
6modify it under the terms of the GNU General Public License
7as published by the Free Software Foundation; either version 2
8of the License, or (at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18*/
19
20#include "linphonecore.h"
21#include "lpconfig.h"
22#include "private.h"
23#include "mediastreamer2/mediastream.h"
24#include <eXosip2/eXosip.h>
25#include "sdphandler.h"
26
27#include <ortp/telephonyevents.h>
28#include "exevents.h"
29
30
31#ifdef INET6 
32#ifndef WIN32
33#include <netdb.h> 
34#endif
35#endif
36
37#ifdef WIN32
38#define HAVE_EXOSIP_GET_VERSION 1
39#endif
40
41
42static const char *liblinphone_version=LIBLINPHONE_VERSION;
43
44#include "enum.h"
45
46void linphone_core_get_local_ip(LinphoneCore *lc, const char *dest, char *result);
47static void apply_nat_settings(LinphoneCore *lc);
48static void toggle_video_preview(LinphoneCore *lc, bool_t val);
49
50/* relative path where is stored local ring*/
51#define LOCAL_RING "rings/oldphone.wav"
52/* same for remote ring (ringback)*/
53#define REMOTE_RING_FR "ringback.wav"
54#define REMOTE_RING_US "ringback.wav"
55
56
57sdp_handler_t linphone_sdphandler={
58        linphone_accept_audio_offer,   /*from remote sdp */
59        linphone_accept_video_offer,   /*from remote sdp */
60        linphone_set_audio_offer,       /*to local sdp */
61        linphone_set_video_offer,       /*to local sdp */
62        linphone_read_audio_answer,     /*from incoming answer  */
63        linphone_read_video_answer      /*from incoming answer  */
64};
65
66void lc_callback_obj_init(LCCallbackObj *obj,LinphoneCoreCbFunc func,void* ud)
67{
68  obj->_func=func;
69  obj->_user_data=ud;
70}
71
72int lc_callback_obj_invoke(LCCallbackObj *obj, LinphoneCore *lc){
73        if (obj->_func!=NULL) obj->_func(lc,obj->_user_data);
74        return 0;
75}
76
77static void  linphone_call_init_common(LinphoneCall *call, char *from, char *to){
78        call->state=LCStateInit;
79        call->start_time=time(NULL);
80        call->log=linphone_call_log_new(call, from, to);
81        linphone_core_notify_all_friends(call->core,LINPHONE_STATUS_ONTHEPHONE);
82        if (linphone_core_get_firewall_policy(call->core)==LINPHONE_POLICY_USE_STUN) 
83                linphone_core_run_stun_tests(call->core,call);
84        call->profile=rtp_profile_new("Call RTP profile");
85}
86
87void linphone_call_init_media_params(LinphoneCall *call){
88        memset(&call->audio_params,0,sizeof(call->audio_params));
89        memset(&call->video_params,0,sizeof(call->video_params));
90}
91
92static void discover_mtu(LinphoneCore *lc, const char *remote){
93        int mtu;
94        if (lc->net_conf.mtu==0 ){
95                /*attempt to discover mtu*/
96                mtu=ms_discover_mtu(remote);
97                if (mtu>0){
98                        ms_set_mtu(mtu);
99                        ms_message("Discovered mtu is %i, RTP payload max size is %i",
100                                mtu, ms_get_payload_max_size());
101                }
102        }
103}
104
105LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, const osip_from_t *from, const osip_to_t *to)
106{
107        LinphoneCall *call=ms_new0(LinphoneCall,1);
108        char localip[LINPHONE_IPADDR_SIZE];
109        char *fromstr=NULL,*tostr=NULL;
110        call->dir=LinphoneCallOutgoing;
111        call->cid=-1;
112        call->did=-1;
113        call->tid=-1;
114        call->core=lc;
115        linphone_core_get_local_ip(lc,to->url->host,localip);
116        osip_from_to_str(from,&fromstr);
117        osip_to_to_str(to,&tostr);
118        linphone_call_init_common(call,fromstr,tostr);
119        call->sdpctx=sdp_handler_create_context(&linphone_sdphandler,
120                call->audio_params.natd_port>0 ? call->audio_params.natd_addr : localip,
121                from->url->username,NULL);
122        sdp_context_set_user_pointer(call->sdpctx,(void*)call);
123        discover_mtu(lc,to->url->host);
124        return call;
125}
126
127
128LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, const char *from, const char *to, int cid, int did, int tid)
129{
130        char localip[LINPHONE_IPADDR_SIZE];
131        LinphoneCall *call=ms_new0(LinphoneCall,1);
132        osip_from_t *me= linphone_core_get_primary_contact_parsed(lc);
133        osip_from_t *from_url=NULL;
134        call->dir=LinphoneCallIncoming;
135        call->cid=cid;
136        call->did=did;
137        call->tid=tid;
138        call->core=lc;
139        osip_from_init(&from_url);
140        osip_from_parse(from_url, from);
141        linphone_core_get_local_ip(lc,from_url->url->host,localip);
142        linphone_call_init_common(call, osip_strdup(from), osip_strdup(to));
143        call->sdpctx=sdp_handler_create_context(&linphone_sdphandler,
144                call->audio_params.natd_port>0 ? call->audio_params.natd_addr : localip,
145                me->url->username,NULL);
146        sdp_context_set_user_pointer(call->sdpctx,(void*)call);
147        discover_mtu(lc,from_url->url->host);
148        osip_from_free(me);
149        osip_from_free(from_url);
150        return call;
151}
152
153void linphone_call_destroy(LinphoneCall *obj)
154{
155        linphone_core_notify_all_friends(obj->core,obj->core->prev_mode);
156        linphone_call_log_completed(obj->log,obj);
157        linphone_core_update_allocated_audio_bandwidth(obj->core);
158        if (obj->profile!=NULL) rtp_profile_destroy(obj->profile);
159        if (obj->sdpctx!=NULL) sdp_context_free(obj->sdpctx);
160        ms_free(obj);
161}
162
163/*prevent a gcc bug with %c*/
164static size_t my_strftime(char *s, size_t max, const char  *fmt,  const struct tm *tm){
165        return strftime(s, max, fmt, tm);
166}
167
168LinphoneCallLog * linphone_call_log_new(LinphoneCall *call, char *from, char *to){
169        LinphoneCallLog *cl=ms_new0(LinphoneCallLog,1);
170        struct tm loctime;
171        cl->dir=call->dir;
172#ifdef WIN32
173        loctime=*localtime(&call->start_time);
174#else
175        localtime_r(&call->start_time,&loctime);
176#endif
177        my_strftime(cl->start_date,sizeof(cl->start_date),"%c",&loctime);
178        cl->from=from;
179        cl->to=to;
180        return cl;
181}
182void linphone_call_log_completed(LinphoneCallLog *calllog, LinphoneCall *call){
183        LinphoneCore *lc=call->core;
184        calllog->duration=time(NULL)-call->start_time;
185        switch(call->state){
186                case LCStateInit:
187                        calllog->status=LinphoneCallAborted;
188                        break;
189                case LCStateRinging:
190                        if (calllog->dir==LinphoneCallIncoming){
191                                char *info;
192                                calllog->status=LinphoneCallMissed;
193                                lc->missed_calls++;
194                                info=ortp_strdup_printf(_("You have missed %i call(s)."),lc->missed_calls);
195                                lc->vtable.display_status(lc,info);
196                                ms_free(info);
197                        }
198                        else calllog->status=LinphoneCallAborted;
199                        break;
200                case LCStateAVRunning:
201                        calllog->status=LinphoneCallSuccess;
202                        break;
203        }
204        lc->call_logs=ms_list_append(lc->call_logs,(void *)calllog);
205        if (ms_list_size(lc->call_logs)>lc->max_call_logs){
206                MSList *elem;
207                elem=lc->call_logs;
208                linphone_call_log_destroy((LinphoneCallLog*)elem->data);
209                lc->call_logs=ms_list_remove_link(lc->call_logs,elem);
210        }
211        if (lc->vtable.call_log_updated!=NULL){
212                lc->vtable.call_log_updated(lc,calllog);
213        }
214}
215
216char * linphone_call_log_to_str(LinphoneCallLog *cl){
217        char *status;
218        switch(cl->status){
219                case LinphoneCallAborted:
220                        status=_("aborted");
221                        break;
222                case LinphoneCallSuccess:
223                        status=_("completed");
224                        break;
225                case LinphoneCallMissed:
226                        status=_("missed");
227                        break;
228                default:
229                        status="unknown";
230        }
231        return ortp_strdup_printf(_("%s at %s\nFrom: %s\nTo: %s\nStatus: %s\nDuration: %i mn %i sec\n"),
232                        (cl->dir==LinphoneCallIncoming) ? _("Incoming call") : _("Outgoing call"),
233                        cl->start_date,
234                        cl->from,
235                        cl->to,
236                        status,
237                        cl->duration/60,
238                        cl->duration%60);
239}
240
241void linphone_call_log_destroy(LinphoneCallLog *cl){
242        if (cl->from!=NULL) osip_free(cl->from);
243        if (cl->to!=NULL) osip_free(cl->to);
244        ms_free(cl);
245}
246
247void _osip_trace_func(char *fi, int li, osip_trace_level_t level, char *chfr, va_list ap){
248        int ortp_level=ORTP_DEBUG;
249        switch(level){
250                case OSIP_INFO1:
251                case OSIP_INFO2:
252                case OSIP_INFO3:
253                case OSIP_INFO4:
254                        ortp_level=ORTP_MESSAGE;
255                        break;
256                case OSIP_WARNING:
257                        ortp_level=ORTP_WARNING;
258                        break;
259                case OSIP_ERROR:
260                case OSIP_BUG:
261                        ortp_level=ORTP_ERROR;
262                        break;
263                case OSIP_FATAL:
264                        ortp_level=ORTP_FATAL;
265                        break;
266                case END_TRACE_LEVEL:
267                        break; 
268        }
269        if (ortp_log_level_enabled(level)){
270                int len=strlen(chfr);
271                char *chfrdup=ortp_strdup(chfr);
272                /*need to remove endline*/
273                if (len>1){
274                        if (chfrdup[len-1]=='\n')
275                                chfrdup[len-1]='\0';
276                        if (chfrdup[len-2]=='\r')
277                                chfrdup[len-2]='\0';
278                }
279                ortp_logv(ortp_level,chfrdup,ap);
280                ortp_free(chfrdup);
281        }
282}
283
284
285void linphone_core_enable_logs(FILE *file){
286        if (file==NULL) file=stdout;
287        ortp_set_log_file(file);
288        ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
289        osip_trace_initialize_func (OSIP_INFO4,&_osip_trace_func);
290}
291
292void linphone_core_enable_logs_with_cb(OrtpLogFunc logfunc){
293        ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
294        osip_trace_initialize_func (OSIP_INFO4,&_osip_trace_func);
295        ortp_set_log_handler(logfunc);
296}
297
298void linphone_core_disable_logs(){
299        int tl;
300        for (tl=0;tl<=OSIP_INFO4;tl++) osip_trace_disable_level(tl);
301        ortp_set_log_level_mask(ORTP_ERROR|ORTP_FATAL);
302}
303
304
305void
306net_config_read (LinphoneCore *lc)
307{
308        int tmp;
309        const char *tmpstr;
310        LpConfig *config=lc->config;
311
312        tmp=lp_config_get_int(config,"net","download_bw",0);
313        linphone_core_set_download_bandwidth(lc,tmp);
314        tmp=lp_config_get_int(config,"net","upload_bw",0);
315        linphone_core_set_upload_bandwidth(lc,tmp);
316        linphone_core_set_stun_server(lc,lp_config_get_string(config,"net","stun_server",NULL));
317        tmpstr=lp_config_get_string(lc->config,"net","nat_address",NULL);
318        if (tmpstr!=NULL && (strlen(tmpstr)<1)) tmpstr=NULL;
319        linphone_core_set_nat_address(lc,tmpstr);
320        tmp=lp_config_get_int(lc->config,"net","firewall_policy",0);
321        linphone_core_set_firewall_policy(lc,tmp);
322        tmp=lp_config_get_int(lc->config,"net","nat_sdp_only",0);
323        lc->net_conf.nat_sdp_only=tmp;
324        tmp=lp_config_get_int(lc->config,"net","mtu",0);
325        linphone_core_set_mtu(lc,tmp);
326}
327
328
329void sound_config_read(LinphoneCore *lc)
330{
331        /*int tmp;*/
332        const char *tmpbuf;
333        const char *devid;
334        const MSList *elem;
335        const char **devices;
336        int ndev;
337        int i;
338#ifndef WIN32
339        /*alsadev let the user use custom alsa device within linphone*/
340        devid=lp_config_get_string(lc->config,"sound","alsadev",NULL);
341        if (devid){
342                MSSndCard *card=ms_alsa_card_new_custom(devid,devid);
343                ms_snd_card_manager_add_card(ms_snd_card_manager_get(),card);
344        }
345#endif
346        /* retrieve all sound devices */
347        elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
348        ndev=ms_list_size(elem);
349        devices=ms_malloc((ndev+1)*sizeof(const char *));
350        for (i=0;elem!=NULL;elem=elem->next,i++){
351                devices[i]=ms_snd_card_get_string_id((MSSndCard *)elem->data);
352        }
353        devices[ndev]=NULL;
354        lc->sound_conf.cards=devices;
355        devid=lp_config_get_string(lc->config,"sound","playback_dev_id",NULL);
356        linphone_core_set_playback_device(lc,devid);
357       
358        devid=lp_config_get_string(lc->config,"sound","ringer_dev_id",NULL);
359        linphone_core_set_ringer_device(lc,devid);
360       
361        devid=lp_config_get_string(lc->config,"sound","capture_dev_id",NULL);
362        linphone_core_set_capture_device(lc,devid);
363       
364/*
365        tmp=lp_config_get_int(lc->config,"sound","play_lev",80);
366        linphone_core_set_play_level(lc,tmp);
367        tmp=lp_config_get_int(lc->config,"sound","ring_lev",80);
368        linphone_core_set_ring_level(lc,tmp);
369        tmp=lp_config_get_int(lc->config,"sound","rec_lev",80);
370        linphone_core_set_rec_level(lc,tmp);
371        tmpbuf=lp_config_get_string(lc->config,"sound","source","m");
372        linphone_core_set_sound_source(lc,tmpbuf[0]);
373*/
374       
375        tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
376        tmpbuf=lp_config_get_string(lc->config,"sound","local_ring",tmpbuf);
377        if (access(tmpbuf,F_OK)==-1) {
378                tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
379        }
380        if (strstr(tmpbuf,".wav")==NULL){
381                /* it currently uses old sound files, so replace them */
382                tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
383        }
384       
385        linphone_core_set_ring(lc,tmpbuf);
386       
387        tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING_FR;
388        tmpbuf=lp_config_get_string(lc->config,"sound","remote_ring",tmpbuf);
389        if (access(tmpbuf,F_OK)==-1){
390                tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING_FR;
391        }
392        if (strstr(tmpbuf,".wav")==NULL){
393                /* it currently uses old sound files, so replace them */
394                tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING_FR;
395        }
396        linphone_core_set_ringback(lc,0);
397        check_sound_device(lc);
398        lc->sound_conf.latency=0;
399
400        linphone_core_enable_echo_cancelation(lc,
401                lp_config_get_int(lc->config,"sound","echocancelation",0));
402}
403
404void sip_config_read(LinphoneCore *lc)
405{
406        char *contact;
407        const char *tmpstr;
408        int port;
409        int i,tmp;
410        int ipv6;
411        port=lp_config_get_int(lc->config,"sip","use_info",0);
412        linphone_core_set_use_info_for_dtmf(lc,port);
413
414        ipv6=lp_config_get_int(lc->config,"sip","use_ipv6",-1);
415        if (ipv6==-1){
416                ipv6=0;
417                if (host_has_ipv6_network()){
418                        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"));
419                }
420        }
421        linphone_core_enable_ipv6(lc,ipv6);
422        port=lp_config_get_int(lc->config,"sip","sip_port",5060);
423        linphone_core_set_sip_port(lc,port);
424       
425        tmpstr=lp_config_get_string(lc->config,"sip","contact",NULL);
426        if (tmpstr==NULL || linphone_core_set_primary_contact(lc,tmpstr)==-1) {
427                char *hostname=getenv("HOST");
428                char *username=getenv("USER");
429                if (hostname==NULL) hostname=getenv("HOSTNAME");
430                if (hostname==NULL)
431                        hostname="unknown-host";
432                if (username==NULL){
433                        username="toto";
434                }
435                contact=ortp_strdup_printf("sip:%s@%s",username,hostname);
436                linphone_core_set_primary_contact(lc,contact);
437                ms_free(contact);
438        }
439
440        tmp=lp_config_get_int(lc->config,"sip","guess_hostname",1);
441        linphone_core_set_guess_hostname(lc,tmp);
442       
443       
444        tmp=lp_config_get_int(lc->config,"sip","inc_timeout",15);
445        linphone_core_set_inc_timeout(lc,tmp);
446
447        /* get proxies config */
448        for(i=0;; i++){
449                LinphoneProxyConfig *cfg=linphone_proxy_config_new_from_config_file(lc->config,i);
450                if (cfg!=NULL){
451                        linphone_core_add_proxy_config(lc,cfg);
452                }else{
453                        break;
454                }
455        }
456        /* get the default proxy */
457        tmp=lp_config_get_int(lc->config,"sip","default_proxy",-1);
458        linphone_core_set_default_proxy_index(lc,tmp);
459       
460        /* read authentication information */
461        for(i=0;; i++){
462                LinphoneAuthInfo *ai=linphone_auth_info_new_from_config_file(lc->config,i);
463                if (ai!=NULL){
464                        linphone_core_add_auth_info(lc,ai);
465                }else{
466                        break;
467                }
468        }
469        /*for test*/
470        lc->sip_conf.sdp_200_ack=lp_config_get_int(lc->config,"sip","sdp_200_ack",0);
471        lc->sip_conf.only_one_codec=lp_config_get_int(lc->config,"sip","only_one_codec",0);
472}
473
474void rtp_config_read(LinphoneCore *lc)
475{
476        int port;
477        int jitt_comp;
478        int nortp_timeout;
479        port=lp_config_get_int(lc->config,"rtp","audio_rtp_port",7078);
480        linphone_core_set_audio_port(lc,port);
481       
482        port=lp_config_get_int(lc->config,"rtp","video_rtp_port",9078);
483        if (port==0) port=9078;
484        linphone_core_set_video_port(lc,port);
485       
486        jitt_comp=lp_config_get_int(lc->config,"rtp","audio_jitt_comp",60);
487        linphone_core_set_audio_jittcomp(lc,jitt_comp);         
488        jitt_comp=lp_config_get_int(lc->config,"rtp","video_jitt_comp",60);
489        nortp_timeout=lp_config_get_int(lc->config,"rtp","nortp_timeout",30);
490        linphone_core_set_nortp_timeout(lc,nortp_timeout);     
491}
492
493
494PayloadType * get_codec(LpConfig *config, char* type,int index){
495        char codeckey[50];
496        const char *mime,*fmtp;
497        int rate,enabled;
498        PayloadType *pt;
499       
500        snprintf(codeckey,50,"%s_%i",type,index);
501        mime=lp_config_get_string(config,codeckey,"mime",NULL);
502        if (mime==NULL || strlen(mime)==0 ) return NULL;
503       
504        pt=payload_type_new();
505        pt->mime_type=ms_strdup(mime);
506       
507        rate=lp_config_get_int(config,codeckey,"rate",8000);
508        pt->clock_rate=rate;
509        fmtp=lp_config_get_string(config,codeckey,"recv_fmtp",NULL);
510        if (fmtp) pt->recv_fmtp=ms_strdup(fmtp);
511        enabled=lp_config_get_int(config,codeckey,"enabled",1);
512        if (enabled ) pt->flags|=PAYLOAD_TYPE_ENABLED;
513        //ms_message("Found codec %s/%i",pt->mime_type,pt->clock_rate);
514        return pt;
515}
516
517void codecs_config_read(LinphoneCore *lc)
518{
519        int i;
520        PayloadType *pt;
521        MSList *audio_codecs=NULL;
522        MSList *video_codecs=NULL;
523        for (i=0;;i++){
524                pt=get_codec(lc->config,"audio_codec",i);
525                if (pt==NULL) break;
526                audio_codecs=ms_list_append(audio_codecs,(void *)pt);
527        }
528        for (i=0;;i++){
529                pt=get_codec(lc->config,"video_codec",i);
530                if (pt==NULL) break;
531                video_codecs=ms_list_append(video_codecs,(void *)pt);
532        }
533        linphone_core_set_audio_codecs(lc,audio_codecs);
534        linphone_core_set_video_codecs(lc,video_codecs);
535        linphone_core_setup_local_rtp_profile(lc);
536}
537
538void video_config_read(LinphoneCore *lc)
539{
540        int capture, display;
541        int enabled;
542        const char *str;
543        int ndev;
544        const char **devices;
545        const MSList *elem;
546        int i;
547
548        /* retrieve all video devices */
549        elem=ms_web_cam_manager_get_list(ms_web_cam_manager_get());
550        ndev=ms_list_size(elem);
551        devices=ms_malloc((ndev+1)*sizeof(const char *));
552        for (i=0;elem!=NULL;elem=elem->next,i++){
553                devices[i]=ms_web_cam_get_string_id((MSWebCam *)elem->data);
554        }
555        devices[ndev]=NULL;
556        lc->video_conf.cams=devices;
557
558        str=lp_config_get_string(lc->config,"video","device",NULL);
559        if (str && str[0]==0) str=NULL;
560        linphone_core_set_video_device(lc,str);
561       
562        linphone_core_set_preferred_video_size_by_name(lc,
563                lp_config_get_string(lc->config,"video","size","cif"));
564
565        enabled=lp_config_get_int(lc->config,"video","enabled",1);
566        capture=lp_config_get_int(lc->config,"video","capture",enabled);
567        display=lp_config_get_int(lc->config,"video","display",enabled);
568#ifdef VIDEO_ENABLED
569        linphone_core_enable_video(lc,capture,display);
570#endif
571}
572
573void ui_config_read(LinphoneCore *lc)
574{
575        LinphoneFriend *lf;
576        int i;
577        for (i=0;(lf=linphone_friend_new_from_config_file(lc,i))!=NULL;i++){
578                linphone_core_add_friend(lc,lf);
579        }
580       
581}
582
583void autoreplier_config_init(LinphoneCore *lc)
584{
585        autoreplier_config_t *config=&lc->autoreplier_conf;
586        config->enabled=lp_config_get_int(lc->config,"autoreplier","enabled",0);
587        config->after_seconds=lp_config_get_int(lc->config,"autoreplier","after_seconds",6);
588        config->max_users=lp_config_get_int(lc->config,"autoreplier","max_users",1);
589        config->max_rec_time=lp_config_get_int(lc->config,"autoreplier","max_rec_time",60);
590        config->max_rec_msg=lp_config_get_int(lc->config,"autoreplier","max_rec_msg",10);
591        config->message=lp_config_get_string(lc->config,"autoreplier","message",NULL);
592}
593
594void linphone_core_set_download_bandwidth(LinphoneCore *lc, int bw){
595        lc->net_conf.download_bw=bw;
596        if (bw==0){ /*infinite*/
597                lc->dw_audio_bw=-1;
598                lc->dw_video_bw=-1;
599        }else {
600                lc->dw_audio_bw=MIN(lc->audio_bw,bw);
601                lc->dw_video_bw=MAX(bw-lc->dw_audio_bw-10,0);/*-10: security margin*/
602        }
603}
604
605void linphone_core_set_upload_bandwidth(LinphoneCore *lc, int bw){
606        lc->net_conf.upload_bw=bw;
607        if (bw==0){ /*infinite*/
608                lc->up_audio_bw=-1;
609                lc->up_video_bw=-1;
610        }else{
611                lc->up_audio_bw=MIN(lc->audio_bw,bw);
612                lc->up_video_bw=MAX(bw-lc->up_audio_bw-10,0);/*-10: security margin*/
613        }
614}
615
616int linphone_core_get_download_bandwidth(const LinphoneCore *lc){
617        return lc->net_conf.download_bw;
618}
619
620int linphone_core_get_upload_bandwidth(const LinphoneCore *lc){
621        return lc->net_conf.upload_bw;
622}
623
624const char * linphone_core_get_version(void){
625        return liblinphone_version;
626}
627
628#ifdef VIDEO_ENABLED
629
630static PayloadType * payload_type_h264_packetization_mode_1=NULL;
631static PayloadType * linphone_h263_1998=NULL;
632static PayloadType * linphone_mp4v_es=NULL;
633#endif
634
635void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vtable, const char *config_path, void * userdata)
636{
637        memset (lc, 0, sizeof (LinphoneCore));
638        lc->data=userdata;
639       
640        memcpy(&lc->vtable,vtable,sizeof(LinphoneCoreVTable));
641
642        gstate_initialize();
643        gstate_new_state(lc, GSTATE_POWER_STARTUP, NULL);
644       
645        ortp_init();
646        rtp_profile_set_payload(&av_profile,115,&payload_type_lpc1015);
647        rtp_profile_set_payload(&av_profile,110,&payload_type_speex_nb);
648        rtp_profile_set_payload(&av_profile,111,&payload_type_speex_wb);
649        rtp_profile_set_payload(&av_profile,112,&payload_type_ilbc);
650        rtp_profile_set_payload(&av_profile,116,&payload_type_truespeech);
651        rtp_profile_set_payload(&av_profile,101,&payload_type_telephone_event);
652       
653#ifdef VIDEO_ENABLED
654        rtp_profile_set_payload(&av_profile,97,&payload_type_theora);
655
656        linphone_h263_1998=payload_type_clone(&payload_type_h263_1998);
657        payload_type_set_recv_fmtp(linphone_h263_1998,"CIF=1;QCIF=1");
658        rtp_profile_set_payload(&av_profile,98,linphone_h263_1998);
659
660        linphone_mp4v_es=payload_type_clone(&payload_type_mp4v);
661        payload_type_set_recv_fmtp(linphone_mp4v_es,"profile-level-id=3");
662        rtp_profile_set_payload(&av_profile,99,linphone_mp4v_es);
663        rtp_profile_set_payload(&av_profile,100,&payload_type_x_snow);
664        payload_type_h264_packetization_mode_1=payload_type_clone(&payload_type_h264);
665        payload_type_set_recv_fmtp(payload_type_h264_packetization_mode_1,"packetization-mode=1");
666        rtp_profile_set_payload(&av_profile,103,payload_type_h264_packetization_mode_1);
667        rtp_profile_set_payload(&av_profile,102,&payload_type_h264);
668#endif
669
670        ms_init();
671       
672        lc->config=lp_config_new(config_path);
673 
674#ifdef VINCENT_MAURY_RSVP
675        /* default qos parameters : rsvp on, rpc off */
676        lc->rsvp_enable = 1;
677        lc->rpc_enable = 0;
678#endif
679        sound_config_read(lc);
680        net_config_read(lc);
681        rtp_config_read(lc);
682        codecs_config_read(lc);
683        sip_config_read(lc); /* this will start eXosip*/
684        video_config_read(lc);
685        //autoreplier_config_init(&lc->autoreplier_conf);
686        lc->prev_mode=LINPHONE_STATUS_ONLINE;
687        lc->presence_mode=LINPHONE_STATUS_ONLINE;
688        lc->max_call_logs=15;
689        ui_config_read(lc);
690        ms_mutex_init(&lc->lock,NULL);
691        lc->vtable.display_status(lc,_("Ready"));
692        gstate_new_state(lc, GSTATE_POWER_ON, NULL);
693}
694
695LinphoneCore *linphone_core_new(const LinphoneCoreVTable *vtable,
696                                                const char *config_path, void * userdata)
697{
698        LinphoneCore *core=ms_new(LinphoneCore,1);
699        linphone_core_init(core,vtable,config_path,userdata);
700        return core;
701}
702
703const MSList *linphone_core_get_audio_codecs(const LinphoneCore *lc)
704{
705        return lc->codecs_conf.audio_codecs;
706}
707
708const MSList *linphone_core_get_video_codecs(const LinphoneCore *lc)
709{
710        return lc->codecs_conf.video_codecs;
711}
712
713int linphone_core_set_primary_contact(LinphoneCore *lc, const char *contact)
714{
715        osip_from_t *ctt=NULL;
716        osip_from_init(&ctt);
717        if (osip_from_parse(ctt,contact)!=0){
718                ms_error("Bad contact url: %s",contact);
719                osip_from_free(ctt);
720                return -1;
721        }
722        if (lc->sip_conf.contact!=NULL) ms_free(lc->sip_conf.contact);
723        lc->sip_conf.contact=ms_strdup(contact);
724        if (lc->sip_conf.guessed_contact!=NULL){
725                ms_free(lc->sip_conf.guessed_contact);
726                lc->sip_conf.guessed_contact=NULL;
727        }
728        osip_from_free(ctt);
729        return 0;
730}
731
732
733/*result must be an array of chars at least LINPHONE_IPADDR_SIZE */
734void linphone_core_get_local_ip(LinphoneCore *lc, const char *dest, char *result){
735        if (lc->apply_nat_settings){
736                apply_nat_settings(lc);
737                lc->apply_nat_settings=FALSE;
738        }
739        if (linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_NAT_ADDRESS){
740                strncpy(result,linphone_core_get_nat_address(lc),LINPHONE_IPADDR_SIZE);
741                return;
742        }
743        if (linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_STUN) {
744                if (lc->sip_conf.ipv6_enabled){
745                        ms_warning("stun support is not implemented for ipv6");
746                }else{
747                        /* we no more use stun for sip socket*/
748#if 0
749                        int mport=0;
750                        ms_message("doing stun lookup for local address...");
751                        if (stun_get_localip(lc,sock,linphone_core_get_sip_port(lc),result,&mport)){
752                                if (!lc->net_conf.nat_sdp_only)
753                                        eXosip_masquerade_contact(result,mport);
754                                return;
755                        }
756                        ms_warning("stun lookup failed, falling back to a local interface...");
757#endif
758                }
759               
760        }
761        if (eXosip_guess_localip(lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,result,LINPHONE_IPADDR_SIZE)<0){
762                /*default to something */
763                strncpy(result,lc->sip_conf.ipv6_enabled ? "::1" : "127.0.0.1",LINPHONE_IPADDR_SIZE);
764                ms_error("Could not find default routable ip address !"); 
765        }       
766        eXosip_masquerade_contact(NULL,0);
767}
768
769const char *linphone_core_get_primary_contact(LinphoneCore *lc)
770{
771        char *identity;
772        char tmp[LINPHONE_IPADDR_SIZE];
773        if (lc->sip_conf.guess_hostname){
774                if (lc->sip_conf.guessed_contact==NULL || lc->sip_conf.loopback_only){
775                        char *guessed=NULL;
776                        osip_from_t *url;
777                        if (lc->sip_conf.guessed_contact!=NULL){
778                                ms_free(lc->sip_conf.guessed_contact);
779                                lc->sip_conf.guessed_contact=NULL;
780                        }
781                       
782                        osip_from_init(&url);
783                        if (osip_from_parse(url,lc->sip_conf.contact)==0){
784                               
785                        }else ms_error("Could not parse identity contact !");
786                        linphone_core_get_local_ip(lc, NULL, tmp);
787                        if (strcmp(tmp,"127.0.0.1")==0 || strcmp(tmp,"::1")==0 ){
788                                ms_warning("Local loopback network only !");
789                                lc->sip_conf.loopback_only=TRUE;
790                        }else lc->sip_conf.loopback_only=FALSE;
791                        osip_free(url->url->host);
792                        url->url->host=osip_strdup(tmp);
793                        if (url->url->port!=NULL){
794                                osip_free(url->url->port);
795                                url->url->port=NULL;
796                        }
797                        if (lc->sip_conf.sip_port!=5060){
798                                url->url->port=ortp_strdup_printf("%i",lc->sip_conf.sip_port);
799                        }
800                        osip_from_to_str(url,&guessed);
801                        lc->sip_conf.guessed_contact=guessed;
802                       
803                        osip_from_free(url);
804                       
805                }
806                identity=lc->sip_conf.guessed_contact;
807        }else{
808                identity=lc->sip_conf.contact;
809        }
810        return identity;
811}
812
813void linphone_core_set_guess_hostname(LinphoneCore *lc, bool_t val){
814        lc->sip_conf.guess_hostname=val;
815}
816       
817bool_t linphone_core_get_guess_hostname(LinphoneCore *lc){
818        return lc->sip_conf.guess_hostname;
819}
820
821osip_from_t *linphone_core_get_primary_contact_parsed(LinphoneCore *lc){
822        int err;
823        osip_from_t *contact;
824        osip_from_init(&contact);
825        err=osip_from_parse(contact,linphone_core_get_primary_contact(lc));
826        if (err<0) {
827                osip_from_free(contact);
828                return NULL;
829        }
830        return contact;
831}
832
833int linphone_core_set_audio_codecs(LinphoneCore *lc, MSList *codecs)
834{
835        if (lc->codecs_conf.audio_codecs!=NULL) ms_list_free(lc->codecs_conf.audio_codecs);
836        lc->codecs_conf.audio_codecs=codecs;
837        return 0;
838}
839
840int linphone_core_set_video_codecs(LinphoneCore *lc, MSList *codecs)
841{
842        if (lc->codecs_conf.video_codecs!=NULL) ms_list_free(lc->codecs_conf.video_codecs);
843        lc->codecs_conf.video_codecs=codecs;
844        return 0;
845}
846
847const MSList * linphone_core_get_friend_list(LinphoneCore *lc)
848{
849        return lc->friends;
850}
851
852int linphone_core_get_audio_jittcomp(LinphoneCore *lc)
853{
854        return lc->rtp_conf.audio_jitt_comp;
855}
856
857int linphone_core_get_audio_port(const LinphoneCore *lc)
858{
859        return lc->rtp_conf.audio_rtp_port;
860}
861
862int linphone_core_get_video_port(const LinphoneCore *lc){
863        return lc->rtp_conf.video_rtp_port;
864}
865
866int linphone_core_get_nortp_timeout(const LinphoneCore *lc){
867        return lc->rtp_conf.nortp_timeout;
868}
869
870void linphone_core_set_audio_jittcomp(LinphoneCore *lc, int value)
871{
872        lc->rtp_conf.audio_jitt_comp=value;
873}
874
875void linphone_core_set_audio_port(LinphoneCore *lc, int port)
876{
877        lc->rtp_conf.audio_rtp_port=port;
878}
879
880void linphone_core_set_video_port(LinphoneCore *lc, int port){
881        lc->rtp_conf.video_rtp_port=port;
882}
883
884void linphone_core_set_nortp_timeout(LinphoneCore *lc, int nortp_timeout){
885        lc->rtp_conf.nortp_timeout=nortp_timeout;
886}
887
888bool_t linphone_core_get_use_info_for_dtmf(LinphoneCore *lc)
889{
890        return lc->sip_conf.use_info;
891}
892
893void linphone_core_set_use_info_for_dtmf(LinphoneCore *lc,bool_t use_info)
894{
895        lc->sip_conf.use_info=use_info;
896}
897
898int linphone_core_get_sip_port(LinphoneCore *lc)
899{
900        return lc->sip_conf.sip_port;
901}
902
903static bool_t exosip_running=FALSE;
904
905void linphone_core_set_sip_port(LinphoneCore *lc,int port)
906{
907        const char *anyaddr;
908        char ua_string[256];
909        int err=0;
910        if (port==lc->sip_conf.sip_port) return;
911        lc->sip_conf.sip_port=port;
912        if (exosip_running) eXosip_quit();
913        eXosip_init();
914        eXosip_enable_ipv6(lc->sip_conf.ipv6_enabled);
915        if (lc->sip_conf.ipv6_enabled)
916                anyaddr="::0";
917        else
918                anyaddr="0.0.0.0";
919        err=eXosip_listen_addr (IPPROTO_UDP, anyaddr, port,
920                lc->sip_conf.ipv6_enabled ?  PF_INET6 : PF_INET, 0);
921        if (err<0){
922                char *msg=ortp_strdup_printf("UDP port %i seems already in use ! Cannot initialize.",port);
923                ms_warning(msg);
924                lc->vtable.display_warning(lc,msg);
925                ms_free(msg);
926                return;
927        }
928#ifdef VINCENT_MAURY_RSVP
929        /* tell exosip the qos settings according to default linphone parameters */
930        eXosip_set_rsvp_mode (lc->rsvp_enable);
931        eXosip_set_rpc_mode (lc->rpc_enable);
932#endif
933        snprintf(ua_string,sizeof(ua_string),"Linphone/%s (eXosip2/%s)",LINPHONE_VERSION,
934#ifdef HAVE_EXOSIP_GET_VERSION
935                 eXosip_get_version()
936#else
937                 "unknown"
938#endif
939);
940        eXosip_set_user_agent(ua_string);
941        exosip_running=TRUE;
942}
943
944bool_t linphone_core_ipv6_enabled(LinphoneCore *lc){
945        return lc->sip_conf.ipv6_enabled;
946}
947void linphone_core_enable_ipv6(LinphoneCore *lc, bool_t val){
948        if (lc->sip_conf.ipv6_enabled!=val){
949                lc->sip_conf.ipv6_enabled=val;
950                if (exosip_running){
951                        /* we need to restart eXosip */
952                        linphone_core_set_sip_port(lc, lc->sip_conf.sip_port);
953                }
954        }
955}
956
957static void display_bandwidth(RtpSession *as, RtpSession *vs){
958        ms_message("bandwidth usage: audio=[d=%.1f,u=%.1f] video=[d=%.1f,u=%.1f] kbit/sec",
959        (as!=NULL) ? (rtp_session_compute_recv_bandwidth(as)*1e-3) : 0,
960        (as!=NULL) ? (rtp_session_compute_send_bandwidth(as)*1e-3) : 0,
961        (vs!=NULL) ? (rtp_session_compute_recv_bandwidth(vs)*1e-3) : 0,
962        (vs!=NULL) ? (rtp_session_compute_send_bandwidth(vs)*1e-3) : 0);
963}
964
965static void linphone_core_disconnected(LinphoneCore *lc){
966        lc->vtable.display_warning(lc,_("Remote end seems to have disconnected, the call is going to be closed."));
967        linphone_core_terminate_call(lc,NULL);
968}
969
970void linphone_core_iterate(LinphoneCore *lc)
971{
972        eXosip_event_t *ev;
973        bool_t disconnected=FALSE;
974        int disconnect_timeout = linphone_core_get_nortp_timeout(lc); 
975        if (lc->preview_finished){
976                lc->preview_finished=0;
977                ring_stop(lc->ringstream);
978                lc->ringstream=NULL;
979                lc_callback_obj_invoke(&lc->preview_finished_cb,lc);
980        }
981       
982        if (exosip_running){
983                while((ev=eXosip_event_wait(0,0))!=NULL){
984                        linphone_core_process_event(lc,ev);
985                }
986                if (lc->automatic_action==0) {
987                        eXosip_lock();
988                        eXosip_automatic_action();
989                        eXosip_unlock();
990                }
991        }
992        if (lc->call!=NULL){
993                LinphoneCall *call=lc->call;
994                int elapsed;
995                time_t curtime=time(NULL);
996                if (call->dir==LinphoneCallIncoming && call->state==LCStateRinging){
997                        elapsed=curtime-call->start_time;
998                        ms_message("incoming call ringing for %i seconds",elapsed);
999                        if (elapsed>lc->sip_conf.inc_timeout){
1000                                linphone_core_terminate_call(lc,NULL);
1001                        }
1002                }else if (call->state==LCStateAVRunning){
1003                        elapsed=curtime-lc->prevtime;
1004                        if (elapsed>=1){
1005                                RtpSession *as=NULL,*vs=NULL;
1006                                lc->prevtime=curtime;
1007                                if (lc->audiostream!=NULL)
1008                                        as=lc->audiostream->session;
1009                                if (lc->videostream!=NULL)
1010                                        vs=lc->videostream->session;
1011                                display_bandwidth(as,vs);
1012                        }
1013#ifdef VIDEO_ENABLED
1014                        if (lc->videostream!=NULL)
1015                                video_stream_iterate(lc->videostream);
1016#endif
1017                        if (lc->audiostream!=NULL && disconnect_timeout>0)
1018                                disconnected=!audio_stream_alive(lc->audiostream,disconnect_timeout);
1019                }
1020        }
1021        if (linphone_core_video_preview_enabled(lc)){
1022                if (lc->previewstream==NULL)
1023                        toggle_video_preview(lc,TRUE);
1024#ifdef VIDEO_ENABLED
1025                else video_stream_iterate(lc->previewstream);
1026#endif
1027        }else{
1028                if (lc->previewstream!=NULL)
1029                        toggle_video_preview(lc,FALSE);
1030        }
1031        if (disconnected)
1032                linphone_core_disconnected(lc);
1033}
1034
1035
1036bool_t linphone_core_is_in_main_thread(LinphoneCore *lc){
1037        return TRUE;
1038}
1039
1040static osip_to_t *osip_to_create(const char *to){
1041        osip_to_t *ret;
1042        osip_to_init(&ret);
1043        if (osip_to_parse(ret,to)<0){
1044                osip_to_free(ret);
1045                return NULL;
1046        }
1047        return ret;
1048}
1049
1050bool_t linphone_core_interpret_url(LinphoneCore *lc, const char *url, char **real_url, osip_to_t **real_parsed_url, char **route){
1051        enum_lookup_res_t *enumres=NULL;
1052        osip_to_t *parsed_url=NULL;
1053        char *enum_domain=NULL;
1054        LinphoneProxyConfig *proxy;
1055        char *tmpurl;
1056        const char *tmproute;
1057        if (real_url!=NULL) *real_url=NULL;
1058        if (real_parsed_url!=NULL) *real_parsed_url=NULL;
1059        *route=NULL;
1060        tmproute=linphone_core_get_route(lc);
1061       
1062        if (is_enum(url,&enum_domain)){
1063                lc->vtable.display_status(lc,_("Looking for telephone number destination..."));
1064                if (enum_lookup(enum_domain,&enumres)<0){
1065                        lc->vtable.display_status(lc,_("Could not resolve this number."));
1066                        ms_free(enum_domain);
1067                        return FALSE;
1068                }
1069                ms_free(enum_domain);
1070                tmpurl=enumres->sip_address[0];
1071                if (real_url!=NULL) *real_url=ms_strdup(tmpurl);
1072                if (real_parsed_url!=NULL) *real_parsed_url=osip_to_create(tmpurl);
1073                enum_lookup_res_free(enumres);
1074                if (tmproute) *route=ms_strdup(tmproute);
1075                return TRUE;
1076        }
1077        /* check if we have a "sip:" */
1078        if (strstr(url,"sip:")==NULL){
1079                /* this doesn't look like a true sip uri */
1080                proxy=lc->default_proxy;
1081                if (proxy!=NULL){
1082                        /* append the proxy domain suffix */
1083                        osip_from_t *uri;
1084                        char *sipaddr;
1085                        const char *identity=linphone_proxy_config_get_identity(proxy);
1086                        osip_from_init(&uri);
1087                        if (osip_from_parse(uri,identity)<0){
1088                                osip_from_free(uri);
1089                                return FALSE;
1090                        }
1091                        sipaddr=ortp_strdup_printf("sip:%s@%s",url,uri->url->host);
1092                        if (real_parsed_url!=NULL) *real_parsed_url=osip_to_create(sipaddr);
1093                        if (real_url!=NULL) *real_url=sipaddr;
1094                        else ms_free(sipaddr);
1095#if 0
1096                        /*if the prompted uri was auto-suffixed with proxy domain,
1097                        then automatically set a route so that the request goes
1098                        through the proxy*/
1099                        if (tmproute==NULL){
1100                                osip_route_t *rt=NULL;
1101                                char *rtstr=NULL;
1102                                osip_route_init(&rt);
1103                                if (osip_route_parse(rt,linphone_proxy_config_get_addr(proxy))==0){
1104                                        osip_uri_uparam_add(rt->url,osip_strdup("lr"),NULL);
1105                                        osip_route_to_str(rt,&rtstr);
1106                                        *route=ms_strdup(rtstr);
1107                                        osip_free(rtstr);
1108                                }
1109                                ms_message("setting automatically a route to %s",*route);
1110                        }
1111                        else *route=ms_strdup(tmproute);
1112#else
1113                        if (tmproute) *route=ms_strdup(tmproute);
1114#endif
1115                        return TRUE;
1116                }
1117        }
1118        parsed_url=osip_to_create(url);
1119        if (parsed_url!=NULL){
1120                if (real_url!=NULL) *real_url=ms_strdup(url);
1121                if (real_parsed_url!=NULL) *real_parsed_url=parsed_url;
1122                else osip_to_free(parsed_url);
1123                if (tmproute) *route=ms_strdup(tmproute);
1124                return TRUE;
1125        }
1126        /* else we could not do anything with url given by user, so display an error */
1127        if (lc->vtable.display_warning!=NULL){
1128                lc->vtable.display_warning(lc,_("Could not parse given sip address. A sip url usually looks like sip:user@domain"));
1129        }
1130        return FALSE;
1131}
1132
1133const char * linphone_core_get_identity(LinphoneCore *lc){
1134        LinphoneProxyConfig *proxy=NULL;
1135        const char *from;
1136        linphone_core_get_default_proxy(lc,&proxy);
1137        if (proxy!=NULL) {
1138                from=linphone_proxy_config_get_identity(proxy);
1139        }else from=linphone_core_get_primary_contact(lc);
1140        return from;
1141}
1142
1143const char * linphone_core_get_route(LinphoneCore *lc){
1144        LinphoneProxyConfig *proxy=NULL;
1145        const char *route=NULL;
1146        linphone_core_get_default_proxy(lc,&proxy);
1147        if (proxy!=NULL) {
1148                route=linphone_proxy_config_get_route(proxy);
1149        }
1150        return route;
1151}
1152
1153void linphone_set_sdp(osip_message_t *sip, const char *sdpmesg){
1154        int sdplen=strlen(sdpmesg);
1155        char clen[10];
1156        snprintf(clen,sizeof(clen),"%i",sdplen);
1157        osip_message_set_body(sip,sdpmesg,sdplen);
1158        osip_message_set_content_type(sip,"application/sdp");
1159        osip_message_set_content_length(sip,clen);
1160}
1161
1162int linphone_core_invite(LinphoneCore *lc, const char *url)
1163{
1164        char *barmsg;
1165        int err=0;
1166        char *sdpmesg=NULL;
1167        char *route=NULL;
1168        const char *from=NULL;
1169        osip_message_t *invite=NULL;
1170        sdp_context_t *ctx=NULL;
1171        LinphoneProxyConfig *proxy=NULL;
1172        osip_from_t *parsed_url2=NULL;
1173        osip_to_t *real_parsed_url=NULL;
1174        char *real_url=NULL;
1175       
1176        if (lc->call!=NULL){
1177                lc->vtable.display_warning(lc,_("Sorry, having multiple simultaneous calls is not supported yet !"));
1178                return -1;
1179        }
1180
1181        gstate_new_state(lc, GSTATE_CALL_OUT_INVITE, url);
1182        linphone_core_get_default_proxy(lc,&proxy);
1183        if (!linphone_core_interpret_url(lc,url,&real_url,&real_parsed_url,&route)){
1184                /* bad url */
1185                gstate_new_state(lc, GSTATE_CALL_ERROR, NULL);
1186                return -1;
1187        }
1188        if (proxy!=NULL) {
1189                from=linphone_proxy_config_get_identity(proxy);
1190               
1191        }
1192        /* if no proxy or no identity defined for this proxy, default to primary contact*/
1193        if (from==NULL) from=linphone_core_get_primary_contact(lc);
1194
1195        err=eXosip_call_build_initial_invite(&invite,real_url,from,
1196                                                route,"Phone call");
1197
1198        if (err<0){
1199                ms_warning("Could not build initial invite");
1200                goto end;
1201        }
1202       
1203        /* make sdp message */
1204       
1205        osip_from_init(&parsed_url2);
1206        osip_from_parse(parsed_url2,from);
1207       
1208        lc->call=linphone_call_new_outgoing(lc,parsed_url2,real_parsed_url);
1209        barmsg=ortp_strdup_printf("%s %s", _("Contacting"), real_url);
1210        lc->vtable.display_status(lc,barmsg);
1211        ms_free(barmsg);
1212        if (!lc->sip_conf.sdp_200_ack){
1213                ctx=lc->call->sdpctx;
1214                sdpmesg=sdp_context_get_offer(ctx);
1215                linphone_set_sdp(invite,sdpmesg);
1216                linphone_core_init_media_streams(lc);
1217        }
1218        eXosip_lock();
1219        err=eXosip_call_send_initial_invite(invite);
1220        lc->call->cid=err;
1221        eXosip_unlock();
1222        if (err<0){
1223                ms_warning("Could not initiate call.");
1224                lc->vtable.display_status(lc,_("could not call"));
1225                linphone_call_destroy(lc->call);
1226                lc->call=NULL;
1227                linphone_core_stop_media_streams(lc);
1228        }
1229       
1230        goto end;
1231        end:
1232                if (real_url!=NULL) ms_free(real_url);
1233                if (real_parsed_url!=NULL) osip_to_free(real_parsed_url);
1234                if (parsed_url2!=NULL) osip_from_free(parsed_url2);
1235                if (err<0)
1236                        gstate_new_state(lc, GSTATE_CALL_ERROR, NULL);
1237                if (route!=NULL) ms_free(route);
1238        return (err<0) ? -1 : 0;
1239}
1240
1241int linphone_core_refer(LinphoneCore *lc, const char *url)
1242{
1243        char *real_url=NULL;
1244        osip_to_t *real_parsed_url=NULL;
1245        LinphoneCall *call;
1246        osip_message_t *msg=NULL;
1247        char *route;
1248        if (!linphone_core_interpret_url(lc,url,&real_url,&real_parsed_url, &route)){
1249                /* bad url */
1250                return -1;
1251        }
1252        if (route!=NULL) ms_free(route);
1253        call=lc->call;
1254        if (call==NULL){
1255                ms_warning("No established call to refer.");
1256                return -1;
1257        }
1258        lc->call=NULL;
1259        eXosip_call_build_refer(call->did, real_url, &msg);
1260        eXosip_lock();
1261        eXosip_call_send_request(call->did, msg);
1262        eXosip_unlock();
1263        return 0;
1264}
1265
1266bool_t linphone_core_inc_invite_pending(LinphoneCore*lc){
1267        if (lc->call!=NULL && lc->call->dir==LinphoneCallIncoming){
1268                return TRUE;
1269        }
1270        return FALSE;
1271}
1272
1273#ifdef VINCENT_MAURY_RSVP
1274/* on=1 for RPC_ENABLE=1...*/
1275int linphone_core_set_rpc_mode(LinphoneCore *lc, int on)
1276{
1277        if (on==1)
1278                printf("RPC_ENABLE set on\n");
1279        else 
1280                printf("RPC_ENABLE set off\n");
1281        lc->rpc_enable = (on==1);
1282        /* need to tell eXosip the new setting */
1283        if (eXosip_set_rpc_mode (lc->rpc_enable)!=0)
1284                return -1;
1285        return 0;
1286}
1287
1288/* on=1 for RSVP_ENABLE=1...*/
1289int linphone_core_set_rsvp_mode(LinphoneCore *lc, int on)
1290{
1291        if (on==1)
1292                printf("RSVP_ENABLE set on\n");
1293        else 
1294                printf("RSVP_ENABLE set off\n");
1295        lc->rsvp_enable = (on==1);
1296        /* need to tell eXosip the new setting */
1297        if (eXosip_set_rsvp_mode (lc->rsvp_enable)!=0)
1298                return -1;
1299        return 0;
1300}
1301
1302/* answer : 1 for yes, 0 for no */
1303int linphone_core_change_qos(LinphoneCore *lc, int answer)
1304{
1305        char *sdpmesg;
1306        if (lc->call==NULL){
1307                return -1;
1308        }
1309       
1310        if (lc->rsvp_enable && answer==1)
1311        {
1312                /* answer is yes, local setting is with qos, so
1313                 * the user chose to continue with no qos ! */
1314                /* so switch in normal mode : ring and 180 */
1315                lc->rsvp_enable = 0; /* no more rsvp */
1316                eXosip_set_rsvp_mode (lc->rsvp_enable);
1317                /* send 180 */
1318                eXosip_lock();
1319                eXosip_answer_call(lc->call->did,180,NULL);
1320                eXosip_unlock();
1321                /* play the ring */
1322                ms_message("Starting local ring...");
1323                lc->ringstream=ring_start(lc->sound_conf.local_ring,
1324                                        2000,ms_snd_card_manager_get_card(ms_snd_card_manager_get(),lc->sound_conf.ring_sndcard));
1325        }
1326        else if (!lc->rsvp_enable && answer==1)
1327        {
1328                /* switch to QoS mode on : answer 183 session progress */
1329                lc->rsvp_enable = 1;
1330                eXosip_set_rsvp_mode (lc->rsvp_enable);
1331                /* take the sdp already computed, see osipuacb.c */
1332                sdpmesg=lc->call->sdpctx->answerstr;
1333                eXosip_lock();
1334                eXosip_answer_call_with_body(lc->call->did,183,"application/sdp",sdpmesg);
1335                eXosip_unlock();
1336        }
1337        else
1338        {
1339                /* decline offer (603) */
1340                linphone_core_terminate_call(lc, NULL);
1341        }
1342        return 0;
1343}
1344#endif
1345
1346void linphone_core_init_media_streams(LinphoneCore *lc){
1347        lc->audiostream=audio_stream_new(linphone_core_get_audio_port(lc),linphone_core_ipv6_enabled(lc));
1348#ifdef VIDEO_ENABLED
1349        if (lc->video_conf.display || lc->video_conf.capture)
1350                lc->videostream=video_stream_new(linphone_core_get_video_port(lc),linphone_core_ipv6_enabled(lc));
1351#else
1352        lc->videostream=NULL;
1353#endif
1354}
1355
1356static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
1357        LinphoneCore* lc = (LinphoneCore*)user_data;
1358        if (lc->vtable.dtmf_received != NULL)
1359                lc->vtable.dtmf_received(lc, dtmf);
1360}
1361
1362void linphone_core_start_media_streams(LinphoneCore *lc, LinphoneCall *call){
1363        osip_from_t *me=linphone_core_get_primary_contact_parsed(lc);
1364        const char *tool="linphone-" LINPHONE_VERSION;
1365        /* adjust rtp jitter compensation. It must be at least the latency of the sound card */
1366        int jitt_comp=MAX(lc->sound_conf.latency,lc->rtp_conf.audio_jitt_comp);
1367        char *cname=ortp_strdup_printf("%s@%s",me->url->username,me->url->host);
1368        {
1369                StreamParams *audio_params=&call->audio_params;
1370                if (!lc->use_files){
1371                        MSSndCard *playcard=lc->sound_conf.play_sndcard;
1372                        MSSndCard *captcard=lc->sound_conf.capt_sndcard;
1373                        if (playcard==NULL) {
1374                                ms_warning("No card defined for playback !");
1375                                goto end;
1376                        }
1377                        if (captcard==NULL) {
1378                                ms_warning("No card defined for capture !");
1379                                goto end;
1380                        }
1381                        if (audio_params->relay_session_id!=NULL) 
1382                                audio_stream_set_relay_session_id(lc->audiostream,audio_params->relay_session_id);
1383                        audio_stream_start_now(
1384                                lc->audiostream,
1385                                call->profile,
1386                                audio_params->remoteaddr,
1387                                audio_params->remoteport,
1388                                audio_params->remotertcpport,
1389                                audio_params->pt,
1390                                jitt_comp,
1391                                playcard,
1392                                captcard,
1393                                linphone_core_echo_cancelation_enabled(lc));
1394                }else{
1395                        audio_stream_start_with_files(
1396                                lc->audiostream,
1397                                call->profile,
1398                                audio_params->remoteaddr,
1399                                audio_params->remoteport,
1400                                audio_params->remotertcpport,
1401                                audio_params->pt,
1402                                100,
1403                                lc->play_file,
1404                                lc->rec_file);
1405                }
1406                if (lc->vtable.dtmf_received!=NULL){
1407                        /* replace by our default action*/
1408                        audio_stream_play_received_dtmfs(lc->audiostream,FALSE);
1409                        rtp_session_signal_connect(lc->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
1410                }
1411                audio_stream_set_rtcp_information(lc->audiostream, cname, tool);
1412        }
1413#ifdef VIDEO_ENABLED
1414        {
1415                /* shutdown preview */
1416                if (lc->previewstream!=NULL) {
1417                        video_preview_stop(lc->previewstream);
1418                        lc->previewstream=NULL;
1419                }
1420                if (lc->video_conf.display || lc->video_conf.capture) {
1421                        StreamParams *video_params=&call->video_params;
1422                       
1423                        if (video_params->remoteport>0){
1424                                if (video_params->relay_session_id!=NULL) 
1425                                        video_stream_set_relay_session_id(lc->videostream,video_params->relay_session_id);
1426                                video_stream_set_sent_video_size(lc->videostream,linphone_core_get_preferred_video_size(lc));
1427                                if (lc->video_conf.display && lc->video_conf.capture)
1428                                        video_stream_start(lc->videostream,
1429                                        call->profile, video_params->remoteaddr, video_params->remoteport,
1430                                        video_params->remotertcpport,
1431                                        video_params->pt, jitt_comp, lc->video_conf.device);
1432                                else if (lc->video_conf.display)
1433                                        video_stream_recv_only_start(lc->videostream,
1434                                        call->profile, video_params->remoteaddr, video_params->remoteport,
1435                                        video_params->pt, jitt_comp);
1436                                else if (lc->video_conf.capture)
1437                                        video_stream_send_only_start(lc->videostream,
1438                                        call->profile, video_params->remoteaddr, video_params->remoteport,
1439                                        video_params->remotertcpport,
1440                                        video_params->pt, jitt_comp, lc->video_conf.device);
1441                                video_stream_set_rtcp_information(lc->videostream, cname,tool);
1442                        }
1443                }
1444        }
1445#endif
1446        goto end;
1447        end:
1448        ms_free(cname);
1449        osip_from_free(me);
1450        lc->call->state=LCStateAVRunning;
1451}
1452
1453void linphone_core_stop_media_streams(LinphoneCore *lc){
1454        if (lc->audiostream!=NULL) {
1455                audio_stream_stop(lc->audiostream);
1456                lc->audiostream=NULL;
1457        }
1458#ifdef VIDEO_ENABLED
1459        if (lc->videostream!=NULL){
1460                if (lc->video_conf.display && lc->video_conf.capture)
1461                        video_stream_stop(lc->videostream);
1462                else if (lc->video_conf.display)
1463                        video_stream_recv_only_stop(lc->videostream);
1464                else if (lc->video_conf.capture)
1465                        video_stream_send_only_stop(lc->videostream);
1466                lc->videostream=NULL;
1467        }
1468        if (linphone_core_video_preview_enabled(lc)){
1469                if (lc->previewstream==NULL){
1470                        lc->previewstream=video_preview_start(lc->video_conf.device, lc->video_conf.vsize);
1471                }
1472        }
1473#endif
1474}
1475
1476int linphone_core_accept_call(LinphoneCore *lc, const char *url)
1477{
1478        char *sdpmesg;
1479        osip_message_t *msg=NULL;
1480        LinphoneCall *call=lc->call;
1481        int err;
1482        bool_t offering=FALSE;
1483       
1484        if (call==NULL){
1485                return -1;
1486        }
1487       
1488        if (lc->call->state==LCStateAVRunning){
1489                /*call already accepted*/
1490                return -1;
1491        }
1492
1493        /*stop ringing */
1494        if (lc->ringstream!=NULL) {
1495                ms_message("stop ringing");
1496                ring_stop(lc->ringstream);
1497                ms_message("ring stopped");
1498                lc->ringstream=NULL;
1499        }
1500        /* sends a 200 OK */
1501        err=eXosip_call_build_answer(call->tid,200,&msg);
1502        if (err<0 || msg==NULL){
1503                ms_error("Fail to build answer for call: err=%i",err);
1504                return -1;
1505        }
1506        /*if a sdp answer is computed, send it, else send an offer */
1507        sdpmesg=call->sdpctx->answerstr;
1508        if (sdpmesg==NULL){
1509                offering=TRUE;
1510                ms_message("generating sdp offer");
1511                sdpmesg=sdp_context_get_offer(call->sdpctx);
1512               
1513                if (sdpmesg==NULL){
1514                        ms_error("fail to generate sdp offer !");
1515                        return -1;
1516                }
1517                linphone_set_sdp(msg,sdpmesg);
1518                linphone_core_init_media_streams(lc);
1519        }else{
1520                linphone_set_sdp(msg,sdpmesg);
1521        }
1522        eXosip_lock();
1523        eXosip_call_send_answer(call->tid,200,msg);
1524        eXosip_unlock();
1525        lc->vtable.display_status(lc,_("Connected."));
1526        gstate_new_state(lc, GSTATE_CALL_IN_CONNECTED, NULL);
1527       
1528        if (!offering) linphone_core_start_media_streams(lc, lc->call);
1529        ms_message("call answered.");
1530        return 0;
1531}
1532
1533int linphone_core_terminate_call(LinphoneCore *lc, const char *url)
1534{
1535        LinphoneCall *call=lc->call;
1536        if (call==NULL){
1537                return -1;
1538        }
1539        lc->call=NULL;
1540       
1541        eXosip_lock();
1542        eXosip_call_terminate(call->cid,call->did);
1543        eXosip_unlock();
1544       
1545        /*stop ringing*/
1546        if (lc->ringstream!=NULL) {
1547                ring_stop(lc->ringstream);
1548                lc->ringstream=NULL;
1549        }
1550        linphone_core_stop_media_streams(lc);
1551        lc->vtable.display_status(lc,_("Call ended") );
1552        gstate_new_state(lc, GSTATE_CALL_END, NULL);
1553        linphone_call_destroy(call);
1554        return 0;
1555}
1556
1557bool_t linphone_core_in_call(const LinphoneCore *lc){
1558        return lc->call!=NULL;
1559}
1560
1561int linphone_core_send_publish(LinphoneCore *lc,
1562                               LinphoneOnlineStatus presence_mode)
1563{
1564        const MSList *elem;
1565        for (elem=linphone_core_get_proxy_config_list(lc);elem!=NULL;elem=ms_list_next(elem)){
1566                LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
1567                if (cfg->publish) linphone_proxy_config_send_publish(cfg,presence_mode);
1568        }
1569        return 0;
1570}
1571
1572void linphone_core_set_inc_timeout(LinphoneCore *lc, int seconds){
1573        lc->sip_conf.inc_timeout=seconds;
1574}
1575
1576int linphone_core_get_inc_timeout(LinphoneCore *lc){
1577        return lc->sip_conf.inc_timeout;
1578}
1579
1580void linphone_core_set_presence_info(LinphoneCore *lc,int minutes_away,
1581                                                                                                        const char *contact,
1582                                                                                                        LinphoneOnlineStatus presence_mode)
1583{
1584        int contactok=-1;
1585        if (minutes_away>0) lc->minutes_away=minutes_away;
1586        if (contact!=NULL) {
1587                osip_from_t *url;
1588                osip_from_init(&url);
1589                contactok=osip_from_parse(url,contact);
1590                if (contactok>=0) {
1591                        ms_message("contact url is correct.");
1592                }
1593                osip_from_free(url);
1594               
1595        }
1596        if (contactok>=0){
1597                if (lc->alt_contact!=NULL) ms_free(lc->alt_contact);
1598                lc->alt_contact=ms_strdup(contact);
1599        }
1600        if (lc->presence_mode!=presence_mode){
1601                linphone_core_notify_all_friends(lc,presence_mode);
1602                /*
1603                   Improve the use of all LINPHONE_STATUS available.
1604                   !TODO Do not mix "presence status" with "answer status code"..
1605                   Use correct parameter to follow sip_if_match/sip_etag.
1606                 */
1607                linphone_core_send_publish(lc,presence_mode);
1608        }
1609        lc->prev_mode=lc->presence_mode;
1610        lc->presence_mode=presence_mode;
1611       
1612}
1613
1614LinphoneOnlineStatus linphone_core_get_presence_info(const LinphoneCore *lc){
1615        return lc->presence_mode;
1616}
1617
1618/* sound functions */
1619int linphone_core_get_play_level(LinphoneCore *lc)
1620{
1621        return lc->sound_conf.play_lev;
1622}
1623int linphone_core_get_ring_level(LinphoneCore *lc)
1624{
1625        return lc->sound_conf.ring_lev;
1626}
1627int linphone_core_get_rec_level(LinphoneCore *lc){
1628        return lc->sound_conf.rec_lev;
1629}
1630void linphone_core_set_ring_level(LinphoneCore *lc, int level){
1631        MSSndCard *sndcard;
1632        lc->sound_conf.ring_lev=level;
1633        sndcard=lc->sound_conf.ring_sndcard;
1634        if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_PLAYBACK,level);
1635}
1636
1637void linphone_core_set_play_level(LinphoneCore *lc, int level){
1638        MSSndCard *sndcard;
1639        lc->sound_conf.play_lev=level;
1640        sndcard=lc->sound_conf.play_sndcard;
1641        if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_PLAYBACK,level);
1642}
1643
1644void linphone_core_set_rec_level(LinphoneCore *lc, int level)
1645{
1646        MSSndCard *sndcard;
1647        lc->sound_conf.rec_lev=level;
1648        sndcard=lc->sound_conf.capt_sndcard;
1649        if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_CAPTURE,level);
1650}
1651
1652static MSSndCard *get_card_from_string_id(const char *devid, unsigned int cap){
1653        MSSndCard *sndcard=NULL;
1654        if (devid!=NULL){
1655                sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
1656                if (sndcard!=NULL && 
1657                        (ms_snd_card_get_capabilities(sndcard) & cap)==0 ){
1658                        ms_warning("%s card does not have the %s capability, ignoring.",
1659                                devid,
1660                                cap==MS_SND_CARD_CAP_CAPTURE ? "capture" : "playback");
1661                        sndcard=NULL;
1662                }
1663        }
1664        if (sndcard==NULL) {
1665                /* get a card that has read+write capabilities */
1666                sndcard=ms_snd_card_manager_get_default_card(ms_snd_card_manager_get());
1667                /* otherwise refine to the first card having the right capability*/
1668                if (sndcard==NULL){
1669                        const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
1670                        for(;elem!=NULL;elem=elem->next){
1671                                sndcard=(MSSndCard*)elem->data;
1672                                if (ms_snd_card_get_capabilities(sndcard) & cap) break;
1673                        }
1674                }
1675                if (sndcard==NULL){/*looks like a bug! take the first one !*/
1676                        const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
1677                        sndcard=(MSSndCard*)elem->data;
1678                }
1679        }
1680        if (sndcard==NULL) ms_error("Could not find a suitable soundcard !");
1681        return sndcard;
1682}
1683
1684bool_t linphone_core_sound_device_can_capture(LinphoneCore *lc, const char *devid){
1685        MSSndCard *sndcard;
1686        sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
1687        if (sndcard!=NULL && (ms_snd_card_get_capabilities(sndcard) & MS_SND_CARD_CAP_CAPTURE)) return TRUE;
1688        return FALSE;
1689}
1690
1691bool_t linphone_core_sound_device_can_playback(LinphoneCore *lc, const char *devid){
1692        MSSndCard *sndcard;
1693        sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
1694        if (sndcard!=NULL && (ms_snd_card_get_capabilities(sndcard) & MS_SND_CARD_CAP_PLAYBACK)) return TRUE;
1695        return FALSE;
1696}
1697
1698int linphone_core_set_ringer_device(LinphoneCore *lc, const char * devid){
1699        lc->sound_conf.ring_sndcard=get_card_from_string_id(devid,MS_SND_CARD_CAP_PLAYBACK);
1700        return 0;
1701}
1702
1703int linphone_core_set_playback_device(LinphoneCore *lc, const char * devid){
1704        lc->sound_conf.play_sndcard=get_card_from_string_id(devid,MS_SND_CARD_CAP_PLAYBACK);
1705        return 0;
1706}
1707
1708int linphone_core_set_capture_device(LinphoneCore *lc, const char * devid){
1709        lc->sound_conf.capt_sndcard=get_card_from_string_id(devid,MS_SND_CARD_CAP_CAPTURE);
1710        return 0;
1711}
1712
1713const char * linphone_core_get_ringer_device(LinphoneCore *lc)
1714{
1715        return ms_snd_card_get_string_id(lc->sound_conf.ring_sndcard);
1716}
1717
1718const char * linphone_core_get_playback_device(LinphoneCore *lc)
1719{
1720        return ms_snd_card_get_string_id(lc->sound_conf.play_sndcard);
1721}
1722
1723const char * linphone_core_get_capture_device(LinphoneCore *lc)
1724{
1725        return ms_snd_card_get_string_id(lc->sound_conf.capt_sndcard);
1726}
1727
1728/* returns a static array of string describing the sound devices */ 
1729const char**  linphone_core_get_sound_devices(LinphoneCore *lc){
1730        return lc->sound_conf.cards;
1731}
1732
1733const char**  linphone_core_get_video_devices(const LinphoneCore *lc){
1734        return lc->video_conf.cams;
1735}
1736
1737char linphone_core_get_sound_source(LinphoneCore *lc)
1738{
1739        return lc->sound_conf.source;
1740}
1741
1742void linphone_core_set_sound_source(LinphoneCore *lc, char source)
1743{
1744        MSSndCard *sndcard=lc->sound_conf.capt_sndcard;
1745        lc->sound_conf.source=source;
1746        if (!sndcard) return;
1747        switch(source){
1748                case 'm':
1749                        ms_snd_card_set_capture(sndcard,MS_SND_CARD_MIC);
1750                        break;
1751                case 'l':
1752                        ms_snd_card_set_capture(sndcard,MS_SND_CARD_LINE);
1753                        break;
1754        }
1755       
1756}
1757
1758void linphone_core_set_ring(LinphoneCore *lc,const char *path){
1759        if (lc->sound_conf.local_ring!=0){
1760                ms_free(lc->sound_conf.local_ring);
1761        }
1762        lc->sound_conf.local_ring=ms_strdup(path);
1763}
1764
1765const char *linphone_core_get_ring(LinphoneCore *lc){
1766        return lc->sound_conf.local_ring;
1767}
1768
1769static void notify_end_of_ring(void *ud ,unsigned int event, void * arg){
1770        LinphoneCore *lc=(LinphoneCore*)ud;
1771        lc->preview_finished=1;
1772}
1773
1774int linphone_core_preview_ring(LinphoneCore *lc, const char *ring,LinphoneCoreCbFunc func,void * userdata)
1775{
1776        if (lc->ringstream!=0){
1777                ms_warning("Cannot start ring now,there's already a ring being played");
1778                return -1;
1779        }
1780        lc_callback_obj_init(&lc->preview_finished_cb,func,userdata);
1781        lc->preview_finished=0;
1782        if (lc->sound_conf.ring_sndcard!=NULL){
1783                lc->ringstream=ring_start_with_cb(ring,2000,lc->sound_conf.ring_sndcard,notify_end_of_ring,(void *)lc);
1784        }
1785        return 0;
1786}
1787
1788
1789void linphone_core_set_ringback(LinphoneCore *lc,RingBackType type){
1790        switch(type){
1791                case RINGBACK_TYPE_FR:
1792                        lc->sound_conf.remote_ring=PACKAGE_SOUND_DIR "/" REMOTE_RING_FR;
1793                break;
1794                case RINGBACK_TYPE_US:
1795                        lc->sound_conf.remote_ring=PACKAGE_SOUND_DIR "/" REMOTE_RING_US;
1796                break;
1797        }
1798}
1799RingBackType linphone_core_get_ringback(LinphoneCore *lc);
1800
1801void linphone_core_enable_echo_cancelation(LinphoneCore *lc, bool_t val){
1802        lc->sound_conf.ec=val;
1803}
1804
1805bool_t linphone_core_echo_cancelation_enabled(LinphoneCore *lc){
1806        return lc->sound_conf.ec;
1807}
1808
1809
1810void linphone_core_send_dtmf(LinphoneCore *lc,char dtmf)
1811{
1812        if (linphone_core_get_use_info_for_dtmf(lc)==0){
1813                /* In Band DTMF */
1814                if (lc->audiostream!=NULL){
1815                        audio_stream_send_dtmf(lc->audiostream,dtmf);
1816                }
1817        }else{
1818                char dtmf_body[1000];
1819                char clen[10];
1820                osip_message_t *msg=NULL;
1821                /* Out of Band DTMF (use INFO method) */
1822                LinphoneCall *call=lc->call;
1823                if (call==NULL){
1824                        return;
1825                }
1826                eXosip_call_build_info(call->did,&msg);
1827                snprintf(dtmf_body, 999, "Signal=%c\r\nDuration=250\r\n", dtmf);
1828                osip_message_set_body(msg,dtmf_body,strlen(dtmf_body));
1829                osip_message_set_content_type(msg,"application/dtmf-relay");
1830                snprintf(clen,sizeof(clen),"%lu",(unsigned long)strlen(dtmf_body));
1831                osip_message_set_content_length(msg,clen);
1832               
1833                eXosip_lock();
1834                eXosip_call_send_request(call->did,msg);
1835                eXosip_unlock();
1836        }
1837}
1838
1839void linphone_core_set_stun_server(LinphoneCore *lc, const char *server){
1840        if (lc->net_conf.stun_server!=NULL)
1841                ms_free(lc->net_conf.stun_server);
1842        if (server)
1843                lc->net_conf.stun_server=ms_strdup(server);
1844        else lc->net_conf.stun_server=NULL;
1845        lc->apply_nat_settings=TRUE;
1846}
1847
1848const char * linphone_core_get_stun_server(const LinphoneCore *lc){
1849        return lc->net_conf.stun_server;
1850}
1851
1852const char * linphone_core_get_relay_addr(const LinphoneCore *lc){
1853        return lc->net_conf.relay;
1854}
1855
1856int linphone_core_set_relay_addr(LinphoneCore *lc, const char *addr){
1857        if (lc->net_conf.relay!=NULL){
1858                ms_free(lc->net_conf.relay);
1859                lc->net_conf.relay=NULL;
1860        }
1861        if (addr){
1862                lc->net_conf.relay=ms_strdup(addr);
1863        }
1864        return 0;
1865}
1866
1867static void apply_nat_settings(LinphoneCore *lc){
1868        char *wmsg;
1869        char *tmp=NULL;
1870        int err;
1871        struct addrinfo hints,*res;
1872        const char *addr=lc->net_conf.nat_address;
1873       
1874        if (lc->net_conf.firewall_policy==LINPHONE_POLICY_USE_NAT_ADDRESS){
1875                if (addr==NULL || strlen(addr)==0){
1876                        lc->vtable.display_warning(lc,_("No nat/firewall address supplied !"));
1877                        linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_NO_FIREWALL);
1878                }
1879                /*check the ip address given */
1880                memset(&hints,0,sizeof(struct addrinfo));
1881                if (lc->sip_conf.ipv6_enabled)
1882                        hints.ai_family=AF_INET6;
1883                else 
1884                        hints.ai_family=AF_INET;
1885                hints.ai_socktype = SOCK_DGRAM;
1886                err=getaddrinfo(addr,NULL,&hints,&res);
1887                if (err!=0){
1888                        wmsg=ortp_strdup_printf(_("Invalid nat address '%s' : %s"),
1889                                addr, gai_strerror(err));
1890                        ms_warning(wmsg); // what is this for ?
1891                        lc->vtable.display_warning(lc, wmsg);
1892                        ms_free(wmsg);
1893                        linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_NO_FIREWALL);
1894                        return;
1895                }
1896                /*now get it as an numeric ip address */
1897                tmp=ms_malloc0(50);
1898                err=getnameinfo(res->ai_addr,res->ai_addrlen,tmp,50,NULL,0,NI_NUMERICHOST);
1899                if (err!=0){
1900                        wmsg=ortp_strdup_printf(_("Invalid nat address '%s' : %s"),
1901                                addr, gai_strerror(err));
1902                        ms_warning(wmsg); // what is this for ?
1903                        lc->vtable.display_warning(lc, wmsg);
1904                        ms_free(wmsg);
1905                        ms_free(tmp);
1906                        freeaddrinfo(res);
1907                        linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_NO_FIREWALL);
1908                        return;
1909                }
1910                freeaddrinfo(res);
1911        }
1912
1913        if (lc->net_conf.firewall_policy==LINPHONE_POLICY_USE_NAT_ADDRESS){
1914                if (tmp!=NULL){
1915                        if (!lc->net_conf.nat_sdp_only)
1916                                eXosip_masquerade_contact(tmp,lc->sip_conf.sip_port);
1917                        ms_free(tmp);
1918                }
1919                else 
1920                        eXosip_masquerade_contact("",0);
1921        }
1922        else {
1923                eXosip_masquerade_contact("",0);       
1924        }
1925}
1926
1927
1928void linphone_core_set_nat_address(LinphoneCore *lc, const char *addr)
1929{
1930        if (lc->net_conf.nat_address!=NULL){
1931                ms_free(lc->net_conf.nat_address);
1932        }
1933        if (addr!=NULL) lc->net_conf.nat_address=ms_strdup(addr);
1934        else lc->net_conf.nat_address=NULL;
1935        lc->apply_nat_settings=TRUE;
1936}
1937
1938const char *linphone_core_get_nat_address(const LinphoneCore *lc)
1939{
1940        return lc->net_conf.nat_address;
1941}
1942
1943void linphone_core_set_firewall_policy(LinphoneCore *lc, LinphoneFirewallPolicy pol){
1944        lc->net_conf.firewall_policy=pol;
1945        lc->apply_nat_settings=TRUE;
1946}
1947
1948LinphoneFirewallPolicy linphone_core_get_firewall_policy(const LinphoneCore *lc){
1949        return lc->net_conf.firewall_policy;
1950}
1951
1952MSList * linphone_core_get_call_logs(LinphoneCore *lc){
1953        lc->missed_calls=0;
1954        return lc->call_logs;
1955}
1956
1957static void toggle_video_preview(LinphoneCore *lc, bool_t val){
1958#ifdef VIDEO_ENABLED
1959        if (lc->videostream==NULL){
1960                if (val){
1961                        if (lc->previewstream==NULL){
1962                                lc->previewstream=video_preview_start(lc->video_conf.device,
1963                                                        lc->video_conf.vsize);
1964                        }
1965                }else{
1966                        if (lc->previewstream!=NULL){
1967                                video_preview_stop(lc->previewstream);
1968                                lc->previewstream=NULL;
1969                        }
1970                }
1971        }
1972#endif
1973}
1974
1975void linphone_core_enable_video(LinphoneCore *lc, bool_t vcap_enabled, bool_t display_enabled){
1976#ifndef VIDEO_ENABLED
1977        if (vcap_enabled || display_enabled)
1978                ms_warning("This version of linphone was built without video support.");
1979#endif
1980        lc->video_conf.capture=vcap_enabled;
1981        lc->video_conf.display=display_enabled;
1982        if (vcap_enabled && display_enabled)
1983                lc->video_conf.show_local=1;
1984        else
1985                lc->video_conf.show_local=0;
1986
1987        /* need to re-apply network bandwidth settings*/
1988        linphone_core_set_download_bandwidth(lc,
1989                linphone_core_get_download_bandwidth(lc));
1990        linphone_core_set_upload_bandwidth(lc,
1991                linphone_core_get_upload_bandwidth(lc));
1992}
1993
1994bool_t linphone_core_video_enabled(LinphoneCore *lc){
1995        return (lc->video_conf.display || lc->video_conf.capture);
1996}
1997
1998void linphone_core_enable_video_preview(LinphoneCore *lc, bool_t val){
1999        lc->video_conf.show_local=val;
2000}
2001
2002bool_t linphone_core_video_preview_enabled(const LinphoneCore *lc){
2003        return lc->video_conf.show_local;
2004}
2005
2006int linphone_core_set_video_device(LinphoneCore *lc, const char *id){
2007        MSWebCam *olddev=lc->video_conf.device;
2008        if (id!=NULL){
2009                lc->video_conf.device=ms_web_cam_manager_get_cam(ms_web_cam_manager_get(),id);
2010                if (lc->video_conf.device==NULL){
2011                        ms_warning("Could not found video device %s",id);
2012                }
2013        }
2014        if (lc->video_conf.device==NULL)
2015                lc->video_conf.device=ms_web_cam_manager_get_default_cam(ms_web_cam_manager_get());
2016        if (olddev!=NULL && olddev!=lc->video_conf.device){
2017                toggle_video_preview(lc,FALSE);/*restart the video local preview*/
2018        }
2019        return 0;
2020}
2021
2022const char *linphone_core_get_video_device(const LinphoneCore *lc){
2023        if (lc->video_conf.device) return ms_web_cam_get_string_id(lc->video_conf.device);
2024        return NULL;
2025}
2026
2027static MSVideoSizeDef supported_resolutions[]={
2028        {       MS_VIDEO_SIZE_SVGA      ,       "svga"  },
2029        {       MS_VIDEO_SIZE_4CIF      ,       "4cif"  },
2030        {       MS_VIDEO_SIZE_VGA       ,       "vga"   },
2031        {       MS_VIDEO_SIZE_CIF       ,       "cif"   },
2032        {       MS_VIDEO_SIZE_QVGA      ,       "qvga"  },
2033        {       MS_VIDEO_SIZE_QCIF      ,       "qcif"  },
2034        {       {0,0}                   ,       NULL    }
2035};
2036
2037const MSVideoSizeDef *linphone_core_get_supported_video_sizes(LinphoneCore *lc){
2038        return supported_resolutions;
2039}
2040
2041static MSVideoSize video_size_get_by_name(const char *name){
2042        MSVideoSizeDef *pdef=supported_resolutions;
2043        for(;pdef->name!=NULL;pdef++){
2044                if (strcasecmp(name,pdef->name)==0){
2045                        return pdef->vsize;
2046                }
2047        }
2048        ms_warning("Video resolution %s is not supported in linphone.",name);
2049        return (MSVideoSize){0,0};
2050}
2051
2052const char *video_size_get_name(MSVideoSize vsize){
2053        MSVideoSizeDef *pdef=supported_resolutions;
2054        for(;pdef->name!=NULL;pdef++){
2055                if (pdef->vsize.width==vsize.width && pdef->vsize.height==vsize.height){
2056                        return pdef->name;
2057                }
2058        }
2059        return NULL;
2060}
2061
2062static bool_t video_size_supported(MSVideoSize vsize){
2063        if (video_size_get_name(vsize)) return TRUE;
2064        ms_warning("Video resolution %ix%i is not supported in linphone.",vsize.width,vsize.height);
2065        return FALSE;
2066}
2067
2068
2069void linphone_core_set_preferred_video_size(LinphoneCore *lc, MSVideoSize vsize){
2070        if (video_size_supported(vsize)){
2071                MSVideoSize oldvsize=lc->video_conf.vsize;
2072                lc->video_conf.vsize=vsize;
2073                if (!ms_video_size_equal(oldvsize,vsize) && lc->previewstream!=NULL){
2074                        toggle_video_preview(lc,FALSE);
2075                        toggle_video_preview(lc,TRUE);
2076                }
2077        }
2078}
2079
2080void linphone_core_set_preferred_video_size_by_name(LinphoneCore *lc, const char *name){
2081        MSVideoSize vsize=video_size_get_by_name(name);
2082        if (vsize.width!=0)     linphone_core_set_preferred_video_size(lc,vsize);
2083        else linphone_core_set_preferred_video_size(lc,MS_VIDEO_SIZE_CIF);
2084}
2085
2086MSVideoSize linphone_core_get_preferred_video_size(LinphoneCore *lc){
2087        return lc->video_conf.vsize;
2088}
2089
2090void linphone_core_use_files(LinphoneCore *lc, bool_t yesno){
2091        lc->use_files=yesno;
2092}
2093
2094void linphone_core_set_play_file(LinphoneCore *lc, const char *file){
2095        if (lc->play_file!=NULL){
2096                ms_free(lc->play_file);
2097                lc->play_file=NULL;
2098        }
2099        if (file!=NULL) {
2100                lc->play_file=ms_strdup(file);
2101                if (lc->audiostream)
2102                        audio_stream_play(lc->audiostream,file);
2103        }
2104}
2105
2106void linphone_core_set_record_file(LinphoneCore *lc, const char *file){
2107        if (lc->rec_file!=NULL){
2108                ms_free(lc->rec_file);
2109                lc->rec_file=NULL;
2110        }
2111        if (file!=NULL) {
2112                lc->rec_file=ms_strdup(file);
2113                if (lc->audiostream) 
2114                        audio_stream_record(lc->audiostream,file);
2115        }
2116}
2117
2118
2119void *linphone_core_get_user_data(LinphoneCore *lc){
2120        return lc->data;
2121}
2122
2123int linphone_core_get_mtu(const LinphoneCore *lc){
2124        return lc->net_conf.mtu;
2125}
2126
2127void linphone_core_set_mtu(LinphoneCore *lc, int mtu){
2128        lc->net_conf.mtu=mtu;
2129        if (mtu>0){
2130                if (mtu<500){
2131                        ms_error("MTU too small !");
2132                        mtu=500;
2133                }
2134                ms_set_mtu(mtu);
2135                ms_message("MTU is supposed to be %i, rtp payload max size will be %i",mtu, ms_get_payload_max_size());
2136        }else ms_set_mtu(0);//use mediastreamer2 default value
2137}
2138
2139void net_config_uninit(LinphoneCore *lc)
2140{
2141        net_config_t *config=&lc->net_conf;
2142        lp_config_set_int(lc->config,"net","download_bw",config->download_bw);
2143        lp_config_set_int(lc->config,"net","upload_bw",config->upload_bw);
2144       
2145        if (config->stun_server!=NULL)
2146                lp_config_set_string(lc->config,"net","stun_server",config->stun_server);
2147        if (config->nat_address!=NULL)
2148                lp_config_set_string(lc->config,"net","nat_address",config->nat_address);
2149        lp_config_set_int(lc->config,"net","firewall_policy",config->firewall_policy);
2150        lp_config_set_int(lc->config,"net","mtu",config->mtu);
2151}
2152
2153
2154void sip_config_uninit(LinphoneCore *lc)
2155{
2156        MSList *elem;
2157        int i;
2158        sip_config_t *config=&lc->sip_conf;
2159        lp_config_set_int(lc->config,"sip","sip_port",config->sip_port);
2160        lp_config_set_int(lc->config,"sip","guess_hostname",config->guess_hostname);
2161        lp_config_set_string(lc->config,"sip","contact",config->contact);
2162        lp_config_set_int(lc->config,"sip","inc_timeout",config->inc_timeout);
2163        lp_config_set_int(lc->config,"sip","use_info",config->use_info);
2164        lp_config_set_int(lc->config,"sip","use_ipv6",config->ipv6_enabled);
2165        for(elem=config->proxies,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
2166                LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)(elem->data);
2167                linphone_proxy_config_write_to_config_file(lc->config,cfg,i);
2168                linphone_proxy_config_edit(cfg);        /* to unregister */
2169        }
2170
2171        if (exosip_running)
2172          {
2173            int i;
2174            for (i=0;i<20;i++)
2175              {
2176                eXosip_event_t *ev;
2177                while((ev=eXosip_event_wait(0,0))!=NULL){
2178                  linphone_core_process_event(lc,ev);
2179                }
2180                eXosip_automatic_action();
2181#ifndef WIN32
2182                usleep(100000);
2183#else
2184        Sleep(100);
2185#endif
2186              }
2187          }
2188       
2189        linphone_proxy_config_write_to_config_file(lc->config,NULL,i);  /*mark the end */
2190       
2191        for(elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
2192                LinphoneAuthInfo *ai=(LinphoneAuthInfo*)(elem->data);
2193                linphone_auth_info_write_config(lc->config,ai,i);
2194        }
2195        linphone_auth_info_write_config(lc->config,NULL,i); /* mark the end */
2196}
2197
2198void rtp_config_uninit(LinphoneCore *lc)
2199{
2200        rtp_config_t *config=&lc->rtp_conf;
2201        lp_config_set_int(lc->config,"rtp","audio_rtp_port",config->audio_rtp_port);
2202        lp_config_set_int(lc->config,"rtp","video_rtp_port",config->video_rtp_port);
2203        lp_config_set_int(lc->config,"rtp","audio_jitt_comp",config->audio_jitt_comp);
2204        lp_config_set_int(lc->config,"rtp","video_jitt_comp",config->audio_jitt_comp);
2205        lp_config_set_int(lc->config,"rtp","nortp_timeout",config->nortp_timeout);
2206}
2207
2208void sound_config_uninit(LinphoneCore *lc)
2209{
2210        /*char tmpbuf[2];*/
2211        sound_config_t *config=&lc->sound_conf;
2212        lp_config_set_string(lc->config,"sound","playback_dev_id",ms_snd_card_get_string_id(config->play_sndcard));
2213        lp_config_set_string(lc->config,"sound","ringer_dev_id",ms_snd_card_get_string_id(config->ring_sndcard));
2214        lp_config_set_string(lc->config,"sound","capture_dev_id",ms_snd_card_get_string_id(config->capt_sndcard));
2215        ms_free(config->cards);
2216        /*
2217        lp_config_set_int(lc->config,"sound","rec_lev",config->rec_lev);
2218        lp_config_set_int(lc->config,"sound","play_lev",config->play_lev);
2219        lp_config_set_int(lc->config,"sound","ring_lev",config->ring_lev);
2220        tmpbuf[0]=config->source;
2221        tmpbuf[1]='\0';
2222        lp_config_set_string(lc->config,"sound","source",tmpbuf);
2223        */
2224        lp_config_set_string(lc->config,"sound","local_ring",config->local_ring);
2225        lp_config_set_string(lc->config,"sound","remote_ring",config->remote_ring);
2226        lp_config_set_int(lc->config,"sound","echocancelation",config->ec);
2227        if (config->local_ring) ms_free(config->local_ring);
2228}
2229
2230void video_config_uninit(LinphoneCore *lc)
2231{
2232        video_config_t *config=&lc->video_conf;
2233        const char *vd=linphone_core_get_video_device(lc);
2234        if (vd && strstr(vd,"Static picture")!=NULL){
2235                vd=NULL;
2236        }
2237        lp_config_set_string(lc->config,"video","device",vd);
2238        lp_config_set_int(lc->config,"video","display",config->display);
2239        lp_config_set_int(lc->config,"video","capture",config->capture);
2240        lp_config_set_int(lc->config,"video","show_local",config->show_local);
2241        lp_config_set_string(lc->config,"video","size",video_size_get_name(config->vsize));
2242}
2243
2244void codecs_config_uninit(LinphoneCore *lc)
2245{
2246        PayloadType *pt;
2247        codecs_config_t *config=&lc->codecs_conf;
2248        MSList *node;
2249        char key[50];
2250        int index;
2251        index=0;
2252        for(node=config->audio_codecs;node!=NULL;node=ms_list_next(node)){
2253                pt=(PayloadType*)(node->data);
2254                sprintf(key,"audio_codec_%i",index);
2255                lp_config_set_string(lc->config,key,"mime",pt->mime_type);
2256                lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
2257                lp_config_set_int(lc->config,key,"enabled",payload_type_enabled(pt));
2258                index++;
2259        }
2260        index=0;
2261        for(node=config->video_codecs;node!=NULL;node=ms_list_next(node)){
2262                pt=(PayloadType*)(node->data);
2263                sprintf(key,"video_codec_%i",index);
2264                lp_config_set_string(lc->config,key,"mime",pt->mime_type);
2265                lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
2266                lp_config_set_int(lc->config,key,"enabled",payload_type_enabled(pt));
2267                lp_config_set_string(lc->config,key,"recv_fmtp",pt->recv_fmtp);
2268                index++;
2269        }
2270}
2271
2272void ui_config_uninit(LinphoneCore* lc)
2273{
2274        MSList *elem;
2275        int i;
2276        for (elem=lc->friends,i=0; elem!=NULL; elem=ms_list_next(elem),i++){
2277                linphone_friend_write_to_config_file(lc->config,(LinphoneFriend*)elem->data,i);
2278                linphone_friend_destroy(elem->data);
2279        }
2280        linphone_friend_write_to_config_file(lc->config,NULL,i);        /* set the end */
2281        ms_list_free(lc->friends);
2282        lc->friends=NULL;
2283}
2284
2285LpConfig *linphone_core_get_config(LinphoneCore *lc){
2286        return lc->config;
2287}
2288
2289void linphone_core_uninit(LinphoneCore *lc)
2290{
2291        gstate_new_state(lc, GSTATE_POWER_SHUTDOWN, NULL);
2292#ifdef VIDEO_ENABLED
2293        if (lc->previewstream!=NULL){
2294                video_preview_stop(lc->previewstream);
2295                lc->previewstream=NULL;
2296        }
2297#endif
2298        /* save all config */
2299        net_config_uninit(lc);
2300        sip_config_uninit(lc);
2301        lp_config_set_int(lc->config,"sip","default_proxy",linphone_core_get_default_proxy(lc,NULL));
2302        rtp_config_uninit(lc);
2303        sound_config_uninit(lc);
2304        video_config_uninit(lc);
2305        codecs_config_uninit(lc);
2306        ui_config_uninit(lc);
2307        lp_config_sync(lc->config);
2308        lp_config_destroy(lc->config);
2309
2310#ifdef VIDEO_ENABLED
2311        if (payload_type_h264_packetization_mode_1!=NULL)
2312                payload_type_destroy(payload_type_h264_packetization_mode_1);
2313#endif
2314       
2315        ortp_exit();
2316        eXosip_quit();
2317        exosip_running=FALSE;
2318        gstate_new_state(lc, GSTATE_POWER_OFF, NULL);
2319}
2320
2321void linphone_core_destroy(LinphoneCore *lc){
2322        linphone_core_uninit(lc);
2323        ms_free(lc);
2324}
Note: See TracBrowser for help on using the repository browser.