source: mediastreamer2/linphone/coreapi/linphonecore.c @ 177:db43877d8d4c

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

merge patch from telezorg

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

File size: 67.4 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
1355static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
1356        LinphoneCore* lc = (LinphoneCore*)user_data;
1357        if (lc->vtable.dtmf_received != NULL)
1358                lc->vtable.dtmf_received(lc, dtmf);
1359}
1360
1361void linphone_core_start_media_streams(LinphoneCore *lc, LinphoneCall *call){
1362        osip_from_t *me=linphone_core_get_primary_contact_parsed(lc);
1363        const char *tool="linphone-" LINPHONE_VERSION;
1364        /* adjust rtp jitter compensation. It must be at least the latency of the sound card */
1365        int jitt_comp=MAX(lc->sound_conf.latency,lc->rtp_conf.audio_jitt_comp);
1366        char *cname=ortp_strdup_printf("%s@%s",me->url->username,me->url->host);
1367        {
1368                StreamParams *audio_params=&call->audio_params;
1369                if (!lc->use_files){
1370                        MSSndCard *playcard=lc->sound_conf.play_sndcard;
1371                        MSSndCard *captcard=lc->sound_conf.capt_sndcard;
1372                        if (playcard==NULL) {
1373                                ms_warning("No card defined for playback !");
1374                                goto end;
1375                        }
1376                        if (captcard==NULL) {
1377                                ms_warning("No card defined for capture !");
1378                                goto end;
1379                        }
1380                        if (audio_params->relay_session_id!=NULL) 
1381                                audio_stream_set_relay_session_id(lc->audiostream,audio_params->relay_session_id);
1382                        audio_stream_start_now(
1383                                lc->audiostream,
1384                                call->profile,
1385                                audio_params->remoteaddr,
1386                                audio_params->remoteport,
1387                                audio_params->remotertcpport,
1388                                audio_params->pt,
1389                                jitt_comp,
1390                                playcard,
1391                                captcard,
1392                                linphone_core_echo_cancelation_enabled(lc));
1393                }else{
1394                        audio_stream_start_with_files(
1395                                lc->audiostream,
1396                                call->profile,
1397                                audio_params->remoteaddr,
1398                                audio_params->remoteport,
1399                                audio_params->remotertcpport,
1400                                audio_params->pt,
1401                                100,
1402                                lc->play_file,
1403                                lc->rec_file);
1404                }
1405                if (lc->vtable.dtmf_received!=NULL){
1406                        /* replace by our default action*/
1407                        audio_stream_play_received_dtmfs(lc->audiostream,FALSE);
1408                        rtp_session_signal_connect(lc->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
1409                }
1410                audio_stream_set_rtcp_information(lc->audiostream, cname, tool);
1411        }
1412#ifdef VIDEO_ENABLED
1413        {
1414                /* shutdown preview */
1415                if (lc->previewstream!=NULL) {
1416                        video_preview_stop(lc->previewstream);
1417                        lc->previewstream=NULL;
1418                }
1419                if (lc->video_conf.display || lc->video_conf.capture) {
1420                        StreamParams *video_params=&call->video_params;
1421                       
1422                        if (video_params->remoteport>0){
1423                                if (video_params->relay_session_id!=NULL) 
1424                                        video_stream_set_relay_session_id(lc->videostream,video_params->relay_session_id);
1425                                video_stream_set_sent_video_size(lc->videostream,linphone_core_get_preferred_video_size(lc));
1426                                if (lc->video_conf.display && lc->video_conf.capture)
1427                                        video_stream_start(lc->videostream,
1428                                        call->profile, video_params->remoteaddr, video_params->remoteport,
1429                                        video_params->remotertcpport,
1430                                        video_params->pt, jitt_comp, lc->video_conf.device);
1431                                else if (lc->video_conf.display)
1432                                        video_stream_recv_only_start(lc->videostream,
1433                                        call->profile, video_params->remoteaddr, video_params->remoteport,
1434                                        video_params->pt, jitt_comp);
1435                                else if (lc->video_conf.capture)
1436                                        video_stream_send_only_start(lc->videostream,
1437                                        call->profile, video_params->remoteaddr, video_params->remoteport,
1438                                        video_params->remotertcpport,
1439                                        video_params->pt, jitt_comp, lc->video_conf.device);
1440                                video_stream_set_rtcp_information(lc->videostream, cname,tool);
1441                        }
1442                }
1443        }
1444#endif
1445        goto end;
1446        end:
1447        ms_free(cname);
1448        osip_from_free(me);
1449        lc->call->state=LCStateAVRunning;
1450}
1451
1452void linphone_core_stop_media_streams(LinphoneCore *lc){
1453        if (lc->audiostream!=NULL) {
1454                audio_stream_stop(lc->audiostream);
1455                lc->audiostream=NULL;
1456        }
1457#ifdef VIDEO_ENABLED
1458        if (lc->videostream!=NULL){
1459                if (lc->video_conf.display && lc->video_conf.capture)
1460                        video_stream_stop(lc->videostream);
1461                else if (lc->video_conf.display)
1462                        video_stream_recv_only_stop(lc->videostream);
1463                else if (lc->video_conf.capture)
1464                        video_stream_send_only_stop(lc->videostream);
1465                lc->videostream=NULL;
1466        }
1467        if (linphone_core_video_preview_enabled(lc)){
1468                if (lc->previewstream==NULL){
1469                        lc->previewstream=video_preview_start(lc->video_conf.device, lc->video_conf.vsize);
1470                }
1471        }
1472#endif
1473}
1474
1475int linphone_core_accept_call(LinphoneCore *lc, const char *url)
1476{
1477        char *sdpmesg;
1478        osip_message_t *msg=NULL;
1479        LinphoneCall *call=lc->call;
1480        int err;
1481        bool_t offering=FALSE;
1482       
1483        if (call==NULL){
1484                return -1;
1485        }
1486       
1487        if (lc->call->state==LCStateAVRunning){
1488                /*call already accepted*/
1489                return -1;
1490        }
1491
1492        /*stop ringing */
1493        if (lc->ringstream!=NULL) {
1494                ms_message("stop ringing");
1495                ring_stop(lc->ringstream);
1496                ms_message("ring stopped");
1497                lc->ringstream=NULL;
1498        }
1499        /* sends a 200 OK */
1500        err=eXosip_call_build_answer(call->tid,200,&msg);
1501        if (err<0 || msg==NULL){
1502                ms_error("Fail to build answer for call: err=%i",err);
1503                return -1;
1504        }
1505        ms_message("eXosip_call_build_answer done");
1506        /*if a sdp answer is computed, send it, else send an offer */
1507        sdpmesg=call->sdpctx->answerstr;
1508        if (sdpmesg==NULL){
1509                offering=TRUE;
1510                call->profile=rtp_profile_clone_full(lc->local_profile);
1511                ms_message("generating sdp offer");
1512                sdpmesg=sdp_context_get_offer(call->sdpctx);
1513               
1514                if (sdpmesg==NULL){
1515                        ms_error("fail to generate sdp offer !");
1516                        return -1;
1517                }
1518                linphone_set_sdp(msg,sdpmesg);
1519                linphone_core_init_media_streams(lc);
1520        }else{
1521                linphone_set_sdp(msg,sdpmesg);
1522        }
1523        eXosip_lock();
1524        eXosip_call_send_answer(call->tid,200,msg);
1525        eXosip_unlock();
1526        lc->vtable.display_status(lc,_("Connected."));
1527        gstate_new_state(lc, GSTATE_CALL_IN_CONNECTED, NULL);
1528       
1529        if (!offering) linphone_core_start_media_streams(lc, lc->call);
1530        ms_message("call answered.");
1531        return 0;
1532}
1533
1534int linphone_core_terminate_call(LinphoneCore *lc, const char *url)
1535{
1536        LinphoneCall *call=lc->call;
1537        if (call==NULL){
1538                return -1;
1539        }
1540        lc->call=NULL;
1541       
1542        eXosip_lock();
1543        eXosip_call_terminate(call->cid,call->did);
1544        eXosip_unlock();
1545       
1546        /*stop ringing*/
1547        if (lc->ringstream!=NULL) {
1548                ring_stop(lc->ringstream);
1549                lc->ringstream=NULL;
1550        }
1551        linphone_core_stop_media_streams(lc);
1552        lc->vtable.display_status(lc,_("Call ended") );
1553        gstate_new_state(lc, GSTATE_CALL_END, NULL);
1554        linphone_call_destroy(call);
1555        return 0;
1556}
1557
1558bool_t linphone_core_in_call(const LinphoneCore *lc){
1559        return lc->call!=NULL;
1560}
1561
1562int linphone_core_send_publish(LinphoneCore *lc,
1563                               LinphoneOnlineStatus presence_mode)
1564{
1565        const MSList *elem;
1566        for (elem=linphone_core_get_proxy_config_list(lc);elem!=NULL;elem=ms_list_next(elem)){
1567                LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)elem->data;
1568                if (cfg->publish) linphone_proxy_config_send_publish(cfg,presence_mode);
1569        }
1570        return 0;
1571}
1572
1573void linphone_core_set_inc_timeout(LinphoneCore *lc, int seconds){
1574        lc->sip_conf.inc_timeout=seconds;
1575}
1576
1577int linphone_core_get_inc_timeout(LinphoneCore *lc){
1578        return lc->sip_conf.inc_timeout;
1579}
1580
1581void linphone_core_set_presence_info(LinphoneCore *lc,int minutes_away,
1582                                                                                                        const char *contact,
1583                                                                                                        LinphoneOnlineStatus presence_mode)
1584{
1585        int contactok=-1;
1586        if (minutes_away>0) lc->minutes_away=minutes_away;
1587        if (contact!=NULL) {
1588                osip_from_t *url;
1589                osip_from_init(&url);
1590                contactok=osip_from_parse(url,contact);
1591                if (contactok>=0) {
1592                        ms_message("contact url is correct.");
1593                }
1594                osip_from_free(url);
1595               
1596        }
1597        if (contactok>=0){
1598                if (lc->alt_contact!=NULL) ms_free(lc->alt_contact);
1599                lc->alt_contact=ms_strdup(contact);
1600        }
1601        if (lc->presence_mode!=presence_mode){
1602                linphone_core_notify_all_friends(lc,presence_mode);
1603                /*
1604                   Improve the use of all LINPHONE_STATUS available.
1605                   !TODO Do not mix "presence status" with "answer status code"..
1606                   Use correct parameter to follow sip_if_match/sip_etag.
1607                 */
1608                linphone_core_send_publish(lc,presence_mode);
1609        }
1610        lc->prev_mode=lc->presence_mode;
1611        lc->presence_mode=presence_mode;
1612       
1613}
1614
1615LinphoneOnlineStatus linphone_core_get_presence_info(const LinphoneCore *lc){
1616        return lc->presence_mode;
1617}
1618
1619/* sound functions */
1620int linphone_core_get_play_level(LinphoneCore *lc)
1621{
1622        return lc->sound_conf.play_lev;
1623}
1624int linphone_core_get_ring_level(LinphoneCore *lc)
1625{
1626        return lc->sound_conf.ring_lev;
1627}
1628int linphone_core_get_rec_level(LinphoneCore *lc){
1629        return lc->sound_conf.rec_lev;
1630}
1631void linphone_core_set_ring_level(LinphoneCore *lc, int level){
1632        MSSndCard *sndcard;
1633        lc->sound_conf.ring_lev=level;
1634        sndcard=lc->sound_conf.ring_sndcard;
1635        if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_PLAYBACK,level);
1636}
1637
1638void linphone_core_set_play_level(LinphoneCore *lc, int level){
1639        MSSndCard *sndcard;
1640        lc->sound_conf.play_lev=level;
1641        sndcard=lc->sound_conf.play_sndcard;
1642        if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_PLAYBACK,level);
1643}
1644
1645void linphone_core_set_rec_level(LinphoneCore *lc, int level)
1646{
1647        MSSndCard *sndcard;
1648        lc->sound_conf.rec_lev=level;
1649        sndcard=lc->sound_conf.capt_sndcard;
1650        if (sndcard) ms_snd_card_set_level(sndcard,MS_SND_CARD_CAPTURE,level);
1651}
1652
1653static MSSndCard *get_card_from_string_id(const char *devid, unsigned int cap){
1654        MSSndCard *sndcard=NULL;
1655        if (devid!=NULL){
1656                sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
1657                if (sndcard!=NULL && 
1658                        (ms_snd_card_get_capabilities(sndcard) & cap)==0 ){
1659                        ms_warning("%s card does not have the %s capability, ignoring.",
1660                                devid,
1661                                cap==MS_SND_CARD_CAP_CAPTURE ? "capture" : "playback");
1662                        sndcard=NULL;
1663                }
1664        }
1665        if (sndcard==NULL) {
1666                /* get a card that has read+write capabilities */
1667                sndcard=ms_snd_card_manager_get_default_card(ms_snd_card_manager_get());
1668                /* otherwise refine to the first card having the right capability*/
1669                if (sndcard==NULL){
1670                        const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
1671                        for(;elem!=NULL;elem=elem->next){
1672                                sndcard=(MSSndCard*)elem->data;
1673                                if (ms_snd_card_get_capabilities(sndcard) & cap) break;
1674                        }
1675                }
1676                if (sndcard==NULL){/*looks like a bug! take the first one !*/
1677                        const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
1678                        sndcard=(MSSndCard*)elem->data;
1679                }
1680        }
1681        if (sndcard==NULL) ms_error("Could not find a suitable soundcard !");
1682        return sndcard;
1683}
1684
1685bool_t linphone_core_sound_device_can_capture(LinphoneCore *lc, const char *devid){
1686        MSSndCard *sndcard;
1687        sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
1688        if (sndcard!=NULL && (ms_snd_card_get_capabilities(sndcard) & MS_SND_CARD_CAP_CAPTURE)) return TRUE;
1689        return FALSE;
1690}
1691
1692bool_t linphone_core_sound_device_can_playback(LinphoneCore *lc, const char *devid){
1693        MSSndCard *sndcard;
1694        sndcard=ms_snd_card_manager_get_card(ms_snd_card_manager_get(),devid);
1695        if (sndcard!=NULL && (ms_snd_card_get_capabilities(sndcard) & MS_SND_CARD_CAP_PLAYBACK)) return TRUE;
1696        return FALSE;
1697}
1698
1699int linphone_core_set_ringer_device(LinphoneCore *lc, const char * devid){
1700        lc->sound_conf.ring_sndcard=get_card_from_string_id(devid,MS_SND_CARD_CAP_PLAYBACK);
1701        return 0;
1702}
1703
1704int linphone_core_set_playback_device(LinphoneCore *lc, const char * devid){
1705        lc->sound_conf.play_sndcard=get_card_from_string_id(devid,MS_SND_CARD_CAP_PLAYBACK);
1706        return 0;
1707}
1708
1709int linphone_core_set_capture_device(LinphoneCore *lc, const char * devid){
1710        lc->sound_conf.capt_sndcard=get_card_from_string_id(devid,MS_SND_CARD_CAP_CAPTURE);
1711        return 0;
1712}
1713
1714const char * linphone_core_get_ringer_device(LinphoneCore *lc)
1715{
1716        return ms_snd_card_get_string_id(lc->sound_conf.ring_sndcard);
1717}
1718
1719const char * linphone_core_get_playback_device(LinphoneCore *lc)
1720{
1721        return ms_snd_card_get_string_id(lc->sound_conf.play_sndcard);
1722}
1723
1724const char * linphone_core_get_capture_device(LinphoneCore *lc)
1725{
1726        return ms_snd_card_get_string_id(lc->sound_conf.capt_sndcard);
1727}
1728
1729/* returns a static array of string describing the sound devices */ 
1730const char**  linphone_core_get_sound_devices(LinphoneCore *lc){
1731        return lc->sound_conf.cards;
1732}
1733
1734const char**  linphone_core_get_video_devices(const LinphoneCore *lc){
1735        return lc->video_conf.cams;
1736}
1737
1738char linphone_core_get_sound_source(LinphoneCore *lc)
1739{
1740        return lc->sound_conf.source;
1741}
1742
1743void linphone_core_set_sound_source(LinphoneCore *lc, char source)
1744{
1745        MSSndCard *sndcard=lc->sound_conf.capt_sndcard;
1746        lc->sound_conf.source=source;
1747        if (!sndcard) return;
1748        switch(source){
1749                case 'm':
1750                        ms_snd_card_set_capture(sndcard,MS_SND_CARD_MIC);
1751                        break;
1752                case 'l':
1753                        ms_snd_card_set_capture(sndcard,MS_SND_CARD_LINE);
1754                        break;
1755        }
1756       
1757}
1758
1759void linphone_core_set_ring(LinphoneCore *lc,const char *path){
1760        if (lc->sound_conf.local_ring!=0){
1761                ms_free(lc->sound_conf.local_ring);
1762        }
1763        lc->sound_conf.local_ring=ms_strdup(path);
1764}
1765
1766const char *linphone_core_get_ring(LinphoneCore *lc){
1767        return lc->sound_conf.local_ring;
1768}
1769
1770static void notify_end_of_ring(void *ud ,unsigned int event, void * arg){
1771        LinphoneCore *lc=(LinphoneCore*)ud;
1772        lc->preview_finished=1;
1773}
1774
1775int linphone_core_preview_ring(LinphoneCore *lc, const char *ring,LinphoneCoreCbFunc func,void * userdata)
1776{
1777        if (lc->ringstream!=0){
1778                ms_warning("Cannot start ring now,there's already a ring being played");
1779                return -1;
1780        }
1781        lc_callback_obj_init(&lc->preview_finished_cb,func,userdata);
1782        lc->preview_finished=0;
1783        if (lc->sound_conf.ring_sndcard!=NULL){
1784                lc->ringstream=ring_start_with_cb(ring,2000,lc->sound_conf.ring_sndcard,notify_end_of_ring,(void *)lc);
1785        }
1786        return 0;
1787}
1788
1789
1790void linphone_core_set_ringback(LinphoneCore *lc,RingBackType type){
1791        switch(type){
1792                case RINGBACK_TYPE_FR:
1793                        lc->sound_conf.remote_ring=PACKAGE_SOUND_DIR "/" REMOTE_RING_FR;
1794                break;
1795                case RINGBACK_TYPE_US:
1796                        lc->sound_conf.remote_ring=PACKAGE_SOUND_DIR "/" REMOTE_RING_US;
1797                break;
1798        }
1799}
1800RingBackType linphone_core_get_ringback(LinphoneCore *lc);
1801
1802void linphone_core_enable_echo_cancelation(LinphoneCore *lc, bool_t val){
1803        lc->sound_conf.ec=val;
1804}
1805
1806bool_t linphone_core_echo_cancelation_enabled(LinphoneCore *lc){
1807        return lc->sound_conf.ec;
1808}
1809
1810
1811void linphone_core_send_dtmf(LinphoneCore *lc,char dtmf)
1812{
1813        if (linphone_core_get_use_info_for_dtmf(lc)==0){
1814                /* In Band DTMF */
1815                if (lc->audiostream!=NULL){
1816                        audio_stream_send_dtmf(lc->audiostream,dtmf);
1817                }
1818        }else{
1819                char dtmf_body[1000];
1820                char clen[10];
1821                osip_message_t *msg=NULL;
1822                /* Out of Band DTMF (use INFO method) */
1823                LinphoneCall *call=lc->call;
1824                if (call==NULL){
1825                        return;
1826                }
1827                eXosip_call_build_info(call->did,&msg);
1828                snprintf(dtmf_body, 999, "Signal=%c\r\nDuration=250\r\n", dtmf);
1829                osip_message_set_body(msg,dtmf_body,strlen(dtmf_body));
1830                osip_message_set_content_type(msg,"application/dtmf-relay");
1831                snprintf(clen,sizeof(clen),"%lu",(unsigned long)strlen(dtmf_body));
1832                osip_message_set_content_length(msg,clen);
1833               
1834                eXosip_lock();
1835                eXosip_call_send_request(call->did,msg);
1836                eXosip_unlock();
1837        }
1838}
1839
1840void linphone_core_set_stun_server(LinphoneCore *lc, const char *server){
1841        if (lc->net_conf.stun_server!=NULL)
1842                ms_free(lc->net_conf.stun_server);
1843        if (server)
1844                lc->net_conf.stun_server=ms_strdup(server);
1845        else lc->net_conf.stun_server=NULL;
1846        lc->apply_nat_settings=TRUE;
1847}
1848
1849const char * linphone_core_get_stun_server(const LinphoneCore *lc){
1850        return lc->net_conf.stun_server;
1851}
1852
1853const char * linphone_core_get_relay_addr(const LinphoneCore *lc){
1854        return lc->net_conf.relay;
1855}
1856
1857int linphone_core_set_relay_addr(LinphoneCore *lc, const char *addr){
1858        if (lc->net_conf.relay!=NULL){
1859                ms_free(lc->net_conf.relay);
1860                lc->net_conf.relay=NULL;
1861        }
1862        if (addr){
1863                lc->net_conf.relay=ms_strdup(addr);
1864        }
1865        return 0;
1866}
1867
1868static void apply_nat_settings(LinphoneCore *lc){
1869        char *wmsg;
1870        char *tmp=NULL;
1871        int err;
1872        struct addrinfo hints,*res;
1873        const char *addr=lc->net_conf.nat_address;
1874       
1875        if (lc->net_conf.firewall_policy==LINPHONE_POLICY_USE_NAT_ADDRESS){
1876                if (addr==NULL || strlen(addr)==0){
1877                        lc->vtable.display_warning(lc,_("No nat/firewall address supplied !"));
1878                        linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_NO_FIREWALL);
1879                }
1880                /*check the ip address given */
1881                memset(&hints,0,sizeof(struct addrinfo));
1882                if (lc->sip_conf.ipv6_enabled)
1883                        hints.ai_family=AF_INET6;
1884                else 
1885                        hints.ai_family=AF_INET;
1886                hints.ai_socktype = SOCK_DGRAM;
1887                err=getaddrinfo(addr,NULL,&hints,&res);
1888                if (err!=0){
1889                        wmsg=ortp_strdup_printf(_("Invalid nat address '%s' : %s"),
1890                                addr, gai_strerror(err));
1891                        ms_warning(wmsg); // what is this for ?
1892                        lc->vtable.display_warning(lc, wmsg);
1893                        ms_free(wmsg);
1894                        linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_NO_FIREWALL);
1895                        return;
1896                }
1897                /*now get it as an numeric ip address */
1898                tmp=ms_malloc0(50);
1899                err=getnameinfo(res->ai_addr,res->ai_addrlen,tmp,50,NULL,0,NI_NUMERICHOST);
1900                if (err!=0){
1901                        wmsg=ortp_strdup_printf(_("Invalid nat address '%s' : %s"),
1902                                addr, gai_strerror(err));
1903                        ms_warning(wmsg); // what is this for ?
1904                        lc->vtable.display_warning(lc, wmsg);
1905                        ms_free(wmsg);
1906                        ms_free(tmp);
1907                        freeaddrinfo(res);
1908                        linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_NO_FIREWALL);
1909                        return;
1910                }
1911                freeaddrinfo(res);
1912        }
1913
1914        if (lc->net_conf.firewall_policy==LINPHONE_POLICY_USE_NAT_ADDRESS){
1915                if (tmp!=NULL){
1916                        if (!lc->net_conf.nat_sdp_only)
1917                                eXosip_masquerade_contact(tmp,lc->sip_conf.sip_port);
1918                        ms_free(tmp);
1919                }
1920                else 
1921                        eXosip_masquerade_contact("",0);
1922        }
1923        else {
1924                eXosip_masquerade_contact("",0);       
1925        }
1926}
1927
1928
1929void linphone_core_set_nat_address(LinphoneCore *lc, const char *addr)
1930{
1931        if (lc->net_conf.nat_address!=NULL){
1932                ms_free(lc->net_conf.nat_address);
1933        }
1934        if (addr!=NULL) lc->net_conf.nat_address=ms_strdup(addr);
1935        else lc->net_conf.nat_address=NULL;
1936        lc->apply_nat_settings=TRUE;
1937}
1938
1939const char *linphone_core_get_nat_address(const LinphoneCore *lc)
1940{
1941        return lc->net_conf.nat_address;
1942}
1943
1944void linphone_core_set_firewall_policy(LinphoneCore *lc, LinphoneFirewallPolicy pol){
1945        lc->net_conf.firewall_policy=pol;
1946        lc->apply_nat_settings=TRUE;
1947}
1948
1949LinphoneFirewallPolicy linphone_core_get_firewall_policy(const LinphoneCore *lc){
1950        return lc->net_conf.firewall_policy;
1951}
1952
1953MSList * linphone_core_get_call_logs(LinphoneCore *lc){
1954        lc->missed_calls=0;
1955        return lc->call_logs;
1956}
1957
1958static void toggle_video_preview(LinphoneCore *lc, bool_t val){
1959#ifdef VIDEO_ENABLED
1960        if (lc->videostream==NULL){
1961                if (val){
1962                        if (lc->previewstream==NULL){
1963                                lc->previewstream=video_preview_start(lc->video_conf.device,
1964                                                        lc->video_conf.vsize);
1965                        }
1966                }else{
1967                        if (lc->previewstream!=NULL){
1968                                video_preview_stop(lc->previewstream);
1969                                lc->previewstream=NULL;
1970                        }
1971                }
1972        }
1973#endif
1974}
1975
1976void linphone_core_enable_video(LinphoneCore *lc, bool_t vcap_enabled, bool_t display_enabled){
1977#ifndef VIDEO_ENABLED
1978        if (vcap_enabled || display_enabled)
1979                ms_warning("This version of linphone was built without video support.");
1980#endif
1981        lc->video_conf.capture=vcap_enabled;
1982        lc->video_conf.display=display_enabled;
1983        if (vcap_enabled && display_enabled)
1984                lc->video_conf.show_local=1;
1985        else
1986                lc->video_conf.show_local=0;
1987
1988        /* need to re-apply network bandwidth settings*/
1989        linphone_core_set_download_bandwidth(lc,
1990                linphone_core_get_download_bandwidth(lc));
1991        linphone_core_set_upload_bandwidth(lc,
1992                linphone_core_get_upload_bandwidth(lc));
1993}
1994
1995bool_t linphone_core_video_enabled(LinphoneCore *lc){
1996        return (lc->video_conf.display || lc->video_conf.capture);
1997}
1998
1999void linphone_core_enable_video_preview(LinphoneCore *lc, bool_t val){
2000        lc->video_conf.show_local=val;
2001}
2002
2003bool_t linphone_core_video_preview_enabled(const LinphoneCore *lc){
2004        return lc->video_conf.show_local;
2005}
2006
2007int linphone_core_set_video_device(LinphoneCore *lc, const char *id){
2008        MSWebCam *olddev=lc->video_conf.device;
2009        if (id!=NULL){
2010                lc->video_conf.device=ms_web_cam_manager_get_cam(ms_web_cam_manager_get(),id);
2011                if (lc->video_conf.device==NULL){
2012                        ms_warning("Could not found video device %s",id);
2013                }
2014        }
2015        if (lc->video_conf.device==NULL)
2016                lc->video_conf.device=ms_web_cam_manager_get_default_cam(ms_web_cam_manager_get());
2017        if (olddev!=NULL && olddev!=lc->video_conf.device){
2018                toggle_video_preview(lc,FALSE);/*restart the video local preview*/
2019        }
2020        return 0;
2021}
2022
2023const char *linphone_core_get_video_device(const LinphoneCore *lc){
2024        if (lc->video_conf.device) return ms_web_cam_get_string_id(lc->video_conf.device);
2025        return NULL;
2026}
2027
2028static MSVideoSizeDef supported_resolutions[]={
2029        {       MS_VIDEO_SIZE_SVGA      ,       "svga"  },
2030        {       MS_VIDEO_SIZE_4CIF      ,       "4cif"  },
2031        {       MS_VIDEO_SIZE_VGA       ,       "vga"   },
2032        {       MS_VIDEO_SIZE_CIF       ,       "cif"   },
2033        {       MS_VIDEO_SIZE_QVGA      ,       "qvga"  },
2034        {       MS_VIDEO_SIZE_QCIF      ,       "qcif"  },
2035        {       {0,0}                   ,       NULL    }
2036};
2037
2038const MSVideoSizeDef *linphone_core_get_supported_video_sizes(LinphoneCore *lc){
2039        return supported_resolutions;
2040}
2041
2042static MSVideoSize video_size_get_by_name(const char *name){
2043        MSVideoSizeDef *pdef=supported_resolutions;
2044        for(;pdef->name!=NULL;pdef++){
2045                if (strcasecmp(name,pdef->name)==0){
2046                        return pdef->vsize;
2047                }
2048        }
2049        ms_warning("Video resolution %s is not supported in linphone.",name);
2050        return (MSVideoSize){0,0};
2051}
2052
2053const char *video_size_get_name(MSVideoSize vsize){
2054        MSVideoSizeDef *pdef=supported_resolutions;
2055        for(;pdef->name!=NULL;pdef++){
2056                if (pdef->vsize.width==vsize.width && pdef->vsize.height==vsize.height){
2057                        return pdef->name;
2058                }
2059        }
2060        return NULL;
2061}
2062
2063static bool_t video_size_supported(MSVideoSize vsize){
2064        if (video_size_get_name(vsize)) return TRUE;
2065        ms_warning("Video resolution %ix%i is not supported in linphone.",vsize.width,vsize.height);
2066        return FALSE;
2067}
2068
2069
2070void linphone_core_set_preferred_video_size(LinphoneCore *lc, MSVideoSize vsize){
2071        if (video_size_supported(vsize)){
2072                MSVideoSize oldvsize=lc->video_conf.vsize;
2073                lc->video_conf.vsize=vsize;
2074                if (!ms_video_size_equal(oldvsize,vsize) && lc->previewstream!=NULL){
2075                        toggle_video_preview(lc,FALSE);
2076                        toggle_video_preview(lc,TRUE);
2077                }
2078        }
2079}
2080
2081void linphone_core_set_preferred_video_size_by_name(LinphoneCore *lc, const char *name){
2082        MSVideoSize vsize=video_size_get_by_name(name);
2083        if (vsize.width!=0)     linphone_core_set_preferred_video_size(lc,vsize);
2084        else linphone_core_set_preferred_video_size(lc,MS_VIDEO_SIZE_CIF);
2085}
2086
2087MSVideoSize linphone_core_get_preferred_video_size(LinphoneCore *lc){
2088        return lc->video_conf.vsize;
2089}
2090
2091void linphone_core_use_files(LinphoneCore *lc, bool_t yesno){
2092        lc->use_files=yesno;
2093}
2094
2095void linphone_core_set_play_file(LinphoneCore *lc, const char *file){
2096        if (lc->play_file!=NULL){
2097                ms_free(lc->play_file);
2098                lc->play_file=NULL;
2099        }
2100        if (file!=NULL) {
2101                lc->play_file=ms_strdup(file);
2102                if (lc->audiostream)
2103                        audio_stream_play(lc->audiostream,file);
2104        }
2105}
2106
2107void linphone_core_set_record_file(LinphoneCore *lc, const char *file){
2108        if (lc->rec_file!=NULL){
2109                ms_free(lc->rec_file);
2110                lc->rec_file=NULL;
2111        }
2112        if (file!=NULL) {
2113                lc->rec_file=ms_strdup(file);
2114                if (lc->audiostream) 
2115                        audio_stream_record(lc->audiostream,file);
2116        }
2117}
2118
2119
2120void *linphone_core_get_user_data(LinphoneCore *lc){
2121        return lc->data;
2122}
2123
2124int linphone_core_get_mtu(const LinphoneCore *lc){
2125        return lc->net_conf.mtu;
2126}
2127
2128void linphone_core_set_mtu(LinphoneCore *lc, int mtu){
2129        lc->net_conf.mtu=mtu;
2130        if (mtu>0){
2131                if (mtu<500){
2132                        ms_error("MTU too small !");
2133                        mtu=500;
2134                }
2135                ms_set_mtu(mtu);
2136                ms_message("MTU is supposed to be %i, rtp payload max size will be %i",mtu, ms_get_payload_max_size());
2137        }else ms_set_mtu(0);//use mediastreamer2 default value
2138}
2139
2140void net_config_uninit(LinphoneCore *lc)
2141{
2142        net_config_t *config=&lc->net_conf;
2143        lp_config_set_int(lc->config,"net","download_bw",config->download_bw);
2144        lp_config_set_int(lc->config,"net","upload_bw",config->upload_bw);
2145       
2146        if (config->stun_server!=NULL)
2147                lp_config_set_string(lc->config,"net","stun_server",config->stun_server);
2148        if (config->nat_address!=NULL)
2149                lp_config_set_string(lc->config,"net","nat_address",config->nat_address);
2150        lp_config_set_int(lc->config,"net","firewall_policy",config->firewall_policy);
2151        lp_config_set_int(lc->config,"net","mtu",config->mtu);
2152}
2153
2154
2155void sip_config_uninit(LinphoneCore *lc)
2156{
2157        MSList *elem;
2158        int i;
2159        sip_config_t *config=&lc->sip_conf;
2160        lp_config_set_int(lc->config,"sip","sip_port",config->sip_port);
2161        lp_config_set_int(lc->config,"sip","guess_hostname",config->guess_hostname);
2162        lp_config_set_string(lc->config,"sip","contact",config->contact);
2163        lp_config_set_int(lc->config,"sip","inc_timeout",config->inc_timeout);
2164        lp_config_set_int(lc->config,"sip","use_info",config->use_info);
2165        lp_config_set_int(lc->config,"sip","use_ipv6",config->ipv6_enabled);
2166        for(elem=config->proxies,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
2167                LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)(elem->data);
2168                linphone_proxy_config_write_to_config_file(lc->config,cfg,i);
2169                linphone_proxy_config_edit(cfg);        /* to unregister */
2170        }
2171
2172        if (exosip_running)
2173          {
2174            int i;
2175            for (i=0;i<20;i++)
2176              {
2177                eXosip_event_t *ev;
2178                while((ev=eXosip_event_wait(0,0))!=NULL){
2179                  linphone_core_process_event(lc,ev);
2180                }
2181                eXosip_automatic_action();
2182#ifndef WIN32
2183                usleep(100000);
2184#else
2185        Sleep(100);
2186#endif
2187              }
2188          }
2189       
2190        linphone_proxy_config_write_to_config_file(lc->config,NULL,i);  /*mark the end */
2191       
2192        for(elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
2193                LinphoneAuthInfo *ai=(LinphoneAuthInfo*)(elem->data);
2194                linphone_auth_info_write_config(lc->config,ai,i);
2195        }
2196        linphone_auth_info_write_config(lc->config,NULL,i); /* mark the end */
2197}
2198
2199void rtp_config_uninit(LinphoneCore *lc)
2200{
2201        rtp_config_t *config=&lc->rtp_conf;
2202        lp_config_set_int(lc->config,"rtp","audio_rtp_port",config->audio_rtp_port);
2203        lp_config_set_int(lc->config,"rtp","video_rtp_port",config->video_rtp_port);
2204        lp_config_set_int(lc->config,"rtp","audio_jitt_comp",config->audio_jitt_comp);
2205        lp_config_set_int(lc->config,"rtp","video_jitt_comp",config->audio_jitt_comp);
2206        lp_config_set_int(lc->config,"rtp","nortp_timeout",config->nortp_timeout);
2207}
2208
2209void sound_config_uninit(LinphoneCore *lc)
2210{
2211        /*char tmpbuf[2];*/
2212        sound_config_t *config=&lc->sound_conf;
2213        lp_config_set_string(lc->config,"sound","playback_dev_id",ms_snd_card_get_string_id(config->play_sndcard));
2214        lp_config_set_string(lc->config,"sound","ringer_dev_id",ms_snd_card_get_string_id(config->ring_sndcard));
2215        lp_config_set_string(lc->config,"sound","capture_dev_id",ms_snd_card_get_string_id(config->capt_sndcard));
2216        ms_free(config->cards);
2217        /*
2218        lp_config_set_int(lc->config,"sound","rec_lev",config->rec_lev);
2219        lp_config_set_int(lc->config,"sound","play_lev",config->play_lev);
2220        lp_config_set_int(lc->config,"sound","ring_lev",config->ring_lev);
2221        tmpbuf[0]=config->source;
2222        tmpbuf[1]='\0';
2223        lp_config_set_string(lc->config,"sound","source",tmpbuf);
2224        */
2225        lp_config_set_string(lc->config,"sound","local_ring",config->local_ring);
2226        lp_config_set_string(lc->config,"sound","remote_ring",config->remote_ring);
2227        lp_config_set_int(lc->config,"sound","echocancelation",config->ec);
2228        if (config->local_ring) ms_free(config->local_ring);
2229}
2230
2231void video_config_uninit(LinphoneCore *lc)
2232{
2233        video_config_t *config=&lc->video_conf;
2234        const char *vd=linphone_core_get_video_device(lc);
2235        if (vd && strstr(vd,"Static picture")!=NULL){
2236                vd=NULL;
2237        }
2238        lp_config_set_string(lc->config,"video","device",vd);
2239        lp_config_set_int(lc->config,"video","display",config->display);
2240        lp_config_set_int(lc->config,"video","capture",config->capture);
2241        lp_config_set_int(lc->config,"video","show_local",config->show_local);
2242        lp_config_set_string(lc->config,"video","size",video_size_get_name(config->vsize));
2243}
2244
2245void codecs_config_uninit(LinphoneCore *lc)
2246{
2247        PayloadType *pt;
2248        codecs_config_t *config=&lc->codecs_conf;
2249        MSList *node;
2250        char key[50];
2251        int index;
2252        index=0;
2253        for(node=config->audio_codecs;node!=NULL;node=ms_list_next(node)){
2254                pt=(PayloadType*)(node->data);
2255                sprintf(key,"audio_codec_%i",index);
2256                lp_config_set_string(lc->config,key,"mime",pt->mime_type);
2257                lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
2258                lp_config_set_int(lc->config,key,"enabled",payload_type_enabled(pt));
2259                index++;
2260        }
2261        index=0;
2262        for(node=config->video_codecs;node!=NULL;node=ms_list_next(node)){
2263                pt=(PayloadType*)(node->data);
2264                sprintf(key,"video_codec_%i",index);
2265                lp_config_set_string(lc->config,key,"mime",pt->mime_type);
2266                lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
2267                lp_config_set_int(lc->config,key,"enabled",payload_type_enabled(pt));
2268                lp_config_set_string(lc->config,key,"recv_fmtp",pt->recv_fmtp);
2269                index++;
2270        }
2271}
2272
2273void ui_config_uninit(LinphoneCore* lc)
2274{
2275        MSList *elem;
2276        int i;
2277        for (elem=lc->friends,i=0; elem!=NULL; elem=ms_list_next(elem),i++){
2278                linphone_friend_write_to_config_file(lc->config,(LinphoneFriend*)elem->data,i);
2279                linphone_friend_destroy(elem->data);
2280        }
2281        linphone_friend_write_to_config_file(lc->config,NULL,i);        /* set the end */
2282        ms_list_free(lc->friends);
2283        lc->friends=NULL;
2284}
2285
2286LpConfig *linphone_core_get_config(LinphoneCore *lc){
2287        return lc->config;
2288}
2289
2290void linphone_core_uninit(LinphoneCore *lc)
2291{
2292        gstate_new_state(lc, GSTATE_POWER_SHUTDOWN, NULL);
2293#ifdef VIDEO_ENABLED
2294        if (lc->previewstream!=NULL){
2295                video_preview_stop(lc->previewstream);
2296                lc->previewstream=NULL;
2297        }
2298#endif
2299        /* save all config */
2300        net_config_uninit(lc);
2301        sip_config_uninit(lc);
2302        lp_config_set_int(lc->config,"sip","default_proxy",linphone_core_get_default_proxy(lc,NULL));
2303        rtp_config_uninit(lc);
2304        sound_config_uninit(lc);
2305        video_config_uninit(lc);
2306        codecs_config_uninit(lc);
2307        ui_config_uninit(lc);
2308        lp_config_sync(lc->config);
2309        lp_config_destroy(lc->config);
2310
2311#ifdef VIDEO_ENABLED
2312        if (payload_type_h264_packetization_mode_1!=NULL)
2313                payload_type_destroy(payload_type_h264_packetization_mode_1);
2314#endif
2315       
2316        ortp_exit();
2317        eXosip_quit();
2318        exosip_running=FALSE;
2319        gstate_new_state(lc, GSTATE_POWER_OFF, NULL);
2320}
2321
2322void linphone_core_destroy(LinphoneCore *lc){
2323        linphone_core_uninit(lc);
2324        ms_free(lc);
2325}
Note: See TracBrowser for help on using the repository browser.