linphonecore.c 230 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
#include "quality_reporting.h"
25

Yann Diorcet's avatar
Yann Diorcet committed
26
#include <math.h>
27 28
#include <sys/types.h>
#include <sys/stat.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
#ifdef HAVE_CONFIG_H
44
#include "config.h"
45 46 47
#ifndef ANDROID /*on Android LIBLINPHONE version is passed from root Makefile*/
	#include "liblinphone_gitversion.h"
#endif
Ghislain MARY's avatar
Ghislain MARY committed
48 49 50
#else
#ifndef LIBLINPHONE_GIT_VERSION
#define LIBLINPHONE_GIT_VERSION "unknown"
51 52 53
#endif
#endif

54 55 56
#ifdef __APPLE__
#include "TargetConditionals.h"
#endif
57

58 59 60 61 62 63 64 65 66 67 68
#ifdef HAVE_ZLIB
#ifdef WIN32
#include <fcntl.h>
#include <io.h>
#define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
#else
#define SET_BINARY_MODE(file)
#endif
#include <zlib.h>
#endif

smorlat's avatar
smorlat committed
69
/*#define UNSTANDART_GSM_11K 1*/
aymeric's avatar
aymeric committed
70

71 72
#define ROOT_CA_FILE PACKAGE_DATA_DIR "/linphone/rootca.pem"

73 74 75 76 77 78 79
static const char *liblinphone_version=
#ifdef LIBLINPHONE_GIT_VERSION
	LIBLINPHONE_GIT_VERSION
#else
	LIBLINPHONE_VERSION
#endif
;
80 81 82
static OrtpLogFunc liblinphone_log_func = NULL;
static bool_t liblinphone_log_collection_enabled = FALSE;
static const char * liblinphone_log_collection_path = ".";
83
static bool_t liblinphone_serialize_logs = FALSE;
84
static void set_network_reachable(LinphoneCore* lc,bool_t isReachable, time_t curtime);
Simon Morlat's avatar
Simon Morlat committed
85 86
static void linphone_core_run_hooks(LinphoneCore *lc);
static void linphone_core_free_hooks(LinphoneCore *lc);
aymeric's avatar
aymeric committed
87 88

#include "enum.h"
89
#include "contact_providers_priv.h"
90

91

92
const char *linphone_core_get_nat_address_resolved(LinphoneCore *lc);
aymeric's avatar
aymeric committed
93 94
static void toggle_video_preview(LinphoneCore *lc, bool_t val);

95
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
96
#define SOUNDS_PREFIX
97 98
#else
#define SOUNDS_PREFIX "Assets/Sounds/"
99
#endif
aymeric's avatar
aymeric committed
100
/* relative path where is stored local ring*/
101
#define LOCAL_RING SOUNDS_PREFIX "rings/oldphone.wav"
aymeric's avatar
aymeric committed
102
/* same for remote ring (ringback)*/
103 104
#define REMOTE_RING SOUNDS_PREFIX "ringback.wav"
#define HOLD_MUSIC SOUNDS_PREFIX "rings/toy-mono.wav"
105

aymeric's avatar
aymeric committed
106

Simon Morlat's avatar
Simon Morlat committed
107
extern SalCallbacks linphone_sal_callbacks;
aymeric's avatar
aymeric committed
108 109 110 111 112 113 114 115 116 117 118 119

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

120

121 122 123 124 125 126 127 128 129 130 131 132
/**
 * 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;
}

133 134
int linphone_core_get_current_call_duration(const LinphoneCore *lc){
	LinphoneCall *call=linphone_core_get_current_call((LinphoneCore *)lc);
135 136
	if (call)  return linphone_call_get_duration(call);
	return -1;
137 138 139 140
}

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
141
	if (call==NULL) return NULL;
142
	return linphone_call_get_remote_address(call);
smorlat's avatar
smorlat committed
143 144
}

145
void linphone_core_set_log_handler(OrtpLogFunc logfunc) {
146 147
	liblinphone_log_func = logfunc;
	ortp_set_log_handler(liblinphone_log_func);
148 149 150 151 152 153 154 155 156
}

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);
157 158 159 160 161
	if (loglevel == 0) {
		sal_disable_logs();
	} else {
		sal_enable_logs();
	}
162 163
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 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
#define LOGFILE_MAXSIZE (10 * 1024 * 1024)

static void linphone_core_log_collection_handler(OrtpLogLevel level, const char *fmt, va_list args) {
	const char *lname="undef";
	char *msg;
	char *log_filename1;
	char *log_filename2;
	FILE *log_file;
	struct timeval tp;
    struct tm *lt;
	time_t tt;
	struct stat statbuf;

	if (liblinphone_log_func != NULL) {
		liblinphone_log_func(level, fmt, args);
	}

	ortp_gettimeofday(&tp, NULL);
	tt = (time_t)tp.tv_sec;
	lt = localtime((const time_t*)&tt);
	switch(level){
		case ORTP_DEBUG:
			lname = "DEBUG";
			break;
		case ORTP_MESSAGE:
			lname = "MESSAGE";
			break;
		case ORTP_WARNING:
			lname = "WARNING";
			break;
		case ORTP_ERROR:
			lname = "ERROR";
			break;
		case ORTP_FATAL:
			lname = "FATAL";
			break;
		default:
			ortp_fatal("Bad level !");
	}
	msg = ortp_strdup_vprintf(fmt, args);

	log_filename1 = ortp_strdup_printf("%s/%s", liblinphone_log_collection_path, "linphone1.log");
	log_filename2 = ortp_strdup_printf("%s/%s", liblinphone_log_collection_path, "linphone2.log");
	log_file = fopen(log_filename1, "a");
	fstat(fileno(log_file), &statbuf);
	if (statbuf.st_size > LOGFILE_MAXSIZE) {
		fclose(log_file);
		log_file = fopen(log_filename2, "a");
		fstat(fileno(log_file), &statbuf);
		if (statbuf.st_size > LOGFILE_MAXSIZE) {
			fclose(log_file);
			unlink(log_filename1);
			rename(log_filename2, log_filename1);
			log_file = fopen(log_filename2, "a");
		}
	}
	fprintf(log_file,"%i-%.2i-%.2i %.2i:%.2i:%.2i:%.3i %s %s\n",
		1900 + lt->tm_year, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec, (int)(tp.tv_usec / 1000), lname, msg);
	fflush(log_file);
	fclose(log_file);

	ortp_free(log_filename1);
	ortp_free(log_filename2);
	ortp_free(msg);
}

void linphone_core_set_log_collection_path(const char *path) {
	liblinphone_log_collection_path = path;
}

const char *linphone_core_get_log_collection_upload_server_url(LinphoneCore *core) {
	return lp_config_get_string(core->config, "misc", "log_collection_upload_server_url", NULL);
}

void linphone_core_set_log_collection_upload_server_url(LinphoneCore *core, const char *server_url) {
	lp_config_set_string(core->config, "misc", "log_collection_upload_server_url", server_url);
}

void linphone_core_enable_log_collection(bool_t enable) {
	liblinphone_log_collection_enabled = enable;
	if (liblinphone_log_collection_enabled == TRUE) {
		ortp_set_log_handler(linphone_core_log_collection_handler);
	} else {
		ortp_set_log_handler(liblinphone_log_func);
	}
}

static void process_io_error_upload_log_collection(void *data, const belle_sip_io_error_event_t *event) {
	LinphoneCore *core = (LinphoneCore *)data;
	ms_error("I/O Error during log collection upload to %s", linphone_core_get_log_collection_upload_server_url(core));
	linphone_core_notify_log_collection_upload_state_changed(core, LinphoneCoreLogCollectionUploadStateNotDelivered, "I/O Error");
}

static void process_auth_requested_upload_log_collection(void *data, belle_sip_auth_event_t *event) {
	LinphoneCore *core = (LinphoneCore *)data;
	ms_error("Error during log collection upload: auth requested to connect %s", linphone_core_get_log_collection_upload_server_url(core));
	linphone_core_notify_log_collection_upload_state_changed(core, LinphoneCoreLogCollectionUploadStateNotDelivered, "Auth requested");
}

extern const char *multipart_boundary;

/**
 * Callback called when posting a log collection file to server (following rcs5.1 recommendation)
 *
 * @param[in] bh The body handler
 * @param[in] msg The belle sip message
 * @param[in] data The user data associated with the handler, contains the LinphoneCore object
 * @param[in] offset The current position in the input buffer
 * @param[in] buffer The ouput buffer where to copy the data to be uploaded
 * @param[in,out] size The size in byte of the data requested, as output it will contain the effective copied size
 *
 */
static int log_collection_upload_on_send_body(belle_sip_user_body_handler_t *bh, belle_sip_message_t *msg, void *data, size_t offset, uint8_t *buffer, size_t *size) {
	LinphoneCore *core = (LinphoneCore *)data;

	/* If we've not reach the end of file yet, fill the buffer with more data */
	if (offset < core->log_collection_upload_information->size) {
281 282 283 284 285
#ifdef HAVE_ZLIB
		char *log_filename = ortp_strdup_printf("%s/%s", liblinphone_log_collection_path, "linphone_log.zlib");
		FILE *log_file = fopen(log_filename, "rb");
#else
		char *log_filename = ortp_strdup_printf("%s/%s", liblinphone_log_collection_path, "linphone_log.txt");
286
		FILE *log_file = fopen(log_filename, "r");
287
#endif
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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
		fseek(log_file, offset, SEEK_SET);
		*size = fread(buffer, 1, *size, log_file);
		fclose(log_file);
		ortp_free(log_filename);
	}

	return BELLE_SIP_CONTINUE;
}

/**
 * Callback called during upload of a log collection to server.
 * It is just forwarding the call and some parameters to the vtable defined callback.
 */
static void log_collection_upload_on_progress(belle_sip_body_handler_t *bh, belle_sip_message_t *msg, void *data, size_t offset, size_t total) {
	LinphoneCore *core = (LinphoneCore *)data;
	linphone_core_notify_log_collection_upload_progress_indication(core, (size_t)(((double)offset / (double)total) * 100.0));
}

/**
 * Callback function called when we have a response from server during the upload of the log collection to the server (rcs5.1 recommandation)
 * Note: The first post is empty and the server shall reply a 204 (No content) message, this will trigger a new post request to the server
 * to upload the file. The server response to this second post is processed by this same function
 *
 * @param[in] data The user-defined pointer associated with the request, it contains the LinphoneCore object
 * @param[in] event The response from server
 */
static void process_response_from_post_file_log_collection(void *data, const belle_http_response_event_t *event) {
	LinphoneCore *core = (LinphoneCore *)data;

	/* Check the answer code */
	if (event->response) {
		int code = belle_http_response_get_status_code(event->response);
		if (code == 204) { /* This is the reply to the first post to the server - an empty file */
			/* Start uploading the file */
			belle_http_request_listener_callbacks_t cbs = { 0 };
			belle_http_request_listener_t *l;
			belle_generic_uri_t *uri;
			belle_http_request_t *req;
			belle_sip_multipart_body_handler_t *bh;
			char* ua;
			char *content_type;
			char *first_part_header;
			belle_sip_user_body_handler_t *first_part_bh;

			linphone_core_notify_log_collection_upload_state_changed(core, LinphoneCoreLogCollectionUploadStateInProgress, NULL);

			/* Temporary storage for the Content-disposition header value */
			first_part_header = belle_sip_strdup_printf("form-data; name=\"File\"; filename=\"%s\"", core->log_collection_upload_information->name);

			/* Create a user body handler to take care of the file and add the content disposition and content-type headers */
			first_part_bh = belle_sip_user_body_handler_new(core->log_collection_upload_information->size, NULL, NULL, log_collection_upload_on_send_body, core);
			belle_sip_body_handler_add_header((belle_sip_body_handler_t *)first_part_bh, belle_sip_header_create("Content-disposition", first_part_header));
			belle_sip_free(first_part_header);
			belle_sip_body_handler_add_header((belle_sip_body_handler_t *)first_part_bh, (belle_sip_header_t *)belle_sip_header_content_type_create(core->log_collection_upload_information->type, core->log_collection_upload_information->subtype));

			/* Insert it in a multipart body handler which will manage the boundaries of multipart message */
			bh = belle_sip_multipart_body_handler_new(log_collection_upload_on_progress, core, (belle_sip_body_handler_t *)first_part_bh);
			ua = ms_strdup_printf("%s/%s", linphone_core_get_user_agent_name(), linphone_core_get_user_agent_version());
			content_type = belle_sip_strdup_printf("multipart/form-data; boundary=%s", multipart_boundary);
			uri = belle_generic_uri_parse(linphone_core_get_log_collection_upload_server_url(core));
			req = belle_http_request_create("POST", uri, belle_sip_header_create("User-Agent", ua), belle_sip_header_create("Content-type", content_type), NULL);
			ms_free(ua);
			belle_sip_free(content_type);
			belle_sip_message_set_body_handler(BELLE_SIP_MESSAGE(req), BELLE_SIP_BODY_HANDLER(bh));
			cbs.process_response = process_response_from_post_file_log_collection;
			cbs.process_io_error = process_io_error_upload_log_collection;
			cbs.process_auth_requested = process_auth_requested_upload_log_collection;
			l = belle_http_request_listener_create_from_callbacks(&cbs, core);
			belle_http_provider_send_request(core->http_provider, req, l);
		}
		if (code == 200) { /* The file has been uploaded correctly, get the server reply */
			xmlDocPtr xmlMessageBody;
			xmlNodePtr cur;
			xmlChar *file_url = NULL;
			const char *body = belle_sip_message_get_body((belle_sip_message_t *)event->response);
			xmlMessageBody = xmlParseDoc((const xmlChar *)body);
			cur = xmlDocGetRootElement(xmlMessageBody);
			if (cur != NULL) {
				cur = cur->xmlChildrenNode;
				while (cur != NULL) {
					if (!xmlStrcmp(cur->name, (const xmlChar *)"file-info")) { /* we found a file info node, check it has a type="file" attribute */
						xmlChar *typeAttribute = xmlGetProp(cur, (const xmlChar *)"type");
						if (!xmlStrcmp(typeAttribute, (const xmlChar *)"file")) { /* this is the node we are looking for */
							cur = cur->xmlChildrenNode; /* now loop on the content of the file-info node */
							while (cur != NULL) {
								if (!xmlStrcmp(cur->name, (const xmlChar *)"data")) {
									file_url = 	xmlGetProp(cur, (const xmlChar *)"url");
								}
								cur=cur->next;
							}
							xmlFree(typeAttribute);
							break;
						}
						xmlFree(typeAttribute);
					}
					cur = cur->next;
				}
			}
			if (file_url != NULL) {
				linphone_core_notify_log_collection_upload_state_changed(core, LinphoneCoreLogCollectionUploadStateDelivered, (const char *)file_url);
			}
		}
	}
}

393 394 395 396 397 398 399 400 401
#ifdef HAVE_ZLIB

static int compress_file(FILE *input_file, FILE *output_file, z_stream *strm) {
	unsigned char in[131072]; /* 128kB */
	unsigned char out[131072]; /* 128kB */
	unsigned int have;
	int flush;
	int ret;
	size_t output_file_size = 0;
Simon Morlat's avatar
Simon Morlat committed
402
	
403 404
	do {
		strm->avail_in = fread(in, 1, sizeof(in), input_file);
Simon Morlat's avatar
Simon Morlat committed
405 406 407 408 409 410
		if (ferror(input_file)) {
		    deflateEnd(strm);
		    return Z_ERRNO;
		}
		flush = feof(input_file) ? Z_FINISH : Z_NO_FLUSH;
		strm->next_in = in;
411 412
		do {
			strm->avail_out = sizeof(out);
Simon Morlat's avatar
Simon Morlat committed
413
			strm->next_out = out;
414 415
			ret = deflate(strm, flush);
			have = sizeof(out) - strm->avail_out;
Simon Morlat's avatar
Simon Morlat committed
416 417 418 419 420
			if (fwrite(out, 1, have, output_file) != have || ferror(output_file)) {
				deflateEnd(strm);
				return Z_ERRNO;
			}
		    output_file_size += have;
421 422
		} while (strm->avail_out == 0);
	} while (flush != Z_FINISH);
Simon Morlat's avatar
Simon Morlat committed
423
	
424 425 426 427 428 429 430 431 432 433 434
	return output_file_size;
}

#else

/**
 * If zlib is not available the two log files are simply concatenated.
 */
static int compress_file(FILE *input_file, FILE *output_file) {
	char buffer[131072]; /* 128kB */
	size_t output_file_size = 0;
Simon Morlat's avatar
Simon Morlat committed
435
	size_t bytes;
436

Simon Morlat's avatar
Simon Morlat committed
437 438
	while ((bytes = fread(buffer, 1, sizeof(buffer), input_file)) > 0) {
		if (bytes == 0) return bytes;
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
		fwrite(buffer, 1, bytes, output_file);
		output_file_size += bytes;
	}
	return output_file_size;
}

#endif

static size_t prepare_log_collection_file_to_upload(const char *filename) {
	char *input_filename = NULL;
	char *output_filename = NULL;
	FILE *input_file = NULL;
	FILE *output_file = NULL;
	size_t output_file_size = 0;
	int ret;

#ifdef HAVE_ZLIB
	z_stream strm;
	strm.zalloc = Z_NULL;
Simon Morlat's avatar
Simon Morlat committed
458 459 460 461
	strm.zfree = Z_NULL;
	strm.opaque = Z_NULL;
	ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
	if (ret != Z_OK) return ret;
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
#endif

	output_filename = ortp_strdup_printf("%s/%s", liblinphone_log_collection_path, filename);
	output_file = fopen(output_filename, "a");
	if (output_file == NULL) goto error;
	input_filename = ortp_strdup_printf("%s/%s", liblinphone_log_collection_path, "linphone1.log");
	input_file = fopen(input_filename, "r");
	if (input_file == NULL) goto error;
#ifdef HAVE_ZLIB
	SET_BINARY_MODE(output_file);
	SET_BINARY_MODE(input_file);
	ret = compress_file(input_file, output_file, &strm);
#else
	ret = compress_file(input_file, output_file);
#endif
	if (ret < 0) goto error;
	output_file_size += ret;
	fclose(input_file);
	ortp_free(input_filename);
	input_filename = ortp_strdup_printf("%s/%s", liblinphone_log_collection_path, "linphone2.log");
	input_file = fopen(input_filename, "r");
	if (input_file != NULL) {
#ifdef HAVE_ZLIB
		SET_BINARY_MODE(input_file);
		ret = compress_file(input_file, output_file, &strm);
#else
		ret = compress_file(input_file, output_file);
#endif
		if (ret < 0) goto error;
		output_file_size += ret;
	}
error:
	if (input_file != NULL) fclose(input_file);
	if (output_file != NULL) fclose(output_file);
	if (input_filename != NULL) ortp_free(input_filename);
	if (output_filename != NULL) ortp_free(output_filename);
	return output_file_size;
}

501 502 503 504 505 506 507 508 509 510
void linphone_core_upload_log_collection(LinphoneCore *core) {
	if ((core->log_collection_upload_information == NULL) && (linphone_core_get_log_collection_upload_server_url(core) != NULL) && (liblinphone_log_collection_enabled == TRUE)) {
		/* open a transaction with the server and send an empty request(RCS5.1 section 3.5.4.8.3.1) */
		belle_http_request_listener_callbacks_t cbs = { 0 };
		belle_http_request_listener_t *l;
		belle_generic_uri_t *uri;
		belle_http_request_t *req;

		core->log_collection_upload_information = (LinphoneContent *)malloc(sizeof(LinphoneContent));
		memset(core->log_collection_upload_information, 0, sizeof(LinphoneContent));
511 512 513 514 515
#ifdef HAVE_ZLIB
		core->log_collection_upload_information->type = "application";
		core->log_collection_upload_information->subtype = "x-deflate";
		core->log_collection_upload_information->name = "linphone_log.zlib";
#else
516 517 518
		core->log_collection_upload_information->type = "text";
		core->log_collection_upload_information->subtype = "plain";
		core->log_collection_upload_information->name = "linphone_log.txt";
519 520
#endif
		core->log_collection_upload_information->size = prepare_log_collection_file_to_upload(core->log_collection_upload_information->name);
521 522 523 524 525 526 527 528 529 530
		uri = belle_generic_uri_parse(linphone_core_get_log_collection_upload_server_url(core));
		req = belle_http_request_create("POST", uri, NULL, NULL, NULL);
		cbs.process_response = process_response_from_post_file_log_collection;
		cbs.process_io_error = process_io_error_upload_log_collection;
		cbs.process_auth_requested = process_auth_requested_upload_log_collection;
		l = belle_http_request_listener_create_from_callbacks(&cbs, core);
		belle_http_provider_send_request(core->http_provider, req, l);
	}
}

531 532 533 534
/**
 * Enable logs in supplied FILE*.
 *
 * @ingroup misc
535
 * @deprecated Use #linphone_core_set_log_file and #linphone_core_set_log_level instead.
536 537
 *
 * @param file a C FILE* where to fprintf logs. If null stdout is used.
538
 *
539
**/
aymeric's avatar
aymeric committed
540 541 542 543
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
544
	sal_enable_logs();
aymeric's avatar
aymeric committed
545 546
}

547 548 549 550
/**
 * Enable logs through the user's supplied log callback.
 *
 * @ingroup misc
551
 * @deprecated Use #linphone_core_set_log_handler and #linphone_core_set_log_level instead.
552 553 554
 *
 * @param logfunc The address of a OrtpLogFunc callback whose protoype is
 *            	  typedef void (*OrtpLogFunc)(OrtpLogLevel lev, const char *fmt, va_list args);
555
 *
556
**/
aymeric's avatar
aymeric committed
557 558 559
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
560
	sal_enable_logs();
aymeric's avatar
aymeric committed
561 562
}

563 564 565 566
/**
 * Entirely disable logging.
 *
 * @ingroup misc
567
 * @deprecated Use #linphone_core_set_log_level instead.
568
**/
569
void linphone_core_disable_logs(void){
aymeric's avatar
aymeric committed
570
	ortp_set_log_level_mask(ORTP_ERROR|ORTP_FATAL);
jehan's avatar
jehan committed
571
	sal_disable_logs();
aymeric's avatar
aymeric committed
572 573
}

574 575 576 577
void linphone_core_serialize_logs(void) {
	liblinphone_serialize_logs = TRUE;
}

aymeric's avatar
aymeric committed
578

579
static void net_config_read (LinphoneCore *lc)
aymeric's avatar
aymeric committed
580 581 582 583 584
{
	int tmp;
	const char *tmpstr;
	LpConfig *config=lc->config;

585
	lc->net_conf.nat_address_ip = NULL;
aymeric's avatar
aymeric committed
586 587 588 589 590 591 592 593 594 595
	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;
596
	tmp=lp_config_get_int(lc->config,"net","mtu",1300);
aymeric's avatar
aymeric committed
597
	linphone_core_set_mtu(lc,tmp);
598 599 600 601 602
	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);
	}
603 604
	tmp = lp_config_get_int(lc->config, "net", "dns_srv_enabled", 1);
	linphone_core_enable_dns_srv(lc, tmp);
605 606 607

	/* 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
608 609
}

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
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
626

627
static void sound_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
628
{
629
	int tmp;
aymeric's avatar
aymeric committed
630 631
	const char *tmpbuf;
	const char *devid;
632
#ifdef __linux
aymeric's avatar
aymeric committed
633 634 635
	/*alsadev let the user use custom alsa device within linphone*/
	devid=lp_config_get_string(lc->config,"sound","alsadev",NULL);
	if (devid){
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
		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
655
	}
Simon Morlat's avatar
Simon Morlat committed
656
	tmp=lp_config_get_int(lc->config,"sound","alsa_forced_rate",-1);
657
	if (tmp>0) ms_alsa_card_set_forced_sample_rate(tmp);
aymeric's avatar
aymeric committed
658 659
#endif
	/* retrieve all sound devices */
660 661
	build_sound_devices_table(lc);

aymeric's avatar
aymeric committed
662 663
	devid=lp_config_get_string(lc->config,"sound","playback_dev_id",NULL);
	linphone_core_set_playback_device(lc,devid);
664

aymeric's avatar
aymeric committed
665 666
	devid=lp_config_get_string(lc->config,"sound","ringer_dev_id",NULL);
	linphone_core_set_ringer_device(lc,devid);
667

aymeric's avatar
aymeric committed
668 669
	devid=lp_config_get_string(lc->config,"sound","capture_dev_id",NULL);
	linphone_core_set_capture_device(lc,devid);
670

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

aymeric's avatar
aymeric committed
682 683
	tmpbuf=PACKAGE_SOUND_DIR "/" LOCAL_RING;
	tmpbuf=lp_config_get_string(lc->config,"sound","local_ring",tmpbuf);
Jehan Monnier's avatar
Jehan Monnier committed
684
	if (ortp_file_exist(tmpbuf)==-1) {
685
		ms_warning("%s does not exist",tmpbuf);
aymeric's avatar
aymeric committed
686 687 688 689 690 691 692
		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);
693

694
	tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
aymeric's avatar
aymeric committed
695
	tmpbuf=lp_config_get_string(lc->config,"sound","remote_ring",tmpbuf);
Jehan Monnier's avatar
Jehan Monnier committed
696
	if (ortp_file_exist(tmpbuf)==-1){
697
		tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
aymeric's avatar
aymeric committed
698 699 700
	}
	if (strstr(tmpbuf,".wav")==NULL){
		/* it currently uses old sound files, so replace them */
701
		tmpbuf=PACKAGE_SOUND_DIR "/" REMOTE_RING;
aymeric's avatar
aymeric committed
702
	}
703
	linphone_core_set_ringback(lc,tmpbuf);
704 705

	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
706
	lc->sound_conf.latency=0;
707
#ifndef __ios
Simon Morlat's avatar
Simon Morlat committed
708
	tmp=TRUE;
709
#else
Simon Morlat's avatar
Simon Morlat committed
710
	tmp=FALSE; /* on iOS we have builtin echo cancellation.*/
711
#endif
Simon Morlat's avatar
Simon Morlat committed
712
	tmp=lp_config_get_int(lc->config,"sound","echocancellation",tmp);
713
	linphone_core_enable_echo_cancellation(lc,tmp);
smorlat's avatar
smorlat committed
714 715
	linphone_core_enable_echo_limiter(lc,
		lp_config_get_int(lc->config,"sound","echolimiter",0));
716 717
	linphone_core_enable_agc(lc,
		lp_config_get_int(lc->config,"sound","agc",0));
718

Yann Diorcet's avatar
Yann Diorcet committed
719 720
	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));
721 722

	linphone_core_set_remote_ringback_tone (lc,lp_config_get_string(lc->config,"sound","ringback_tone",NULL));
723 724 725

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

727
	_linphone_core_set_tone(lc,LinphoneReasonBusy,LinphoneToneBusy,NULL);
aymeric's avatar
aymeric committed
728 729
}

730 731
static void certificates_config_read(LinphoneCore *lc)
{
732
	const char *rootca;
733
#ifdef __linux
734
	rootca=lp_config_get_string(lc->config,"sip","root_ca", "/etc/ssl/certs");
735
#else
736
	rootca=lp_config_get_string(lc->config,"sip","root_ca", ROOT_CA_FILE);
737
#endif
738
	linphone_core_set_root_ca(lc,rootca);
739
	linphone_core_verify_server_certificates(lc,lp_config_get_int(lc->config,"sip","verify_server_certs",TRUE));
740
	linphone_core_verify_server_cn(lc,lp_config_get_int(lc->config,"sip","verify_server_cn",TRUE));
741 742
}

743
static void sip_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
744 745 746
{
	char *contact;
	const char *tmpstr;
747
	LCSipTransports tr;
aymeric's avatar
aymeric committed
748 749
	int i,tmp;
	int ipv6;
750 751 752 753

	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
754

755
	sal_use_no_initial_route(lc->sal,lp_config_get_int(lc->config,"sip","use_no_initial_route",0));
756 757
	sal_use_rport(lc->sal,lp_config_get_int(lc->config,"sip","use_rport",1));

aymeric's avatar
aymeric committed
758 759 760 761 762
	ipv6=lp_config_get_int(lc->config,"sip","use_ipv6",-1);
	if (ipv6==-1){
		ipv6=0;
	}
	linphone_core_enable_ipv6(lc,ipv6);
763
	memset(&tr,0,sizeof(tr));
764

765 766 767 768
	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);
769

770
	certificates_config_read(lc);
771 772
	/*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));
773
	/*start listening on ports*/
774
	linphone_core_set_sip_transports(lc,&tr);
775

aymeric's avatar
aymeric committed
776 777
	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
778 779
		const char *hostname=NULL;
		const char *username=NULL;
780
#ifdef HAVE_GETENV
smorlat's avatar
smorlat committed
781 782
		hostname=getenv("HOST");
		username=getenv("USER");
aymeric's avatar
aymeric committed
783
		if (hostname==NULL) hostname=getenv("HOSTNAME");
784
#endif /*HAVE_GETENV*/
aymeric's avatar
aymeric committed
785 786 787 788 789 790 791 792 793 794 795 796
		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);
797 798


799
	tmp=lp_config_get_int(lc->config,"sip","inc_timeout",30);
aymeric's avatar
aymeric committed
800 801
	linphone_core_set_inc_timeout(lc,tmp);

Yann Diorcet's avatar
Yann Diorcet committed
802 803
	tmp=lp_config_get_int(lc->config,"sip","in_call_timeout",0);
	linphone_core_set_in_call_timeout(lc,tmp);
804

805 806
	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
807

aymeric's avatar
aymeric committed
808 809
	/* get proxies config */
	for(i=0;; i++){
jehan's avatar
jehan committed
810
		LinphoneProxyConfig *cfg=linphone_proxy_config_new_from_config_file(lc,i);
aymeric's avatar
aymeric committed
811 812
		if (cfg!=NULL){
			linphone_core_add_proxy_config(lc,cfg);
Simon Morlat's avatar
Simon Morlat committed
813
			linphone_proxy_config_unref(cfg);
aymeric's avatar
aymeric committed
814 815 816 817 818 819 820
		}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);
821

aymeric's avatar
aymeric committed
822 823 824 825 826
	/* 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
827
			linphone_auth_info_destroy(ai);
aymeric's avatar
aymeric committed
828 829 830 831
		}else{
			break;
		}
	}
832 833
	/*this is to filter out unsupported encryption schemes*/
	linphone_core_set_media_encryption(lc,linphone_core_get_media_encryption(lc));
834

835
	/*for tuning or test*/
aymeric's avatar
aymeric committed
836
	lc->sip_conf.sdp_200_ack=lp_config_get_int(lc->config,"sip","sdp_200_ack",0);
837
	lc->sip_conf.register_only_when_network_is_up=
838
		lp_config_get_int(lc->config,"sip","register_only_when_network_is_up",1);
839 840
	lc->sip_conf.register_only_when_upnp_is_ok=
		lp_config_get_int(lc->config,"sip","register_only_when_upnp_is_ok",1);
841
	lc->sip_conf.ping_with_options=lp_config_get_int(lc->config,"sip","ping_with_options",0);
jehan's avatar
jehan committed
842
	lc->sip_conf.auto_net_state_mon=lp_config_get_int(lc->config,"sip","auto_net_state_mon",1);
843
	lc->sip_conf.keepalive_period=lp_config_get_int(lc->config,"sip","keepalive_period",10000);
844 845
	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
846
	sal_use_one_matching_codec_policy(lc->sal,lp_config_get_int(lc->config,"sip","only_one_codec",0));
847
	sal_use_dates(lc->sal,lp_config_get_int(lc->config,"sip","put_date",0));
848
	sal_enable_sip_update_method(lc->sal,lp_config_get_int(lc->config,"sip","sip_update",1));
849
	lc->sip_conf.vfu_with_info=lp_config_get_int(lc->config,"sip","vfu_with_info",1);
850
	linphone_core_set_sip_transport_timeout(lc, lp_config_get_int(lc->config, "sip", "transport_timeout", 63000));
851
	sal_set_supported_tags(lc->sal,lp_config_get_string(lc->config,"sip","supported","replaces, outbound"));
aymeric's avatar
aymeric committed
852 853
}

854
static void rtp_config_read(LinphoneCore *lc)
aymeric's avatar
aymeric committed
855
{
856
	int min_port, max_port;
aymeric's avatar
aymeric committed
857 858
	int jitt_comp;
	int nortp_timeout;
Simon Morlat's avatar
Simon Morlat committed
859
	bool_t rtp_no_xmit_on_audio_mute;
860
	bool_t adaptive_jitt_comp_enabled;
Simon Morlat's avatar
Simon Morlat committed
861

862 863 864 865 866 867 868 869
	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);
	}
870

871 872 873 874 875 876 877 878
	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);
	}
879

aymeric's avatar
aymeric committed
880
	jitt_comp=lp_config_get_int(lc->config,"rtp","audio_jitt_comp",60);
881
	linphone_core_set_audio_jittcomp(lc,jitt_comp);
aymeric's avatar
aymeric committed
882
	jitt_comp=lp_config_get_int(lc->config,"rtp","video_jitt_comp",60);
883
	if (jitt_comp==0) jitt_comp=60;
884
	linphone_core_set_video_jittcomp(lc,jitt_comp);
aymeric's avatar
aymeric committed
885
	nortp_timeout=lp_config_get_int(lc->config,"rtp","nortp_timeout",30);
886
	linphone_core_set_nortp_timeout(lc,nortp_timeout);
Simon Morlat's avatar
Simon Morlat committed
887
	rtp_no_xmit_on_audio_mute=lp_config_get_int(lc->config,"rtp","rtp_no_xmit_on_audio_mute",FALSE);
888
	linphone_core_set_rtp_no_xmit_on_audio_mute(lc,rtp_no_xmit_on_audio_mute);
889 890 891 892
	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);
893
	lc->rtp_conf.disable_upnp = lp_config_get_int(lc->config, "rtp", "disable_upnp", FALSE);
894
	linphone_core_set_avpf_mode(lc,lp_config_get_int(lc->config,"rtp","avpf",0));
aymeric's avatar
aymeric committed
895 896
}

897
static PayloadType * find_payload(RtpProfile *prof, const char *mime_type, int clock_rate, int channels, const char *recv_fmtp){
898 899 900
	PayloadType *candidate=NULL;
	int i;
	PayloadType *it;
901
	for(i=0;i<RTP_PROFILE_MAX_PAYLOADS;++i){
902 903
		it=rtp_profile_get_payload(prof,i);
		if (it!=NULL && strcasecmp(mime_type,it->mime_type)==0
904 905
			&& (clock_rate==it->clock_rate || clock_rate<=0)
			&& (channels==it->channels || channels<=0) ){
906
			if ( (recv_fmtp && it->recv_fmtp && strstr(recv_fmtp,it->recv_fmtp)!=NULL) ||
907 908
				(recv_fmtp==NULL && it->recv_fmtp==NULL) ){
				/*exact match*/
909
				if (recv_fmtp) payload_type_set_recv_fmtp(it,recv_fmtp);
910
				return it;
911 912 913 914 915
			}else {
				if (candidate){
					if (it->recv_fmtp==NULL) candidate=it;
				}else candidate=it;
			}
916 917
		}
	}
918 919 920
	if (candidate && recv_fmtp){
		payload_type_set_recv_fmtp(candidate,recv_fmtp);
	}
921 922
	return candidate;
}
aymeric's avatar
aymeric committed
923

Simon Morlat's avatar
Simon Morlat committed
924
static bool_t get_codec(LinphoneCore *lc, const char* type, int index, PayloadType **ret){
aymeric's avatar
aymeric committed
925 926
	char codeckey[50];
	const char *mime,*fmtp;
927
	int rate,channels,enabled;
aymeric's avatar
aymeric committed
928
	PayloadType *pt;
Simon Morlat's avatar
Simon Morlat committed
929
	LpConfig *config=lc->config;
930

931
	*ret=NULL;
aymeric's avatar
aymeric committed
932 933
	snprintf(codeckey,50,"%s_%i",type,index);
	mime=lp_config_get_string(config,codeckey,"mime",NULL);
934
	if (mime==NULL || strlen(mime)==0 ) return FALSE;
935

aymeric's avatar
aymeric committed
936 937
	rate=lp_config_get_int(config,codeckey,"rate",8000);
	fmtp=lp_config_get_string(config,codeckey,"recv_fmtp",NULL);
938
	channels=lp_config_get_int(config,codeckey,"channels",0);
aymeric's avatar
aymeric committed
939
	enabled=lp_config_get_int(config,codeckey,"enabled",1);
Simon Morlat's avatar
Simon Morlat committed
940
	pt=find_payload(lc->default_profile,mime,rate,channels,fmtp);
941
	if (pt && enabled ) pt->flags|=PAYLOAD_TYPE_ENABLED;
aymeric's avatar
aymeric committed
942
	//ms_message("Found codec %s/%i",pt->mime_type,pt->clock_rate);
943
	if (pt==NULL) ms_warning("Ignoring codec config %s/%i with fmtp=%s because unsupported",
944
				mime,rate,fmtp ? fmtp : "");
945 946 947 948
	*ret=pt;
	return TRUE;
}

949
#define RANK_END 10000
Simon Morlat's avatar
Simon Morlat committed
950 951 952 953 954 955 956

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
957
	{"opus", 48000},
Simon Morlat's avatar
Simon Morlat committed
958 959 960 961 962 963 964 965 966
	{"SILK", 16000},
	{"speex", 16000},
	{"speex", 8000},
	{"pcmu",8000},
	{"pcma",8000},
	{"VP8",90000},
	{"H264",90000},
	{"MP4V-ES",90000},
	{NULL,0}
967 968
};

Simon Morlat's avatar
Simon Morlat committed
969
static int find_codec_rank(const char *mime, int clock_rate){
970
	int i;
971 972 973 974 975 976 977

#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
978 979
	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)
980
			return i;
981
	}
982
	return RANK_END;
983 984 985 986
}

static int codec_compare(const PayloadType *a, const PayloadType *b){
	int ra,rb;
Simon Morlat's avatar
Simon Morlat committed
987 988
	ra=find_codec_rank(a->mime_type,a->clock_rate);
	rb=find_codec_rank(b->mime_type,b->clock_rate);
989 990
	if (ra>rb) return 1;
	if (ra<rb) return -1;
991
	return 1;
992 993
}

Simon Morlat's avatar
Simon Morlat committed
994
static MSList *add_missing_codecs(LinphoneCore *lc, SalStreamType mtype, MSList *l){
995
	int i;
996
	for(i=0;i<RTP_PROFILE_MAX_PAYLOADS;++i){
Simon Morlat's avatar
Simon Morlat committed
997
		PayloadType *pt=rtp_profile_get_payload(lc->default_profile,i);
998 999 1000
		if (pt){
			if (mtype==SalVideo && pt->type!=PAYLOAD_VIDEO)
				pt=NULL;
1001
			else if (mtype==SalAudio && (pt->type!=PAYLOAD_AUDIO_PACKETIZED
1002
				&& pt->type!=PAYLOAD_AUDIO_CONTINUOUS)){
1003 1004 1005 1006
				pt=NULL;
			}
			if (pt && ms_filter_codec_supported(pt->mime_type)){
				if (ms_list_find(l,pt)==NULL){
1007
					/*unranked codecs are disabled by default*/
Simon Morlat's avatar
Simon Morlat committed
1008
					if (find_codec_rank(pt->mime_type, pt->clock_rate)!=RANK_END){
1009 1010
						payload_type_set_flag(pt,PAYLOAD_TYPE_ENABLED);
					}
1011
					ms_message("Adding new codec %s/%i with fmtp %s",
1012
						pt->mime_type,pt->clock_rate,pt->recv_fmtp ? pt->recv_fmtp : "");
1013
					l=ms_list_insert_sorted(l,pt,(int (*)(const void *, const void *))codec_compare);