summaryrefslogtreecommitdiff
path: root/src/mos.c
blob: 8f7851284d75d7158cd9525019919ac278099676 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
 * @file src/mos.c  MOS (Mean Opinion Score)
 *
 * Copyright (C) 2010 - 2016 Creytiv.com
 */
#include <re.h>
#include <baresip.h>
#include "core.h"


static double rfactor_to_mos(double r)
{
	double mos;

	mos = 1 + (0.035) * (r) + (0.000007) * (r) * ((r) - 60) * (100 - (r));

	if (mos > 5)
		mos = 5;

	return mos;
}


/**
 * Calculate Pseudo-MOS (Mean Opinion Score)
 *
 * @param r_factor          Pointer to where R-factor is written (optional)
 * @param rtt               Average roundtrip time
 * @param jitter            Jitter
 * @param num_packets_lost  Number of packets lost
 *
 * @return The calculated MOS value from 1 to 5
 *
 * Reference:  https://metacpan.org/pod/Algorithm::MOS
 */
double mos_calculate(double *r_factor, double rtt,
		     double jitter, u_int32_t num_packets_lost)
{
	double effective_latency = rtt + (jitter * 2) + 10;
	double mos_val;
	double r;

	if (effective_latency < 160) {
		r = 93.2 - (effective_latency / 40);
	}
	else {
		r = 93.2 - (effective_latency - 120) / 10;
	}

	r = r - (num_packets_lost * 2.5);

	if (r > 100)
		r = 100;
	else if (r < 0)
		r = 0;

	mos_val = rfactor_to_mos(r);

	if (r_factor)
		*r_factor = r;

	return mos_val;
}