Asterisk - The Open Source Telephony Project GIT-master-f36a736
Data Structures | Macros | Functions | Variables
res_speech_aeap.c File Reference

Asterisk External Application Speech Engine. More...

#include "asterisk.h"
#include "asterisk/astobj2.h"
#include "asterisk/config.h"
#include "asterisk/format.h"
#include "asterisk/format_cap.h"
#include "asterisk/json.h"
#include "asterisk/module.h"
#include "asterisk/speech.h"
#include "asterisk/sorcery.h"
#include "asterisk/res_aeap.h"
#include "asterisk/res_aeap_message.h"
Include dependency graph for res_speech_aeap.c:

Go to the source code of this file.

Data Structures

struct  speech_param
 
struct  speech_setting
 

Macros

#define CONNECTION_TIMEOUT   2000
 
#define log_error(obj, fmt, ...)    ast_log(LOG_ERROR, "AEAP speech (%p): " fmt "\n", obj, ##__VA_ARGS__)
 
#define SPEECH_AEAP_VERSION   "0.1.0"
 
#define SPEECH_PROTOCOL   "speech_to_text"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static void ast_aeap_speech_on_error (struct ast_aeap *aeap)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static struct ast_jsoncustom_fields_to_params (const struct ast_variable *variables)
 
static int handle_request_set (struct ast_aeap *aeap, struct ast_aeap_message *message, void *data)
 
static int handle_response_get (struct ast_aeap *aeap, struct ast_aeap_message *message, void *data)
 
static int handle_response_set (struct ast_aeap *aeap, struct ast_aeap_message *message, void *data)
 
static int handle_response_setup (struct ast_aeap *aeap, struct ast_aeap_message *message, void *data)
 
static int handle_results (struct ast_aeap *aeap, struct ast_json_iter *iter, struct ast_speech_result **speech_results)
 
static int handle_setting (struct ast_aeap *aeap, struct ast_json_iter *iter, struct speech_setting *setting)
 
static int load_engine (void *obj, void *arg, int flags)
 
static int load_module (void)
 
static int matches_engine (void *obj, void *arg, int flags)
 
static int should_unregister (const struct ast_speech_engine *engine, void *data)
 
static int speech_aeap_engine_change (struct ast_speech *speech, const char *name, const char *value)
 
static int speech_aeap_engine_change_results_type (struct ast_speech *speech, enum ast_speech_results_type results_type)
 
static int speech_aeap_engine_create (struct ast_speech *speech, struct ast_format *format)
 
static int speech_aeap_engine_destroy (struct ast_speech *speech)
 
static int speech_aeap_engine_dtmf (struct ast_speech *speech, const char *dtmf)
 
static struct ast_speech_resultspeech_aeap_engine_get (struct ast_speech *speech)
 
static int speech_aeap_engine_get_setting (struct ast_speech *speech, const char *name, char *buf, size_t len)
 
static int speech_aeap_engine_start (struct ast_speech *speech)
 
static int speech_aeap_engine_write (struct ast_speech *speech, void *data, int len)
 
static int speech_aeap_get (struct ast_speech *speech, const char *param, void *data)
 
static int speech_aeap_send_request (struct ast_aeap *aeap, const char *name, struct ast_json *json, void *data)
 
static int speech_aeap_set (struct ast_speech *speech, const char *name, const char *value)
 
static struct ast_speech_enginespeech_engine_alloc (const char *name)
 
static void speech_engine_alloc_and_register (const char *name, const struct ast_format_cap *formats)
 
static void speech_engine_destroy (void *obj)
 
static void speech_observer_loaded (const char *object_type)
 
static int unload_engine (void *obj, void *arg, int flags)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Asterisk External Application Speech Engine" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CHANNEL_DEPEND, .requires = "res_speech,res_aeap", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static const struct ast_aeap_message_handler request_handlers []
 
static const struct ast_aeap_message_handler response_handlers []
 
static struct ast_aeap_params speech_aeap_params
 
static const struct ast_sorcery_observer speech_observer
 Observer for AEAP reloads. More...
 

Detailed Description

Asterisk External Application Speech Engine.

Definition in file res_speech_aeap.c.

Macro Definition Documentation

◆ CONNECTION_TIMEOUT

#define CONNECTION_TIMEOUT   2000

Definition at line 46 of file res_speech_aeap.c.

◆ log_error

#define log_error (   obj,
  fmt,
  ... 
)     ast_log(LOG_ERROR, "AEAP speech (%p): " fmt "\n", obj, ##__VA_ARGS__)

Definition at line 48 of file res_speech_aeap.c.

◆ SPEECH_AEAP_VERSION

#define SPEECH_AEAP_VERSION   "0.1.0"

Definition at line 43 of file res_speech_aeap.c.

◆ SPEECH_PROTOCOL

#define SPEECH_PROTOCOL   "speech_to_text"

Definition at line 44 of file res_speech_aeap.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 763 of file res_speech_aeap.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 763 of file res_speech_aeap.c.

◆ ast_aeap_speech_on_error()

static void ast_aeap_speech_on_error ( struct ast_aeap aeap)
static

Definition at line 359 of file res_speech_aeap.c.

360{
361 struct ast_speech *speech = ast_aeap_user_data_object_by_id(aeap, "speech");
362 if (!speech) {
363 ast_log(LOG_ERROR, "aeap generated error with no associated speech object");
364 return;
365 }
366
368}
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
void * ast_aeap_user_data_object_by_id(struct ast_aeap *aeap, const char *id)
Retrieve a registered user data object by its id.
Definition: aeap.c:174
int ast_speech_change_state(struct ast_speech *speech, int state)
Change state of a speech structure.
Definition: res_speech.c:278
@ AST_SPEECH_STATE_DONE
Definition: speech.h:42

References ast_aeap_user_data_object_by_id(), ast_log, ast_speech_change_state(), AST_SPEECH_STATE_DONE, and LOG_ERROR.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 763 of file res_speech_aeap.c.

◆ custom_fields_to_params()

static struct ast_json * custom_fields_to_params ( const struct ast_variable variables)
static

Definition at line 51 of file res_speech_aeap.c.

52{
53 const struct ast_variable *i;
54 struct ast_json *obj;
55
56 if (!variables) {
57 return NULL;
58 }
59
61 if (!obj) {
62 return NULL;
63 }
64
65 for (i = variables; i; i = i->next) {
66 if (i->name[0] == '@' && i->name[1]) {
68 }
69 }
70
71 return obj;
72}
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:278
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
Definition: json.c:399
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
Set a field in a JSON object.
Definition: json.c:414
#define NULL
Definition: resample.c:96
Abstract JSON element (object, array, string, int, ...).
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next

References ast_json_object_create(), ast_json_object_set(), ast_json_string_create(), ast_variable::name, ast_variable::next, NULL, and ast_variable::value.

Referenced by speech_aeap_engine_create().

◆ handle_request_set()

static int handle_request_set ( struct ast_aeap aeap,
struct ast_aeap_message message,
void *  data 
)
static

Definition at line 313 of file res_speech_aeap.c.

314{
315 struct ast_json_iter *iter;
316 const char *error_msg = NULL;
317
319 if (!iter) {
320 error_msg = "no parameter(s) requested";
321 } else if (!strcmp(ast_json_object_iter_key(iter), "results")) {
322 struct ast_speech *speech = ast_aeap_user_data_object_by_id(aeap, "speech");
323
324 if (!speech) {
325 error_msg = "no associated speech object";
326 } else if (handle_results(aeap, iter, &speech->results)) {
327 error_msg = "unable to handle results";
328 } else {
330 }
331 } else {
332 error_msg = "can only set 'results'";
333 }
334
335 if (error_msg) {
336 log_error(aeap, "set - %s", error_msg);
339 } else {
342 }
343
345
346 return 0;
347}
struct ast_json_iter * ast_json_object_iter(struct ast_json *object)
Get an iterator pointing to the first field in a JSON object.
Definition: json.c:439
const char * ast_json_object_iter_key(struct ast_json_iter *iter)
Get the key from an iterator.
Definition: json.c:451
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:407
int ast_aeap_send_msg(struct ast_aeap *aeap, struct ast_aeap_message *msg)
Send a message to an external application.
Definition: aeap.c:439
struct ast_aeap_message * ast_aeap_message_create_error(const struct ast_aeap_message_type *type, const char *name, const char *id, const char *error_msg)
Create an Asterisk external application error response object.
const char * ast_aeap_message_id(const struct ast_aeap_message *message)
Retrieve a message id.
const char * ast_aeap_message_name(const struct ast_aeap_message *message)
Retrieve a message name.
const struct ast_aeap_message_type * ast_aeap_message_type_json
Asterisk external application JSON message type.
Definition: message_json.c:191
struct ast_aeap_message * ast_aeap_message_create_response(const struct ast_aeap_message_type *type, const char *name, const char *id, const void *params)
Create an Asterisk external application response object.
void * ast_aeap_message_data(struct ast_aeap_message *message)
Retrieve the core message data/body.
static int handle_results(struct ast_aeap *aeap, struct ast_json_iter *iter, struct ast_speech_result **speech_results)
#define log_error(obj, fmt,...)
Iterator for JSON object key/values.
struct ast_speech_result * results
Definition: speech.h:68

References ast_aeap_message_create_error(), ast_aeap_message_create_response(), ast_aeap_message_data(), ast_aeap_message_id(), ast_aeap_message_name(), ast_aeap_message_type_json, ast_aeap_send_msg(), ast_aeap_user_data_object_by_id(), ast_json_object_get(), ast_json_object_iter(), ast_json_object_iter_key(), ast_speech_change_state(), AST_SPEECH_STATE_DONE, handle_results(), log_error, NULL, and ast_speech::results.

◆ handle_response_get()

static int handle_response_get ( struct ast_aeap aeap,
struct ast_aeap_message message,
void *  data 
)
static

Definition at line 259 of file res_speech_aeap.c.

260{
261 struct ast_json_iter *iter;
262
264 if (!iter) {
265 log_error(aeap, "no 'get' parameters returned");
266 return -1;
267 }
268
269 if (!strcmp(ast_json_object_iter_key(iter), "results")) {
270 return handle_results(aeap, iter, data);
271 }
272
273 return handle_setting(aeap, iter, data);
274}
static int handle_setting(struct ast_aeap *aeap, struct ast_json_iter *iter, struct speech_setting *setting)

References ast_aeap_message_data(), ast_json_object_get(), ast_json_object_iter(), ast_json_object_iter_key(), handle_results(), handle_setting(), and log_error.

◆ handle_response_set()

static int handle_response_set ( struct ast_aeap aeap,
struct ast_aeap_message message,
void *  data 
)
static

Definition at line 173 of file res_speech_aeap.c.

174{
175 return 0;
176}

◆ handle_response_setup()

static int handle_response_setup ( struct ast_aeap aeap,
struct ast_aeap_message message,
void *  data 
)
static

Definition at line 276 of file res_speech_aeap.c.

277{
278 struct ast_format *format = data;
279 struct ast_json *json = ast_aeap_message_data(message);
280 const char *codec_name;
281
282 if (!format) {
283 log_error(aeap, "no 'format' set");
284 return -1;
285 }
286
287 if (!json) {
288 log_error(aeap, "no 'setup' object returned");
289 return -1;
290 }
291
292 json = ast_json_object_get(json, "codecs");
293 if (!json || ast_json_array_size(json) == 0) {
294 log_error(aeap, "no 'setup' codecs available");
295 return -1;
296 }
297
298 codec_name = ast_json_object_string_get(ast_json_array_get(json, 0), "name");
299 if (!codec_name || strcmp(codec_name, ast_format_get_codec_name(format))) {
300 log_error(aeap, "setup codec '%s' unsupported", ast_format_get_codec_name(format));
301 return -1;
302 }
303
304 return 0;
305}
const char * ast_format_get_codec_name(const struct ast_format *format)
Get the codec name associated with a format.
Definition: format.c:339
#define ast_json_object_string_get(object, key)
Get a string field from a JSON object.
Definition: json.h:600
struct ast_json * ast_json_array_get(const struct ast_json *array, size_t index)
Get an element from an array.
Definition: json.c:370
size_t ast_json_array_size(const struct ast_json *array)
Get the size of a JSON array.
Definition: json.c:366
Definition of a media format.
Definition: format.c:43

References ast_aeap_message_data(), ast_format_get_codec_name(), ast_json_array_get(), ast_json_array_size(), ast_json_object_get(), ast_json_object_string_get, and log_error.

◆ handle_results()

static int handle_results ( struct ast_aeap aeap,
struct ast_json_iter iter,
struct ast_speech_result **  speech_results 
)
static

Definition at line 204 of file res_speech_aeap.c.

206{
207 struct ast_speech_result *result = NULL;
208 struct ast_json *json_results;
209 struct ast_json *json_result;
210 size_t i;
211
212 json_results = ast_json_object_iter_value(iter);
213 if (!json_results || !speech_results) {
214 log_error(aeap, "Unable to 'get' speech results");
215 return -1;
216 }
217
218 for (i = 0; i < ast_json_array_size(json_results); ++i) {
219 if (!(result = ast_calloc(1, sizeof(*result)))) {
220 continue;
221 }
222
223 json_result = ast_json_array_get(json_results, i);
224
225 result->text = ast_strdup(ast_json_object_string_get(json_result, "text"));
226 result->score = ast_json_object_integer_get(json_result, "score");
227 result->grammar = ast_strdup(ast_json_object_string_get(json_result, "grammar"));
228 result->nbest_num = ast_json_object_integer_get(json_result, "best");
229 if (*speech_results) {
230 AST_LIST_NEXT(result, list) = *speech_results;
231 *speech_results = result;
232 } else {
233 *speech_results = result;
234 }
235 }
236
237 return 0;
238}
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
static PGresult * result
Definition: cel_pgsql.c:84
struct ast_json * ast_json_object_iter_value(struct ast_json_iter *iter)
Get the value from an iterator.
Definition: json.c:455
#define ast_json_object_integer_get(object, key)
Get an integer field from a JSON object.
Definition: json.h:609
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:439

References ast_calloc, ast_json_array_get(), ast_json_array_size(), ast_json_object_integer_get, ast_json_object_iter_value(), ast_json_object_string_get, AST_LIST_NEXT, ast_strdup, log_error, NULL, and result.

Referenced by handle_request_set(), and handle_response_get().

◆ handle_setting()

static int handle_setting ( struct ast_aeap aeap,
struct ast_json_iter iter,
struct speech_setting setting 
)
static

Definition at line 184 of file res_speech_aeap.c.

186{
187 const char *value;
188
189 if (strcmp(ast_json_object_iter_key(iter), setting->param)) {
190 log_error(aeap, "Unable to 'get' speech setting for '%s'", setting->param);
191 return -1;
192 }
193
195 if (!value) {
196 log_error(aeap, "No value for speech setting '%s'", setting->param);
197 return -1;
198 }
199
200 ast_copy_string(setting->buf, value, setting->len);
201 return 0;
202}
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:283
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
const char * param
int value
Definition: syslog.c:37

References ast_copy_string(), ast_json_object_iter_key(), ast_json_object_iter_value(), ast_json_string_get(), speech_setting::buf, speech_setting::len, log_error, speech_setting::param, and value.

Referenced by handle_response_get().

◆ load_engine()

static int load_engine ( void *  obj,
void *  arg,
int  flags 
)
static

Definition at line 610 of file res_speech_aeap.c.

611{
612 const char *id;
613 const struct ast_format_cap *formats;
614 const struct ast_speech_engine *engine;
615
617 return 0;
618 }
619
622 if (!formats) {
624 if (!formats) {
625 ast_log(LOG_ERROR, "AEAP speech: unable to allocate default engine format for '%s'\n", id);
626 return 0;
627 }
628 }
629
630 engine = ast_speech_find_engine(id);
631 if (!engine) {
633 return 0;
634 }
635
637 /* Same name, same formats then nothing changed */
638 return 0;
639 }
640
641 ao2_ref(ast_speech_unregister2(engine->name), -1);
643
644 return 0;
645}
enum queue_result id
Definition: app_queue.c:1667
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
static struct formats formats
@ AST_FORMAT_CAP_FLAG_DEFAULT
Definition: format_cap.h:38
int ast_format_cap_identical(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2)
Determine if two capabilities structures are identical.
Definition: format_cap.c:687
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition: format_cap.h:49
int ast_aeap_client_config_has_protocol(const struct ast_aeap_client_config *cfg, const char *protocol)
Check a given protocol against that in an Asterisk external application configuration.
Definition: res_aeap.c:136
const struct ast_format_cap * ast_aeap_client_config_codecs(const struct ast_aeap_client_config *cfg)
Retrieve codec capabilities from the configuration.
Definition: res_aeap.c:131
#define SPEECH_PROTOCOL
static void speech_engine_alloc_and_register(const char *name, const struct ast_format_cap *formats)
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2317
struct ast_speech_engine * ast_speech_find_engine(const char *engine_name)
Retrieve a speech recognition engine.
Definition: res_speech.c:46
struct ast_speech_engine * ast_speech_unregister2(const char *engine_name)
Unregister a speech recognition engine.
Definition: res_speech.c:352
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
struct ast_format_cap * formats
Definition: speech.h:106
char * name
Definition: speech.h:78
Definition: file.c:69

References ao2_ref, ast_aeap_client_config_codecs(), ast_aeap_client_config_has_protocol(), ast_format_cap_alloc, AST_FORMAT_CAP_FLAG_DEFAULT, ast_format_cap_identical(), ast_log, ast_sorcery_object_get_id(), ast_speech_find_engine(), ast_speech_unregister2(), ast_speech_engine::formats, formats, id, LOG_ERROR, ast_speech_engine::name, speech_engine_alloc_and_register(), and SPEECH_PROTOCOL.

Referenced by load_module(), and speech_observer_loaded().

◆ load_module()

static int load_module ( void  )
static

Definition at line 729 of file res_speech_aeap.c.

730{
731 struct ao2_container *container;
732
734
736 if (container) {
738 ao2_ref(container, -1);
739 }
740
741 /*
742 * Add an observer since a named speech server must be created,
743 * registered, and eventually removed for all AEAP client
744 * configuration matching the "speech_to_text" protocol.
745 */
748 }
749
750#ifdef TEST_FRAMEWORK
751 speech_engine_alloc_and_register2("_aeap_test_speech_", "ulaw");
752#endif
753
755}
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container,...
Definition: astobj2.h:1693
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
struct ast_sorcery * ast_aeap_sorcery(void)
Retrieve the AEAP sorcery object.
Definition: res_aeap.c:67
#define AEAP_CONFIG_CLIENT
Definition: res_aeap.h:35
struct ao2_container * ast_aeap_client_configs_get(const char *protocol)
Retrieve a listing of all client configuration objects by protocol.
Definition: res_aeap.c:142
struct ao2_container * container
Definition: res_fax.c:501
static int load_engine(void *obj, void *arg, int flags)
static const struct ast_sorcery_observer speech_observer
Observer for AEAP reloads.
static struct ast_aeap_params speech_aeap_params
int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
Add an observer to a specific object type.
Definition: sorcery.c:2391
Generic container type.
const struct ast_aeap_message_type * msg_type
Definition: res_aeap.h:152

References AEAP_CONFIG_CLIENT, ao2_callback, ao2_ref, ast_aeap_client_configs_get(), ast_aeap_message_type_json, ast_aeap_sorcery(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_sorcery_observer_add(), container, load_engine(), ast_aeap_params::msg_type, NULL, speech_aeap_params, speech_observer, and SPEECH_PROTOCOL.

◆ matches_engine()

static int matches_engine ( void *  obj,
void *  arg,
int  flags 
)
static

Definition at line 647 of file res_speech_aeap.c.

648{
649 const struct ast_speech_engine *engine = arg;
650
651 return strcmp(ast_sorcery_object_get_id(obj), engine->name) ? 0 : CMP_MATCH;
652}
@ CMP_MATCH
Definition: astobj2.h:1027

References ast_sorcery_object_get_id(), CMP_MATCH, and ast_speech_engine::name.

Referenced by should_unregister().

◆ should_unregister()

static int should_unregister ( const struct ast_speech_engine engine,
void *  data 
)
static

Definition at line 654 of file res_speech_aeap.c.

655{
656 void *obj;
657
658 if (engine->create != speech_aeap_engine_create) {
659 /* Only want to potentially unregister AEAP speech engines */
660 return 0;
661 }
662
663#ifdef TEST_FRAMEWORK
664 if (!strcmp("_aeap_test_speech_", engine->name)) {
665 /* Don't remove the test engine */
666 return 0;
667 }
668#endif
669
670 obj = ao2_callback(data, 0, matches_engine, (void*)engine);
671
672 if (obj) {
673 ao2_ref(obj, -1);
674 return 0;
675 }
676
677 /* If no match in given container then unregister engine */
678 return 1;
679}
static int matches_engine(void *obj, void *arg, int flags)
static int speech_aeap_engine_create(struct ast_speech *speech, struct ast_format *format)
int(* create)(struct ast_speech *speech, struct ast_format *format)
Definition: speech.h:80

References ao2_callback, ao2_ref, ast_speech_engine::create, matches_engine(), ast_speech_engine::name, and speech_aeap_engine_create().

Referenced by ast_speech_unregister_engines(), and speech_observer_loaded().

◆ speech_aeap_engine_change()

static int speech_aeap_engine_change ( struct ast_speech speech,
const char *  name,
const char *  value 
)
static

Definition at line 475 of file res_speech_aeap.c.

476{
477 return speech_aeap_set(speech, name, value);
478}
static const char name[]
Definition: format_mp3.c:68
static int speech_aeap_set(struct ast_speech *speech, const char *name, const char *value)

References name, speech_aeap_set(), and value.

Referenced by speech_engine_alloc().

◆ speech_aeap_engine_change_results_type()

static int speech_aeap_engine_change_results_type ( struct ast_speech speech,
enum ast_speech_results_type  results_type 
)
static

Definition at line 492 of file res_speech_aeap.c.

494{
495 return speech_aeap_set(speech, "results_type",
497}
const char * ast_speech_results_type_to_string(enum ast_speech_results_type type)
Convert a speech results type to a string.
Definition: res_speech.c:294

References ast_speech_results_type_to_string(), and speech_aeap_set().

Referenced by speech_engine_alloc().

◆ speech_aeap_engine_create()

static int speech_aeap_engine_create ( struct ast_speech speech,
struct ast_format format 
)
static

Definition at line 403 of file res_speech_aeap.c.

404{
405 struct ast_aeap *aeap;
406 struct ast_variable *vars;
407 struct ast_json *json;
408
411 if (!aeap) {
412 return -1;
413 }
414
415 speech->data = aeap;
416
417 /* Don't allow unloading of this module while an external application is in use */
419
420 vars = ast_aeap_custom_fields_get(speech->engine->name);
421
422 /* While the protocol allows sending of codec attributes, for now don't */
423 json = ast_json_pack("{s:s,s:[{s:s}],s:o*}", "version", SPEECH_AEAP_VERSION, "codecs",
424 "name", ast_format_get_codec_name(format), "params", custom_fields_to_params(vars));
425
427
428 if (ast_aeap_user_data_register(aeap, "speech", speech, NULL)) {
430 return -1;
431 }
432
433 /* send_request handles json ref */
434 if (speech_aeap_send_request(speech->data, "setup", json, format)) {
436 return -1;
437 }
438
439 /*
440 * Add a reference to the engine here, so if it happens to get unregistered
441 * while executing it won't disappear.
442 */
443 ao2_ref(speech->engine, 1);
444
445 return 0;
446}
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1262
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:483
#define ast_module_ref(mod)
Hold a reference to the module.
Definition: module.h:457
struct ast_aeap * ast_aeap_create_and_connect_by_id(const char *id, const struct ast_aeap_params *params, int timeout)
Create and connect to an Asterisk external application by sorcery id.
Definition: res_aeap.c:322
int ast_aeap_user_data_register(struct ast_aeap *aeap, const char *id, void *obj, ast_aeap_user_obj_cleanup cleanup)
Register a user data object.
Definition: aeap.c:150
struct ast_variable * ast_aeap_custom_fields_get(const char *id)
Retrieve a list of custom configuration fields.
Definition: res_aeap.c:328
#define CONNECTION_TIMEOUT
static struct ast_json * custom_fields_to_params(const struct ast_variable *variables)
static int speech_aeap_send_request(struct ast_aeap *aeap, const char *name, struct ast_json *json, void *data)
#define SPEECH_AEAP_VERSION
Definition: aeap.c:47
struct ast_module * self
Definition: module.h:356
void * data
Definition: speech.h:66
struct ast_speech_engine * engine
Definition: speech.h:72

References ao2_ref, ast_aeap_create_and_connect_by_id(), ast_aeap_custom_fields_get(), ast_aeap_user_data_register(), ast_format_get_codec_name(), ast_json_pack(), ast_module_ref, ast_module_unref, ast_variables_destroy(), CONNECTION_TIMEOUT, custom_fields_to_params(), ast_speech::data, ast_speech::engine, ast_speech_engine::name, NULL, ast_module_info::self, speech_aeap_params, speech_aeap_send_request(), and SPEECH_AEAP_VERSION.

Referenced by should_unregister(), and speech_engine_alloc().

◆ speech_aeap_engine_destroy()

static int speech_aeap_engine_destroy ( struct ast_speech speech)
static

Definition at line 448 of file res_speech_aeap.c.

449{
450 ao2_ref(speech->engine, -1);
451 ao2_cleanup(speech->data);
452
454
455 return 0;
456}
#define ao2_cleanup(obj)
Definition: astobj2.h:1934

References ao2_cleanup, ao2_ref, ast_module_unref, ast_speech::data, ast_speech::engine, and ast_module_info::self.

Referenced by speech_engine_alloc().

◆ speech_aeap_engine_dtmf()

static int speech_aeap_engine_dtmf ( struct ast_speech speech,
const char *  dtmf 
)
static

Definition at line 463 of file res_speech_aeap.c.

464{
465 return speech_aeap_set(speech, "dtmf", dtmf);
466}

References speech_aeap_set().

Referenced by speech_engine_alloc().

◆ speech_aeap_engine_get()

static struct ast_speech_result * speech_aeap_engine_get ( struct ast_speech speech)
static

Definition at line 499 of file res_speech_aeap.c.

500{
501 struct ast_speech_result *results = NULL;
502
503 if (speech->results) {
504 return speech->results;
505 }
506
507 if (speech_aeap_get(speech, "results", &results)) {
508 return NULL;
509 }
510
511 return results;
512}
static int speech_aeap_get(struct ast_speech *speech, const char *param, void *data)

References NULL, ast_speech::results, and speech_aeap_get().

Referenced by speech_engine_alloc().

◆ speech_aeap_engine_get_setting()

static int speech_aeap_engine_get_setting ( struct ast_speech speech,
const char *  name,
char *  buf,
size_t  len 
)
static

Definition at line 480 of file res_speech_aeap.c.

482{
483 struct speech_setting setting = {
484 .param = name,
485 .len = len,
486 .buf = buf,
487 };
488
489 return speech_aeap_get(speech, name, &setting);
490}
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)

References buf, len(), name, speech_setting::param, and speech_aeap_get().

Referenced by speech_engine_alloc().

◆ speech_aeap_engine_start()

static int speech_aeap_engine_start ( struct ast_speech speech)
static

Definition at line 468 of file res_speech_aeap.c.

469{
471
472 return 0;
473}
@ AST_SPEECH_STATE_READY
Definition: speech.h:40

References ast_speech_change_state(), and AST_SPEECH_STATE_READY.

Referenced by speech_engine_alloc().

◆ speech_aeap_engine_write()

static int speech_aeap_engine_write ( struct ast_speech speech,
void *  data,
int  len 
)
static

Definition at line 458 of file res_speech_aeap.c.

459{
460 return ast_aeap_send_binary(speech->data, data, len);
461}
int ast_aeap_send_binary(struct ast_aeap *aeap, const void *buf, uintmax_t size)
Send a binary data to an external application.
Definition: aeap.c:434

References ast_aeap_send_binary(), ast_speech::data, and len().

Referenced by speech_engine_alloc().

◆ speech_aeap_get()

static int speech_aeap_get ( struct ast_speech speech,
const char *  param,
void *  data 
)
static

Definition at line 130 of file res_speech_aeap.c.

131{
132 if (!param) {
133 return -1;
134 }
135
136 /* send_request handles json ref */
137 return speech_aeap_send_request(speech->data,
138 "get", ast_json_pack("{s:[s]}", "params", param), data);
139}

References ast_json_pack(), ast_speech::data, and speech_aeap_send_request().

Referenced by speech_aeap_engine_get(), and speech_aeap_engine_get_setting().

◆ speech_aeap_send_request()

static int speech_aeap_send_request ( struct ast_aeap aeap,
const char *  name,
struct ast_json json,
void *  data 
)
static

Definition at line 90 of file res_speech_aeap.c.

92{
93 /*
94 * Wait for a response. Also since we're blocking,
95 * data is expected to be on the stack so no cleanup required.
96 */
97 struct ast_aeap_tsx_params tsx_params = {
98 .timeout = 1000,
99 .wait = 1,
100 .obj = data,
101 };
102
103 /* "steals" the json ref */
106 if (!tsx_params.msg) {
107 return -1;
108 }
109
110 /* Send "steals" the json msg ref */
111 return ast_aeap_send_msg_tsx(aeap, &tsx_params);
112}
int ast_aeap_send_msg_tsx(struct ast_aeap *aeap, struct ast_aeap_tsx_params *params)
Send a transaction based message to an external application using the given parameters.
Definition: aeap.c:464
struct ast_aeap_message * ast_aeap_message_create_request(const struct ast_aeap_message_type *type, const char *name, const char *id, const void *params)
Create an Asterisk external application request object.
Parameters to be used when sending a transaction based message.
Definition: res_aeap.h:331
struct ast_aeap_message * msg
Definition: res_aeap.h:333

References ast_aeap_message_create_request(), ast_aeap_message_type_json, ast_aeap_send_msg_tsx(), ast_aeap_tsx_params::msg, name, NULL, and ast_aeap_tsx_params::timeout.

Referenced by speech_aeap_engine_create(), speech_aeap_get(), and speech_aeap_set().

◆ speech_aeap_set()

static int speech_aeap_set ( struct ast_speech speech,
const char *  name,
const char *  value 
)
static

Definition at line 162 of file res_speech_aeap.c.

163{
164 if (!name) {
165 return -1;
166 }
167
168 /* send_request handles json ref */
169 return speech_aeap_send_request(speech->data,
170 "set", ast_json_pack("{s:{s:s}}", "params", name, value), NULL);
171}

References ast_json_pack(), ast_speech::data, name, NULL, speech_aeap_send_request(), and value.

Referenced by speech_aeap_engine_change(), speech_aeap_engine_change_results_type(), and speech_aeap_engine_dtmf().

◆ speech_engine_alloc()

static struct ast_speech_engine * speech_engine_alloc ( const char *  name)
static

Definition at line 522 of file res_speech_aeap.c.

523{
524 struct ast_speech_engine *engine;
525
526 engine = ao2_t_alloc_options(sizeof(*engine), speech_engine_destroy,
528 if (!engine) {
529 ast_log(LOG_ERROR, "AEAP speech: unable create engine '%s'\n", name);
530 return NULL;
531 }
532
533 engine->name = ast_strdup(name);
534 if (!engine->name) {
535 ao2_ref(engine, -1);
536 return NULL;
537 }
538
547 engine->get = speech_aeap_engine_get;
548
550
551 return engine;
552}
@ AO2_ALLOC_OPT_LOCK_NOLOCK
Definition: astobj2.h:367
#define ao2_t_alloc_options(data_size, destructor_fn, options, debug_msg)
Allocate and initialize an object.
Definition: astobj2.h:402
static int speech_aeap_engine_destroy(struct ast_speech *speech)
static int speech_aeap_engine_change(struct ast_speech *speech, const char *name, const char *value)
static struct ast_speech_result * speech_aeap_engine_get(struct ast_speech *speech)
static int speech_aeap_engine_get_setting(struct ast_speech *speech, const char *name, char *buf, size_t len)
static int speech_aeap_engine_write(struct ast_speech *speech, void *data, int len)
static void speech_engine_destroy(void *obj)
static int speech_aeap_engine_start(struct ast_speech *speech)
static int speech_aeap_engine_dtmf(struct ast_speech *speech, const char *dtmf)
static int speech_aeap_engine_change_results_type(struct ast_speech *speech, enum ast_speech_results_type results_type)
int(* start)(struct ast_speech *speech)
Definition: speech.h:96
int(* change_results_type)(struct ast_speech *speech, enum ast_speech_results_type results_type)
Definition: speech.h:102
struct ast_speech_result *(* get)(struct ast_speech *speech)
Definition: speech.h:104
int(* get_setting)(struct ast_speech *speech, const char *name, char *buf, size_t len)
Definition: speech.h:100
int(* destroy)(struct ast_speech *speech)
Definition: speech.h:82
int(* dtmf)(struct ast_speech *speech, const char *dtmf)
Definition: speech.h:94
int(* change)(struct ast_speech *speech, const char *name, const char *value)
Definition: speech.h:98
int(* write)(struct ast_speech *speech, void *data, int len)
Definition: speech.h:92

References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_ref, ao2_t_alloc_options, ast_format_cap_alloc, AST_FORMAT_CAP_FLAG_DEFAULT, ast_log, ast_strdup, ast_speech_engine::change, ast_speech_engine::change_results_type, ast_speech_engine::create, ast_speech_engine::destroy, ast_speech_engine::dtmf, ast_speech_engine::formats, ast_speech_engine::get, ast_speech_engine::get_setting, LOG_ERROR, name, ast_speech_engine::name, NULL, speech_aeap_engine_change(), speech_aeap_engine_change_results_type(), speech_aeap_engine_create(), speech_aeap_engine_destroy(), speech_aeap_engine_dtmf(), speech_aeap_engine_get(), speech_aeap_engine_get_setting(), speech_aeap_engine_start(), speech_aeap_engine_write(), speech_engine_destroy(), ast_speech_engine::start, and ast_speech_engine::write.

Referenced by speech_engine_alloc_and_register().

◆ speech_engine_alloc_and_register()

static void speech_engine_alloc_and_register ( const char *  name,
const struct ast_format_cap formats 
)
static

Definition at line 554 of file res_speech_aeap.c.

555{
556 struct ast_speech_engine *engine;
557
558 engine = speech_engine_alloc(name);
559 if (!engine) {
560 return;
561 }
562
565 ast_log(LOG_WARNING, "AEAP speech: Unable to add engine '%s' formats\n", name);
566 ao2_ref(engine, -1);
567 return;
568 }
569
570 if (ast_speech_register(engine)) {
571 ast_log(LOG_WARNING, "AEAP speech: Unable to register engine '%s'\n", name);
572 ao2_ref(engine, -1);
573 }
574}
@ AST_MEDIA_TYPE_AUDIO
Definition: codec.h:32
int ast_format_cap_append_from_cap(struct ast_format_cap *dst, const struct ast_format_cap *src, enum ast_media_type type)
Append the formats of provided type in src to dst.
Definition: format_cap.c:269
#define LOG_WARNING
static struct ast_speech_engine * speech_engine_alloc(const char *name)
int ast_speech_register(struct ast_speech_engine *engine)
Register a speech recognition engine.
Definition: res_speech.c:316

References ao2_ref, ast_format_cap_append_from_cap(), ast_log, AST_MEDIA_TYPE_AUDIO, ast_speech_register(), ast_speech_engine::formats, LOG_WARNING, name, and speech_engine_alloc().

Referenced by load_engine().

◆ speech_engine_destroy()

static void speech_engine_destroy ( void *  obj)
static

Definition at line 514 of file res_speech_aeap.c.

515{
516 struct ast_speech_engine *engine = obj;
517
518 ao2_cleanup(engine->formats);
519 ast_free(engine->name);
520}
#define ast_free(a)
Definition: astmm.h:180

References ao2_cleanup, ast_free, ast_speech_engine::formats, and ast_speech_engine::name.

Referenced by speech_engine_alloc().

◆ speech_observer_loaded()

static void speech_observer_loaded ( const char *  object_type)
static

Definition at line 681 of file res_speech_aeap.c.

682{
683 struct ao2_container *container;
684
685 if (strcmp(object_type, AEAP_CONFIG_CLIENT)) {
686 return;
687 }
688
690 if (!container) {
691 return;
692 }
693
694 /*
695 * An AEAP module reload has occurred. First
696 * remove all engines that no longer exist.
697 */
699
700 /* Now add or update engines */
702 ao2_ref(container, -1);
703}
void __ao2_cleanup(void *obj)
Definition: astobj2.c:677
static int should_unregister(const struct ast_speech_engine *engine, void *data)
void ast_speech_unregister_engines(int(*should_unregister)(const struct ast_speech_engine *engine, void *data), void *data, void(*on_unregistered)(void *obj))
Unregister all speech recognition engines told to by callback.
Definition: res_speech.c:380

References __ao2_cleanup(), AEAP_CONFIG_CLIENT, ao2_callback, ao2_ref, ast_aeap_client_configs_get(), ast_speech_unregister_engines(), container, load_engine(), NULL, should_unregister(), and SPEECH_PROTOCOL.

◆ unload_engine()

static int unload_engine ( void *  obj,
void *  arg,
int  flags 
)
static

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 710 of file res_speech_aeap.c.

711{
712 struct ao2_container *container;
713
714#ifdef TEST_FRAMEWORK
715 ao2_cleanup(ast_speech_unregister2("_aeap_test_speech_"));
716#endif
717
719
721 if (container) {
723 ao2_ref(container, -1);
724 }
725
726 return 0;
727}
static int unload_engine(void *obj, void *arg, int flags)
void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
Remove an observer from a specific object type.
Definition: sorcery.c:2423

References AEAP_CONFIG_CLIENT, ao2_callback, ao2_cleanup, ao2_ref, ast_aeap_client_configs_get(), ast_aeap_sorcery(), ast_sorcery_observer_remove(), ast_speech_unregister2(), container, NULL, speech_observer, SPEECH_PROTOCOL, and unload_engine().

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Asterisk External Application Speech Engine" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CHANNEL_DEPEND, .requires = "res_speech,res_aeap", }
static

Definition at line 763 of file res_speech_aeap.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 763 of file res_speech_aeap.c.

◆ request_handlers

const struct ast_aeap_message_handler request_handlers[]
static
Initial value:
= {
{ "set", handle_request_set },
}
static int handle_request_set(struct ast_aeap *aeap, struct ast_aeap_message *message, void *data)

Definition at line 349 of file res_speech_aeap.c.

◆ response_handlers

const struct ast_aeap_message_handler response_handlers[]
static
Initial value:
= {
{ "setup", handle_response_setup },
{ "get", handle_response_get },
{ "set", handle_response_set },
}
static int handle_response_get(struct ast_aeap *aeap, struct ast_aeap_message *message, void *data)
static int handle_response_setup(struct ast_aeap *aeap, struct ast_aeap_message *message, void *data)
static int handle_response_set(struct ast_aeap *aeap, struct ast_aeap_message *message, void *data)

Definition at line 307 of file res_speech_aeap.c.

◆ speech_aeap_params

struct ast_aeap_params speech_aeap_params
static

Definition at line 370 of file res_speech_aeap.c.

Referenced by load_module(), and speech_aeap_engine_create().

◆ speech_observer

const struct ast_sorcery_observer speech_observer
static
Initial value:
= {
}
static void speech_observer_loaded(const char *object_type)

Observer for AEAP reloads.

Definition at line 706 of file res_speech_aeap.c.

Referenced by load_module(), and unload_module().