linphonecore.c 176 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

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

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

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

51

smorlat's avatar
smorlat committed
52
/*#define UNSTANDART_GSM_11K 1*/
aymeric's avatar
aymeric committed
53

54 55
#define ROOT_CA_FILE PACKAGE_DATA_DIR "/linphone/rootca.pem"

56 57 58 59 60 61 62
static const char *liblinphone_version=
#ifdef LIBLINPHONE_GIT_VERSION
	LIBLINPHONE_GIT_VERSION
#else
	LIBLINPHONE_VERSION
#endif
;
63
static void set_network_reachable(LinphoneCore* lc,bool_t isReachable, time_t curtime);
Simon Morlat's avatar
Simon Morlat committed
64 65
static void linphone_core_run_hooks(LinphoneCore *lc);
static void linphone_core_free_hooks(LinphoneCore *lc);
aymeric's avatar
aymeric committed
66 67

#include "enum.h"
68

69
const char *linphone_core_get_nat_address_resolved(LinphoneCore *lc);
aymeric's avatar
aymeric committed
70 71 72 73 74
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)*/
75
#define REMOTE_RING "ringback.wav"
76 77
#define HOLD_MUSIC "rings/toy-mono.wav"

aymeric's avatar
aymeric committed
78

Simon Morlat's avatar
Simon Morlat committed
79
extern SalCallbacks linphone_sal_callbacks;
aymeric's avatar
aymeric committed
80 81 82 83 84 85 86 87 88 89 90 91

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

92

aymeric's avatar
aymeric committed
93 94 95 96 97
/*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);
}

98
static void set_call_log_date(LinphoneCallLog *cl, time_t start_time){
aymeric's avatar
aymeric committed
99 100
	struct tm loctime;
#ifdef WIN32
Jehan Monnier's avatar
Jehan Monnier committed
101
#if !defined(_WIN32_WCE)
102
	loctime=*localtime(&start_time);
Jehan Monnier's avatar
Jehan Monnier committed
103 104
	/*FIXME*/
#endif /*_WIN32_WCE*/
aymeric's avatar
aymeric committed
105
#else
106
	localtime_r(&start_time,&loctime);
aymeric's avatar
aymeric committed
107
#endif
108 109 110 111 112 113 114 115
	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;
	cl->start_date_time=call->start_time;
	set_call_log_date(cl,cl->start_date_time);
aymeric's avatar
aymeric committed
116 117
	cl->from=from;
	cl->to=to;
118
	cl->status=LinphoneCallAborted; /*default status*/
aymeric's avatar
aymeric committed
119 120
	return cl;
}
121

122
void call_logs_write_to_config_file(LinphoneCore *lc){
123 124 125 126 127 128
	MSList *elem;
	char logsection[32];
	int i;
	char *tmp;
	LpConfig *cfg=lc->config;

129
	if (linphone_core_get_global_state (lc)==LinphoneGlobalStartup) return;
130

131 132 133
	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);
134
		lp_config_clean_section(cfg,logsection);
135 136 137 138 139 140 141 142
		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);
143 144 145
		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);
146 147
		lp_config_set_int(cfg,logsection,"duration",cl->duration);
		if (cl->refkey) lp_config_set_string(cfg,logsection,"refkey",cl->refkey);
148
		lp_config_set_float(cfg,logsection,"quality",cl->quality);
149
		lp_config_set_int(cfg,logsection,"video_enabled", cl->video_enabled);
jehan's avatar
jehan committed
150
		lp_config_set_string(cfg,logsection,"call_id",cl->call_id);
151 152 153 154 155 156 157
	}
	for(;i<lc->max_call_logs;++i){
		snprintf(logsection,sizeof(logsection),"call_log_%i",i);
		lp_config_clean_section(cfg,logsection);
	}
}

158
static time_t string_to_time(const char *date){
159
#ifndef WIN32
160 161 162
	struct tm tmtime={0};
	strptime(date,"%c",&tmtime);
	return mktime(&tmtime);
163 164 165
#else
	return 0;
#endif
166 167
}

168 169 170 171
static void call_logs_read_from_config_file(LinphoneCore *lc){
	char logsection[32];
	int i;
	const char *tmp;
172
	uint64_t sec;
173 174 175 176 177 178 179 180 181 182 183
	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);
184 185 186 187 188 189 190 191 192 193 194 195
			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);
				}
			}
196 197 198
			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);
199
			cl->quality=lp_config_get_float(cfg,logsection,"quality",-1);
200
			cl->video_enabled=lp_config_get_int(cfg,logsection,"video_enabled",0);
201 202
			tmp=lp_config_get_string(cfg,logsection,"call_id",NULL);
			if (tmp) cl->call_id=ms_strdup(tmp);
203
			lc->call_logs=ms_list_append(lc->call_logs,cl);
204
		}else break;
205 206 207 208
	}
}


aymeric's avatar
aymeric committed
209

smorlat's avatar
smorlat committed
210 211 212 213 214 215 216
/**
 * @addtogroup call_logs
 * @{
**/

/**
 * Returns a human readable string describing the call.
217
 *
smorlat's avatar
smorlat committed
218 219
 * @note: the returned char* must be freed by the application (use ms_free()).
**/
aymeric's avatar
aymeric committed
220 221
char * linphone_call_log_to_str(LinphoneCallLog *cl){
	char *status;
222
	char *tmp;
223 224
	char *from=linphone_address_as_string (cl->from);
	char *to=linphone_address_as_string (cl->to);
aymeric's avatar
aymeric committed
225 226 227 228 229 230 231 232 233 234 235 236 237
	switch(cl->status){
		case LinphoneCallAborted:
			status=_("aborted");
			break;
		case LinphoneCallSuccess:
			status=_("completed");
			break;
		case LinphoneCallMissed:
			status=_("missed");
			break;
		default:
			status="unknown";
	}
238
	tmp=ortp_strdup_printf(_("%s at %s\nFrom: %s\nTo: %s\nStatus: %s\nDuration: %i mn %i sec\n"),
aymeric's avatar
aymeric committed
239 240
			(cl->dir==LinphoneCallIncoming) ? _("Incoming call") : _("Outgoing call"),
			cl->start_date,
241 242
			from,
			to,
aymeric's avatar
aymeric committed
243 244 245
			status,
			cl->duration/60,
			cl->duration%60);
246 247 248
	ms_free(from);
	ms_free(to);
	return tmp;
aymeric's avatar
aymeric committed
249 250
}

251 252
/**
 * Returns RTP statistics computed locally regarding the call.
253
 *
254 255 256 257 258 259 260 261 262 263 264 265 266 267
**/
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
268 269 270 271
const char *linphone_call_log_get_call_id(const LinphoneCallLog *cl){
	return cl->call_id;
}

272 273 274
/**
 * Assign a user pointer to the call log.
**/
smorlat's avatar
smorlat committed
275 276 277 278
void linphone_call_log_set_user_pointer(LinphoneCallLog *cl, void *up){
	cl->user_pointer=up;
}

279 280 281
/**
 * Returns the user pointer associated with the call log.
**/
smorlat's avatar
smorlat committed
282 283 284 285
void *linphone_call_log_get_user_pointer(const LinphoneCallLog *cl){
	return cl->user_pointer;
}

smorlat's avatar
smorlat committed
286 287


288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
/**
 * 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;
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327
/**
 * 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
328 329 330 331 332 333 334
/**
 * 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;
}

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
/**
 * 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;
}

smorlat's avatar
smorlat committed
370 371
/** @} */

aymeric's avatar
aymeric committed
372
void linphone_call_log_destroy(LinphoneCallLog *cl){
373 374 375
	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);
376
	if (cl->call_id) ms_free(cl->call_id);
aymeric's avatar
aymeric committed
377 378 379
	ms_free(cl);
}

380 381 382 383 384 385 386 387 388 389 390 391
/**
 * 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;
}

392 393
int linphone_core_get_current_call_duration(const LinphoneCore *lc){
	LinphoneCall *call=linphone_core_get_current_call((LinphoneCore *)lc);
394 395
	if (call)  return linphone_call_get_duration(call);
	return -1;
396 397 398 399
}

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
400
	if (call==NULL) return NULL;
401
	return linphone_call_get_remote_address(call);
smorlat's avatar
smorlat committed
402 403
}

404 405 406 407 408 409
/**
 * Enable logs in supplied FILE*.
 *
 * @ingroup misc
 *
 * @param file a C FILE* where to fprintf logs. If null stdout is used.
410
 *
411
**/
aymeric's avatar
aymeric committed
412 413 414 415 416 417
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);
}

418 419 420 421 422 423 424
/**
 * 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);
425
 *
426
**/
aymeric's avatar
aymeric committed
427 428 429 430 431
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);
}

432 433 434 435 436
/**
 * Entirely disable logging.
 *
 * @ingroup misc
**/
aymeric's avatar
aymeric committed
437 438 439 440 441
void linphone_core_disable_logs(){
	ortp_set_log_level_mask(ORTP_ERROR|ORTP_FATAL);
}


442
static void net_config_read (LinphoneCore *lc)
aymeric's avatar
aymeric committed
443 444 445 446 447
{
	int tmp;
	const char *tmpstr;
	LpConfig *config=lc->config;

448
	lc->net_conf.nat_address_ip = NULL;
aymeric's avatar
aymeric committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462
	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
463 464 465
	tmp=lp_config_get_int(lc->config,"net","download_ptime",0);
	linphone_core_set_download_ptime(lc,tmp);

aymeric's avatar
aymeric committed
466 467
}

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
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
484

485
static void sound_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
486
{
487
	int tmp;
aymeric's avatar
aymeric committed
488 489
	const char *tmpbuf;
	const char *devid;
490
#ifdef __linux
aymeric's avatar
aymeric committed
491 492 493 494 495 496
	/*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);
	}
Simon Morlat's avatar
Simon Morlat committed
497
	tmp=lp_config_get_int(lc->config,"sound","alsa_forced_rate",-1);
498
	if (tmp>0) ms_alsa_card_set_forced_sample_rate(tmp);
aymeric's avatar
aymeric committed
499 500
#endif
	/* retrieve all sound devices */
501 502
	build_sound_devices_table(lc);

aymeric's avatar
aymeric committed
503 504
	devid=lp_config_get_string(lc->config,"sound","playback_dev_id",NULL);
	linphone_core_set_playback_device(lc,devid);
505

aymeric's avatar
aymeric committed
506 507
	devid=lp_config_get_string(lc->config,"sound","ringer_dev_id",NULL);
	linphone_core_set_ringer_device(lc,devid);
508

aymeric's avatar
aymeric committed
509 510
	devid=lp_config_get_string(lc->config,"sound","capture_dev_id",NULL);
	linphone_core_set_capture_device(lc,devid);
511

aymeric's avatar
aymeric committed
512 513 514 515 516 517 518 519 520 521
/*
	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]);
*/
522

aymeric's avatar
aymeric committed
523 524
	tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
	tmpbuf=lp_config_get_string(lc->config,"sound","local_ring",tmpbuf);
Jehan Monnier's avatar
Jehan Monnier committed
525
	if (ortp_file_exist(tmpbuf)==-1) {
526
		ms_warning("%s does not exist",tmpbuf);
aymeric's avatar
aymeric committed
527 528 529 530 531 532 533
		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);
534

535
	tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
aymeric's avatar
aymeric committed
536
	tmpbuf=lp_config_get_string(lc->config,"sound","remote_ring",tmpbuf);
Jehan Monnier's avatar
Jehan Monnier committed
537
	if (ortp_file_exist(tmpbuf)==-1){
538
		tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
aymeric's avatar
aymeric committed
539 540 541
	}
	if (strstr(tmpbuf,".wav")==NULL){
		/* it currently uses old sound files, so replace them */
542
		tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
aymeric's avatar
aymeric committed
543
	}
544
	linphone_core_set_ringback(lc,tmpbuf);
545 546

	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
547 548
	check_sound_device(lc);
	lc->sound_conf.latency=0;
Simon Morlat's avatar
Simon Morlat committed
549
#ifndef __ios 
Simon Morlat's avatar
Simon Morlat committed
550
	tmp=TRUE;
551
#else
Simon Morlat's avatar
Simon Morlat committed
552
	tmp=FALSE; /* on iOS we have builtin echo cancellation.*/
553
#endif
Simon Morlat's avatar
Simon Morlat committed
554
	tmp=lp_config_get_int(lc->config,"sound","echocancellation",tmp);
555
	linphone_core_enable_echo_cancellation(lc,tmp);
smorlat's avatar
smorlat committed
556 557
	linphone_core_enable_echo_limiter(lc,
		lp_config_get_int(lc->config,"sound","echolimiter",0));
558 559
	linphone_core_enable_agc(lc,
		lp_config_get_int(lc->config,"sound","agc",0));
560

Yann Diorcet's avatar
Yann Diorcet committed
561 562
	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));
563 564

	linphone_core_set_remote_ringback_tone (lc,lp_config_get_string(lc->config,"sound","ringback_tone",NULL));
565 566 567

	/*just parse requested stream feature once at start to print out eventual errors*/
	linphone_core_get_audio_features(lc);
aymeric's avatar
aymeric committed
568 569
}

570
static void sip_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
571 572 573
{
	char *contact;
	const char *tmpstr;
574
	LCSipTransports tr;
aymeric's avatar
aymeric committed
575 576
	int i,tmp;
	int ipv6;
Simon Morlat's avatar
Simon Morlat committed
577
	int random_port;
578

579 580
	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
581

582 583 584
	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
585

586
	sal_use_rport(lc->sal,lp_config_get_int(lc->config,"sip","use_rport",1));
587
	sal_use_101(lc->sal,lp_config_get_int(lc->config,"sip","use_101",1));
588
	sal_reuse_authorization(lc->sal, lp_config_get_int(lc->config,"sip","reuse_authorization",0));
589
	sal_expire_old_registration_contacts(lc->sal,lp_config_get_int(lc->config,"sip","expire_old_registration_contacts",0));
590

591
	tmp=lp_config_get_int(lc->config,"sip","use_rfc2833",1);
592
	linphone_core_set_use_rfc2833_for_dtmf(lc,tmp);
593

aymeric's avatar
aymeric committed
594 595 596 597 598
	ipv6=lp_config_get_int(lc->config,"sip","use_ipv6",-1);
	if (ipv6==-1){
		ipv6=0;
	}
	linphone_core_enable_ipv6(lc,ipv6);
599
	memset(&tr,0,sizeof(tr));
Simon Morlat's avatar
Simon Morlat committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
	
	tr.udp_port=lp_config_get_int(lc->config,"sip","sip_port",0);
	tr.tcp_port=lp_config_get_int(lc->config,"sip","sip_tcp_port",0);
	tr.tls_port=lp_config_get_int(lc->config,"sip","sip_tls_port",0);
	
	if (lp_config_get_int(lc->config,"sip","sip_random_port",0)==1)
		random_port=(0xDFFF&random())+1024;
	else random_port=0;
	
	if (tr.udp_port==0 && tr.tcp_port==0 && tr.tls_port==0){
		tr.udp_port=5060;
	}	
	
	if (tr.udp_port>0 && random_port){
		tr.udp_port=random_port;
615
		tr.tls_port=tr.tcp_port=0; /*make sure only one transport is active at a time*/
Simon Morlat's avatar
Simon Morlat committed
616 617
	}else if (tr.tcp_port>0 && random_port){
		tr.tcp_port=random_port;
618
		tr.tls_port=tr.udp_port=0; /*make sure only one transport is active at a time*/
Simon Morlat's avatar
Simon Morlat committed
619 620
	}else if (tr.tls_port>0 && random_port){
		tr.tls_port=random_port;
621
		tr.udp_port=tr.tcp_port=0; /*make sure only one transport is active at a time*/
Simon Morlat's avatar
Simon Morlat committed
622
	} 
623 624 625 626 627 628 629

#ifdef __linux
	sal_set_root_ca(lc->sal, lp_config_get_string(lc->config,"sip","root_ca", "/etc/ssl/certs"));
#else
	sal_set_root_ca(lc->sal, lp_config_get_string(lc->config,"sip","root_ca", ROOT_CA_FILE));
#endif
	linphone_core_verify_server_certificates(lc,lp_config_get_int(lc->config,"sip","verify_server_certs",TRUE));
630
	linphone_core_verify_server_cn(lc,lp_config_get_int(lc->config,"sip","verify_server_cn",TRUE));
631 632
	/*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));
633 634
	/*start listening on ports*/
 	linphone_core_set_sip_transports(lc,&tr);
635

aymeric's avatar
aymeric committed
636 637
	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
638 639
		const char *hostname=NULL;
		const char *username=NULL;
640
#ifdef HAVE_GETENV
smorlat's avatar
smorlat committed
641 642
		hostname=getenv("HOST");
		username=getenv("USER");
aymeric's avatar
aymeric committed
643
		if (hostname==NULL) hostname=getenv("HOSTNAME");
644
#endif /*HAVE_GETENV*/
aymeric's avatar
aymeric committed
645 646 647 648 649 650 651 652 653 654 655 656
		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);
657 658


659
	tmp=lp_config_get_int(lc->config,"sip","inc_timeout",30);
aymeric's avatar
aymeric committed
660 661
	linphone_core_set_inc_timeout(lc,tmp);

Yann Diorcet's avatar
Yann Diorcet committed
662 663
	tmp=lp_config_get_int(lc->config,"sip","in_call_timeout",0);
	linphone_core_set_in_call_timeout(lc,tmp);
664 665 666
	
	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
667

aymeric's avatar
aymeric committed
668 669 670 671 672 673 674 675 676 677 678 679
	/* 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);
680

aymeric's avatar
aymeric committed
681 682 683 684 685
	/* 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
686
			linphone_auth_info_destroy(ai);
aymeric's avatar
aymeric committed
687 688 689 690
		}else{
			break;
		}
	}
691 692
	/*this is to filter out unsupported encryption schemes*/
	linphone_core_set_media_encryption(lc,linphone_core_get_media_encryption(lc));
693

694
	/*for tuning or test*/
aymeric's avatar
aymeric committed
695
	lc->sip_conf.sdp_200_ack=lp_config_get_int(lc->config,"sip","sdp_200_ack",0);
696
	lc->sip_conf.register_only_when_network_is_up=
697
		lp_config_get_int(lc->config,"sip","register_only_when_network_is_up",1);
698 699
	lc->sip_conf.register_only_when_upnp_is_ok=
		lp_config_get_int(lc->config,"sip","register_only_when_upnp_is_ok",1);
700
	lc->sip_conf.ping_with_options=lp_config_get_int(lc->config,"sip","ping_with_options",1);
jehan's avatar
jehan committed
701
	lc->sip_conf.auto_net_state_mon=lp_config_get_int(lc->config,"sip","auto_net_state_mon",1);
702
	lc->sip_conf.keepalive_period=lp_config_get_int(lc->config,"sip","keepalive_period",10000);
703 704
	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
705
	sal_use_one_matching_codec_policy(lc->sal,lp_config_get_int(lc->config,"sip","only_one_codec",0));
706
	sal_use_double_registrations(lc->sal,lp_config_get_int(lc->config,"sip","use_double_registrations",1));
707
	sal_use_dates(lc->sal,lp_config_get_int(lc->config,"sip","put_date",0));
aymeric's avatar
aymeric committed
708 709
}

710
static void rtp_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
711
{
712
	int min_port, max_port;
aymeric's avatar
aymeric committed
713 714
	int jitt_comp;
	int nortp_timeout;
Simon Morlat's avatar
Simon Morlat committed
715
	bool_t rtp_no_xmit_on_audio_mute;
716
	bool_t adaptive_jitt_comp_enabled;
Simon Morlat's avatar
Simon Morlat committed
717

718 719 720 721 722 723 724 725
	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);
	}
726

727 728 729 730 731 732 733 734
	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);
	}
735

aymeric's avatar
aymeric committed
736
	jitt_comp=lp_config_get_int(lc->config,"rtp","audio_jitt_comp",60);
737
	linphone_core_set_audio_jittcomp(lc,jitt_comp);
aymeric's avatar
aymeric committed
738
	jitt_comp=lp_config_get_int(lc->config,"rtp","video_jitt_comp",60);
739
	if (jitt_comp==0) jitt_comp=60;
740
	linphone_core_set_video_jittcomp(lc,jitt_comp);
aymeric's avatar
aymeric committed
741
	nortp_timeout=lp_config_get_int(lc->config,"rtp","nortp_timeout",30);
742
	linphone_core_set_nortp_timeout(lc,nortp_timeout);
Simon Morlat's avatar
Simon Morlat committed
743
	rtp_no_xmit_on_audio_mute=lp_config_get_int(lc->config,"rtp","rtp_no_xmit_on_audio_mute",FALSE);
744
	linphone_core_set_rtp_no_xmit_on_audio_mute(lc,rtp_no_xmit_on_audio_mute);
745 746 747 748
	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);
aymeric's avatar
aymeric committed
749 750
}

751
static PayloadType * find_payload(RtpProfile *prof, const char *mime_type, int clock_rate, int channels, const char *recv_fmtp){
752 753 754
	PayloadType *candidate=NULL;
	int i;
	PayloadType *it;
755
	for(i=0;i<RTP_PROFILE_MAX_PAYLOADS;++i){
756 757
		it=rtp_profile_get_payload(prof,i);
		if (it!=NULL && strcasecmp(mime_type,it->mime_type)==0
758 759
			&& (clock_rate==it->clock_rate || clock_rate<=0)
			&& (channels==it->channels || channels<=0) ){
760
			if ( (recv_fmtp && it->recv_fmtp && strstr(recv_fmtp,it->recv_fmtp)!=NULL) ||
761 762
				(recv_fmtp==NULL && it->recv_fmtp==NULL) ){
				/*exact match*/
763
				if (recv_fmtp) payload_type_set_recv_fmtp(it,recv_fmtp);
764
				return it;
765 766 767 768 769
			}else {
				if (candidate){
					if (it->recv_fmtp==NULL) candidate=it;
				}else candidate=it;
			}
770 771
		}
	}
772 773 774
	if (candidate && recv_fmtp){
		payload_type_set_recv_fmtp(candidate,recv_fmtp);
	}
775 776
	return candidate;
}
aymeric's avatar
aymeric committed
777

Simon Morlat's avatar
Simon Morlat committed
778
static bool_t get_codec(LinphoneCore *lc, const char* type, int index, PayloadType **ret){
aymeric's avatar
aymeric committed
779 780
	char codeckey[50];
	const char *mime,*fmtp;
781
	int rate,channels,enabled;
aymeric's avatar
aymeric committed
782
	PayloadType *pt;
Simon Morlat's avatar
Simon Morlat committed
783
	LpConfig *config=lc->config;
784

785
	*ret=NULL;
aymeric's avatar
aymeric committed
786 787
	snprintf(codeckey,50,"%s_%i",type,index);
	mime=lp_config_get_string(config,codeckey,"mime",NULL);
788
	if (mime==NULL || strlen(mime)==0 ) return FALSE;
789

aymeric's avatar
aymeric committed
790 791
	rate=lp_config_get_int(config,codeckey,"rate",8000);
	fmtp=lp_config_get_string(config,codeckey,"recv_fmtp",NULL);
792
	channels=lp_config_get_int(config,codeckey,"channels",0);
aymeric's avatar
aymeric committed
793
	enabled=lp_config_get_int(config,codeckey,"enabled",1);
Simon Morlat's avatar
Simon Morlat committed
794
	pt=find_payload(lc->default_profile,mime,rate,channels,fmtp);
795
	if (pt && enabled ) pt->flags|=PAYLOAD_TYPE_ENABLED;
aymeric's avatar
aymeric committed
796
	//ms_message("Found codec %s/%i",pt->mime_type,pt->clock_rate);
797 798 799 800 801 802
	if (pt==NULL) ms_warning("Ignoring codec config %s/%i with fmtp=%s because unsupported",
	    		mime,rate,fmtp ? fmtp : "");
	*ret=pt;
	return TRUE;
}

803
#define RANK_END 10000
Simon Morlat's avatar
Simon Morlat committed
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819

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

static codec_desc_t codec_pref_order[]={
	{"SILK", 16000},
	{"speex", 16000},
	{"speex", 8000},
	{"pcmu",8000},
	{"pcma",8000},
	{"VP8",90000},
	{"H264",90000},
	{"MP4V-ES",90000},
	{NULL,0}
820 821
};

Simon Morlat's avatar
Simon Morlat committed
822
static int find_codec_rank(const char *mime, int clock_rate){
823
	int i;
Simon Morlat's avatar
Simon Morlat committed
824 825
	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)
826
			return i;
827
	}
828
	return RANK_END;
829 830 831 832
}

static int codec_compare(const PayloadType *a, const PayloadType *b){
	int ra,rb;
Simon Morlat's avatar
Simon Morlat committed
833 834
	ra=find_codec_rank(a->mime_type,a->clock_rate);
	rb=find_codec_rank(b->mime_type,b->clock_rate);
835 836
	if (ra>rb) return 1;
	if (ra<rb) return -1;
837
	return 0;
838 839
}

Simon Morlat's avatar
Simon Morlat committed
840
static MSList *add_missing_codecs(LinphoneCore *lc, SalStreamType mtype, MSList *l){
841
	int i;
842
	for(i=0;i<RTP_PROFILE_MAX_PAYLOADS;++i){
Simon Morlat's avatar
Simon Morlat committed
843
		PayloadType *pt=rtp_profile_get_payload(lc->default_profile,i);
844 845 846
		if (pt){
			if (mtype==SalVideo && pt->type!=PAYLOAD_VIDEO)
				pt=NULL;
847
			else if (mtype==SalAudio && (pt->type!=PAYLOAD_AUDIO_PACKETIZED
848 849 850 851 852
			    && pt->type!=PAYLOAD_AUDIO_CONTINUOUS)){
				pt=NULL;
			}
			if (pt && ms_filter_codec_supported(pt->mime_type)){
				if (ms_list_find(l,pt)==NULL){
853
					/*unranked codecs are disabled by default*/
Simon Morlat's avatar
Simon Morlat committed
854
					if (find_codec_rank(pt->mime_type, pt->clock_rate)!=RANK_END){
855 856
						payload_type_set_flag(pt,PAYLOAD_TYPE_ENABLED);
					}
857 858
					ms_message("Adding new codec %s/%i with fmtp %s",
					    pt->mime_type,pt->clock_rate,pt->recv_fmtp ? pt->recv_fmtp : "");
859
					l=ms_list_insert_sorted(l,pt,(int (*)(const void *, const void *))codec_compare);
860 861 862 863 864
				}
			}
		}
	}
	return l;
aymeric's avatar
aymeric committed
865 866
}

867 868
static MSList *codec_append_if_new(MSList *l, PayloadType *pt){
	MSList *elem;
869
	for (elem=l;elem!=NULL;elem=elem->next){
870 871 872 873 874 875 876 877
		PayloadType *ept=(PayloadType*)elem->data;
		if (pt==ept)
			return l;
	}
	l=ms_list_append(l,pt);
	return l;
}

878
static void codecs_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
879 880 881 882 883
{
	int i;
	PayloadType *pt;
	MSList *audio_codecs=NULL;
	MSList *video_codecs=NULL;
Simon Morlat's avatar
Simon Morlat committed
884
	for (i=0;get_codec(lc,"audio_codec",i,&pt);i++){
885 886 887
		if (pt){
			if (!ms_filter_codec_supported(pt->mime_type)){
				ms_warning("Codec %s is not supported by mediastreamer2, removed.",pt->mime_type);
888
			}else audio_codecs=codec_append_if_new(audio_codecs,pt);
889
		}
aymeric's avatar
aymeric committed
890
	}
Simon Morlat's avatar
Simon Morlat committed
891 892
	audio_codecs=add_missing_codecs(lc,SalAudio,audio_codecs);
	for (i=0;get_codec(lc,"video_codec",i,&pt);i++){
893 894 895
		if (pt){
			if (!ms_filter_codec_supported(pt->mime_type)){
				ms_warning("Codec %s is not supported by mediastreamer2, removed.",pt->mime_type);
896
			}else video_codecs=codec_append_if_new(video_codecs,(void *)pt);
897
		}
aymeric's avatar
aymeric committed
898
	}
Simon Morlat's avatar
Simon Morlat committed
899
	video_codecs=add_missing_codecs(lc,SalVideo,video_codecs);
aymeric's avatar
aymeric committed
900 901
	linphone_core_set_audio_codecs(lc,audio_codecs);
	linphone_core_set_video_codecs(lc,video_codecs);
902
	linphone_core_update_allocated_audio_bandwidth(lc);
aymeric's avatar
aymeric committed
903 904
}

905
static void build_video_devices_table(LinphoneCore *lc){
aymeric's avatar
aymeric committed
906 907
	const MSList *elem;
	int i;
908 909 910 911
	int ndev;
	const char **devices;
	if (lc->video_conf.cams)
		ms_free(lc->video_conf.cams);
aymeric's avatar
aymeric committed
912 913 914 915 916 917 918 919 920
	/* 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;
921 922 923 924 925 926 927
}

static void video_config_read(LinphoneCore *lc){
#ifdef VIDEO_ENABLED
	int capture, display, self_view;
#endif
	const char *str;	
928
#ifdef VIDEO_ENABLED	
929
	LinphoneVideoPolicy vpol;
930
	memset(&vpol, 0, sizeof(LinphoneVideoPolicy));
931
#endif
932
	build_video_devices_table(lc);
aymeric's avatar
aymeric committed
933 934 935 936

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