linphonecore.c 115 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
linphone
Copyright (C) 2000  Simon MORLAT (simon.morlat@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "linphonecore.h"
smorlat's avatar
smorlat committed
21
#include "sipsetup.h"
aymeric's avatar
aymeric committed
22 23
#include "lpconfig.h"
#include "private.h"
24 25

#include <ortp/telephonyevents.h>
aymeric's avatar
aymeric committed
26
#include "mediastreamer2/mediastream.h"
27
#include "mediastreamer2/mseventqueue.h"
smorlat's avatar
smorlat committed
28
#include "mediastreamer2/msvolume.h"
29
#include "mediastreamer2/msequalizer.h"
30
#include "mediastreamer2/dtmfgen.h"
aymeric's avatar
aymeric committed
31

32
#ifdef INET6
aymeric's avatar
aymeric committed
33
#ifndef WIN32
34
#include <netdb.h>
aymeric's avatar
aymeric committed
35 36 37
#endif
#endif

smorlat's avatar
smorlat committed
38
/*#define UNSTANDART_GSM_11K 1*/
aymeric's avatar
aymeric committed
39 40

static const char *liblinphone_version=LIBLINPHONE_VERSION;
41
static void set_network_reachable(LinphoneCore* lc,bool_t isReachable);
aymeric's avatar
aymeric committed
42 43 44 45 46 47 48 49 50

#include "enum.h"

void linphone_core_get_local_ip(LinphoneCore *lc, const char *dest, char *result);
static void toggle_video_preview(LinphoneCore *lc, bool_t val);

/* relative path where is stored local ring*/
#define LOCAL_RING "rings/oldphone.wav"
/* same for remote ring (ringback)*/
51
#define REMOTE_RING "ringback.wav"
aymeric's avatar
aymeric committed
52

Simon Morlat's avatar
Simon Morlat committed
53
extern SalCallbacks linphone_sal_callbacks;
aymeric's avatar
aymeric committed
54 55 56 57 58 59 60 61 62 63 64 65

void lc_callback_obj_init(LCCallbackObj *obj,LinphoneCoreCbFunc func,void* ud)
{
  obj->_func=func;
  obj->_user_data=ud;
}

int lc_callback_obj_invoke(LCCallbackObj *obj, LinphoneCore *lc){
	if (obj->_func!=NULL) obj->_func(lc,obj->_user_data);
	return 0;
}

66
		
aymeric's avatar
aymeric committed
67 68
/*prevent a gcc bug with %c*/
static size_t my_strftime(char *s, size_t max, const char  *fmt,  const struct tm *tm){
Jehan Monnier's avatar
Jehan Monnier committed
69
#if !defined(_WIN32_WCE)
aymeric's avatar
aymeric committed
70
	return strftime(s, max, fmt, tm);
Jehan Monnier's avatar
Jehan Monnier committed
71 72 73 74
#else
	return 0;
	/*FIXME*/
#endif /*_WIN32_WCE*/
aymeric's avatar
aymeric committed
75 76
}

77 78 79 80
static void set_call_log_date(LinphoneCallLog *cl, const struct tm *loctime){
	my_strftime(cl->start_date,sizeof(cl->start_date),"%c",loctime);
}

81
LinphoneCallLog * linphone_call_log_new(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
aymeric's avatar
aymeric committed
82 83 84 85
	LinphoneCallLog *cl=ms_new0(LinphoneCallLog,1);
	struct tm loctime;
	cl->dir=call->dir;
#ifdef WIN32
Jehan Monnier's avatar
Jehan Monnier committed
86
#if !defined(_WIN32_WCE)
aymeric's avatar
aymeric committed
87
	loctime=*localtime(&call->start_time);
Jehan Monnier's avatar
Jehan Monnier committed
88 89
	/*FIXME*/
#endif /*_WIN32_WCE*/
aymeric's avatar
aymeric committed
90 91 92
#else
	localtime_r(&call->start_time,&loctime);
#endif
93
	set_call_log_date(cl,&loctime);
aymeric's avatar
aymeric committed
94 95 96 97
	cl->from=from;
	cl->to=to;
	return cl;
}
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

static void call_logs_write_to_config_file(LinphoneCore *lc){
	MSList *elem;
	char logsection[32];
	int i;
	char *tmp;
	LpConfig *cfg=lc->config;

	if (!lc->ready) return;
	
	for(i=0,elem=lc->call_logs;elem!=NULL;elem=elem->next,++i){
		LinphoneCallLog *cl=(LinphoneCallLog*)elem->data;
		snprintf(logsection,sizeof(logsection),"call_log_%i",i);
		lp_config_set_int(cfg,logsection,"dir",cl->dir);
		lp_config_set_int(cfg,logsection,"status",cl->status);
		tmp=linphone_address_as_string(cl->from);
		lp_config_set_string(cfg,logsection,"from",tmp);
		ms_free(tmp);
		tmp=linphone_address_as_string(cl->to);
		lp_config_set_string(cfg,logsection,"to",tmp);
		ms_free(tmp);
		lp_config_set_string(cfg,logsection,"start_date",cl->start_date);
		lp_config_set_int(cfg,logsection,"duration",cl->duration);
		if (cl->refkey) lp_config_set_string(cfg,logsection,"refkey",cl->refkey);
	}
	for(;i<lc->max_call_logs;++i){
		snprintf(logsection,sizeof(logsection),"call_log_%i",i);
		lp_config_clean_section(cfg,logsection);
	}
}

static void call_logs_read_from_config_file(LinphoneCore *lc){
	char logsection[32];
	int i;
	const char *tmp;
	LpConfig *cfg=lc->config;
	for(i=0;;++i){
		snprintf(logsection,sizeof(logsection),"call_log_%i",i);
		if (lp_config_has_section(cfg,logsection)){
			LinphoneCallLog *cl=ms_new0(LinphoneCallLog,1);
			cl->dir=lp_config_get_int(cfg,logsection,"dir",0);
			cl->status=lp_config_get_int(cfg,logsection,"status",0);
			tmp=lp_config_get_string(cfg,logsection,"from",NULL);
			if (tmp) cl->from=linphone_address_new(tmp);
			tmp=lp_config_get_string(cfg,logsection,"to",NULL);
			if (tmp) cl->to=linphone_address_new(tmp);
			tmp=lp_config_get_string(cfg,logsection,"start_date",NULL);
			if (tmp) strncpy(cl->start_date,tmp,sizeof(cl->start_date));
			cl->duration=lp_config_get_int(cfg,logsection,"duration",0);
			tmp=lp_config_get_string(cfg,logsection,"refkey",NULL);
			if (tmp) cl->refkey=ms_strdup(tmp);
			lc->call_logs=ms_list_append(lc->call_logs,cl);
		}else break;	
	}
}


155
void linphone_call_log_completed(LinphoneCallLog *calllog, LinphoneCall *call, LinphoneCallStatus status){
aymeric's avatar
aymeric committed
156
	LinphoneCore *lc=call->core;
157
	
aymeric's avatar
aymeric committed
158
	calllog->duration=time(NULL)-call->start_time;
159 160 161 162 163 164 165
	
	if (status==LinphoneCallMissed){
		char *info;
		lc->missed_calls++;
		info=ortp_strdup_printf(ngettext("You have missed %i call.",
                    "You have missed %i calls.", lc->missed_calls),
                lc->missed_calls);
Simon Morlat's avatar
Simon Morlat committed
166 167
				if (lc->vtable.display_status!=NULL)
					lc->vtable.display_status(lc,info);
168 169
		ms_free(info);
	}else calllog->status=status;
Simon Morlat's avatar
Simon Morlat committed
170
	lc->call_logs=ms_list_prepend(lc->call_logs,(void *)calllog);
aymeric's avatar
aymeric committed
171
	if (ms_list_size(lc->call_logs)>lc->max_call_logs){
Simon Morlat's avatar
Simon Morlat committed
172 173 174 175 176 177
		MSList *elem,*prevelem=NULL;
		/*find the last element*/
		for(elem=lc->call_logs;elem!=NULL;elem=elem->next){
			prevelem=elem;
		}
		elem=prevelem;
aymeric's avatar
aymeric committed
178 179 180 181 182 183
		linphone_call_log_destroy((LinphoneCallLog*)elem->data);
		lc->call_logs=ms_list_remove_link(lc->call_logs,elem);
	}
	if (lc->vtable.call_log_updated!=NULL){
		lc->vtable.call_log_updated(lc,calllog);
	}
184
	call_logs_write_to_config_file(lc);
aymeric's avatar
aymeric committed
185 186
}

smorlat's avatar
smorlat committed
187 188 189 190 191 192 193 194 195 196
/**
 * @addtogroup call_logs
 * @{
**/

/**
 * Returns a human readable string describing the call.
 * 
 * @note: the returned char* must be freed by the application (use ms_free()).
**/
aymeric's avatar
aymeric committed
197 198
char * linphone_call_log_to_str(LinphoneCallLog *cl){
	char *status;
199
	char *tmp;
200 201
	char *from=linphone_address_as_string (cl->from);
	char *to=linphone_address_as_string (cl->to);
aymeric's avatar
aymeric committed
202 203 204 205 206 207 208 209 210 211 212 213 214
	switch(cl->status){
		case LinphoneCallAborted:
			status=_("aborted");
			break;
		case LinphoneCallSuccess:
			status=_("completed");
			break;
		case LinphoneCallMissed:
			status=_("missed");
			break;
		default:
			status="unknown";
	}
215
	tmp=ortp_strdup_printf(_("%s at %s\nFrom: %s\nTo: %s\nStatus: %s\nDuration: %i mn %i sec\n"),
aymeric's avatar
aymeric committed
216 217
			(cl->dir==LinphoneCallIncoming) ? _("Incoming call") : _("Outgoing call"),
			cl->start_date,
218 219
			from,
			to,
aymeric's avatar
aymeric committed
220 221 222
			status,
			cl->duration/60,
			cl->duration%60);
223 224 225
	ms_free(from);
	ms_free(to);
	return tmp;
aymeric's avatar
aymeric committed
226 227
}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
/**
 * Returns RTP statistics computed locally regarding the call.
 * 
**/
const rtp_stats_t *linphone_call_log_get_local_stats(const LinphoneCallLog *cl){
	return &cl->local_stats;
}

/**
 * Returns RTP statistics computed by remote end and sent back via RTCP.
 *
 * @note Not implemented yet.
**/
const rtp_stats_t *linphone_call_log_get_remote_stats(const LinphoneCallLog *cl){
	return &cl->remote_stats;
}

smorlat's avatar
smorlat committed
245 246 247 248 249 250 251 252
void linphone_call_log_set_user_pointer(LinphoneCallLog *cl, void *up){
	cl->user_pointer=up;
}

void *linphone_call_log_get_user_pointer(const LinphoneCallLog *cl){
	return cl->user_pointer;
}

smorlat's avatar
smorlat committed
253 254


255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
/**
 * Associate a persistent reference key to the call log.
 *
 * The reference key can be for example an id to an external database.
 * It is stored in the config file, thus can survive to process exits/restarts.
 *
**/
void linphone_call_log_set_ref_key(LinphoneCallLog *cl, const char *refkey){
	if (cl->refkey!=NULL){
		ms_free(cl->refkey);
		cl->refkey=NULL;
	}
	if (refkey) cl->refkey=ms_strdup(refkey);
	call_logs_write_to_config_file(cl->lc);
}

/**
 * Get the persistent reference key associated to the call log.
 *
 * The reference key can be for example an id to an external database.
 * It is stored in the config file, thus can survive to process exits/restarts.
 *
**/
const char *linphone_call_log_get_ref_key(const LinphoneCallLog *cl){
	return cl->refkey;
}

smorlat's avatar
smorlat committed
282 283
/** @} */

aymeric's avatar
aymeric committed
284
void linphone_call_log_destroy(LinphoneCallLog *cl){
285 286 287
	if (cl->from!=NULL) linphone_address_destroy(cl->from);
	if (cl->to!=NULL) linphone_address_destroy(cl->to);
	if (cl->refkey!=NULL) ms_free(cl->refkey);
aymeric's avatar
aymeric committed
288 289 290
	ms_free(cl);
}

291 292 293 294 295 296 297 298 299 300 301 302
/**
 * Returns TRUE if the LinphoneCall asked to autoanswer
 *
**/
bool_t linphone_call_asked_to_autoanswer(LinphoneCall *call){
	//return TRUE if the unique(for the moment) incoming call asked to be autoanswered
	if(call)
		return sal_call_autoanswer_asked(call->op);
	else
		return FALSE;
}

303 304
int linphone_core_get_current_call_duration(const LinphoneCore *lc){
	LinphoneCall *call=linphone_core_get_current_call((LinphoneCore *)lc);
305 306
	if (call)  return linphone_call_get_duration(call);
	return -1;
307 308 309 310
}

const LinphoneAddress *linphone_core_get_current_call_remote_address(struct _LinphoneCore *lc){
	LinphoneCall *call=linphone_core_get_current_call(lc);
Simon Morlat's avatar
Simon Morlat committed
311
	if (call==NULL) return NULL;
312
	return linphone_call_get_remote_address(call);
smorlat's avatar
smorlat committed
313 314
}

315 316 317 318 319 320 321 322
/**
 * Enable logs in supplied FILE*.
 *
 * @ingroup misc
 *
 * @param file a C FILE* where to fprintf logs. If null stdout is used.
 * 
**/
aymeric's avatar
aymeric committed
323 324 325 326 327 328
void linphone_core_enable_logs(FILE *file){
	if (file==NULL) file=stdout;
	ortp_set_log_file(file);
	ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
}

329 330 331 332 333 334 335 336 337
/**
 * Enable logs through the user's supplied log callback.
 *
 * @ingroup misc
 *
 * @param logfunc The address of a OrtpLogFunc callback whose protoype is
 *            	  typedef void (*OrtpLogFunc)(OrtpLogLevel lev, const char *fmt, va_list args);
 * 
**/
aymeric's avatar
aymeric committed
338 339 340 341 342
void linphone_core_enable_logs_with_cb(OrtpLogFunc logfunc){
	ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR|ORTP_FATAL);
	ortp_set_log_handler(logfunc);
}

343 344 345 346 347
/**
 * Entirely disable logging.
 *
 * @ingroup misc
**/
aymeric's avatar
aymeric committed
348 349 350 351 352
void linphone_core_disable_logs(){
	ortp_set_log_level_mask(ORTP_ERROR|ORTP_FATAL);
}


353
static void net_config_read (LinphoneCore *lc)
aymeric's avatar
aymeric committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
{
	int tmp;
	const char *tmpstr;
	LpConfig *config=lc->config;

	tmp=lp_config_get_int(config,"net","download_bw",0);
	linphone_core_set_download_bandwidth(lc,tmp);
	tmp=lp_config_get_int(config,"net","upload_bw",0);
	linphone_core_set_upload_bandwidth(lc,tmp);
	linphone_core_set_stun_server(lc,lp_config_get_string(config,"net","stun_server",NULL));
	tmpstr=lp_config_get_string(lc->config,"net","nat_address",NULL);
	if (tmpstr!=NULL && (strlen(tmpstr)<1)) tmpstr=NULL;
	linphone_core_set_nat_address(lc,tmpstr);
	tmp=lp_config_get_int(lc->config,"net","firewall_policy",0);
	linphone_core_set_firewall_policy(lc,tmp);
	tmp=lp_config_get_int(lc->config,"net","nat_sdp_only",0);
	lc->net_conf.nat_sdp_only=tmp;
	tmp=lp_config_get_int(lc->config,"net","mtu",0);
	linphone_core_set_mtu(lc,tmp);
jehan's avatar
jehan committed
373 374 375
	tmp=lp_config_get_int(lc->config,"net","download_ptime",0);
	linphone_core_set_download_ptime(lc,tmp);

aymeric's avatar
aymeric committed
376 377
}

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
static void build_sound_devices_table(LinphoneCore *lc){
	const char **devices;
	const char **old;
	int ndev;
	int i;
	const MSList *elem=ms_snd_card_manager_get_list(ms_snd_card_manager_get());
	ndev=ms_list_size(elem);
	devices=ms_malloc((ndev+1)*sizeof(const char *));
	for (i=0;elem!=NULL;elem=elem->next,i++){
		devices[i]=ms_snd_card_get_string_id((MSSndCard *)elem->data);
	}
	devices[ndev]=NULL;
	old=lc->sound_conf.cards;
	lc->sound_conf.cards=devices;
	if (old!=NULL) ms_free(old);
}
aymeric's avatar
aymeric committed
394

395
static void sound_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
396 397 398 399
{
	/*int tmp;*/
	const char *tmpbuf;
	const char *devid;
400
	float gain=0;
401
#ifdef __linux
aymeric's avatar
aymeric committed
402 403 404 405 406 407 408 409
	/*alsadev let the user use custom alsa device within linphone*/
	devid=lp_config_get_string(lc->config,"sound","alsadev",NULL);
	if (devid){
		MSSndCard *card=ms_alsa_card_new_custom(devid,devid);
		ms_snd_card_manager_add_card(ms_snd_card_manager_get(),card);
	}
#endif
	/* retrieve all sound devices */
410 411
	build_sound_devices_table(lc);

aymeric's avatar
aymeric committed
412 413
	devid=lp_config_get_string(lc->config,"sound","playback_dev_id",NULL);
	linphone_core_set_playback_device(lc,devid);
414

aymeric's avatar
aymeric committed
415 416
	devid=lp_config_get_string(lc->config,"sound","ringer_dev_id",NULL);
	linphone_core_set_ringer_device(lc,devid);
417

aymeric's avatar
aymeric committed
418 419
	devid=lp_config_get_string(lc->config,"sound","capture_dev_id",NULL);
	linphone_core_set_capture_device(lc,devid);
420

aymeric's avatar
aymeric committed
421 422 423 424 425 426 427 428 429 430
/*
	tmp=lp_config_get_int(lc->config,"sound","play_lev",80);
	linphone_core_set_play_level(lc,tmp);
	tmp=lp_config_get_int(lc->config,"sound","ring_lev",80);
	linphone_core_set_ring_level(lc,tmp);
	tmp=lp_config_get_int(lc->config,"sound","rec_lev",80);
	linphone_core_set_rec_level(lc,tmp);
	tmpbuf=lp_config_get_string(lc->config,"sound","source","m");
	linphone_core_set_sound_source(lc,tmpbuf[0]);
*/
431

aymeric's avatar
aymeric committed
432 433
	tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
	tmpbuf=lp_config_get_string(lc->config,"sound","local_ring",tmpbuf);
Jehan Monnier's avatar
Jehan Monnier committed
434
	if (ortp_file_exist(tmpbuf)==-1) {
aymeric's avatar
aymeric committed
435 436 437 438 439 440
		tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
	}
	if (strstr(tmpbuf,".wav")==NULL){
		/* it currently uses old sound files, so replace them */
		tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
	}
441

aymeric's avatar
aymeric committed
442
	linphone_core_set_ring(lc,tmpbuf);
443

444
	tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
aymeric's avatar
aymeric committed
445
	tmpbuf=lp_config_get_string(lc->config,"sound","remote_ring",tmpbuf);
Jehan Monnier's avatar
Jehan Monnier committed
446
	if (ortp_file_exist(tmpbuf)==-1){
447
		tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
aymeric's avatar
aymeric committed
448 449 450
	}
	if (strstr(tmpbuf,".wav")==NULL){
		/* it currently uses old sound files, so replace them */
451
		tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
aymeric's avatar
aymeric committed
452
	}
453
	linphone_core_set_ringback(lc,tmpbuf);
aymeric's avatar
aymeric committed
454 455 456
	check_sound_device(lc);
	lc->sound_conf.latency=0;

457 458 459 460
	linphone_core_enable_echo_cancellation(lc,
	    lp_config_get_int(lc->config,"sound","echocancelation",0) |
	    lp_config_get_int(lc->config,"sound","echocancellation",0)
		);
smorlat's avatar
smorlat committed
461 462 463

	linphone_core_enable_echo_limiter(lc,
		lp_config_get_int(lc->config,"sound","echolimiter",0));
464 465
	linphone_core_enable_agc(lc,
		lp_config_get_int(lc->config,"sound","agc",0));
466

467 468
	gain=lp_config_get_float(lc->config,"sound","playback_gain_db",0);
	linphone_core_set_playback_gain_db (lc,gain);
469 470

	linphone_core_set_remote_ringback_tone (lc,lp_config_get_string(lc->config,"sound","ringback_tone",NULL));
aymeric's avatar
aymeric committed
471 472
}

473
static void sip_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
474 475 476
{
	char *contact;
	const char *tmpstr;
477
	LCSipTransports tr;
aymeric's avatar
aymeric committed
478 479
	int i,tmp;
	int ipv6;
480

481 482
	tmp=lp_config_get_int(lc->config,"sip","use_info",0);
	linphone_core_set_use_info_for_dtmf(lc,tmp);
aymeric's avatar
aymeric committed
483

484 485 486
	if (lp_config_get_int(lc->config,"sip","use_session_timers",0)==1){
		sal_use_session_timers(lc->sal,200);
	}
aymeric's avatar
aymeric committed
487

488

489 490
	tmp=lp_config_get_int(lc->config,"sip","use_rfc2833",0);
	linphone_core_set_use_rfc2833_for_dtmf(lc,tmp);
491

aymeric's avatar
aymeric committed
492 493 494 495
	ipv6=lp_config_get_int(lc->config,"sip","use_ipv6",-1);
	if (ipv6==-1){
		ipv6=0;
		if (host_has_ipv6_network()){
496 497
			if (lc->vtable.display_message)
				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"));
aymeric's avatar
aymeric committed
498 499 500
		}
	}
	linphone_core_enable_ipv6(lc,ipv6);
501
	memset(&tr,0,sizeof(tr));
jehan's avatar
jehan committed
502 503 504 505 506 507 508 509 510 511
	if (lp_config_get_int(lc->config,"sip","sip_random_port",0)) {
		tr.udp_port=(0xDFF&+random())+1024;
	} else {
		tr.udp_port=lp_config_get_int(lc->config,"sip","sip_port",5060);
	}
	if (lp_config_get_int(lc->config,"sip","sip_tcp_random_port",0)) {
		tr.tcp_port=(0xDFF&+random())+1024;
	} else {
		tr.tcp_port=lp_config_get_int(lc->config,"sip","sip_tcp_port",0);
	}
512 513
	/*start listening on ports*/
 	linphone_core_set_sip_transports(lc,&tr);
514

aymeric's avatar
aymeric committed
515 516
	tmpstr=lp_config_get_string(lc->config,"sip","contact",NULL);
	if (tmpstr==NULL || linphone_core_set_primary_contact(lc,tmpstr)==-1) {
smorlat's avatar
smorlat committed
517 518
		const char *hostname=NULL;
		const char *username=NULL;
519
#ifdef HAVE_GETENV
smorlat's avatar
smorlat committed
520 521
		hostname=getenv("HOST");
		username=getenv("USER");
aymeric's avatar
aymeric committed
522
		if (hostname==NULL) hostname=getenv("HOSTNAME");
523
#endif /*HAVE_GETENV*/
aymeric's avatar
aymeric committed
524 525 526 527 528 529 530 531 532 533 534 535
		if (hostname==NULL)
			hostname="unknown-host";
		if (username==NULL){
			username="toto";
		}
		contact=ortp_strdup_printf("sip:%s@%s",username,hostname);
		linphone_core_set_primary_contact(lc,contact);
		ms_free(contact);
	}

	tmp=lp_config_get_int(lc->config,"sip","guess_hostname",1);
	linphone_core_set_guess_hostname(lc,tmp);
536 537


aymeric's avatar
aymeric committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
	tmp=lp_config_get_int(lc->config,"sip","inc_timeout",15);
	linphone_core_set_inc_timeout(lc,tmp);

	/* get proxies config */
	for(i=0;; i++){
		LinphoneProxyConfig *cfg=linphone_proxy_config_new_from_config_file(lc->config,i);
		if (cfg!=NULL){
			linphone_core_add_proxy_config(lc,cfg);
		}else{
			break;
		}
	}
	/* get the default proxy */
	tmp=lp_config_get_int(lc->config,"sip","default_proxy",-1);
	linphone_core_set_default_proxy_index(lc,tmp);
553

aymeric's avatar
aymeric committed
554 555 556 557 558
	/* read authentication information */
	for(i=0;; i++){
		LinphoneAuthInfo *ai=linphone_auth_info_new_from_config_file(lc->config,i);
		if (ai!=NULL){
			linphone_core_add_auth_info(lc,ai);
Simon Morlat's avatar
Simon Morlat committed
559
			linphone_auth_info_destroy(ai);
aymeric's avatar
aymeric committed
560 561 562 563
		}else{
			break;
		}
	}
jehan's avatar
jehan committed
564 565 566 567 568




	lc->sip_conf.sdp_200_ack=lp_config_get_int(lc->config,"sip","sdp_200_ack",0);
569
	
570
	/*for tuning or test*/
aymeric's avatar
aymeric committed
571
	lc->sip_conf.sdp_200_ack=lp_config_get_int(lc->config,"sip","sdp_200_ack",0);
smorlat's avatar
smorlat committed
572
	lc->sip_conf.only_one_codec=lp_config_get_int(lc->config,"sip","only_one_codec",0);
573
	lc->sip_conf.register_only_when_network_is_up=
574
		lp_config_get_int(lc->config,"sip","register_only_when_network_is_up",1);
575
	lc->sip_conf.ping_with_options=lp_config_get_int(lc->config,"sip","ping_with_options",1);
jehan's avatar
jehan committed
576
	lc->sip_conf.auto_net_state_mon=lp_config_get_int(lc->config,"sip","auto_net_state_mon",1);
577 578
	lc->sip_conf.keepalive_period=lp_config_get_int(lc->config,"sip","keepalive_period",10000);
	sal_set_keepalive_period(lc->sal,lc->sip_conf.keepalive_period);
aymeric's avatar
aymeric committed
579 580
}

581
static void rtp_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
582 583 584 585
{
	int port;
	int jitt_comp;
	int nortp_timeout;
Simon Morlat's avatar
Simon Morlat committed
586 587
	bool_t rtp_no_xmit_on_audio_mute;

aymeric's avatar
aymeric committed
588 589
	port=lp_config_get_int(lc->config,"rtp","audio_rtp_port",7078);
	linphone_core_set_audio_port(lc,port);
590

aymeric's avatar
aymeric committed
591 592 593
	port=lp_config_get_int(lc->config,"rtp","video_rtp_port",9078);
	if (port==0) port=9078;
	linphone_core_set_video_port(lc,port);
594

aymeric's avatar
aymeric committed
595
	jitt_comp=lp_config_get_int(lc->config,"rtp","audio_jitt_comp",60);
596
	linphone_core_set_audio_jittcomp(lc,jitt_comp);
aymeric's avatar
aymeric committed
597 598
	jitt_comp=lp_config_get_int(lc->config,"rtp","video_jitt_comp",60);
	nortp_timeout=lp_config_get_int(lc->config,"rtp","nortp_timeout",30);
599
	linphone_core_set_nortp_timeout(lc,nortp_timeout);
Simon Morlat's avatar
Simon Morlat committed
600 601
	rtp_no_xmit_on_audio_mute=lp_config_get_int(lc->config,"rtp","rtp_no_xmit_on_audio_mute",FALSE);
	linphone_core_set_rtp_no_xmit_on_audio_mute(lc,rtp_no_xmit_on_audio_mute);	
aymeric's avatar
aymeric committed
602 603
}

604 605 606 607 608 609 610 611
static PayloadType * find_payload(RtpProfile *prof, const char *mime_type, int clock_rate, const char *recv_fmtp){
	PayloadType *candidate=NULL;
	int i;
	PayloadType *it;
	for(i=0;i<127;++i){
		it=rtp_profile_get_payload(prof,i);
		if (it!=NULL && strcasecmp(mime_type,it->mime_type)==0
			&& (clock_rate==it->clock_rate || clock_rate<=0) ){
612
			if ( (recv_fmtp && it->recv_fmtp && strstr(recv_fmtp,it->recv_fmtp)!=NULL) ||
613 614
				(recv_fmtp==NULL && it->recv_fmtp==NULL) ){
				/*exact match*/
615
				if (recv_fmtp) payload_type_set_recv_fmtp(it,recv_fmtp);
616
				return it;
617 618 619 620 621
			}else {
				if (candidate){
					if (it->recv_fmtp==NULL) candidate=it;
				}else candidate=it;
			}
622 623
		}
	}
624 625 626
	if (candidate && recv_fmtp){
		payload_type_set_recv_fmtp(candidate,recv_fmtp);
	}
627 628
	return candidate;
}
aymeric's avatar
aymeric committed
629

630
static bool_t get_codec(LpConfig *config, char* type, int index, PayloadType **ret){
aymeric's avatar
aymeric committed
631 632 633 634
	char codeckey[50];
	const char *mime,*fmtp;
	int rate,enabled;
	PayloadType *pt;
635

636
	*ret=NULL;
aymeric's avatar
aymeric committed
637 638
	snprintf(codeckey,50,"%s_%i",type,index);
	mime=lp_config_get_string(config,codeckey,"mime",NULL);
639
	if (mime==NULL || strlen(mime)==0 ) return FALSE;
640

aymeric's avatar
aymeric committed
641 642 643
	rate=lp_config_get_int(config,codeckey,"rate",8000);
	fmtp=lp_config_get_string(config,codeckey,"recv_fmtp",NULL);
	enabled=lp_config_get_int(config,codeckey,"enabled",1);
644 645
	pt=find_payload(&av_profile,mime,rate,fmtp);
	if (pt && enabled ) pt->flags|=PAYLOAD_TYPE_ENABLED;
aymeric's avatar
aymeric committed
646
	//ms_message("Found codec %s/%i",pt->mime_type,pt->clock_rate);
647 648 649 650 651 652
	if (pt==NULL) ms_warning("Ignoring codec config %s/%i with fmtp=%s because unsupported",
	    		mime,rate,fmtp ? fmtp : "");
	*ret=pt;
	return TRUE;
}

653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
static const char *codec_pref_order[]={
	"speex",
	"gsm",
	"pcmu",
	"pcma",
	"H264",
	"MP4V-ES",
	"theora",
	"H263-1998",
	"H263",
	NULL,
};

static int find_codec_rank(const char *mime){
	int i;
	for(i=0;codec_pref_order[i]!=NULL;++i){
		if (strcasecmp(codec_pref_order[i],mime)==0)
			break;
	}
	return i;
}

static int codec_compare(const PayloadType *a, const PayloadType *b){
	int ra,rb;
	ra=find_codec_rank(a->mime_type);
	rb=find_codec_rank(b->mime_type);
	if (ra>rb) return 1;
	if (ra<rb) return -1;
681
	return 0;
682 683
}

684 685 686 687 688 689 690 691 692 693 694 695 696
static MSList *add_missing_codecs(SalStreamType mtype, MSList *l){
	int i;
	for(i=0;i<127;++i){
		PayloadType *pt=rtp_profile_get_payload(&av_profile,i);
		if (pt){
			if (mtype==SalVideo && pt->type!=PAYLOAD_VIDEO)
				pt=NULL;
			else if (mtype==SalAudio && (pt->type!=PAYLOAD_AUDIO_PACKETIZED 
			    && pt->type!=PAYLOAD_AUDIO_CONTINUOUS)){
				pt=NULL;
			}
			if (pt && ms_filter_codec_supported(pt->mime_type)){
				if (ms_list_find(l,pt)==NULL){
Simon Morlat's avatar
Simon Morlat committed
697
					payload_type_set_flag(pt,PAYLOAD_TYPE_ENABLED);
698 699
					ms_message("Adding new codec %s/%i with fmtp %s",
					    pt->mime_type,pt->clock_rate,pt->recv_fmtp ? pt->recv_fmtp : "");
700
					l=ms_list_insert_sorted(l,pt,(int (*)(const void *, const void *))codec_compare);
701 702 703 704 705
				}
			}
		}
	}
	return l;
aymeric's avatar
aymeric committed
706 707
}

708 709 710 711 712 713 714 715 716 717 718
static MSList *codec_append_if_new(MSList *l, PayloadType *pt){
	MSList *elem;
	for (elem=l;l!=NULL;l=elem->next){
		PayloadType *ept=(PayloadType*)elem->data;
		if (pt==ept)
			return l;
	}
	l=ms_list_append(l,pt);
	return l;
}

719
static void codecs_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
720 721 722 723 724
{
	int i;
	PayloadType *pt;
	MSList *audio_codecs=NULL;
	MSList *video_codecs=NULL;
725 726 727 728
	for (i=0;get_codec(lc->config,"audio_codec",i,&pt);i++){
		if (pt){
			if (!ms_filter_codec_supported(pt->mime_type)){
				ms_warning("Codec %s is not supported by mediastreamer2, removed.",pt->mime_type);
729
			}else audio_codecs=codec_append_if_new(audio_codecs,pt);
730
		}
aymeric's avatar
aymeric committed
731
	}
732 733 734 735 736
	audio_codecs=add_missing_codecs(SalAudio,audio_codecs);
	for (i=0;get_codec(lc->config,"video_codec",i,&pt);i++){
		if (pt){
			if (!ms_filter_codec_supported(pt->mime_type)){
				ms_warning("Codec %s is not supported by mediastreamer2, removed.",pt->mime_type);
737
			}else video_codecs=codec_append_if_new(video_codecs,(void *)pt);
738
		}
aymeric's avatar
aymeric committed
739
	}
740
	video_codecs=add_missing_codecs(SalVideo,video_codecs);
aymeric's avatar
aymeric committed
741 742
	linphone_core_set_audio_codecs(lc,audio_codecs);
	linphone_core_set_video_codecs(lc,video_codecs);
743
	linphone_core_update_allocated_audio_bandwidth(lc);
aymeric's avatar
aymeric committed
744 745
}

746
static void video_config_read(LinphoneCore *lc){
Simon Morlat's avatar
Simon Morlat committed
747
	int capture, display, self_view;
aymeric's avatar
aymeric committed
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
	int enabled;
	const char *str;
	int ndev;
	const char **devices;
	const MSList *elem;
	int i;

	/* retrieve all video devices */
	elem=ms_web_cam_manager_get_list(ms_web_cam_manager_get());
	ndev=ms_list_size(elem);
	devices=ms_malloc((ndev+1)*sizeof(const char *));
	for (i=0;elem!=NULL;elem=elem->next,i++){
		devices[i]=ms_web_cam_get_string_id((MSWebCam *)elem->data);
	}
	devices[ndev]=NULL;
	lc->video_conf.cams=devices;

	str=lp_config_get_string(lc->config,"video","device",NULL);
	if (str && str[0]==0) str=NULL;
	linphone_core_set_video_device(lc,str);
768

smorlat's avatar
smorlat committed
769 770 771
	linphone_core_set_preferred_video_size_by_name(lc,
		lp_config_get_string(lc->config,"video","size","cif"));

aymeric's avatar
aymeric committed
772 773 774
	enabled=lp_config_get_int(lc->config,"video","enabled",1);
	capture=lp_config_get_int(lc->config,"video","capture",enabled);
	display=lp_config_get_int(lc->config,"video","display",enabled);
Simon Morlat's avatar
Simon Morlat committed
775
	self_view=lp_config_get_int(lc->config,"video","self_view",enabled);
776 777 778
	lc->video_conf.displaytype=lp_config_get_string(lc->config,"video","displaytype",NULL);
	if(lc->video_conf.displaytype)
		ms_message("we are using a specific display:%s\n",lc->video_conf.displaytype);
aymeric's avatar
aymeric committed
779 780
#ifdef VIDEO_ENABLED
	linphone_core_enable_video(lc,capture,display);
Simon Morlat's avatar
Simon Morlat committed
781
	linphone_core_enable_self_view(lc,self_view);
aymeric's avatar
aymeric committed
782 783 784
#endif
}

785
static void ui_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
786 787 788 789 790 791
{
	LinphoneFriend *lf;
	int i;
	for (i=0;(lf=linphone_friend_new_from_config_file(lc,i))!=NULL;i++){
		linphone_core_add_friend(lc,lf);
	}
792
	call_logs_read_from_config_file(lc);
aymeric's avatar
aymeric committed
793 794
}

795 796
/*
static void autoreplier_config_init(LinphoneCore *lc)
aymeric's avatar
aymeric committed
797 798 799 800 801 802 803 804 805
{
	autoreplier_config_t *config=&lc->autoreplier_conf;
	config->enabled=lp_config_get_int(lc->config,"autoreplier","enabled",0);
	config->after_seconds=lp_config_get_int(lc->config,"autoreplier","after_seconds",6);
	config->max_users=lp_config_get_int(lc->config,"autoreplier","max_users",1);
	config->max_rec_time=lp_config_get_int(lc->config,"autoreplier","max_rec_time",60);
	config->max_rec_msg=lp_config_get_int(lc->config,"autoreplier","max_rec_msg",10);
	config->message=lp_config_get_string(lc->config,"autoreplier","message",NULL);
}
806
*/
aymeric's avatar
aymeric committed
807

808 809 810 811 812 813 814 815 816 817 818 819 820 821
/**
 * Sets maximum available download bandwidth
 *
 * @ingroup media_parameters
 *
 * This is IP bandwidth, in kbit/s.
 * This information is used signaled to other parties during
 * calls (within SDP messages) so that the remote end can have
 * sufficient knowledge to properly configure its audio & video
 * codec output bitrate to not overflow available bandwidth.
 *
 * @param lc the LinphoneCore object
 * @param bw the bandwidth in kbits/s, 0 for infinite
 */
aymeric's avatar
aymeric committed
822 823 824 825 826 827
void linphone_core_set_download_bandwidth(LinphoneCore *lc, int bw){
	lc->net_conf.download_bw=bw;
	if (bw==0){ /*infinite*/
		lc->dw_audio_bw=-1;
		lc->dw_video_bw=-1;
	}else {
828 829
		lc->dw_audio_bw=MIN(lc->audio_bw,bw);
		lc->dw_video_bw=MAX(bw-lc->dw_audio_bw-10,0);/*-10: security margin*/
aymeric's avatar
aymeric committed
830 831 832
	}
}

833 834 835 836 837 838 839 840 841 842 843 844 845
/**
 * Sets maximum available upload bandwidth
 *
 * @ingroup media_parameters
 *
 * This is IP bandwidth, in kbit/s.
 * This information is used by liblinphone together with remote
 * side available bandwidth signaled in SDP messages to properly
 * configure audio & video codec's output bitrate.
 *
 * @param lc the LinphoneCore object
 * @param bw the bandwidth in kbits/s, 0 for infinite
 */
aymeric's avatar
aymeric committed
846 847 848 849 850 851
void linphone_core_set_upload_bandwidth(LinphoneCore *lc, int bw){
	lc->net_conf.upload_bw=bw;
	if (bw==0){ /*infinite*/
		lc->up_audio_bw=-1;
		lc->up_video_bw=-1;
	}else{
852 853
		lc->up_audio_bw=MIN(lc->audio_bw,bw);
		lc->up_video_bw=MAX(bw-lc->up_audio_bw-10,0);/*-10: security margin*/
aymeric's avatar
aymeric committed
854 855 856
	}
}

857 858 859 860 861 862 863 864
/**
 * Retrieve the maximum available download bandwidth.
 *
 * @ingroup media_parameters
 *
 * This value was set by linphone_core_set_download_bandwidth().
 *
**/
aymeric's avatar
aymeric committed
865 866 867 868
int linphone_core_get_download_bandwidth(const LinphoneCore *lc){
	return lc->net_conf.download_bw;
}

869 870 871 872 873 874 875 876
/**
 * Retrieve the maximum available upload bandwidth.
 *
 * @ingroup media_parameters
 *
 * This value was set by linphone_core_set_upload_bandwidth().
 *
**/
aymeric's avatar
aymeric committed
877 878 879
int linphone_core_get_upload_bandwidth(const LinphoneCore *lc){
	return lc->net_conf.upload_bw;
}
jehan's avatar
jehan committed
880 881 882 883
/**
 * set audio packetization time linphone expect to received from peer
 */
void linphone_core_set_download_ptime(LinphoneCore *lc, int ptime) {
884
	lc->net_conf.down_ptime=ptime;
jehan's avatar
jehan committed
885
}
886

jehan's avatar
jehan committed
887
int  linphone_core_get_download_ptime(LinphoneCore *lc) {
888
	return lc->net_conf.down_ptime;
jehan's avatar
jehan committed
889 890
}

aymeric's avatar
aymeric committed
891

892 893 894 895 896 897
/**
 * Returns liblinphone's version as a string.
 *
 * @ingroup misc
 *
**/
aymeric's avatar
aymeric committed
898 899 900 901 902
const char * linphone_core_get_version(void){
	return liblinphone_version;
}


903
static MSList *linphone_payload_types=NULL;
aymeric's avatar
aymeric committed
904

905 906 907
static void linphone_core_assign_payload_type(PayloadType *const_pt, int number, const char *recv_fmtp){
	PayloadType *pt;
	pt=payload_type_clone(const_pt);
908
	payload_type_set_number(pt,number);
909 910
	if (recv_fmtp!=NULL) payload_type_set_recv_fmtp(pt,recv_fmtp);
	rtp_profile_set_payload(&av_profile,number,pt);
911
	linphone_payload_types=ms_list_append(linphone_payload_types,pt);
912 913 914 915 916 917 918
}

static void linphone_core_free_payload_types(void){
	ms_list_for_each(linphone_payload_types,(void (*)(void*))payload_type_destroy);
	ms_list_free(linphone_payload_types);
	linphone_payload_types=NULL;
}
919

920
void linphone_core_set_state(LinphoneCore *lc, LinphoneGlobalState gstate, const char *message){
Simon Morlat's avatar
Simon Morlat committed
921
	lc->state=gstate;
922 923 924 925 926
	if (lc->vtable.global_state_changed){
		lc->vtable.global_state_changed(lc,gstate,message);
	}
}

927 928
static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vtable, const char *config_path, 
    const char *factory_config_path, void * userdata)
aymeric's avatar
aymeric committed
929 930 931
{
	memset (lc, 0, sizeof (LinphoneCore));
	lc->data=userdata;
smorlat's avatar
fixes  
smorlat committed
932

aymeric's avatar
aymeric committed
933 934
	memcpy(&lc->vtable,vtable,sizeof(LinphoneCoreVTable));

935
	linphone_core_set_state(lc,LinphoneGlobalStartup,"Starting up");
aymeric's avatar
aymeric committed
936
	ortp_init();
937 938 939
	linphone_core_assign_payload_type(&payload_type_pcmu8000,0,NULL);
	linphone_core_assign_payload_type(&payload_type_gsm,3,NULL);
	linphone_core_assign_payload_type(&payload_type_pcma8000,8,NULL);
940
	linphone_core_assign_payload_type(&payload_type_lpc1015,115,NULL);
Simon Morlat's avatar
Simon Morlat committed
941 942
	linphone_core_assign_payload_type(&payload_type_speex_nb,110,"vbr=vad");
	linphone_core_assign_payload_type(&payload_type_speex_wb,111,"vbr=vad");
943
	linphone_core_assign_payload_type(&payload_type_speex_uwb,112,"vbr=on");
944
	linphone_core_assign_payload_type(&payload_type_telephone_event,101,"0-11");
945
	linphone_core_assign_payload_type(&payload_type_ilbc,113,"mode=30");
Simon Morlat's avatar
Simon Morlat committed
946
	linphone_core_assign_payload_type(&payload_type_amr,114,"octet-align=1");
947 948 949 950 951 952 953

#ifdef ENABLE_NONSTANDARD_GSM
	{
		PayloadType *pt;
		pt=payload_type_clone(&payload_type_gsm);
		pt->clock_rate=11025;
		rtp_profile_set_payload(&av_profile,114,pt);
954 955 956 957 958
		linphone_payload_types=ms_list_append(linphone_payload_types,pt);
		pt=payload_type_clone(&payload_type_gsm);
		pt->clock_rate=22050;
		rtp_profile_set_payload(&av_profile,115,pt);
		linphone_payload_types=ms_list_append(linphone_payload_types,pt);
959
	}
aymeric's avatar
aymeric committed
960 961
#endif

962 963 964 965 966 967 968 969
#ifdef VIDEO_ENABLED
	linphone_core_assign_payload_type(&payload_type_h263,34,NULL);
	linphone_core_assign_payload_type(&payload_type_theora,97,NULL);
	linphone_core_assign_payload_type(&payload_type_h263_1998,98,"CIF=1;QCIF=1");
	linphone_core_assign_payload_type(&payload_type_mp4v,99,"profile-level-id=3");
	linphone_core_assign_payload_type(&payload_type_x_snow,100,NULL);
	linphone_core_assign_payload_type(&payload_type_h264,102,NULL);
	linphone_core_assign_payload_type(&payload_type_h264,103,"packetization-mode=1");
970 971
#endif

aymeric's avatar
aymeric committed
972
	ms_init();
973 974 975 976
	/* create a mediastreamer2 event queue and set it as global */
	/* This allows to run event's callback in linphone_core_iterate() */
	lc->msevq=ms_event_queue_new();
	ms_set_global_event_queue(lc->msevq);
977

aymeric's avatar
aymeric committed
978
	lc->config=lp_config_new(config_path);
979 980
	if (factory_config_path)
		lp_config_read_file(lc->config,factory_config_path);
981

982
	lc->sal=sal_init();
Simon Morlat's avatar
Simon Morlat committed
983
	sal_set_user_pointer(lc->sal,lc);
Simon Morlat's avatar
Simon Morlat committed
984
	sal_set_callbacks(lc->sal,&linphone_sal_callbacks);
985
	
smorlat's avatar
smorlat committed
986
	sip_setup_register_all();
aymeric's avatar
aymeric committed
987 988 989 990 991 992 993
	sound_config_read(lc);
	net_config_read(lc);
	rtp_config_read(lc);
	codecs_config_read(lc);
	sip_config_read(lc); /* this will start eXosip*/
	video_config_read(lc);
	//autoreplier_config_init(&lc->autoreplier_conf);
994
	lc->presence_mode=LinphoneStatusOnline;
aymeric's avatar
aymeric committed
995 996
	lc->max_call_logs=15;
	ui_config_read(lc);
997 998
	if (lc->vtable.display_status)
		lc->vtable.display_status(lc,_("Ready"));
999
	linphone_core_set_state(lc,LinphoneGlobalOn,"Ready");
jehan's avatar
jehan committed
1000
	lc->auto_net_state_mon=lc->sip_conf.auto_net_state_mon;
jehan's avatar
jehan committed
1001

jehan's avatar
jehan committed
1002
    lc->ready=TRUE;
aymeric's avatar
aymeric committed
1003 1004
}

1005 1006 1007 1008 1009 1010 1011 1012
/**
 * Instanciates a LinphoneCore object.
 * @ingroup initializing
 * 
 * The LinphoneCore object is the primary handle for doing all phone actions.
 * It should be unique within your application.
 * @param vtable a LinphoneCoreVTable structure holding your application callbacks
 * @param config_path a path to a config file. If it does not exists it will be created.
Simon Morlat's avatar
Simon Morlat committed
1013 1014 1015
 *        The config file is used to store all settings, call logs, friends, proxies... so that all these settings
 *	       become persistent over the life of the LinphoneCore object.
 *	       It is allowed to set a NULL config file. In that case LinphoneCore will not store any settings.
1016 1017 1018 1019 1020 1021 1022 1023
 * @param factory_config_path a path to a read-only config file that can be used to 
 *        to store hard-coded preference such as proxy settings or internal preferences.
 *        The settings in this factory file always override the one in the normal config file.
 *        It is OPTIONAL, use NULL if unneeded.
 * @param userdata an opaque user pointer that can be retrieved at any time (for example in
 *        callbacks) using linphone_core_get_user_data().
 * 
**/
aymeric's avatar
aymeric committed
1024
LinphoneCore *linphone_core_new(const LinphoneCoreVTable *vtable,
1025
						const char *config_path, const char *factory_config_path, void * userdata)
aymeric's avatar
aymeric committed
1026 1027
{
	LinphoneCore *core=ms_new(LinphoneCore,1);
1028
	linphone_core_init(core,vtable,config_path, factory_config_path, userdata);
aymeric's avatar
aymeric committed
1029 1030 1031
	return core;
}

1032 1033 1034 1035 1036 1037 1038 1039
/**
 * Returns the list of available audio codecs.
 *
 * This list is unmodifiable. The ->data field of the MSList points a PayloadType
 * structure holding the codec information.
 * It is possible to make copy of the list with ms_list_copy() in order to modify it
 * (such as the order of codecs).
**/
aymeric's avatar
aymeric committed
1040 1041 1042 1043 1044
const MSList *linphone_core_get_audio_codecs(const LinphoneCore *lc)
{
	return lc->codecs_conf.audio_codecs;
}

1045 1046 1047 1048 1049 1050 1051 1052
/**
 * Returns the list of available video codecs.
 *
 * This list is unmodifiable. The ->data field of the MSList points a PayloadType
 * structure holding the codec information.
 * It is possible to make copy of the list with ms_list_copy() in order to modify it
 * (such as the order of codecs).
**/
aymeric's avatar
aymeric committed
1053 1054 1055 1056 1057
const MSList *linphone_core_get_video_codecs(const LinphoneCore *lc)
{
	return lc->codecs_conf.video_codecs;
}

smorlat's avatar
smorlat committed
1058 1059 1060 1061 1062 1063 1064
/**
 * Sets the local "from" identity.
 *
 * @ingroup proxies
 * This data is used in absence of any proxy configuration or when no
 * default proxy configuration is set. See LinphoneProxyConfig
**/
aymeric's avatar
aymeric committed
1065 1066
int linphone_core_set_primary_contact(LinphoneCore *lc, const char *contact)
{
1067 1068
	LinphoneAddress *ctt;

Simon Morlat's avatar
Simon Morlat committed
1069
	if ((ctt=linphone_address_new(contact))==0) {
aymeric's avatar
aymeric committed
1070 1071 1072 1073 1074 1075 1076 1077 1078
		ms_error("Bad contact url: %s",contact);
		return -1;
	}
	if (lc->sip_conf.contact!=NULL) ms_free(lc->sip_conf.contact);
	lc->sip_conf.contact=ms_strdup(contact);
	if (lc->sip_conf.guessed_contact!=NULL){
		ms_free(lc->sip_conf.guessed_contact);
		lc->sip_conf.guessed_contact=NULL;
	}
1079
	linphone_address_destroy(ctt);
aymeric's avatar
aymeric committed
1080 1081 1082 1083 1084 1085
	return 0;
}


/*result must be an array of chars at least LINPHONE_IPADDR_SIZE */
void linphone_core_get_local_ip(LinphoneCore *lc, const char *dest, char *result){
1086
	if (linphone_core_get_firewall_policy(lc)==LinphonePolicyUseNatAddress
1087
	    && linphone_core_get_nat_address(lc)!=NULL){
aymeric's avatar
aymeric committed
1088 1089 1090
		strncpy(result,linphone_core_get_nat_address(lc),LINPHONE_IPADDR_SIZE);
		return;
	}
1091
	if (linphone_core_get_local_ip_for(lc->sip_conf.ipv6_enabled ? AF_INET6 : AF_INET,dest,result)==0)