gscon: use BSS-common payload types on BSS side

In cases where a codec has no fixed (IANA) payload type number, a
dynamically coosen payload type number is used. For the route between
BSC and MSC 3GPP as designated certain payload type numbers. However,
beond that, those payload type numbers may not necessarly apply. The
RTP communication between BTS and BSC still might run on a completely
different payload type number.

libosmo-netif contains a header file which payload type numbers
shall be used. Lets use those in order to signal the same payload type
numbers as we actually use in the RTP packets to the MGW.

Change-Id: I507a1b1446c8f140b2950d73cf737797604c1ac3
Related: OS#2728
Related: OS#3384
diff --git a/include/osmocom/bsc/mgw_endpoint_fsm.h b/include/osmocom/bsc/mgw_endpoint_fsm.h
index 34fdcb7..e264a3c 100644
--- a/include/osmocom/bsc/mgw_endpoint_fsm.h
+++ b/include/osmocom/bsc/mgw_endpoint_fsm.h
@@ -56,4 +56,4 @@
 const char *mgcp_conn_peer_name(const struct mgcp_conn_peer *info);
 
 enum mgcp_codecs chan_mode_to_mgcp_codec(enum gsm48_chan_mode chan_mode, bool full_rate);
-void mgcp_pick_codec(struct mgcp_conn_peer *verb_info, const struct gsm_lchan *lchan);
+void mgcp_pick_codec(struct mgcp_conn_peer *verb_info, const struct gsm_lchan *lchan, bool bss_side);
diff --git a/src/osmo-bsc/bsc_subscr_conn_fsm.c b/src/osmo-bsc/bsc_subscr_conn_fsm.c
index fd3c293..81fe9f6 100644
--- a/src/osmo-bsc/bsc_subscr_conn_fsm.c
+++ b/src/osmo-bsc/bsc_subscr_conn_fsm.c
@@ -491,7 +491,7 @@
 		.call_id = conn->sccp.conn_id,
 		.ptime = 20,
 	};
-	mgcp_pick_codec(&mgw_info, for_lchan);
+	mgcp_pick_codec(&mgw_info, for_lchan, false);
 
 	rc = osmo_strlcpy(mgw_info.addr, addr, sizeof(mgw_info.addr));
 	if (rc <= 0 || rc >= sizeof(mgw_info.addr)) {
diff --git a/src/osmo-bsc/lchan_rtp_fsm.c b/src/osmo-bsc/lchan_rtp_fsm.c
index f7efc1b..2cc9a78 100644
--- a/src/osmo-bsc/lchan_rtp_fsm.c
+++ b/src/osmo-bsc/lchan_rtp_fsm.c
@@ -157,7 +157,7 @@
 	if (lchan->conn)
 		crcx_info.call_id = lchan->conn->sccp.conn_id;
 	crcx_info.ptime = 20;
-	mgcp_pick_codec(&crcx_info, lchan);
+	mgcp_pick_codec(&crcx_info, lchan, true);
 
 	mgw_endpoint_ci_request(lchan->mgw_endpoint_ci_bts, MGCP_VERB_CRCX, &crcx_info,
 				fi, LCHAN_RTP_EV_MGW_ENDPOINT_AVAILABLE, LCHAN_RTP_EV_MGW_ENDPOINT_ERROR,
@@ -406,7 +406,7 @@
 		.port = to_lchan->abis_ip.bound_port,
 		.ptime = 20,
 	};
-	mgcp_pick_codec(&mdcx_info, to_lchan);
+	mgcp_pick_codec(&mdcx_info, to_lchan, true);
 
 	addr.s_addr = ntohl(to_lchan->abis_ip.bound_ip);
 	addr_str = inet_ntoa(addr);
diff --git a/src/osmo-bsc/mgw_endpoint_fsm.c b/src/osmo-bsc/mgw_endpoint_fsm.c
index 18e9dcf..fc49886 100644
--- a/src/osmo-bsc/mgw_endpoint_fsm.c
+++ b/src/osmo-bsc/mgw_endpoint_fsm.c
@@ -26,6 +26,7 @@
 
 #include <osmocom/core/fsm.h>
 #include <osmocom/core/byteswap.h>
+#include <osmocom/netif/rtp.h>
 
 #include <osmocom/bsc/debug.h>
 #include <osmocom/bsc/gsm_timers.h>
@@ -730,10 +731,31 @@
 	}
 }
 
-void mgcp_pick_codec(struct mgcp_conn_peer *verb_info, const struct gsm_lchan *lchan)
+int chan_mode_to_mgcp_bss_pt(enum mgcp_codecs codec)
+{
+	switch (codec) {
+	case CODEC_GSMHR_8000_1:
+		return RTP_PT_GSM_HALF;
+
+	case CODEC_GSMEFR_8000_1:
+		return RTP_PT_GSM_EFR;
+
+	case CODEC_AMR_8000_1:
+		return RTP_PT_AMR;
+
+	default:
+		/* Not an error, we just leave it to libosmo-mgcp-client to
+		 * decide over the PT. */
+		return -1;
+	}
+}
+
+void mgcp_pick_codec(struct mgcp_conn_peer *verb_info, const struct gsm_lchan *lchan, bool bss_side)
 {
 	enum mgcp_codecs codec = chan_mode_to_mgcp_codec(lchan->tch_mode,
 							 lchan->type == GSM_LCHAN_TCH_H? false : true);
+	int custom_pt;
+
 	if (codec < 0) {
 		LOG_LCHAN(lchan, LOGL_ERROR,
 			  "Unable to determine MGCP codec type for %s in chan-mode %s\n",
@@ -744,4 +766,12 @@
 
 	verb_info->codecs[0] = codec;
 	verb_info->codecs_len = 1;
+
+	/* Setup custom payload types (only for BSS side and when required) */
+	custom_pt = chan_mode_to_mgcp_bss_pt(codec);
+	if (bss_side && custom_pt > 0) {
+		verb_info->ptmap[0].codec = codec;
+	        verb_info->ptmap[0].pt = custom_pt;
+	        verb_info->ptmap_len = 1;
+	}
 }
diff --git a/src/osmo-bsc/osmo_bsc_lcls.c b/src/osmo-bsc/osmo_bsc_lcls.c
index 0482661..4639c4e 100644
--- a/src/osmo-bsc/osmo_bsc_lcls.c
+++ b/src/osmo-bsc/osmo_bsc_lcls.c
@@ -242,7 +242,7 @@
 		.port = conn->user_plane.msc_assigned_rtp_port,
 	};
 	osmo_strlcpy(mdcx_info.addr, conn->user_plane.msc_assigned_rtp_addr, sizeof(mdcx_info.addr));
-	mgcp_pick_codec(&mdcx_info, conn->lchan);
+	mgcp_pick_codec(&mdcx_info, conn->lchan, false);
 
 	mgw_endpoint_ci_request(conn->user_plane.mgw_endpoint_ci_msc,
 				MGCP_VERB_MDCX, &mdcx_info,
@@ -573,7 +573,7 @@
 	mdcx_info = *other_mgw_info;
 	/* Make sure the request doesn't want to use the other side's endpoint string. */
 	mdcx_info.endpoint[0] = 0;
-	mgcp_pick_codec(&mdcx_info, conn->lchan);
+	mgcp_pick_codec(&mdcx_info, conn->lchan, false);
 	mgw_endpoint_ci_request(conn->user_plane.mgw_endpoint_ci_msc,
 				MGCP_VERB_MDCX, &mdcx_info,
 				NULL, 0, 0, NULL);