mgcp: Fix/test the case of a time jump and the resync

In case the sender didn't send a couple of frames we will have
a time gap that is bigger than the accepted delta. Add a new
testcase for this and update the next_time.
diff --git a/openbsc/src/libmgcp/mgcp_transcode.c b/openbsc/src/libmgcp/mgcp_transcode.c
index 8e14d7f..4d4cec8 100644
--- a/openbsc/src/libmgcp/mgcp_transcode.c
+++ b/openbsc/src/libmgcp/mgcp_transcode.c
@@ -450,6 +450,7 @@
 					"0x%x dropping sample buffer due delta=%d sample_cnt=%d\n",
 					ENDPOINT_NUMBER(endp), delta, state->sample_cnt);
 				state->sample_cnt = 0;
+				state->next_time = ts_no;
 			} else if (delta < 0) {
 				LOGP(DMGCP, LOGL_NOTICE,
 				     "RTP time jumps backwards, delta = %d, "
diff --git a/openbsc/tests/mgcp/mgcp_transcoding_test.c b/openbsc/tests/mgcp/mgcp_transcoding_test.c
index e163f71..2e857a1 100644
--- a/openbsc/tests/mgcp/mgcp_transcoding_test.c
+++ b/openbsc/tests/mgcp/mgcp_transcoding_test.c
@@ -384,6 +384,53 @@
 
 		talloc_free(ctx);
 	}
+
+	{
+		/* from PCMA to GSM with a big time jump */
+		struct rtp_hdr *hdr;
+		uint32_t ts;
+
+		given_configured_endpoint(80, 160, "pcma", "gsm", &ctx, &endp);
+		state = endp->bts_end.rtp_process_data;
+
+		/* Add the first sample */
+		len = audio_packets_pcma[1].len;
+		memcpy(buf, audio_packets_pcma[1].data, len);
+		res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf));
+		OSMO_ASSERT(state->sample_cnt == 80);
+		OSMO_ASSERT(state->next_time == 232640);
+		OSMO_ASSERT(state->next_seq == 26527);
+		OSMO_ASSERT(res < 0);
+
+		/* Add a skip to the packet to force a 'resync' */
+		len = audio_packets_pcma[2].len;
+		memcpy(buf, audio_packets_pcma[2].data, len);
+		hdr = (struct rtp_hdr *) &buf[0];
+		/* jump the time and add alignment error */
+		ts = ntohl(hdr->timestamp) + 123 * 80 + 2;
+		hdr->timestamp = htonl(ts);
+		res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf));
+		OSMO_ASSERT(res < 0);
+		OSMO_ASSERT(state->sample_cnt == 80);
+		OSMO_ASSERT(state->next_time == ts);
+		OSMO_ASSERT(state->next_seq == 26527);
+		/* TODO: this can create alignment errors */
+
+
+		/* Now attempt to consume 160 samples */
+		len = audio_packets_pcma[2].len;
+		memcpy(buf, audio_packets_pcma[2].data, len);
+		hdr = (struct rtp_hdr *) &buf[0];
+		ts += 80;
+		hdr->timestamp = htonl(ts);
+		res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf));
+		OSMO_ASSERT(res == 12);
+		OSMO_ASSERT(state->sample_cnt == 0);
+		OSMO_ASSERT(state->next_time == ts + 160);
+		OSMO_ASSERT(state->next_seq == 26528);
+
+		talloc_free(ctx);
+	}
 }
 
 static int test_repacking(int in_samples, int out_samples, int no_transcode)