Asterisk - The Open Source Telephony Project GIT-master-b023714
Loading...
Searching...
No Matches
Macros | Functions | Variables
test_aeap_speech.c File Reference
#include "asterisk.h"
#include "asterisk/test.h"
#include "asterisk/module.h"
#include "asterisk/file.h"
#include "asterisk/format_cap.h"
#include "asterisk/http.h"
#include "asterisk/http_websocket.h"
#include "asterisk/json.h"
#include "asterisk/speech.h"
#include "asterisk/res_aeap.h"
#include "asterisk/res_aeap_message.h"
Include dependency graph for test_aeap_speech.c:

Go to the source code of this file.

Macros

#define ADDR   "127.0.0.1:8088"
 
#define TEST_SPEECH_RESULTS_BEST   1
 
#define TEST_SPEECH_RESULTS_GRAMMAR   "bar"
 
#define TEST_SPEECH_RESULTS_SCORE   7
 
#define TEST_SPEECH_RESULTS_TEXT   "foo"
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (res_speech_aeap_test)
 
static int load_module (void)
 
static void speech_test_server_cb (struct ast_websocket *ws, struct ast_variable *parameters, struct ast_variable *headers)
 
static int speech_test_server_get (struct ast_json *req, struct ast_json *resp)
 
static int speech_test_server_handle_request (struct ast_websocket *ws, const void *buf, uint64_t size)
 
static int speech_test_server_set (struct ast_json *req, struct ast_json *resp)
 
static int speech_test_server_setup (struct ast_json *req, struct ast_json *resp)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Asterisk External Application Protocol Speech test(s)" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_speech_aeap", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_http_serverhttp_server
 

Macro Definition Documentation

◆ ADDR

#define ADDR   "127.0.0.1:8088"

Definition at line 39 of file test_aeap_speech.c.

◆ TEST_SPEECH_RESULTS_BEST

#define TEST_SPEECH_RESULTS_BEST   1

Definition at line 60 of file test_aeap_speech.c.

◆ TEST_SPEECH_RESULTS_GRAMMAR

#define TEST_SPEECH_RESULTS_GRAMMAR   "bar"

Definition at line 59 of file test_aeap_speech.c.

◆ TEST_SPEECH_RESULTS_SCORE

#define TEST_SPEECH_RESULTS_SCORE   7

Definition at line 58 of file test_aeap_speech.c.

◆ TEST_SPEECH_RESULTS_TEXT

#define TEST_SPEECH_RESULTS_TEXT   "foo"

Definition at line 57 of file test_aeap_speech.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 287 of file test_aeap_speech.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 287 of file test_aeap_speech.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 287 of file test_aeap_speech.c.

◆ AST_TEST_DEFINE()

AST_TEST_DEFINE ( res_speech_aeap_test  )

Definition at line 209 of file test_aeap_speech.c.

210{
211 RAII_VAR(struct ast_format_cap *, cap, NULL, ao2_cleanup);
213 struct ast_speech *speech = NULL;
215 char buf[8] = "";
216
217 switch (cmd) {
218 case TEST_INIT:
219 info->name = __func__;
220 info->explicit_only = 0;
221 info->category = "/res/aeap/speech/";
222 info->summary = "test the speech AEAP interface";
223 info->description = info->summary;
224 return AST_TEST_NOT_RUN;
225 case TEST_EXECUTE:
226 break;
227 }
228
229 ast_test_validate(test, !ast_websocket_add_protocol("_aeap_test_speech_", speech_test_server_cb));
230
231 ast_test_validate(test, (cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)));
232 ast_test_validate(test, !ast_format_cap_update_by_allow_disallow(cap, "ulaw", 1));
233
234 ast_test_validate_cleanup(test, (speech = ast_speech_new("_aeap_test_speech_", cap)), res, cleanup);
235 ast_speech_start(speech);
236 ast_test_validate_cleanup(test, !ast_speech_dtmf(speech, "1"), res, cleanup);
237 ast_test_validate_cleanup(test, !ast_speech_change(speech, "foo", "bar"), res, cleanup);
238 ast_test_validate_cleanup(test, !ast_speech_change_results_type(
240
241 ast_test_validate_cleanup(test, !ast_speech_get_setting(
242 speech, "foo", buf, sizeof(buf)), res, cleanup);
243 ast_test_validate_cleanup(test, !strcmp(buf, "bar"), res, cleanup);
244
245 ast_test_validate_cleanup(test, (results = ast_speech_results_get(speech)), res, cleanup);
246 ast_test_validate_cleanup(test, !strcmp(results->text, TEST_SPEECH_RESULTS_TEXT), res, cleanup);
247 ast_test_validate_cleanup(test, results->score == TEST_SPEECH_RESULTS_SCORE, res, cleanup);
248 ast_test_validate_cleanup(test, !strcmp(results->grammar, TEST_SPEECH_RESULTS_GRAMMAR), res, cleanup);
249 ast_test_validate_cleanup(test, results->nbest_num == TEST_SPEECH_RESULTS_BEST, res, cleanup);
250
251cleanup:
252 if (speech) {
253 ast_speech_destroy(speech);
254 }
256
257 return res;
258}
#define ao2_cleanup(obj)
Definition astobj2.h:1934
char buf[BUFSIZE]
Definition eagi_proxy.c:66
int ast_format_cap_update_by_allow_disallow(struct ast_format_cap *cap, const char *list, int allowing)
Parse an "allow" or "deny" list and modify a format capabilities structure accordingly.
Definition format_cap.c:320
@ AST_FORMAT_CAP_FLAG_DEFAULT
Definition format_cap.h:38
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition format_cap.h:49
int AST_OPTIONAL_API_NAME() ast_websocket_add_protocol(const char *name, ast_websocket_callback callback)
Add a sub-protocol handler to the default /ws server.
int AST_OPTIONAL_API_NAME() ast_websocket_remove_protocol(const char *name, ast_websocket_callback callback)
Remove a sub-protocol handler from the default /ws server.
static void cleanup(void)
Clean up any old apps that we don't need any more.
Definition res_stasis.c:327
#define NULL
Definition resample.c:96
int ast_speech_dtmf(struct ast_speech *speech, const char *dtmf)
Signal to the engine that DTMF was received.
Definition res_speech.c:154
int ast_speech_results_free(struct ast_speech_result *result)
Free a set of results.
Definition res_speech.c:96
void ast_speech_start(struct ast_speech *speech)
Indicate to the speech engine that audio is now going to start being written.
Definition res_speech.c:122
int ast_speech_change(struct ast_speech *speech, const char *name, const char *value)
Change an engine specific attribute.
Definition res_speech.c:169
struct ast_speech * ast_speech_new(const char *engine_name, const struct ast_format_cap *formats)
Create a new speech structure.
Definition res_speech.c:181
int ast_speech_destroy(struct ast_speech *speech)
Destroy a speech structure.
Definition res_speech.c:251
int ast_speech_change_results_type(struct ast_speech *speech, enum ast_speech_results_type results_type)
Change the type of results we want.
Definition res_speech.c:308
int ast_speech_get_setting(struct ast_speech *speech, const char *name, char *buf, size_t len)
Get an engine specific attribute.
Definition res_speech.c:175
@ AST_SPEECH_RESULTS_TYPE_NBEST
Definition speech.h:47
struct ast_speech_result * ast_speech_results_get(struct ast_speech *speech)
Get speech recognition results.
Definition res_speech.c:90
Format capabilities structure, holds formats + preference order + etc.
Definition format_cap.c:54
struct ast_speech_result * results
Definition speech.h:68
@ TEST_INIT
Definition test.h:200
@ TEST_EXECUTE
Definition test.h:201
ast_test_result_state
Definition test.h:193
@ AST_TEST_PASS
Definition test.h:195
@ AST_TEST_NOT_RUN
Definition test.h:194
#define TEST_SPEECH_RESULTS_SCORE
static void speech_test_server_cb(struct ast_websocket *ws, struct ast_variable *parameters, struct ast_variable *headers)
#define TEST_SPEECH_RESULTS_TEXT
#define TEST_SPEECH_RESULTS_GRAMMAR
#define TEST_SPEECH_RESULTS_BEST
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition utils.h:978

References ao2_cleanup, ast_format_cap_alloc, AST_FORMAT_CAP_FLAG_DEFAULT, ast_format_cap_update_by_allow_disallow(), ast_speech_change(), ast_speech_change_results_type(), ast_speech_destroy(), ast_speech_dtmf(), ast_speech_get_setting(), ast_speech_new(), ast_speech_results_free(), ast_speech_results_get(), AST_SPEECH_RESULTS_TYPE_NBEST, ast_speech_start(), AST_TEST_NOT_RUN, AST_TEST_PASS, ast_websocket_add_protocol(), ast_websocket_remove_protocol(), buf, cleanup(), ast_speech_result::grammar, ast_speech_result::nbest_num, NULL, RAII_VAR, ast_speech::results, ast_speech_result::score, speech_test_server_cb(), TEST_EXECUTE, TEST_INIT, TEST_SPEECH_RESULTS_BEST, TEST_SPEECH_RESULTS_GRAMMAR, TEST_SPEECH_RESULTS_SCORE, TEST_SPEECH_RESULTS_TEXT, and ast_speech_result::text.

◆ load_module()

static int load_module ( void  )
static

Definition at line 262 of file test_aeap_speech.c.

263{
264 if (!(http_server = ast_http_test_server_get("aeap transport http server", NULL))) {
266 }
267
268 AST_TEST_REGISTER(res_speech_aeap_test);
269
271}
@ 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
#define AST_TEST_REGISTER(cb)
Definition test.h:127
static struct ast_http_server * http_server

References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_TEST_REGISTER, http_server, and NULL.

◆ speech_test_server_cb()

static void speech_test_server_cb ( struct ast_websocket ws,
struct ast_variable parameters,
struct ast_variable headers 
)
static

Definition at line 167 of file test_aeap_speech.c.

169{
170 int res;
171
172 if (ast_fd_set_flags(ast_websocket_fd(ws), O_NONBLOCK)) {
174 return;
175 }
176
177 while ((res = ast_websocket_wait_for_input(ws, -1)) > 0) {
178 char *payload;
179 uint64_t payload_len;
180 enum ast_websocket_opcode opcode;
181 int fragmented;
182
183 if (ast_websocket_read(ws, &payload, &payload_len, &opcode, &fragmented)) {
184 ast_log(LOG_ERROR, "speech test: Read failure in server loop\n");
185 break;
186 }
187
188 switch (opcode) {
191 return;
193 ast_websocket_write(ws, opcode, payload, payload_len);
194 break;
196 ast_debug(3, "payload=%.*s\n", (int)payload_len, payload);
197 if (speech_test_server_handle_request(ws, payload, payload_len)) {
199 return;
200 }
201 break;
202 default:
203 break;
204 }
205 }
207}
#define ast_log
Definition astobj2.c:42
int AST_OPTIONAL_API_NAME() ast_websocket_write(struct ast_websocket *session, enum ast_websocket_opcode opcode, char *payload, uint64_t payload_size)
Construct and transmit a WebSocket frame.
int AST_OPTIONAL_API_NAME() ast_websocket_wait_for_input(struct ast_websocket *session, int timeout)
Wait for the WebSocket session to be ready to be read.
int AST_OPTIONAL_API_NAME() ast_websocket_fd(struct ast_websocket *session)
Get the file descriptor for a WebSocket session.
ast_websocket_opcode
WebSocket operation codes.
@ AST_WEBSOCKET_OPCODE_BINARY
@ AST_WEBSOCKET_OPCODE_CLOSE
@ AST_WEBSOCKET_OPCODE_TEXT
int AST_OPTIONAL_API_NAME() ast_websocket_read(struct ast_websocket *session, char **payload, uint64_t *payload_len, enum ast_websocket_opcode *opcode, int *fragmented)
Read a WebSocket frame and handle it.
void AST_OPTIONAL_API_NAME() ast_websocket_unref(struct ast_websocket *session)
Decrease the reference count for a WebSocket session.
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
static int speech_test_server_handle_request(struct ast_websocket *ws, const void *buf, uint64_t size)
#define ast_fd_set_flags(fd, flags)
Set flags on the given file descriptor.
Definition utils.h:1076

References ast_debug, ast_fd_set_flags, ast_log, ast_websocket_fd(), AST_WEBSOCKET_OPCODE_BINARY, AST_WEBSOCKET_OPCODE_CLOSE, AST_WEBSOCKET_OPCODE_TEXT, ast_websocket_read(), ast_websocket_unref(), ast_websocket_wait_for_input(), ast_websocket_write(), LOG_ERROR, and speech_test_server_handle_request().

Referenced by AST_TEST_DEFINE().

◆ speech_test_server_get()

static int speech_test_server_get ( struct ast_json req,
struct ast_json resp 
)
static

Definition at line 62 of file test_aeap_speech.c.

63{
64 const char *param;
65 struct ast_json *json = NULL;
66
68 if (!param) {
69 return -1;
70 }
71
72 if (!strcmp(param, "results")) {
73 json = ast_json_pack("{s:[{s:s,s:i,s:s,s:i}]}",
74 param,
79 } else {
80 /* Assume setting */
81 json = ast_json_pack("{s:s}", param, "bar");
82 }
83
84 if (!json || ast_json_object_set(resp, "params", json)) {
85 return -1;
86 }
87
88 return 0;
89}
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
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition json.c:612
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
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition json.c:283
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
Abstract JSON element (object, array, string, int, ...).

References ast_json_array_get(), ast_json_object_get(), ast_json_object_set(), ast_json_pack(), ast_json_string_get(), NULL, TEST_SPEECH_RESULTS_BEST, TEST_SPEECH_RESULTS_GRAMMAR, TEST_SPEECH_RESULTS_SCORE, and TEST_SPEECH_RESULTS_TEXT.

Referenced by speech_test_server_handle_request().

◆ speech_test_server_handle_request()

static int speech_test_server_handle_request ( struct ast_websocket ws,
const void *  buf,
uint64_t  size 
)
static

Definition at line 100 of file test_aeap_speech.c.

101{
102 struct ast_json *req;
103 struct ast_json *resp;
104 const char *name;
105 char *resp_buf;
106 int res = 0;
107
108 req = ast_json_load_buf(buf, size, NULL);
109 if (!req) {
110 ast_log(LOG_ERROR, "speech test handle request: unable to load json\n");
111 return -1;
112 }
113
114 name = ast_json_object_string_get(req, "request");
115 if (!name) {
116 ast_log(LOG_ERROR, "speech test handle request: no name\n");
117 ast_json_unref(req);
118 return -1;
119 }
120
121 resp = ast_json_pack("{s:s, s:s}", "response", name,
122 "id", ast_json_object_string_get(req, "id"));
123 if (!resp) {
124 ast_log(LOG_ERROR, "speech test handle request: unable to create response '%s'\n", name);
125 ast_json_unref(req);
126 return -1;
127 }
128
129 if (!strcmp(name, "setup")) {
130 res = speech_test_server_setup(req, resp);
131 } else if (!strcmp(name, "get")) {
132 res = speech_test_server_get(req, resp);
133 } else if (!strcmp(name, "set")) {
134 res = speech_test_server_set(req, resp);
135 } else {
136 ast_log(LOG_ERROR, "speech test handle request: unsupported request '%s'\n", name);
137 return -1;
138 }
139
140 if (res) {
141 ast_log(LOG_ERROR, "speech test handle request: unable to build response '%s'\n", name);
142 ast_json_unref(resp);
143 ast_json_unref(req);
144 return -1;
145 }
146
147 resp_buf = ast_json_dump_string(resp);
148 ast_json_unref(resp);
149
150 if (!resp_buf) {
151 ast_log(LOG_ERROR, "speech test handle request: unable to dump response '%s'\n", name);
152 ast_json_unref(req);
153 return -1;
154 }
155
156 res = ast_websocket_write_string(ws, resp_buf);
157 if (res) {
158 ast_log(LOG_ERROR, "speech test handle request: unable to write response '%s'\n", name);
159 }
160
161 ast_json_unref(req);
162 ast_free(resp_buf);
163
164 return res;
165}
#define ast_free(a)
Definition astmm.h:180
static const char name[]
Definition format_mp3.c:68
int AST_OPTIONAL_API_NAME() ast_websocket_write_string(struct ast_websocket *ws, const char *buf)
Construct and transmit a WebSocket frame containing string data.
#define ast_json_object_string_get(object, key)
Get a string field from a JSON object.
Definition json.h:600
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition json.c:73
#define ast_json_dump_string(root)
Encode a JSON value to a compact string.
Definition json.h:810
struct ast_json * ast_json_load_buf(const char *buffer, size_t buflen, struct ast_json_error *error)
Parse buffer with known length into a JSON object or array.
Definition json.c:585
static int speech_test_server_get(struct ast_json *req, struct ast_json *resp)
static int speech_test_server_setup(struct ast_json *req, struct ast_json *resp)
static int speech_test_server_set(struct ast_json *req, struct ast_json *resp)

References ast_free, ast_json_dump_string, ast_json_load_buf(), ast_json_object_string_get, ast_json_pack(), ast_json_unref(), ast_log, ast_websocket_write_string(), buf, LOG_ERROR, name, NULL, speech_test_server_get(), speech_test_server_set(), and speech_test_server_setup().

Referenced by speech_test_server_cb().

◆ speech_test_server_set()

static int speech_test_server_set ( struct ast_json req,
struct ast_json resp 
)
static

Definition at line 91 of file test_aeap_speech.c.

92{
93 if (ast_json_object_set(resp, "params", ast_json_ref(ast_json_object_get(req, "params")))) {
94 return -1;
95 }
96
97 return 0;
98}
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition json.c:67

References ast_json_object_get(), ast_json_object_set(), and ast_json_ref().

Referenced by speech_test_server_handle_request().

◆ speech_test_server_setup()

static int speech_test_server_setup ( struct ast_json req,
struct ast_json resp 
)
static

Definition at line 41 of file test_aeap_speech.c.

42{
43 struct ast_json *params;
44
45 if (ast_json_object_set(resp, "codecs", ast_json_ref(ast_json_object_get(req, "codecs")))) {
46 return -1;
47 }
48
49 params = ast_json_object_get(req, "params"); /* Optional */
50 if (params && ast_json_object_set(resp, "params", ast_json_ref(params))) {
51 return -1;
52 }
53
54 return 0;
55}

References ast_json_object_get(), ast_json_object_set(), and ast_json_ref().

Referenced by speech_test_server_handle_request().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 273 of file test_aeap_speech.c.

274{
275 AST_TEST_UNREGISTER(res_speech_aeap_test);
276
277 ast_http_test_server_discard(http_server);
278
279 return 0;
280}
#define AST_TEST_UNREGISTER(cb)
Definition test.h:128

References AST_TEST_UNREGISTER, and http_server.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Asterisk External Application Protocol Speech test(s)" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_speech_aeap", }
static

Definition at line 287 of file test_aeap_speech.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 287 of file test_aeap_speech.c.

◆ http_server

struct ast_http_server* http_server
static

Definition at line 260 of file test_aeap_speech.c.

Referenced by load_module(), and unload_module().