mgcp_network: translate payload type numbers in RTP packets

Since no transcoding is in place osmo-mgw forwards the incoming rtp
packets as they are (there may be minor modifications of the header) from
an ingress connection to an egress connection.

This works without problems as long as both connections use the same
payload type. For IANA defined fixed payload type numbers this is
usually the case, but for dynemic payload type numbers both ends may set
up the same codecs but with different payload type numbers.

When different payload type numbers are set up, and the packet is passed
through without modification, it will have the wrong payload type when
it is sent. The receiving end may then toss the packet since it expects
packets with the payload type it has configured.

The machanism, which is introduced with this patch looks up actual codec
inside the struct data of the ingress connection and then looks for the
matching codec in the struct data of the egress connection. When it
finds the codec there it looks up the payload type of this codec. The
header of the RTP packet is then patched with the correct payoad type.

- Add function mgcp_codec_pt_translate() to look up the payload type
- Add unit-test for function mgcp_codec_pt_translate()
- Add payload type translation to mgcp_network.c

Change-Id: I3a874e59fa07bcc2a67c376cafa197360036f539
Related: OS#2728
Related: OS#3384
diff --git a/src/libosmo-mgcp/mgcp_codec.c b/src/libosmo-mgcp/mgcp_codec.c
index 2ce90dd..55be554 100644
--- a/src/libosmo-mgcp/mgcp_codec.c
+++ b/src/libosmo-mgcp/mgcp_codec.c
@@ -341,3 +341,70 @@
 
 	return -EINVAL;
 }
+
+/* Compare two codecs, all parameters must match up, except for the payload type
+ * number. */
+static bool codecs_cmp(struct mgcp_rtp_codec *codec_a, struct mgcp_rtp_codec *codec_b)
+{
+	if (codec_a->rate != codec_b->rate)
+		return false;
+	if (codec_a->channels != codec_b->channels)
+		return false;
+	if (codec_a->frame_duration_num != codec_b->frame_duration_num)
+		return false;
+	if (codec_a->frame_duration_den != codec_b->frame_duration_den)
+		return false;
+	if (strcmp(codec_a->audio_name, codec_b->audio_name))
+		return false;
+	if (strcmp(codec_a->subtype_name, codec_b->subtype_name))
+		return false;
+
+	return true;
+}
+
+/*! Translate a given payload type number that belongs to the packet of a
+ *  source connection to the equivalent payload type number that matches the
+ *  configuration of a destination connection.
+ *  \param[in] conn_src related source rtp-connection.
+ *  \param[in] conn_dst related destination rtp-connection.
+ *  \param[in] payload_type number from the source packet or source connection.
+ *  \returns translated payload type number on success, -EINVAL on failure. */
+int mgcp_codec_pt_translate(struct mgcp_conn_rtp *conn_src, struct mgcp_conn_rtp *conn_dst, int payload_type)
+{
+	struct mgcp_rtp_end *rtp_src;
+	struct mgcp_rtp_end *rtp_dst;
+	struct mgcp_rtp_codec *codec_src = NULL;
+	struct mgcp_rtp_codec *codec_dst = NULL;
+	unsigned int i;
+	unsigned int codecs_assigned;
+
+	rtp_src = &conn_src->end;
+	rtp_dst = &conn_dst->end;
+
+	/* Find the codec information that is used on the source side */
+	codecs_assigned = rtp_src->codecs_assigned;
+	OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
+	for (i = 0; i < codecs_assigned; i++) {
+		if (payload_type == rtp_src->codecs[i].payload_type) {
+			codec_src = &rtp_src->codecs[i];
+			break;
+		}
+	}
+	if (!codec_src)
+		return -EINVAL;
+
+	/* Use the codec infrmation from the source and try to find the
+	 * equivalent of it on the destination side */
+	codecs_assigned = rtp_dst->codecs_assigned;
+	OSMO_ASSERT(codecs_assigned <= MGCP_MAX_CODECS);
+	for (i = 0; i < codecs_assigned; i++) {
+		if (codecs_cmp(codec_src, &rtp_dst->codecs[i])) {
+			codec_dst = &rtp_dst->codecs[i];
+			break;
+		}
+	}
+	if (!codec_dst)
+		return -EINVAL;
+
+	return codec_dst->payload_type;
+}