[Asterisk-code-review] res_rtp: Add unit tests for RTCP stats. (...asterisk[16])

George Joseph asteriskteam at digium.com
Fri Sep 13 07:05:10 CDT 2019


George Joseph has submitted this change and it was merged. ( https://gerrit.asterisk.org/c/asterisk/+/12813 )

Change subject: res_rtp: Add unit tests for RTCP stats.
......................................................................

res_rtp: Add unit tests for RTCP stats.

Added unit tests for RTCP video stats. These tests include NACK, REMB,
FIR/FUR/PLI, SR/RR/SDES, and packet loss statistics. The REMB and FIR
tests are currently disabled due to a bug. We expect to receive a
compound packet, but the code sends this out as a single packet, which
the browser accepts, but makes Asterisk upset.

While writing these tests, I noticed an issue with NACK as well. Where
it is handling a received NACK request, it was reading in only the first
8 bits of following packets that were also lost. This has been changed
to the correct value of 16 bits.

Also made a minor fix to the data buffer unit test.

Change-Id: I56107c7411003a247589bbb6086d25c54719901b
---
M include/asterisk/rtp_engine.h
M main/rtp_engine.c
M res/res_rtp_asterisk.c
M tests/test_data_buffer.c
A tests/test_res_rtp.c
5 files changed, 831 insertions(+), 6 deletions(-)

Approvals:
  Kevin Harwell: Looks good to me, but someone else must approve
  Joshua C. Colp: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved; Approved for Submit



diff --git a/include/asterisk/rtp_engine.h b/include/asterisk/rtp_engine.h
index 206ed63..57f29b4 100644
--- a/include/asterisk/rtp_engine.h
+++ b/include/asterisk/rtp_engine.h
@@ -589,6 +589,26 @@
 	const char *(*get_fingerprint)(struct ast_rtp_instance *instance);
 };
 
+#ifdef TEST_FRAMEWORK
+/*! \brief Structure that represents the test functionality for res_rtp_asterisk unit tests */
+struct ast_rtp_engine_test {
+	/*! Drops RTP packets while this has a value greater than 0 */
+	int packets_to_drop;
+	/*! Sends a SR/RR instead of RTP the next time RTP would be sent */
+	int send_report;
+	/*! Set to 1 whenever SDES is received */
+	int sdes_received;
+	/*! Get the number of packets in the receive buffer for a RTP instance */
+	size_t (*recv_buffer_count)(struct ast_rtp_instance *instance);
+	/*! Get the maximum number of packets the receive buffer can hold for a RTP instance */
+	size_t (*recv_buffer_max)(struct ast_rtp_instance *instance);
+	/*! Get the number of packets in the send buffer for a RTP instance */
+	size_t (*send_buffer_count)(struct ast_rtp_instance *instance);
+	/*! Set the schedid for RTCP */
+	void (*set_schedid)(struct ast_rtp_instance *instance, int id);
+};
+#endif
+
 /*! Structure that represents an RTP stack (engine) */
 struct ast_rtp_engine {
 	/*! Name of the RTP engine, used when explicitly requested */
@@ -670,6 +690,10 @@
 	struct ast_rtp_engine_ice *ice;
 	/*! Callback to pointer for optional DTLS SRTP support */
 	struct ast_rtp_engine_dtls *dtls;
+#ifdef TEST_FRAMEWORK
+	/*! Callback to pointer for test callbacks for RTP/RTCP unit tests */
+	struct ast_rtp_engine_test *test;
+#endif
 	/*! Callback to enable an RTP extension (returns non-zero if supported) */
 	int (*extension_enable)(struct ast_rtp_instance *instance, enum ast_rtp_extension extension);
 	/*! Linked list information */
@@ -2513,6 +2537,18 @@
  */
 struct ast_rtp_engine_ice *ast_rtp_instance_get_ice(struct ast_rtp_instance *instance);
 
+#ifdef TEST_FRAMEWORK
+/*!
+ * \brief Obtain a pointer to the test callbacks on an RTP instance
+ *
+ * \param instance the RTP instance
+ *
+ * \retval test callbacks if present
+ * \retval NULL if not present
+ */
+struct ast_rtp_engine_test *ast_rtp_instance_get_test(struct ast_rtp_instance *instance);
+#endif
+
 /*!
  * \brief Obtain a pointer to the DTLS support present on an RTP instance
  *
@@ -2686,6 +2722,70 @@
  */
 struct stasis_message_type *ast_rtp_rtcp_received_type(void);
 
+#ifdef TEST_FRAMEWORK
+/*!
+ * \brief Get the maximum size of the receive buffer
+ *
+ * \param instance The RTP instance
+ * \retval The recv_buffer max size if it exists, else 0
+ */
+size_t ast_rtp_instance_get_recv_buffer_max(struct ast_rtp_instance *instance);
+
+/*!
+ * \brief Get the current size of the receive buffer
+ *
+ * \param instance The RTP instance
+ * \retval The recv_buffer size if it exists, else 0
+ */
+size_t ast_rtp_instance_get_recv_buffer_count(struct ast_rtp_instance *instance);
+
+/*!
+ * \brief Get the current size of the send buffer
+ *
+ * \param instance The RTP instance
+ * \retval The send_buffer size if it exists, else 0
+ */
+size_t ast_rtp_instance_get_send_buffer_count(struct ast_rtp_instance *instance);
+
+/*!
+ * \brief Set the schedid for RTCP
+ *
+ * \param instance The RTP instance
+ * \param id The number to set schedid to
+ */
+void ast_rtp_instance_set_schedid(struct ast_rtp_instance *instance, int id);
+
+/*!
+ * \brief Set the number of packets to drop on RTP read
+ *
+ * \param instance The RTP instance
+ * \param num The number of packets to drop
+ */
+void ast_rtp_instance_drop_packets(struct ast_rtp_instance *instance, int num);
+
+/*!
+ * \brief Sends a SR/RR report the next time RTP would be sent
+ *
+ * \param instance The RTP instance
+ */
+void ast_rtp_instance_queue_report(struct ast_rtp_instance *instance);
+
+/*!
+ * \brief Get the value of sdes_received on the test engine
+ *
+ * \param instance The RTP instance
+ * \retval 1 if sdes_received, else 0
+ */
+int ast_rtp_instance_get_sdes_received(struct ast_rtp_instance *instance);
+
+/*!
+ * \brief Resets all the fields to default values for the test engine
+ *
+ * \param instance The RTP instance
+ */
+void ast_rtp_instance_reset_test_engine(struct ast_rtp_instance *instance);
+#endif
+
 /*!
  * \brief Convert given stat instance into json format
  * \param stats
diff --git a/main/rtp_engine.c b/main/rtp_engine.c
index 39ad1b3..3403d70 100644
--- a/main/rtp_engine.c
+++ b/main/rtp_engine.c
@@ -457,7 +457,7 @@
 
 int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
 {
-	ao2_ref(instance, -1);
+	ao2_cleanup(instance);
 
 	return 0;
 }
@@ -2897,6 +2897,13 @@
 	return NULL;
 }
 
+#ifdef TEST_FRAMEWORK
+struct ast_rtp_engine_test *ast_rtp_instance_get_test(struct ast_rtp_instance *instance)
+{
+	return instance->engine->test;
+}
+#endif
+
 static int rtp_dtls_wrap_set_configuration(struct ast_rtp_instance *instance,
 	const struct ast_rtp_dtls_cfg *dtls_cfg)
 {
@@ -3759,6 +3766,123 @@
 	ao2_unlock(rtp);
 }
 
+#ifdef TEST_FRAMEWORK
+size_t ast_rtp_instance_get_recv_buffer_max(struct ast_rtp_instance *instance)
+{
+	size_t res;
+	struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
+
+	if (!test) {
+		ast_log(LOG_ERROR, "There is no test engine set up!\n");
+		return 0;
+	}
+
+	ao2_lock(instance);
+	res = test->recv_buffer_max(instance);
+	ao2_unlock(instance);
+
+	return res;
+}
+
+size_t ast_rtp_instance_get_recv_buffer_count(struct ast_rtp_instance *instance)
+{
+	size_t res;
+	struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
+
+	if (!test) {
+		ast_log(LOG_ERROR, "There is no test engine set up!\n");
+		return 0;
+	}
+
+	ao2_lock(instance);
+	res = test->recv_buffer_count(instance);
+	ao2_unlock(instance);
+
+	return res;
+}
+
+size_t ast_rtp_instance_get_send_buffer_count(struct ast_rtp_instance *instance)
+{
+	size_t res;
+	struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
+
+	if (!test) {
+		ast_log(LOG_ERROR, "There is no test engine set up!\n");
+		return 0;
+	}
+
+	ao2_lock(instance);
+	res = test->send_buffer_count(instance);
+	ao2_unlock(instance);
+
+	return res;
+}
+
+void ast_rtp_instance_set_schedid(struct ast_rtp_instance *instance, int id)
+{
+	struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
+
+	if (!test) {
+		ast_log(LOG_ERROR, "There is no test engine set up!\n");
+		return;
+	}
+
+	ao2_lock(instance);
+	test->set_schedid(instance, id);
+	ao2_unlock(instance);
+}
+
+void ast_rtp_instance_drop_packets(struct ast_rtp_instance *instance, int num)
+{
+	struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
+
+	if (!test) {
+		ast_log(LOG_ERROR, "There is no test engine set up!\n");
+		return;
+	}
+
+	test->packets_to_drop = num;
+}
+
+void ast_rtp_instance_queue_report(struct ast_rtp_instance *instance)
+{
+	struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
+
+	if (!test) {
+		ast_log(LOG_ERROR, "There is no test engine set up!\n");
+		return;
+	}
+
+	test->send_report = 1;
+}
+
+int ast_rtp_instance_get_sdes_received(struct ast_rtp_instance *instance)
+{
+	struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
+
+	if (!test) {
+		ast_log(LOG_ERROR, "There is no test engine set up!\n");
+		return 0;
+	}
+
+	return test->sdes_received;
+}
+
+void ast_rtp_instance_reset_test_engine(struct ast_rtp_instance *instance)
+{
+	struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
+
+	if (!test) {
+		ast_log(LOG_ERROR, "There is no test engine set up!\n");
+		return;
+	}
+
+	test->packets_to_drop = 0;
+	test->send_report = 0;
+	test->sdes_received = 0;
+}
+#endif
+
 struct ast_json *ast_rtp_convert_stats_json(const struct ast_rtp_instance_stats *stats)
 {
 	struct ast_json *j_res;
diff --git a/res/res_rtp_asterisk.c b/res/res_rtp_asterisk.c
index 08138cb..5da095e 100644
--- a/res/res_rtp_asterisk.c
+++ b/res/res_rtp_asterisk.c
@@ -2371,6 +2371,60 @@
 
 #endif
 
+#ifdef TEST_FRAMEWORK
+static size_t get_recv_buffer_count(struct ast_rtp_instance *instance)
+{
+	struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
+
+	if (rtp && rtp->recv_buffer) {
+		return ast_data_buffer_count(rtp->recv_buffer);
+	}
+
+	return 0;
+}
+
+static size_t get_recv_buffer_max(struct ast_rtp_instance *instance)
+{
+	struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
+
+	if (rtp && rtp->recv_buffer) {
+		return ast_data_buffer_max(rtp->recv_buffer);
+	}
+
+	return 0;
+}
+
+static size_t get_send_buffer_count(struct ast_rtp_instance *instance)
+{
+	struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
+
+	if (rtp && rtp->send_buffer) {
+		return ast_data_buffer_count(rtp->send_buffer);
+	}
+
+	return 0;
+}
+
+static void set_rtp_rtcp_schedid(struct ast_rtp_instance *instance, int id)
+{
+	struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
+
+	if (rtp && rtp->rtcp) {
+		rtp->rtcp->schedid = id;
+	}
+}
+
+static struct ast_rtp_engine_test ast_rtp_test = {
+	.packets_to_drop = 0,
+	.send_report = 0,
+	.sdes_received = 0,
+	.recv_buffer_count = get_recv_buffer_count,
+	.recv_buffer_max = get_recv_buffer_max,
+	.send_buffer_count = get_send_buffer_count,
+	.set_schedid = set_rtp_rtcp_schedid,
+};
+#endif
+
 /* RTP Engine Declaration */
 static struct ast_rtp_engine asterisk_rtp_engine = {
 	.name = "asterisk",
@@ -2410,6 +2464,9 @@
 	.set_stream_num = ast_rtp_set_stream_num,
 	.extension_enable = ast_rtp_extension_enable,
 	.bundle = ast_rtp_bundle,
+#ifdef TEST_FRAMEWORK
+	.test = &ast_rtp_test,
+#endif
 };
 
 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
@@ -2923,11 +2980,21 @@
 #ifdef HAVE_PJPROJECT
 	struct ast_sockaddr *loop = rtcp ? &rtp->rtcp_loop : &rtp->rtp_loop;
 #endif
+#ifdef TEST_FRAMEWORK
+	struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
+#endif
 
 	if ((len = ast_recvfrom(rtcp ? rtp->rtcp->s : rtp->s, buf, size, flags, sa)) < 0) {
-	   return len;
+		return len;
 	}
 
+#ifdef TEST_FRAMEWORK
+	if (test && test->packets_to_drop > 0) {
+		test->packets_to_drop--;
+		return 0;
+	}
+#endif
+
 #if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
 	/* If this is an SSL packet pass it to OpenSSL for processing. RFC section for first byte value:
 	 * https://tools.ietf.org/html/rfc5764#section-5.1.2 */
@@ -4595,6 +4662,9 @@
 	struct ast_sockaddr remote_address = { {0,} };
 	int rate = rtp_get_rate(frame->subclass.format) / 1000;
 	unsigned int seqno;
+#ifdef TEST_FRAMEWORK
+	struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
+#endif
 
 	if (ast_format_cmp(frame->subclass.format, ast_format_g722) == AST_FORMAT_CMP_EQUAL) {
 		frame->samples /= 2;
@@ -4604,6 +4674,14 @@
 		return 0;
 	}
 
+#ifdef TEST_FRAMEWORK
+	if (test && test->send_report) {
+		test->send_report = 0;
+		ast_rtcp_write(instance);
+		return 0;
+	}
+#endif
+
 	if (frame->frametype == AST_FRAME_VOICE) {
 		pred = rtp->lastts + frame->samples;
 
@@ -5641,7 +5719,7 @@
 		 * packet (pid+i)(modulo 2^16). Otherwise, it is set to 0. We cannot assume bits set
 		 * to 0 after a bit set to 1 have actually been received.
 		 */
-		blp = current_word & 0xFF;
+		blp = current_word & 0xffff;
 		blp_index = 1;
 		while (blp) {
 			if (blp & 1) {
@@ -5721,6 +5799,9 @@
 	unsigned int ssrc_seen;
 	struct ast_rtp_rtcp_report_block *report_block;
 	struct ast_frame *f = &ast_null_frame;
+#ifdef TEST_FRAMEWORK
+	struct ast_rtp_engine_test *test_engine;
+#endif
 
 	/* If this is encrypted then decrypt the payload */
 	if ((*rtcpheader & 0xC0) && res_srtp && srtp && res_srtp->unprotect(
@@ -6161,6 +6242,11 @@
 				ast_verbose("Received an SDES from %s\n",
 					ast_sockaddr_stringify(addr));
 			}
+#ifdef TEST_FRAMEWORK
+			if ((test_engine = ast_rtp_instance_get_test(instance))) {
+				test_engine->sdes_received = 1;
+			}
+#endif
 			break;
 		case RTCP_PT_BYE:
 			if (rtcp_debug_test_addr(addr)) {
@@ -7656,11 +7742,10 @@
 			if (res < 0) {
 				ast_debug(1, "Failed to send NACK request out\n");
 			} else {
+				ast_debug(2, "Sending a NACK request on RTP instance '%p' to get missing packets\n", instance);
 				/* Update RTCP SR/RR statistics */
 				ast_rtcp_calculate_sr_rr_statistics(instance, rtcp_report, remote_address, ice, sr);
 			}
-
-			ast_debug(2, "Sending a NACK request on RTP instance '%p' to get missing packets\n", instance);
 		}
 
 		return &ast_null_frame;
diff --git a/tests/test_data_buffer.c b/tests/test_data_buffer.c
index 93c2c06..2fd56e1 100644
--- a/tests/test_data_buffer.c
+++ b/tests/test_data_buffer.c
@@ -18,7 +18,7 @@
 
 /*!
  * \file
- * \brief Media Stream API Unit Tests
+ * \brief Data Buffer API Unit Tests
  *
  * \author Ben Ford <bford at digium.com>
  *
diff --git a/tests/test_res_rtp.c b/tests/test_res_rtp.c
new file mode 100644
index 0000000..ecedb4f
--- /dev/null
+++ b/tests/test_res_rtp.c
@@ -0,0 +1,516 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2019, Sangoma, Inc.
+ *
+ * Ben Ford <bford at sangoma.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief RTP/RTCP Unit Tests
+ *
+ * \author Ben Ford <bford at digium.com>
+ *
+ */
+
+/*** MODULEINFO
+	<depend>TEST_FRAMEWORK</depend>
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+#include "asterisk/module.h"
+#include "asterisk/test.h"
+#include "asterisk/rtp_engine.h"
+#include "asterisk/data_buffer.h"
+#include "asterisk/format_cache.h"
+
+enum test_type {
+	TEST_TYPE_NONE = 0,	/* No special setup required */
+	TEST_TYPE_NACK,		/* Enable NACK */
+	TEST_TYPE_REMB,		/* Enable REMB */
+};
+
+static void ast_sched_context_destroy_wrapper(struct ast_sched_context *sched)
+{
+	if (sched) {
+		ast_sched_context_destroy(sched);
+	}
+}
+
+static int test_init_rtp_instances(struct ast_rtp_instance **instance1,
+	struct ast_rtp_instance **instance2, struct ast_sched_context *test_sched,
+	enum test_type type)
+{
+	struct ast_sockaddr addr;
+
+	ast_sockaddr_parse(&addr, "127.0.0.1", 0);
+
+	*instance1 = ast_rtp_instance_new("asterisk", test_sched, &addr, NULL);
+	*instance2 = ast_rtp_instance_new("asterisk", test_sched, &addr, NULL);
+	if (!instance1 || !instance2) {
+		return -1;
+	}
+
+	ast_rtp_instance_set_prop(*instance1, AST_RTP_PROPERTY_RTCP, AST_RTP_INSTANCE_RTCP_MUX);
+	ast_rtp_instance_set_prop(*instance2, AST_RTP_PROPERTY_RTCP, AST_RTP_INSTANCE_RTCP_MUX);
+
+	if (type == TEST_TYPE_NACK) {
+		ast_rtp_instance_set_prop(*instance1, AST_RTP_PROPERTY_RETRANS_RECV, 1);
+		ast_rtp_instance_set_prop(*instance1, AST_RTP_PROPERTY_RETRANS_SEND, 1);
+		ast_rtp_instance_set_prop(*instance2, AST_RTP_PROPERTY_RETRANS_RECV, 2);
+		ast_rtp_instance_set_prop(*instance2, AST_RTP_PROPERTY_RETRANS_SEND, 2);
+	} else if (type == TEST_TYPE_REMB) {
+		ast_rtp_instance_set_prop(*instance1, AST_RTP_PROPERTY_REMB, 1);
+		ast_rtp_instance_set_prop(*instance2, AST_RTP_PROPERTY_REMB, 1);
+	}
+
+	ast_rtp_instance_get_local_address(*instance1, &addr);
+	ast_rtp_instance_set_remote_address(*instance2, &addr);
+
+	ast_rtp_instance_get_local_address(*instance2, &addr);
+	ast_rtp_instance_set_remote_address(*instance1, &addr);
+
+	ast_rtp_instance_reset_test_engine(*instance1);
+
+	ast_rtp_instance_activate(*instance1);
+	ast_rtp_instance_activate(*instance2);
+
+	return 0;
+}
+
+static void test_write_frames(struct ast_rtp_instance *instance, int seqno, int num)
+{
+	char data[320] = "";
+	struct ast_frame frame_out = {
+		.frametype = AST_FRAME_VOICE,
+		.subclass.format = ast_format_ulaw,
+		.data.ptr = data,
+		.datalen = 160,
+	};
+	int index;
+
+	ast_set_flag(&frame_out, AST_FRFLAG_HAS_SEQUENCE_NUMBER);
+
+	for (index = 0; index < num; index++) {
+		frame_out.seqno = seqno + index;
+		ast_rtp_instance_write(instance, &frame_out);
+	}
+}
+
+static void test_read_frames(struct ast_rtp_instance *instance, int num)
+{
+	struct ast_frame *frame_in;
+	int index;
+
+	for (index = 0; index < num; index++) {
+		frame_in = ast_rtp_instance_read(instance, 0);
+		if (frame_in) {
+			ast_frfree(frame_in);
+		}
+	}
+}
+
+static void test_write_and_read_frames(struct ast_rtp_instance *instance1,
+	struct ast_rtp_instance *instance2, int seqno, int num)
+{
+	test_write_frames(instance1, seqno, num);
+	test_read_frames(instance2, num);
+}
+
+AST_TEST_DEFINE(nack_no_packet_loss)
+{
+	RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ast_rtp_instance_destroy);
+	RAII_VAR(struct ast_rtp_instance *, instance2, NULL, ast_rtp_instance_destroy);
+	RAII_VAR(struct ast_sched_context *, test_sched, NULL, ast_sched_context_destroy_wrapper);
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "nack_no_packet_loss";
+		info->category = "/res/res_rtp/";
+		info->summary = "nack no packet loss unit test";
+		info->description =
+			"Tests sending packets with no packet loss and "
+			"validates that the send buffer stores sent packets "
+			"and the receive buffer is empty";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	test_sched = ast_sched_context_create();
+
+	if ((test_init_rtp_instances(&instance1, &instance2, test_sched, TEST_TYPE_NACK)) < 0) {
+		ast_log(LOG_ERROR, "Failed to initialize test!\n");
+		return AST_TEST_FAIL;
+	}
+
+	test_write_and_read_frames(instance1, instance2, 1000, 10);
+
+	ast_test_validate(test, ast_rtp_instance_get_send_buffer_count(instance1) == 10,
+		"Send buffer did not have the expected count of 10");
+
+	ast_test_validate(test, ast_rtp_instance_get_recv_buffer_count(instance2) == 0,
+		"Receive buffer did not have the expected count of 0");
+
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(nack_nominal)
+{
+	RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ast_rtp_instance_destroy);
+	RAII_VAR(struct ast_rtp_instance *, instance2, NULL, ast_rtp_instance_destroy);
+	RAII_VAR(struct ast_sched_context *, test_sched, NULL, ast_sched_context_destroy_wrapper);
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "nack_nominal";
+		info->category = "/res/res_rtp/";
+		info->summary = "nack nominal unit test";
+		info->description =
+			"Tests sending packets with some packet loss and "
+			"validates that a NACK request is sent on reaching "
+			"the triggering amount of lost packets";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	test_sched = ast_sched_context_create();
+
+	if ((test_init_rtp_instances(&instance1, &instance2, test_sched, TEST_TYPE_NACK)) < 0) {
+		ast_log(LOG_ERROR, "Failed to initialize test!\n");
+		return AST_TEST_FAIL;
+	}
+
+	/* Start normally */
+	test_write_and_read_frames(instance1, instance2, 1000, 10);
+
+	/* Set the number of packets to drop when we send them next */
+	ast_rtp_instance_drop_packets(instance2, 10);
+	test_write_and_read_frames(instance1, instance2, 1010, 10);
+
+	/* Send enough packets to reach the NACK trigger */
+	test_write_and_read_frames(instance1, instance2, 1020, ast_rtp_instance_get_recv_buffer_max(instance2) / 2);
+
+	/* This needs to be read as RTCP */
+	test_read_frames(instance1, 1);
+
+	/* We should have the missing packets to read now */
+	test_read_frames(instance2, 10);
+
+	ast_test_validate(test, ast_rtp_instance_get_recv_buffer_count(instance2) == 0,
+		"Receive buffer did not have the expected count of 0");
+
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(nack_overflow)
+{
+	RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ast_rtp_instance_destroy);
+	RAII_VAR(struct ast_rtp_instance *, instance2, NULL, ast_rtp_instance_destroy);
+	RAII_VAR(struct ast_sched_context *, test_sched, NULL, ast_sched_context_destroy_wrapper);
+	int max_packets;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "nack_overflow";
+		info->category = "/res/res_rtp/";
+		info->summary = "nack overflow unit test";
+		info->description =
+			"Tests that when the buffer hits its capacity, we "
+			"queue all the packets we currently have stored";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	test_sched = ast_sched_context_create();
+
+	if ((test_init_rtp_instances(&instance1, &instance2, test_sched, TEST_TYPE_NACK)) < 0) {
+		ast_log(LOG_ERROR, "Failed to initialize test!\n");
+		return AST_TEST_FAIL;
+	}
+
+	/* Start normally */
+	test_write_and_read_frames(instance1, instance2, 1000, 10);
+
+	/* Send enough packets to fill the buffer */
+	max_packets = ast_rtp_instance_get_recv_buffer_max(instance2);
+	test_write_and_read_frames(instance1, instance2, 1020, max_packets);
+
+	ast_test_validate(test, ast_rtp_instance_get_recv_buffer_count(instance2) == max_packets,
+		"Receive buffer did not have the expected count of max buffer size");
+
+	/* Send the packet that will overflow the buffer */
+	test_write_and_read_frames(instance1, instance2, 1020 + max_packets, 1);
+
+	ast_test_validate(test, ast_rtp_instance_get_recv_buffer_count(instance2) == 0,
+		"Receive buffer did not have the expected count of 0");
+
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(lost_packet_stats_nominal)
+{
+	RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ast_rtp_instance_destroy);
+	RAII_VAR(struct ast_rtp_instance *, instance2, NULL, ast_rtp_instance_destroy);
+	RAII_VAR(struct ast_sched_context *, test_sched, NULL, ast_sched_context_destroy_wrapper);
+	struct ast_rtp_instance_stats stats = { 0, };
+	enum ast_rtp_instance_stat stat = AST_RTP_INSTANCE_STAT_RXPLOSS;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "lost_packet_stats_nominal";
+		info->category = "/res/res_rtp/";
+		info->summary = "lost packet stats nominal unit test";
+		info->description =
+			"Tests that when some packets are lost, we calculate that "
+			"loss correctly when doing lost packet statistics";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	test_sched = ast_sched_context_create();
+
+	if ((test_init_rtp_instances(&instance1, &instance2, test_sched, TEST_TYPE_NONE)) < 0) {
+		ast_log(LOG_ERROR, "Failed to initialize test!\n");
+		return AST_TEST_FAIL;
+	}
+
+	/* Start normally */
+	test_write_and_read_frames(instance1, instance2, 1000, 10);
+
+	/* Send some more packets, but with a gap */
+	test_write_and_read_frames(instance1, instance2, 1015, 5);
+
+	/* Send a RR to calculate lost packet statistics. We should be missing 5 packets */
+	ast_rtp_instance_queue_report(instance1);
+	test_write_frames(instance2, 1000, 1);
+
+	/* Check RTCP stats to see if we got the expected packet loss count */
+	ast_rtp_instance_get_stats(instance2, &stats, stat);
+	ast_test_validate(test, stats.rxploss == 5,
+		"Condition of 5 lost packets was not met");
+
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(remb_nominal)
+{
+	RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ast_rtp_instance_destroy);
+	RAII_VAR(struct ast_rtp_instance *, instance2, NULL, ast_rtp_instance_destroy);
+	RAII_VAR(struct ast_sched_context *, test_sched, NULL, ast_sched_context_destroy_wrapper);
+	RAII_VAR(struct ast_frame *, frame_in, NULL, ast_frfree);
+	/* Use the structure softmix_remb_collector uses to store information for REMB */
+	struct ast_rtp_rtcp_feedback feedback = {
+		.fmt = AST_RTP_RTCP_FMT_REMB,
+		.remb.br_exp = 0,
+		.remb.br_mantissa = 1000,
+	};
+	struct ast_frame frame_out = {
+		.frametype = AST_FRAME_RTCP,
+		.subclass.integer = AST_RTP_RTCP_PSFB,
+		.data.ptr = &feedback,
+		.datalen = sizeof(feedback),
+	};
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "remb_nominal";
+		info->category = "/res/res_rtp/";
+		info->summary = "remb nominal unit test";
+		info->description =
+			"Tests sending and receiving a REMB packet";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		/* Disable for now - there's a bug! */
+		return AST_TEST_NOT_RUN;
+	}
+
+	test_sched = ast_sched_context_create();
+
+	if ((test_init_rtp_instances(&instance1, &instance2, test_sched, TEST_TYPE_REMB)) < 0) {
+		ast_log(LOG_ERROR, "Failed to initialize test!\n");
+		return AST_TEST_FAIL;
+	}
+
+	/* The schedid must be 0 or greater, so let's do that now */
+	ast_rtp_instance_set_schedid(instance1, 0);
+
+	ast_rtp_instance_write(instance1, &frame_out);
+
+	/*
+	 * There may be some additional work that needs to be done here, depending on how
+	 * Asterisk handles the reading in of compound packets. We might get an ast_null_frame
+	 * here instead of the REMB frame. We'll need to check the frametype to distinguish
+	 * between them (AST_FRAME_NULL for ast_null_frame, AST_FRAME_RTCP for REMB).
+	 */
+	frame_in = ast_rtp_instance_read(instance2, 0);
+	ast_test_validate(test, frame_in != NULL, "Did not receive a REMB frame");
+	ast_test_validate(test, frame_in->frametype == AST_FRAME_RTCP,
+		"REMB frame did not have the expected frametype");
+	ast_test_validate(test, frame_in->subclass.integer == AST_RTP_RTCP_PSFB,
+		"REMB frame did not have the expected subclass integer");
+
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(sr_rr_nominal)
+{
+	RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ast_rtp_instance_destroy);
+	RAII_VAR(struct ast_rtp_instance *, instance2, NULL, ast_rtp_instance_destroy);
+	RAII_VAR(struct ast_sched_context *, test_sched, NULL, ast_sched_context_destroy_wrapper);
+	RAII_VAR(struct ast_frame *, frame_in, NULL, ast_frfree);
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "sr_rr_nominal";
+		info->category = "/res/res_rtp/";
+		info->summary = "SR/RR nominal unit test";
+		info->description =
+			"Tests sending SR/RR and receiving it; includes SDES";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	test_sched = ast_sched_context_create();
+
+	if ((test_init_rtp_instances(&instance1, &instance2, test_sched, TEST_TYPE_NONE)) < 0) {
+		ast_log(LOG_ERROR, "Failed to initialize test!\n");
+		return AST_TEST_FAIL;
+	}
+
+	test_write_and_read_frames(instance1, instance2, 1000, 10);
+
+	/*
+	 * Set the send_report flag so we send a sender report instead of normal RTP. We
+	 * also need to ensure that SDES processed.
+	 */
+	ast_rtp_instance_queue_report(instance1);
+	test_write_frames(instance1, 1010, 1);
+
+	frame_in = ast_rtp_instance_read(instance2, 0);
+	ast_test_validate(test, frame_in->frametype == AST_FRAME_RTCP,
+		"Sender report frame did not have the expected frametype");
+	ast_test_validate(test, frame_in->subclass.integer == AST_RTP_RTCP_SR,
+		"Sender report frame did not have the expected subclass integer");
+	ast_test_validate(test, ast_rtp_instance_get_sdes_received(instance2) == 1,
+		"SDES was never processed for sender report");
+
+	ast_frfree(frame_in);
+
+	/* Set the send_report flag so we send a receiver report instead of normal RTP */
+	ast_rtp_instance_queue_report(instance1);
+	test_write_frames(instance1, 1010, 1);
+
+	frame_in = ast_rtp_instance_read(instance2, 0);
+	ast_test_validate(test, frame_in->frametype == AST_FRAME_RTCP,
+		"Receiver report frame did not have the expected frametype");
+	ast_test_validate(test, frame_in->subclass.integer == AST_RTP_RTCP_RR,
+		"Receiver report frame did not have the expected subclass integer");
+
+	return AST_TEST_PASS;
+}
+
+AST_TEST_DEFINE(fir_nominal)
+{
+	RAII_VAR(struct ast_rtp_instance *, instance1, NULL, ast_rtp_instance_destroy);
+	RAII_VAR(struct ast_rtp_instance *, instance2, NULL, ast_rtp_instance_destroy);
+	RAII_VAR(struct ast_sched_context *, test_sched, NULL, ast_sched_context_destroy_wrapper);
+	RAII_VAR(struct ast_frame *, frame_in, NULL, ast_frfree);
+	struct ast_frame frame_out = {
+		.frametype = AST_FRAME_CONTROL,
+		.subclass.integer = AST_CONTROL_VIDUPDATE,
+	};
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "fir_nominal";
+		info->category = "/res/res_rtp/";
+		info->summary = "fir nominal unit test";
+		info->description =
+			"Tests sending and receiving a FIR packet";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		/* Disable for now - there's a bug! */
+		return AST_TEST_NOT_RUN;
+	}
+
+	test_sched = ast_sched_context_create();
+
+	if ((test_init_rtp_instances(&instance1, &instance2, test_sched, TEST_TYPE_NONE)) < 0) {
+		ast_log(LOG_ERROR, "Failed to initialize test!\n");
+		return AST_TEST_FAIL;
+	}
+
+	/* Send some packets to learn SSRC */
+	test_write_and_read_frames(instance2, instance1, 1000, 10);
+
+	/* The schedid must be 0 or greater, so let's do that now */
+	ast_rtp_instance_set_schedid(instance1, 0);
+
+	/*
+	 * This will not directly write a frame out, but cause Asterisk to see it as a FIR
+	 * request, which will then trigger rtp_write_rtcp_fir, which will send out the
+	 * appropriate packet.
+	 */
+	ast_rtp_instance_write(instance1, &frame_out);
+
+	/*
+	 * We only receive one frame, the FIR request. It won't have a subclass integer of
+	 * 206 (PSFB) because ast_rtcp_interpret sets it to 18 (AST_CONTROL_VIDUPDATE), so
+	 * check for that.
+	 *
+	 * NOTE - similar to REMB, there may be more that needs to be done here when the
+	 * packet is sent as a compound packet!
+	 */
+	frame_in = ast_rtp_instance_read(instance2, 0);
+	ast_test_validate(test, frame_in != NULL, "Did not receive a FIR frame");
+	ast_test_validate(test, frame_in->frametype == AST_FRAME_CONTROL,
+		"FIR frame did not have the expected frametype");
+	ast_test_validate(test, frame_in->subclass.integer == AST_CONTROL_VIDUPDATE,
+		"FIR frame did not have the expected subclass integer");
+
+	return AST_TEST_PASS;
+}
+
+static int unload_module(void)
+{
+	AST_TEST_UNREGISTER(nack_no_packet_loss);
+	AST_TEST_UNREGISTER(nack_nominal);
+	AST_TEST_UNREGISTER(nack_overflow);
+	AST_TEST_UNREGISTER(lost_packet_stats_nominal);
+	AST_TEST_UNREGISTER(remb_nominal);
+	AST_TEST_UNREGISTER(sr_rr_nominal);
+	AST_TEST_UNREGISTER(fir_nominal);
+	return 0;
+}
+
+static int load_module(void)
+{
+	AST_TEST_REGISTER(nack_no_packet_loss);
+	AST_TEST_REGISTER(nack_nominal);
+	AST_TEST_REGISTER(nack_overflow);
+	AST_TEST_REGISTER(lost_packet_stats_nominal);
+	AST_TEST_REGISTER(remb_nominal);
+	AST_TEST_REGISTER(sr_rr_nominal);
+	AST_TEST_REGISTER(fir_nominal);
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "RTP/RTCP test module");

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/12813
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-Change-Id: I56107c7411003a247589bbb6086d25c54719901b
Gerrit-Change-Number: 12813
Gerrit-PatchSet: 7
Gerrit-Owner: Benjamin Keith Ford <bford at digium.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua C. Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190913/821d28ff/attachment-0001.html>


More information about the asterisk-code-review mailing list