Merge "contrib/sip_to_pjsip: handle setvar in conversion"
[asterisk/asterisk.git] / funcs / func_speex.c
index 51cea99..1a88fae 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (C) 2008, Digium, Inc.
  *
  * Brian Degenhardt <bmd@digium.com>
- * Brett Bryant <bbryant@digium.com> 
+ * Brett Bryant <bbryant@digium.com>
  *
  * See http://www.asterisk.org for more information about
  * the Asterisk project. Please do not directly contact
  *
  * \brief Noise reduction and automatic gain control (AGC)
  *
- * \author Brian Degenhardt <bmd@digium.com> 
- * \author Brett Bryant <bbryant@digium.com> 
+ * \author Brian Degenhardt <bmd@digium.com>
+ * \author Brett Bryant <bbryant@digium.com>
  *
  * \ingroup functions
  *
- * \extref The Speex 1.2 library - http://www.speex.org
+ * The Speex 1.2 library - http://www.speex.org
  * \note Requires the 1.2 version of the Speex library (which might not be what you find in Linux packages)
  */
 
 /*** MODULEINFO
        <depend>speex</depend>
        <depend>speex_preprocess</depend>
-       <use>speexdsp</use>
+       <use type="external">speexdsp</use>
+       <support_level>core</support_level>
  ***/
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include <speex/speex_preprocess.h>
 #include "asterisk/module.h"
 #include "asterisk/channel.h"
@@ -64,7 +63,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        channel that it is executed on. Using <literal>rx</literal> for audio received
                        and <literal>tx</literal> for audio transmitted to the channel. When using this
                        function you set a target audio level. It is primarily intended for use with
-                       analog lines, but could be useful for other channels as well. The target volume 
+                       analog lines, but could be useful for other channels as well. The target volume
                        is set with a number between <literal>1-32768</literal>. The larger the number
                        the louder (more gain) the channel will receive.</para>
                        <para>Examples:</para>
@@ -78,7 +77,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                </synopsis>
                <syntax>
                        <parameter name="channeldirection" required="true">
-                               <para>This can be either <literal>rx</literal> or <literal>tx</literal> 
+                               <para>This can be either <literal>rx</literal> or <literal>tx</literal>
                                the values that can be set to this are either <literal>on</literal> and
                                <literal>off</literal></para>
                        </parameter>
@@ -109,7 +108,7 @@ struct speex_info {
        struct speex_direction_info *tx, *rx;
 };
 
-static void destroy_callback(void *data) 
+static void destroy_callback(void *data)
 {
        struct speex_info *si = data;
 
@@ -164,8 +163,8 @@ static int speex_callback(struct ast_audiohook *audiohook, struct ast_channel *c
                return -1;
        }
 
-       if ((sdi->samples != frame->samples) || (ast_format_rate(&frame->subclass.format) != si->lastrate)) {
-               si->lastrate = ast_format_rate(&frame->subclass.format);
+       if ((sdi->samples != frame->samples) || (ast_format_get_sample_rate(frame->subclass.format) != si->lastrate)) {
+               si->lastrate = ast_format_get_sample_rate(frame->subclass.format);
                if (sdi->state) {
                        speex_preprocess_state_destroy(sdi->state);
                }
@@ -201,6 +200,16 @@ static int speex_write(struct ast_channel *chan, const char *cmd, char *data, co
        struct speex_direction_info **sdi = NULL;
        int is_new = 0;
 
+       if (!chan) {
+               ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
+               return -1;
+       }
+
+       if (strcasecmp(data, "rx") && strcasecmp(data, "tx")) {
+               ast_log(LOG_ERROR, "Invalid argument provided to the %s function\n", cmd);
+               return -1;
+       }
+
        ast_channel_lock(chan);
        if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) {
                ast_channel_unlock(chan);
@@ -225,15 +234,8 @@ static int speex_write(struct ast_channel *chan, const char *cmd, char *data, co
 
        if (!strcasecmp(data, "rx")) {
                sdi = &si->rx;
-       } else if (!strcasecmp(data, "tx")) {
-               sdi = &si->tx;
        } else {
-               ast_log(LOG_ERROR, "Invalid argument provided to the %s function\n", cmd);
-
-               if (is_new) {
-                       ast_datastore_free(datastore);
-                       return -1;
-               }
+               sdi = &si->tx;
        }
 
        if (!*sdi) {
@@ -249,13 +251,13 @@ static int speex_write(struct ast_channel *chan, const char *cmd, char *data, co
        if (!strcasecmp(cmd, "agc")) {
                if (!sscanf(value, "%30f", &(*sdi)->agclevel))
                        (*sdi)->agclevel = ast_true(value) ? DEFAULT_AGC_LEVEL : 0.0;
-       
+
                if ((*sdi)->agclevel > 32768.0) {
-                       ast_log(LOG_WARNING, "AGC(%s)=%.01f is greater than 32768... setting to 32768 instead\n", 
+                       ast_log(LOG_WARNING, "AGC(%s)=%.01f is greater than 32768... setting to 32768 instead\n",
                                        ((*sdi == si->rx) ? "rx" : "tx"), (*sdi)->agclevel);
                        (*sdi)->agclevel = 32768.0;
                }
-       
+
                (*sdi)->agc = !!((*sdi)->agclevel);
 
                if ((*sdi)->state) {
@@ -290,11 +292,11 @@ static int speex_write(struct ast_channel *chan, const char *cmd, char *data, co
                        ast_audiohook_remove(chan, &si->audiohook);
                        ast_audiohook_detach(&si->audiohook);
                }
-               
+
                ast_datastore_free(datastore);
        }
 
-       if (is_new) { 
+       if (is_new) {
                datastore->data = si;
                ast_channel_lock(chan);
                ast_channel_datastore_add(chan, datastore);