changeset 205:0047c4c08d9e

mgw: accept the new TRAUlike RTP format in addition to standard, old BFI and zero-length payload formats
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 06 Apr 2023 19:50:09 -0800
parents de7c64c4d6fd
children c76f42e0cd3f
files include/codec_defs.h mgw/dtmf_ctrl.c mgw/gsm2pstn.c mgw/int_defs.h mgw/mdcx.c mgw/pstn2gsm.c mgw/struct.h
diffstat 7 files changed, 51 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/codec_defs.h	Thu Apr 06 19:50:09 2023 -0800
@@ -0,0 +1,6 @@
+/*
+ * This header file holds some definitions for the codecs we work with.
+ */
+
+#define	GSM_FR_BYTES	33
+#define	GSM_EFR_BYTES	31
--- a/mgw/dtmf_ctrl.c	Tue Apr 04 21:21:04 2023 -0800
+++ b/mgw/dtmf_ctrl.c	Thu Apr 06 19:50:09 2023 -0800
@@ -16,7 +16,6 @@
 #include "../include/tmgw_ctrl.h"
 #include "../include/tmgw_const.h"
 #include "struct.h"
-#include "int_defs.h"
 #include "dtmf_defs.h"
 
 extern struct endpoint *find_ep_by_id();
--- a/mgw/gsm2pstn.c	Tue Apr 04 21:21:04 2023 -0800
+++ b/mgw/gsm2pstn.c	Thu Apr 06 19:50:09 2023 -0800
@@ -17,6 +17,7 @@
 #include <gsm.h>	/* libgsm dependency */
 #include <gsm_fr_preproc.h>
 #include <gsm_efr.h>
+#include "../include/codec_defs.h"
 #include "../include/rtp_defs.h"
 #include "../include/tmgw_ctrl.h"
 #include "../include/tmgw_const.h"
@@ -41,8 +42,9 @@
 	unsigned pktsize;
 	int16_t seq_delta;
 	int32_t ts_delta;
+	uint8_t codec_frame[GSM_FR_BYTES];
 	int16_t pcm_samples[SAMPLES_PER_FRAME];
-	int rc, bfi, taf, m_out;
+	int rc, bfi, bfi_nodata, taf, m_out;
 
 	addrlen = sizeof(struct sockaddr_in);
 	rc = recvfrom(in_fd, &pkt, sizeof pkt, 0,
@@ -70,16 +72,38 @@
 		goto bad_rtp_pkt;
 	if ((pkt.m_pt & 0x7F) != ep->gsm_payload_type)
 		goto bad_rtp_pkt;
-	if (pktsize == ep->gsm_rtp_pkt_size &&
-	    (pkt.payload[0] & 0xF0) == ep->gsm_payload_magic) {
-		bfi = 0;
+	pktsize -= RTP_PACKET_HDR_SIZE;
+	if (!pktsize) {
+		/* zero length payload */
+		bfi = bfi_nodata = 1;
+		taf = 0;
+	} else if (pktsize == ep->gsm_payload_len &&
+		   (pkt.payload[0] & 0xF0) == ep->gsm_payload_magic) {
+		/* standard RFC 3551 or TS 101 318 payload */
+		bfi = bfi_nodata = 0;
 		taf = 0;
-	} else if (pktsize == RTP_PACKET_SIZE_BFI && pkt.payload[0] == 0xBF) {
-		bfi = 1;
+		bcopy(pkt.payload, codec_frame, pktsize);
+	} else if (pktsize == 2 && pkt.payload[0] == 0xBF) {
+		/* old BFI marker */
+		bfi = bfi_nodata = 1;
 		taf = pkt.payload[1] & 1;
-	} else if (pktsize == RTP_PACKET_HDR_SIZE) {
-		bfi = 1;
-		taf = 0;
+	} else if ((pkt.payload[0] & 0xF0) == 0xE0) {
+		/* new TRAUlike RTP payload format */
+		bfi_nodata = (pkt.payload[0] & 0x04) >> 2;
+		bfi = (pkt.payload[0] & 0x02) >> 1;
+		taf = pkt.payload[0] & 0x01;
+		if (bfi_nodata) {
+			if (!bfi)
+				goto bad_rtp_pkt;
+			if (pktsize != 1)
+				goto bad_rtp_pkt;
+		} else {
+			if (pktsize != ep->gsm_payload_len + 1)
+				goto bad_rtp_pkt;
+			if ((pkt.payload[1] & 0xF0) != ep->gsm_payload_magic)
+				goto bad_rtp_pkt;
+			bcopy(pkt.payload+1, codec_frame, ep->gsm_payload_len);
+		}
 	} else
 		goto bad_rtp_pkt;
 	if (ep->g2p_state && pkt.ssrc != ep->g2p_ssrc) {
@@ -127,19 +151,19 @@
 	case GSM_TCHF_FRAME:
 		if (bfi)
 			gsmfr_preproc_bfi(ep->gsm_decoder_extra_state, taf,
-					  pkt.payload);
+					  codec_frame);
 		else
 			gsmfr_preproc_good_frame(ep->gsm_decoder_extra_state,
-						 pkt.payload);
-		gsm_decode(ep->gsm_decoder_state, pkt.payload, pcm_samples);
+						 codec_frame);
+		gsm_decode(ep->gsm_decoder_state, codec_frame, pcm_samples);
 		break;
 	case GSM_TCHF_FRAME_EFR:
-		if (bfi)
+		if (bfi_nodata)
 			EFR_decode_bfi_nodata(ep->gsm_decoder_state, taf,
 					      pcm_samples);
 		else
-			EFR_decode_frame(ep->gsm_decoder_state, pkt.payload,
-					 0, 0, pcm_samples);
+			EFR_decode_frame(ep->gsm_decoder_state, codec_frame,
+					 bfi, taf, pcm_samples);
 		break;
 	}
 	if (ep->dtmf_sample_ptr) {
--- a/mgw/int_defs.h	Tue Apr 04 21:21:04 2023 -0800
+++ b/mgw/int_defs.h	Thu Apr 06 19:50:09 2023 -0800
@@ -2,8 +2,4 @@
  * This header file holds miscellaneous internal definitions for themwi-mgw.
  */
 
-#define	RTP_PACKET_SIZE_GSM_FR	45
-#define	RTP_PACKET_SIZE_GSM_EFR	43
-#define	RTP_PACKET_SIZE_BFI	14
-
 #define	SAMPLES_PER_FRAME	160
--- a/mgw/mdcx.c	Tue Apr 04 21:21:04 2023 -0800
+++ b/mgw/mdcx.c	Thu Apr 06 19:50:09 2023 -0800
@@ -12,10 +12,10 @@
 #include <string.h>
 #include <strings.h>
 #include <syslog.h>
+#include "../include/codec_defs.h"
 #include "../include/tmgw_ctrl.h"
 #include "../include/tmgw_const.h"
 #include "struct.h"
-#include "int_defs.h"
 
 extern struct endpoint *find_ep_by_id();
 
@@ -25,11 +25,11 @@
 {
 	switch (ep->gsm_payload_msg_type) {
 	case GSM_TCHF_FRAME:
-		ep->gsm_rtp_pkt_size = RTP_PACKET_SIZE_GSM_FR;
+		ep->gsm_payload_len = GSM_FR_BYTES;
 		ep->gsm_payload_magic = 0xD0;
 		return;
 	case GSM_TCHF_FRAME_EFR:
-		ep->gsm_rtp_pkt_size = RTP_PACKET_SIZE_GSM_EFR;
+		ep->gsm_payload_len = GSM_EFR_BYTES;
 		ep->gsm_payload_magic = 0xC0;
 		return;
 	}
--- a/mgw/pstn2gsm.c	Tue Apr 04 21:21:04 2023 -0800
+++ b/mgw/pstn2gsm.c	Thu Apr 06 19:50:09 2023 -0800
@@ -136,7 +136,8 @@
 		break;
 	}
 	addrlen = sizeof(struct sockaddr_in);
-	sendto(ep->rtp_gsm.rtp_fd, &pkt, ep->gsm_rtp_pkt_size, 0,
+	sendto(ep->rtp_gsm.rtp_fd, &pkt,
+		RTP_PACKET_HDR_SIZE + ep->gsm_payload_len, 0,
 		(struct sockaddr *) &ep->rtp_gsm.remote_addr, addrlen);
 }
 
--- a/mgw/struct.h	Tue Apr 04 21:21:04 2023 -0800
+++ b/mgw/struct.h	Thu Apr 06 19:50:09 2023 -0800
@@ -23,7 +23,7 @@
 	struct rtp_one_end rtp_pstn;
 	unsigned	gsm_payload_type;
 	unsigned	gsm_payload_msg_type;
-	unsigned	gsm_rtp_pkt_size;
+	unsigned	gsm_payload_len;
 	unsigned	gsm_payload_magic;
 	unsigned	pstn_payload_type;
 	unsigned	fwd_mode;