From e00ac3b227d586296e4e043ae9b6500fde0e539f Mon Sep 17 00:00:00 2001 From: Matan Perelman Date: Tue, 28 Mar 2023 08:32:17 +0300 Subject: [PATCH] fmt_rtp_amr,fmt_rtp_efr: replace damaged packets with silence Change-Id: I7245aa0bc0955cc8b94d5401a15e694f50498093 --- src/fmt_rtp_amr.c | 9 +++++++++ src/fmt_rtp_efr.c | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/fmt_rtp_amr.c b/src/fmt_rtp_amr.c index a6a3c9f..4982c83 100644 --- a/src/fmt_rtp_amr.c +++ b/src/fmt_rtp_amr.c @@ -27,6 +27,9 @@ #include #include +/* AMR SID frame is according to TS 126 101 4.2.3 */ +static const uint8_t SILENCE[] = {0x80, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00}; + /* conversion function: RTP payload -> canonical format */ static int rtp_amr_from_canon(uint8_t *dst, const uint8_t *src, unsigned int src_len) @@ -42,6 +45,12 @@ rtp_amr_from_canon(uint8_t *dst, const uint8_t *src, unsigned int src_len) static int rtp_amr_to_canon(uint8_t *dst, const uint8_t *src, unsigned int src_len) { + /* Detect severely damaged frames according to RFC4867 4.3.2 */ + if ((src[1] & 0x04) == 0) { + src = SILENCE; + src_len = sizeof(SILENCE); + } + /* skip Payload Header according to RFC4867 4.4.1 */ memcpy(dst, src+1, src_len-1); diff --git a/src/fmt_rtp_efr.c b/src/fmt_rtp_efr.c index 7afb1a3..214010e 100644 --- a/src/fmt_rtp_efr.c +++ b/src/fmt_rtp_efr.c @@ -30,6 +30,17 @@ #define EFR_LEN 31 #define EFR_MAGIC 0xc +/* EFR encoding starts with 0xc, if all other bits are 0 the packet is probably damaged */ +static const uint8_t DAMAGED_PACKET[EFR_LEN] = { + 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; +/* EFR SID frame is according to TS 101 318 5.3.2 */ +static const uint8_t SILENCE[EFR_LEN] = { + 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + /* conversion function: RTP payload -> canonical format */ static int rtp_efr_from_canon(uint8_t *dst, const uint8_t *src, unsigned int src_len) @@ -58,6 +69,9 @@ rtp_efr_to_canon(uint8_t *dst, const uint8_t *src, unsigned int src_len) return EFR_CANON_LEN; } + if (!memcmp(DAMAGED_PACKET, src, EFR_LEN)) + src = SILENCE; + for (i=0; i<(EFR_LEN-1); i++) dst[i] = (src[i] << 4) | (src[i+1] >> 4); dst[EFR_LEN-1] = src[EFR_LEN-1] << 4;