Skip to content

Commit

Permalink
ticket VS-1412
Browse files Browse the repository at this point in the history
redesign management of mos calculation
 - fix calculation interval
 - modify calculation by time instead of counter
  • Loading branch information
rbucek committed Jan 24, 2023
1 parent d7a7b3a commit 27e140c
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 28 deletions.
2 changes: 1 addition & 1 deletion calltable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ Call::closeRawFiles() {
// close GRAPH files
if(opt_saveGRAPH || (flags & FLAG_SAVEGRAPH)) {
if(rtp_i->graph.isOpen()) {
if(!rtp_i->mos_processed or (rtp_i->last_mos_time + 1 < rtp_i->_last_ts.tv_sec)) {
if(!rtp_i->mos_processed or (rtp_i->last_save_mos_graph_ms + 1000 < TIME_US_TO_MS(rtp_i->last_packet_time_us))) {
rtp_i->save_mos_graph(true);
}
rtp_i->graph.close();
Expand Down
1 change: 1 addition & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

struct sVerbose {
int graph;
int graph_mos;
int process_rtp;
int read_rtp;
int hash_rtp;
Expand Down
3 changes: 2 additions & 1 deletion jitterbuffer/abstract_jb.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ extern struct sVerbose sverb;
/*! \brief On and Off plc*/
extern int opt_disableplc ;
extern int opt_fix_packetization_in_create_audio;
extern int opt_ignore_mos_degradation_for_contiguous_packet_loss_greater_than;

/*! Internal jb flags */
enum {
Expand Down Expand Up @@ -603,7 +604,7 @@ static void jb_get_and_deliver(struct ast_channel *chan, struct timeval *mynow)
* and also if there was dtmf last time
* */
if( !(((f->seqno - chan->last_seqno) == 1) && (abs(f->ts - chan->last_ms) > (chan->packetization)))
&& !f->marker && chan->last_loss_burst > 0 && chan->last_loss_burst < 1024
&& !f->marker && chan->last_loss_burst > 0 && chan->last_loss_burst < opt_ignore_mos_degradation_for_contiguous_packet_loss_greater_than
&& f->lastframetype == AST_FRAME_VOICE // if the lastframetype was no frame voice(for example dtmf), do not count packet loss
//&& !(chan->codec == PAYLOAD_AMR && f->datalen2 <= 7) // if AMR frame is VAD frame do not count interpolated frames
// && !(chan->codec == PAYLOAD_G729 && f->datalen2 <= 12) // if g729 frame is CNG frame do not count interpolated frames
Expand Down
88 changes: 64 additions & 24 deletions rtp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ RTP::RTP(int sensor_id, vmIP sensor_ip)
mosf2_avg = 0;
mosAD_avg = 0;
mosSilence_avg = 0;
mos_counter = 0;
mos_time_ms = 0;
resetgraph = false;
jitter = 0;
last_stat_lost = 0;
Expand Down Expand Up @@ -334,9 +334,9 @@ RTP::RTP(int sensor_id, vmIP sensor_ip)
channel_record->rtp_stream = this;
#endif

last_mos_time = 0;
last_save_mos_graph_ms = 0;
mos_processed = false;
save_mos_graph_wait = false;
save_mos_graph_wait = 0;

last_voice_frame_ts.tv_sec = 0;
last_voice_frame_ts.tv_usec = 0;
Expand Down Expand Up @@ -452,6 +452,8 @@ RTP::save_mos_graph(bool delimiter) {
this->graph.write((char*)&graph_mos, 4);
}

u_int64_t new_mos_time_ms = TIME_US_TO_MS(last_packet_time_us - first_packet_time_us);
u_int64_t diff_mos_time_ms = mos_time_ms ? new_mos_time_ms - mos_time_ms : 0;
#if not EXPERIMENTAL_SUPPRESS_AST_CHANNELS
if(opt_jitterbuffer_f1 and channel_fix1) {
last_interval_mosf1 = calculate_mos_fromrtp(this, 1, 1);
Expand All @@ -464,8 +466,18 @@ RTP::save_mos_graph(bool delimiter) {
if(mosf1_min > last_interval_mosf1) {
mosf1_min = last_interval_mosf1;
}
mosf1_avg = ((mosf1_avg * mos_counter) + last_interval_mosf1) / (mos_counter + 1);
// if(sverb.graph) printf("rtp[%p] saddr[%s] ts[%u] ssrc[%x] mosf1_avg[%f] mosf1[%u]\n", this, saddr.getString().c_str(), header->ts.tv_sec, ssrc, mosf1_avg, last_interval_mosf1);
if(mos_time_ms && new_mos_time_ms) {
mosf1_avg = ((mosf1_avg * mos_time_ms) + (last_interval_mosf1 * diff_mos_time_ms)) / new_mos_time_ms;
} else {
mosf1_avg = last_interval_mosf1;
}
if(sverb.graph_mos) {
cout << "MOS F1"
<< " saddr: " << saddr.getString().c_str() << " ssrc: " << hex << ssrc << dec
<< " act_time: " << (double)new_mos_time_ms / 1000 << "s " << " old_time: " << (double)mos_time_ms / 1000 << "s" << " diff_time: " << (double)diff_mos_time_ms / 1000 << "s"
<< " last_mos: " << (int)last_interval_mosf1 << " avg_mos: " << mosf1_avg
<< endl;
}
} else
#endif
{
Expand All @@ -488,8 +500,18 @@ RTP::save_mos_graph(bool delimiter) {
if(mosf2_min > last_interval_mosf2) {
mosf2_min = last_interval_mosf2;
}
mosf2_avg = ((mosf2_avg * mos_counter) + last_interval_mosf2) / (mos_counter + 1);
// if(sverb.graph) printf("rtp[%p] saddr[%s] ts[%u] ssrc[%x] mosf2_avg[%f] mosf2[%u]\n", this, saddr.getString().c_str(), header->ts.tv_sec, ssrc, mosf2_avg, last_interval_mosf2);
if(mos_time_ms && new_mos_time_ms) {
mosf2_avg = ((mosf2_avg * mos_time_ms) + (last_interval_mosf2 * diff_mos_time_ms)) / new_mos_time_ms;
} else {
mosf2_avg = last_interval_mosf2;
}
if(sverb.graph_mos) {
cout << "MOS F2"
<< " saddr: " << saddr.getString().c_str() << " ssrc: " << hex << ssrc << dec
<< " act_time: " << (double)new_mos_time_ms / 1000 << "s " << " old_time: " << (double)mos_time_ms / 1000 << "s" << " diff_time: " << (double)diff_mos_time_ms / 1000 << "s"
<< " last_mos: " << (int)last_interval_mosf2 << " avg_mos: " << mosf2_avg
<< endl;
}
} else
#endif
{
Expand All @@ -512,8 +534,18 @@ RTP::save_mos_graph(bool delimiter) {
if(mosAD_min > last_interval_mosAD) {
mosAD_min = last_interval_mosAD;
}
mosAD_avg = ((mosAD_avg * mos_counter) + last_interval_mosAD) / (mos_counter + 1);
// if(sverb.graph) printf("rtp[%p] saddr[%s] ts[%u] ssrc[%x] mosAD_avg[%f] mosAD[%u]\n", this, saddr.getString().c_str(), header->ts.tv_sec, ssrc, mosAD_avg, last_interval_mosAD);
if(mos_time_ms && new_mos_time_ms) {
mosAD_avg = ((mosAD_avg * mos_time_ms) + (last_interval_mosAD * diff_mos_time_ms)) / new_mos_time_ms;
} else {
mosAD_avg = last_interval_mosAD;
}
if(sverb.graph_mos) {
cout << "MOS AD"
<< " saddr: " << saddr.getString().c_str() << " ssrc: " << hex << ssrc << dec
<< " act_time: " << (double)new_mos_time_ms / 1000 << "s " << " old_time: " << (double)mos_time_ms / 1000 << "s" << " diff_time: " << (double)diff_mos_time_ms / 1000 << "s"
<< " last_mos: " << (int)last_interval_mosAD << " avg_mos: " << mosAD_avg
<< endl;
}
} else
#endif
{
Expand All @@ -538,8 +570,18 @@ RTP::save_mos_graph(bool delimiter) {
mosSilence_min = last_interval_mosSilence;
//printf("[%p] min[%u] %p DSP[%p]\n", this, mosSilence_min, &mosSilence_min, DSP);
}
mosSilence_avg = ((mosSilence_avg * mos_counter) + last_interval_mosSilence) / (mos_counter + 1);
// if(sverb.graph) printf("rtp[%p] saddr[%s] ts[%u] ssrc[%x] mosSilence_avg[%f] mosSilence[%u]\n", this, saddr.getString().c_str(), header->ts.tv_sec, ssrc, mosSilence_avg, last_interval_mosSilence);
if(mos_time_ms && new_mos_time_ms) {
mosSilence_avg = ((mosSilence_avg * mos_time_ms) + (last_interval_mosSilence * diff_mos_time_ms)) / new_mos_time_ms;
} else {
mosSilence_avg = last_interval_mosSilence;
}
if(sverb.graph_mos) {
cout << "MOS SL"
<< " saddr: " << saddr.getString().c_str() << " ssrc: " << hex << ssrc << dec
<< " act_time: " << (double)new_mos_time_ms / 1000 << "s " << " old_time: " << (double)mos_time_ms / 1000 << "s" << " diff_time: " << (double)diff_mos_time_ms / 1000 << "s"
<< " last_mos: " << (int)last_interval_mosSilence << " avg_mos: " << mosSilence_avg
<< endl;
}
} else {
last_interval_mosSilence = 45;
mosSilence_min = 45;
Expand All @@ -561,7 +603,8 @@ RTP::save_mos_graph(bool delimiter) {
this->graph.write((char*)&graph_delimiter, 4);
}
}
mos_counter++;

mos_time_ms = new_mos_time_ms;

if(sverb.graph) {
printf("rtp[%p] saddr[%s] ssrc[%x] time[%u] seq[%u] \nMOS F1 cur[%d] min[%d] avg[%f]\nMOS F2 cur[%d] min[%d] avg[%f]\nMOS AD cur[%d] min[%d] avg[%f]\n ------\n",
Expand Down Expand Up @@ -1063,9 +1106,6 @@ RTP::read(unsigned char* data, iphdr2 *header_ip, unsigned *len, struct pcap_pkt
had_audio = true;
}

if(last_mos_time == 0) {
last_mos_time = header->ts.tv_sec;
}
if(sverb.ssrc and getSSRC() != sverb.ssrc) return(false);

#if not EXPERIMENTAL_SUPPRESS_AST_CHANNELS
Expand Down Expand Up @@ -2333,21 +2373,21 @@ RTP::read(unsigned char* data, iphdr2 *header_ip, unsigned *len, struct pcap_pkt
avg_ptime = (avg_ptime * (avg_ptime_count - 1) + packetization) / avg_ptime_count;

// write MOS to .graph every 10 seconds and reset jitter last mos interval
if((last_mos_time + 10 < header->ts.tv_sec) or save_mos_graph_wait) {
if(!last_save_mos_graph_ms) {
last_save_mos_graph_ms = TIME_US_TO_MS(last_packet_time_us);
} else if(last_save_mos_graph_ms + 10000 < TIME_US_TO_MS(last_packet_time_us) || save_mos_graph_wait > 0) {
mos_processed = true;
if(save_mos_graph_wait > 1) {
save_mos_graph_wait--;
if(!save_mos_graph_wait) {
save_mos_graph_wait = 20; // wait 10 packets
last_save_mos_graph_ms = TIME_US_TO_MS(last_packet_time_us);
} else {
if(!save_mos_graph_wait and ((header->ts.tv_sec - last_mos_time) > 10)) {
//wait few frames - there was loss generated so the jitter can settle
save_mos_graph_wait = 20; // wait 10 packets
} else {
save_mos_graph_wait = false;
save_mos_graph_wait--;
if(!save_mos_graph_wait) {
save_mos_graph(false);
last_mos_time = header->ts.tv_sec;
}
}
}

resetgraph = false;

if(forcemark_by_owner_set) {
Expand Down
4 changes: 2 additions & 2 deletions rtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ class RTP {
char ignore;
uint8_t dscp;
bool skip;
unsigned int last_mos_time;
u_int64_t last_save_mos_graph_ms;
uint8_t mosf1_min;
uint8_t mosf2_min;
uint8_t mosAD_min;
Expand All @@ -405,7 +405,7 @@ class RTP {
float mosf2_avg;
float mosAD_avg;
float mosSilence_avg;
uint32_t mos_counter;
uint64_t mos_time_ms;
char save_mos_graph_wait;
timeval _last_ts;
timeval last_voice_frame_ts;
Expand Down
1 change: 1 addition & 0 deletions tools_global.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ __inline__ unsigned long long rdtsc(void)
#define TIME_S_TO_US(s) ((u_int64_t)((s) * 1000000ull))
#define TIME_US_TO_S(us) ((u_int32_t)((us) / 1000000ull))
#define TIME_US_TO_S_signed(us) ((int32_t)((us) / 1000000ll))
#define TIME_US_TO_MS(us) ((u_int64_t)((us) / 1000ull))
#define TIME_US_TO_SF(us) ((double)((us) / 1000000.))
#define TIME_US_TO_DEC_MS(us) ((u_int32_t)((us) % 1000000ull / 1000ull))
#define TIME_US_TO_DEC_US(us) ((u_int32_t)((us) % 1000000ull))
Expand Down
6 changes: 6 additions & 0 deletions voipmonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ bool opt_database_backup_check_src_tables = false;
char opt_mos_lqo_bin[1024] = "pesq";
char opt_mos_lqo_ref[1024] = "/usr/local/share/voipmonitor/audio/mos_lqe_original.wav";
char opt_mos_lqo_ref16[1024] = "/usr/local/share/voipmonitor/audio/mos_lqe_original_16khz.wav";
int opt_ignore_mos_degradation_for_contiguous_packet_loss_greater_than = 5000;
regcache *regfailedcache;
int opt_onewaytimeout = 15;
int opt_bye_timeout = 20 * 60;
Expand Down Expand Up @@ -7778,6 +7779,7 @@ void cConfig::addConfigItems() {
addConfigItem(new FILE_LINE(42332) cConfigItem_string("mos_lqo_bin", opt_mos_lqo_bin, sizeof(opt_mos_lqo_bin)));
addConfigItem(new FILE_LINE(42333) cConfigItem_string("mos_lqo_ref", opt_mos_lqo_ref, sizeof(opt_mos_lqo_ref)));
addConfigItem(new FILE_LINE(42334) cConfigItem_string("mos_lqo_ref16", opt_mos_lqo_ref16, sizeof(opt_mos_lqo_ref16)));
addConfigItem(new FILE_LINE(0) cConfigItem_integer("ignore_mos_degradation_for_contiguous_packet_loss_greater_than", &opt_ignore_mos_degradation_for_contiguous_packet_loss_greater_than));
subgroup("FAX");
addConfigItem(new FILE_LINE(42335) cConfigItem_yesno("faxdetect", &opt_faxt30detect));
subgroup("jitterbufer");
Expand Down Expand Up @@ -8522,6 +8524,7 @@ void parse_command_line_arguments(int argc, char *argv[]) {
void parse_verb_param(string verbParam) {
if(verbParam == "process_rtp") sverb.process_rtp = 1;
else if(verbParam == "graph") sverb.graph = 1;
else if(verbParam == "graph_mos") sverb.graph_mos = 1;
else if(verbParam == "read_rtp") sverb.read_rtp = 1;
else if(verbParam == "hash_rtp") sverb.hash_rtp = 1;
else if(verbParam == "rtp_set_base_seq") sverb.rtp_set_base_seq = 1;
Expand Down Expand Up @@ -12381,6 +12384,9 @@ int eval_config(string inistr) {
if((value = ini.GetValue("general", "mos_lqo_ref16", NULL))) {
strcpy_null_term(opt_mos_lqo_ref16, value);
}
if((value = ini.GetValue("general", "ignore_mos_degradation_for_contiguous_packet_loss_greater_than", NULL))) {
opt_ignore_mos_degradation_for_contiguous_packet_loss_greater_than = atoi(value);
}
if((value = ini.GetValue("general", "php_path", NULL))) {
strcpy_null_term(opt_php_path, value);
}
Expand Down

0 comments on commit 27e140c

Please sign in to comment.