source: mediastreamer2/linphone/coreapi/linphonecore.c @ 173:dd88e7fefb9b

Last change on this file since 173:dd88e7fefb9b was 173:dd88e7fefb9b, checked in by smorlat <smorlat@…>, 5 years ago

plenty of improvements

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

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