[Asterisk-code-review] dsp: Translate non-PCM codecs if necessary (asterisk[master])

N A asteriskteam at digium.com
Tue Jan 25 14:01:58 CST 2022


N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/17944 )


Change subject: dsp: Translate non-PCM codecs if necessary
......................................................................

dsp: Translate non-PCM codecs if necessary

The DSP engine in Asterisk is currently only compatible
with PCM codecs (G.711 ulaw and alaw) and signed linear
audio. This usually isn't an issue, but this prevents
codecs like G.722 from being used if any kind of DSP
might be needed during the call, and the historical
restriction to PCM codecs is arbitrary in this regard.

This will now attempt to translate the current codec
to slin audio before bailing out immediately, as it
does now. This is only done on the processed frames,
as opposed to changing the codec used on the channel
itself.

Additionally, a misleading error message is updated
to provide a more accurate error to the user.

ASTERISK-29862 #close

Change-Id: I59740c7acc9c73b4dc20616a967be2b961856bbc
---
M main/dsp.c
1 file changed, 43 insertions(+), 4 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/44/17944/1

diff --git a/main/dsp.c b/main/dsp.c
index 8b4e3ee..47d5462 100644
--- a/main/dsp.c
+++ b/main/dsp.c
@@ -68,6 +68,7 @@
 #include "asterisk/options.h"
 #include "asterisk/config.h"
 #include "asterisk/test.h"
+#include "asterisk/translate.h"
 
 /*! Number of goertzels for progress detect */
 enum gsamp_size {
@@ -437,6 +438,7 @@
 	digit_detect_state_t digit_state;
 	tone_detect_state_t cng_tone_state;
 	tone_detect_state_t ced_tone_state;
+	struct ast_trans_pvt *trans_pvt; /*! Used to convert non-PCM codecs to SLIN if needed */
 };
 
 static void mute_fragment(struct ast_dsp *dsp, fragment_t *fragment)
@@ -1529,12 +1531,44 @@
 			shortdata[x] = AST_ALAW(odata[x]);
 		}
 	} else {
-		/*Display warning only once. Otherwise you would get hundreds of warnings every second */
+		int translated = 0;
+
 		if (dsp->display_inband_dtmf_warning) {
-			ast_log(LOG_WARNING, "Inband DTMF is not supported on codec %s. Use RFC2833\n", ast_format_get_name(af->subclass.format));
+			if (!dsp->trans_pvt) {
+				/* try to build a path from whatever codec is being used to slin */
+				if (!(dsp->trans_pvt = ast_translator_build_path(ast_format_cache_get_slin_by_rate(DEFAULT_SAMPLE_RATE), af->subclass.format))) {
+					ast_log(LOG_WARNING, "Cannot build a path from %s (%u) to signed linear\n",
+						ast_format_get_name(af->subclass.format),
+						ast_format_get_codec_id(af->subclass.format));
+				} else {
+					ast_debug(1, "Successfully created translation path from %s (%u) to signed linear for DSP processing\n",
+						ast_format_get_name(af->subclass.format),
+						ast_format_get_codec_id(af->subclass.format));
+				}
+			}
+			if (dsp->trans_pvt) {
+				/* Convert to slin, and allow the original frame to be freed */
+				af = ast_translate(dsp->trans_pvt, af, 1);
+				/* update frame variables */
+				odata = af->data.ptr;
+				len = af->datalen;
+				if (ast_format_cache_is_slinear(af->subclass.format)) {
+					shortdata = af->data.ptr;
+					len = af->datalen / 2;
+					translated = 1;
+				} else {
+					ast_log(LOG_WARNING, "Codec translated, but not one of the cached slin formats\n");
+				}
+			}
 		}
-		dsp->display_inband_dtmf_warning = 0;
-		return af;
+		if (!translated) {
+			/* Display warning only once. Otherwise you would get hundreds of warnings every second */
+			if (dsp->display_inband_dtmf_warning) {
+				ast_log(LOG_WARNING, "Inband signaling is not supported on codec %s. Use RFC2833 or switch to G.711 codec\n", ast_format_get_name(af->subclass.format));
+				dsp->display_inband_dtmf_warning = 0;
+			}
+			return af;
+		}
 	}
 
 	/* Initially we do not want to mute anything */
@@ -1741,6 +1775,8 @@
 		ast_dsp_prog_reset(dsp);
 		/* Initialize fax detector */
 		ast_fax_detect_init(dsp);
+		/* Don't immediately build a translation path */
+		dsp->trans_pvt = NULL;
 	}
 	return dsp;
 }
@@ -1772,6 +1808,9 @@
 
 void ast_dsp_free(struct ast_dsp *dsp)
 {
+	if (dsp->trans_pvt) {
+		ast_translator_free_path(dsp->trans_pvt);
+	}
 	ast_free(dsp);
 }
 

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/17944
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I59740c7acc9c73b4dc20616a967be2b961856bbc
Gerrit-Change-Number: 17944
Gerrit-PatchSet: 1
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220125/b884039a/attachment.html>


More information about the asterisk-code-review mailing list