osmux: Make sure RTP AMR feed to osmux is in octet-aligned mode

The Osmux implementation in libosmo-netif expects to work with RTP AMR
in octet-aligned mode. Therefore, if the peer connection received RTP
AMR in bandwidth-efficient mode, we need to convert it to octet-aligned
before feeding the packets to the osmux layer.

Related: SYS#6161
Change-Id: Ifeec44241079f7a31da12745c92bfdc4fb222f3a
diff --git a/include/osmocom/mgcp/mgcp_network.h b/include/osmocom/mgcp/mgcp_network.h
index a3d57f0..e95907d 100644
--- a/include/osmocom/mgcp/mgcp_network.h
+++ b/include/osmocom/mgcp/mgcp_network.h
@@ -183,3 +183,5 @@
 				 int id);
 void forward_data_tap(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg);
 uint32_t mgcp_get_current_ts(unsigned codec_rate);
+
+int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg, bool target_is_oa);
diff --git a/src/libosmo-mgcp/mgcp_network.c b/src/libosmo-mgcp/mgcp_network.c
index 3248cbd..72d97fd 100644
--- a/src/libosmo-mgcp/mgcp_network.c
+++ b/src/libosmo-mgcp/mgcp_network.c
@@ -717,11 +717,18 @@
 	return 0;
 }
 
-/* For AMR RTP two framing modes are defined RFC3267. There is a bandwith
+/*! Convert msg to AMR RTP framing mode specified by target_is_oa.
+ *  \param[in] endp MGCP Endpoint where this message belongs to (used for logging purposes)
+ *  \param[in] msg Message containing an AMR RTP payload (in octet-aligned or bandwidth-efficient format).
+ *  \param[in] target_is_oa the target framing mode that msg will contain after this function succeeds.
+ *  \returns The size of the new RTP AMR content on success, negative on error.
+ *
+ * For AMR RTP two framing modes are defined RFC3267. There is a bandwidth
  * efficient encoding scheme where all fields are packed together one after
  * another and an octet aligned mode where all fields are aligned to octet
- * boundaries. This function is used to convert between the two modes */
-static int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg,
+ * boundaries. This function is used to convert between the two modes.
+ */
+int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg,
 			      bool target_is_oa)
 {
 	/* NOTE: the msgb has an allocated length of RTP_BUF_SIZE, so there is
diff --git a/src/libosmo-mgcp/mgcp_osmux.c b/src/libosmo-mgcp/mgcp_osmux.c
index e1380ec..5df5446 100644
--- a/src/libosmo-mgcp/mgcp_osmux.c
+++ b/src/libosmo-mgcp/mgcp_osmux.c
@@ -226,6 +226,12 @@
 	if (!msg2)
 		return -1;
 
+	/* Osmux implementation works with AMR OA only, make sure we convert to it if needed: */
+	if (amr_oa_bwe_convert(conn->conn->endp, msg2, true) < 0) {
+		LOGPCONN(conn->conn, DOSMUX, LOGL_ERROR,
+			 "Error converting to AMR octet-aligned mode\n");
+		return -1;
+	}
 
 	while ((ret = osmux_xfrm_input(conn->osmux.in, msg2, conn->osmux.remote_cid)) > 0) {
 		/* batch full, build and deliver it */