[PATCH 10/10] mgcp/rtp: Refactored packet_duration computation

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/OpenBSC@lists.osmocom.org/.

Jacob Erlbeck jerlbeck at sysmocom.de
Thu Dec 5 16:44:25 UTC 2013


Since the packet duration is given in ms with the 'ptime' RTP media
attribute and also with the 'p' MGCP local connection option, the
computation is changed to use this value (if present). The
computation assumes, that there are N complete frames in a packet and
takes into account, that the ptime value possibly had been rounded
towards the next ms value (which is never the case with a frame length
of exact 20ms).

Sponsored-by: On-Waves ehf
---
 openbsc/include/openbsc/mgcp_internal.h |    3 +++
 openbsc/src/libmgcp/mgcp_network.c      |    5 +----
 openbsc/src/libmgcp/mgcp_protocol.c     |   24 +++++++++++++++++++++---
 3 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/openbsc/include/openbsc/mgcp_internal.h b/openbsc/include/openbsc/mgcp_internal.h
index b6f4a0d..d59c5d7 100644
--- a/openbsc/include/openbsc/mgcp_internal.h
+++ b/openbsc/include/openbsc/mgcp_internal.h
@@ -82,6 +82,7 @@ struct mgcp_rtp_end {
 	uint32_t frame_duration_num;
 	uint32_t frame_duration_den;
 	int  frames_per_packet;
+	uint32_t packet_duration_ms;
 	char *fmtp_extra;
 
 	/* RTP patching */
@@ -180,6 +181,8 @@ struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index);
 
 void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
 			 struct mgcp_rtp_end *rtp);
+uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
+				  struct mgcp_rtp_end *rtp);
 
 void mgcp_state_calc_loss(struct mgcp_rtp_state *s, struct mgcp_rtp_end *,
 			uint32_t *expected, int *loss);
diff --git a/openbsc/src/libmgcp/mgcp_network.c b/openbsc/src/libmgcp/mgcp_network.c
index 6463ae0..bf205d1 100644
--- a/openbsc/src/libmgcp/mgcp_network.c
+++ b/openbsc/src/libmgcp/mgcp_network.c
@@ -237,10 +237,7 @@ void mgcp_patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *sta
 		state->initialized = 1;
 		state->jitter = 0;
 		state->transit = arrival_time - timestamp;
-		state->packet_duration =
-			rtp_end->rate * rtp_end->frames_per_packet *
-			rtp_end->frame_duration_num /
-			rtp_end->frame_duration_den;
+		state->packet_duration = mgcp_rtp_packet_duration(endp, rtp_end);
 		state->out_stream = state->in_stream;
 		state->out_stream.last_timestamp = timestamp;
 		state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
diff --git a/openbsc/src/libmgcp/mgcp_protocol.c b/openbsc/src/libmgcp/mgcp_protocol.c
index facbeb2..3e26ad6 100644
--- a/openbsc/src/libmgcp/mgcp_protocol.c
+++ b/openbsc/src/libmgcp/mgcp_protocol.c
@@ -75,7 +75,7 @@ char *strline_r(char *str, char **saveptr)
 /* Assume audio frame length of 20ms */
 #define DEFAULT_RTP_AUDIO_FRAME_DUR_NUM 20
 #define DEFAULT_RTP_AUDIO_FRAME_DUR_DEN 1000
-#define DEFAULT_RTP_AUDIO_FRAMES_PER_PACKET 1
+#define DEFAULT_RTP_AUDIO_PACKET_DURATION_MS 20
 #define DEFAULT_RTP_AUDIO_DEFAULT_RATE  8000
 
 static void mgcp_rtp_end_reset(struct mgcp_rtp_end *end);
@@ -625,6 +625,23 @@ void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
 	rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
 }
 
+uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
+				  struct mgcp_rtp_end *rtp)
+{
+	int f = 0;
+
+	/* Get the number of frames per channel and packet */
+	if (rtp->frames_per_packet)
+		f = rtp->frames_per_packet;
+	else if (rtp->packet_duration_ms && rtp->frame_duration_num) {
+		int den = 1000 * rtp->frame_duration_num;
+		f = (rtp->packet_duration_ms * rtp->frame_duration_den + den/2)
+			/ den;
+	}
+
+	return rtp->rate * f * rtp->frame_duration_num / rtp->frame_duration_den;
+}
+
 static struct msgb *handle_create_con(struct mgcp_parse_data *p)
 {
 	struct mgcp_trunk_config *tcfg;
@@ -1080,8 +1097,9 @@ static void mgcp_rtp_end_reset(struct mgcp_rtp_end *end)
 	/* Set default values */
 	end->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM;
 	end->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN;
-	end->frames_per_packet  = DEFAULT_RTP_AUDIO_FRAMES_PER_PACKET;
-	end->rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE;
+	end->frames_per_packet  = 0; /* unknown */
+	end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS;
+	end->rate               = DEFAULT_RTP_AUDIO_DEFAULT_RATE;
 }
 
 static void mgcp_rtp_end_init(struct mgcp_rtp_end *end)
-- 
1.7.9.5





More information about the OpenBSC mailing list