mgcp/rtp: Fix timestamp offset when patching RTP packets

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
diff --git a/openbsc/tests/mgcp/mgcp_test.c b/openbsc/tests/mgcp/mgcp_test.c
index b2d20b4..d36aaa8 100644
--- a/openbsc/tests/mgcp/mgcp_test.c
+++ b/openbsc/tests/mgcp/mgcp_test.c
@@ -417,7 +417,7 @@
 	struct mgcp_trunk_config trunk;
 	struct mgcp_endpoint endp;
 	struct mgcp_rtp_state state;
-	struct mgcp_rtp_end rtp;
+	struct mgcp_rtp_end *rtp = &endp.net_end;
 	struct sockaddr_in addr = {0};
 	char buffer[4096];
 
@@ -426,15 +426,21 @@
 	memset(&trunk, 0, sizeof(trunk));
 	memset(&endp, 0, sizeof(endp));
 	memset(&state, 0, sizeof(state));
-	memset(&rtp, 0, sizeof(rtp));
 
 	trunk.number_endpoints = 1;
 	trunk.endpoints = &endp;
-
-	rtp.payload_type = 98;
-	endp.allow_patch = 1;
 	endp.tcfg = &trunk;
 
+	/* This doesn't free endp but resets/frees all fields of the structure
+	 * and invokes mgcp_rtp_end_reset() for each mgcp_rtp_end. OTOH, it
+	 * expects valid pointer fields (either NULL or talloc'ed), so the
+	 * memset is still needed. It also requires that endp.tcfg and
+	 * trunk.endpoints are set up properly. */
+	mgcp_free_endp(&endp);
+
+	rtp->payload_type = 98;
+	endp.allow_patch = 1;
+
 	for (i = 0; i < ARRAY_SIZE(test_rtp_packets1); ++i) {
 		struct rtp_packet_info *info = test_rtp_packets1 + i;
 
@@ -442,7 +448,7 @@
 		OSMO_ASSERT(info->len >= 0);
 		memmove(buffer, info->data, info->len);
 
-		mgcp_patch_and_count(&endp, &state, &rtp, &addr,
+		mgcp_patch_and_count(&endp, &state, rtp, &addr,
 				     buffer, info->len);
 
 		printf("TS: %d, dTS: %d, TS Errs: in %d, out %d\n",