source: mediastreamer2/linphone/coreapi/linphonecore.c @ 194:dd674a337c93

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

fix some little bugs concerning selfview.

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

File size: 67.6 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 "ringback.wav"
54
55
56sdp_handler_t linphone_sdphandler={
57        linphone_accept_audio_offer,   /*from remote sdp */
58        linphone_accept_video_offer,   /*from remote sdp */
59        linphone_set_audio_offer,       /*to local sdp */
60        linphone_set_video_offer,       /*to local sdp */
61        linphone_read_audio_answer,     /*from incoming answer  */
62        linphone_read_video_answer      /*from incoming answer  */
63};
64
65void lc_callback_obj_init(LCCallbackObj *obj,LinphoneCoreCbFunc func,void* ud)
66{
67  obj->_func=func;
68  obj->_user_data=ud;
69}
70
71int lc_callback_obj_invoke(LCCallbackObj *obj, LinphoneCore *lc){
72        if (obj->_func!=NULL) obj->_func(lc,obj->_user_data);
73        return 0;
74}
75
76static void  linphone_call_init_common(LinphoneCall *call, char *from, char *to){
77        call->state=LCStateInit;
78        call->start_time=time(NULL);
79        call->log=linphone_call_log_new(call, from, to);
80        linphone_core_notify_all_friends(call->core,LINPHONE_STATUS_ONTHEPHONE);
81        if (linphone_core_get_firewall_policy(call->core)==LINPHONE_POLICY_USE_STUN) 
82                linphone_core_run_stun_tests(call->core,call);
83        call->profile=rtp_profile_new("Call RTP profile");
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;
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;
390        }
391        if (strstr(tmpbuf,".wav")==NULL){
392                /* it currently uses old sound files, so replace them */
393                tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
394        }
395        linphone_core_set_ringback(lc,tmpbuf);
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        linphone_core_enable_self_view(lc,TRUE);
570#endif
571}
572
573void ui_config_read(LinphoneCore *lc)
574{
575        LinphoneFriend *lf;
576        int i;
577        for (i=0;(lf=linphone_friend_new_from_config_file(lc,i))!=NULL;i++){
578                linphone_core_add_friend(lc,lf);
579        }
580       
581}
582
583void autoreplier_config_init(LinphoneCore *lc)
584{
585        autoreplier_config_t *config=&lc->autoreplier_conf;
586        config->enabled=lp_config_get_int(lc->config,"autoreplier","enabled",0);
587        config->after_seconds=lp_config_get_int(lc->config,"autoreplier","after_seconds",6);
588        config->max_users=lp_config_get_int(lc->config,"autoreplier","max_users",1);
589        config->max_rec_time=lp_config_get_int(lc->config,"autoreplier","max_rec_time",60);
590        config->max_rec_msg=lp_config_get_int(lc->config,"autoreplier","max_rec_msg",10);
591        config->message=lp_config_get_string(lc->config,"autoreplier","message",NULL);
592}
593
594void linphone_core_set_download_bandwidth(LinphoneCore *lc, int bw){
595        lc->net_conf.download_bw=bw;
596        if (bw==0){ /*infinite*/
597                lc->dw_audio_bw=-1;
598                lc->dw_video_bw=-1;
599        }else {
600                lc->dw_audio_bw=MIN(lc->audio_bw,bw);
601                lc->dw_video_bw=MAX(bw-lc->dw_audio_bw-10,0);/*-10: security margin*/
602        }
603}
604
605void linphone_core_set_upload_bandwidth(LinphoneCore *lc, int bw){
606        lc->net_conf.upload_bw=bw;
607        if (bw==0){ /*infinite*/
608                lc->up_audio_bw=-1;
609                lc->up_video_bw=-1;
610        }else{
611                lc->up_audio_bw=MIN(lc->audio_bw,bw);
612                lc->up_video_bw=MAX(bw-lc->up_audio_bw-10,0);/*-10: security margin*/
613        }
614}
615
616int linphone_core_get_download_bandwidth(const LinphoneCore *lc){
617        return lc->net_conf.download_bw;
618}
619
620int linphone_core_get_upload_bandwidth(const LinphoneCore *lc){
621        return lc->net_conf.upload_bw;
622}
623
624const char * linphone_core_get_version(void){
625        return liblinphone_version;
626}
627
628#ifdef VIDEO_ENABLED
629
630static PayloadType * payload_type_h264_packetization_mode_1=NULL;
631static PayloadType * linphone_h263_1998=NULL;
632static PayloadType * linphone_mp4v_es=NULL;
633#endif
634
635void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vtable, const char *config_path, void * userdata)
636{
637        memset (lc, 0, sizeof (LinphoneCore));
638        lc->data=userdata;
639       
640        memcpy(&lc->vtable,vtable,sizeof(LinphoneCoreVTable));
641
642        gstate_initialize();
643        gstate_new_state(lc, GSTATE_POWER_STARTUP, NULL);
644       
645        ortp_init();
646        rtp_profile_set_payload(&av_profile,115,&payload_type_lpc1015);
647        rtp_profile_set_payload(&av_profile,110,&payload_type_speex_nb);
648        rtp_profile_set_payload(&av_profile,111,&payload_type_speex_wb);
649        rtp_profile_set_payload(&av_profile,112,&payload_type_ilbc);
650        rtp_profile_set_payload(&av_profile,116,&payload_type_truespeech);
651        rtp_profile_set_payload(&av_profile,101,&payload_type_telephone_event);
652       
653#ifdef VIDEO_ENABLED
654        rtp_profile_set_payload(&av_profile,97,&payload_type_theora);
655
656        linphone_h263_1998=payload_type_clone(&payload_type_h263_1998);
657        payload_type_set_recv_fmtp(linphone_h263_1998,"CIF=1;QCIF=1");
658        rtp_profile_set_payload(&av_profile,98,linphone_h263_1998);
659
660        linphone_mp4v_es=payload_type_clone(&payload_type_mp4v);
661        payload_type_set_recv_fmtp(linphone_mp4v_es,"profile-level-id=3");
662        rtp_profile_set_payload(&av_profile,99,linphone_mp4v_es);
663        rtp_profile_set_payload(&av_profile,100,&payload_type_x_snow);
664        payload_type_h264_packetization_mode_1=payload_type_clone(&payload_type_h264);
665        payload_type_set_recv_fmtp(payload_type_h264_packetization_mode_1,"packetization-mode=1");
666        rtp_profile_set_payload(&av_profile,103,payload_type_h264_packetization_mode_1);
667        rtp_profile_set_payload(&av_profile,102,&payload_type_h264);
668#endif
669
670        ms_init();
671       
672        lc->config=lp_config_new(config_path);
673 
674#ifdef VINCENT_MAURY_RSVP
675        /* default qos parameters : rsvp on, rpc off */
676        lc->rsvp_enable = 1;
677        lc->rpc_enable = 0;
678#endif
679        sound_config_read(lc);
680        net_config_read(lc);
681        rtp_config_read(lc);
682        codecs_config_read(lc);
683        sip_config_read(lc); /* this will start eXosip*/
684        video_config_read(lc);
685        //autoreplier_config_init(&lc->autoreplier_conf);
686        lc->prev_mode=LINPHONE_STATUS_ONLINE;
687        lc->presence_mode=LINPHONE_STATUS_ONLINE;
688        lc->max_call_logs=15;
689        ui_config_read(lc);
690        ms_mutex_init(&lc->lock,NULL);
691        lc->vtable.display_status(lc,_("Ready"));
692        gstate_new_state(lc, GSTATE_POWER_ON, NULL);
693}
694
695LinphoneCore *linphone_core_new(const LinphoneCoreVTable *vtable,
696                                                const char *config_path, void * userdata)
697{
698        LinphoneCore *core=ms_new(LinphoneCore,1);
699        linphone_core_init(core,vtable,config_path,userdata);
700        return core;
701}
702
703const MSList *linphone_core_get_audio_codecs(const LinphoneCore *lc)
704{
705        return lc->codecs_conf.audio_codecs;
706}
707
708const MSList *linphone_core_get_video_codecs(const LinphoneCore *lc)
709{
710        return lc->codecs_conf.video_codecs;
711}
712
713int linphone_core_set_primary_contact(LinphoneCore *lc, const char *contact)
714{
715        osip_from_t *ctt=NULL;
716        osip_from_init(&ctt);
717        if (osip_from_parse(ctt,contact)!=0){
718                ms_error("Bad contact url: %s",contact);
719                osip_from_free(ctt);
720                return -1;
721        }
722        if (lc->sip_conf.contact!=NULL) ms_free(lc->sip_conf.contact);
723        lc->sip_conf.contact=ms_strdup(contact);
724        if (lc->sip_conf.guessed_contact!=NULL){
725                ms_free(lc->sip_conf.guessed_contact);
726                lc->sip_conf.guessed_contact=NULL;
727        }
728        osip_from_free(ctt);
729        return 0;
730}
731
732
733/*result must be an array of chars at least LINPHONE_IPADDR_SIZE */
734void linphone_core_get_local_ip(LinphoneCore *lc, const char *dest, char *result){
735        if (lc->apply_nat_settings){
736                apply_nat_settings(lc);
737                lc->apply_nat_settings=FALSE;
738        }
739        if (linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_NAT_ADDRESS){
740                strncpy(result,linphone_core_get_nat_address(lc),LINPHONE_IPADDR_SIZE);
741                return;
742        }
743        if (linphone_core_get_firewall_policy(lc)==LINPHONE_POLICY_USE_STUN) {
744                if (lc->sip_conf.ipv6_enabled){
745                        ms_warning("stun support is not implemented for ipv6");
746                }else{
747                        /* we no more use stun for sip socket*/
748#if 0
749                        int mport=0;
750                        ms_message("doing stun lookup for local address...");
751                        if (stun_get_localip(lc,sock,linphone_core_get_sip_port(lc),result,&mport)){
752                                if (!lc->net_conf.nat_sdp_only)
753                                        eXosip_masquerade_contact(result,mport);
754                                return;
755                        }
756                        ms_warning("stun lookup failed, falling back to a local interface...");
757#endif
758                }
759               
760        }
761        if (eXosip_guess_localip(lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,result,LINPHONE_IPADDR_SIZE)<0){
762                /*default to something */
763                strncpy(result,lc->sip_conf.ipv6_enabled ? "::1" : "127.0.0.1",LINPHONE_IPADDR_SIZE);
764                ms_error("Could not find default routable ip address !"); 
765        }       
766        eXosip_masquerade_contact(NULL,0);
767}
768
769const char *linphone_core_get_primary_contact(LinphoneCore *lc)
770{
771        char *identity;
772        char tmp[LINPHONE_IPADDR_SIZE];
773        if (lc->sip_conf.guess_hostname){
774                if (lc->sip_conf.guessed_contact==NULL || lc->sip_conf.loopback_only){
775                        char *guessed=NULL;
776                        osip_from_t *url;
777                        if (lc->sip_conf.guessed_contact!=NULL){
778                                ms_free(lc->sip_conf.guessed_contact);
779                                lc->sip_conf.guessed_contact=NULL;
780                        }
781                       
782                        osip_from_init(&url);
783                        if (osip_from_parse(url,lc->sip_conf.contact)==0){
784                               
785                        }else ms_error("Could not parse identity contact !");
786                        linphone_core_get_local_ip(lc, NULL, tmp);
787                        if (strcmp(tmp,"127.0.0.1")==0 || strcmp(tmp,"::1")==0 ){
788                                ms_warning("Local loopback network only !");
789                                lc->sip_conf.loopback_only=TRUE;
790                        }else lc->sip_conf.loopback_only=FALSE;
791                        osip_free(url->url->host);
792                        url->url->host=osip_strdup(tmp);
793                        if (url->url->port!=NULL){
794                                osip_free(url->url->port);
795                                url->url->port=NULL;
796                        }
797                        if (lc->sip_conf.sip_port!=5060){
798                                url->url->port=ortp_strdup_printf("%i",lc->sip_conf.sip_port);
799                        }
800                        osip_from_to_str(url,&guessed);
801                        lc->sip_conf.guessed_contact=guessed;
802                       
803                        osip_from_free(url);
804                       
805                }
806                identity=lc->sip_conf.guessed_contact;
807        }else{
808                identity=lc->sip_conf.contact;
809        }
810        return identity;
811}
812
813void linphone_core_set_guess_hostname(LinphoneCore *lc, bool_t val){
814        lc->sip_conf.guess_hostname=val;
815}
816       
817bool_t linphone_core_get_guess_hostname(LinphoneCore *lc){
818        return lc->sip_conf.guess_hostname;
819}
820
821osip_from_t *linphone_core_get_primary_contact_parsed(LinphoneCore *lc){
822        int err;
823        osip_from_t *contact;
824        osip_from_init(&contact);
825        err=osip_from_parse(contact,linphone_core_get_primary_contact(lc));
826        if (err<0) {
827                osip_from_free(contact);
828                return NULL;
829        }
830        return contact;
831}
832
833int linphone_core_set_audio_codecs(LinphoneCore *lc, MSList *codecs)
834{
835        if (lc->codecs_conf.audio_codecs!=NULL) ms_list_free(lc->codecs_conf.audio_codecs);
836        lc->codecs_conf.audio_codecs=codecs;
837        return 0;
838}
839
840int linphone_core_set_video_codecs(LinphoneCore *lc, MSList *codecs)
841{
842        if (lc->codecs_conf.video_codecs!=NULL) ms_list_free(lc->codecs_conf.video_codecs);
843        lc->codecs_conf.video_codecs=codecs;
844        return 0;
845}
846
847const MSList * linphone_core_get_friend_list(LinphoneCore *lc)
848{
849        return lc->friends;
850}
851
852int linphone_core_get_audio_jittcomp(LinphoneCore *lc)
853{
854        return lc->rtp_conf.audio_jitt_comp;
855}
856
857int linphone_core_get_audio_port(const LinphoneCore *lc)
858{
859        return lc->rtp_conf.audio_rtp_port;
860}
861
862int linphone_core_get_video_port(const LinphoneCore *lc){
863        return lc->rtp_conf.video_rtp_port;
864}
865
866int linphone_core_get_nortp_timeout(const LinphoneCore *lc){
867        return lc->rtp_conf.nortp_timeout;
868}
869
870void linphone_core_set_audio_jittcomp(LinphoneCore *lc, int value)
871{
872        lc->rtp_conf.audio_jitt_comp=value;
873}
874
875void linphone_core_set_audio_port(LinphoneCore *lc, int port)
876{
877        lc->rtp_conf.audio_rtp_port=port;
878}
879
880void linphone_core_set_video_port(LinphoneCore *lc, int port){
881        lc->rtp_conf.video_rtp_port=port;
882}
883
884void linphone_core_set_nortp_timeout(LinphoneCore *lc, int nortp_timeout){
885        lc->rtp_conf.nortp_timeout=nortp_timeout;
886}
887
888bool_t linphone_core_get_use_info_for_dtmf(LinphoneCore *lc)
889{
890        return lc->sip_conf.use_info;
891}
892
893void linphone_core_set_use_info_for_dtmf(LinphoneCore *lc,bool_t use_info)
894{
895        lc->sip_conf.use_info=use_info;
896}
897
898int linphone_core_get_sip_port(LinphoneCore *lc)
899{
900        return lc->sip_conf.sip_port;
901}
902
903static bool_t exosip_running=FALSE;
904
905void linphone_core_set_sip_port(LinphoneCore *lc,int port)
906{
907        const char *anyaddr;
908        char ua_string[256];
909        int err=0;
910        if (port==lc->sip_conf.sip_port) return;
911        lc->sip_conf.sip_port=port;
912        if (exosip_running) eXosip_quit();
913        eXosip_init();
914        eXosip_enable_ipv6(lc->sip_conf.ipv6_enabled);
915        if (lc->sip_conf.ipv6_enabled)
916                anyaddr="::0";
917        else
918                anyaddr="0.0.0.0";
919        err=eXosip_listen_addr (IPPROTO_UDP, anyaddr, port,
920                lc->sip_conf.ipv6_enabled ?  PF_INET6 : PF_INET, 0);
921        if (err<0){
922                char *msg=ortp_strdup_printf("UDP port %i seems already in use ! Cannot initialize.",port);
923                ms_warning(msg);
924                lc->vtable.display_warning(lc,msg);
925                ms_free(msg);
926                return;
927        }
928#ifdef VINCENT_MAURY_RSVP
929        /* tell exosip the qos settings according to default linphone parameters */
930        eXosip_set_rsvp_mode (lc->rsvp_enable);
931        eXosip_set_rpc_mode (lc->rpc_enable);
932#endif
933        snprintf(ua_string,sizeof(ua_string),"Linphone/%s (eXosip2/%s)",LINPHONE_VERSION,
934#ifdef HAVE_EXOSIP_GET_VERSION
935                 eXosip_get_version()
936#else
937                 "unknown"
938#endif
939);
940        eXosip_set_user_agent(ua_string);
941        exosip_running=TRUE;
942}
943
944bool_t linphone_core_ipv6_enabled(LinphoneCore *lc){
945        return lc->sip_conf.ipv6_enabled;
946}
947void linphone_core_enable_ipv6(LinphoneCore *lc, bool_t val){
948        if (lc->sip_conf.ipv6_enabled!=val){
949                lc->sip_conf.ipv6_enabled=val;
950                if (exosip_running){
951                        /* we need to restart eXosip */
952                        linphone_core_set_sip_port(lc, lc->sip_conf.sip_port);
953                }
954        }
955}
956
957static void display_bandwidth(RtpSession *as, RtpSession *vs){
958        ms_message("bandwidth usage: audio=[d=%.1f,u=%.1f] video=[d=%.1f,u=%.1f] kbit/sec",
959        (as!=NULL) ? (rtp_session_compute_recv_bandwidth(as)*1e-3) : 0,
960        (as!=NULL) ? (rtp_session_compute_send_bandwidth(as)*1e-3) : 0,
961        (vs!=NULL) ? (rtp_session_compute_recv_bandwidth(vs)*1e-3) : 0,
962        (vs!=NULL) ? (rtp_session_compute_send_bandwidth(vs)*1e-3) : 0);
963}
964
965static void linphone_core_disconnected(LinphoneCore *lc){
966        lc->vtable.display_warning(lc,_("Remote end seems to have disconnected, the call is going to be closed."));
967        linphone_core_terminate_call(lc,NULL);
968}
969
970void linphone_core_iterate(LinphoneCore *lc)
971{
972        eXosip_event_t *ev;
973        bool_t disconnected=FALSE;
974        int disconnect_timeout = linphone_core_get_nortp_timeout(lc); 
975        if (lc->preview_finished){
976                lc->preview_finished=0;
977                ring_stop(lc->ringstream);
978                lc->ringstream=NULL;
979                lc_callback_obj_invoke(&lc->preview_finished_cb,lc);
980        }
981       
982        if (exosip_running){
983                while((ev=eXosip_event_wait(0,0))!=NULL){
984                        linphone_core_process_event(lc,ev);
985                }
986                if (lc->automatic_action==0) {
987                        eXosip_lock();
988                        eXosip_automatic_action();
989                        eXosip_unlock();
990                }
991        }
992        if (lc->call!=NULL){
993                LinphoneCall *call=lc->call;
994                int elapsed;
995                time_t curtime=time(NULL);
996                if (call->dir==LinphoneCallIncoming && call->state==LCStateRinging){
997                        elapsed=curtime-call->start_time;
998                        ms_message("incoming call ringing for %i seconds",elapsed);
999                        if (elapsed>lc->sip_conf.inc_timeout){
1000                                linphone_core_terminate_call(lc,NULL);
1001                        }
1002                }else if (call->state==LCStateAVRunning){
1003                        elapsed=curtime-lc->prevtime;
1004                        if (elapsed>=1){
1005                                RtpSession *as=NULL,*vs=NULL;
1006                                lc->prevtime=curtime;
1007                                if (lc->audiostream!=NULL)
1008                                        as=lc->audiostream->session;
1009                                if (lc->videostream!=NULL)
1010                                        vs=lc->videostream->session;
1011                                display_bandwidth(as,vs);
1012                        }
1013#ifdef VIDEO_ENABLED
1014                        if (lc->videostream!=NULL)
1015                                video_stream_iterate(lc->videostream);
1016#endif
1017                        if (lc->audiostream!=NULL && disconnect_timeout>0)
1018                                disconnected=!audio_stream_alive(lc->audiostream,disconnect_timeout);
1019                }
1020        }
1021        if (linphone_core_video_preview_enabled(lc)){
1022                if (lc->previewstream==NULL)
1023                        toggle_video_preview(lc,TRUE);
1024#ifdef VIDEO_ENABLED
1025                else video_stream_iterate(lc->previewstream);
1026#endif
1027        }else{
1028                if (lc->previewstream!=NULL)
1029                        toggle_video_preview(lc,FALSE);
1030        }
1031        if (disconnected)
1032                linphone_core_disconnected(lc);
1033}
1034
1035
1036bool_t linphone_core_is_in_main_thread(LinphoneCore *lc){
1037        return TRUE;
1038}
1039
1040static osip_to_t *osip_to_create(const char *to){
1041        osip_to_t *ret;
1042        osip_to_init(&ret);
1043        if (osip_to_parse(ret,to)<0){
1044                osip_to_free(ret);
1045                return NULL;
1046        }
1047        return ret;
1048}
1049
1050bool_t linphone_core_interpret_url(LinphoneCore *lc, const char *url, char **real_url, osip_to_t **real_parsed_url, char **route){
1051        enum_lookup_res_t *enumres=NULL;
1052        osip_to_t *parsed_url=NULL;
1053        char *enum_domain=NULL;
1054        LinphoneProxyConfig *proxy;
1055        char *tmpurl;
1056        const char *tmproute;
1057        if (real_url!=NULL) *real_url=NULL;
1058        if (real_parsed_url!=NULL) *real_parsed_url=NULL;
1059        *route=NULL;
1060        tmproute=linphone_core_get_route(lc);
1061       
1062        if (is_enum(url,&enum_domain)){
1063                lc->vtable.display_status(lc,_("Looking for telephone number destination..."));
1064                if (enum_lookup(enum_domain,&enumres)<0){
1065                        lc->vtable.display_status(lc,_("Could not resolve this number."));
1066                        ms_free(enum_domain);
1067                        return FALSE;
1068                }
1069                ms_free(enum_domain);
1070                tmpurl=enumres->sip_address[0];
1071                if (real_url!=NULL) *real_url=ms_strdup(tmpurl);
1072                if (real_parsed_url!=NULL) *real_parsed_url=osip_to_create(tmpurl);
1073                enum_lookup_res_free(enumres);
1074                if (tmproute) *route=ms_strdup(tmproute);
1075                return TRUE;
1076        }
1077        /* check if we have a "sip:" */
1078        if (strstr(url,"sip:")==NULL){
1079                /* this doesn't look like a true sip uri */
1080                proxy=lc->default_proxy;
1081                if (proxy!=NULL){
1082                        /* append the proxy domain suffix */
1083                        osip_from_t *uri;
1084                        char *sipaddr;
1085                        const char *identity=linphone_proxy_config_get_identity(proxy);
1086                        osip_from_init(&uri);
1087                        if (osip_from_parse(uri,identity)<0){
1088                                osip_from_free(uri);
1089                                return FALSE;
1090                        }
1091                        sipaddr=ortp_strdup_printf("sip:%s@%s",url,uri->url->host);
1092                        if (real_parsed_url!=NULL) *real_parsed_url=osip_to_create(sipaddr);
1093                        if (real_url!=NULL) *real_url=sipaddr;
1094                        else ms_free(sipaddr);
1095#if 0
1096                        /*if the prompted uri was auto-suffixed with proxy domain,
1097                        then automatically set a route so that the request goes
1098                        through the proxy*/
1099                        if (tmproute==NULL){
1100                                osip_route_t *rt=NULL;
1101                                char *rtstr=NULL;
1102                                osip_route_init(&rt);
1103                                if (osip_route_parse(rt,linphone_proxy_config_get_addr(proxy))==0){
1104                                        osip_uri_uparam_add(rt->url,osip_strdup("lr"),NULL);
1105                                        osip_route_to_str(rt,&rtstr);
1106                                        *route=ms_strdup(rtstr);
1107                                        osip_free(rtstr);
1108                                }
1109                                ms_message("setting automatically a route to %s",*route);
1110                        }
1111                        else *route=ms_strdup(tmproute);
1112#else
1113                        if (tmproute) *route=ms_strdup(tmproute);
1114#endif
1115                        return TRUE;
1116                }
1117        }
1118        parsed_url=osip_to_create(url);
1119        if (parsed_url!=NULL){
1120                if (real_url!=NULL) *real_url=ms_strdup(url);
1121                if (real_parsed_url!=NULL) *real_parsed_url=parsed_url;
1122                else osip_to_free(parsed_url);
1123                if (tmproute) *route=ms_strdup(tmproute);
1124                return TRUE;
1125        }
1126        /* else we could not do anything with url given by user, so display an error */
1127        if (lc->vtable.display_warning!=NULL){
1128                lc->vtable.display_warning(lc,_("Could not parse given sip address. A sip url usually looks like sip:user@domain"));
1129        }
1130        return FALSE;
1131}
1132
1133const char * linphone_core_get_identity(LinphoneCore *lc){
1134        LinphoneProxyConfig *proxy=NULL;
1135        const char *from;
1136        linphone_core_get_default_proxy(lc,&proxy);
1137        if (proxy!=NULL) {
1138                from=linphone_proxy_config_get_identity(proxy);
1139        }else from=linphone_core_get_primary_contact(lc);
1140        return from;
1141}
1142
1143const char * linphone_core_get_route(LinphoneCore *lc){
1144        LinphoneProxyConfig *proxy=NULL;
1145        const char *route=NULL;
1146        linphone_core_get_default_proxy(lc,&proxy);
1147        if (proxy!=NULL) {
1148                route=linphone_proxy_config_get_route(proxy);
1149        }
1150        return route;
1151}
1152
1153void linphone_set_sdp(osip_message_t *sip, const char *sdpmesg){
1154        int sdplen=strlen(sdpmesg);
1155        char clen[10];
1156        snprintf(clen,sizeof(clen),"%i",sdplen);
1157        osip_message_set_body(sip,sdpmesg,sdplen);
1158        osip_message_set_content_type(sip,"application/sdp");
1159        osip_message_set_content_length(sip,clen);
1160}
1161
1162int linphone_core_invite(LinphoneCore *lc, const char *url)
1163{
1164        char *barmsg;
1165        int err=0;
1166        char *sdpmesg=NULL;
1167        char *route=NULL;
1168        const char *from=NULL;
1169        osip_message_t *invite=NULL;
1170        sdp_context_t *ctx=NULL;
1171        LinphoneProxyConfig *proxy=NULL;
1172        osip_from_t *parsed_url2=NULL;
1173        osip_to_t *real_parsed_url=NULL;
1174        char *real_url=NULL;
1175       
1176        if (lc->call!=NULL){
1177                lc->vtable.display_warning(lc,_("Sorry, having multiple simultaneous calls is not supported yet !"));
1178                return -1;
1179        }
1180
1181        gstate_new_state(lc, GSTATE_CALL_OUT_INVITE, url);
1182        linphone_core_get_default_proxy(lc,&proxy);
1183        if (!linphone_core_interpret_url(lc,url,&real_url,&real_parsed_url,&route)){
1184                /* bad url */
1185                gstate_new_state(lc, GSTATE_CALL_ERROR, NULL);
1186                return -1;
1187        }
1188        if (proxy!=NULL) {
1189                from=linphone_proxy_config_get_identity(proxy);
1190               
1191        }
1192        /* if no proxy or no identity defined for this proxy, default to primary contact*/
1193        if (from==NULL) from=linphone_core_get_primary_contact(lc);
1194
1195        err=eXosip_call_build_initial_invite(&invite,real_url,from,
1196                                                route,"Phone call");
1197
1198        if (err<0){
1199                ms_warning("Could not build initial invite");
1200                goto end;
1201        }
1202       
1203        /* make sdp message */
1204       
1205        osip_from_init(&parsed_url2);
1206        osip_from_parse(parsed_url2,from);
1207       
1208        lc->call=linphone_call_new_outgoing(lc,parsed_url2,real_parsed_url);
1209        barmsg=ortp_strdup_printf("%s %s", _("Contacting"), real_url);
1210        lc->vtable.display_status(lc,barmsg);
1211        ms_free(barmsg);
1212        if (!lc->sip_conf.sdp_200_ack){
1213                ctx=lc->call->sdpctx;
1214                sdpmesg=sdp_context_get_offer(ctx);
1215                linphone_set_sdp(invite,sdpmesg);
1216                linphone_core_init_media_streams(lc);
1217        }
1218        eXosip_lock();
1219        err=eXosip_call_send_initial_invite(invite);
1220        lc->call->cid=err;
1221        eXosip_unlock();
1222        if (err<0){
1223                ms_warning("Could not initiate call.");
1224                lc->vtable.display_status(lc,_("could not call"));
1225                linphone_call_destroy(lc->call);
1226                lc->call=NULL;
1227                linphone_core_stop_media_streams(lc);
1228        }
1229       
1230        goto end;
1231        end:
1232                if (real_url!=NULL) ms_free(real_url);
1233                if (real_parsed_url!=NULL) osip_to_free(real_parsed_url);
1234                if (parsed_url2!=NULL) osip_from_free(parsed_url2);
1235                if (err<0)
1236                        gstate_new_state(lc, GSTATE_CALL_ERROR, NULL);
1237                if (route!=NULL) ms_free(route);
1238        return (err<0) ? -1 : 0;
1239}
1240
1241int linphone_core_refer(LinphoneCore *lc, const char *url)
1242{
1243        char *real_url=NULL;
1244        osip_to_t *real_parsed_url=NULL;
1245        LinphoneCall *call;
1246        osip_message_t *msg=NULL;
1247        char *route;
1248        if (!linphone_core_interpret_url(lc,url,&real_url,&real_parsed_url, &route)){
1249                /* bad url */
1250                return -1;
1251        }
1252        if (route!=NULL) ms_free(route);
1253        call=lc->call;
1254        if (call==NULL){
1255                ms_warning("No established call to refer.");
1256                return -1;
1257        }
1258        lc->call=NULL;
1259        eXosip_call_build_refer(call->did, real_url, &msg);
1260        eXosip_lock();
1261        eXosip_call_send_request(call->did, msg);
1262        eXosip_unlock();
1263        return 0;
1264}
1265
1266bool_t linphone_core_inc_invite_pending(LinphoneCore*lc){
1267        if (lc->call!=NULL && lc->call->dir==LinphoneCallIncoming){
1268                return TRUE;
1269        }
1270        return FALSE;
1271}
1272
1273#ifdef VINCENT_MAURY_RSVP
1274/* on=1 for RPC_ENABLE=1...*/
1275int linphone_core_set_rpc_mode(LinphoneCore *lc, int on)
1276{
1277        if (on==1)
1278                printf("RPC_ENABLE set on\n");
1279        else 
1280                printf("RPC_ENABLE set off\n");
1281        lc->rpc_enable = (on==1);
1282        /* need to tell eXosip the new setting */
1283        if (eXosip_set_rpc_mode (lc->rpc_enable)!=0)
1284                return -1;
1285        return 0;
1286}
1287
1288/* on=1 for RSVP_ENABLE=1...*/
1289int linphone_core_set_rsvp_mode(LinphoneCore *lc, int on)
1290{
1291        if (on==1)
1292                printf("RSVP_ENABLE set on\n");
1293        else 
1294                printf("RSVP_ENABLE set off\n");
1295        lc->rsvp_enable = (on==1);
1296        /* need to tell eXosip the new setting */
1297        if (eXosip_set_rsvp_mode (lc->rsvp_enable)!=0)
1298                return -1;
1299        return 0;
1300}
1301
1302/* answer : 1 for yes, 0 for no */
1303int linphone_core_change_qos(LinphoneCore *lc, int answer)
1304{
1305        char *sdpmesg;
1306        if (lc->call==NULL){
1307                return -1;
1308        }
1309       
1310        if (lc->rsvp_enable && answer==1)
1311        {
1312                /* answer is yes, local setting is with qos, so
1313                 * the user chose to continue with no qos ! */
1314                /* so switch in normal mode : ring and 180 */
1315                lc->rsvp_enable = 0; /* no more rsvp */
1316                eXosip_set_rsvp_mode (lc->rsvp_enable);
1317                /* send 180 */
1318                eXosip_lock();
1319                eXosip_answer_call(lc->call->did,180,NULL);
1320                eXosip_unlock();
1321                /* play the ring */
1322                ms_message("Starting local ring...");
1323                lc->ringstream=ring_start(lc->sound_conf.local_ring,
1324                                        2000,ms_snd_card_manager_get_card(ms_snd_card_manager_get(),lc->sound_conf.ring_sndcard));
1325        }
1326        else if (!lc->rsvp_enable && answer==1)
1327        {
1328                /* switch to QoS mode on : answer 183 session progress */
1329                lc->rsvp_enable = 1;
1330                eXosip_set_rsvp_mode (lc->rsvp_enable);
1331                /* take the sdp already computed, see osipuacb.c */
1332                sdpmesg=lc->call->sdpctx->answerstr;
1333                eXosip_lock();
1334                eXosip_answer_call_with_body(lc->call->did,183,"application/sdp",sdpmesg);
1335                eXosip_unlock();
1336        }
1337        else
1338        {
1339                /* decline offer (603) */
1340                linphone_core_terminate_call(lc, NULL);
1341        }
1342        return 0;
1343}
1344#endif
1345
1346void linphone_core_init_media_streams(LinphoneCore *lc){
1347        lc->audiostream=audio_stream_new(linphone_core_get_audio_port(lc),linphone_core_ipv6_enabled(lc));
1348#ifdef VIDEO_ENABLED
1349        if (lc->video_conf.display || lc->video_conf.capture)
1350                lc->videostream=video_stream_new(linphone_core_get_video_port(lc),linphone_core_ipv6_enabled(lc));
1351#else
1352        lc->videostream=NULL;
1353#endif
1354}
1355
1356static void linphone_core_dtmf_received(RtpSession* s, int dtmf, void* user_data){
1357        LinphoneCore* lc = (LinphoneCore*)user_data;
1358        if (lc->vtable.dtmf_received != NULL)
1359                lc->vtable.dtmf_received(lc, dtmf);
1360}
1361
1362void linphone_core_start_media_streams(LinphoneCore *lc, LinphoneCall *call){
1363        osip_from_t *me=linphone_core_get_primary_contact_parsed(lc);
1364        const char *tool="linphone-" LINPHONE_VERSION;
1365        /* adjust rtp jitter compensation. It must be at least the latency of the sound card */
1366        int jitt_comp=MAX(lc->sound_conf.latency,lc->rtp_conf.audio_jitt_comp);
1367        char *cname=ortp_strdup_printf("%s@%s",me->url->username,me->url->host);
1368        {
1369                StreamParams *audio_params=&call->audio_params;
1370                if (!lc->use_files){
1371                        MSSndCard *playcard=lc->sound_conf.play_sndcard;
1372                        MSSndCard *captcard=lc->sound_conf.capt_sndcard;
1373                        if (playcard==NULL) {
1374                                ms_warning("No card defined for playback !");
1375                                goto end;
1376                        }
1377                        if (captcard==NULL) {
1378                                ms_warning("No card defined for capture !");
1379                                goto end;
1380                        }
1381                        if (audio_params->relay_session_id!=NULL) 
1382                                audio_stream_set_relay_session_id(lc->audiostream,audio_params->relay_session_id);
1383                        audio_stream_start_now(
1384                                lc->audiostream,
1385                                call->profile,
1386                                audio_params->remoteaddr,
1387                                audio_params->remoteport,
1388                                audio_params->remotertcpport,
1389                                audio_params->pt,
1390                                jitt_comp,
1391                                playcard,
1392                                captcard,
1393                                linphone_core_echo_cancelation_enabled(lc));
1394                }else{
1395                        audio_stream_start_with_files(
1396                                lc->audiostream,
1397                                call->profile,
1398                                audio_params->remoteaddr,
1399                                audio_params->remoteport,
1400                                audio_params->remotertcpport,
1401                                audio_params->pt,
1402                                100,
1403                                lc->play_file,
1404                                lc->rec_file);
1405                }
1406                if (lc->vtable.dtmf_received!=NULL){
1407                        /* replace by our default action*/
1408                        audio_stream_play_received_dtmfs(lc->audiostream,FALSE);
1409                        rtp_session_signal_connect(lc->audiostream->session,"telephone-event",(RtpCallback)linphone_core_dtmf_received,(unsigned long)lc);
1410                }
1411                audio_stream_set_rtcp_information(lc->audiostream, cname, tool);
1412        }
1413#ifdef VIDEO_ENABLED
1414        {
1415                /* shutdown preview */
1416                if (lc->previewstream!=NULL) {
1417                        video_preview_stop(lc->previewstream);
1418                        lc->previewstream=NULL;
1419                }
1420                if (lc->video_conf.display || lc->video_conf.capture) {
1421                        StreamParams *video_params=&call->video_params;
1422                       
1423                        if (video_params->remoteport>0){
1424                                if (video_params->relay_session_id!=NULL) 
1425                                        video_stream_set_relay_session_id(lc->videostream,video_params->relay_session_id);
1426                                video_stream_set_sent_video_size(lc->videostream,linphone_core_get_preferred_video_size(lc));
1427                                video_stream_enable_self_view(lc->videostream,lc->video_conf.selfview);
1428                                if (lc->video_conf.display && lc->video_conf.capture)
1429                                        video_stream_start(lc->videostream,
1430                                        call->profile, video_params->remoteaddr, video_params->remoteport,
1431                                        video_params->remotertcpport,
1432                                        video_params->pt, jitt_comp, lc->video_conf.device);
1433                                else if (lc->video_conf.display)
1434                                        video_stream_recv_only_start(lc->videostream,
1435                                        call->profile, video_params->remoteaddr, video_params->remoteport,
1436                                        video_params->pt, jitt_comp);
1437                                else if (lc->video_conf.capture)
1438                                        video_stream_send_only_start(lc->videostream,
1439                                        call->profile, video_params->remoteaddr, video_params->remoteport,
1440                                        video_params->remotertcpport,
1441                                        video_params->pt, jitt_comp, lc->video_conf.device);
1442                                video_stream_set_rtcp_information(lc->videostream, cname,tool);
1443                        }
1444                }
1445        }
1446#endif
1447        goto end;
1448        end:
1449        ms_free(cname);
1450        osip_from_free(me);
1451        lc->call->state=LCStateAVRunning;
1452}
1453
1454void linphone_core_stop_media_streams(LinphoneCore *lc){
1455        if (lc->audiostream!=NULL) {
1456                audio_stream_stop(lc->audiostream);
1457                lc->audiostream=NULL;
1458        }
1459#ifdef VIDEO_ENABLED
1460        if (lc->videostream!=NULL){
1461                if (lc->video_conf.display && lc->video_conf.capture)
1462                        video_stream_stop(lc->videostream);
1463                else if (lc->video_conf.display)
1464                        video_stream_recv_only_stop(lc->videostream);
1465                else if (lc->video_conf.capture)
1466                        video_stream_send_only_stop(lc->videostream);
1467                lc->videostream=NULL;
1468        }
1469        if (linphone_core_video_preview_enabled(lc)){
1470                if (lc->previewstream==NULL){
1471                        lc->previewstream=video_preview_start(lc->video_conf.device, lc->video_conf.vsize);
1472                }
1473        }
1474#endif
1475}
1476
1477int linphone_core_accept_call(LinphoneCore *lc, const char *url)
1478{
1479        char *sdpmesg;
1480        osip_message_t *msg=NULL;
1481        LinphoneCall *call=lc->call;
1482        int err;
1483        bool_t offering=FALSE;
1484       
1485        if (call==NULL){
1486                return -1;
1487        }
1488       
1489        if (lc->call->state==LCStateAVRunning){
1490                /*call already accepted*/
1491                return -1;
1492        }
1493
1494        /*stop ringing */
1495        if (lc->ringstream!=NULL) {
1496                ms_message("stop ringing");
1497                ring_stop(lc->ringstream);
1498                ms_message("ring stopped");
1499                lc->ringstream=NULL;
1500        }
1501        /* sends a 200 OK */
1502        err=eXosip_call_build_answer(call->tid,200,&msg);
1503        if (err<0 || msg==NULL){
1504                ms_error("Fail to build answer for call: err=%i",err);
1505                return -1;
1506        }
1507        /*if a sdp answer is computed, send it, else send an offer */
1508        sdpmesg=call->sdpctx->answerstr;
1509        if (sdpmesg==NULL){
1510                offering=TRUE;
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(const 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, const char *path){
1791        if (lc->sound_conf.remote_ring!=0){
1792                ms_free(lc->sound_conf.remote_ring);
1793        }
1794        lc->sound_conf.remote_ring=ms_strdup(path);
1795}
1796
1797const char * linphone_core_get_ringback(const LinphoneCore *lc){
1798        return lc->sound_conf.remote_ring;
1799}
1800
1801void linphone_core_enable_echo_cancelation(LinphoneCore *lc, bool_t val){
1802        lc->sound_conf.ec=val;
1803}
1804
1805bool_t linphone_core_echo_cancelation_enabled(LinphoneCore *lc){
1806        return lc->sound_conf.ec;
1807}
1808
1809
1810void linphone_core_send_dtmf(LinphoneCore *lc,char dtmf)
1811{
1812        if (linphone_core_get_use_info_for_dtmf(lc)==0){
1813                /* In Band DTMF */
1814                if (lc->audiostream!=NULL){
1815                        audio_stream_send_dtmf(lc->audiostream,dtmf);
1816                }
1817        }else{
1818                char dtmf_body[1000];
1819                char clen[10];
1820                osip_message_t *msg=NULL;
1821                /* Out of Band DTMF (use INFO method) */
1822                LinphoneCall *call=lc->call;
1823                if (call==NULL){
1824                        return;
1825                }
1826                eXosip_call_build_info(call->did,&msg);
1827                snprintf(dtmf_body, 999, "Signal=%c\r\nDuration=250\r\n", dtmf);
1828                osip_message_set_body(msg,dtmf_body,strlen(dtmf_body));
1829                osip_message_set_content_type(msg,"application/dtmf-relay");
1830                snprintf(clen,sizeof(clen),"%lu",(unsigned long)strlen(dtmf_body));
1831                osip_message_set_content_length(msg,clen);
1832               
1833                eXosip_lock();
1834                eXosip_call_send_request(call->did,msg);
1835                eXosip_unlock();
1836        }
1837}
1838
1839void linphone_core_set_stun_server(LinphoneCore *lc, const char *server){
1840        if (lc->net_conf.stun_server!=NULL)
1841                ms_free(lc->net_conf.stun_server);
1842        if (server)
1843                lc->net_conf.stun_server=ms_strdup(server);
1844        else lc->net_conf.stun_server=NULL;
1845        lc->apply_nat_settings=TRUE;
1846}
1847
1848const char * linphone_core_get_stun_server(const LinphoneCore *lc){
1849        return lc->net_conf.stun_server;
1850}
1851
1852const char * linphone_core_get_relay_addr(const LinphoneCore *lc){
1853        return lc->net_conf.relay;
1854}
1855
1856int linphone_core_set_relay_addr(LinphoneCore *lc, const char *addr){
1857        if (lc->net_conf.relay!=NULL){
1858                ms_free(lc->net_conf.relay);
1859                lc->net_conf.relay=NULL;
1860        }
1861        if (addr){
1862                lc->net_conf.relay=ms_strdup(addr);
1863        }
1864        return 0;
1865}
1866
1867static void apply_nat_settings(LinphoneCore *lc){
1868        char *wmsg;
1869        char *tmp=NULL;
1870        int err;
1871        struct addrinfo hints,*res;
1872        const char *addr=lc->net_conf.nat_address;
1873       
1874        if (lc->net_conf.firewall_policy==LINPHONE_POLICY_USE_NAT_ADDRESS){
1875                if (addr==NULL || strlen(addr)==0){
1876                        lc->vtable.display_warning(lc,_("No nat/firewall address supplied !"));
1877                        linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_NO_FIREWALL);
1878                }
1879                /*check the ip address given */
1880                memset(&hints,0,sizeof(struct addrinfo));
1881                if (lc->sip_conf.ipv6_enabled)
1882                        hints.ai_family=AF_INET6;
1883                else 
1884                        hints.ai_family=AF_INET;
1885                hints.ai_socktype = SOCK_DGRAM;
1886                err=getaddrinfo(addr,NULL,&hints,&res);
1887                if (err!=0){
1888                        wmsg=ortp_strdup_printf(_("Invalid nat address '%s' : %s"),
1889                                addr, gai_strerror(err));
1890                        ms_warning(wmsg); // what is this for ?
1891                        lc->vtable.display_warning(lc, wmsg);
1892                        ms_free(wmsg);
1893                        linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_NO_FIREWALL);
1894                        return;
1895                }
1896                /*now get it as an numeric ip address */
1897                tmp=ms_malloc0(50);
1898                err=getnameinfo(res->ai_addr,res->ai_addrlen,tmp,50,NULL,0,NI_NUMERICHOST);
1899                if (err!=0){
1900                        wmsg=ortp_strdup_printf(_("Invalid nat address '%s' : %s"),
1901                                addr, gai_strerror(err));
1902                        ms_warning(wmsg); // what is this for ?
1903                        lc->vtable.display_warning(lc, wmsg);
1904                        ms_free(wmsg);
1905                        ms_free(tmp);
1906                        freeaddrinfo(res);
1907                        linphone_core_set_firewall_policy(lc,LINPHONE_POLICY_NO_FIREWALL);
1908                        return;
1909                }
1910                freeaddrinfo(res);
1911        }
1912
1913        if (lc->net_conf.firewall_policy==LINPHONE_POLICY_USE_NAT_ADDRESS){
1914                if (tmp!=NULL){
1915                        if (!lc->net_conf.nat_sdp_only)
1916                                eXosip_masquerade_contact(tmp,lc->sip_conf.sip_port);
1917                        ms_free(tmp);
1918                }
1919                else 
1920                        eXosip_masquerade_contact("",0);
1921        }
1922        else {
1923                eXosip_masquerade_contact("",0);       
1924        }
1925}
1926
1927
1928void linphone_core_set_nat_address(LinphoneCore *lc, const char *addr)
1929{
1930        if (lc->net_conf.nat_address!=NULL){
1931                ms_free(lc->net_conf.nat_address);
1932        }
1933        if (addr!=NULL) lc->net_conf.nat_address=ms_strdup(addr);
1934        else lc->net_conf.nat_address=NULL;
1935        lc->apply_nat_settings=TRUE;
1936}
1937
1938const char *linphone_core_get_nat_address(const LinphoneCore *lc)
1939{
1940        return lc->net_conf.nat_address;
1941}
1942
1943void linphone_core_set_firewall_policy(LinphoneCore *lc, LinphoneFirewallPolicy pol){
1944        lc->net_conf.firewall_policy=pol;
1945        lc->apply_nat_settings=TRUE;
1946}
1947
1948LinphoneFirewallPolicy linphone_core_get_firewall_policy(const LinphoneCore *lc){
1949        return lc->net_conf.firewall_policy;
1950}
1951
1952MSList * linphone_core_get_call_logs(LinphoneCore *lc){
1953        lc->missed_calls=0;
1954        return lc->call_logs;
1955}
1956
1957static void toggle_video_preview(LinphoneCore *lc, bool_t val){
1958#ifdef VIDEO_ENABLED
1959        if (lc->videostream==NULL){
1960                if (val){
1961                        if (lc->previewstream==NULL){
1962                                lc->previewstream=video_preview_start(lc->video_conf.device,
1963                                                        lc->video_conf.vsize);
1964                        }
1965                }else{
1966                        if (lc->previewstream!=NULL){
1967                                video_preview_stop(lc->previewstream);
1968                                lc->previewstream=NULL;
1969                        }
1970                }
1971        }
1972#endif
1973}
1974
1975void linphone_core_enable_video(LinphoneCore *lc, bool_t vcap_enabled, bool_t display_enabled){
1976#ifndef VIDEO_ENABLED
1977        if (vcap_enabled || display_enabled)
1978                ms_warning("This version of linphone was built without video support.");
1979#endif
1980        lc->video_conf.capture=vcap_enabled;
1981        lc->video_conf.display=display_enabled;
1982
1983        /* need to re-apply network bandwidth settings*/
1984        linphone_core_set_download_bandwidth(lc,
1985                linphone_core_get_download_bandwidth(lc));
1986        linphone_core_set_upload_bandwidth(lc,
1987                linphone_core_get_upload_bandwidth(lc));
1988}
1989
1990bool_t linphone_core_video_enabled(LinphoneCore *lc){
1991        return (lc->video_conf.display || lc->video_conf.capture);
1992}
1993
1994void linphone_core_enable_video_preview(LinphoneCore *lc, bool_t val){
1995        lc->video_conf.show_local=val;
1996}
1997
1998bool_t linphone_core_video_preview_enabled(const LinphoneCore *lc){
1999        return lc->video_conf.show_local;
2000}
2001
2002void linphone_core_enable_self_view(LinphoneCore *lc, bool_t val){
2003        lc->video_conf.selfview=val;
2004        if (lc->videostream){
2005                video_stream_enable_self_view(lc->videostream,val);
2006        }
2007}
2008
2009bool_t linphone_core_self_view_enabled(const LinphoneCore *lc){
2010        return lc->video_conf.selfview;
2011}
2012
2013int linphone_core_set_video_device(LinphoneCore *lc, const char *id){
2014        MSWebCam *olddev=lc->video_conf.device;
2015        if (id!=NULL){
2016                lc->video_conf.device=ms_web_cam_manager_get_cam(ms_web_cam_manager_get(),id);
2017                if (lc->video_conf.device==NULL){
2018                        ms_warning("Could not found video device %s",id);
2019                }
2020        }
2021        if (lc->video_conf.device==NULL)
2022                lc->video_conf.device=ms_web_cam_manager_get_default_cam(ms_web_cam_manager_get());
2023        if (olddev!=NULL && olddev!=lc->video_conf.device){
2024                toggle_video_preview(lc,FALSE);/*restart the video local preview*/
2025        }
2026        return 0;
2027}
2028
2029const char *linphone_core_get_video_device(const LinphoneCore *lc){
2030        if (lc->video_conf.device) return ms_web_cam_get_string_id(lc->video_conf.device);
2031        return NULL;
2032}
2033
2034static MSVideoSizeDef supported_resolutions[]={
2035        {       MS_VIDEO_SIZE_SVGA      ,       "svga"  },
2036        {       MS_VIDEO_SIZE_4CIF      ,       "4cif"  },
2037        {       MS_VIDEO_SIZE_VGA       ,       "vga"   },
2038        {       MS_VIDEO_SIZE_CIF       ,       "cif"   },
2039        {       MS_VIDEO_SIZE_QVGA      ,       "qvga"  },
2040        {       MS_VIDEO_SIZE_QCIF      ,       "qcif"  },
2041        {       {0,0}                   ,       NULL    }
2042};
2043
2044const MSVideoSizeDef *linphone_core_get_supported_video_sizes(LinphoneCore *lc){
2045        return supported_resolutions;
2046}
2047
2048static MSVideoSize video_size_get_by_name(const char *name){
2049        MSVideoSizeDef *pdef=supported_resolutions;
2050        for(;pdef->name!=NULL;pdef++){
2051                if (strcasecmp(name,pdef->name)==0){
2052                        return pdef->vsize;
2053                }
2054        }
2055        ms_warning("Video resolution %s is not supported in linphone.",name);
2056        return (MSVideoSize){0,0};
2057}
2058
2059const char *video_size_get_name(MSVideoSize vsize){
2060        MSVideoSizeDef *pdef=supported_resolutions;
2061        for(;pdef->name!=NULL;pdef++){
2062                if (pdef->vsize.width==vsize.width && pdef->vsize.height==vsize.height){
2063                        return pdef->name;
2064                }
2065        }
2066        return NULL;
2067}
2068
2069static bool_t video_size_supported(MSVideoSize vsize){
2070        if (video_size_get_name(vsize)) return TRUE;
2071        ms_warning("Video resolution %ix%i is not supported in linphone.",vsize.width,vsize.height);
2072        return FALSE;
2073}
2074
2075
2076void linphone_core_set_preferred_video_size(LinphoneCore *lc, MSVideoSize vsize){
2077        if (video_size_supported(vsize)){
2078                MSVideoSize oldvsize=lc->video_conf.vsize;
2079                lc->video_conf.vsize=vsize;
2080                if (!ms_video_size_equal(oldvsize,vsize) && lc->previewstream!=NULL){
2081                        toggle_video_preview(lc,FALSE);
2082                        toggle_video_preview(lc,TRUE);
2083                }
2084        }
2085}
2086
2087void linphone_core_set_preferred_video_size_by_name(LinphoneCore *lc, const char *name){
2088        MSVideoSize vsize=video_size_get_by_name(name);
2089        if (vsize.width!=0)     linphone_core_set_preferred_video_size(lc,vsize);
2090        else linphone_core_set_preferred_video_size(lc,MS_VIDEO_SIZE_CIF);
2091}
2092
2093MSVideoSize linphone_core_get_preferred_video_size(LinphoneCore *lc){
2094        return lc->video_conf.vsize;
2095}
2096
2097void linphone_core_use_files(LinphoneCore *lc, bool_t yesno){
2098        lc->use_files=yesno;
2099}
2100
2101void linphone_core_set_play_file(LinphoneCore *lc, const char *file){
2102        if (lc->play_file!=NULL){
2103                ms_free(lc->play_file);
2104                lc->play_file=NULL;
2105        }
2106        if (file!=NULL) {
2107                lc->play_file=ms_strdup(file);
2108                if (lc->audiostream)
2109                        audio_stream_play(lc->audiostream,file);
2110        }
2111}
2112
2113void linphone_core_set_record_file(LinphoneCore *lc, const char *file){
2114        if (lc->rec_file!=NULL){
2115                ms_free(lc->rec_file);
2116                lc->rec_file=NULL;
2117        }
2118        if (file!=NULL) {
2119                lc->rec_file=ms_strdup(file);
2120                if (lc->audiostream) 
2121                        audio_stream_record(lc->audiostream,file);
2122        }
2123}
2124
2125
2126void *linphone_core_get_user_data(LinphoneCore *lc){
2127        return lc->data;
2128}
2129
2130int linphone_core_get_mtu(const LinphoneCore *lc){
2131        return lc->net_conf.mtu;
2132}
2133
2134void linphone_core_set_mtu(LinphoneCore *lc, int mtu){
2135        lc->net_conf.mtu=mtu;
2136        if (mtu>0){
2137                if (mtu<500){
2138                        ms_error("MTU too small !");
2139                        mtu=500;
2140                }
2141                ms_set_mtu(mtu);
2142                ms_message("MTU is supposed to be %i, rtp payload max size will be %i",mtu, ms_get_payload_max_size());
2143        }else ms_set_mtu(0);//use mediastreamer2 default value
2144}
2145
2146void net_config_uninit(LinphoneCore *lc)
2147{
2148        net_config_t *config=&lc->net_conf;
2149        lp_config_set_int(lc->config,"net","download_bw",config->download_bw);
2150        lp_config_set_int(lc->config,"net","upload_bw",config->upload_bw);
2151       
2152        if (config->stun_server!=NULL)
2153                lp_config_set_string(lc->config,"net","stun_server",config->stun_server);
2154        if (config->nat_address!=NULL)
2155                lp_config_set_string(lc->config,"net","nat_address",config->nat_address);
2156        lp_config_set_int(lc->config,"net","firewall_policy",config->firewall_policy);
2157        lp_config_set_int(lc->config,"net","mtu",config->mtu);
2158}
2159
2160
2161void sip_config_uninit(LinphoneCore *lc)
2162{
2163        MSList *elem;
2164        int i;
2165        sip_config_t *config=&lc->sip_conf;
2166        lp_config_set_int(lc->config,"sip","sip_port",config->sip_port);
2167        lp_config_set_int(lc->config,"sip","guess_hostname",config->guess_hostname);
2168        lp_config_set_string(lc->config,"sip","contact",config->contact);
2169        lp_config_set_int(lc->config,"sip","inc_timeout",config->inc_timeout);
2170        lp_config_set_int(lc->config,"sip","use_info",config->use_info);
2171        lp_config_set_int(lc->config,"sip","use_ipv6",config->ipv6_enabled);
2172        for(elem=config->proxies,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
2173                LinphoneProxyConfig *cfg=(LinphoneProxyConfig*)(elem->data);
2174                linphone_proxy_config_write_to_config_file(lc->config,cfg,i);
2175                linphone_proxy_config_edit(cfg);        /* to unregister */
2176        }
2177
2178        if (exosip_running)
2179          {
2180            int i;
2181            for (i=0;i<20;i++)
2182              {
2183                eXosip_event_t *ev;
2184                while((ev=eXosip_event_wait(0,0))!=NULL){
2185                  linphone_core_process_event(lc,ev);
2186                }
2187                eXosip_automatic_action();
2188#ifndef WIN32
2189                usleep(100000);
2190#else
2191        Sleep(100);
2192#endif
2193              }
2194          }
2195       
2196        linphone_proxy_config_write_to_config_file(lc->config,NULL,i);  /*mark the end */
2197       
2198        for(elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
2199                LinphoneAuthInfo *ai=(LinphoneAuthInfo*)(elem->data);
2200                linphone_auth_info_write_config(lc->config,ai,i);
2201        }
2202        linphone_auth_info_write_config(lc->config,NULL,i); /* mark the end */
2203}
2204
2205void rtp_config_uninit(LinphoneCore *lc)
2206{
2207        rtp_config_t *config=&lc->rtp_conf;
2208        lp_config_set_int(lc->config,"rtp","audio_rtp_port",config->audio_rtp_port);
2209        lp_config_set_int(lc->config,"rtp","video_rtp_port",config->video_rtp_port);
2210        lp_config_set_int(lc->config,"rtp","audio_jitt_comp",config->audio_jitt_comp);
2211        lp_config_set_int(lc->config,"rtp","video_jitt_comp",config->audio_jitt_comp);
2212        lp_config_set_int(lc->config,"rtp","nortp_timeout",config->nortp_timeout);
2213}
2214
2215void sound_config_uninit(LinphoneCore *lc)
2216{
2217        /*char tmpbuf[2];*/
2218        sound_config_t *config=&lc->sound_conf;
2219        lp_config_set_string(lc->config,"sound","playback_dev_id",ms_snd_card_get_string_id(config->play_sndcard));
2220        lp_config_set_string(lc->config,"sound","ringer_dev_id",ms_snd_card_get_string_id(config->ring_sndcard));
2221        lp_config_set_string(lc->config,"sound","capture_dev_id",ms_snd_card_get_string_id(config->capt_sndcard));
2222        ms_free(config->cards);
2223        /*
2224        lp_config_set_int(lc->config,"sound","rec_lev",config->rec_lev);
2225        lp_config_set_int(lc->config,"sound","play_lev",config->play_lev);
2226        lp_config_set_int(lc->config,"sound","ring_lev",config->ring_lev);
2227        tmpbuf[0]=config->source;
2228        tmpbuf[1]='\0';
2229        lp_config_set_string(lc->config,"sound","source",tmpbuf);
2230        */
2231        lp_config_set_string(lc->config,"sound","local_ring",config->local_ring);
2232        lp_config_set_string(lc->config,"sound","remote_ring",config->remote_ring);
2233        lp_config_set_int(lc->config,"sound","echocancelation",config->ec);
2234        if (config->local_ring) ms_free(config->local_ring);
2235        if (config->remote_ring) ms_free(config->remote_ring);
2236}
2237
2238void video_config_uninit(LinphoneCore *lc)
2239{
2240        video_config_t *config=&lc->video_conf;
2241        const char *vd=linphone_core_get_video_device(lc);
2242        if (vd && strstr(vd,"Static picture")!=NULL){
2243                vd=NULL;
2244        }
2245        lp_config_set_string(lc->config,"video","device",vd);
2246        lp_config_set_int(lc->config,"video","display",config->display);
2247        lp_config_set_int(lc->config,"video","capture",config->capture);
2248        lp_config_set_int(lc->config,"video","show_local",config->show_local);
2249        lp_config_set_string(lc->config,"video","size",video_size_get_name(config->vsize));
2250}
2251
2252void codecs_config_uninit(LinphoneCore *lc)
2253{
2254        PayloadType *pt;
2255        codecs_config_t *config=&lc->codecs_conf;
2256        MSList *node;
2257        char key[50];
2258        int index;
2259        index=0;
2260        for(node=config->audio_codecs;node!=NULL;node=ms_list_next(node)){
2261                pt=(PayloadType*)(node->data);
2262                sprintf(key,"audio_codec_%i",index);
2263                lp_config_set_string(lc->config,key,"mime",pt->mime_type);
2264                lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
2265                lp_config_set_int(lc->config,key,"enabled",payload_type_enabled(pt));
2266                index++;
2267        }
2268        index=0;
2269        for(node=config->video_codecs;node!=NULL;node=ms_list_next(node)){
2270                pt=(PayloadType*)(node->data);
2271                sprintf(key,"video_codec_%i",index);
2272                lp_config_set_string(lc->config,key,"mime",pt->mime_type);
2273                lp_config_set_int(lc->config,key,"rate",pt->clock_rate);
2274                lp_config_set_int(lc->config,key,"enabled",payload_type_enabled(pt));
2275                lp_config_set_string(lc->config,key,"recv_fmtp",pt->recv_fmtp);
2276                index++;
2277        }
2278}
2279
2280void ui_config_uninit(LinphoneCore* lc)
2281{
2282        MSList *elem;
2283        int i;
2284        for (elem=lc->friends,i=0; elem!=NULL; elem=ms_list_next(elem),i++){
2285                linphone_friend_write_to_config_file(lc->config,(LinphoneFriend*)elem->data,i);
2286                linphone_friend_destroy(elem->data);
2287        }
2288        linphone_friend_write_to_config_file(lc->config,NULL,i);        /* set the end */
2289        ms_list_free(lc->friends);
2290        lc->friends=NULL;
2291}
2292
2293LpConfig *linphone_core_get_config(LinphoneCore *lc){
2294        return lc->config;
2295}
2296
2297void linphone_core_uninit(LinphoneCore *lc)
2298{
2299        gstate_new_state(lc, GSTATE_POWER_SHUTDOWN, NULL);
2300#ifdef VIDEO_ENABLED
2301        if (lc->previewstream!=NULL){
2302                video_preview_stop(lc->previewstream);
2303                lc->previewstream=NULL;
2304        }
2305#endif
2306        /* save all config */
2307        net_config_uninit(lc);
2308        sip_config_uninit(lc);
2309        lp_config_set_int(lc->config,"sip","default_proxy",linphone_core_get_default_proxy(lc,NULL));
2310        rtp_config_uninit(lc);
2311        sound_config_uninit(lc);
2312        video_config_uninit(lc);
2313        codecs_config_uninit(lc);
2314        ui_config_uninit(lc);
2315        lp_config_sync(lc->config);
2316        lp_config_destroy(lc->config);
2317
2318#ifdef VIDEO_ENABLED
2319        if (payload_type_h264_packetization_mode_1!=NULL)
2320                payload_type_destroy(payload_type_h264_packetization_mode_1);
2321#endif
2322       
2323        ortp_exit();
2324        eXosip_quit();
2325        exosip_running=FALSE;
2326        gstate_new_state(lc, GSTATE_POWER_OFF, NULL);
2327}
2328
2329void linphone_core_destroy(LinphoneCore *lc){
2330        linphone_core_uninit(lc);
2331        ms_free(lc);
2332}
Note: See TracBrowser for help on using the repository browser.