quality_reporting.c 31.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
linphone
Copyright (C) 2014 - Belledonne Communications, Grenoble, France

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.
*/

#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

#include "linphonecore.h"
#include "private.h"
#include "sal/sal.h"
27 28
#include "ortp/rtpsession.h"

29 30
#include <math.h>

31 32
/***************************************************************************
 *  				TODO / REMINDER LIST
33
 ***************************************************************************
34
	For codecs that are able to change sample rates, the lowest and highest sample rates MUST be reported (e.g., 8000;16000).
35

36 37 38 39 40 41 42 43 44
	rlq value: need algo to compute it

	3.4 overload avoidance?
	   a.  Send only one report at the end of each call. (audio | video?)
	   b.  Use interval reports only on "problem" calls that are being closely monitored.

	- The Session report when
		codec change
		session fork
45 46

	if BYE and continue received packet drop them
47
 ***************************************************************************
48 49 50
 *  				END OF TODO / REMINDER LIST
 ****************************************************************************/

51
#define STR_REASSIGN(dest, src) {\
52 53 54 55
	if (dest != NULL) \
		ms_free(dest); \
	dest = src; \
}
56

57 58
/*since printf family functions are LOCALE dependent, float separator may differ
depending on the user's locale (LC_NUMERIC environment var).*/
59
static char * float_to_one_decimal_string(float f) {
60 61 62 63
	float rounded_f = floorf(f * 10 + .5f) / 10;

	int floor_part = (int) rounded_f;
	int one_decimal_part = floorf (10 * (rounded_f - floor_part) + .5f);
64

65
	return ms_strdup_printf("%d.%d", floor_part, one_decimal_part);
66 67
}

68 69 70 71
static void append_to_buffer_valist(char **buff, size_t *buff_size, size_t *offset, const char *fmt, va_list args) {
	belle_sip_error_code ret;
	size_t prevoffset = *offset;

72
	#ifndef WIN32
73 74
		va_list cap;/*copy of our argument list: a va_list cannot be re-used (SIGSEGV on linux 64 bits)*/
		va_copy(cap,args);
75
		ret = belle_sip_snprintf_valist(*buff, *buff_size, offset, fmt, cap);
76
		va_end(cap);
77
	#else
78
		ret = belle_sip_snprintf_valist(*buff, *buff_size, offset, fmt, args);
79
	#endif
80

81
	/*if we are out of memory, we add some size to buffer*/
82
	if (ret == BELLE_SIP_BUFFER_OVERFLOW) {
83
		/*some compilers complain that size_t cannot be formatted as unsigned long, hence forcing cast*/
84
		ms_warning("QualityReporting: Buffer was too small to contain the whole report - increasing its size from %lu to %lu",
85
			(unsigned long)*buff_size, (unsigned long)*buff_size + 2048);
86 87 88 89
		*buff_size += 2048;
		*buff = (char *) ms_realloc(*buff, *buff_size);

		*offset = prevoffset;
90
		/*recall itself since we did not write all things into the buffer but
91
		only a part of it*/
92 93 94 95 96 97 98 99 100 101 102
		append_to_buffer_valist(buff, buff_size, offset, fmt, args);
	}
}

static void append_to_buffer(char **buff, size_t *buff_size, size_t *offset, const char *fmt, ...) {
	va_list args;
	va_start(args, fmt);
	append_to_buffer_valist(buff, buff_size, offset, fmt, args);
	va_end(args);
}

103
static void reset_avg_metrics(reporting_session_report_t * report){
104
	int i;
105
	reporting_content_metrics_t * metrics[2] = {&report->local_metrics, &report->remote_metrics};
106 107 108 109 110 111 112

	for (i = 0; i < 2; i++) {
		metrics[i]->rtcp_xr_count = 0;
		metrics[i]->jitter_buffer.nominal = 0;
		metrics[i]->jitter_buffer.max = 0;

		metrics[i]->delay.round_trip_delay = 0;
113

114
		/*metrics[i]->delay.symm_one_way_delay = 0;*/
115
	}
116
	report->last_report_date = ms_time(NULL);
117
}
118

119 120 121 122
#define APPEND_IF_NOT_NULL_STR(buffer, size, offset, fmt, arg) if (arg != NULL) append_to_buffer(buffer, size, offset, fmt, arg)
#define APPEND_IF_NUM_IN_RANGE(buffer, size, offset, fmt, arg, inf, sup) if (inf <= arg && arg <= sup) append_to_buffer(buffer, size, offset, fmt, arg)
#define APPEND_IF(buffer, size, offset, fmt, arg, cond) if (cond) append_to_buffer(buffer, size, offset, fmt, arg)
#define IF_NUM_IN_RANGE(num, inf, sup, statement) if (inf <= num && num <= sup) statement
123

124 125 126 127 128 129
#define METRICS_PACKET_LOSS 1 << 0
#define METRICS_QUALITY_ESTIMATES 1 << 1
#define METRICS_SESSION_DESCRIPTION 1 << 2
#define METRICS_JITTER_BUFFER 1 << 3
#define METRICS_DELAY 1 << 4
#define METRICS_SIGNAL 1 << 5
130 131
#define METRICS_ADAPTIVE_ALGORITHM 1 << 6

132 133 134
static uint8_t are_metrics_filled(const reporting_content_metrics_t rm) {
	uint8_t ret = 0;

135 136
	IF_NUM_IN_RANGE(rm.packet_loss.network_packet_loss_rate, 0, 255, ret|=METRICS_PACKET_LOSS);
	IF_NUM_IN_RANGE(rm.packet_loss.jitter_buffer_discard_rate, 0, 255, ret|=METRICS_PACKET_LOSS);
137

138
	/*since these are same values than local ones, do not check them*/
139 140 141 142 143 144 145 146 147 148 149
	/*if (rm.session_description.payload_type != -1) ret|=METRICS_SESSION_DESCRIPTION;*/
	/*if (rm.session_description.payload_desc != NULL) ret|=METRICS_SESSION_DESCRIPTION;*/
	/*if (rm.session_description.sample_rate != -1) ret|=METRICS_SESSION_DESCRIPTION;*/
	/*if (rm.session_description.fmtp != NULL) ret|=METRICS_SESSION_DESCRIPTION;*/
	if (rm.session_description.frame_duration != -1) ret|=METRICS_SESSION_DESCRIPTION;
	if (rm.session_description.packet_loss_concealment != -1) ret|=METRICS_SESSION_DESCRIPTION;

	IF_NUM_IN_RANGE(rm.jitter_buffer.adaptive, 0, 3, ret|=METRICS_JITTER_BUFFER);
	IF_NUM_IN_RANGE(rm.jitter_buffer.abs_max, 0, 65535, ret|=METRICS_JITTER_BUFFER);

	IF_NUM_IN_RANGE(rm.delay.end_system_delay, 0, 65535, ret|=METRICS_DELAY);
150
	/*IF_NUM_IN_RANGE(rm.delay.symm_one_way_delay, 0, 65535, ret|=METRICS_DELAY);*/
151 152 153 154 155 156
	IF_NUM_IN_RANGE(rm.delay.interarrival_jitter, 0, 65535, ret|=METRICS_DELAY);
	IF_NUM_IN_RANGE(rm.delay.mean_abs_jitter, 0, 65535, ret|=METRICS_DELAY);

	if (rm.signal.level != 127) ret|=METRICS_SIGNAL;
	if (rm.signal.noise_level != 127) ret|=METRICS_SIGNAL;

157
	if (rm.qos_analyzer.input_leg!=NULL) ret|=METRICS_ADAPTIVE_ALGORITHM;
158
	if (rm.qos_analyzer.input!=NULL) ret|=METRICS_ADAPTIVE_ALGORITHM;
159
	if (rm.qos_analyzer.output_leg!=NULL) ret|=METRICS_ADAPTIVE_ALGORITHM;
160
	if (rm.qos_analyzer.output!=NULL) ret|=METRICS_ADAPTIVE_ALGORITHM;
161 162 163 164 165 166 167 168 169 170

	if (rm.rtcp_xr_count>0){
		IF_NUM_IN_RANGE(rm.jitter_buffer.nominal/rm.rtcp_xr_count, 0, 65535, ret|=METRICS_JITTER_BUFFER);
		IF_NUM_IN_RANGE(rm.jitter_buffer.max/rm.rtcp_xr_count, 0, 65535, ret|=METRICS_JITTER_BUFFER);
		IF_NUM_IN_RANGE(rm.delay.round_trip_delay, 0, 65535, ret|=METRICS_DELAY);
		IF_NUM_IN_RANGE(rm.quality_estimates.moslq/rm.rtcp_xr_count, 1, 5, ret|=METRICS_QUALITY_ESTIMATES);
		IF_NUM_IN_RANGE(rm.quality_estimates.moscq/rm.rtcp_xr_count, 1, 5, ret|=METRICS_QUALITY_ESTIMATES);
		IF_NUM_IN_RANGE(rm.quality_estimates.rlq/rm.rtcp_xr_count, 1, 120, ret|=METRICS_QUALITY_ESTIMATES);
		IF_NUM_IN_RANGE(rm.quality_estimates.rcq/rm.rtcp_xr_count, 1, 120, ret|=METRICS_QUALITY_ESTIMATES);
	}
171 172

	return ret;
173
}
174

175 176 177 178 179 180 181 182 183 184 185
static bool_t quality_reporting_enabled(const LinphoneCall * call) {
	return (call->dest_proxy != NULL && linphone_proxy_config_quality_reporting_enabled(call->dest_proxy));
}

static bool_t media_report_enabled(LinphoneCall * call, int stats_type){
	if (! quality_reporting_enabled(call))
		return FALSE;

	if (stats_type == LINPHONE_CALL_STATS_VIDEO && !linphone_call_params_video_enabled(linphone_call_get_current_params(call)))
		return FALSE;

186
	return (call->log->reporting.reports[stats_type] != NULL);
187 188
}

189
static void append_metrics_to_buffer(char ** buffer, size_t * size, size_t * offset, const reporting_content_metrics_t rm) {
190 191 192 193
	char * timestamps_start_str = NULL;
	char * timestamps_stop_str = NULL;
	char * network_packet_loss_rate_str = NULL;
	char * jitter_buffer_discard_rate_str = NULL;
194
	/*char * gap_loss_density_str = NULL;*/
195 196
	char * moslq_str = NULL;
	char * moscq_str = NULL;
197
	uint8_t available_metrics = are_metrics_filled(rm);
198

199
	if (rm.timestamps.start > 0)
200
		timestamps_start_str = linphone_timestamp_to_rfc3339_string(rm.timestamps.start);
201
	if (rm.timestamps.stop > 0)
202 203 204 205 206 207
		timestamps_stop_str = linphone_timestamp_to_rfc3339_string(rm.timestamps.stop);

	append_to_buffer(buffer, size, offset, "Timestamps:");
		APPEND_IF_NOT_NULL_STR(buffer, size, offset, " START=%s", timestamps_start_str);
		APPEND_IF_NOT_NULL_STR(buffer, size, offset, " STOP=%s", timestamps_stop_str);

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	if ((available_metrics & METRICS_SESSION_DESCRIPTION) != 0){
		append_to_buffer(buffer, size, offset, "\r\nSessionDesc:");
			APPEND_IF(buffer, size, offset, " PT=%d", rm.session_description.payload_type, rm.session_description.payload_type != -1);
			APPEND_IF_NOT_NULL_STR(buffer, size, offset, " PD=%s", rm.session_description.payload_desc);
			APPEND_IF(buffer, size, offset, " SR=%d", rm.session_description.sample_rate, rm.session_description.sample_rate != -1);
			APPEND_IF(buffer, size, offset, " FD=%d", rm.session_description.frame_duration, rm.session_description.frame_duration != -1);
			/*append_to_buffer(buffer, size, offset, " FO=%d", rm.session_description.frame_ocets);*/
			/*append_to_buffer(buffer, size, offset, " FPP=%d", rm.session_description.frames_per_sec);*/
			/*append_to_buffer(buffer, size, offset, " PPS=%d", rm.session_description.packets_per_sec);*/
			APPEND_IF_NOT_NULL_STR(buffer, size, offset, " FMTP=\"%s\"", rm.session_description.fmtp);
			APPEND_IF(buffer, size, offset, " PLC=%d", rm.session_description.packet_loss_concealment, rm.session_description.packet_loss_concealment != -1);
			/*APPEND_IF_NOT_NULL_STR(buffer, size, offset, " SSUP=%s", rm.session_description.silence_suppression_state);*/
	}

	if ((available_metrics & METRICS_JITTER_BUFFER) != 0){
		append_to_buffer(buffer, size, offset, "\r\nJitterBuffer:");
			APPEND_IF_NUM_IN_RANGE(buffer, size, offset, " JBA=%d", rm.jitter_buffer.adaptive, 0, 3);
			/*APPEND_IF_NUM_IN_RANGE(buffer, size, offset, " JBR=%d", rm.jitter_buffer.rate, 0, 15);*/
226 227 228 229
			if (rm.rtcp_xr_count){
				APPEND_IF_NUM_IN_RANGE(buffer, size, offset, " JBN=%d", rm.jitter_buffer.nominal/rm.rtcp_xr_count, 0, 65535);
				APPEND_IF_NUM_IN_RANGE(buffer, size, offset, " JBM=%d", rm.jitter_buffer.max/rm.rtcp_xr_count, 0, 65535);
			}
230 231 232
			APPEND_IF_NUM_IN_RANGE(buffer, size, offset, " JBX=%d",  rm.jitter_buffer.abs_max, 0, 65535);

		append_to_buffer(buffer, size, offset, "\r\nPacketLoss:");
233 234 235
			IF_NUM_IN_RANGE(rm.packet_loss.network_packet_loss_rate, 0, 255, network_packet_loss_rate_str = float_to_one_decimal_string(rm.packet_loss.network_packet_loss_rate / 256));
			IF_NUM_IN_RANGE(rm.packet_loss.jitter_buffer_discard_rate, 0, 255, jitter_buffer_discard_rate_str = float_to_one_decimal_string(rm.packet_loss.jitter_buffer_discard_rate / 256));

236 237 238 239 240
			APPEND_IF_NOT_NULL_STR(buffer, size, offset, " NLR=%s", network_packet_loss_rate_str);
			APPEND_IF_NOT_NULL_STR(buffer, size, offset, " JDR=%s", jitter_buffer_discard_rate_str);
	}

		/*append_to_buffer(buffer, size, offset, "\r\nBurstGapLoss:");*/
241
			/*IF_NUM_IN_RANGE(rm.burst_gap_loss.gap_loss_density, 0, 10, gap_loss_density_str = float_to_one_decimal_string(rm.burst_gap_loss.gap_loss_density));*/
242 243 244 245 246 247 248 249
		/*	append_to_buffer(buffer, size, offset, " BLD=%d", rm.burst_gap_loss.burst_loss_density);*/
		/*	append_to_buffer(buffer, size, offset, " BD=%d", rm.burst_gap_loss.burst_duration);*/
		/*	APPEND_IF_NOT_NULL_STR(buffer, size, offset, " GLD=%s", gap_loss_density_str);*/
		/*	append_to_buffer(buffer, size, offset, " GD=%d", rm.burst_gap_loss.gap_duration);*/
		/*	append_to_buffer(buffer, size, offset, " GMIN=%d", rm.burst_gap_loss.min_gap_threshold);*/

	if ((available_metrics & METRICS_DELAY) != 0){
		append_to_buffer(buffer, size, offset, "\r\nDelay:");
250 251 252
			if (rm.rtcp_xr_count){
				APPEND_IF_NUM_IN_RANGE(buffer, size, offset, " RTD=%d", rm.delay.round_trip_delay/rm.rtcp_xr_count, 0, 65535);
			}
253 254
			APPEND_IF_NUM_IN_RANGE(buffer, size, offset, " ESD=%d", rm.delay.end_system_delay, 0, 65535);
			/*APPEND_IF_NUM_IN_RANGE(buffer, size, offset, " OWD=%d", rm.delay.one_way_delay, 0, 65535);*/
255
			/*APPEND_IF_NUM_IN_RANGE(buffer, size, offset, " SOWD=%d", rm.delay.symm_one_way_delay, 0, 65535);*/
256 257 258 259 260 261 262 263 264 265 266
			APPEND_IF_NUM_IN_RANGE(buffer, size, offset, " IAJ=%d", rm.delay.interarrival_jitter, 0, 65535);
			APPEND_IF_NUM_IN_RANGE(buffer, size, offset, " MAJ=%d", rm.delay.mean_abs_jitter, 0, 65535);
	}

	if ((available_metrics & METRICS_SIGNAL) != 0){
		append_to_buffer(buffer, size, offset, "\r\nSignal:");
			APPEND_IF(buffer, size, offset, " SL=%d", rm.signal.level, rm.signal.level != 127);
			APPEND_IF(buffer, size, offset, " NL=%d", rm.signal.noise_level, rm.signal.noise_level != 127);
			/*append_to_buffer(buffer, size, offset, " RERL=%d", rm.signal.residual_echo_return_loss);*/
	}

267
	/*if quality estimates metrics are available, rtcp_xr_count should be always not null*/
268
	if ((available_metrics & METRICS_QUALITY_ESTIMATES) != 0){
269 270
		IF_NUM_IN_RANGE(rm.quality_estimates.moslq/rm.rtcp_xr_count, 1, 5, moslq_str = float_to_one_decimal_string(rm.quality_estimates.moslq/rm.rtcp_xr_count));
		IF_NUM_IN_RANGE(rm.quality_estimates.moscq/rm.rtcp_xr_count, 1, 5, moscq_str = float_to_one_decimal_string(rm.quality_estimates.moscq/rm.rtcp_xr_count));
271

272
		append_to_buffer(buffer, size, offset, "\r\nQualityEst:");
273
			APPEND_IF_NUM_IN_RANGE(buffer, size, offset, " RLQ=%d", rm.quality_estimates.rlq/rm.rtcp_xr_count, 1, 120);
274
			/*APPEND_IF_NOT_NULL_STR(buffer, size, offset, " RLQEstAlg=%s", rm.quality_estimates.rlqestalg);*/
275
			APPEND_IF_NUM_IN_RANGE(buffer, size, offset, " RCQ=%d", rm.quality_estimates.rcq/rm.rtcp_xr_count, 1, 120);
276 277 278 279 280 281 282 283 284 285 286 287
			/*APPEND_IF_NOT_NULL_STR(buffer, size, offset, " RCQEstAlgo=%s", rm.quality_estimates.rcqestalg);*/
			/*append_to_buffer(buffer, size, offset, " EXTRI=%d", rm.quality_estimates.extri);*/
			/*APPEND_IF_NOT_NULL_STR(buffer, size, offset, " ExtRIEstAlg=%s", rm.quality_estimates.extriestalg);*/
			/*append_to_buffer(buffer, size, offset, " EXTRO=%d", rm.quality_estimates.extro);*/
			/*APPEND_IF_NOT_NULL_STR(buffer, size, offset, " ExtROEstAlg=%s", rm.quality_estimates.extroutestalg);*/
			APPEND_IF_NOT_NULL_STR(buffer, size, offset, " MOSLQ=%s", moslq_str);
			/*APPEND_IF_NOT_NULL_STR(buffer, size, offset, " MOSLQEstAlgo=%s", rm.quality_estimates.moslqestalg);*/
			APPEND_IF_NOT_NULL_STR(buffer, size, offset, " MOSCQ=%s", moscq_str);
			/*APPEND_IF_NOT_NULL_STR(buffer, size, offset, " MOSCQEstAlgo=%s", rm.quality_estimates.moscqestalg);*/
			/*APPEND_IF_NOT_NULL_STR(buffer, size, offset, " QoEEstAlg=%s", rm.quality_estimates.qoestalg);*/
	}

288 289
	if ((available_metrics & METRICS_ADAPTIVE_ALGORITHM) != 0){
		append_to_buffer(buffer, size, offset, "\r\nAdaptiveAlg:");
290
			APPEND_IF_NOT_NULL_STR(buffer, size, offset, " IN_LEG=%s", rm.qos_analyzer.input_leg);
291
			APPEND_IF_NOT_NULL_STR(buffer, size, offset, " IN=%s", rm.qos_analyzer.input);
292
			APPEND_IF_NOT_NULL_STR(buffer, size, offset, " OUT_LEG=%s", rm.qos_analyzer.output_leg);
293
			APPEND_IF_NOT_NULL_STR(buffer, size, offset, " OUT=%s", rm.qos_analyzer.output);
294 295
	}

296 297
	append_to_buffer(buffer, size, offset, "\r\n");

298 299 300 301
	ms_free(timestamps_start_str);
	ms_free(timestamps_stop_str);
	ms_free(network_packet_loss_rate_str);
	ms_free(jitter_buffer_discard_rate_str);
302
	/*ms_free(gap_loss_density_str);*/
303 304
	ms_free(moslq_str);
	ms_free(moscq_str);
305 306
}

307
static int send_report(const LinphoneCall* call, reporting_session_report_t * report, const char * report_event) {
308
	LinphoneContent content = {0};
309
	LinphoneAddress *addr;
310
	int expires = -1;
311 312 313
	size_t offset = 0;
	size_t size = 2048;
	char * buffer;
314
	int ret = 0;
315

316 317
	/*if the call was hung up too early, we might have invalid IPs information
	in that case, we abort the report since it's not useful data*/
318 319
	if (report->info.local_addr.ip == NULL || strlen(report->info.local_addr.ip) == 0
		|| report->info.remote_addr.ip == NULL || strlen(report->info.remote_addr.ip) == 0) {
320
		ms_warning("QualityReporting[%p]: Trying to submit a %s too early (call duration: %d sec) but %s IP could "
321
			"not be retrieved so dropping this report"
322
			, report
323
			, report_event
324 325
			, linphone_call_get_duration(call)
			, (report->info.local_addr.ip == NULL || strlen(report->info.local_addr.ip) == 0) ? "local" : "remote");
326
		return 1;
327 328
	}

329
	addr = linphone_address_new(linphone_proxy_config_get_quality_reporting_collector(call->dest_proxy));
330
	if (addr == NULL) {
331 332
		ms_warning("QualityReporting[%p]: Asked to submit reporting statistics but no collector address found"
			, report);
333
		return 2;
334 335
	}

336
	buffer = (char *) ms_malloc(size);
337 338 339
	content.type = ms_strdup("application");
	content.subtype = ms_strdup("vq-rtcpxr");

340
	append_to_buffer(&buffer, &size, &offset, "%s\r\n", report_event);
341 342 343 344 345 346 347 348 349 350 351
	append_to_buffer(&buffer, &size, &offset, "CallID: %s\r\n", report->info.call_id);
	append_to_buffer(&buffer, &size, &offset, "LocalID: %s\r\n", report->info.local_id);
	append_to_buffer(&buffer, &size, &offset, "RemoteID: %s\r\n", report->info.remote_id);
	append_to_buffer(&buffer, &size, &offset, "OrigID: %s\r\n", report->info.orig_id);

	APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "LocalGroup: %s\r\n", report->info.local_group);
	APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "RemoteGroup: %s\r\n", report->info.remote_group);
	append_to_buffer(&buffer, &size, &offset, "LocalAddr: IP=%s PORT=%d SSRC=%d\r\n", report->info.local_addr.ip, report->info.local_addr.port, report->info.local_addr.ssrc);
	APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "LocalMAC: %s\r\n", report->info.local_mac_addr);
	append_to_buffer(&buffer, &size, &offset, "RemoteAddr: IP=%s PORT=%d SSRC=%d\r\n", report->info.remote_addr.ip, report->info.remote_addr.port, report->info.remote_addr.ssrc);
	APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "RemoteMAC: %s\r\n", report->info.remote_mac_addr);
352

353 354
	append_to_buffer(&buffer, &size, &offset, "LocalMetrics:\r\n");
	append_metrics_to_buffer(&buffer, &size, &offset, report->local_metrics);
355

356
	if (are_metrics_filled(report->remote_metrics)!=0) {
357 358 359
		append_to_buffer(&buffer, &size, &offset, "RemoteMetrics:\r\n");
		append_metrics_to_buffer(&buffer, &size, &offset, report->remote_metrics);
	}
360 361 362
	APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "DialogID: %s\r\n", report->dialog_id);

	content.data = buffer;
363
	content.size = strlen(buffer);
364

365
	/*(WIP) Memory leak: PUBLISH message is never freed (issue 1283)*/
366 367 368
	if (! linphone_core_publish(call->core, addr, "vq-rtcpxr", expires, &content)){
		ret=3;
	}
369
	linphone_address_destroy(addr);
370

371
	reset_avg_metrics(report);
372
	linphone_content_uninit(&content);
373
	return ret;
374 375
}

376 377 378
static const SalStreamDescription * get_media_stream_for_desc(const SalMediaDescription * smd, SalStreamType sal_stream_type) {
	int count;
	if (smd != NULL) {
379
		for (count = 0; count < smd->nb_streams; ++count) {
380 381
			if (smd->streams[count].type == sal_stream_type) {
				return &smd->streams[count];
382 383
			}
		}
384
	}
385 386
	return NULL;
}
387

388
static void update_ip(LinphoneCall * call, int stats_type) {
389
	SalStreamType sal_stream_type = (stats_type == LINPHONE_CALL_STATS_AUDIO) ? SalAudio : SalVideo;
390 391
	const SalStreamDescription * local_desc = get_media_stream_for_desc(call->localdesc, sal_stream_type);
	const SalStreamDescription * remote_desc = get_media_stream_for_desc(sal_call_get_remote_media_description(call->op), sal_stream_type);
392

393 394 395 396
	if (local_desc != NULL) {
		/*since this function might be called for video stream AFTER it has been uninitialized, local description might
		be invalid. In any other case, IP/port should be always filled and valid*/
		if (local_desc->rtp_addr != NULL && strlen(local_desc->rtp_addr) > 0) {
397 398
			call->log->reporting.reports[stats_type]->info.local_addr.port = local_desc->rtp_port;
			STR_REASSIGN(call->log->reporting.reports[stats_type]->info.local_addr.ip, ms_strdup(local_desc->rtp_addr));
399
		}
400
	}
401

402 403 404
	if (remote_desc != NULL) {
		/*port is always stored in stream description struct*/
		call->log->reporting.reports[stats_type]->info.remote_addr.port = remote_desc->rtp_port;
405

406 407 408 409 410
		/*for IP it can be not set if we are using a direct route*/
		if (remote_desc->rtp_addr != NULL && strlen(remote_desc->rtp_addr) > 0) {
			STR_REASSIGN(call->log->reporting.reports[stats_type]->info.remote_addr.ip, ms_strdup(remote_desc->rtp_addr));
		} else {
			STR_REASSIGN(call->log->reporting.reports[stats_type]->info.remote_addr.ip, ms_strdup(sal_call_get_remote_media_description(call->op)->addr));
411
		}
412
	}
413 414
}

415
static void qos_analyzer_on_action_suggested(void *user_data, int datac, const char** data){
416
	reporting_content_metrics_t *metrics = (reporting_content_metrics_t*) user_data;
417
	char * appendbuf;
418

419 420 421 422 423 424 425
	STR_REASSIGN(metrics->qos_analyzer.input_leg, ms_strdup(data[0]));
	appendbuf=ms_strdup_printf("%s%s;", metrics->qos_analyzer.input?metrics->qos_analyzer.input:"", data[1]);
	STR_REASSIGN(metrics->qos_analyzer.input,appendbuf);

	STR_REASSIGN(metrics->qos_analyzer.output_leg, ms_strdup(data[2]));
	appendbuf=ms_strdup_printf("%s%s;", metrics->qos_analyzer.output?metrics->qos_analyzer.output:"", data[3]);
	STR_REASSIGN(metrics->qos_analyzer.output, appendbuf);
426 427
}

428
void linphone_reporting_update_ip(LinphoneCall * call) {
429
	update_ip(call, LINPHONE_CALL_STATS_AUDIO);
430
	update_ip(call, LINPHONE_CALL_STATS_VIDEO);
431
}
432

433
void linphone_reporting_update_media_info(LinphoneCall * call, int stats_type) {
434 435 436
	MediaStream * stream = NULL;
	const PayloadType * local_payload = NULL;
	const PayloadType * remote_payload = NULL;
437
	const LinphoneCallParams * current_params = linphone_call_get_current_params(call);
438
	reporting_session_report_t * report = call->log->reporting.reports[stats_type];
439

440
	if (!media_report_enabled(call, stats_type))
441 442
		return;

443

444
	STR_REASSIGN(report->info.call_id, ms_strdup(call->log->call_id));
445
	STR_REASSIGN(report->info.local_group, ms_strdup_printf("linphone-%s-%s-%s", (stats_type == LINPHONE_CALL_STATS_AUDIO ? "audio" : "video"),
446
		linphone_core_get_user_agent_name(), report->info.call_id));
447
	STR_REASSIGN(report->info.remote_group, ms_strdup_printf("linphone-%s-%s-%s", (stats_type == LINPHONE_CALL_STATS_AUDIO ? "audio" : "video"),
448
		linphone_call_get_remote_user_agent(call), report->info.call_id));
449

450
	if (call->dir == LinphoneCallIncoming) {
451 452 453
		STR_REASSIGN(report->info.remote_id, linphone_address_as_string(call->log->from));
		STR_REASSIGN(report->info.local_id, linphone_address_as_string(call->log->to));
		STR_REASSIGN(report->info.orig_id, ms_strdup(report->info.remote_id));
454
	} else {
455 456 457
		STR_REASSIGN(report->info.remote_id, linphone_address_as_string(call->log->to));
		STR_REASSIGN(report->info.local_id, linphone_address_as_string(call->log->from));
		STR_REASSIGN(report->info.orig_id, ms_strdup(report->info.local_id));
458
	}
459

460
	STR_REASSIGN(report->dialog_id, sal_op_get_dialog_id(call->op));
461

462 463
	report->local_metrics.timestamps.start = call->log->start_date_time;
	report->local_metrics.timestamps.stop = call->log->start_date_time + linphone_call_get_duration(call);
464

465
	/*we use same timestamps for remote too*/
466 467
	report->remote_metrics.timestamps.start = call->log->start_date_time;
	report->remote_metrics.timestamps.stop = call->log->start_date_time + linphone_call_get_duration(call);
468

469
	/*yet we use the same payload config for local and remote, since this is the largest use case*/
470 471
	if (stats_type == LINPHONE_CALL_STATS_AUDIO && call->audiostream != NULL) {
		stream = &call->audiostream->ms;
472 473
		local_payload = linphone_call_params_get_used_audio_codec(current_params);
		remote_payload = local_payload;
474 475
	} else if (stats_type == LINPHONE_CALL_STATS_VIDEO && call->videostream != NULL) {
		stream = &call->videostream->ms;
476 477
		local_payload = linphone_call_params_get_used_video_codec(current_params);
		remote_payload = local_payload;
478
	}
479

480 481
	if (stream != NULL) {
		RtpSession * session = stream->sessions.rtp_session;
482

483 484 485
		report->info.local_addr.ssrc = rtp_session_get_send_ssrc(session);
		report->info.remote_addr.ssrc = rtp_session_get_recv_ssrc(session);
	}
486

487 488
	if (local_payload != NULL) {
		report->local_metrics.session_description.payload_type = local_payload->type;
489
		STR_REASSIGN(report->local_metrics.session_description.payload_desc, ms_strdup(local_payload->mime_type));
490
		report->local_metrics.session_description.sample_rate = local_payload->clock_rate;
491
		STR_REASSIGN(report->local_metrics.session_description.fmtp, ms_strdup(local_payload->recv_fmtp));
492
	}
493

494 495
	if (remote_payload != NULL) {
		report->remote_metrics.session_description.payload_type = remote_payload->type;
496
		STR_REASSIGN(report->remote_metrics.session_description.payload_desc, ms_strdup(remote_payload->mime_type));
497
		report->remote_metrics.session_description.sample_rate = remote_payload->clock_rate;
498
		STR_REASSIGN(report->remote_metrics.session_description.fmtp, ms_strdup(remote_payload->recv_fmtp));
499
	}
500 501
}

502
void linphone_reporting_on_rtcp_received(LinphoneCall *call, int stats_type) {
503
	reporting_session_report_t * report = call->log->reporting.reports[stats_type];
504 505
	reporting_content_metrics_t * metrics = NULL;
	LinphoneCallStats stats = call->stats[stats_type];
506
	mblk_t *block = NULL;
507

508 509
	int report_interval = linphone_proxy_config_get_quality_reporting_interval(call->dest_proxy);

510
	if (! media_report_enabled(call,stats_type))
511 512
		return;

513
	if (stats.updated == LINPHONE_CALL_STATS_RECEIVED_RTCP_UPDATE) {
514
		metrics = &report->remote_metrics;
515
		block = stats.received_rtcp;
516
	} else if (stats.updated == LINPHONE_CALL_STATS_SENT_RTCP_UPDATE) {
517
		metrics = &report->local_metrics;
518
		block = stats.sent_rtcp;
519
	}
520

521 522
	do{
		if (rtcp_is_XR(block) && (rtcp_XR_get_block_type(block) == RTCP_XR_VOIP_METRICS)){
523

524
			uint8_t config = rtcp_XR_voip_metrics_get_rx_config(block);
525

526
			metrics->rtcp_xr_count++;
527

528 529 530
			metrics->quality_estimates.rcq += rtcp_XR_voip_metrics_get_r_factor(block);
			metrics->quality_estimates.moslq += rtcp_XR_voip_metrics_get_mos_lq(block) / 10.f;
			metrics->quality_estimates.moscq += rtcp_XR_voip_metrics_get_mos_cq(block) / 10.f;
531

532 533 534 535 536 537
			metrics->jitter_buffer.nominal += rtcp_XR_voip_metrics_get_jb_nominal(block);
			metrics->jitter_buffer.max += rtcp_XR_voip_metrics_get_jb_maximum(block);
			metrics->jitter_buffer.abs_max = rtcp_XR_voip_metrics_get_jb_abs_max(block);
			metrics->jitter_buffer.adaptive = (config >> 4) & 0x3;
			metrics->packet_loss.network_packet_loss_rate = rtcp_XR_voip_metrics_get_loss_rate(block);
			metrics->packet_loss.jitter_buffer_discard_rate = rtcp_XR_voip_metrics_get_discard_rate(block);
538

539 540 541
			metrics->session_description.packet_loss_concealment = (config >> 6) & 0x3;

			metrics->delay.round_trip_delay += rtcp_XR_voip_metrics_get_round_trip_delay(block);
542
		}
543
	}while(rtcp_next_packet(block));
544 545

	/* check if we should send an interval report */
546
	if (report_interval>0 && ms_time(NULL)-report->last_report_date>report_interval){
547 548 549 550
		linphone_reporting_publish_interval_report(call);
	}
}

551 552
static int publish_report(LinphoneCall *call, const char *event_type){
	int ret = 0;
553 554 555
	int i;
	for (i = 0; i < 2; i++){
		if (media_report_enabled(call, i)){
556
			int sndret;
557
			linphone_reporting_update_media_info(call, i);
558 559 560 561 562 563
			sndret=send_report(call, call->log->reporting.reports[i], event_type);
			if (sndret>0){
				ret += 10+(i+1)*sndret;
			}
		} else{
			ret += i+1;
564
		}
565
	}
566
	return ret;
567
}
568 569 570 571

int linphone_reporting_publish_session_report(LinphoneCall* call, bool_t call_term) {
	char * session_type = call_term?"VQSessionReport: CallTerm":"VQSessionReport";
	return publish_report(call, session_type);
572
}
573

574 575
int linphone_reporting_publish_interval_report(LinphoneCall* call) {
	return publish_report(call, "VQIntervalReport");
576 577 578 579 580 581
}

reporting_session_report_t * linphone_reporting_new() {
	int i;
	reporting_session_report_t * rm = ms_new0(reporting_session_report_t,1);
	reporting_content_metrics_t * metrics[2] = {&rm->local_metrics, &rm->remote_metrics};
582 583 584

	memset(rm, 0, sizeof(reporting_session_report_t));

585 586 587 588 589 590 591 592 593 594 595
	for (i = 0; i < 2; i++) {
		metrics[i]->session_description.payload_type = -1;
		metrics[i]->session_description.sample_rate = -1;
		metrics[i]->session_description.frame_duration = -1;

		metrics[i]->packet_loss.network_packet_loss_rate = -1;
		metrics[i]->packet_loss.jitter_buffer_discard_rate = -1;

		metrics[i]->session_description.packet_loss_concealment = -1;

		metrics[i]->jitter_buffer.adaptive = -1;
596
		/*metrics[i]->jitter_buffer.rate = -1;*/
597 598 599
		metrics[i]->jitter_buffer.abs_max = -1;

		metrics[i]->delay.end_system_delay = -1;
600
		/*metrics[i]->delay.one_way_delay = -1;*/
601 602 603 604 605
		metrics[i]->delay.interarrival_jitter = -1;
		metrics[i]->delay.mean_abs_jitter = -1;

		metrics[i]->signal.level = 127;
		metrics[i]->signal.noise_level = 127;
606
	}
607 608

	reset_avg_metrics(rm);
609
	return rm;
610
}
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

void linphone_reporting_destroy(reporting_session_report_t * report) {
	if (report->info.call_id != NULL) ms_free(report->info.call_id);
	if (report->info.local_id != NULL) ms_free(report->info.local_id);
	if (report->info.remote_id != NULL) ms_free(report->info.remote_id);
	if (report->info.orig_id != NULL) ms_free(report->info.orig_id);
	if (report->info.local_addr.ip != NULL) ms_free(report->info.local_addr.ip);
	if (report->info.remote_addr.ip != NULL) ms_free(report->info.remote_addr.ip);
	if (report->info.local_group != NULL) ms_free(report->info.local_group);
	if (report->info.remote_group != NULL) ms_free(report->info.remote_group);
	if (report->info.local_mac_addr != NULL) ms_free(report->info.local_mac_addr);
	if (report->info.remote_mac_addr != NULL) ms_free(report->info.remote_mac_addr);
	if (report->dialog_id != NULL) ms_free(report->dialog_id);
	if (report->local_metrics.session_description.fmtp != NULL) ms_free(report->local_metrics.session_description.fmtp);
	if (report->local_metrics.session_description.payload_desc != NULL) ms_free(report->local_metrics.session_description.payload_desc);
	if (report->remote_metrics.session_description.fmtp != NULL) ms_free(report->remote_metrics.session_description.fmtp);
	if (report->remote_metrics.session_description.payload_desc != NULL) ms_free(report->remote_metrics.session_description.payload_desc);
628
	if (report->local_metrics.qos_analyzer.input_leg != NULL) ms_free(report->local_metrics.qos_analyzer.input_leg);
629
	if (report->local_metrics.qos_analyzer.input != NULL) ms_free(report->local_metrics.qos_analyzer.input);
630
	if (report->local_metrics.qos_analyzer.output_leg != NULL) ms_free(report->local_metrics.qos_analyzer.output_leg);
631
	if (report->local_metrics.qos_analyzer.output != NULL) ms_free(report->local_metrics.qos_analyzer.output);
632 633 634

	ms_free(report);
}
635 636 637 638 639

void linphone_reporting_call_state_updated(LinphoneCall *call){
	LinphoneCallState state=linphone_call_get_state(call);
	bool_t enabled=media_report_enabled(call, LINPHONE_CALL_STATS_VIDEO);
	switch (state){
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
		case LinphoneCallConnected:{
			int i;
			MediaStream *streams[2] = {(MediaStream*) call->audiostream, (MediaStream *) call->videostream};
			MSQosAnalyzer *analyzer;
			for (i=0;i<2;i++){
				if (streams[i]==NULL)
					continue;

				analyzer=ms_bitrate_controller_get_qos_analyzer(streams[i]->rc);
				if (analyzer){
					ms_qos_analyzer_set_on_action_suggested(analyzer,
						qos_analyzer_on_action_suggested,
						&call->log->reporting.reports[i]->local_metrics);
				}
			}
			break;
		}
657
		case LinphoneCallStreamsRunning:
658 659 660 661 662
			linphone_reporting_update_ip(call);
			if (!enabled && call->log->reporting.was_video_running){
				ms_message("Send midterm report with status %d",
					send_report(call, call->log->reporting.reports[LINPHONE_CALL_STATS_VIDEO], "VQSessionReport")
				);
663 664 665 666 667 668 669 670 671 672 673 674 675 676
			}
			call->log->reporting.was_video_running=enabled;
			break;
		case LinphoneCallEnd:
			if (call->log->status==LinphoneCallSuccess || call->log->status==LinphoneCallAborted){
				ms_message("Send report with status %d",
					linphone_reporting_publish_session_report(call, TRUE)
				);
			}
			break;
		default:
			break;
	}
}