From 8eda172aab291fa92d308e19de5107816352c3aa Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthm@freeswitch.org>
Date: Mon, 12 Sep 2016 12:21:21 -0500
Subject: [PATCH] FS-9503 #resolve [Add flaws and consecutive_flaws to
 error_log in rtp]

---
 src/include/switch_types.h |  2 ++
 src/switch_ivr.c           | 10 ++++++++++
 src/switch_rtp.c           | 17 +++++++++++++++++
 3 files changed, 29 insertions(+)

diff --git a/src/include/switch_types.h b/src/include/switch_types.h
index 4f5d0d1b74..707ede43ac 100644
--- a/src/include/switch_types.h
+++ b/src/include/switch_types.h
@@ -621,6 +621,8 @@ typedef uint32_t switch_vad_flag_t;
 typedef struct error_period {
 	int64_t start;
 	int64_t stop;
+	uint32_t flaws;
+	uint32_t consecutive_flaws;
 	struct error_period *next;
 } switch_error_period_t;
 
diff --git a/src/switch_ivr.c b/src/switch_ivr.c
index 5f8b9ea2bc..41b55f4a1d 100644
--- a/src/switch_ivr.c
+++ b/src/switch_ivr.c
@@ -2645,6 +2645,14 @@ SWITCH_DECLARE(int) switch_ivr_set_xml_call_stats(switch_xml_t xml, switch_core_
 			x_tmp = switch_xml_add_child_d(x_err, "stop", 1);
 			switch_xml_set_txt_d(x_tmp, var_val);
 
+			switch_snprintf(var_val, sizeof(var_val), "%" SWITCH_TIME_T_FMT, ep->flaws);
+			x_tmp = switch_xml_add_child_d(x_err, "flaws", 1);
+			switch_xml_set_txt_d(x_tmp, var_val);
+
+			switch_snprintf(var_val, sizeof(var_val), "%" SWITCH_TIME_T_FMT, ep->consecutive_flaws);
+			x_tmp = switch_xml_add_child_d(x_err, "consecutive-flaws", 1);
+			switch_xml_set_txt_d(x_tmp, var_val);
+
 			switch_snprintf(var_val, sizeof(var_val), "%" SWITCH_TIME_T_FMT, (ep->stop - ep->start) / 1000);
 			x_tmp = switch_xml_add_child_d(x_err, "duration-msec", 2);
 			switch_xml_set_txt_d(x_tmp, var_val);
@@ -3127,6 +3135,8 @@ SWITCH_DECLARE(void) switch_ivr_set_json_call_stats(cJSON *json, switch_core_ses
 
 			cJSON_AddItemToObject(j_err, "start", cJSON_CreateNumber(ep->start));
 			cJSON_AddItemToObject(j_err, "stop", cJSON_CreateNumber(ep->stop));
+			cJSON_AddItemToObject(j_err, "flaws", cJSON_CreateNumber(ep->flaws));
+			cJSON_AddItemToObject(j_err, "Consecutiveflaws", cJSON_CreateNumber(ep->consecutive_flaws));
 			cJSON_AddItemToObject(j_err, "durationMS", cJSON_CreateNumber((ep->stop - ep->start) / 1000));
 			cJSON_AddItemToArray(j_err_log, j_err);
 		}
diff --git a/src/switch_rtp.c b/src/switch_rtp.c
index 0879ab63aa..a0575747fc 100644
--- a/src/switch_rtp.c
+++ b/src/switch_rtp.c
@@ -1544,6 +1544,11 @@ static void do_mos(switch_rtp_t *rtp_session, int force) {
 							  rtp_session->consecutive_flaws, penalty);
 			rtp_session->bad_stream++;
 			rtp_session->stats.inbound.flaws += penalty;
+			
+			if (rtp_session->stats.inbound.error_log) {
+				rtp_session->stats.inbound.error_log->flaws += penalty;
+				rtp_session->stats.inbound.error_log->consecutive_flaws++;
+			}
 		}
 
 		R = (int)((double)((double)(rtp_session->stats.inbound.recved - rtp_session->stats.inbound.flaws) / (double)rtp_session->stats.inbound.recved) * 100.0);
@@ -1643,6 +1648,11 @@ static void check_jitter(switch_rtp_t *rtp_session)
 
 		rtp_session->bad_stream++;
 		rtp_session->stats.inbound.flaws += lost;
+
+		if (rtp_session->stats.inbound.error_log) {
+			rtp_session->stats.inbound.error_log->flaws += lost;
+		}
+
 	} else {
 		rtp_session->stats.inbound.last_loss = 0;
 	}
@@ -6291,6 +6301,10 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
 
 						rtp_session->bad_stream++;
 						rtp_session->stats.inbound.flaws += rtp_session->sync_packets;
+
+						if (rtp_session->stats.inbound.error_log) {
+							rtp_session->stats.inbound.error_log->flaws += rtp_session->sync_packets;
+						}
 					}
 
 					switch_core_timer_sync(&rtp_session->timer);
@@ -6857,6 +6871,9 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
 					switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "%s %s timeout\n", 
 									  rtp_session_name(rtp_session), rtp_type(rtp_session));
 
+					if (rtp_session->stats.inbound.error_log) {
+						rtp_session->stats.inbound.error_log->flaws++;
+					}
 					rtp_session->stats.inbound.flaws++;
 					do_mos(rtp_session, SWITCH_FALSE);
 				}