[PATCH 4/4] mgcp/rtp: Fix timestamp offset when patching RTP packets

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 Nov 21 18:05:45 UTC 2013


The current implementation increments the seqno but does not increment
the RTP timestamp, leading to two identical timestamps following one
after the other.

This patch fixes this by adding the computed tsdelta when the offset
is calulated. In the unlikely case, that a tsdelta hasn't been
computed yet when the SSRC changes, a tsdelta is computed based on
the RTP rate and a RTP packet duration of 20ms (one speech frame per
channel and packet). If the RTP rate is not known, a rate of 8000 is
assumed.

Note that this approach presumes, that the per RTP packet duration
(in samples) is the same for the last two packets of the stream being
replaced (the first one).

Sponsored-by: On-Waves ehf
---
 openbsc/include/openbsc/mgcp_internal.h |    1 +
 openbsc/src/libmgcp/mgcp_network.c      |   33 ++++++++++++++++++++++++++++---
 openbsc/tests/mgcp/mgcp_test.ok         |    6 +++---
 3 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/openbsc/include/openbsc/mgcp_internal.h b/openbsc/include/openbsc/mgcp_internal.h
index 2ad7058..b9bff53 100644
--- a/openbsc/include/openbsc/mgcp_internal.h
+++ b/openbsc/include/openbsc/mgcp_internal.h
@@ -73,6 +73,7 @@ struct mgcp_rtp_end {
 
 	/* per endpoint data */
 	int payload_type;
+	uint32_t rate;
 	char *fmtp_extra;
 
 	/*
diff --git a/openbsc/src/libmgcp/mgcp_network.c b/openbsc/src/libmgcp/mgcp_network.c
index 17e3d87..2ac873e 100644
--- a/openbsc/src/libmgcp/mgcp_network.c
+++ b/openbsc/src/libmgcp/mgcp_network.c
@@ -78,6 +78,10 @@ struct rtp_hdr {
 #define RTP_MAX_DROPOUT		3000
 #define RTP_MAX_MISORDER	100
 
+/* Assume audio frame length of 20ms */
+#define RTP_AUDIO_FRAME_DUR_NUM 20
+#define RTP_AUDIO_FRAME_DUR_DEN 1000
+#define RTP_AUDIO_DEFAULT_RATE  8000
 
 enum {
 	MGCP_DEST_NET = 0,
@@ -173,14 +177,37 @@ void mgcp_patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *sta
 		state->jitter = 0;
 		state->transit = arrival_time - timestamp;
 	} else if (state->ssrc != rtp_hdr->ssrc) {
+		tsdelta = state->last_tsdelta;
+		tsdelta_valid = tsdelta != 0;
+		if (!tsdelta_valid) {
+			int rate = rtp_end->rate;
+			if (!rate) {
+				rate = RTP_AUDIO_DEFAULT_RATE;
+				LOGP(DMGCP, LOGL_NOTICE,
+				     "Using default RTP rate %d which might be "
+				     "different from the rate contained in the "
+				     "SDP data.\n",
+				     rate
+				    );
+			}
+			tsdelta = rate * RTP_AUDIO_FRAME_DUR_NUM /
+				RTP_AUDIO_FRAME_DUR_DEN;
+			state->last_tsdelta = tsdelta;
+			tsdelta_valid = 1;
+		}
+
 		state->ssrc = rtp_hdr->ssrc;
 		state->seq_offset = (state->max_seq + 1) - seq;
-		state->timestamp_offset = state->last_timestamp - timestamp;
+		state->timestamp_offset = (state->last_timestamp + tsdelta) -
+			timestamp;
 		state->patch = endp->allow_patch;
 		LOGP(DMGCP, LOGL_NOTICE,
-			"The SSRC changed on 0x%x SSRC: %u offset: %d from %s:%d in %d\n",
+			"The SSRC changed on 0x%x SSRC: %u offset: %d tsdelta: %d "
+			"from %s:%d in %d\n",
 			ENDPOINT_NUMBER(endp), state->ssrc, state->seq_offset,
-			inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), endp->conn_mode);
+			tsdelta,
+			inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
+			endp->conn_mode);
 	} else {
 		/* Compute current per-packet timestamp delta */
 		if (state->initialized && seq != state->max_seq) {
diff --git a/openbsc/tests/mgcp/mgcp_test.ok b/openbsc/tests/mgcp/mgcp_test.ok
index 8c3fa26..5666424 100644
--- a/openbsc/tests/mgcp/mgcp_test.ok
+++ b/openbsc/tests/mgcp/mgcp_test.ok
@@ -41,7 +41,7 @@ TS: 1280, dTS: 160, TS Errs: in 3, out 3
 TS: 1400, dTS: 120, TS Errs: in 4, out 4
 TS: 1560, dTS: 160, TS Errs: in 5, out 5
 TS: 1720, dTS: 160, TS Errs: in 5, out 5
-TS: 1720, dTS: 160, TS Errs: in 5, out 6
-TS: 1880, dTS: 160, TS Errs: in 5, out 6
-TS: 2040, dTS: 160, TS Errs: in 5, out 6
+TS: 1880, dTS: 160, TS Errs: in 5, out 5
+TS: 2040, dTS: 160, TS Errs: in 5, out 5
+TS: 2200, dTS: 160, TS Errs: in 5, out 5
 Done
-- 
1.7.9.5





More information about the OpenBSC mailing list