linphonecore.c 212 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
/*
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.
*/

20 21
#define _GNU_SOURCE

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

Yann Diorcet's avatar
Yann Diorcet committed
28
#include <math.h>
29
#include <ortp/telephonyevents.h>
30
#include <ortp/zrtp.h>
aymeric's avatar
aymeric committed
31
#include "mediastreamer2/mediastream.h"
32
#include "mediastreamer2/mseventqueue.h"
smorlat's avatar
smorlat committed
33
#include "mediastreamer2/msvolume.h"
34
#include "mediastreamer2/msequalizer.h"
35
#include "mediastreamer2/dtmfgen.h"
aymeric's avatar
aymeric committed
36

37
#ifdef INET6
aymeric's avatar
aymeric committed
38
#ifndef WIN32
39
#include <netdb.h>
aymeric's avatar
aymeric committed
40 41 42
#endif
#endif

43 44
#ifdef HAVE_CONFIG_H
#include "config.h"
Ghislain MARY's avatar
Ghislain MARY committed
45 46 47 48
#include "liblinphone_gitversion.h"
#else
#ifndef LIBLINPHONE_GIT_VERSION
#define LIBLINPHONE_GIT_VERSION "unknown"
49 50 51
#endif
#endif

52 53 54
#ifdef __APPLE__
#include "TargetConditionals.h"
#endif
55

smorlat's avatar
smorlat committed
56
/*#define UNSTANDART_GSM_11K 1*/
aymeric's avatar
aymeric committed
57

58 59
#define ROOT_CA_FILE PACKAGE_DATA_DIR "/linphone/rootca.pem"

60 61 62 63 64 65 66
static const char *liblinphone_version=
#ifdef LIBLINPHONE_GIT_VERSION
	LIBLINPHONE_GIT_VERSION
#else
	LIBLINPHONE_VERSION
#endif
;
67
static bool_t liblinphone_serialize_logs = FALSE;
68
static void set_network_reachable(LinphoneCore* lc,bool_t isReachable, time_t curtime);
Simon Morlat's avatar
Simon Morlat committed
69 70
static void linphone_core_run_hooks(LinphoneCore *lc);
static void linphone_core_free_hooks(LinphoneCore *lc);
aymeric's avatar
aymeric committed
71 72

#include "enum.h"
73
#include "contact_providers_priv.h"
74

75

76
const char *linphone_core_get_nat_address_resolved(LinphoneCore *lc);
aymeric's avatar
aymeric committed
77 78
static void toggle_video_preview(LinphoneCore *lc, bool_t val);

79
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
80
#define SOUNDS_PREFIX
81 82
#else
#define SOUNDS_PREFIX "Assets/Sounds/"
83
#endif
aymeric's avatar
aymeric committed
84
/* relative path where is stored local ring*/
85
#define LOCAL_RING SOUNDS_PREFIX "rings/oldphone.wav"
aymeric's avatar
aymeric committed
86
/* same for remote ring (ringback)*/
87 88
#define REMOTE_RING SOUNDS_PREFIX "ringback.wav"
#define HOLD_MUSIC SOUNDS_PREFIX "rings/toy-mono.wav"
89

aymeric's avatar
aymeric committed
90

Simon Morlat's avatar
Simon Morlat committed
91
extern SalCallbacks linphone_sal_callbacks;
aymeric's avatar
aymeric committed
92 93 94 95 96 97 98 99 100 101 102 103

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;
}

104

aymeric's avatar
aymeric committed
105 106 107 108 109
/*prevent a gcc bug with %c*/
static size_t my_strftime(char *s, size_t max, const char  *fmt,  const struct tm *tm){
	return strftime(s, max, fmt, tm);
}

110
static void set_call_log_date(LinphoneCallLog *cl, time_t start_time){
aymeric's avatar
aymeric committed
111 112
	struct tm loctime;
#ifdef WIN32
Jehan Monnier's avatar
Jehan Monnier committed
113
#if !defined(_WIN32_WCE)
114
	loctime=*localtime(&start_time);
Jehan Monnier's avatar
Jehan Monnier committed
115 116
	/*FIXME*/
#endif /*_WIN32_WCE*/
aymeric's avatar
aymeric committed
117
#else
118
	localtime_r(&start_time,&loctime);
aymeric's avatar
aymeric committed
119
#endif
120 121 122 123 124 125
	my_strftime(cl->start_date,sizeof(cl->start_date),"%c",&loctime);
}

LinphoneCallLog * linphone_call_log_new(LinphoneCall *call, LinphoneAddress *from, LinphoneAddress *to){
	LinphoneCallLog *cl=ms_new0(LinphoneCallLog,1);
	cl->dir=call->dir;
126
	cl->start_date_time=time(NULL);
127
	set_call_log_date(cl,cl->start_date_time);
aymeric's avatar
aymeric committed
128 129
	cl->from=from;
	cl->to=to;
130
	cl->status=LinphoneCallAborted; /*default status*/
131
	cl->quality=-1;
132

133 134
	cl->reporting.reports[LINPHONE_CALL_STATS_AUDIO]=linphone_reporting_new();
	cl->reporting.reports[LINPHONE_CALL_STATS_VIDEO]=linphone_reporting_new();
aymeric's avatar
aymeric committed
135 136
	return cl;
}
137

138
void call_logs_write_to_config_file(LinphoneCore *lc){
139 140 141 142 143 144
	MSList *elem;
	char logsection[32];
	int i;
	char *tmp;
	LpConfig *cfg=lc->config;

145
	if (linphone_core_get_global_state (lc)==LinphoneGlobalStartup) return;
146

147 148 149
	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);
150
		lp_config_clean_section(cfg,logsection);
151 152 153 154 155 156 157 158
		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);
159 160 161
		if (cl->start_date_time)
			lp_config_set_int64(cfg,logsection,"start_date_time",(int64_t)cl->start_date_time);
		else lp_config_set_string(cfg,logsection,"start_date",cl->start_date);
162 163
		lp_config_set_int(cfg,logsection,"duration",cl->duration);
		if (cl->refkey) lp_config_set_string(cfg,logsection,"refkey",cl->refkey);
164
		lp_config_set_float(cfg,logsection,"quality",cl->quality);
165
		lp_config_set_int(cfg,logsection,"video_enabled", cl->video_enabled);
jehan's avatar
jehan committed
166
		lp_config_set_string(cfg,logsection,"call_id",cl->call_id);
167 168 169 170 171 172 173
	}
	for(;i<lc->max_call_logs;++i){
		snprintf(logsection,sizeof(logsection),"call_log_%i",i);
		lp_config_clean_section(cfg,logsection);
	}
}

174
static time_t string_to_time(const char *date){
175
#ifndef WIN32
176 177 178
	struct tm tmtime={0};
	strptime(date,"%c",&tmtime);
	return mktime(&tmtime);
179 180 181
#else
	return 0;
#endif
182 183
}

184 185 186 187
static void call_logs_read_from_config_file(LinphoneCore *lc){
	char logsection[32];
	int i;
	const char *tmp;
188
	uint64_t sec;
189 190 191 192 193 194 195 196 197 198 199
	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);
200 201 202 203 204 205 206 207 208 209 210 211
			sec=lp_config_get_int64(cfg,logsection,"start_date_time",0);
			if (sec) {
				/*new call log format with date expressed in seconds */
				cl->start_date_time=(time_t)sec;
				set_call_log_date(cl,cl->start_date_time);
			}else{
				tmp=lp_config_get_string(cfg,logsection,"start_date",NULL);
				if (tmp) {
					strncpy(cl->start_date,tmp,sizeof(cl->start_date));
					cl->start_date_time=string_to_time(cl->start_date);
				}
			}
212 213 214
			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);
215
			cl->quality=lp_config_get_float(cfg,logsection,"quality",-1);
216
			cl->video_enabled=lp_config_get_int(cfg,logsection,"video_enabled",0);
217 218
			tmp=lp_config_get_string(cfg,logsection,"call_id",NULL);
			if (tmp) cl->call_id=ms_strdup(tmp);
219
			lc->call_logs=ms_list_append(lc->call_logs,cl);
220
		}else break;
221 222 223 224
	}
}


aymeric's avatar
aymeric committed
225

smorlat's avatar
smorlat committed
226 227 228 229 230 231 232
/**
 * @addtogroup call_logs
 * @{
**/

/**
 * Returns a human readable string describing the call.
233
 *
smorlat's avatar
smorlat committed
234 235
 * @note: the returned char* must be freed by the application (use ms_free()).
**/
aymeric's avatar
aymeric committed
236 237
char * linphone_call_log_to_str(LinphoneCallLog *cl){
	char *status;
238
	char *tmp;
239 240
	char *from=linphone_address_as_string (cl->from);
	char *to=linphone_address_as_string (cl->to);
aymeric's avatar
aymeric committed
241 242 243 244 245 246 247 248 249 250 251 252 253
	switch(cl->status){
		case LinphoneCallAborted:
			status=_("aborted");
			break;
		case LinphoneCallSuccess:
			status=_("completed");
			break;
		case LinphoneCallMissed:
			status=_("missed");
			break;
		default:
			status="unknown";
	}
254
	tmp=ortp_strdup_printf(_("%s at %s\nFrom: %s\nTo: %s\nStatus: %s\nDuration: %i mn %i sec\n"),
aymeric's avatar
aymeric committed
255 256
			(cl->dir==LinphoneCallIncoming) ? _("Incoming call") : _("Outgoing call"),
			cl->start_date,
257 258
			from,
			to,
aymeric's avatar
aymeric committed
259 260 261
			status,
			cl->duration/60,
			cl->duration%60);
262 263 264
	ms_free(from);
	ms_free(to);
	return tmp;
aymeric's avatar
aymeric committed
265 266
}

267 268
/**
 * Returns RTP statistics computed locally regarding the call.
269
 *
270 271 272 273 274 275 276 277 278 279 280 281 282 283
**/
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;
}

Simon Morlat's avatar
Simon Morlat committed
284 285 286 287
const char *linphone_call_log_get_call_id(const LinphoneCallLog *cl){
	return cl->call_id;
}

288 289 290
/**
 * Assign a user pointer to the call log.
**/
291
void linphone_call_log_set_user_data(LinphoneCallLog *cl, void *up){
smorlat's avatar
smorlat committed
292 293 294
	cl->user_pointer=up;
}

295 296 297
/**
 * Returns the user pointer associated with the call log.
**/
298
void *linphone_call_log_get_user_data(const LinphoneCallLog *cl){
smorlat's avatar
smorlat committed
299 300 301
	return cl->user_pointer;
}

smorlat's avatar
smorlat committed
302 303


304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
/**
 * 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);
}

/**
 * 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;
}

330 331 332 333 334 335 336 337 338 339 340 341 342 343
/**
 * Returns origin (ie from) address of the call.
**/
LinphoneAddress *linphone_call_log_get_from(LinphoneCallLog *cl){
	return cl->from;
}

/**
 * Returns destination address (ie to) of the call.
**/
LinphoneAddress *linphone_call_log_get_to(LinphoneCallLog *cl){
	return cl->to;
}

Simon Morlat's avatar
Simon Morlat committed
344 345 346 347 348 349 350
/**
 * Returns remote address (that is from or to depending on call direction).
**/
LinphoneAddress *linphone_call_log_get_remote_address(LinphoneCallLog *cl){
	return (cl->dir == LinphoneCallIncoming) ? cl->from : cl->to;
}

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
/**
 * Returns the direction of the call.
**/
LinphoneCallDir linphone_call_log_get_dir(LinphoneCallLog *cl){
	return cl->dir;
}

/**
 * Returns the status of the call.
**/
LinphoneCallStatus linphone_call_log_get_status(LinphoneCallLog *cl){
	return cl->status;
}

/**
 * Returns the start date of the call, expressed as a POSIX time_t.
**/
time_t linphone_call_log_get_start_date(LinphoneCallLog *cl){
	return cl->start_date_time;
}

/**
 * Returns duration of the call.
**/
int linphone_call_log_get_duration(LinphoneCallLog *cl){
	return cl->duration;
}

/**
 * Returns overall quality indication of the call.
**/
float linphone_call_log_get_quality(LinphoneCallLog *cl){
	return cl->quality;
}
385 386 387
/**
 * return true if video was enabled at the end of the call
 */
388
bool_t linphone_call_log_video_enabled(LinphoneCallLog *cl) {
389 390
	return cl->video_enabled;
}
smorlat's avatar
smorlat committed
391 392
/** @} */

aymeric's avatar
aymeric committed
393
void linphone_call_log_destroy(LinphoneCallLog *cl){
394 395 396
	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);
397
	if (cl->call_id) ms_free(cl->call_id);
398 399
	if (cl->reporting.reports[LINPHONE_CALL_STATS_AUDIO]!=NULL) linphone_reporting_destroy(cl->reporting.reports[LINPHONE_CALL_STATS_AUDIO]);
	if (cl->reporting.reports[LINPHONE_CALL_STATS_VIDEO]!=NULL) linphone_reporting_destroy(cl->reporting.reports[LINPHONE_CALL_STATS_VIDEO]);
400

aymeric's avatar
aymeric committed
401 402 403
	ms_free(cl);
}

404 405 406 407 408 409 410 411 412 413 414 415
/**
 * 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;
}

416 417
int linphone_core_get_current_call_duration(const LinphoneCore *lc){
	LinphoneCall *call=linphone_core_get_current_call((LinphoneCore *)lc);
418 419
	if (call)  return linphone_call_get_duration(call);
	return -1;
420 421 422 423
}

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
424
	if (call==NULL) return NULL;
425
	return linphone_call_get_remote_address(call);
smorlat's avatar
smorlat committed
426 427
}

428 429 430 431 432 433 434 435 436 437 438
void linphone_core_set_log_handler(OrtpLogFunc logfunc) {
	ortp_set_log_handler(logfunc);
}

void linphone_core_set_log_file(FILE *file) {
	if (file == NULL) file = stdout;
	ortp_set_log_file(file);
}

void linphone_core_set_log_level(OrtpLogLevel loglevel) {
	ortp_set_log_level_mask(loglevel);
439 440 441 442 443
	if (loglevel == 0) {
		sal_disable_logs();
	} else {
		sal_enable_logs();
	}
444 445
}

446 447 448 449
/**
 * Enable logs in supplied FILE*.
 *
 * @ingroup misc
450
 * @deprecated Use #linphone_core_set_log_file and #linphone_core_set_log_level instead.
451 452
 *
 * @param file a C FILE* where to fprintf logs. If null stdout is used.
453
 *
454
**/
aymeric's avatar
aymeric committed
455 456 457 458
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);
jehan's avatar
jehan committed
459
	sal_enable_logs();
aymeric's avatar
aymeric committed
460 461
}

462 463 464 465
/**
 * Enable logs through the user's supplied log callback.
 *
 * @ingroup misc
466
 * @deprecated Use #linphone_core_set_log_handler and #linphone_core_set_log_level instead.
467 468 469
 *
 * @param logfunc The address of a OrtpLogFunc callback whose protoype is
 *            	  typedef void (*OrtpLogFunc)(OrtpLogLevel lev, const char *fmt, va_list args);
470
 *
471
**/
aymeric's avatar
aymeric committed
472 473 474
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);
jehan's avatar
jehan committed
475
	sal_enable_logs();
aymeric's avatar
aymeric committed
476 477
}

478 479 480 481
/**
 * Entirely disable logging.
 *
 * @ingroup misc
482
 * @deprecated Use #linphone_core_set_log_level instead.
483
**/
484
void linphone_core_disable_logs(void){
aymeric's avatar
aymeric committed
485
	ortp_set_log_level_mask(ORTP_ERROR|ORTP_FATAL);
jehan's avatar
jehan committed
486
	sal_disable_logs();
aymeric's avatar
aymeric committed
487 488
}

489 490 491 492
void linphone_core_serialize_logs(void) {
	liblinphone_serialize_logs = TRUE;
}

aymeric's avatar
aymeric committed
493

494
static void net_config_read (LinphoneCore *lc)
aymeric's avatar
aymeric committed
495 496 497 498 499
{
	int tmp;
	const char *tmpstr;
	LpConfig *config=lc->config;

500
	lc->net_conf.nat_address_ip = NULL;
aymeric's avatar
aymeric committed
501 502 503 504 505 506 507 508 509 510
	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","nat_sdp_only",0);
	lc->net_conf.nat_sdp_only=tmp;
511
	tmp=lp_config_get_int(lc->config,"net","mtu",1300);
aymeric's avatar
aymeric committed
512
	linphone_core_set_mtu(lc,tmp);
513 514 515 516 517
	tmp=lp_config_get_int(lc->config,"net","download_ptime",-1);
	if (tmp !=-1 && linphone_core_get_download_ptime(lc) !=0) {
		/*legacy parameter*/
		linphone_core_set_download_ptime(lc,tmp);
	}
518 519
	tmp = lp_config_get_int(lc->config, "net", "dns_srv_enabled", 1);
	linphone_core_enable_dns_srv(lc, tmp);
520 521 522

	/* This is to filter out unsupported firewall policies */
	linphone_core_set_firewall_policy(lc, linphone_core_get_firewall_policy(lc));
aymeric's avatar
aymeric committed
523 524
}

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
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
541

542
static void sound_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
543
{
544
	int tmp;
aymeric's avatar
aymeric committed
545 546
	const char *tmpbuf;
	const char *devid;
547
#ifdef __linux
aymeric's avatar
aymeric committed
548 549 550
	/*alsadev let the user use custom alsa device within linphone*/
	devid=lp_config_get_string(lc->config,"sound","alsadev",NULL);
	if (devid){
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
		MSSndCard* card;
		const char* delim=",";
		size_t l=strlen(devid);
		char* d=malloc(l+1);
		char* i;
		memcpy(d,devid,l+1);
		for (l=0,i=strpbrk(d+l,delim);i;i=strpbrk(d+l,delim)){
			char s=*i;
			*i='\0';
			card=ms_alsa_card_new_custom(d+l,d+l);
			ms_snd_card_manager_add_card(ms_snd_card_manager_get(),card);
			*i=s;
			l=i-d+1;
		}
		if(d[l]!='\0') {
			card=ms_alsa_card_new_custom(d+l,d+l);
			ms_snd_card_manager_add_card(ms_snd_card_manager_get(),card);
		}
		free(d);
aymeric's avatar
aymeric committed
570
	}
Simon Morlat's avatar
Simon Morlat committed
571
	tmp=lp_config_get_int(lc->config,"sound","alsa_forced_rate",-1);
572
	if (tmp>0) ms_alsa_card_set_forced_sample_rate(tmp);
aymeric's avatar
aymeric committed
573 574
#endif
	/* retrieve all sound devices */
575 576
	build_sound_devices_table(lc);

aymeric's avatar
aymeric committed
577 578
	devid=lp_config_get_string(lc->config,"sound","playback_dev_id",NULL);
	linphone_core_set_playback_device(lc,devid);
579

aymeric's avatar
aymeric committed
580 581
	devid=lp_config_get_string(lc->config,"sound","ringer_dev_id",NULL);
	linphone_core_set_ringer_device(lc,devid);
582

aymeric's avatar
aymeric committed
583 584
	devid=lp_config_get_string(lc->config,"sound","capture_dev_id",NULL);
	linphone_core_set_capture_device(lc,devid);
585

aymeric's avatar
aymeric committed
586 587 588 589 590 591 592 593 594 595
/*
	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]);
*/
596

aymeric's avatar
aymeric committed
597 598
	tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
	tmpbuf=lp_config_get_string(lc->config,"sound","local_ring",tmpbuf);
Jehan Monnier's avatar
Jehan Monnier committed
599
	if (ortp_file_exist(tmpbuf)==-1) {
600
		ms_warning("%s does not exist",tmpbuf);
aymeric's avatar
aymeric committed
601 602 603 604 605 606 607
		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;
	}
	linphone_core_set_ring(lc,tmpbuf);
608

609
	tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
aymeric's avatar
aymeric committed
610
	tmpbuf=lp_config_get_string(lc->config,"sound","remote_ring",tmpbuf);
Jehan Monnier's avatar
Jehan Monnier committed
611
	if (ortp_file_exist(tmpbuf)==-1){
612
		tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
aymeric's avatar
aymeric committed
613 614 615
	}
	if (strstr(tmpbuf,".wav")==NULL){
		/* it currently uses old sound files, so replace them */
616
		tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
aymeric's avatar
aymeric committed
617
	}
618
	linphone_core_set_ringback(lc,tmpbuf);
619 620

	linphone_core_set_play_file(lc,lp_config_get_string(lc->config,"sound","hold_music",PACKAGE_SOUND_DIR "/" HOLD_MUSIC));
aymeric's avatar
aymeric committed
621
	lc->sound_conf.latency=0;
622
#ifndef __ios
Simon Morlat's avatar
Simon Morlat committed
623
	tmp=TRUE;
624
#else
Simon Morlat's avatar
Simon Morlat committed
625
	tmp=FALSE; /* on iOS we have builtin echo cancellation.*/
626
#endif
Simon Morlat's avatar
Simon Morlat committed
627
	tmp=lp_config_get_int(lc->config,"sound","echocancellation",tmp);
628
	linphone_core_enable_echo_cancellation(lc,tmp);
smorlat's avatar
smorlat committed
629 630
	linphone_core_enable_echo_limiter(lc,
		lp_config_get_int(lc->config,"sound","echolimiter",0));
631 632
	linphone_core_enable_agc(lc,
		lp_config_get_int(lc->config,"sound","agc",0));
633

Yann Diorcet's avatar
Yann Diorcet committed
634 635
	linphone_core_set_playback_gain_db (lc,lp_config_get_float(lc->config,"sound","playback_gain_db",0));
	linphone_core_set_mic_gain_db (lc,lp_config_get_float(lc->config,"sound","mic_gain_db",0));
636 637

	linphone_core_set_remote_ringback_tone (lc,lp_config_get_string(lc->config,"sound","ringback_tone",NULL));
638 639 640

	/*just parse requested stream feature once at start to print out eventual errors*/
	linphone_core_get_audio_features(lc);
641

642
	_linphone_core_set_tone(lc,LinphoneReasonBusy,LinphoneToneBusy,NULL);
aymeric's avatar
aymeric committed
643 644
}

645 646
static void certificates_config_read(LinphoneCore *lc)
{
647
	const char *rootca;
648
#ifdef __linux
649
	rootca=lp_config_get_string(lc->config,"sip","root_ca", "/etc/ssl/certs");
650
#else
651
	rootca=lp_config_get_string(lc->config,"sip","root_ca", ROOT_CA_FILE);
652
#endif
653
	linphone_core_set_root_ca(lc,rootca);
654
	linphone_core_verify_server_certificates(lc,lp_config_get_int(lc->config,"sip","verify_server_certs",TRUE));
655
	linphone_core_verify_server_cn(lc,lp_config_get_int(lc->config,"sip","verify_server_cn",TRUE));
656 657
}

658
static void sip_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
659 660 661
{
	char *contact;
	const char *tmpstr;
662
	LCSipTransports tr;
aymeric's avatar
aymeric committed
663 664
	int i,tmp;
	int ipv6;
665 666 667 668

	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
669

670
	sal_use_no_initial_route(lc->sal,lp_config_get_int(lc->config,"sip","use_no_initial_route",0));
671 672
	sal_use_rport(lc->sal,lp_config_get_int(lc->config,"sip","use_rport",1));

aymeric's avatar
aymeric committed
673 674 675 676 677
	ipv6=lp_config_get_int(lc->config,"sip","use_ipv6",-1);
	if (ipv6==-1){
		ipv6=0;
	}
	linphone_core_enable_ipv6(lc,ipv6);
678
	memset(&tr,0,sizeof(tr));
679

680 681 682 683
	tr.udp_port=lp_config_get_int(lc->config,"sip","sip_port",5060);
	tr.tcp_port=lp_config_get_int(lc->config,"sip","sip_tcp_port",5060);
	/*we are not listening inbound connection for tls, port has no meaning*/
	tr.tls_port=lp_config_get_int(lc->config,"sip","sip_tls_port",LC_SIP_TRANSPORT_RANDOM);
684

685
	certificates_config_read(lc);
686 687
	/*setting the dscp must be done before starting the transports, otherwise it is not taken into effect*/
	sal_set_dscp(lc->sal,linphone_core_get_sip_dscp(lc));
688
	/*start listening on ports*/
689
	linphone_core_set_sip_transports(lc,&tr);
690

aymeric's avatar
aymeric committed
691 692
	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
693 694
		const char *hostname=NULL;
		const char *username=NULL;
695
#ifdef HAVE_GETENV
smorlat's avatar
smorlat committed
696 697
		hostname=getenv("HOST");
		username=getenv("USER");
aymeric's avatar
aymeric committed
698
		if (hostname==NULL) hostname=getenv("HOSTNAME");
699
#endif /*HAVE_GETENV*/
aymeric's avatar
aymeric committed
700 701 702 703 704 705 706 707 708 709 710 711
		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);
712 713


714
	tmp=lp_config_get_int(lc->config,"sip","inc_timeout",30);
aymeric's avatar
aymeric committed
715 716
	linphone_core_set_inc_timeout(lc,tmp);

Yann Diorcet's avatar
Yann Diorcet committed
717 718
	tmp=lp_config_get_int(lc->config,"sip","in_call_timeout",0);
	linphone_core_set_in_call_timeout(lc,tmp);
719

720 721
	tmp=lp_config_get_int(lc->config,"sip","delayed_timeout",4);
	linphone_core_set_delayed_timeout(lc,tmp);
Yann Diorcet's avatar
Yann Diorcet committed
722

aymeric's avatar
aymeric committed
723 724
	/* get proxies config */
	for(i=0;; i++){
jehan's avatar
jehan committed
725
		LinphoneProxyConfig *cfg=linphone_proxy_config_new_from_config_file(lc,i);
aymeric's avatar
aymeric committed
726 727 728 729 730 731 732 733 734
		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);
735

aymeric's avatar
aymeric committed
736 737 738 739 740
	/* 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
741
			linphone_auth_info_destroy(ai);
aymeric's avatar
aymeric committed
742 743 744 745
		}else{
			break;
		}
	}
746 747
	/*this is to filter out unsupported encryption schemes*/
	linphone_core_set_media_encryption(lc,linphone_core_get_media_encryption(lc));
748

749
	/*for tuning or test*/
aymeric's avatar
aymeric committed
750
	lc->sip_conf.sdp_200_ack=lp_config_get_int(lc->config,"sip","sdp_200_ack",0);
751
	lc->sip_conf.register_only_when_network_is_up=
752
		lp_config_get_int(lc->config,"sip","register_only_when_network_is_up",1);
753 754
	lc->sip_conf.register_only_when_upnp_is_ok=
		lp_config_get_int(lc->config,"sip","register_only_when_upnp_is_ok",1);
755
	lc->sip_conf.ping_with_options=lp_config_get_int(lc->config,"sip","ping_with_options",0);
jehan's avatar
jehan committed
756
	lc->sip_conf.auto_net_state_mon=lp_config_get_int(lc->config,"sip","auto_net_state_mon",1);
757
	lc->sip_conf.keepalive_period=lp_config_get_int(lc->config,"sip","keepalive_period",10000);
758 759
	lc->sip_conf.tcp_tls_keepalive=lp_config_get_int(lc->config,"sip","tcp_tls_keepalive",0);
	linphone_core_enable_keep_alive(lc, (lc->sip_conf.keepalive_period > 0));
Simon Morlat's avatar
Simon Morlat committed
760
	sal_use_one_matching_codec_policy(lc->sal,lp_config_get_int(lc->config,"sip","only_one_codec",0));
761
	sal_use_dates(lc->sal,lp_config_get_int(lc->config,"sip","put_date",0));
762
	sal_enable_sip_update_method(lc->sal,lp_config_get_int(lc->config,"sip","sip_update",1));
763
	lc->sip_conf.vfu_with_info=lp_config_get_int(lc->config,"sip","vfu_with_info",1);
764
	linphone_core_set_sip_transport_timeout(lc, lp_config_get_int(lc->config, "sip", "transport_timeout", 63000));
aymeric's avatar
aymeric committed
765 766
}

767
static void rtp_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
768
{
769
	int min_port, max_port;
aymeric's avatar
aymeric committed
770 771
	int jitt_comp;
	int nortp_timeout;
Simon Morlat's avatar
Simon Morlat committed
772
	bool_t rtp_no_xmit_on_audio_mute;
773
	bool_t adaptive_jitt_comp_enabled;
Simon Morlat's avatar
Simon Morlat committed
774

775 776 777 778 779 780 781 782
	if (lp_config_get_range(lc->config, "rtp", "audio_rtp_port", &min_port, &max_port, 7078, 7078) == TRUE) {
		if (min_port <= 0) min_port = 1;
		if (max_port > 65535) max_port = 65535;
		linphone_core_set_audio_port_range(lc, min_port, max_port);
	} else {
		min_port = lp_config_get_int(lc->config, "rtp", "audio_rtp_port", 7078);
		linphone_core_set_audio_port(lc, min_port);
	}
783

784 785 786 787 788 789 790 791
	if (lp_config_get_range(lc->config, "rtp", "video_rtp_port", &min_port, &max_port, 9078, 9078) == TRUE) {
		if (min_port <= 0) min_port = 1;
		if (max_port > 65535) max_port = 65535;
		linphone_core_set_video_port_range(lc, min_port, max_port);
	} else {
		min_port = lp_config_get_int(lc->config, "rtp", "video_rtp_port", 9078);
		linphone_core_set_video_port(lc, min_port);
	}
792

aymeric's avatar
aymeric committed
793
	jitt_comp=lp_config_get_int(lc->config,"rtp","audio_jitt_comp",60);
794
	linphone_core_set_audio_jittcomp(lc,jitt_comp);
aymeric's avatar
aymeric committed
795
	jitt_comp=lp_config_get_int(lc->config,"rtp","video_jitt_comp",60);
796
	if (jitt_comp==0) jitt_comp=60;
797
	linphone_core_set_video_jittcomp(lc,jitt_comp);
aymeric's avatar
aymeric committed
798
	nortp_timeout=lp_config_get_int(lc->config,"rtp","nortp_timeout",30);
799
	linphone_core_set_nortp_timeout(lc,nortp_timeout);
Simon Morlat's avatar
Simon Morlat committed
800
	rtp_no_xmit_on_audio_mute=lp_config_get_int(lc->config,"rtp","rtp_no_xmit_on_audio_mute",FALSE);
801
	linphone_core_set_rtp_no_xmit_on_audio_mute(lc,rtp_no_xmit_on_audio_mute);
802 803 804 805
	adaptive_jitt_comp_enabled = lp_config_get_int(lc->config, "rtp", "audio_adaptive_jitt_comp_enabled", TRUE);
	linphone_core_enable_audio_adaptive_jittcomp(lc, adaptive_jitt_comp_enabled);
	adaptive_jitt_comp_enabled = lp_config_get_int(lc->config, "rtp", "video_adaptive_jitt_comp_enabled", TRUE);
	linphone_core_enable_video_adaptive_jittcomp(lc, adaptive_jitt_comp_enabled);
806
	lc->rtp_conf.disable_upnp = lp_config_get_int(lc->config, "rtp", "disable_upnp", FALSE);
aymeric's avatar
aymeric committed
807 808
}

809
static PayloadType * find_payload(RtpProfile *prof, const char *mime_type, int clock_rate, int channels, const char *recv_fmtp){
810 811 812
	PayloadType *candidate=NULL;
	int i;
	PayloadType *it;
813
	for(i=0;i<RTP_PROFILE_MAX_PAYLOADS;++i){
814 815
		it=rtp_profile_get_payload(prof,i);
		if (it!=NULL && strcasecmp(mime_type,it->mime_type)==0
816 817
			&& (clock_rate==it->clock_rate || clock_rate<=0)
			&& (channels==it->channels || channels<=0) ){
818
			if ( (recv_fmtp && it->recv_fmtp && strstr(recv_fmtp,it->recv_fmtp)!=NULL) ||
819 820
				(recv_fmtp==NULL && it->recv_fmtp==NULL) ){
				/*exact match*/
821
				if (recv_fmtp) payload_type_set_recv_fmtp(it,recv_fmtp);
822
				return it;
823 824 825 826 827
			}else {
				if (candidate){
					if (it->recv_fmtp==NULL) candidate=it;
				}else candidate=it;
			}
828 829
		}
	}
830 831 832
	if (candidate && recv_fmtp){
		payload_type_set_recv_fmtp(candidate,recv_fmtp);
	}
833 834
	return candidate;
}
aymeric's avatar
aymeric committed
835

Simon Morlat's avatar
Simon Morlat committed
836
static bool_t get_codec(LinphoneCore *lc, const char* type, int index, PayloadType **ret){
aymeric's avatar
aymeric committed
837 838
	char codeckey[50];
	const char *mime,*fmtp;
839
	int rate,channels,enabled;
aymeric's avatar
aymeric committed
840
	PayloadType *pt;
Simon Morlat's avatar
Simon Morlat committed
841
	LpConfig *config=lc->config;
842

843
	*ret=NULL;
aymeric's avatar
aymeric committed
844 845
	snprintf(codeckey,50,"%s_%i",type,index);
	mime=lp_config_get_string(config,codeckey,"mime",NULL);
846
	if (mime==NULL || strlen(mime)==0 ) return FALSE;
847

aymeric's avatar
aymeric committed
848 849
	rate=lp_config_get_int(config,codeckey,"rate",8000);
	fmtp=lp_config_get_string(config,codeckey,"recv_fmtp",NULL);
850
	channels=lp_config_get_int(config,codeckey,"channels",0);
aymeric's avatar
aymeric committed
851
	enabled=lp_config_get_int(config,codeckey,"enabled",1);
Simon Morlat's avatar
Simon Morlat committed
852
	pt=find_payload(lc->default_profile,mime,rate,channels,fmtp);
853
	if (pt && enabled ) pt->flags|=PAYLOAD_TYPE_ENABLED;
aymeric's avatar
aymeric committed
854
	//ms_message("Found codec %s/%i",pt->mime_type,pt->clock_rate);
855
	if (pt==NULL) ms_warning("Ignoring codec config %s/%i with fmtp=%s because unsupported",
856
				mime,rate,fmtp ? fmtp : "");
857 858 859 860
	*ret=pt;
	return TRUE;
}

861
#define RANK_END 10000
Simon Morlat's avatar
Simon Morlat committed
862 863 864 865 866 867 868

typedef struct codec_desc{
	const char *name;
	int rate;
}codec_desc_t;

static codec_desc_t codec_pref_order[]={
Ghislain MARY's avatar
Ghislain MARY committed
869
	{"opus", 48000},
Simon Morlat's avatar
Simon Morlat committed
870 871 872 873 874 875 876 877 878
	{"SILK", 16000},
	{"speex", 16000},
	{"speex", 8000},
	{"pcmu",8000},
	{"pcma",8000},
	{"VP8",90000},
	{"H264",90000},
	{"MP4V-ES",90000},
	{NULL,0}
879 880
};

Simon Morlat's avatar
Simon Morlat committed
881
static int find_codec_rank(const char *mime, int clock_rate){
882
	int i;
883 884 885 886 887 888 889

#ifdef __arm__
	/*hack for opus, that needs to be disabed by default on ARM single processor, otherwise there is no cpu left for video processing*/
	if (strcasecmp(mime,"opus")==0){
		if (ms_get_cpu_count()==1) return RANK_END;
	}
#endif
Simon Morlat's avatar
Simon Morlat committed
890 891
	for(i=0;codec_pref_order[i].name!=NULL;++i){
		if (strcasecmp(codec_pref_order[i].name,mime)==0 && clock_rate==codec_pref_order[i].rate)
892
			return i;
893
	}
894
	return RANK_END;
895 896 897 898
}

static int codec_compare(const PayloadType *a, const PayloadType *b){
	int ra,rb;
Simon Morlat's avatar
Simon Morlat committed
899 900
	ra=find_codec_rank(a->mime_type,a->clock_rate);
	rb=find_codec_rank(b->mime_type,b->clock_rate);
901 902
	if (ra>rb) return 1;
	if (ra<rb) return -1;
903
	return 1;
904 905
}

Simon Morlat's avatar
Simon Morlat committed
906
static MSList *add_missing_codecs(LinphoneCore *lc, SalStreamType mtype, MSList *l){
907
	int i;
908
	for(i=0;i<RTP_PROFILE_MAX_PAYLOADS;++i){
Simon Morlat's avatar
Simon Morlat committed
909
		PayloadType *pt=rtp_profile_get_payload(lc->default_profile,i);
910 911 912
		if (pt){
			if (mtype==SalVideo && pt->type!=PAYLOAD_VIDEO)
				pt=NULL;