mgcp: Optionally send ptime in SDP

Currently the SDP 'ptime' media attribute is never set in generated
MGCP responses.

This patch optionally includes the 'ptime' attribute if
packet_duration_ms is != 0. This behaviour can be enabled/disabled
by using the VTY command "sdp audio-payload send-ptime" (enabled by
default).

Sponsored-by: On-Waves ehf
diff --git a/openbsc/src/libmgcp/mgcp_protocol.c b/openbsc/src/libmgcp/mgcp_protocol.c
index b8d1920..7d3ad74 100644
--- a/openbsc/src/libmgcp/mgcp_protocol.c
+++ b/openbsc/src/libmgcp/mgcp_protocol.c
@@ -230,11 +230,12 @@
 	const char *addr = endp->cfg->local_ip;
 	const char *fmtp_extra = endp->bts_end.fmtp_extra;
 	char sdp_record[4096];
+	int len;
 
 	if (!addr)
 		addr = endp->cfg->source_addr;
 
-	snprintf(sdp_record, sizeof(sdp_record) - 1,
+	len = snprintf(sdp_record, sizeof(sdp_record) - 1,
 			"I: %u\n\n"
 			"v=0\r\n"
 			"o=- %u 23 IN IP4 %s\r\n"
@@ -247,7 +248,25 @@
 			endp->net_end.local_port, endp->bts_end.payload_type,
 			endp->bts_end.payload_type, endp->tcfg->audio_name,
 			fmtp_extra ? fmtp_extra : "", fmtp_extra ? "\r\n" : "");
+
+	if (len < 0 || len >= sizeof(sdp_record))
+		goto buffer_too_small;
+
+	if (endp->bts_end.packet_duration_ms > 0 && endp->tcfg->audio_send_ptime) {
+		int nchars = snprintf(sdp_record + len, sizeof(sdp_record) - len,
+				      "a=ptime:%d\r\n",
+				      endp->bts_end.packet_duration_ms);
+		if (nchars < 0 || nchars >= sizeof(sdp_record) - len)
+			goto buffer_too_small;
+
+		len += nchars;
+	}
 	return create_resp(endp, 200, " OK", msg, trans_id, NULL, sdp_record);
+
+buffer_too_small:
+	LOGP(DMGCP, LOGL_ERROR, "SDP buffer too small: %d (needed %d)\n",
+	     sizeof(sdp_record), len);
+	return NULL;
 }
 
 /*
@@ -1109,6 +1128,7 @@
 	cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
 	cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
 	cfg->trunk.audio_payload = 126;
+	cfg->trunk.audio_send_ptime = 1;
 	cfg->trunk.omit_rtcp = 0;
 
 	INIT_LLIST_HEAD(&cfg->trunks);
@@ -1131,6 +1151,7 @@
 	trunk->trunk_nr = nr;
 	trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
 	trunk->audio_payload = 126;
+	trunk->audio_send_ptime = 1;
 	trunk->number_endpoints = 33;
 	trunk->omit_rtcp = 0;
 	llist_add_tail(&trunk->entry, &cfg->trunks);