mgcp: Fix/test reading/writing the sequence number

The sequence number was read from the wrong place and then
the wrong byte order conversion routine was used so we ended
up wirting 0x00, 0x00 into the patched sequence number. Add
a testcase for that.
diff --git a/openbsc/src/libmgcp/mgcp_transcode.c b/openbsc/src/libmgcp/mgcp_transcode.c
index 8ab9a04..296020c 100644
--- a/openbsc/src/libmgcp/mgcp_transcode.c
+++ b/openbsc/src/libmgcp/mgcp_transcode.c
@@ -429,7 +429,7 @@
 	if (payload_len > 0) {
 		ts_no = ntohl(*(uint32_t*)(data+4));
 		if (!state->is_running)
-			state->next_seq = ntohs(*(uint32_t*)(data+4));
+			state->next_seq = ntohs(*(uint16_t*)(data+2));
 
 		state->is_running = 1;
 
@@ -494,7 +494,7 @@
 	nsamples -= state->sample_cnt;
 
 	*len = rtp_hdr_size + rc;
-	*(uint16_t*)(data+2) = htonl(state->next_seq);
+	*(uint16_t*)(data+2) = htons(state->next_seq);
 	*(uint32_t*)(data+4) = htonl(ts_no);
 
 	state->next_seq += 1;
diff --git a/openbsc/tests/mgcp/mgcp_transcoding_test.c b/openbsc/tests/mgcp/mgcp_transcoding_test.c
index 6b33645..404268a 100644
--- a/openbsc/tests/mgcp/mgcp_transcoding_test.c
+++ b/openbsc/tests/mgcp/mgcp_transcoding_test.c
@@ -11,6 +11,7 @@
 #include <openbsc/gsm_data.h>
 #include <openbsc/mgcp.h>
 #include <openbsc/mgcp_internal.h>
+#include <openbsc/rtp.h>
 
 #include "bscconfig.h"
 #ifndef BUILD_MGCP_TRANSCODING
@@ -245,6 +246,56 @@
 	return 0;
 }
 
+static void test_rtp_seq_state(void)
+{
+	char buf[4096];
+	int len;
+	int cont;
+	void *ctx;
+	struct mgcp_endpoint *endp;
+	struct mgcp_process_rtp_state *state;
+	struct rtp_hdr *hdr;
+	uint32_t ts_no;
+	uint16_t seq_no;
+
+	given_configured_endpoint(160, 0, "pcma", "l16", &ctx, &endp);
+	state = endp->bts_end.rtp_process_data;
+	OSMO_ASSERT(!state->is_running);
+	OSMO_ASSERT(state->next_seq == 0);
+	OSMO_ASSERT(state->next_time == 0);
+
+	/* initialize packet */
+	len = audio_packets_pcma[0].len;
+	memcpy(buf, audio_packets_pcma[0].data, len);
+	cont = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, len);
+	OSMO_ASSERT(cont >= 0);
+	OSMO_ASSERT(state->is_running);
+	OSMO_ASSERT(state->next_seq == 2);
+	OSMO_ASSERT(state->next_time = 240);
+
+	/* verify that the right timestamp was written */
+	OSMO_ASSERT(len == audio_packets_pcma[0].len);
+	hdr = (struct rtp_hdr *) &buf[0];
+
+	memcpy(&ts_no, &hdr->timestamp, sizeof(ts_no));
+	OSMO_ASSERT(htonl(ts_no) == 160);
+	memcpy(&seq_no, &hdr->sequence, sizeof(seq_no));
+	OSMO_ASSERT(htons(seq_no) == 1);
+	/* Check the right sequence number is written */
+	state->next_seq = 1234;
+	len = audio_packets_pcma[0].len;
+	memcpy(buf, audio_packets_pcma[0].data, len);
+	cont = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, len);
+	OSMO_ASSERT(cont >= 0);
+	OSMO_ASSERT(len == audio_packets_pcma[0].len);
+	hdr = (struct rtp_hdr *) &buf[0];
+
+	memcpy(&seq_no, &hdr->sequence, sizeof(seq_no));
+	OSMO_ASSERT(htons(seq_no) == 1234);
+
+	talloc_free(ctx);
+}
+
 static int test_repacking(int in_samples, int out_samples, int no_transcode)
 {
 	char buf[4096] = {0x80, 0};
@@ -388,6 +439,7 @@
 	test_repacking(160, 240, 1);
 	test_repacking(160, 100, 0);
 	test_repacking(160, 100, 1);
+	test_rtp_seq_state();
 
 	return 0;
 }
diff --git a/openbsc/tests/mgcp/mgcp_transcoding_test.ok b/openbsc/tests/mgcp/mgcp_transcoding_test.ok
index 07929a8..e06b0e1 100644
--- a/openbsc/tests/mgcp/mgcp_transcoding_test.ok
+++ b/openbsc/tests/mgcp/mgcp_transcoding_test.ok
@@ -27,14 +27,14 @@
 == Transcoding test ==
 converting l16 -> gsm
 encoded:
-    80 0b 00 00 00 00 00 a0 11 22 33 44 d4 7c e3 e9 
+    80 0b 00 01 00 00 00 a0 11 22 33 44 d4 7c e3 e9 
     62 50 39 f0 f8 b4 68 ea 6c 0e 81 1b 56 2a d5 bc 
     69 9c d1 f0 66 7a ec 49 7a 33 3d 0a de 
 counted: 12
 == Transcoding test ==
 converting l16 -> pcma
 encoded:
-    80 0b 00 00 00 00 00 a0 11 22 33 44 d5 a5 a3 a5 
+    80 0b 00 01 00 00 00 a0 11 22 33 44 d5 a5 a3 a5 
     d5 25 23 25 d5 a5 a3 a5 d5 25 23 25 d5 a5 a3 a5 
     d5 25 23 25 d5 a5 a3 a5 d5 25 23 25 d5 a5 a3 a5 
     d5 25 23 25 d5 a5 a3 a5 d5 25 23 25 d5 a5 a3 a5 
@@ -49,7 +49,7 @@
 == Transcoding test ==
 converting gsm -> l16
 encoded:
-    80 03 00 00 00 00 00 a0 11 22 33 44 00 00 54 00 
+    80 03 00 01 00 00 00 a0 11 22 33 44 00 00 54 00 
     59 f0 34 20 c4 c8 b9 f8 e2 18 f1 e8 f2 28 f0 e0 
     46 08 4f 80 2c a0 a9 c8 80 00 c0 58 3f 80 63 c0 
     24 b8 fa b8 f6 88 0b a0 c8 70 a8 b0 c8 c0 3b a8 
@@ -81,7 +81,7 @@
 == Transcoding test ==
 converting gsm -> pcma
 encoded:
-    80 03 00 00 00 00 00 a0 11 22 33 44 d5 a0 a3 bf 
+    80 03 00 01 00 00 00 a0 11 22 33 44 d5 a0 a3 bf 
     38 24 08 19 1e 1b a4 a6 b3 20 2a 3a ba ad b7 60 
     17 92 3e 20 3e b8 ac b2 32 2c 20 02 b6 be be 82 
     04 27 26 35 8d a4 a6 b5 35 21 20 31 8d a7 a6 b6 
@@ -96,7 +96,7 @@
 == Transcoding test ==
 converting pcma -> l16
 encoded:
-    80 08 00 00 00 00 00 a0 11 22 33 44 00 08 42 00 
+    80 08 00 01 00 00 00 a0 11 22 33 44 00 08 42 00 
     5a 00 42 00 00 08 be 00 a6 00 be 00 00 08 42 00 
     5a 00 42 00 00 08 be 00 a6 00 be 00 00 08 42 00 
     5a 00 42 00 00 08 be 00 a6 00 be 00 00 08 42 00 
@@ -121,7 +121,7 @@
 == Transcoding test ==
 converting pcma -> gsm
 encoded:
-    80 08 00 00 00 00 00 a0 11 22 33 44 d4 b9 f4 5d 
+    80 08 00 01 00 00 00 a0 11 22 33 44 d4 b9 f4 5d 
     d9 50 5a e1 a0 cd 76 ea 52 0e 87 53 ad d4 ea a2 
     0a 63 ca e9 60 79 e2 2a 25 d2 c0 f3 39 
 counted: 12
@@ -161,7 +161,7 @@
 == Transcoding test ==
 converting gsm -> pcma
 encoded:
-    80 08 00 00 00 00 00 a0 11 22 33 44 d5 a0 a3 bf 
+    80 08 00 01 00 00 00 a0 11 22 33 44 d5 a0 a3 bf 
     38 24 08 19 1e 1b a4 a6 b3 20 2a 3a ba ad b7 60 
     17 92 3e 20 3e b8 ac b2 32 2c 20 02 b6 be be 82 
     04 27 26 35 8d a4 a6 b5 35 21 20 31 8d a7 a6 b6