Merge "res_pjsip/res_pjsip_callerid: NULL check on caller id name string"
[asterisk/asterisk.git] / res / res_speech.c
index 46a846f..d6c5329 100644 (file)
  * \author Joshua Colp <jcolp@digium.com>
  */
 
-#include "asterisk.h"
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
+#include "asterisk.h"
 
 #include "asterisk/channel.h"
 #include "asterisk/module.h"
@@ -34,7 +36,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
 #include "asterisk/cli.h"
 #include "asterisk/term.h"
 #include "asterisk/speech.h"
-
+#include "asterisk/format_cache.h"
 
 static AST_RWLIST_HEAD_STATIC(engines, ast_speech_engine);
 static struct ast_speech_engine *default_engine = NULL;
@@ -168,28 +170,45 @@ int ast_speech_change(struct ast_speech *speech, const char *name, const char *v
        return (speech->engine->change ? speech->engine->change(speech, name, value) : -1);
 }
 
+/*! \brief Get an engine specific attribute */
+int ast_speech_get_setting(struct ast_speech *speech, const char *name, char *buf, size_t len)
+{
+       return (speech->engine->get_setting ? speech->engine->get_setting(speech, name, buf, len) : -1);
+}
+
 /*! \brief Create a new speech structure using the engine specified */
-struct ast_speech *ast_speech_new(const char *engine_name, int formats)
+struct ast_speech *ast_speech_new(const char *engine_name, const struct ast_format_cap *cap)
 {
        struct ast_speech_engine *engine = NULL;
        struct ast_speech *new_speech = NULL;
-       int format = AST_FORMAT_SLINEAR;
+       struct ast_format_cap *joint;
+       RAII_VAR(struct ast_format *, best, NULL, ao2_cleanup);
 
        /* Try to find the speech recognition engine that was requested */
        if (!(engine = find_engine(engine_name)))
                return NULL;
 
-       /* Before even allocating the memory below do some codec negotiation, we choose the best codec possible and fall back to signed linear if possible */
-       if ((format = (engine->formats & formats)))
-               format = ast_best_codec(format);
-       else if ((engine->formats & AST_FORMAT_SLINEAR))
-               format = AST_FORMAT_SLINEAR;
-       else
+       joint = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+       if (!joint) {
                return NULL;
+       }
+
+       ast_format_cap_get_compatible(engine->formats, cap, joint);
+       best = ast_format_cap_get_format(joint, 0);
+       ao2_ref(joint, -1);
+
+       if (!best) {
+               if (ast_format_cap_iscompatible_format(engine->formats, ast_format_slin) != AST_FORMAT_CMP_NOT_EQUAL) {
+                       best = ao2_bump(ast_format_slin);
+               } else {
+                       return NULL;
+               }
+       }
 
        /* Allocate our own speech structure, and try to allocate a structure from the engine too */
-       if (!(new_speech = ast_calloc(1, sizeof(*new_speech))))
+       if (!(new_speech = ast_calloc(1, sizeof(*new_speech)))) {
                return NULL;
+       }
 
        /* Initialize the lock */
        ast_mutex_init(&new_speech->lock);
@@ -201,13 +220,13 @@ struct ast_speech *ast_speech_new(const char *engine_name, int formats)
        new_speech->engine = engine;
 
        /* Can't forget the format audio is going to be in */
-       new_speech->format = format;
+       new_speech->format = best;
 
        /* We are not ready to accept audio yet */
        ast_speech_change_state(new_speech, AST_SPEECH_STATE_NOT_READY);
 
        /* Pass ourselves to the engine so they can set us up some more and if they error out then do not create a structure */
-       if (engine->create(new_speech, format)) {
+       if (engine->create(new_speech, best)) {
                ast_mutex_destroy(&new_speech->lock);
                ast_free(new_speech);
                new_speech = NULL;
@@ -235,6 +254,8 @@ int ast_speech_destroy(struct ast_speech *speech)
        if (speech->processing_sound)
                ast_free(speech->processing_sound);
 
+       ao2_ref(speech->format, -1);
+
        /* Aloha we are done */
        ast_free(speech);
 
@@ -269,7 +290,6 @@ int ast_speech_change_results_type(struct ast_speech *speech, enum ast_speech_re
 /*! \brief Register a speech recognition engine */
 int ast_speech_register(struct ast_speech_engine *engine)
 {
-       struct ast_speech_engine *existing_engine = NULL;
        int res = 0;
 
        /* Confirm the engine meets the minimum API requirements */
@@ -279,7 +299,7 @@ int ast_speech_register(struct ast_speech_engine *engine)
        }
 
        /* If an engine is already loaded with this name, error out */
-       if ((existing_engine = find_engine(engine->name))) {
+       if (find_engine(engine->name)) {
                ast_log(LOG_WARNING, "Speech recognition engine '%s' already exists.\n", engine->name);
                return -1;
        }
@@ -340,7 +360,8 @@ static int load_module(void)
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Generic Speech Recognition API",
-               .load = load_module,
-               .unload = unload_module,
-               .load_pri = AST_MODPRI_APP_DEPEND,
-               );
+       .support_level = AST_MODULE_SUPPORT_CORE,
+       .load = load_module,
+       .unload = unload_module,
+       .load_pri = AST_MODPRI_APP_DEPEND,
+);